blob: 8ca936f07dfd8902f51091cb55d28117ea4bf2a1 [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 Korobeynikov0a42d2b2009-07-16 14:14:33 +000033static const unsigned subreg_32bit = 1;
34static const unsigned subreg_even = 1;
35static const unsigned subreg_odd = 2;
36
Anton Korobeynikov3360da92009-07-16 13:44:00 +000037namespace {
38 /// SystemZRRIAddressMode - This corresponds to rriaddr, but uses SDValue's
39 /// instead of register numbers for the leaves of the matched tree.
40 struct SystemZRRIAddressMode {
41 enum {
42 RegBase,
43 FrameIndexBase
44 } BaseType;
45
46 struct { // This is really a union, discriminated by BaseType!
47 SDValue Reg;
48 int FrameIndex;
49 } Base;
50
51 SDValue IndexReg;
Anton Korobeynikov32407402009-07-16 13:48:23 +000052 int64_t Disp;
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +000053 bool isRI;
Anton Korobeynikov3360da92009-07-16 13:44:00 +000054
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +000055 SystemZRRIAddressMode(bool RI = false)
56 : BaseType(RegBase), IndexReg(), Disp(0), isRI(RI) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +000057 }
58
59 void dump() {
Anton Korobeynikov961bb6f2009-07-16 13:45:00 +000060 cerr << "SystemZRRIAddressMode " << this << '\n';
Anton Korobeynikov3360da92009-07-16 13:44:00 +000061 if (BaseType == RegBase) {
62 cerr << "Base.Reg ";
63 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
64 else cerr << "nul";
Anton Korobeynikov961bb6f2009-07-16 13:45:00 +000065 cerr << '\n';
Anton Korobeynikov3360da92009-07-16 13:44:00 +000066 } else {
Anton Korobeynikov961bb6f2009-07-16 13:45:00 +000067 cerr << " Base.FrameIndex " << Base.FrameIndex << '\n';
Anton Korobeynikov3360da92009-07-16 13:44:00 +000068 }
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +000069 if (!isRI) {
70 cerr << "IndexReg ";
71 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
72 else cerr << "nul";
73 }
Anton Korobeynikov961bb6f2009-07-16 13:45:00 +000074 cerr << " Disp " << Disp << '\n';
Anton Korobeynikov3360da92009-07-16 13:44:00 +000075 }
76 };
77}
78
Anton Korobeynikov4403b932009-07-16 13:27:25 +000079/// SystemZDAGToDAGISel - SystemZ specific code to select SystemZ machine
80/// instructions for SelectionDAG operations.
81///
82namespace {
83 class SystemZDAGToDAGISel : public SelectionDAGISel {
84 SystemZTargetLowering &Lowering;
85 const SystemZSubtarget &Subtarget;
86
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +000087 void getAddressOperandsRI(const SystemZRRIAddressMode &AM,
88 SDValue &Base, SDValue &Disp);
Anton Korobeynikov720e3b02009-07-16 14:09:35 +000089 void getAddressOperands(const SystemZRRIAddressMode &AM,
90 SDValue &Base, SDValue &Disp,
91 SDValue &Index);
92
Anton Korobeynikov4403b932009-07-16 13:27:25 +000093 public:
94 SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel)
95 : SelectionDAGISel(TM, OptLevel),
96 Lowering(*TM.getTargetLowering()),
97 Subtarget(*TM.getSubtargetImpl()) { }
98
99 virtual void InstructionSelect();
100
101 virtual const char *getPassName() const {
102 return "SystemZ DAG->DAG Pattern Instruction Selection";
103 }
104
Anton Korobeynikov89edcd02009-07-16 13:33:57 +0000105 /// getI16Imm - Return a target constant with the specified value, of type
106 /// i16.
107 inline SDValue getI16Imm(uint64_t Imm) {
108 return CurDAG->getTargetConstant(Imm, MVT::i16);
109 }
110
Anton Korobeynikovda308c92009-07-16 13:34:50 +0000111 /// getI32Imm - Return a target constant with the specified value, of type
112 /// i32.
113 inline SDValue getI32Imm(uint64_t Imm) {
114 return CurDAG->getTargetConstant(Imm, MVT::i32);
115 }
116
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000117 // Include the pieces autogenerated from the target description.
Anton Korobeynikov89edcd02009-07-16 13:33:57 +0000118 #include "SystemZGenDAGISel.inc"
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000119
120 private:
Anton Korobeynikov014d4632009-07-16 14:13:24 +0000121 bool SelectAddrRI12Only(SDValue Op, SDValue& Addr,
122 SDValue &Base, SDValue &Disp);
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000123 bool SelectAddrRI12(SDValue Op, SDValue& Addr,
Anton Korobeynikov014d4632009-07-16 14:13:24 +0000124 SDValue &Base, SDValue &Disp,
125 bool is12BitOnly = false);
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000126 bool SelectAddrRI(SDValue Op, SDValue& Addr,
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000127 SDValue &Base, SDValue &Disp);
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000128 bool SelectAddrRRI12(SDValue Op, SDValue Addr,
129 SDValue &Base, SDValue &Disp, SDValue &Index);
130 bool SelectAddrRRI20(SDValue Op, SDValue Addr,
131 SDValue &Base, SDValue &Disp, SDValue &Index);
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000132 bool SelectLAAddr(SDValue Op, SDValue Addr,
133 SDValue &Base, SDValue &Disp, SDValue &Index);
134
135 SDNode *Select(SDValue Op);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000136
137 bool TryFoldLoad(SDValue P, SDValue N,
138 SDValue &Base, SDValue &Disp, SDValue &Index);
139
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000140 bool MatchAddress(SDValue N, SystemZRRIAddressMode &AM,
141 bool is12Bit, unsigned Depth = 0);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000142 bool MatchAddressBase(SDValue N, SystemZRRIAddressMode &AM);
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000143 bool MatchAddressRI(SDValue N, SystemZRRIAddressMode &AM,
144 bool is12Bit);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000145
146 #ifndef NDEBUG
147 unsigned Indent;
148 #endif
149 };
150} // end anonymous namespace
151
152/// createSystemZISelDag - This pass converts a legalized DAG into a
153/// SystemZ-specific DAG, ready for instruction scheduling.
154///
155FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
156 CodeGenOpt::Level OptLevel) {
157 return new SystemZDAGToDAGISel(TM, OptLevel);
158}
159
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000160/// isImmSExt20 - This method tests to see if the node is either a 32-bit
161/// or 64-bit immediate, and if the value can be accurately represented as a
162/// sign extension from a 20-bit value. If so, this returns true and the
163/// immediate.
Anton Korobeynikov32407402009-07-16 13:48:23 +0000164static bool isImmSExt20(int64_t Val, int64_t &Imm) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000165 if (Val >= -524288 && Val <= 524287) {
Anton Korobeynikov32407402009-07-16 13:48:23 +0000166 Imm = Val;
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000167 return true;
168 }
169 return false;
170}
171
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000172/// isImmZExt12 - This method tests to see if the node is either a 32-bit
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000173/// or 64-bit immediate, and if the value can be accurately represented as a
174/// zero extension from a 12-bit value. If so, this returns true and the
175/// immediate.
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000176static bool isImmZExt12(int64_t Val, int64_t &Imm) {
177 if (Val >= 0 && Val <= 0xFFF) {
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000178 Imm = Val;
179 return true;
180 }
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000181 return false;
182}
183
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000184/// MatchAddress - Add the specified node to the specified addressing mode,
185/// returning true if it cannot be done. This just pattern matches for the
186/// addressing mode.
187bool SystemZDAGToDAGISel::MatchAddress(SDValue N, SystemZRRIAddressMode &AM,
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000188 bool is12Bit, unsigned Depth) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000189 DebugLoc dl = N.getDebugLoc();
190 DOUT << "MatchAddress: "; DEBUG(AM.dump());
191 // Limit recursion.
192 if (Depth > 5)
193 return MatchAddressBase(N, AM);
194
Anton Korobeynikovdc289552009-07-16 13:44:30 +0000195 // FIXME: We can perform better here. If we have something like
196 // (shift (add A, imm), N), we can try to reassociate stuff and fold shift of
197 // imm into addressing mode.
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000198 switch (N.getOpcode()) {
199 default: break;
200 case ISD::Constant: {
Anton Korobeynikov32407402009-07-16 13:48:23 +0000201 int64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
202 int64_t Imm;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000203 bool Match = (is12Bit ?
204 isImmZExt12(AM.Disp + Val, Imm) :
205 isImmSExt20(AM.Disp + Val, Imm));
206 if (Match) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000207 AM.Disp = Imm;
208 return false;
209 }
210 break;
211 }
212
213 case ISD::FrameIndex:
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000214 if (AM.BaseType == SystemZRRIAddressMode::RegBase &&
215 AM.Base.Reg.getNode() == 0) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000216 AM.BaseType = SystemZRRIAddressMode::FrameIndexBase;
217 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
218 return false;
219 }
220 break;
221
222 case ISD::SUB: {
223 // Given A-B, if A can be completely folded into the address and
224 // the index field with the index field unused, use -B as the index.
225 // This is a win if a has multiple parts that can be folded into
226 // the address. Also, this saves a mov if the base register has
227 // other uses, since it avoids a two-address sub instruction, however
228 // it costs an additional mov if the index register has other uses.
229
230 // Test if the LHS of the sub can be folded.
231 SystemZRRIAddressMode Backup = AM;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000232 if (MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1)) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000233 AM = Backup;
234 break;
235 }
236 // Test if the index field is free for use.
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000237 if (AM.IndexReg.getNode() && !AM.isRI) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000238 AM = Backup;
239 break;
240 }
241
242 // If the base is a register with multiple uses, this transformation may
243 // save a mov. Otherwise it's probably better not to do it.
244 if (AM.BaseType == SystemZRRIAddressMode::RegBase &&
245 (!AM.Base.Reg.getNode() || AM.Base.Reg.getNode()->hasOneUse())) {
246 AM = Backup;
247 break;
248 }
249
250 // Ok, the transformation is legal and appears profitable. Go for it.
251 SDValue RHS = N.getNode()->getOperand(1);
252 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
253 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
254 AM.IndexReg = Neg;
255
256 // Insert the new nodes into the topological ordering.
257 if (Zero.getNode()->getNodeId() == -1 ||
258 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
259 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
260 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
261 }
262 if (Neg.getNode()->getNodeId() == -1 ||
263 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
264 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
265 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
266 }
267 return false;
268 }
269
270 case ISD::ADD: {
271 SystemZRRIAddressMode Backup = AM;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000272 if (!MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1) &&
273 !MatchAddress(N.getNode()->getOperand(1), AM, is12Bit, Depth+1))
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000274 return false;
275 AM = Backup;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000276 if (!MatchAddress(N.getNode()->getOperand(1), AM, is12Bit, Depth+1) &&
277 !MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1))
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000278 return false;
279 AM = Backup;
280
281 // If we couldn't fold both operands into the address at the same time,
282 // see if we can just put each operand into a register and fold at least
283 // the add.
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000284 if (!AM.isRI &&
285 AM.BaseType == SystemZRRIAddressMode::RegBase &&
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000286 !AM.Base.Reg.getNode() && !AM.IndexReg.getNode()) {
287 AM.Base.Reg = N.getNode()->getOperand(0);
288 AM.IndexReg = N.getNode()->getOperand(1);
289 return false;
290 }
291 break;
292 }
293
294 case ISD::OR:
295 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
296 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
297 SystemZRRIAddressMode Backup = AM;
Anton Korobeynikov32407402009-07-16 13:48:23 +0000298 int64_t Offset = CN->getSExtValue();
299 int64_t Imm;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000300 bool MatchOffset = (is12Bit ?
301 isImmZExt12(AM.Disp + Offset, Imm) :
302 isImmSExt20(AM.Disp + Offset, Imm));
303 // The resultant disp must fit in 12 or 20-bits.
304 if (MatchOffset &&
305 // LHS should be an addr mode.
306 !MatchAddress(N.getOperand(0), AM, is12Bit, Depth+1) &&
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000307 // Check to see if the LHS & C is zero.
308 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
309 AM.Disp = Imm;
310 return false;
311 }
312 AM = Backup;
313 }
314 break;
315 }
316
317 return MatchAddressBase(N, AM);
318}
319
320/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
321/// specified addressing mode without any further recursion.
322bool SystemZDAGToDAGISel::MatchAddressBase(SDValue N,
323 SystemZRRIAddressMode &AM) {
324 // Is the base register already occupied?
325 if (AM.BaseType != SystemZRRIAddressMode::RegBase || AM.Base.Reg.getNode()) {
Anton Korobeynikov46567602009-07-16 14:10:35 +0000326 // If so, check to see if the index register is set.
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000327 if (AM.IndexReg.getNode() == 0 && !AM.isRI) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000328 AM.IndexReg = N;
329 return false;
330 }
331
332 // Otherwise, we cannot select it.
333 return true;
334 }
335
336 // Default, generate it as a register.
337 AM.BaseType = SystemZRRIAddressMode::RegBase;
338 AM.Base.Reg = N;
339 return false;
340}
341
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000342void SystemZDAGToDAGISel::getAddressOperandsRI(const SystemZRRIAddressMode &AM,
343 SDValue &Base, SDValue &Disp) {
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000344 if (AM.BaseType == SystemZRRIAddressMode::RegBase)
345 Base = AM.Base.Reg;
346 else
347 Base = CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy());
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000348 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i64);
349}
350
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000351void SystemZDAGToDAGISel::getAddressOperands(const SystemZRRIAddressMode &AM,
352 SDValue &Base, SDValue &Disp,
353 SDValue &Index) {
354 getAddressOperandsRI(AM, Base, Disp);
355 Index = AM.IndexReg;
356}
357
358/// Returns true if the address can be represented by a base register plus
359/// an unsigned 12-bit displacement [r+imm].
Anton Korobeynikov014d4632009-07-16 14:13:24 +0000360bool SystemZDAGToDAGISel::SelectAddrRI12Only(SDValue Op, SDValue& Addr,
361 SDValue &Base, SDValue &Disp) {
362 return SelectAddrRI12(Op, Addr, Base, Disp, /*is12BitOnly*/true);
363}
364
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000365bool SystemZDAGToDAGISel::SelectAddrRI12(SDValue Op, SDValue& Addr,
Anton Korobeynikov014d4632009-07-16 14:13:24 +0000366 SDValue &Base, SDValue &Disp,
367 bool is12BitOnly) {
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000368 SystemZRRIAddressMode AM20(/*isRI*/true), AM12(/*isRI*/true);
369 bool Done = false;
370
371 if (!Addr.hasOneUse()) {
372 unsigned Opcode = Addr.getOpcode();
373 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
374 // If we are able to fold N into addressing mode, then we'll allow it even
375 // if N has multiple uses. In general, addressing computation is used as
376 // addresses by all of its uses. But watch out for CopyToReg uses, that
377 // means the address computation is liveout. It will be computed by a LA
378 // so we want to avoid computing the address twice.
379 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
380 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
381 if (UI->getOpcode() == ISD::CopyToReg) {
382 MatchAddressBase(Addr, AM12);
383 Done = true;
384 break;
385 }
386 }
387 }
388 }
389 if (!Done && MatchAddress(Addr, AM12, /* is12Bit */ true))
390 return false;
391
392 // Check, whether we can match stuff using 20-bit displacements
Anton Korobeynikov014d4632009-07-16 14:13:24 +0000393 if (!Done && !is12BitOnly &&
394 !MatchAddress(Addr, AM20, /* is12Bit */ false))
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000395 if (AM12.Disp == 0 && AM20.Disp != 0)
396 return false;
397
398 DOUT << "MatchAddress (final): "; DEBUG(AM12.dump());
399
400 MVT VT = Addr.getValueType();
401 if (AM12.BaseType == SystemZRRIAddressMode::RegBase) {
402 if (!AM12.Base.Reg.getNode())
403 AM12.Base.Reg = CurDAG->getRegister(0, VT);
404 }
405
406 assert(AM12.IndexReg.getNode() == 0 && "Invalid reg-imm address mode!");
407
408 getAddressOperandsRI(AM12, Base, Disp);
409
410 return true;
411}
412
413/// Returns true if the address can be represented by a base register plus
414/// a signed 20-bit displacement [r+imm].
415bool SystemZDAGToDAGISel::SelectAddrRI(SDValue Op, SDValue& Addr,
416 SDValue &Base, SDValue &Disp) {
417 SystemZRRIAddressMode AM(/*isRI*/true);
418 bool Done = false;
419
420 if (!Addr.hasOneUse()) {
421 unsigned Opcode = Addr.getOpcode();
422 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
423 // If we are able to fold N into addressing mode, then we'll allow it even
424 // if N has multiple uses. In general, addressing computation is used as
425 // addresses by all of its uses. But watch out for CopyToReg uses, that
426 // means the address computation is liveout. It will be computed by a LA
427 // so we want to avoid computing the address twice.
428 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
429 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
430 if (UI->getOpcode() == ISD::CopyToReg) {
431 MatchAddressBase(Addr, AM);
432 Done = true;
433 break;
434 }
435 }
436 }
437 }
438 if (!Done && MatchAddress(Addr, AM, /* is12Bit */ false))
439 return false;
440
441 DOUT << "MatchAddress (final): "; DEBUG(AM.dump());
442
443 MVT VT = Addr.getValueType();
444 if (AM.BaseType == SystemZRRIAddressMode::RegBase) {
445 if (!AM.Base.Reg.getNode())
446 AM.Base.Reg = CurDAG->getRegister(0, VT);
447 }
448
449 assert(AM.IndexReg.getNode() == 0 && "Invalid reg-imm address mode!");
450
451 getAddressOperandsRI(AM, Base, Disp);
452
453 return true;
454}
455
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000456/// Returns true if the address can be represented by a base register plus
457/// index register plus an unsigned 12-bit displacement [base + idx + imm].
458bool SystemZDAGToDAGISel::SelectAddrRRI12(SDValue Op, SDValue Addr,
459 SDValue &Base, SDValue &Disp, SDValue &Index) {
Anton Korobeynikov46567602009-07-16 14:10:35 +0000460 SystemZRRIAddressMode AM20, AM12;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000461 bool Done = false;
462
463 if (!Addr.hasOneUse()) {
464 unsigned Opcode = Addr.getOpcode();
465 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
466 // If we are able to fold N into addressing mode, then we'll allow it even
467 // if N has multiple uses. In general, addressing computation is used as
468 // addresses by all of its uses. But watch out for CopyToReg uses, that
469 // means the address computation is liveout. It will be computed by a LA
470 // so we want to avoid computing the address twice.
471 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
472 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
473 if (UI->getOpcode() == ISD::CopyToReg) {
474 MatchAddressBase(Addr, AM12);
475 Done = true;
476 break;
477 }
478 }
479 }
480 }
481 if (!Done && MatchAddress(Addr, AM12, /* is12Bit */ true))
482 return false;
483
484 // Check, whether we can match stuff using 20-bit displacements
485 if (!Done && !MatchAddress(Addr, AM20, /* is12Bit */ false))
486 if (AM12.Disp == 0 && AM20.Disp != 0)
487 return false;
488
489 DOUT << "MatchAddress (final): "; DEBUG(AM12.dump());
490
491 MVT VT = Addr.getValueType();
492 if (AM12.BaseType == SystemZRRIAddressMode::RegBase) {
493 if (!AM12.Base.Reg.getNode())
494 AM12.Base.Reg = CurDAG->getRegister(0, VT);
495 }
496
497 if (!AM12.IndexReg.getNode())
498 AM12.IndexReg = CurDAG->getRegister(0, VT);
499
500 getAddressOperands(AM12, Base, Disp, Index);
501
502 return true;
503}
504
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000505/// Returns true if the address can be represented by a base register plus
506/// index register plus a signed 20-bit displacement [base + idx + imm].
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000507bool SystemZDAGToDAGISel::SelectAddrRRI20(SDValue Op, SDValue Addr,
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000508 SDValue &Base, SDValue &Disp, SDValue &Index) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000509 SystemZRRIAddressMode AM;
510 bool Done = false;
511
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000512 if (!Addr.hasOneUse()) {
513 unsigned Opcode = Addr.getOpcode();
514 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
515 // If we are able to fold N into addressing mode, then we'll allow it even
516 // if N has multiple uses. In general, addressing computation is used as
517 // addresses by all of its uses. But watch out for CopyToReg uses, that
518 // means the address computation is liveout. It will be computed by a LA
519 // so we want to avoid computing the address twice.
520 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
521 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
522 if (UI->getOpcode() == ISD::CopyToReg) {
523 MatchAddressBase(Addr, AM);
524 Done = true;
525 break;
526 }
527 }
528 }
529 }
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000530 if (!Done && MatchAddress(Addr, AM, /* is12Bit */ false))
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000531 return false;
532
Anton Korobeynikov32407402009-07-16 13:48:23 +0000533 DOUT << "MatchAddress (final): "; DEBUG(AM.dump());
534
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000535 MVT VT = Addr.getValueType();
536 if (AM.BaseType == SystemZRRIAddressMode::RegBase) {
537 if (!AM.Base.Reg.getNode())
538 AM.Base.Reg = CurDAG->getRegister(0, VT);
539 }
540
541 if (!AM.IndexReg.getNode())
542 AM.IndexReg = CurDAG->getRegister(0, VT);
543
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000544 getAddressOperands(AM, Base, Disp, Index);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000545
546 return true;
547}
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000548
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000549/// SelectLAAddr - it calls SelectAddr and determines if the maximal addressing
550/// mode it matches can be cost effectively emitted as an LA/LAY instruction.
551bool SystemZDAGToDAGISel::SelectLAAddr(SDValue Op, SDValue Addr,
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000552 SDValue &Base, SDValue &Disp, SDValue &Index) {
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000553 SystemZRRIAddressMode AM;
554
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000555 if (MatchAddress(Addr, AM, false))
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000556 return false;
557
558 MVT VT = Addr.getValueType();
559 unsigned Complexity = 0;
560 if (AM.BaseType == SystemZRRIAddressMode::RegBase)
561 if (AM.Base.Reg.getNode())
562 Complexity = 1;
563 else
564 AM.Base.Reg = CurDAG->getRegister(0, VT);
565 else if (AM.BaseType == SystemZRRIAddressMode::FrameIndexBase)
566 Complexity = 4;
567
568 if (AM.IndexReg.getNode())
569 Complexity += 1;
570 else
571 AM.IndexReg = CurDAG->getRegister(0, VT);
572
573 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
574 Complexity += 1;
575
576 if (Complexity > 2) {
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000577 getAddressOperands(AM, Base, Disp, Index);
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000578 return true;
579 }
580
581 return false;
582}
583
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000584bool SystemZDAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
585 SDValue &Base, SDValue &Disp, SDValue &Index) {
586 if (ISD::isNON_EXTLoad(N.getNode()) &&
587 N.hasOneUse() &&
588 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
589 return SelectAddrRRI20(P, N.getOperand(1), Base, Disp, Index);
590 return false;
591}
592
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000593/// InstructionSelect - This callback is invoked by
594/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
595void SystemZDAGToDAGISel::InstructionSelect() {
596 DEBUG(BB->dump());
597
598 // Codegen the basic block.
599#ifndef NDEBUG
600 DOUT << "===== Instruction selection begins:\n";
601 Indent = 0;
602#endif
603 SelectRoot(*CurDAG);
604#ifndef NDEBUG
605 DOUT << "===== Instruction selection ends:\n";
606#endif
607
608 CurDAG->RemoveDeadNodes();
609}
610
611SDNode *SystemZDAGToDAGISel::Select(SDValue Op) {
612 SDNode *Node = Op.getNode();
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000613 MVT NVT = Node->getValueType(0);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000614 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000615 unsigned Opcode = Node->getOpcode();
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000616
617 // Dump information about the Node being selected
618 #ifndef NDEBUG
619 DOUT << std::string(Indent, ' ') << "Selecting: ";
620 DEBUG(Node->dump(CurDAG));
621 DOUT << "\n";
622 Indent += 2;
623 #endif
624
625 // If we have a custom node, we already have selected!
626 if (Node->isMachineOpcode()) {
627 #ifndef NDEBUG
628 DOUT << std::string(Indent-2, ' ') << "== ";
629 DEBUG(Node->dump(CurDAG));
630 DOUT << "\n";
631 Indent -= 2;
632 #endif
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000633 return NULL; // Already selected.
634 }
635
636 switch (Opcode) {
637 default: break;
638 case ISD::SDIVREM: {
Anton Korobeynikov09e39002009-07-16 14:17:52 +0000639 unsigned Opc, MOpc;
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000640 SDValue N0 = Node->getOperand(0);
641 SDValue N1 = Node->getOperand(1);
642
643 MVT ResVT;
Anton Korobeynikov09e39002009-07-16 14:17:52 +0000644 bool is32Bit = false;
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000645 switch (NVT.getSimpleVT()) {
646 default: assert(0 && "Unsupported VT!");
647 case MVT::i32:
648 Opc = SystemZ::SDIVREM32r; MOpc = SystemZ::SDIVREM32m;
Anton Korobeynikov09e39002009-07-16 14:17:52 +0000649 ResVT = MVT::v2i64;
650 is32Bit = true;
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000651 break;
652 case MVT::i64:
653 Opc = SystemZ::SDIVREM64r; MOpc = SystemZ::SDIVREM64m;
654 ResVT = MVT::v2i64;
655 break;
656 }
657
658 SDValue Tmp0, Tmp1, Tmp2;
659 bool foldedLoad = TryFoldLoad(Op, N1, Tmp0, Tmp1, Tmp2);
660
661 // Prepare the dividend
Anton Korobeynikov09e39002009-07-16 14:17:52 +0000662 SDNode *Dividend;
663 if (is32Bit)
664 Dividend = CurDAG->getTargetNode(SystemZ::MOVSX64rr32, dl, MVT::i64, N0);
665 else
666 Dividend = N0.getNode();
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000667
668 // Insert prepared dividend into suitable 'subreg'
669 SDNode *Tmp = CurDAG->getTargetNode(TargetInstrInfo::IMPLICIT_DEF,
670 dl, ResVT);
671 Dividend =
672 CurDAG->getTargetNode(TargetInstrInfo::INSERT_SUBREG, dl, ResVT,
673 SDValue(Tmp, 0), SDValue(Dividend, 0),
674 CurDAG->getTargetConstant(subreg_odd, MVT::i32));
675
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000676 SDNode *Result;
677 SDValue DivVal = SDValue(Dividend, 0);
678 if (foldedLoad) {
679 SDValue Ops[] = { DivVal, Tmp0, Tmp1, Tmp2, N1.getOperand(0) };
680 Result = CurDAG->getTargetNode(MOpc, dl, ResVT, Ops, array_lengthof(Ops));
681 // Update the chain.
682 ReplaceUses(N1.getValue(1), SDValue(Result, 0));
683 } else {
684 Result = CurDAG->getTargetNode(Opc, dl, ResVT, SDValue(Dividend, 0), N1);
685 }
686
687 // Copy the division (odd subreg) result, if it is needed.
688 if (!Op.getValue(0).use_empty()) {
689 SDNode *Div = CurDAG->getTargetNode(TargetInstrInfo::EXTRACT_SUBREG,
Anton Korobeynikov09e39002009-07-16 14:17:52 +0000690 dl, (is32Bit ? MVT::v2i32 : MVT::i64),
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000691 SDValue(Result, 0),
692 CurDAG->getTargetConstant(subreg_odd,
693 MVT::i32));
Anton Korobeynikov09e39002009-07-16 14:17:52 +0000694 if (is32Bit)
695 Div = CurDAG->getTargetNode(TargetInstrInfo::EXTRACT_SUBREG,
696 dl, NVT,
697 SDValue(Div, 0),
698 CurDAG->getTargetConstant(subreg_32bit,
699 MVT::i32));
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000700 ReplaceUses(Op.getValue(0), SDValue(Div, 0));
701 #ifndef NDEBUG
702 DOUT << std::string(Indent-2, ' ') << "=> ";
703 DEBUG(Result->dump(CurDAG));
704 DOUT << "\n";
705 #endif
706 }
707
708 // Copy the remainder (even subreg) result, if it is needed.
709 if (!Op.getValue(1).use_empty()) {
710 SDNode *Rem = CurDAG->getTargetNode(TargetInstrInfo::EXTRACT_SUBREG,
Anton Korobeynikov09e39002009-07-16 14:17:52 +0000711 dl, (is32Bit ? MVT::v2i32 : MVT::i64),
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000712 SDValue(Result, 0),
713 CurDAG->getTargetConstant(subreg_even,
714 MVT::i32));
Anton Korobeynikov09e39002009-07-16 14:17:52 +0000715 if (is32Bit)
716 Rem = CurDAG->getTargetNode(TargetInstrInfo::EXTRACT_SUBREG,
717 dl, NVT,
718 SDValue(Rem, 0),
719 CurDAG->getTargetConstant(subreg_32bit,
720 MVT::i32));
721
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000722 ReplaceUses(Op.getValue(1), SDValue(Rem, 0));
723 #ifndef NDEBUG
724 DOUT << std::string(Indent-2, ' ') << "=> ";
725 DEBUG(Result->dump(CurDAG));
726 DOUT << "\n";
727 #endif
728 }
729
730#ifndef NDEBUG
731 Indent -= 2;
732#endif
733
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000734 return NULL;
735 }
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000736 case ISD::UDIVREM: {
737 unsigned Opc, MOpc, ClrOpc;
738 SDValue N0 = Node->getOperand(0);
739 SDValue N1 = Node->getOperand(1);
740 MVT ResVT;
741
742 switch (NVT.getSimpleVT()) {
743 default: assert(0 && "Unsupported VT!");
744 case MVT::i32:
745 Opc = SystemZ::UDIVREM32r; MOpc = SystemZ::UDIVREM32m;
Anton Korobeynikove3a7f7a2009-07-16 14:14:54 +0000746 ClrOpc = SystemZ::MOV64Pr0_even;
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000747 ResVT = MVT::v2i32;
748 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);
765 Dividend =
766 CurDAG->getTargetNode(TargetInstrInfo::INSERT_SUBREG, dl, ResVT,
767 SDValue(Tmp, 0), SDValue(Dividend, 0),
768 CurDAG->getTargetConstant(subreg_odd, MVT::i32));
769
Anton Korobeynikove3a7f7a2009-07-16 14:14:54 +0000770 // Zero out even subreg
771 Dividend = CurDAG->getTargetNode(ClrOpc, dl, ResVT, SDValue(Dividend, 0));
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000772
773 SDValue DivVal = SDValue(Dividend, 0);
774 SDNode *Result;
775 if (foldedLoad) {
776 SDValue Ops[] = { DivVal, Tmp0, Tmp1, Tmp2, N1.getOperand(0) };
777 Result = CurDAG->getTargetNode(MOpc, dl,ResVT,
778 Ops, array_lengthof(Ops));
779 // Update the chain.
780 ReplaceUses(N1.getValue(1), SDValue(Result, 0));
781 } else {
782 Result = CurDAG->getTargetNode(Opc, dl, ResVT, DivVal, N1);
783 }
784
785 // Copy the division (odd subreg) result, if it is needed.
786 if (!Op.getValue(0).use_empty()) {
787 SDNode *Div = CurDAG->getTargetNode(TargetInstrInfo::EXTRACT_SUBREG,
788 dl, NVT,
789 SDValue(Result, 0),
790 CurDAG->getTargetConstant(subreg_odd,
791 MVT::i32));
792 ReplaceUses(Op.getValue(0), SDValue(Div, 0));
793 #ifndef NDEBUG
794 DOUT << std::string(Indent-2, ' ') << "=> ";
795 DEBUG(Result->dump(CurDAG));
796 DOUT << "\n";
797 #endif
798 }
799
800 // Copy the remainder (even subreg) result, if it is needed.
801 if (!Op.getValue(1).use_empty()) {
802 SDNode *Rem = CurDAG->getTargetNode(TargetInstrInfo::EXTRACT_SUBREG,
803 dl, NVT,
804 SDValue(Result, 0),
805 CurDAG->getTargetConstant(subreg_even,
806 MVT::i32));
807 ReplaceUses(Op.getValue(1), SDValue(Rem, 0));
808 #ifndef NDEBUG
809 DOUT << std::string(Indent-2, ' ') << "=> ";
810 DEBUG(Result->dump(CurDAG));
811 DOUT << "\n";
812 #endif
813 }
814
815#ifndef NDEBUG
816 Indent -= 2;
817#endif
818
819 return NULL;
820 }
821 }
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000822
823 // Select the default instruction
824 SDNode *ResNode = SelectCode(Op);
825
826 #ifndef NDEBUG
827 DOUT << std::string(Indent-2, ' ') << "=> ";
828 if (ResNode == NULL || ResNode == Op.getNode())
829 DEBUG(Op.getNode()->dump(CurDAG));
830 else
831 DEBUG(ResNode->dump(CurDAG));
832 DOUT << "\n";
833 Indent -= 2;
834 #endif
835
836 return ResNode;
837}