LLVM 24.0.0git
SystemZISelDAGToDAG.cpp
Go to the documentation of this file.
1//===-- SystemZISelDAGToDAG.cpp - A dag to dag inst selector for SystemZ --===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines an instruction selector for the SystemZ target.
10//
11//===----------------------------------------------------------------------===//
12
13#include "SystemZ.h"
14#include "SystemZISelLowering.h"
18#include "llvm/IR/Module.h"
20#include "llvm/Support/Debug.h"
23
24using namespace llvm;
25
26#define DEBUG_TYPE "systemz-isel"
27#define PASS_NAME "SystemZ DAG->DAG Pattern Instruction Selection"
28
29namespace {
30// Used to build addressing modes.
31struct SystemZAddressingMode {
32 // The shape of the address.
33 enum AddrForm {
34 // base+displacement
35 FormBD,
36
37 // base+displacement+index for load and store operands
38 FormBDXNormal,
39
40 // base+displacement+index for load address operands
41 FormBDXLA,
42
43 // base+displacement+index+ADJDYNALLOC
44 FormBDXDynAlloc
45 };
46 AddrForm Form;
47
48 // The type of displacement. The enum names here correspond directly
49 // to the definitions in SystemZOperand.td. We could split them into
50 // flags -- single/pair, 128-bit, etc. -- but it hardly seems worth it.
51 enum DispRange {
52 Disp12Only,
53 Disp12Pair,
54 Disp20Only,
55 Disp20Only128,
56 Disp20Pair
57 };
58 DispRange DR;
59
60 // The parts of the address. The address is equivalent to:
61 //
62 // Base + Disp + Index + (IncludesDynAlloc ? ADJDYNALLOC : 0)
64 int64_t Disp;
65 SDValue Index;
66 bool IncludesDynAlloc;
67
68 SystemZAddressingMode(AddrForm form, DispRange dr)
69 : Form(form), DR(dr), Disp(0), IncludesDynAlloc(false) {}
70
71 // True if the address can have an index register.
72 bool hasIndexField() { return Form != FormBD; }
73
74 // True if the address can (and must) include ADJDYNALLOC.
75 bool isDynAlloc() { return Form == FormBDXDynAlloc; }
76
77 void dump(const llvm::SelectionDAG *DAG) {
78 errs() << "SystemZAddressingMode " << this << '\n';
79
80 errs() << " Base ";
81 if (Base.getNode())
82 Base.getNode()->dump(DAG);
83 else
84 errs() << "null\n";
85
86 if (hasIndexField()) {
87 errs() << " Index ";
88 if (Index.getNode())
89 Index.getNode()->dump(DAG);
90 else
91 errs() << "null\n";
92 }
93
94 errs() << " Disp " << Disp;
95 if (IncludesDynAlloc)
96 errs() << " + ADJDYNALLOC";
97 errs() << '\n';
98 }
99};
100
101// Return a mask with Count low bits set.
102static uint64_t allOnes(unsigned int Count) {
103 assert(Count <= 64);
104 if (Count > 63)
105 return UINT64_MAX;
106 return (uint64_t(1) << Count) - 1;
107}
108
109// Represents operands 2 to 5 of the ROTATE AND ... SELECTED BITS operation
110// given by Opcode. The operands are: Input (R2), Start (I3), End (I4) and
111// Rotate (I5). The combined operand value is effectively:
112//
113// (or (rotl Input, Rotate), ~Mask)
114//
115// for RNSBG and:
116//
117// (and (rotl Input, Rotate), Mask)
118//
119// otherwise. The output value has BitSize bits, although Input may be
120// narrower (in which case the upper bits are don't care), or wider (in which
121// case the result will be truncated as part of the operation).
122struct RxSBGOperands {
123 RxSBGOperands(unsigned Op, SDValue N)
124 : Opcode(Op), BitSize(N.getValueSizeInBits()),
125 Mask(allOnes(BitSize)), Input(N), Start(64 - BitSize), End(63),
126 Rotate(0) {}
127
128 unsigned Opcode;
129 unsigned BitSize;
130 uint64_t Mask;
132 unsigned Start;
133 unsigned End;
134 unsigned Rotate;
135};
136
137class SystemZDAGToDAGISel : public SelectionDAGISel {
139
140 // Used by SystemZOperands.td to create integer constants.
141 inline SDValue getImm(const SDNode *Node, uint64_t Imm) const {
142 return CurDAG->getTargetConstant(Imm, SDLoc(Node), Node->getValueType(0));
143 }
144
145 const SystemZTargetMachine &getTargetMachine() const {
146 return static_cast<const SystemZTargetMachine &>(TM);
147 }
148
149 const SystemZInstrInfo *getInstrInfo() const {
150 return Subtarget->getInstrInfo();
151 }
152
153 // Try to fold more of the base or index of AM into AM, where IsBase
154 // selects between the base and index.
155 bool expandAddress(SystemZAddressingMode &AM, bool IsBase) const;
156
157 // Try to describe N in AM, returning true on success.
158 bool selectAddress(SDValue N, SystemZAddressingMode &AM) const;
159
160 // Extract individual target operands from matched address AM.
161 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
162 SDValue &Base, SDValue &Disp) const;
163 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
164 SDValue &Base, SDValue &Disp, SDValue &Index) const;
165
166 // Try to match Addr as a FormBD address with displacement type DR.
167 // Return true on success, storing the base and displacement in
168 // Base and Disp respectively.
169 bool selectBDAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
170 SDValue &Base, SDValue &Disp, int64_t Offset = 0) const;
171
172 // Try to match Addr as a FormBDX address with displacement type DR.
173 // Return true on success and if the result had no index. Store the
174 // base and displacement in Base and Disp respectively.
175 bool selectMVIAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
176 SDValue &Base, SDValue &Disp) const;
177
178 // Try to match Addr as a FormBDX* address of form Form with
179 // displacement type DR. Return true on success, storing the base,
180 // displacement and index in Base, Disp and Index respectively.
181 bool selectBDXAddr(SystemZAddressingMode::AddrForm Form,
182 SystemZAddressingMode::DispRange DR, SDValue Addr,
183 SDValue &Base, SDValue &Disp, SDValue &Index) const;
184
185 // PC-relative address matching routines used by SystemZOperands.td.
186 bool selectPCRelAddress(SDValue Addr, SDValue &Target) const {
187 if (SystemZISD::isPCREL(Addr.getOpcode())) {
188 Target = Addr.getOperand(0);
189 return true;
190 }
191 return false;
192 }
193
194 // BD matching routines used by SystemZOperands.td.
195 bool selectBDAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
196 return selectBDAddr(SystemZAddressingMode::Disp12Only, Addr, Base, Disp);
197 }
198 bool selectBDAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
199 return selectBDAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
200 }
201 bool selectBDAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
202 return selectBDAddr(SystemZAddressingMode::Disp20Only, Addr, Base, Disp);
203 }
204 bool selectBDAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
205 return selectBDAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
206 }
207
208 // MVI matching routines used by SystemZOperands.td.
209 bool selectMVIAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
210 return selectMVIAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
211 }
212 bool selectMVIAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
213 return selectMVIAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
214 }
215
216 // BDX matching routines used by SystemZOperands.td.
217 bool selectBDXAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
218 SDValue &Index) const {
219 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
220 SystemZAddressingMode::Disp12Only,
221 Addr, Base, Disp, Index);
222 }
223 bool selectBDXAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
224 SDValue &Index) const {
225 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
226 SystemZAddressingMode::Disp12Pair,
227 Addr, Base, Disp, Index);
228 }
229 bool selectDynAlloc12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
230 SDValue &Index) const {
231 return selectBDXAddr(SystemZAddressingMode::FormBDXDynAlloc,
232 SystemZAddressingMode::Disp12Only,
233 Addr, Base, Disp, Index);
234 }
235 bool selectBDXAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp,
236 SDValue &Index) const {
237 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
238 SystemZAddressingMode::Disp20Only,
239 Addr, Base, Disp, Index);
240 }
241 bool selectBDXAddr20Only128(SDValue Addr, SDValue &Base, SDValue &Disp,
242 SDValue &Index) const {
243 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
244 SystemZAddressingMode::Disp20Only128,
245 Addr, Base, Disp, Index);
246 }
247 bool selectBDXAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
248 SDValue &Index) const {
249 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
250 SystemZAddressingMode::Disp20Pair,
251 Addr, Base, Disp, Index);
252 }
253 bool selectLAAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
254 SDValue &Index) const {
255 return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
256 SystemZAddressingMode::Disp12Pair,
257 Addr, Base, Disp, Index);
258 }
259 bool selectLAAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
260 SDValue &Index) const {
261 return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
262 SystemZAddressingMode::Disp20Pair,
263 Addr, Base, Disp, Index);
264 }
265
266 // Try to match Addr as an address with a base, 12-bit displacement
267 // and index, where the index is element Elem of a vector.
268 // Return true on success, storing the base, displacement and vector
269 // in Base, Disp and Index respectively.
270 bool selectBDVAddr12Only(SDValue Addr, SDValue Elem, SDValue &Base,
271 SDValue &Disp, SDValue &Index) const;
272 // Wrapper functions for LSB access on big-endian multi-byte types.
273 // Selects a Base + Displacement address and applies a fixed byte Offset.
274 // Offsets: i16 LSB = +1, i32 LSB = +3, i64 LSB = +7.
275 bool selectBDAddr12off1(SDValue Addr, SDValue &Base, SDValue &Disp) const;
276 bool selectBDAddr12off3(SDValue Addr, SDValue &Base, SDValue &Disp) const;
277 bool selectBDAddr12off7(SDValue Addr, SDValue &Base, SDValue &Disp) const;
278 bool selectBDAddr20off1(SDValue Addr, SDValue &Base, SDValue &Disp) const;
279 bool selectBDAddr20off3(SDValue Addr, SDValue &Base, SDValue &Disp) const;
280 bool selectBDAddr20off7(SDValue Addr, SDValue &Base, SDValue &Disp) const;
281
282 // Check whether (or Op (and X InsertMask)) is effectively an insertion
283 // of X into bits InsertMask of some Y != Op. Return true if so and
284 // set Op to that Y.
285 bool detectOrAndInsertion(SDValue &Op, uint64_t InsertMask) const;
286
287 // Try to update RxSBG so that only the bits of RxSBG.Input in Mask are used.
288 // Return true on success.
289 bool refineRxSBGMask(RxSBGOperands &RxSBG, uint64_t Mask) const;
290
291 // Try to fold some of RxSBG.Input into other fields of RxSBG.
292 // Return true on success.
293 bool expandRxSBG(RxSBGOperands &RxSBG) const;
294
295 // Return an undefined value of type VT.
296 SDValue getUNDEF(const SDLoc &DL, EVT VT) const;
297
298 // Convert N to VT, if it isn't already.
299 SDValue convertTo(const SDLoc &DL, EVT VT, SDValue N) const;
300
301 // Try to implement AND or shift node N using RISBG with the zero flag set.
302 // Return the selected node on success, otherwise return null.
303 bool tryRISBGZero(SDNode *N);
304
305 // Try to use RISBG or Opcode to implement OR or XOR node N.
306 // Return the selected node on success, otherwise return null.
307 bool tryRxSBG(SDNode *N, unsigned Opcode);
308
309 // If Op0 is null, then Node is a constant that can be loaded using:
310 //
311 // (Opcode UpperVal LowerVal)
312 //
313 // If Op0 is nonnull, then Node can be implemented using:
314 //
315 // (Opcode (Opcode Op0 UpperVal) LowerVal)
316 void splitLargeImmediate(unsigned Opcode, SDNode *Node, SDValue Op0,
317 uint64_t UpperVal, uint64_t LowerVal);
318
319 void loadVectorConstant(const SystemZVectorConstantInfo &VCI,
320 SDNode *Node);
321
322 SDNode *loadPoolVectorConstant(APInt Val, EVT VT, SDLoc DL);
323
324 // Try to use gather instruction Opcode to implement vector insertion N.
325 bool tryGather(SDNode *N, unsigned Opcode);
326
327 // Try to use scatter instruction Opcode to implement store Store.
328 bool tryScatter(StoreSDNode *Store, unsigned Opcode);
329
330 // Change a chain of {load; op; store} of the same value into a simple op
331 // through memory of that value, if the uses of the modified value and its
332 // address are suitable.
333 bool tryFoldLoadStoreIntoMemOperand(SDNode *Node);
334
335 // Return true if Load and Store are loads and stores of the same size
336 // and are guaranteed not to overlap. Such operations can be implemented
337 // using block (SS-format) instructions.
338 //
339 // Partial overlap would lead to incorrect code, since the block operations
340 // are logically bytewise, even though they have a fast path for the
341 // non-overlapping case. We also need to avoid full overlap (i.e. two
342 // addresses that might be equal at run time) because although that case
343 // would be handled correctly, it might be implemented by millicode.
344 bool canUseBlockOperation(StoreSDNode *Store, LoadSDNode *Load) const;
345
346 // N is a (store (load Y), X) pattern. Return true if it can use an MVC
347 // from Y to X.
348 bool storeLoadCanUseMVC(SDNode *N) const;
349
350 // N is a (store (op (load A[0]), (load A[1])), X) pattern. Return true
351 // if A[1 - I] == X and if N can use a block operation like NC from A[I]
352 // to X.
353 bool storeLoadCanUseBlockBinary(SDNode *N, unsigned I) const;
354
355 // Return true if N (a load or a store) fullfills the alignment
356 // requirements for a PC-relative access.
357 bool storeLoadIsAligned(SDNode *N) const;
358
359 // Return the load extension type of a load or atomic load.
360 ISD::LoadExtType getLoadExtType(SDNode *N) const;
361
362 // Try to expand a boolean SELECT_CCMASK using an IPM sequence.
363 SDValue expandSelectBoolean(SDNode *Node);
364
365 // Return true if the flags of N and the subtarget allows for
366 // reassociation, in which case a reg/reg opcode is needed as input to the
367 // MachineCombiner.
368 bool shouldSelectForReassoc(SDNode *N) const;
369
370public:
371 SystemZDAGToDAGISel() = delete;
372
373 SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOptLevel OptLevel)
374 : SelectionDAGISel(TM, OptLevel) {}
375
376 bool runOnMachineFunction(MachineFunction &MF) override {
377 const Function &F = MF.getFunction();
378 if (F.getParent()->getStackProtectorGuard() != "global") {
379 if (F.getParent()->hasStackProtectorGuardRecord())
380 report_fatal_error("mstack-protector-guard-record only supported with "
381 "mstack-protector-guard=global");
382 }
385 }
386
387 // Override SelectionDAGISel.
388 void Select(SDNode *Node) override;
389 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
390 InlineAsm::ConstraintCode ConstraintID,
391 std::vector<SDValue> &OutOps) override;
392 bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const override;
393 void PreprocessISelDAG() override;
394
395 // Include the pieces autogenerated from the target description.
396 #include "SystemZGenDAGISel.inc"
397};
398
399class SystemZDAGToDAGISelLegacy : public SelectionDAGISelLegacy {
400public:
401 static char ID;
402 explicit SystemZDAGToDAGISelLegacy(SystemZTargetMachine &TM,
403 CodeGenOptLevel OptLevel)
405 ID, std::make_unique<SystemZDAGToDAGISel>(TM, OptLevel)) {}
406};
407} // end anonymous namespace
408
409char SystemZDAGToDAGISelLegacy::ID = 0;
410
411INITIALIZE_PASS(SystemZDAGToDAGISelLegacy, DEBUG_TYPE, PASS_NAME, false, false)
412
414 CodeGenOptLevel OptLevel)
416 std::make_unique<SystemZDAGToDAGISel>(TM, OptLevel)) {}
417
419 CodeGenOptLevel OptLevel) {
420 return new SystemZDAGToDAGISelLegacy(TM, OptLevel);
421}
422
423// Return true if Val should be selected as a displacement for an address
424// with range DR. Here we're interested in the range of both the instruction
425// described by DR and of any pairing instruction.
426static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
427 switch (DR) {
428 case SystemZAddressingMode::Disp12Only:
429 return isUInt<12>(Val);
430
431 case SystemZAddressingMode::Disp12Pair:
432 case SystemZAddressingMode::Disp20Only:
433 case SystemZAddressingMode::Disp20Pair:
434 return isInt<20>(Val);
435
436 case SystemZAddressingMode::Disp20Only128:
437 return isInt<20>(Val) && isInt<20>(Val + 8);
438 }
439 llvm_unreachable("Unhandled displacement range");
440}
441
442// Change the base or index in AM to Value, where IsBase selects
443// between the base and index.
444static void changeComponent(SystemZAddressingMode &AM, bool IsBase,
445 SDValue Value) {
446 if (IsBase)
447 AM.Base = Value;
448 else
449 AM.Index = Value;
450}
451
452// The base or index of AM is equivalent to Value + ADJDYNALLOC,
453// where IsBase selects between the base and index. Try to fold the
454// ADJDYNALLOC into AM.
455static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase,
456 SDValue Value) {
457 if (AM.isDynAlloc() && !AM.IncludesDynAlloc) {
458 changeComponent(AM, IsBase, Value);
459 AM.IncludesDynAlloc = true;
460 return true;
461 }
462 return false;
463}
464
465// The base of AM is equivalent to Base + Index. Try to use Index as
466// the index register.
467static bool expandIndex(SystemZAddressingMode &AM, SDValue Base,
468 SDValue Index) {
469 if (AM.hasIndexField() && !AM.Index.getNode()) {
470 AM.Base = Base;
471 AM.Index = Index;
472 return true;
473 }
474 return false;
475}
476
477// The base or index of AM is equivalent to Op0 + Op1, where IsBase selects
478// between the base and index. Try to fold Op1 into AM's displacement.
479static bool expandDisp(SystemZAddressingMode &AM, bool IsBase,
480 SDValue Op0, uint64_t Op1) {
481 // First try adjusting the displacement.
482 int64_t TestDisp = AM.Disp + Op1;
483 if (selectDisp(AM.DR, TestDisp)) {
484 changeComponent(AM, IsBase, Op0);
485 AM.Disp = TestDisp;
486 return true;
487 }
488
489 // We could consider forcing the displacement into a register and
490 // using it as an index, but it would need to be carefully tuned.
491 return false;
492}
493
494bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM,
495 bool IsBase) const {
496 SDValue N = IsBase ? AM.Base : AM.Index;
497 unsigned Opcode = N.getOpcode();
498 // Look through no-op truncations.
499 if (Opcode == ISD::TRUNCATE && N.getOperand(0).getValueSizeInBits() <= 64) {
500 N = N.getOperand(0);
501 Opcode = N.getOpcode();
502 }
503 if (Opcode == ISD::ADD || CurDAG->isBaseWithConstantOffset(N)) {
504 SDValue Op0 = N.getOperand(0);
505 SDValue Op1 = N.getOperand(1);
506
507 unsigned Op0Code = Op0->getOpcode();
508 unsigned Op1Code = Op1->getOpcode();
509
510 if (Op0Code == SystemZISD::ADJDYNALLOC)
511 return expandAdjDynAlloc(AM, IsBase, Op1);
512 if (Op1Code == SystemZISD::ADJDYNALLOC)
513 return expandAdjDynAlloc(AM, IsBase, Op0);
514
515 if (Op0Code == ISD::Constant)
516 return expandDisp(AM, IsBase, Op1,
517 cast<ConstantSDNode>(Op0)->getSExtValue());
518 if (Op1Code == ISD::Constant)
519 return expandDisp(AM, IsBase, Op0,
520 cast<ConstantSDNode>(Op1)->getSExtValue());
521
522 if (IsBase && expandIndex(AM, Op0, Op1))
523 return true;
524 }
525 if (Opcode == SystemZISD::PCREL_OFFSET) {
526 SDValue Full = N.getOperand(0);
527 SDValue Base = N.getOperand(1);
528 SDValue Anchor = Base.getOperand(0);
530 cast<GlobalAddressSDNode>(Anchor)->getOffset());
531 return expandDisp(AM, IsBase, Base, Offset);
532 }
533 return false;
534}
535
536// Return true if an instruction with displacement range DR should be
537// used for displacement value Val. selectDisp(DR, Val) must already hold.
538static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
539 assert(selectDisp(DR, Val) && "Invalid displacement");
540 switch (DR) {
541 case SystemZAddressingMode::Disp12Only:
542 case SystemZAddressingMode::Disp20Only:
543 case SystemZAddressingMode::Disp20Only128:
544 return true;
545
546 case SystemZAddressingMode::Disp12Pair:
547 // Use the other instruction if the displacement is too large.
548 return isUInt<12>(Val);
549
550 case SystemZAddressingMode::Disp20Pair:
551 // Use the other instruction if the displacement is small enough.
552 return !isUInt<12>(Val);
553 }
554 llvm_unreachable("Unhandled displacement range");
555}
556
557// Return true if Base + Disp + Index should be performed by LA(Y).
558static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index) {
559 // Don't use LA(Y) for constants.
560 if (!Base)
561 return false;
562
563 // Always use LA(Y) for frame addresses, since we know that the destination
564 // register is almost always (perhaps always) going to be different from
565 // the frame register.
566 if (Base->getOpcode() == ISD::FrameIndex)
567 return true;
568
569 if (Disp) {
570 // Always use LA(Y) if there is a base, displacement and index.
571 if (Index)
572 return true;
573
574 // Always use LA if the displacement is small enough. It should always
575 // be no worse than AGHI (and better if it avoids a move).
576 if (isUInt<12>(Disp))
577 return true;
578
579 // For similar reasons, always use LAY if the constant is too big for AGHI.
580 // LAY should be no worse than AGFI.
581 if (!isInt<16>(Disp))
582 return true;
583 } else {
584 // Don't use LA for plain registers.
585 if (!Index)
586 return false;
587
588 // Don't use LA for plain addition if the index operand is only used
589 // once. It should be a natural two-operand addition in that case.
590 if (Index->hasOneUse())
591 return false;
592
593 // Prefer addition if the second operation is sign-extended, in the
594 // hope of using AGF.
595 unsigned IndexOpcode = Index->getOpcode();
596 if (IndexOpcode == ISD::SIGN_EXTEND ||
597 IndexOpcode == ISD::SIGN_EXTEND_INREG)
598 return false;
599 }
600
601 // Don't use LA for two-operand addition if either operand is only
602 // used once. The addition instructions are better in that case.
603 if (Base->hasOneUse())
604 return false;
605
606 return true;
607}
608
609// Return true if Addr is suitable for AM, updating AM if so.
610bool SystemZDAGToDAGISel::selectAddress(SDValue Addr,
611 SystemZAddressingMode &AM) const {
612 // Start out assuming that the address will need to be loaded separately,
613 // then try to extend it as much as we can.
614 AM.Base = Addr;
615
616 // First try treating the address as a constant.
617 if (Addr.getOpcode() == ISD::Constant &&
618 expandDisp(AM, true, SDValue(),
619 cast<ConstantSDNode>(Addr)->getSExtValue()))
620 ;
621 // Also see if it's a bare ADJDYNALLOC.
622 else if (Addr.getOpcode() == SystemZISD::ADJDYNALLOC &&
623 expandAdjDynAlloc(AM, true, SDValue()))
624 ;
625 else
626 // Otherwise try expanding each component.
627 while (expandAddress(AM, true) ||
628 (AM.Index.getNode() && expandAddress(AM, false)))
629 continue;
630
631 // Reject cases where it isn't profitable to use LA(Y).
632 if (AM.Form == SystemZAddressingMode::FormBDXLA &&
633 !shouldUseLA(AM.Base.getNode(), AM.Disp, AM.Index.getNode()))
634 return false;
635
636 // Reject cases where the other instruction in a pair should be used.
637 if (!isValidDisp(AM.DR, AM.Disp))
638 return false;
639
640 // Make sure that ADJDYNALLOC is included where necessary.
641 if (AM.isDynAlloc() && !AM.IncludesDynAlloc)
642 return false;
643
644 LLVM_DEBUG(AM.dump(CurDAG));
645 return true;
646}
647
648// Insert a node into the DAG at least before Pos. This will reposition
649// the node as needed, and will assign it a node ID that is <= Pos's ID.
650// Note that this does *not* preserve the uniqueness of node IDs!
651// The selection DAG must no longer depend on their uniqueness when this
652// function is used.
653static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N) {
654 if (N->getNodeId() == -1 ||
657 DAG->RepositionNode(Pos->getIterator(), N.getNode());
658 // Mark Node as invalid for pruning as after this it may be a successor to a
659 // selected node but otherwise be in the same position of Pos.
660 // Conservatively mark it with the same -abs(Id) to assure node id
661 // invariant is preserved.
662 N->setNodeId(Pos->getNodeId());
664 }
665}
666
667void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
668 EVT VT, SDValue &Base,
669 SDValue &Disp) const {
670 Base = AM.Base;
671 if (!Base.getNode())
672 // Register 0 means "no base". This is mostly useful for shifts.
673 Base = CurDAG->getRegister(0, VT);
674 else if (Base.getOpcode() == ISD::FrameIndex) {
675 // Lower a FrameIndex to a TargetFrameIndex.
676 int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex();
677 Base = CurDAG->getTargetFrameIndex(FrameIndex, VT);
678 } else if (Base.getValueType() != VT) {
679 // Truncate values from i64 to i32, for shifts.
680 assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 &&
681 "Unexpected truncation");
682 SDLoc DL(Base);
683 SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base);
684 insertDAGNode(CurDAG, Base.getNode(), Trunc);
685 Base = Trunc;
686 }
687
688 // Lower the displacement to a TargetConstant.
689 Disp = CurDAG->getSignedTargetConstant(AM.Disp, SDLoc(Base), VT);
690}
691
692void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
693 EVT VT, SDValue &Base,
694 SDValue &Disp,
695 SDValue &Index) const {
696 getAddressOperands(AM, VT, Base, Disp);
697
698 Index = AM.Index;
699 if (!Index.getNode())
700 // Register 0 means "no index".
701 Index = CurDAG->getRegister(0, VT);
702}
703
704bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR,
705 SDValue Addr, SDValue &Base,
706 SDValue &Disp, int64_t Offset) const {
707 SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR);
708 AM.Disp += Offset;
709 if (!selectAddress(Addr, AM))
710 return false;
711
712 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
713 return true;
714}
715
716bool SystemZDAGToDAGISel::selectMVIAddr(SystemZAddressingMode::DispRange DR,
717 SDValue Addr, SDValue &Base,
718 SDValue &Disp) const {
719 SystemZAddressingMode AM(SystemZAddressingMode::FormBDXNormal, DR);
720 if (!selectAddress(Addr, AM) || AM.Index.getNode())
721 return false;
722
723 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
724 return true;
725}
726
727bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form,
728 SystemZAddressingMode::DispRange DR,
729 SDValue Addr, SDValue &Base,
730 SDValue &Disp, SDValue &Index) const {
731 SystemZAddressingMode AM(Form, DR);
732 if (!selectAddress(Addr, AM))
733 return false;
734
735 getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index);
736 return true;
737}
738
739bool SystemZDAGToDAGISel::selectBDVAddr12Only(SDValue Addr, SDValue Elem,
740 SDValue &Base,
741 SDValue &Disp,
742 SDValue &Index) const {
743 SDValue Regs[2];
744 if (selectBDXAddr12Only(Addr, Regs[0], Disp, Regs[1]) &&
745 Regs[0].getNode() && Regs[1].getNode()) {
746 for (unsigned int I = 0; I < 2; ++I) {
747 Base = Regs[I];
748 Index = Regs[1 - I];
749 // We can't tell here whether the index vector has the right type
750 // for the access; the caller needs to do that instead.
751 if (Index.getOpcode() == ISD::ZERO_EXTEND)
752 Index = Index.getOperand(0);
753 if (Index.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
754 Index.getOperand(1) == Elem) {
755 Index = Index.getOperand(0);
756 return true;
757 }
758 }
759 }
760 return false;
761}
762
763bool SystemZDAGToDAGISel::selectBDAddr12off1(SDValue A, SDValue &B,
764 SDValue &D) const {
765 return selectBDAddr(SystemZAddressingMode::Disp12Pair, A, B, D, 1);
766}
767bool SystemZDAGToDAGISel::selectBDAddr12off3(SDValue A, SDValue &B,
768 SDValue &D) const {
769 return selectBDAddr(SystemZAddressingMode::Disp12Pair, A, B, D, 3);
770}
771bool SystemZDAGToDAGISel::selectBDAddr12off7(SDValue A, SDValue &B,
772 SDValue &D) const {
773 return selectBDAddr(SystemZAddressingMode::Disp12Pair, A, B, D, 7);
774}
775
776bool SystemZDAGToDAGISel::selectBDAddr20off1(SDValue A, SDValue &B,
777 SDValue &D) const {
778 return selectBDAddr(SystemZAddressingMode::Disp20Pair, A, B, D, 1);
779}
780bool SystemZDAGToDAGISel::selectBDAddr20off3(SDValue A, SDValue &B,
781 SDValue &D) const {
782 return selectBDAddr(SystemZAddressingMode::Disp20Pair, A, B, D, 3);
783}
784bool SystemZDAGToDAGISel::selectBDAddr20off7(SDValue A, SDValue &B,
785 SDValue &D) const {
786 return selectBDAddr(SystemZAddressingMode::Disp20Pair, A, B, D, 7);
787}
788
789bool SystemZDAGToDAGISel::detectOrAndInsertion(SDValue &Op,
790 uint64_t InsertMask) const {
791 // We're only interested in cases where the insertion is into some operand
792 // of Op, rather than into Op itself. The only useful case is an AND.
793 if (Op.getOpcode() != ISD::AND)
794 return false;
795
796 // We need a constant mask.
797 auto *MaskNode = dyn_cast<ConstantSDNode>(Op.getOperand(1).getNode());
798 if (!MaskNode)
799 return false;
800
801 // It's not an insertion of Op.getOperand(0) if the two masks overlap.
802 uint64_t AndMask = MaskNode->getZExtValue();
803 if (InsertMask & AndMask)
804 return false;
805
806 // It's only an insertion if all bits are covered or are known to be zero.
807 // The inner check covers all cases but is more expensive.
808 uint64_t Used = allOnes(Op.getValueSizeInBits());
809 if (Used != (AndMask | InsertMask)) {
810 KnownBits Known = CurDAG->computeKnownBits(Op.getOperand(0));
811 if (Used != (AndMask | InsertMask | Known.Zero.getZExtValue()))
812 return false;
813 }
814
815 Op = Op.getOperand(0);
816 return true;
817}
818
819bool SystemZDAGToDAGISel::refineRxSBGMask(RxSBGOperands &RxSBG,
820 uint64_t Mask) const {
821 const SystemZInstrInfo *TII = getInstrInfo();
822 if (RxSBG.Rotate != 0)
823 Mask = (Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate));
824 Mask &= RxSBG.Mask;
825 if (TII->isRxSBGMask(Mask, RxSBG.BitSize, RxSBG.Start, RxSBG.End)) {
826 RxSBG.Mask = Mask;
827 return true;
828 }
829 return false;
830}
831
832// Return true if any bits of (RxSBG.Input & Mask) are significant.
833static bool maskMatters(RxSBGOperands &RxSBG, uint64_t Mask) {
834 // Rotate the mask in the same way as RxSBG.Input is rotated.
835 if (RxSBG.Rotate != 0)
836 Mask = ((Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate)));
837 return (Mask & RxSBG.Mask) != 0;
838}
839
840bool SystemZDAGToDAGISel::expandRxSBG(RxSBGOperands &RxSBG) const {
841 SDValue N = RxSBG.Input;
842 unsigned Opcode = N.getOpcode();
843 switch (Opcode) {
844 case ISD::TRUNCATE: {
845 if (RxSBG.Opcode == SystemZ::RNSBG)
846 return false;
847 if (N.getOperand(0).getValueSizeInBits() > 64)
848 return false;
849 uint64_t BitSize = N.getValueSizeInBits();
850 uint64_t Mask = allOnes(BitSize);
851 if (!refineRxSBGMask(RxSBG, Mask))
852 return false;
853 RxSBG.Input = N.getOperand(0);
854 return true;
855 }
856 case ISD::AND: {
857 if (RxSBG.Opcode == SystemZ::RNSBG)
858 return false;
859
860 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
861 if (!MaskNode)
862 return false;
863
864 SDValue Input = N.getOperand(0);
865 uint64_t Mask = MaskNode->getZExtValue();
866 if (!refineRxSBGMask(RxSBG, Mask)) {
867 // If some bits of Input are already known zeros, those bits will have
868 // been removed from the mask. See if adding them back in makes the
869 // mask suitable.
870 KnownBits Known = CurDAG->computeKnownBits(Input);
871 Mask |= Known.Zero.getZExtValue();
872 if (!refineRxSBGMask(RxSBG, Mask))
873 return false;
874 }
875 RxSBG.Input = Input;
876 return true;
877 }
878
879 case ISD::OR: {
880 if (RxSBG.Opcode != SystemZ::RNSBG)
881 return false;
882
883 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
884 if (!MaskNode)
885 return false;
886
887 SDValue Input = N.getOperand(0);
888 uint64_t Mask = ~MaskNode->getZExtValue();
889 if (!refineRxSBGMask(RxSBG, Mask)) {
890 // If some bits of Input are already known ones, those bits will have
891 // been removed from the mask. See if adding them back in makes the
892 // mask suitable.
893 KnownBits Known = CurDAG->computeKnownBits(Input);
894 Mask &= ~Known.One.getZExtValue();
895 if (!refineRxSBGMask(RxSBG, Mask))
896 return false;
897 }
898 RxSBG.Input = Input;
899 return true;
900 }
901
902 case ISD::ROTL: {
903 // Any 64-bit rotate left can be merged into the RxSBG.
904 if (RxSBG.BitSize != 64 || N.getValueType() != MVT::i64)
905 return false;
906 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
907 if (!CountNode)
908 return false;
909
910 RxSBG.Rotate = (RxSBG.Rotate + CountNode->getZExtValue()) & 63;
911 RxSBG.Input = N.getOperand(0);
912 return true;
913 }
914
915 case ISD::ANY_EXTEND:
916 // Bits above the extended operand are don't-care.
917 RxSBG.Input = N.getOperand(0);
918 return true;
919
920 case ISD::ZERO_EXTEND:
921 if (RxSBG.Opcode != SystemZ::RNSBG) {
922 // Restrict the mask to the extended operand.
923 unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits();
924 if (!refineRxSBGMask(RxSBG, allOnes(InnerBitSize)))
925 return false;
926
927 RxSBG.Input = N.getOperand(0);
928 return true;
929 }
930 [[fallthrough]];
931
932 case ISD::SIGN_EXTEND: {
933 // Check that the extension bits are don't-care (i.e. are masked out
934 // by the final mask).
935 unsigned BitSize = N.getValueSizeInBits();
936 unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits();
937 if (maskMatters(RxSBG, allOnes(BitSize) - allOnes(InnerBitSize))) {
938 // In the case where only the sign bit is active, increase Rotate with
939 // the extension width.
940 if (RxSBG.Mask == 1 && RxSBG.Rotate == 1)
941 RxSBG.Rotate += (BitSize - InnerBitSize);
942 else
943 return false;
944 }
945
946 RxSBG.Input = N.getOperand(0);
947 return true;
948 }
949
950 case ISD::SHL: {
951 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
952 if (!CountNode)
953 return false;
954
955 uint64_t Count = CountNode->getZExtValue();
956 unsigned BitSize = N.getValueSizeInBits();
957 if (Count < 1 || Count >= BitSize)
958 return false;
959
960 if (RxSBG.Opcode == SystemZ::RNSBG) {
961 // Treat (shl X, count) as (rotl X, size-count) as long as the bottom
962 // count bits from RxSBG.Input are ignored.
963 if (maskMatters(RxSBG, allOnes(Count)))
964 return false;
965 } else {
966 // Treat (shl X, count) as (and (rotl X, count), ~0<<count).
967 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count) << Count))
968 return false;
969 }
970
971 RxSBG.Rotate = (RxSBG.Rotate + Count) & 63;
972 RxSBG.Input = N.getOperand(0);
973 return true;
974 }
975
976 case ISD::SRL:
977 case ISD::SRA: {
978 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
979 if (!CountNode)
980 return false;
981
982 uint64_t Count = CountNode->getZExtValue();
983 unsigned BitSize = N.getValueSizeInBits();
984 if (Count < 1 || Count >= BitSize)
985 return false;
986
987 if (RxSBG.Opcode == SystemZ::RNSBG || Opcode == ISD::SRA) {
988 // Treat (srl|sra X, count) as (rotl X, size-count) as long as the top
989 // count bits from RxSBG.Input are ignored.
990 if (maskMatters(RxSBG, allOnes(Count) << (BitSize - Count)))
991 return false;
992 } else {
993 // Treat (srl X, count), mask) as (and (rotl X, size-count), ~0>>count),
994 // which is similar to SLL above.
995 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count)))
996 return false;
997 }
998
999 RxSBG.Rotate = (RxSBG.Rotate - Count) & 63;
1000 RxSBG.Input = N.getOperand(0);
1001 return true;
1002 }
1003 default:
1004 return false;
1005 }
1006}
1007
1008SDValue SystemZDAGToDAGISel::getUNDEF(const SDLoc &DL, EVT VT) const {
1009 SDNode *N = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT);
1010 return SDValue(N, 0);
1011}
1012
1013SDValue SystemZDAGToDAGISel::convertTo(const SDLoc &DL, EVT VT,
1014 SDValue N) const {
1015 if (N.getValueType() == MVT::i32 && VT == MVT::i64)
1016 return CurDAG->getTargetInsertSubreg(SystemZ::subreg_l32,
1017 DL, VT, getUNDEF(DL, MVT::i64), N);
1018 if (N.getValueType() == MVT::i64 && VT == MVT::i32)
1019 return CurDAG->getTargetExtractSubreg(SystemZ::subreg_l32, DL, VT, N);
1020 assert(N.getValueType() == VT && "Unexpected value types");
1021 return N;
1022}
1023
1024bool SystemZDAGToDAGISel::tryRISBGZero(SDNode *N) {
1025 SDLoc DL(N);
1026 EVT VT = N->getValueType(0);
1027 if (!VT.isInteger() || VT.getSizeInBits() > 64)
1028 return false;
1029 RxSBGOperands RISBG(SystemZ::RISBG, SDValue(N, 0));
1030 unsigned Count = 0;
1031 while (expandRxSBG(RISBG))
1032 // The widening or narrowing is expected to be free.
1033 // Counting widening or narrowing as a saved operation will result in
1034 // preferring an R*SBG over a simple shift/logical instruction.
1035 if (RISBG.Input.getOpcode() != ISD::ANY_EXTEND &&
1036 RISBG.Input.getOpcode() != ISD::TRUNCATE)
1037 Count += 1;
1038 if (Count == 0 || isa<ConstantSDNode>(RISBG.Input))
1039 return false;
1040
1041 // Prefer to use normal shift instructions over RISBG, since they can handle
1042 // all cases and are sometimes shorter.
1043 if (Count == 1 && N->getOpcode() != ISD::AND)
1044 return false;
1045
1046 // Prefer LOAD LOGICAL INDEXED ADDRESS over RISBG in the case where we
1047 // can use its displacement to pull in an addition.
1048 if (Subtarget->hasMiscellaneousExtensions4() &&
1049 RISBG.Rotate >= 1 && RISBG.Rotate <= 4 &&
1050 RISBG.Mask == (((uint64_t)1 << 32) - 1) << RISBG.Rotate &&
1051 RISBG.Input.getOpcode() == ISD::ADD)
1052 if (auto *C = dyn_cast<ConstantSDNode>(RISBG.Input.getOperand(1)))
1053 if (isInt<20>(C->getSExtValue()))
1054 return false;
1055
1056 // Prefer register extensions like LLC over RISBG. Also prefer to start
1057 // out with normal ANDs if one instruction would be enough. We can convert
1058 // these ANDs into an RISBG later if a three-address instruction is useful.
1059 if (RISBG.Rotate == 0) {
1060 bool PreferAnd = false;
1061 // Prefer AND for any 32-bit and-immediate operation.
1062 if (VT == MVT::i32)
1063 PreferAnd = true;
1064 // As well as for any 64-bit operation that can be implemented via LLC(R),
1065 // LLH(R), LLGT(R), or one of the and-immediate instructions.
1066 else if (RISBG.Mask == 0xff ||
1067 RISBG.Mask == 0xffff ||
1068 RISBG.Mask == 0x7fffffff ||
1069 SystemZ::isImmLF(~RISBG.Mask) ||
1070 SystemZ::isImmHF(~RISBG.Mask))
1071 PreferAnd = true;
1072 // And likewise for the LLZRGF instruction, which doesn't have a register
1073 // to register version.
1074 else if (auto *Load = dyn_cast<LoadSDNode>(RISBG.Input)) {
1075 if (Load->getMemoryVT() == MVT::i32 &&
1076 (Load->getExtensionType() == ISD::EXTLOAD ||
1077 Load->getExtensionType() == ISD::ZEXTLOAD) &&
1078 RISBG.Mask == 0xffffff00 &&
1079 Subtarget->hasLoadAndZeroRightmostByte())
1080 PreferAnd = true;
1081 }
1082 if (PreferAnd) {
1083 // Replace the current node with an AND. Note that the current node
1084 // might already be that same AND, in which case it is already CSE'd
1085 // with it, and we must not call ReplaceNode.
1086 SDValue In = convertTo(DL, VT, RISBG.Input);
1087 SDValue Mask = CurDAG->getConstant(RISBG.Mask, DL, VT);
1088 SDValue New = CurDAG->getNode(ISD::AND, DL, VT, In, Mask);
1089 if (N != New.getNode()) {
1090 insertDAGNode(CurDAG, N, Mask);
1091 insertDAGNode(CurDAG, N, New);
1092 ReplaceNode(N, New.getNode());
1093 N = New.getNode();
1094 }
1095 // Now, select the machine opcode to implement this operation.
1096 if (!N->isMachineOpcode())
1097 SelectCode(N);
1098 return true;
1099 }
1100 }
1101
1102 unsigned Opcode = SystemZ::RISBG;
1103 // Prefer RISBGN if available, since it does not clobber CC.
1104 if (Subtarget->hasMiscellaneousExtensions())
1105 Opcode = SystemZ::RISBGN;
1106 EVT OpcodeVT = MVT::i64;
1107 if (VT == MVT::i32 && Subtarget->hasHighWord() &&
1108 // We can only use the 32-bit instructions if all source bits are
1109 // in the low 32 bits without wrapping, both after rotation (because
1110 // of the smaller range for Start and End) and before rotation
1111 // (because the input value is truncated).
1112 RISBG.Start >= 32 && RISBG.End >= RISBG.Start &&
1113 ((RISBG.Start + RISBG.Rotate) & 63) >= 32 &&
1114 ((RISBG.End + RISBG.Rotate) & 63) >=
1115 ((RISBG.Start + RISBG.Rotate) & 63)) {
1116 Opcode = SystemZ::RISBMux;
1117 OpcodeVT = MVT::i32;
1118 RISBG.Start &= 31;
1119 RISBG.End &= 31;
1120 }
1121 SDValue Ops[5] = {
1122 getUNDEF(DL, OpcodeVT),
1123 convertTo(DL, OpcodeVT, RISBG.Input),
1124 CurDAG->getTargetConstant(RISBG.Start, DL, MVT::i32),
1125 CurDAG->getTargetConstant(RISBG.End | 128, DL, MVT::i32),
1126 CurDAG->getTargetConstant(RISBG.Rotate, DL, MVT::i32)
1127 };
1129 DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, OpcodeVT, Ops), 0));
1130 ReplaceNode(N, New.getNode());
1131 return true;
1132}
1133
1134bool SystemZDAGToDAGISel::tryRxSBG(SDNode *N, unsigned Opcode) {
1135 SDLoc DL(N);
1136 EVT VT = N->getValueType(0);
1137 if (!VT.isInteger() || VT.getSizeInBits() > 64)
1138 return false;
1139 // Try treating each operand of N as the second operand of the RxSBG
1140 // and see which goes deepest.
1141 RxSBGOperands RxSBG[] = {
1142 RxSBGOperands(Opcode, N->getOperand(0)),
1143 RxSBGOperands(Opcode, N->getOperand(1))
1144 };
1145 unsigned Count[] = { 0, 0 };
1146 for (unsigned I = 0; I < 2; ++I)
1147 while (RxSBG[I].Input->hasOneUse() && expandRxSBG(RxSBG[I]))
1148 // In cases of multiple users it seems better to keep the simple
1149 // instruction as they are one cycle faster, and it also helps in cases
1150 // where both inputs share a common node.
1151 // The widening or narrowing is expected to be free. Counting widening
1152 // or narrowing as a saved operation will result in preferring an R*SBG
1153 // over a simple shift/logical instruction.
1154 if (RxSBG[I].Input.getOpcode() != ISD::ANY_EXTEND &&
1155 RxSBG[I].Input.getOpcode() != ISD::TRUNCATE)
1156 Count[I] += 1;
1157
1158 // Do nothing if neither operand is suitable.
1159 if (Count[0] == 0 && Count[1] == 0)
1160 return false;
1161
1162 // Pick the deepest second operand.
1163 unsigned I = Count[0] > Count[1] ? 0 : 1;
1164 SDValue Op0 = N->getOperand(I ^ 1);
1165
1166 // Prefer IC for character insertions from memory.
1167 if (Opcode == SystemZ::ROSBG && (RxSBG[I].Mask & 0xff) == 0)
1168 if (auto *Load = dyn_cast<LoadSDNode>(Op0.getNode()))
1169 if (Load->getMemoryVT() == MVT::i8)
1170 return false;
1171
1172 // See whether we can avoid an AND in the first operand by converting
1173 // ROSBG to RISBG.
1174 if (Opcode == SystemZ::ROSBG && detectOrAndInsertion(Op0, RxSBG[I].Mask)) {
1175 Opcode = SystemZ::RISBG;
1176 // Prefer RISBGN if available, since it does not clobber CC.
1177 if (Subtarget->hasMiscellaneousExtensions())
1178 Opcode = SystemZ::RISBGN;
1179 }
1180
1181 SDValue Ops[5] = {
1182 convertTo(DL, MVT::i64, Op0),
1183 convertTo(DL, MVT::i64, RxSBG[I].Input),
1184 CurDAG->getTargetConstant(RxSBG[I].Start, DL, MVT::i32),
1185 CurDAG->getTargetConstant(RxSBG[I].End, DL, MVT::i32),
1186 CurDAG->getTargetConstant(RxSBG[I].Rotate, DL, MVT::i32)
1187 };
1189 DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, MVT::i64, Ops), 0));
1190 ReplaceNode(N, New.getNode());
1191 return true;
1192}
1193
1194void SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node,
1195 SDValue Op0, uint64_t UpperVal,
1196 uint64_t LowerVal) {
1197 EVT VT = Node->getValueType(0);
1198 SDLoc DL(Node);
1199 SDValue Upper = CurDAG->getConstant(UpperVal, DL, VT);
1200 if (Op0.getNode())
1201 Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper);
1202
1203 {
1204 // When we haven't passed in Op0, Upper will be a constant. In order to
1205 // prevent folding back to the large immediate in `Or = getNode(...)` we run
1206 // SelectCode first and end up with an opaque machine node. This means that
1207 // we need to use a handle to keep track of Upper in case it gets CSE'd by
1208 // SelectCode.
1209 //
1210 // Note that in the case where Op0 is passed in we could just call
1211 // SelectCode(Upper) later, along with the SelectCode(Or), and avoid needing
1212 // the handle at all, but it's fine to do it here.
1213 //
1214 // TODO: This is a pretty hacky way to do this. Can we do something that
1215 // doesn't require a two paragraph explanation?
1216 HandleSDNode Handle(Upper);
1217 SelectCode(Upper.getNode());
1218 Upper = Handle.getValue();
1219 }
1220
1221 SDValue Lower = CurDAG->getConstant(LowerVal, DL, VT);
1222 SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower);
1223
1224 ReplaceNode(Node, Or.getNode());
1225
1226 SelectCode(Or.getNode());
1227}
1228
1229void SystemZDAGToDAGISel::loadVectorConstant(
1230 const SystemZVectorConstantInfo &VCI, SDNode *Node) {
1231 assert((VCI.Opcode == SystemZISD::BYTE_MASK ||
1232 VCI.Opcode == SystemZISD::REPLICATE ||
1233 VCI.Opcode == SystemZISD::ROTATE_MASK) &&
1234 "Bad opcode!");
1235 assert(VCI.VecVT.getSizeInBits() == 128 && "Expected a vector type");
1236 EVT VT = Node->getValueType(0);
1237 SDLoc DL(Node);
1239 for (unsigned OpVal : VCI.OpVals)
1240 Ops.push_back(CurDAG->getTargetConstant(OpVal, DL, MVT::i32));
1241 SDValue Op = CurDAG->getNode(VCI.Opcode, DL, VCI.VecVT, Ops);
1242
1243 if (VCI.VecVT == VT.getSimpleVT())
1244 ReplaceNode(Node, Op.getNode());
1245 else if (VT.getSizeInBits() == 128) {
1246 SDValue BitCast = CurDAG->getNode(ISD::BITCAST, DL, VT, Op);
1247 ReplaceNode(Node, BitCast.getNode());
1248 SelectCode(BitCast.getNode());
1249 } else { // half, float or double
1250 unsigned SubRegIdx = (VT.getSizeInBits() == 16 ? SystemZ::subreg_h16
1251 : VT.getSizeInBits() == 32 ? SystemZ::subreg_h32
1252 : SystemZ::subreg_h64);
1253 ReplaceNode(
1254 Node, CurDAG->getTargetExtractSubreg(SubRegIdx, DL, VT, Op).getNode());
1255 }
1256 SelectCode(Op.getNode());
1257}
1258
1259SDNode *SystemZDAGToDAGISel::loadPoolVectorConstant(APInt Val, EVT VT, SDLoc DL) {
1260 SDNode *ResNode;
1261 assert (VT.getSizeInBits() == 128);
1262
1263 SDValue CP = CurDAG->getTargetConstantPool(
1264 ConstantInt::get(Type::getInt128Ty(*CurDAG->getContext()), Val),
1265 TLI->getPointerTy(CurDAG->getDataLayout()));
1266
1267 EVT PtrVT = CP.getValueType();
1268 SDValue Ops[] = {
1269 SDValue(CurDAG->getMachineNode(SystemZ::LARL, DL, PtrVT, CP), 0),
1270 CurDAG->getTargetConstant(0, DL, PtrVT),
1271 CurDAG->getRegister(0, PtrVT),
1272 CurDAG->getEntryNode()
1273 };
1274 ResNode = CurDAG->getMachineNode(SystemZ::VL, DL, VT, MVT::Other, Ops);
1275
1276 // Annotate ResNode with memory operand information so that MachineInstr
1277 // queries work properly. This e.g. gives the register allocation the
1278 // required information for rematerialization.
1279 MachineFunction& MF = CurDAG->getMachineFunction();
1280 MachineMemOperand *MemOp =
1283
1284 CurDAG->setNodeMemRefs(cast<MachineSDNode>(ResNode), {MemOp});
1285 return ResNode;
1286}
1287
1288bool SystemZDAGToDAGISel::tryGather(SDNode *N, unsigned Opcode) {
1289 SDValue ElemV = N->getOperand(2);
1290 auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
1291 if (!ElemN)
1292 return false;
1293
1294 unsigned Elem = ElemN->getZExtValue();
1295 EVT VT = N->getValueType(0);
1296 if (Elem >= VT.getVectorNumElements())
1297 return false;
1298
1299 auto *Load = dyn_cast<LoadSDNode>(N->getOperand(1));
1300 if (!Load || !Load->hasNUsesOfValue(1, 0))
1301 return false;
1302 if (Load->getMemoryVT().getSizeInBits() !=
1303 Load->getValueType(0).getSizeInBits())
1304 return false;
1305
1306 SDValue Base, Disp, Index;
1307 if (!selectBDVAddr12Only(Load->getBasePtr(), ElemV, Base, Disp, Index) ||
1308 Index.getValueType() != VT.changeVectorElementTypeToInteger())
1309 return false;
1310
1311 SDLoc DL(Load);
1312 SDValue Ops[] = {
1313 N->getOperand(0), Base, Disp, Index,
1314 CurDAG->getTargetConstant(Elem, DL, MVT::i32), Load->getChain()
1315 };
1316 SDNode *Res = CurDAG->getMachineNode(Opcode, DL, VT, MVT::Other, Ops);
1317 ReplaceUses(SDValue(Load, 1), SDValue(Res, 1));
1318 ReplaceNode(N, Res);
1319 return true;
1320}
1321
1322bool SystemZDAGToDAGISel::tryScatter(StoreSDNode *Store, unsigned Opcode) {
1323 SDValue Value = Store->getValue();
1324 if (Value.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
1325 return false;
1326 if (Store->getMemoryVT().getSizeInBits() != Value.getValueSizeInBits())
1327 return false;
1328
1329 SDValue ElemV = Value.getOperand(1);
1330 auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
1331 if (!ElemN)
1332 return false;
1333
1334 SDValue Vec = Value.getOperand(0);
1335 EVT VT = Vec.getValueType();
1336 unsigned Elem = ElemN->getZExtValue();
1337 if (Elem >= VT.getVectorNumElements())
1338 return false;
1339
1340 SDValue Base, Disp, Index;
1341 if (!selectBDVAddr12Only(Store->getBasePtr(), ElemV, Base, Disp, Index) ||
1342 Index.getValueType() != VT.changeVectorElementTypeToInteger())
1343 return false;
1344
1345 SDLoc DL(Store);
1346 SDValue Ops[] = {
1347 Vec, Base, Disp, Index, CurDAG->getTargetConstant(Elem, DL, MVT::i32),
1348 Store->getChain()
1349 };
1350 ReplaceNode(Store, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops));
1351 return true;
1352}
1353
1354// Check whether or not the chain ending in StoreNode is suitable for doing
1355// the {load; op; store} to modify transformation.
1357 SDValue StoredVal, SelectionDAG *CurDAG,
1358 LoadSDNode *&LoadNode,
1359 SDValue &InputChain) {
1360 // Is the stored value result 0 of the operation?
1361 if (StoredVal.getResNo() != 0)
1362 return false;
1363
1364 // Are there other uses of the loaded value than the operation?
1365 if (!StoredVal.getNode()->hasNUsesOfValue(1, 0))
1366 return false;
1367
1368 // Is the store non-extending and non-indexed?
1369 if (!ISD::isNormalStore(StoreNode) || StoreNode->isNonTemporal())
1370 return false;
1371
1372 SDValue Load = StoredVal->getOperand(0);
1373 // Is the stored value a non-extending and non-indexed load?
1374 if (!ISD::isNormalLoad(Load.getNode()))
1375 return false;
1376
1377 // Return LoadNode by reference.
1378 LoadNode = cast<LoadSDNode>(Load);
1379
1380 // Is store the only read of the loaded value?
1381 if (!Load.hasOneUse())
1382 return false;
1383
1384 // Is the address of the store the same as the load?
1385 if (LoadNode->getBasePtr() != StoreNode->getBasePtr() ||
1386 LoadNode->getOffset() != StoreNode->getOffset())
1387 return false;
1388
1389 // Check if the chain is produced by the load or is a TokenFactor with
1390 // the load output chain as an operand. Return InputChain by reference.
1391 SDValue Chain = StoreNode->getChain();
1392
1393 bool ChainCheck = false;
1394 if (Chain == Load.getValue(1)) {
1395 ChainCheck = true;
1396 InputChain = LoadNode->getChain();
1397 } else if (Chain.getOpcode() == ISD::TokenFactor) {
1398 SmallVector<SDValue, 4> ChainOps;
1399 SmallVector<const SDNode *, 4> LoopWorklist;
1401 const unsigned int Max = 1024;
1402 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i) {
1403 SDValue Op = Chain.getOperand(i);
1404 if (Op == Load.getValue(1)) {
1405 ChainCheck = true;
1406 // Drop Load, but keep its chain. No cycle check necessary.
1407 ChainOps.push_back(Load.getOperand(0));
1408 continue;
1409 }
1410 LoopWorklist.push_back(Op.getNode());
1411 ChainOps.push_back(Op);
1412 }
1413
1414 if (ChainCheck) {
1415 // Add the other operand of StoredVal to worklist.
1416 for (SDValue Op : StoredVal->ops())
1417 if (Op.getNode() != LoadNode)
1418 LoopWorklist.push_back(Op.getNode());
1419
1420 // Check if Load is reachable from any of the nodes in the worklist.
1421 if (SDNode::hasPredecessorHelper(Load.getNode(), Visited, LoopWorklist, Max,
1422 true))
1423 return false;
1424
1425 // Make a new TokenFactor with all the other input chains except
1426 // for the load.
1427 InputChain = CurDAG->getNode(ISD::TokenFactor, SDLoc(Chain),
1428 MVT::Other, ChainOps);
1429 }
1430 }
1431 if (!ChainCheck)
1432 return false;
1433
1434 return true;
1435}
1436
1437// Change a chain of {load; op; store} of the same value into a simple op
1438// through memory of that value, if the uses of the modified value and its
1439// address are suitable.
1440//
1441// The tablegen pattern memory operand pattern is currently not able to match
1442// the case where the CC on the original operation are used.
1443//
1444// See the equivalent routine in X86ISelDAGToDAG for further comments.
1445bool SystemZDAGToDAGISel::tryFoldLoadStoreIntoMemOperand(SDNode *Node) {
1446 StoreSDNode *StoreNode = cast<StoreSDNode>(Node);
1447 SDValue StoredVal = StoreNode->getOperand(1);
1448 unsigned Opc = StoredVal->getOpcode();
1449 SDLoc DL(StoreNode);
1450
1451 // Before we try to select anything, make sure this is memory operand size
1452 // and opcode we can handle. Note that this must match the code below that
1453 // actually lowers the opcodes.
1454 EVT MemVT = StoreNode->getMemoryVT();
1455 unsigned NewOpc = 0;
1456 bool NegateOperand = false;
1457 switch (Opc) {
1458 default:
1459 return false;
1460 case SystemZISD::SSUBO:
1461 NegateOperand = true;
1462 [[fallthrough]];
1463 case SystemZISD::SADDO:
1464 if (MemVT == MVT::i32)
1465 NewOpc = SystemZ::ASI;
1466 else if (MemVT == MVT::i64)
1467 NewOpc = SystemZ::AGSI;
1468 else
1469 return false;
1470 break;
1471 case SystemZISD::USUBO:
1472 NegateOperand = true;
1473 [[fallthrough]];
1474 case SystemZISD::UADDO:
1475 if (MemVT == MVT::i32)
1476 NewOpc = SystemZ::ALSI;
1477 else if (MemVT == MVT::i64)
1478 NewOpc = SystemZ::ALGSI;
1479 else
1480 return false;
1481 break;
1482 }
1483
1484 LoadSDNode *LoadNode = nullptr;
1485 SDValue InputChain;
1486 if (!isFusableLoadOpStorePattern(StoreNode, StoredVal, CurDAG, LoadNode,
1487 InputChain))
1488 return false;
1489
1490 SDValue Operand = StoredVal.getOperand(1);
1491 auto *OperandC = dyn_cast<ConstantSDNode>(Operand);
1492 if (!OperandC)
1493 return false;
1494 auto OperandV = OperandC->getAPIntValue();
1495 if (NegateOperand)
1496 OperandV = -OperandV;
1497 if (OperandV.getSignificantBits() > 8)
1498 return false;
1499 Operand = CurDAG->getTargetConstant(OperandV, DL, MemVT);
1500
1501 SDValue Base, Disp;
1502 if (!selectBDAddr20Only(StoreNode->getBasePtr(), Base, Disp))
1503 return false;
1504
1505 SDValue Ops[] = { Base, Disp, Operand, InputChain };
1506 MachineSDNode *Result =
1507 CurDAG->getMachineNode(NewOpc, DL, MVT::i32, MVT::Other, Ops);
1508 CurDAG->setNodeMemRefs(
1509 Result, {StoreNode->getMemOperand(), LoadNode->getMemOperand()});
1510
1511 ReplaceUses(SDValue(StoreNode, 0), SDValue(Result, 1));
1512 ReplaceUses(SDValue(StoredVal.getNode(), 1), SDValue(Result, 0));
1513 CurDAG->RemoveDeadNode(Node);
1514 return true;
1515}
1516
1517bool SystemZDAGToDAGISel::canUseBlockOperation(StoreSDNode *Store,
1518 LoadSDNode *Load) const {
1519 // Check that the two memory operands have the same size.
1520 if (Load->getMemoryVT() != Store->getMemoryVT())
1521 return false;
1522
1523 // Volatility stops an access from being decomposed.
1524 if (Load->isVolatile() || Store->isVolatile())
1525 return false;
1526
1527 // There's no chance of overlap if the load is invariant.
1528 if (Load->isInvariant() && Load->isDereferenceable())
1529 return true;
1530
1531 // Otherwise we need to check whether there's an alias.
1532 const Value *V1 = Load->getMemOperand()->getValue();
1533 const Value *V2 = Store->getMemOperand()->getValue();
1534 if (!V1 || !V2)
1535 return false;
1536
1537 // Reject equality.
1538 uint64_t Size = Load->getMemoryVT().getStoreSize();
1539 int64_t End1 = Load->getSrcValueOffset() + Size;
1540 int64_t End2 = Store->getSrcValueOffset() + Size;
1541 if (V1 == V2 && End1 == End2)
1542 return false;
1543
1544 return BatchAA->isNoAlias(MemoryLocation(V1, End1, Load->getAAInfo()),
1545 MemoryLocation(V2, End2, Store->getAAInfo()));
1546}
1547
1548bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const {
1549 auto *Store = cast<StoreSDNode>(N);
1550 auto *Load = cast<LoadSDNode>(Store->getValue());
1551
1552 // Prefer not to use MVC if either address can use ... RELATIVE LONG
1553 // instructions.
1554 uint64_t Size = Load->getMemoryVT().getStoreSize();
1555 if (Size > 1 && Size <= 8) {
1556 // Prefer LHRL, LRL and LGRL.
1557 if (SystemZISD::isPCREL(Load->getBasePtr().getOpcode()))
1558 return false;
1559 // Prefer STHRL, STRL and STGRL.
1560 if (SystemZISD::isPCREL(Store->getBasePtr().getOpcode()))
1561 return false;
1562 }
1563
1564 return canUseBlockOperation(Store, Load);
1565}
1566
1567bool SystemZDAGToDAGISel::storeLoadCanUseBlockBinary(SDNode *N,
1568 unsigned I) const {
1569 auto *StoreA = cast<StoreSDNode>(N);
1570 auto *LoadA = cast<LoadSDNode>(StoreA->getValue().getOperand(1 - I));
1571 auto *LoadB = cast<LoadSDNode>(StoreA->getValue().getOperand(I));
1572 return !LoadA->isVolatile() && LoadA->getMemoryVT() == LoadB->getMemoryVT() &&
1573 canUseBlockOperation(StoreA, LoadB);
1574}
1575
1576bool SystemZDAGToDAGISel::storeLoadIsAligned(SDNode *N) const {
1577
1578 auto *MemAccess = cast<MemSDNode>(N);
1579 auto *LdSt = dyn_cast<LSBaseSDNode>(MemAccess);
1580 TypeSize StoreSize = MemAccess->getMemoryVT().getStoreSize();
1581 SDValue BasePtr = MemAccess->getBasePtr();
1582 MachineMemOperand *MMO = MemAccess->getMemOperand();
1583 assert(MMO && "Expected a memory operand.");
1584
1585 // The memory access must have a proper alignment and no index register.
1586 // Only load and store nodes have the offset operand (atomic loads do not).
1587 if (MemAccess->getAlign().value() < StoreSize ||
1588 (LdSt && !LdSt->getOffset().isUndef()))
1589 return false;
1590
1591 // The MMO must not have an unaligned offset.
1592 if (MMO->getOffset() % StoreSize != 0)
1593 return false;
1594
1595 // An access to GOT or the Constant Pool is aligned.
1596 if (const PseudoSourceValue *PSV = MMO->getPseudoValue())
1597 if ((PSV->isGOT() || PSV->isConstantPool()))
1598 return true;
1599
1600 // Check the alignment of a Global Address.
1601 if (BasePtr.getNumOperands())
1602 if (GlobalAddressSDNode *GA =
1603 dyn_cast<GlobalAddressSDNode>(BasePtr.getOperand(0))) {
1604 // The immediate offset must be aligned.
1605 if (GA->getOffset() % StoreSize != 0)
1606 return false;
1607
1608 // The alignment of the symbol itself must be at least the store size.
1609 const GlobalValue *GV = GA->getGlobal();
1610 const DataLayout &DL = GV->getDataLayout();
1611 if (GV->getPointerAlignment(DL).value() < StoreSize)
1612 return false;
1613 }
1614
1615 return true;
1616}
1617
1618ISD::LoadExtType SystemZDAGToDAGISel::getLoadExtType(SDNode *N) const {
1619 ISD::LoadExtType ETy;
1620 if (auto *L = dyn_cast<LoadSDNode>(N))
1621 ETy = L->getExtensionType();
1622 else if (auto *AL = dyn_cast<AtomicSDNode>(N))
1623 ETy = AL->getExtensionType();
1624 else
1625 llvm_unreachable("Unkown load node type.");
1626 return ETy;
1627}
1628
1629void SystemZDAGToDAGISel::Select(SDNode *Node) {
1630 // If we have a custom node, we already have selected!
1631 if (Node->isMachineOpcode()) {
1632 LLVM_DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
1633 Node->setNodeId(-1);
1634 return;
1635 }
1636
1637 unsigned Opcode = Node->getOpcode();
1638 switch (Opcode) {
1639 case ISD::OR:
1640 if (Node->getOperand(1).getOpcode() != ISD::Constant)
1641 if (tryRxSBG(Node, SystemZ::ROSBG))
1642 return;
1643 goto or_xor;
1644
1645 case ISD::XOR:
1646 if (Node->getOperand(1).getOpcode() != ISD::Constant)
1647 if (tryRxSBG(Node, SystemZ::RXSBG))
1648 return;
1649 // Fall through.
1650 or_xor:
1651 // If this is a 64-bit operation in which both 32-bit halves are nonzero,
1652 // split the operation into two. If both operands here happen to be
1653 // constant, leave this to common code to optimize.
1654 if (Node->getValueType(0) == MVT::i64 &&
1655 Node->getOperand(0).getOpcode() != ISD::Constant)
1656 if (auto *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) {
1657 uint64_t Val = Op1->getZExtValue();
1658 // Don't split the operation if we can match one of the combined
1659 // logical operations provided by miscellaneous-extensions-3.
1660 if (Subtarget->hasMiscellaneousExtensions3()) {
1661 unsigned ChildOpcode = Node->getOperand(0).getOpcode();
1662 // Check whether this expression matches NAND/NOR/NXOR.
1663 if (Val == (uint64_t)-1 && Opcode == ISD::XOR)
1664 if (ChildOpcode == ISD::AND || ChildOpcode == ISD::OR ||
1665 ChildOpcode == ISD::XOR)
1666 break;
1667 // Check whether this expression matches OR-with-complement
1668 // (or matches an alternate pattern for NXOR).
1669 if (ChildOpcode == ISD::XOR) {
1670 auto Op0 = Node->getOperand(0);
1671 if (auto *Op0Op1 = dyn_cast<ConstantSDNode>(Op0->getOperand(1)))
1672 if (Op0Op1->getZExtValue() == (uint64_t)-1)
1673 break;
1674 }
1675 }
1676 // Don't split an XOR with -1 as LCGR/AGHI is more compact.
1677 if (Opcode == ISD::XOR && Op1->isAllOnes())
1678 break;
1679 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val)) {
1680 splitLargeImmediate(Opcode, Node, Node->getOperand(0),
1681 Val - uint32_t(Val), uint32_t(Val));
1682 return;
1683 }
1684 }
1685 break;
1686
1687 case ISD::AND:
1688 if (Node->getOperand(1).getOpcode() != ISD::Constant) {
1689 if (tryRxSBG(Node, SystemZ::RNSBG))
1690 return;
1691 } else {
1692 // Use patterns for zero-extending of vector element extraction.
1693 if (Node->getValueType(0) == MVT::i64 &&
1694 Node->getOperand(0)->getOpcode() == ISD::ANY_EXTEND) {
1695 SDValue Input = Node->getOperand(0)->getOperand(0);
1696 uint64_t Mask =
1697 cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
1698 if (Input->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1699 EVT VecVT = Input->getOperand(0)->getValueType(0);
1700 unsigned EltBits = VecVT.getScalarSizeInBits();
1701 if (allOnes(EltBits) == Mask)
1702 break;
1703 }
1704 }
1705 }
1706
1707 [[fallthrough]];
1708 case ISD::ROTL:
1709 case ISD::SHL:
1710 case ISD::SRL:
1711 case ISD::ZERO_EXTEND:
1712 if (tryRISBGZero(Node))
1713 return;
1714 break;
1715
1716 case ISD::BSWAP:
1717 if (Node->getValueType(0) == MVT::i128) {
1718 SDLoc DL(Node);
1719 SDValue Src = Node->getOperand(0);
1720 Src = CurDAG->getNode(ISD::BITCAST, DL, MVT::v16i8, Src);
1721
1722 uint64_t Bytes[2] = { 0x0706050403020100ULL, 0x0f0e0d0c0b0a0908ULL };
1723 SDNode *Mask = loadPoolVectorConstant(APInt(128, Bytes), MVT::v16i8, DL);
1724 SDValue Ops[] = { Src, Src, SDValue(Mask, 0) };
1725 SDValue Res = SDValue(CurDAG->getMachineNode(SystemZ::VPERM, DL,
1726 MVT::v16i8, Ops), 0);
1727
1728 Res = CurDAG->getNode(ISD::BITCAST, DL, MVT::i128, Res);
1729 SDNode *ResNode = Res.getNode();
1730 ReplaceNode(Node, ResNode);
1731 SelectCode(Src.getNode());
1732 SelectCode(ResNode);
1733 return;
1734 }
1735 break;
1736
1737 case ISD::Constant:
1738 // If this is a 64-bit constant that is out of the range of LLILF,
1739 // LLIHF and LGFI, split it into two 32-bit pieces.
1740 if (Node->getValueType(0) == MVT::i64) {
1741 uint64_t Val = Node->getAsZExtVal();
1742 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val)) {
1743 splitLargeImmediate(ISD::OR, Node, SDValue(), Val - uint32_t(Val),
1744 uint32_t(Val));
1745 return;
1746 }
1747 }
1748 if (Node->getValueType(0) == MVT::i128) {
1749 const APInt &Val = Node->getAsAPIntVal();
1750 SystemZVectorConstantInfo VCI(Val);
1751 if (VCI.isVectorConstantLegal(*Subtarget)) {
1752 loadVectorConstant(VCI, Node);
1753 return;
1754 }
1755 // If we can't materialize the constant we need to use a literal pool.
1756 SDNode *ResNode = loadPoolVectorConstant(Val, MVT::i128, SDLoc(Node));
1757 ReplaceNode(Node, ResNode);
1758 return;
1759 }
1760 break;
1761
1762 case SystemZISD::SELECT_CCMASK: {
1763 SDValue Op0 = Node->getOperand(0);
1764 SDValue Op1 = Node->getOperand(1);
1765 // Prefer to put any load first, so that it can be matched as a
1766 // conditional load. Likewise for constants in range for LOCHI.
1767 if ((Op1.getOpcode() == ISD::LOAD && Op0.getOpcode() != ISD::LOAD) ||
1768 (Subtarget->hasLoadStoreOnCond2() &&
1769 Node->getValueType(0).isInteger() &&
1770 Node->getValueType(0).getSizeInBits() <= 64 &&
1771 Op1.getOpcode() == ISD::Constant &&
1772 isInt<16>(cast<ConstantSDNode>(Op1)->getSExtValue()) &&
1773 !(Op0.getOpcode() == ISD::Constant &&
1774 isInt<16>(cast<ConstantSDNode>(Op0)->getSExtValue())))) {
1775 SDValue CCValid = Node->getOperand(2);
1776 SDValue CCMask = Node->getOperand(3);
1777 uint64_t ConstCCValid = CCValid.getNode()->getAsZExtVal();
1778 uint64_t ConstCCMask = CCMask.getNode()->getAsZExtVal();
1779 // Invert the condition.
1780 CCMask = CurDAG->getTargetConstant(ConstCCValid ^ ConstCCMask,
1781 SDLoc(Node), CCMask.getValueType());
1782 SDValue Op4 = Node->getOperand(4);
1783 SDNode *UpdatedNode =
1784 CurDAG->UpdateNodeOperands(Node, Op1, Op0, CCValid, CCMask, Op4);
1785 if (UpdatedNode != Node) {
1786 // In case this node already exists then replace Node with it.
1787 ReplaceNode(Node, UpdatedNode);
1788 Node = UpdatedNode;
1789 }
1790 }
1791 break;
1792 }
1793
1795 EVT VT = Node->getValueType(0);
1796 unsigned ElemBitSize = VT.getScalarSizeInBits();
1797 if (ElemBitSize == 32) {
1798 if (tryGather(Node, SystemZ::VGEF))
1799 return;
1800 } else if (ElemBitSize == 64) {
1801 if (tryGather(Node, SystemZ::VGEG))
1802 return;
1803 }
1804 break;
1805 }
1806
1807 case ISD::BUILD_VECTOR: {
1808 if (!Subtarget->hasVector())
1809 break;
1810 auto *BVN = cast<BuildVectorSDNode>(Node);
1811 SystemZVectorConstantInfo VCI(BVN);
1812 if (VCI.isVectorConstantLegal(*Subtarget)) {
1813 loadVectorConstant(VCI, Node);
1814 return;
1815 }
1816 break;
1817 }
1818
1819 case ISD::ConstantFP: {
1820 APFloat Imm = cast<ConstantFPSDNode>(Node)->getValueAPF();
1821 if (Imm.isZero() || Imm.isNegZero())
1822 break;
1823 SystemZVectorConstantInfo VCI(Imm);
1824 bool Success = VCI.isVectorConstantLegal(*Subtarget); (void)Success;
1825 assert(Success && "Expected legal FP immediate");
1826 loadVectorConstant(VCI, Node);
1827 return;
1828 }
1829
1830 case ISD::STORE: {
1831 if (tryFoldLoadStoreIntoMemOperand(Node))
1832 return;
1833 auto *Store = cast<StoreSDNode>(Node);
1834 unsigned ElemBitSize = Store->getValue().getValueSizeInBits();
1835 if (ElemBitSize == 32) {
1836 if (tryScatter(Store, SystemZ::VSCEF))
1837 return;
1838 } else if (ElemBitSize == 64) {
1839 if (tryScatter(Store, SystemZ::VSCEG))
1840 return;
1841 }
1842 break;
1843 }
1844
1845 case ISD::ATOMIC_STORE: {
1846 auto *AtomOp = cast<AtomicSDNode>(Node);
1847 // Replace the atomic_store with a regular store and select it. This is
1848 // ok since we know all store instructions <= 8 bytes are atomic, and the
1849 // 16 byte case is already handled during lowering.
1850 StoreSDNode *St = cast<StoreSDNode>(CurDAG->getTruncStore(
1851 AtomOp->getChain(), SDLoc(AtomOp), AtomOp->getVal(),
1852 AtomOp->getBasePtr(), AtomOp->getMemoryVT(), AtomOp->getMemOperand()));
1853 assert(St->getMemOperand()->isAtomic() && "Broken MMO.");
1854 SDNode *Chain = St;
1855 // We have to enforce sequential consistency by performing a
1856 // serialization operation after the store.
1857 if (AtomOp->getSuccessOrdering() == AtomicOrdering::SequentiallyConsistent)
1858 Chain = CurDAG->getMachineNode(SystemZ::Serialize, SDLoc(AtomOp),
1859 MVT::Other, SDValue(Chain, 0));
1860 ReplaceNode(Node, Chain);
1861 SelectCode(St);
1862 return;
1863 }
1864 }
1865
1866 SelectCode(Node);
1867}
1868
1869bool SystemZDAGToDAGISel::SelectInlineAsmMemoryOperand(
1870 const SDValue &Op, InlineAsm::ConstraintCode ConstraintID,
1871 std::vector<SDValue> &OutOps) {
1872 SystemZAddressingMode::AddrForm Form;
1873 SystemZAddressingMode::DispRange DispRange;
1874 SDValue Base, Disp, Index;
1875
1876 switch(ConstraintID) {
1877 default:
1878 llvm_unreachable("Unexpected asm memory constraint");
1879 case InlineAsm::ConstraintCode::i:
1880 case InlineAsm::ConstraintCode::Q:
1881 case InlineAsm::ConstraintCode::ZQ:
1882 // Accept an address with a short displacement, but no index.
1883 Form = SystemZAddressingMode::FormBD;
1884 DispRange = SystemZAddressingMode::Disp12Only;
1885 break;
1886 case InlineAsm::ConstraintCode::R:
1887 case InlineAsm::ConstraintCode::ZR:
1888 // Accept an address with a short displacement and an index.
1889 Form = SystemZAddressingMode::FormBDXNormal;
1890 DispRange = SystemZAddressingMode::Disp12Only;
1891 break;
1892 case InlineAsm::ConstraintCode::S:
1893 case InlineAsm::ConstraintCode::ZS:
1894 // Accept an address with a long displacement, but no index.
1895 Form = SystemZAddressingMode::FormBD;
1896 DispRange = SystemZAddressingMode::Disp20Only;
1897 break;
1898 case InlineAsm::ConstraintCode::T:
1899 case InlineAsm::ConstraintCode::m:
1900 case InlineAsm::ConstraintCode::o:
1901 case InlineAsm::ConstraintCode::p:
1902 case InlineAsm::ConstraintCode::ZT:
1903 // Accept an address with a long displacement and an index.
1904 // m works the same as T, as this is the most general case.
1905 // We don't really have any special handling of "offsettable"
1906 // memory addresses, so just treat o the same as m.
1907 Form = SystemZAddressingMode::FormBDXNormal;
1908 DispRange = SystemZAddressingMode::Disp20Only;
1909 break;
1910 }
1911
1912 if (selectBDXAddr(Form, DispRange, Op, Base, Disp, Index)) {
1913 const TargetRegisterClass *TRC =
1915 ConstraintID);
1916 SDLoc DL(Base);
1917 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), DL, MVT::i32);
1918
1919 // Make sure that the base address doesn't go into %r0.
1920 // If it's a TargetFrameIndex or a fixed register, we shouldn't do anything.
1921 if (Base.getOpcode() != ISD::TargetFrameIndex &&
1922 Base.getOpcode() != ISD::Register) {
1923 Base =
1924 SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
1925 DL, Base.getValueType(),
1926 Base, RC), 0);
1927 }
1928
1929 // Make sure that the index register isn't assigned to %r0 either.
1930 if (Index.getOpcode() != ISD::Register) {
1931 Index =
1932 SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
1933 DL, Index.getValueType(),
1934 Index, RC), 0);
1935 }
1936
1937 OutOps.push_back(Base);
1938 OutOps.push_back(Disp);
1939 OutOps.push_back(Index);
1940 return false;
1941 }
1942
1943 return true;
1944}
1945
1946// IsProfitableToFold - Returns true if is profitable to fold the specific
1947// operand node N of U during instruction selection that starts at Root.
1948bool
1949SystemZDAGToDAGISel::IsProfitableToFold(SDValue N, SDNode *U,
1950 SDNode *Root) const {
1951 // We want to avoid folding a LOAD into an ICMP node if as a result
1952 // we would be forced to spill the condition code into a GPR.
1953 if (N.getOpcode() == ISD::LOAD && U->getOpcode() == SystemZISD::ICMP) {
1954 if (!N.hasOneUse() || !U->hasOneUse())
1955 return false;
1956
1957 // The user of the CC value will usually be a CopyToReg into the
1958 // physical CC register, which in turn is glued and chained to the
1959 // actual instruction that uses the CC value. Bail out if we have
1960 // anything else than that.
1961 SDNode *CCUser = *U->user_begin();
1962 SDNode *CCRegUser = nullptr;
1963 if (CCUser->getOpcode() == ISD::CopyToReg ||
1964 cast<RegisterSDNode>(CCUser->getOperand(1))->getReg() == SystemZ::CC) {
1965 for (auto *U : CCUser->users()) {
1966 if (CCRegUser == nullptr)
1967 CCRegUser = U;
1968 else if (CCRegUser != U)
1969 return false;
1970 }
1971 }
1972 if (CCRegUser == nullptr)
1973 return false;
1974
1975 // If the actual instruction is a branch, the only thing that remains to be
1976 // checked is whether the CCUser chain is a predecessor of the load.
1977 if (CCRegUser->isMachineOpcode() &&
1978 CCRegUser->getMachineOpcode() == SystemZ::BRC)
1979 return !N->isPredecessorOf(CCUser->getOperand(0).getNode());
1980
1981 // Otherwise, the instruction may have multiple operands, and we need to
1982 // verify that none of them are a predecessor of the load. This is exactly
1983 // the same check that would be done by common code if the CC setter were
1984 // glued to the CC user, so simply invoke that check here.
1985 if (!IsLegalToFold(N, U, CCRegUser, OptLevel, false))
1986 return false;
1987 }
1988
1989 return true;
1990}
1991
1992namespace {
1993// Represents a sequence for extracting a 0/1 value from an IPM result:
1994// (((X ^ XORValue) + AddValue) >> Bit)
1995struct IPMConversion {
1996 IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit)
1997 : XORValue(xorValue), AddValue(addValue), Bit(bit) {}
1998
1999 int64_t XORValue;
2000 int64_t AddValue;
2001 unsigned Bit;
2002};
2003} // end anonymous namespace
2004
2005// Return a sequence for getting a 1 from an IPM result when CC has a
2006// value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
2007// The handling of CC values outside CCValid doesn't matter.
2008static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
2009 // Deal with cases where the result can be taken directly from a bit
2010 // of the IPM result.
2011 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
2012 return IPMConversion(0, 0, SystemZ::IPM_CC);
2013 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
2014 return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
2015
2016 // Deal with cases where we can add a value to force the sign bit
2017 // to contain the right value. Putting the bit in 31 means we can
2018 // use SRL rather than RISBG(L), and also makes it easier to get a
2019 // 0/-1 value, so it has priority over the other tests below.
2020 //
2021 // These sequences rely on the fact that the upper two bits of the
2022 // IPM result are zero.
2023 uint64_t TopBit = uint64_t(1) << 31;
2024 if (CCMask == (CCValid & SystemZ::CCMASK_0))
2025 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
2026 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
2027 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
2028 if (CCMask == (CCValid & (SystemZ::CCMASK_0
2031 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
2032 if (CCMask == (CCValid & SystemZ::CCMASK_3))
2033 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
2034 if (CCMask == (CCValid & (SystemZ::CCMASK_1
2037 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
2038
2039 // Next try inverting the value and testing a bit. 0/1 could be
2040 // handled this way too, but we dealt with that case above.
2041 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
2042 return IPMConversion(-1, 0, SystemZ::IPM_CC);
2043
2044 // Handle cases where adding a value forces a non-sign bit to contain
2045 // the right value.
2046 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
2047 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
2048 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
2049 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
2050
2051 // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are
2052 // can be done by inverting the low CC bit and applying one of the
2053 // sign-based extractions above.
2054 if (CCMask == (CCValid & SystemZ::CCMASK_1))
2055 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
2056 if (CCMask == (CCValid & SystemZ::CCMASK_2))
2057 return IPMConversion(1 << SystemZ::IPM_CC,
2058 TopBit - (3 << SystemZ::IPM_CC), 31);
2059 if (CCMask == (CCValid & (SystemZ::CCMASK_0
2062 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
2063 if (CCMask == (CCValid & (SystemZ::CCMASK_0
2066 return IPMConversion(1 << SystemZ::IPM_CC,
2067 TopBit - (1 << SystemZ::IPM_CC), 31);
2068
2069 llvm_unreachable("Unexpected CC combination");
2070}
2071
2072SDValue SystemZDAGToDAGISel::expandSelectBoolean(SDNode *Node) {
2073 auto *TrueOp = dyn_cast<ConstantSDNode>(Node->getOperand(0));
2074 auto *FalseOp = dyn_cast<ConstantSDNode>(Node->getOperand(1));
2075 if (!TrueOp || !FalseOp)
2076 return SDValue();
2077 if (FalseOp->getZExtValue() != 0)
2078 return SDValue();
2079 if (TrueOp->getSExtValue() != 1 && TrueOp->getSExtValue() != -1)
2080 return SDValue();
2081
2082 auto *CCValidOp = dyn_cast<ConstantSDNode>(Node->getOperand(2));
2083 auto *CCMaskOp = dyn_cast<ConstantSDNode>(Node->getOperand(3));
2084 if (!CCValidOp || !CCMaskOp)
2085 return SDValue();
2086 int CCValid = CCValidOp->getZExtValue();
2087 int CCMask = CCMaskOp->getZExtValue();
2088
2089 SDLoc DL(Node);
2090 SDValue CCReg = Node->getOperand(4);
2091 IPMConversion IPM = getIPMConversion(CCValid, CCMask);
2092 SDValue Result = CurDAG->getNode(SystemZISD::IPM, DL, MVT::i32, CCReg);
2093
2094 if (IPM.XORValue)
2095 Result = CurDAG->getNode(ISD::XOR, DL, MVT::i32, Result,
2096 CurDAG->getConstant(IPM.XORValue, DL, MVT::i32));
2097
2098 if (IPM.AddValue)
2099 Result =
2100 CurDAG->getNode(ISD::ADD, DL, MVT::i32, Result,
2101 CurDAG->getSignedConstant(IPM.AddValue, DL, MVT::i32));
2102
2103 EVT VT = Node->getValueType(0);
2104 if (VT == MVT::i32 && IPM.Bit == 31) {
2105 unsigned ShiftOp = TrueOp->getSExtValue() == 1 ? ISD::SRL : ISD::SRA;
2106 Result = CurDAG->getNode(ShiftOp, DL, MVT::i32, Result,
2107 CurDAG->getConstant(IPM.Bit, DL, MVT::i32));
2108 } else {
2109 if (VT != MVT::i32)
2110 Result = CurDAG->getNode(ISD::ANY_EXTEND, DL, VT, Result);
2111
2112 if (TrueOp->getSExtValue() == 1) {
2113 // The SHR/AND sequence should get optimized to an RISBG.
2114 Result = CurDAG->getNode(ISD::SRL, DL, VT, Result,
2115 CurDAG->getConstant(IPM.Bit, DL, MVT::i32));
2116 Result = CurDAG->getNode(ISD::AND, DL, VT, Result,
2117 CurDAG->getConstant(1, DL, VT));
2118 } else {
2119 // Sign-extend from IPM.Bit using a pair of shifts.
2120 int ShlAmt = VT.getSizeInBits() - 1 - IPM.Bit;
2121 int SraAmt = VT.getSizeInBits() - 1;
2122 Result = CurDAG->getNode(ISD::SHL, DL, VT, Result,
2123 CurDAG->getConstant(ShlAmt, DL, MVT::i32));
2124 Result = CurDAG->getNode(ISD::SRA, DL, VT, Result,
2125 CurDAG->getConstant(SraAmt, DL, MVT::i32));
2126 }
2127 }
2128
2129 return Result;
2130}
2131
2132bool SystemZDAGToDAGISel::shouldSelectForReassoc(SDNode *N) const {
2133 EVT VT = N->getValueType(0);
2134 assert(VT.isFloatingPoint() && "Expected FP SDNode");
2135 return N->getFlags().hasAllowReassociation() &&
2136 N->getFlags().hasNoSignedZeros() && Subtarget->hasVector() &&
2137 (VT != MVT::f32 || Subtarget->hasVectorEnhancements1()) &&
2138 !N->isStrictFPOpcode();
2139}
2140
2141void SystemZDAGToDAGISel::PreprocessISelDAG() {
2142 // If we have conditional immediate loads, we always prefer
2143 // using those over an IPM sequence.
2144 if (Subtarget->hasLoadStoreOnCond2())
2145 return;
2146
2147 bool MadeChange = false;
2148
2149 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
2150 E = CurDAG->allnodes_end();
2151 I != E;) {
2152 SDNode *N = &*I++;
2153 if (N->use_empty())
2154 continue;
2155
2156 SDValue Res;
2157 switch (N->getOpcode()) {
2158 default: break;
2159 case SystemZISD::SELECT_CCMASK:
2160 Res = expandSelectBoolean(N);
2161 break;
2162 }
2163
2164 if (Res) {
2165 LLVM_DEBUG(dbgs() << "SystemZ DAG preprocessing replacing:\nOld: ");
2166 LLVM_DEBUG(N->dump(CurDAG));
2167 LLVM_DEBUG(dbgs() << "\nNew: ");
2168 LLVM_DEBUG(Res.getNode()->dump(CurDAG));
2169 LLVM_DEBUG(dbgs() << "\n");
2170
2171 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Res);
2172 MadeChange = true;
2173 }
2174 }
2175
2176 if (MadeChange)
2177 CurDAG->RemoveDeadNodes();
2178}
#define Success
return SDValue()
const AArch64Subtarget & Subtarget
const TargetLowering & TLI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static msgpack::DocNode getNode(msgpack::DocNode DN, msgpack::Type Type, MCValue Val)
uint64_t uint64_t
uint64_t Imm
AMDGPU Register Bank Select
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
bpf unwind BPF Unwind Lower
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define DEBUG_TYPE
const HexagonInstrInfo * TII
Module.h This file contains the declarations for the Module class.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
#define LLVM_DEBUG(...)
Definition Debug.h:119
static void changeComponent(SystemZAddressingMode &AM, bool IsBase, SDValue Value)
static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask)
static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val)
static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase, SDValue Value)
static bool isFusableLoadOpStorePattern(StoreSDNode *StoreNode, SDValue StoredVal, SelectionDAG *CurDAG, LoadSDNode *&LoadNode, SDValue &InputChain)
static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val)
static bool expandIndex(SystemZAddressingMode &AM, SDValue Base, SDValue Index)
static bool maskMatters(RxSBGOperands &RxSBG, uint64_t Mask)
static bool expandDisp(SystemZAddressingMode &AM, bool IsBase, SDValue Op0, uint64_t Op1)
static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index)
static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N)
static uint64_t allOnes(unsigned int Count)
#define PASS_NAME
The Input class is used to parse a yaml document into in-memory structs and vectors.
const TargetRegisterClass * getInlineAsmMemoryOperandRegClass(InlineAsm::ConstraintCode C) const override
const AArch64InstrInfo * getInstrInfo() const override
Class for arbitrary precision integers.
Definition APInt.h:78
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Definition Globals.cpp:205
This class is used to represent ISD::LOAD nodes.
const SDValue & getBasePtr() const
const SDValue & getOffset() const
unsigned getID() const
getID() - Return the register class ID number.
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags F, LLT MemTy, Align BaseAlignment, const MMOMetadata &Metadata=MMOMetadata(), SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
const PseudoSourceValue * getPseudoValue() const
@ MOLoad
The memory access reads data.
int64_t getOffset() const
For normal values, this is a byte offset added to the base address.
MachineMemOperand * getMemOperand() const
Return the unique MachineMemOperand object describing the memory reference performed by operation.
const SDValue & getChain() const
bool isNonTemporal() const
EVT getMemoryVT() const
Return the type of the in-memory value.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
ArrayRef< SDUse > ops() const
bool isMachineOpcode() const
Test if this node has a post-isel opcode, directly corresponding to a MachineInstr opcode.
int getNodeId() const
Return the unique node id.
LLVM_ABI void dump() const
Dump this node, for debugging.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
static bool hasPredecessorHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallVectorImpl< const SDNode * > &Worklist, unsigned int MaxSteps=0, bool TopologicalPrune=false)
Returns true if N is a predecessor of any node in Worklist.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
unsigned getMachineOpcode() const
This may only be called if isMachineOpcode returns true.
const SDValue & getOperand(unsigned Num) const
bool hasNUsesOfValue(unsigned NUses, unsigned Value) const
Return true if there are exactly NUSES uses of the indicated value.
iterator_range< user_iterator > users()
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
SDNode * getNode() const
get the SDNode which holds the desired result
EVT getValueType() const
Return the ValueType of the referenced return value.
const SDValue & getOperand(unsigned i) const
unsigned getResNo() const
get the index which selects a specific result in the SDNode
unsigned getOpcode() const
unsigned getNumOperands() const
SelectionDAGISelPass(std::unique_ptr< SelectionDAGISel > Selector)
SelectionDAGISel - This is the common base class used for SelectionDAG-based pattern-matching instruc...
static int getUninvalidatedNodeId(SDNode *N)
virtual bool runOnMachineFunction(MachineFunction &mf)
static void InvalidateNodeId(SDNode *N)
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
void RepositionNode(allnodes_iterator Position, SDNode *N)
Move node N in the AllNodes list to be immediately before the given iterator Position.
ilist< SDNode >::iterator allnodes_iterator
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class is used to represent ISD::STORE nodes.
const SDValue & getBasePtr() const
const SDValue & getOffset() const
SystemZISelDAGToDAGPass(SystemZTargetMachine &TM, CodeGenOptLevel OptLevel)
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
Target - Wrapper for Target specific information.
LLVM Value Representation.
Definition Value.h:75
LLVM_ABI Align getPointerAlignment(const DataLayout &DL) const
Returns an alignment of the pointer value.
Definition Value.cpp:985
self_iterator getIterator()
Definition ilist_node.h:123
#define UINT64_MAX
Definition DataTypes.h:77
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:811
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, val, ptr) This corresponds to "store atomic" instruction.
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:266
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:885
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:876
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:793
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:581
@ CopyToReg
CopyToReg - This node has three operands: a chain, a register number to set to this value,...
Definition ISDOpcodes.h:226
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:882
@ TargetFrameIndex
Definition ISDOpcodes.h:189
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:920
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:763
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:570
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:55
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:888
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:561
bool isNormalStore(const SDNode *N)
Returns true if the specified node is a non-truncating and unindexed store.
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
bool isNormalLoad(const SDNode *N)
Returns true if the specified node is a non-extending and unindexed load.
bool isPCREL(unsigned Opcode)
const unsigned CCMASK_0
Definition SystemZ.h:33
const unsigned CCMASK_1
Definition SystemZ.h:34
const unsigned IPM_CC
Definition SystemZ.h:118
static bool isImmHF(uint64_t Val)
Definition SystemZ.h:192
const unsigned CCMASK_3
Definition SystemZ.h:36
static bool isImmLF(uint64_t Val)
Definition SystemZ.h:187
const unsigned CCMASK_2
Definition SystemZ.h:35
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
std::pair< Instruction *, SmallVector< Value * > > convertTo(Instruction *I, const InstructionsState &S)
This is an optimization pass for GlobalISel generic memory operations.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
@ Offset
Definition DWP.cpp:577
FunctionPass * createSystemZISelDagLegacyPass(SystemZTargetMachine &TM, CodeGenOptLevel OptLevel)
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
@ Known
Known to have no common set bits.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
@ Load
The value being inserted comes from a load (InsertElement only).
@ Store
The extracted value is stored (ExtractElement only).
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:280
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Or
Bitwise or logical OR of integers.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:879
#define N
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:82
Extended Value Type.
Definition ValueTypes.h:35
EVT changeVectorElementTypeToInteger() const
Return a vector with the same number of elements as this vector, but with the element type converted ...
Definition ValueTypes.h:90
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition ValueTypes.h:170
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:411
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:423
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:354
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:374
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:175
static LLVM_ABI MachinePointerInfo getConstantPool(MachineFunction &MF)
Return a MachinePointerInfo record that refers to the constant pool.
SmallVector< unsigned, 2 > OpVals
bool isVectorConstantLegal(const SystemZSubtarget &Subtarget)