blob: fb0c5dc42d37146be343a8af6141548f1eda87ff [file] [log] [blame]
Anton Korobeynikov4403b932009-07-16 13:27:25 +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 "SystemZ.h"
15#include "SystemZISelLowering.h"
16#include "SystemZTargetMachine.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Intrinsics.h"
20#include "llvm/CallingConv.h"
21#include "llvm/Constants.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
26#include "llvm/CodeGen/SelectionDAG.h"
27#include "llvm/CodeGen/SelectionDAGISel.h"
28#include "llvm/Target/TargetLowering.h"
29#include "llvm/Support/Compiler.h"
30#include "llvm/Support/Debug.h"
31using namespace llvm;
32
Anton Korobeynikov3360da92009-07-16 13:44:00 +000033namespace {
34 /// SystemZRRIAddressMode - This corresponds to rriaddr, but uses SDValue's
35 /// instead of register numbers for the leaves of the matched tree.
36 struct SystemZRRIAddressMode {
37 enum {
38 RegBase,
39 FrameIndexBase
40 } BaseType;
41
42 struct { // This is really a union, discriminated by BaseType!
43 SDValue Reg;
44 int FrameIndex;
45 } Base;
46
47 SDValue IndexReg;
Anton Korobeynikov32407402009-07-16 13:48:23 +000048 int64_t Disp;
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +000049 bool isRI;
Anton Korobeynikov3360da92009-07-16 13:44:00 +000050
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +000051 SystemZRRIAddressMode(bool RI = false)
52 : BaseType(RegBase), IndexReg(), Disp(0), isRI(RI) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +000053 }
54
55 void dump() {
Anton Korobeynikov961bb6f2009-07-16 13:45:00 +000056 cerr << "SystemZRRIAddressMode " << this << '\n';
Anton Korobeynikov3360da92009-07-16 13:44:00 +000057 if (BaseType == RegBase) {
58 cerr << "Base.Reg ";
59 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
60 else cerr << "nul";
Anton Korobeynikov961bb6f2009-07-16 13:45:00 +000061 cerr << '\n';
Anton Korobeynikov3360da92009-07-16 13:44:00 +000062 } else {
Anton Korobeynikov961bb6f2009-07-16 13:45:00 +000063 cerr << " Base.FrameIndex " << Base.FrameIndex << '\n';
Anton Korobeynikov3360da92009-07-16 13:44:00 +000064 }
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +000065 if (!isRI) {
66 cerr << "IndexReg ";
67 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
68 else cerr << "nul";
69 }
Anton Korobeynikov961bb6f2009-07-16 13:45:00 +000070 cerr << " Disp " << Disp << '\n';
Anton Korobeynikov3360da92009-07-16 13:44:00 +000071 }
72 };
73}
74
Anton Korobeynikov4403b932009-07-16 13:27:25 +000075/// SystemZDAGToDAGISel - SystemZ specific code to select SystemZ machine
76/// instructions for SelectionDAG operations.
77///
78namespace {
79 class SystemZDAGToDAGISel : public SelectionDAGISel {
80 SystemZTargetLowering &Lowering;
81 const SystemZSubtarget &Subtarget;
82
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +000083 void getAddressOperandsRI(const SystemZRRIAddressMode &AM,
84 SDValue &Base, SDValue &Disp);
Anton Korobeynikov720e3b02009-07-16 14:09:35 +000085 void getAddressOperands(const SystemZRRIAddressMode &AM,
86 SDValue &Base, SDValue &Disp,
87 SDValue &Index);
88
Anton Korobeynikov4403b932009-07-16 13:27:25 +000089 public:
90 SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel)
91 : SelectionDAGISel(TM, OptLevel),
92 Lowering(*TM.getTargetLowering()),
93 Subtarget(*TM.getSubtargetImpl()) { }
94
95 virtual void InstructionSelect();
96
97 virtual const char *getPassName() const {
98 return "SystemZ DAG->DAG Pattern Instruction Selection";
99 }
100
Anton Korobeynikov89edcd02009-07-16 13:33:57 +0000101 /// getI16Imm - Return a target constant with the specified value, of type
102 /// i16.
103 inline SDValue getI16Imm(uint64_t Imm) {
104 return CurDAG->getTargetConstant(Imm, MVT::i16);
105 }
106
Anton Korobeynikovda308c92009-07-16 13:34:50 +0000107 /// getI32Imm - Return a target constant with the specified value, of type
108 /// i32.
109 inline SDValue getI32Imm(uint64_t Imm) {
110 return CurDAG->getTargetConstant(Imm, MVT::i32);
111 }
112
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000113 // Include the pieces autogenerated from the target description.
Anton Korobeynikov89edcd02009-07-16 13:33:57 +0000114 #include "SystemZGenDAGISel.inc"
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000115
116 private:
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000117 bool SelectAddrRI12(SDValue Op, SDValue& Addr,
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000118 SDValue &Base, SDValue &Disp);
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000119 bool SelectAddrRI(SDValue Op, SDValue& Addr,
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000120 SDValue &Base, SDValue &Disp);
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000121 bool SelectAddrRRI12(SDValue Op, SDValue Addr,
122 SDValue &Base, SDValue &Disp, SDValue &Index);
123 bool SelectAddrRRI20(SDValue Op, SDValue Addr,
124 SDValue &Base, SDValue &Disp, SDValue &Index);
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000125 bool SelectLAAddr(SDValue Op, SDValue Addr,
126 SDValue &Base, SDValue &Disp, SDValue &Index);
127
128 SDNode *Select(SDValue Op);
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000129 bool MatchAddress(SDValue N, SystemZRRIAddressMode &AM,
130 bool is12Bit, unsigned Depth = 0);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000131 bool MatchAddressBase(SDValue N, SystemZRRIAddressMode &AM);
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000132 bool MatchAddressRI(SDValue N, SystemZRRIAddressMode &AM,
133 bool is12Bit);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000134
135 #ifndef NDEBUG
136 unsigned Indent;
137 #endif
138 };
139} // end anonymous namespace
140
141/// createSystemZISelDag - This pass converts a legalized DAG into a
142/// SystemZ-specific DAG, ready for instruction scheduling.
143///
144FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
145 CodeGenOpt::Level OptLevel) {
146 return new SystemZDAGToDAGISel(TM, OptLevel);
147}
148
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000149/// isImmSExt20 - This method tests to see if the node is either a 32-bit
150/// or 64-bit immediate, and if the value can be accurately represented as a
151/// sign extension from a 20-bit value. If so, this returns true and the
152/// immediate.
Anton Korobeynikov32407402009-07-16 13:48:23 +0000153static bool isImmSExt20(int64_t Val, int64_t &Imm) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000154 if (Val >= -524288 && Val <= 524287) {
Anton Korobeynikov32407402009-07-16 13:48:23 +0000155 Imm = Val;
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000156 return true;
157 }
158 return false;
159}
160
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000161/// isImmZExt12 - This method tests to see if the node is either a 32-bit
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000162/// or 64-bit immediate, and if the value can be accurately represented as a
163/// zero extension from a 12-bit value. If so, this returns true and the
164/// immediate.
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000165static bool isImmZExt12(int64_t Val, int64_t &Imm) {
166 if (Val >= 0 && Val <= 0xFFF) {
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000167 Imm = Val;
168 return true;
169 }
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000170 return false;
171}
172
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000173/// MatchAddress - Add the specified node to the specified addressing mode,
174/// returning true if it cannot be done. This just pattern matches for the
175/// addressing mode.
176bool SystemZDAGToDAGISel::MatchAddress(SDValue N, SystemZRRIAddressMode &AM,
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000177 bool is12Bit, unsigned Depth) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000178 DebugLoc dl = N.getDebugLoc();
179 DOUT << "MatchAddress: "; DEBUG(AM.dump());
180 // Limit recursion.
181 if (Depth > 5)
182 return MatchAddressBase(N, AM);
183
Anton Korobeynikovdc289552009-07-16 13:44:30 +0000184 // FIXME: We can perform better here. If we have something like
185 // (shift (add A, imm), N), we can try to reassociate stuff and fold shift of
186 // imm into addressing mode.
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000187 switch (N.getOpcode()) {
188 default: break;
189 case ISD::Constant: {
Anton Korobeynikov32407402009-07-16 13:48:23 +0000190 int64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
191 int64_t Imm;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000192 bool Match = (is12Bit ?
193 isImmZExt12(AM.Disp + Val, Imm) :
194 isImmSExt20(AM.Disp + Val, Imm));
195 if (Match) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000196 AM.Disp = Imm;
197 return false;
198 }
199 break;
200 }
201
202 case ISD::FrameIndex:
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000203 if (AM.BaseType == SystemZRRIAddressMode::RegBase &&
204 AM.Base.Reg.getNode() == 0) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000205 AM.BaseType = SystemZRRIAddressMode::FrameIndexBase;
206 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
207 return false;
208 }
209 break;
210
211 case ISD::SUB: {
212 // Given A-B, if A can be completely folded into the address and
213 // the index field with the index field unused, use -B as the index.
214 // This is a win if a has multiple parts that can be folded into
215 // the address. Also, this saves a mov if the base register has
216 // other uses, since it avoids a two-address sub instruction, however
217 // it costs an additional mov if the index register has other uses.
218
219 // Test if the LHS of the sub can be folded.
220 SystemZRRIAddressMode Backup = AM;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000221 if (MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1)) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000222 AM = Backup;
223 break;
224 }
225 // Test if the index field is free for use.
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000226 if (AM.IndexReg.getNode() && !AM.isRI) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000227 AM = Backup;
228 break;
229 }
230
231 // If the base is a register with multiple uses, this transformation may
232 // save a mov. Otherwise it's probably better not to do it.
233 if (AM.BaseType == SystemZRRIAddressMode::RegBase &&
234 (!AM.Base.Reg.getNode() || AM.Base.Reg.getNode()->hasOneUse())) {
235 AM = Backup;
236 break;
237 }
238
239 // Ok, the transformation is legal and appears profitable. Go for it.
240 SDValue RHS = N.getNode()->getOperand(1);
241 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
242 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
243 AM.IndexReg = Neg;
244
245 // Insert the new nodes into the topological ordering.
246 if (Zero.getNode()->getNodeId() == -1 ||
247 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
248 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
249 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
250 }
251 if (Neg.getNode()->getNodeId() == -1 ||
252 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
253 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
254 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
255 }
256 return false;
257 }
258
259 case ISD::ADD: {
260 SystemZRRIAddressMode Backup = AM;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000261 if (!MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1) &&
262 !MatchAddress(N.getNode()->getOperand(1), AM, is12Bit, Depth+1))
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000263 return false;
264 AM = Backup;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000265 if (!MatchAddress(N.getNode()->getOperand(1), AM, is12Bit, Depth+1) &&
266 !MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1))
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000267 return false;
268 AM = Backup;
269
270 // If we couldn't fold both operands into the address at the same time,
271 // see if we can just put each operand into a register and fold at least
272 // the add.
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000273 if (!AM.isRI &&
274 AM.BaseType == SystemZRRIAddressMode::RegBase &&
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000275 !AM.Base.Reg.getNode() && !AM.IndexReg.getNode()) {
276 AM.Base.Reg = N.getNode()->getOperand(0);
277 AM.IndexReg = N.getNode()->getOperand(1);
278 return false;
279 }
280 break;
281 }
282
283 case ISD::OR:
284 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
285 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
286 SystemZRRIAddressMode Backup = AM;
Anton Korobeynikov32407402009-07-16 13:48:23 +0000287 int64_t Offset = CN->getSExtValue();
288 int64_t Imm;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000289 bool MatchOffset = (is12Bit ?
290 isImmZExt12(AM.Disp + Offset, Imm) :
291 isImmSExt20(AM.Disp + Offset, Imm));
292 // The resultant disp must fit in 12 or 20-bits.
293 if (MatchOffset &&
294 // LHS should be an addr mode.
295 !MatchAddress(N.getOperand(0), AM, is12Bit, Depth+1) &&
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000296 // Check to see if the LHS & C is zero.
297 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
298 AM.Disp = Imm;
299 return false;
300 }
301 AM = Backup;
302 }
303 break;
304 }
305
306 return MatchAddressBase(N, AM);
307}
308
309/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
310/// specified addressing mode without any further recursion.
311bool SystemZDAGToDAGISel::MatchAddressBase(SDValue N,
312 SystemZRRIAddressMode &AM) {
313 // Is the base register already occupied?
314 if (AM.BaseType != SystemZRRIAddressMode::RegBase || AM.Base.Reg.getNode()) {
Anton Korobeynikov46567602009-07-16 14:10:35 +0000315 // If so, check to see if the index register is set.
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000316 if (AM.IndexReg.getNode() == 0 && !AM.isRI) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000317 AM.IndexReg = N;
318 return false;
319 }
320
321 // Otherwise, we cannot select it.
322 return true;
323 }
324
325 // Default, generate it as a register.
326 AM.BaseType = SystemZRRIAddressMode::RegBase;
327 AM.Base.Reg = N;
328 return false;
329}
330
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000331void SystemZDAGToDAGISel::getAddressOperandsRI(const SystemZRRIAddressMode &AM,
332 SDValue &Base, SDValue &Disp) {
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000333 if (AM.BaseType == SystemZRRIAddressMode::RegBase)
334 Base = AM.Base.Reg;
335 else
336 Base = CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy());
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000337 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i64);
338}
339
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000340void SystemZDAGToDAGISel::getAddressOperands(const SystemZRRIAddressMode &AM,
341 SDValue &Base, SDValue &Disp,
342 SDValue &Index) {
343 getAddressOperandsRI(AM, Base, Disp);
344 Index = AM.IndexReg;
345}
346
347/// Returns true if the address can be represented by a base register plus
348/// an unsigned 12-bit displacement [r+imm].
349bool SystemZDAGToDAGISel::SelectAddrRI12(SDValue Op, SDValue& Addr,
350 SDValue &Base, SDValue &Disp) {
351 SystemZRRIAddressMode AM20(/*isRI*/true), AM12(/*isRI*/true);
352 bool Done = false;
353
354 if (!Addr.hasOneUse()) {
355 unsigned Opcode = Addr.getOpcode();
356 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
357 // If we are able to fold N into addressing mode, then we'll allow it even
358 // if N has multiple uses. In general, addressing computation is used as
359 // addresses by all of its uses. But watch out for CopyToReg uses, that
360 // means the address computation is liveout. It will be computed by a LA
361 // so we want to avoid computing the address twice.
362 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
363 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
364 if (UI->getOpcode() == ISD::CopyToReg) {
365 MatchAddressBase(Addr, AM12);
366 Done = true;
367 break;
368 }
369 }
370 }
371 }
372 if (!Done && MatchAddress(Addr, AM12, /* is12Bit */ true))
373 return false;
374
375 // Check, whether we can match stuff using 20-bit displacements
376 if (!Done && !MatchAddress(Addr, AM20, /* is12Bit */ false))
377 if (AM12.Disp == 0 && AM20.Disp != 0)
378 return false;
379
380 DOUT << "MatchAddress (final): "; DEBUG(AM12.dump());
381
382 MVT VT = Addr.getValueType();
383 if (AM12.BaseType == SystemZRRIAddressMode::RegBase) {
384 if (!AM12.Base.Reg.getNode())
385 AM12.Base.Reg = CurDAG->getRegister(0, VT);
386 }
387
388 assert(AM12.IndexReg.getNode() == 0 && "Invalid reg-imm address mode!");
389
390 getAddressOperandsRI(AM12, Base, Disp);
391
392 return true;
393}
394
395/// Returns true if the address can be represented by a base register plus
396/// a signed 20-bit displacement [r+imm].
397bool SystemZDAGToDAGISel::SelectAddrRI(SDValue Op, SDValue& Addr,
398 SDValue &Base, SDValue &Disp) {
399 SystemZRRIAddressMode AM(/*isRI*/true);
400 bool Done = false;
401
402 if (!Addr.hasOneUse()) {
403 unsigned Opcode = Addr.getOpcode();
404 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
405 // If we are able to fold N into addressing mode, then we'll allow it even
406 // if N has multiple uses. In general, addressing computation is used as
407 // addresses by all of its uses. But watch out for CopyToReg uses, that
408 // means the address computation is liveout. It will be computed by a LA
409 // so we want to avoid computing the address twice.
410 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
411 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
412 if (UI->getOpcode() == ISD::CopyToReg) {
413 MatchAddressBase(Addr, AM);
414 Done = true;
415 break;
416 }
417 }
418 }
419 }
420 if (!Done && MatchAddress(Addr, AM, /* is12Bit */ false))
421 return false;
422
423 DOUT << "MatchAddress (final): "; DEBUG(AM.dump());
424
425 MVT VT = Addr.getValueType();
426 if (AM.BaseType == SystemZRRIAddressMode::RegBase) {
427 if (!AM.Base.Reg.getNode())
428 AM.Base.Reg = CurDAG->getRegister(0, VT);
429 }
430
431 assert(AM.IndexReg.getNode() == 0 && "Invalid reg-imm address mode!");
432
433 getAddressOperandsRI(AM, Base, Disp);
434
435 return true;
436}
437
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000438/// Returns true if the address can be represented by a base register plus
439/// index register plus an unsigned 12-bit displacement [base + idx + imm].
440bool SystemZDAGToDAGISel::SelectAddrRRI12(SDValue Op, SDValue Addr,
441 SDValue &Base, SDValue &Disp, SDValue &Index) {
Anton Korobeynikov46567602009-07-16 14:10:35 +0000442 SystemZRRIAddressMode AM20, AM12;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000443 bool Done = false;
444
445 if (!Addr.hasOneUse()) {
446 unsigned Opcode = Addr.getOpcode();
447 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
448 // If we are able to fold N into addressing mode, then we'll allow it even
449 // if N has multiple uses. In general, addressing computation is used as
450 // addresses by all of its uses. But watch out for CopyToReg uses, that
451 // means the address computation is liveout. It will be computed by a LA
452 // so we want to avoid computing the address twice.
453 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
454 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
455 if (UI->getOpcode() == ISD::CopyToReg) {
456 MatchAddressBase(Addr, AM12);
457 Done = true;
458 break;
459 }
460 }
461 }
462 }
463 if (!Done && MatchAddress(Addr, AM12, /* is12Bit */ true))
464 return false;
465
466 // Check, whether we can match stuff using 20-bit displacements
467 if (!Done && !MatchAddress(Addr, AM20, /* is12Bit */ false))
468 if (AM12.Disp == 0 && AM20.Disp != 0)
469 return false;
470
471 DOUT << "MatchAddress (final): "; DEBUG(AM12.dump());
472
473 MVT VT = Addr.getValueType();
474 if (AM12.BaseType == SystemZRRIAddressMode::RegBase) {
475 if (!AM12.Base.Reg.getNode())
476 AM12.Base.Reg = CurDAG->getRegister(0, VT);
477 }
478
479 if (!AM12.IndexReg.getNode())
480 AM12.IndexReg = CurDAG->getRegister(0, VT);
481
482 getAddressOperands(AM12, Base, Disp, Index);
483
484 return true;
485}
486
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000487/// Returns true if the address can be represented by a base register plus
488/// index register plus a signed 20-bit displacement [base + idx + imm].
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000489bool SystemZDAGToDAGISel::SelectAddrRRI20(SDValue Op, SDValue Addr,
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000490 SDValue &Base, SDValue &Disp, SDValue &Index) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000491 SystemZRRIAddressMode AM;
492 bool Done = false;
493
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000494 if (!Addr.hasOneUse()) {
495 unsigned Opcode = Addr.getOpcode();
496 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
497 // If we are able to fold N into addressing mode, then we'll allow it even
498 // if N has multiple uses. In general, addressing computation is used as
499 // addresses by all of its uses. But watch out for CopyToReg uses, that
500 // means the address computation is liveout. It will be computed by a LA
501 // so we want to avoid computing the address twice.
502 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
503 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
504 if (UI->getOpcode() == ISD::CopyToReg) {
505 MatchAddressBase(Addr, AM);
506 Done = true;
507 break;
508 }
509 }
510 }
511 }
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000512 if (!Done && MatchAddress(Addr, AM, /* is12Bit */ false))
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000513 return false;
514
Anton Korobeynikov32407402009-07-16 13:48:23 +0000515 DOUT << "MatchAddress (final): "; DEBUG(AM.dump());
516
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000517 MVT VT = Addr.getValueType();
518 if (AM.BaseType == SystemZRRIAddressMode::RegBase) {
519 if (!AM.Base.Reg.getNode())
520 AM.Base.Reg = CurDAG->getRegister(0, VT);
521 }
522
523 if (!AM.IndexReg.getNode())
524 AM.IndexReg = CurDAG->getRegister(0, VT);
525
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000526 getAddressOperands(AM, Base, Disp, Index);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000527
528 return true;
529}
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000530
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000531/// SelectLAAddr - it calls SelectAddr and determines if the maximal addressing
532/// mode it matches can be cost effectively emitted as an LA/LAY instruction.
533bool SystemZDAGToDAGISel::SelectLAAddr(SDValue Op, SDValue Addr,
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000534 SDValue &Base, SDValue &Disp, SDValue &Index) {
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000535 SystemZRRIAddressMode AM;
536
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000537 if (MatchAddress(Addr, AM, false))
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000538 return false;
539
540 MVT VT = Addr.getValueType();
541 unsigned Complexity = 0;
542 if (AM.BaseType == SystemZRRIAddressMode::RegBase)
543 if (AM.Base.Reg.getNode())
544 Complexity = 1;
545 else
546 AM.Base.Reg = CurDAG->getRegister(0, VT);
547 else if (AM.BaseType == SystemZRRIAddressMode::FrameIndexBase)
548 Complexity = 4;
549
550 if (AM.IndexReg.getNode())
551 Complexity += 1;
552 else
553 AM.IndexReg = CurDAG->getRegister(0, VT);
554
555 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
556 Complexity += 1;
557
558 if (Complexity > 2) {
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000559 getAddressOperands(AM, Base, Disp, Index);
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000560 return true;
561 }
562
563 return false;
564}
565
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000566/// InstructionSelect - This callback is invoked by
567/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
568void SystemZDAGToDAGISel::InstructionSelect() {
569 DEBUG(BB->dump());
570
571 // Codegen the basic block.
572#ifndef NDEBUG
573 DOUT << "===== Instruction selection begins:\n";
574 Indent = 0;
575#endif
576 SelectRoot(*CurDAG);
577#ifndef NDEBUG
578 DOUT << "===== Instruction selection ends:\n";
579#endif
580
581 CurDAG->RemoveDeadNodes();
582}
583
584SDNode *SystemZDAGToDAGISel::Select(SDValue Op) {
585 SDNode *Node = Op.getNode();
586 DebugLoc dl = Op.getDebugLoc();
587
588 // Dump information about the Node being selected
589 #ifndef NDEBUG
590 DOUT << std::string(Indent, ' ') << "Selecting: ";
591 DEBUG(Node->dump(CurDAG));
592 DOUT << "\n";
593 Indent += 2;
594 #endif
595
596 // If we have a custom node, we already have selected!
597 if (Node->isMachineOpcode()) {
598 #ifndef NDEBUG
599 DOUT << std::string(Indent-2, ' ') << "== ";
600 DEBUG(Node->dump(CurDAG));
601 DOUT << "\n";
602 Indent -= 2;
603 #endif
604 return NULL;
605 }
606
607 // Select the default instruction
608 SDNode *ResNode = SelectCode(Op);
609
610 #ifndef NDEBUG
611 DOUT << std::string(Indent-2, ' ') << "=> ";
612 if (ResNode == NULL || ResNode == Op.getNode())
613 DEBUG(Op.getNode()->dump(CurDAG));
614 else
615 DEBUG(ResNode->dump(CurDAG));
616 DOUT << "\n";
617 Indent -= 2;
618 #endif
619
620 return ResNode;
621}