blob: a97449a40d67b201f1705459ffe2c3ea098937ff [file] [log] [blame]
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001//===-- SystemZISelDAGToDAG.cpp - A dag to dag inst selector for SystemZ --===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the SystemZ target.
11//
12//===----------------------------------------------------------------------===//
13
14#include "SystemZTargetMachine.h"
Richard Sandiford97846492013-07-09 09:46:39 +000015#include "llvm/Analysis/AliasAnalysis.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000016#include "llvm/CodeGen/SelectionDAGISel.h"
17#include "llvm/Support/Debug.h"
18#include "llvm/Support/raw_ostream.h"
19
20using namespace llvm;
21
22namespace {
23// Used to build addressing modes.
24struct SystemZAddressingMode {
25 // The shape of the address.
26 enum AddrForm {
27 // base+displacement
28 FormBD,
29
30 // base+displacement+index for load and store operands
31 FormBDXNormal,
32
33 // base+displacement+index for load address operands
34 FormBDXLA,
35
36 // base+displacement+index+ADJDYNALLOC
37 FormBDXDynAlloc
38 };
39 AddrForm Form;
40
41 // The type of displacement. The enum names here correspond directly
42 // to the definitions in SystemZOperand.td. We could split them into
43 // flags -- single/pair, 128-bit, etc. -- but it hardly seems worth it.
44 enum DispRange {
45 Disp12Only,
46 Disp12Pair,
47 Disp20Only,
48 Disp20Only128,
49 Disp20Pair
50 };
51 DispRange DR;
52
53 // The parts of the address. The address is equivalent to:
54 //
55 // Base + Disp + Index + (IncludesDynAlloc ? ADJDYNALLOC : 0)
56 SDValue Base;
57 int64_t Disp;
58 SDValue Index;
59 bool IncludesDynAlloc;
60
61 SystemZAddressingMode(AddrForm form, DispRange dr)
62 : Form(form), DR(dr), Base(), Disp(0), Index(),
63 IncludesDynAlloc(false) {}
64
65 // True if the address can have an index register.
66 bool hasIndexField() { return Form != FormBD; }
67
68 // True if the address can (and must) include ADJDYNALLOC.
69 bool isDynAlloc() { return Form == FormBDXDynAlloc; }
70
71 void dump() {
72 errs() << "SystemZAddressingMode " << this << '\n';
73
74 errs() << " Base ";
75 if (Base.getNode() != 0)
76 Base.getNode()->dump();
77 else
78 errs() << "null\n";
79
80 if (hasIndexField()) {
81 errs() << " Index ";
82 if (Index.getNode() != 0)
83 Index.getNode()->dump();
84 else
85 errs() << "null\n";
86 }
87
88 errs() << " Disp " << Disp;
89 if (IncludesDynAlloc)
90 errs() << " + ADJDYNALLOC";
91 errs() << '\n';
92 }
93};
94
Richard Sandiford82ec87d2013-07-16 11:02:24 +000095// Return a mask with Count low bits set.
96static uint64_t allOnes(unsigned int Count) {
97 return Count == 0 ? 0 : (uint64_t(1) << (Count - 1) << 1) - 1;
98}
99
Richard Sandiford51093212013-07-18 10:40:35 +0000100// Represents operands 2 to 5 of the ROTATE AND ... SELECTED BITS operation
101// given by Opcode. The operands are: Input (R2), Start (I3), End (I4) and
102// Rotate (I5). The combined operand value is effectively:
103//
104// (or (rotl Input, Rotate), ~Mask)
105//
106// for RNSBG and:
107//
108// (and (rotl Input, Rotate), Mask)
109//
110// otherwise. The value has BitSize bits.
Richard Sandiford5cbac962013-07-18 09:45:08 +0000111struct RxSBGOperands {
Richard Sandiford51093212013-07-18 10:40:35 +0000112 RxSBGOperands(unsigned Op, SDValue N)
113 : Opcode(Op), BitSize(N.getValueType().getSizeInBits()),
114 Mask(allOnes(BitSize)), Input(N), Start(64 - BitSize), End(63),
115 Rotate(0) {}
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000116
Richard Sandiford51093212013-07-18 10:40:35 +0000117 unsigned Opcode;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000118 unsigned BitSize;
119 uint64_t Mask;
120 SDValue Input;
121 unsigned Start;
122 unsigned End;
123 unsigned Rotate;
124};
125
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000126class SystemZDAGToDAGISel : public SelectionDAGISel {
127 const SystemZTargetLowering &Lowering;
128 const SystemZSubtarget &Subtarget;
129
130 // Used by SystemZOperands.td to create integer constants.
Richard Sandiford54b36912013-09-27 15:14:04 +0000131 inline SDValue getImm(const SDNode *Node, uint64_t Imm) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000132 return CurDAG->getTargetConstant(Imm, Node->getValueType(0));
133 }
134
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000135 const SystemZTargetMachine &getTargetMachine() const {
136 return static_cast<const SystemZTargetMachine &>(TM);
137 }
138
139 const SystemZInstrInfo *getInstrInfo() const {
140 return getTargetMachine().getInstrInfo();
141 }
142
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000143 // Try to fold more of the base or index of AM into AM, where IsBase
144 // selects between the base and index.
Richard Sandiford54b36912013-09-27 15:14:04 +0000145 bool expandAddress(SystemZAddressingMode &AM, bool IsBase) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000146
147 // Try to describe N in AM, returning true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000148 bool selectAddress(SDValue N, SystemZAddressingMode &AM) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000149
150 // Extract individual target operands from matched address AM.
151 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
Richard Sandiford54b36912013-09-27 15:14:04 +0000152 SDValue &Base, SDValue &Disp) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000153 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
Richard Sandiford54b36912013-09-27 15:14:04 +0000154 SDValue &Base, SDValue &Disp, SDValue &Index) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000155
156 // Try to match Addr as a FormBD address with displacement type DR.
157 // Return true on success, storing the base and displacement in
158 // Base and Disp respectively.
159 bool selectBDAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000160 SDValue &Base, SDValue &Disp) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000161
Richard Sandiforda481f582013-08-23 11:18:53 +0000162 // Try to match Addr as a FormBDX address with displacement type DR.
163 // Return true on success and if the result had no index. Store the
164 // base and displacement in Base and Disp respectively.
165 bool selectMVIAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000166 SDValue &Base, SDValue &Disp) const;
Richard Sandiforda481f582013-08-23 11:18:53 +0000167
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000168 // Try to match Addr as a FormBDX* address of form Form with
169 // displacement type DR. Return true on success, storing the base,
170 // displacement and index in Base, Disp and Index respectively.
171 bool selectBDXAddr(SystemZAddressingMode::AddrForm Form,
172 SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000173 SDValue &Base, SDValue &Disp, SDValue &Index) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000174
175 // PC-relative address matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000176 bool selectPCRelAddress(SDValue Addr, SDValue &Target) const {
177 if (SystemZISD::isPCREL(Addr.getOpcode())) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000178 Target = Addr.getOperand(0);
179 return true;
180 }
181 return false;
182 }
183
184 // BD matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000185 bool selectBDAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000186 return selectBDAddr(SystemZAddressingMode::Disp12Only, Addr, Base, Disp);
187 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000188 bool selectBDAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000189 return selectBDAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
190 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000191 bool selectBDAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000192 return selectBDAddr(SystemZAddressingMode::Disp20Only, Addr, Base, Disp);
193 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000194 bool selectBDAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000195 return selectBDAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
196 }
197
Richard Sandiforda481f582013-08-23 11:18:53 +0000198 // MVI matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000199 bool selectMVIAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000200 return selectMVIAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
201 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000202 bool selectMVIAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000203 return selectMVIAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
204 }
205
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000206 // BDX matching routines used by SystemZOperands.td.
207 bool selectBDXAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000208 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000209 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
210 SystemZAddressingMode::Disp12Only,
211 Addr, Base, Disp, Index);
212 }
213 bool selectBDXAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000214 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000215 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
216 SystemZAddressingMode::Disp12Pair,
217 Addr, Base, Disp, Index);
218 }
219 bool selectDynAlloc12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000220 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000221 return selectBDXAddr(SystemZAddressingMode::FormBDXDynAlloc,
222 SystemZAddressingMode::Disp12Only,
223 Addr, Base, Disp, Index);
224 }
225 bool selectBDXAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000226 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000227 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
228 SystemZAddressingMode::Disp20Only,
229 Addr, Base, Disp, Index);
230 }
231 bool selectBDXAddr20Only128(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000232 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000233 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
234 SystemZAddressingMode::Disp20Only128,
235 Addr, Base, Disp, Index);
236 }
237 bool selectBDXAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000238 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000239 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
240 SystemZAddressingMode::Disp20Pair,
241 Addr, Base, Disp, Index);
242 }
243 bool selectLAAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000244 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000245 return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
246 SystemZAddressingMode::Disp12Pair,
247 Addr, Base, Disp, Index);
248 }
249 bool selectLAAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000250 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000251 return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
252 SystemZAddressingMode::Disp20Pair,
253 Addr, Base, Disp, Index);
254 }
255
Richard Sandiford885140c2013-07-16 11:55:57 +0000256 // Check whether (or Op (and X InsertMask)) is effectively an insertion
257 // of X into bits InsertMask of some Y != Op. Return true if so and
258 // set Op to that Y.
Richard Sandiford54b36912013-09-27 15:14:04 +0000259 bool detectOrAndInsertion(SDValue &Op, uint64_t InsertMask) const;
Richard Sandiford885140c2013-07-16 11:55:57 +0000260
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000261 // Try to update RxSBG so that only the bits of RxSBG.Input in Mask are used.
262 // Return true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000263 bool refineRxSBGMask(RxSBGOperands &RxSBG, uint64_t Mask) const;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000264
Richard Sandiford5cbac962013-07-18 09:45:08 +0000265 // Try to fold some of RxSBG.Input into other fields of RxSBG.
266 // Return true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000267 bool expandRxSBG(RxSBGOperands &RxSBG) const;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000268
Richard Sandiford84f54a32013-07-11 08:59:12 +0000269 // Return an undefined i64 value.
Richard Sandiford54b36912013-09-27 15:14:04 +0000270 SDValue getUNDEF64(SDLoc DL) const;
Richard Sandiford84f54a32013-07-11 08:59:12 +0000271
272 // Convert N to VT, if it isn't already.
Richard Sandiford54b36912013-09-27 15:14:04 +0000273 SDValue convertTo(SDLoc DL, EVT VT, SDValue N) const;
Richard Sandiford84f54a32013-07-11 08:59:12 +0000274
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000275 // Try to implement AND or shift node N using RISBG with the zero flag set.
276 // Return the selected node on success, otherwise return null.
277 SDNode *tryRISBGZero(SDNode *N);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000278
Richard Sandiford7878b852013-07-18 10:06:15 +0000279 // Try to use RISBG or Opcode to implement OR or XOR node N.
280 // Return the selected node on success, otherwise return null.
281 SDNode *tryRxSBG(SDNode *N, unsigned Opcode);
Richard Sandiford885140c2013-07-16 11:55:57 +0000282
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000283 // If Op0 is null, then Node is a constant that can be loaded using:
284 //
285 // (Opcode UpperVal LowerVal)
286 //
287 // If Op0 is nonnull, then Node can be implemented using:
288 //
289 // (Opcode (Opcode Op0 UpperVal) LowerVal)
290 SDNode *splitLargeImmediate(unsigned Opcode, SDNode *Node, SDValue Op0,
291 uint64_t UpperVal, uint64_t LowerVal);
292
Richard Sandiford178273a2013-09-05 10:36:45 +0000293 // N is a (store (load Y), X) pattern. Return true if it can use an MVC
294 // from Y to X.
Richard Sandiford97846492013-07-09 09:46:39 +0000295 bool storeLoadCanUseMVC(SDNode *N) const;
296
Richard Sandiford178273a2013-09-05 10:36:45 +0000297 // N is a (store (op (load A[0]), (load A[1])), X) pattern. Return true
298 // if A[1 - I] == X and if N can use a block operation like NC from A[I]
299 // to X.
300 bool storeLoadCanUseBlockBinary(SDNode *N, unsigned I) const;
301
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000302public:
303 SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel)
304 : SelectionDAGISel(TM, OptLevel),
305 Lowering(*TM.getTargetLowering()),
306 Subtarget(*TM.getSubtargetImpl()) { }
307
308 // Override MachineFunctionPass.
309 virtual const char *getPassName() const LLVM_OVERRIDE {
310 return "SystemZ DAG->DAG Pattern Instruction Selection";
311 }
312
313 // Override SelectionDAGISel.
314 virtual SDNode *Select(SDNode *Node) LLVM_OVERRIDE;
315 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
316 char ConstraintCode,
317 std::vector<SDValue> &OutOps)
318 LLVM_OVERRIDE;
319
320 // Include the pieces autogenerated from the target description.
321 #include "SystemZGenDAGISel.inc"
322};
323} // end anonymous namespace
324
325FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
326 CodeGenOpt::Level OptLevel) {
327 return new SystemZDAGToDAGISel(TM, OptLevel);
328}
329
330// Return true if Val should be selected as a displacement for an address
331// with range DR. Here we're interested in the range of both the instruction
332// described by DR and of any pairing instruction.
333static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
334 switch (DR) {
335 case SystemZAddressingMode::Disp12Only:
336 return isUInt<12>(Val);
337
338 case SystemZAddressingMode::Disp12Pair:
339 case SystemZAddressingMode::Disp20Only:
340 case SystemZAddressingMode::Disp20Pair:
341 return isInt<20>(Val);
342
343 case SystemZAddressingMode::Disp20Only128:
344 return isInt<20>(Val) && isInt<20>(Val + 8);
345 }
346 llvm_unreachable("Unhandled displacement range");
347}
348
349// Change the base or index in AM to Value, where IsBase selects
350// between the base and index.
351static void changeComponent(SystemZAddressingMode &AM, bool IsBase,
352 SDValue Value) {
353 if (IsBase)
354 AM.Base = Value;
355 else
356 AM.Index = Value;
357}
358
359// The base or index of AM is equivalent to Value + ADJDYNALLOC,
360// where IsBase selects between the base and index. Try to fold the
361// ADJDYNALLOC into AM.
362static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase,
363 SDValue Value) {
364 if (AM.isDynAlloc() && !AM.IncludesDynAlloc) {
365 changeComponent(AM, IsBase, Value);
366 AM.IncludesDynAlloc = true;
367 return true;
368 }
369 return false;
370}
371
372// The base of AM is equivalent to Base + Index. Try to use Index as
373// the index register.
374static bool expandIndex(SystemZAddressingMode &AM, SDValue Base,
375 SDValue Index) {
376 if (AM.hasIndexField() && !AM.Index.getNode()) {
377 AM.Base = Base;
378 AM.Index = Index;
379 return true;
380 }
381 return false;
382}
383
384// The base or index of AM is equivalent to Op0 + Op1, where IsBase selects
385// between the base and index. Try to fold Op1 into AM's displacement.
386static bool expandDisp(SystemZAddressingMode &AM, bool IsBase,
Richard Sandiford54b36912013-09-27 15:14:04 +0000387 SDValue Op0, uint64_t Op1) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000388 // First try adjusting the displacement.
Richard Sandiford54b36912013-09-27 15:14:04 +0000389 int64_t TestDisp = AM.Disp + Op1;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000390 if (selectDisp(AM.DR, TestDisp)) {
391 changeComponent(AM, IsBase, Op0);
392 AM.Disp = TestDisp;
393 return true;
394 }
395
396 // We could consider forcing the displacement into a register and
397 // using it as an index, but it would need to be carefully tuned.
398 return false;
399}
400
401bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM,
Richard Sandiford54b36912013-09-27 15:14:04 +0000402 bool IsBase) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000403 SDValue N = IsBase ? AM.Base : AM.Index;
404 unsigned Opcode = N.getOpcode();
405 if (Opcode == ISD::TRUNCATE) {
406 N = N.getOperand(0);
407 Opcode = N.getOpcode();
408 }
409 if (Opcode == ISD::ADD || CurDAG->isBaseWithConstantOffset(N)) {
410 SDValue Op0 = N.getOperand(0);
411 SDValue Op1 = N.getOperand(1);
412
413 unsigned Op0Code = Op0->getOpcode();
414 unsigned Op1Code = Op1->getOpcode();
415
416 if (Op0Code == SystemZISD::ADJDYNALLOC)
417 return expandAdjDynAlloc(AM, IsBase, Op1);
418 if (Op1Code == SystemZISD::ADJDYNALLOC)
419 return expandAdjDynAlloc(AM, IsBase, Op0);
420
421 if (Op0Code == ISD::Constant)
Richard Sandiford54b36912013-09-27 15:14:04 +0000422 return expandDisp(AM, IsBase, Op1,
423 cast<ConstantSDNode>(Op0)->getSExtValue());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000424 if (Op1Code == ISD::Constant)
Richard Sandiford54b36912013-09-27 15:14:04 +0000425 return expandDisp(AM, IsBase, Op0,
426 cast<ConstantSDNode>(Op1)->getSExtValue());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000427
428 if (IsBase && expandIndex(AM, Op0, Op1))
429 return true;
430 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000431 if (Opcode == SystemZISD::PCREL_OFFSET) {
432 SDValue Full = N.getOperand(0);
433 SDValue Base = N.getOperand(1);
434 SDValue Anchor = Base.getOperand(0);
435 uint64_t Offset = (cast<GlobalAddressSDNode>(Full)->getOffset() -
436 cast<GlobalAddressSDNode>(Anchor)->getOffset());
437 return expandDisp(AM, IsBase, Base, Offset);
438 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000439 return false;
440}
441
442// Return true if an instruction with displacement range DR should be
443// used for displacement value Val. selectDisp(DR, Val) must already hold.
444static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
445 assert(selectDisp(DR, Val) && "Invalid displacement");
446 switch (DR) {
447 case SystemZAddressingMode::Disp12Only:
448 case SystemZAddressingMode::Disp20Only:
449 case SystemZAddressingMode::Disp20Only128:
450 return true;
451
452 case SystemZAddressingMode::Disp12Pair:
453 // Use the other instruction if the displacement is too large.
454 return isUInt<12>(Val);
455
456 case SystemZAddressingMode::Disp20Pair:
457 // Use the other instruction if the displacement is small enough.
458 return !isUInt<12>(Val);
459 }
460 llvm_unreachable("Unhandled displacement range");
461}
462
463// Return true if Base + Disp + Index should be performed by LA(Y).
464static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index) {
465 // Don't use LA(Y) for constants.
466 if (!Base)
467 return false;
468
469 // Always use LA(Y) for frame addresses, since we know that the destination
470 // register is almost always (perhaps always) going to be different from
471 // the frame register.
472 if (Base->getOpcode() == ISD::FrameIndex)
473 return true;
474
475 if (Disp) {
476 // Always use LA(Y) if there is a base, displacement and index.
477 if (Index)
478 return true;
479
480 // Always use LA if the displacement is small enough. It should always
481 // be no worse than AGHI (and better if it avoids a move).
482 if (isUInt<12>(Disp))
483 return true;
484
485 // For similar reasons, always use LAY if the constant is too big for AGHI.
486 // LAY should be no worse than AGFI.
487 if (!isInt<16>(Disp))
488 return true;
489 } else {
490 // Don't use LA for plain registers.
491 if (!Index)
492 return false;
493
494 // Don't use LA for plain addition if the index operand is only used
495 // once. It should be a natural two-operand addition in that case.
496 if (Index->hasOneUse())
497 return false;
498
499 // Prefer addition if the second operation is sign-extended, in the
500 // hope of using AGF.
501 unsigned IndexOpcode = Index->getOpcode();
502 if (IndexOpcode == ISD::SIGN_EXTEND ||
503 IndexOpcode == ISD::SIGN_EXTEND_INREG)
504 return false;
505 }
506
507 // Don't use LA for two-operand addition if either operand is only
508 // used once. The addition instructions are better in that case.
509 if (Base->hasOneUse())
510 return false;
511
512 return true;
513}
514
515// Return true if Addr is suitable for AM, updating AM if so.
516bool SystemZDAGToDAGISel::selectAddress(SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000517 SystemZAddressingMode &AM) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000518 // Start out assuming that the address will need to be loaded separately,
519 // then try to extend it as much as we can.
520 AM.Base = Addr;
521
522 // First try treating the address as a constant.
523 if (Addr.getOpcode() == ISD::Constant &&
Richard Sandiford54b36912013-09-27 15:14:04 +0000524 expandDisp(AM, true, SDValue(),
525 cast<ConstantSDNode>(Addr)->getSExtValue()))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000526 ;
527 else
528 // Otherwise try expanding each component.
529 while (expandAddress(AM, true) ||
530 (AM.Index.getNode() && expandAddress(AM, false)))
531 continue;
532
533 // Reject cases where it isn't profitable to use LA(Y).
534 if (AM.Form == SystemZAddressingMode::FormBDXLA &&
535 !shouldUseLA(AM.Base.getNode(), AM.Disp, AM.Index.getNode()))
536 return false;
537
538 // Reject cases where the other instruction in a pair should be used.
539 if (!isValidDisp(AM.DR, AM.Disp))
540 return false;
541
542 // Make sure that ADJDYNALLOC is included where necessary.
543 if (AM.isDynAlloc() && !AM.IncludesDynAlloc)
544 return false;
545
546 DEBUG(AM.dump());
547 return true;
548}
549
550// Insert a node into the DAG at least before Pos. This will reposition
551// the node as needed, and will assign it a node ID that is <= Pos's ID.
552// Note that this does *not* preserve the uniqueness of node IDs!
553// The selection DAG must no longer depend on their uniqueness when this
554// function is used.
555static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N) {
556 if (N.getNode()->getNodeId() == -1 ||
557 N.getNode()->getNodeId() > Pos->getNodeId()) {
558 DAG->RepositionNode(Pos, N.getNode());
559 N.getNode()->setNodeId(Pos->getNodeId());
560 }
561}
562
563void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
564 EVT VT, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000565 SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000566 Base = AM.Base;
567 if (!Base.getNode())
568 // Register 0 means "no base". This is mostly useful for shifts.
569 Base = CurDAG->getRegister(0, VT);
570 else if (Base.getOpcode() == ISD::FrameIndex) {
571 // Lower a FrameIndex to a TargetFrameIndex.
572 int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex();
573 Base = CurDAG->getTargetFrameIndex(FrameIndex, VT);
574 } else if (Base.getValueType() != VT) {
575 // Truncate values from i64 to i32, for shifts.
576 assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 &&
577 "Unexpected truncation");
Andrew Trickef9de2a2013-05-25 02:42:55 +0000578 SDLoc DL(Base);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000579 SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base);
580 insertDAGNode(CurDAG, Base.getNode(), Trunc);
581 Base = Trunc;
582 }
583
584 // Lower the displacement to a TargetConstant.
585 Disp = CurDAG->getTargetConstant(AM.Disp, VT);
586}
587
588void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
589 EVT VT, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000590 SDValue &Disp,
591 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000592 getAddressOperands(AM, VT, Base, Disp);
593
594 Index = AM.Index;
595 if (!Index.getNode())
596 // Register 0 means "no index".
597 Index = CurDAG->getRegister(0, VT);
598}
599
600bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR,
601 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000602 SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000603 SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR);
604 if (!selectAddress(Addr, AM))
605 return false;
606
607 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
608 return true;
609}
610
Richard Sandiforda481f582013-08-23 11:18:53 +0000611bool SystemZDAGToDAGISel::selectMVIAddr(SystemZAddressingMode::DispRange DR,
612 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000613 SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000614 SystemZAddressingMode AM(SystemZAddressingMode::FormBDXNormal, DR);
615 if (!selectAddress(Addr, AM) || AM.Index.getNode())
616 return false;
617
618 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
619 return true;
620}
621
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000622bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form,
623 SystemZAddressingMode::DispRange DR,
624 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000625 SDValue &Disp, SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000626 SystemZAddressingMode AM(Form, DR);
627 if (!selectAddress(Addr, AM))
628 return false;
629
630 getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index);
631 return true;
632}
633
Richard Sandiford885140c2013-07-16 11:55:57 +0000634bool SystemZDAGToDAGISel::detectOrAndInsertion(SDValue &Op,
Richard Sandiford54b36912013-09-27 15:14:04 +0000635 uint64_t InsertMask) const {
Richard Sandiford885140c2013-07-16 11:55:57 +0000636 // We're only interested in cases where the insertion is into some operand
637 // of Op, rather than into Op itself. The only useful case is an AND.
638 if (Op.getOpcode() != ISD::AND)
639 return false;
640
641 // We need a constant mask.
642 ConstantSDNode *MaskNode =
643 dyn_cast<ConstantSDNode>(Op.getOperand(1).getNode());
644 if (!MaskNode)
645 return false;
646
647 // It's not an insertion of Op.getOperand(0) if the two masks overlap.
648 uint64_t AndMask = MaskNode->getZExtValue();
649 if (InsertMask & AndMask)
650 return false;
651
652 // It's only an insertion if all bits are covered or are known to be zero.
653 // The inner check covers all cases but is more expensive.
654 uint64_t Used = allOnes(Op.getValueType().getSizeInBits());
655 if (Used != (AndMask | InsertMask)) {
656 APInt KnownZero, KnownOne;
657 CurDAG->ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne);
658 if (Used != (AndMask | InsertMask | KnownZero.getZExtValue()))
659 return false;
660 }
661
662 Op = Op.getOperand(0);
663 return true;
664}
665
Richard Sandiford54b36912013-09-27 15:14:04 +0000666bool SystemZDAGToDAGISel::refineRxSBGMask(RxSBGOperands &RxSBG,
667 uint64_t Mask) const {
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000668 const SystemZInstrInfo *TII = getInstrInfo();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000669 if (RxSBG.Rotate != 0)
670 Mask = (Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate));
671 Mask &= RxSBG.Mask;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000672 if (TII->isRxSBGMask(Mask, RxSBG.BitSize, RxSBG.Start, RxSBG.End)) {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000673 RxSBG.Mask = Mask;
Richard Sandiford5cbac962013-07-18 09:45:08 +0000674 return true;
675 }
Richard Sandiford84f54a32013-07-11 08:59:12 +0000676 return false;
677}
678
Richard Sandiford297f7d22013-07-18 10:14:55 +0000679// RxSBG.Input is a shift of Count bits in the direction given by IsLeft.
680// Return true if the result depends on the signs or zeros that are
681// shifted in.
682static bool shiftedInBitsMatter(RxSBGOperands &RxSBG, uint64_t Count,
683 bool IsLeft) {
684 // Work out which bits of the shift result are zeros or sign copies.
685 uint64_t ShiftedIn = allOnes(Count);
686 if (!IsLeft)
687 ShiftedIn <<= RxSBG.BitSize - Count;
688
689 // Rotate that mask in the same way as RxSBG.Input is rotated.
690 if (RxSBG.Rotate != 0)
691 ShiftedIn = ((ShiftedIn << RxSBG.Rotate) |
692 (ShiftedIn >> (64 - RxSBG.Rotate)));
693
694 // Fail if any of the zero or sign bits are used.
695 return (ShiftedIn & RxSBG.Mask) != 0;
696}
697
Richard Sandiford54b36912013-09-27 15:14:04 +0000698bool SystemZDAGToDAGISel::expandRxSBG(RxSBGOperands &RxSBG) const {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000699 SDValue N = RxSBG.Input;
Richard Sandiford297f7d22013-07-18 10:14:55 +0000700 unsigned Opcode = N.getOpcode();
701 switch (Opcode) {
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000702 case ISD::AND: {
Richard Sandiford51093212013-07-18 10:40:35 +0000703 if (RxSBG.Opcode == SystemZ::RNSBG)
704 return false;
705
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000706 ConstantSDNode *MaskNode =
707 dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
708 if (!MaskNode)
709 return false;
710
711 SDValue Input = N.getOperand(0);
712 uint64_t Mask = MaskNode->getZExtValue();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000713 if (!refineRxSBGMask(RxSBG, Mask)) {
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000714 // If some bits of Input are already known zeros, those bits will have
715 // been removed from the mask. See if adding them back in makes the
716 // mask suitable.
717 APInt KnownZero, KnownOne;
718 CurDAG->ComputeMaskedBits(Input, KnownZero, KnownOne);
719 Mask |= KnownZero.getZExtValue();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000720 if (!refineRxSBGMask(RxSBG, Mask))
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000721 return false;
722 }
Richard Sandiford5cbac962013-07-18 09:45:08 +0000723 RxSBG.Input = Input;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000724 return true;
725 }
726
Richard Sandiford51093212013-07-18 10:40:35 +0000727 case ISD::OR: {
728 if (RxSBG.Opcode != SystemZ::RNSBG)
729 return false;
730
731 ConstantSDNode *MaskNode =
732 dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
733 if (!MaskNode)
734 return false;
735
736 SDValue Input = N.getOperand(0);
737 uint64_t Mask = ~MaskNode->getZExtValue();
738 if (!refineRxSBGMask(RxSBG, Mask)) {
739 // If some bits of Input are already known ones, those bits will have
740 // been removed from the mask. See if adding them back in makes the
741 // mask suitable.
742 APInt KnownZero, KnownOne;
743 CurDAG->ComputeMaskedBits(Input, KnownZero, KnownOne);
744 Mask &= ~KnownOne.getZExtValue();
745 if (!refineRxSBGMask(RxSBG, Mask))
746 return false;
747 }
748 RxSBG.Input = Input;
749 return true;
750 }
751
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000752 case ISD::ROTL: {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000753 // Any 64-bit rotate left can be merged into the RxSBG.
754 if (RxSBG.BitSize != 64)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000755 return false;
756 ConstantSDNode *CountNode
757 = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
758 if (!CountNode)
759 return false;
760
Richard Sandiford5cbac962013-07-18 09:45:08 +0000761 RxSBG.Rotate = (RxSBG.Rotate + CountNode->getZExtValue()) & 63;
762 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000763 return true;
764 }
765
766 case ISD::SHL: {
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000767 ConstantSDNode *CountNode =
768 dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
769 if (!CountNode)
770 return false;
771
772 uint64_t Count = CountNode->getZExtValue();
Richard Sandiford51093212013-07-18 10:40:35 +0000773 if (Count < 1 || Count >= RxSBG.BitSize)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000774 return false;
775
Richard Sandiford51093212013-07-18 10:40:35 +0000776 if (RxSBG.Opcode == SystemZ::RNSBG) {
777 // Treat (shl X, count) as (rotl X, size-count) as long as the bottom
778 // count bits from RxSBG.Input are ignored.
779 if (shiftedInBitsMatter(RxSBG, Count, true))
780 return false;
781 } else {
782 // Treat (shl X, count) as (and (rotl X, count), ~0<<count).
783 if (!refineRxSBGMask(RxSBG, allOnes(RxSBG.BitSize - Count) << Count))
784 return false;
785 }
786
Richard Sandiford5cbac962013-07-18 09:45:08 +0000787 RxSBG.Rotate = (RxSBG.Rotate + Count) & 63;
788 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000789 return true;
790 }
791
Richard Sandiford297f7d22013-07-18 10:14:55 +0000792 case ISD::SRL:
793 case ISD::SRA: {
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000794 ConstantSDNode *CountNode =
795 dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
796 if (!CountNode)
797 return false;
798
799 uint64_t Count = CountNode->getZExtValue();
Richard Sandiford297f7d22013-07-18 10:14:55 +0000800 if (Count < 1 || Count >= RxSBG.BitSize)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000801 return false;
802
Richard Sandiford51093212013-07-18 10:40:35 +0000803 if (RxSBG.Opcode == SystemZ::RNSBG || Opcode == ISD::SRA) {
804 // Treat (srl|sra X, count) as (rotl X, size-count) as long as the top
805 // count bits from RxSBG.Input are ignored.
Richard Sandiford297f7d22013-07-18 10:14:55 +0000806 if (shiftedInBitsMatter(RxSBG, Count, false))
807 return false;
808 } else {
809 // Treat (srl X, count), mask) as (and (rotl X, size-count), ~0>>count),
810 // which is similar to SLL above.
811 if (!refineRxSBGMask(RxSBG, allOnes(RxSBG.BitSize - Count)))
812 return false;
813 }
814
Richard Sandiford5cbac962013-07-18 09:45:08 +0000815 RxSBG.Rotate = (RxSBG.Rotate - Count) & 63;
816 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000817 return true;
818 }
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000819 default:
820 return false;
821 }
822}
823
Richard Sandiford54b36912013-09-27 15:14:04 +0000824SDValue SystemZDAGToDAGISel::getUNDEF64(SDLoc DL) const {
Richard Sandiford84f54a32013-07-11 08:59:12 +0000825 SDNode *N = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::i64);
826 return SDValue(N, 0);
827}
828
Richard Sandiford54b36912013-09-27 15:14:04 +0000829SDValue SystemZDAGToDAGISel::convertTo(SDLoc DL, EVT VT, SDValue N) const {
Richard Sandifordd8163202013-09-13 09:12:44 +0000830 if (N.getValueType() == MVT::i32 && VT == MVT::i64)
831 return CurDAG->getTargetInsertSubreg(SystemZ::subreg_32bit,
832 DL, VT, getUNDEF64(DL), N);
833 if (N.getValueType() == MVT::i64 && VT == MVT::i32)
834 return CurDAG->getTargetExtractSubreg(SystemZ::subreg_32bit, DL, VT, N);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000835 assert(N.getValueType() == VT && "Unexpected value types");
836 return N;
837}
838
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000839SDNode *SystemZDAGToDAGISel::tryRISBGZero(SDNode *N) {
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000840 EVT VT = N->getValueType(0);
Richard Sandiford51093212013-07-18 10:40:35 +0000841 RxSBGOperands RISBG(SystemZ::RISBG, SDValue(N, 0));
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000842 unsigned Count = 0;
Richard Sandiford5cbac962013-07-18 09:45:08 +0000843 while (expandRxSBG(RISBG))
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000844 Count += 1;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000845 if (Count == 0)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000846 return 0;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000847 if (Count == 1) {
848 // Prefer to use normal shift instructions over RISBG, since they can handle
849 // all cases and are sometimes shorter.
850 if (N->getOpcode() != ISD::AND)
851 return 0;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000852
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000853 // Prefer register extensions like LLC over RISBG. Also prefer to start
854 // out with normal ANDs if one instruction would be enough. We can convert
855 // these ANDs into an RISBG later if a three-address instruction is useful.
856 if (VT == MVT::i32 ||
857 RISBG.Mask == 0xff ||
858 RISBG.Mask == 0xffff ||
859 SystemZ::isImmLF(~RISBG.Mask) ||
860 SystemZ::isImmHF(~RISBG.Mask)) {
861 // Force the new mask into the DAG, since it may include known-one bits.
862 ConstantSDNode *MaskN = cast<ConstantSDNode>(N->getOperand(1).getNode());
863 if (MaskN->getZExtValue() != RISBG.Mask) {
864 SDValue NewMask = CurDAG->getConstant(RISBG.Mask, VT);
865 N = CurDAG->UpdateNodeOperands(N, N->getOperand(0), NewMask);
866 return SelectCode(N);
867 }
868 return 0;
869 }
870 }
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000871
Richard Sandiford84f54a32013-07-11 08:59:12 +0000872 SDValue Ops[5] = {
873 getUNDEF64(SDLoc(N)),
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000874 convertTo(SDLoc(N), MVT::i64, RISBG.Input),
875 CurDAG->getTargetConstant(RISBG.Start, MVT::i32),
876 CurDAG->getTargetConstant(RISBG.End | 128, MVT::i32),
877 CurDAG->getTargetConstant(RISBG.Rotate, MVT::i32)
Richard Sandiford84f54a32013-07-11 08:59:12 +0000878 };
879 N = CurDAG->getMachineNode(SystemZ::RISBG, SDLoc(N), MVT::i64, Ops);
880 return convertTo(SDLoc(N), VT, SDValue(N, 0)).getNode();
881}
882
Richard Sandiford7878b852013-07-18 10:06:15 +0000883SDNode *SystemZDAGToDAGISel::tryRxSBG(SDNode *N, unsigned Opcode) {
884 // Try treating each operand of N as the second operand of the RxSBG
Richard Sandiford885140c2013-07-16 11:55:57 +0000885 // and see which goes deepest.
Richard Sandiford51093212013-07-18 10:40:35 +0000886 RxSBGOperands RxSBG[] = {
887 RxSBGOperands(Opcode, N->getOperand(0)),
888 RxSBGOperands(Opcode, N->getOperand(1))
889 };
Richard Sandiford885140c2013-07-16 11:55:57 +0000890 unsigned Count[] = { 0, 0 };
891 for (unsigned I = 0; I < 2; ++I)
Richard Sandiford5cbac962013-07-18 09:45:08 +0000892 while (expandRxSBG(RxSBG[I]))
Richard Sandiford885140c2013-07-16 11:55:57 +0000893 Count[I] += 1;
894
895 // Do nothing if neither operand is suitable.
896 if (Count[0] == 0 && Count[1] == 0)
897 return 0;
898
899 // Pick the deepest second operand.
900 unsigned I = Count[0] > Count[1] ? 0 : 1;
901 SDValue Op0 = N->getOperand(I ^ 1);
902
903 // Prefer IC for character insertions from memory.
Richard Sandiford7878b852013-07-18 10:06:15 +0000904 if (Opcode == SystemZ::ROSBG && (RxSBG[I].Mask & 0xff) == 0)
Richard Sandiford885140c2013-07-16 11:55:57 +0000905 if (LoadSDNode *Load = dyn_cast<LoadSDNode>(Op0.getNode()))
906 if (Load->getMemoryVT() == MVT::i8)
907 return 0;
908
909 // See whether we can avoid an AND in the first operand by converting
910 // ROSBG to RISBG.
Richard Sandiford7878b852013-07-18 10:06:15 +0000911 if (Opcode == SystemZ::ROSBG && detectOrAndInsertion(Op0, RxSBG[I].Mask))
Richard Sandiford885140c2013-07-16 11:55:57 +0000912 Opcode = SystemZ::RISBG;
913
914 EVT VT = N->getValueType(0);
915 SDValue Ops[5] = {
916 convertTo(SDLoc(N), MVT::i64, Op0),
Richard Sandiford5cbac962013-07-18 09:45:08 +0000917 convertTo(SDLoc(N), MVT::i64, RxSBG[I].Input),
918 CurDAG->getTargetConstant(RxSBG[I].Start, MVT::i32),
919 CurDAG->getTargetConstant(RxSBG[I].End, MVT::i32),
920 CurDAG->getTargetConstant(RxSBG[I].Rotate, MVT::i32)
Richard Sandiford885140c2013-07-16 11:55:57 +0000921 };
922 N = CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i64, Ops);
923 return convertTo(SDLoc(N), VT, SDValue(N, 0)).getNode();
924}
925
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000926SDNode *SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node,
927 SDValue Op0, uint64_t UpperVal,
928 uint64_t LowerVal) {
929 EVT VT = Node->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000930 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000931 SDValue Upper = CurDAG->getConstant(UpperVal, VT);
932 if (Op0.getNode())
933 Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper);
934 Upper = SDValue(Select(Upper.getNode()), 0);
935
936 SDValue Lower = CurDAG->getConstant(LowerVal, VT);
937 SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower);
938 return Or.getNode();
939}
940
Richard Sandiford178273a2013-09-05 10:36:45 +0000941// Return true if Load and Store:
942// - are loads and stores of the same size;
943// - do not partially overlap; and
944// - can be decomposed into what are logically individual character accesses
945// without changing the semantics.
946static bool canUseBlockOperation(StoreSDNode *Store, LoadSDNode *Load,
947 AliasAnalysis *AA) {
948 // Check that the two memory operands have the same size.
949 if (Load->getMemoryVT() != Store->getMemoryVT())
Richard Sandiford97846492013-07-09 09:46:39 +0000950 return false;
951
Richard Sandiford178273a2013-09-05 10:36:45 +0000952 // Volatility stops an access from being decomposed.
953 if (Load->isVolatile() || Store->isVolatile())
954 return false;
Richard Sandiford97846492013-07-09 09:46:39 +0000955
956 // There's no chance of overlap if the load is invariant.
957 if (Load->isInvariant())
958 return true;
959
960 // If both operands are aligned, they must be equal or not overlap.
Richard Sandiford178273a2013-09-05 10:36:45 +0000961 uint64_t Size = Load->getMemoryVT().getStoreSize();
Richard Sandiford97846492013-07-09 09:46:39 +0000962 if (Load->getAlignment() >= Size && Store->getAlignment() >= Size)
963 return true;
964
965 // Otherwise we need to check whether there's an alias.
966 const Value *V1 = Load->getSrcValue();
967 const Value *V2 = Store->getSrcValue();
968 if (!V1 || !V2)
969 return false;
970
971 int64_t End1 = Load->getSrcValueOffset() + Size;
972 int64_t End2 = Store->getSrcValueOffset() + Size;
973 return !AA->alias(AliasAnalysis::Location(V1, End1, Load->getTBAAInfo()),
974 AliasAnalysis::Location(V2, End2, Store->getTBAAInfo()));
975}
976
Richard Sandiford178273a2013-09-05 10:36:45 +0000977bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const {
978 StoreSDNode *Store = cast<StoreSDNode>(N);
979 LoadSDNode *Load = cast<LoadSDNode>(Store->getValue());
980
981 // Prefer not to use MVC if either address can use ... RELATIVE LONG
982 // instructions.
983 uint64_t Size = Load->getMemoryVT().getStoreSize();
984 if (Size > 1 && Size <= 8) {
985 // Prefer LHRL, LRL and LGRL.
Richard Sandiford54b36912013-09-27 15:14:04 +0000986 if (SystemZISD::isPCREL(Load->getBasePtr().getOpcode()))
Richard Sandiford178273a2013-09-05 10:36:45 +0000987 return false;
988 // Prefer STHRL, STRL and STGRL.
Richard Sandiford54b36912013-09-27 15:14:04 +0000989 if (SystemZISD::isPCREL(Store->getBasePtr().getOpcode()))
Richard Sandiford178273a2013-09-05 10:36:45 +0000990 return false;
991 }
992
993 return canUseBlockOperation(Store, Load, AA);
994}
995
996bool SystemZDAGToDAGISel::storeLoadCanUseBlockBinary(SDNode *N,
997 unsigned I) const {
998 StoreSDNode *StoreA = cast<StoreSDNode>(N);
999 LoadSDNode *LoadA = cast<LoadSDNode>(StoreA->getValue().getOperand(1 - I));
1000 LoadSDNode *LoadB = cast<LoadSDNode>(StoreA->getValue().getOperand(I));
1001 if (LoadA->isVolatile() ||
1002 LoadA->getMemoryVT() != StoreA->getMemoryVT() ||
1003 LoadA->getBasePtr() != StoreA->getBasePtr())
1004 return false;
1005 return canUseBlockOperation(StoreA, LoadB, AA);
1006}
1007
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001008SDNode *SystemZDAGToDAGISel::Select(SDNode *Node) {
1009 // Dump information about the Node being selected
1010 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
1011
1012 // If we have a custom node, we already have selected!
1013 if (Node->isMachineOpcode()) {
1014 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Tim Northover31d093c2013-09-22 08:21:56 +00001015 Node->setNodeId(-1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001016 return 0;
1017 }
1018
1019 unsigned Opcode = Node->getOpcode();
Richard Sandiford84f54a32013-07-11 08:59:12 +00001020 SDNode *ResNode = 0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001021 switch (Opcode) {
1022 case ISD::OR:
Richard Sandiford885140c2013-07-16 11:55:57 +00001023 if (Node->getOperand(1).getOpcode() != ISD::Constant)
Richard Sandiford7878b852013-07-18 10:06:15 +00001024 ResNode = tryRxSBG(Node, SystemZ::ROSBG);
1025 goto or_xor;
1026
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001027 case ISD::XOR:
Richard Sandiford7878b852013-07-18 10:06:15 +00001028 if (Node->getOperand(1).getOpcode() != ISD::Constant)
1029 ResNode = tryRxSBG(Node, SystemZ::RXSBG);
1030 // Fall through.
1031 or_xor:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001032 // If this is a 64-bit operation in which both 32-bit halves are nonzero,
1033 // split the operation into two.
Richard Sandiford885140c2013-07-16 11:55:57 +00001034 if (!ResNode && Node->getValueType(0) == MVT::i64)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001035 if (ConstantSDNode *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) {
1036 uint64_t Val = Op1->getZExtValue();
1037 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val))
1038 Node = splitLargeImmediate(Opcode, Node, Node->getOperand(0),
1039 Val - uint32_t(Val), uint32_t(Val));
1040 }
1041 break;
1042
Richard Sandiford84f54a32013-07-11 08:59:12 +00001043 case ISD::AND:
Richard Sandiford51093212013-07-18 10:40:35 +00001044 if (Node->getOperand(1).getOpcode() != ISD::Constant)
1045 ResNode = tryRxSBG(Node, SystemZ::RNSBG);
1046 // Fall through.
Richard Sandiford82ec87d2013-07-16 11:02:24 +00001047 case ISD::ROTL:
1048 case ISD::SHL:
1049 case ISD::SRL:
Richard Sandiford7878b852013-07-18 10:06:15 +00001050 if (!ResNode)
1051 ResNode = tryRISBGZero(Node);
Richard Sandiford84f54a32013-07-11 08:59:12 +00001052 break;
1053
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001054 case ISD::Constant:
1055 // If this is a 64-bit constant that is out of the range of LLILF,
1056 // LLIHF and LGFI, split it into two 32-bit pieces.
1057 if (Node->getValueType(0) == MVT::i64) {
1058 uint64_t Val = cast<ConstantSDNode>(Node)->getZExtValue();
1059 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val))
1060 Node = splitLargeImmediate(ISD::OR, Node, SDValue(),
1061 Val - uint32_t(Val), uint32_t(Val));
1062 }
1063 break;
1064
1065 case ISD::ATOMIC_LOAD_SUB:
1066 // Try to convert subtractions of constants to additions.
1067 if (ConstantSDNode *Op2 = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
1068 uint64_t Value = -Op2->getZExtValue();
1069 EVT VT = Node->getValueType(0);
1070 if (VT == MVT::i32 || isInt<32>(Value)) {
1071 SDValue Ops[] = { Node->getOperand(0), Node->getOperand(1),
1072 CurDAG->getConstant(int32_t(Value), VT) };
1073 Node = CurDAG->MorphNodeTo(Node, ISD::ATOMIC_LOAD_ADD,
1074 Node->getVTList(), Ops, array_lengthof(Ops));
1075 }
1076 }
1077 break;
Richard Sandifordee834382013-07-31 12:38:08 +00001078
1079 case SystemZISD::SELECT_CCMASK: {
1080 SDValue Op0 = Node->getOperand(0);
1081 SDValue Op1 = Node->getOperand(1);
1082 // Prefer to put any load first, so that it can be matched as a
1083 // conditional load.
1084 if (Op1.getOpcode() == ISD::LOAD && Op0.getOpcode() != ISD::LOAD) {
1085 SDValue CCValid = Node->getOperand(2);
1086 SDValue CCMask = Node->getOperand(3);
1087 uint64_t ConstCCValid =
1088 cast<ConstantSDNode>(CCValid.getNode())->getZExtValue();
1089 uint64_t ConstCCMask =
1090 cast<ConstantSDNode>(CCMask.getNode())->getZExtValue();
1091 // Invert the condition.
1092 CCMask = CurDAG->getConstant(ConstCCValid ^ ConstCCMask,
1093 CCMask.getValueType());
1094 SDValue Op4 = Node->getOperand(4);
1095 Node = CurDAG->UpdateNodeOperands(Node, Op1, Op0, CCValid, CCMask, Op4);
1096 }
1097 break;
1098 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001099 }
1100
1101 // Select the default instruction
Richard Sandiford84f54a32013-07-11 08:59:12 +00001102 if (!ResNode)
1103 ResNode = SelectCode(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001104
1105 DEBUG(errs() << "=> ";
1106 if (ResNode == NULL || ResNode == Node)
1107 Node->dump(CurDAG);
1108 else
1109 ResNode->dump(CurDAG);
1110 errs() << "\n";
1111 );
1112 return ResNode;
1113}
1114
1115bool SystemZDAGToDAGISel::
1116SelectInlineAsmMemoryOperand(const SDValue &Op,
1117 char ConstraintCode,
1118 std::vector<SDValue> &OutOps) {
1119 assert(ConstraintCode == 'm' && "Unexpected constraint code");
1120 // Accept addresses with short displacements, which are compatible
1121 // with Q, R, S and T. But keep the index operand for future expansion.
1122 SDValue Base, Disp, Index;
1123 if (!selectBDXAddr(SystemZAddressingMode::FormBD,
1124 SystemZAddressingMode::Disp12Only,
1125 Op, Base, Disp, Index))
1126 return true;
1127 OutOps.push_back(Base);
1128 OutOps.push_back(Disp);
1129 OutOps.push_back(Index);
1130 return false;
1131}