blob: c2b85377ff90e0d6c5ed82b29e769cc9009d71f8 [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;
48 int32_t Disp;
49
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
80 public:
81 SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel)
82 : SelectionDAGISel(TM, OptLevel),
83 Lowering(*TM.getTargetLowering()),
84 Subtarget(*TM.getSubtargetImpl()) { }
85
86 virtual void InstructionSelect();
87
88 virtual const char *getPassName() const {
89 return "SystemZ DAG->DAG Pattern Instruction Selection";
90 }
91
Anton Korobeynikov89edcd02009-07-16 13:33:57 +000092 /// getI16Imm - Return a target constant with the specified value, of type
93 /// i16.
94 inline SDValue getI16Imm(uint64_t Imm) {
95 return CurDAG->getTargetConstant(Imm, MVT::i16);
96 }
97
Anton Korobeynikovda308c92009-07-16 13:34:50 +000098 /// getI32Imm - Return a target constant with the specified value, of type
99 /// i32.
100 inline SDValue getI32Imm(uint64_t Imm) {
101 return CurDAG->getTargetConstant(Imm, MVT::i32);
102 }
103
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000104 // Include the pieces autogenerated from the target description.
Anton Korobeynikov89edcd02009-07-16 13:33:57 +0000105 #include "SystemZGenDAGISel.inc"
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000106
107 private:
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000108 bool SelectAddrRRI(SDValue Op, SDValue Addr,
109 SDValue &Base, SDValue &Index, SDValue &Disp);
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000110 bool SelectLAAddr(SDValue Op, SDValue Addr,
111 SDValue &Base, SDValue &Index, SDValue &Disp);
112
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000113 SDNode *Select(SDValue Op);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000114 bool SelectAddrRI(const SDValue& Op, SDValue& Addr,
115 SDValue &Base, SDValue &Disp);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000116 bool MatchAddress(SDValue N, SystemZRRIAddressMode &AM, unsigned Depth = 0);
117 bool MatchAddressBase(SDValue N, SystemZRRIAddressMode &AM);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000118
119 #ifndef NDEBUG
120 unsigned Indent;
121 #endif
122 };
123} // end anonymous namespace
124
125/// createSystemZISelDag - This pass converts a legalized DAG into a
126/// SystemZ-specific DAG, ready for instruction scheduling.
127///
128FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
129 CodeGenOpt::Level OptLevel) {
130 return new SystemZDAGToDAGISel(TM, OptLevel);
131}
132
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000133/// isImmSExt20 - This method tests to see if the node is either a 32-bit
134/// or 64-bit immediate, and if the value can be accurately represented as a
135/// sign extension from a 20-bit value. If so, this returns true and the
136/// immediate.
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000137static bool isImmSExt20(int64_t Val, int32_t &Imm) {
138 if (Val >= -524288 && Val <= 524287) {
139 Imm = (int32_t)Val;
140 return true;
141 }
142 return false;
143}
144
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000145static bool isImmSExt20(SDNode *N, int32_t &Imm) {
146 if (N->getOpcode() != ISD::Constant)
147 return false;
148
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000149 return isImmSExt20(cast<ConstantSDNode>(N)->getSExtValue(), Imm);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000150}
151
152static bool isImmSExt20(SDValue Op, int32_t &Imm) {
153 return isImmSExt20(Op.getNode(), Imm);
154}
155
156/// Returns true if the address can be represented by a base register plus
157/// a signed 20-bit displacement [r+imm].
158bool SystemZDAGToDAGISel::SelectAddrRI(const SDValue& Op, SDValue& Addr,
159 SDValue &Base, SDValue &Disp) {
160 // FIXME dl should come from parent load or store, not from address
161 DebugLoc dl = Addr.getDebugLoc();
162 MVT VT = Addr.getValueType();
163
164 if (Addr.getOpcode() == ISD::ADD) {
165 int32_t Imm = 0;
166 if (isImmSExt20(Addr.getOperand(1), Imm)) {
167 Disp = CurDAG->getTargetConstant(Imm, MVT::i32);
168 if (FrameIndexSDNode *FI =
169 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
170 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), VT);
171 } else {
172 Base = Addr.getOperand(0);
173 }
174 return true; // [r+i]
175 }
176 } else if (Addr.getOpcode() == ISD::OR) {
177 int32_t Imm = 0;
178 if (isImmSExt20(Addr.getOperand(1), Imm)) {
179 // If this is an or of disjoint bitfields, we can codegen this as an add
180 // (for better address arithmetic) if the LHS and RHS of the OR are
181 // provably disjoint.
182 APInt LHSKnownZero, LHSKnownOne;
183 CurDAG->ComputeMaskedBits(Addr.getOperand(0),
184 APInt::getAllOnesValue(Addr.getOperand(0)
185 .getValueSizeInBits()),
186 LHSKnownZero, LHSKnownOne);
187
188 if ((LHSKnownZero.getZExtValue()|~(uint64_t)Imm) == ~0ULL) {
189 // If all of the bits are known zero on the LHS or RHS, the add won't
190 // carry.
191 Base = Addr.getOperand(0);
192 Disp = CurDAG->getTargetConstant(Imm, MVT::i32);
193 return true;
194 }
195 }
196 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr)) {
197 // Loading from a constant address.
198
199 // If this address fits entirely in a 20-bit sext immediate field, codegen
200 // this as "d(r0)"
201 int32_t Imm;
202 if (isImmSExt20(CN, Imm)) {
203 Disp = CurDAG->getTargetConstant(Imm, MVT::i32);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000204 Base = CurDAG->getRegister(0, VT);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000205 return true;
206 }
207 }
208
209 Disp = CurDAG->getTargetConstant(0, MVT::i32);
210 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Addr))
211 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), VT);
212 else
213 Base = Addr;
214 return true; // [r+0]
215}
216
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000217/// MatchAddress - Add the specified node to the specified addressing mode,
218/// returning true if it cannot be done. This just pattern matches for the
219/// addressing mode.
220bool SystemZDAGToDAGISel::MatchAddress(SDValue N, SystemZRRIAddressMode &AM,
221 unsigned Depth) {
222 DebugLoc dl = N.getDebugLoc();
223 DOUT << "MatchAddress: "; DEBUG(AM.dump());
224 // Limit recursion.
225 if (Depth > 5)
226 return MatchAddressBase(N, AM);
227
Anton Korobeynikovdc289552009-07-16 13:44:30 +0000228 // FIXME: We can perform better here. If we have something like
229 // (shift (add A, imm), N), we can try to reassociate stuff and fold shift of
230 // imm into addressing mode.
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000231 switch (N.getOpcode()) {
232 default: break;
233 case ISD::Constant: {
234 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
235 int32_t Imm;
236 if (isImmSExt20(AM.Disp + Val, Imm)) {
237 AM.Disp = Imm;
238 return false;
239 }
240 break;
241 }
242
243 case ISD::FrameIndex:
244 if (AM.BaseType == SystemZRRIAddressMode::RegBase
245 && AM.Base.Reg.getNode() == 0) {
246 AM.BaseType = SystemZRRIAddressMode::FrameIndexBase;
247 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
248 return false;
249 }
250 break;
251
252 case ISD::SUB: {
253 // Given A-B, if A can be completely folded into the address and
254 // the index field with the index field unused, use -B as the index.
255 // This is a win if a has multiple parts that can be folded into
256 // the address. Also, this saves a mov if the base register has
257 // other uses, since it avoids a two-address sub instruction, however
258 // it costs an additional mov if the index register has other uses.
259
260 // Test if the LHS of the sub can be folded.
261 SystemZRRIAddressMode Backup = AM;
262 if (MatchAddress(N.getNode()->getOperand(0), AM, Depth+1)) {
263 AM = Backup;
264 break;
265 }
266 // Test if the index field is free for use.
267 if (AM.IndexReg.getNode()) {
268 AM = Backup;
269 break;
270 }
271
272 // If the base is a register with multiple uses, this transformation may
273 // save a mov. Otherwise it's probably better not to do it.
274 if (AM.BaseType == SystemZRRIAddressMode::RegBase &&
275 (!AM.Base.Reg.getNode() || AM.Base.Reg.getNode()->hasOneUse())) {
276 AM = Backup;
277 break;
278 }
279
280 // Ok, the transformation is legal and appears profitable. Go for it.
281 SDValue RHS = N.getNode()->getOperand(1);
282 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
283 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
284 AM.IndexReg = Neg;
285
286 // Insert the new nodes into the topological ordering.
287 if (Zero.getNode()->getNodeId() == -1 ||
288 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
289 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
290 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
291 }
292 if (Neg.getNode()->getNodeId() == -1 ||
293 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
294 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
295 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
296 }
297 return false;
298 }
299
300 case ISD::ADD: {
301 SystemZRRIAddressMode Backup = AM;
302 if (!MatchAddress(N.getNode()->getOperand(0), AM, Depth+1) &&
303 !MatchAddress(N.getNode()->getOperand(1), AM, Depth+1))
304 return false;
305 AM = Backup;
306 if (!MatchAddress(N.getNode()->getOperand(1), AM, Depth+1) &&
307 !MatchAddress(N.getNode()->getOperand(0), AM, Depth+1))
308 return false;
309 AM = Backup;
310
311 // If we couldn't fold both operands into the address at the same time,
312 // see if we can just put each operand into a register and fold at least
313 // the add.
314 if (AM.BaseType == SystemZRRIAddressMode::RegBase &&
315 !AM.Base.Reg.getNode() && !AM.IndexReg.getNode()) {
316 AM.Base.Reg = N.getNode()->getOperand(0);
317 AM.IndexReg = N.getNode()->getOperand(1);
318 return false;
319 }
320 break;
321 }
322
323 case ISD::OR:
324 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
325 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
326 SystemZRRIAddressMode Backup = AM;
327 uint64_t Offset = CN->getSExtValue();
328 int32_t Imm;
329 // Start with the LHS as an addr mode.
330 if (!MatchAddress(N.getOperand(0), AM, Depth+1) &&
331 // The resultant disp must fit in 20-bits.
332 isImmSExt20(AM.Disp + Offset, Imm) &&
333 // Check to see if the LHS & C is zero.
334 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
335 AM.Disp = Imm;
336 return false;
337 }
338 AM = Backup;
339 }
340 break;
341 }
342
343 return MatchAddressBase(N, AM);
344}
345
346/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
347/// specified addressing mode without any further recursion.
348bool SystemZDAGToDAGISel::MatchAddressBase(SDValue N,
349 SystemZRRIAddressMode &AM) {
350 // Is the base register already occupied?
351 if (AM.BaseType != SystemZRRIAddressMode::RegBase || AM.Base.Reg.getNode()) {
352 // If so, check to see if the scale index register is set.
353 if (AM.IndexReg.getNode() == 0) {
354 AM.IndexReg = N;
355 return false;
356 }
357
358 // Otherwise, we cannot select it.
359 return true;
360 }
361
362 // Default, generate it as a register.
363 AM.BaseType = SystemZRRIAddressMode::RegBase;
364 AM.Base.Reg = N;
365 return false;
366}
367
368/// Returns true if the address can be represented by a base register plus
369/// index register plus a signed 20-bit displacement [base + idx + imm].
370bool SystemZDAGToDAGISel::SelectAddrRRI(SDValue Op, SDValue Addr,
371 SDValue &Base, SDValue &Index, SDValue &Disp) {
372 SystemZRRIAddressMode AM;
373 bool Done = false;
374
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000375 if (!Addr.hasOneUse()) {
376 unsigned Opcode = Addr.getOpcode();
377 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
378 // If we are able to fold N into addressing mode, then we'll allow it even
379 // if N has multiple uses. In general, addressing computation is used as
380 // addresses by all of its uses. But watch out for CopyToReg uses, that
381 // means the address computation is liveout. It will be computed by a LA
382 // so we want to avoid computing the address twice.
383 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
384 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
385 if (UI->getOpcode() == ISD::CopyToReg) {
386 MatchAddressBase(Addr, AM);
387 Done = true;
388 break;
389 }
390 }
391 }
392 }
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000393 if (!Done && MatchAddress(Addr, AM))
394 return false;
395
396 MVT VT = Addr.getValueType();
397 if (AM.BaseType == SystemZRRIAddressMode::RegBase) {
398 if (!AM.Base.Reg.getNode())
399 AM.Base.Reg = CurDAG->getRegister(0, VT);
400 }
401
402 if (!AM.IndexReg.getNode())
403 AM.IndexReg = CurDAG->getRegister(0, VT);
404
405 if (AM.BaseType == SystemZRRIAddressMode::RegBase)
406 Base = AM.Base.Reg;
407 else
408 Base = CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy());
409 Index = AM.IndexReg;
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000410 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000411
412 return true;
413}
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000414
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000415/// SelectLAAddr - it calls SelectAddr and determines if the maximal addressing
416/// mode it matches can be cost effectively emitted as an LA/LAY instruction.
417bool SystemZDAGToDAGISel::SelectLAAddr(SDValue Op, SDValue Addr,
418 SDValue &Base, SDValue &Index, SDValue &Disp) {
419 SystemZRRIAddressMode AM;
420
421 if (MatchAddress(Addr, AM))
422 return false;
423
424 MVT VT = Addr.getValueType();
425 unsigned Complexity = 0;
426 if (AM.BaseType == SystemZRRIAddressMode::RegBase)
427 if (AM.Base.Reg.getNode())
428 Complexity = 1;
429 else
430 AM.Base.Reg = CurDAG->getRegister(0, VT);
431 else if (AM.BaseType == SystemZRRIAddressMode::FrameIndexBase)
432 Complexity = 4;
433
434 if (AM.IndexReg.getNode())
435 Complexity += 1;
436 else
437 AM.IndexReg = CurDAG->getRegister(0, VT);
438
439 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
440 Complexity += 1;
441
442 if (Complexity > 2) {
443 if (AM.BaseType == SystemZRRIAddressMode::RegBase)
444 Base = AM.Base.Reg;
445 else
446 Base = CurDAG->getTargetFrameIndex(AM.Base.FrameIndex,
447 TLI.getPointerTy());
448 Index = AM.IndexReg;
449 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
450 return true;
451 }
452
453 return false;
454}
455
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000456/// InstructionSelect - This callback is invoked by
457/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
458void SystemZDAGToDAGISel::InstructionSelect() {
459 DEBUG(BB->dump());
460
461 // Codegen the basic block.
462#ifndef NDEBUG
463 DOUT << "===== Instruction selection begins:\n";
464 Indent = 0;
465#endif
466 SelectRoot(*CurDAG);
467#ifndef NDEBUG
468 DOUT << "===== Instruction selection ends:\n";
469#endif
470
471 CurDAG->RemoveDeadNodes();
472}
473
474SDNode *SystemZDAGToDAGISel::Select(SDValue Op) {
475 SDNode *Node = Op.getNode();
476 DebugLoc dl = Op.getDebugLoc();
477
478 // Dump information about the Node being selected
479 #ifndef NDEBUG
480 DOUT << std::string(Indent, ' ') << "Selecting: ";
481 DEBUG(Node->dump(CurDAG));
482 DOUT << "\n";
483 Indent += 2;
484 #endif
485
486 // If we have a custom node, we already have selected!
487 if (Node->isMachineOpcode()) {
488 #ifndef NDEBUG
489 DOUT << std::string(Indent-2, ' ') << "== ";
490 DEBUG(Node->dump(CurDAG));
491 DOUT << "\n";
492 Indent -= 2;
493 #endif
494 return NULL;
495 }
496
497 // Select the default instruction
498 SDNode *ResNode = SelectCode(Op);
499
500 #ifndef NDEBUG
501 DOUT << std::string(Indent-2, ' ') << "=> ";
502 if (ResNode == NULL || ResNode == Op.getNode())
503 DEBUG(Op.getNode()->dump(CurDAG));
504 else
505 DEBUG(ResNode->dump(CurDAG));
506 DOUT << "\n";
507 Indent -= 2;
508 #endif
509
510 return ResNode;
511}