blob: 7096c0e0d8ea90c43a1f777eeda02d9932f0d2ac [file] [log] [blame]
Anton Korobeynikov32b7d5b2009-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"
Anton Korobeynikov147a9a72009-07-16 14:36:52 +000031#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000032using namespace llvm;
33
Anton Korobeynikovcf7a0e92009-07-16 14:18:17 +000034static const unsigned subreg_even32 = 1;
35static const unsigned subreg_odd32 = 2;
36static const unsigned subreg_even = 3;
37static const unsigned subreg_odd = 4;
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +000038
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +000039namespace {
40 /// SystemZRRIAddressMode - This corresponds to rriaddr, but uses SDValue's
41 /// instead of register numbers for the leaves of the matched tree.
42 struct SystemZRRIAddressMode {
43 enum {
44 RegBase,
45 FrameIndexBase
46 } BaseType;
47
48 struct { // This is really a union, discriminated by BaseType!
49 SDValue Reg;
50 int FrameIndex;
51 } Base;
52
53 SDValue IndexReg;
Anton Korobeynikovd3c94e72009-07-16 13:48:23 +000054 int64_t Disp;
Anton Korobeynikovdb3bc872009-07-16 14:10:17 +000055 bool isRI;
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +000056
Anton Korobeynikovdb3bc872009-07-16 14:10:17 +000057 SystemZRRIAddressMode(bool RI = false)
58 : BaseType(RegBase), IndexReg(), Disp(0), isRI(RI) {
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +000059 }
60
61 void dump() {
Chris Lattner36eef822009-08-23 07:05:07 +000062 errs() << "SystemZRRIAddressMode " << this << '\n';
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +000063 if (BaseType == RegBase) {
Chris Lattner36eef822009-08-23 07:05:07 +000064 errs() << "Base.Reg ";
65 if (Base.Reg.getNode() != 0)
66 Base.Reg.getNode()->dump();
67 else
68 errs() << "nul";
69 errs() << '\n';
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +000070 } else {
Chris Lattner36eef822009-08-23 07:05:07 +000071 errs() << " Base.FrameIndex " << Base.FrameIndex << '\n';
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +000072 }
Anton Korobeynikovdb3bc872009-07-16 14:10:17 +000073 if (!isRI) {
Chris Lattner36eef822009-08-23 07:05:07 +000074 errs() << "IndexReg ";
Anton Korobeynikovdb3bc872009-07-16 14:10:17 +000075 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump();
Chris Lattner36eef822009-08-23 07:05:07 +000076 else errs() << "nul";
Anton Korobeynikovdb3bc872009-07-16 14:10:17 +000077 }
Chris Lattner36eef822009-08-23 07:05:07 +000078 errs() << " Disp " << Disp << '\n';
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +000079 }
80 };
81}
82
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000083/// SystemZDAGToDAGISel - SystemZ specific code to select SystemZ machine
84/// instructions for SelectionDAG operations.
85///
86namespace {
87 class SystemZDAGToDAGISel : public SelectionDAGISel {
88 SystemZTargetLowering &Lowering;
89 const SystemZSubtarget &Subtarget;
90
Anton Korobeynikovdb3bc872009-07-16 14:10:17 +000091 void getAddressOperandsRI(const SystemZRRIAddressMode &AM,
92 SDValue &Base, SDValue &Disp);
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +000093 void getAddressOperands(const SystemZRRIAddressMode &AM,
94 SDValue &Base, SDValue &Disp,
95 SDValue &Index);
96
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000097 public:
98 SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel)
99 : SelectionDAGISel(TM, OptLevel),
100 Lowering(*TM.getTargetLowering()),
101 Subtarget(*TM.getSubtargetImpl()) { }
102
103 virtual void InstructionSelect();
104
105 virtual const char *getPassName() const {
106 return "SystemZ DAG->DAG Pattern Instruction Selection";
107 }
108
Anton Korobeynikov1da6edd2009-07-16 14:26:38 +0000109 /// getI8Imm - Return a target constant with the specified value, of type
110 /// i8.
111 inline SDValue getI8Imm(uint64_t Imm) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000112 return CurDAG->getTargetConstant(Imm, MVT::i8);
Anton Korobeynikov1da6edd2009-07-16 14:26:38 +0000113 }
114
Anton Korobeynikov98383422009-07-16 13:33:57 +0000115 /// getI16Imm - Return a target constant with the specified value, of type
116 /// i16.
117 inline SDValue getI16Imm(uint64_t Imm) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000118 return CurDAG->getTargetConstant(Imm, MVT::i16);
Anton Korobeynikov98383422009-07-16 13:33:57 +0000119 }
120
Anton Korobeynikovf4f580f2009-07-16 13:34:50 +0000121 /// getI32Imm - Return a target constant with the specified value, of type
122 /// i32.
123 inline SDValue getI32Imm(uint64_t Imm) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000124 return CurDAG->getTargetConstant(Imm, MVT::i32);
Anton Korobeynikovf4f580f2009-07-16 13:34:50 +0000125 }
126
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000127 // Include the pieces autogenerated from the target description.
Anton Korobeynikov98383422009-07-16 13:33:57 +0000128 #include "SystemZGenDAGISel.inc"
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000129
130 private:
Dan Gohman5f082a72010-01-05 01:24:18 +0000131 bool SelectAddrRI12Only(SDNode *Op, SDValue& Addr,
Anton Korobeynikov002103c2009-07-16 14:13:24 +0000132 SDValue &Base, SDValue &Disp);
Dan Gohman5f082a72010-01-05 01:24:18 +0000133 bool SelectAddrRI12(SDNode *Op, SDValue& Addr,
Anton Korobeynikov002103c2009-07-16 14:13:24 +0000134 SDValue &Base, SDValue &Disp,
135 bool is12BitOnly = false);
Dan Gohman5f082a72010-01-05 01:24:18 +0000136 bool SelectAddrRI(SDNode *Op, SDValue& Addr,
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000137 SDValue &Base, SDValue &Disp);
Dan Gohman5f082a72010-01-05 01:24:18 +0000138 bool SelectAddrRRI12(SDNode *Op, SDValue Addr,
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +0000139 SDValue &Base, SDValue &Disp, SDValue &Index);
Dan Gohman5f082a72010-01-05 01:24:18 +0000140 bool SelectAddrRRI20(SDNode *Op, SDValue Addr,
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +0000141 SDValue &Base, SDValue &Disp, SDValue &Index);
Dan Gohman5f082a72010-01-05 01:24:18 +0000142 bool SelectLAAddr(SDNode *Op, SDValue Addr,
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000143 SDValue &Base, SDValue &Disp, SDValue &Index);
144
Dan Gohman5f082a72010-01-05 01:24:18 +0000145 SDNode *Select(SDNode *Node);
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000146
Dan Gohman5f082a72010-01-05 01:24:18 +0000147 bool TryFoldLoad(SDNode *P, SDValue N,
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000148 SDValue &Base, SDValue &Disp, SDValue &Index);
149
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +0000150 bool MatchAddress(SDValue N, SystemZRRIAddressMode &AM,
151 bool is12Bit, unsigned Depth = 0);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000152 bool MatchAddressBase(SDValue N, SystemZRRIAddressMode &AM);
Anton Korobeynikovdb3bc872009-07-16 14:10:17 +0000153 bool MatchAddressRI(SDValue N, SystemZRRIAddressMode &AM,
154 bool is12Bit);
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000155
156 #ifndef NDEBUG
157 unsigned Indent;
158 #endif
159 };
160} // end anonymous namespace
161
162/// createSystemZISelDag - This pass converts a legalized DAG into a
163/// SystemZ-specific DAG, ready for instruction scheduling.
164///
165FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
166 CodeGenOpt::Level OptLevel) {
167 return new SystemZDAGToDAGISel(TM, OptLevel);
168}
169
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000170/// isImmSExt20 - This method tests to see if the node is either a 32-bit
171/// or 64-bit immediate, and if the value can be accurately represented as a
172/// sign extension from a 20-bit value. If so, this returns true and the
173/// immediate.
Anton Korobeynikovd3c94e72009-07-16 13:48:23 +0000174static bool isImmSExt20(int64_t Val, int64_t &Imm) {
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000175 if (Val >= -524288 && Val <= 524287) {
Anton Korobeynikovd3c94e72009-07-16 13:48:23 +0000176 Imm = Val;
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000177 return true;
178 }
179 return false;
180}
181
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +0000182/// isImmZExt12 - This method tests to see if the node is either a 32-bit
Anton Korobeynikov09c5c3e2009-07-16 14:03:41 +0000183/// or 64-bit immediate, and if the value can be accurately represented as a
184/// zero extension from a 12-bit value. If so, this returns true and the
185/// immediate.
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +0000186static bool isImmZExt12(int64_t Val, int64_t &Imm) {
187 if (Val >= 0 && Val <= 0xFFF) {
Anton Korobeynikov09c5c3e2009-07-16 14:03:41 +0000188 Imm = Val;
189 return true;
190 }
Anton Korobeynikov09c5c3e2009-07-16 14:03:41 +0000191 return false;
192}
193
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000194/// MatchAddress - Add the specified node to the specified addressing mode,
195/// returning true if it cannot be done. This just pattern matches for the
196/// addressing mode.
197bool SystemZDAGToDAGISel::MatchAddress(SDValue N, SystemZRRIAddressMode &AM,
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +0000198 bool is12Bit, unsigned Depth) {
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000199 DebugLoc dl = N.getDebugLoc();
Chris Lattner2c6014b2009-08-23 06:49:22 +0000200 DEBUG(errs() << "MatchAddress: "; AM.dump());
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000201 // Limit recursion.
202 if (Depth > 5)
203 return MatchAddressBase(N, AM);
204
Anton Korobeynikov6f73af92009-07-16 13:44:30 +0000205 // FIXME: We can perform better here. If we have something like
206 // (shift (add A, imm), N), we can try to reassociate stuff and fold shift of
207 // imm into addressing mode.
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000208 switch (N.getOpcode()) {
209 default: break;
210 case ISD::Constant: {
Anton Korobeynikovd3c94e72009-07-16 13:48:23 +0000211 int64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Daniel Dunbar0a85adb2009-07-17 02:19:26 +0000212 int64_t Imm = 0;
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +0000213 bool Match = (is12Bit ?
214 isImmZExt12(AM.Disp + Val, Imm) :
215 isImmSExt20(AM.Disp + Val, Imm));
216 if (Match) {
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000217 AM.Disp = Imm;
218 return false;
219 }
220 break;
221 }
222
223 case ISD::FrameIndex:
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +0000224 if (AM.BaseType == SystemZRRIAddressMode::RegBase &&
225 AM.Base.Reg.getNode() == 0) {
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000226 AM.BaseType = SystemZRRIAddressMode::FrameIndexBase;
227 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
228 return false;
229 }
230 break;
231
232 case ISD::SUB: {
233 // Given A-B, if A can be completely folded into the address and
234 // the index field with the index field unused, use -B as the index.
235 // This is a win if a has multiple parts that can be folded into
236 // the address. Also, this saves a mov if the base register has
237 // other uses, since it avoids a two-address sub instruction, however
238 // it costs an additional mov if the index register has other uses.
239
240 // Test if the LHS of the sub can be folded.
241 SystemZRRIAddressMode Backup = AM;
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +0000242 if (MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1)) {
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000243 AM = Backup;
244 break;
245 }
246 // Test if the index field is free for use.
Anton Korobeynikovf9828992009-07-16 14:31:14 +0000247 if (AM.IndexReg.getNode() || AM.isRI) {
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000248 AM = Backup;
249 break;
250 }
251
252 // If the base is a register with multiple uses, this transformation may
253 // save a mov. Otherwise it's probably better not to do it.
254 if (AM.BaseType == SystemZRRIAddressMode::RegBase &&
255 (!AM.Base.Reg.getNode() || AM.Base.Reg.getNode()->hasOneUse())) {
256 AM = Backup;
257 break;
258 }
259
260 // Ok, the transformation is legal and appears profitable. Go for it.
261 SDValue RHS = N.getNode()->getOperand(1);
262 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
263 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
264 AM.IndexReg = Neg;
265
266 // Insert the new nodes into the topological ordering.
267 if (Zero.getNode()->getNodeId() == -1 ||
268 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
269 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
270 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
271 }
272 if (Neg.getNode()->getNodeId() == -1 ||
273 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
274 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
275 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
276 }
277 return false;
278 }
279
280 case ISD::ADD: {
281 SystemZRRIAddressMode Backup = AM;
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +0000282 if (!MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1) &&
283 !MatchAddress(N.getNode()->getOperand(1), AM, is12Bit, Depth+1))
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000284 return false;
285 AM = Backup;
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +0000286 if (!MatchAddress(N.getNode()->getOperand(1), AM, is12Bit, Depth+1) &&
287 !MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1))
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000288 return false;
289 AM = Backup;
290
291 // If we couldn't fold both operands into the address at the same time,
292 // see if we can just put each operand into a register and fold at least
293 // the add.
Anton Korobeynikovdb3bc872009-07-16 14:10:17 +0000294 if (!AM.isRI &&
295 AM.BaseType == SystemZRRIAddressMode::RegBase &&
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000296 !AM.Base.Reg.getNode() && !AM.IndexReg.getNode()) {
297 AM.Base.Reg = N.getNode()->getOperand(0);
298 AM.IndexReg = N.getNode()->getOperand(1);
299 return false;
300 }
301 break;
302 }
303
304 case ISD::OR:
305 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
306 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
307 SystemZRRIAddressMode Backup = AM;
Anton Korobeynikovd3c94e72009-07-16 13:48:23 +0000308 int64_t Offset = CN->getSExtValue();
Daniel Dunbar0a85adb2009-07-17 02:19:26 +0000309 int64_t Imm = 0;
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +0000310 bool MatchOffset = (is12Bit ?
311 isImmZExt12(AM.Disp + Offset, Imm) :
312 isImmSExt20(AM.Disp + Offset, Imm));
313 // The resultant disp must fit in 12 or 20-bits.
314 if (MatchOffset &&
315 // LHS should be an addr mode.
316 !MatchAddress(N.getOperand(0), AM, is12Bit, Depth+1) &&
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000317 // Check to see if the LHS & C is zero.
318 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
319 AM.Disp = Imm;
320 return false;
321 }
322 AM = Backup;
323 }
324 break;
325 }
326
327 return MatchAddressBase(N, AM);
328}
329
330/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
331/// specified addressing mode without any further recursion.
332bool SystemZDAGToDAGISel::MatchAddressBase(SDValue N,
333 SystemZRRIAddressMode &AM) {
334 // Is the base register already occupied?
335 if (AM.BaseType != SystemZRRIAddressMode::RegBase || AM.Base.Reg.getNode()) {
Anton Korobeynikov514a0972009-07-16 14:10:35 +0000336 // If so, check to see if the index register is set.
Anton Korobeynikovdb3bc872009-07-16 14:10:17 +0000337 if (AM.IndexReg.getNode() == 0 && !AM.isRI) {
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000338 AM.IndexReg = N;
339 return false;
340 }
341
342 // Otherwise, we cannot select it.
343 return true;
344 }
345
346 // Default, generate it as a register.
347 AM.BaseType = SystemZRRIAddressMode::RegBase;
348 AM.Base.Reg = N;
349 return false;
350}
351
Anton Korobeynikovdb3bc872009-07-16 14:10:17 +0000352void SystemZDAGToDAGISel::getAddressOperandsRI(const SystemZRRIAddressMode &AM,
353 SDValue &Base, SDValue &Disp) {
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +0000354 if (AM.BaseType == SystemZRRIAddressMode::RegBase)
355 Base = AM.Base.Reg;
356 else
357 Base = CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy());
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000358 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i64);
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +0000359}
360
Anton Korobeynikovdb3bc872009-07-16 14:10:17 +0000361void SystemZDAGToDAGISel::getAddressOperands(const SystemZRRIAddressMode &AM,
362 SDValue &Base, SDValue &Disp,
363 SDValue &Index) {
364 getAddressOperandsRI(AM, Base, Disp);
365 Index = AM.IndexReg;
366}
367
368/// Returns true if the address can be represented by a base register plus
369/// an unsigned 12-bit displacement [r+imm].
Dan Gohman5f082a72010-01-05 01:24:18 +0000370bool SystemZDAGToDAGISel::SelectAddrRI12Only(SDNode *Op, SDValue& Addr,
Anton Korobeynikov002103c2009-07-16 14:13:24 +0000371 SDValue &Base, SDValue &Disp) {
372 return SelectAddrRI12(Op, Addr, Base, Disp, /*is12BitOnly*/true);
373}
374
Dan Gohman5f082a72010-01-05 01:24:18 +0000375bool SystemZDAGToDAGISel::SelectAddrRI12(SDNode *Op, SDValue& Addr,
Anton Korobeynikov002103c2009-07-16 14:13:24 +0000376 SDValue &Base, SDValue &Disp,
377 bool is12BitOnly) {
Anton Korobeynikovdb3bc872009-07-16 14:10:17 +0000378 SystemZRRIAddressMode AM20(/*isRI*/true), AM12(/*isRI*/true);
379 bool Done = false;
380
381 if (!Addr.hasOneUse()) {
382 unsigned Opcode = Addr.getOpcode();
383 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
384 // If we are able to fold N into addressing mode, then we'll allow it even
385 // if N has multiple uses. In general, addressing computation is used as
386 // addresses by all of its uses. But watch out for CopyToReg uses, that
387 // means the address computation is liveout. It will be computed by a LA
388 // so we want to avoid computing the address twice.
389 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
390 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
391 if (UI->getOpcode() == ISD::CopyToReg) {
392 MatchAddressBase(Addr, AM12);
393 Done = true;
394 break;
395 }
396 }
397 }
398 }
399 if (!Done && MatchAddress(Addr, AM12, /* is12Bit */ true))
400 return false;
401
402 // Check, whether we can match stuff using 20-bit displacements
Anton Korobeynikov002103c2009-07-16 14:13:24 +0000403 if (!Done && !is12BitOnly &&
404 !MatchAddress(Addr, AM20, /* is12Bit */ false))
Anton Korobeynikovdb3bc872009-07-16 14:10:17 +0000405 if (AM12.Disp == 0 && AM20.Disp != 0)
406 return false;
407
Chris Lattner2c6014b2009-08-23 06:49:22 +0000408 DEBUG(errs() << "MatchAddress (final): "; AM12.dump());
Anton Korobeynikovdb3bc872009-07-16 14:10:17 +0000409
Owen Andersonac9de032009-08-10 22:56:29 +0000410 EVT VT = Addr.getValueType();
Anton Korobeynikovdb3bc872009-07-16 14:10:17 +0000411 if (AM12.BaseType == SystemZRRIAddressMode::RegBase) {
412 if (!AM12.Base.Reg.getNode())
413 AM12.Base.Reg = CurDAG->getRegister(0, VT);
414 }
415
416 assert(AM12.IndexReg.getNode() == 0 && "Invalid reg-imm address mode!");
417
418 getAddressOperandsRI(AM12, Base, Disp);
419
420 return true;
421}
422
423/// Returns true if the address can be represented by a base register plus
424/// a signed 20-bit displacement [r+imm].
Dan Gohman5f082a72010-01-05 01:24:18 +0000425bool SystemZDAGToDAGISel::SelectAddrRI(SDNode *Op, SDValue& Addr,
Anton Korobeynikovdb3bc872009-07-16 14:10:17 +0000426 SDValue &Base, SDValue &Disp) {
427 SystemZRRIAddressMode AM(/*isRI*/true);
428 bool Done = false;
429
430 if (!Addr.hasOneUse()) {
431 unsigned Opcode = Addr.getOpcode();
432 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
433 // If we are able to fold N into addressing mode, then we'll allow it even
434 // if N has multiple uses. In general, addressing computation is used as
435 // addresses by all of its uses. But watch out for CopyToReg uses, that
436 // means the address computation is liveout. It will be computed by a LA
437 // so we want to avoid computing the address twice.
438 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
439 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
440 if (UI->getOpcode() == ISD::CopyToReg) {
441 MatchAddressBase(Addr, AM);
442 Done = true;
443 break;
444 }
445 }
446 }
447 }
448 if (!Done && MatchAddress(Addr, AM, /* is12Bit */ false))
449 return false;
450
Chris Lattner2c6014b2009-08-23 06:49:22 +0000451 DEBUG(errs() << "MatchAddress (final): "; AM.dump());
Anton Korobeynikovdb3bc872009-07-16 14:10:17 +0000452
Owen Andersonac9de032009-08-10 22:56:29 +0000453 EVT VT = Addr.getValueType();
Anton Korobeynikovdb3bc872009-07-16 14:10:17 +0000454 if (AM.BaseType == SystemZRRIAddressMode::RegBase) {
455 if (!AM.Base.Reg.getNode())
456 AM.Base.Reg = CurDAG->getRegister(0, VT);
457 }
458
459 assert(AM.IndexReg.getNode() == 0 && "Invalid reg-imm address mode!");
460
461 getAddressOperandsRI(AM, Base, Disp);
462
463 return true;
464}
465
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +0000466/// Returns true if the address can be represented by a base register plus
467/// index register plus an unsigned 12-bit displacement [base + idx + imm].
Dan Gohman5f082a72010-01-05 01:24:18 +0000468bool SystemZDAGToDAGISel::SelectAddrRRI12(SDNode *Op, SDValue Addr,
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +0000469 SDValue &Base, SDValue &Disp, SDValue &Index) {
Anton Korobeynikov514a0972009-07-16 14:10:35 +0000470 SystemZRRIAddressMode AM20, AM12;
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +0000471 bool Done = false;
472
473 if (!Addr.hasOneUse()) {
474 unsigned Opcode = Addr.getOpcode();
475 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
476 // If we are able to fold N into addressing mode, then we'll allow it even
477 // if N has multiple uses. In general, addressing computation is used as
478 // addresses by all of its uses. But watch out for CopyToReg uses, that
479 // means the address computation is liveout. It will be computed by a LA
480 // so we want to avoid computing the address twice.
481 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
482 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
483 if (UI->getOpcode() == ISD::CopyToReg) {
484 MatchAddressBase(Addr, AM12);
485 Done = true;
486 break;
487 }
488 }
489 }
490 }
491 if (!Done && MatchAddress(Addr, AM12, /* is12Bit */ true))
492 return false;
493
494 // Check, whether we can match stuff using 20-bit displacements
495 if (!Done && !MatchAddress(Addr, AM20, /* is12Bit */ false))
496 if (AM12.Disp == 0 && AM20.Disp != 0)
497 return false;
498
Chris Lattner2c6014b2009-08-23 06:49:22 +0000499 DEBUG(errs() << "MatchAddress (final): "; AM12.dump());
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +0000500
Owen Andersonac9de032009-08-10 22:56:29 +0000501 EVT VT = Addr.getValueType();
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +0000502 if (AM12.BaseType == SystemZRRIAddressMode::RegBase) {
503 if (!AM12.Base.Reg.getNode())
504 AM12.Base.Reg = CurDAG->getRegister(0, VT);
505 }
506
507 if (!AM12.IndexReg.getNode())
508 AM12.IndexReg = CurDAG->getRegister(0, VT);
509
510 getAddressOperands(AM12, Base, Disp, Index);
511
512 return true;
513}
514
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000515/// Returns true if the address can be represented by a base register plus
516/// index register plus a signed 20-bit displacement [base + idx + imm].
Dan Gohman5f082a72010-01-05 01:24:18 +0000517bool SystemZDAGToDAGISel::SelectAddrRRI20(SDNode *Op, SDValue Addr,
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000518 SDValue &Base, SDValue &Disp, SDValue &Index) {
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000519 SystemZRRIAddressMode AM;
520 bool Done = false;
521
Anton Korobeynikov3de52b72009-07-16 13:47:59 +0000522 if (!Addr.hasOneUse()) {
523 unsigned Opcode = Addr.getOpcode();
524 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) {
525 // If we are able to fold N into addressing mode, then we'll allow it even
526 // if N has multiple uses. In general, addressing computation is used as
527 // addresses by all of its uses. But watch out for CopyToReg uses, that
528 // means the address computation is liveout. It will be computed by a LA
529 // so we want to avoid computing the address twice.
530 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
531 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
532 if (UI->getOpcode() == ISD::CopyToReg) {
533 MatchAddressBase(Addr, AM);
534 Done = true;
535 break;
536 }
537 }
538 }
539 }
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +0000540 if (!Done && MatchAddress(Addr, AM, /* is12Bit */ false))
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000541 return false;
542
Chris Lattner2c6014b2009-08-23 06:49:22 +0000543 DEBUG(errs() << "MatchAddress (final): "; AM.dump());
Anton Korobeynikovd3c94e72009-07-16 13:48:23 +0000544
Owen Andersonac9de032009-08-10 22:56:29 +0000545 EVT VT = Addr.getValueType();
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000546 if (AM.BaseType == SystemZRRIAddressMode::RegBase) {
547 if (!AM.Base.Reg.getNode())
548 AM.Base.Reg = CurDAG->getRegister(0, VT);
549 }
550
551 if (!AM.IndexReg.getNode())
552 AM.IndexReg = CurDAG->getRegister(0, VT);
553
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +0000554 getAddressOperands(AM, Base, Disp, Index);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000555
556 return true;
557}
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000558
Anton Korobeynikov3de52b72009-07-16 13:47:59 +0000559/// SelectLAAddr - it calls SelectAddr and determines if the maximal addressing
560/// mode it matches can be cost effectively emitted as an LA/LAY instruction.
Dan Gohman5f082a72010-01-05 01:24:18 +0000561bool SystemZDAGToDAGISel::SelectLAAddr(SDNode *Op, SDValue Addr,
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000562 SDValue &Base, SDValue &Disp, SDValue &Index) {
Anton Korobeynikov3de52b72009-07-16 13:47:59 +0000563 SystemZRRIAddressMode AM;
564
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +0000565 if (MatchAddress(Addr, AM, false))
Anton Korobeynikov3de52b72009-07-16 13:47:59 +0000566 return false;
567
Owen Andersonac9de032009-08-10 22:56:29 +0000568 EVT VT = Addr.getValueType();
Anton Korobeynikov3de52b72009-07-16 13:47:59 +0000569 unsigned Complexity = 0;
570 if (AM.BaseType == SystemZRRIAddressMode::RegBase)
571 if (AM.Base.Reg.getNode())
572 Complexity = 1;
573 else
574 AM.Base.Reg = CurDAG->getRegister(0, VT);
575 else if (AM.BaseType == SystemZRRIAddressMode::FrameIndexBase)
576 Complexity = 4;
577
578 if (AM.IndexReg.getNode())
579 Complexity += 1;
580 else
581 AM.IndexReg = CurDAG->getRegister(0, VT);
582
583 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
584 Complexity += 1;
585
586 if (Complexity > 2) {
Anton Korobeynikov24f6ec42009-07-16 14:09:35 +0000587 getAddressOperands(AM, Base, Disp, Index);
Anton Korobeynikov3de52b72009-07-16 13:47:59 +0000588 return true;
589 }
590
591 return false;
592}
593
Dan Gohman5f082a72010-01-05 01:24:18 +0000594bool SystemZDAGToDAGISel::TryFoldLoad(SDNode *P, SDValue N,
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000595 SDValue &Base, SDValue &Disp, SDValue &Index) {
596 if (ISD::isNON_EXTLoad(N.getNode()) &&
597 N.hasOneUse() &&
Dan Gohman5f082a72010-01-05 01:24:18 +0000598 IsLegalAndProfitableToFold(N.getNode(), P, P))
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000599 return SelectAddrRRI20(P, N.getOperand(1), Base, Disp, Index);
600 return false;
601}
602
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000603/// InstructionSelect - This callback is invoked by
604/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
605void SystemZDAGToDAGISel::InstructionSelect() {
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000606 // Codegen the basic block.
Chris Lattner2c6014b2009-08-23 06:49:22 +0000607 DEBUG(errs() << "===== Instruction selection begins:\n");
Daniel Dunbar33b26632009-08-23 08:50:52 +0000608 DEBUG(Indent = 0);
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000609 SelectRoot(*CurDAG);
Chris Lattner2c6014b2009-08-23 06:49:22 +0000610 DEBUG(errs() << "===== Instruction selection ends:\n");
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000611
612 CurDAG->RemoveDeadNodes();
613}
614
Dan Gohman5f082a72010-01-05 01:24:18 +0000615SDNode *SystemZDAGToDAGISel::Select(SDNode *Node) {
Owen Andersonac9de032009-08-10 22:56:29 +0000616 EVT NVT = Node->getValueType(0);
Dan Gohman5f082a72010-01-05 01:24:18 +0000617 DebugLoc dl = Node->getDebugLoc();
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000618 unsigned Opcode = Node->getOpcode();
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000619
620 // Dump information about the Node being selected
Chris Lattner2c6014b2009-08-23 06:49:22 +0000621 DEBUG(errs().indent(Indent) << "Selecting: ";
622 Node->dump(CurDAG);
623 errs() << "\n");
Daniel Dunbar33b26632009-08-23 08:50:52 +0000624 DEBUG(Indent += 2);
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000625
626 // If we have a custom node, we already have selected!
627 if (Node->isMachineOpcode()) {
Chris Lattner2c6014b2009-08-23 06:49:22 +0000628 DEBUG(errs().indent(Indent-2) << "== ";
629 Node->dump(CurDAG);
630 errs() << "\n");
Daniel Dunbar33b26632009-08-23 08:50:52 +0000631 DEBUG(Indent -= 2);
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000632 return NULL; // Already selected.
633 }
634
635 switch (Opcode) {
636 default: break;
637 case ISD::SDIVREM: {
Anton Korobeynikov5b35e2c2009-07-16 14:17:52 +0000638 unsigned Opc, MOpc;
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000639 SDValue N0 = Node->getOperand(0);
640 SDValue N1 = Node->getOperand(1);
641
Owen Andersonac9de032009-08-10 22:56:29 +0000642 EVT ResVT;
Anton Korobeynikov5b35e2c2009-07-16 14:17:52 +0000643 bool is32Bit = false;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000644 switch (NVT.getSimpleVT().SimpleTy) {
Anton Korobeynikova087db32010-01-04 10:31:54 +0000645 default: assert(0 && "Unsupported VT!");
646 case MVT::i32:
647 Opc = SystemZ::SDIVREM32r; MOpc = SystemZ::SDIVREM32m;
648 ResVT = MVT::v2i64;
649 is32Bit = true;
650 break;
651 case MVT::i64:
652 Opc = SystemZ::SDIVREM64r; MOpc = SystemZ::SDIVREM64m;
653 ResVT = MVT::v2i64;
654 break;
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000655 }
656
657 SDValue Tmp0, Tmp1, Tmp2;
Dan Gohman5f082a72010-01-05 01:24:18 +0000658 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2);
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000659
660 // Prepare the dividend
Anton Korobeynikov5b35e2c2009-07-16 14:17:52 +0000661 SDNode *Dividend;
662 if (is32Bit)
Dan Gohman61fda0d2009-09-25 18:54:59 +0000663 Dividend = CurDAG->getMachineNode(SystemZ::MOVSX64rr32, dl, MVT::i64, N0);
Anton Korobeynikov5b35e2c2009-07-16 14:17:52 +0000664 else
665 Dividend = N0.getNode();
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000666
667 // Insert prepared dividend into suitable 'subreg'
Dan Gohman61fda0d2009-09-25 18:54:59 +0000668 SDNode *Tmp = CurDAG->getMachineNode(TargetInstrInfo::IMPLICIT_DEF,
669 dl, ResVT);
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000670 Dividend =
Dan Gohman61fda0d2009-09-25 18:54:59 +0000671 CurDAG->getMachineNode(TargetInstrInfo::INSERT_SUBREG, dl, ResVT,
672 SDValue(Tmp, 0), SDValue(Dividend, 0),
673 CurDAG->getTargetConstant(subreg_odd, MVT::i32));
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000674
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000675 SDNode *Result;
676 SDValue DivVal = SDValue(Dividend, 0);
677 if (foldedLoad) {
678 SDValue Ops[] = { DivVal, Tmp0, Tmp1, Tmp2, N1.getOperand(0) };
Anton Korobeynikova087db32010-01-04 10:31:54 +0000679 Result = CurDAG->getMachineNode(MOpc, dl, ResVT, MVT::Other,
Dan Gohman61fda0d2009-09-25 18:54:59 +0000680 Ops, array_lengthof(Ops));
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000681 // Update the chain.
Anton Korobeynikova087db32010-01-04 10:31:54 +0000682 ReplaceUses(N1.getValue(1), SDValue(Result, 1));
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000683 } else {
Dan Gohman61fda0d2009-09-25 18:54:59 +0000684 Result = CurDAG->getMachineNode(Opc, dl, ResVT, SDValue(Dividend, 0), N1);
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000685 }
686
687 // Copy the division (odd subreg) result, if it is needed.
Dan Gohman5f082a72010-01-05 01:24:18 +0000688 if (!SDValue(Node, 0).use_empty()) {
Anton Korobeynikovcf7a0e92009-07-16 14:18:17 +0000689 unsigned SubRegIdx = (is32Bit ? subreg_odd32 : subreg_odd);
Dan Gohman61fda0d2009-09-25 18:54:59 +0000690 SDNode *Div = CurDAG->getMachineNode(TargetInstrInfo::EXTRACT_SUBREG,
691 dl, NVT,
692 SDValue(Result, 0),
693 CurDAG->getTargetConstant(SubRegIdx,
694 MVT::i32));
Anton Korobeynikovcf7a0e92009-07-16 14:18:17 +0000695
Dan Gohman5f082a72010-01-05 01:24:18 +0000696 ReplaceUses(SDValue(Node, 0), SDValue(Div, 0));
Chris Lattner2c6014b2009-08-23 06:49:22 +0000697 DEBUG(errs().indent(Indent-2) << "=> ";
698 Result->dump(CurDAG);
699 errs() << "\n");
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000700 }
701
702 // Copy the remainder (even subreg) result, if it is needed.
Dan Gohman5f082a72010-01-05 01:24:18 +0000703 if (!SDValue(Node, 1).use_empty()) {
Anton Korobeynikovcf7a0e92009-07-16 14:18:17 +0000704 unsigned SubRegIdx = (is32Bit ? subreg_even32 : subreg_even);
Dan Gohman61fda0d2009-09-25 18:54:59 +0000705 SDNode *Rem = CurDAG->getMachineNode(TargetInstrInfo::EXTRACT_SUBREG,
706 dl, NVT,
707 SDValue(Result, 0),
708 CurDAG->getTargetConstant(SubRegIdx,
709 MVT::i32));
Anton Korobeynikov5b35e2c2009-07-16 14:17:52 +0000710
Dan Gohman5f082a72010-01-05 01:24:18 +0000711 ReplaceUses(SDValue(Node, 1), SDValue(Rem, 0));
Chris Lattner2c6014b2009-08-23 06:49:22 +0000712 DEBUG(errs().indent(Indent-2) << "=> ";
713 Result->dump(CurDAG);
714 errs() << "\n");
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000715 }
716
717#ifndef NDEBUG
718 Indent -= 2;
719#endif
720
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000721 return NULL;
722 }
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000723 case ISD::UDIVREM: {
724 unsigned Opc, MOpc, ClrOpc;
725 SDValue N0 = Node->getOperand(0);
726 SDValue N1 = Node->getOperand(1);
Owen Andersonac9de032009-08-10 22:56:29 +0000727 EVT ResVT;
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000728
Anton Korobeynikovcf7a0e92009-07-16 14:18:17 +0000729 bool is32Bit = false;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000730 switch (NVT.getSimpleVT().SimpleTy) {
Anton Korobeynikova087db32010-01-04 10:31:54 +0000731 default: assert(0 && "Unsupported VT!");
732 case MVT::i32:
733 Opc = SystemZ::UDIVREM32r; MOpc = SystemZ::UDIVREM32m;
734 ClrOpc = SystemZ::MOV64Pr0_even;
735 ResVT = MVT::v2i32;
736 is32Bit = true;
737 break;
738 case MVT::i64:
739 Opc = SystemZ::UDIVREM64r; MOpc = SystemZ::UDIVREM64m;
740 ClrOpc = SystemZ::MOV128r0_even;
741 ResVT = MVT::v2i64;
742 break;
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000743 }
744
745 SDValue Tmp0, Tmp1, Tmp2;
Dan Gohman5f082a72010-01-05 01:24:18 +0000746 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2);
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000747
748 // Prepare the dividend
749 SDNode *Dividend = N0.getNode();
750
751 // Insert prepared dividend into suitable 'subreg'
Dan Gohman61fda0d2009-09-25 18:54:59 +0000752 SDNode *Tmp = CurDAG->getMachineNode(TargetInstrInfo::IMPLICIT_DEF,
753 dl, ResVT);
Anton Korobeynikovcf7a0e92009-07-16 14:18:17 +0000754 {
755 unsigned SubRegIdx = (is32Bit ? subreg_odd32 : subreg_odd);
756 Dividend =
Dan Gohman61fda0d2009-09-25 18:54:59 +0000757 CurDAG->getMachineNode(TargetInstrInfo::INSERT_SUBREG, dl, ResVT,
758 SDValue(Tmp, 0), SDValue(Dividend, 0),
759 CurDAG->getTargetConstant(SubRegIdx, MVT::i32));
Anton Korobeynikovcf7a0e92009-07-16 14:18:17 +0000760 }
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000761
Anton Korobeynikov5fbf3c32009-07-16 14:14:54 +0000762 // Zero out even subreg
Dan Gohman61fda0d2009-09-25 18:54:59 +0000763 Dividend = CurDAG->getMachineNode(ClrOpc, dl, ResVT, SDValue(Dividend, 0));
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000764
765 SDValue DivVal = SDValue(Dividend, 0);
766 SDNode *Result;
767 if (foldedLoad) {
768 SDValue Ops[] = { DivVal, Tmp0, Tmp1, Tmp2, N1.getOperand(0) };
Anton Korobeynikova087db32010-01-04 10:31:54 +0000769 Result = CurDAG->getMachineNode(MOpc, dl, ResVT, MVT::Other,
Dan Gohman61fda0d2009-09-25 18:54:59 +0000770 Ops, array_lengthof(Ops));
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000771 // Update the chain.
Anton Korobeynikova087db32010-01-04 10:31:54 +0000772 ReplaceUses(N1.getValue(1), SDValue(Result, 1));
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000773 } else {
Dan Gohman61fda0d2009-09-25 18:54:59 +0000774 Result = CurDAG->getMachineNode(Opc, dl, ResVT, DivVal, N1);
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000775 }
776
777 // Copy the division (odd subreg) result, if it is needed.
Dan Gohman5f082a72010-01-05 01:24:18 +0000778 if (!SDValue(Node, 0).use_empty()) {
Anton Korobeynikovcf7a0e92009-07-16 14:18:17 +0000779 unsigned SubRegIdx = (is32Bit ? subreg_odd32 : subreg_odd);
Dan Gohman61fda0d2009-09-25 18:54:59 +0000780 SDNode *Div = CurDAG->getMachineNode(TargetInstrInfo::EXTRACT_SUBREG,
781 dl, NVT,
782 SDValue(Result, 0),
783 CurDAG->getTargetConstant(SubRegIdx,
784 MVT::i32));
Dan Gohman5f082a72010-01-05 01:24:18 +0000785 ReplaceUses(SDValue(Node, 0), SDValue(Div, 0));
Chris Lattner2c6014b2009-08-23 06:49:22 +0000786 DEBUG(errs().indent(Indent-2) << "=> ";
787 Result->dump(CurDAG);
788 errs() << "\n");
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000789 }
790
791 // Copy the remainder (even subreg) result, if it is needed.
Dan Gohman5f082a72010-01-05 01:24:18 +0000792 if (!SDValue(Node, 1).use_empty()) {
Anton Korobeynikovcf7a0e92009-07-16 14:18:17 +0000793 unsigned SubRegIdx = (is32Bit ? subreg_even32 : subreg_even);
Dan Gohman61fda0d2009-09-25 18:54:59 +0000794 SDNode *Rem = CurDAG->getMachineNode(TargetInstrInfo::EXTRACT_SUBREG,
795 dl, NVT,
796 SDValue(Result, 0),
797 CurDAG->getTargetConstant(SubRegIdx,
798 MVT::i32));
Dan Gohman5f082a72010-01-05 01:24:18 +0000799 ReplaceUses(SDValue(Node, 1), SDValue(Rem, 0));
Chris Lattner2c6014b2009-08-23 06:49:22 +0000800 DEBUG(errs().indent(Indent-2) << "=> ";
801 Result->dump(CurDAG);
802 errs() << "\n");
Anton Korobeynikovfc655ac2009-07-16 14:14:33 +0000803 }
804
805#ifndef NDEBUG
806 Indent -= 2;
807#endif
808
809 return NULL;
810 }
811 }
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000812
813 // Select the default instruction
Dan Gohman5f082a72010-01-05 01:24:18 +0000814 SDNode *ResNode = SelectCode(Node);
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000815
Chris Lattner2c6014b2009-08-23 06:49:22 +0000816 DEBUG(errs().indent(Indent-2) << "=> ";
Dan Gohman5f082a72010-01-05 01:24:18 +0000817 if (ResNode == NULL || ResNode == Node)
818 Node->dump(CurDAG);
Chris Lattner2c6014b2009-08-23 06:49:22 +0000819 else
820 ResNode->dump(CurDAG);
821 errs() << "\n";
822 );
Daniel Dunbar33b26632009-08-23 08:50:52 +0000823 DEBUG(Indent -= 2);
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000824
825 return ResNode;
826}