blob: 75e9faecb290b3f72e95c2b170acf1e94153e1c1 [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"
Evan Cheng0475ab52008-01-05 00:41:47 +000019#include "X86MachineFunctionInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000020#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000021#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000022#include "X86TargetMachine.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000023#include "llvm/GlobalValue.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000024#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000025#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000026#include "llvm/Support/CFG.h"
Reid Spencer7aa8a452007-01-12 23:22:14 +000027#include "llvm/Type.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000029#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
34#include "llvm/Target/TargetMachine.h"
Evan Chenge9c608d2008-02-19 23:36:51 +000035#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000036#include "llvm/Support/Compiler.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000037#include "llvm/Support/Debug.h"
38#include "llvm/Support/MathExtras.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000039#include "llvm/ADT/Statistic.h"
Evan Cheng2ef88a02006-08-07 22:28:20 +000040#include <queue>
Evan Chengba2f0a92006-02-05 06:46:41 +000041#include <set>
Chris Lattnerc961eea2005-11-16 01:54:32 +000042using namespace llvm;
43
Chris Lattner95b2c7d2006-12-19 22:59:26 +000044STATISTIC(NumFPKill , "Number of FP_REG_KILL instructions added");
45STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
46
Evan Chenge9c608d2008-02-19 23:36:51 +000047namespace {
48 static cl::opt<bool>
Evan Cheng3738f2d2008-02-20 20:57:32 +000049 AlwaysFoldAndInTest("always-fold-and-in-test",
50 cl::desc("Always fold and operation in test"),
51 cl::init(true), cl::Hidden);
Evan Chenge9c608d2008-02-19 23:36:51 +000052}
Chris Lattner95b2c7d2006-12-19 22:59:26 +000053
Chris Lattnerc961eea2005-11-16 01:54:32 +000054//===----------------------------------------------------------------------===//
55// Pattern Matcher Implementation
56//===----------------------------------------------------------------------===//
57
58namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000059 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
60 /// SDOperand's instead of register numbers for the leaves of the matched
61 /// tree.
62 struct X86ISelAddressMode {
63 enum {
64 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000065 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000066 } BaseType;
67
68 struct { // This is really a union, discriminated by BaseType!
69 SDOperand Reg;
70 int FrameIndex;
71 } Base;
72
Evan Chengbe3bf422008-02-07 08:53:49 +000073 bool isRIPRel; // RIP as base?
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000074 unsigned Scale;
75 SDOperand IndexReg;
76 unsigned Disp;
77 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000078 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000079 const char *ES;
80 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000081 unsigned Align; // CP alignment.
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000082
83 X86ISelAddressMode()
Evan Cheng25ab6902006-09-08 06:48:29 +000084 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
85 GV(0), CP(0), ES(0), JT(-1), Align(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000086 }
87 };
88}
89
90namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +000091 //===--------------------------------------------------------------------===//
92 /// ISel - X86 specific code to select X86 machine instructions for
93 /// SelectionDAG operations.
94 ///
Chris Lattner2c79de82006-06-28 23:27:49 +000095 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +000096 /// ContainsFPCode - Every instruction we select that uses or defines a FP
97 /// register should set this to true.
98 bool ContainsFPCode;
99
Evan Chenge50794a2006-08-29 18:28:33 +0000100 /// FastISel - Enable fast(er) instruction selection.
101 ///
102 bool FastISel;
103
Evan Cheng25ab6902006-09-08 06:48:29 +0000104 /// TM - Keep a reference to X86TargetMachine.
105 ///
106 X86TargetMachine &TM;
107
Chris Lattnerc961eea2005-11-16 01:54:32 +0000108 /// X86Lowering - This object fully describes how to lower LLVM code to an
109 /// X86-specific SelectionDAG.
110 X86TargetLowering X86Lowering;
111
112 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
113 /// make the right decision when generating code for different targets.
114 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000115
Evan Cheng25ab6902006-09-08 06:48:29 +0000116 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
117 /// base register.
Evan Cheng7ccced62006-02-18 00:15:05 +0000118 unsigned GlobalBaseReg;
Evan Chenga8df1b42006-07-27 16:44:36 +0000119
Chris Lattnerc961eea2005-11-16 01:54:32 +0000120 public:
Evan Cheng25ab6902006-09-08 06:48:29 +0000121 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Evan Chengc4c62572006-03-13 23:20:37 +0000122 : SelectionDAGISel(X86Lowering),
Evan Cheng25ab6902006-09-08 06:48:29 +0000123 ContainsFPCode(false), FastISel(fast), TM(tm),
Evan Chenga8df1b42006-07-27 16:44:36 +0000124 X86Lowering(*TM.getTargetLowering()),
Evan Chengf4b4c412006-08-08 00:31:00 +0000125 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000126
Evan Cheng7ccced62006-02-18 00:15:05 +0000127 virtual bool runOnFunction(Function &Fn) {
128 // Make sure we re-emit a set of the global base reg if necessary
129 GlobalBaseReg = 0;
130 return SelectionDAGISel::runOnFunction(Fn);
131 }
132
Chris Lattnerc961eea2005-11-16 01:54:32 +0000133 virtual const char *getPassName() const {
134 return "X86 DAG->DAG Instruction Selection";
135 }
136
137 /// InstructionSelectBasicBlock - This callback is invoked by
138 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
139 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
140
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000141 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
142
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000143 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000144
Chris Lattnerc961eea2005-11-16 01:54:32 +0000145// Include the pieces autogenerated from the target description.
146#include "X86GenDAGISel.inc"
147
148 private:
Evan Cheng9ade2182006-08-26 05:34:46 +0000149 SDNode *Select(SDOperand N);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000150
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000151 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM,
Anton Korobeynikovf6e93532007-03-28 18:38:33 +0000152 bool isRoot = true, unsigned Depth = 0);
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000153 bool MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
154 bool isRoot, unsigned Depth);
Evan Cheng0d538262006-11-08 20:34:28 +0000155 bool SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
156 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
157 bool SelectLEAAddr(SDOperand Op, SDOperand N, SDOperand &Base,
158 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
159 bool SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Cheng07e4b002006-10-16 06:34:55 +0000160 SDOperand N, SDOperand &Base, SDOperand &Scale,
Evan Cheng82a91642006-10-11 21:06:01 +0000161 SDOperand &Index, SDOperand &Disp,
162 SDOperand &InChain, SDOperand &OutChain);
Evan Cheng5e351682006-02-06 06:02:33 +0000163 bool TryFoldLoad(SDOperand P, SDOperand N,
164 SDOperand &Base, SDOperand &Scale,
Evan Cheng0114e942006-01-06 20:36:21 +0000165 SDOperand &Index, SDOperand &Disp);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000166 void PreprocessForRMW(SelectionDAG &DAG);
167 void PreprocessForFPConvert(SelectionDAG &DAG);
Evan Cheng2ef88a02006-08-07 22:28:20 +0000168
Chris Lattnerc0bad572006-06-08 18:03:49 +0000169 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
170 /// inline asm expressions.
171 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
172 char ConstraintCode,
173 std::vector<SDOperand> &OutOps,
174 SelectionDAG &DAG);
175
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000176 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
177
Evan Chenge5280532005-12-12 21:49:40 +0000178 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
179 SDOperand &Scale, SDOperand &Index,
180 SDOperand &Disp) {
181 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000182 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
183 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000184 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000185 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000186 // These are 32-bit even in 64-bit mode since RIP relative offset
187 // is 32-bit.
188 if (AM.GV)
189 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
190 else if (AM.CP)
191 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp);
192 else if (AM.ES)
193 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
194 else if (AM.JT != -1)
195 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
196 else
197 Disp = getI32Imm(AM.Disp);
Evan Chenge5280532005-12-12 21:49:40 +0000198 }
199
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000200 /// getI8Imm - Return a target constant with the specified value, of type
201 /// i8.
202 inline SDOperand getI8Imm(unsigned Imm) {
203 return CurDAG->getTargetConstant(Imm, MVT::i8);
204 }
205
Chris Lattnerc961eea2005-11-16 01:54:32 +0000206 /// getI16Imm - Return a target constant with the specified value, of type
207 /// i16.
208 inline SDOperand getI16Imm(unsigned Imm) {
209 return CurDAG->getTargetConstant(Imm, MVT::i16);
210 }
211
212 /// getI32Imm - Return a target constant with the specified value, of type
213 /// i32.
214 inline SDOperand getI32Imm(unsigned Imm) {
215 return CurDAG->getTargetConstant(Imm, MVT::i32);
216 }
Evan Chengf597dc72006-02-10 22:24:32 +0000217
Evan Cheng7ccced62006-02-18 00:15:05 +0000218 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
219 /// base register. Return the virtual register that holds this value.
Evan Cheng9ade2182006-08-26 05:34:46 +0000220 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000221
Christopher Lambc59e5212007-08-10 21:48:46 +0000222 /// getTruncate - return an SDNode that implements a subreg based truncate
223 /// of the specified operand to the the specified value type.
224 SDNode *getTruncate(SDOperand N0, MVT::ValueType VT);
225
Evan Cheng23addc02006-02-10 22:46:26 +0000226#ifndef NDEBUG
227 unsigned Indent;
228#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000229 };
230}
231
Evan Chenga275ecb2006-10-10 01:46:56 +0000232static SDNode *findFlagUse(SDNode *N) {
233 unsigned FlagResNo = N->getNumValues()-1;
234 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
235 SDNode *User = *I;
236 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
237 SDOperand Op = User->getOperand(i);
Evan Cheng494cec62006-10-12 19:13:59 +0000238 if (Op.Val == N && Op.ResNo == FlagResNo)
Evan Chenga275ecb2006-10-10 01:46:56 +0000239 return User;
240 }
241 }
242 return NULL;
243}
244
Evan Cheng27e1fe92006-10-14 08:33:25 +0000245static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
246 SDNode *Root, SDNode *Skip, bool &found,
Evan Chengf4b4c412006-08-08 00:31:00 +0000247 std::set<SDNode *> &Visited) {
248 if (found ||
249 Use->getNodeId() > Def->getNodeId() ||
250 !Visited.insert(Use).second)
251 return;
252
Evan Cheng27e1fe92006-10-14 08:33:25 +0000253 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000254 SDNode *N = Use->getOperand(i).Val;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000255 if (N == Skip)
Evan Chenga275ecb2006-10-10 01:46:56 +0000256 continue;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000257 if (N == Def) {
258 if (Use == ImmedUse)
259 continue; // Immediate use is ok.
260 if (Use == Root) {
261 assert(Use->getOpcode() == ISD::STORE ||
262 Use->getOpcode() == X86ISD::CMP);
263 continue;
264 }
Evan Chengf4b4c412006-08-08 00:31:00 +0000265 found = true;
266 break;
267 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000268 findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000269 }
270}
271
Evan Cheng27e1fe92006-10-14 08:33:25 +0000272/// isNonImmUse - Start searching from Root up the DAG to check is Def can
273/// be reached. Return true if that's the case. However, ignore direct uses
274/// by ImmedUse (which would be U in the example illustrated in
275/// CanBeFoldedBy) and by Root (which can happen in the store case).
276/// FIXME: to be really generic, we should allow direct use by any node
277/// that is being folded. But realisticly since we only fold loads which
278/// have one non-chain use, we only need to watch out for load/op/store
279/// and load/op/cmp case where the root (store / cmp) may reach the load via
280/// its chain operand.
281static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
282 SDNode *Skip = NULL) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000283 std::set<SDNode *> Visited;
284 bool found = false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000285 findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000286 return found;
287}
288
289
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000290bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
Evan Cheng27e1fe92006-10-14 08:33:25 +0000291 if (FastISel) return false;
292
Evan Chenga8df1b42006-07-27 16:44:36 +0000293 // If U use can somehow reach N through another path then U can't fold N or
294 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Cheng37e18032006-07-28 06:33:41 +0000295 // through X. If N is folded into into U, then X is both a predecessor and
Evan Chenga8df1b42006-07-27 16:44:36 +0000296 // a successor of U.
297 //
298 // [ N ]
299 // ^ ^
300 // | |
301 // / \---
302 // / [X]
303 // | ^
304 // [U]--------|
Evan Cheng27e1fe92006-10-14 08:33:25 +0000305
306 if (isNonImmUse(Root, N, U))
307 return false;
308
309 // If U produces a flag, then it gets (even more) interesting. Since it
310 // would have been "glued" together with its flag use, we need to check if
311 // it might reach N:
312 //
313 // [ N ]
314 // ^ ^
315 // | |
316 // [U] \--
317 // ^ [TF]
318 // | ^
319 // | |
320 // \ /
321 // [FU]
322 //
323 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
324 // NU), then TF is a predecessor of FU and a successor of NU. But since
325 // NU and FU are flagged together, this effectively creates a cycle.
326 bool HasFlagUse = false;
327 MVT::ValueType VT = Root->getValueType(Root->getNumValues()-1);
328 while ((VT == MVT::Flag && !Root->use_empty())) {
329 SDNode *FU = findFlagUse(Root);
330 if (FU == NULL)
331 break;
332 else {
333 Root = FU;
334 HasFlagUse = true;
Evan Chenga275ecb2006-10-10 01:46:56 +0000335 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000336 VT = Root->getValueType(Root->getNumValues()-1);
Evan Chenga275ecb2006-10-10 01:46:56 +0000337 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000338
339 if (HasFlagUse)
340 return !isNonImmUse(Root, N, Root, U);
341 return true;
Evan Chenga8df1b42006-07-27 16:44:36 +0000342}
343
Evan Cheng70e674e2006-08-28 20:10:17 +0000344/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
345/// and move load below the TokenFactor. Replace store's chain operand with
346/// load's chain result.
347static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
348 SDOperand Store, SDOperand TF) {
349 std::vector<SDOperand> Ops;
350 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
351 if (Load.Val == TF.Val->getOperand(i).Val)
352 Ops.push_back(Load.Val->getOperand(0));
353 else
354 Ops.push_back(TF.Val->getOperand(i));
355 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
356 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
357 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
358 Store.getOperand(2), Store.getOperand(3));
359}
360
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000361/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
362/// This is only run if not in -fast mode (aka -O0).
363/// This allows the instruction selector to pick more read-modify-write
364/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000365///
366/// [Load chain]
367/// ^
368/// |
369/// [Load]
370/// ^ ^
371/// | |
372/// / \-
373/// / |
374/// [TokenFactor] [Op]
375/// ^ ^
376/// | |
377/// \ /
378/// \ /
379/// [Store]
380///
381/// The fact the store's chain operand != load's chain will prevent the
382/// (store (op (load))) instruction from being selected. We can transform it to:
383///
384/// [Load chain]
385/// ^
386/// |
387/// [TokenFactor]
388/// ^
389/// |
390/// [Load]
391/// ^ ^
392/// | |
393/// | \-
394/// | |
395/// | [Op]
396/// | ^
397/// | |
398/// \ /
399/// \ /
400/// [Store]
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000401void X86DAGToDAGISel::PreprocessForRMW(SelectionDAG &DAG) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000402 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
403 E = DAG.allnodes_end(); I != E; ++I) {
Evan Cheng8b2794a2006-10-13 21:14:26 +0000404 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000405 continue;
406 SDOperand Chain = I->getOperand(0);
407 if (Chain.Val->getOpcode() != ISD::TokenFactor)
408 continue;
409
410 SDOperand N1 = I->getOperand(1);
411 SDOperand N2 = I->getOperand(2);
Evan Cheng1453de52006-09-01 22:52:28 +0000412 if (MVT::isFloatingPoint(N1.getValueType()) ||
413 MVT::isVector(N1.getValueType()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000414 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000415 continue;
416
417 bool RModW = false;
418 SDOperand Load;
419 unsigned Opcode = N1.Val->getOpcode();
420 switch (Opcode) {
421 case ISD::ADD:
422 case ISD::MUL:
Evan Cheng70e674e2006-08-28 20:10:17 +0000423 case ISD::AND:
424 case ISD::OR:
425 case ISD::XOR:
426 case ISD::ADDC:
427 case ISD::ADDE: {
428 SDOperand N10 = N1.getOperand(0);
429 SDOperand N11 = N1.getOperand(1);
Evan Cheng466685d2006-10-09 20:57:25 +0000430 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng70e674e2006-08-28 20:10:17 +0000431 RModW = true;
Evan Cheng466685d2006-10-09 20:57:25 +0000432 else if (ISD::isNON_EXTLoad(N11.Val)) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000433 RModW = true;
434 std::swap(N10, N11);
435 }
436 RModW = RModW && N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000437 (N10.getOperand(1) == N2) &&
438 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000439 if (RModW)
440 Load = N10;
441 break;
442 }
443 case ISD::SUB:
444 case ISD::SHL:
445 case ISD::SRA:
446 case ISD::SRL:
447 case ISD::ROTL:
448 case ISD::ROTR:
449 case ISD::SUBC:
450 case ISD::SUBE:
451 case X86ISD::SHLD:
452 case X86ISD::SHRD: {
453 SDOperand N10 = N1.getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +0000454 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng70e674e2006-08-28 20:10:17 +0000455 RModW = N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000456 (N10.getOperand(1) == N2) &&
457 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000458 if (RModW)
459 Load = N10;
460 break;
461 }
462 }
463
Evan Cheng82a35b32006-08-29 06:44:17 +0000464 if (RModW) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000465 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000466 ++NumLoadMoved;
467 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000468 }
469}
470
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000471
472/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
473/// nodes that target the FP stack to be store and load to the stack. This is a
474/// gross hack. We would like to simply mark these as being illegal, but when
475/// we do that, legalize produces these when it expands calls, then expands
476/// these in the same legalize pass. We would like dag combine to be able to
477/// hack on these between the call expansion and the node legalization. As such
478/// this pass basically does "really late" legalization of these inline with the
479/// X86 isel pass.
480void X86DAGToDAGISel::PreprocessForFPConvert(SelectionDAG &DAG) {
481 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
482 E = DAG.allnodes_end(); I != E; ) {
483 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
484 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
485 continue;
486
487 // If the source and destination are SSE registers, then this is a legal
488 // conversion that should not be lowered.
489 MVT::ValueType SrcVT = N->getOperand(0).getValueType();
490 MVT::ValueType DstVT = N->getValueType(0);
491 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
492 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
493 if (SrcIsSSE && DstIsSSE)
494 continue;
495
496 // If this is an FPStack extension (but not a truncation), it is a noop.
497 if (!SrcIsSSE && !DstIsSSE && N->getOpcode() == ISD::FP_EXTEND)
498 continue;
499
500 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
501 // FPStack has extload and truncstore. SSE can fold direct loads into other
502 // operations. Based on this, decide what we want to do.
503 MVT::ValueType MemVT;
504 if (N->getOpcode() == ISD::FP_ROUND)
505 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
506 else
507 MemVT = SrcIsSSE ? SrcVT : DstVT;
508
509 SDOperand MemTmp = DAG.CreateStackTemporary(MemVT);
510
511 // FIXME: optimize the case where the src/dest is a load or store?
512 SDOperand Store = DAG.getTruncStore(DAG.getEntryNode(), N->getOperand(0),
513 MemTmp, NULL, 0, MemVT);
514 SDOperand Result = DAG.getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp,
515 NULL, 0, MemVT);
516
517 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
518 // extload we created. This will cause general havok on the dag because
519 // anything below the conversion could be folded into other existing nodes.
520 // To avoid invalidating 'I', back it up to the convert node.
521 --I;
522 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result);
523
524 // Now that we did that, the node is dead. Increment the iterator to the
525 // next node to process, then delete N.
526 ++I;
527 DAG.DeleteNode(N);
528 }
529}
530
Chris Lattnerc961eea2005-11-16 01:54:32 +0000531/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
532/// when it has created a SelectionDAG for us to codegen.
533void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
534 DEBUG(BB->dump());
Chris Lattner92cb0af2006-01-11 01:15:34 +0000535 MachineFunction::iterator FirstMBB = BB;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000536
Evan Chenge50794a2006-08-29 18:28:33 +0000537 if (!FastISel)
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000538 PreprocessForRMW(DAG);
539
540 // FIXME: This should only happen when not -fast.
541 PreprocessForFPConvert(DAG);
Evan Cheng70e674e2006-08-28 20:10:17 +0000542
Chris Lattnerc961eea2005-11-16 01:54:32 +0000543 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000544#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000545 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000546 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000547#endif
Evan Chengba2f0a92006-02-05 06:46:41 +0000548 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengf597dc72006-02-10 22:24:32 +0000549#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000550 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000551#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000552
Chris Lattnerc961eea2005-11-16 01:54:32 +0000553 DAG.RemoveDeadNodes();
554
555 // Emit machine code to BB.
556 ScheduleAndEmitDAG(DAG);
Chris Lattner92cb0af2006-01-11 01:15:34 +0000557
558 // If we are emitting FP stack code, scan the basic block to determine if this
559 // block defines any FP values. If so, put an FP_REG_KILL instruction before
560 // the terminator of the block.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000561
Dale Johannesen48d1e452007-09-24 22:52:39 +0000562 // Note that FP stack instructions are used in all modes for long double,
563 // so we always need to do this check.
564 // Also note that it's possible for an FP stack register to be live across
565 // an instruction that produces multiple basic blocks (SSE CMOV) so we
566 // must check all the generated basic blocks.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000567
568 // Scan all of the machine instructions in these MBBs, checking for FP
569 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
570 MachineFunction::iterator MBBI = FirstMBB;
571 do {
Dale Johannesen48d1e452007-09-24 22:52:39 +0000572 bool ContainsFPCode = false;
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000573 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
574 !ContainsFPCode && I != E; ++I) {
575 if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) {
576 const TargetRegisterClass *clas;
577 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
578 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
Dan Gohman6f0d0242008-02-10 18:45:23 +0000579 TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
Chris Lattner84bc5422007-12-31 04:13:23 +0000580 ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) ==
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000581 X86::RFP32RegisterClass ||
582 clas == X86::RFP64RegisterClass ||
583 clas == X86::RFP80RegisterClass)) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000584 ContainsFPCode = true;
585 break;
586 }
587 }
588 }
589 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000590 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
591 // a copy of the input value in this block. In SSE mode, we only care about
592 // 80-bit values.
593 if (!ContainsFPCode) {
594 // Final check, check LLVM BB's that are successors to the LLVM BB
595 // corresponding to BB for FP PHI nodes.
596 const BasicBlock *LLVMBB = BB->getBasicBlock();
597 const PHINode *PN;
598 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
599 !ContainsFPCode && SI != E; ++SI) {
600 for (BasicBlock::const_iterator II = SI->begin();
601 (PN = dyn_cast<PHINode>(II)); ++II) {
602 if (PN->getType()==Type::X86_FP80Ty ||
603 (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) ||
604 (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) {
605 ContainsFPCode = true;
606 break;
607 }
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000608 }
609 }
Chris Lattner92cb0af2006-01-11 01:15:34 +0000610 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000611 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
612 if (ContainsFPCode) {
613 BuildMI(*MBBI, MBBI->getFirstTerminator(),
614 TM.getInstrInfo()->get(X86::FP_REG_KILL));
615 ++NumFPKill;
616 }
617 } while (&*(MBBI++) != BB);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000618}
619
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000620/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
621/// the main function.
622void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
623 MachineFrameInfo *MFI) {
624 const TargetInstrInfo *TII = TM.getInstrInfo();
625 if (Subtarget->isTargetCygMing())
626 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
627}
628
629void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
630 // If this is main, emit special code for main.
631 MachineBasicBlock *BB = MF.begin();
632 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
633 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
634}
635
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000636/// MatchAddress - Add the specified node to the specified addressing mode,
637/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000638/// addressing mode.
Evan Cheng2486af12006-02-11 02:05:36 +0000639bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000640 bool isRoot, unsigned Depth) {
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000641 // Limit recursion.
642 if (Depth > 5)
643 return MatchAddressBase(N, AM, isRoot, Depth);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000644
Evan Cheng25ab6902006-09-08 06:48:29 +0000645 // RIP relative addressing: %rip + 32-bit displacement!
646 if (AM.isRIPRel) {
647 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000648 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000649 if (isInt32(AM.Disp + Val)) {
650 AM.Disp += Val;
651 return false;
652 }
653 }
654 return true;
655 }
656
Evan Cheng2ef88a02006-08-07 22:28:20 +0000657 int id = N.Val->getNodeId();
Evan Cheng1314b002007-12-13 00:43:27 +0000658 bool AlreadySelected = isSelected(id); // Already selected, not yet replaced.
Evan Cheng2486af12006-02-11 02:05:36 +0000659
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000660 switch (N.getOpcode()) {
661 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000662 case ISD::Constant: {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000663 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000664 if (isInt32(AM.Disp + Val)) {
665 AM.Disp += Val;
666 return false;
667 }
668 break;
669 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000670
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000671 case X86ISD::Wrapper: {
672 bool is64Bit = Subtarget->is64Bit();
Evan Cheng0085a282006-11-30 21:55:46 +0000673 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Chengbe3bf422008-02-07 08:53:49 +0000674 // Also, base and index reg must be 0 in order to use rip as base.
675 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
676 AM.Base.Reg.Val || AM.IndexReg.Val))
Evan Cheng0085a282006-11-30 21:55:46 +0000677 break;
Evan Cheng28b514392006-12-05 19:50:18 +0000678 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
679 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000680 // If value is available in a register both base and index components have
681 // been picked, we can't fit the result available in the register in the
682 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Evan Cheng1314b002007-12-13 00:43:27 +0000683 if (!AlreadySelected || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
Evan Cheng28b514392006-12-05 19:50:18 +0000684 SDOperand N0 = N.getOperand(0);
685 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
686 GlobalValue *GV = G->getGlobal();
Evan Chengbe3bf422008-02-07 08:53:49 +0000687 AM.GV = GV;
688 AM.Disp += G->getOffset();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000689 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
690 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000691 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000692 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000693 AM.CP = CP->getConstVal();
694 AM.Align = CP->getAlignment();
695 AM.Disp += CP->getOffset();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000696 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
697 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000698 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000699 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000700 AM.ES = S->getSymbol();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000701 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
702 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000703 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000704 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000705 AM.JT = J->getIndex();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000706 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
707 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000708 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000709 }
710 }
711 break;
Evan Cheng0085a282006-11-30 21:55:46 +0000712 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000713
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000714 case ISD::FrameIndex:
715 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
716 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
717 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
718 return false;
719 }
720 break;
Evan Chengec693f72005-12-08 02:01:35 +0000721
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000722 case ISD::SHL:
Evan Chengbe3bf422008-02-07 08:53:49 +0000723 if (AlreadySelected || AM.IndexReg.Val != 0 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000724 break;
725
726 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
727 unsigned Val = CN->getValue();
728 if (Val == 1 || Val == 2 || Val == 3) {
729 AM.Scale = 1 << Val;
730 SDOperand ShVal = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000731
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000732 // Okay, we know that we have a scale by now. However, if the scaled
733 // value is an add of something and a constant, we can fold the
734 // constant into the disp field here.
735 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
736 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
737 AM.IndexReg = ShVal.Val->getOperand(0);
738 ConstantSDNode *AddVal =
739 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
740 uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
741 if (isInt32(Disp))
742 AM.Disp = Disp;
743 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000744 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000745 } else {
746 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000747 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000748 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000749 }
750 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000751 }
Evan Chengec693f72005-12-08 02:01:35 +0000752
Dan Gohman83688052007-10-22 20:22:24 +0000753 case ISD::SMUL_LOHI:
754 case ISD::UMUL_LOHI:
755 // A mul_lohi where we need the low part can be folded as a plain multiply.
756 if (N.ResNo != 0) break;
757 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000758 case ISD::MUL:
759 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng1314b002007-12-13 00:43:27 +0000760 if (!AlreadySelected &&
Evan Cheng51a9ed92006-02-25 10:09:08 +0000761 AM.BaseType == X86ISelAddressMode::RegBase &&
762 AM.Base.Reg.Val == 0 &&
Evan Chengbe3bf422008-02-07 08:53:49 +0000763 AM.IndexReg.Val == 0 &&
764 !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000765 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
766 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
767 AM.Scale = unsigned(CN->getValue())-1;
768
769 SDOperand MulVal = N.Val->getOperand(0);
770 SDOperand Reg;
771
772 // Okay, we know that we have a scale by now. However, if the scaled
773 // value is an add of something and a constant, we can fold the
774 // constant into the disp field here.
775 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
776 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
777 Reg = MulVal.Val->getOperand(0);
778 ConstantSDNode *AddVal =
779 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
Evan Cheng25ab6902006-09-08 06:48:29 +0000780 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
781 if (isInt32(Disp))
782 AM.Disp = Disp;
783 else
784 Reg = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000785 } else {
786 Reg = N.Val->getOperand(0);
787 }
788
789 AM.IndexReg = AM.Base.Reg = Reg;
790 return false;
791 }
Chris Lattner62412262007-02-04 20:18:17 +0000792 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000793 break;
794
Chris Lattner62412262007-02-04 20:18:17 +0000795 case ISD::ADD:
Evan Cheng1314b002007-12-13 00:43:27 +0000796 if (!AlreadySelected) {
Evan Cheng2486af12006-02-11 02:05:36 +0000797 X86ISelAddressMode Backup = AM;
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000798 if (!MatchAddress(N.Val->getOperand(0), AM, false, Depth+1) &&
799 !MatchAddress(N.Val->getOperand(1), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000800 return false;
801 AM = Backup;
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000802 if (!MatchAddress(N.Val->getOperand(1), AM, false, Depth+1) &&
803 !MatchAddress(N.Val->getOperand(0), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000804 return false;
805 AM = Backup;
806 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000807 break;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000808
Chris Lattner62412262007-02-04 20:18:17 +0000809 case ISD::OR:
810 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Evan Cheng1314b002007-12-13 00:43:27 +0000811 if (AlreadySelected) break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000812
813 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
814 X86ISelAddressMode Backup = AM;
815 // Start with the LHS as an addr mode.
816 if (!MatchAddress(N.getOperand(0), AM, false) &&
817 // Address could not have picked a GV address for the displacement.
818 AM.GV == NULL &&
819 // On x86-64, the resultant disp must fit in 32-bits.
820 isInt32(AM.Disp + CN->getSignExtended()) &&
821 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000822 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000823 AM.Disp += CN->getValue();
824 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000825 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000826 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000827 }
828 break;
Evan Cheng1314b002007-12-13 00:43:27 +0000829
830 case ISD::AND: {
831 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
832 // allows us to fold the shift into this addressing mode.
833 if (AlreadySelected) break;
834 SDOperand Shift = N.getOperand(0);
835 if (Shift.getOpcode() != ISD::SHL) break;
836
837 // Scale must not be used already.
838 if (AM.IndexReg.Val != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +0000839
840 // Not when RIP is used as the base.
841 if (AM.isRIPRel) break;
Evan Cheng1314b002007-12-13 00:43:27 +0000842
843 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
844 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
845 if (!C1 || !C2) break;
846
847 // Not likely to be profitable if either the AND or SHIFT node has more
848 // than one use (unless all uses are for address computation). Besides,
849 // isel mechanism requires their node ids to be reused.
850 if (!N.hasOneUse() || !Shift.hasOneUse())
851 break;
852
853 // Verify that the shift amount is something we can fold.
854 unsigned ShiftCst = C1->getValue();
855 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
856 break;
857
858 // Get the new AND mask, this folds to a constant.
859 SDOperand NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
860 SDOperand(C2, 0), SDOperand(C1, 0));
861 SDOperand NewAND = CurDAG->getNode(ISD::AND, N.getValueType(),
862 Shift.getOperand(0), NewANDMask);
863 NewANDMask.Val->setNodeId(Shift.Val->getNodeId());
864 NewAND.Val->setNodeId(N.Val->getNodeId());
865
866 AM.Scale = 1 << ShiftCst;
867 AM.IndexReg = NewAND;
868 return false;
869 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000870 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000871
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000872 return MatchAddressBase(N, AM, isRoot, Depth);
873}
874
875/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
876/// specified addressing mode without any further recursion.
877bool X86DAGToDAGISel::MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
878 bool isRoot, unsigned Depth) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000879 // Is the base register already occupied?
880 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
881 // If so, check to see if the scale index register is set.
Evan Chengbe3bf422008-02-07 08:53:49 +0000882 if (AM.IndexReg.Val == 0 && !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000883 AM.IndexReg = N;
884 AM.Scale = 1;
885 return false;
886 }
887
888 // Otherwise, we cannot select it.
889 return true;
890 }
891
892 // Default, generate it as a register.
893 AM.BaseType = X86ISelAddressMode::RegBase;
894 AM.Base.Reg = N;
895 return false;
896}
897
Evan Chengec693f72005-12-08 02:01:35 +0000898/// SelectAddr - returns true if it is able pattern match an addressing mode.
899/// It returns the operands which make up the maximal addressing mode it can
900/// match by reference.
Evan Cheng0d538262006-11-08 20:34:28 +0000901bool X86DAGToDAGISel::SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
902 SDOperand &Scale, SDOperand &Index,
903 SDOperand &Disp) {
Evan Chengec693f72005-12-08 02:01:35 +0000904 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +0000905 if (MatchAddress(N, AM))
906 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000907
Evan Cheng25ab6902006-09-08 06:48:29 +0000908 MVT::ValueType VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +0000909 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Cheng7dd281b2006-02-05 05:25:07 +0000910 if (!AM.Base.Reg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000911 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +0000912 }
Evan Cheng8700e142006-01-11 06:09:51 +0000913
Evan Cheng7dd281b2006-02-05 05:25:07 +0000914 if (!AM.IndexReg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000915 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +0000916
917 getAddressOperands(AM, Base, Scale, Index, Disp);
918 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000919}
920
Chris Lattner4fe4f252006-10-11 22:09:58 +0000921/// isZeroNode - Returns true if Elt is a constant zero or a floating point
922/// constant +0.0.
923static inline bool isZeroNode(SDOperand Elt) {
924 return ((isa<ConstantSDNode>(Elt) &&
925 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
926 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +0000927 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Chris Lattner4fe4f252006-10-11 22:09:58 +0000928}
929
930
Chris Lattner3a7cd952006-10-07 21:55:32 +0000931/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
932/// match a load whose top elements are either undef or zeros. The load flavor
933/// is derived from the type of N, which is either v4f32 or v2f64.
Evan Cheng0d538262006-11-08 20:34:28 +0000934bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Cheng07e4b002006-10-16 06:34:55 +0000935 SDOperand N, SDOperand &Base,
Evan Cheng82a91642006-10-11 21:06:01 +0000936 SDOperand &Scale, SDOperand &Index,
937 SDOperand &Disp, SDOperand &InChain,
938 SDOperand &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +0000939 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +0000940 InChain = N.getOperand(0).getValue(1);
Evan Cheng07e4b002006-10-16 06:34:55 +0000941 if (ISD::isNON_EXTLoad(InChain.Val) &&
942 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +0000943 N.hasOneUse() &&
Evan Cheng0d538262006-11-08 20:34:28 +0000944 CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
Evan Cheng82a91642006-10-11 21:06:01 +0000945 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Evan Cheng0d538262006-11-08 20:34:28 +0000946 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner3a7cd952006-10-07 21:55:32 +0000947 return false;
Evan Cheng82a91642006-10-11 21:06:01 +0000948 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +0000949 return true;
950 }
951 }
Chris Lattner4fe4f252006-10-11 22:09:58 +0000952
953 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +0000954 // elements. This is a vector shuffle from the zero vector.
Chris Lattner4fe4f252006-10-11 22:09:58 +0000955 if (N.getOpcode() == ISD::VECTOR_SHUFFLE && N.Val->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +0000956 // Check to see if the top elements are all zeros (or bitcast of zeros).
957 ISD::isBuildVectorAllZeros(N.getOperand(0).Val) &&
Chris Lattner4fe4f252006-10-11 22:09:58 +0000958 N.getOperand(1).getOpcode() == ISD::SCALAR_TO_VECTOR &&
959 N.getOperand(1).Val->hasOneUse() &&
960 ISD::isNON_EXTLoad(N.getOperand(1).getOperand(0).Val) &&
961 N.getOperand(1).getOperand(0).hasOneUse()) {
Chris Lattner4fe4f252006-10-11 22:09:58 +0000962 // Check to see if the shuffle mask is 4/L/L/L or 2/L, where L is something
963 // from the LHS.
Chris Lattner8a594482007-11-25 00:24:49 +0000964 unsigned VecWidth=MVT::getVectorNumElements(N.getOperand(0).getValueType());
Chris Lattner4fe4f252006-10-11 22:09:58 +0000965 SDOperand ShufMask = N.getOperand(2);
966 assert(ShufMask.getOpcode() == ISD::BUILD_VECTOR && "Invalid shuf mask!");
967 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(ShufMask.getOperand(0))) {
968 if (C->getValue() == VecWidth) {
969 for (unsigned i = 1; i != VecWidth; ++i) {
970 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF) {
971 // ok.
972 } else {
973 ConstantSDNode *C = cast<ConstantSDNode>(ShufMask.getOperand(i));
974 if (C->getValue() >= VecWidth) return false;
975 }
976 }
977 }
978
979 // Okay, this is a zero extending load. Fold it.
980 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(1).getOperand(0));
Evan Cheng0d538262006-11-08 20:34:28 +0000981 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner4fe4f252006-10-11 22:09:58 +0000982 return false;
983 OutChain = LD->getChain();
984 InChain = SDOperand(LD, 1);
985 return true;
986 }
987 }
Chris Lattner3a7cd952006-10-07 21:55:32 +0000988 return false;
989}
990
991
Evan Cheng51a9ed92006-02-25 10:09:08 +0000992/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
993/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng0d538262006-11-08 20:34:28 +0000994bool X86DAGToDAGISel::SelectLEAAddr(SDOperand Op, SDOperand N,
995 SDOperand &Base, SDOperand &Scale,
Evan Cheng51a9ed92006-02-25 10:09:08 +0000996 SDOperand &Index, SDOperand &Disp) {
997 X86ISelAddressMode AM;
998 if (MatchAddress(N, AM))
999 return false;
1000
Evan Cheng25ab6902006-09-08 06:48:29 +00001001 MVT::ValueType VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001002 unsigned Complexity = 0;
1003 if (AM.BaseType == X86ISelAddressMode::RegBase)
1004 if (AM.Base.Reg.Val)
1005 Complexity = 1;
1006 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001007 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001008 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1009 Complexity = 4;
1010
1011 if (AM.IndexReg.Val)
1012 Complexity++;
1013 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001014 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001015
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001016 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1017 // a simple shift.
1018 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001019 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001020
1021 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1022 // to a LEA. This is determined with some expermentation but is by no means
1023 // optimal (especially for code size consideration). LEA is nice because of
1024 // its three-address nature. Tweak the cost function again when we can run
1025 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +00001026 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
1027 // For X86-64, we should always use lea to materialize RIP relative
1028 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001029 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001030 Complexity = 4;
1031 else
1032 Complexity += 2;
1033 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001034
1035 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
1036 Complexity++;
1037
1038 if (Complexity > 2) {
1039 getAddressOperands(AM, Base, Scale, Index, Disp);
1040 return true;
1041 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001042 return false;
1043}
1044
Evan Cheng5e351682006-02-06 06:02:33 +00001045bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
1046 SDOperand &Base, SDOperand &Scale,
1047 SDOperand &Index, SDOperand &Disp) {
Evan Cheng466685d2006-10-09 20:57:25 +00001048 if (ISD::isNON_EXTLoad(N.Val) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001049 N.hasOneUse() &&
Evan Cheng27e1fe92006-10-14 08:33:25 +00001050 CanBeFoldedBy(N.Val, P.Val, P.Val))
Evan Cheng0d538262006-11-08 20:34:28 +00001051 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
Evan Cheng0114e942006-01-06 20:36:21 +00001052 return false;
1053}
1054
Evan Cheng7ccced62006-02-18 00:15:05 +00001055/// getGlobalBaseReg - Output the instructions required to put the
1056/// base address to use for accessing globals into a register.
1057///
Evan Cheng9ade2182006-08-26 05:34:46 +00001058SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng25ab6902006-09-08 06:48:29 +00001059 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Evan Cheng7ccced62006-02-18 00:15:05 +00001060 if (!GlobalBaseReg) {
1061 // Insert the set of GlobalBaseReg into the first MBB of the function
Evan Cheng0475ab52008-01-05 00:41:47 +00001062 MachineFunction *MF = BB->getParent();
1063 MachineBasicBlock &FirstMBB = MF->front();
Evan Cheng7ccced62006-02-18 00:15:05 +00001064 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Evan Cheng0475ab52008-01-05 00:41:47 +00001065 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Chris Lattner84bc5422007-12-31 04:13:23 +00001066 unsigned PC = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001067
Evan Chengc0f64ff2006-11-27 23:37:22 +00001068 const TargetInstrInfo *TII = TM.getInstrInfo();
Evan Chengf02ca692007-12-22 02:26:46 +00001069 // Operand of MovePCtoStack is completely ignored by asm printer. It's
1070 // only used in JIT code emission as displacement to pc.
Evan Cheng0475ab52008-01-05 00:41:47 +00001071 BuildMI(FirstMBB, MBBI, TII->get(X86::MOVPC32r), PC).addImm(0);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001072
1073 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
1074 // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
Evan Cheng706535d2007-01-22 21:34:25 +00001075 if (TM.getRelocationModel() == Reloc::PIC_ &&
1076 Subtarget->isPICStyleGOT()) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001077 GlobalBaseReg = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng0475ab52008-01-05 00:41:47 +00001078 BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg)
1079 .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001080 } else {
1081 GlobalBaseReg = PC;
1082 }
1083
Evan Cheng7ccced62006-02-18 00:15:05 +00001084 }
Evan Cheng25ab6902006-09-08 06:48:29 +00001085 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
Evan Cheng7ccced62006-02-18 00:15:05 +00001086}
1087
Evan Chengb245d922006-05-20 01:36:52 +00001088static SDNode *FindCallStartFromCall(SDNode *Node) {
1089 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1090 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1091 "Node doesn't have a token chain argument!");
1092 return FindCallStartFromCall(Node->getOperand(0).Val);
1093}
1094
Christopher Lambc59e5212007-08-10 21:48:46 +00001095SDNode *X86DAGToDAGISel::getTruncate(SDOperand N0, MVT::ValueType VT) {
1096 SDOperand SRIdx;
1097 switch (VT) {
1098 case MVT::i8:
1099 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1100 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1101 if (!Subtarget->is64Bit()) {
1102 unsigned Opc;
1103 MVT::ValueType VT;
1104 switch (N0.getValueType()) {
1105 default: assert(0 && "Unknown truncate!");
1106 case MVT::i16:
1107 Opc = X86::MOV16to16_;
1108 VT = MVT::i16;
1109 break;
1110 case MVT::i32:
1111 Opc = X86::MOV32to32_;
1112 VT = MVT::i32;
1113 break;
1114 }
Evan Cheng96aaa542007-10-12 07:55:53 +00001115 N0 = SDOperand(CurDAG->getTargetNode(Opc, VT, MVT::Flag, N0), 0);
1116 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1117 VT, N0, SRIdx, N0.getValue(1));
Christopher Lambc59e5212007-08-10 21:48:46 +00001118 }
1119 break;
1120 case MVT::i16:
1121 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1122 break;
1123 case MVT::i32:
1124 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1125 break;
Evan Cheng96aaa542007-10-12 07:55:53 +00001126 default: assert(0 && "Unknown truncate!"); break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001127 }
Evan Cheng96aaa542007-10-12 07:55:53 +00001128 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, VT, N0, SRIdx);
Christopher Lambc59e5212007-08-10 21:48:46 +00001129}
1130
1131
Evan Cheng9ade2182006-08-26 05:34:46 +00001132SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Chengdef941b2005-12-15 01:02:48 +00001133 SDNode *Node = N.Val;
1134 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001135 unsigned Opc, MOpc;
1136 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +00001137
Evan Chengf597dc72006-02-10 22:24:32 +00001138#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001139 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001140 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001141 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001142 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001143#endif
1144
Evan Cheng34167212006-02-09 00:37:58 +00001145 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengf597dc72006-02-10 22:24:32 +00001146#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001147 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001148 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001149 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001150 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001151#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001152 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001153 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001154
Evan Cheng0114e942006-01-06 20:36:21 +00001155 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001156 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001157 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001158 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001159
Evan Cheng0d9e9762008-01-29 19:34:22 +00001160 case X86ISD::FP_GET_RESULT2: {
1161 SDOperand Chain = N.getOperand(0);
1162 SDOperand InFlag = N.getOperand(1);
1163 AddToISelQueue(Chain);
1164 AddToISelQueue(InFlag);
1165 std::vector<MVT::ValueType> Tys;
1166 Tys.push_back(MVT::f80);
1167 Tys.push_back(MVT::f80);
1168 Tys.push_back(MVT::Other);
1169 Tys.push_back(MVT::Flag);
1170 SDOperand Ops[] = { Chain, InFlag };
1171 SDNode *ResNode = CurDAG->getTargetNode(X86::FpGETRESULT80x2, Tys,
1172 Ops, 2);
1173 Chain = SDOperand(ResNode, 2);
1174 InFlag = SDOperand(ResNode, 3);
1175 ReplaceUses(SDOperand(N.Val, 2), Chain);
1176 ReplaceUses(SDOperand(N.Val, 3), InFlag);
1177 return ResNode;
1178 }
1179
Evan Cheng51a9ed92006-02-25 10:09:08 +00001180 case ISD::ADD: {
1181 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1182 // code and is matched first so to prevent it from being turned into
1183 // LEA32r X+c.
Evan Chengb1a9aec2008-01-08 02:06:11 +00001184 // In 64-bit small code size mode, use LEA to take advantage of
1185 // RIP-relative addressing.
1186 if (TM.getCodeModel() != CodeModel::Small)
1187 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001188 MVT::ValueType PtrVT = TLI.getPointerTy();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001189 SDOperand N0 = N.getOperand(0);
1190 SDOperand N1 = N.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00001191 if (N.Val->getValueType(0) == PtrVT &&
Evan Cheng19f2ffc2006-12-05 04:01:03 +00001192 N0.getOpcode() == X86ISD::Wrapper &&
Evan Cheng51a9ed92006-02-25 10:09:08 +00001193 N1.getOpcode() == ISD::Constant) {
1194 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1195 SDOperand C(0, 0);
1196 // TODO: handle ExternalSymbolSDNode.
1197 if (GlobalAddressSDNode *G =
1198 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001199 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001200 G->getOffset() + Offset);
1201 } else if (ConstantPoolSDNode *CP =
1202 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Chengc356a572006-09-12 21:04:05 +00001203 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001204 CP->getAlignment(),
1205 CP->getOffset()+Offset);
1206 }
1207
Evan Cheng25ab6902006-09-08 06:48:29 +00001208 if (C.Val) {
1209 if (Subtarget->is64Bit()) {
1210 SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
1211 CurDAG->getRegister(0, PtrVT), C };
1212 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1213 } else
1214 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1215 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001216 }
1217
1218 // Other cases are handled by auto-generated code.
1219 break;
Evan Chenga0ea0532006-02-23 02:43:52 +00001220 }
Evan Cheng020d2e82006-02-23 20:41:18 +00001221
Dan Gohman525178c2007-10-08 18:33:35 +00001222 case ISD::SMUL_LOHI:
1223 case ISD::UMUL_LOHI: {
1224 SDOperand N0 = Node->getOperand(0);
1225 SDOperand N1 = Node->getOperand(1);
1226
Dan Gohman525178c2007-10-08 18:33:35 +00001227 bool isSigned = Opcode == ISD::SMUL_LOHI;
1228 if (!isSigned)
Evan Cheng0114e942006-01-06 20:36:21 +00001229 switch (NVT) {
1230 default: assert(0 && "Unsupported VT!");
1231 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1232 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1233 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001234 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001235 }
1236 else
1237 switch (NVT) {
1238 default: assert(0 && "Unsupported VT!");
1239 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1240 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1241 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001242 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001243 }
1244
1245 unsigned LoReg, HiReg;
1246 switch (NVT) {
1247 default: assert(0 && "Unsupported VT!");
1248 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1249 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1250 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001251 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001252 }
1253
Evan Cheng0114e942006-01-06 20:36:21 +00001254 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng7afa1662007-08-02 05:48:35 +00001255 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman525178c2007-10-08 18:33:35 +00001256 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001257 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +00001258 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng7afa1662007-08-02 05:48:35 +00001259 if (foldedLoad)
1260 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001261 }
1262
Evan Cheng04699902006-08-26 01:05:16 +00001263 AddToISelQueue(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001264 SDOperand InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1265 N0, SDOperand()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001266
1267 if (foldedLoad) {
Dan Gohman525178c2007-10-08 18:33:35 +00001268 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001269 AddToISelQueue(Tmp0);
1270 AddToISelQueue(Tmp1);
1271 AddToISelQueue(Tmp2);
1272 AddToISelQueue(Tmp3);
Dan Gohman525178c2007-10-08 18:33:35 +00001273 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001274 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001275 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001276 InFlag = SDOperand(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001277 // Update the chain.
1278 ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001279 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001280 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001281 InFlag =
1282 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001283 }
1284
Dan Gohman525178c2007-10-08 18:33:35 +00001285 // Copy the low half of the result, if it is needed.
1286 if (!N.getValue(0).use_empty()) {
1287 SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1288 LoReg, NVT, InFlag);
1289 InFlag = Result.getValue(2);
1290 ReplaceUses(N.getValue(0), Result);
1291#ifndef NDEBUG
1292 DOUT << std::string(Indent-2, ' ') << "=> ";
1293 DEBUG(Result.Val->dump(CurDAG));
1294 DOUT << "\n";
1295#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001296 }
Dan Gohman525178c2007-10-08 18:33:35 +00001297 // Copy the high half of the result, if it is needed.
1298 if (!N.getValue(1).use_empty()) {
1299 SDOperand Result;
1300 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1301 // Prevent use of AH in a REX instruction by referencing AX instead.
1302 // Shift it down 8 bits.
1303 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1304 X86::AX, MVT::i16, InFlag);
1305 InFlag = Result.getValue(2);
1306 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1307 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1308 // Then truncate it down to i8.
1309 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1310 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1311 MVT::i8, Result, SRIdx), 0);
1312 } else {
1313 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1314 HiReg, NVT, InFlag);
1315 InFlag = Result.getValue(2);
1316 }
1317 ReplaceUses(N.getValue(1), Result);
1318#ifndef NDEBUG
1319 DOUT << std::string(Indent-2, ' ') << "=> ";
1320 DEBUG(Result.Val->dump(CurDAG));
1321 DOUT << "\n";
1322#endif
1323 }
Evan Cheng34167212006-02-09 00:37:58 +00001324
Evan Chengf597dc72006-02-10 22:24:32 +00001325#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001326 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001327#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001328
Evan Cheng64a752f2006-08-11 09:08:15 +00001329 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001330 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001331
Dan Gohman525178c2007-10-08 18:33:35 +00001332 case ISD::SDIVREM:
1333 case ISD::UDIVREM: {
1334 SDOperand N0 = Node->getOperand(0);
1335 SDOperand N1 = Node->getOperand(1);
1336
1337 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001338 if (!isSigned)
1339 switch (NVT) {
1340 default: assert(0 && "Unsupported VT!");
1341 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1342 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1343 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001344 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001345 }
1346 else
1347 switch (NVT) {
1348 default: assert(0 && "Unsupported VT!");
1349 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1350 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1351 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001352 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001353 }
1354
1355 unsigned LoReg, HiReg;
1356 unsigned ClrOpcode, SExtOpcode;
1357 switch (NVT) {
1358 default: assert(0 && "Unsupported VT!");
1359 case MVT::i8:
1360 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001361 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001362 SExtOpcode = X86::CBW;
1363 break;
1364 case MVT::i16:
1365 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001366 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001367 SExtOpcode = X86::CWD;
1368 break;
1369 case MVT::i32:
1370 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001371 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001372 SExtOpcode = X86::CDQ;
1373 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001374 case MVT::i64:
1375 LoReg = X86::RAX; HiReg = X86::RDX;
1376 ClrOpcode = X86::MOV64r0;
1377 SExtOpcode = X86::CQO;
1378 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001379 }
1380
Dan Gohman525178c2007-10-08 18:33:35 +00001381 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
1382 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1383
1384 SDOperand InFlag;
Evan Chengb1409ce2006-11-17 22:10:14 +00001385 if (NVT == MVT::i8 && !isSigned) {
1386 // Special case for div8, just use a move with zero extension to AX to
1387 // clear the upper 8 bits (AH).
1388 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
1389 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
1390 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
1391 AddToISelQueue(N0.getOperand(0));
1392 AddToISelQueue(Tmp0);
1393 AddToISelQueue(Tmp1);
1394 AddToISelQueue(Tmp2);
1395 AddToISelQueue(Tmp3);
1396 Move =
1397 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
1398 Ops, 5), 0);
1399 Chain = Move.getValue(1);
1400 ReplaceUses(N0.getValue(1), Chain);
1401 } else {
1402 AddToISelQueue(N0);
1403 Move =
1404 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
1405 Chain = CurDAG->getEntryNode();
1406 }
Dan Gohman525178c2007-10-08 18:33:35 +00001407 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDOperand());
Evan Cheng948f3432006-01-06 23:19:29 +00001408 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001409 } else {
1410 AddToISelQueue(N0);
1411 InFlag =
Dan Gohman525178c2007-10-08 18:33:35 +00001412 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
1413 LoReg, N0, SDOperand()).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001414 if (isSigned) {
1415 // Sign extend the low part into the high part.
1416 InFlag =
1417 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
1418 } else {
1419 // Zero out the high part, effectively zero extending the input.
1420 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001421 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1422 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001423 }
Evan Cheng948f3432006-01-06 23:19:29 +00001424 }
1425
1426 if (foldedLoad) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001427 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001428 AddToISelQueue(Tmp0);
1429 AddToISelQueue(Tmp1);
1430 AddToISelQueue(Tmp2);
1431 AddToISelQueue(Tmp3);
Evan Chengb1409ce2006-11-17 22:10:14 +00001432 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001433 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001434 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001435 InFlag = SDOperand(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001436 // Update the chain.
1437 ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001438 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001439 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001440 InFlag =
1441 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001442 }
1443
Dan Gohmana37c9f72007-09-25 18:23:27 +00001444 // Copy the division (low) result, if it is needed.
1445 if (!N.getValue(0).use_empty()) {
Dan Gohman525178c2007-10-08 18:33:35 +00001446 SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1447 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001448 InFlag = Result.getValue(2);
1449 ReplaceUses(N.getValue(0), Result);
1450#ifndef NDEBUG
1451 DOUT << std::string(Indent-2, ' ') << "=> ";
1452 DEBUG(Result.Val->dump(CurDAG));
1453 DOUT << "\n";
1454#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001455 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001456 // Copy the remainder (high) result, if it is needed.
1457 if (!N.getValue(1).use_empty()) {
1458 SDOperand Result;
1459 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1460 // Prevent use of AH in a REX instruction by referencing AX instead.
1461 // Shift it down 8 bits.
Dan Gohman525178c2007-10-08 18:33:35 +00001462 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1463 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001464 InFlag = Result.getValue(2);
1465 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1466 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1467 // Then truncate it down to i8.
1468 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1469 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1470 MVT::i8, Result, SRIdx), 0);
1471 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00001472 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1473 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001474 InFlag = Result.getValue(2);
1475 }
1476 ReplaceUses(N.getValue(1), Result);
1477#ifndef NDEBUG
1478 DOUT << std::string(Indent-2, ' ') << "=> ";
1479 DEBUG(Result.Val->dump(CurDAG));
1480 DOUT << "\n";
1481#endif
1482 }
Evan Chengf597dc72006-02-10 22:24:32 +00001483
1484#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001485 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001486#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001487
1488 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001489 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001490
1491 case ISD::ANY_EXTEND: {
1492 SDOperand N0 = Node->getOperand(0);
1493 AddToISelQueue(N0);
1494 if (NVT == MVT::i64 || NVT == MVT::i32 || NVT == MVT::i16) {
1495 SDOperand SRIdx;
1496 switch(N0.getValueType()) {
1497 case MVT::i32:
1498 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1499 break;
1500 case MVT::i16:
1501 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1502 break;
1503 case MVT::i8:
1504 if (Subtarget->is64Bit())
1505 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1506 break;
1507 default: assert(0 && "Unknown any_extend!");
1508 }
1509 if (SRIdx.Val) {
Evan Cheng96aaa542007-10-12 07:55:53 +00001510 SDNode *ResNode = CurDAG->getTargetNode(X86::INSERT_SUBREG,
1511 NVT, N0, SRIdx);
Christopher Lamba1eb1552007-08-10 22:22:41 +00001512
1513#ifndef NDEBUG
1514 DOUT << std::string(Indent-2, ' ') << "=> ";
1515 DEBUG(ResNode->dump(CurDAG));
1516 DOUT << "\n";
1517 Indent -= 2;
1518#endif
1519 return ResNode;
1520 } // Otherwise let generated ISel handle it.
1521 }
1522 break;
1523 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001524
1525 case ISD::SIGN_EXTEND_INREG: {
1526 SDOperand N0 = Node->getOperand(0);
1527 AddToISelQueue(N0);
Evan Cheng403be7e2006-05-08 08:01:26 +00001528
Christopher Lambc59e5212007-08-10 21:48:46 +00001529 MVT::ValueType SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
1530 SDOperand TruncOp = SDOperand(getTruncate(N0, SVT), 0);
Bill Wendling0d642872007-11-01 08:51:44 +00001531 unsigned Opc = 0;
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001532 switch (NVT) {
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001533 case MVT::i16:
Christopher Lambc59e5212007-08-10 21:48:46 +00001534 if (SVT == MVT::i8) Opc = X86::MOVSX16rr8;
1535 else assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001536 break;
1537 case MVT::i32:
Christopher Lambc59e5212007-08-10 21:48:46 +00001538 switch (SVT) {
1539 case MVT::i8: Opc = X86::MOVSX32rr8; break;
1540 case MVT::i16: Opc = X86::MOVSX32rr16; break;
1541 default: assert(0 && "Unknown sign_extend_inreg!");
1542 }
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001543 break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001544 case MVT::i64:
1545 switch (SVT) {
1546 case MVT::i8: Opc = X86::MOVSX64rr8; break;
1547 case MVT::i16: Opc = X86::MOVSX64rr16; break;
1548 case MVT::i32: Opc = X86::MOVSX64rr32; break;
1549 default: assert(0 && "Unknown sign_extend_inreg!");
1550 }
1551 break;
1552 default: assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001553 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001554
1555 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
1556
1557#ifndef NDEBUG
1558 DOUT << std::string(Indent-2, ' ') << "=> ";
1559 DEBUG(TruncOp.Val->dump(CurDAG));
1560 DOUT << "\n";
1561 DOUT << std::string(Indent-2, ' ') << "=> ";
1562 DEBUG(ResNode->dump(CurDAG));
1563 DOUT << "\n";
1564 Indent -= 2;
1565#endif
1566 return ResNode;
1567 break;
1568 }
1569
1570 case ISD::TRUNCATE: {
1571 SDOperand Input = Node->getOperand(0);
1572 AddToISelQueue(Node->getOperand(0));
1573 SDNode *ResNode = getTruncate(Input, NVT);
1574
Evan Cheng403be7e2006-05-08 08:01:26 +00001575#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001576 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001577 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001578 DOUT << "\n";
Evan Cheng403be7e2006-05-08 08:01:26 +00001579 Indent -= 2;
1580#endif
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001581 return ResNode;
Evan Cheng6b2e2542006-05-20 07:44:28 +00001582 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001583 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001584 }
1585
Evan Cheng9ade2182006-08-26 05:34:46 +00001586 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001587
Evan Chengf597dc72006-02-10 22:24:32 +00001588#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001589 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001590 if (ResNode == NULL || ResNode == N.Val)
1591 DEBUG(N.Val->dump(CurDAG));
1592 else
1593 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001594 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001595 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001596#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001597
1598 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001599}
1600
Chris Lattnerc0bad572006-06-08 18:03:49 +00001601bool X86DAGToDAGISel::
1602SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1603 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1604 SDOperand Op0, Op1, Op2, Op3;
1605 switch (ConstraintCode) {
1606 case 'o': // offsetable ??
1607 case 'v': // not offsetable ??
1608 default: return true;
1609 case 'm': // memory
Evan Cheng0d538262006-11-08 20:34:28 +00001610 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001611 return true;
1612 break;
1613 }
1614
Evan Cheng04699902006-08-26 01:05:16 +00001615 OutOps.push_back(Op0);
1616 OutOps.push_back(Op1);
1617 OutOps.push_back(Op2);
1618 OutOps.push_back(Op3);
1619 AddToISelQueue(Op0);
1620 AddToISelQueue(Op1);
1621 AddToISelQueue(Op2);
1622 AddToISelQueue(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001623 return false;
1624}
1625
Chris Lattnerc961eea2005-11-16 01:54:32 +00001626/// createX86ISelDag - This pass converts a legalized DAG into a
1627/// X86-specific DAG, ready for instruction scheduling.
1628///
Evan Chenge50794a2006-08-29 18:28:33 +00001629FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1630 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001631}