blob: 4fccbf256398112b4d0638ace7ed4bd8c77a86c8 [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 Korobeynikov8bd0db72009-07-16 14:18:17 +000033static const unsigned subreg_even32 = 1;
34static const unsigned subreg_odd32 = 2;
35static const unsigned subreg_even = 3;
36static const unsigned subreg_odd = 4;
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +000037
Anton Korobeynikov3360da92009-07-16 13:44:00 +000038namespace {
39 /// SystemZRRIAddressMode - This corresponds to rriaddr, but uses SDValue's
40 /// instead of register numbers for the leaves of the matched tree.
41 struct SystemZRRIAddressMode {
42 enum {
43 RegBase,
44 FrameIndexBase
45 } BaseType;
46
47 struct { // This is really a union, discriminated by BaseType!
48 SDValue Reg;
49 int FrameIndex;
50 } Base;
51
52 SDValue IndexReg;
Anton Korobeynikov32407402009-07-16 13:48:23 +000053 int64_t Disp;
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +000054 bool isRI;
Anton Korobeynikov3360da92009-07-16 13:44:00 +000055
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +000056 SystemZRRIAddressMode(bool RI = false)
57 : BaseType(RegBase), IndexReg(), Disp(0), isRI(RI) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +000058 }
59
60 void dump() {
Anton Korobeynikov961bb6f2009-07-16 13:45:00 +000061 cerr << "SystemZRRIAddressMode " << this << '\n';
Anton Korobeynikov3360da92009-07-16 13:44:00 +000062 if (BaseType == RegBase) {
63 cerr << "Base.Reg ";
64 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
65 else cerr << "nul";
Anton Korobeynikov961bb6f2009-07-16 13:45:00 +000066 cerr << '\n';
Anton Korobeynikov3360da92009-07-16 13:44:00 +000067 } else {
Anton Korobeynikov961bb6f2009-07-16 13:45:00 +000068 cerr << " Base.FrameIndex " << Base.FrameIndex << '\n';
Anton Korobeynikov3360da92009-07-16 13:44:00 +000069 }
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +000070 if (!isRI) {
71 cerr << "IndexReg ";
72 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
73 else cerr << "nul";
74 }
Anton Korobeynikov961bb6f2009-07-16 13:45:00 +000075 cerr << " Disp " << Disp << '\n';
Anton Korobeynikov3360da92009-07-16 13:44:00 +000076 }
77 };
78}
79
Anton Korobeynikov4403b932009-07-16 13:27:25 +000080/// SystemZDAGToDAGISel - SystemZ specific code to select SystemZ machine
81/// instructions for SelectionDAG operations.
82///
83namespace {
84 class SystemZDAGToDAGISel : public SelectionDAGISel {
85 SystemZTargetLowering &Lowering;
86 const SystemZSubtarget &Subtarget;
87
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +000088 void getAddressOperandsRI(const SystemZRRIAddressMode &AM,
89 SDValue &Base, SDValue &Disp);
Anton Korobeynikov720e3b02009-07-16 14:09:35 +000090 void getAddressOperands(const SystemZRRIAddressMode &AM,
91 SDValue &Base, SDValue &Disp,
92 SDValue &Index);
93
Anton Korobeynikov4403b932009-07-16 13:27:25 +000094 public:
95 SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel)
96 : SelectionDAGISel(TM, OptLevel),
97 Lowering(*TM.getTargetLowering()),
98 Subtarget(*TM.getSubtargetImpl()) { }
99
100 virtual void InstructionSelect();
101
102 virtual const char *getPassName() const {
103 return "SystemZ DAG->DAG Pattern Instruction Selection";
104 }
105
Anton Korobeynikovb6831cb2009-07-16 14:26:38 +0000106 /// getI8Imm - Return a target constant with the specified value, of type
107 /// i8.
108 inline SDValue getI8Imm(uint64_t Imm) {
109 return CurDAG->getTargetConstant(Imm, MVT::i8);
110 }
111
Anton Korobeynikov89edcd02009-07-16 13:33:57 +0000112 /// getI16Imm - Return a target constant with the specified value, of type
113 /// i16.
114 inline SDValue getI16Imm(uint64_t Imm) {
115 return CurDAG->getTargetConstant(Imm, MVT::i16);
116 }
117
Anton Korobeynikovda308c92009-07-16 13:34:50 +0000118 /// getI32Imm - Return a target constant with the specified value, of type
119 /// i32.
120 inline SDValue getI32Imm(uint64_t Imm) {
121 return CurDAG->getTargetConstant(Imm, MVT::i32);
122 }
123
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000124 // Include the pieces autogenerated from the target description.
Anton Korobeynikov89edcd02009-07-16 13:33:57 +0000125 #include "SystemZGenDAGISel.inc"
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000126
127 private:
Anton Korobeynikov014d4632009-07-16 14:13:24 +0000128 bool SelectAddrRI12Only(SDValue Op, SDValue& Addr,
129 SDValue &Base, SDValue &Disp);
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000130 bool SelectAddrRI12(SDValue Op, SDValue& Addr,
Anton Korobeynikov014d4632009-07-16 14:13:24 +0000131 SDValue &Base, SDValue &Disp,
132 bool is12BitOnly = false);
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000133 bool SelectAddrRI(SDValue Op, SDValue& Addr,
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000134 SDValue &Base, SDValue &Disp);
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000135 bool SelectAddrRRI12(SDValue Op, SDValue Addr,
136 SDValue &Base, SDValue &Disp, SDValue &Index);
137 bool SelectAddrRRI20(SDValue Op, SDValue Addr,
138 SDValue &Base, SDValue &Disp, SDValue &Index);
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000139 bool SelectLAAddr(SDValue Op, SDValue Addr,
140 SDValue &Base, SDValue &Disp, SDValue &Index);
141
142 SDNode *Select(SDValue Op);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000143
144 bool TryFoldLoad(SDValue P, SDValue N,
145 SDValue &Base, SDValue &Disp, SDValue &Index);
146
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000147 bool MatchAddress(SDValue N, SystemZRRIAddressMode &AM,
148 bool is12Bit, unsigned Depth = 0);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000149 bool MatchAddressBase(SDValue N, SystemZRRIAddressMode &AM);
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000150 bool MatchAddressRI(SDValue N, SystemZRRIAddressMode &AM,
151 bool is12Bit);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000152
153 #ifndef NDEBUG
154 unsigned Indent;
155 #endif
156 };
157} // end anonymous namespace
158
159/// createSystemZISelDag - This pass converts a legalized DAG into a
160/// SystemZ-specific DAG, ready for instruction scheduling.
161///
162FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
163 CodeGenOpt::Level OptLevel) {
164 return new SystemZDAGToDAGISel(TM, OptLevel);
165}
166
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000167/// isImmSExt20 - This method tests to see if the node is either a 32-bit
168/// or 64-bit immediate, and if the value can be accurately represented as a
169/// sign extension from a 20-bit value. If so, this returns true and the
170/// immediate.
Anton Korobeynikov32407402009-07-16 13:48:23 +0000171static bool isImmSExt20(int64_t Val, int64_t &Imm) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000172 if (Val >= -524288 && Val <= 524287) {
Anton Korobeynikov32407402009-07-16 13:48:23 +0000173 Imm = Val;
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000174 return true;
175 }
176 return false;
177}
178
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000179/// isImmZExt12 - This method tests to see if the node is either a 32-bit
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000180/// or 64-bit immediate, and if the value can be accurately represented as a
181/// zero extension from a 12-bit value. If so, this returns true and the
182/// immediate.
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000183static bool isImmZExt12(int64_t Val, int64_t &Imm) {
184 if (Val >= 0 && Val <= 0xFFF) {
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000185 Imm = Val;
186 return true;
187 }
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000188 return false;
189}
190
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000191/// MatchAddress - Add the specified node to the specified addressing mode,
192/// returning true if it cannot be done. This just pattern matches for the
193/// addressing mode.
194bool SystemZDAGToDAGISel::MatchAddress(SDValue N, SystemZRRIAddressMode &AM,
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000195 bool is12Bit, unsigned Depth) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000196 DebugLoc dl = N.getDebugLoc();
197 DOUT << "MatchAddress: "; DEBUG(AM.dump());
198 // Limit recursion.
199 if (Depth > 5)
200 return MatchAddressBase(N, AM);
201
Anton Korobeynikovdc289552009-07-16 13:44:30 +0000202 // FIXME: We can perform better here. If we have something like
203 // (shift (add A, imm), N), we can try to reassociate stuff and fold shift of
204 // imm into addressing mode.
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000205 switch (N.getOpcode()) {
206 default: break;
207 case ISD::Constant: {
Anton Korobeynikov32407402009-07-16 13:48:23 +0000208 int64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
209 int64_t Imm;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000210 bool Match = (is12Bit ?
211 isImmZExt12(AM.Disp + Val, Imm) :
212 isImmSExt20(AM.Disp + Val, Imm));
213 if (Match) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000214 AM.Disp = Imm;
215 return false;
216 }
217 break;
218 }
219
220 case ISD::FrameIndex:
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000221 if (AM.BaseType == SystemZRRIAddressMode::RegBase &&
222 AM.Base.Reg.getNode() == 0) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000223 AM.BaseType = SystemZRRIAddressMode::FrameIndexBase;
224 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
225 return false;
226 }
227 break;
228
229 case ISD::SUB: {
230 // Given A-B, if A can be completely folded into the address and
231 // the index field with the index field unused, use -B as the index.
232 // This is a win if a has multiple parts that can be folded into
233 // the address. Also, this saves a mov if the base register has
234 // other uses, since it avoids a two-address sub instruction, however
235 // it costs an additional mov if the index register has other uses.
236
237 // Test if the LHS of the sub can be folded.
238 SystemZRRIAddressMode Backup = AM;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000239 if (MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1)) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000240 AM = Backup;
241 break;
242 }
243 // Test if the index field is free for use.
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000244 if (AM.IndexReg.getNode() && !AM.isRI) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000245 AM = Backup;
246 break;
247 }
248
249 // If the base is a register with multiple uses, this transformation may
250 // save a mov. Otherwise it's probably better not to do it.
251 if (AM.BaseType == SystemZRRIAddressMode::RegBase &&
252 (!AM.Base.Reg.getNode() || AM.Base.Reg.getNode()->hasOneUse())) {
253 AM = Backup;
254 break;
255 }
256
257 // Ok, the transformation is legal and appears profitable. Go for it.
258 SDValue RHS = N.getNode()->getOperand(1);
259 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
260 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
261 AM.IndexReg = Neg;
262
263 // Insert the new nodes into the topological ordering.
264 if (Zero.getNode()->getNodeId() == -1 ||
265 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
266 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
267 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
268 }
269 if (Neg.getNode()->getNodeId() == -1 ||
270 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
271 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
272 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
273 }
274 return false;
275 }
276
277 case ISD::ADD: {
278 SystemZRRIAddressMode Backup = AM;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000279 if (!MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1) &&
280 !MatchAddress(N.getNode()->getOperand(1), AM, is12Bit, Depth+1))
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000281 return false;
282 AM = Backup;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000283 if (!MatchAddress(N.getNode()->getOperand(1), AM, is12Bit, Depth+1) &&
284 !MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1))
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000285 return false;
286 AM = Backup;
287
288 // If we couldn't fold both operands into the address at the same time,
289 // see if we can just put each operand into a register and fold at least
290 // the add.
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000291 if (!AM.isRI &&
292 AM.BaseType == SystemZRRIAddressMode::RegBase &&
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000293 !AM.Base.Reg.getNode() && !AM.IndexReg.getNode()) {
294 AM.Base.Reg = N.getNode()->getOperand(0);
295 AM.IndexReg = N.getNode()->getOperand(1);
296 return false;
297 }
298 break;
299 }
300
301 case ISD::OR:
302 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
303 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
304 SystemZRRIAddressMode Backup = AM;
Anton Korobeynikov32407402009-07-16 13:48:23 +0000305 int64_t Offset = CN->getSExtValue();
306 int64_t Imm;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000307 bool MatchOffset = (is12Bit ?
308 isImmZExt12(AM.Disp + Offset, Imm) :
309 isImmSExt20(AM.Disp + Offset, Imm));
310 // The resultant disp must fit in 12 or 20-bits.
311 if (MatchOffset &&
312 // LHS should be an addr mode.
313 !MatchAddress(N.getOperand(0), AM, is12Bit, Depth+1) &&
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000314 // Check to see if the LHS & C is zero.
315 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
316 AM.Disp = Imm;
317 return false;
318 }
319 AM = Backup;
320 }
321 break;
322 }
323
324 return MatchAddressBase(N, AM);
325}
326
327/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
328/// specified addressing mode without any further recursion.
329bool SystemZDAGToDAGISel::MatchAddressBase(SDValue N,
330 SystemZRRIAddressMode &AM) {
331 // Is the base register already occupied?
332 if (AM.BaseType != SystemZRRIAddressMode::RegBase || AM.Base.Reg.getNode()) {
Anton Korobeynikov46567602009-07-16 14:10:35 +0000333 // If so, check to see if the index register is set.
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000334 if (AM.IndexReg.getNode() == 0 && !AM.isRI) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000335 AM.IndexReg = N;
336 return false;
337 }
338
339 // Otherwise, we cannot select it.
340 return true;
341 }
342
343 // Default, generate it as a register.
344 AM.BaseType = SystemZRRIAddressMode::RegBase;
345 AM.Base.Reg = N;
346 return false;
347}
348
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000349void SystemZDAGToDAGISel::getAddressOperandsRI(const SystemZRRIAddressMode &AM,
350 SDValue &Base, SDValue &Disp) {
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000351 if (AM.BaseType == SystemZRRIAddressMode::RegBase)
352 Base = AM.Base.Reg;
353 else
354 Base = CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy());
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000355 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i64);
356}
357
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000358void SystemZDAGToDAGISel::getAddressOperands(const SystemZRRIAddressMode &AM,
359 SDValue &Base, SDValue &Disp,
360 SDValue &Index) {
361 getAddressOperandsRI(AM, Base, Disp);
362 Index = AM.IndexReg;
363}
364
365/// Returns true if the address can be represented by a base register plus
366/// an unsigned 12-bit displacement [r+imm].
Anton Korobeynikov014d4632009-07-16 14:13:24 +0000367bool SystemZDAGToDAGISel::SelectAddrRI12Only(SDValue Op, SDValue& Addr,
368 SDValue &Base, SDValue &Disp) {
369 return SelectAddrRI12(Op, Addr, Base, Disp, /*is12BitOnly*/true);
370}
371
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000372bool SystemZDAGToDAGISel::SelectAddrRI12(SDValue Op, SDValue& Addr,
Anton Korobeynikov014d4632009-07-16 14:13:24 +0000373 SDValue &Base, SDValue &Disp,
374 bool is12BitOnly) {
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000375 SystemZRRIAddressMode AM20(/*isRI*/true), AM12(/*isRI*/true);
376 bool Done = false;
377
378 if (!Addr.hasOneUse()) {
379 unsigned Opcode = Addr.getOpcode();
380 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
381 // If we are able to fold N into addressing mode, then we'll allow it even
382 // if N has multiple uses. In general, addressing computation is used as
383 // addresses by all of its uses. But watch out for CopyToReg uses, that
384 // means the address computation is liveout. It will be computed by a LA
385 // so we want to avoid computing the address twice.
386 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
387 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
388 if (UI->getOpcode() == ISD::CopyToReg) {
389 MatchAddressBase(Addr, AM12);
390 Done = true;
391 break;
392 }
393 }
394 }
395 }
396 if (!Done && MatchAddress(Addr, AM12, /* is12Bit */ true))
397 return false;
398
399 // Check, whether we can match stuff using 20-bit displacements
Anton Korobeynikov014d4632009-07-16 14:13:24 +0000400 if (!Done && !is12BitOnly &&
401 !MatchAddress(Addr, AM20, /* is12Bit */ false))
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000402 if (AM12.Disp == 0 && AM20.Disp != 0)
403 return false;
404
405 DOUT << "MatchAddress (final): "; DEBUG(AM12.dump());
406
407 MVT VT = Addr.getValueType();
408 if (AM12.BaseType == SystemZRRIAddressMode::RegBase) {
409 if (!AM12.Base.Reg.getNode())
410 AM12.Base.Reg = CurDAG->getRegister(0, VT);
411 }
412
413 assert(AM12.IndexReg.getNode() == 0 && "Invalid reg-imm address mode!");
414
415 getAddressOperandsRI(AM12, Base, Disp);
416
417 return true;
418}
419
420/// Returns true if the address can be represented by a base register plus
421/// a signed 20-bit displacement [r+imm].
422bool SystemZDAGToDAGISel::SelectAddrRI(SDValue Op, SDValue& Addr,
423 SDValue &Base, SDValue &Disp) {
424 SystemZRRIAddressMode AM(/*isRI*/true);
425 bool Done = false;
426
427 if (!Addr.hasOneUse()) {
428 unsigned Opcode = Addr.getOpcode();
429 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
430 // If we are able to fold N into addressing mode, then we'll allow it even
431 // if N has multiple uses. In general, addressing computation is used as
432 // addresses by all of its uses. But watch out for CopyToReg uses, that
433 // means the address computation is liveout. It will be computed by a LA
434 // so we want to avoid computing the address twice.
435 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
436 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
437 if (UI->getOpcode() == ISD::CopyToReg) {
438 MatchAddressBase(Addr, AM);
439 Done = true;
440 break;
441 }
442 }
443 }
444 }
445 if (!Done && MatchAddress(Addr, AM, /* is12Bit */ false))
446 return false;
447
448 DOUT << "MatchAddress (final): "; DEBUG(AM.dump());
449
450 MVT VT = Addr.getValueType();
451 if (AM.BaseType == SystemZRRIAddressMode::RegBase) {
452 if (!AM.Base.Reg.getNode())
453 AM.Base.Reg = CurDAG->getRegister(0, VT);
454 }
455
456 assert(AM.IndexReg.getNode() == 0 && "Invalid reg-imm address mode!");
457
458 getAddressOperandsRI(AM, Base, Disp);
459
460 return true;
461}
462
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000463/// Returns true if the address can be represented by a base register plus
464/// index register plus an unsigned 12-bit displacement [base + idx + imm].
465bool SystemZDAGToDAGISel::SelectAddrRRI12(SDValue Op, SDValue Addr,
466 SDValue &Base, SDValue &Disp, SDValue &Index) {
Anton Korobeynikov46567602009-07-16 14:10:35 +0000467 SystemZRRIAddressMode AM20, AM12;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000468 bool Done = false;
469
470 if (!Addr.hasOneUse()) {
471 unsigned Opcode = Addr.getOpcode();
472 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
473 // If we are able to fold N into addressing mode, then we'll allow it even
474 // if N has multiple uses. In general, addressing computation is used as
475 // addresses by all of its uses. But watch out for CopyToReg uses, that
476 // means the address computation is liveout. It will be computed by a LA
477 // so we want to avoid computing the address twice.
478 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
479 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
480 if (UI->getOpcode() == ISD::CopyToReg) {
481 MatchAddressBase(Addr, AM12);
482 Done = true;
483 break;
484 }
485 }
486 }
487 }
488 if (!Done && MatchAddress(Addr, AM12, /* is12Bit */ true))
489 return false;
490
491 // Check, whether we can match stuff using 20-bit displacements
492 if (!Done && !MatchAddress(Addr, AM20, /* is12Bit */ false))
493 if (AM12.Disp == 0 && AM20.Disp != 0)
494 return false;
495
496 DOUT << "MatchAddress (final): "; DEBUG(AM12.dump());
497
498 MVT VT = Addr.getValueType();
499 if (AM12.BaseType == SystemZRRIAddressMode::RegBase) {
500 if (!AM12.Base.Reg.getNode())
501 AM12.Base.Reg = CurDAG->getRegister(0, VT);
502 }
503
504 if (!AM12.IndexReg.getNode())
505 AM12.IndexReg = CurDAG->getRegister(0, VT);
506
507 getAddressOperands(AM12, Base, Disp, Index);
508
509 return true;
510}
511
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000512/// Returns true if the address can be represented by a base register plus
513/// index register plus a signed 20-bit displacement [base + idx + imm].
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000514bool SystemZDAGToDAGISel::SelectAddrRRI20(SDValue Op, SDValue Addr,
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000515 SDValue &Base, SDValue &Disp, SDValue &Index) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000516 SystemZRRIAddressMode AM;
517 bool Done = false;
518
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000519 if (!Addr.hasOneUse()) {
520 unsigned Opcode = Addr.getOpcode();
521 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
522 // If we are able to fold N into addressing mode, then we'll allow it even
523 // if N has multiple uses. In general, addressing computation is used as
524 // addresses by all of its uses. But watch out for CopyToReg uses, that
525 // means the address computation is liveout. It will be computed by a LA
526 // so we want to avoid computing the address twice.
527 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
528 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
529 if (UI->getOpcode() == ISD::CopyToReg) {
530 MatchAddressBase(Addr, AM);
531 Done = true;
532 break;
533 }
534 }
535 }
536 }
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000537 if (!Done && MatchAddress(Addr, AM, /* is12Bit */ false))
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000538 return false;
539
Anton Korobeynikov32407402009-07-16 13:48:23 +0000540 DOUT << "MatchAddress (final): "; DEBUG(AM.dump());
541
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000542 MVT VT = Addr.getValueType();
543 if (AM.BaseType == SystemZRRIAddressMode::RegBase) {
544 if (!AM.Base.Reg.getNode())
545 AM.Base.Reg = CurDAG->getRegister(0, VT);
546 }
547
548 if (!AM.IndexReg.getNode())
549 AM.IndexReg = CurDAG->getRegister(0, VT);
550
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000551 getAddressOperands(AM, Base, Disp, Index);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000552
553 return true;
554}
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000555
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000556/// SelectLAAddr - it calls SelectAddr and determines if the maximal addressing
557/// mode it matches can be cost effectively emitted as an LA/LAY instruction.
558bool SystemZDAGToDAGISel::SelectLAAddr(SDValue Op, SDValue Addr,
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000559 SDValue &Base, SDValue &Disp, SDValue &Index) {
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000560 SystemZRRIAddressMode AM;
561
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000562 if (MatchAddress(Addr, AM, false))
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000563 return false;
564
565 MVT VT = Addr.getValueType();
566 unsigned Complexity = 0;
567 if (AM.BaseType == SystemZRRIAddressMode::RegBase)
568 if (AM.Base.Reg.getNode())
569 Complexity = 1;
570 else
571 AM.Base.Reg = CurDAG->getRegister(0, VT);
572 else if (AM.BaseType == SystemZRRIAddressMode::FrameIndexBase)
573 Complexity = 4;
574
575 if (AM.IndexReg.getNode())
576 Complexity += 1;
577 else
578 AM.IndexReg = CurDAG->getRegister(0, VT);
579
580 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
581 Complexity += 1;
582
583 if (Complexity > 2) {
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000584 getAddressOperands(AM, Base, Disp, Index);
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000585 return true;
586 }
587
588 return false;
589}
590
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000591bool SystemZDAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
592 SDValue &Base, SDValue &Disp, SDValue &Index) {
593 if (ISD::isNON_EXTLoad(N.getNode()) &&
594 N.hasOneUse() &&
595 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
596 return SelectAddrRRI20(P, N.getOperand(1), Base, Disp, Index);
597 return false;
598}
599
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000600/// InstructionSelect - This callback is invoked by
601/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
602void SystemZDAGToDAGISel::InstructionSelect() {
603 DEBUG(BB->dump());
604
605 // Codegen the basic block.
606#ifndef NDEBUG
607 DOUT << "===== Instruction selection begins:\n";
608 Indent = 0;
609#endif
610 SelectRoot(*CurDAG);
611#ifndef NDEBUG
612 DOUT << "===== Instruction selection ends:\n";
613#endif
614
615 CurDAG->RemoveDeadNodes();
616}
617
618SDNode *SystemZDAGToDAGISel::Select(SDValue Op) {
619 SDNode *Node = Op.getNode();
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000620 MVT NVT = Node->getValueType(0);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000621 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000622 unsigned Opcode = Node->getOpcode();
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000623
624 // Dump information about the Node being selected
625 #ifndef NDEBUG
626 DOUT << std::string(Indent, ' ') << "Selecting: ";
627 DEBUG(Node->dump(CurDAG));
628 DOUT << "\n";
629 Indent += 2;
630 #endif
631
632 // If we have a custom node, we already have selected!
633 if (Node->isMachineOpcode()) {
634 #ifndef NDEBUG
635 DOUT << std::string(Indent-2, ' ') << "== ";
636 DEBUG(Node->dump(CurDAG));
637 DOUT << "\n";
638 Indent -= 2;
639 #endif
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000640 return NULL; // Already selected.
641 }
642
643 switch (Opcode) {
644 default: break;
645 case ISD::SDIVREM: {
Anton Korobeynikov09e39002009-07-16 14:17:52 +0000646 unsigned Opc, MOpc;
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000647 SDValue N0 = Node->getOperand(0);
648 SDValue N1 = Node->getOperand(1);
649
650 MVT ResVT;
Anton Korobeynikov09e39002009-07-16 14:17:52 +0000651 bool is32Bit = false;
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000652 switch (NVT.getSimpleVT()) {
653 default: assert(0 && "Unsupported VT!");
654 case MVT::i32:
655 Opc = SystemZ::SDIVREM32r; MOpc = SystemZ::SDIVREM32m;
Anton Korobeynikov09e39002009-07-16 14:17:52 +0000656 ResVT = MVT::v2i64;
657 is32Bit = true;
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000658 break;
659 case MVT::i64:
660 Opc = SystemZ::SDIVREM64r; MOpc = SystemZ::SDIVREM64m;
661 ResVT = MVT::v2i64;
662 break;
663 }
664
665 SDValue Tmp0, Tmp1, Tmp2;
666 bool foldedLoad = TryFoldLoad(Op, N1, Tmp0, Tmp1, Tmp2);
667
668 // Prepare the dividend
Anton Korobeynikov09e39002009-07-16 14:17:52 +0000669 SDNode *Dividend;
670 if (is32Bit)
671 Dividend = CurDAG->getTargetNode(SystemZ::MOVSX64rr32, dl, MVT::i64, N0);
672 else
673 Dividend = N0.getNode();
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000674
675 // Insert prepared dividend into suitable 'subreg'
676 SDNode *Tmp = CurDAG->getTargetNode(TargetInstrInfo::IMPLICIT_DEF,
677 dl, ResVT);
678 Dividend =
679 CurDAG->getTargetNode(TargetInstrInfo::INSERT_SUBREG, dl, ResVT,
680 SDValue(Tmp, 0), SDValue(Dividend, 0),
681 CurDAG->getTargetConstant(subreg_odd, MVT::i32));
682
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000683 SDNode *Result;
684 SDValue DivVal = SDValue(Dividend, 0);
685 if (foldedLoad) {
686 SDValue Ops[] = { DivVal, Tmp0, Tmp1, Tmp2, N1.getOperand(0) };
687 Result = CurDAG->getTargetNode(MOpc, dl, ResVT, Ops, array_lengthof(Ops));
688 // Update the chain.
689 ReplaceUses(N1.getValue(1), SDValue(Result, 0));
690 } else {
691 Result = CurDAG->getTargetNode(Opc, dl, ResVT, SDValue(Dividend, 0), N1);
692 }
693
694 // Copy the division (odd subreg) result, if it is needed.
695 if (!Op.getValue(0).use_empty()) {
Anton Korobeynikov8bd0db72009-07-16 14:18:17 +0000696 unsigned SubRegIdx = (is32Bit ? subreg_odd32 : subreg_odd);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000697 SDNode *Div = CurDAG->getTargetNode(TargetInstrInfo::EXTRACT_SUBREG,
Anton Korobeynikov8bd0db72009-07-16 14:18:17 +0000698 dl, NVT,
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000699 SDValue(Result, 0),
Anton Korobeynikov8bd0db72009-07-16 14:18:17 +0000700 CurDAG->getTargetConstant(SubRegIdx,
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000701 MVT::i32));
Anton Korobeynikov8bd0db72009-07-16 14:18:17 +0000702
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000703 ReplaceUses(Op.getValue(0), SDValue(Div, 0));
704 #ifndef NDEBUG
705 DOUT << std::string(Indent-2, ' ') << "=> ";
706 DEBUG(Result->dump(CurDAG));
707 DOUT << "\n";
708 #endif
709 }
710
711 // Copy the remainder (even subreg) result, if it is needed.
712 if (!Op.getValue(1).use_empty()) {
Anton Korobeynikov8bd0db72009-07-16 14:18:17 +0000713 unsigned SubRegIdx = (is32Bit ? subreg_even32 : subreg_even);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000714 SDNode *Rem = CurDAG->getTargetNode(TargetInstrInfo::EXTRACT_SUBREG,
Anton Korobeynikov8bd0db72009-07-16 14:18:17 +0000715 dl, NVT,
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000716 SDValue(Result, 0),
Anton Korobeynikov8bd0db72009-07-16 14:18:17 +0000717 CurDAG->getTargetConstant(SubRegIdx,
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000718 MVT::i32));
Anton Korobeynikov09e39002009-07-16 14:17:52 +0000719
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000720 ReplaceUses(Op.getValue(1), SDValue(Rem, 0));
721 #ifndef NDEBUG
722 DOUT << std::string(Indent-2, ' ') << "=> ";
723 DEBUG(Result->dump(CurDAG));
724 DOUT << "\n";
725 #endif
726 }
727
728#ifndef NDEBUG
729 Indent -= 2;
730#endif
731
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000732 return NULL;
733 }
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000734 case ISD::UDIVREM: {
735 unsigned Opc, MOpc, ClrOpc;
736 SDValue N0 = Node->getOperand(0);
737 SDValue N1 = Node->getOperand(1);
738 MVT ResVT;
739
Anton Korobeynikov8bd0db72009-07-16 14:18:17 +0000740 bool is32Bit = false;
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000741 switch (NVT.getSimpleVT()) {
742 default: assert(0 && "Unsupported VT!");
743 case MVT::i32:
744 Opc = SystemZ::UDIVREM32r; MOpc = SystemZ::UDIVREM32m;
Anton Korobeynikove3a7f7a2009-07-16 14:14:54 +0000745 ClrOpc = SystemZ::MOV64Pr0_even;
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000746 ResVT = MVT::v2i32;
Anton Korobeynikov8bd0db72009-07-16 14:18:17 +0000747 is32Bit = true;
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000748 break;
749 case MVT::i64:
750 Opc = SystemZ::UDIVREM64r; MOpc = SystemZ::UDIVREM64m;
Anton Korobeynikove3a7f7a2009-07-16 14:14:54 +0000751 ClrOpc = SystemZ::MOV128r0_even;
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000752 ResVT = MVT::v2i64;
753 break;
754 }
755
756 SDValue Tmp0, Tmp1, Tmp2;
757 bool foldedLoad = TryFoldLoad(Op, N1, Tmp0, Tmp1, Tmp2);
758
759 // Prepare the dividend
760 SDNode *Dividend = N0.getNode();
761
762 // Insert prepared dividend into suitable 'subreg'
763 SDNode *Tmp = CurDAG->getTargetNode(TargetInstrInfo::IMPLICIT_DEF,
764 dl, ResVT);
Anton Korobeynikov8bd0db72009-07-16 14:18:17 +0000765 {
766 unsigned SubRegIdx = (is32Bit ? subreg_odd32 : subreg_odd);
767 Dividend =
768 CurDAG->getTargetNode(TargetInstrInfo::INSERT_SUBREG, dl, ResVT,
769 SDValue(Tmp, 0), SDValue(Dividend, 0),
770 CurDAG->getTargetConstant(SubRegIdx, MVT::i32));
771 }
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000772
Anton Korobeynikove3a7f7a2009-07-16 14:14:54 +0000773 // Zero out even subreg
774 Dividend = CurDAG->getTargetNode(ClrOpc, dl, ResVT, SDValue(Dividend, 0));
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000775
776 SDValue DivVal = SDValue(Dividend, 0);
777 SDNode *Result;
778 if (foldedLoad) {
779 SDValue Ops[] = { DivVal, Tmp0, Tmp1, Tmp2, N1.getOperand(0) };
780 Result = CurDAG->getTargetNode(MOpc, dl,ResVT,
781 Ops, array_lengthof(Ops));
782 // Update the chain.
783 ReplaceUses(N1.getValue(1), SDValue(Result, 0));
784 } else {
785 Result = CurDAG->getTargetNode(Opc, dl, ResVT, DivVal, N1);
786 }
787
788 // Copy the division (odd subreg) result, if it is needed.
789 if (!Op.getValue(0).use_empty()) {
Anton Korobeynikov8bd0db72009-07-16 14:18:17 +0000790 unsigned SubRegIdx = (is32Bit ? subreg_odd32 : subreg_odd);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000791 SDNode *Div = CurDAG->getTargetNode(TargetInstrInfo::EXTRACT_SUBREG,
792 dl, NVT,
793 SDValue(Result, 0),
Anton Korobeynikov8bd0db72009-07-16 14:18:17 +0000794 CurDAG->getTargetConstant(SubRegIdx,
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000795 MVT::i32));
796 ReplaceUses(Op.getValue(0), SDValue(Div, 0));
797 #ifndef NDEBUG
798 DOUT << std::string(Indent-2, ' ') << "=> ";
799 DEBUG(Result->dump(CurDAG));
800 DOUT << "\n";
801 #endif
802 }
803
804 // Copy the remainder (even subreg) result, if it is needed.
805 if (!Op.getValue(1).use_empty()) {
Anton Korobeynikov8bd0db72009-07-16 14:18:17 +0000806 unsigned SubRegIdx = (is32Bit ? subreg_even32 : subreg_even);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000807 SDNode *Rem = CurDAG->getTargetNode(TargetInstrInfo::EXTRACT_SUBREG,
808 dl, NVT,
809 SDValue(Result, 0),
Anton Korobeynikov8bd0db72009-07-16 14:18:17 +0000810 CurDAG->getTargetConstant(SubRegIdx,
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000811 MVT::i32));
812 ReplaceUses(Op.getValue(1), SDValue(Rem, 0));
813 #ifndef NDEBUG
814 DOUT << std::string(Indent-2, ' ') << "=> ";
815 DEBUG(Result->dump(CurDAG));
816 DOUT << "\n";
817 #endif
818 }
819
820#ifndef NDEBUG
821 Indent -= 2;
822#endif
823
824 return NULL;
825 }
826 }
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000827
828 // Select the default instruction
829 SDNode *ResNode = SelectCode(Op);
830
831 #ifndef NDEBUG
832 DOUT << std::string(Indent-2, ' ') << "=> ";
833 if (ResNode == NULL || ResNode == Op.getNode())
834 DEBUG(Op.getNode()->dump(CurDAG));
835 else
836 DEBUG(ResNode->dump(CurDAG));
837 DOUT << "\n";
838 Indent -= 2;
839 #endif
840
841 return ResNode;
842}