blob: 6e2130828bbb0b39105d8630fe884381f884bfb6 [file] [log] [blame]
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001//===-- SystemZISelDAGToDAG.cpp - A dag to dag inst selector for SystemZ --===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the SystemZ target.
11//
12//===----------------------------------------------------------------------===//
13
14#include "SystemZTargetMachine.h"
Richard Sandiford97846492013-07-09 09:46:39 +000015#include "llvm/Analysis/AliasAnalysis.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000016#include "llvm/CodeGen/SelectionDAGISel.h"
17#include "llvm/Support/Debug.h"
Craig Topperd0af7e82017-04-28 05:31:46 +000018#include "llvm/Support/KnownBits.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000019#include "llvm/Support/raw_ostream.h"
20
21using namespace llvm;
22
Chandler Carruthe96dd892014-04-21 22:55:11 +000023#define DEBUG_TYPE "systemz-isel"
24
Ulrich Weigand5f613df2013-05-06 16:15:19 +000025namespace {
26// Used to build addressing modes.
27struct SystemZAddressingMode {
28 // The shape of the address.
29 enum AddrForm {
30 // base+displacement
31 FormBD,
32
33 // base+displacement+index for load and store operands
34 FormBDXNormal,
35
36 // base+displacement+index for load address operands
37 FormBDXLA,
38
39 // base+displacement+index+ADJDYNALLOC
40 FormBDXDynAlloc
41 };
42 AddrForm Form;
43
44 // The type of displacement. The enum names here correspond directly
45 // to the definitions in SystemZOperand.td. We could split them into
46 // flags -- single/pair, 128-bit, etc. -- but it hardly seems worth it.
47 enum DispRange {
48 Disp12Only,
49 Disp12Pair,
50 Disp20Only,
51 Disp20Only128,
52 Disp20Pair
53 };
54 DispRange DR;
55
56 // The parts of the address. The address is equivalent to:
57 //
58 // Base + Disp + Index + (IncludesDynAlloc ? ADJDYNALLOC : 0)
59 SDValue Base;
60 int64_t Disp;
61 SDValue Index;
62 bool IncludesDynAlloc;
63
64 SystemZAddressingMode(AddrForm form, DispRange dr)
65 : Form(form), DR(dr), Base(), Disp(0), Index(),
66 IncludesDynAlloc(false) {}
67
68 // True if the address can have an index register.
69 bool hasIndexField() { return Form != FormBD; }
70
71 // True if the address can (and must) include ADJDYNALLOC.
72 bool isDynAlloc() { return Form == FormBDXDynAlloc; }
73
74 void dump() {
75 errs() << "SystemZAddressingMode " << this << '\n';
76
77 errs() << " Base ";
Craig Topper062a2ba2014-04-25 05:30:21 +000078 if (Base.getNode())
Ulrich Weigand5f613df2013-05-06 16:15:19 +000079 Base.getNode()->dump();
80 else
81 errs() << "null\n";
82
83 if (hasIndexField()) {
84 errs() << " Index ";
Craig Topper062a2ba2014-04-25 05:30:21 +000085 if (Index.getNode())
Ulrich Weigand5f613df2013-05-06 16:15:19 +000086 Index.getNode()->dump();
87 else
88 errs() << "null\n";
89 }
90
91 errs() << " Disp " << Disp;
92 if (IncludesDynAlloc)
93 errs() << " + ADJDYNALLOC";
94 errs() << '\n';
95 }
96};
97
Richard Sandiford82ec87d2013-07-16 11:02:24 +000098// Return a mask with Count low bits set.
99static uint64_t allOnes(unsigned int Count) {
Ulrich Weigand77884bc2015-06-25 11:52:36 +0000100 assert(Count <= 64);
Justin Bognerc97c48a2015-06-24 05:59:19 +0000101 if (Count > 63)
102 return UINT64_MAX;
103 return (uint64_t(1) << Count) - 1;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000104}
105
Richard Sandiford51093212013-07-18 10:40:35 +0000106// Represents operands 2 to 5 of the ROTATE AND ... SELECTED BITS operation
107// given by Opcode. The operands are: Input (R2), Start (I3), End (I4) and
108// Rotate (I5). The combined operand value is effectively:
109//
110// (or (rotl Input, Rotate), ~Mask)
111//
112// for RNSBG and:
113//
114// (and (rotl Input, Rotate), Mask)
115//
Richard Sandiford3e382972013-10-16 13:35:13 +0000116// otherwise. The output value has BitSize bits, although Input may be
Zhan Jun Liau0df35052016-06-22 16:16:27 +0000117// narrower (in which case the upper bits are don't care), or wider (in which
118// case the result will be truncated as part of the operation).
Richard Sandiford5cbac962013-07-18 09:45:08 +0000119struct RxSBGOperands {
Richard Sandiford51093212013-07-18 10:40:35 +0000120 RxSBGOperands(unsigned Op, SDValue N)
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000121 : Opcode(Op), BitSize(N.getValueSizeInBits()),
Richard Sandiford51093212013-07-18 10:40:35 +0000122 Mask(allOnes(BitSize)), Input(N), Start(64 - BitSize), End(63),
123 Rotate(0) {}
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000124
Richard Sandiford51093212013-07-18 10:40:35 +0000125 unsigned Opcode;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000126 unsigned BitSize;
127 uint64_t Mask;
128 SDValue Input;
129 unsigned Start;
130 unsigned End;
131 unsigned Rotate;
132};
133
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000134class SystemZDAGToDAGISel : public SelectionDAGISel {
Eric Christophera6734172015-01-31 00:06:45 +0000135 const SystemZSubtarget *Subtarget;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000136
137 // Used by SystemZOperands.td to create integer constants.
Richard Sandiford54b36912013-09-27 15:14:04 +0000138 inline SDValue getImm(const SDNode *Node, uint64_t Imm) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000139 return CurDAG->getTargetConstant(Imm, SDLoc(Node), Node->getValueType(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000140 }
141
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000142 const SystemZTargetMachine &getTargetMachine() const {
143 return static_cast<const SystemZTargetMachine &>(TM);
144 }
145
146 const SystemZInstrInfo *getInstrInfo() const {
Eric Christophera6734172015-01-31 00:06:45 +0000147 return Subtarget->getInstrInfo();
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000148 }
149
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000150 // Try to fold more of the base or index of AM into AM, where IsBase
151 // selects between the base and index.
Richard Sandiford54b36912013-09-27 15:14:04 +0000152 bool expandAddress(SystemZAddressingMode &AM, bool IsBase) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000153
154 // Try to describe N in AM, returning true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000155 bool selectAddress(SDValue N, SystemZAddressingMode &AM) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000156
157 // Extract individual target operands from matched address AM.
158 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
Richard Sandiford54b36912013-09-27 15:14:04 +0000159 SDValue &Base, SDValue &Disp) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000160 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
Richard Sandiford54b36912013-09-27 15:14:04 +0000161 SDValue &Base, SDValue &Disp, SDValue &Index) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000162
163 // Try to match Addr as a FormBD address with displacement type DR.
164 // Return true on success, storing the base and displacement in
165 // Base and Disp respectively.
166 bool selectBDAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000167 SDValue &Base, SDValue &Disp) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000168
Richard Sandiforda481f582013-08-23 11:18:53 +0000169 // Try to match Addr as a FormBDX address with displacement type DR.
170 // Return true on success and if the result had no index. Store the
171 // base and displacement in Base and Disp respectively.
172 bool selectMVIAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000173 SDValue &Base, SDValue &Disp) const;
Richard Sandiforda481f582013-08-23 11:18:53 +0000174
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000175 // Try to match Addr as a FormBDX* address of form Form with
176 // displacement type DR. Return true on success, storing the base,
177 // displacement and index in Base, Disp and Index respectively.
178 bool selectBDXAddr(SystemZAddressingMode::AddrForm Form,
179 SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000180 SDValue &Base, SDValue &Disp, SDValue &Index) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000181
182 // PC-relative address matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000183 bool selectPCRelAddress(SDValue Addr, SDValue &Target) const {
184 if (SystemZISD::isPCREL(Addr.getOpcode())) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000185 Target = Addr.getOperand(0);
186 return true;
187 }
188 return false;
189 }
190
191 // BD matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000192 bool selectBDAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000193 return selectBDAddr(SystemZAddressingMode::Disp12Only, Addr, Base, Disp);
194 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000195 bool selectBDAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000196 return selectBDAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
197 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000198 bool selectBDAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000199 return selectBDAddr(SystemZAddressingMode::Disp20Only, Addr, Base, Disp);
200 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000201 bool selectBDAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000202 return selectBDAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
203 }
204
Richard Sandiforda481f582013-08-23 11:18:53 +0000205 // MVI matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000206 bool selectMVIAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000207 return selectMVIAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
208 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000209 bool selectMVIAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000210 return selectMVIAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
211 }
212
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000213 // BDX matching routines used by SystemZOperands.td.
214 bool selectBDXAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000215 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000216 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
217 SystemZAddressingMode::Disp12Only,
218 Addr, Base, Disp, Index);
219 }
220 bool selectBDXAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000221 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000222 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
223 SystemZAddressingMode::Disp12Pair,
224 Addr, Base, Disp, Index);
225 }
226 bool selectDynAlloc12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000227 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000228 return selectBDXAddr(SystemZAddressingMode::FormBDXDynAlloc,
229 SystemZAddressingMode::Disp12Only,
230 Addr, Base, Disp, Index);
231 }
232 bool selectBDXAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000233 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000234 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
235 SystemZAddressingMode::Disp20Only,
236 Addr, Base, Disp, Index);
237 }
238 bool selectBDXAddr20Only128(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000239 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000240 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
241 SystemZAddressingMode::Disp20Only128,
242 Addr, Base, Disp, Index);
243 }
244 bool selectBDXAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000245 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000246 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
247 SystemZAddressingMode::Disp20Pair,
248 Addr, Base, Disp, Index);
249 }
250 bool selectLAAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000251 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000252 return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
253 SystemZAddressingMode::Disp12Pair,
254 Addr, Base, Disp, Index);
255 }
256 bool selectLAAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000257 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000258 return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
259 SystemZAddressingMode::Disp20Pair,
260 Addr, Base, Disp, Index);
261 }
262
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000263 // Try to match Addr as an address with a base, 12-bit displacement
264 // and index, where the index is element Elem of a vector.
265 // Return true on success, storing the base, displacement and vector
266 // in Base, Disp and Index respectively.
267 bool selectBDVAddr12Only(SDValue Addr, SDValue Elem, SDValue &Base,
268 SDValue &Disp, SDValue &Index) const;
269
Richard Sandiford885140c2013-07-16 11:55:57 +0000270 // Check whether (or Op (and X InsertMask)) is effectively an insertion
271 // of X into bits InsertMask of some Y != Op. Return true if so and
272 // set Op to that Y.
Richard Sandiford54b36912013-09-27 15:14:04 +0000273 bool detectOrAndInsertion(SDValue &Op, uint64_t InsertMask) const;
Richard Sandiford885140c2013-07-16 11:55:57 +0000274
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000275 // Try to update RxSBG so that only the bits of RxSBG.Input in Mask are used.
276 // Return true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000277 bool refineRxSBGMask(RxSBGOperands &RxSBG, uint64_t Mask) const;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000278
Richard Sandiford5cbac962013-07-18 09:45:08 +0000279 // Try to fold some of RxSBG.Input into other fields of RxSBG.
280 // Return true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000281 bool expandRxSBG(RxSBGOperands &RxSBG) const;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000282
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000283 // Return an undefined value of type VT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000284 SDValue getUNDEF(const SDLoc &DL, EVT VT) const;
Richard Sandiford84f54a32013-07-11 08:59:12 +0000285
286 // Convert N to VT, if it isn't already.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000287 SDValue convertTo(const SDLoc &DL, EVT VT, SDValue N) const;
Richard Sandiford84f54a32013-07-11 08:59:12 +0000288
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000289 // Try to implement AND or shift node N using RISBG with the zero flag set.
290 // Return the selected node on success, otherwise return null.
Justin Bognerbbcd2232016-05-10 21:11:26 +0000291 bool tryRISBGZero(SDNode *N);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000292
Richard Sandiford7878b852013-07-18 10:06:15 +0000293 // Try to use RISBG or Opcode to implement OR or XOR node N.
294 // Return the selected node on success, otherwise return null.
Justin Bogner9b34e8a2016-05-13 22:42:08 +0000295 bool tryRxSBG(SDNode *N, unsigned Opcode);
Richard Sandiford885140c2013-07-16 11:55:57 +0000296
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000297 // If Op0 is null, then Node is a constant that can be loaded using:
298 //
299 // (Opcode UpperVal LowerVal)
300 //
301 // If Op0 is nonnull, then Node can be implemented using:
302 //
303 // (Opcode (Opcode Op0 UpperVal) LowerVal)
Justin Bognerffb273d2016-05-09 23:54:23 +0000304 void splitLargeImmediate(unsigned Opcode, SDNode *Node, SDValue Op0,
305 uint64_t UpperVal, uint64_t LowerVal);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000306
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000307 // Try to use gather instruction Opcode to implement vector insertion N.
Justin Bogner9b34e8a2016-05-13 22:42:08 +0000308 bool tryGather(SDNode *N, unsigned Opcode);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000309
310 // Try to use scatter instruction Opcode to implement store Store.
Justin Bogner9b34e8a2016-05-13 22:42:08 +0000311 bool tryScatter(StoreSDNode *Store, unsigned Opcode);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000312
Richard Sandiford067817e2013-09-27 15:29:20 +0000313 // Return true if Load and Store are loads and stores of the same size
314 // and are guaranteed not to overlap. Such operations can be implemented
315 // using block (SS-format) instructions.
316 //
317 // Partial overlap would lead to incorrect code, since the block operations
318 // are logically bytewise, even though they have a fast path for the
319 // non-overlapping case. We also need to avoid full overlap (i.e. two
320 // addresses that might be equal at run time) because although that case
321 // would be handled correctly, it might be implemented by millicode.
322 bool canUseBlockOperation(StoreSDNode *Store, LoadSDNode *Load) const;
323
Richard Sandiford178273a2013-09-05 10:36:45 +0000324 // N is a (store (load Y), X) pattern. Return true if it can use an MVC
325 // from Y to X.
Richard Sandiford97846492013-07-09 09:46:39 +0000326 bool storeLoadCanUseMVC(SDNode *N) const;
327
Richard Sandiford178273a2013-09-05 10:36:45 +0000328 // N is a (store (op (load A[0]), (load A[1])), X) pattern. Return true
329 // if A[1 - I] == X and if N can use a block operation like NC from A[I]
330 // to X.
331 bool storeLoadCanUseBlockBinary(SDNode *N, unsigned I) const;
332
Ulrich Weigand849a59f2018-01-19 20:52:04 +0000333 // Try to expand a boolean SELECT_CCMASK using an IPM sequence.
334 SDValue expandSelectBoolean(SDNode *Node);
335
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000336public:
337 SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel)
Eric Christophera6734172015-01-31 00:06:45 +0000338 : SelectionDAGISel(TM, OptLevel) {}
339
340 bool runOnMachineFunction(MachineFunction &MF) override {
341 Subtarget = &MF.getSubtarget<SystemZSubtarget>();
342 return SelectionDAGISel::runOnMachineFunction(MF);
343 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000344
345 // Override MachineFunctionPass.
Mehdi Amini117296c2016-10-01 02:56:57 +0000346 StringRef getPassName() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000347 return "SystemZ DAG->DAG Pattern Instruction Selection";
348 }
349
350 // Override SelectionDAGISel.
Justin Bogner9b34e8a2016-05-13 22:42:08 +0000351 void Select(SDNode *Node) override;
Daniel Sanders60f1db02015-03-13 12:45:09 +0000352 bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000353 std::vector<SDValue> &OutOps) override;
Ulrich Weigand849a59f2018-01-19 20:52:04 +0000354 void PreprocessISelDAG() override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000355
356 // Include the pieces autogenerated from the target description.
357 #include "SystemZGenDAGISel.inc"
358};
359} // end anonymous namespace
360
361FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
362 CodeGenOpt::Level OptLevel) {
363 return new SystemZDAGToDAGISel(TM, OptLevel);
364}
365
366// Return true if Val should be selected as a displacement for an address
367// with range DR. Here we're interested in the range of both the instruction
368// described by DR and of any pairing instruction.
369static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
370 switch (DR) {
371 case SystemZAddressingMode::Disp12Only:
372 return isUInt<12>(Val);
373
374 case SystemZAddressingMode::Disp12Pair:
375 case SystemZAddressingMode::Disp20Only:
376 case SystemZAddressingMode::Disp20Pair:
377 return isInt<20>(Val);
378
379 case SystemZAddressingMode::Disp20Only128:
380 return isInt<20>(Val) && isInt<20>(Val + 8);
381 }
382 llvm_unreachable("Unhandled displacement range");
383}
384
385// Change the base or index in AM to Value, where IsBase selects
386// between the base and index.
387static void changeComponent(SystemZAddressingMode &AM, bool IsBase,
388 SDValue Value) {
389 if (IsBase)
390 AM.Base = Value;
391 else
392 AM.Index = Value;
393}
394
395// The base or index of AM is equivalent to Value + ADJDYNALLOC,
396// where IsBase selects between the base and index. Try to fold the
397// ADJDYNALLOC into AM.
398static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase,
399 SDValue Value) {
400 if (AM.isDynAlloc() && !AM.IncludesDynAlloc) {
401 changeComponent(AM, IsBase, Value);
402 AM.IncludesDynAlloc = true;
403 return true;
404 }
405 return false;
406}
407
408// The base of AM is equivalent to Base + Index. Try to use Index as
409// the index register.
410static bool expandIndex(SystemZAddressingMode &AM, SDValue Base,
411 SDValue Index) {
412 if (AM.hasIndexField() && !AM.Index.getNode()) {
413 AM.Base = Base;
414 AM.Index = Index;
415 return true;
416 }
417 return false;
418}
419
420// The base or index of AM is equivalent to Op0 + Op1, where IsBase selects
421// between the base and index. Try to fold Op1 into AM's displacement.
422static bool expandDisp(SystemZAddressingMode &AM, bool IsBase,
Richard Sandiford54b36912013-09-27 15:14:04 +0000423 SDValue Op0, uint64_t Op1) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000424 // First try adjusting the displacement.
Richard Sandiford54b36912013-09-27 15:14:04 +0000425 int64_t TestDisp = AM.Disp + Op1;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000426 if (selectDisp(AM.DR, TestDisp)) {
427 changeComponent(AM, IsBase, Op0);
428 AM.Disp = TestDisp;
429 return true;
430 }
431
432 // We could consider forcing the displacement into a register and
433 // using it as an index, but it would need to be carefully tuned.
434 return false;
435}
436
437bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM,
Richard Sandiford54b36912013-09-27 15:14:04 +0000438 bool IsBase) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000439 SDValue N = IsBase ? AM.Base : AM.Index;
440 unsigned Opcode = N.getOpcode();
441 if (Opcode == ISD::TRUNCATE) {
442 N = N.getOperand(0);
443 Opcode = N.getOpcode();
444 }
445 if (Opcode == ISD::ADD || CurDAG->isBaseWithConstantOffset(N)) {
446 SDValue Op0 = N.getOperand(0);
447 SDValue Op1 = N.getOperand(1);
448
449 unsigned Op0Code = Op0->getOpcode();
450 unsigned Op1Code = Op1->getOpcode();
451
452 if (Op0Code == SystemZISD::ADJDYNALLOC)
453 return expandAdjDynAlloc(AM, IsBase, Op1);
454 if (Op1Code == SystemZISD::ADJDYNALLOC)
455 return expandAdjDynAlloc(AM, IsBase, Op0);
456
457 if (Op0Code == ISD::Constant)
Richard Sandiford54b36912013-09-27 15:14:04 +0000458 return expandDisp(AM, IsBase, Op1,
459 cast<ConstantSDNode>(Op0)->getSExtValue());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000460 if (Op1Code == ISD::Constant)
Richard Sandiford54b36912013-09-27 15:14:04 +0000461 return expandDisp(AM, IsBase, Op0,
462 cast<ConstantSDNode>(Op1)->getSExtValue());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000463
464 if (IsBase && expandIndex(AM, Op0, Op1))
465 return true;
466 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000467 if (Opcode == SystemZISD::PCREL_OFFSET) {
468 SDValue Full = N.getOperand(0);
469 SDValue Base = N.getOperand(1);
470 SDValue Anchor = Base.getOperand(0);
471 uint64_t Offset = (cast<GlobalAddressSDNode>(Full)->getOffset() -
472 cast<GlobalAddressSDNode>(Anchor)->getOffset());
473 return expandDisp(AM, IsBase, Base, Offset);
474 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000475 return false;
476}
477
478// Return true if an instruction with displacement range DR should be
479// used for displacement value Val. selectDisp(DR, Val) must already hold.
480static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
481 assert(selectDisp(DR, Val) && "Invalid displacement");
482 switch (DR) {
483 case SystemZAddressingMode::Disp12Only:
484 case SystemZAddressingMode::Disp20Only:
485 case SystemZAddressingMode::Disp20Only128:
486 return true;
487
488 case SystemZAddressingMode::Disp12Pair:
489 // Use the other instruction if the displacement is too large.
490 return isUInt<12>(Val);
491
492 case SystemZAddressingMode::Disp20Pair:
493 // Use the other instruction if the displacement is small enough.
494 return !isUInt<12>(Val);
495 }
496 llvm_unreachable("Unhandled displacement range");
497}
498
499// Return true if Base + Disp + Index should be performed by LA(Y).
500static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index) {
501 // Don't use LA(Y) for constants.
502 if (!Base)
503 return false;
504
505 // Always use LA(Y) for frame addresses, since we know that the destination
506 // register is almost always (perhaps always) going to be different from
507 // the frame register.
508 if (Base->getOpcode() == ISD::FrameIndex)
509 return true;
510
511 if (Disp) {
512 // Always use LA(Y) if there is a base, displacement and index.
513 if (Index)
514 return true;
515
516 // Always use LA if the displacement is small enough. It should always
517 // be no worse than AGHI (and better if it avoids a move).
518 if (isUInt<12>(Disp))
519 return true;
520
521 // For similar reasons, always use LAY if the constant is too big for AGHI.
522 // LAY should be no worse than AGFI.
523 if (!isInt<16>(Disp))
524 return true;
525 } else {
526 // Don't use LA for plain registers.
527 if (!Index)
528 return false;
529
530 // Don't use LA for plain addition if the index operand is only used
531 // once. It should be a natural two-operand addition in that case.
532 if (Index->hasOneUse())
533 return false;
534
535 // Prefer addition if the second operation is sign-extended, in the
536 // hope of using AGF.
537 unsigned IndexOpcode = Index->getOpcode();
538 if (IndexOpcode == ISD::SIGN_EXTEND ||
539 IndexOpcode == ISD::SIGN_EXTEND_INREG)
540 return false;
541 }
542
543 // Don't use LA for two-operand addition if either operand is only
544 // used once. The addition instructions are better in that case.
545 if (Base->hasOneUse())
546 return false;
547
548 return true;
549}
550
551// Return true if Addr is suitable for AM, updating AM if so.
552bool SystemZDAGToDAGISel::selectAddress(SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000553 SystemZAddressingMode &AM) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000554 // Start out assuming that the address will need to be loaded separately,
555 // then try to extend it as much as we can.
556 AM.Base = Addr;
557
558 // First try treating the address as a constant.
559 if (Addr.getOpcode() == ISD::Constant &&
Richard Sandiford54b36912013-09-27 15:14:04 +0000560 expandDisp(AM, true, SDValue(),
561 cast<ConstantSDNode>(Addr)->getSExtValue()))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000562 ;
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +0000563 // Also see if it's a bare ADJDYNALLOC.
564 else if (Addr.getOpcode() == SystemZISD::ADJDYNALLOC &&
565 expandAdjDynAlloc(AM, true, SDValue()))
566 ;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000567 else
568 // Otherwise try expanding each component.
569 while (expandAddress(AM, true) ||
570 (AM.Index.getNode() && expandAddress(AM, false)))
571 continue;
572
573 // Reject cases where it isn't profitable to use LA(Y).
574 if (AM.Form == SystemZAddressingMode::FormBDXLA &&
575 !shouldUseLA(AM.Base.getNode(), AM.Disp, AM.Index.getNode()))
576 return false;
577
578 // Reject cases where the other instruction in a pair should be used.
579 if (!isValidDisp(AM.DR, AM.Disp))
580 return false;
581
582 // Make sure that ADJDYNALLOC is included where necessary.
583 if (AM.isDynAlloc() && !AM.IncludesDynAlloc)
584 return false;
585
586 DEBUG(AM.dump());
587 return true;
588}
589
590// Insert a node into the DAG at least before Pos. This will reposition
591// the node as needed, and will assign it a node ID that is <= Pos's ID.
592// Note that this does *not* preserve the uniqueness of node IDs!
593// The selection DAG must no longer depend on their uniqueness when this
594// function is used.
595static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N) {
596 if (N.getNode()->getNodeId() == -1 ||
597 N.getNode()->getNodeId() > Pos->getNodeId()) {
Duncan P. N. Exon Smitha2c90e42015-10-20 01:12:46 +0000598 DAG->RepositionNode(Pos->getIterator(), N.getNode());
Nirav Dave3264c1b2018-03-19 20:19:46 +0000599 // Mark Node as invalid for pruning as after this it may be a successor to a
600 // selected node but otherwise be in the same position of Pos.
601 // Conservatively mark it with the same -abs(Id) to assure node id
602 // invariant is preserved.
603 int PId = Pos->getNodeId();
604 int InvalidatedPId = -(PId + 1);
605 N->setNodeId((PId > 0) ? InvalidatedPId : PId);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000606 }
607}
608
609void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
610 EVT VT, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000611 SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000612 Base = AM.Base;
613 if (!Base.getNode())
614 // Register 0 means "no base". This is mostly useful for shifts.
615 Base = CurDAG->getRegister(0, VT);
616 else if (Base.getOpcode() == ISD::FrameIndex) {
617 // Lower a FrameIndex to a TargetFrameIndex.
618 int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex();
619 Base = CurDAG->getTargetFrameIndex(FrameIndex, VT);
620 } else if (Base.getValueType() != VT) {
621 // Truncate values from i64 to i32, for shifts.
622 assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 &&
623 "Unexpected truncation");
Andrew Trickef9de2a2013-05-25 02:42:55 +0000624 SDLoc DL(Base);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000625 SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base);
626 insertDAGNode(CurDAG, Base.getNode(), Trunc);
627 Base = Trunc;
628 }
629
630 // Lower the displacement to a TargetConstant.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000631 Disp = CurDAG->getTargetConstant(AM.Disp, SDLoc(Base), VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000632}
633
634void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
635 EVT VT, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000636 SDValue &Disp,
637 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000638 getAddressOperands(AM, VT, Base, Disp);
639
640 Index = AM.Index;
641 if (!Index.getNode())
642 // Register 0 means "no index".
643 Index = CurDAG->getRegister(0, VT);
644}
645
646bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR,
647 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000648 SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000649 SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR);
650 if (!selectAddress(Addr, AM))
651 return false;
652
653 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
654 return true;
655}
656
Richard Sandiforda481f582013-08-23 11:18:53 +0000657bool SystemZDAGToDAGISel::selectMVIAddr(SystemZAddressingMode::DispRange DR,
658 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000659 SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000660 SystemZAddressingMode AM(SystemZAddressingMode::FormBDXNormal, DR);
661 if (!selectAddress(Addr, AM) || AM.Index.getNode())
662 return false;
663
664 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
665 return true;
666}
667
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000668bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form,
669 SystemZAddressingMode::DispRange DR,
670 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000671 SDValue &Disp, SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000672 SystemZAddressingMode AM(Form, DR);
673 if (!selectAddress(Addr, AM))
674 return false;
675
676 getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index);
677 return true;
678}
679
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000680bool SystemZDAGToDAGISel::selectBDVAddr12Only(SDValue Addr, SDValue Elem,
681 SDValue &Base,
682 SDValue &Disp,
683 SDValue &Index) const {
684 SDValue Regs[2];
685 if (selectBDXAddr12Only(Addr, Regs[0], Disp, Regs[1]) &&
686 Regs[0].getNode() && Regs[1].getNode()) {
687 for (unsigned int I = 0; I < 2; ++I) {
688 Base = Regs[I];
689 Index = Regs[1 - I];
690 // We can't tell here whether the index vector has the right type
691 // for the access; the caller needs to do that instead.
692 if (Index.getOpcode() == ISD::ZERO_EXTEND)
693 Index = Index.getOperand(0);
694 if (Index.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
695 Index.getOperand(1) == Elem) {
696 Index = Index.getOperand(0);
697 return true;
698 }
699 }
700 }
701 return false;
702}
703
Richard Sandiford885140c2013-07-16 11:55:57 +0000704bool SystemZDAGToDAGISel::detectOrAndInsertion(SDValue &Op,
Richard Sandiford54b36912013-09-27 15:14:04 +0000705 uint64_t InsertMask) const {
Richard Sandiford885140c2013-07-16 11:55:57 +0000706 // We're only interested in cases where the insertion is into some operand
707 // of Op, rather than into Op itself. The only useful case is an AND.
708 if (Op.getOpcode() != ISD::AND)
709 return false;
710
711 // We need a constant mask.
Richard Sandiford21f5d682014-03-06 11:22:58 +0000712 auto *MaskNode = dyn_cast<ConstantSDNode>(Op.getOperand(1).getNode());
Richard Sandiford885140c2013-07-16 11:55:57 +0000713 if (!MaskNode)
714 return false;
715
716 // It's not an insertion of Op.getOperand(0) if the two masks overlap.
717 uint64_t AndMask = MaskNode->getZExtValue();
718 if (InsertMask & AndMask)
719 return false;
720
721 // It's only an insertion if all bits are covered or are known to be zero.
722 // The inner check covers all cases but is more expensive.
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000723 uint64_t Used = allOnes(Op.getValueSizeInBits());
Richard Sandiford885140c2013-07-16 11:55:57 +0000724 if (Used != (AndMask | InsertMask)) {
Craig Topperd0af7e82017-04-28 05:31:46 +0000725 KnownBits Known;
726 CurDAG->computeKnownBits(Op.getOperand(0), Known);
727 if (Used != (AndMask | InsertMask | Known.Zero.getZExtValue()))
Richard Sandiford885140c2013-07-16 11:55:57 +0000728 return false;
729 }
730
731 Op = Op.getOperand(0);
732 return true;
733}
734
Richard Sandiford54b36912013-09-27 15:14:04 +0000735bool SystemZDAGToDAGISel::refineRxSBGMask(RxSBGOperands &RxSBG,
736 uint64_t Mask) const {
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000737 const SystemZInstrInfo *TII = getInstrInfo();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000738 if (RxSBG.Rotate != 0)
739 Mask = (Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate));
740 Mask &= RxSBG.Mask;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000741 if (TII->isRxSBGMask(Mask, RxSBG.BitSize, RxSBG.Start, RxSBG.End)) {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000742 RxSBG.Mask = Mask;
Richard Sandiford5cbac962013-07-18 09:45:08 +0000743 return true;
744 }
Richard Sandiford84f54a32013-07-11 08:59:12 +0000745 return false;
746}
747
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000748// Return true if any bits of (RxSBG.Input & Mask) are significant.
749static bool maskMatters(RxSBGOperands &RxSBG, uint64_t Mask) {
750 // Rotate the mask in the same way as RxSBG.Input is rotated.
Richard Sandiford297f7d22013-07-18 10:14:55 +0000751 if (RxSBG.Rotate != 0)
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000752 Mask = ((Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate)));
753 return (Mask & RxSBG.Mask) != 0;
Richard Sandiford297f7d22013-07-18 10:14:55 +0000754}
755
Richard Sandiford54b36912013-09-27 15:14:04 +0000756bool SystemZDAGToDAGISel::expandRxSBG(RxSBGOperands &RxSBG) const {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000757 SDValue N = RxSBG.Input;
Richard Sandiford297f7d22013-07-18 10:14:55 +0000758 unsigned Opcode = N.getOpcode();
759 switch (Opcode) {
Zhan Jun Liau0df35052016-06-22 16:16:27 +0000760 case ISD::TRUNCATE: {
761 if (RxSBG.Opcode == SystemZ::RNSBG)
762 return false;
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000763 uint64_t BitSize = N.getValueSizeInBits();
Zhan Jun Liau0df35052016-06-22 16:16:27 +0000764 uint64_t Mask = allOnes(BitSize);
765 if (!refineRxSBGMask(RxSBG, Mask))
766 return false;
767 RxSBG.Input = N.getOperand(0);
768 return true;
769 }
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000770 case ISD::AND: {
Richard Sandiford51093212013-07-18 10:40:35 +0000771 if (RxSBG.Opcode == SystemZ::RNSBG)
772 return false;
773
Richard Sandiford21f5d682014-03-06 11:22:58 +0000774 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000775 if (!MaskNode)
776 return false;
777
778 SDValue Input = N.getOperand(0);
779 uint64_t Mask = MaskNode->getZExtValue();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000780 if (!refineRxSBGMask(RxSBG, Mask)) {
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000781 // If some bits of Input are already known zeros, those bits will have
782 // been removed from the mask. See if adding them back in makes the
783 // mask suitable.
Craig Topperd0af7e82017-04-28 05:31:46 +0000784 KnownBits Known;
785 CurDAG->computeKnownBits(Input, Known);
786 Mask |= Known.Zero.getZExtValue();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000787 if (!refineRxSBGMask(RxSBG, Mask))
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000788 return false;
789 }
Richard Sandiford5cbac962013-07-18 09:45:08 +0000790 RxSBG.Input = Input;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000791 return true;
792 }
793
Richard Sandiford51093212013-07-18 10:40:35 +0000794 case ISD::OR: {
795 if (RxSBG.Opcode != SystemZ::RNSBG)
796 return false;
797
Richard Sandiford21f5d682014-03-06 11:22:58 +0000798 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford51093212013-07-18 10:40:35 +0000799 if (!MaskNode)
800 return false;
801
802 SDValue Input = N.getOperand(0);
803 uint64_t Mask = ~MaskNode->getZExtValue();
804 if (!refineRxSBGMask(RxSBG, Mask)) {
805 // If some bits of Input are already known ones, those bits will have
806 // been removed from the mask. See if adding them back in makes the
807 // mask suitable.
Craig Topperd0af7e82017-04-28 05:31:46 +0000808 KnownBits Known;
809 CurDAG->computeKnownBits(Input, Known);
810 Mask &= ~Known.One.getZExtValue();
Richard Sandiford51093212013-07-18 10:40:35 +0000811 if (!refineRxSBGMask(RxSBG, Mask))
812 return false;
813 }
814 RxSBG.Input = Input;
815 return true;
816 }
817
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000818 case ISD::ROTL: {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000819 // Any 64-bit rotate left can be merged into the RxSBG.
Richard Sandiford3e382972013-10-16 13:35:13 +0000820 if (RxSBG.BitSize != 64 || N.getValueType() != MVT::i64)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000821 return false;
Richard Sandiford21f5d682014-03-06 11:22:58 +0000822 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000823 if (!CountNode)
824 return false;
825
Richard Sandiford5cbac962013-07-18 09:45:08 +0000826 RxSBG.Rotate = (RxSBG.Rotate + CountNode->getZExtValue()) & 63;
827 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000828 return true;
829 }
Simon Pilgrim0750c842015-08-15 13:27:30 +0000830
Richard Sandiford220ee492013-12-20 11:49:48 +0000831 case ISD::ANY_EXTEND:
832 // Bits above the extended operand are don't-care.
833 RxSBG.Input = N.getOperand(0);
834 return true;
835
Richard Sandiford3875cb62014-01-09 11:28:53 +0000836 case ISD::ZERO_EXTEND:
837 if (RxSBG.Opcode != SystemZ::RNSBG) {
838 // Restrict the mask to the extended operand.
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000839 unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits();
Richard Sandiford3875cb62014-01-09 11:28:53 +0000840 if (!refineRxSBGMask(RxSBG, allOnes(InnerBitSize)))
841 return false;
Richard Sandiford220ee492013-12-20 11:49:48 +0000842
Richard Sandiford3875cb62014-01-09 11:28:53 +0000843 RxSBG.Input = N.getOperand(0);
844 return true;
845 }
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000846 LLVM_FALLTHROUGH;
Simon Pilgrim0750c842015-08-15 13:27:30 +0000847
Richard Sandiford220ee492013-12-20 11:49:48 +0000848 case ISD::SIGN_EXTEND: {
Richard Sandiford3e382972013-10-16 13:35:13 +0000849 // Check that the extension bits are don't-care (i.e. are masked out
850 // by the final mask).
Jonas Paulsson19380ba2017-12-06 13:53:24 +0000851 unsigned BitSize = N.getValueSizeInBits();
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000852 unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits();
Jonas Paulsson19380ba2017-12-06 13:53:24 +0000853 if (maskMatters(RxSBG, allOnes(BitSize) - allOnes(InnerBitSize))) {
854 // In the case where only the sign bit is active, increase Rotate with
855 // the extension width.
856 if (RxSBG.Mask == 1 && RxSBG.Rotate == 1)
857 RxSBG.Rotate += (BitSize - InnerBitSize);
858 else
859 return false;
860 }
Richard Sandiford3e382972013-10-16 13:35:13 +0000861
862 RxSBG.Input = N.getOperand(0);
863 return true;
864 }
865
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000866 case ISD::SHL: {
Richard Sandiford21f5d682014-03-06 11:22:58 +0000867 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000868 if (!CountNode)
869 return false;
870
871 uint64_t Count = CountNode->getZExtValue();
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000872 unsigned BitSize = N.getValueSizeInBits();
Richard Sandiford3e382972013-10-16 13:35:13 +0000873 if (Count < 1 || Count >= BitSize)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000874 return false;
875
Richard Sandiford51093212013-07-18 10:40:35 +0000876 if (RxSBG.Opcode == SystemZ::RNSBG) {
877 // Treat (shl X, count) as (rotl X, size-count) as long as the bottom
878 // count bits from RxSBG.Input are ignored.
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000879 if (maskMatters(RxSBG, allOnes(Count)))
Richard Sandiford51093212013-07-18 10:40:35 +0000880 return false;
881 } else {
882 // Treat (shl X, count) as (and (rotl X, count), ~0<<count).
Richard Sandiford3e382972013-10-16 13:35:13 +0000883 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count) << Count))
Richard Sandiford51093212013-07-18 10:40:35 +0000884 return false;
885 }
886
Richard Sandiford5cbac962013-07-18 09:45:08 +0000887 RxSBG.Rotate = (RxSBG.Rotate + Count) & 63;
888 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000889 return true;
890 }
891
Richard Sandiford297f7d22013-07-18 10:14:55 +0000892 case ISD::SRL:
893 case ISD::SRA: {
Richard Sandiford21f5d682014-03-06 11:22:58 +0000894 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000895 if (!CountNode)
896 return false;
897
898 uint64_t Count = CountNode->getZExtValue();
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000899 unsigned BitSize = N.getValueSizeInBits();
Richard Sandiford3e382972013-10-16 13:35:13 +0000900 if (Count < 1 || Count >= BitSize)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000901 return false;
902
Richard Sandiford51093212013-07-18 10:40:35 +0000903 if (RxSBG.Opcode == SystemZ::RNSBG || Opcode == ISD::SRA) {
904 // Treat (srl|sra X, count) as (rotl X, size-count) as long as the top
905 // count bits from RxSBG.Input are ignored.
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000906 if (maskMatters(RxSBG, allOnes(Count) << (BitSize - Count)))
Richard Sandiford297f7d22013-07-18 10:14:55 +0000907 return false;
908 } else {
909 // Treat (srl X, count), mask) as (and (rotl X, size-count), ~0>>count),
910 // which is similar to SLL above.
Richard Sandiford3e382972013-10-16 13:35:13 +0000911 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count)))
Richard Sandiford297f7d22013-07-18 10:14:55 +0000912 return false;
913 }
914
Richard Sandiford5cbac962013-07-18 09:45:08 +0000915 RxSBG.Rotate = (RxSBG.Rotate - Count) & 63;
916 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000917 return true;
918 }
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000919 default:
920 return false;
921 }
922}
923
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000924SDValue SystemZDAGToDAGISel::getUNDEF(const SDLoc &DL, EVT VT) const {
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000925 SDNode *N = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000926 return SDValue(N, 0);
927}
928
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000929SDValue SystemZDAGToDAGISel::convertTo(const SDLoc &DL, EVT VT,
930 SDValue N) const {
Richard Sandifordd8163202013-09-13 09:12:44 +0000931 if (N.getValueType() == MVT::i32 && VT == MVT::i64)
Richard Sandiford87a44362013-09-30 10:28:35 +0000932 return CurDAG->getTargetInsertSubreg(SystemZ::subreg_l32,
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000933 DL, VT, getUNDEF(DL, MVT::i64), N);
Richard Sandifordd8163202013-09-13 09:12:44 +0000934 if (N.getValueType() == MVT::i64 && VT == MVT::i32)
Richard Sandiford87a44362013-09-30 10:28:35 +0000935 return CurDAG->getTargetExtractSubreg(SystemZ::subreg_l32, DL, VT, N);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000936 assert(N.getValueType() == VT && "Unexpected value types");
937 return N;
938}
939
Justin Bognerbbcd2232016-05-10 21:11:26 +0000940bool SystemZDAGToDAGISel::tryRISBGZero(SDNode *N) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000941 SDLoc DL(N);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000942 EVT VT = N->getValueType(0);
Ulrich Weigand77884bc2015-06-25 11:52:36 +0000943 if (!VT.isInteger() || VT.getSizeInBits() > 64)
Justin Bognerbbcd2232016-05-10 21:11:26 +0000944 return false;
Richard Sandiford51093212013-07-18 10:40:35 +0000945 RxSBGOperands RISBG(SystemZ::RISBG, SDValue(N, 0));
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000946 unsigned Count = 0;
Richard Sandiford5cbac962013-07-18 09:45:08 +0000947 while (expandRxSBG(RISBG))
Zhan Jun Liau0df35052016-06-22 16:16:27 +0000948 // The widening or narrowing is expected to be free.
949 // Counting widening or narrowing as a saved operation will result in
950 // preferring an R*SBG over a simple shift/logical instruction.
951 if (RISBG.Input.getOpcode() != ISD::ANY_EXTEND &&
952 RISBG.Input.getOpcode() != ISD::TRUNCATE)
Richard Sandiford3e382972013-10-16 13:35:13 +0000953 Count += 1;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000954 if (Count == 0)
Justin Bognerbbcd2232016-05-10 21:11:26 +0000955 return false;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000956
Ulrich Weigand5dc7b672016-11-11 12:43:51 +0000957 // Prefer to use normal shift instructions over RISBG, since they can handle
958 // all cases and are sometimes shorter.
959 if (Count == 1 && N->getOpcode() != ISD::AND)
960 return false;
961
962 // Prefer register extensions like LLC over RISBG. Also prefer to start
963 // out with normal ANDs if one instruction would be enough. We can convert
964 // these ANDs into an RISBG later if a three-address instruction is useful.
965 if (RISBG.Rotate == 0) {
966 bool PreferAnd = false;
967 // Prefer AND for any 32-bit and-immediate operation.
968 if (VT == MVT::i32)
969 PreferAnd = true;
970 // As well as for any 64-bit operation that can be implemented via LLC(R),
971 // LLH(R), LLGT(R), or one of the and-immediate instructions.
972 else if (RISBG.Mask == 0xff ||
973 RISBG.Mask == 0xffff ||
974 RISBG.Mask == 0x7fffffff ||
975 SystemZ::isImmLF(~RISBG.Mask) ||
976 SystemZ::isImmHF(~RISBG.Mask))
977 PreferAnd = true;
Ulrich Weigand92c2c672016-11-11 12:46:28 +0000978 // And likewise for the LLZRGF instruction, which doesn't have a register
979 // to register version.
980 else if (auto *Load = dyn_cast<LoadSDNode>(RISBG.Input)) {
981 if (Load->getMemoryVT() == MVT::i32 &&
982 (Load->getExtensionType() == ISD::EXTLOAD ||
983 Load->getExtensionType() == ISD::ZEXTLOAD) &&
984 RISBG.Mask == 0xffffff00 &&
985 Subtarget->hasLoadAndZeroRightmostByte())
986 PreferAnd = true;
987 }
Ulrich Weigand5dc7b672016-11-11 12:43:51 +0000988 if (PreferAnd) {
989 // Replace the current node with an AND. Note that the current node
990 // might already be that same AND, in which case it is already CSE'd
991 // with it, and we must not call ReplaceNode.
992 SDValue In = convertTo(DL, VT, RISBG.Input);
993 SDValue Mask = CurDAG->getConstant(RISBG.Mask, DL, VT);
994 SDValue New = CurDAG->getNode(ISD::AND, DL, VT, In, Mask);
995 if (N != New.getNode()) {
996 insertDAGNode(CurDAG, N, Mask);
997 insertDAGNode(CurDAG, N, New);
998 ReplaceNode(N, New.getNode());
999 N = New.getNode();
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001000 }
Ulrich Weigand5dc7b672016-11-11 12:43:51 +00001001 // Now, select the machine opcode to implement this operation.
Jonas Paulssonf268cd02018-02-27 07:53:23 +00001002 if (!N->isMachineOpcode())
1003 SelectCode(N);
Ulrich Weigand5dc7b672016-11-11 12:43:51 +00001004 return true;
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001005 }
Simon Pilgrim0750c842015-08-15 13:27:30 +00001006 }
1007
Richard Sandiford3ad5a152013-10-01 14:36:20 +00001008 unsigned Opcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001009 // Prefer RISBGN if available, since it does not clobber CC.
1010 if (Subtarget->hasMiscellaneousExtensions())
1011 Opcode = SystemZ::RISBGN;
Richard Sandiford3ad5a152013-10-01 14:36:20 +00001012 EVT OpcodeVT = MVT::i64;
Ulrich Weigand55b85902017-11-14 19:20:46 +00001013 if (VT == MVT::i32 && Subtarget->hasHighWord() &&
1014 // We can only use the 32-bit instructions if all source bits are
1015 // in the low 32 bits without wrapping, both after rotation (because
1016 // of the smaller range for Start and End) and before rotation
1017 // (because the input value is truncated).
1018 RISBG.Start >= 32 && RISBG.End >= RISBG.Start &&
1019 ((RISBG.Start + RISBG.Rotate) & 63) >= 32 &&
1020 ((RISBG.End + RISBG.Rotate) & 63) >=
1021 ((RISBG.Start + RISBG.Rotate) & 63)) {
Richard Sandiford3ad5a152013-10-01 14:36:20 +00001022 Opcode = SystemZ::RISBMux;
1023 OpcodeVT = MVT::i32;
1024 RISBG.Start &= 31;
1025 RISBG.End &= 31;
1026 }
Richard Sandiford84f54a32013-07-11 08:59:12 +00001027 SDValue Ops[5] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001028 getUNDEF(DL, OpcodeVT),
1029 convertTo(DL, OpcodeVT, RISBG.Input),
1030 CurDAG->getTargetConstant(RISBG.Start, DL, MVT::i32),
1031 CurDAG->getTargetConstant(RISBG.End | 128, DL, MVT::i32),
1032 CurDAG->getTargetConstant(RISBG.Rotate, DL, MVT::i32)
Richard Sandiford84f54a32013-07-11 08:59:12 +00001033 };
Justin Bognerbbcd2232016-05-10 21:11:26 +00001034 SDValue New = convertTo(
1035 DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, OpcodeVT, Ops), 0));
Nirav Dave3264c1b2018-03-19 20:19:46 +00001036 ReplaceNode(N, New.getNode());
Justin Bognerbbcd2232016-05-10 21:11:26 +00001037 return true;
Richard Sandiford84f54a32013-07-11 08:59:12 +00001038}
1039
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001040bool SystemZDAGToDAGISel::tryRxSBG(SDNode *N, unsigned Opcode) {
Ulrich Weigand77884bc2015-06-25 11:52:36 +00001041 SDLoc DL(N);
1042 EVT VT = N->getValueType(0);
1043 if (!VT.isInteger() || VT.getSizeInBits() > 64)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001044 return false;
Richard Sandiford7878b852013-07-18 10:06:15 +00001045 // Try treating each operand of N as the second operand of the RxSBG
Richard Sandiford885140c2013-07-16 11:55:57 +00001046 // and see which goes deepest.
Richard Sandiford51093212013-07-18 10:40:35 +00001047 RxSBGOperands RxSBG[] = {
1048 RxSBGOperands(Opcode, N->getOperand(0)),
1049 RxSBGOperands(Opcode, N->getOperand(1))
1050 };
Richard Sandiford885140c2013-07-16 11:55:57 +00001051 unsigned Count[] = { 0, 0 };
1052 for (unsigned I = 0; I < 2; ++I)
Richard Sandiford5cbac962013-07-18 09:45:08 +00001053 while (expandRxSBG(RxSBG[I]))
Zhan Jun Liau0df35052016-06-22 16:16:27 +00001054 // The widening or narrowing is expected to be free.
1055 // Counting widening or narrowing as a saved operation will result in
1056 // preferring an R*SBG over a simple shift/logical instruction.
1057 if (RxSBG[I].Input.getOpcode() != ISD::ANY_EXTEND &&
1058 RxSBG[I].Input.getOpcode() != ISD::TRUNCATE)
Richard Sandiford3e382972013-10-16 13:35:13 +00001059 Count[I] += 1;
Richard Sandiford885140c2013-07-16 11:55:57 +00001060
1061 // Do nothing if neither operand is suitable.
1062 if (Count[0] == 0 && Count[1] == 0)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001063 return false;
Richard Sandiford885140c2013-07-16 11:55:57 +00001064
1065 // Pick the deepest second operand.
1066 unsigned I = Count[0] > Count[1] ? 0 : 1;
1067 SDValue Op0 = N->getOperand(I ^ 1);
1068
1069 // Prefer IC for character insertions from memory.
Richard Sandiford7878b852013-07-18 10:06:15 +00001070 if (Opcode == SystemZ::ROSBG && (RxSBG[I].Mask & 0xff) == 0)
Richard Sandiford21f5d682014-03-06 11:22:58 +00001071 if (auto *Load = dyn_cast<LoadSDNode>(Op0.getNode()))
Richard Sandiford885140c2013-07-16 11:55:57 +00001072 if (Load->getMemoryVT() == MVT::i8)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001073 return false;
Richard Sandiford885140c2013-07-16 11:55:57 +00001074
1075 // See whether we can avoid an AND in the first operand by converting
1076 // ROSBG to RISBG.
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001077 if (Opcode == SystemZ::ROSBG && detectOrAndInsertion(Op0, RxSBG[I].Mask)) {
Richard Sandiford885140c2013-07-16 11:55:57 +00001078 Opcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001079 // Prefer RISBGN if available, since it does not clobber CC.
1080 if (Subtarget->hasMiscellaneousExtensions())
1081 Opcode = SystemZ::RISBGN;
1082 }
1083
Richard Sandiford885140c2013-07-16 11:55:57 +00001084 SDValue Ops[5] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001085 convertTo(DL, MVT::i64, Op0),
1086 convertTo(DL, MVT::i64, RxSBG[I].Input),
1087 CurDAG->getTargetConstant(RxSBG[I].Start, DL, MVT::i32),
1088 CurDAG->getTargetConstant(RxSBG[I].End, DL, MVT::i32),
1089 CurDAG->getTargetConstant(RxSBG[I].Rotate, DL, MVT::i32)
Richard Sandiford885140c2013-07-16 11:55:57 +00001090 };
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001091 SDValue New = convertTo(
1092 DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, MVT::i64, Ops), 0));
1093 ReplaceNode(N, New.getNode());
1094 return true;
Richard Sandiford885140c2013-07-16 11:55:57 +00001095}
1096
Justin Bognerffb273d2016-05-09 23:54:23 +00001097void SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node,
1098 SDValue Op0, uint64_t UpperVal,
1099 uint64_t LowerVal) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001100 EVT VT = Node->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001101 SDLoc DL(Node);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001102 SDValue Upper = CurDAG->getConstant(UpperVal, DL, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001103 if (Op0.getNode())
1104 Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper);
Justin Bognerffb273d2016-05-09 23:54:23 +00001105
1106 {
1107 // When we haven't passed in Op0, Upper will be a constant. In order to
1108 // prevent folding back to the large immediate in `Or = getNode(...)` we run
1109 // SelectCode first and end up with an opaque machine node. This means that
1110 // we need to use a handle to keep track of Upper in case it gets CSE'd by
1111 // SelectCode.
1112 //
1113 // Note that in the case where Op0 is passed in we could just call
1114 // SelectCode(Upper) later, along with the SelectCode(Or), and avoid needing
1115 // the handle at all, but it's fine to do it here.
1116 //
1117 // TODO: This is a pretty hacky way to do this. Can we do something that
1118 // doesn't require a two paragraph explanation?
1119 HandleSDNode Handle(Upper);
1120 SelectCode(Upper.getNode());
1121 Upper = Handle.getValue();
1122 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001123
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001124 SDValue Lower = CurDAG->getConstant(LowerVal, DL, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001125 SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower);
Justin Bognerffb273d2016-05-09 23:54:23 +00001126
Nirav Dave3264c1b2018-03-19 20:19:46 +00001127 ReplaceNode(Node, Or.getNode());
Justin Bognerffb273d2016-05-09 23:54:23 +00001128
1129 SelectCode(Or.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001130}
1131
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001132bool SystemZDAGToDAGISel::tryGather(SDNode *N, unsigned Opcode) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001133 SDValue ElemV = N->getOperand(2);
1134 auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
1135 if (!ElemN)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001136 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001137
1138 unsigned Elem = ElemN->getZExtValue();
1139 EVT VT = N->getValueType(0);
1140 if (Elem >= VT.getVectorNumElements())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001141 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001142
1143 auto *Load = dyn_cast<LoadSDNode>(N->getOperand(1));
1144 if (!Load || !Load->hasOneUse())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001145 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001146 if (Load->getMemoryVT().getSizeInBits() !=
1147 Load->getValueType(0).getSizeInBits())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001148 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001149
1150 SDValue Base, Disp, Index;
1151 if (!selectBDVAddr12Only(Load->getBasePtr(), ElemV, Base, Disp, Index) ||
1152 Index.getValueType() != VT.changeVectorElementTypeToInteger())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001153 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001154
1155 SDLoc DL(Load);
1156 SDValue Ops[] = {
1157 N->getOperand(0), Base, Disp, Index,
1158 CurDAG->getTargetConstant(Elem, DL, MVT::i32), Load->getChain()
1159 };
1160 SDNode *Res = CurDAG->getMachineNode(Opcode, DL, VT, MVT::Other, Ops);
1161 ReplaceUses(SDValue(Load, 1), SDValue(Res, 1));
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001162 ReplaceNode(N, Res);
1163 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001164}
1165
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001166bool SystemZDAGToDAGISel::tryScatter(StoreSDNode *Store, unsigned Opcode) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001167 SDValue Value = Store->getValue();
1168 if (Value.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001169 return false;
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00001170 if (Store->getMemoryVT().getSizeInBits() != Value.getValueSizeInBits())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001171 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001172
1173 SDValue ElemV = Value.getOperand(1);
1174 auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
1175 if (!ElemN)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001176 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001177
1178 SDValue Vec = Value.getOperand(0);
1179 EVT VT = Vec.getValueType();
1180 unsigned Elem = ElemN->getZExtValue();
1181 if (Elem >= VT.getVectorNumElements())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001182 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001183
1184 SDValue Base, Disp, Index;
1185 if (!selectBDVAddr12Only(Store->getBasePtr(), ElemV, Base, Disp, Index) ||
1186 Index.getValueType() != VT.changeVectorElementTypeToInteger())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001187 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001188
1189 SDLoc DL(Store);
1190 SDValue Ops[] = {
1191 Vec, Base, Disp, Index, CurDAG->getTargetConstant(Elem, DL, MVT::i32),
1192 Store->getChain()
1193 };
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001194 ReplaceNode(Store, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops));
1195 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001196}
1197
Richard Sandiford067817e2013-09-27 15:29:20 +00001198bool SystemZDAGToDAGISel::canUseBlockOperation(StoreSDNode *Store,
1199 LoadSDNode *Load) const {
Richard Sandiford178273a2013-09-05 10:36:45 +00001200 // Check that the two memory operands have the same size.
1201 if (Load->getMemoryVT() != Store->getMemoryVT())
Richard Sandiford97846492013-07-09 09:46:39 +00001202 return false;
1203
Richard Sandiford178273a2013-09-05 10:36:45 +00001204 // Volatility stops an access from being decomposed.
1205 if (Load->isVolatile() || Store->isVolatile())
1206 return false;
Richard Sandiford97846492013-07-09 09:46:39 +00001207
1208 // There's no chance of overlap if the load is invariant.
Justin Lebaradbf09e2016-09-11 01:38:58 +00001209 if (Load->isInvariant() && Load->isDereferenceable())
Richard Sandiford97846492013-07-09 09:46:39 +00001210 return true;
1211
Richard Sandiford97846492013-07-09 09:46:39 +00001212 // Otherwise we need to check whether there's an alias.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001213 const Value *V1 = Load->getMemOperand()->getValue();
1214 const Value *V2 = Store->getMemOperand()->getValue();
Richard Sandiford97846492013-07-09 09:46:39 +00001215 if (!V1 || !V2)
1216 return false;
1217
Richard Sandiford067817e2013-09-27 15:29:20 +00001218 // Reject equality.
1219 uint64_t Size = Load->getMemoryVT().getStoreSize();
Richard Sandiford97846492013-07-09 09:46:39 +00001220 int64_t End1 = Load->getSrcValueOffset() + Size;
1221 int64_t End2 = Store->getSrcValueOffset() + Size;
Richard Sandiford067817e2013-09-27 15:29:20 +00001222 if (V1 == V2 && End1 == End2)
1223 return false;
1224
Chandler Carruthac80dc72015-06-17 07:18:54 +00001225 return !AA->alias(MemoryLocation(V1, End1, Load->getAAInfo()),
1226 MemoryLocation(V2, End2, Store->getAAInfo()));
Richard Sandiford97846492013-07-09 09:46:39 +00001227}
1228
Richard Sandiford178273a2013-09-05 10:36:45 +00001229bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001230 auto *Store = cast<StoreSDNode>(N);
1231 auto *Load = cast<LoadSDNode>(Store->getValue());
Richard Sandiford178273a2013-09-05 10:36:45 +00001232
1233 // Prefer not to use MVC if either address can use ... RELATIVE LONG
1234 // instructions.
1235 uint64_t Size = Load->getMemoryVT().getStoreSize();
1236 if (Size > 1 && Size <= 8) {
1237 // Prefer LHRL, LRL and LGRL.
Richard Sandiford54b36912013-09-27 15:14:04 +00001238 if (SystemZISD::isPCREL(Load->getBasePtr().getOpcode()))
Richard Sandiford178273a2013-09-05 10:36:45 +00001239 return false;
1240 // Prefer STHRL, STRL and STGRL.
Richard Sandiford54b36912013-09-27 15:14:04 +00001241 if (SystemZISD::isPCREL(Store->getBasePtr().getOpcode()))
Richard Sandiford178273a2013-09-05 10:36:45 +00001242 return false;
1243 }
1244
Richard Sandiford067817e2013-09-27 15:29:20 +00001245 return canUseBlockOperation(Store, Load);
Richard Sandiford178273a2013-09-05 10:36:45 +00001246}
1247
1248bool SystemZDAGToDAGISel::storeLoadCanUseBlockBinary(SDNode *N,
1249 unsigned I) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001250 auto *StoreA = cast<StoreSDNode>(N);
1251 auto *LoadA = cast<LoadSDNode>(StoreA->getValue().getOperand(1 - I));
1252 auto *LoadB = cast<LoadSDNode>(StoreA->getValue().getOperand(I));
Richard Sandiford067817e2013-09-27 15:29:20 +00001253 return !LoadA->isVolatile() && canUseBlockOperation(StoreA, LoadB);
Richard Sandiford178273a2013-09-05 10:36:45 +00001254}
1255
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001256void SystemZDAGToDAGISel::Select(SDNode *Node) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001257 // If we have a custom node, we already have selected!
1258 if (Node->isMachineOpcode()) {
1259 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Tim Northover31d093c2013-09-22 08:21:56 +00001260 Node->setNodeId(-1);
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001261 return;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001262 }
1263
1264 unsigned Opcode = Node->getOpcode();
1265 switch (Opcode) {
1266 case ISD::OR:
Richard Sandiford885140c2013-07-16 11:55:57 +00001267 if (Node->getOperand(1).getOpcode() != ISD::Constant)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001268 if (tryRxSBG(Node, SystemZ::ROSBG))
1269 return;
Richard Sandiford7878b852013-07-18 10:06:15 +00001270 goto or_xor;
1271
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001272 case ISD::XOR:
Richard Sandiford7878b852013-07-18 10:06:15 +00001273 if (Node->getOperand(1).getOpcode() != ISD::Constant)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001274 if (tryRxSBG(Node, SystemZ::RXSBG))
1275 return;
Richard Sandiford7878b852013-07-18 10:06:15 +00001276 // Fall through.
1277 or_xor:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001278 // If this is a 64-bit operation in which both 32-bit halves are nonzero,
Ulrich Weigand5f4373a2017-11-14 20:00:34 +00001279 // split the operation into two. If both operands here happen to be
1280 // constant, leave this to common code to optimize.
1281 if (Node->getValueType(0) == MVT::i64 &&
1282 Node->getOperand(0).getOpcode() != ISD::Constant)
Richard Sandiford21f5d682014-03-06 11:22:58 +00001283 if (auto *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001284 uint64_t Val = Op1->getZExtValue();
Justin Bognerffb273d2016-05-09 23:54:23 +00001285 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val)) {
1286 splitLargeImmediate(Opcode, Node, Node->getOperand(0),
1287 Val - uint32_t(Val), uint32_t(Val));
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001288 return;
Justin Bognerffb273d2016-05-09 23:54:23 +00001289 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001290 }
1291 break;
1292
Richard Sandiford84f54a32013-07-11 08:59:12 +00001293 case ISD::AND:
Richard Sandiford51093212013-07-18 10:40:35 +00001294 if (Node->getOperand(1).getOpcode() != ISD::Constant)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001295 if (tryRxSBG(Node, SystemZ::RNSBG))
1296 return;
Justin Bognercd1d5aa2016-08-17 20:30:52 +00001297 LLVM_FALLTHROUGH;
Richard Sandiford82ec87d2013-07-16 11:02:24 +00001298 case ISD::ROTL:
1299 case ISD::SHL:
1300 case ISD::SRL:
Richard Sandiford220ee492013-12-20 11:49:48 +00001301 case ISD::ZERO_EXTEND:
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001302 if (tryRISBGZero(Node))
1303 return;
Richard Sandiford84f54a32013-07-11 08:59:12 +00001304 break;
1305
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001306 case ISD::Constant:
1307 // If this is a 64-bit constant that is out of the range of LLILF,
1308 // LLIHF and LGFI, split it into two 32-bit pieces.
1309 if (Node->getValueType(0) == MVT::i64) {
1310 uint64_t Val = cast<ConstantSDNode>(Node)->getZExtValue();
Justin Bognerffb273d2016-05-09 23:54:23 +00001311 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val)) {
1312 splitLargeImmediate(ISD::OR, Node, SDValue(), Val - uint32_t(Val),
1313 uint32_t(Val));
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001314 return;
Justin Bognerffb273d2016-05-09 23:54:23 +00001315 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001316 }
1317 break;
1318
Richard Sandifordee834382013-07-31 12:38:08 +00001319 case SystemZISD::SELECT_CCMASK: {
1320 SDValue Op0 = Node->getOperand(0);
1321 SDValue Op1 = Node->getOperand(1);
1322 // Prefer to put any load first, so that it can be matched as a
Ulrich Weigand524f2762016-11-28 13:34:08 +00001323 // conditional load. Likewise for constants in range for LOCHI.
1324 if ((Op1.getOpcode() == ISD::LOAD && Op0.getOpcode() != ISD::LOAD) ||
1325 (Subtarget->hasLoadStoreOnCond2() &&
1326 Node->getValueType(0).isInteger() &&
1327 Op1.getOpcode() == ISD::Constant &&
1328 isInt<16>(cast<ConstantSDNode>(Op1)->getSExtValue()) &&
1329 !(Op0.getOpcode() == ISD::Constant &&
1330 isInt<16>(cast<ConstantSDNode>(Op0)->getSExtValue())))) {
Richard Sandifordee834382013-07-31 12:38:08 +00001331 SDValue CCValid = Node->getOperand(2);
1332 SDValue CCMask = Node->getOperand(3);
1333 uint64_t ConstCCValid =
1334 cast<ConstantSDNode>(CCValid.getNode())->getZExtValue();
1335 uint64_t ConstCCMask =
1336 cast<ConstantSDNode>(CCMask.getNode())->getZExtValue();
1337 // Invert the condition.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001338 CCMask = CurDAG->getConstant(ConstCCValid ^ ConstCCMask, SDLoc(Node),
Richard Sandifordee834382013-07-31 12:38:08 +00001339 CCMask.getValueType());
1340 SDValue Op4 = Node->getOperand(4);
1341 Node = CurDAG->UpdateNodeOperands(Node, Op1, Op0, CCValid, CCMask, Op4);
1342 }
1343 break;
1344 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001345
1346 case ISD::INSERT_VECTOR_ELT: {
1347 EVT VT = Node->getValueType(0);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00001348 unsigned ElemBitSize = VT.getScalarSizeInBits();
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001349 if (ElemBitSize == 32) {
1350 if (tryGather(Node, SystemZ::VGEF))
1351 return;
1352 } else if (ElemBitSize == 64) {
1353 if (tryGather(Node, SystemZ::VGEG))
1354 return;
1355 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001356 break;
1357 }
1358
1359 case ISD::STORE: {
1360 auto *Store = cast<StoreSDNode>(Node);
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00001361 unsigned ElemBitSize = Store->getValue().getValueSizeInBits();
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001362 if (ElemBitSize == 32) {
1363 if (tryScatter(Store, SystemZ::VSCEF))
1364 return;
1365 } else if (ElemBitSize == 64) {
1366 if (tryScatter(Store, SystemZ::VSCEG))
1367 return;
1368 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001369 break;
1370 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001371 }
1372
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001373 SelectCode(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001374}
1375
1376bool SystemZDAGToDAGISel::
1377SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +00001378 unsigned ConstraintID,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001379 std::vector<SDValue> &OutOps) {
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001380 SystemZAddressingMode::AddrForm Form;
1381 SystemZAddressingMode::DispRange DispRange;
Ulrich Weigand79564612016-06-09 15:19:16 +00001382 SDValue Base, Disp, Index;
1383
Daniel Sanders2eeace22015-03-17 16:16:14 +00001384 switch(ConstraintID) {
1385 default:
1386 llvm_unreachable("Unexpected asm memory constraint");
1387 case InlineAsm::Constraint_i:
Daniel Sanders2eeace22015-03-17 16:16:14 +00001388 case InlineAsm::Constraint_Q:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001389 // Accept an address with a short displacement, but no index.
1390 Form = SystemZAddressingMode::FormBD;
1391 DispRange = SystemZAddressingMode::Disp12Only;
1392 break;
Daniel Sanders2eeace22015-03-17 16:16:14 +00001393 case InlineAsm::Constraint_R:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001394 // Accept an address with a short displacement and an index.
1395 Form = SystemZAddressingMode::FormBDXNormal;
1396 DispRange = SystemZAddressingMode::Disp12Only;
Daniel Sanders2eeace22015-03-17 16:16:14 +00001397 break;
Ulrich Weigand79564612016-06-09 15:19:16 +00001398 case InlineAsm::Constraint_S:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001399 // Accept an address with a long displacement, but no index.
1400 Form = SystemZAddressingMode::FormBD;
1401 DispRange = SystemZAddressingMode::Disp20Only;
1402 break;
Ulrich Weigand79564612016-06-09 15:19:16 +00001403 case InlineAsm::Constraint_T:
1404 case InlineAsm::Constraint_m:
Ulrich Weigandd39e9dc2017-11-09 16:31:57 +00001405 case InlineAsm::Constraint_o:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001406 // Accept an address with a long displacement and an index.
1407 // m works the same as T, as this is the most general case.
Ulrich Weigandd39e9dc2017-11-09 16:31:57 +00001408 // We don't really have any special handling of "offsettable"
1409 // memory addresses, so just treat o the same as m.
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001410 Form = SystemZAddressingMode::FormBDXNormal;
1411 DispRange = SystemZAddressingMode::Disp20Only;
Ulrich Weigand79564612016-06-09 15:19:16 +00001412 break;
Daniel Sanders2eeace22015-03-17 16:16:14 +00001413 }
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001414
1415 if (selectBDXAddr(Form, DispRange, Op, Base, Disp, Index)) {
Zhan Jun Liaucf2f4b32016-08-18 21:44:15 +00001416 const TargetRegisterClass *TRC =
1417 Subtarget->getRegisterInfo()->getPointerRegClass(*MF);
1418 SDLoc DL(Base);
1419 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), DL, MVT::i32);
1420
1421 // Make sure that the base address doesn't go into %r0.
1422 // If it's a TargetFrameIndex or a fixed register, we shouldn't do anything.
1423 if (Base.getOpcode() != ISD::TargetFrameIndex &&
1424 Base.getOpcode() != ISD::Register) {
1425 Base =
1426 SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
1427 DL, Base.getValueType(),
1428 Base, RC), 0);
1429 }
1430
1431 // Make sure that the index register isn't assigned to %r0 either.
1432 if (Index.getOpcode() != ISD::Register) {
1433 Index =
1434 SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
1435 DL, Index.getValueType(),
1436 Index, RC), 0);
1437 }
1438
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001439 OutOps.push_back(Base);
1440 OutOps.push_back(Disp);
1441 OutOps.push_back(Index);
1442 return false;
1443 }
1444
Daniel Sanders2eeace22015-03-17 16:16:14 +00001445 return true;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001446}
Ulrich Weigand849a59f2018-01-19 20:52:04 +00001447
1448namespace {
1449// Represents a sequence for extracting a 0/1 value from an IPM result:
1450// (((X ^ XORValue) + AddValue) >> Bit)
1451struct IPMConversion {
1452 IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit)
1453 : XORValue(xorValue), AddValue(addValue), Bit(bit) {}
1454
1455 int64_t XORValue;
1456 int64_t AddValue;
1457 unsigned Bit;
1458};
1459} // end anonymous namespace
1460
1461// Return a sequence for getting a 1 from an IPM result when CC has a
1462// value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1463// The handling of CC values outside CCValid doesn't matter.
1464static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1465 // Deal with cases where the result can be taken directly from a bit
1466 // of the IPM result.
1467 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1468 return IPMConversion(0, 0, SystemZ::IPM_CC);
1469 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1470 return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1471
1472 // Deal with cases where we can add a value to force the sign bit
1473 // to contain the right value. Putting the bit in 31 means we can
1474 // use SRL rather than RISBG(L), and also makes it easier to get a
1475 // 0/-1 value, so it has priority over the other tests below.
1476 //
1477 // These sequences rely on the fact that the upper two bits of the
1478 // IPM result are zero.
1479 uint64_t TopBit = uint64_t(1) << 31;
1480 if (CCMask == (CCValid & SystemZ::CCMASK_0))
1481 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1482 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1483 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1484 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1485 | SystemZ::CCMASK_1
1486 | SystemZ::CCMASK_2)))
1487 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1488 if (CCMask == (CCValid & SystemZ::CCMASK_3))
1489 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1490 if (CCMask == (CCValid & (SystemZ::CCMASK_1
1491 | SystemZ::CCMASK_2
1492 | SystemZ::CCMASK_3)))
1493 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1494
1495 // Next try inverting the value and testing a bit. 0/1 could be
1496 // handled this way too, but we dealt with that case above.
1497 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1498 return IPMConversion(-1, 0, SystemZ::IPM_CC);
1499
1500 // Handle cases where adding a value forces a non-sign bit to contain
1501 // the right value.
1502 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1503 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1504 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1505 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1506
1507 // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are
1508 // can be done by inverting the low CC bit and applying one of the
1509 // sign-based extractions above.
1510 if (CCMask == (CCValid & SystemZ::CCMASK_1))
1511 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1512 if (CCMask == (CCValid & SystemZ::CCMASK_2))
1513 return IPMConversion(1 << SystemZ::IPM_CC,
1514 TopBit - (3 << SystemZ::IPM_CC), 31);
1515 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1516 | SystemZ::CCMASK_1
1517 | SystemZ::CCMASK_3)))
1518 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1519 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1520 | SystemZ::CCMASK_2
1521 | SystemZ::CCMASK_3)))
1522 return IPMConversion(1 << SystemZ::IPM_CC,
1523 TopBit - (1 << SystemZ::IPM_CC), 31);
1524
1525 llvm_unreachable("Unexpected CC combination");
1526}
1527
1528SDValue SystemZDAGToDAGISel::expandSelectBoolean(SDNode *Node) {
1529 auto *TrueOp = dyn_cast<ConstantSDNode>(Node->getOperand(0));
1530 auto *FalseOp = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1531 if (!TrueOp || !FalseOp)
1532 return SDValue();
1533 if (FalseOp->getZExtValue() != 0)
1534 return SDValue();
1535 if (TrueOp->getSExtValue() != 1 && TrueOp->getSExtValue() != -1)
1536 return SDValue();
1537
1538 auto *CCValidOp = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1539 auto *CCMaskOp = dyn_cast<ConstantSDNode>(Node->getOperand(3));
1540 if (!CCValidOp || !CCMaskOp)
1541 return SDValue();
1542 int CCValid = CCValidOp->getZExtValue();
1543 int CCMask = CCMaskOp->getZExtValue();
1544
1545 SDLoc DL(Node);
1546 SDValue Glue = Node->getOperand(4);
1547 IPMConversion IPM = getIPMConversion(CCValid, CCMask);
1548 SDValue Result = CurDAG->getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
1549
1550 if (IPM.XORValue)
1551 Result = CurDAG->getNode(ISD::XOR, DL, MVT::i32, Result,
1552 CurDAG->getConstant(IPM.XORValue, DL, MVT::i32));
1553
1554 if (IPM.AddValue)
1555 Result = CurDAG->getNode(ISD::ADD, DL, MVT::i32, Result,
1556 CurDAG->getConstant(IPM.AddValue, DL, MVT::i32));
1557
1558 EVT VT = Node->getValueType(0);
1559 if (VT == MVT::i32 && IPM.Bit == 31) {
1560 unsigned ShiftOp = TrueOp->getSExtValue() == 1 ? ISD::SRL : ISD::SRA;
1561 Result = CurDAG->getNode(ShiftOp, DL, MVT::i32, Result,
1562 CurDAG->getConstant(IPM.Bit, DL, MVT::i32));
1563 } else {
1564 if (VT != MVT::i32)
1565 Result = CurDAG->getNode(ISD::ANY_EXTEND, DL, VT, Result);
1566
1567 if (TrueOp->getSExtValue() == 1) {
1568 // The SHR/AND sequence should get optimized to an RISBG.
1569 Result = CurDAG->getNode(ISD::SRL, DL, VT, Result,
1570 CurDAG->getConstant(IPM.Bit, DL, MVT::i32));
1571 Result = CurDAG->getNode(ISD::AND, DL, VT, Result,
1572 CurDAG->getConstant(1, DL, VT));
1573 } else {
1574 // Sign-extend from IPM.Bit using a pair of shifts.
1575 int ShlAmt = VT.getSizeInBits() - 1 - IPM.Bit;
1576 int SraAmt = VT.getSizeInBits() - 1;
1577 Result = CurDAG->getNode(ISD::SHL, DL, VT, Result,
1578 CurDAG->getConstant(ShlAmt, DL, MVT::i32));
1579 Result = CurDAG->getNode(ISD::SRA, DL, VT, Result,
1580 CurDAG->getConstant(SraAmt, DL, MVT::i32));
1581 }
1582 }
1583
1584 return Result;
1585}
1586
1587void SystemZDAGToDAGISel::PreprocessISelDAG() {
Ulrich Weigand426f6be2018-01-19 20:56:04 +00001588 // If we have conditional immediate loads, we always prefer
1589 // using those over an IPM sequence.
1590 if (Subtarget->hasLoadStoreOnCond2())
1591 return;
1592
Ulrich Weigand849a59f2018-01-19 20:52:04 +00001593 bool MadeChange = false;
1594
1595 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
1596 E = CurDAG->allnodes_end();
1597 I != E;) {
1598 SDNode *N = &*I++;
1599 if (N->use_empty())
1600 continue;
1601
1602 SDValue Res;
1603 switch (N->getOpcode()) {
1604 default: break;
1605 case SystemZISD::SELECT_CCMASK:
1606 Res = expandSelectBoolean(N);
1607 break;
1608 }
1609
1610 if (Res) {
1611 DEBUG(dbgs() << "SystemZ DAG preprocessing replacing:\nOld: ");
1612 DEBUG(N->dump(CurDAG));
1613 DEBUG(dbgs() << "\nNew: ");
1614 DEBUG(Res.getNode()->dump(CurDAG));
1615 DEBUG(dbgs() << "\n");
1616
1617 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Res);
1618 MadeChange = true;
1619 }
1620 }
1621
1622 if (MadeChange)
1623 CurDAG->RemoveDeadNodes();
1624}