blob: fa343238284a69dcdef2fc9d31806b531d6bb387 [file] [log] [blame]
Chris Lattner7a125372005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattnerc961eea2005-11-16 01:54:32 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc961eea2005-11-16 01:54:32 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a DAG pattern matching instruction selector for X86,
11// converting from a legalized dag to a X86 dag.
12//
13//===----------------------------------------------------------------------===//
14
Evan Cheng2ef88a02006-08-07 22:28:20 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerc961eea2005-11-16 01:54:32 +000016#include "X86.h"
Evan Cheng8700e142006-01-11 06:09:51 +000017#include "X86InstrBuilder.h"
Evan Chengc4c62572006-03-13 23:20:37 +000018#include "X86ISelLowering.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000019#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000020#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000021#include "X86TargetMachine.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000022#include "llvm/GlobalValue.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000023#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000024#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000025#include "llvm/Support/CFG.h"
Reid Spencer7aa8a452007-01-12 23:22:14 +000026#include "llvm/Type.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000027#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000028#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000030#include "llvm/CodeGen/MachineInstrBuilder.h"
31#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000032#include "llvm/CodeGen/SelectionDAGISel.h"
33#include "llvm/Target/TargetMachine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000034#include "llvm/Support/Compiler.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000035#include "llvm/Support/Debug.h"
36#include "llvm/Support/MathExtras.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000037#include "llvm/ADT/Statistic.h"
Evan Cheng2ef88a02006-08-07 22:28:20 +000038#include <queue>
Evan Chengba2f0a92006-02-05 06:46:41 +000039#include <set>
Chris Lattnerc961eea2005-11-16 01:54:32 +000040using namespace llvm;
41
Chris Lattner95b2c7d2006-12-19 22:59:26 +000042STATISTIC(NumFPKill , "Number of FP_REG_KILL instructions added");
43STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
44
45
Chris Lattnerc961eea2005-11-16 01:54:32 +000046//===----------------------------------------------------------------------===//
47// Pattern Matcher Implementation
48//===----------------------------------------------------------------------===//
49
50namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000051 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
52 /// SDOperand's instead of register numbers for the leaves of the matched
53 /// tree.
54 struct X86ISelAddressMode {
55 enum {
56 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000057 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000058 } BaseType;
59
60 struct { // This is really a union, discriminated by BaseType!
61 SDOperand Reg;
62 int FrameIndex;
63 } Base;
64
Evan Cheng25ab6902006-09-08 06:48:29 +000065 bool isRIPRel; // RIP relative?
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000066 unsigned Scale;
67 SDOperand IndexReg;
68 unsigned Disp;
69 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000070 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000071 const char *ES;
72 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000073 unsigned Align; // CP alignment.
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000074
75 X86ISelAddressMode()
Evan Cheng25ab6902006-09-08 06:48:29 +000076 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
77 GV(0), CP(0), ES(0), JT(-1), Align(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000078 }
79 };
80}
81
82namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +000083 //===--------------------------------------------------------------------===//
84 /// ISel - X86 specific code to select X86 machine instructions for
85 /// SelectionDAG operations.
86 ///
Chris Lattner2c79de82006-06-28 23:27:49 +000087 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +000088 /// ContainsFPCode - Every instruction we select that uses or defines a FP
89 /// register should set this to true.
90 bool ContainsFPCode;
91
Evan Chenge50794a2006-08-29 18:28:33 +000092 /// FastISel - Enable fast(er) instruction selection.
93 ///
94 bool FastISel;
95
Evan Cheng25ab6902006-09-08 06:48:29 +000096 /// TM - Keep a reference to X86TargetMachine.
97 ///
98 X86TargetMachine &TM;
99
Chris Lattnerc961eea2005-11-16 01:54:32 +0000100 /// X86Lowering - This object fully describes how to lower LLVM code to an
101 /// X86-specific SelectionDAG.
102 X86TargetLowering X86Lowering;
103
104 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
105 /// make the right decision when generating code for different targets.
106 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000107
Evan Cheng25ab6902006-09-08 06:48:29 +0000108 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
109 /// base register.
Evan Cheng7ccced62006-02-18 00:15:05 +0000110 unsigned GlobalBaseReg;
Evan Chenga8df1b42006-07-27 16:44:36 +0000111
Chris Lattnerc961eea2005-11-16 01:54:32 +0000112 public:
Evan Cheng25ab6902006-09-08 06:48:29 +0000113 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Evan Chengc4c62572006-03-13 23:20:37 +0000114 : SelectionDAGISel(X86Lowering),
Evan Cheng25ab6902006-09-08 06:48:29 +0000115 ContainsFPCode(false), FastISel(fast), TM(tm),
Evan Chenga8df1b42006-07-27 16:44:36 +0000116 X86Lowering(*TM.getTargetLowering()),
Evan Chengf4b4c412006-08-08 00:31:00 +0000117 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000118
Evan Cheng7ccced62006-02-18 00:15:05 +0000119 virtual bool runOnFunction(Function &Fn) {
120 // Make sure we re-emit a set of the global base reg if necessary
121 GlobalBaseReg = 0;
122 return SelectionDAGISel::runOnFunction(Fn);
123 }
124
Chris Lattnerc961eea2005-11-16 01:54:32 +0000125 virtual const char *getPassName() const {
126 return "X86 DAG->DAG Instruction Selection";
127 }
128
129 /// InstructionSelectBasicBlock - This callback is invoked by
130 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
131 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
132
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000133 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
134
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000135 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000136
Chris Lattnerc961eea2005-11-16 01:54:32 +0000137// Include the pieces autogenerated from the target description.
138#include "X86GenDAGISel.inc"
139
140 private:
Evan Cheng9ade2182006-08-26 05:34:46 +0000141 SDNode *Select(SDOperand N);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000142
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000143 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM,
Anton Korobeynikovf6e93532007-03-28 18:38:33 +0000144 bool isRoot = true, unsigned Depth = 0);
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000145 bool MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
146 bool isRoot, unsigned Depth);
Evan Cheng0d538262006-11-08 20:34:28 +0000147 bool SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
148 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
149 bool SelectLEAAddr(SDOperand Op, SDOperand N, SDOperand &Base,
150 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
151 bool SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Cheng07e4b002006-10-16 06:34:55 +0000152 SDOperand N, SDOperand &Base, SDOperand &Scale,
Evan Cheng82a91642006-10-11 21:06:01 +0000153 SDOperand &Index, SDOperand &Disp,
154 SDOperand &InChain, SDOperand &OutChain);
Evan Cheng5e351682006-02-06 06:02:33 +0000155 bool TryFoldLoad(SDOperand P, SDOperand N,
156 SDOperand &Base, SDOperand &Scale,
Evan Cheng0114e942006-01-06 20:36:21 +0000157 SDOperand &Index, SDOperand &Disp);
Evan Cheng70e674e2006-08-28 20:10:17 +0000158 void InstructionSelectPreprocess(SelectionDAG &DAG);
Evan Cheng2ef88a02006-08-07 22:28:20 +0000159
Chris Lattnerc0bad572006-06-08 18:03:49 +0000160 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
161 /// inline asm expressions.
162 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
163 char ConstraintCode,
164 std::vector<SDOperand> &OutOps,
165 SelectionDAG &DAG);
166
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000167 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
168
Evan Chenge5280532005-12-12 21:49:40 +0000169 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
170 SDOperand &Scale, SDOperand &Index,
171 SDOperand &Disp) {
172 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000173 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
174 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000175 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000176 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000177 // These are 32-bit even in 64-bit mode since RIP relative offset
178 // is 32-bit.
179 if (AM.GV)
180 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
181 else if (AM.CP)
182 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp);
183 else if (AM.ES)
184 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
185 else if (AM.JT != -1)
186 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
187 else
188 Disp = getI32Imm(AM.Disp);
Evan Chenge5280532005-12-12 21:49:40 +0000189 }
190
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000191 /// getI8Imm - Return a target constant with the specified value, of type
192 /// i8.
193 inline SDOperand getI8Imm(unsigned Imm) {
194 return CurDAG->getTargetConstant(Imm, MVT::i8);
195 }
196
Chris Lattnerc961eea2005-11-16 01:54:32 +0000197 /// getI16Imm - Return a target constant with the specified value, of type
198 /// i16.
199 inline SDOperand getI16Imm(unsigned Imm) {
200 return CurDAG->getTargetConstant(Imm, MVT::i16);
201 }
202
203 /// getI32Imm - Return a target constant with the specified value, of type
204 /// i32.
205 inline SDOperand getI32Imm(unsigned Imm) {
206 return CurDAG->getTargetConstant(Imm, MVT::i32);
207 }
Evan Chengf597dc72006-02-10 22:24:32 +0000208
Evan Cheng7ccced62006-02-18 00:15:05 +0000209 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
210 /// base register. Return the virtual register that holds this value.
Evan Cheng9ade2182006-08-26 05:34:46 +0000211 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000212
Christopher Lambc59e5212007-08-10 21:48:46 +0000213 /// getTruncate - return an SDNode that implements a subreg based truncate
214 /// of the specified operand to the the specified value type.
215 SDNode *getTruncate(SDOperand N0, MVT::ValueType VT);
216
Evan Cheng23addc02006-02-10 22:46:26 +0000217#ifndef NDEBUG
218 unsigned Indent;
219#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000220 };
221}
222
Evan Chenga275ecb2006-10-10 01:46:56 +0000223static SDNode *findFlagUse(SDNode *N) {
224 unsigned FlagResNo = N->getNumValues()-1;
225 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
226 SDNode *User = *I;
227 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
228 SDOperand Op = User->getOperand(i);
Evan Cheng494cec62006-10-12 19:13:59 +0000229 if (Op.Val == N && Op.ResNo == FlagResNo)
Evan Chenga275ecb2006-10-10 01:46:56 +0000230 return User;
231 }
232 }
233 return NULL;
234}
235
Evan Cheng27e1fe92006-10-14 08:33:25 +0000236static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
237 SDNode *Root, SDNode *Skip, bool &found,
Evan Chengf4b4c412006-08-08 00:31:00 +0000238 std::set<SDNode *> &Visited) {
239 if (found ||
240 Use->getNodeId() > Def->getNodeId() ||
241 !Visited.insert(Use).second)
242 return;
243
Evan Cheng27e1fe92006-10-14 08:33:25 +0000244 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000245 SDNode *N = Use->getOperand(i).Val;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000246 if (N == Skip)
Evan Chenga275ecb2006-10-10 01:46:56 +0000247 continue;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000248 if (N == Def) {
249 if (Use == ImmedUse)
250 continue; // Immediate use is ok.
251 if (Use == Root) {
252 assert(Use->getOpcode() == ISD::STORE ||
253 Use->getOpcode() == X86ISD::CMP);
254 continue;
255 }
Evan Chengf4b4c412006-08-08 00:31:00 +0000256 found = true;
257 break;
258 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000259 findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000260 }
261}
262
Evan Cheng27e1fe92006-10-14 08:33:25 +0000263/// isNonImmUse - Start searching from Root up the DAG to check is Def can
264/// be reached. Return true if that's the case. However, ignore direct uses
265/// by ImmedUse (which would be U in the example illustrated in
266/// CanBeFoldedBy) and by Root (which can happen in the store case).
267/// FIXME: to be really generic, we should allow direct use by any node
268/// that is being folded. But realisticly since we only fold loads which
269/// have one non-chain use, we only need to watch out for load/op/store
270/// and load/op/cmp case where the root (store / cmp) may reach the load via
271/// its chain operand.
272static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
273 SDNode *Skip = NULL) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000274 std::set<SDNode *> Visited;
275 bool found = false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000276 findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000277 return found;
278}
279
280
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000281bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
Evan Cheng27e1fe92006-10-14 08:33:25 +0000282 if (FastISel) return false;
283
Evan Chenga8df1b42006-07-27 16:44:36 +0000284 // If U use can somehow reach N through another path then U can't fold N or
285 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Cheng37e18032006-07-28 06:33:41 +0000286 // through X. If N is folded into into U, then X is both a predecessor and
Evan Chenga8df1b42006-07-27 16:44:36 +0000287 // a successor of U.
288 //
289 // [ N ]
290 // ^ ^
291 // | |
292 // / \---
293 // / [X]
294 // | ^
295 // [U]--------|
Evan Cheng27e1fe92006-10-14 08:33:25 +0000296
297 if (isNonImmUse(Root, N, U))
298 return false;
299
300 // If U produces a flag, then it gets (even more) interesting. Since it
301 // would have been "glued" together with its flag use, we need to check if
302 // it might reach N:
303 //
304 // [ N ]
305 // ^ ^
306 // | |
307 // [U] \--
308 // ^ [TF]
309 // | ^
310 // | |
311 // \ /
312 // [FU]
313 //
314 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
315 // NU), then TF is a predecessor of FU and a successor of NU. But since
316 // NU and FU are flagged together, this effectively creates a cycle.
317 bool HasFlagUse = false;
318 MVT::ValueType VT = Root->getValueType(Root->getNumValues()-1);
319 while ((VT == MVT::Flag && !Root->use_empty())) {
320 SDNode *FU = findFlagUse(Root);
321 if (FU == NULL)
322 break;
323 else {
324 Root = FU;
325 HasFlagUse = true;
Evan Chenga275ecb2006-10-10 01:46:56 +0000326 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000327 VT = Root->getValueType(Root->getNumValues()-1);
Evan Chenga275ecb2006-10-10 01:46:56 +0000328 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000329
330 if (HasFlagUse)
331 return !isNonImmUse(Root, N, Root, U);
332 return true;
Evan Chenga8df1b42006-07-27 16:44:36 +0000333}
334
Evan Cheng70e674e2006-08-28 20:10:17 +0000335/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
336/// and move load below the TokenFactor. Replace store's chain operand with
337/// load's chain result.
338static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
339 SDOperand Store, SDOperand TF) {
340 std::vector<SDOperand> Ops;
341 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
342 if (Load.Val == TF.Val->getOperand(i).Val)
343 Ops.push_back(Load.Val->getOperand(0));
344 else
345 Ops.push_back(TF.Val->getOperand(i));
346 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
347 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
348 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
349 Store.getOperand(2), Store.getOperand(3));
350}
351
352/// InstructionSelectPreprocess - Preprocess the DAG to allow the instruction
353/// selector to pick more load-modify-store instructions. This is a common
354/// case:
355///
356/// [Load chain]
357/// ^
358/// |
359/// [Load]
360/// ^ ^
361/// | |
362/// / \-
363/// / |
364/// [TokenFactor] [Op]
365/// ^ ^
366/// | |
367/// \ /
368/// \ /
369/// [Store]
370///
371/// The fact the store's chain operand != load's chain will prevent the
372/// (store (op (load))) instruction from being selected. We can transform it to:
373///
374/// [Load chain]
375/// ^
376/// |
377/// [TokenFactor]
378/// ^
379/// |
380/// [Load]
381/// ^ ^
382/// | |
383/// | \-
384/// | |
385/// | [Op]
386/// | ^
387/// | |
388/// \ /
389/// \ /
390/// [Store]
391void X86DAGToDAGISel::InstructionSelectPreprocess(SelectionDAG &DAG) {
392 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
393 E = DAG.allnodes_end(); I != E; ++I) {
Evan Cheng8b2794a2006-10-13 21:14:26 +0000394 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000395 continue;
396 SDOperand Chain = I->getOperand(0);
397 if (Chain.Val->getOpcode() != ISD::TokenFactor)
398 continue;
399
400 SDOperand N1 = I->getOperand(1);
401 SDOperand N2 = I->getOperand(2);
Evan Cheng1453de52006-09-01 22:52:28 +0000402 if (MVT::isFloatingPoint(N1.getValueType()) ||
403 MVT::isVector(N1.getValueType()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000404 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000405 continue;
406
407 bool RModW = false;
408 SDOperand Load;
409 unsigned Opcode = N1.Val->getOpcode();
410 switch (Opcode) {
411 case ISD::ADD:
412 case ISD::MUL:
Evan Cheng70e674e2006-08-28 20:10:17 +0000413 case ISD::AND:
414 case ISD::OR:
415 case ISD::XOR:
416 case ISD::ADDC:
417 case ISD::ADDE: {
418 SDOperand N10 = N1.getOperand(0);
419 SDOperand N11 = N1.getOperand(1);
Evan Cheng466685d2006-10-09 20:57:25 +0000420 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng70e674e2006-08-28 20:10:17 +0000421 RModW = true;
Evan Cheng466685d2006-10-09 20:57:25 +0000422 else if (ISD::isNON_EXTLoad(N11.Val)) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000423 RModW = true;
424 std::swap(N10, N11);
425 }
426 RModW = RModW && N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000427 (N10.getOperand(1) == N2) &&
428 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000429 if (RModW)
430 Load = N10;
431 break;
432 }
433 case ISD::SUB:
434 case ISD::SHL:
435 case ISD::SRA:
436 case ISD::SRL:
437 case ISD::ROTL:
438 case ISD::ROTR:
439 case ISD::SUBC:
440 case ISD::SUBE:
441 case X86ISD::SHLD:
442 case X86ISD::SHRD: {
443 SDOperand N10 = N1.getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +0000444 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng70e674e2006-08-28 20:10:17 +0000445 RModW = N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000446 (N10.getOperand(1) == N2) &&
447 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000448 if (RModW)
449 Load = N10;
450 break;
451 }
452 }
453
Evan Cheng82a35b32006-08-29 06:44:17 +0000454 if (RModW) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000455 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000456 ++NumLoadMoved;
457 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000458 }
459}
460
Chris Lattnerc961eea2005-11-16 01:54:32 +0000461/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
462/// when it has created a SelectionDAG for us to codegen.
463void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
464 DEBUG(BB->dump());
Chris Lattner92cb0af2006-01-11 01:15:34 +0000465 MachineFunction::iterator FirstMBB = BB;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000466
Evan Chenge50794a2006-08-29 18:28:33 +0000467 if (!FastISel)
Evan Cheng70e674e2006-08-28 20:10:17 +0000468 InstructionSelectPreprocess(DAG);
469
Chris Lattnerc961eea2005-11-16 01:54:32 +0000470 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000471#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000472 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000473 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000474#endif
Evan Chengba2f0a92006-02-05 06:46:41 +0000475 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengf597dc72006-02-10 22:24:32 +0000476#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000477 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000478#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000479
Chris Lattnerc961eea2005-11-16 01:54:32 +0000480 DAG.RemoveDeadNodes();
481
482 // Emit machine code to BB.
483 ScheduleAndEmitDAG(DAG);
Chris Lattner92cb0af2006-01-11 01:15:34 +0000484
485 // If we are emitting FP stack code, scan the basic block to determine if this
486 // block defines any FP values. If so, put an FP_REG_KILL instruction before
487 // the terminator of the block.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000488
Dale Johannesen48d1e452007-09-24 22:52:39 +0000489 // Note that FP stack instructions are used in all modes for long double,
490 // so we always need to do this check.
491 // Also note that it's possible for an FP stack register to be live across
492 // an instruction that produces multiple basic blocks (SSE CMOV) so we
493 // must check all the generated basic blocks.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000494
495 // Scan all of the machine instructions in these MBBs, checking for FP
496 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
497 MachineFunction::iterator MBBI = FirstMBB;
498 do {
Dale Johannesen48d1e452007-09-24 22:52:39 +0000499 bool ContainsFPCode = false;
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000500 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
501 !ContainsFPCode && I != E; ++I) {
502 if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) {
503 const TargetRegisterClass *clas;
504 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
505 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
506 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
507 ((clas = RegMap->getRegClass(I->getOperand(0).getReg())) ==
508 X86::RFP32RegisterClass ||
509 clas == X86::RFP64RegisterClass ||
510 clas == X86::RFP80RegisterClass)) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000511 ContainsFPCode = true;
512 break;
513 }
514 }
515 }
516 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000517 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
518 // a copy of the input value in this block. In SSE mode, we only care about
519 // 80-bit values.
520 if (!ContainsFPCode) {
521 // Final check, check LLVM BB's that are successors to the LLVM BB
522 // corresponding to BB for FP PHI nodes.
523 const BasicBlock *LLVMBB = BB->getBasicBlock();
524 const PHINode *PN;
525 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
526 !ContainsFPCode && SI != E; ++SI) {
527 for (BasicBlock::const_iterator II = SI->begin();
528 (PN = dyn_cast<PHINode>(II)); ++II) {
529 if (PN->getType()==Type::X86_FP80Ty ||
530 (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) ||
531 (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) {
532 ContainsFPCode = true;
533 break;
534 }
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000535 }
536 }
Chris Lattner92cb0af2006-01-11 01:15:34 +0000537 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000538 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
539 if (ContainsFPCode) {
540 BuildMI(*MBBI, MBBI->getFirstTerminator(),
541 TM.getInstrInfo()->get(X86::FP_REG_KILL));
542 ++NumFPKill;
543 }
544 } while (&*(MBBI++) != BB);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000545}
546
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000547/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
548/// the main function.
549void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
550 MachineFrameInfo *MFI) {
551 const TargetInstrInfo *TII = TM.getInstrInfo();
552 if (Subtarget->isTargetCygMing())
553 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
554}
555
556void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
557 // If this is main, emit special code for main.
558 MachineBasicBlock *BB = MF.begin();
559 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
560 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
561}
562
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000563/// MatchAddress - Add the specified node to the specified addressing mode,
564/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000565/// addressing mode.
Evan Cheng2486af12006-02-11 02:05:36 +0000566bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000567 bool isRoot, unsigned Depth) {
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000568 // Limit recursion.
569 if (Depth > 5)
570 return MatchAddressBase(N, AM, isRoot, Depth);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000571
Evan Cheng25ab6902006-09-08 06:48:29 +0000572 // RIP relative addressing: %rip + 32-bit displacement!
573 if (AM.isRIPRel) {
574 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000575 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000576 if (isInt32(AM.Disp + Val)) {
577 AM.Disp += Val;
578 return false;
579 }
580 }
581 return true;
582 }
583
Evan Cheng2ef88a02006-08-07 22:28:20 +0000584 int id = N.Val->getNodeId();
Evan Cheng1314b002007-12-13 00:43:27 +0000585 bool AlreadySelected = isSelected(id); // Already selected, not yet replaced.
Evan Cheng2486af12006-02-11 02:05:36 +0000586
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000587 switch (N.getOpcode()) {
588 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000589 case ISD::Constant: {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000590 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000591 if (isInt32(AM.Disp + Val)) {
592 AM.Disp += Val;
593 return false;
594 }
595 break;
596 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000597
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000598 case X86ISD::Wrapper: {
599 bool is64Bit = Subtarget->is64Bit();
Evan Cheng0085a282006-11-30 21:55:46 +0000600 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000601 if (is64Bit && TM.getCodeModel() != CodeModel::Small)
Evan Cheng0085a282006-11-30 21:55:46 +0000602 break;
Evan Cheng28b514392006-12-05 19:50:18 +0000603 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
604 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000605 // If value is available in a register both base and index components have
606 // been picked, we can't fit the result available in the register in the
607 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Evan Cheng1314b002007-12-13 00:43:27 +0000608 if (!AlreadySelected || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
Evan Cheng28b514392006-12-05 19:50:18 +0000609 bool isStatic = TM.getRelocationModel() == Reloc::Static;
610 SDOperand N0 = N.getOperand(0);
Evan Cheng518143d2007-07-26 07:35:15 +0000611 // Mac OS X X86-64 lower 4G address is not available.
Evan Chengf6844ca2007-08-01 23:45:51 +0000612 bool isAbs32 = !is64Bit ||
613 (isStatic && Subtarget->hasLow4GUserSpaceAddress());
Evan Cheng28b514392006-12-05 19:50:18 +0000614 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
615 GlobalValue *GV = G->getGlobal();
Evan Cheng28b514392006-12-05 19:50:18 +0000616 if (isAbs32 || isRoot) {
Evan Chenga70d14b2006-12-19 21:31:42 +0000617 AM.GV = GV;
Evan Cheng28b514392006-12-05 19:50:18 +0000618 AM.Disp += G->getOffset();
619 AM.isRIPRel = !isAbs32;
620 return false;
621 }
622 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Evan Cheng518143d2007-07-26 07:35:15 +0000623 if (isAbs32 || isRoot) {
Evan Chengc356a572006-09-12 21:04:05 +0000624 AM.CP = CP->getConstVal();
Evan Cheng51a9ed92006-02-25 10:09:08 +0000625 AM.Align = CP->getAlignment();
626 AM.Disp += CP->getOffset();
Evan Chengcf5543c2007-07-26 17:02:45 +0000627 AM.isRIPRel = !isAbs32;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000628 return false;
629 }
Evan Cheng28b514392006-12-05 19:50:18 +0000630 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Cheng518143d2007-07-26 07:35:15 +0000631 if (isAbs32 || isRoot) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000632 AM.ES = S->getSymbol();
Evan Chengcf5543c2007-07-26 17:02:45 +0000633 AM.isRIPRel = !isAbs32;
Evan Cheng25ab6902006-09-08 06:48:29 +0000634 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000635 }
636 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Cheng518143d2007-07-26 07:35:15 +0000637 if (isAbs32 || isRoot) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000638 AM.JT = J->getIndex();
Evan Chengcf5543c2007-07-26 17:02:45 +0000639 AM.isRIPRel = !isAbs32;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000640 return false;
641 }
642 }
643 }
644 break;
Evan Cheng0085a282006-11-30 21:55:46 +0000645 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000646
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000647 case ISD::FrameIndex:
648 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
649 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
650 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
651 return false;
652 }
653 break;
Evan Chengec693f72005-12-08 02:01:35 +0000654
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000655 case ISD::SHL:
Evan Cheng1314b002007-12-13 00:43:27 +0000656 if (AlreadySelected || AM.IndexReg.Val != 0 || AM.Scale != 1)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000657 break;
658
659 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
660 unsigned Val = CN->getValue();
661 if (Val == 1 || Val == 2 || Val == 3) {
662 AM.Scale = 1 << Val;
663 SDOperand ShVal = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000664
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000665 // Okay, we know that we have a scale by now. However, if the scaled
666 // value is an add of something and a constant, we can fold the
667 // constant into the disp field here.
668 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
669 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
670 AM.IndexReg = ShVal.Val->getOperand(0);
671 ConstantSDNode *AddVal =
672 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
673 uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
674 if (isInt32(Disp))
675 AM.Disp = Disp;
676 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000677 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000678 } else {
679 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000680 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000681 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000682 }
683 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000684 }
Evan Chengec693f72005-12-08 02:01:35 +0000685
Dan Gohman83688052007-10-22 20:22:24 +0000686 case ISD::SMUL_LOHI:
687 case ISD::UMUL_LOHI:
688 // A mul_lohi where we need the low part can be folded as a plain multiply.
689 if (N.ResNo != 0) break;
690 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000691 case ISD::MUL:
692 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng1314b002007-12-13 00:43:27 +0000693 if (!AlreadySelected &&
Evan Cheng51a9ed92006-02-25 10:09:08 +0000694 AM.BaseType == X86ISelAddressMode::RegBase &&
695 AM.Base.Reg.Val == 0 &&
Chris Lattner62412262007-02-04 20:18:17 +0000696 AM.IndexReg.Val == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000697 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
698 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
699 AM.Scale = unsigned(CN->getValue())-1;
700
701 SDOperand MulVal = N.Val->getOperand(0);
702 SDOperand Reg;
703
704 // Okay, we know that we have a scale by now. However, if the scaled
705 // value is an add of something and a constant, we can fold the
706 // constant into the disp field here.
707 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
708 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
709 Reg = MulVal.Val->getOperand(0);
710 ConstantSDNode *AddVal =
711 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
Evan Cheng25ab6902006-09-08 06:48:29 +0000712 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
713 if (isInt32(Disp))
714 AM.Disp = Disp;
715 else
716 Reg = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000717 } else {
718 Reg = N.Val->getOperand(0);
719 }
720
721 AM.IndexReg = AM.Base.Reg = Reg;
722 return false;
723 }
Chris Lattner62412262007-02-04 20:18:17 +0000724 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000725 break;
726
Chris Lattner62412262007-02-04 20:18:17 +0000727 case ISD::ADD:
Evan Cheng1314b002007-12-13 00:43:27 +0000728 if (!AlreadySelected) {
Evan Cheng2486af12006-02-11 02:05:36 +0000729 X86ISelAddressMode Backup = AM;
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000730 if (!MatchAddress(N.Val->getOperand(0), AM, false, Depth+1) &&
731 !MatchAddress(N.Val->getOperand(1), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000732 return false;
733 AM = Backup;
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000734 if (!MatchAddress(N.Val->getOperand(1), AM, false, Depth+1) &&
735 !MatchAddress(N.Val->getOperand(0), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000736 return false;
737 AM = Backup;
738 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000739 break;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000740
Chris Lattner62412262007-02-04 20:18:17 +0000741 case ISD::OR:
742 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Evan Cheng1314b002007-12-13 00:43:27 +0000743 if (AlreadySelected) break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000744
745 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
746 X86ISelAddressMode Backup = AM;
747 // Start with the LHS as an addr mode.
748 if (!MatchAddress(N.getOperand(0), AM, false) &&
749 // Address could not have picked a GV address for the displacement.
750 AM.GV == NULL &&
751 // On x86-64, the resultant disp must fit in 32-bits.
752 isInt32(AM.Disp + CN->getSignExtended()) &&
753 // Check to see if the LHS & C is zero.
754 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getValue())) {
755 AM.Disp += CN->getValue();
756 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000757 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000758 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000759 }
760 break;
Evan Cheng1314b002007-12-13 00:43:27 +0000761
762 case ISD::AND: {
763 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
764 // allows us to fold the shift into this addressing mode.
765 if (AlreadySelected) break;
766 SDOperand Shift = N.getOperand(0);
767 if (Shift.getOpcode() != ISD::SHL) break;
768
769 // Scale must not be used already.
770 if (AM.IndexReg.Val != 0 || AM.Scale != 1) break;
771
772 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
773 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
774 if (!C1 || !C2) break;
775
776 // Not likely to be profitable if either the AND or SHIFT node has more
777 // than one use (unless all uses are for address computation). Besides,
778 // isel mechanism requires their node ids to be reused.
779 if (!N.hasOneUse() || !Shift.hasOneUse())
780 break;
781
782 // Verify that the shift amount is something we can fold.
783 unsigned ShiftCst = C1->getValue();
784 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
785 break;
786
787 // Get the new AND mask, this folds to a constant.
788 SDOperand NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
789 SDOperand(C2, 0), SDOperand(C1, 0));
790 SDOperand NewAND = CurDAG->getNode(ISD::AND, N.getValueType(),
791 Shift.getOperand(0), NewANDMask);
792 NewANDMask.Val->setNodeId(Shift.Val->getNodeId());
793 NewAND.Val->setNodeId(N.Val->getNodeId());
794
795 AM.Scale = 1 << ShiftCst;
796 AM.IndexReg = NewAND;
797 return false;
798 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000799 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000800
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000801 return MatchAddressBase(N, AM, isRoot, Depth);
802}
803
804/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
805/// specified addressing mode without any further recursion.
806bool X86DAGToDAGISel::MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
807 bool isRoot, unsigned Depth) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000808 // Is the base register already occupied?
809 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
810 // If so, check to see if the scale index register is set.
811 if (AM.IndexReg.Val == 0) {
812 AM.IndexReg = N;
813 AM.Scale = 1;
814 return false;
815 }
816
817 // Otherwise, we cannot select it.
818 return true;
819 }
820
821 // Default, generate it as a register.
822 AM.BaseType = X86ISelAddressMode::RegBase;
823 AM.Base.Reg = N;
824 return false;
825}
826
Evan Chengec693f72005-12-08 02:01:35 +0000827/// SelectAddr - returns true if it is able pattern match an addressing mode.
828/// It returns the operands which make up the maximal addressing mode it can
829/// match by reference.
Evan Cheng0d538262006-11-08 20:34:28 +0000830bool X86DAGToDAGISel::SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
831 SDOperand &Scale, SDOperand &Index,
832 SDOperand &Disp) {
Evan Chengec693f72005-12-08 02:01:35 +0000833 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +0000834 if (MatchAddress(N, AM))
835 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000836
Evan Cheng25ab6902006-09-08 06:48:29 +0000837 MVT::ValueType VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +0000838 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Cheng7dd281b2006-02-05 05:25:07 +0000839 if (!AM.Base.Reg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000840 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +0000841 }
Evan Cheng8700e142006-01-11 06:09:51 +0000842
Evan Cheng7dd281b2006-02-05 05:25:07 +0000843 if (!AM.IndexReg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000844 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +0000845
846 getAddressOperands(AM, Base, Scale, Index, Disp);
847 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000848}
849
Chris Lattner4fe4f252006-10-11 22:09:58 +0000850/// isZeroNode - Returns true if Elt is a constant zero or a floating point
851/// constant +0.0.
852static inline bool isZeroNode(SDOperand Elt) {
853 return ((isa<ConstantSDNode>(Elt) &&
854 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
855 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +0000856 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Chris Lattner4fe4f252006-10-11 22:09:58 +0000857}
858
859
Chris Lattner3a7cd952006-10-07 21:55:32 +0000860/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
861/// match a load whose top elements are either undef or zeros. The load flavor
862/// is derived from the type of N, which is either v4f32 or v2f64.
Evan Cheng0d538262006-11-08 20:34:28 +0000863bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Cheng07e4b002006-10-16 06:34:55 +0000864 SDOperand N, SDOperand &Base,
Evan Cheng82a91642006-10-11 21:06:01 +0000865 SDOperand &Scale, SDOperand &Index,
866 SDOperand &Disp, SDOperand &InChain,
867 SDOperand &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +0000868 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +0000869 InChain = N.getOperand(0).getValue(1);
Evan Cheng07e4b002006-10-16 06:34:55 +0000870 if (ISD::isNON_EXTLoad(InChain.Val) &&
871 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +0000872 N.hasOneUse() &&
Evan Cheng0d538262006-11-08 20:34:28 +0000873 CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
Evan Cheng82a91642006-10-11 21:06:01 +0000874 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Evan Cheng0d538262006-11-08 20:34:28 +0000875 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner3a7cd952006-10-07 21:55:32 +0000876 return false;
Evan Cheng82a91642006-10-11 21:06:01 +0000877 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +0000878 return true;
879 }
880 }
Chris Lattner4fe4f252006-10-11 22:09:58 +0000881
882 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +0000883 // elements. This is a vector shuffle from the zero vector.
Chris Lattner4fe4f252006-10-11 22:09:58 +0000884 if (N.getOpcode() == ISD::VECTOR_SHUFFLE && N.Val->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +0000885 // Check to see if the top elements are all zeros (or bitcast of zeros).
886 ISD::isBuildVectorAllZeros(N.getOperand(0).Val) &&
Chris Lattner4fe4f252006-10-11 22:09:58 +0000887 N.getOperand(1).getOpcode() == ISD::SCALAR_TO_VECTOR &&
888 N.getOperand(1).Val->hasOneUse() &&
889 ISD::isNON_EXTLoad(N.getOperand(1).getOperand(0).Val) &&
890 N.getOperand(1).getOperand(0).hasOneUse()) {
Chris Lattner4fe4f252006-10-11 22:09:58 +0000891 // Check to see if the shuffle mask is 4/L/L/L or 2/L, where L is something
892 // from the LHS.
Chris Lattner8a594482007-11-25 00:24:49 +0000893 unsigned VecWidth=MVT::getVectorNumElements(N.getOperand(0).getValueType());
Chris Lattner4fe4f252006-10-11 22:09:58 +0000894 SDOperand ShufMask = N.getOperand(2);
895 assert(ShufMask.getOpcode() == ISD::BUILD_VECTOR && "Invalid shuf mask!");
896 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(ShufMask.getOperand(0))) {
897 if (C->getValue() == VecWidth) {
898 for (unsigned i = 1; i != VecWidth; ++i) {
899 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF) {
900 // ok.
901 } else {
902 ConstantSDNode *C = cast<ConstantSDNode>(ShufMask.getOperand(i));
903 if (C->getValue() >= VecWidth) return false;
904 }
905 }
906 }
907
908 // Okay, this is a zero extending load. Fold it.
909 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(1).getOperand(0));
Evan Cheng0d538262006-11-08 20:34:28 +0000910 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner4fe4f252006-10-11 22:09:58 +0000911 return false;
912 OutChain = LD->getChain();
913 InChain = SDOperand(LD, 1);
914 return true;
915 }
916 }
Chris Lattner3a7cd952006-10-07 21:55:32 +0000917 return false;
918}
919
920
Evan Cheng51a9ed92006-02-25 10:09:08 +0000921/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
922/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng0d538262006-11-08 20:34:28 +0000923bool X86DAGToDAGISel::SelectLEAAddr(SDOperand Op, SDOperand N,
924 SDOperand &Base, SDOperand &Scale,
Evan Cheng51a9ed92006-02-25 10:09:08 +0000925 SDOperand &Index, SDOperand &Disp) {
926 X86ISelAddressMode AM;
927 if (MatchAddress(N, AM))
928 return false;
929
Evan Cheng25ab6902006-09-08 06:48:29 +0000930 MVT::ValueType VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +0000931 unsigned Complexity = 0;
932 if (AM.BaseType == X86ISelAddressMode::RegBase)
933 if (AM.Base.Reg.Val)
934 Complexity = 1;
935 else
Evan Cheng25ab6902006-09-08 06:48:29 +0000936 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +0000937 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
938 Complexity = 4;
939
940 if (AM.IndexReg.Val)
941 Complexity++;
942 else
Evan Cheng25ab6902006-09-08 06:48:29 +0000943 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +0000944
Chris Lattnera16b7cb2007-03-20 06:08:29 +0000945 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
946 // a simple shift.
947 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +0000948 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000949
950 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
951 // to a LEA. This is determined with some expermentation but is by no means
952 // optimal (especially for code size consideration). LEA is nice because of
953 // its three-address nature. Tweak the cost function again when we can run
954 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +0000955 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
956 // For X86-64, we should always use lea to materialize RIP relative
957 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +0000958 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +0000959 Complexity = 4;
960 else
961 Complexity += 2;
962 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000963
964 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
965 Complexity++;
966
967 if (Complexity > 2) {
968 getAddressOperands(AM, Base, Scale, Index, Disp);
969 return true;
970 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000971 return false;
972}
973
Evan Cheng5e351682006-02-06 06:02:33 +0000974bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
975 SDOperand &Base, SDOperand &Scale,
976 SDOperand &Index, SDOperand &Disp) {
Evan Cheng466685d2006-10-09 20:57:25 +0000977 if (ISD::isNON_EXTLoad(N.Val) &&
Evan Cheng5e351682006-02-06 06:02:33 +0000978 N.hasOneUse() &&
Evan Cheng27e1fe92006-10-14 08:33:25 +0000979 CanBeFoldedBy(N.Val, P.Val, P.Val))
Evan Cheng0d538262006-11-08 20:34:28 +0000980 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
Evan Cheng0114e942006-01-06 20:36:21 +0000981 return false;
982}
983
Evan Cheng7ccced62006-02-18 00:15:05 +0000984/// getGlobalBaseReg - Output the instructions required to put the
985/// base address to use for accessing globals into a register.
986///
Evan Cheng9ade2182006-08-26 05:34:46 +0000987SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng25ab6902006-09-08 06:48:29 +0000988 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Evan Cheng7ccced62006-02-18 00:15:05 +0000989 if (!GlobalBaseReg) {
990 // Insert the set of GlobalBaseReg into the first MBB of the function
991 MachineBasicBlock &FirstMBB = BB->getParent()->front();
992 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
993 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000994 unsigned PC = RegMap->createVirtualRegister(X86::GR32RegisterClass);
995
Evan Chengc0f64ff2006-11-27 23:37:22 +0000996 const TargetInstrInfo *TII = TM.getInstrInfo();
Evan Chengf02ca692007-12-22 02:26:46 +0000997 // Operand of MovePCtoStack is completely ignored by asm printer. It's
998 // only used in JIT code emission as displacement to pc.
999 BuildMI(FirstMBB, MBBI, TII->get(X86::MovePCtoStack)).addImm(0);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001000 BuildMI(FirstMBB, MBBI, TII->get(X86::POP32r), PC);
1001
1002 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
1003 // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
Evan Cheng706535d2007-01-22 21:34:25 +00001004 if (TM.getRelocationModel() == Reloc::PIC_ &&
1005 Subtarget->isPICStyleGOT()) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001006 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
1007 BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg).
1008 addReg(PC).
1009 addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
1010 } else {
1011 GlobalBaseReg = PC;
1012 }
1013
Evan Cheng7ccced62006-02-18 00:15:05 +00001014 }
Evan Cheng25ab6902006-09-08 06:48:29 +00001015 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
Evan Cheng7ccced62006-02-18 00:15:05 +00001016}
1017
Evan Chengb245d922006-05-20 01:36:52 +00001018static SDNode *FindCallStartFromCall(SDNode *Node) {
1019 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1020 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1021 "Node doesn't have a token chain argument!");
1022 return FindCallStartFromCall(Node->getOperand(0).Val);
1023}
1024
Christopher Lambc59e5212007-08-10 21:48:46 +00001025SDNode *X86DAGToDAGISel::getTruncate(SDOperand N0, MVT::ValueType VT) {
1026 SDOperand SRIdx;
1027 switch (VT) {
1028 case MVT::i8:
1029 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1030 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1031 if (!Subtarget->is64Bit()) {
1032 unsigned Opc;
1033 MVT::ValueType VT;
1034 switch (N0.getValueType()) {
1035 default: assert(0 && "Unknown truncate!");
1036 case MVT::i16:
1037 Opc = X86::MOV16to16_;
1038 VT = MVT::i16;
1039 break;
1040 case MVT::i32:
1041 Opc = X86::MOV32to32_;
1042 VT = MVT::i32;
1043 break;
1044 }
Evan Cheng96aaa542007-10-12 07:55:53 +00001045 N0 = SDOperand(CurDAG->getTargetNode(Opc, VT, MVT::Flag, N0), 0);
1046 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1047 VT, N0, SRIdx, N0.getValue(1));
Christopher Lambc59e5212007-08-10 21:48:46 +00001048 }
1049 break;
1050 case MVT::i16:
1051 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1052 break;
1053 case MVT::i32:
1054 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1055 break;
Evan Cheng96aaa542007-10-12 07:55:53 +00001056 default: assert(0 && "Unknown truncate!"); break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001057 }
Evan Cheng96aaa542007-10-12 07:55:53 +00001058 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, VT, N0, SRIdx);
Christopher Lambc59e5212007-08-10 21:48:46 +00001059}
1060
1061
Evan Cheng9ade2182006-08-26 05:34:46 +00001062SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Chengdef941b2005-12-15 01:02:48 +00001063 SDNode *Node = N.Val;
1064 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001065 unsigned Opc, MOpc;
1066 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +00001067
Evan Chengf597dc72006-02-10 22:24:32 +00001068#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001069 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001070 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001071 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001072 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001073#endif
1074
Evan Cheng34167212006-02-09 00:37:58 +00001075 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengf597dc72006-02-10 22:24:32 +00001076#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001077 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001078 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001079 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001080 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001081#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001082 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001083 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001084
Evan Cheng0114e942006-01-06 20:36:21 +00001085 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001086 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001087 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001088 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001089
Evan Cheng51a9ed92006-02-25 10:09:08 +00001090 case ISD::ADD: {
1091 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1092 // code and is matched first so to prevent it from being turned into
1093 // LEA32r X+c.
Evan Cheng25ab6902006-09-08 06:48:29 +00001094 // In 64-bit mode, use LEA to take advantage of RIP-relative addressing.
1095 MVT::ValueType PtrVT = TLI.getPointerTy();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001096 SDOperand N0 = N.getOperand(0);
1097 SDOperand N1 = N.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00001098 if (N.Val->getValueType(0) == PtrVT &&
Evan Cheng19f2ffc2006-12-05 04:01:03 +00001099 N0.getOpcode() == X86ISD::Wrapper &&
Evan Cheng51a9ed92006-02-25 10:09:08 +00001100 N1.getOpcode() == ISD::Constant) {
1101 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1102 SDOperand C(0, 0);
1103 // TODO: handle ExternalSymbolSDNode.
1104 if (GlobalAddressSDNode *G =
1105 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001106 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001107 G->getOffset() + Offset);
1108 } else if (ConstantPoolSDNode *CP =
1109 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Chengc356a572006-09-12 21:04:05 +00001110 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001111 CP->getAlignment(),
1112 CP->getOffset()+Offset);
1113 }
1114
Evan Cheng25ab6902006-09-08 06:48:29 +00001115 if (C.Val) {
1116 if (Subtarget->is64Bit()) {
1117 SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
1118 CurDAG->getRegister(0, PtrVT), C };
1119 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1120 } else
1121 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1122 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001123 }
1124
1125 // Other cases are handled by auto-generated code.
1126 break;
Evan Chenga0ea0532006-02-23 02:43:52 +00001127 }
Evan Cheng020d2e82006-02-23 20:41:18 +00001128
Dan Gohman525178c2007-10-08 18:33:35 +00001129 case ISD::SMUL_LOHI:
1130 case ISD::UMUL_LOHI: {
1131 SDOperand N0 = Node->getOperand(0);
1132 SDOperand N1 = Node->getOperand(1);
1133
Dan Gohman74f87a62007-10-09 15:44:37 +00001134 // There are several forms of IMUL that just return the low part and
1135 // don't have fixed-register operands. If we don't need the high part,
1136 // use these instead. They can be selected with the generated ISel code.
Dan Gohman525178c2007-10-08 18:33:35 +00001137 if (NVT != MVT::i8 &&
1138 N.getValue(1).use_empty()) {
1139 N = CurDAG->getNode(ISD::MUL, NVT, N0, N1);
1140 break;
1141 }
1142
1143 bool isSigned = Opcode == ISD::SMUL_LOHI;
1144 if (!isSigned)
Evan Cheng0114e942006-01-06 20:36:21 +00001145 switch (NVT) {
1146 default: assert(0 && "Unsupported VT!");
1147 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1148 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1149 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001150 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001151 }
1152 else
1153 switch (NVT) {
1154 default: assert(0 && "Unsupported VT!");
1155 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1156 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1157 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001158 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001159 }
1160
1161 unsigned LoReg, HiReg;
1162 switch (NVT) {
1163 default: assert(0 && "Unsupported VT!");
1164 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1165 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1166 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001167 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001168 }
1169
Evan Cheng0114e942006-01-06 20:36:21 +00001170 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng7afa1662007-08-02 05:48:35 +00001171 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman525178c2007-10-08 18:33:35 +00001172 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001173 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +00001174 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng7afa1662007-08-02 05:48:35 +00001175 if (foldedLoad)
1176 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001177 }
1178
Evan Cheng04699902006-08-26 01:05:16 +00001179 AddToISelQueue(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001180 SDOperand InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1181 N0, SDOperand()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001182
1183 if (foldedLoad) {
Dan Gohman525178c2007-10-08 18:33:35 +00001184 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001185 AddToISelQueue(Tmp0);
1186 AddToISelQueue(Tmp1);
1187 AddToISelQueue(Tmp2);
1188 AddToISelQueue(Tmp3);
Dan Gohman525178c2007-10-08 18:33:35 +00001189 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001190 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001191 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001192 InFlag = SDOperand(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001193 // Update the chain.
1194 ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001195 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001196 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001197 InFlag =
1198 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001199 }
1200
Dan Gohman525178c2007-10-08 18:33:35 +00001201 // Copy the low half of the result, if it is needed.
1202 if (!N.getValue(0).use_empty()) {
1203 SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1204 LoReg, NVT, InFlag);
1205 InFlag = Result.getValue(2);
1206 ReplaceUses(N.getValue(0), Result);
1207#ifndef NDEBUG
1208 DOUT << std::string(Indent-2, ' ') << "=> ";
1209 DEBUG(Result.Val->dump(CurDAG));
1210 DOUT << "\n";
1211#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001212 }
Dan Gohman525178c2007-10-08 18:33:35 +00001213 // Copy the high half of the result, if it is needed.
1214 if (!N.getValue(1).use_empty()) {
1215 SDOperand Result;
1216 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1217 // Prevent use of AH in a REX instruction by referencing AX instead.
1218 // Shift it down 8 bits.
1219 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1220 X86::AX, MVT::i16, InFlag);
1221 InFlag = Result.getValue(2);
1222 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1223 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1224 // Then truncate it down to i8.
1225 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1226 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1227 MVT::i8, Result, SRIdx), 0);
1228 } else {
1229 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1230 HiReg, NVT, InFlag);
1231 InFlag = Result.getValue(2);
1232 }
1233 ReplaceUses(N.getValue(1), Result);
1234#ifndef NDEBUG
1235 DOUT << std::string(Indent-2, ' ') << "=> ";
1236 DEBUG(Result.Val->dump(CurDAG));
1237 DOUT << "\n";
1238#endif
1239 }
Evan Cheng34167212006-02-09 00:37:58 +00001240
Evan Chengf597dc72006-02-10 22:24:32 +00001241#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001242 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001243#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001244
Evan Cheng64a752f2006-08-11 09:08:15 +00001245 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001246 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001247
Dan Gohman525178c2007-10-08 18:33:35 +00001248 case ISD::SDIVREM:
1249 case ISD::UDIVREM: {
1250 SDOperand N0 = Node->getOperand(0);
1251 SDOperand N1 = Node->getOperand(1);
1252
1253 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001254 if (!isSigned)
1255 switch (NVT) {
1256 default: assert(0 && "Unsupported VT!");
1257 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1258 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1259 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001260 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001261 }
1262 else
1263 switch (NVT) {
1264 default: assert(0 && "Unsupported VT!");
1265 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1266 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1267 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001268 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001269 }
1270
1271 unsigned LoReg, HiReg;
1272 unsigned ClrOpcode, SExtOpcode;
1273 switch (NVT) {
1274 default: assert(0 && "Unsupported VT!");
1275 case MVT::i8:
1276 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001277 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001278 SExtOpcode = X86::CBW;
1279 break;
1280 case MVT::i16:
1281 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001282 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001283 SExtOpcode = X86::CWD;
1284 break;
1285 case MVT::i32:
1286 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001287 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001288 SExtOpcode = X86::CDQ;
1289 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001290 case MVT::i64:
1291 LoReg = X86::RAX; HiReg = X86::RDX;
1292 ClrOpcode = X86::MOV64r0;
1293 SExtOpcode = X86::CQO;
1294 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001295 }
1296
Dan Gohman525178c2007-10-08 18:33:35 +00001297 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
1298 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1299
1300 SDOperand InFlag;
Evan Chengb1409ce2006-11-17 22:10:14 +00001301 if (NVT == MVT::i8 && !isSigned) {
1302 // Special case for div8, just use a move with zero extension to AX to
1303 // clear the upper 8 bits (AH).
1304 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
1305 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
1306 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
1307 AddToISelQueue(N0.getOperand(0));
1308 AddToISelQueue(Tmp0);
1309 AddToISelQueue(Tmp1);
1310 AddToISelQueue(Tmp2);
1311 AddToISelQueue(Tmp3);
1312 Move =
1313 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
1314 Ops, 5), 0);
1315 Chain = Move.getValue(1);
1316 ReplaceUses(N0.getValue(1), Chain);
1317 } else {
1318 AddToISelQueue(N0);
1319 Move =
1320 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
1321 Chain = CurDAG->getEntryNode();
1322 }
Dan Gohman525178c2007-10-08 18:33:35 +00001323 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDOperand());
Evan Cheng948f3432006-01-06 23:19:29 +00001324 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001325 } else {
1326 AddToISelQueue(N0);
1327 InFlag =
Dan Gohman525178c2007-10-08 18:33:35 +00001328 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
1329 LoReg, N0, SDOperand()).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001330 if (isSigned) {
1331 // Sign extend the low part into the high part.
1332 InFlag =
1333 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
1334 } else {
1335 // Zero out the high part, effectively zero extending the input.
1336 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001337 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1338 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001339 }
Evan Cheng948f3432006-01-06 23:19:29 +00001340 }
1341
1342 if (foldedLoad) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001343 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001344 AddToISelQueue(Tmp0);
1345 AddToISelQueue(Tmp1);
1346 AddToISelQueue(Tmp2);
1347 AddToISelQueue(Tmp3);
Evan Chengb1409ce2006-11-17 22:10:14 +00001348 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001349 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001350 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001351 InFlag = SDOperand(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001352 // Update the chain.
1353 ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001354 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001355 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001356 InFlag =
1357 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001358 }
1359
Dan Gohmana37c9f72007-09-25 18:23:27 +00001360 // Copy the division (low) result, if it is needed.
1361 if (!N.getValue(0).use_empty()) {
Dan Gohman525178c2007-10-08 18:33:35 +00001362 SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1363 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001364 InFlag = Result.getValue(2);
1365 ReplaceUses(N.getValue(0), Result);
1366#ifndef NDEBUG
1367 DOUT << std::string(Indent-2, ' ') << "=> ";
1368 DEBUG(Result.Val->dump(CurDAG));
1369 DOUT << "\n";
1370#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001371 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001372 // Copy the remainder (high) result, if it is needed.
1373 if (!N.getValue(1).use_empty()) {
1374 SDOperand Result;
1375 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1376 // Prevent use of AH in a REX instruction by referencing AX instead.
1377 // Shift it down 8 bits.
Dan Gohman525178c2007-10-08 18:33:35 +00001378 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1379 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001380 InFlag = Result.getValue(2);
1381 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1382 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1383 // Then truncate it down to i8.
1384 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1385 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1386 MVT::i8, Result, SRIdx), 0);
1387 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00001388 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1389 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001390 InFlag = Result.getValue(2);
1391 }
1392 ReplaceUses(N.getValue(1), Result);
1393#ifndef NDEBUG
1394 DOUT << std::string(Indent-2, ' ') << "=> ";
1395 DEBUG(Result.Val->dump(CurDAG));
1396 DOUT << "\n";
1397#endif
1398 }
Evan Chengf597dc72006-02-10 22:24:32 +00001399
1400#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001401 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001402#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001403
1404 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001405 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001406
1407 case ISD::ANY_EXTEND: {
1408 SDOperand N0 = Node->getOperand(0);
1409 AddToISelQueue(N0);
1410 if (NVT == MVT::i64 || NVT == MVT::i32 || NVT == MVT::i16) {
1411 SDOperand SRIdx;
1412 switch(N0.getValueType()) {
1413 case MVT::i32:
1414 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1415 break;
1416 case MVT::i16:
1417 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1418 break;
1419 case MVT::i8:
1420 if (Subtarget->is64Bit())
1421 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1422 break;
1423 default: assert(0 && "Unknown any_extend!");
1424 }
1425 if (SRIdx.Val) {
Evan Cheng96aaa542007-10-12 07:55:53 +00001426 SDNode *ResNode = CurDAG->getTargetNode(X86::INSERT_SUBREG,
1427 NVT, N0, SRIdx);
Christopher Lamba1eb1552007-08-10 22:22:41 +00001428
1429#ifndef NDEBUG
1430 DOUT << std::string(Indent-2, ' ') << "=> ";
1431 DEBUG(ResNode->dump(CurDAG));
1432 DOUT << "\n";
1433 Indent -= 2;
1434#endif
1435 return ResNode;
1436 } // Otherwise let generated ISel handle it.
1437 }
1438 break;
1439 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001440
1441 case ISD::SIGN_EXTEND_INREG: {
1442 SDOperand N0 = Node->getOperand(0);
1443 AddToISelQueue(N0);
Evan Cheng403be7e2006-05-08 08:01:26 +00001444
Christopher Lambc59e5212007-08-10 21:48:46 +00001445 MVT::ValueType SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
1446 SDOperand TruncOp = SDOperand(getTruncate(N0, SVT), 0);
Bill Wendling0d642872007-11-01 08:51:44 +00001447 unsigned Opc = 0;
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001448 switch (NVT) {
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001449 case MVT::i16:
Christopher Lambc59e5212007-08-10 21:48:46 +00001450 if (SVT == MVT::i8) Opc = X86::MOVSX16rr8;
1451 else assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001452 break;
1453 case MVT::i32:
Christopher Lambc59e5212007-08-10 21:48:46 +00001454 switch (SVT) {
1455 case MVT::i8: Opc = X86::MOVSX32rr8; break;
1456 case MVT::i16: Opc = X86::MOVSX32rr16; break;
1457 default: assert(0 && "Unknown sign_extend_inreg!");
1458 }
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001459 break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001460 case MVT::i64:
1461 switch (SVT) {
1462 case MVT::i8: Opc = X86::MOVSX64rr8; break;
1463 case MVT::i16: Opc = X86::MOVSX64rr16; break;
1464 case MVT::i32: Opc = X86::MOVSX64rr32; break;
1465 default: assert(0 && "Unknown sign_extend_inreg!");
1466 }
1467 break;
1468 default: assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001469 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001470
1471 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
1472
1473#ifndef NDEBUG
1474 DOUT << std::string(Indent-2, ' ') << "=> ";
1475 DEBUG(TruncOp.Val->dump(CurDAG));
1476 DOUT << "\n";
1477 DOUT << std::string(Indent-2, ' ') << "=> ";
1478 DEBUG(ResNode->dump(CurDAG));
1479 DOUT << "\n";
1480 Indent -= 2;
1481#endif
1482 return ResNode;
1483 break;
1484 }
1485
1486 case ISD::TRUNCATE: {
1487 SDOperand Input = Node->getOperand(0);
1488 AddToISelQueue(Node->getOperand(0));
1489 SDNode *ResNode = getTruncate(Input, NVT);
1490
Evan Cheng403be7e2006-05-08 08:01:26 +00001491#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001492 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001493 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001494 DOUT << "\n";
Evan Cheng403be7e2006-05-08 08:01:26 +00001495 Indent -= 2;
1496#endif
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001497 return ResNode;
Evan Cheng6b2e2542006-05-20 07:44:28 +00001498 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001499 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001500 }
1501
Evan Cheng9ade2182006-08-26 05:34:46 +00001502 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001503
Evan Chengf597dc72006-02-10 22:24:32 +00001504#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001505 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001506 if (ResNode == NULL || ResNode == N.Val)
1507 DEBUG(N.Val->dump(CurDAG));
1508 else
1509 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001510 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001511 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001512#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001513
1514 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001515}
1516
Chris Lattnerc0bad572006-06-08 18:03:49 +00001517bool X86DAGToDAGISel::
1518SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1519 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1520 SDOperand Op0, Op1, Op2, Op3;
1521 switch (ConstraintCode) {
1522 case 'o': // offsetable ??
1523 case 'v': // not offsetable ??
1524 default: return true;
1525 case 'm': // memory
Evan Cheng0d538262006-11-08 20:34:28 +00001526 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001527 return true;
1528 break;
1529 }
1530
Evan Cheng04699902006-08-26 01:05:16 +00001531 OutOps.push_back(Op0);
1532 OutOps.push_back(Op1);
1533 OutOps.push_back(Op2);
1534 OutOps.push_back(Op3);
1535 AddToISelQueue(Op0);
1536 AddToISelQueue(Op1);
1537 AddToISelQueue(Op2);
1538 AddToISelQueue(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001539 return false;
1540}
1541
Chris Lattnerc961eea2005-11-16 01:54:32 +00001542/// createX86ISelDag - This pass converts a legalized DAG into a
1543/// X86-specific DAG, ready for instruction scheduling.
1544///
Evan Chenge50794a2006-08-29 18:28:33 +00001545FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1546 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001547}