blob: e009eec48b111d288b56089ff2b1632f51aee9b2 [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 Korobeynikov014d4632009-07-16 14:13:24 +0000117 bool SelectAddrRI12Only(SDValue Op, SDValue& Addr,
118 SDValue &Base, SDValue &Disp);
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000119 bool SelectAddrRI12(SDValue Op, SDValue& Addr,
Anton Korobeynikov014d4632009-07-16 14:13:24 +0000120 SDValue &Base, SDValue &Disp,
121 bool is12BitOnly = false);
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000122 bool SelectAddrRI(SDValue Op, SDValue& Addr,
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000123 SDValue &Base, SDValue &Disp);
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000124 bool SelectAddrRRI12(SDValue Op, SDValue Addr,
125 SDValue &Base, SDValue &Disp, SDValue &Index);
126 bool SelectAddrRRI20(SDValue Op, SDValue Addr,
127 SDValue &Base, SDValue &Disp, SDValue &Index);
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000128 bool SelectLAAddr(SDValue Op, SDValue Addr,
129 SDValue &Base, SDValue &Disp, SDValue &Index);
130
131 SDNode *Select(SDValue Op);
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000132 bool MatchAddress(SDValue N, SystemZRRIAddressMode &AM,
133 bool is12Bit, unsigned Depth = 0);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000134 bool MatchAddressBase(SDValue N, SystemZRRIAddressMode &AM);
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000135 bool MatchAddressRI(SDValue N, SystemZRRIAddressMode &AM,
136 bool is12Bit);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000137
138 #ifndef NDEBUG
139 unsigned Indent;
140 #endif
141 };
142} // end anonymous namespace
143
144/// createSystemZISelDag - This pass converts a legalized DAG into a
145/// SystemZ-specific DAG, ready for instruction scheduling.
146///
147FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
148 CodeGenOpt::Level OptLevel) {
149 return new SystemZDAGToDAGISel(TM, OptLevel);
150}
151
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000152/// isImmSExt20 - This method tests to see if the node is either a 32-bit
153/// or 64-bit immediate, and if the value can be accurately represented as a
154/// sign extension from a 20-bit value. If so, this returns true and the
155/// immediate.
Anton Korobeynikov32407402009-07-16 13:48:23 +0000156static bool isImmSExt20(int64_t Val, int64_t &Imm) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000157 if (Val >= -524288 && Val <= 524287) {
Anton Korobeynikov32407402009-07-16 13:48:23 +0000158 Imm = Val;
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000159 return true;
160 }
161 return false;
162}
163
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000164/// isImmZExt12 - This method tests to see if the node is either a 32-bit
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000165/// or 64-bit immediate, and if the value can be accurately represented as a
166/// zero extension from a 12-bit value. If so, this returns true and the
167/// immediate.
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000168static bool isImmZExt12(int64_t Val, int64_t &Imm) {
169 if (Val >= 0 && Val <= 0xFFF) {
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000170 Imm = Val;
171 return true;
172 }
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000173 return false;
174}
175
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000176/// MatchAddress - Add the specified node to the specified addressing mode,
177/// returning true if it cannot be done. This just pattern matches for the
178/// addressing mode.
179bool SystemZDAGToDAGISel::MatchAddress(SDValue N, SystemZRRIAddressMode &AM,
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000180 bool is12Bit, unsigned Depth) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000181 DebugLoc dl = N.getDebugLoc();
182 DOUT << "MatchAddress: "; DEBUG(AM.dump());
183 // Limit recursion.
184 if (Depth > 5)
185 return MatchAddressBase(N, AM);
186
Anton Korobeynikovdc289552009-07-16 13:44:30 +0000187 // FIXME: We can perform better here. If we have something like
188 // (shift (add A, imm), N), we can try to reassociate stuff and fold shift of
189 // imm into addressing mode.
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000190 switch (N.getOpcode()) {
191 default: break;
192 case ISD::Constant: {
Anton Korobeynikov32407402009-07-16 13:48:23 +0000193 int64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
194 int64_t Imm;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000195 bool Match = (is12Bit ?
196 isImmZExt12(AM.Disp + Val, Imm) :
197 isImmSExt20(AM.Disp + Val, Imm));
198 if (Match) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000199 AM.Disp = Imm;
200 return false;
201 }
202 break;
203 }
204
205 case ISD::FrameIndex:
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000206 if (AM.BaseType == SystemZRRIAddressMode::RegBase &&
207 AM.Base.Reg.getNode() == 0) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000208 AM.BaseType = SystemZRRIAddressMode::FrameIndexBase;
209 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
210 return false;
211 }
212 break;
213
214 case ISD::SUB: {
215 // Given A-B, if A can be completely folded into the address and
216 // the index field with the index field unused, use -B as the index.
217 // This is a win if a has multiple parts that can be folded into
218 // the address. Also, this saves a mov if the base register has
219 // other uses, since it avoids a two-address sub instruction, however
220 // it costs an additional mov if the index register has other uses.
221
222 // Test if the LHS of the sub can be folded.
223 SystemZRRIAddressMode Backup = AM;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000224 if (MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1)) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000225 AM = Backup;
226 break;
227 }
228 // Test if the index field is free for use.
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000229 if (AM.IndexReg.getNode() && !AM.isRI) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000230 AM = Backup;
231 break;
232 }
233
234 // If the base is a register with multiple uses, this transformation may
235 // save a mov. Otherwise it's probably better not to do it.
236 if (AM.BaseType == SystemZRRIAddressMode::RegBase &&
237 (!AM.Base.Reg.getNode() || AM.Base.Reg.getNode()->hasOneUse())) {
238 AM = Backup;
239 break;
240 }
241
242 // Ok, the transformation is legal and appears profitable. Go for it.
243 SDValue RHS = N.getNode()->getOperand(1);
244 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
245 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
246 AM.IndexReg = Neg;
247
248 // Insert the new nodes into the topological ordering.
249 if (Zero.getNode()->getNodeId() == -1 ||
250 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
251 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
252 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
253 }
254 if (Neg.getNode()->getNodeId() == -1 ||
255 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
256 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
257 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
258 }
259 return false;
260 }
261
262 case ISD::ADD: {
263 SystemZRRIAddressMode Backup = AM;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000264 if (!MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1) &&
265 !MatchAddress(N.getNode()->getOperand(1), AM, is12Bit, Depth+1))
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000266 return false;
267 AM = Backup;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000268 if (!MatchAddress(N.getNode()->getOperand(1), AM, is12Bit, Depth+1) &&
269 !MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1))
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000270 return false;
271 AM = Backup;
272
273 // If we couldn't fold both operands into the address at the same time,
274 // see if we can just put each operand into a register and fold at least
275 // the add.
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000276 if (!AM.isRI &&
277 AM.BaseType == SystemZRRIAddressMode::RegBase &&
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000278 !AM.Base.Reg.getNode() && !AM.IndexReg.getNode()) {
279 AM.Base.Reg = N.getNode()->getOperand(0);
280 AM.IndexReg = N.getNode()->getOperand(1);
281 return false;
282 }
283 break;
284 }
285
286 case ISD::OR:
287 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
288 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
289 SystemZRRIAddressMode Backup = AM;
Anton Korobeynikov32407402009-07-16 13:48:23 +0000290 int64_t Offset = CN->getSExtValue();
291 int64_t Imm;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000292 bool MatchOffset = (is12Bit ?
293 isImmZExt12(AM.Disp + Offset, Imm) :
294 isImmSExt20(AM.Disp + Offset, Imm));
295 // The resultant disp must fit in 12 or 20-bits.
296 if (MatchOffset &&
297 // LHS should be an addr mode.
298 !MatchAddress(N.getOperand(0), AM, is12Bit, Depth+1) &&
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000299 // Check to see if the LHS & C is zero.
300 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
301 AM.Disp = Imm;
302 return false;
303 }
304 AM = Backup;
305 }
306 break;
307 }
308
309 return MatchAddressBase(N, AM);
310}
311
312/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
313/// specified addressing mode without any further recursion.
314bool SystemZDAGToDAGISel::MatchAddressBase(SDValue N,
315 SystemZRRIAddressMode &AM) {
316 // Is the base register already occupied?
317 if (AM.BaseType != SystemZRRIAddressMode::RegBase || AM.Base.Reg.getNode()) {
Anton Korobeynikov46567602009-07-16 14:10:35 +0000318 // If so, check to see if the index register is set.
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000319 if (AM.IndexReg.getNode() == 0 && !AM.isRI) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000320 AM.IndexReg = N;
321 return false;
322 }
323
324 // Otherwise, we cannot select it.
325 return true;
326 }
327
328 // Default, generate it as a register.
329 AM.BaseType = SystemZRRIAddressMode::RegBase;
330 AM.Base.Reg = N;
331 return false;
332}
333
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000334void SystemZDAGToDAGISel::getAddressOperandsRI(const SystemZRRIAddressMode &AM,
335 SDValue &Base, SDValue &Disp) {
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000336 if (AM.BaseType == SystemZRRIAddressMode::RegBase)
337 Base = AM.Base.Reg;
338 else
339 Base = CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy());
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000340 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i64);
341}
342
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000343void SystemZDAGToDAGISel::getAddressOperands(const SystemZRRIAddressMode &AM,
344 SDValue &Base, SDValue &Disp,
345 SDValue &Index) {
346 getAddressOperandsRI(AM, Base, Disp);
347 Index = AM.IndexReg;
348}
349
350/// Returns true if the address can be represented by a base register plus
351/// an unsigned 12-bit displacement [r+imm].
Anton Korobeynikov014d4632009-07-16 14:13:24 +0000352bool SystemZDAGToDAGISel::SelectAddrRI12Only(SDValue Op, SDValue& Addr,
353 SDValue &Base, SDValue &Disp) {
354 return SelectAddrRI12(Op, Addr, Base, Disp, /*is12BitOnly*/true);
355}
356
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000357bool SystemZDAGToDAGISel::SelectAddrRI12(SDValue Op, SDValue& Addr,
Anton Korobeynikov014d4632009-07-16 14:13:24 +0000358 SDValue &Base, SDValue &Disp,
359 bool is12BitOnly) {
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000360 SystemZRRIAddressMode AM20(/*isRI*/true), AM12(/*isRI*/true);
361 bool Done = false;
362
363 if (!Addr.hasOneUse()) {
364 unsigned Opcode = Addr.getOpcode();
365 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
366 // If we are able to fold N into addressing mode, then we'll allow it even
367 // if N has multiple uses. In general, addressing computation is used as
368 // addresses by all of its uses. But watch out for CopyToReg uses, that
369 // means the address computation is liveout. It will be computed by a LA
370 // so we want to avoid computing the address twice.
371 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
372 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
373 if (UI->getOpcode() == ISD::CopyToReg) {
374 MatchAddressBase(Addr, AM12);
375 Done = true;
376 break;
377 }
378 }
379 }
380 }
381 if (!Done && MatchAddress(Addr, AM12, /* is12Bit */ true))
382 return false;
383
384 // Check, whether we can match stuff using 20-bit displacements
Anton Korobeynikov014d4632009-07-16 14:13:24 +0000385 if (!Done && !is12BitOnly &&
386 !MatchAddress(Addr, AM20, /* is12Bit */ false))
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000387 if (AM12.Disp == 0 && AM20.Disp != 0)
388 return false;
389
390 DOUT << "MatchAddress (final): "; DEBUG(AM12.dump());
391
392 MVT VT = Addr.getValueType();
393 if (AM12.BaseType == SystemZRRIAddressMode::RegBase) {
394 if (!AM12.Base.Reg.getNode())
395 AM12.Base.Reg = CurDAG->getRegister(0, VT);
396 }
397
398 assert(AM12.IndexReg.getNode() == 0 && "Invalid reg-imm address mode!");
399
400 getAddressOperandsRI(AM12, Base, Disp);
401
402 return true;
403}
404
405/// Returns true if the address can be represented by a base register plus
406/// a signed 20-bit displacement [r+imm].
407bool SystemZDAGToDAGISel::SelectAddrRI(SDValue Op, SDValue& Addr,
408 SDValue &Base, SDValue &Disp) {
409 SystemZRRIAddressMode AM(/*isRI*/true);
410 bool Done = false;
411
412 if (!Addr.hasOneUse()) {
413 unsigned Opcode = Addr.getOpcode();
414 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
415 // If we are able to fold N into addressing mode, then we'll allow it even
416 // if N has multiple uses. In general, addressing computation is used as
417 // addresses by all of its uses. But watch out for CopyToReg uses, that
418 // means the address computation is liveout. It will be computed by a LA
419 // so we want to avoid computing the address twice.
420 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
421 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
422 if (UI->getOpcode() == ISD::CopyToReg) {
423 MatchAddressBase(Addr, AM);
424 Done = true;
425 break;
426 }
427 }
428 }
429 }
430 if (!Done && MatchAddress(Addr, AM, /* is12Bit */ false))
431 return false;
432
433 DOUT << "MatchAddress (final): "; DEBUG(AM.dump());
434
435 MVT VT = Addr.getValueType();
436 if (AM.BaseType == SystemZRRIAddressMode::RegBase) {
437 if (!AM.Base.Reg.getNode())
438 AM.Base.Reg = CurDAG->getRegister(0, VT);
439 }
440
441 assert(AM.IndexReg.getNode() == 0 && "Invalid reg-imm address mode!");
442
443 getAddressOperandsRI(AM, Base, Disp);
444
445 return true;
446}
447
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000448/// Returns true if the address can be represented by a base register plus
449/// index register plus an unsigned 12-bit displacement [base + idx + imm].
450bool SystemZDAGToDAGISel::SelectAddrRRI12(SDValue Op, SDValue Addr,
451 SDValue &Base, SDValue &Disp, SDValue &Index) {
Anton Korobeynikov46567602009-07-16 14:10:35 +0000452 SystemZRRIAddressMode AM20, AM12;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000453 bool Done = false;
454
455 if (!Addr.hasOneUse()) {
456 unsigned Opcode = Addr.getOpcode();
457 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
458 // If we are able to fold N into addressing mode, then we'll allow it even
459 // if N has multiple uses. In general, addressing computation is used as
460 // addresses by all of its uses. But watch out for CopyToReg uses, that
461 // means the address computation is liveout. It will be computed by a LA
462 // so we want to avoid computing the address twice.
463 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
464 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
465 if (UI->getOpcode() == ISD::CopyToReg) {
466 MatchAddressBase(Addr, AM12);
467 Done = true;
468 break;
469 }
470 }
471 }
472 }
473 if (!Done && MatchAddress(Addr, AM12, /* is12Bit */ true))
474 return false;
475
476 // Check, whether we can match stuff using 20-bit displacements
477 if (!Done && !MatchAddress(Addr, AM20, /* is12Bit */ false))
478 if (AM12.Disp == 0 && AM20.Disp != 0)
479 return false;
480
481 DOUT << "MatchAddress (final): "; DEBUG(AM12.dump());
482
483 MVT VT = Addr.getValueType();
484 if (AM12.BaseType == SystemZRRIAddressMode::RegBase) {
485 if (!AM12.Base.Reg.getNode())
486 AM12.Base.Reg = CurDAG->getRegister(0, VT);
487 }
488
489 if (!AM12.IndexReg.getNode())
490 AM12.IndexReg = CurDAG->getRegister(0, VT);
491
492 getAddressOperands(AM12, Base, Disp, Index);
493
494 return true;
495}
496
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000497/// Returns true if the address can be represented by a base register plus
498/// index register plus a signed 20-bit displacement [base + idx + imm].
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000499bool SystemZDAGToDAGISel::SelectAddrRRI20(SDValue Op, SDValue Addr,
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000500 SDValue &Base, SDValue &Disp, SDValue &Index) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000501 SystemZRRIAddressMode AM;
502 bool Done = false;
503
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000504 if (!Addr.hasOneUse()) {
505 unsigned Opcode = Addr.getOpcode();
506 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
507 // If we are able to fold N into addressing mode, then we'll allow it even
508 // if N has multiple uses. In general, addressing computation is used as
509 // addresses by all of its uses. But watch out for CopyToReg uses, that
510 // means the address computation is liveout. It will be computed by a LA
511 // so we want to avoid computing the address twice.
512 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
513 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
514 if (UI->getOpcode() == ISD::CopyToReg) {
515 MatchAddressBase(Addr, AM);
516 Done = true;
517 break;
518 }
519 }
520 }
521 }
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000522 if (!Done && MatchAddress(Addr, AM, /* is12Bit */ false))
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000523 return false;
524
Anton Korobeynikov32407402009-07-16 13:48:23 +0000525 DOUT << "MatchAddress (final): "; DEBUG(AM.dump());
526
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000527 MVT VT = Addr.getValueType();
528 if (AM.BaseType == SystemZRRIAddressMode::RegBase) {
529 if (!AM.Base.Reg.getNode())
530 AM.Base.Reg = CurDAG->getRegister(0, VT);
531 }
532
533 if (!AM.IndexReg.getNode())
534 AM.IndexReg = CurDAG->getRegister(0, VT);
535
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000536 getAddressOperands(AM, Base, Disp, Index);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000537
538 return true;
539}
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000540
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000541/// SelectLAAddr - it calls SelectAddr and determines if the maximal addressing
542/// mode it matches can be cost effectively emitted as an LA/LAY instruction.
543bool SystemZDAGToDAGISel::SelectLAAddr(SDValue Op, SDValue Addr,
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000544 SDValue &Base, SDValue &Disp, SDValue &Index) {
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000545 SystemZRRIAddressMode AM;
546
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000547 if (MatchAddress(Addr, AM, false))
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000548 return false;
549
550 MVT VT = Addr.getValueType();
551 unsigned Complexity = 0;
552 if (AM.BaseType == SystemZRRIAddressMode::RegBase)
553 if (AM.Base.Reg.getNode())
554 Complexity = 1;
555 else
556 AM.Base.Reg = CurDAG->getRegister(0, VT);
557 else if (AM.BaseType == SystemZRRIAddressMode::FrameIndexBase)
558 Complexity = 4;
559
560 if (AM.IndexReg.getNode())
561 Complexity += 1;
562 else
563 AM.IndexReg = CurDAG->getRegister(0, VT);
564
565 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
566 Complexity += 1;
567
568 if (Complexity > 2) {
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000569 getAddressOperands(AM, Base, Disp, Index);
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000570 return true;
571 }
572
573 return false;
574}
575
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000576/// InstructionSelect - This callback is invoked by
577/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
578void SystemZDAGToDAGISel::InstructionSelect() {
579 DEBUG(BB->dump());
580
581 // Codegen the basic block.
582#ifndef NDEBUG
583 DOUT << "===== Instruction selection begins:\n";
584 Indent = 0;
585#endif
586 SelectRoot(*CurDAG);
587#ifndef NDEBUG
588 DOUT << "===== Instruction selection ends:\n";
589#endif
590
591 CurDAG->RemoveDeadNodes();
592}
593
594SDNode *SystemZDAGToDAGISel::Select(SDValue Op) {
595 SDNode *Node = Op.getNode();
596 DebugLoc dl = Op.getDebugLoc();
597
598 // Dump information about the Node being selected
599 #ifndef NDEBUG
600 DOUT << std::string(Indent, ' ') << "Selecting: ";
601 DEBUG(Node->dump(CurDAG));
602 DOUT << "\n";
603 Indent += 2;
604 #endif
605
606 // If we have a custom node, we already have selected!
607 if (Node->isMachineOpcode()) {
608 #ifndef NDEBUG
609 DOUT << std::string(Indent-2, ' ') << "== ";
610 DEBUG(Node->dump(CurDAG));
611 DOUT << "\n";
612 Indent -= 2;
613 #endif
614 return NULL;
615 }
616
617 // Select the default instruction
618 SDNode *ResNode = SelectCode(Op);
619
620 #ifndef NDEBUG
621 DOUT << std::string(Indent-2, ' ') << "=> ";
622 if (ResNode == NULL || ResNode == Op.getNode())
623 DEBUG(Op.getNode()->dump(CurDAG));
624 else
625 DEBUG(ResNode->dump(CurDAG));
626 DOUT << "\n";
627 Indent -= 2;
628 #endif
629
630 return ResNode;
631}