blob: 26af3f4ebcc00e346495c470b6f8508264e651e9 [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 Weigandb32f3652018-04-30 17:52:32 +0000354 bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const override;
Ulrich Weigand849a59f2018-01-19 20:52:04 +0000355 void PreprocessISelDAG() override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000356
357 // Include the pieces autogenerated from the target description.
358 #include "SystemZGenDAGISel.inc"
359};
360} // end anonymous namespace
361
362FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
363 CodeGenOpt::Level OptLevel) {
364 return new SystemZDAGToDAGISel(TM, OptLevel);
365}
366
367// Return true if Val should be selected as a displacement for an address
368// with range DR. Here we're interested in the range of both the instruction
369// described by DR and of any pairing instruction.
370static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
371 switch (DR) {
372 case SystemZAddressingMode::Disp12Only:
373 return isUInt<12>(Val);
374
375 case SystemZAddressingMode::Disp12Pair:
376 case SystemZAddressingMode::Disp20Only:
377 case SystemZAddressingMode::Disp20Pair:
378 return isInt<20>(Val);
379
380 case SystemZAddressingMode::Disp20Only128:
381 return isInt<20>(Val) && isInt<20>(Val + 8);
382 }
383 llvm_unreachable("Unhandled displacement range");
384}
385
386// Change the base or index in AM to Value, where IsBase selects
387// between the base and index.
388static void changeComponent(SystemZAddressingMode &AM, bool IsBase,
389 SDValue Value) {
390 if (IsBase)
391 AM.Base = Value;
392 else
393 AM.Index = Value;
394}
395
396// The base or index of AM is equivalent to Value + ADJDYNALLOC,
397// where IsBase selects between the base and index. Try to fold the
398// ADJDYNALLOC into AM.
399static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase,
400 SDValue Value) {
401 if (AM.isDynAlloc() && !AM.IncludesDynAlloc) {
402 changeComponent(AM, IsBase, Value);
403 AM.IncludesDynAlloc = true;
404 return true;
405 }
406 return false;
407}
408
409// The base of AM is equivalent to Base + Index. Try to use Index as
410// the index register.
411static bool expandIndex(SystemZAddressingMode &AM, SDValue Base,
412 SDValue Index) {
413 if (AM.hasIndexField() && !AM.Index.getNode()) {
414 AM.Base = Base;
415 AM.Index = Index;
416 return true;
417 }
418 return false;
419}
420
421// The base or index of AM is equivalent to Op0 + Op1, where IsBase selects
422// between the base and index. Try to fold Op1 into AM's displacement.
423static bool expandDisp(SystemZAddressingMode &AM, bool IsBase,
Richard Sandiford54b36912013-09-27 15:14:04 +0000424 SDValue Op0, uint64_t Op1) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000425 // First try adjusting the displacement.
Richard Sandiford54b36912013-09-27 15:14:04 +0000426 int64_t TestDisp = AM.Disp + Op1;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000427 if (selectDisp(AM.DR, TestDisp)) {
428 changeComponent(AM, IsBase, Op0);
429 AM.Disp = TestDisp;
430 return true;
431 }
432
433 // We could consider forcing the displacement into a register and
434 // using it as an index, but it would need to be carefully tuned.
435 return false;
436}
437
438bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM,
Richard Sandiford54b36912013-09-27 15:14:04 +0000439 bool IsBase) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000440 SDValue N = IsBase ? AM.Base : AM.Index;
441 unsigned Opcode = N.getOpcode();
442 if (Opcode == ISD::TRUNCATE) {
443 N = N.getOperand(0);
444 Opcode = N.getOpcode();
445 }
446 if (Opcode == ISD::ADD || CurDAG->isBaseWithConstantOffset(N)) {
447 SDValue Op0 = N.getOperand(0);
448 SDValue Op1 = N.getOperand(1);
449
450 unsigned Op0Code = Op0->getOpcode();
451 unsigned Op1Code = Op1->getOpcode();
452
453 if (Op0Code == SystemZISD::ADJDYNALLOC)
454 return expandAdjDynAlloc(AM, IsBase, Op1);
455 if (Op1Code == SystemZISD::ADJDYNALLOC)
456 return expandAdjDynAlloc(AM, IsBase, Op0);
457
458 if (Op0Code == ISD::Constant)
Richard Sandiford54b36912013-09-27 15:14:04 +0000459 return expandDisp(AM, IsBase, Op1,
460 cast<ConstantSDNode>(Op0)->getSExtValue());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000461 if (Op1Code == ISD::Constant)
Richard Sandiford54b36912013-09-27 15:14:04 +0000462 return expandDisp(AM, IsBase, Op0,
463 cast<ConstantSDNode>(Op1)->getSExtValue());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000464
465 if (IsBase && expandIndex(AM, Op0, Op1))
466 return true;
467 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000468 if (Opcode == SystemZISD::PCREL_OFFSET) {
469 SDValue Full = N.getOperand(0);
470 SDValue Base = N.getOperand(1);
471 SDValue Anchor = Base.getOperand(0);
472 uint64_t Offset = (cast<GlobalAddressSDNode>(Full)->getOffset() -
473 cast<GlobalAddressSDNode>(Anchor)->getOffset());
474 return expandDisp(AM, IsBase, Base, Offset);
475 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000476 return false;
477}
478
479// Return true if an instruction with displacement range DR should be
480// used for displacement value Val. selectDisp(DR, Val) must already hold.
481static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
482 assert(selectDisp(DR, Val) && "Invalid displacement");
483 switch (DR) {
484 case SystemZAddressingMode::Disp12Only:
485 case SystemZAddressingMode::Disp20Only:
486 case SystemZAddressingMode::Disp20Only128:
487 return true;
488
489 case SystemZAddressingMode::Disp12Pair:
490 // Use the other instruction if the displacement is too large.
491 return isUInt<12>(Val);
492
493 case SystemZAddressingMode::Disp20Pair:
494 // Use the other instruction if the displacement is small enough.
495 return !isUInt<12>(Val);
496 }
497 llvm_unreachable("Unhandled displacement range");
498}
499
500// Return true if Base + Disp + Index should be performed by LA(Y).
501static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index) {
502 // Don't use LA(Y) for constants.
503 if (!Base)
504 return false;
505
506 // Always use LA(Y) for frame addresses, since we know that the destination
507 // register is almost always (perhaps always) going to be different from
508 // the frame register.
509 if (Base->getOpcode() == ISD::FrameIndex)
510 return true;
511
512 if (Disp) {
513 // Always use LA(Y) if there is a base, displacement and index.
514 if (Index)
515 return true;
516
517 // Always use LA if the displacement is small enough. It should always
518 // be no worse than AGHI (and better if it avoids a move).
519 if (isUInt<12>(Disp))
520 return true;
521
522 // For similar reasons, always use LAY if the constant is too big for AGHI.
523 // LAY should be no worse than AGFI.
524 if (!isInt<16>(Disp))
525 return true;
526 } else {
527 // Don't use LA for plain registers.
528 if (!Index)
529 return false;
530
531 // Don't use LA for plain addition if the index operand is only used
532 // once. It should be a natural two-operand addition in that case.
533 if (Index->hasOneUse())
534 return false;
535
536 // Prefer addition if the second operation is sign-extended, in the
537 // hope of using AGF.
538 unsigned IndexOpcode = Index->getOpcode();
539 if (IndexOpcode == ISD::SIGN_EXTEND ||
540 IndexOpcode == ISD::SIGN_EXTEND_INREG)
541 return false;
542 }
543
544 // Don't use LA for two-operand addition if either operand is only
545 // used once. The addition instructions are better in that case.
546 if (Base->hasOneUse())
547 return false;
548
549 return true;
550}
551
552// Return true if Addr is suitable for AM, updating AM if so.
553bool SystemZDAGToDAGISel::selectAddress(SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000554 SystemZAddressingMode &AM) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000555 // Start out assuming that the address will need to be loaded separately,
556 // then try to extend it as much as we can.
557 AM.Base = Addr;
558
559 // First try treating the address as a constant.
560 if (Addr.getOpcode() == ISD::Constant &&
Richard Sandiford54b36912013-09-27 15:14:04 +0000561 expandDisp(AM, true, SDValue(),
562 cast<ConstantSDNode>(Addr)->getSExtValue()))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000563 ;
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +0000564 // Also see if it's a bare ADJDYNALLOC.
565 else if (Addr.getOpcode() == SystemZISD::ADJDYNALLOC &&
566 expandAdjDynAlloc(AM, true, SDValue()))
567 ;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000568 else
569 // Otherwise try expanding each component.
570 while (expandAddress(AM, true) ||
571 (AM.Index.getNode() && expandAddress(AM, false)))
572 continue;
573
574 // Reject cases where it isn't profitable to use LA(Y).
575 if (AM.Form == SystemZAddressingMode::FormBDXLA &&
576 !shouldUseLA(AM.Base.getNode(), AM.Disp, AM.Index.getNode()))
577 return false;
578
579 // Reject cases where the other instruction in a pair should be used.
580 if (!isValidDisp(AM.DR, AM.Disp))
581 return false;
582
583 // Make sure that ADJDYNALLOC is included where necessary.
584 if (AM.isDynAlloc() && !AM.IncludesDynAlloc)
585 return false;
586
587 DEBUG(AM.dump());
588 return true;
589}
590
591// Insert a node into the DAG at least before Pos. This will reposition
592// the node as needed, and will assign it a node ID that is <= Pos's ID.
593// Note that this does *not* preserve the uniqueness of node IDs!
594// The selection DAG must no longer depend on their uniqueness when this
595// function is used.
596static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N) {
Nirav Dave8c5f47a2018-03-22 19:32:07 +0000597 if (N->getNodeId() == -1 ||
598 (SelectionDAGISel::getUninvalidatedNodeId(N.getNode()) >
599 SelectionDAGISel::getUninvalidatedNodeId(Pos))) {
Duncan P. N. Exon Smitha2c90e42015-10-20 01:12:46 +0000600 DAG->RepositionNode(Pos->getIterator(), N.getNode());
Nirav Dave3264c1b2018-03-19 20:19:46 +0000601 // Mark Node as invalid for pruning as after this it may be a successor to a
602 // selected node but otherwise be in the same position of Pos.
603 // Conservatively mark it with the same -abs(Id) to assure node id
604 // invariant is preserved.
Nirav Dave8c5f47a2018-03-22 19:32:07 +0000605 N->setNodeId(Pos->getNodeId());
606 SelectionDAGISel::InvalidateNodeId(N.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000607 }
608}
609
610void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
611 EVT VT, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000612 SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000613 Base = AM.Base;
614 if (!Base.getNode())
615 // Register 0 means "no base". This is mostly useful for shifts.
616 Base = CurDAG->getRegister(0, VT);
617 else if (Base.getOpcode() == ISD::FrameIndex) {
618 // Lower a FrameIndex to a TargetFrameIndex.
619 int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex();
620 Base = CurDAG->getTargetFrameIndex(FrameIndex, VT);
621 } else if (Base.getValueType() != VT) {
622 // Truncate values from i64 to i32, for shifts.
623 assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 &&
624 "Unexpected truncation");
Andrew Trickef9de2a2013-05-25 02:42:55 +0000625 SDLoc DL(Base);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000626 SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base);
627 insertDAGNode(CurDAG, Base.getNode(), Trunc);
628 Base = Trunc;
629 }
630
631 // Lower the displacement to a TargetConstant.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000632 Disp = CurDAG->getTargetConstant(AM.Disp, SDLoc(Base), VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000633}
634
635void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
636 EVT VT, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000637 SDValue &Disp,
638 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000639 getAddressOperands(AM, VT, Base, Disp);
640
641 Index = AM.Index;
642 if (!Index.getNode())
643 // Register 0 means "no index".
644 Index = CurDAG->getRegister(0, VT);
645}
646
647bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR,
648 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000649 SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000650 SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR);
651 if (!selectAddress(Addr, AM))
652 return false;
653
654 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
655 return true;
656}
657
Richard Sandiforda481f582013-08-23 11:18:53 +0000658bool SystemZDAGToDAGISel::selectMVIAddr(SystemZAddressingMode::DispRange DR,
659 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000660 SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000661 SystemZAddressingMode AM(SystemZAddressingMode::FormBDXNormal, DR);
662 if (!selectAddress(Addr, AM) || AM.Index.getNode())
663 return false;
664
665 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
666 return true;
667}
668
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000669bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form,
670 SystemZAddressingMode::DispRange DR,
671 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000672 SDValue &Disp, SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000673 SystemZAddressingMode AM(Form, DR);
674 if (!selectAddress(Addr, AM))
675 return false;
676
677 getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index);
678 return true;
679}
680
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000681bool SystemZDAGToDAGISel::selectBDVAddr12Only(SDValue Addr, SDValue Elem,
682 SDValue &Base,
683 SDValue &Disp,
684 SDValue &Index) const {
685 SDValue Regs[2];
686 if (selectBDXAddr12Only(Addr, Regs[0], Disp, Regs[1]) &&
687 Regs[0].getNode() && Regs[1].getNode()) {
688 for (unsigned int I = 0; I < 2; ++I) {
689 Base = Regs[I];
690 Index = Regs[1 - I];
691 // We can't tell here whether the index vector has the right type
692 // for the access; the caller needs to do that instead.
693 if (Index.getOpcode() == ISD::ZERO_EXTEND)
694 Index = Index.getOperand(0);
695 if (Index.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
696 Index.getOperand(1) == Elem) {
697 Index = Index.getOperand(0);
698 return true;
699 }
700 }
701 }
702 return false;
703}
704
Richard Sandiford885140c2013-07-16 11:55:57 +0000705bool SystemZDAGToDAGISel::detectOrAndInsertion(SDValue &Op,
Richard Sandiford54b36912013-09-27 15:14:04 +0000706 uint64_t InsertMask) const {
Richard Sandiford885140c2013-07-16 11:55:57 +0000707 // We're only interested in cases where the insertion is into some operand
708 // of Op, rather than into Op itself. The only useful case is an AND.
709 if (Op.getOpcode() != ISD::AND)
710 return false;
711
712 // We need a constant mask.
Richard Sandiford21f5d682014-03-06 11:22:58 +0000713 auto *MaskNode = dyn_cast<ConstantSDNode>(Op.getOperand(1).getNode());
Richard Sandiford885140c2013-07-16 11:55:57 +0000714 if (!MaskNode)
715 return false;
716
717 // It's not an insertion of Op.getOperand(0) if the two masks overlap.
718 uint64_t AndMask = MaskNode->getZExtValue();
719 if (InsertMask & AndMask)
720 return false;
721
722 // It's only an insertion if all bits are covered or are known to be zero.
723 // The inner check covers all cases but is more expensive.
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000724 uint64_t Used = allOnes(Op.getValueSizeInBits());
Richard Sandiford885140c2013-07-16 11:55:57 +0000725 if (Used != (AndMask | InsertMask)) {
Craig Topperd0af7e82017-04-28 05:31:46 +0000726 KnownBits Known;
727 CurDAG->computeKnownBits(Op.getOperand(0), Known);
728 if (Used != (AndMask | InsertMask | Known.Zero.getZExtValue()))
Richard Sandiford885140c2013-07-16 11:55:57 +0000729 return false;
730 }
731
732 Op = Op.getOperand(0);
733 return true;
734}
735
Richard Sandiford54b36912013-09-27 15:14:04 +0000736bool SystemZDAGToDAGISel::refineRxSBGMask(RxSBGOperands &RxSBG,
737 uint64_t Mask) const {
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000738 const SystemZInstrInfo *TII = getInstrInfo();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000739 if (RxSBG.Rotate != 0)
740 Mask = (Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate));
741 Mask &= RxSBG.Mask;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000742 if (TII->isRxSBGMask(Mask, RxSBG.BitSize, RxSBG.Start, RxSBG.End)) {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000743 RxSBG.Mask = Mask;
Richard Sandiford5cbac962013-07-18 09:45:08 +0000744 return true;
745 }
Richard Sandiford84f54a32013-07-11 08:59:12 +0000746 return false;
747}
748
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000749// Return true if any bits of (RxSBG.Input & Mask) are significant.
750static bool maskMatters(RxSBGOperands &RxSBG, uint64_t Mask) {
751 // Rotate the mask in the same way as RxSBG.Input is rotated.
Richard Sandiford297f7d22013-07-18 10:14:55 +0000752 if (RxSBG.Rotate != 0)
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000753 Mask = ((Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate)));
754 return (Mask & RxSBG.Mask) != 0;
Richard Sandiford297f7d22013-07-18 10:14:55 +0000755}
756
Richard Sandiford54b36912013-09-27 15:14:04 +0000757bool SystemZDAGToDAGISel::expandRxSBG(RxSBGOperands &RxSBG) const {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000758 SDValue N = RxSBG.Input;
Richard Sandiford297f7d22013-07-18 10:14:55 +0000759 unsigned Opcode = N.getOpcode();
760 switch (Opcode) {
Zhan Jun Liau0df35052016-06-22 16:16:27 +0000761 case ISD::TRUNCATE: {
762 if (RxSBG.Opcode == SystemZ::RNSBG)
763 return false;
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000764 uint64_t BitSize = N.getValueSizeInBits();
Zhan Jun Liau0df35052016-06-22 16:16:27 +0000765 uint64_t Mask = allOnes(BitSize);
766 if (!refineRxSBGMask(RxSBG, Mask))
767 return false;
768 RxSBG.Input = N.getOperand(0);
769 return true;
770 }
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000771 case ISD::AND: {
Richard Sandiford51093212013-07-18 10:40:35 +0000772 if (RxSBG.Opcode == SystemZ::RNSBG)
773 return false;
774
Richard Sandiford21f5d682014-03-06 11:22:58 +0000775 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000776 if (!MaskNode)
777 return false;
778
779 SDValue Input = N.getOperand(0);
780 uint64_t Mask = MaskNode->getZExtValue();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000781 if (!refineRxSBGMask(RxSBG, Mask)) {
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000782 // If some bits of Input are already known zeros, those bits will have
783 // been removed from the mask. See if adding them back in makes the
784 // mask suitable.
Craig Topperd0af7e82017-04-28 05:31:46 +0000785 KnownBits Known;
786 CurDAG->computeKnownBits(Input, Known);
787 Mask |= Known.Zero.getZExtValue();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000788 if (!refineRxSBGMask(RxSBG, Mask))
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000789 return false;
790 }
Richard Sandiford5cbac962013-07-18 09:45:08 +0000791 RxSBG.Input = Input;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000792 return true;
793 }
794
Richard Sandiford51093212013-07-18 10:40:35 +0000795 case ISD::OR: {
796 if (RxSBG.Opcode != SystemZ::RNSBG)
797 return false;
798
Richard Sandiford21f5d682014-03-06 11:22:58 +0000799 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford51093212013-07-18 10:40:35 +0000800 if (!MaskNode)
801 return false;
802
803 SDValue Input = N.getOperand(0);
804 uint64_t Mask = ~MaskNode->getZExtValue();
805 if (!refineRxSBGMask(RxSBG, Mask)) {
806 // If some bits of Input are already known ones, those bits will have
807 // been removed from the mask. See if adding them back in makes the
808 // mask suitable.
Craig Topperd0af7e82017-04-28 05:31:46 +0000809 KnownBits Known;
810 CurDAG->computeKnownBits(Input, Known);
811 Mask &= ~Known.One.getZExtValue();
Richard Sandiford51093212013-07-18 10:40:35 +0000812 if (!refineRxSBGMask(RxSBG, Mask))
813 return false;
814 }
815 RxSBG.Input = Input;
816 return true;
817 }
818
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000819 case ISD::ROTL: {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000820 // Any 64-bit rotate left can be merged into the RxSBG.
Richard Sandiford3e382972013-10-16 13:35:13 +0000821 if (RxSBG.BitSize != 64 || N.getValueType() != MVT::i64)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000822 return false;
Richard Sandiford21f5d682014-03-06 11:22:58 +0000823 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000824 if (!CountNode)
825 return false;
826
Richard Sandiford5cbac962013-07-18 09:45:08 +0000827 RxSBG.Rotate = (RxSBG.Rotate + CountNode->getZExtValue()) & 63;
828 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000829 return true;
830 }
Simon Pilgrim0750c842015-08-15 13:27:30 +0000831
Richard Sandiford220ee492013-12-20 11:49:48 +0000832 case ISD::ANY_EXTEND:
833 // Bits above the extended operand are don't-care.
834 RxSBG.Input = N.getOperand(0);
835 return true;
836
Richard Sandiford3875cb62014-01-09 11:28:53 +0000837 case ISD::ZERO_EXTEND:
838 if (RxSBG.Opcode != SystemZ::RNSBG) {
839 // Restrict the mask to the extended operand.
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000840 unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits();
Richard Sandiford3875cb62014-01-09 11:28:53 +0000841 if (!refineRxSBGMask(RxSBG, allOnes(InnerBitSize)))
842 return false;
Richard Sandiford220ee492013-12-20 11:49:48 +0000843
Richard Sandiford3875cb62014-01-09 11:28:53 +0000844 RxSBG.Input = N.getOperand(0);
845 return true;
846 }
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000847 LLVM_FALLTHROUGH;
Simon Pilgrim0750c842015-08-15 13:27:30 +0000848
Richard Sandiford220ee492013-12-20 11:49:48 +0000849 case ISD::SIGN_EXTEND: {
Richard Sandiford3e382972013-10-16 13:35:13 +0000850 // Check that the extension bits are don't-care (i.e. are masked out
851 // by the final mask).
Jonas Paulsson19380ba2017-12-06 13:53:24 +0000852 unsigned BitSize = N.getValueSizeInBits();
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000853 unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits();
Jonas Paulsson19380ba2017-12-06 13:53:24 +0000854 if (maskMatters(RxSBG, allOnes(BitSize) - allOnes(InnerBitSize))) {
855 // In the case where only the sign bit is active, increase Rotate with
856 // the extension width.
857 if (RxSBG.Mask == 1 && RxSBG.Rotate == 1)
858 RxSBG.Rotate += (BitSize - InnerBitSize);
859 else
860 return false;
861 }
Richard Sandiford3e382972013-10-16 13:35:13 +0000862
863 RxSBG.Input = N.getOperand(0);
864 return true;
865 }
866
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000867 case ISD::SHL: {
Richard Sandiford21f5d682014-03-06 11:22:58 +0000868 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000869 if (!CountNode)
870 return false;
871
872 uint64_t Count = CountNode->getZExtValue();
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000873 unsigned BitSize = N.getValueSizeInBits();
Richard Sandiford3e382972013-10-16 13:35:13 +0000874 if (Count < 1 || Count >= BitSize)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000875 return false;
876
Richard Sandiford51093212013-07-18 10:40:35 +0000877 if (RxSBG.Opcode == SystemZ::RNSBG) {
878 // Treat (shl X, count) as (rotl X, size-count) as long as the bottom
879 // count bits from RxSBG.Input are ignored.
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000880 if (maskMatters(RxSBG, allOnes(Count)))
Richard Sandiford51093212013-07-18 10:40:35 +0000881 return false;
882 } else {
883 // Treat (shl X, count) as (and (rotl X, count), ~0<<count).
Richard Sandiford3e382972013-10-16 13:35:13 +0000884 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count) << Count))
Richard Sandiford51093212013-07-18 10:40:35 +0000885 return false;
886 }
887
Richard Sandiford5cbac962013-07-18 09:45:08 +0000888 RxSBG.Rotate = (RxSBG.Rotate + Count) & 63;
889 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000890 return true;
891 }
892
Richard Sandiford297f7d22013-07-18 10:14:55 +0000893 case ISD::SRL:
894 case ISD::SRA: {
Richard Sandiford21f5d682014-03-06 11:22:58 +0000895 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000896 if (!CountNode)
897 return false;
898
899 uint64_t Count = CountNode->getZExtValue();
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000900 unsigned BitSize = N.getValueSizeInBits();
Richard Sandiford3e382972013-10-16 13:35:13 +0000901 if (Count < 1 || Count >= BitSize)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000902 return false;
903
Richard Sandiford51093212013-07-18 10:40:35 +0000904 if (RxSBG.Opcode == SystemZ::RNSBG || Opcode == ISD::SRA) {
905 // Treat (srl|sra X, count) as (rotl X, size-count) as long as the top
906 // count bits from RxSBG.Input are ignored.
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000907 if (maskMatters(RxSBG, allOnes(Count) << (BitSize - Count)))
Richard Sandiford297f7d22013-07-18 10:14:55 +0000908 return false;
909 } else {
910 // Treat (srl X, count), mask) as (and (rotl X, size-count), ~0>>count),
911 // which is similar to SLL above.
Richard Sandiford3e382972013-10-16 13:35:13 +0000912 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count)))
Richard Sandiford297f7d22013-07-18 10:14:55 +0000913 return false;
914 }
915
Richard Sandiford5cbac962013-07-18 09:45:08 +0000916 RxSBG.Rotate = (RxSBG.Rotate - Count) & 63;
917 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000918 return true;
919 }
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000920 default:
921 return false;
922 }
923}
924
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000925SDValue SystemZDAGToDAGISel::getUNDEF(const SDLoc &DL, EVT VT) const {
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000926 SDNode *N = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000927 return SDValue(N, 0);
928}
929
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000930SDValue SystemZDAGToDAGISel::convertTo(const SDLoc &DL, EVT VT,
931 SDValue N) const {
Richard Sandifordd8163202013-09-13 09:12:44 +0000932 if (N.getValueType() == MVT::i32 && VT == MVT::i64)
Richard Sandiford87a44362013-09-30 10:28:35 +0000933 return CurDAG->getTargetInsertSubreg(SystemZ::subreg_l32,
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000934 DL, VT, getUNDEF(DL, MVT::i64), N);
Richard Sandifordd8163202013-09-13 09:12:44 +0000935 if (N.getValueType() == MVT::i64 && VT == MVT::i32)
Richard Sandiford87a44362013-09-30 10:28:35 +0000936 return CurDAG->getTargetExtractSubreg(SystemZ::subreg_l32, DL, VT, N);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000937 assert(N.getValueType() == VT && "Unexpected value types");
938 return N;
939}
940
Justin Bognerbbcd2232016-05-10 21:11:26 +0000941bool SystemZDAGToDAGISel::tryRISBGZero(SDNode *N) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000942 SDLoc DL(N);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000943 EVT VT = N->getValueType(0);
Ulrich Weigand77884bc2015-06-25 11:52:36 +0000944 if (!VT.isInteger() || VT.getSizeInBits() > 64)
Justin Bognerbbcd2232016-05-10 21:11:26 +0000945 return false;
Richard Sandiford51093212013-07-18 10:40:35 +0000946 RxSBGOperands RISBG(SystemZ::RISBG, SDValue(N, 0));
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000947 unsigned Count = 0;
Richard Sandiford5cbac962013-07-18 09:45:08 +0000948 while (expandRxSBG(RISBG))
Zhan Jun Liau0df35052016-06-22 16:16:27 +0000949 // The widening or narrowing is expected to be free.
950 // Counting widening or narrowing as a saved operation will result in
951 // preferring an R*SBG over a simple shift/logical instruction.
952 if (RISBG.Input.getOpcode() != ISD::ANY_EXTEND &&
953 RISBG.Input.getOpcode() != ISD::TRUNCATE)
Richard Sandiford3e382972013-10-16 13:35:13 +0000954 Count += 1;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000955 if (Count == 0)
Justin Bognerbbcd2232016-05-10 21:11:26 +0000956 return false;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000957
Ulrich Weigand5dc7b672016-11-11 12:43:51 +0000958 // Prefer to use normal shift instructions over RISBG, since they can handle
959 // all cases and are sometimes shorter.
960 if (Count == 1 && N->getOpcode() != ISD::AND)
961 return false;
962
963 // Prefer register extensions like LLC over RISBG. Also prefer to start
964 // out with normal ANDs if one instruction would be enough. We can convert
965 // these ANDs into an RISBG later if a three-address instruction is useful.
966 if (RISBG.Rotate == 0) {
967 bool PreferAnd = false;
968 // Prefer AND for any 32-bit and-immediate operation.
969 if (VT == MVT::i32)
970 PreferAnd = true;
971 // As well as for any 64-bit operation that can be implemented via LLC(R),
972 // LLH(R), LLGT(R), or one of the and-immediate instructions.
973 else if (RISBG.Mask == 0xff ||
974 RISBG.Mask == 0xffff ||
975 RISBG.Mask == 0x7fffffff ||
976 SystemZ::isImmLF(~RISBG.Mask) ||
977 SystemZ::isImmHF(~RISBG.Mask))
978 PreferAnd = true;
Ulrich Weigand92c2c672016-11-11 12:46:28 +0000979 // And likewise for the LLZRGF instruction, which doesn't have a register
980 // to register version.
981 else if (auto *Load = dyn_cast<LoadSDNode>(RISBG.Input)) {
982 if (Load->getMemoryVT() == MVT::i32 &&
983 (Load->getExtensionType() == ISD::EXTLOAD ||
984 Load->getExtensionType() == ISD::ZEXTLOAD) &&
985 RISBG.Mask == 0xffffff00 &&
986 Subtarget->hasLoadAndZeroRightmostByte())
987 PreferAnd = true;
988 }
Ulrich Weigand5dc7b672016-11-11 12:43:51 +0000989 if (PreferAnd) {
990 // Replace the current node with an AND. Note that the current node
991 // might already be that same AND, in which case it is already CSE'd
992 // with it, and we must not call ReplaceNode.
993 SDValue In = convertTo(DL, VT, RISBG.Input);
994 SDValue Mask = CurDAG->getConstant(RISBG.Mask, DL, VT);
995 SDValue New = CurDAG->getNode(ISD::AND, DL, VT, In, Mask);
996 if (N != New.getNode()) {
997 insertDAGNode(CurDAG, N, Mask);
998 insertDAGNode(CurDAG, N, New);
999 ReplaceNode(N, New.getNode());
1000 N = New.getNode();
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001001 }
Ulrich Weigand5dc7b672016-11-11 12:43:51 +00001002 // Now, select the machine opcode to implement this operation.
Jonas Paulssonf268cd02018-02-27 07:53:23 +00001003 if (!N->isMachineOpcode())
1004 SelectCode(N);
Ulrich Weigand5dc7b672016-11-11 12:43:51 +00001005 return true;
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001006 }
Simon Pilgrim0750c842015-08-15 13:27:30 +00001007 }
1008
Richard Sandiford3ad5a152013-10-01 14:36:20 +00001009 unsigned Opcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001010 // Prefer RISBGN if available, since it does not clobber CC.
1011 if (Subtarget->hasMiscellaneousExtensions())
1012 Opcode = SystemZ::RISBGN;
Richard Sandiford3ad5a152013-10-01 14:36:20 +00001013 EVT OpcodeVT = MVT::i64;
Ulrich Weigand55b85902017-11-14 19:20:46 +00001014 if (VT == MVT::i32 && Subtarget->hasHighWord() &&
1015 // We can only use the 32-bit instructions if all source bits are
1016 // in the low 32 bits without wrapping, both after rotation (because
1017 // of the smaller range for Start and End) and before rotation
1018 // (because the input value is truncated).
1019 RISBG.Start >= 32 && RISBG.End >= RISBG.Start &&
1020 ((RISBG.Start + RISBG.Rotate) & 63) >= 32 &&
1021 ((RISBG.End + RISBG.Rotate) & 63) >=
1022 ((RISBG.Start + RISBG.Rotate) & 63)) {
Richard Sandiford3ad5a152013-10-01 14:36:20 +00001023 Opcode = SystemZ::RISBMux;
1024 OpcodeVT = MVT::i32;
1025 RISBG.Start &= 31;
1026 RISBG.End &= 31;
1027 }
Richard Sandiford84f54a32013-07-11 08:59:12 +00001028 SDValue Ops[5] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001029 getUNDEF(DL, OpcodeVT),
1030 convertTo(DL, OpcodeVT, RISBG.Input),
1031 CurDAG->getTargetConstant(RISBG.Start, DL, MVT::i32),
1032 CurDAG->getTargetConstant(RISBG.End | 128, DL, MVT::i32),
1033 CurDAG->getTargetConstant(RISBG.Rotate, DL, MVT::i32)
Richard Sandiford84f54a32013-07-11 08:59:12 +00001034 };
Justin Bognerbbcd2232016-05-10 21:11:26 +00001035 SDValue New = convertTo(
1036 DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, OpcodeVT, Ops), 0));
Nirav Dave3264c1b2018-03-19 20:19:46 +00001037 ReplaceNode(N, New.getNode());
Justin Bognerbbcd2232016-05-10 21:11:26 +00001038 return true;
Richard Sandiford84f54a32013-07-11 08:59:12 +00001039}
1040
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001041bool SystemZDAGToDAGISel::tryRxSBG(SDNode *N, unsigned Opcode) {
Ulrich Weigand77884bc2015-06-25 11:52:36 +00001042 SDLoc DL(N);
1043 EVT VT = N->getValueType(0);
1044 if (!VT.isInteger() || VT.getSizeInBits() > 64)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001045 return false;
Richard Sandiford7878b852013-07-18 10:06:15 +00001046 // Try treating each operand of N as the second operand of the RxSBG
Richard Sandiford885140c2013-07-16 11:55:57 +00001047 // and see which goes deepest.
Richard Sandiford51093212013-07-18 10:40:35 +00001048 RxSBGOperands RxSBG[] = {
1049 RxSBGOperands(Opcode, N->getOperand(0)),
1050 RxSBGOperands(Opcode, N->getOperand(1))
1051 };
Richard Sandiford885140c2013-07-16 11:55:57 +00001052 unsigned Count[] = { 0, 0 };
1053 for (unsigned I = 0; I < 2; ++I)
Richard Sandiford5cbac962013-07-18 09:45:08 +00001054 while (expandRxSBG(RxSBG[I]))
Zhan Jun Liau0df35052016-06-22 16:16:27 +00001055 // The widening or narrowing is expected to be free.
1056 // Counting widening or narrowing as a saved operation will result in
1057 // preferring an R*SBG over a simple shift/logical instruction.
1058 if (RxSBG[I].Input.getOpcode() != ISD::ANY_EXTEND &&
1059 RxSBG[I].Input.getOpcode() != ISD::TRUNCATE)
Richard Sandiford3e382972013-10-16 13:35:13 +00001060 Count[I] += 1;
Richard Sandiford885140c2013-07-16 11:55:57 +00001061
1062 // Do nothing if neither operand is suitable.
1063 if (Count[0] == 0 && Count[1] == 0)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001064 return false;
Richard Sandiford885140c2013-07-16 11:55:57 +00001065
1066 // Pick the deepest second operand.
1067 unsigned I = Count[0] > Count[1] ? 0 : 1;
1068 SDValue Op0 = N->getOperand(I ^ 1);
1069
1070 // Prefer IC for character insertions from memory.
Richard Sandiford7878b852013-07-18 10:06:15 +00001071 if (Opcode == SystemZ::ROSBG && (RxSBG[I].Mask & 0xff) == 0)
Richard Sandiford21f5d682014-03-06 11:22:58 +00001072 if (auto *Load = dyn_cast<LoadSDNode>(Op0.getNode()))
Richard Sandiford885140c2013-07-16 11:55:57 +00001073 if (Load->getMemoryVT() == MVT::i8)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001074 return false;
Richard Sandiford885140c2013-07-16 11:55:57 +00001075
1076 // See whether we can avoid an AND in the first operand by converting
1077 // ROSBG to RISBG.
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001078 if (Opcode == SystemZ::ROSBG && detectOrAndInsertion(Op0, RxSBG[I].Mask)) {
Richard Sandiford885140c2013-07-16 11:55:57 +00001079 Opcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001080 // Prefer RISBGN if available, since it does not clobber CC.
1081 if (Subtarget->hasMiscellaneousExtensions())
1082 Opcode = SystemZ::RISBGN;
1083 }
1084
Richard Sandiford885140c2013-07-16 11:55:57 +00001085 SDValue Ops[5] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001086 convertTo(DL, MVT::i64, Op0),
1087 convertTo(DL, MVT::i64, RxSBG[I].Input),
1088 CurDAG->getTargetConstant(RxSBG[I].Start, DL, MVT::i32),
1089 CurDAG->getTargetConstant(RxSBG[I].End, DL, MVT::i32),
1090 CurDAG->getTargetConstant(RxSBG[I].Rotate, DL, MVT::i32)
Richard Sandiford885140c2013-07-16 11:55:57 +00001091 };
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001092 SDValue New = convertTo(
1093 DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, MVT::i64, Ops), 0));
1094 ReplaceNode(N, New.getNode());
1095 return true;
Richard Sandiford885140c2013-07-16 11:55:57 +00001096}
1097
Justin Bognerffb273d2016-05-09 23:54:23 +00001098void SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node,
1099 SDValue Op0, uint64_t UpperVal,
1100 uint64_t LowerVal) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001101 EVT VT = Node->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001102 SDLoc DL(Node);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001103 SDValue Upper = CurDAG->getConstant(UpperVal, DL, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001104 if (Op0.getNode())
1105 Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper);
Justin Bognerffb273d2016-05-09 23:54:23 +00001106
1107 {
1108 // When we haven't passed in Op0, Upper will be a constant. In order to
1109 // prevent folding back to the large immediate in `Or = getNode(...)` we run
1110 // SelectCode first and end up with an opaque machine node. This means that
1111 // we need to use a handle to keep track of Upper in case it gets CSE'd by
1112 // SelectCode.
1113 //
1114 // Note that in the case where Op0 is passed in we could just call
1115 // SelectCode(Upper) later, along with the SelectCode(Or), and avoid needing
1116 // the handle at all, but it's fine to do it here.
1117 //
1118 // TODO: This is a pretty hacky way to do this. Can we do something that
1119 // doesn't require a two paragraph explanation?
1120 HandleSDNode Handle(Upper);
1121 SelectCode(Upper.getNode());
1122 Upper = Handle.getValue();
1123 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001124
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001125 SDValue Lower = CurDAG->getConstant(LowerVal, DL, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001126 SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower);
Justin Bognerffb273d2016-05-09 23:54:23 +00001127
Nirav Dave3264c1b2018-03-19 20:19:46 +00001128 ReplaceNode(Node, Or.getNode());
Justin Bognerffb273d2016-05-09 23:54:23 +00001129
1130 SelectCode(Or.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001131}
1132
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001133bool SystemZDAGToDAGISel::tryGather(SDNode *N, unsigned Opcode) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001134 SDValue ElemV = N->getOperand(2);
1135 auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
1136 if (!ElemN)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001137 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001138
1139 unsigned Elem = ElemN->getZExtValue();
1140 EVT VT = N->getValueType(0);
1141 if (Elem >= VT.getVectorNumElements())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001142 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001143
1144 auto *Load = dyn_cast<LoadSDNode>(N->getOperand(1));
1145 if (!Load || !Load->hasOneUse())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001146 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001147 if (Load->getMemoryVT().getSizeInBits() !=
1148 Load->getValueType(0).getSizeInBits())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001149 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001150
1151 SDValue Base, Disp, Index;
1152 if (!selectBDVAddr12Only(Load->getBasePtr(), ElemV, Base, Disp, Index) ||
1153 Index.getValueType() != VT.changeVectorElementTypeToInteger())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001154 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001155
1156 SDLoc DL(Load);
1157 SDValue Ops[] = {
1158 N->getOperand(0), Base, Disp, Index,
1159 CurDAG->getTargetConstant(Elem, DL, MVT::i32), Load->getChain()
1160 };
1161 SDNode *Res = CurDAG->getMachineNode(Opcode, DL, VT, MVT::Other, Ops);
1162 ReplaceUses(SDValue(Load, 1), SDValue(Res, 1));
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001163 ReplaceNode(N, Res);
1164 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001165}
1166
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001167bool SystemZDAGToDAGISel::tryScatter(StoreSDNode *Store, unsigned Opcode) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001168 SDValue Value = Store->getValue();
1169 if (Value.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001170 return false;
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00001171 if (Store->getMemoryVT().getSizeInBits() != Value.getValueSizeInBits())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001172 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001173
1174 SDValue ElemV = Value.getOperand(1);
1175 auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
1176 if (!ElemN)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001177 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001178
1179 SDValue Vec = Value.getOperand(0);
1180 EVT VT = Vec.getValueType();
1181 unsigned Elem = ElemN->getZExtValue();
1182 if (Elem >= VT.getVectorNumElements())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001183 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001184
1185 SDValue Base, Disp, Index;
1186 if (!selectBDVAddr12Only(Store->getBasePtr(), ElemV, Base, Disp, Index) ||
1187 Index.getValueType() != VT.changeVectorElementTypeToInteger())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001188 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001189
1190 SDLoc DL(Store);
1191 SDValue Ops[] = {
1192 Vec, Base, Disp, Index, CurDAG->getTargetConstant(Elem, DL, MVT::i32),
1193 Store->getChain()
1194 };
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001195 ReplaceNode(Store, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops));
1196 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001197}
1198
Richard Sandiford067817e2013-09-27 15:29:20 +00001199bool SystemZDAGToDAGISel::canUseBlockOperation(StoreSDNode *Store,
1200 LoadSDNode *Load) const {
Richard Sandiford178273a2013-09-05 10:36:45 +00001201 // Check that the two memory operands have the same size.
1202 if (Load->getMemoryVT() != Store->getMemoryVT())
Richard Sandiford97846492013-07-09 09:46:39 +00001203 return false;
1204
Richard Sandiford178273a2013-09-05 10:36:45 +00001205 // Volatility stops an access from being decomposed.
1206 if (Load->isVolatile() || Store->isVolatile())
1207 return false;
Richard Sandiford97846492013-07-09 09:46:39 +00001208
1209 // There's no chance of overlap if the load is invariant.
Justin Lebaradbf09e2016-09-11 01:38:58 +00001210 if (Load->isInvariant() && Load->isDereferenceable())
Richard Sandiford97846492013-07-09 09:46:39 +00001211 return true;
1212
Richard Sandiford97846492013-07-09 09:46:39 +00001213 // Otherwise we need to check whether there's an alias.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001214 const Value *V1 = Load->getMemOperand()->getValue();
1215 const Value *V2 = Store->getMemOperand()->getValue();
Richard Sandiford97846492013-07-09 09:46:39 +00001216 if (!V1 || !V2)
1217 return false;
1218
Richard Sandiford067817e2013-09-27 15:29:20 +00001219 // Reject equality.
1220 uint64_t Size = Load->getMemoryVT().getStoreSize();
Richard Sandiford97846492013-07-09 09:46:39 +00001221 int64_t End1 = Load->getSrcValueOffset() + Size;
1222 int64_t End2 = Store->getSrcValueOffset() + Size;
Richard Sandiford067817e2013-09-27 15:29:20 +00001223 if (V1 == V2 && End1 == End2)
1224 return false;
1225
Chandler Carruthac80dc72015-06-17 07:18:54 +00001226 return !AA->alias(MemoryLocation(V1, End1, Load->getAAInfo()),
1227 MemoryLocation(V2, End2, Store->getAAInfo()));
Richard Sandiford97846492013-07-09 09:46:39 +00001228}
1229
Richard Sandiford178273a2013-09-05 10:36:45 +00001230bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001231 auto *Store = cast<StoreSDNode>(N);
1232 auto *Load = cast<LoadSDNode>(Store->getValue());
Richard Sandiford178273a2013-09-05 10:36:45 +00001233
1234 // Prefer not to use MVC if either address can use ... RELATIVE LONG
1235 // instructions.
1236 uint64_t Size = Load->getMemoryVT().getStoreSize();
1237 if (Size > 1 && Size <= 8) {
1238 // Prefer LHRL, LRL and LGRL.
Richard Sandiford54b36912013-09-27 15:14:04 +00001239 if (SystemZISD::isPCREL(Load->getBasePtr().getOpcode()))
Richard Sandiford178273a2013-09-05 10:36:45 +00001240 return false;
1241 // Prefer STHRL, STRL and STGRL.
Richard Sandiford54b36912013-09-27 15:14:04 +00001242 if (SystemZISD::isPCREL(Store->getBasePtr().getOpcode()))
Richard Sandiford178273a2013-09-05 10:36:45 +00001243 return false;
1244 }
1245
Richard Sandiford067817e2013-09-27 15:29:20 +00001246 return canUseBlockOperation(Store, Load);
Richard Sandiford178273a2013-09-05 10:36:45 +00001247}
1248
1249bool SystemZDAGToDAGISel::storeLoadCanUseBlockBinary(SDNode *N,
1250 unsigned I) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001251 auto *StoreA = cast<StoreSDNode>(N);
1252 auto *LoadA = cast<LoadSDNode>(StoreA->getValue().getOperand(1 - I));
1253 auto *LoadB = cast<LoadSDNode>(StoreA->getValue().getOperand(I));
Richard Sandiford067817e2013-09-27 15:29:20 +00001254 return !LoadA->isVolatile() && canUseBlockOperation(StoreA, LoadB);
Richard Sandiford178273a2013-09-05 10:36:45 +00001255}
1256
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001257void SystemZDAGToDAGISel::Select(SDNode *Node) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001258 // If we have a custom node, we already have selected!
1259 if (Node->isMachineOpcode()) {
1260 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Tim Northover31d093c2013-09-22 08:21:56 +00001261 Node->setNodeId(-1);
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001262 return;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001263 }
1264
1265 unsigned Opcode = Node->getOpcode();
1266 switch (Opcode) {
1267 case ISD::OR:
Richard Sandiford885140c2013-07-16 11:55:57 +00001268 if (Node->getOperand(1).getOpcode() != ISD::Constant)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001269 if (tryRxSBG(Node, SystemZ::ROSBG))
1270 return;
Richard Sandiford7878b852013-07-18 10:06:15 +00001271 goto or_xor;
1272
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001273 case ISD::XOR:
Richard Sandiford7878b852013-07-18 10:06:15 +00001274 if (Node->getOperand(1).getOpcode() != ISD::Constant)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001275 if (tryRxSBG(Node, SystemZ::RXSBG))
1276 return;
Richard Sandiford7878b852013-07-18 10:06:15 +00001277 // Fall through.
1278 or_xor:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001279 // If this is a 64-bit operation in which both 32-bit halves are nonzero,
Ulrich Weigand5f4373a2017-11-14 20:00:34 +00001280 // split the operation into two. If both operands here happen to be
1281 // constant, leave this to common code to optimize.
1282 if (Node->getValueType(0) == MVT::i64 &&
1283 Node->getOperand(0).getOpcode() != ISD::Constant)
Richard Sandiford21f5d682014-03-06 11:22:58 +00001284 if (auto *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001285 uint64_t Val = Op1->getZExtValue();
Justin Bognerffb273d2016-05-09 23:54:23 +00001286 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val)) {
1287 splitLargeImmediate(Opcode, Node, Node->getOperand(0),
1288 Val - uint32_t(Val), uint32_t(Val));
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001289 return;
Justin Bognerffb273d2016-05-09 23:54:23 +00001290 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001291 }
1292 break;
1293
Richard Sandiford84f54a32013-07-11 08:59:12 +00001294 case ISD::AND:
Richard Sandiford51093212013-07-18 10:40:35 +00001295 if (Node->getOperand(1).getOpcode() != ISD::Constant)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001296 if (tryRxSBG(Node, SystemZ::RNSBG))
1297 return;
Justin Bognercd1d5aa2016-08-17 20:30:52 +00001298 LLVM_FALLTHROUGH;
Richard Sandiford82ec87d2013-07-16 11:02:24 +00001299 case ISD::ROTL:
1300 case ISD::SHL:
1301 case ISD::SRL:
Richard Sandiford220ee492013-12-20 11:49:48 +00001302 case ISD::ZERO_EXTEND:
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001303 if (tryRISBGZero(Node))
1304 return;
Richard Sandiford84f54a32013-07-11 08:59:12 +00001305 break;
1306
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001307 case ISD::Constant:
1308 // If this is a 64-bit constant that is out of the range of LLILF,
1309 // LLIHF and LGFI, split it into two 32-bit pieces.
1310 if (Node->getValueType(0) == MVT::i64) {
1311 uint64_t Val = cast<ConstantSDNode>(Node)->getZExtValue();
Justin Bognerffb273d2016-05-09 23:54:23 +00001312 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val)) {
1313 splitLargeImmediate(ISD::OR, Node, SDValue(), Val - uint32_t(Val),
1314 uint32_t(Val));
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001315 return;
Justin Bognerffb273d2016-05-09 23:54:23 +00001316 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001317 }
1318 break;
1319
Richard Sandifordee834382013-07-31 12:38:08 +00001320 case SystemZISD::SELECT_CCMASK: {
1321 SDValue Op0 = Node->getOperand(0);
1322 SDValue Op1 = Node->getOperand(1);
1323 // Prefer to put any load first, so that it can be matched as a
Ulrich Weigand524f2762016-11-28 13:34:08 +00001324 // conditional load. Likewise for constants in range for LOCHI.
1325 if ((Op1.getOpcode() == ISD::LOAD && Op0.getOpcode() != ISD::LOAD) ||
1326 (Subtarget->hasLoadStoreOnCond2() &&
1327 Node->getValueType(0).isInteger() &&
1328 Op1.getOpcode() == ISD::Constant &&
1329 isInt<16>(cast<ConstantSDNode>(Op1)->getSExtValue()) &&
1330 !(Op0.getOpcode() == ISD::Constant &&
1331 isInt<16>(cast<ConstantSDNode>(Op0)->getSExtValue())))) {
Richard Sandifordee834382013-07-31 12:38:08 +00001332 SDValue CCValid = Node->getOperand(2);
1333 SDValue CCMask = Node->getOperand(3);
1334 uint64_t ConstCCValid =
1335 cast<ConstantSDNode>(CCValid.getNode())->getZExtValue();
1336 uint64_t ConstCCMask =
1337 cast<ConstantSDNode>(CCMask.getNode())->getZExtValue();
1338 // Invert the condition.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001339 CCMask = CurDAG->getConstant(ConstCCValid ^ ConstCCMask, SDLoc(Node),
Richard Sandifordee834382013-07-31 12:38:08 +00001340 CCMask.getValueType());
1341 SDValue Op4 = Node->getOperand(4);
1342 Node = CurDAG->UpdateNodeOperands(Node, Op1, Op0, CCValid, CCMask, Op4);
1343 }
1344 break;
1345 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001346
1347 case ISD::INSERT_VECTOR_ELT: {
1348 EVT VT = Node->getValueType(0);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00001349 unsigned ElemBitSize = VT.getScalarSizeInBits();
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001350 if (ElemBitSize == 32) {
1351 if (tryGather(Node, SystemZ::VGEF))
1352 return;
1353 } else if (ElemBitSize == 64) {
1354 if (tryGather(Node, SystemZ::VGEG))
1355 return;
1356 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001357 break;
1358 }
1359
1360 case ISD::STORE: {
1361 auto *Store = cast<StoreSDNode>(Node);
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00001362 unsigned ElemBitSize = Store->getValue().getValueSizeInBits();
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001363 if (ElemBitSize == 32) {
1364 if (tryScatter(Store, SystemZ::VSCEF))
1365 return;
1366 } else if (ElemBitSize == 64) {
1367 if (tryScatter(Store, SystemZ::VSCEG))
1368 return;
1369 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001370 break;
1371 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001372 }
1373
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001374 SelectCode(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001375}
1376
1377bool SystemZDAGToDAGISel::
1378SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +00001379 unsigned ConstraintID,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001380 std::vector<SDValue> &OutOps) {
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001381 SystemZAddressingMode::AddrForm Form;
1382 SystemZAddressingMode::DispRange DispRange;
Ulrich Weigand79564612016-06-09 15:19:16 +00001383 SDValue Base, Disp, Index;
1384
Daniel Sanders2eeace22015-03-17 16:16:14 +00001385 switch(ConstraintID) {
1386 default:
1387 llvm_unreachable("Unexpected asm memory constraint");
1388 case InlineAsm::Constraint_i:
Daniel Sanders2eeace22015-03-17 16:16:14 +00001389 case InlineAsm::Constraint_Q:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001390 // Accept an address with a short displacement, but no index.
1391 Form = SystemZAddressingMode::FormBD;
1392 DispRange = SystemZAddressingMode::Disp12Only;
1393 break;
Daniel Sanders2eeace22015-03-17 16:16:14 +00001394 case InlineAsm::Constraint_R:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001395 // Accept an address with a short displacement and an index.
1396 Form = SystemZAddressingMode::FormBDXNormal;
1397 DispRange = SystemZAddressingMode::Disp12Only;
Daniel Sanders2eeace22015-03-17 16:16:14 +00001398 break;
Ulrich Weigand79564612016-06-09 15:19:16 +00001399 case InlineAsm::Constraint_S:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001400 // Accept an address with a long displacement, but no index.
1401 Form = SystemZAddressingMode::FormBD;
1402 DispRange = SystemZAddressingMode::Disp20Only;
1403 break;
Ulrich Weigand79564612016-06-09 15:19:16 +00001404 case InlineAsm::Constraint_T:
1405 case InlineAsm::Constraint_m:
Ulrich Weigandd39e9dc2017-11-09 16:31:57 +00001406 case InlineAsm::Constraint_o:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001407 // Accept an address with a long displacement and an index.
1408 // m works the same as T, as this is the most general case.
Ulrich Weigandd39e9dc2017-11-09 16:31:57 +00001409 // We don't really have any special handling of "offsettable"
1410 // memory addresses, so just treat o the same as m.
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001411 Form = SystemZAddressingMode::FormBDXNormal;
1412 DispRange = SystemZAddressingMode::Disp20Only;
Ulrich Weigand79564612016-06-09 15:19:16 +00001413 break;
Daniel Sanders2eeace22015-03-17 16:16:14 +00001414 }
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001415
1416 if (selectBDXAddr(Form, DispRange, Op, Base, Disp, Index)) {
Zhan Jun Liaucf2f4b32016-08-18 21:44:15 +00001417 const TargetRegisterClass *TRC =
1418 Subtarget->getRegisterInfo()->getPointerRegClass(*MF);
1419 SDLoc DL(Base);
1420 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), DL, MVT::i32);
1421
1422 // Make sure that the base address doesn't go into %r0.
1423 // If it's a TargetFrameIndex or a fixed register, we shouldn't do anything.
1424 if (Base.getOpcode() != ISD::TargetFrameIndex &&
1425 Base.getOpcode() != ISD::Register) {
1426 Base =
1427 SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
1428 DL, Base.getValueType(),
1429 Base, RC), 0);
1430 }
1431
1432 // Make sure that the index register isn't assigned to %r0 either.
1433 if (Index.getOpcode() != ISD::Register) {
1434 Index =
1435 SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
1436 DL, Index.getValueType(),
1437 Index, RC), 0);
1438 }
1439
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001440 OutOps.push_back(Base);
1441 OutOps.push_back(Disp);
1442 OutOps.push_back(Index);
1443 return false;
1444 }
1445
Daniel Sanders2eeace22015-03-17 16:16:14 +00001446 return true;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001447}
Ulrich Weigand849a59f2018-01-19 20:52:04 +00001448
Ulrich Weigandb32f3652018-04-30 17:52:32 +00001449// IsProfitableToFold - Returns true if is profitable to fold the specific
1450// operand node N of U during instruction selection that starts at Root.
1451bool
1452SystemZDAGToDAGISel::IsProfitableToFold(SDValue N, SDNode *U,
1453 SDNode *Root) const {
1454 // We want to avoid folding a LOAD into an ICMP node if as a result
1455 // we would be forced to spill the condition code into a GPR.
1456 if (N.getOpcode() == ISD::LOAD && U->getOpcode() == SystemZISD::ICMP) {
1457 if (!N.hasOneUse() || !U->hasOneUse())
1458 return false;
1459
1460 // The user of the CC value will usually be a CopyToReg into the
1461 // physical CC register, which in turn is glued and chained to the
1462 // actual instruction that uses the CC value. Bail out if we have
1463 // anything else than that.
1464 SDNode *CCUser = *U->use_begin();
1465 SDNode *CCRegUser = nullptr;
1466 if (CCUser->getOpcode() == ISD::CopyToReg ||
1467 cast<RegisterSDNode>(CCUser->getOperand(1))->getReg() == SystemZ::CC) {
1468 for (auto *U : CCUser->uses()) {
1469 if (CCRegUser == nullptr)
1470 CCRegUser = U;
1471 else if (CCRegUser != U)
1472 return false;
1473 }
1474 }
1475 if (CCRegUser == nullptr)
1476 return false;
1477
1478 // If the actual instruction is a branch, the only thing that remains to be
1479 // checked is whether the CCUser chain is a predecessor of the load.
1480 if (CCRegUser->isMachineOpcode() &&
1481 CCRegUser->getMachineOpcode() == SystemZ::BRC)
1482 return !N->isPredecessorOf(CCUser->getOperand(0).getNode());
1483
1484 // Otherwise, the instruction may have multiple operands, and we need to
1485 // verify that none of them are a predecessor of the load. This is exactly
1486 // the same check that would be done by common code if the CC setter were
1487 // glued to the CC user, so simply invoke that check here.
1488 if (!IsLegalToFold(N, U, CCRegUser, OptLevel, false))
1489 return false;
1490 }
1491
1492 return true;
1493}
1494
Ulrich Weigand849a59f2018-01-19 20:52:04 +00001495namespace {
1496// Represents a sequence for extracting a 0/1 value from an IPM result:
1497// (((X ^ XORValue) + AddValue) >> Bit)
1498struct IPMConversion {
1499 IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit)
1500 : XORValue(xorValue), AddValue(addValue), Bit(bit) {}
1501
1502 int64_t XORValue;
1503 int64_t AddValue;
1504 unsigned Bit;
1505};
1506} // end anonymous namespace
1507
1508// Return a sequence for getting a 1 from an IPM result when CC has a
1509// value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1510// The handling of CC values outside CCValid doesn't matter.
1511static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1512 // Deal with cases where the result can be taken directly from a bit
1513 // of the IPM result.
1514 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1515 return IPMConversion(0, 0, SystemZ::IPM_CC);
1516 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1517 return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1518
1519 // Deal with cases where we can add a value to force the sign bit
1520 // to contain the right value. Putting the bit in 31 means we can
1521 // use SRL rather than RISBG(L), and also makes it easier to get a
1522 // 0/-1 value, so it has priority over the other tests below.
1523 //
1524 // These sequences rely on the fact that the upper two bits of the
1525 // IPM result are zero.
1526 uint64_t TopBit = uint64_t(1) << 31;
1527 if (CCMask == (CCValid & SystemZ::CCMASK_0))
1528 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1529 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1530 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1531 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1532 | SystemZ::CCMASK_1
1533 | SystemZ::CCMASK_2)))
1534 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1535 if (CCMask == (CCValid & SystemZ::CCMASK_3))
1536 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1537 if (CCMask == (CCValid & (SystemZ::CCMASK_1
1538 | SystemZ::CCMASK_2
1539 | SystemZ::CCMASK_3)))
1540 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1541
1542 // Next try inverting the value and testing a bit. 0/1 could be
1543 // handled this way too, but we dealt with that case above.
1544 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1545 return IPMConversion(-1, 0, SystemZ::IPM_CC);
1546
1547 // Handle cases where adding a value forces a non-sign bit to contain
1548 // the right value.
1549 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1550 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1551 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1552 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1553
1554 // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are
1555 // can be done by inverting the low CC bit and applying one of the
1556 // sign-based extractions above.
1557 if (CCMask == (CCValid & SystemZ::CCMASK_1))
1558 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1559 if (CCMask == (CCValid & SystemZ::CCMASK_2))
1560 return IPMConversion(1 << SystemZ::IPM_CC,
1561 TopBit - (3 << SystemZ::IPM_CC), 31);
1562 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1563 | SystemZ::CCMASK_1
1564 | SystemZ::CCMASK_3)))
1565 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1566 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1567 | SystemZ::CCMASK_2
1568 | SystemZ::CCMASK_3)))
1569 return IPMConversion(1 << SystemZ::IPM_CC,
1570 TopBit - (1 << SystemZ::IPM_CC), 31);
1571
1572 llvm_unreachable("Unexpected CC combination");
1573}
1574
1575SDValue SystemZDAGToDAGISel::expandSelectBoolean(SDNode *Node) {
1576 auto *TrueOp = dyn_cast<ConstantSDNode>(Node->getOperand(0));
1577 auto *FalseOp = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1578 if (!TrueOp || !FalseOp)
1579 return SDValue();
1580 if (FalseOp->getZExtValue() != 0)
1581 return SDValue();
1582 if (TrueOp->getSExtValue() != 1 && TrueOp->getSExtValue() != -1)
1583 return SDValue();
1584
1585 auto *CCValidOp = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1586 auto *CCMaskOp = dyn_cast<ConstantSDNode>(Node->getOperand(3));
1587 if (!CCValidOp || !CCMaskOp)
1588 return SDValue();
1589 int CCValid = CCValidOp->getZExtValue();
1590 int CCMask = CCMaskOp->getZExtValue();
1591
1592 SDLoc DL(Node);
Ulrich Weigandb32f3652018-04-30 17:52:32 +00001593 SDValue CCReg = Node->getOperand(4);
Ulrich Weigand849a59f2018-01-19 20:52:04 +00001594 IPMConversion IPM = getIPMConversion(CCValid, CCMask);
Ulrich Weigandb32f3652018-04-30 17:52:32 +00001595 SDValue Result = CurDAG->getNode(SystemZISD::IPM, DL, MVT::i32, CCReg);
Ulrich Weigand849a59f2018-01-19 20:52:04 +00001596
1597 if (IPM.XORValue)
1598 Result = CurDAG->getNode(ISD::XOR, DL, MVT::i32, Result,
1599 CurDAG->getConstant(IPM.XORValue, DL, MVT::i32));
1600
1601 if (IPM.AddValue)
1602 Result = CurDAG->getNode(ISD::ADD, DL, MVT::i32, Result,
1603 CurDAG->getConstant(IPM.AddValue, DL, MVT::i32));
1604
1605 EVT VT = Node->getValueType(0);
1606 if (VT == MVT::i32 && IPM.Bit == 31) {
1607 unsigned ShiftOp = TrueOp->getSExtValue() == 1 ? ISD::SRL : ISD::SRA;
1608 Result = CurDAG->getNode(ShiftOp, DL, MVT::i32, Result,
1609 CurDAG->getConstant(IPM.Bit, DL, MVT::i32));
1610 } else {
1611 if (VT != MVT::i32)
1612 Result = CurDAG->getNode(ISD::ANY_EXTEND, DL, VT, Result);
1613
1614 if (TrueOp->getSExtValue() == 1) {
1615 // The SHR/AND sequence should get optimized to an RISBG.
1616 Result = CurDAG->getNode(ISD::SRL, DL, VT, Result,
1617 CurDAG->getConstant(IPM.Bit, DL, MVT::i32));
1618 Result = CurDAG->getNode(ISD::AND, DL, VT, Result,
1619 CurDAG->getConstant(1, DL, VT));
1620 } else {
1621 // Sign-extend from IPM.Bit using a pair of shifts.
1622 int ShlAmt = VT.getSizeInBits() - 1 - IPM.Bit;
1623 int SraAmt = VT.getSizeInBits() - 1;
1624 Result = CurDAG->getNode(ISD::SHL, DL, VT, Result,
1625 CurDAG->getConstant(ShlAmt, DL, MVT::i32));
1626 Result = CurDAG->getNode(ISD::SRA, DL, VT, Result,
1627 CurDAG->getConstant(SraAmt, DL, MVT::i32));
1628 }
1629 }
1630
1631 return Result;
1632}
1633
1634void SystemZDAGToDAGISel::PreprocessISelDAG() {
Ulrich Weigand426f6be2018-01-19 20:56:04 +00001635 // If we have conditional immediate loads, we always prefer
1636 // using those over an IPM sequence.
1637 if (Subtarget->hasLoadStoreOnCond2())
1638 return;
1639
Ulrich Weigand849a59f2018-01-19 20:52:04 +00001640 bool MadeChange = false;
1641
1642 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
1643 E = CurDAG->allnodes_end();
1644 I != E;) {
1645 SDNode *N = &*I++;
1646 if (N->use_empty())
1647 continue;
1648
1649 SDValue Res;
1650 switch (N->getOpcode()) {
1651 default: break;
1652 case SystemZISD::SELECT_CCMASK:
1653 Res = expandSelectBoolean(N);
1654 break;
1655 }
1656
1657 if (Res) {
1658 DEBUG(dbgs() << "SystemZ DAG preprocessing replacing:\nOld: ");
1659 DEBUG(N->dump(CurDAG));
1660 DEBUG(dbgs() << "\nNew: ");
1661 DEBUG(Res.getNode()->dump(CurDAG));
1662 DEBUG(dbgs() << "\n");
1663
1664 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Res);
1665 MadeChange = true;
1666 }
1667 }
1668
1669 if (MadeChange)
1670 CurDAG->RemoveDeadNodes();
1671}