blob: ac7957c07f8f13343b3f492c78ef7b9baa8f38b6 [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
Chris Lattnerc961eea2005-11-16 01:54:32 +000047//===----------------------------------------------------------------------===//
48// Pattern Matcher Implementation
49//===----------------------------------------------------------------------===//
50
51namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000052 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
53 /// SDOperand's instead of register numbers for the leaves of the matched
54 /// tree.
55 struct X86ISelAddressMode {
56 enum {
57 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000058 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000059 } BaseType;
60
61 struct { // This is really a union, discriminated by BaseType!
62 SDOperand Reg;
63 int FrameIndex;
64 } Base;
65
Evan Chengbe3bf422008-02-07 08:53:49 +000066 bool isRIPRel; // RIP as base?
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000067 unsigned Scale;
68 SDOperand IndexReg;
69 unsigned Disp;
70 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000071 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000072 const char *ES;
73 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000074 unsigned Align; // CP alignment.
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000075
76 X86ISelAddressMode()
Evan Cheng25ab6902006-09-08 06:48:29 +000077 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
78 GV(0), CP(0), ES(0), JT(-1), Align(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000079 }
80 };
81}
82
83namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +000084 //===--------------------------------------------------------------------===//
85 /// ISel - X86 specific code to select X86 machine instructions for
86 /// SelectionDAG operations.
87 ///
Chris Lattner2c79de82006-06-28 23:27:49 +000088 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +000089 /// ContainsFPCode - Every instruction we select that uses or defines a FP
90 /// register should set this to true.
91 bool ContainsFPCode;
92
Evan Chenge50794a2006-08-29 18:28:33 +000093 /// FastISel - Enable fast(er) instruction selection.
94 ///
95 bool FastISel;
96
Evan Cheng25ab6902006-09-08 06:48:29 +000097 /// TM - Keep a reference to X86TargetMachine.
98 ///
99 X86TargetMachine &TM;
100
Chris Lattnerc961eea2005-11-16 01:54:32 +0000101 /// X86Lowering - This object fully describes how to lower LLVM code to an
102 /// X86-specific SelectionDAG.
103 X86TargetLowering X86Lowering;
104
105 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
106 /// make the right decision when generating code for different targets.
107 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000108
Evan Cheng25ab6902006-09-08 06:48:29 +0000109 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
110 /// base register.
Evan Cheng7ccced62006-02-18 00:15:05 +0000111 unsigned GlobalBaseReg;
Evan Chenga8df1b42006-07-27 16:44:36 +0000112
Chris Lattnerc961eea2005-11-16 01:54:32 +0000113 public:
Evan Cheng25ab6902006-09-08 06:48:29 +0000114 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Evan Chengc4c62572006-03-13 23:20:37 +0000115 : SelectionDAGISel(X86Lowering),
Evan Cheng25ab6902006-09-08 06:48:29 +0000116 ContainsFPCode(false), FastISel(fast), TM(tm),
Evan Chenga8df1b42006-07-27 16:44:36 +0000117 X86Lowering(*TM.getTargetLowering()),
Evan Chengf4b4c412006-08-08 00:31:00 +0000118 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000119
Evan Cheng7ccced62006-02-18 00:15:05 +0000120 virtual bool runOnFunction(Function &Fn) {
121 // Make sure we re-emit a set of the global base reg if necessary
122 GlobalBaseReg = 0;
123 return SelectionDAGISel::runOnFunction(Fn);
124 }
125
Chris Lattnerc961eea2005-11-16 01:54:32 +0000126 virtual const char *getPassName() const {
127 return "X86 DAG->DAG Instruction Selection";
128 }
129
130 /// InstructionSelectBasicBlock - This callback is invoked by
131 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
132 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
133
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000134 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
135
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000136 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000137
Chris Lattnerc961eea2005-11-16 01:54:32 +0000138// Include the pieces autogenerated from the target description.
139#include "X86GenDAGISel.inc"
140
141 private:
Evan Cheng9ade2182006-08-26 05:34:46 +0000142 SDNode *Select(SDOperand N);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000143
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000144 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM,
Anton Korobeynikovf6e93532007-03-28 18:38:33 +0000145 bool isRoot = true, unsigned Depth = 0);
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000146 bool MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
147 bool isRoot, unsigned Depth);
Evan Cheng0d538262006-11-08 20:34:28 +0000148 bool SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
149 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
150 bool SelectLEAAddr(SDOperand Op, SDOperand N, SDOperand &Base,
151 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
152 bool SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Cheng07e4b002006-10-16 06:34:55 +0000153 SDOperand N, SDOperand &Base, SDOperand &Scale,
Evan Cheng82a91642006-10-11 21:06:01 +0000154 SDOperand &Index, SDOperand &Disp,
155 SDOperand &InChain, SDOperand &OutChain);
Evan Cheng5e351682006-02-06 06:02:33 +0000156 bool TryFoldLoad(SDOperand P, SDOperand N,
157 SDOperand &Base, SDOperand &Scale,
Evan Cheng0114e942006-01-06 20:36:21 +0000158 SDOperand &Index, SDOperand &Disp);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000159 void PreprocessForRMW(SelectionDAG &DAG);
160 void PreprocessForFPConvert(SelectionDAG &DAG);
Evan Cheng2ef88a02006-08-07 22:28:20 +0000161
Chris Lattnerc0bad572006-06-08 18:03:49 +0000162 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
163 /// inline asm expressions.
164 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
165 char ConstraintCode,
166 std::vector<SDOperand> &OutOps,
167 SelectionDAG &DAG);
168
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000169 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
170
Evan Chenge5280532005-12-12 21:49:40 +0000171 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
172 SDOperand &Scale, SDOperand &Index,
173 SDOperand &Disp) {
174 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000175 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
176 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000177 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000178 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000179 // These are 32-bit even in 64-bit mode since RIP relative offset
180 // is 32-bit.
181 if (AM.GV)
182 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
183 else if (AM.CP)
184 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp);
185 else if (AM.ES)
186 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
187 else if (AM.JT != -1)
188 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
189 else
190 Disp = getI32Imm(AM.Disp);
Evan Chenge5280532005-12-12 21:49:40 +0000191 }
192
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000193 /// getI8Imm - Return a target constant with the specified value, of type
194 /// i8.
195 inline SDOperand getI8Imm(unsigned Imm) {
196 return CurDAG->getTargetConstant(Imm, MVT::i8);
197 }
198
Chris Lattnerc961eea2005-11-16 01:54:32 +0000199 /// getI16Imm - Return a target constant with the specified value, of type
200 /// i16.
201 inline SDOperand getI16Imm(unsigned Imm) {
202 return CurDAG->getTargetConstant(Imm, MVT::i16);
203 }
204
205 /// getI32Imm - Return a target constant with the specified value, of type
206 /// i32.
207 inline SDOperand getI32Imm(unsigned Imm) {
208 return CurDAG->getTargetConstant(Imm, MVT::i32);
209 }
Evan Chengf597dc72006-02-10 22:24:32 +0000210
Evan Cheng7ccced62006-02-18 00:15:05 +0000211 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
212 /// base register. Return the virtual register that holds this value.
Evan Cheng9ade2182006-08-26 05:34:46 +0000213 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000214
Christopher Lambc59e5212007-08-10 21:48:46 +0000215 /// getTruncate - return an SDNode that implements a subreg based truncate
216 /// of the specified operand to the the specified value type.
217 SDNode *getTruncate(SDOperand N0, MVT::ValueType VT);
218
Evan Cheng23addc02006-02-10 22:46:26 +0000219#ifndef NDEBUG
220 unsigned Indent;
221#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000222 };
223}
224
Evan Chenga275ecb2006-10-10 01:46:56 +0000225static SDNode *findFlagUse(SDNode *N) {
226 unsigned FlagResNo = N->getNumValues()-1;
227 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
228 SDNode *User = *I;
229 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
230 SDOperand Op = User->getOperand(i);
Evan Cheng494cec62006-10-12 19:13:59 +0000231 if (Op.Val == N && Op.ResNo == FlagResNo)
Evan Chenga275ecb2006-10-10 01:46:56 +0000232 return User;
233 }
234 }
235 return NULL;
236}
237
Evan Cheng27e1fe92006-10-14 08:33:25 +0000238static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
239 SDNode *Root, SDNode *Skip, bool &found,
Evan Chengf4b4c412006-08-08 00:31:00 +0000240 std::set<SDNode *> &Visited) {
241 if (found ||
242 Use->getNodeId() > Def->getNodeId() ||
243 !Visited.insert(Use).second)
244 return;
245
Evan Cheng27e1fe92006-10-14 08:33:25 +0000246 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000247 SDNode *N = Use->getOperand(i).Val;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000248 if (N == Skip)
Evan Chenga275ecb2006-10-10 01:46:56 +0000249 continue;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000250 if (N == Def) {
251 if (Use == ImmedUse)
252 continue; // Immediate use is ok.
253 if (Use == Root) {
254 assert(Use->getOpcode() == ISD::STORE ||
255 Use->getOpcode() == X86ISD::CMP);
256 continue;
257 }
Evan Chengf4b4c412006-08-08 00:31:00 +0000258 found = true;
259 break;
260 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000261 findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000262 }
263}
264
Evan Cheng27e1fe92006-10-14 08:33:25 +0000265/// isNonImmUse - Start searching from Root up the DAG to check is Def can
266/// be reached. Return true if that's the case. However, ignore direct uses
267/// by ImmedUse (which would be U in the example illustrated in
268/// CanBeFoldedBy) and by Root (which can happen in the store case).
269/// FIXME: to be really generic, we should allow direct use by any node
270/// that is being folded. But realisticly since we only fold loads which
271/// have one non-chain use, we only need to watch out for load/op/store
272/// and load/op/cmp case where the root (store / cmp) may reach the load via
273/// its chain operand.
274static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
275 SDNode *Skip = NULL) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000276 std::set<SDNode *> Visited;
277 bool found = false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000278 findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000279 return found;
280}
281
282
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000283bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
Evan Cheng27e1fe92006-10-14 08:33:25 +0000284 if (FastISel) return false;
285
Evan Chenga8df1b42006-07-27 16:44:36 +0000286 // If U use can somehow reach N through another path then U can't fold N or
287 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Cheng37e18032006-07-28 06:33:41 +0000288 // through X. If N is folded into into U, then X is both a predecessor and
Evan Chenga8df1b42006-07-27 16:44:36 +0000289 // a successor of U.
290 //
291 // [ N ]
292 // ^ ^
293 // | |
294 // / \---
295 // / [X]
296 // | ^
297 // [U]--------|
Evan Cheng27e1fe92006-10-14 08:33:25 +0000298
299 if (isNonImmUse(Root, N, U))
300 return false;
301
302 // If U produces a flag, then it gets (even more) interesting. Since it
303 // would have been "glued" together with its flag use, we need to check if
304 // it might reach N:
305 //
306 // [ N ]
307 // ^ ^
308 // | |
309 // [U] \--
310 // ^ [TF]
311 // | ^
312 // | |
313 // \ /
314 // [FU]
315 //
316 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
317 // NU), then TF is a predecessor of FU and a successor of NU. But since
318 // NU and FU are flagged together, this effectively creates a cycle.
319 bool HasFlagUse = false;
320 MVT::ValueType VT = Root->getValueType(Root->getNumValues()-1);
321 while ((VT == MVT::Flag && !Root->use_empty())) {
322 SDNode *FU = findFlagUse(Root);
323 if (FU == NULL)
324 break;
325 else {
326 Root = FU;
327 HasFlagUse = true;
Evan Chenga275ecb2006-10-10 01:46:56 +0000328 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000329 VT = Root->getValueType(Root->getNumValues()-1);
Evan Chenga275ecb2006-10-10 01:46:56 +0000330 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000331
332 if (HasFlagUse)
333 return !isNonImmUse(Root, N, Root, U);
334 return true;
Evan Chenga8df1b42006-07-27 16:44:36 +0000335}
336
Evan Cheng70e674e2006-08-28 20:10:17 +0000337/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
338/// and move load below the TokenFactor. Replace store's chain operand with
339/// load's chain result.
340static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
341 SDOperand Store, SDOperand TF) {
342 std::vector<SDOperand> Ops;
343 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
344 if (Load.Val == TF.Val->getOperand(i).Val)
345 Ops.push_back(Load.Val->getOperand(0));
346 else
347 Ops.push_back(TF.Val->getOperand(i));
348 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
349 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
350 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
351 Store.getOperand(2), Store.getOperand(3));
352}
353
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000354/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
355/// This is only run if not in -fast mode (aka -O0).
356/// This allows the instruction selector to pick more read-modify-write
357/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000358///
359/// [Load chain]
360/// ^
361/// |
362/// [Load]
363/// ^ ^
364/// | |
365/// / \-
366/// / |
367/// [TokenFactor] [Op]
368/// ^ ^
369/// | |
370/// \ /
371/// \ /
372/// [Store]
373///
374/// The fact the store's chain operand != load's chain will prevent the
375/// (store (op (load))) instruction from being selected. We can transform it to:
376///
377/// [Load chain]
378/// ^
379/// |
380/// [TokenFactor]
381/// ^
382/// |
383/// [Load]
384/// ^ ^
385/// | |
386/// | \-
387/// | |
388/// | [Op]
389/// | ^
390/// | |
391/// \ /
392/// \ /
393/// [Store]
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000394void X86DAGToDAGISel::PreprocessForRMW(SelectionDAG &DAG) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000395 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
396 E = DAG.allnodes_end(); I != E; ++I) {
Evan Cheng8b2794a2006-10-13 21:14:26 +0000397 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000398 continue;
399 SDOperand Chain = I->getOperand(0);
400 if (Chain.Val->getOpcode() != ISD::TokenFactor)
401 continue;
402
403 SDOperand N1 = I->getOperand(1);
404 SDOperand N2 = I->getOperand(2);
Evan Cheng1453de52006-09-01 22:52:28 +0000405 if (MVT::isFloatingPoint(N1.getValueType()) ||
406 MVT::isVector(N1.getValueType()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000407 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000408 continue;
409
410 bool RModW = false;
411 SDOperand Load;
412 unsigned Opcode = N1.Val->getOpcode();
413 switch (Opcode) {
414 case ISD::ADD:
415 case ISD::MUL:
Evan Cheng70e674e2006-08-28 20:10:17 +0000416 case ISD::AND:
417 case ISD::OR:
418 case ISD::XOR:
419 case ISD::ADDC:
420 case ISD::ADDE: {
421 SDOperand N10 = N1.getOperand(0);
422 SDOperand N11 = N1.getOperand(1);
Evan Cheng466685d2006-10-09 20:57:25 +0000423 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng70e674e2006-08-28 20:10:17 +0000424 RModW = true;
Evan Cheng466685d2006-10-09 20:57:25 +0000425 else if (ISD::isNON_EXTLoad(N11.Val)) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000426 RModW = true;
427 std::swap(N10, N11);
428 }
Evan Cheng07b7ea12008-03-04 00:40:35 +0000429 RModW = RModW && N10.Val->isOperandOf(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000430 (N10.getOperand(1) == N2) &&
431 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000432 if (RModW)
433 Load = N10;
434 break;
435 }
436 case ISD::SUB:
437 case ISD::SHL:
438 case ISD::SRA:
439 case ISD::SRL:
440 case ISD::ROTL:
441 case ISD::ROTR:
442 case ISD::SUBC:
443 case ISD::SUBE:
444 case X86ISD::SHLD:
445 case X86ISD::SHRD: {
446 SDOperand N10 = N1.getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +0000447 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng07b7ea12008-03-04 00:40:35 +0000448 RModW = N10.Val->isOperandOf(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000449 (N10.getOperand(1) == N2) &&
450 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000451 if (RModW)
452 Load = N10;
453 break;
454 }
455 }
456
Evan Cheng82a35b32006-08-29 06:44:17 +0000457 if (RModW) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000458 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000459 ++NumLoadMoved;
460 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000461 }
462}
463
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000464
465/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
466/// nodes that target the FP stack to be store and load to the stack. This is a
467/// gross hack. We would like to simply mark these as being illegal, but when
468/// we do that, legalize produces these when it expands calls, then expands
469/// these in the same legalize pass. We would like dag combine to be able to
470/// hack on these between the call expansion and the node legalization. As such
471/// this pass basically does "really late" legalization of these inline with the
472/// X86 isel pass.
473void X86DAGToDAGISel::PreprocessForFPConvert(SelectionDAG &DAG) {
474 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
475 E = DAG.allnodes_end(); I != E; ) {
476 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
477 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
478 continue;
479
480 // If the source and destination are SSE registers, then this is a legal
481 // conversion that should not be lowered.
482 MVT::ValueType SrcVT = N->getOperand(0).getValueType();
483 MVT::ValueType DstVT = N->getValueType(0);
484 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
485 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
486 if (SrcIsSSE && DstIsSSE)
487 continue;
488
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000489 if (!SrcIsSSE && !DstIsSSE) {
490 // If this is an FPStack extension, it is a noop.
491 if (N->getOpcode() == ISD::FP_EXTEND)
492 continue;
493 // If this is a value-preserving FPStack truncation, it is a noop.
494 if (N->getConstantOperandVal(1))
495 continue;
496 }
497
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000498 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
499 // FPStack has extload and truncstore. SSE can fold direct loads into other
500 // operations. Based on this, decide what we want to do.
501 MVT::ValueType MemVT;
502 if (N->getOpcode() == ISD::FP_ROUND)
503 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
504 else
505 MemVT = SrcIsSSE ? SrcVT : DstVT;
506
507 SDOperand MemTmp = DAG.CreateStackTemporary(MemVT);
508
509 // FIXME: optimize the case where the src/dest is a load or store?
510 SDOperand Store = DAG.getTruncStore(DAG.getEntryNode(), N->getOperand(0),
511 MemTmp, NULL, 0, MemVT);
512 SDOperand Result = DAG.getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp,
513 NULL, 0, MemVT);
514
515 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
516 // extload we created. This will cause general havok on the dag because
517 // anything below the conversion could be folded into other existing nodes.
518 // To avoid invalidating 'I', back it up to the convert node.
519 --I;
520 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result);
521
522 // Now that we did that, the node is dead. Increment the iterator to the
523 // next node to process, then delete N.
524 ++I;
525 DAG.DeleteNode(N);
526 }
527}
528
Chris Lattnerc961eea2005-11-16 01:54:32 +0000529/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
530/// when it has created a SelectionDAG for us to codegen.
531void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
532 DEBUG(BB->dump());
Chris Lattner92cb0af2006-01-11 01:15:34 +0000533 MachineFunction::iterator FirstMBB = BB;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000534
Evan Chenge50794a2006-08-29 18:28:33 +0000535 if (!FastISel)
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000536 PreprocessForRMW(DAG);
537
538 // FIXME: This should only happen when not -fast.
539 PreprocessForFPConvert(DAG);
Evan Cheng70e674e2006-08-28 20:10:17 +0000540
Chris Lattnerc961eea2005-11-16 01:54:32 +0000541 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000542#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000543 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000544 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000545#endif
Evan Chengba2f0a92006-02-05 06:46:41 +0000546 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengf597dc72006-02-10 22:24:32 +0000547#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000548 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000549#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000550
Chris Lattnerc961eea2005-11-16 01:54:32 +0000551 DAG.RemoveDeadNodes();
552
553 // Emit machine code to BB.
554 ScheduleAndEmitDAG(DAG);
Chris Lattner92cb0af2006-01-11 01:15:34 +0000555
556 // If we are emitting FP stack code, scan the basic block to determine if this
557 // block defines any FP values. If so, put an FP_REG_KILL instruction before
558 // the terminator of the block.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000559
Dale Johannesen48d1e452007-09-24 22:52:39 +0000560 // Note that FP stack instructions are used in all modes for long double,
561 // so we always need to do this check.
562 // Also note that it's possible for an FP stack register to be live across
563 // an instruction that produces multiple basic blocks (SSE CMOV) so we
564 // must check all the generated basic blocks.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000565
566 // Scan all of the machine instructions in these MBBs, checking for FP
567 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
568 MachineFunction::iterator MBBI = FirstMBB;
569 do {
Dale Johannesen48d1e452007-09-24 22:52:39 +0000570 bool ContainsFPCode = false;
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000571 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
572 !ContainsFPCode && I != E; ++I) {
573 if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) {
574 const TargetRegisterClass *clas;
575 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
576 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
Dan Gohman6f0d0242008-02-10 18:45:23 +0000577 TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
Chris Lattner84bc5422007-12-31 04:13:23 +0000578 ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) ==
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000579 X86::RFP32RegisterClass ||
580 clas == X86::RFP64RegisterClass ||
581 clas == X86::RFP80RegisterClass)) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000582 ContainsFPCode = true;
583 break;
584 }
585 }
586 }
587 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000588 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
589 // a copy of the input value in this block. In SSE mode, we only care about
590 // 80-bit values.
591 if (!ContainsFPCode) {
592 // Final check, check LLVM BB's that are successors to the LLVM BB
593 // corresponding to BB for FP PHI nodes.
594 const BasicBlock *LLVMBB = BB->getBasicBlock();
595 const PHINode *PN;
596 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
597 !ContainsFPCode && SI != E; ++SI) {
598 for (BasicBlock::const_iterator II = SI->begin();
599 (PN = dyn_cast<PHINode>(II)); ++II) {
600 if (PN->getType()==Type::X86_FP80Ty ||
601 (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) ||
602 (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) {
603 ContainsFPCode = true;
604 break;
605 }
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000606 }
607 }
Chris Lattner92cb0af2006-01-11 01:15:34 +0000608 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000609 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
610 if (ContainsFPCode) {
611 BuildMI(*MBBI, MBBI->getFirstTerminator(),
612 TM.getInstrInfo()->get(X86::FP_REG_KILL));
613 ++NumFPKill;
614 }
615 } while (&*(MBBI++) != BB);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000616}
617
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000618/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
619/// the main function.
620void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
621 MachineFrameInfo *MFI) {
622 const TargetInstrInfo *TII = TM.getInstrInfo();
623 if (Subtarget->isTargetCygMing())
624 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
625}
626
627void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
628 // If this is main, emit special code for main.
629 MachineBasicBlock *BB = MF.begin();
630 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
631 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
632}
633
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000634/// MatchAddress - Add the specified node to the specified addressing mode,
635/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000636/// addressing mode.
Evan Cheng2486af12006-02-11 02:05:36 +0000637bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000638 bool isRoot, unsigned Depth) {
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000639 // Limit recursion.
640 if (Depth > 5)
641 return MatchAddressBase(N, AM, isRoot, Depth);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000642
Evan Cheng25ab6902006-09-08 06:48:29 +0000643 // RIP relative addressing: %rip + 32-bit displacement!
644 if (AM.isRIPRel) {
645 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000646 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000647 if (isInt32(AM.Disp + Val)) {
648 AM.Disp += Val;
649 return false;
650 }
651 }
652 return true;
653 }
654
Evan Cheng2ef88a02006-08-07 22:28:20 +0000655 int id = N.Val->getNodeId();
Evan Cheng1314b002007-12-13 00:43:27 +0000656 bool AlreadySelected = isSelected(id); // Already selected, not yet replaced.
Evan Cheng2486af12006-02-11 02:05:36 +0000657
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000658 switch (N.getOpcode()) {
659 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000660 case ISD::Constant: {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000661 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000662 if (isInt32(AM.Disp + Val)) {
663 AM.Disp += Val;
664 return false;
665 }
666 break;
667 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000668
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000669 case X86ISD::Wrapper: {
670 bool is64Bit = Subtarget->is64Bit();
Evan Cheng0085a282006-11-30 21:55:46 +0000671 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Chengbe3bf422008-02-07 08:53:49 +0000672 // Also, base and index reg must be 0 in order to use rip as base.
673 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
674 AM.Base.Reg.Val || AM.IndexReg.Val))
Evan Cheng0085a282006-11-30 21:55:46 +0000675 break;
Evan Cheng28b514392006-12-05 19:50:18 +0000676 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
677 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000678 // If value is available in a register both base and index components have
679 // been picked, we can't fit the result available in the register in the
680 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Evan Cheng1314b002007-12-13 00:43:27 +0000681 if (!AlreadySelected || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
Evan Cheng28b514392006-12-05 19:50:18 +0000682 SDOperand N0 = N.getOperand(0);
683 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
684 GlobalValue *GV = G->getGlobal();
Evan Chengbe3bf422008-02-07 08:53:49 +0000685 AM.GV = GV;
686 AM.Disp += G->getOffset();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000687 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
688 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000689 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000690 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000691 AM.CP = CP->getConstVal();
692 AM.Align = CP->getAlignment();
693 AM.Disp += CP->getOffset();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000694 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
695 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000696 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000697 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000698 AM.ES = S->getSymbol();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000699 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
700 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000701 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000702 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000703 AM.JT = J->getIndex();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000704 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
705 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000706 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000707 }
708 }
709 break;
Evan Cheng0085a282006-11-30 21:55:46 +0000710 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000711
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000712 case ISD::FrameIndex:
713 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
714 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
715 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
716 return false;
717 }
718 break;
Evan Chengec693f72005-12-08 02:01:35 +0000719
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000720 case ISD::SHL:
Evan Chengbe3bf422008-02-07 08:53:49 +0000721 if (AlreadySelected || AM.IndexReg.Val != 0 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000722 break;
723
724 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
725 unsigned Val = CN->getValue();
726 if (Val == 1 || Val == 2 || Val == 3) {
727 AM.Scale = 1 << Val;
728 SDOperand ShVal = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000729
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000730 // Okay, we know that we have a scale by now. However, if the scaled
731 // value is an add of something and a constant, we can fold the
732 // constant into the disp field here.
733 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
734 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
735 AM.IndexReg = ShVal.Val->getOperand(0);
736 ConstantSDNode *AddVal =
737 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
738 uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
739 if (isInt32(Disp))
740 AM.Disp = Disp;
741 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000742 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000743 } else {
744 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000745 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000746 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000747 }
748 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000749 }
Evan Chengec693f72005-12-08 02:01:35 +0000750
Dan Gohman83688052007-10-22 20:22:24 +0000751 case ISD::SMUL_LOHI:
752 case ISD::UMUL_LOHI:
753 // A mul_lohi where we need the low part can be folded as a plain multiply.
754 if (N.ResNo != 0) break;
755 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000756 case ISD::MUL:
757 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng1314b002007-12-13 00:43:27 +0000758 if (!AlreadySelected &&
Evan Cheng51a9ed92006-02-25 10:09:08 +0000759 AM.BaseType == X86ISelAddressMode::RegBase &&
760 AM.Base.Reg.Val == 0 &&
Evan Chengbe3bf422008-02-07 08:53:49 +0000761 AM.IndexReg.Val == 0 &&
762 !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000763 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
764 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
765 AM.Scale = unsigned(CN->getValue())-1;
766
767 SDOperand MulVal = N.Val->getOperand(0);
768 SDOperand Reg;
769
770 // Okay, we know that we have a scale by now. However, if the scaled
771 // value is an add of something and a constant, we can fold the
772 // constant into the disp field here.
773 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
774 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
775 Reg = MulVal.Val->getOperand(0);
776 ConstantSDNode *AddVal =
777 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
Evan Cheng25ab6902006-09-08 06:48:29 +0000778 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
779 if (isInt32(Disp))
780 AM.Disp = Disp;
781 else
782 Reg = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000783 } else {
784 Reg = N.Val->getOperand(0);
785 }
786
787 AM.IndexReg = AM.Base.Reg = Reg;
788 return false;
789 }
Chris Lattner62412262007-02-04 20:18:17 +0000790 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000791 break;
792
Chris Lattner62412262007-02-04 20:18:17 +0000793 case ISD::ADD:
Evan Cheng1314b002007-12-13 00:43:27 +0000794 if (!AlreadySelected) {
Evan Cheng2486af12006-02-11 02:05:36 +0000795 X86ISelAddressMode Backup = AM;
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000796 if (!MatchAddress(N.Val->getOperand(0), AM, false, Depth+1) &&
797 !MatchAddress(N.Val->getOperand(1), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000798 return false;
799 AM = Backup;
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000800 if (!MatchAddress(N.Val->getOperand(1), AM, false, Depth+1) &&
801 !MatchAddress(N.Val->getOperand(0), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000802 return false;
803 AM = Backup;
804 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000805 break;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000806
Chris Lattner62412262007-02-04 20:18:17 +0000807 case ISD::OR:
808 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Evan Cheng1314b002007-12-13 00:43:27 +0000809 if (AlreadySelected) break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000810
811 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
812 X86ISelAddressMode Backup = AM;
813 // Start with the LHS as an addr mode.
814 if (!MatchAddress(N.getOperand(0), AM, false) &&
815 // Address could not have picked a GV address for the displacement.
816 AM.GV == NULL &&
817 // On x86-64, the resultant disp must fit in 32-bits.
818 isInt32(AM.Disp + CN->getSignExtended()) &&
819 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000820 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000821 AM.Disp += CN->getValue();
822 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000823 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000824 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000825 }
826 break;
Evan Cheng1314b002007-12-13 00:43:27 +0000827
828 case ISD::AND: {
829 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
830 // allows us to fold the shift into this addressing mode.
831 if (AlreadySelected) break;
832 SDOperand Shift = N.getOperand(0);
833 if (Shift.getOpcode() != ISD::SHL) break;
834
835 // Scale must not be used already.
836 if (AM.IndexReg.Val != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +0000837
838 // Not when RIP is used as the base.
839 if (AM.isRIPRel) break;
Evan Cheng1314b002007-12-13 00:43:27 +0000840
841 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
842 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
843 if (!C1 || !C2) break;
844
845 // Not likely to be profitable if either the AND or SHIFT node has more
846 // than one use (unless all uses are for address computation). Besides,
847 // isel mechanism requires their node ids to be reused.
848 if (!N.hasOneUse() || !Shift.hasOneUse())
849 break;
850
851 // Verify that the shift amount is something we can fold.
852 unsigned ShiftCst = C1->getValue();
853 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
854 break;
855
856 // Get the new AND mask, this folds to a constant.
857 SDOperand NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
858 SDOperand(C2, 0), SDOperand(C1, 0));
859 SDOperand NewAND = CurDAG->getNode(ISD::AND, N.getValueType(),
860 Shift.getOperand(0), NewANDMask);
861 NewANDMask.Val->setNodeId(Shift.Val->getNodeId());
862 NewAND.Val->setNodeId(N.Val->getNodeId());
863
864 AM.Scale = 1 << ShiftCst;
865 AM.IndexReg = NewAND;
866 return false;
867 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000868 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000869
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000870 return MatchAddressBase(N, AM, isRoot, Depth);
871}
872
873/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
874/// specified addressing mode without any further recursion.
875bool X86DAGToDAGISel::MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
876 bool isRoot, unsigned Depth) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000877 // Is the base register already occupied?
878 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
879 // If so, check to see if the scale index register is set.
Evan Chengbe3bf422008-02-07 08:53:49 +0000880 if (AM.IndexReg.Val == 0 && !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000881 AM.IndexReg = N;
882 AM.Scale = 1;
883 return false;
884 }
885
886 // Otherwise, we cannot select it.
887 return true;
888 }
889
890 // Default, generate it as a register.
891 AM.BaseType = X86ISelAddressMode::RegBase;
892 AM.Base.Reg = N;
893 return false;
894}
895
Evan Chengec693f72005-12-08 02:01:35 +0000896/// SelectAddr - returns true if it is able pattern match an addressing mode.
897/// It returns the operands which make up the maximal addressing mode it can
898/// match by reference.
Evan Cheng0d538262006-11-08 20:34:28 +0000899bool X86DAGToDAGISel::SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
900 SDOperand &Scale, SDOperand &Index,
901 SDOperand &Disp) {
Evan Chengec693f72005-12-08 02:01:35 +0000902 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +0000903 if (MatchAddress(N, AM))
904 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000905
Evan Cheng25ab6902006-09-08 06:48:29 +0000906 MVT::ValueType VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +0000907 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Cheng7dd281b2006-02-05 05:25:07 +0000908 if (!AM.Base.Reg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000909 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +0000910 }
Evan Cheng8700e142006-01-11 06:09:51 +0000911
Evan Cheng7dd281b2006-02-05 05:25:07 +0000912 if (!AM.IndexReg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000913 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +0000914
915 getAddressOperands(AM, Base, Scale, Index, Disp);
916 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000917}
918
Chris Lattner4fe4f252006-10-11 22:09:58 +0000919/// isZeroNode - Returns true if Elt is a constant zero or a floating point
920/// constant +0.0.
921static inline bool isZeroNode(SDOperand Elt) {
922 return ((isa<ConstantSDNode>(Elt) &&
923 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
924 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +0000925 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Chris Lattner4fe4f252006-10-11 22:09:58 +0000926}
927
928
Chris Lattner3a7cd952006-10-07 21:55:32 +0000929/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
930/// match a load whose top elements are either undef or zeros. The load flavor
931/// is derived from the type of N, which is either v4f32 or v2f64.
Evan Cheng0d538262006-11-08 20:34:28 +0000932bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Cheng07e4b002006-10-16 06:34:55 +0000933 SDOperand N, SDOperand &Base,
Evan Cheng82a91642006-10-11 21:06:01 +0000934 SDOperand &Scale, SDOperand &Index,
935 SDOperand &Disp, SDOperand &InChain,
936 SDOperand &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +0000937 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +0000938 InChain = N.getOperand(0).getValue(1);
Evan Cheng07e4b002006-10-16 06:34:55 +0000939 if (ISD::isNON_EXTLoad(InChain.Val) &&
940 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +0000941 N.hasOneUse() &&
Evan Cheng0d538262006-11-08 20:34:28 +0000942 CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
Evan Cheng82a91642006-10-11 21:06:01 +0000943 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Evan Cheng0d538262006-11-08 20:34:28 +0000944 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner3a7cd952006-10-07 21:55:32 +0000945 return false;
Evan Cheng82a91642006-10-11 21:06:01 +0000946 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +0000947 return true;
948 }
949 }
Chris Lattner4fe4f252006-10-11 22:09:58 +0000950
951 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +0000952 // elements. This is a vector shuffle from the zero vector.
Chris Lattner4fe4f252006-10-11 22:09:58 +0000953 if (N.getOpcode() == ISD::VECTOR_SHUFFLE && N.Val->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +0000954 // Check to see if the top elements are all zeros (or bitcast of zeros).
955 ISD::isBuildVectorAllZeros(N.getOperand(0).Val) &&
Chris Lattner4fe4f252006-10-11 22:09:58 +0000956 N.getOperand(1).getOpcode() == ISD::SCALAR_TO_VECTOR &&
957 N.getOperand(1).Val->hasOneUse() &&
958 ISD::isNON_EXTLoad(N.getOperand(1).getOperand(0).Val) &&
959 N.getOperand(1).getOperand(0).hasOneUse()) {
Chris Lattner4fe4f252006-10-11 22:09:58 +0000960 // Check to see if the shuffle mask is 4/L/L/L or 2/L, where L is something
961 // from the LHS.
Chris Lattner8a594482007-11-25 00:24:49 +0000962 unsigned VecWidth=MVT::getVectorNumElements(N.getOperand(0).getValueType());
Chris Lattner4fe4f252006-10-11 22:09:58 +0000963 SDOperand ShufMask = N.getOperand(2);
964 assert(ShufMask.getOpcode() == ISD::BUILD_VECTOR && "Invalid shuf mask!");
965 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(ShufMask.getOperand(0))) {
966 if (C->getValue() == VecWidth) {
967 for (unsigned i = 1; i != VecWidth; ++i) {
968 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF) {
969 // ok.
970 } else {
971 ConstantSDNode *C = cast<ConstantSDNode>(ShufMask.getOperand(i));
972 if (C->getValue() >= VecWidth) return false;
973 }
974 }
975 }
976
977 // Okay, this is a zero extending load. Fold it.
978 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(1).getOperand(0));
Evan Cheng0d538262006-11-08 20:34:28 +0000979 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner4fe4f252006-10-11 22:09:58 +0000980 return false;
981 OutChain = LD->getChain();
982 InChain = SDOperand(LD, 1);
983 return true;
984 }
985 }
Chris Lattner3a7cd952006-10-07 21:55:32 +0000986 return false;
987}
988
989
Evan Cheng51a9ed92006-02-25 10:09:08 +0000990/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
991/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng0d538262006-11-08 20:34:28 +0000992bool X86DAGToDAGISel::SelectLEAAddr(SDOperand Op, SDOperand N,
993 SDOperand &Base, SDOperand &Scale,
Evan Cheng51a9ed92006-02-25 10:09:08 +0000994 SDOperand &Index, SDOperand &Disp) {
995 X86ISelAddressMode AM;
996 if (MatchAddress(N, AM))
997 return false;
998
Evan Cheng25ab6902006-09-08 06:48:29 +0000999 MVT::ValueType VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001000 unsigned Complexity = 0;
1001 if (AM.BaseType == X86ISelAddressMode::RegBase)
1002 if (AM.Base.Reg.Val)
1003 Complexity = 1;
1004 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001005 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001006 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1007 Complexity = 4;
1008
1009 if (AM.IndexReg.Val)
1010 Complexity++;
1011 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001012 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001013
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001014 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1015 // a simple shift.
1016 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001017 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001018
1019 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1020 // to a LEA. This is determined with some expermentation but is by no means
1021 // optimal (especially for code size consideration). LEA is nice because of
1022 // its three-address nature. Tweak the cost function again when we can run
1023 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +00001024 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
1025 // For X86-64, we should always use lea to materialize RIP relative
1026 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001027 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001028 Complexity = 4;
1029 else
1030 Complexity += 2;
1031 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001032
1033 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
1034 Complexity++;
1035
1036 if (Complexity > 2) {
1037 getAddressOperands(AM, Base, Scale, Index, Disp);
1038 return true;
1039 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001040 return false;
1041}
1042
Evan Cheng5e351682006-02-06 06:02:33 +00001043bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
1044 SDOperand &Base, SDOperand &Scale,
1045 SDOperand &Index, SDOperand &Disp) {
Evan Cheng466685d2006-10-09 20:57:25 +00001046 if (ISD::isNON_EXTLoad(N.Val) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001047 N.hasOneUse() &&
Evan Cheng27e1fe92006-10-14 08:33:25 +00001048 CanBeFoldedBy(N.Val, P.Val, P.Val))
Evan Cheng0d538262006-11-08 20:34:28 +00001049 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
Evan Cheng0114e942006-01-06 20:36:21 +00001050 return false;
1051}
1052
Evan Cheng7ccced62006-02-18 00:15:05 +00001053/// getGlobalBaseReg - Output the instructions required to put the
1054/// base address to use for accessing globals into a register.
1055///
Evan Cheng9ade2182006-08-26 05:34:46 +00001056SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng25ab6902006-09-08 06:48:29 +00001057 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Evan Cheng7ccced62006-02-18 00:15:05 +00001058 if (!GlobalBaseReg) {
1059 // Insert the set of GlobalBaseReg into the first MBB of the function
Evan Cheng0475ab52008-01-05 00:41:47 +00001060 MachineFunction *MF = BB->getParent();
1061 MachineBasicBlock &FirstMBB = MF->front();
Evan Cheng7ccced62006-02-18 00:15:05 +00001062 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Evan Cheng0475ab52008-01-05 00:41:47 +00001063 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Chris Lattner84bc5422007-12-31 04:13:23 +00001064 unsigned PC = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001065
Evan Chengc0f64ff2006-11-27 23:37:22 +00001066 const TargetInstrInfo *TII = TM.getInstrInfo();
Evan Chengf02ca692007-12-22 02:26:46 +00001067 // Operand of MovePCtoStack is completely ignored by asm printer. It's
1068 // only used in JIT code emission as displacement to pc.
Evan Cheng0475ab52008-01-05 00:41:47 +00001069 BuildMI(FirstMBB, MBBI, TII->get(X86::MOVPC32r), PC).addImm(0);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001070
1071 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
1072 // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
Evan Cheng706535d2007-01-22 21:34:25 +00001073 if (TM.getRelocationModel() == Reloc::PIC_ &&
1074 Subtarget->isPICStyleGOT()) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001075 GlobalBaseReg = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng0475ab52008-01-05 00:41:47 +00001076 BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg)
1077 .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001078 } else {
1079 GlobalBaseReg = PC;
1080 }
1081
Evan Cheng7ccced62006-02-18 00:15:05 +00001082 }
Evan Cheng25ab6902006-09-08 06:48:29 +00001083 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
Evan Cheng7ccced62006-02-18 00:15:05 +00001084}
1085
Evan Chengb245d922006-05-20 01:36:52 +00001086static SDNode *FindCallStartFromCall(SDNode *Node) {
1087 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1088 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1089 "Node doesn't have a token chain argument!");
1090 return FindCallStartFromCall(Node->getOperand(0).Val);
1091}
1092
Christopher Lambc59e5212007-08-10 21:48:46 +00001093SDNode *X86DAGToDAGISel::getTruncate(SDOperand N0, MVT::ValueType VT) {
1094 SDOperand SRIdx;
1095 switch (VT) {
1096 case MVT::i8:
1097 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1098 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1099 if (!Subtarget->is64Bit()) {
1100 unsigned Opc;
1101 MVT::ValueType VT;
1102 switch (N0.getValueType()) {
1103 default: assert(0 && "Unknown truncate!");
1104 case MVT::i16:
1105 Opc = X86::MOV16to16_;
1106 VT = MVT::i16;
1107 break;
1108 case MVT::i32:
1109 Opc = X86::MOV32to32_;
1110 VT = MVT::i32;
1111 break;
1112 }
Evan Cheng96aaa542007-10-12 07:55:53 +00001113 N0 = SDOperand(CurDAG->getTargetNode(Opc, VT, MVT::Flag, N0), 0);
1114 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1115 VT, N0, SRIdx, N0.getValue(1));
Christopher Lambc59e5212007-08-10 21:48:46 +00001116 }
1117 break;
1118 case MVT::i16:
1119 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1120 break;
1121 case MVT::i32:
1122 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1123 break;
Evan Cheng96aaa542007-10-12 07:55:53 +00001124 default: assert(0 && "Unknown truncate!"); break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001125 }
Evan Cheng96aaa542007-10-12 07:55:53 +00001126 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, VT, N0, SRIdx);
Christopher Lambc59e5212007-08-10 21:48:46 +00001127}
1128
1129
Evan Cheng9ade2182006-08-26 05:34:46 +00001130SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Chengdef941b2005-12-15 01:02:48 +00001131 SDNode *Node = N.Val;
1132 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001133 unsigned Opc, MOpc;
1134 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +00001135
Evan Chengf597dc72006-02-10 22:24:32 +00001136#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001137 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001138 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001139 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001140 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001141#endif
1142
Evan Cheng34167212006-02-09 00:37:58 +00001143 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengf597dc72006-02-10 22:24:32 +00001144#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001145 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001146 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001147 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001148 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001149#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001150 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001151 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001152
Evan Cheng0114e942006-01-06 20:36:21 +00001153 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001154 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001155 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001156 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001157
Chris Lattner6fa2f9c2008-03-09 07:05:32 +00001158 case X86ISD::FP_GET_ST0_ST1: {
Evan Cheng0d9e9762008-01-29 19:34:22 +00001159 SDOperand Chain = N.getOperand(0);
1160 SDOperand InFlag = N.getOperand(1);
1161 AddToISelQueue(Chain);
1162 AddToISelQueue(InFlag);
1163 std::vector<MVT::ValueType> Tys;
1164 Tys.push_back(MVT::f80);
1165 Tys.push_back(MVT::f80);
1166 Tys.push_back(MVT::Other);
1167 Tys.push_back(MVT::Flag);
1168 SDOperand Ops[] = { Chain, InFlag };
Chris Lattner6fa2f9c2008-03-09 07:05:32 +00001169 SDNode *ResNode = CurDAG->getTargetNode(X86::FpGET_ST0_ST1, Tys,
Evan Cheng0d9e9762008-01-29 19:34:22 +00001170 Ops, 2);
1171 Chain = SDOperand(ResNode, 2);
1172 InFlag = SDOperand(ResNode, 3);
1173 ReplaceUses(SDOperand(N.Val, 2), Chain);
1174 ReplaceUses(SDOperand(N.Val, 3), InFlag);
1175 return ResNode;
1176 }
1177
Evan Cheng51a9ed92006-02-25 10:09:08 +00001178 case ISD::ADD: {
1179 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1180 // code and is matched first so to prevent it from being turned into
1181 // LEA32r X+c.
Evan Chengb1a9aec2008-01-08 02:06:11 +00001182 // In 64-bit small code size mode, use LEA to take advantage of
1183 // RIP-relative addressing.
1184 if (TM.getCodeModel() != CodeModel::Small)
1185 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001186 MVT::ValueType PtrVT = TLI.getPointerTy();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001187 SDOperand N0 = N.getOperand(0);
1188 SDOperand N1 = N.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00001189 if (N.Val->getValueType(0) == PtrVT &&
Evan Cheng19f2ffc2006-12-05 04:01:03 +00001190 N0.getOpcode() == X86ISD::Wrapper &&
Evan Cheng51a9ed92006-02-25 10:09:08 +00001191 N1.getOpcode() == ISD::Constant) {
1192 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1193 SDOperand C(0, 0);
1194 // TODO: handle ExternalSymbolSDNode.
1195 if (GlobalAddressSDNode *G =
1196 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001197 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001198 G->getOffset() + Offset);
1199 } else if (ConstantPoolSDNode *CP =
1200 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Chengc356a572006-09-12 21:04:05 +00001201 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001202 CP->getAlignment(),
1203 CP->getOffset()+Offset);
1204 }
1205
Evan Cheng25ab6902006-09-08 06:48:29 +00001206 if (C.Val) {
1207 if (Subtarget->is64Bit()) {
1208 SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
1209 CurDAG->getRegister(0, PtrVT), C };
1210 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1211 } else
1212 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1213 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001214 }
1215
1216 // Other cases are handled by auto-generated code.
1217 break;
Evan Chenga0ea0532006-02-23 02:43:52 +00001218 }
Evan Cheng020d2e82006-02-23 20:41:18 +00001219
Dan Gohman525178c2007-10-08 18:33:35 +00001220 case ISD::SMUL_LOHI:
1221 case ISD::UMUL_LOHI: {
1222 SDOperand N0 = Node->getOperand(0);
1223 SDOperand N1 = Node->getOperand(1);
1224
Dan Gohman525178c2007-10-08 18:33:35 +00001225 bool isSigned = Opcode == ISD::SMUL_LOHI;
1226 if (!isSigned)
Evan Cheng0114e942006-01-06 20:36:21 +00001227 switch (NVT) {
1228 default: assert(0 && "Unsupported VT!");
1229 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1230 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1231 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001232 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001233 }
1234 else
1235 switch (NVT) {
1236 default: assert(0 && "Unsupported VT!");
1237 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1238 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1239 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001240 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001241 }
1242
1243 unsigned LoReg, HiReg;
1244 switch (NVT) {
1245 default: assert(0 && "Unsupported VT!");
1246 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1247 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1248 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001249 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001250 }
1251
Evan Cheng0114e942006-01-06 20:36:21 +00001252 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng7afa1662007-08-02 05:48:35 +00001253 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman525178c2007-10-08 18:33:35 +00001254 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001255 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +00001256 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng7afa1662007-08-02 05:48:35 +00001257 if (foldedLoad)
1258 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001259 }
1260
Evan Cheng04699902006-08-26 01:05:16 +00001261 AddToISelQueue(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001262 SDOperand InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1263 N0, SDOperand()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001264
1265 if (foldedLoad) {
Dan Gohman525178c2007-10-08 18:33:35 +00001266 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001267 AddToISelQueue(Tmp0);
1268 AddToISelQueue(Tmp1);
1269 AddToISelQueue(Tmp2);
1270 AddToISelQueue(Tmp3);
Dan Gohman525178c2007-10-08 18:33:35 +00001271 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001272 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001273 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001274 InFlag = SDOperand(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001275 // Update the chain.
1276 ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001277 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001278 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001279 InFlag =
1280 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001281 }
1282
Dan Gohman525178c2007-10-08 18:33:35 +00001283 // Copy the low half of the result, if it is needed.
1284 if (!N.getValue(0).use_empty()) {
1285 SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1286 LoReg, NVT, InFlag);
1287 InFlag = Result.getValue(2);
1288 ReplaceUses(N.getValue(0), Result);
1289#ifndef NDEBUG
1290 DOUT << std::string(Indent-2, ' ') << "=> ";
1291 DEBUG(Result.Val->dump(CurDAG));
1292 DOUT << "\n";
1293#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001294 }
Dan Gohman525178c2007-10-08 18:33:35 +00001295 // Copy the high half of the result, if it is needed.
1296 if (!N.getValue(1).use_empty()) {
1297 SDOperand Result;
1298 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1299 // Prevent use of AH in a REX instruction by referencing AX instead.
1300 // Shift it down 8 bits.
1301 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1302 X86::AX, MVT::i16, InFlag);
1303 InFlag = Result.getValue(2);
1304 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1305 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1306 // Then truncate it down to i8.
1307 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1308 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1309 MVT::i8, Result, SRIdx), 0);
1310 } else {
1311 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1312 HiReg, NVT, InFlag);
1313 InFlag = Result.getValue(2);
1314 }
1315 ReplaceUses(N.getValue(1), Result);
1316#ifndef NDEBUG
1317 DOUT << std::string(Indent-2, ' ') << "=> ";
1318 DEBUG(Result.Val->dump(CurDAG));
1319 DOUT << "\n";
1320#endif
1321 }
Evan Cheng34167212006-02-09 00:37:58 +00001322
Evan Chengf597dc72006-02-10 22:24:32 +00001323#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001324 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001325#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001326
Evan Cheng64a752f2006-08-11 09:08:15 +00001327 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001328 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001329
Dan Gohman525178c2007-10-08 18:33:35 +00001330 case ISD::SDIVREM:
1331 case ISD::UDIVREM: {
1332 SDOperand N0 = Node->getOperand(0);
1333 SDOperand N1 = Node->getOperand(1);
1334
1335 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001336 if (!isSigned)
1337 switch (NVT) {
1338 default: assert(0 && "Unsupported VT!");
1339 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1340 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1341 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001342 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001343 }
1344 else
1345 switch (NVT) {
1346 default: assert(0 && "Unsupported VT!");
1347 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1348 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1349 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001350 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001351 }
1352
1353 unsigned LoReg, HiReg;
1354 unsigned ClrOpcode, SExtOpcode;
1355 switch (NVT) {
1356 default: assert(0 && "Unsupported VT!");
1357 case MVT::i8:
1358 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001359 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001360 SExtOpcode = X86::CBW;
1361 break;
1362 case MVT::i16:
1363 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001364 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001365 SExtOpcode = X86::CWD;
1366 break;
1367 case MVT::i32:
1368 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001369 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001370 SExtOpcode = X86::CDQ;
1371 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001372 case MVT::i64:
1373 LoReg = X86::RAX; HiReg = X86::RDX;
1374 ClrOpcode = X86::MOV64r0;
1375 SExtOpcode = X86::CQO;
1376 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001377 }
1378
Dan Gohman525178c2007-10-08 18:33:35 +00001379 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
1380 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1381
1382 SDOperand InFlag;
Evan Chengb1409ce2006-11-17 22:10:14 +00001383 if (NVT == MVT::i8 && !isSigned) {
1384 // Special case for div8, just use a move with zero extension to AX to
1385 // clear the upper 8 bits (AH).
1386 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
1387 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
1388 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
1389 AddToISelQueue(N0.getOperand(0));
1390 AddToISelQueue(Tmp0);
1391 AddToISelQueue(Tmp1);
1392 AddToISelQueue(Tmp2);
1393 AddToISelQueue(Tmp3);
1394 Move =
1395 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
1396 Ops, 5), 0);
1397 Chain = Move.getValue(1);
1398 ReplaceUses(N0.getValue(1), Chain);
1399 } else {
1400 AddToISelQueue(N0);
1401 Move =
1402 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
1403 Chain = CurDAG->getEntryNode();
1404 }
Dan Gohman525178c2007-10-08 18:33:35 +00001405 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDOperand());
Evan Cheng948f3432006-01-06 23:19:29 +00001406 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001407 } else {
1408 AddToISelQueue(N0);
1409 InFlag =
Dan Gohman525178c2007-10-08 18:33:35 +00001410 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
1411 LoReg, N0, SDOperand()).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001412 if (isSigned) {
1413 // Sign extend the low part into the high part.
1414 InFlag =
1415 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
1416 } else {
1417 // Zero out the high part, effectively zero extending the input.
1418 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001419 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1420 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001421 }
Evan Cheng948f3432006-01-06 23:19:29 +00001422 }
1423
1424 if (foldedLoad) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001425 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001426 AddToISelQueue(Tmp0);
1427 AddToISelQueue(Tmp1);
1428 AddToISelQueue(Tmp2);
1429 AddToISelQueue(Tmp3);
Evan Chengb1409ce2006-11-17 22:10:14 +00001430 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001431 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001432 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001433 InFlag = SDOperand(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001434 // Update the chain.
1435 ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001436 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001437 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001438 InFlag =
1439 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001440 }
1441
Dan Gohmana37c9f72007-09-25 18:23:27 +00001442 // Copy the division (low) result, if it is needed.
1443 if (!N.getValue(0).use_empty()) {
Dan Gohman525178c2007-10-08 18:33:35 +00001444 SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1445 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001446 InFlag = Result.getValue(2);
1447 ReplaceUses(N.getValue(0), Result);
1448#ifndef NDEBUG
1449 DOUT << std::string(Indent-2, ' ') << "=> ";
1450 DEBUG(Result.Val->dump(CurDAG));
1451 DOUT << "\n";
1452#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001453 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001454 // Copy the remainder (high) result, if it is needed.
1455 if (!N.getValue(1).use_empty()) {
1456 SDOperand Result;
1457 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1458 // Prevent use of AH in a REX instruction by referencing AX instead.
1459 // Shift it down 8 bits.
Dan Gohman525178c2007-10-08 18:33:35 +00001460 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1461 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001462 InFlag = Result.getValue(2);
1463 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1464 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1465 // Then truncate it down to i8.
1466 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1467 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1468 MVT::i8, Result, SRIdx), 0);
1469 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00001470 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1471 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001472 InFlag = Result.getValue(2);
1473 }
1474 ReplaceUses(N.getValue(1), Result);
1475#ifndef NDEBUG
1476 DOUT << std::string(Indent-2, ' ') << "=> ";
1477 DEBUG(Result.Val->dump(CurDAG));
1478 DOUT << "\n";
1479#endif
1480 }
Evan Chengf597dc72006-02-10 22:24:32 +00001481
1482#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001483 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001484#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001485
1486 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001487 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001488
1489 case ISD::ANY_EXTEND: {
1490 SDOperand N0 = Node->getOperand(0);
1491 AddToISelQueue(N0);
1492 if (NVT == MVT::i64 || NVT == MVT::i32 || NVT == MVT::i16) {
1493 SDOperand SRIdx;
Christopher Lamb3feb0172008-03-10 06:12:08 +00001494 SDOperand ImplVal = CurDAG->getTargetConstant(X86::IMPL_VAL_UNDEF,
1495 MVT::i32);
Christopher Lamba1eb1552007-08-10 22:22:41 +00001496 switch(N0.getValueType()) {
1497 case MVT::i32:
Christopher Lamb3feb0172008-03-10 06:12:08 +00001498 SRIdx = CurDAG->getTargetConstant(X86::SUBREG_32BIT, MVT::i32);
1499 // x86-64 zero extends 32-bit inserts int 64-bit registers
1500 if (Subtarget->is64Bit())
1501 ImplVal = CurDAG->getTargetConstant(X86::IMPL_VAL_ZERO, MVT::i32);
Christopher Lamba1eb1552007-08-10 22:22:41 +00001502 break;
1503 case MVT::i16:
Christopher Lamb3feb0172008-03-10 06:12:08 +00001504 SRIdx = CurDAG->getTargetConstant(X86::SUBREG_16BIT, MVT::i32);
Christopher Lamba1eb1552007-08-10 22:22:41 +00001505 break;
1506 case MVT::i8:
1507 if (Subtarget->is64Bit())
Christopher Lamb3feb0172008-03-10 06:12:08 +00001508 SRIdx = CurDAG->getTargetConstant(X86::SUBREG_8BIT, MVT::i32);
Christopher Lamba1eb1552007-08-10 22:22:41 +00001509 break;
1510 default: assert(0 && "Unknown any_extend!");
1511 }
1512 if (SRIdx.Val) {
Evan Cheng96aaa542007-10-12 07:55:53 +00001513 SDNode *ResNode = CurDAG->getTargetNode(X86::INSERT_SUBREG,
Christopher Lamb3feb0172008-03-10 06:12:08 +00001514 NVT, ImplVal, N0, SRIdx);
Christopher Lamba1eb1552007-08-10 22:22:41 +00001515
1516#ifndef NDEBUG
1517 DOUT << std::string(Indent-2, ' ') << "=> ";
1518 DEBUG(ResNode->dump(CurDAG));
1519 DOUT << "\n";
1520 Indent -= 2;
1521#endif
1522 return ResNode;
1523 } // Otherwise let generated ISel handle it.
1524 }
1525 break;
1526 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001527
1528 case ISD::SIGN_EXTEND_INREG: {
1529 SDOperand N0 = Node->getOperand(0);
1530 AddToISelQueue(N0);
Evan Cheng403be7e2006-05-08 08:01:26 +00001531
Christopher Lambc59e5212007-08-10 21:48:46 +00001532 MVT::ValueType SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
1533 SDOperand TruncOp = SDOperand(getTruncate(N0, SVT), 0);
Bill Wendling0d642872007-11-01 08:51:44 +00001534 unsigned Opc = 0;
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001535 switch (NVT) {
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001536 case MVT::i16:
Christopher Lambc59e5212007-08-10 21:48:46 +00001537 if (SVT == MVT::i8) Opc = X86::MOVSX16rr8;
1538 else assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001539 break;
1540 case MVT::i32:
Christopher Lambc59e5212007-08-10 21:48:46 +00001541 switch (SVT) {
1542 case MVT::i8: Opc = X86::MOVSX32rr8; break;
1543 case MVT::i16: Opc = X86::MOVSX32rr16; break;
1544 default: assert(0 && "Unknown sign_extend_inreg!");
1545 }
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001546 break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001547 case MVT::i64:
1548 switch (SVT) {
1549 case MVT::i8: Opc = X86::MOVSX64rr8; break;
1550 case MVT::i16: Opc = X86::MOVSX64rr16; break;
1551 case MVT::i32: Opc = X86::MOVSX64rr32; break;
1552 default: assert(0 && "Unknown sign_extend_inreg!");
1553 }
1554 break;
1555 default: assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001556 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001557
1558 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
1559
1560#ifndef NDEBUG
1561 DOUT << std::string(Indent-2, ' ') << "=> ";
1562 DEBUG(TruncOp.Val->dump(CurDAG));
1563 DOUT << "\n";
1564 DOUT << std::string(Indent-2, ' ') << "=> ";
1565 DEBUG(ResNode->dump(CurDAG));
1566 DOUT << "\n";
1567 Indent -= 2;
1568#endif
1569 return ResNode;
1570 break;
1571 }
1572
1573 case ISD::TRUNCATE: {
1574 SDOperand Input = Node->getOperand(0);
1575 AddToISelQueue(Node->getOperand(0));
1576 SDNode *ResNode = getTruncate(Input, NVT);
1577
Evan Cheng403be7e2006-05-08 08:01:26 +00001578#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001579 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001580 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001581 DOUT << "\n";
Evan Cheng403be7e2006-05-08 08:01:26 +00001582 Indent -= 2;
1583#endif
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001584 return ResNode;
Evan Cheng6b2e2542006-05-20 07:44:28 +00001585 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001586 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001587 }
1588
Evan Cheng9ade2182006-08-26 05:34:46 +00001589 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001590
Evan Chengf597dc72006-02-10 22:24:32 +00001591#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001592 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001593 if (ResNode == NULL || ResNode == N.Val)
1594 DEBUG(N.Val->dump(CurDAG));
1595 else
1596 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001597 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001598 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001599#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001600
1601 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001602}
1603
Chris Lattnerc0bad572006-06-08 18:03:49 +00001604bool X86DAGToDAGISel::
1605SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1606 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1607 SDOperand Op0, Op1, Op2, Op3;
1608 switch (ConstraintCode) {
1609 case 'o': // offsetable ??
1610 case 'v': // not offsetable ??
1611 default: return true;
1612 case 'm': // memory
Evan Cheng0d538262006-11-08 20:34:28 +00001613 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001614 return true;
1615 break;
1616 }
1617
Evan Cheng04699902006-08-26 01:05:16 +00001618 OutOps.push_back(Op0);
1619 OutOps.push_back(Op1);
1620 OutOps.push_back(Op2);
1621 OutOps.push_back(Op3);
1622 AddToISelQueue(Op0);
1623 AddToISelQueue(Op1);
1624 AddToISelQueue(Op2);
1625 AddToISelQueue(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001626 return false;
1627}
1628
Chris Lattnerc961eea2005-11-16 01:54:32 +00001629/// createX86ISelDag - This pass converts a legalized DAG into a
1630/// X86-specific DAG, ready for instruction scheduling.
1631///
Evan Chenge50794a2006-08-29 18:28:33 +00001632FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1633 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001634}