blob: 81175013ed2af39bda41adcc87c942fe30e237b6 [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());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000599 N.getNode()->setNodeId(Pos->getNodeId());
600 }
601}
602
603void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
604 EVT VT, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000605 SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000606 Base = AM.Base;
607 if (!Base.getNode())
608 // Register 0 means "no base". This is mostly useful for shifts.
609 Base = CurDAG->getRegister(0, VT);
610 else if (Base.getOpcode() == ISD::FrameIndex) {
611 // Lower a FrameIndex to a TargetFrameIndex.
612 int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex();
613 Base = CurDAG->getTargetFrameIndex(FrameIndex, VT);
614 } else if (Base.getValueType() != VT) {
615 // Truncate values from i64 to i32, for shifts.
616 assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 &&
617 "Unexpected truncation");
Andrew Trickef9de2a2013-05-25 02:42:55 +0000618 SDLoc DL(Base);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000619 SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base);
620 insertDAGNode(CurDAG, Base.getNode(), Trunc);
621 Base = Trunc;
622 }
623
624 // Lower the displacement to a TargetConstant.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000625 Disp = CurDAG->getTargetConstant(AM.Disp, SDLoc(Base), VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000626}
627
628void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
629 EVT VT, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000630 SDValue &Disp,
631 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000632 getAddressOperands(AM, VT, Base, Disp);
633
634 Index = AM.Index;
635 if (!Index.getNode())
636 // Register 0 means "no index".
637 Index = CurDAG->getRegister(0, VT);
638}
639
640bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR,
641 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000642 SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000643 SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR);
644 if (!selectAddress(Addr, AM))
645 return false;
646
647 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
648 return true;
649}
650
Richard Sandiforda481f582013-08-23 11:18:53 +0000651bool SystemZDAGToDAGISel::selectMVIAddr(SystemZAddressingMode::DispRange DR,
652 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000653 SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000654 SystemZAddressingMode AM(SystemZAddressingMode::FormBDXNormal, DR);
655 if (!selectAddress(Addr, AM) || AM.Index.getNode())
656 return false;
657
658 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
659 return true;
660}
661
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000662bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form,
663 SystemZAddressingMode::DispRange DR,
664 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000665 SDValue &Disp, SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000666 SystemZAddressingMode AM(Form, DR);
667 if (!selectAddress(Addr, AM))
668 return false;
669
670 getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index);
671 return true;
672}
673
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000674bool SystemZDAGToDAGISel::selectBDVAddr12Only(SDValue Addr, SDValue Elem,
675 SDValue &Base,
676 SDValue &Disp,
677 SDValue &Index) const {
678 SDValue Regs[2];
679 if (selectBDXAddr12Only(Addr, Regs[0], Disp, Regs[1]) &&
680 Regs[0].getNode() && Regs[1].getNode()) {
681 for (unsigned int I = 0; I < 2; ++I) {
682 Base = Regs[I];
683 Index = Regs[1 - I];
684 // We can't tell here whether the index vector has the right type
685 // for the access; the caller needs to do that instead.
686 if (Index.getOpcode() == ISD::ZERO_EXTEND)
687 Index = Index.getOperand(0);
688 if (Index.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
689 Index.getOperand(1) == Elem) {
690 Index = Index.getOperand(0);
691 return true;
692 }
693 }
694 }
695 return false;
696}
697
Richard Sandiford885140c2013-07-16 11:55:57 +0000698bool SystemZDAGToDAGISel::detectOrAndInsertion(SDValue &Op,
Richard Sandiford54b36912013-09-27 15:14:04 +0000699 uint64_t InsertMask) const {
Richard Sandiford885140c2013-07-16 11:55:57 +0000700 // We're only interested in cases where the insertion is into some operand
701 // of Op, rather than into Op itself. The only useful case is an AND.
702 if (Op.getOpcode() != ISD::AND)
703 return false;
704
705 // We need a constant mask.
Richard Sandiford21f5d682014-03-06 11:22:58 +0000706 auto *MaskNode = dyn_cast<ConstantSDNode>(Op.getOperand(1).getNode());
Richard Sandiford885140c2013-07-16 11:55:57 +0000707 if (!MaskNode)
708 return false;
709
710 // It's not an insertion of Op.getOperand(0) if the two masks overlap.
711 uint64_t AndMask = MaskNode->getZExtValue();
712 if (InsertMask & AndMask)
713 return false;
714
715 // It's only an insertion if all bits are covered or are known to be zero.
716 // The inner check covers all cases but is more expensive.
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000717 uint64_t Used = allOnes(Op.getValueSizeInBits());
Richard Sandiford885140c2013-07-16 11:55:57 +0000718 if (Used != (AndMask | InsertMask)) {
Craig Topperd0af7e82017-04-28 05:31:46 +0000719 KnownBits Known;
720 CurDAG->computeKnownBits(Op.getOperand(0), Known);
721 if (Used != (AndMask | InsertMask | Known.Zero.getZExtValue()))
Richard Sandiford885140c2013-07-16 11:55:57 +0000722 return false;
723 }
724
725 Op = Op.getOperand(0);
726 return true;
727}
728
Richard Sandiford54b36912013-09-27 15:14:04 +0000729bool SystemZDAGToDAGISel::refineRxSBGMask(RxSBGOperands &RxSBG,
730 uint64_t Mask) const {
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000731 const SystemZInstrInfo *TII = getInstrInfo();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000732 if (RxSBG.Rotate != 0)
733 Mask = (Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate));
734 Mask &= RxSBG.Mask;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000735 if (TII->isRxSBGMask(Mask, RxSBG.BitSize, RxSBG.Start, RxSBG.End)) {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000736 RxSBG.Mask = Mask;
Richard Sandiford5cbac962013-07-18 09:45:08 +0000737 return true;
738 }
Richard Sandiford84f54a32013-07-11 08:59:12 +0000739 return false;
740}
741
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000742// Return true if any bits of (RxSBG.Input & Mask) are significant.
743static bool maskMatters(RxSBGOperands &RxSBG, uint64_t Mask) {
744 // Rotate the mask in the same way as RxSBG.Input is rotated.
Richard Sandiford297f7d22013-07-18 10:14:55 +0000745 if (RxSBG.Rotate != 0)
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000746 Mask = ((Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate)));
747 return (Mask & RxSBG.Mask) != 0;
Richard Sandiford297f7d22013-07-18 10:14:55 +0000748}
749
Richard Sandiford54b36912013-09-27 15:14:04 +0000750bool SystemZDAGToDAGISel::expandRxSBG(RxSBGOperands &RxSBG) const {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000751 SDValue N = RxSBG.Input;
Richard Sandiford297f7d22013-07-18 10:14:55 +0000752 unsigned Opcode = N.getOpcode();
753 switch (Opcode) {
Zhan Jun Liau0df35052016-06-22 16:16:27 +0000754 case ISD::TRUNCATE: {
755 if (RxSBG.Opcode == SystemZ::RNSBG)
756 return false;
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000757 uint64_t BitSize = N.getValueSizeInBits();
Zhan Jun Liau0df35052016-06-22 16:16:27 +0000758 uint64_t Mask = allOnes(BitSize);
759 if (!refineRxSBGMask(RxSBG, Mask))
760 return false;
761 RxSBG.Input = N.getOperand(0);
762 return true;
763 }
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000764 case ISD::AND: {
Richard Sandiford51093212013-07-18 10:40:35 +0000765 if (RxSBG.Opcode == SystemZ::RNSBG)
766 return false;
767
Richard Sandiford21f5d682014-03-06 11:22:58 +0000768 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000769 if (!MaskNode)
770 return false;
771
772 SDValue Input = N.getOperand(0);
773 uint64_t Mask = MaskNode->getZExtValue();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000774 if (!refineRxSBGMask(RxSBG, Mask)) {
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000775 // If some bits of Input are already known zeros, those bits will have
776 // been removed from the mask. See if adding them back in makes the
777 // mask suitable.
Craig Topperd0af7e82017-04-28 05:31:46 +0000778 KnownBits Known;
779 CurDAG->computeKnownBits(Input, Known);
780 Mask |= Known.Zero.getZExtValue();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000781 if (!refineRxSBGMask(RxSBG, Mask))
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000782 return false;
783 }
Richard Sandiford5cbac962013-07-18 09:45:08 +0000784 RxSBG.Input = Input;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000785 return true;
786 }
787
Richard Sandiford51093212013-07-18 10:40:35 +0000788 case ISD::OR: {
789 if (RxSBG.Opcode != SystemZ::RNSBG)
790 return false;
791
Richard Sandiford21f5d682014-03-06 11:22:58 +0000792 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford51093212013-07-18 10:40:35 +0000793 if (!MaskNode)
794 return false;
795
796 SDValue Input = N.getOperand(0);
797 uint64_t Mask = ~MaskNode->getZExtValue();
798 if (!refineRxSBGMask(RxSBG, Mask)) {
799 // If some bits of Input are already known ones, those bits will have
800 // been removed from the mask. See if adding them back in makes the
801 // mask suitable.
Craig Topperd0af7e82017-04-28 05:31:46 +0000802 KnownBits Known;
803 CurDAG->computeKnownBits(Input, Known);
804 Mask &= ~Known.One.getZExtValue();
Richard Sandiford51093212013-07-18 10:40:35 +0000805 if (!refineRxSBGMask(RxSBG, Mask))
806 return false;
807 }
808 RxSBG.Input = Input;
809 return true;
810 }
811
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000812 case ISD::ROTL: {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000813 // Any 64-bit rotate left can be merged into the RxSBG.
Richard Sandiford3e382972013-10-16 13:35:13 +0000814 if (RxSBG.BitSize != 64 || N.getValueType() != MVT::i64)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000815 return false;
Richard Sandiford21f5d682014-03-06 11:22:58 +0000816 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000817 if (!CountNode)
818 return false;
819
Richard Sandiford5cbac962013-07-18 09:45:08 +0000820 RxSBG.Rotate = (RxSBG.Rotate + CountNode->getZExtValue()) & 63;
821 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000822 return true;
823 }
Simon Pilgrim0750c842015-08-15 13:27:30 +0000824
Richard Sandiford220ee492013-12-20 11:49:48 +0000825 case ISD::ANY_EXTEND:
826 // Bits above the extended operand are don't-care.
827 RxSBG.Input = N.getOperand(0);
828 return true;
829
Richard Sandiford3875cb62014-01-09 11:28:53 +0000830 case ISD::ZERO_EXTEND:
831 if (RxSBG.Opcode != SystemZ::RNSBG) {
832 // Restrict the mask to the extended operand.
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000833 unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits();
Richard Sandiford3875cb62014-01-09 11:28:53 +0000834 if (!refineRxSBGMask(RxSBG, allOnes(InnerBitSize)))
835 return false;
Richard Sandiford220ee492013-12-20 11:49:48 +0000836
Richard Sandiford3875cb62014-01-09 11:28:53 +0000837 RxSBG.Input = N.getOperand(0);
838 return true;
839 }
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000840 LLVM_FALLTHROUGH;
Simon Pilgrim0750c842015-08-15 13:27:30 +0000841
Richard Sandiford220ee492013-12-20 11:49:48 +0000842 case ISD::SIGN_EXTEND: {
Richard Sandiford3e382972013-10-16 13:35:13 +0000843 // Check that the extension bits are don't-care (i.e. are masked out
844 // by the final mask).
Jonas Paulsson19380ba2017-12-06 13:53:24 +0000845 unsigned BitSize = N.getValueSizeInBits();
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000846 unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits();
Jonas Paulsson19380ba2017-12-06 13:53:24 +0000847 if (maskMatters(RxSBG, allOnes(BitSize) - allOnes(InnerBitSize))) {
848 // In the case where only the sign bit is active, increase Rotate with
849 // the extension width.
850 if (RxSBG.Mask == 1 && RxSBG.Rotate == 1)
851 RxSBG.Rotate += (BitSize - InnerBitSize);
852 else
853 return false;
854 }
Richard Sandiford3e382972013-10-16 13:35:13 +0000855
856 RxSBG.Input = N.getOperand(0);
857 return true;
858 }
859
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000860 case ISD::SHL: {
Richard Sandiford21f5d682014-03-06 11:22:58 +0000861 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000862 if (!CountNode)
863 return false;
864
865 uint64_t Count = CountNode->getZExtValue();
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000866 unsigned BitSize = N.getValueSizeInBits();
Richard Sandiford3e382972013-10-16 13:35:13 +0000867 if (Count < 1 || Count >= BitSize)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000868 return false;
869
Richard Sandiford51093212013-07-18 10:40:35 +0000870 if (RxSBG.Opcode == SystemZ::RNSBG) {
871 // Treat (shl X, count) as (rotl X, size-count) as long as the bottom
872 // count bits from RxSBG.Input are ignored.
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000873 if (maskMatters(RxSBG, allOnes(Count)))
Richard Sandiford51093212013-07-18 10:40:35 +0000874 return false;
875 } else {
876 // Treat (shl X, count) as (and (rotl X, count), ~0<<count).
Richard Sandiford3e382972013-10-16 13:35:13 +0000877 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count) << Count))
Richard Sandiford51093212013-07-18 10:40:35 +0000878 return false;
879 }
880
Richard Sandiford5cbac962013-07-18 09:45:08 +0000881 RxSBG.Rotate = (RxSBG.Rotate + Count) & 63;
882 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000883 return true;
884 }
885
Richard Sandiford297f7d22013-07-18 10:14:55 +0000886 case ISD::SRL:
887 case ISD::SRA: {
Richard Sandiford21f5d682014-03-06 11:22:58 +0000888 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000889 if (!CountNode)
890 return false;
891
892 uint64_t Count = CountNode->getZExtValue();
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000893 unsigned BitSize = N.getValueSizeInBits();
Richard Sandiford3e382972013-10-16 13:35:13 +0000894 if (Count < 1 || Count >= BitSize)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000895 return false;
896
Richard Sandiford51093212013-07-18 10:40:35 +0000897 if (RxSBG.Opcode == SystemZ::RNSBG || Opcode == ISD::SRA) {
898 // Treat (srl|sra X, count) as (rotl X, size-count) as long as the top
899 // count bits from RxSBG.Input are ignored.
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000900 if (maskMatters(RxSBG, allOnes(Count) << (BitSize - Count)))
Richard Sandiford297f7d22013-07-18 10:14:55 +0000901 return false;
902 } else {
903 // Treat (srl X, count), mask) as (and (rotl X, size-count), ~0>>count),
904 // which is similar to SLL above.
Richard Sandiford3e382972013-10-16 13:35:13 +0000905 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count)))
Richard Sandiford297f7d22013-07-18 10:14:55 +0000906 return false;
907 }
908
Richard Sandiford5cbac962013-07-18 09:45:08 +0000909 RxSBG.Rotate = (RxSBG.Rotate - Count) & 63;
910 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000911 return true;
912 }
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000913 default:
914 return false;
915 }
916}
917
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000918SDValue SystemZDAGToDAGISel::getUNDEF(const SDLoc &DL, EVT VT) const {
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000919 SDNode *N = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000920 return SDValue(N, 0);
921}
922
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000923SDValue SystemZDAGToDAGISel::convertTo(const SDLoc &DL, EVT VT,
924 SDValue N) const {
Richard Sandifordd8163202013-09-13 09:12:44 +0000925 if (N.getValueType() == MVT::i32 && VT == MVT::i64)
Richard Sandiford87a44362013-09-30 10:28:35 +0000926 return CurDAG->getTargetInsertSubreg(SystemZ::subreg_l32,
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000927 DL, VT, getUNDEF(DL, MVT::i64), N);
Richard Sandifordd8163202013-09-13 09:12:44 +0000928 if (N.getValueType() == MVT::i64 && VT == MVT::i32)
Richard Sandiford87a44362013-09-30 10:28:35 +0000929 return CurDAG->getTargetExtractSubreg(SystemZ::subreg_l32, DL, VT, N);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000930 assert(N.getValueType() == VT && "Unexpected value types");
931 return N;
932}
933
Justin Bognerbbcd2232016-05-10 21:11:26 +0000934bool SystemZDAGToDAGISel::tryRISBGZero(SDNode *N) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000935 SDLoc DL(N);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000936 EVT VT = N->getValueType(0);
Ulrich Weigand77884bc2015-06-25 11:52:36 +0000937 if (!VT.isInteger() || VT.getSizeInBits() > 64)
Justin Bognerbbcd2232016-05-10 21:11:26 +0000938 return false;
Richard Sandiford51093212013-07-18 10:40:35 +0000939 RxSBGOperands RISBG(SystemZ::RISBG, SDValue(N, 0));
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000940 unsigned Count = 0;
Richard Sandiford5cbac962013-07-18 09:45:08 +0000941 while (expandRxSBG(RISBG))
Zhan Jun Liau0df35052016-06-22 16:16:27 +0000942 // The widening or narrowing is expected to be free.
943 // Counting widening or narrowing as a saved operation will result in
944 // preferring an R*SBG over a simple shift/logical instruction.
945 if (RISBG.Input.getOpcode() != ISD::ANY_EXTEND &&
946 RISBG.Input.getOpcode() != ISD::TRUNCATE)
Richard Sandiford3e382972013-10-16 13:35:13 +0000947 Count += 1;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000948 if (Count == 0)
Justin Bognerbbcd2232016-05-10 21:11:26 +0000949 return false;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000950
Ulrich Weigand5dc7b672016-11-11 12:43:51 +0000951 // Prefer to use normal shift instructions over RISBG, since they can handle
952 // all cases and are sometimes shorter.
953 if (Count == 1 && N->getOpcode() != ISD::AND)
954 return false;
955
956 // Prefer register extensions like LLC over RISBG. Also prefer to start
957 // out with normal ANDs if one instruction would be enough. We can convert
958 // these ANDs into an RISBG later if a three-address instruction is useful.
959 if (RISBG.Rotate == 0) {
960 bool PreferAnd = false;
961 // Prefer AND for any 32-bit and-immediate operation.
962 if (VT == MVT::i32)
963 PreferAnd = true;
964 // As well as for any 64-bit operation that can be implemented via LLC(R),
965 // LLH(R), LLGT(R), or one of the and-immediate instructions.
966 else if (RISBG.Mask == 0xff ||
967 RISBG.Mask == 0xffff ||
968 RISBG.Mask == 0x7fffffff ||
969 SystemZ::isImmLF(~RISBG.Mask) ||
970 SystemZ::isImmHF(~RISBG.Mask))
971 PreferAnd = true;
Ulrich Weigand92c2c672016-11-11 12:46:28 +0000972 // And likewise for the LLZRGF instruction, which doesn't have a register
973 // to register version.
974 else if (auto *Load = dyn_cast<LoadSDNode>(RISBG.Input)) {
975 if (Load->getMemoryVT() == MVT::i32 &&
976 (Load->getExtensionType() == ISD::EXTLOAD ||
977 Load->getExtensionType() == ISD::ZEXTLOAD) &&
978 RISBG.Mask == 0xffffff00 &&
979 Subtarget->hasLoadAndZeroRightmostByte())
980 PreferAnd = true;
981 }
Ulrich Weigand5dc7b672016-11-11 12:43:51 +0000982 if (PreferAnd) {
983 // Replace the current node with an AND. Note that the current node
984 // might already be that same AND, in which case it is already CSE'd
985 // with it, and we must not call ReplaceNode.
986 SDValue In = convertTo(DL, VT, RISBG.Input);
987 SDValue Mask = CurDAG->getConstant(RISBG.Mask, DL, VT);
988 SDValue New = CurDAG->getNode(ISD::AND, DL, VT, In, Mask);
989 if (N != New.getNode()) {
990 insertDAGNode(CurDAG, N, Mask);
991 insertDAGNode(CurDAG, N, New);
992 ReplaceNode(N, New.getNode());
993 N = New.getNode();
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000994 }
Ulrich Weigand5dc7b672016-11-11 12:43:51 +0000995 // Now, select the machine opcode to implement this operation.
996 SelectCode(N);
997 return true;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000998 }
Simon Pilgrim0750c842015-08-15 13:27:30 +0000999 }
1000
Richard Sandiford3ad5a152013-10-01 14:36:20 +00001001 unsigned Opcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001002 // Prefer RISBGN if available, since it does not clobber CC.
1003 if (Subtarget->hasMiscellaneousExtensions())
1004 Opcode = SystemZ::RISBGN;
Richard Sandiford3ad5a152013-10-01 14:36:20 +00001005 EVT OpcodeVT = MVT::i64;
Ulrich Weigand55b85902017-11-14 19:20:46 +00001006 if (VT == MVT::i32 && Subtarget->hasHighWord() &&
1007 // We can only use the 32-bit instructions if all source bits are
1008 // in the low 32 bits without wrapping, both after rotation (because
1009 // of the smaller range for Start and End) and before rotation
1010 // (because the input value is truncated).
1011 RISBG.Start >= 32 && RISBG.End >= RISBG.Start &&
1012 ((RISBG.Start + RISBG.Rotate) & 63) >= 32 &&
1013 ((RISBG.End + RISBG.Rotate) & 63) >=
1014 ((RISBG.Start + RISBG.Rotate) & 63)) {
Richard Sandiford3ad5a152013-10-01 14:36:20 +00001015 Opcode = SystemZ::RISBMux;
1016 OpcodeVT = MVT::i32;
1017 RISBG.Start &= 31;
1018 RISBG.End &= 31;
1019 }
Richard Sandiford84f54a32013-07-11 08:59:12 +00001020 SDValue Ops[5] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001021 getUNDEF(DL, OpcodeVT),
1022 convertTo(DL, OpcodeVT, RISBG.Input),
1023 CurDAG->getTargetConstant(RISBG.Start, DL, MVT::i32),
1024 CurDAG->getTargetConstant(RISBG.End | 128, DL, MVT::i32),
1025 CurDAG->getTargetConstant(RISBG.Rotate, DL, MVT::i32)
Richard Sandiford84f54a32013-07-11 08:59:12 +00001026 };
Justin Bognerbbcd2232016-05-10 21:11:26 +00001027 SDValue New = convertTo(
1028 DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, OpcodeVT, Ops), 0));
1029 ReplaceUses(N, New.getNode());
1030 CurDAG->RemoveDeadNode(N);
1031 return true;
Richard Sandiford84f54a32013-07-11 08:59:12 +00001032}
1033
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001034bool SystemZDAGToDAGISel::tryRxSBG(SDNode *N, unsigned Opcode) {
Ulrich Weigand77884bc2015-06-25 11:52:36 +00001035 SDLoc DL(N);
1036 EVT VT = N->getValueType(0);
1037 if (!VT.isInteger() || VT.getSizeInBits() > 64)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001038 return false;
Richard Sandiford7878b852013-07-18 10:06:15 +00001039 // Try treating each operand of N as the second operand of the RxSBG
Richard Sandiford885140c2013-07-16 11:55:57 +00001040 // and see which goes deepest.
Richard Sandiford51093212013-07-18 10:40:35 +00001041 RxSBGOperands RxSBG[] = {
1042 RxSBGOperands(Opcode, N->getOperand(0)),
1043 RxSBGOperands(Opcode, N->getOperand(1))
1044 };
Richard Sandiford885140c2013-07-16 11:55:57 +00001045 unsigned Count[] = { 0, 0 };
1046 for (unsigned I = 0; I < 2; ++I)
Richard Sandiford5cbac962013-07-18 09:45:08 +00001047 while (expandRxSBG(RxSBG[I]))
Zhan Jun Liau0df35052016-06-22 16:16:27 +00001048 // The widening or narrowing is expected to be free.
1049 // Counting widening or narrowing as a saved operation will result in
1050 // preferring an R*SBG over a simple shift/logical instruction.
1051 if (RxSBG[I].Input.getOpcode() != ISD::ANY_EXTEND &&
1052 RxSBG[I].Input.getOpcode() != ISD::TRUNCATE)
Richard Sandiford3e382972013-10-16 13:35:13 +00001053 Count[I] += 1;
Richard Sandiford885140c2013-07-16 11:55:57 +00001054
1055 // Do nothing if neither operand is suitable.
1056 if (Count[0] == 0 && Count[1] == 0)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001057 return false;
Richard Sandiford885140c2013-07-16 11:55:57 +00001058
1059 // Pick the deepest second operand.
1060 unsigned I = Count[0] > Count[1] ? 0 : 1;
1061 SDValue Op0 = N->getOperand(I ^ 1);
1062
1063 // Prefer IC for character insertions from memory.
Richard Sandiford7878b852013-07-18 10:06:15 +00001064 if (Opcode == SystemZ::ROSBG && (RxSBG[I].Mask & 0xff) == 0)
Richard Sandiford21f5d682014-03-06 11:22:58 +00001065 if (auto *Load = dyn_cast<LoadSDNode>(Op0.getNode()))
Richard Sandiford885140c2013-07-16 11:55:57 +00001066 if (Load->getMemoryVT() == MVT::i8)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001067 return false;
Richard Sandiford885140c2013-07-16 11:55:57 +00001068
1069 // See whether we can avoid an AND in the first operand by converting
1070 // ROSBG to RISBG.
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001071 if (Opcode == SystemZ::ROSBG && detectOrAndInsertion(Op0, RxSBG[I].Mask)) {
Richard Sandiford885140c2013-07-16 11:55:57 +00001072 Opcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001073 // Prefer RISBGN if available, since it does not clobber CC.
1074 if (Subtarget->hasMiscellaneousExtensions())
1075 Opcode = SystemZ::RISBGN;
1076 }
1077
Richard Sandiford885140c2013-07-16 11:55:57 +00001078 SDValue Ops[5] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001079 convertTo(DL, MVT::i64, Op0),
1080 convertTo(DL, MVT::i64, RxSBG[I].Input),
1081 CurDAG->getTargetConstant(RxSBG[I].Start, DL, MVT::i32),
1082 CurDAG->getTargetConstant(RxSBG[I].End, DL, MVT::i32),
1083 CurDAG->getTargetConstant(RxSBG[I].Rotate, DL, MVT::i32)
Richard Sandiford885140c2013-07-16 11:55:57 +00001084 };
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001085 SDValue New = convertTo(
1086 DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, MVT::i64, Ops), 0));
1087 ReplaceNode(N, New.getNode());
1088 return true;
Richard Sandiford885140c2013-07-16 11:55:57 +00001089}
1090
Justin Bognerffb273d2016-05-09 23:54:23 +00001091void SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node,
1092 SDValue Op0, uint64_t UpperVal,
1093 uint64_t LowerVal) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001094 EVT VT = Node->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001095 SDLoc DL(Node);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001096 SDValue Upper = CurDAG->getConstant(UpperVal, DL, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001097 if (Op0.getNode())
1098 Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper);
Justin Bognerffb273d2016-05-09 23:54:23 +00001099
1100 {
1101 // When we haven't passed in Op0, Upper will be a constant. In order to
1102 // prevent folding back to the large immediate in `Or = getNode(...)` we run
1103 // SelectCode first and end up with an opaque machine node. This means that
1104 // we need to use a handle to keep track of Upper in case it gets CSE'd by
1105 // SelectCode.
1106 //
1107 // Note that in the case where Op0 is passed in we could just call
1108 // SelectCode(Upper) later, along with the SelectCode(Or), and avoid needing
1109 // the handle at all, but it's fine to do it here.
1110 //
1111 // TODO: This is a pretty hacky way to do this. Can we do something that
1112 // doesn't require a two paragraph explanation?
1113 HandleSDNode Handle(Upper);
1114 SelectCode(Upper.getNode());
1115 Upper = Handle.getValue();
1116 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001117
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001118 SDValue Lower = CurDAG->getConstant(LowerVal, DL, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001119 SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower);
Justin Bognerffb273d2016-05-09 23:54:23 +00001120
1121 ReplaceUses(Node, Or.getNode());
1122 CurDAG->RemoveDeadNode(Node);
1123
1124 SelectCode(Or.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001125}
1126
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001127bool SystemZDAGToDAGISel::tryGather(SDNode *N, unsigned Opcode) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001128 SDValue ElemV = N->getOperand(2);
1129 auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
1130 if (!ElemN)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001131 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001132
1133 unsigned Elem = ElemN->getZExtValue();
1134 EVT VT = N->getValueType(0);
1135 if (Elem >= VT.getVectorNumElements())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001136 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001137
1138 auto *Load = dyn_cast<LoadSDNode>(N->getOperand(1));
1139 if (!Load || !Load->hasOneUse())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001140 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001141 if (Load->getMemoryVT().getSizeInBits() !=
1142 Load->getValueType(0).getSizeInBits())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001143 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001144
1145 SDValue Base, Disp, Index;
1146 if (!selectBDVAddr12Only(Load->getBasePtr(), ElemV, Base, Disp, Index) ||
1147 Index.getValueType() != VT.changeVectorElementTypeToInteger())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001148 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001149
1150 SDLoc DL(Load);
1151 SDValue Ops[] = {
1152 N->getOperand(0), Base, Disp, Index,
1153 CurDAG->getTargetConstant(Elem, DL, MVT::i32), Load->getChain()
1154 };
1155 SDNode *Res = CurDAG->getMachineNode(Opcode, DL, VT, MVT::Other, Ops);
1156 ReplaceUses(SDValue(Load, 1), SDValue(Res, 1));
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001157 ReplaceNode(N, Res);
1158 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001159}
1160
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001161bool SystemZDAGToDAGISel::tryScatter(StoreSDNode *Store, unsigned Opcode) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001162 SDValue Value = Store->getValue();
1163 if (Value.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001164 return false;
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00001165 if (Store->getMemoryVT().getSizeInBits() != Value.getValueSizeInBits())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001166 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001167
1168 SDValue ElemV = Value.getOperand(1);
1169 auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
1170 if (!ElemN)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001171 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001172
1173 SDValue Vec = Value.getOperand(0);
1174 EVT VT = Vec.getValueType();
1175 unsigned Elem = ElemN->getZExtValue();
1176 if (Elem >= VT.getVectorNumElements())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001177 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001178
1179 SDValue Base, Disp, Index;
1180 if (!selectBDVAddr12Only(Store->getBasePtr(), ElemV, Base, Disp, Index) ||
1181 Index.getValueType() != VT.changeVectorElementTypeToInteger())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001182 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001183
1184 SDLoc DL(Store);
1185 SDValue Ops[] = {
1186 Vec, Base, Disp, Index, CurDAG->getTargetConstant(Elem, DL, MVT::i32),
1187 Store->getChain()
1188 };
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001189 ReplaceNode(Store, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops));
1190 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001191}
1192
Richard Sandiford067817e2013-09-27 15:29:20 +00001193bool SystemZDAGToDAGISel::canUseBlockOperation(StoreSDNode *Store,
1194 LoadSDNode *Load) const {
Richard Sandiford178273a2013-09-05 10:36:45 +00001195 // Check that the two memory operands have the same size.
1196 if (Load->getMemoryVT() != Store->getMemoryVT())
Richard Sandiford97846492013-07-09 09:46:39 +00001197 return false;
1198
Richard Sandiford178273a2013-09-05 10:36:45 +00001199 // Volatility stops an access from being decomposed.
1200 if (Load->isVolatile() || Store->isVolatile())
1201 return false;
Richard Sandiford97846492013-07-09 09:46:39 +00001202
1203 // There's no chance of overlap if the load is invariant.
Justin Lebaradbf09e2016-09-11 01:38:58 +00001204 if (Load->isInvariant() && Load->isDereferenceable())
Richard Sandiford97846492013-07-09 09:46:39 +00001205 return true;
1206
Richard Sandiford97846492013-07-09 09:46:39 +00001207 // Otherwise we need to check whether there's an alias.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001208 const Value *V1 = Load->getMemOperand()->getValue();
1209 const Value *V2 = Store->getMemOperand()->getValue();
Richard Sandiford97846492013-07-09 09:46:39 +00001210 if (!V1 || !V2)
1211 return false;
1212
Richard Sandiford067817e2013-09-27 15:29:20 +00001213 // Reject equality.
1214 uint64_t Size = Load->getMemoryVT().getStoreSize();
Richard Sandiford97846492013-07-09 09:46:39 +00001215 int64_t End1 = Load->getSrcValueOffset() + Size;
1216 int64_t End2 = Store->getSrcValueOffset() + Size;
Richard Sandiford067817e2013-09-27 15:29:20 +00001217 if (V1 == V2 && End1 == End2)
1218 return false;
1219
Chandler Carruthac80dc72015-06-17 07:18:54 +00001220 return !AA->alias(MemoryLocation(V1, End1, Load->getAAInfo()),
1221 MemoryLocation(V2, End2, Store->getAAInfo()));
Richard Sandiford97846492013-07-09 09:46:39 +00001222}
1223
Richard Sandiford178273a2013-09-05 10:36:45 +00001224bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001225 auto *Store = cast<StoreSDNode>(N);
1226 auto *Load = cast<LoadSDNode>(Store->getValue());
Richard Sandiford178273a2013-09-05 10:36:45 +00001227
1228 // Prefer not to use MVC if either address can use ... RELATIVE LONG
1229 // instructions.
1230 uint64_t Size = Load->getMemoryVT().getStoreSize();
1231 if (Size > 1 && Size <= 8) {
1232 // Prefer LHRL, LRL and LGRL.
Richard Sandiford54b36912013-09-27 15:14:04 +00001233 if (SystemZISD::isPCREL(Load->getBasePtr().getOpcode()))
Richard Sandiford178273a2013-09-05 10:36:45 +00001234 return false;
1235 // Prefer STHRL, STRL and STGRL.
Richard Sandiford54b36912013-09-27 15:14:04 +00001236 if (SystemZISD::isPCREL(Store->getBasePtr().getOpcode()))
Richard Sandiford178273a2013-09-05 10:36:45 +00001237 return false;
1238 }
1239
Richard Sandiford067817e2013-09-27 15:29:20 +00001240 return canUseBlockOperation(Store, Load);
Richard Sandiford178273a2013-09-05 10:36:45 +00001241}
1242
1243bool SystemZDAGToDAGISel::storeLoadCanUseBlockBinary(SDNode *N,
1244 unsigned I) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001245 auto *StoreA = cast<StoreSDNode>(N);
1246 auto *LoadA = cast<LoadSDNode>(StoreA->getValue().getOperand(1 - I));
1247 auto *LoadB = cast<LoadSDNode>(StoreA->getValue().getOperand(I));
Richard Sandiford067817e2013-09-27 15:29:20 +00001248 return !LoadA->isVolatile() && canUseBlockOperation(StoreA, LoadB);
Richard Sandiford178273a2013-09-05 10:36:45 +00001249}
1250
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001251void SystemZDAGToDAGISel::Select(SDNode *Node) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001252 // Dump information about the Node being selected
1253 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
1254
1255 // If we have a custom node, we already have selected!
1256 if (Node->isMachineOpcode()) {
1257 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Tim Northover31d093c2013-09-22 08:21:56 +00001258 Node->setNodeId(-1);
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001259 return;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001260 }
1261
1262 unsigned Opcode = Node->getOpcode();
1263 switch (Opcode) {
1264 case ISD::OR:
Richard Sandiford885140c2013-07-16 11:55:57 +00001265 if (Node->getOperand(1).getOpcode() != ISD::Constant)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001266 if (tryRxSBG(Node, SystemZ::ROSBG))
1267 return;
Richard Sandiford7878b852013-07-18 10:06:15 +00001268 goto or_xor;
1269
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001270 case ISD::XOR:
Richard Sandiford7878b852013-07-18 10:06:15 +00001271 if (Node->getOperand(1).getOpcode() != ISD::Constant)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001272 if (tryRxSBG(Node, SystemZ::RXSBG))
1273 return;
Richard Sandiford7878b852013-07-18 10:06:15 +00001274 // Fall through.
1275 or_xor:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001276 // If this is a 64-bit operation in which both 32-bit halves are nonzero,
Ulrich Weigand5f4373a2017-11-14 20:00:34 +00001277 // split the operation into two. If both operands here happen to be
1278 // constant, leave this to common code to optimize.
1279 if (Node->getValueType(0) == MVT::i64 &&
1280 Node->getOperand(0).getOpcode() != ISD::Constant)
Richard Sandiford21f5d682014-03-06 11:22:58 +00001281 if (auto *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001282 uint64_t Val = Op1->getZExtValue();
Justin Bognerffb273d2016-05-09 23:54:23 +00001283 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val)) {
1284 splitLargeImmediate(Opcode, Node, Node->getOperand(0),
1285 Val - uint32_t(Val), uint32_t(Val));
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001286 return;
Justin Bognerffb273d2016-05-09 23:54:23 +00001287 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001288 }
1289 break;
1290
Richard Sandiford84f54a32013-07-11 08:59:12 +00001291 case ISD::AND:
Richard Sandiford51093212013-07-18 10:40:35 +00001292 if (Node->getOperand(1).getOpcode() != ISD::Constant)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001293 if (tryRxSBG(Node, SystemZ::RNSBG))
1294 return;
Justin Bognercd1d5aa2016-08-17 20:30:52 +00001295 LLVM_FALLTHROUGH;
Richard Sandiford82ec87d2013-07-16 11:02:24 +00001296 case ISD::ROTL:
1297 case ISD::SHL:
1298 case ISD::SRL:
Richard Sandiford220ee492013-12-20 11:49:48 +00001299 case ISD::ZERO_EXTEND:
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001300 if (tryRISBGZero(Node))
1301 return;
Richard Sandiford84f54a32013-07-11 08:59:12 +00001302 break;
1303
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001304 case ISD::Constant:
1305 // If this is a 64-bit constant that is out of the range of LLILF,
1306 // LLIHF and LGFI, split it into two 32-bit pieces.
1307 if (Node->getValueType(0) == MVT::i64) {
1308 uint64_t Val = cast<ConstantSDNode>(Node)->getZExtValue();
Justin Bognerffb273d2016-05-09 23:54:23 +00001309 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val)) {
1310 splitLargeImmediate(ISD::OR, Node, SDValue(), Val - uint32_t(Val),
1311 uint32_t(Val));
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001312 return;
Justin Bognerffb273d2016-05-09 23:54:23 +00001313 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001314 }
1315 break;
1316
Richard Sandifordee834382013-07-31 12:38:08 +00001317 case SystemZISD::SELECT_CCMASK: {
1318 SDValue Op0 = Node->getOperand(0);
1319 SDValue Op1 = Node->getOperand(1);
1320 // Prefer to put any load first, so that it can be matched as a
Ulrich Weigand524f2762016-11-28 13:34:08 +00001321 // conditional load. Likewise for constants in range for LOCHI.
1322 if ((Op1.getOpcode() == ISD::LOAD && Op0.getOpcode() != ISD::LOAD) ||
1323 (Subtarget->hasLoadStoreOnCond2() &&
1324 Node->getValueType(0).isInteger() &&
1325 Op1.getOpcode() == ISD::Constant &&
1326 isInt<16>(cast<ConstantSDNode>(Op1)->getSExtValue()) &&
1327 !(Op0.getOpcode() == ISD::Constant &&
1328 isInt<16>(cast<ConstantSDNode>(Op0)->getSExtValue())))) {
Richard Sandifordee834382013-07-31 12:38:08 +00001329 SDValue CCValid = Node->getOperand(2);
1330 SDValue CCMask = Node->getOperand(3);
1331 uint64_t ConstCCValid =
1332 cast<ConstantSDNode>(CCValid.getNode())->getZExtValue();
1333 uint64_t ConstCCMask =
1334 cast<ConstantSDNode>(CCMask.getNode())->getZExtValue();
1335 // Invert the condition.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001336 CCMask = CurDAG->getConstant(ConstCCValid ^ ConstCCMask, SDLoc(Node),
Richard Sandifordee834382013-07-31 12:38:08 +00001337 CCMask.getValueType());
1338 SDValue Op4 = Node->getOperand(4);
1339 Node = CurDAG->UpdateNodeOperands(Node, Op1, Op0, CCValid, CCMask, Op4);
1340 }
1341 break;
1342 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001343
1344 case ISD::INSERT_VECTOR_ELT: {
1345 EVT VT = Node->getValueType(0);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00001346 unsigned ElemBitSize = VT.getScalarSizeInBits();
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001347 if (ElemBitSize == 32) {
1348 if (tryGather(Node, SystemZ::VGEF))
1349 return;
1350 } else if (ElemBitSize == 64) {
1351 if (tryGather(Node, SystemZ::VGEG))
1352 return;
1353 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001354 break;
1355 }
1356
1357 case ISD::STORE: {
1358 auto *Store = cast<StoreSDNode>(Node);
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00001359 unsigned ElemBitSize = Store->getValue().getValueSizeInBits();
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001360 if (ElemBitSize == 32) {
1361 if (tryScatter(Store, SystemZ::VSCEF))
1362 return;
1363 } else if (ElemBitSize == 64) {
1364 if (tryScatter(Store, SystemZ::VSCEG))
1365 return;
1366 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001367 break;
1368 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001369 }
1370
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001371 SelectCode(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001372}
1373
1374bool SystemZDAGToDAGISel::
1375SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +00001376 unsigned ConstraintID,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001377 std::vector<SDValue> &OutOps) {
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001378 SystemZAddressingMode::AddrForm Form;
1379 SystemZAddressingMode::DispRange DispRange;
Ulrich Weigand79564612016-06-09 15:19:16 +00001380 SDValue Base, Disp, Index;
1381
Daniel Sanders2eeace22015-03-17 16:16:14 +00001382 switch(ConstraintID) {
1383 default:
1384 llvm_unreachable("Unexpected asm memory constraint");
1385 case InlineAsm::Constraint_i:
Daniel Sanders2eeace22015-03-17 16:16:14 +00001386 case InlineAsm::Constraint_Q:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001387 // Accept an address with a short displacement, but no index.
1388 Form = SystemZAddressingMode::FormBD;
1389 DispRange = SystemZAddressingMode::Disp12Only;
1390 break;
Daniel Sanders2eeace22015-03-17 16:16:14 +00001391 case InlineAsm::Constraint_R:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001392 // Accept an address with a short displacement and an index.
1393 Form = SystemZAddressingMode::FormBDXNormal;
1394 DispRange = SystemZAddressingMode::Disp12Only;
Daniel Sanders2eeace22015-03-17 16:16:14 +00001395 break;
Ulrich Weigand79564612016-06-09 15:19:16 +00001396 case InlineAsm::Constraint_S:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001397 // Accept an address with a long displacement, but no index.
1398 Form = SystemZAddressingMode::FormBD;
1399 DispRange = SystemZAddressingMode::Disp20Only;
1400 break;
Ulrich Weigand79564612016-06-09 15:19:16 +00001401 case InlineAsm::Constraint_T:
1402 case InlineAsm::Constraint_m:
Ulrich Weigandd39e9dc2017-11-09 16:31:57 +00001403 case InlineAsm::Constraint_o:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001404 // Accept an address with a long displacement and an index.
1405 // m works the same as T, as this is the most general case.
Ulrich Weigandd39e9dc2017-11-09 16:31:57 +00001406 // We don't really have any special handling of "offsettable"
1407 // memory addresses, so just treat o the same as m.
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001408 Form = SystemZAddressingMode::FormBDXNormal;
1409 DispRange = SystemZAddressingMode::Disp20Only;
Ulrich Weigand79564612016-06-09 15:19:16 +00001410 break;
Daniel Sanders2eeace22015-03-17 16:16:14 +00001411 }
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001412
1413 if (selectBDXAddr(Form, DispRange, Op, Base, Disp, Index)) {
Zhan Jun Liaucf2f4b32016-08-18 21:44:15 +00001414 const TargetRegisterClass *TRC =
1415 Subtarget->getRegisterInfo()->getPointerRegClass(*MF);
1416 SDLoc DL(Base);
1417 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), DL, MVT::i32);
1418
1419 // Make sure that the base address doesn't go into %r0.
1420 // If it's a TargetFrameIndex or a fixed register, we shouldn't do anything.
1421 if (Base.getOpcode() != ISD::TargetFrameIndex &&
1422 Base.getOpcode() != ISD::Register) {
1423 Base =
1424 SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
1425 DL, Base.getValueType(),
1426 Base, RC), 0);
1427 }
1428
1429 // Make sure that the index register isn't assigned to %r0 either.
1430 if (Index.getOpcode() != ISD::Register) {
1431 Index =
1432 SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
1433 DL, Index.getValueType(),
1434 Index, RC), 0);
1435 }
1436
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001437 OutOps.push_back(Base);
1438 OutOps.push_back(Disp);
1439 OutOps.push_back(Index);
1440 return false;
1441 }
1442
Daniel Sanders2eeace22015-03-17 16:16:14 +00001443 return true;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001444}
Ulrich Weigand849a59f2018-01-19 20:52:04 +00001445
1446namespace {
1447// Represents a sequence for extracting a 0/1 value from an IPM result:
1448// (((X ^ XORValue) + AddValue) >> Bit)
1449struct IPMConversion {
1450 IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit)
1451 : XORValue(xorValue), AddValue(addValue), Bit(bit) {}
1452
1453 int64_t XORValue;
1454 int64_t AddValue;
1455 unsigned Bit;
1456};
1457} // end anonymous namespace
1458
1459// Return a sequence for getting a 1 from an IPM result when CC has a
1460// value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1461// The handling of CC values outside CCValid doesn't matter.
1462static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1463 // Deal with cases where the result can be taken directly from a bit
1464 // of the IPM result.
1465 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1466 return IPMConversion(0, 0, SystemZ::IPM_CC);
1467 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1468 return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1469
1470 // Deal with cases where we can add a value to force the sign bit
1471 // to contain the right value. Putting the bit in 31 means we can
1472 // use SRL rather than RISBG(L), and also makes it easier to get a
1473 // 0/-1 value, so it has priority over the other tests below.
1474 //
1475 // These sequences rely on the fact that the upper two bits of the
1476 // IPM result are zero.
1477 uint64_t TopBit = uint64_t(1) << 31;
1478 if (CCMask == (CCValid & SystemZ::CCMASK_0))
1479 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1480 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1481 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1482 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1483 | SystemZ::CCMASK_1
1484 | SystemZ::CCMASK_2)))
1485 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1486 if (CCMask == (CCValid & SystemZ::CCMASK_3))
1487 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1488 if (CCMask == (CCValid & (SystemZ::CCMASK_1
1489 | SystemZ::CCMASK_2
1490 | SystemZ::CCMASK_3)))
1491 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1492
1493 // Next try inverting the value and testing a bit. 0/1 could be
1494 // handled this way too, but we dealt with that case above.
1495 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1496 return IPMConversion(-1, 0, SystemZ::IPM_CC);
1497
1498 // Handle cases where adding a value forces a non-sign bit to contain
1499 // the right value.
1500 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1501 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1502 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1503 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1504
1505 // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are
1506 // can be done by inverting the low CC bit and applying one of the
1507 // sign-based extractions above.
1508 if (CCMask == (CCValid & SystemZ::CCMASK_1))
1509 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1510 if (CCMask == (CCValid & SystemZ::CCMASK_2))
1511 return IPMConversion(1 << SystemZ::IPM_CC,
1512 TopBit - (3 << SystemZ::IPM_CC), 31);
1513 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1514 | SystemZ::CCMASK_1
1515 | SystemZ::CCMASK_3)))
1516 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1517 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1518 | SystemZ::CCMASK_2
1519 | SystemZ::CCMASK_3)))
1520 return IPMConversion(1 << SystemZ::IPM_CC,
1521 TopBit - (1 << SystemZ::IPM_CC), 31);
1522
1523 llvm_unreachable("Unexpected CC combination");
1524}
1525
1526SDValue SystemZDAGToDAGISel::expandSelectBoolean(SDNode *Node) {
1527 auto *TrueOp = dyn_cast<ConstantSDNode>(Node->getOperand(0));
1528 auto *FalseOp = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1529 if (!TrueOp || !FalseOp)
1530 return SDValue();
1531 if (FalseOp->getZExtValue() != 0)
1532 return SDValue();
1533 if (TrueOp->getSExtValue() != 1 && TrueOp->getSExtValue() != -1)
1534 return SDValue();
1535
1536 auto *CCValidOp = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1537 auto *CCMaskOp = dyn_cast<ConstantSDNode>(Node->getOperand(3));
1538 if (!CCValidOp || !CCMaskOp)
1539 return SDValue();
1540 int CCValid = CCValidOp->getZExtValue();
1541 int CCMask = CCMaskOp->getZExtValue();
1542
1543 SDLoc DL(Node);
1544 SDValue Glue = Node->getOperand(4);
1545 IPMConversion IPM = getIPMConversion(CCValid, CCMask);
1546 SDValue Result = CurDAG->getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
1547
1548 if (IPM.XORValue)
1549 Result = CurDAG->getNode(ISD::XOR, DL, MVT::i32, Result,
1550 CurDAG->getConstant(IPM.XORValue, DL, MVT::i32));
1551
1552 if (IPM.AddValue)
1553 Result = CurDAG->getNode(ISD::ADD, DL, MVT::i32, Result,
1554 CurDAG->getConstant(IPM.AddValue, DL, MVT::i32));
1555
1556 EVT VT = Node->getValueType(0);
1557 if (VT == MVT::i32 && IPM.Bit == 31) {
1558 unsigned ShiftOp = TrueOp->getSExtValue() == 1 ? ISD::SRL : ISD::SRA;
1559 Result = CurDAG->getNode(ShiftOp, DL, MVT::i32, Result,
1560 CurDAG->getConstant(IPM.Bit, DL, MVT::i32));
1561 } else {
1562 if (VT != MVT::i32)
1563 Result = CurDAG->getNode(ISD::ANY_EXTEND, DL, VT, Result);
1564
1565 if (TrueOp->getSExtValue() == 1) {
1566 // The SHR/AND sequence should get optimized to an RISBG.
1567 Result = CurDAG->getNode(ISD::SRL, DL, VT, Result,
1568 CurDAG->getConstant(IPM.Bit, DL, MVT::i32));
1569 Result = CurDAG->getNode(ISD::AND, DL, VT, Result,
1570 CurDAG->getConstant(1, DL, VT));
1571 } else {
1572 // Sign-extend from IPM.Bit using a pair of shifts.
1573 int ShlAmt = VT.getSizeInBits() - 1 - IPM.Bit;
1574 int SraAmt = VT.getSizeInBits() - 1;
1575 Result = CurDAG->getNode(ISD::SHL, DL, VT, Result,
1576 CurDAG->getConstant(ShlAmt, DL, MVT::i32));
1577 Result = CurDAG->getNode(ISD::SRA, DL, VT, Result,
1578 CurDAG->getConstant(SraAmt, DL, MVT::i32));
1579 }
1580 }
1581
1582 return Result;
1583}
1584
1585void SystemZDAGToDAGISel::PreprocessISelDAG() {
Ulrich Weigand426f6be2018-01-19 20:56:04 +00001586 // If we have conditional immediate loads, we always prefer
1587 // using those over an IPM sequence.
1588 if (Subtarget->hasLoadStoreOnCond2())
1589 return;
1590
Ulrich Weigand849a59f2018-01-19 20:52:04 +00001591 bool MadeChange = false;
1592
1593 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
1594 E = CurDAG->allnodes_end();
1595 I != E;) {
1596 SDNode *N = &*I++;
1597 if (N->use_empty())
1598 continue;
1599
1600 SDValue Res;
1601 switch (N->getOpcode()) {
1602 default: break;
1603 case SystemZISD::SELECT_CCMASK:
1604 Res = expandSelectBoolean(N);
1605 break;
1606 }
1607
1608 if (Res) {
1609 DEBUG(dbgs() << "SystemZ DAG preprocessing replacing:\nOld: ");
1610 DEBUG(N->dump(CurDAG));
1611 DEBUG(dbgs() << "\nNew: ");
1612 DEBUG(Res.getNode()->dump(CurDAG));
1613 DEBUG(dbgs() << "\n");
1614
1615 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Res);
1616 MadeChange = true;
1617 }
1618 }
1619
1620 if (MadeChange)
1621 CurDAG->RemoveDeadNodes();
1622}
1623