blob: 2bc186b07f99d8b7577e0a8817e14613164dffaf [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 Korobeynikov3360da92009-07-16 13:44:00 +000049
50 SystemZRRIAddressMode()
51 : BaseType(RegBase), IndexReg(), Disp(0) {
52 }
53
54 void dump() {
Anton Korobeynikov961bb6f2009-07-16 13:45:00 +000055 cerr << "SystemZRRIAddressMode " << this << '\n';
Anton Korobeynikov3360da92009-07-16 13:44:00 +000056 if (BaseType == RegBase) {
57 cerr << "Base.Reg ";
58 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
59 else cerr << "nul";
Anton Korobeynikov961bb6f2009-07-16 13:45:00 +000060 cerr << '\n';
Anton Korobeynikov3360da92009-07-16 13:44:00 +000061 } else {
Anton Korobeynikov961bb6f2009-07-16 13:45:00 +000062 cerr << " Base.FrameIndex " << Base.FrameIndex << '\n';
Anton Korobeynikov3360da92009-07-16 13:44:00 +000063 }
64 cerr << "IndexReg ";
65 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
66 else cerr << "nul";
Anton Korobeynikov961bb6f2009-07-16 13:45:00 +000067 cerr << " Disp " << Disp << '\n';
Anton Korobeynikov3360da92009-07-16 13:44:00 +000068 }
69 };
70}
71
Anton Korobeynikov4403b932009-07-16 13:27:25 +000072/// SystemZDAGToDAGISel - SystemZ specific code to select SystemZ machine
73/// instructions for SelectionDAG operations.
74///
75namespace {
76 class SystemZDAGToDAGISel : public SelectionDAGISel {
77 SystemZTargetLowering &Lowering;
78 const SystemZSubtarget &Subtarget;
79
Anton Korobeynikov720e3b02009-07-16 14:09:35 +000080 void getAddressOperands(const SystemZRRIAddressMode &AM,
81 SDValue &Base, SDValue &Disp,
82 SDValue &Index);
83
Anton Korobeynikov4403b932009-07-16 13:27:25 +000084 public:
85 SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel)
86 : SelectionDAGISel(TM, OptLevel),
87 Lowering(*TM.getTargetLowering()),
88 Subtarget(*TM.getSubtargetImpl()) { }
89
90 virtual void InstructionSelect();
91
92 virtual const char *getPassName() const {
93 return "SystemZ DAG->DAG Pattern Instruction Selection";
94 }
95
Anton Korobeynikov89edcd02009-07-16 13:33:57 +000096 /// getI16Imm - Return a target constant with the specified value, of type
97 /// i16.
98 inline SDValue getI16Imm(uint64_t Imm) {
99 return CurDAG->getTargetConstant(Imm, MVT::i16);
100 }
101
Anton Korobeynikovda308c92009-07-16 13:34:50 +0000102 /// getI32Imm - Return a target constant with the specified value, of type
103 /// i32.
104 inline SDValue getI32Imm(uint64_t Imm) {
105 return CurDAG->getTargetConstant(Imm, MVT::i32);
106 }
107
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000108 // Include the pieces autogenerated from the target description.
Anton Korobeynikov89edcd02009-07-16 13:33:57 +0000109 #include "SystemZGenDAGISel.inc"
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000110
111 private:
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000112 bool SelectAddrRI32(const SDValue& Op, SDValue& Addr,
113 SDValue &Base, SDValue &Disp);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000114 bool SelectAddrRI(const SDValue& Op, SDValue& Addr,
115 SDValue &Base, SDValue &Disp);
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000116 bool SelectAddrRRI12(SDValue Op, SDValue Addr,
117 SDValue &Base, SDValue &Disp, SDValue &Index);
118 bool SelectAddrRRI20(SDValue Op, SDValue Addr,
119 SDValue &Base, SDValue &Disp, SDValue &Index);
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000120 bool SelectLAAddr(SDValue Op, SDValue Addr,
121 SDValue &Base, SDValue &Disp, SDValue &Index);
122
123 SDNode *Select(SDValue Op);
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000124 bool MatchAddress(SDValue N, SystemZRRIAddressMode &AM,
125 bool is12Bit, unsigned Depth = 0);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000126 bool MatchAddressBase(SDValue N, SystemZRRIAddressMode &AM);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000127
128 #ifndef NDEBUG
129 unsigned Indent;
130 #endif
131 };
132} // end anonymous namespace
133
134/// createSystemZISelDag - This pass converts a legalized DAG into a
135/// SystemZ-specific DAG, ready for instruction scheduling.
136///
137FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
138 CodeGenOpt::Level OptLevel) {
139 return new SystemZDAGToDAGISel(TM, OptLevel);
140}
141
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000142/// isImmSExt20 - This method tests to see if the node is either a 32-bit
143/// or 64-bit immediate, and if the value can be accurately represented as a
144/// sign extension from a 20-bit value. If so, this returns true and the
145/// immediate.
Anton Korobeynikov32407402009-07-16 13:48:23 +0000146static bool isImmSExt20(int64_t Val, int64_t &Imm) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000147 if (Val >= -524288 && Val <= 524287) {
Anton Korobeynikov32407402009-07-16 13:48:23 +0000148 Imm = Val;
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000149 return true;
150 }
151 return false;
152}
153
Anton Korobeynikov32407402009-07-16 13:48:23 +0000154static bool isImmSExt20(SDNode *N, int64_t &Imm) {
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000155 if (N->getOpcode() != ISD::Constant)
156 return false;
157
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000158 return isImmSExt20(cast<ConstantSDNode>(N)->getSExtValue(), Imm);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000159}
160
Anton Korobeynikov32407402009-07-16 13:48:23 +0000161static bool isImmSExt20(SDValue Op, int64_t &Imm) {
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000162 return isImmSExt20(Op.getNode(), Imm);
163}
164
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000165/// isImmZExt12 - This method tests to see if the node is either a 32-bit
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000166/// or 64-bit immediate, and if the value can be accurately represented as a
167/// zero extension from a 12-bit value. If so, this returns true and the
168/// immediate.
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000169static bool isImmZExt12(int64_t Val, int64_t &Imm) {
170 if (Val >= 0 && Val <= 0xFFF) {
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000171 Imm = Val;
172 return true;
173 }
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000174 return false;
175}
176
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000177static bool isImmZExt12(SDNode *N, int64_t &Imm) {
178 if (N->getOpcode() != ISD::Constant)
179 return false;
180
181 return isImmZExt12(cast<ConstantSDNode>(N)->getSExtValue(), Imm);
182}
183
184static bool isImmZExt12(SDValue Op, int64_t &Imm) {
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000185 return isImmZExt12(Op.getNode(), Imm);
186}
187
188/// Returns true if the address can be represented by a base register plus
189/// an unsigned 12-bit displacement [r+imm].
190bool SystemZDAGToDAGISel::SelectAddrRI32(const SDValue& Op, SDValue& Addr,
191 SDValue &Base, SDValue &Disp) {
192 // FIXME dl should come from parent load or store, not from address
193 DebugLoc dl = Addr.getDebugLoc();
194 MVT VT = Addr.getValueType();
195
196 if (Addr.getOpcode() == ISD::ADD) {
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000197 int64_t Imm = 0;
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000198 if (isImmZExt12(Addr.getOperand(1), Imm)) {
199 Disp = CurDAG->getTargetConstant(Imm, MVT::i64);
200 if (FrameIndexSDNode *FI =
201 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
202 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), VT);
203 } else {
204 Base = Addr.getOperand(0);
205 }
206 return true; // [r+i]
207 }
208 } else if (Addr.getOpcode() == ISD::OR) {
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000209 int64_t Imm = 0;
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000210 if (isImmZExt12(Addr.getOperand(1), Imm)) {
211 // If this is an or of disjoint bitfields, we can codegen this as an add
212 // (for better address arithmetic) if the LHS and RHS of the OR are
213 // provably disjoint.
214 APInt LHSKnownZero, LHSKnownOne;
215 CurDAG->ComputeMaskedBits(Addr.getOperand(0),
216 APInt::getAllOnesValue(Addr.getOperand(0)
217 .getValueSizeInBits()),
218 LHSKnownZero, LHSKnownOne);
219
220 if ((LHSKnownZero.getZExtValue()|~(uint64_t)Imm) == ~0ULL) {
221 // If all of the bits are known zero on the LHS or RHS, the add won't
222 // carry.
223 Base = Addr.getOperand(0);
224 Disp = CurDAG->getTargetConstant(Imm, MVT::i64);
225 return true;
226 }
227 }
228 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr)) {
229 // Loading from a constant address.
230
231 // If this address fits entirely in a 12-bit zext immediate field, codegen
232 // this as "d(r0)"
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000233 int64_t Imm;
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000234 if (isImmZExt12(CN, Imm)) {
235 Disp = CurDAG->getTargetConstant(Imm, MVT::i64);
236 Base = CurDAG->getRegister(0, VT);
237 return true;
238 }
239 }
240
241 Disp = CurDAG->getTargetConstant(0, MVT::i64);
242 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Addr))
243 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), VT);
244 else
245 Base = Addr;
246 return true; // [r+0]
247}
248
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000249/// Returns true if the address can be represented by a base register plus
250/// a signed 20-bit displacement [r+imm].
251bool SystemZDAGToDAGISel::SelectAddrRI(const SDValue& Op, SDValue& Addr,
252 SDValue &Base, SDValue &Disp) {
253 // FIXME dl should come from parent load or store, not from address
254 DebugLoc dl = Addr.getDebugLoc();
255 MVT VT = Addr.getValueType();
256
257 if (Addr.getOpcode() == ISD::ADD) {
Anton Korobeynikov32407402009-07-16 13:48:23 +0000258 int64_t Imm = 0;
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000259 if (isImmSExt20(Addr.getOperand(1), Imm)) {
Anton Korobeynikov32407402009-07-16 13:48:23 +0000260 Disp = CurDAG->getTargetConstant(Imm, MVT::i64);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000261 if (FrameIndexSDNode *FI =
262 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
263 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), VT);
264 } else {
265 Base = Addr.getOperand(0);
266 }
267 return true; // [r+i]
268 }
269 } else if (Addr.getOpcode() == ISD::OR) {
Anton Korobeynikov32407402009-07-16 13:48:23 +0000270 int64_t Imm = 0;
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000271 if (isImmSExt20(Addr.getOperand(1), Imm)) {
272 // If this is an or of disjoint bitfields, we can codegen this as an add
273 // (for better address arithmetic) if the LHS and RHS of the OR are
274 // provably disjoint.
275 APInt LHSKnownZero, LHSKnownOne;
276 CurDAG->ComputeMaskedBits(Addr.getOperand(0),
277 APInt::getAllOnesValue(Addr.getOperand(0)
278 .getValueSizeInBits()),
279 LHSKnownZero, LHSKnownOne);
280
281 if ((LHSKnownZero.getZExtValue()|~(uint64_t)Imm) == ~0ULL) {
282 // If all of the bits are known zero on the LHS or RHS, the add won't
283 // carry.
284 Base = Addr.getOperand(0);
Anton Korobeynikov32407402009-07-16 13:48:23 +0000285 Disp = CurDAG->getTargetConstant(Imm, MVT::i64);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000286 return true;
287 }
288 }
289 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr)) {
290 // Loading from a constant address.
291
292 // If this address fits entirely in a 20-bit sext immediate field, codegen
293 // this as "d(r0)"
Anton Korobeynikov32407402009-07-16 13:48:23 +0000294 int64_t Imm;
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000295 if (isImmSExt20(CN, Imm)) {
Anton Korobeynikov32407402009-07-16 13:48:23 +0000296 Disp = CurDAG->getTargetConstant(Imm, MVT::i64);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000297 Base = CurDAG->getRegister(0, VT);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000298 return true;
299 }
300 }
301
Anton Korobeynikov32407402009-07-16 13:48:23 +0000302 Disp = CurDAG->getTargetConstant(0, MVT::i64);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000303 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Addr))
304 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), VT);
305 else
306 Base = Addr;
307 return true; // [r+0]
308}
309
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000310/// MatchAddress - Add the specified node to the specified addressing mode,
311/// returning true if it cannot be done. This just pattern matches for the
312/// addressing mode.
313bool SystemZDAGToDAGISel::MatchAddress(SDValue N, SystemZRRIAddressMode &AM,
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000314 bool is12Bit, unsigned Depth) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000315 DebugLoc dl = N.getDebugLoc();
316 DOUT << "MatchAddress: "; DEBUG(AM.dump());
317 // Limit recursion.
318 if (Depth > 5)
319 return MatchAddressBase(N, AM);
320
Anton Korobeynikovdc289552009-07-16 13:44:30 +0000321 // FIXME: We can perform better here. If we have something like
322 // (shift (add A, imm), N), we can try to reassociate stuff and fold shift of
323 // imm into addressing mode.
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000324 switch (N.getOpcode()) {
325 default: break;
326 case ISD::Constant: {
Anton Korobeynikov32407402009-07-16 13:48:23 +0000327 int64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
328 int64_t Imm;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000329 bool Match = (is12Bit ?
330 isImmZExt12(AM.Disp + Val, Imm) :
331 isImmSExt20(AM.Disp + Val, Imm));
332 if (Match) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000333 AM.Disp = Imm;
334 return false;
335 }
336 break;
337 }
338
339 case ISD::FrameIndex:
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000340 if (AM.BaseType == SystemZRRIAddressMode::RegBase &&
341 AM.Base.Reg.getNode() == 0) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000342 AM.BaseType = SystemZRRIAddressMode::FrameIndexBase;
343 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
344 return false;
345 }
346 break;
347
348 case ISD::SUB: {
349 // Given A-B, if A can be completely folded into the address and
350 // the index field with the index field unused, use -B as the index.
351 // This is a win if a has multiple parts that can be folded into
352 // the address. Also, this saves a mov if the base register has
353 // other uses, since it avoids a two-address sub instruction, however
354 // it costs an additional mov if the index register has other uses.
355
356 // Test if the LHS of the sub can be folded.
357 SystemZRRIAddressMode Backup = AM;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000358 if (MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1)) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000359 AM = Backup;
360 break;
361 }
362 // Test if the index field is free for use.
363 if (AM.IndexReg.getNode()) {
364 AM = Backup;
365 break;
366 }
367
368 // If the base is a register with multiple uses, this transformation may
369 // save a mov. Otherwise it's probably better not to do it.
370 if (AM.BaseType == SystemZRRIAddressMode::RegBase &&
371 (!AM.Base.Reg.getNode() || AM.Base.Reg.getNode()->hasOneUse())) {
372 AM = Backup;
373 break;
374 }
375
376 // Ok, the transformation is legal and appears profitable. Go for it.
377 SDValue RHS = N.getNode()->getOperand(1);
378 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
379 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
380 AM.IndexReg = Neg;
381
382 // Insert the new nodes into the topological ordering.
383 if (Zero.getNode()->getNodeId() == -1 ||
384 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
385 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
386 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
387 }
388 if (Neg.getNode()->getNodeId() == -1 ||
389 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
390 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
391 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
392 }
393 return false;
394 }
395
396 case ISD::ADD: {
397 SystemZRRIAddressMode Backup = AM;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000398 if (!MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1) &&
399 !MatchAddress(N.getNode()->getOperand(1), AM, is12Bit, Depth+1))
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000400 return false;
401 AM = Backup;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000402 if (!MatchAddress(N.getNode()->getOperand(1), AM, is12Bit, Depth+1) &&
403 !MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1))
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000404 return false;
405 AM = Backup;
406
407 // If we couldn't fold both operands into the address at the same time,
408 // see if we can just put each operand into a register and fold at least
409 // the add.
410 if (AM.BaseType == SystemZRRIAddressMode::RegBase &&
411 !AM.Base.Reg.getNode() && !AM.IndexReg.getNode()) {
412 AM.Base.Reg = N.getNode()->getOperand(0);
413 AM.IndexReg = N.getNode()->getOperand(1);
414 return false;
415 }
416 break;
417 }
418
419 case ISD::OR:
420 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
421 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
422 SystemZRRIAddressMode Backup = AM;
Anton Korobeynikov32407402009-07-16 13:48:23 +0000423 int64_t Offset = CN->getSExtValue();
424 int64_t Imm;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000425 bool MatchOffset = (is12Bit ?
426 isImmZExt12(AM.Disp + Offset, Imm) :
427 isImmSExt20(AM.Disp + Offset, Imm));
428 // The resultant disp must fit in 12 or 20-bits.
429 if (MatchOffset &&
430 // LHS should be an addr mode.
431 !MatchAddress(N.getOperand(0), AM, is12Bit, Depth+1) &&
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000432 // Check to see if the LHS & C is zero.
433 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
434 AM.Disp = Imm;
435 return false;
436 }
437 AM = Backup;
438 }
439 break;
440 }
441
442 return MatchAddressBase(N, AM);
443}
444
445/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
446/// specified addressing mode without any further recursion.
447bool SystemZDAGToDAGISel::MatchAddressBase(SDValue N,
448 SystemZRRIAddressMode &AM) {
449 // Is the base register already occupied?
450 if (AM.BaseType != SystemZRRIAddressMode::RegBase || AM.Base.Reg.getNode()) {
451 // If so, check to see if the scale index register is set.
452 if (AM.IndexReg.getNode() == 0) {
453 AM.IndexReg = N;
454 return false;
455 }
456
457 // Otherwise, we cannot select it.
458 return true;
459 }
460
461 // Default, generate it as a register.
462 AM.BaseType = SystemZRRIAddressMode::RegBase;
463 AM.Base.Reg = N;
464 return false;
465}
466
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000467void SystemZDAGToDAGISel::getAddressOperands(const SystemZRRIAddressMode &AM,
468 SDValue &Base, SDValue &Disp,
469 SDValue &Index) {
470 if (AM.BaseType == SystemZRRIAddressMode::RegBase)
471 Base = AM.Base.Reg;
472 else
473 Base = CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy());
474 Index = AM.IndexReg;
475 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i64);
476}
477
478/// Returns true if the address can be represented by a base register plus
479/// index register plus an unsigned 12-bit displacement [base + idx + imm].
480bool SystemZDAGToDAGISel::SelectAddrRRI12(SDValue Op, SDValue Addr,
481 SDValue &Base, SDValue &Disp, SDValue &Index) {
482 SystemZRRIAddressMode AM20, AM12;
483 bool Done = false;
484
485 if (!Addr.hasOneUse()) {
486 unsigned Opcode = Addr.getOpcode();
487 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
488 // If we are able to fold N into addressing mode, then we'll allow it even
489 // if N has multiple uses. In general, addressing computation is used as
490 // addresses by all of its uses. But watch out for CopyToReg uses, that
491 // means the address computation is liveout. It will be computed by a LA
492 // so we want to avoid computing the address twice.
493 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
494 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
495 if (UI->getOpcode() == ISD::CopyToReg) {
496 MatchAddressBase(Addr, AM12);
497 Done = true;
498 break;
499 }
500 }
501 }
502 }
503 if (!Done && MatchAddress(Addr, AM12, /* is12Bit */ true))
504 return false;
505
506 // Check, whether we can match stuff using 20-bit displacements
507 if (!Done && !MatchAddress(Addr, AM20, /* is12Bit */ false))
508 if (AM12.Disp == 0 && AM20.Disp != 0)
509 return false;
510
511 DOUT << "MatchAddress (final): "; DEBUG(AM12.dump());
512
513 MVT VT = Addr.getValueType();
514 if (AM12.BaseType == SystemZRRIAddressMode::RegBase) {
515 if (!AM12.Base.Reg.getNode())
516 AM12.Base.Reg = CurDAG->getRegister(0, VT);
517 }
518
519 if (!AM12.IndexReg.getNode())
520 AM12.IndexReg = CurDAG->getRegister(0, VT);
521
522 getAddressOperands(AM12, Base, Disp, Index);
523
524 return true;
525}
526
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000527/// Returns true if the address can be represented by a base register plus
528/// index register plus a signed 20-bit displacement [base + idx + imm].
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000529bool SystemZDAGToDAGISel::SelectAddrRRI20(SDValue Op, SDValue Addr,
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000530 SDValue &Base, SDValue &Disp, SDValue &Index) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000531 SystemZRRIAddressMode AM;
532 bool Done = false;
533
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000534 if (!Addr.hasOneUse()) {
535 unsigned Opcode = Addr.getOpcode();
536 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
537 // If we are able to fold N into addressing mode, then we'll allow it even
538 // if N has multiple uses. In general, addressing computation is used as
539 // addresses by all of its uses. But watch out for CopyToReg uses, that
540 // means the address computation is liveout. It will be computed by a LA
541 // so we want to avoid computing the address twice.
542 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
543 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
544 if (UI->getOpcode() == ISD::CopyToReg) {
545 MatchAddressBase(Addr, AM);
546 Done = true;
547 break;
548 }
549 }
550 }
551 }
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000552 if (!Done && MatchAddress(Addr, AM, /* is12Bit */ false))
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000553 return false;
554
Anton Korobeynikov32407402009-07-16 13:48:23 +0000555 DOUT << "MatchAddress (final): "; DEBUG(AM.dump());
556
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000557 MVT VT = Addr.getValueType();
558 if (AM.BaseType == SystemZRRIAddressMode::RegBase) {
559 if (!AM.Base.Reg.getNode())
560 AM.Base.Reg = CurDAG->getRegister(0, VT);
561 }
562
563 if (!AM.IndexReg.getNode())
564 AM.IndexReg = CurDAG->getRegister(0, VT);
565
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000566 getAddressOperands(AM, Base, Disp, Index);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000567
568 return true;
569}
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000570
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000571/// SelectLAAddr - it calls SelectAddr and determines if the maximal addressing
572/// mode it matches can be cost effectively emitted as an LA/LAY instruction.
573bool SystemZDAGToDAGISel::SelectLAAddr(SDValue Op, SDValue Addr,
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000574 SDValue &Base, SDValue &Disp, SDValue &Index) {
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000575 SystemZRRIAddressMode AM;
576
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000577 if (MatchAddress(Addr, AM, false))
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000578 return false;
579
580 MVT VT = Addr.getValueType();
581 unsigned Complexity = 0;
582 if (AM.BaseType == SystemZRRIAddressMode::RegBase)
583 if (AM.Base.Reg.getNode())
584 Complexity = 1;
585 else
586 AM.Base.Reg = CurDAG->getRegister(0, VT);
587 else if (AM.BaseType == SystemZRRIAddressMode::FrameIndexBase)
588 Complexity = 4;
589
590 if (AM.IndexReg.getNode())
591 Complexity += 1;
592 else
593 AM.IndexReg = CurDAG->getRegister(0, VT);
594
595 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
596 Complexity += 1;
597
598 if (Complexity > 2) {
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000599 getAddressOperands(AM, Base, Disp, Index);
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000600 return true;
601 }
602
603 return false;
604}
605
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000606/// InstructionSelect - This callback is invoked by
607/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
608void SystemZDAGToDAGISel::InstructionSelect() {
609 DEBUG(BB->dump());
610
611 // Codegen the basic block.
612#ifndef NDEBUG
613 DOUT << "===== Instruction selection begins:\n";
614 Indent = 0;
615#endif
616 SelectRoot(*CurDAG);
617#ifndef NDEBUG
618 DOUT << "===== Instruction selection ends:\n";
619#endif
620
621 CurDAG->RemoveDeadNodes();
622}
623
624SDNode *SystemZDAGToDAGISel::Select(SDValue Op) {
625 SDNode *Node = Op.getNode();
626 DebugLoc dl = Op.getDebugLoc();
627
628 // Dump information about the Node being selected
629 #ifndef NDEBUG
630 DOUT << std::string(Indent, ' ') << "Selecting: ";
631 DEBUG(Node->dump(CurDAG));
632 DOUT << "\n";
633 Indent += 2;
634 #endif
635
636 // If we have a custom node, we already have selected!
637 if (Node->isMachineOpcode()) {
638 #ifndef NDEBUG
639 DOUT << std::string(Indent-2, ' ') << "== ";
640 DEBUG(Node->dump(CurDAG));
641 DOUT << "\n";
642 Indent -= 2;
643 #endif
644 return NULL;
645 }
646
647 // Select the default instruction
648 SDNode *ResNode = SelectCode(Op);
649
650 #ifndef NDEBUG
651 DOUT << std::string(Indent-2, ' ') << "=> ";
652 if (ResNode == NULL || ResNode == Op.getNode())
653 DEBUG(Op.getNode()->dump(CurDAG));
654 else
655 DEBUG(ResNode->dump(CurDAG));
656 DOUT << "\n";
657 Indent -= 2;
658 #endif
659
660 return ResNode;
661}