blob: e379c6999aa16934d8ab325d6982b0f6b48f09ee [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() {
55 cerr << "SystemZRRIAddressMode " << this << "\n";
56 if (BaseType == RegBase) {
57 cerr << "Base.Reg ";
58 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump();
59 else cerr << "nul";
60 } else {
61 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n";
62 }
63 cerr << "IndexReg ";
64 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
65 else cerr << "nul";
66 cerr << " Disp " << Disp << "\n";
67 }
68 };
69}
70
Anton Korobeynikov4403b932009-07-16 13:27:25 +000071/// SystemZDAGToDAGISel - SystemZ specific code to select SystemZ machine
72/// instructions for SelectionDAG operations.
73///
74namespace {
75 class SystemZDAGToDAGISel : public SelectionDAGISel {
76 SystemZTargetLowering &Lowering;
77 const SystemZSubtarget &Subtarget;
78
79 public:
80 SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel)
81 : SelectionDAGISel(TM, OptLevel),
82 Lowering(*TM.getTargetLowering()),
83 Subtarget(*TM.getSubtargetImpl()) { }
84
85 virtual void InstructionSelect();
86
87 virtual const char *getPassName() const {
88 return "SystemZ DAG->DAG Pattern Instruction Selection";
89 }
90
Anton Korobeynikov89edcd02009-07-16 13:33:57 +000091 /// getI16Imm - Return a target constant with the specified value, of type
92 /// i16.
93 inline SDValue getI16Imm(uint64_t Imm) {
94 return CurDAG->getTargetConstant(Imm, MVT::i16);
95 }
96
Anton Korobeynikovda308c92009-07-16 13:34:50 +000097 /// getI32Imm - Return a target constant with the specified value, of type
98 /// i32.
99 inline SDValue getI32Imm(uint64_t Imm) {
100 return CurDAG->getTargetConstant(Imm, MVT::i32);
101 }
102
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000103 // Include the pieces autogenerated from the target description.
Anton Korobeynikov89edcd02009-07-16 13:33:57 +0000104 #include "SystemZGenDAGISel.inc"
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000105
106 private:
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000107 bool SelectAddrRRI(SDValue Op, SDValue Addr,
108 SDValue &Base, SDValue &Index, SDValue &Disp);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000109 SDNode *Select(SDValue Op);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000110 bool SelectAddrRI(const SDValue& Op, SDValue& Addr,
111 SDValue &Base, SDValue &Disp);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000112 bool MatchAddress(SDValue N, SystemZRRIAddressMode &AM, unsigned Depth = 0);
113 bool MatchAddressBase(SDValue N, SystemZRRIAddressMode &AM);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000114
115 #ifndef NDEBUG
116 unsigned Indent;
117 #endif
118 };
119} // end anonymous namespace
120
121/// createSystemZISelDag - This pass converts a legalized DAG into a
122/// SystemZ-specific DAG, ready for instruction scheduling.
123///
124FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
125 CodeGenOpt::Level OptLevel) {
126 return new SystemZDAGToDAGISel(TM, OptLevel);
127}
128
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000129/// isImmSExt20 - This method tests to see if the node is either a 32-bit
130/// or 64-bit immediate, and if the value can be accurately represented as a
131/// sign extension from a 20-bit value. If so, this returns true and the
132/// immediate.
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000133static bool isImmSExt20(int64_t Val, int32_t &Imm) {
134 if (Val >= -524288 && Val <= 524287) {
135 Imm = (int32_t)Val;
136 return true;
137 }
138 return false;
139}
140
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000141static bool isImmSExt20(SDNode *N, int32_t &Imm) {
142 if (N->getOpcode() != ISD::Constant)
143 return false;
144
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000145 return isImmSExt20(cast<ConstantSDNode>(N)->getSExtValue(), Imm);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000146}
147
148static bool isImmSExt20(SDValue Op, int32_t &Imm) {
149 return isImmSExt20(Op.getNode(), Imm);
150}
151
152/// Returns true if the address can be represented by a base register plus
153/// a signed 20-bit displacement [r+imm].
154bool SystemZDAGToDAGISel::SelectAddrRI(const SDValue& Op, SDValue& Addr,
155 SDValue &Base, SDValue &Disp) {
156 // FIXME dl should come from parent load or store, not from address
157 DebugLoc dl = Addr.getDebugLoc();
158 MVT VT = Addr.getValueType();
159
160 if (Addr.getOpcode() == ISD::ADD) {
161 int32_t Imm = 0;
162 if (isImmSExt20(Addr.getOperand(1), Imm)) {
163 Disp = CurDAG->getTargetConstant(Imm, MVT::i32);
164 if (FrameIndexSDNode *FI =
165 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
166 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), VT);
167 } else {
168 Base = Addr.getOperand(0);
169 }
170 return true; // [r+i]
171 }
172 } else if (Addr.getOpcode() == ISD::OR) {
173 int32_t Imm = 0;
174 if (isImmSExt20(Addr.getOperand(1), Imm)) {
175 // If this is an or of disjoint bitfields, we can codegen this as an add
176 // (for better address arithmetic) if the LHS and RHS of the OR are
177 // provably disjoint.
178 APInt LHSKnownZero, LHSKnownOne;
179 CurDAG->ComputeMaskedBits(Addr.getOperand(0),
180 APInt::getAllOnesValue(Addr.getOperand(0)
181 .getValueSizeInBits()),
182 LHSKnownZero, LHSKnownOne);
183
184 if ((LHSKnownZero.getZExtValue()|~(uint64_t)Imm) == ~0ULL) {
185 // If all of the bits are known zero on the LHS or RHS, the add won't
186 // carry.
187 Base = Addr.getOperand(0);
188 Disp = CurDAG->getTargetConstant(Imm, MVT::i32);
189 return true;
190 }
191 }
192 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr)) {
193 // Loading from a constant address.
194
195 // If this address fits entirely in a 20-bit sext immediate field, codegen
196 // this as "d(r0)"
197 int32_t Imm;
198 if (isImmSExt20(CN, Imm)) {
199 Disp = CurDAG->getTargetConstant(Imm, MVT::i32);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000200 Base = CurDAG->getRegister(0, VT);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000201 return true;
202 }
203 }
204
205 Disp = CurDAG->getTargetConstant(0, MVT::i32);
206 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Addr))
207 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), VT);
208 else
209 Base = Addr;
210 return true; // [r+0]
211}
212
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000213/// MatchAddress - Add the specified node to the specified addressing mode,
214/// returning true if it cannot be done. This just pattern matches for the
215/// addressing mode.
216bool SystemZDAGToDAGISel::MatchAddress(SDValue N, SystemZRRIAddressMode &AM,
217 unsigned Depth) {
218 DebugLoc dl = N.getDebugLoc();
219 DOUT << "MatchAddress: "; DEBUG(AM.dump());
220 // Limit recursion.
221 if (Depth > 5)
222 return MatchAddressBase(N, AM);
223
224 switch (N.getOpcode()) {
225 default: break;
226 case ISD::Constant: {
227 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
228 int32_t Imm;
229 if (isImmSExt20(AM.Disp + Val, Imm)) {
230 AM.Disp = Imm;
231 return false;
232 }
233 break;
234 }
235
236 case ISD::FrameIndex:
237 if (AM.BaseType == SystemZRRIAddressMode::RegBase
238 && AM.Base.Reg.getNode() == 0) {
239 AM.BaseType = SystemZRRIAddressMode::FrameIndexBase;
240 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
241 return false;
242 }
243 break;
244
245 case ISD::SUB: {
246 // Given A-B, if A can be completely folded into the address and
247 // the index field with the index field unused, use -B as the index.
248 // This is a win if a has multiple parts that can be folded into
249 // the address. Also, this saves a mov if the base register has
250 // other uses, since it avoids a two-address sub instruction, however
251 // it costs an additional mov if the index register has other uses.
252
253 // Test if the LHS of the sub can be folded.
254 SystemZRRIAddressMode Backup = AM;
255 if (MatchAddress(N.getNode()->getOperand(0), AM, Depth+1)) {
256 AM = Backup;
257 break;
258 }
259 // Test if the index field is free for use.
260 if (AM.IndexReg.getNode()) {
261 AM = Backup;
262 break;
263 }
264
265 // If the base is a register with multiple uses, this transformation may
266 // save a mov. Otherwise it's probably better not to do it.
267 if (AM.BaseType == SystemZRRIAddressMode::RegBase &&
268 (!AM.Base.Reg.getNode() || AM.Base.Reg.getNode()->hasOneUse())) {
269 AM = Backup;
270 break;
271 }
272
273 // Ok, the transformation is legal and appears profitable. Go for it.
274 SDValue RHS = N.getNode()->getOperand(1);
275 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
276 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
277 AM.IndexReg = Neg;
278
279 // Insert the new nodes into the topological ordering.
280 if (Zero.getNode()->getNodeId() == -1 ||
281 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
282 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
283 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
284 }
285 if (Neg.getNode()->getNodeId() == -1 ||
286 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
287 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
288 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
289 }
290 return false;
291 }
292
293 case ISD::ADD: {
294 SystemZRRIAddressMode Backup = AM;
295 if (!MatchAddress(N.getNode()->getOperand(0), AM, Depth+1) &&
296 !MatchAddress(N.getNode()->getOperand(1), AM, Depth+1))
297 return false;
298 AM = Backup;
299 if (!MatchAddress(N.getNode()->getOperand(1), AM, Depth+1) &&
300 !MatchAddress(N.getNode()->getOperand(0), AM, Depth+1))
301 return false;
302 AM = Backup;
303
304 // If we couldn't fold both operands into the address at the same time,
305 // see if we can just put each operand into a register and fold at least
306 // the add.
307 if (AM.BaseType == SystemZRRIAddressMode::RegBase &&
308 !AM.Base.Reg.getNode() && !AM.IndexReg.getNode()) {
309 AM.Base.Reg = N.getNode()->getOperand(0);
310 AM.IndexReg = N.getNode()->getOperand(1);
311 return false;
312 }
313 break;
314 }
315
316 case ISD::OR:
317 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
318 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
319 SystemZRRIAddressMode Backup = AM;
320 uint64_t Offset = CN->getSExtValue();
321 int32_t Imm;
322 // Start with the LHS as an addr mode.
323 if (!MatchAddress(N.getOperand(0), AM, Depth+1) &&
324 // The resultant disp must fit in 20-bits.
325 isImmSExt20(AM.Disp + Offset, Imm) &&
326 // Check to see if the LHS & C is zero.
327 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
328 AM.Disp = Imm;
329 return false;
330 }
331 AM = Backup;
332 }
333 break;
334 }
335
336 return MatchAddressBase(N, AM);
337}
338
339/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
340/// specified addressing mode without any further recursion.
341bool SystemZDAGToDAGISel::MatchAddressBase(SDValue N,
342 SystemZRRIAddressMode &AM) {
343 // Is the base register already occupied?
344 if (AM.BaseType != SystemZRRIAddressMode::RegBase || AM.Base.Reg.getNode()) {
345 // If so, check to see if the scale index register is set.
346 if (AM.IndexReg.getNode() == 0) {
347 AM.IndexReg = N;
348 return false;
349 }
350
351 // Otherwise, we cannot select it.
352 return true;
353 }
354
355 // Default, generate it as a register.
356 AM.BaseType = SystemZRRIAddressMode::RegBase;
357 AM.Base.Reg = N;
358 return false;
359}
360
361/// Returns true if the address can be represented by a base register plus
362/// index register plus a signed 20-bit displacement [base + idx + imm].
363bool SystemZDAGToDAGISel::SelectAddrRRI(SDValue Op, SDValue Addr,
364 SDValue &Base, SDValue &Index, SDValue &Disp) {
365 SystemZRRIAddressMode AM;
366 bool Done = false;
367
368 // FIXME: Should we better use lay instruction for non-single uses?
369
370 if (!Done && MatchAddress(Addr, AM))
371 return false;
372
373 MVT VT = Addr.getValueType();
374 if (AM.BaseType == SystemZRRIAddressMode::RegBase) {
375 if (!AM.Base.Reg.getNode())
376 AM.Base.Reg = CurDAG->getRegister(0, VT);
377 }
378
379 if (!AM.IndexReg.getNode())
380 AM.IndexReg = CurDAG->getRegister(0, VT);
381
382 if (AM.BaseType == SystemZRRIAddressMode::RegBase)
383 Base = AM.Base.Reg;
384 else
385 Base = CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy());
386 Index = AM.IndexReg;
387 Disp = Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
388
389 return true;
390}
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000391
392/// InstructionSelect - This callback is invoked by
393/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
394void SystemZDAGToDAGISel::InstructionSelect() {
395 DEBUG(BB->dump());
396
397 // Codegen the basic block.
398#ifndef NDEBUG
399 DOUT << "===== Instruction selection begins:\n";
400 Indent = 0;
401#endif
402 SelectRoot(*CurDAG);
403#ifndef NDEBUG
404 DOUT << "===== Instruction selection ends:\n";
405#endif
406
407 CurDAG->RemoveDeadNodes();
408}
409
410SDNode *SystemZDAGToDAGISel::Select(SDValue Op) {
411 SDNode *Node = Op.getNode();
412 DebugLoc dl = Op.getDebugLoc();
413
414 // Dump information about the Node being selected
415 #ifndef NDEBUG
416 DOUT << std::string(Indent, ' ') << "Selecting: ";
417 DEBUG(Node->dump(CurDAG));
418 DOUT << "\n";
419 Indent += 2;
420 #endif
421
422 // If we have a custom node, we already have selected!
423 if (Node->isMachineOpcode()) {
424 #ifndef NDEBUG
425 DOUT << std::string(Indent-2, ' ') << "== ";
426 DEBUG(Node->dump(CurDAG));
427 DOUT << "\n";
428 Indent -= 2;
429 #endif
430 return NULL;
431 }
432
433 // Select the default instruction
434 SDNode *ResNode = SelectCode(Op);
435
436 #ifndef NDEBUG
437 DOUT << std::string(Indent-2, ' ') << "=> ";
438 if (ResNode == NULL || ResNode == Op.getNode())
439 DEBUG(Op.getNode()->dump(CurDAG));
440 else
441 DEBUG(ResNode->dump(CurDAG));
442 DOUT << "\n";
443 Indent -= 2;
444 #endif
445
446 return ResNode;
447}