blob: ed290ca7ed95eda9525f2f58fe23356342ed03ac [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"
Anton Korobeynikov4403b932009-07-16 13:27:25 +000015#include "SystemZTargetMachine.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/Function.h"
18#include "llvm/Intrinsics.h"
19#include "llvm/CallingConv.h"
20#include "llvm/Constants.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
27#include "llvm/Target/TargetLowering.h"
28#include "llvm/Support/Compiler.h"
29#include "llvm/Support/Debug.h"
Anton Korobeynikov7df84622009-07-16 14:36:52 +000030#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov4403b932009-07-16 13:27:25 +000031using namespace llvm;
32
Anton Korobeynikov3360da92009-07-16 13:44:00 +000033namespace {
34 /// SystemZRRIAddressMode - This corresponds to rriaddr, but uses SDValue's
35 /// instead of register numbers for the leaves of the matched tree.
36 struct SystemZRRIAddressMode {
37 enum {
38 RegBase,
39 FrameIndexBase
40 } BaseType;
41
42 struct { // This is really a union, discriminated by BaseType!
43 SDValue Reg;
44 int FrameIndex;
45 } Base;
46
47 SDValue IndexReg;
Anton Korobeynikov32407402009-07-16 13:48:23 +000048 int64_t Disp;
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +000049 bool isRI;
Anton Korobeynikov3360da92009-07-16 13:44:00 +000050
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +000051 SystemZRRIAddressMode(bool RI = false)
52 : BaseType(RegBase), IndexReg(), Disp(0), isRI(RI) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +000053 }
54
55 void dump() {
Chris Lattner4437ae22009-08-23 07:05:07 +000056 errs() << "SystemZRRIAddressMode " << this << '\n';
Anton Korobeynikov3360da92009-07-16 13:44:00 +000057 if (BaseType == RegBase) {
Chris Lattner4437ae22009-08-23 07:05:07 +000058 errs() << "Base.Reg ";
59 if (Base.Reg.getNode() != 0)
60 Base.Reg.getNode()->dump();
61 else
62 errs() << "nul";
63 errs() << '\n';
Anton Korobeynikov3360da92009-07-16 13:44:00 +000064 } else {
Chris Lattner4437ae22009-08-23 07:05:07 +000065 errs() << " Base.FrameIndex " << Base.FrameIndex << '\n';
Anton Korobeynikov3360da92009-07-16 13:44:00 +000066 }
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +000067 if (!isRI) {
Chris Lattner4437ae22009-08-23 07:05:07 +000068 errs() << "IndexReg ";
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +000069 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
Chris Lattner4437ae22009-08-23 07:05:07 +000070 else errs() << "nul";
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +000071 }
Chris Lattner4437ae22009-08-23 07:05:07 +000072 errs() << " Disp " << Disp << '\n';
Anton Korobeynikov3360da92009-07-16 13:44:00 +000073 }
74 };
75}
76
Anton Korobeynikov4403b932009-07-16 13:27:25 +000077/// SystemZDAGToDAGISel - SystemZ specific code to select SystemZ machine
78/// instructions for SelectionDAG operations.
79///
80namespace {
81 class SystemZDAGToDAGISel : public SelectionDAGISel {
Dan Gohmand858e902010-04-17 15:26:15 +000082 const SystemZTargetLowering &Lowering;
Anton Korobeynikov4403b932009-07-16 13:27:25 +000083 const SystemZSubtarget &Subtarget;
84
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +000085 void getAddressOperandsRI(const SystemZRRIAddressMode &AM,
86 SDValue &Base, SDValue &Disp);
Anton Korobeynikov720e3b02009-07-16 14:09:35 +000087 void getAddressOperands(const SystemZRRIAddressMode &AM,
88 SDValue &Base, SDValue &Disp,
89 SDValue &Index);
90
Anton Korobeynikov4403b932009-07-16 13:27:25 +000091 public:
92 SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel)
93 : SelectionDAGISel(TM, OptLevel),
94 Lowering(*TM.getTargetLowering()),
95 Subtarget(*TM.getSubtargetImpl()) { }
96
Anton Korobeynikov4403b932009-07-16 13:27:25 +000097 virtual const char *getPassName() const {
98 return "SystemZ DAG->DAG Pattern Instruction Selection";
99 }
100
Anton Korobeynikovb6831cb2009-07-16 14:26:38 +0000101 /// getI8Imm - Return a target constant with the specified value, of type
102 /// i8.
103 inline SDValue getI8Imm(uint64_t Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000104 return CurDAG->getTargetConstant(Imm, MVT::i8);
Anton Korobeynikovb6831cb2009-07-16 14:26:38 +0000105 }
106
Anton Korobeynikov89edcd02009-07-16 13:33:57 +0000107 /// getI16Imm - Return a target constant with the specified value, of type
108 /// i16.
109 inline SDValue getI16Imm(uint64_t Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000110 return CurDAG->getTargetConstant(Imm, MVT::i16);
Anton Korobeynikov89edcd02009-07-16 13:33:57 +0000111 }
112
Anton Korobeynikovda308c92009-07-16 13:34:50 +0000113 /// getI32Imm - Return a target constant with the specified value, of type
114 /// i32.
115 inline SDValue getI32Imm(uint64_t Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000116 return CurDAG->getTargetConstant(Imm, MVT::i32);
Anton Korobeynikovda308c92009-07-16 13:34:50 +0000117 }
118
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000119 // Include the pieces autogenerated from the target description.
Anton Korobeynikov89edcd02009-07-16 13:33:57 +0000120 #include "SystemZGenDAGISel.inc"
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000121
122 private:
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000123 bool SelectAddrRI12Only(SDNode *Op, SDValue& Addr,
Anton Korobeynikov014d4632009-07-16 14:13:24 +0000124 SDValue &Base, SDValue &Disp);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000125 bool SelectAddrRI12(SDNode *Op, SDValue& Addr,
Anton Korobeynikov014d4632009-07-16 14:13:24 +0000126 SDValue &Base, SDValue &Disp,
127 bool is12BitOnly = false);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000128 bool SelectAddrRI(SDNode *Op, SDValue& Addr,
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000129 SDValue &Base, SDValue &Disp);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000130 bool SelectAddrRRI12(SDNode *Op, SDValue Addr,
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000131 SDValue &Base, SDValue &Disp, SDValue &Index);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000132 bool SelectAddrRRI20(SDNode *Op, SDValue Addr,
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000133 SDValue &Base, SDValue &Disp, SDValue &Index);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000134 bool SelectLAAddr(SDNode *Op, SDValue Addr,
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000135 SDValue &Base, SDValue &Disp, SDValue &Index);
136
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000137 SDNode *Select(SDNode *Node);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000138
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000139 bool TryFoldLoad(SDNode *P, SDValue N,
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000140 SDValue &Base, SDValue &Disp, SDValue &Index);
141
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000142 bool MatchAddress(SDValue N, SystemZRRIAddressMode &AM,
143 bool is12Bit, unsigned Depth = 0);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000144 bool MatchAddressBase(SDValue N, SystemZRRIAddressMode &AM);
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000145 bool MatchAddressRI(SDValue N, SystemZRRIAddressMode &AM,
146 bool is12Bit);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000147 };
148} // end anonymous namespace
149
150/// createSystemZISelDag - This pass converts a legalized DAG into a
151/// SystemZ-specific DAG, ready for instruction scheduling.
152///
153FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
154 CodeGenOpt::Level OptLevel) {
155 return new SystemZDAGToDAGISel(TM, OptLevel);
156}
157
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000158/// isImmSExt20 - This method tests to see if the node is either a 32-bit
159/// or 64-bit immediate, and if the value can be accurately represented as a
160/// sign extension from a 20-bit value. If so, this returns true and the
161/// immediate.
Anton Korobeynikov32407402009-07-16 13:48:23 +0000162static bool isImmSExt20(int64_t Val, int64_t &Imm) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000163 if (Val >= -524288 && Val <= 524287) {
Anton Korobeynikov32407402009-07-16 13:48:23 +0000164 Imm = Val;
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000165 return true;
166 }
167 return false;
168}
169
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000170/// isImmZExt12 - This method tests to see if the node is either a 32-bit
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000171/// or 64-bit immediate, and if the value can be accurately represented as a
172/// zero extension from a 12-bit value. If so, this returns true and the
173/// immediate.
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000174static bool isImmZExt12(int64_t Val, int64_t &Imm) {
175 if (Val >= 0 && Val <= 0xFFF) {
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000176 Imm = Val;
177 return true;
178 }
Anton Korobeynikov3166a9a2009-07-16 14:03:41 +0000179 return false;
180}
181
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000182/// MatchAddress - Add the specified node to the specified addressing mode,
183/// returning true if it cannot be done. This just pattern matches for the
184/// addressing mode.
185bool SystemZDAGToDAGISel::MatchAddress(SDValue N, SystemZRRIAddressMode &AM,
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000186 bool is12Bit, unsigned Depth) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000187 DebugLoc dl = N.getDebugLoc();
Chris Lattner893e1c92009-08-23 06:49:22 +0000188 DEBUG(errs() << "MatchAddress: "; AM.dump());
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000189 // Limit recursion.
190 if (Depth > 5)
191 return MatchAddressBase(N, AM);
192
Anton Korobeynikovdc289552009-07-16 13:44:30 +0000193 // FIXME: We can perform better here. If we have something like
194 // (shift (add A, imm), N), we can try to reassociate stuff and fold shift of
195 // imm into addressing mode.
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000196 switch (N.getOpcode()) {
197 default: break;
198 case ISD::Constant: {
Anton Korobeynikov32407402009-07-16 13:48:23 +0000199 int64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Daniel Dunbar19c29f52009-07-17 02:19:26 +0000200 int64_t Imm = 0;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000201 bool Match = (is12Bit ?
202 isImmZExt12(AM.Disp + Val, Imm) :
203 isImmSExt20(AM.Disp + Val, Imm));
204 if (Match) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000205 AM.Disp = Imm;
206 return false;
207 }
208 break;
209 }
210
211 case ISD::FrameIndex:
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000212 if (AM.BaseType == SystemZRRIAddressMode::RegBase &&
213 AM.Base.Reg.getNode() == 0) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000214 AM.BaseType = SystemZRRIAddressMode::FrameIndexBase;
215 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
216 return false;
217 }
218 break;
219
220 case ISD::SUB: {
221 // Given A-B, if A can be completely folded into the address and
222 // the index field with the index field unused, use -B as the index.
223 // This is a win if a has multiple parts that can be folded into
224 // the address. Also, this saves a mov if the base register has
225 // other uses, since it avoids a two-address sub instruction, however
226 // it costs an additional mov if the index register has other uses.
227
228 // Test if the LHS of the sub can be folded.
229 SystemZRRIAddressMode Backup = AM;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000230 if (MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1)) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000231 AM = Backup;
232 break;
233 }
234 // Test if the index field is free for use.
Anton Korobeynikov54681ec2009-07-16 14:31:14 +0000235 if (AM.IndexReg.getNode() || AM.isRI) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000236 AM = Backup;
237 break;
238 }
239
240 // If the base is a register with multiple uses, this transformation may
241 // save a mov. Otherwise it's probably better not to do it.
242 if (AM.BaseType == SystemZRRIAddressMode::RegBase &&
243 (!AM.Base.Reg.getNode() || AM.Base.Reg.getNode()->hasOneUse())) {
244 AM = Backup;
245 break;
246 }
247
248 // Ok, the transformation is legal and appears profitable. Go for it.
249 SDValue RHS = N.getNode()->getOperand(1);
250 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
251 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
252 AM.IndexReg = Neg;
253
254 // Insert the new nodes into the topological ordering.
255 if (Zero.getNode()->getNodeId() == -1 ||
256 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
257 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
258 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
259 }
260 if (Neg.getNode()->getNodeId() == -1 ||
261 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
262 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
263 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
264 }
265 return false;
266 }
267
268 case ISD::ADD: {
269 SystemZRRIAddressMode Backup = AM;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000270 if (!MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1) &&
271 !MatchAddress(N.getNode()->getOperand(1), AM, is12Bit, Depth+1))
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000272 return false;
273 AM = Backup;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000274 if (!MatchAddress(N.getNode()->getOperand(1), AM, is12Bit, Depth+1) &&
275 !MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1))
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000276 return false;
277 AM = Backup;
278
279 // If we couldn't fold both operands into the address at the same time,
280 // see if we can just put each operand into a register and fold at least
281 // the add.
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000282 if (!AM.isRI &&
283 AM.BaseType == SystemZRRIAddressMode::RegBase &&
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000284 !AM.Base.Reg.getNode() && !AM.IndexReg.getNode()) {
285 AM.Base.Reg = N.getNode()->getOperand(0);
286 AM.IndexReg = N.getNode()->getOperand(1);
287 return false;
288 }
289 break;
290 }
291
292 case ISD::OR:
293 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
294 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
295 SystemZRRIAddressMode Backup = AM;
Anton Korobeynikov32407402009-07-16 13:48:23 +0000296 int64_t Offset = CN->getSExtValue();
Daniel Dunbar19c29f52009-07-17 02:19:26 +0000297 int64_t Imm = 0;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000298 bool MatchOffset = (is12Bit ?
299 isImmZExt12(AM.Disp + Offset, Imm) :
300 isImmSExt20(AM.Disp + Offset, Imm));
301 // The resultant disp must fit in 12 or 20-bits.
302 if (MatchOffset &&
303 // LHS should be an addr mode.
304 !MatchAddress(N.getOperand(0), AM, is12Bit, Depth+1) &&
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000305 // Check to see if the LHS & C is zero.
306 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
307 AM.Disp = Imm;
308 return false;
309 }
310 AM = Backup;
311 }
312 break;
313 }
314
315 return MatchAddressBase(N, AM);
316}
317
318/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
319/// specified addressing mode without any further recursion.
320bool SystemZDAGToDAGISel::MatchAddressBase(SDValue N,
321 SystemZRRIAddressMode &AM) {
322 // Is the base register already occupied?
323 if (AM.BaseType != SystemZRRIAddressMode::RegBase || AM.Base.Reg.getNode()) {
Anton Korobeynikov46567602009-07-16 14:10:35 +0000324 // If so, check to see if the index register is set.
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000325 if (AM.IndexReg.getNode() == 0 && !AM.isRI) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000326 AM.IndexReg = N;
327 return false;
328 }
329
330 // Otherwise, we cannot select it.
331 return true;
332 }
333
334 // Default, generate it as a register.
335 AM.BaseType = SystemZRRIAddressMode::RegBase;
336 AM.Base.Reg = N;
337 return false;
338}
339
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000340void SystemZDAGToDAGISel::getAddressOperandsRI(const SystemZRRIAddressMode &AM,
341 SDValue &Base, SDValue &Disp) {
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000342 if (AM.BaseType == SystemZRRIAddressMode::RegBase)
343 Base = AM.Base.Reg;
344 else
345 Base = CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +0000346 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i64);
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000347}
348
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000349void SystemZDAGToDAGISel::getAddressOperands(const SystemZRRIAddressMode &AM,
350 SDValue &Base, SDValue &Disp,
351 SDValue &Index) {
352 getAddressOperandsRI(AM, Base, Disp);
353 Index = AM.IndexReg;
354}
355
356/// Returns true if the address can be represented by a base register plus
357/// an unsigned 12-bit displacement [r+imm].
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000358bool SystemZDAGToDAGISel::SelectAddrRI12Only(SDNode *Op, SDValue& Addr,
Anton Korobeynikov014d4632009-07-16 14:13:24 +0000359 SDValue &Base, SDValue &Disp) {
360 return SelectAddrRI12(Op, Addr, Base, Disp, /*is12BitOnly*/true);
361}
362
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000363bool SystemZDAGToDAGISel::SelectAddrRI12(SDNode *Op, SDValue& Addr,
Anton Korobeynikov014d4632009-07-16 14:13:24 +0000364 SDValue &Base, SDValue &Disp,
365 bool is12BitOnly) {
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000366 SystemZRRIAddressMode AM20(/*isRI*/true), AM12(/*isRI*/true);
367 bool Done = false;
368
369 if (!Addr.hasOneUse()) {
370 unsigned Opcode = Addr.getOpcode();
371 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
372 // If we are able to fold N into addressing mode, then we'll allow it even
373 // if N has multiple uses. In general, addressing computation is used as
374 // addresses by all of its uses. But watch out for CopyToReg uses, that
375 // means the address computation is liveout. It will be computed by a LA
376 // so we want to avoid computing the address twice.
377 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
378 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
379 if (UI->getOpcode() == ISD::CopyToReg) {
380 MatchAddressBase(Addr, AM12);
381 Done = true;
382 break;
383 }
384 }
385 }
386 }
387 if (!Done && MatchAddress(Addr, AM12, /* is12Bit */ true))
388 return false;
389
390 // Check, whether we can match stuff using 20-bit displacements
Anton Korobeynikov014d4632009-07-16 14:13:24 +0000391 if (!Done && !is12BitOnly &&
392 !MatchAddress(Addr, AM20, /* is12Bit */ false))
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000393 if (AM12.Disp == 0 && AM20.Disp != 0)
394 return false;
395
Chris Lattner893e1c92009-08-23 06:49:22 +0000396 DEBUG(errs() << "MatchAddress (final): "; AM12.dump());
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000397
Owen Andersone50ed302009-08-10 22:56:29 +0000398 EVT VT = Addr.getValueType();
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000399 if (AM12.BaseType == SystemZRRIAddressMode::RegBase) {
400 if (!AM12.Base.Reg.getNode())
401 AM12.Base.Reg = CurDAG->getRegister(0, VT);
402 }
403
404 assert(AM12.IndexReg.getNode() == 0 && "Invalid reg-imm address mode!");
405
406 getAddressOperandsRI(AM12, Base, Disp);
407
408 return true;
409}
410
411/// Returns true if the address can be represented by a base register plus
412/// a signed 20-bit displacement [r+imm].
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000413bool SystemZDAGToDAGISel::SelectAddrRI(SDNode *Op, SDValue& Addr,
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000414 SDValue &Base, SDValue &Disp) {
415 SystemZRRIAddressMode AM(/*isRI*/true);
416 bool Done = false;
417
418 if (!Addr.hasOneUse()) {
419 unsigned Opcode = Addr.getOpcode();
420 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
421 // If we are able to fold N into addressing mode, then we'll allow it even
422 // if N has multiple uses. In general, addressing computation is used as
423 // addresses by all of its uses. But watch out for CopyToReg uses, that
424 // means the address computation is liveout. It will be computed by a LA
425 // so we want to avoid computing the address twice.
426 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
427 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
428 if (UI->getOpcode() == ISD::CopyToReg) {
429 MatchAddressBase(Addr, AM);
430 Done = true;
431 break;
432 }
433 }
434 }
435 }
436 if (!Done && MatchAddress(Addr, AM, /* is12Bit */ false))
437 return false;
438
Chris Lattner893e1c92009-08-23 06:49:22 +0000439 DEBUG(errs() << "MatchAddress (final): "; AM.dump());
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000440
Owen Andersone50ed302009-08-10 22:56:29 +0000441 EVT VT = Addr.getValueType();
Anton Korobeynikov1ed1e3e2009-07-16 14:10:17 +0000442 if (AM.BaseType == SystemZRRIAddressMode::RegBase) {
443 if (!AM.Base.Reg.getNode())
444 AM.Base.Reg = CurDAG->getRegister(0, VT);
445 }
446
447 assert(AM.IndexReg.getNode() == 0 && "Invalid reg-imm address mode!");
448
449 getAddressOperandsRI(AM, Base, Disp);
450
451 return true;
452}
453
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000454/// Returns true if the address can be represented by a base register plus
455/// index register plus an unsigned 12-bit displacement [base + idx + imm].
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000456bool SystemZDAGToDAGISel::SelectAddrRRI12(SDNode *Op, SDValue Addr,
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000457 SDValue &Base, SDValue &Disp, SDValue &Index) {
Anton Korobeynikov46567602009-07-16 14:10:35 +0000458 SystemZRRIAddressMode AM20, AM12;
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000459 bool Done = false;
460
461 if (!Addr.hasOneUse()) {
462 unsigned Opcode = Addr.getOpcode();
463 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
464 // If we are able to fold N into addressing mode, then we'll allow it even
465 // if N has multiple uses. In general, addressing computation is used as
466 // addresses by all of its uses. But watch out for CopyToReg uses, that
467 // means the address computation is liveout. It will be computed by a LA
468 // so we want to avoid computing the address twice.
469 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
470 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
471 if (UI->getOpcode() == ISD::CopyToReg) {
472 MatchAddressBase(Addr, AM12);
473 Done = true;
474 break;
475 }
476 }
477 }
478 }
479 if (!Done && MatchAddress(Addr, AM12, /* is12Bit */ true))
480 return false;
481
482 // Check, whether we can match stuff using 20-bit displacements
483 if (!Done && !MatchAddress(Addr, AM20, /* is12Bit */ false))
484 if (AM12.Disp == 0 && AM20.Disp != 0)
485 return false;
486
Chris Lattner893e1c92009-08-23 06:49:22 +0000487 DEBUG(errs() << "MatchAddress (final): "; AM12.dump());
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000488
Owen Andersone50ed302009-08-10 22:56:29 +0000489 EVT VT = Addr.getValueType();
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000490 if (AM12.BaseType == SystemZRRIAddressMode::RegBase) {
491 if (!AM12.Base.Reg.getNode())
492 AM12.Base.Reg = CurDAG->getRegister(0, VT);
493 }
494
495 if (!AM12.IndexReg.getNode())
496 AM12.IndexReg = CurDAG->getRegister(0, VT);
497
498 getAddressOperands(AM12, Base, Disp, Index);
499
500 return true;
501}
502
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000503/// Returns true if the address can be represented by a base register plus
504/// index register plus a signed 20-bit displacement [base + idx + imm].
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000505bool SystemZDAGToDAGISel::SelectAddrRRI20(SDNode *Op, SDValue Addr,
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000506 SDValue &Base, SDValue &Disp, SDValue &Index) {
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000507 SystemZRRIAddressMode AM;
508 bool Done = false;
509
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000510 if (!Addr.hasOneUse()) {
511 unsigned Opcode = Addr.getOpcode();
512 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
513 // If we are able to fold N into addressing mode, then we'll allow it even
514 // if N has multiple uses. In general, addressing computation is used as
515 // addresses by all of its uses. But watch out for CopyToReg uses, that
516 // means the address computation is liveout. It will be computed by a LA
517 // so we want to avoid computing the address twice.
518 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
519 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
520 if (UI->getOpcode() == ISD::CopyToReg) {
521 MatchAddressBase(Addr, AM);
522 Done = true;
523 break;
524 }
525 }
526 }
527 }
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000528 if (!Done && MatchAddress(Addr, AM, /* is12Bit */ false))
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000529 return false;
530
Chris Lattner893e1c92009-08-23 06:49:22 +0000531 DEBUG(errs() << "MatchAddress (final): "; AM.dump());
Anton Korobeynikov32407402009-07-16 13:48:23 +0000532
Owen Andersone50ed302009-08-10 22:56:29 +0000533 EVT VT = Addr.getValueType();
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000534 if (AM.BaseType == SystemZRRIAddressMode::RegBase) {
535 if (!AM.Base.Reg.getNode())
536 AM.Base.Reg = CurDAG->getRegister(0, VT);
537 }
538
539 if (!AM.IndexReg.getNode())
540 AM.IndexReg = CurDAG->getRegister(0, VT);
541
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000542 getAddressOperands(AM, Base, Disp, Index);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000543
544 return true;
545}
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000546
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000547/// SelectLAAddr - it calls SelectAddr and determines if the maximal addressing
548/// mode it matches can be cost effectively emitted as an LA/LAY instruction.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000549bool SystemZDAGToDAGISel::SelectLAAddr(SDNode *Op, SDValue Addr,
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000550 SDValue &Base, SDValue &Disp, SDValue &Index) {
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000551 SystemZRRIAddressMode AM;
552
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000553 if (MatchAddress(Addr, AM, false))
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000554 return false;
555
Owen Andersone50ed302009-08-10 22:56:29 +0000556 EVT VT = Addr.getValueType();
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000557 unsigned Complexity = 0;
558 if (AM.BaseType == SystemZRRIAddressMode::RegBase)
559 if (AM.Base.Reg.getNode())
560 Complexity = 1;
561 else
562 AM.Base.Reg = CurDAG->getRegister(0, VT);
563 else if (AM.BaseType == SystemZRRIAddressMode::FrameIndexBase)
564 Complexity = 4;
565
566 if (AM.IndexReg.getNode())
567 Complexity += 1;
568 else
569 AM.IndexReg = CurDAG->getRegister(0, VT);
570
571 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
572 Complexity += 1;
573
574 if (Complexity > 2) {
Anton Korobeynikov720e3b02009-07-16 14:09:35 +0000575 getAddressOperands(AM, Base, Disp, Index);
Anton Korobeynikov711d5b62009-07-16 13:47:59 +0000576 return true;
577 }
578
579 return false;
580}
581
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000582bool SystemZDAGToDAGISel::TryFoldLoad(SDNode *P, SDValue N,
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000583 SDValue &Base, SDValue &Disp, SDValue &Index) {
584 if (ISD::isNON_EXTLoad(N.getNode()) &&
Dan Gohmand858e902010-04-17 15:26:15 +0000585 IsLegalToFold(N, P, P, OptLevel))
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000586 return SelectAddrRRI20(P, N.getOperand(1), Base, Disp, Index);
587 return false;
588}
589
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000590SDNode *SystemZDAGToDAGISel::Select(SDNode *Node) {
Owen Andersone50ed302009-08-10 22:56:29 +0000591 EVT NVT = Node->getValueType(0);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000592 DebugLoc dl = Node->getDebugLoc();
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000593 unsigned Opcode = Node->getOpcode();
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000594
595 // Dump information about the Node being selected
Chris Lattner7c306da2010-03-02 06:34:30 +0000596 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000597
598 // If we have a custom node, we already have selected!
599 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +0000600 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000601 return NULL; // Already selected.
602 }
603
604 switch (Opcode) {
605 default: break;
606 case ISD::SDIVREM: {
Anton Korobeynikov09e39002009-07-16 14:17:52 +0000607 unsigned Opc, MOpc;
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000608 SDValue N0 = Node->getOperand(0);
609 SDValue N1 = Node->getOperand(1);
610
Owen Andersone50ed302009-08-10 22:56:29 +0000611 EVT ResVT;
Anton Korobeynikov09e39002009-07-16 14:17:52 +0000612 bool is32Bit = false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000613 switch (NVT.getSimpleVT().SimpleTy) {
Anton Korobeynikov39784e12010-01-04 10:31:54 +0000614 default: assert(0 && "Unsupported VT!");
615 case MVT::i32:
616 Opc = SystemZ::SDIVREM32r; MOpc = SystemZ::SDIVREM32m;
617 ResVT = MVT::v2i64;
618 is32Bit = true;
619 break;
620 case MVT::i64:
621 Opc = SystemZ::SDIVREM64r; MOpc = SystemZ::SDIVREM64m;
622 ResVT = MVT::v2i64;
623 break;
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000624 }
625
626 SDValue Tmp0, Tmp1, Tmp2;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000627 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000628
629 // Prepare the dividend
Anton Korobeynikov09e39002009-07-16 14:17:52 +0000630 SDNode *Dividend;
631 if (is32Bit)
Dan Gohman602b0c82009-09-25 18:54:59 +0000632 Dividend = CurDAG->getMachineNode(SystemZ::MOVSX64rr32, dl, MVT::i64, N0);
Anton Korobeynikov09e39002009-07-16 14:17:52 +0000633 else
634 Dividend = N0.getNode();
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000635
636 // Insert prepared dividend into suitable 'subreg'
Chris Lattner518bb532010-02-09 19:54:29 +0000637 SDNode *Tmp = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
Dan Gohman602b0c82009-09-25 18:54:59 +0000638 dl, ResVT);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000639 Dividend =
Chris Lattner518bb532010-02-09 19:54:29 +0000640 CurDAG->getMachineNode(TargetOpcode::INSERT_SUBREG, dl, ResVT,
Dan Gohman602b0c82009-09-25 18:54:59 +0000641 SDValue(Tmp, 0), SDValue(Dividend, 0),
Jakob Stoklund Olesenc159fba2010-05-25 17:04:18 +0000642 CurDAG->getTargetConstant(SystemZ::subreg_odd, MVT::i32));
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000643
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000644 SDNode *Result;
645 SDValue DivVal = SDValue(Dividend, 0);
646 if (foldedLoad) {
647 SDValue Ops[] = { DivVal, Tmp0, Tmp1, Tmp2, N1.getOperand(0) };
Anton Korobeynikov39784e12010-01-04 10:31:54 +0000648 Result = CurDAG->getMachineNode(MOpc, dl, ResVT, MVT::Other,
Dan Gohman602b0c82009-09-25 18:54:59 +0000649 Ops, array_lengthof(Ops));
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000650 // Update the chain.
Anton Korobeynikov39784e12010-01-04 10:31:54 +0000651 ReplaceUses(N1.getValue(1), SDValue(Result, 1));
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000652 } else {
Dan Gohman602b0c82009-09-25 18:54:59 +0000653 Result = CurDAG->getMachineNode(Opc, dl, ResVT, SDValue(Dividend, 0), N1);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000654 }
655
656 // Copy the division (odd subreg) result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000657 if (!SDValue(Node, 0).use_empty()) {
Jakob Stoklund Olesenc159fba2010-05-25 17:04:18 +0000658 unsigned SubRegIdx = (is32Bit ?
659 SystemZ::subreg_odd32 : SystemZ::subreg_odd);
Chris Lattner518bb532010-02-09 19:54:29 +0000660 SDNode *Div = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
Dan Gohman602b0c82009-09-25 18:54:59 +0000661 dl, NVT,
662 SDValue(Result, 0),
663 CurDAG->getTargetConstant(SubRegIdx,
664 MVT::i32));
Anton Korobeynikov8bd0db72009-07-16 14:18:17 +0000665
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000666 ReplaceUses(SDValue(Node, 0), SDValue(Div, 0));
Chris Lattner7c306da2010-03-02 06:34:30 +0000667 DEBUG(errs() << "=> "; Result->dump(CurDAG); errs() << "\n");
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000668 }
669
670 // Copy the remainder (even subreg) result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000671 if (!SDValue(Node, 1).use_empty()) {
Jakob Stoklund Olesenc159fba2010-05-25 17:04:18 +0000672 unsigned SubRegIdx = (is32Bit ?
Jakob Stoklund Olesen05ce4892010-05-28 23:48:29 +0000673 SystemZ::subreg_32bit : SystemZ::subreg_even);
Chris Lattner518bb532010-02-09 19:54:29 +0000674 SDNode *Rem = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
Dan Gohman602b0c82009-09-25 18:54:59 +0000675 dl, NVT,
676 SDValue(Result, 0),
677 CurDAG->getTargetConstant(SubRegIdx,
678 MVT::i32));
Anton Korobeynikov09e39002009-07-16 14:17:52 +0000679
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000680 ReplaceUses(SDValue(Node, 1), SDValue(Rem, 0));
Chris Lattner7c306da2010-03-02 06:34:30 +0000681 DEBUG(errs() << "=> "; Result->dump(CurDAG); errs() << "\n");
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000682 }
683
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000684 return NULL;
685 }
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000686 case ISD::UDIVREM: {
687 unsigned Opc, MOpc, ClrOpc;
688 SDValue N0 = Node->getOperand(0);
689 SDValue N1 = Node->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +0000690 EVT ResVT;
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000691
Anton Korobeynikov8bd0db72009-07-16 14:18:17 +0000692 bool is32Bit = false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000693 switch (NVT.getSimpleVT().SimpleTy) {
Anton Korobeynikov39784e12010-01-04 10:31:54 +0000694 default: assert(0 && "Unsupported VT!");
695 case MVT::i32:
696 Opc = SystemZ::UDIVREM32r; MOpc = SystemZ::UDIVREM32m;
697 ClrOpc = SystemZ::MOV64Pr0_even;
698 ResVT = MVT::v2i32;
699 is32Bit = true;
700 break;
701 case MVT::i64:
702 Opc = SystemZ::UDIVREM64r; MOpc = SystemZ::UDIVREM64m;
703 ClrOpc = SystemZ::MOV128r0_even;
704 ResVT = MVT::v2i64;
705 break;
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000706 }
707
708 SDValue Tmp0, Tmp1, Tmp2;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000709 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000710
711 // Prepare the dividend
712 SDNode *Dividend = N0.getNode();
713
714 // Insert prepared dividend into suitable 'subreg'
Chris Lattner518bb532010-02-09 19:54:29 +0000715 SDNode *Tmp = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
Dan Gohman602b0c82009-09-25 18:54:59 +0000716 dl, ResVT);
Anton Korobeynikov8bd0db72009-07-16 14:18:17 +0000717 {
Jakob Stoklund Olesenc159fba2010-05-25 17:04:18 +0000718 unsigned SubRegIdx = (is32Bit ?
719 SystemZ::subreg_odd32 : SystemZ::subreg_odd);
Anton Korobeynikov8bd0db72009-07-16 14:18:17 +0000720 Dividend =
Chris Lattner518bb532010-02-09 19:54:29 +0000721 CurDAG->getMachineNode(TargetOpcode::INSERT_SUBREG, dl, ResVT,
Dan Gohman602b0c82009-09-25 18:54:59 +0000722 SDValue(Tmp, 0), SDValue(Dividend, 0),
723 CurDAG->getTargetConstant(SubRegIdx, MVT::i32));
Anton Korobeynikov8bd0db72009-07-16 14:18:17 +0000724 }
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000725
Anton Korobeynikove3a7f7a2009-07-16 14:14:54 +0000726 // Zero out even subreg
Dan Gohman602b0c82009-09-25 18:54:59 +0000727 Dividend = CurDAG->getMachineNode(ClrOpc, dl, ResVT, SDValue(Dividend, 0));
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000728
729 SDValue DivVal = SDValue(Dividend, 0);
730 SDNode *Result;
731 if (foldedLoad) {
732 SDValue Ops[] = { DivVal, Tmp0, Tmp1, Tmp2, N1.getOperand(0) };
Anton Korobeynikov39784e12010-01-04 10:31:54 +0000733 Result = CurDAG->getMachineNode(MOpc, dl, ResVT, MVT::Other,
Dan Gohman602b0c82009-09-25 18:54:59 +0000734 Ops, array_lengthof(Ops));
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000735 // Update the chain.
Anton Korobeynikov39784e12010-01-04 10:31:54 +0000736 ReplaceUses(N1.getValue(1), SDValue(Result, 1));
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000737 } else {
Dan Gohman602b0c82009-09-25 18:54:59 +0000738 Result = CurDAG->getMachineNode(Opc, dl, ResVT, DivVal, N1);
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000739 }
740
741 // Copy the division (odd subreg) result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000742 if (!SDValue(Node, 0).use_empty()) {
Jakob Stoklund Olesenc159fba2010-05-25 17:04:18 +0000743 unsigned SubRegIdx = (is32Bit ?
744 SystemZ::subreg_odd32 : SystemZ::subreg_odd);
Chris Lattner518bb532010-02-09 19:54:29 +0000745 SDNode *Div = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
Dan Gohman602b0c82009-09-25 18:54:59 +0000746 dl, NVT,
747 SDValue(Result, 0),
748 CurDAG->getTargetConstant(SubRegIdx,
749 MVT::i32));
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000750 ReplaceUses(SDValue(Node, 0), SDValue(Div, 0));
Chris Lattner7c306da2010-03-02 06:34:30 +0000751 DEBUG(errs() << "=> "; Result->dump(CurDAG); errs() << "\n");
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000752 }
753
754 // Copy the remainder (even subreg) result, if it is needed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000755 if (!SDValue(Node, 1).use_empty()) {
Jakob Stoklund Olesenc159fba2010-05-25 17:04:18 +0000756 unsigned SubRegIdx = (is32Bit ?
Jakob Stoklund Olesen05ce4892010-05-28 23:48:29 +0000757 SystemZ::subreg_32bit : SystemZ::subreg_even);
Chris Lattner518bb532010-02-09 19:54:29 +0000758 SDNode *Rem = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
Dan Gohman602b0c82009-09-25 18:54:59 +0000759 dl, NVT,
760 SDValue(Result, 0),
761 CurDAG->getTargetConstant(SubRegIdx,
762 MVT::i32));
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000763 ReplaceUses(SDValue(Node, 1), SDValue(Rem, 0));
Chris Lattner7c306da2010-03-02 06:34:30 +0000764 DEBUG(errs() << "=> "; Result->dump(CurDAG); errs() << "\n");
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000765 }
766
Anton Korobeynikov0a42d2b2009-07-16 14:14:33 +0000767 return NULL;
768 }
769 }
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000770
771 // Select the default instruction
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000772 SDNode *ResNode = SelectCode(Node);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000773
Chris Lattner7c306da2010-03-02 06:34:30 +0000774 DEBUG(errs() << "=> ";
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000775 if (ResNode == NULL || ResNode == Node)
776 Node->dump(CurDAG);
Chris Lattner893e1c92009-08-23 06:49:22 +0000777 else
778 ResNode->dump(CurDAG);
779 errs() << "\n";
780 );
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000781 return ResNode;
782}