blob: 9bf9d122637c0d82178fbb5a7d4732f2937bee50 [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) {
Roman Levensteindc1adac2008-04-07 10:06:32 +0000228 SDNode *User = I->getUser();
Evan Chenga275ecb2006-10-10 01:46:56 +0000229 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 ||
Chris Lattner25453ea2008-04-25 05:13:01 +0000255 Use->getOpcode() == X86ISD::CMP ||
256 Use->getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
257 Use->getOpcode() == ISD::INTRINSIC_W_CHAIN ||
258 Use->getOpcode() == ISD::INTRINSIC_VOID);
Evan Cheng27e1fe92006-10-14 08:33:25 +0000259 continue;
260 }
Evan Chengf4b4c412006-08-08 00:31:00 +0000261 found = true;
262 break;
263 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000264 findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000265 }
266}
267
Evan Cheng27e1fe92006-10-14 08:33:25 +0000268/// isNonImmUse - Start searching from Root up the DAG to check is Def can
269/// be reached. Return true if that's the case. However, ignore direct uses
270/// by ImmedUse (which would be U in the example illustrated in
271/// CanBeFoldedBy) and by Root (which can happen in the store case).
272/// FIXME: to be really generic, we should allow direct use by any node
273/// that is being folded. But realisticly since we only fold loads which
274/// have one non-chain use, we only need to watch out for load/op/store
275/// and load/op/cmp case where the root (store / cmp) may reach the load via
276/// its chain operand.
277static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
278 SDNode *Skip = NULL) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000279 std::set<SDNode *> Visited;
280 bool found = false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000281 findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000282 return found;
283}
284
285
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000286bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
Evan Cheng27e1fe92006-10-14 08:33:25 +0000287 if (FastISel) return false;
288
Evan Chenga8df1b42006-07-27 16:44:36 +0000289 // If U use can somehow reach N through another path then U can't fold N or
290 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Cheng37e18032006-07-28 06:33:41 +0000291 // through X. If N is folded into into U, then X is both a predecessor and
Evan Chenga8df1b42006-07-27 16:44:36 +0000292 // a successor of U.
293 //
294 // [ N ]
295 // ^ ^
296 // | |
297 // / \---
298 // / [X]
299 // | ^
300 // [U]--------|
Evan Cheng27e1fe92006-10-14 08:33:25 +0000301
302 if (isNonImmUse(Root, N, U))
303 return false;
304
305 // If U produces a flag, then it gets (even more) interesting. Since it
306 // would have been "glued" together with its flag use, we need to check if
307 // it might reach N:
308 //
309 // [ N ]
310 // ^ ^
311 // | |
312 // [U] \--
313 // ^ [TF]
314 // | ^
315 // | |
316 // \ /
317 // [FU]
318 //
319 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
320 // NU), then TF is a predecessor of FU and a successor of NU. But since
321 // NU and FU are flagged together, this effectively creates a cycle.
322 bool HasFlagUse = false;
323 MVT::ValueType VT = Root->getValueType(Root->getNumValues()-1);
324 while ((VT == MVT::Flag && !Root->use_empty())) {
325 SDNode *FU = findFlagUse(Root);
326 if (FU == NULL)
327 break;
328 else {
329 Root = FU;
330 HasFlagUse = true;
Evan Chenga275ecb2006-10-10 01:46:56 +0000331 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000332 VT = Root->getValueType(Root->getNumValues()-1);
Evan Chenga275ecb2006-10-10 01:46:56 +0000333 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000334
335 if (HasFlagUse)
336 return !isNonImmUse(Root, N, Root, U);
337 return true;
Evan Chenga8df1b42006-07-27 16:44:36 +0000338}
339
Evan Cheng70e674e2006-08-28 20:10:17 +0000340/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
341/// and move load below the TokenFactor. Replace store's chain operand with
342/// load's chain result.
343static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
344 SDOperand Store, SDOperand TF) {
345 std::vector<SDOperand> Ops;
346 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
347 if (Load.Val == TF.Val->getOperand(i).Val)
348 Ops.push_back(Load.Val->getOperand(0));
349 else
350 Ops.push_back(TF.Val->getOperand(i));
351 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
352 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
353 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
354 Store.getOperand(2), Store.getOperand(3));
355}
356
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000357/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
358/// This is only run if not in -fast mode (aka -O0).
359/// This allows the instruction selector to pick more read-modify-write
360/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000361///
362/// [Load chain]
363/// ^
364/// |
365/// [Load]
366/// ^ ^
367/// | |
368/// / \-
369/// / |
370/// [TokenFactor] [Op]
371/// ^ ^
372/// | |
373/// \ /
374/// \ /
375/// [Store]
376///
377/// The fact the store's chain operand != load's chain will prevent the
378/// (store (op (load))) instruction from being selected. We can transform it to:
379///
380/// [Load chain]
381/// ^
382/// |
383/// [TokenFactor]
384/// ^
385/// |
386/// [Load]
387/// ^ ^
388/// | |
389/// | \-
390/// | |
391/// | [Op]
392/// | ^
393/// | |
394/// \ /
395/// \ /
396/// [Store]
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000397void X86DAGToDAGISel::PreprocessForRMW(SelectionDAG &DAG) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000398 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
399 E = DAG.allnodes_end(); I != E; ++I) {
Evan Cheng8b2794a2006-10-13 21:14:26 +0000400 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000401 continue;
402 SDOperand Chain = I->getOperand(0);
403 if (Chain.Val->getOpcode() != ISD::TokenFactor)
404 continue;
405
406 SDOperand N1 = I->getOperand(1);
407 SDOperand N2 = I->getOperand(2);
Evan Cheng1453de52006-09-01 22:52:28 +0000408 if (MVT::isFloatingPoint(N1.getValueType()) ||
409 MVT::isVector(N1.getValueType()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000410 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000411 continue;
412
413 bool RModW = false;
414 SDOperand Load;
415 unsigned Opcode = N1.Val->getOpcode();
416 switch (Opcode) {
417 case ISD::ADD:
418 case ISD::MUL:
Evan Cheng70e674e2006-08-28 20:10:17 +0000419 case ISD::AND:
420 case ISD::OR:
421 case ISD::XOR:
422 case ISD::ADDC:
423 case ISD::ADDE: {
424 SDOperand N10 = N1.getOperand(0);
425 SDOperand N11 = N1.getOperand(1);
Evan Cheng466685d2006-10-09 20:57:25 +0000426 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng70e674e2006-08-28 20:10:17 +0000427 RModW = true;
Evan Cheng466685d2006-10-09 20:57:25 +0000428 else if (ISD::isNON_EXTLoad(N11.Val)) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000429 RModW = true;
430 std::swap(N10, N11);
431 }
Evan Cheng07b7ea12008-03-04 00:40:35 +0000432 RModW = RModW && N10.Val->isOperandOf(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000433 (N10.getOperand(1) == N2) &&
434 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000435 if (RModW)
436 Load = N10;
437 break;
438 }
439 case ISD::SUB:
440 case ISD::SHL:
441 case ISD::SRA:
442 case ISD::SRL:
443 case ISD::ROTL:
444 case ISD::ROTR:
445 case ISD::SUBC:
446 case ISD::SUBE:
447 case X86ISD::SHLD:
448 case X86ISD::SHRD: {
449 SDOperand N10 = N1.getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +0000450 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng07b7ea12008-03-04 00:40:35 +0000451 RModW = N10.Val->isOperandOf(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000452 (N10.getOperand(1) == N2) &&
453 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000454 if (RModW)
455 Load = N10;
456 break;
457 }
458 }
459
Evan Cheng82a35b32006-08-29 06:44:17 +0000460 if (RModW) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000461 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000462 ++NumLoadMoved;
463 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000464 }
465}
466
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000467
468/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
469/// nodes that target the FP stack to be store and load to the stack. This is a
470/// gross hack. We would like to simply mark these as being illegal, but when
471/// we do that, legalize produces these when it expands calls, then expands
472/// these in the same legalize pass. We would like dag combine to be able to
473/// hack on these between the call expansion and the node legalization. As such
474/// this pass basically does "really late" legalization of these inline with the
475/// X86 isel pass.
476void X86DAGToDAGISel::PreprocessForFPConvert(SelectionDAG &DAG) {
477 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
478 E = DAG.allnodes_end(); I != E; ) {
479 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
480 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
481 continue;
482
483 // If the source and destination are SSE registers, then this is a legal
484 // conversion that should not be lowered.
485 MVT::ValueType SrcVT = N->getOperand(0).getValueType();
486 MVT::ValueType DstVT = N->getValueType(0);
487 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
488 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
489 if (SrcIsSSE && DstIsSSE)
490 continue;
491
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000492 if (!SrcIsSSE && !DstIsSSE) {
493 // If this is an FPStack extension, it is a noop.
494 if (N->getOpcode() == ISD::FP_EXTEND)
495 continue;
496 // If this is a value-preserving FPStack truncation, it is a noop.
497 if (N->getConstantOperandVal(1))
498 continue;
499 }
500
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000501 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
502 // FPStack has extload and truncstore. SSE can fold direct loads into other
503 // operations. Based on this, decide what we want to do.
504 MVT::ValueType MemVT;
505 if (N->getOpcode() == ISD::FP_ROUND)
506 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
507 else
508 MemVT = SrcIsSSE ? SrcVT : DstVT;
509
510 SDOperand MemTmp = DAG.CreateStackTemporary(MemVT);
511
512 // FIXME: optimize the case where the src/dest is a load or store?
513 SDOperand Store = DAG.getTruncStore(DAG.getEntryNode(), N->getOperand(0),
514 MemTmp, NULL, 0, MemVT);
515 SDOperand Result = DAG.getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp,
516 NULL, 0, MemVT);
517
518 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
519 // extload we created. This will cause general havok on the dag because
520 // anything below the conversion could be folded into other existing nodes.
521 // To avoid invalidating 'I', back it up to the convert node.
522 --I;
523 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result);
524
525 // Now that we did that, the node is dead. Increment the iterator to the
526 // next node to process, then delete N.
527 ++I;
528 DAG.DeleteNode(N);
529 }
530}
531
Chris Lattnerc961eea2005-11-16 01:54:32 +0000532/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
533/// when it has created a SelectionDAG for us to codegen.
534void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
535 DEBUG(BB->dump());
Chris Lattner92cb0af2006-01-11 01:15:34 +0000536 MachineFunction::iterator FirstMBB = BB;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000537
Evan Chenge50794a2006-08-29 18:28:33 +0000538 if (!FastISel)
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000539 PreprocessForRMW(DAG);
540
541 // FIXME: This should only happen when not -fast.
542 PreprocessForFPConvert(DAG);
Evan Cheng70e674e2006-08-28 20:10:17 +0000543
Chris Lattnerc961eea2005-11-16 01:54:32 +0000544 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000545#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000546 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000547 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000548#endif
Evan Chengba2f0a92006-02-05 06:46:41 +0000549 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengf597dc72006-02-10 22:24:32 +0000550#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000551 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000552#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000553
Chris Lattnerc961eea2005-11-16 01:54:32 +0000554 DAG.RemoveDeadNodes();
555
Chris Lattner03fdec02008-03-10 23:34:12 +0000556 // Emit machine code to BB. This can change 'BB' to the last block being
557 // inserted into.
Chris Lattnerc961eea2005-11-16 01:54:32 +0000558 ScheduleAndEmitDAG(DAG);
Chris Lattner92cb0af2006-01-11 01:15:34 +0000559
560 // If we are emitting FP stack code, scan the basic block to determine if this
561 // block defines any FP values. If so, put an FP_REG_KILL instruction before
562 // the terminator of the block.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000563
Dale Johannesen48d1e452007-09-24 22:52:39 +0000564 // Note that FP stack instructions are used in all modes for long double,
565 // so we always need to do this check.
566 // Also note that it's possible for an FP stack register to be live across
567 // an instruction that produces multiple basic blocks (SSE CMOV) so we
568 // must check all the generated basic blocks.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000569
570 // Scan all of the machine instructions in these MBBs, checking for FP
571 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
572 MachineFunction::iterator MBBI = FirstMBB;
Chris Lattner03fdec02008-03-10 23:34:12 +0000573 MachineFunction::iterator EndMBB = BB; ++EndMBB;
574 for (; MBBI != EndMBB; ++MBBI) {
575 MachineBasicBlock *MBB = MBBI;
576
577 // If this block returns, ignore it. We don't want to insert an FP_REG_KILL
578 // before the return.
579 if (!MBB->empty()) {
580 MachineBasicBlock::iterator EndI = MBB->end();
581 --EndI;
582 if (EndI->getDesc().isReturn())
583 continue;
584 }
585
Dale Johannesen48d1e452007-09-24 22:52:39 +0000586 bool ContainsFPCode = false;
Chris Lattner03fdec02008-03-10 23:34:12 +0000587 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000588 !ContainsFPCode && I != E; ++I) {
589 if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) {
590 const TargetRegisterClass *clas;
591 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
592 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
Chris Lattner03fdec02008-03-10 23:34:12 +0000593 TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
Chris Lattner84bc5422007-12-31 04:13:23 +0000594 ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) ==
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000595 X86::RFP32RegisterClass ||
596 clas == X86::RFP64RegisterClass ||
597 clas == X86::RFP80RegisterClass)) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000598 ContainsFPCode = true;
599 break;
600 }
601 }
602 }
603 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000604 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
605 // a copy of the input value in this block. In SSE mode, we only care about
606 // 80-bit values.
607 if (!ContainsFPCode) {
608 // Final check, check LLVM BB's that are successors to the LLVM BB
609 // corresponding to BB for FP PHI nodes.
610 const BasicBlock *LLVMBB = BB->getBasicBlock();
611 const PHINode *PN;
612 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
613 !ContainsFPCode && SI != E; ++SI) {
614 for (BasicBlock::const_iterator II = SI->begin();
615 (PN = dyn_cast<PHINode>(II)); ++II) {
616 if (PN->getType()==Type::X86_FP80Ty ||
617 (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) ||
618 (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) {
619 ContainsFPCode = true;
620 break;
621 }
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000622 }
623 }
Chris Lattner92cb0af2006-01-11 01:15:34 +0000624 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000625 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
626 if (ContainsFPCode) {
Chris Lattner03fdec02008-03-10 23:34:12 +0000627 BuildMI(*MBB, MBBI->getFirstTerminator(),
Dale Johannesen48d1e452007-09-24 22:52:39 +0000628 TM.getInstrInfo()->get(X86::FP_REG_KILL));
629 ++NumFPKill;
630 }
Chris Lattner03fdec02008-03-10 23:34:12 +0000631 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000632}
633
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000634/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
635/// the main function.
636void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
637 MachineFrameInfo *MFI) {
638 const TargetInstrInfo *TII = TM.getInstrInfo();
639 if (Subtarget->isTargetCygMing())
640 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
641}
642
643void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
644 // If this is main, emit special code for main.
645 MachineBasicBlock *BB = MF.begin();
646 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
647 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
648}
649
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000650/// MatchAddress - Add the specified node to the specified addressing mode,
651/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000652/// addressing mode.
Evan Cheng2486af12006-02-11 02:05:36 +0000653bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000654 bool isRoot, unsigned Depth) {
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000655 // Limit recursion.
656 if (Depth > 5)
657 return MatchAddressBase(N, AM, isRoot, Depth);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000658
Evan Cheng25ab6902006-09-08 06:48:29 +0000659 // RIP relative addressing: %rip + 32-bit displacement!
660 if (AM.isRIPRel) {
661 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000662 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000663 if (isInt32(AM.Disp + Val)) {
664 AM.Disp += Val;
665 return false;
666 }
667 }
668 return true;
669 }
670
Evan Cheng2ef88a02006-08-07 22:28:20 +0000671 int id = N.Val->getNodeId();
Evan Cheng1314b002007-12-13 00:43:27 +0000672 bool AlreadySelected = isSelected(id); // Already selected, not yet replaced.
Evan Cheng2486af12006-02-11 02:05:36 +0000673
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000674 switch (N.getOpcode()) {
675 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000676 case ISD::Constant: {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000677 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000678 if (isInt32(AM.Disp + Val)) {
679 AM.Disp += Val;
680 return false;
681 }
682 break;
683 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000684
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000685 case X86ISD::Wrapper: {
686 bool is64Bit = Subtarget->is64Bit();
Evan Cheng0085a282006-11-30 21:55:46 +0000687 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Chengbe3bf422008-02-07 08:53:49 +0000688 // Also, base and index reg must be 0 in order to use rip as base.
689 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
690 AM.Base.Reg.Val || AM.IndexReg.Val))
Evan Cheng0085a282006-11-30 21:55:46 +0000691 break;
Evan Cheng28b514392006-12-05 19:50:18 +0000692 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
693 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000694 // If value is available in a register both base and index components have
695 // been picked, we can't fit the result available in the register in the
696 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Evan Cheng1314b002007-12-13 00:43:27 +0000697 if (!AlreadySelected || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
Evan Cheng28b514392006-12-05 19:50:18 +0000698 SDOperand N0 = N.getOperand(0);
699 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
700 GlobalValue *GV = G->getGlobal();
Evan Chengbe3bf422008-02-07 08:53:49 +0000701 AM.GV = GV;
702 AM.Disp += G->getOffset();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000703 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
704 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000705 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000706 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000707 AM.CP = CP->getConstVal();
708 AM.Align = CP->getAlignment();
709 AM.Disp += CP->getOffset();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000710 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
711 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000712 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000713 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000714 AM.ES = S->getSymbol();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000715 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
716 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000717 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000718 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000719 AM.JT = J->getIndex();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000720 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
721 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000722 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000723 }
724 }
725 break;
Evan Cheng0085a282006-11-30 21:55:46 +0000726 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000727
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000728 case ISD::FrameIndex:
729 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
730 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
731 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
732 return false;
733 }
734 break;
Evan Chengec693f72005-12-08 02:01:35 +0000735
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000736 case ISD::SHL:
Evan Chengbe3bf422008-02-07 08:53:49 +0000737 if (AlreadySelected || AM.IndexReg.Val != 0 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000738 break;
739
740 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
741 unsigned Val = CN->getValue();
742 if (Val == 1 || Val == 2 || Val == 3) {
743 AM.Scale = 1 << Val;
744 SDOperand ShVal = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000745
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000746 // Okay, we know that we have a scale by now. However, if the scaled
747 // value is an add of something and a constant, we can fold the
748 // constant into the disp field here.
749 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
750 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
751 AM.IndexReg = ShVal.Val->getOperand(0);
752 ConstantSDNode *AddVal =
753 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
754 uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
755 if (isInt32(Disp))
756 AM.Disp = Disp;
757 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000758 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000759 } else {
760 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000761 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000762 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000763 }
764 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000765 }
Evan Chengec693f72005-12-08 02:01:35 +0000766
Dan Gohman83688052007-10-22 20:22:24 +0000767 case ISD::SMUL_LOHI:
768 case ISD::UMUL_LOHI:
769 // A mul_lohi where we need the low part can be folded as a plain multiply.
770 if (N.ResNo != 0) break;
771 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000772 case ISD::MUL:
773 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng1314b002007-12-13 00:43:27 +0000774 if (!AlreadySelected &&
Evan Cheng51a9ed92006-02-25 10:09:08 +0000775 AM.BaseType == X86ISelAddressMode::RegBase &&
776 AM.Base.Reg.Val == 0 &&
Evan Chengbe3bf422008-02-07 08:53:49 +0000777 AM.IndexReg.Val == 0 &&
778 !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000779 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
780 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
781 AM.Scale = unsigned(CN->getValue())-1;
782
783 SDOperand MulVal = N.Val->getOperand(0);
784 SDOperand Reg;
785
786 // Okay, we know that we have a scale by now. However, if the scaled
787 // value is an add of something and a constant, we can fold the
788 // constant into the disp field here.
789 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
790 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
791 Reg = MulVal.Val->getOperand(0);
792 ConstantSDNode *AddVal =
793 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
Evan Cheng25ab6902006-09-08 06:48:29 +0000794 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
795 if (isInt32(Disp))
796 AM.Disp = Disp;
797 else
798 Reg = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000799 } else {
800 Reg = N.Val->getOperand(0);
801 }
802
803 AM.IndexReg = AM.Base.Reg = Reg;
804 return false;
805 }
Chris Lattner62412262007-02-04 20:18:17 +0000806 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000807 break;
808
Chris Lattner62412262007-02-04 20:18:17 +0000809 case ISD::ADD:
Evan Cheng1314b002007-12-13 00:43:27 +0000810 if (!AlreadySelected) {
Evan Cheng2486af12006-02-11 02:05:36 +0000811 X86ISelAddressMode Backup = AM;
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000812 if (!MatchAddress(N.Val->getOperand(0), AM, false, Depth+1) &&
813 !MatchAddress(N.Val->getOperand(1), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000814 return false;
815 AM = Backup;
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000816 if (!MatchAddress(N.Val->getOperand(1), AM, false, Depth+1) &&
817 !MatchAddress(N.Val->getOperand(0), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000818 return false;
819 AM = Backup;
820 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000821 break;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000822
Chris Lattner62412262007-02-04 20:18:17 +0000823 case ISD::OR:
824 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Evan Cheng1314b002007-12-13 00:43:27 +0000825 if (AlreadySelected) break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000826
827 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
828 X86ISelAddressMode Backup = AM;
829 // Start with the LHS as an addr mode.
830 if (!MatchAddress(N.getOperand(0), AM, false) &&
831 // Address could not have picked a GV address for the displacement.
832 AM.GV == NULL &&
833 // On x86-64, the resultant disp must fit in 32-bits.
834 isInt32(AM.Disp + CN->getSignExtended()) &&
835 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000836 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000837 AM.Disp += CN->getValue();
838 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000839 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000840 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000841 }
842 break;
Evan Cheng1314b002007-12-13 00:43:27 +0000843
844 case ISD::AND: {
845 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
846 // allows us to fold the shift into this addressing mode.
847 if (AlreadySelected) break;
848 SDOperand Shift = N.getOperand(0);
849 if (Shift.getOpcode() != ISD::SHL) break;
850
851 // Scale must not be used already.
852 if (AM.IndexReg.Val != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +0000853
854 // Not when RIP is used as the base.
855 if (AM.isRIPRel) break;
Evan Cheng1314b002007-12-13 00:43:27 +0000856
857 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
858 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
859 if (!C1 || !C2) break;
860
861 // Not likely to be profitable if either the AND or SHIFT node has more
862 // than one use (unless all uses are for address computation). Besides,
863 // isel mechanism requires their node ids to be reused.
864 if (!N.hasOneUse() || !Shift.hasOneUse())
865 break;
866
867 // Verify that the shift amount is something we can fold.
868 unsigned ShiftCst = C1->getValue();
869 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
870 break;
871
872 // Get the new AND mask, this folds to a constant.
873 SDOperand NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
874 SDOperand(C2, 0), SDOperand(C1, 0));
875 SDOperand NewAND = CurDAG->getNode(ISD::AND, N.getValueType(),
876 Shift.getOperand(0), NewANDMask);
877 NewANDMask.Val->setNodeId(Shift.Val->getNodeId());
878 NewAND.Val->setNodeId(N.Val->getNodeId());
879
880 AM.Scale = 1 << ShiftCst;
881 AM.IndexReg = NewAND;
882 return false;
883 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000884 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000885
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000886 return MatchAddressBase(N, AM, isRoot, Depth);
887}
888
889/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
890/// specified addressing mode without any further recursion.
891bool X86DAGToDAGISel::MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
892 bool isRoot, unsigned Depth) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000893 // Is the base register already occupied?
894 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
895 // If so, check to see if the scale index register is set.
Evan Chengbe3bf422008-02-07 08:53:49 +0000896 if (AM.IndexReg.Val == 0 && !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000897 AM.IndexReg = N;
898 AM.Scale = 1;
899 return false;
900 }
901
902 // Otherwise, we cannot select it.
903 return true;
904 }
905
906 // Default, generate it as a register.
907 AM.BaseType = X86ISelAddressMode::RegBase;
908 AM.Base.Reg = N;
909 return false;
910}
911
Evan Chengec693f72005-12-08 02:01:35 +0000912/// SelectAddr - returns true if it is able pattern match an addressing mode.
913/// It returns the operands which make up the maximal addressing mode it can
914/// match by reference.
Evan Cheng0d538262006-11-08 20:34:28 +0000915bool X86DAGToDAGISel::SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
916 SDOperand &Scale, SDOperand &Index,
917 SDOperand &Disp) {
Evan Chengec693f72005-12-08 02:01:35 +0000918 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +0000919 if (MatchAddress(N, AM))
920 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000921
Evan Cheng25ab6902006-09-08 06:48:29 +0000922 MVT::ValueType VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +0000923 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Cheng7dd281b2006-02-05 05:25:07 +0000924 if (!AM.Base.Reg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000925 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +0000926 }
Evan Cheng8700e142006-01-11 06:09:51 +0000927
Evan Cheng7dd281b2006-02-05 05:25:07 +0000928 if (!AM.IndexReg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000929 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +0000930
931 getAddressOperands(AM, Base, Scale, Index, Disp);
932 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000933}
934
Chris Lattner4fe4f252006-10-11 22:09:58 +0000935/// isZeroNode - Returns true if Elt is a constant zero or a floating point
936/// constant +0.0.
937static inline bool isZeroNode(SDOperand Elt) {
938 return ((isa<ConstantSDNode>(Elt) &&
939 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
940 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +0000941 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Chris Lattner4fe4f252006-10-11 22:09:58 +0000942}
943
944
Chris Lattner3a7cd952006-10-07 21:55:32 +0000945/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
946/// match a load whose top elements are either undef or zeros. The load flavor
947/// is derived from the type of N, which is either v4f32 or v2f64.
Evan Cheng0d538262006-11-08 20:34:28 +0000948bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Cheng07e4b002006-10-16 06:34:55 +0000949 SDOperand N, SDOperand &Base,
Evan Cheng82a91642006-10-11 21:06:01 +0000950 SDOperand &Scale, SDOperand &Index,
951 SDOperand &Disp, SDOperand &InChain,
952 SDOperand &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +0000953 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +0000954 InChain = N.getOperand(0).getValue(1);
Evan Cheng07e4b002006-10-16 06:34:55 +0000955 if (ISD::isNON_EXTLoad(InChain.Val) &&
956 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +0000957 N.hasOneUse() &&
Evan Cheng0d538262006-11-08 20:34:28 +0000958 CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
Evan Cheng82a91642006-10-11 21:06:01 +0000959 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Evan Cheng0d538262006-11-08 20:34:28 +0000960 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner3a7cd952006-10-07 21:55:32 +0000961 return false;
Evan Cheng82a91642006-10-11 21:06:01 +0000962 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +0000963 return true;
964 }
965 }
Chris Lattner4fe4f252006-10-11 22:09:58 +0000966
967 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +0000968 // elements. This is a vector shuffle from the zero vector.
Chris Lattner4fe4f252006-10-11 22:09:58 +0000969 if (N.getOpcode() == ISD::VECTOR_SHUFFLE && N.Val->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +0000970 // Check to see if the top elements are all zeros (or bitcast of zeros).
971 ISD::isBuildVectorAllZeros(N.getOperand(0).Val) &&
Chris Lattner4fe4f252006-10-11 22:09:58 +0000972 N.getOperand(1).getOpcode() == ISD::SCALAR_TO_VECTOR &&
973 N.getOperand(1).Val->hasOneUse() &&
974 ISD::isNON_EXTLoad(N.getOperand(1).getOperand(0).Val) &&
975 N.getOperand(1).getOperand(0).hasOneUse()) {
Chris Lattner4fe4f252006-10-11 22:09:58 +0000976 // Check to see if the shuffle mask is 4/L/L/L or 2/L, where L is something
977 // from the LHS.
Chris Lattner8a594482007-11-25 00:24:49 +0000978 unsigned VecWidth=MVT::getVectorNumElements(N.getOperand(0).getValueType());
Chris Lattner4fe4f252006-10-11 22:09:58 +0000979 SDOperand ShufMask = N.getOperand(2);
980 assert(ShufMask.getOpcode() == ISD::BUILD_VECTOR && "Invalid shuf mask!");
981 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(ShufMask.getOperand(0))) {
982 if (C->getValue() == VecWidth) {
983 for (unsigned i = 1; i != VecWidth; ++i) {
984 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF) {
985 // ok.
986 } else {
987 ConstantSDNode *C = cast<ConstantSDNode>(ShufMask.getOperand(i));
988 if (C->getValue() >= VecWidth) return false;
989 }
990 }
991 }
992
993 // Okay, this is a zero extending load. Fold it.
994 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(1).getOperand(0));
Evan Cheng0d538262006-11-08 20:34:28 +0000995 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner4fe4f252006-10-11 22:09:58 +0000996 return false;
997 OutChain = LD->getChain();
998 InChain = SDOperand(LD, 1);
999 return true;
1000 }
1001 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001002 return false;
1003}
1004
1005
Evan Cheng51a9ed92006-02-25 10:09:08 +00001006/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1007/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng0d538262006-11-08 20:34:28 +00001008bool X86DAGToDAGISel::SelectLEAAddr(SDOperand Op, SDOperand N,
1009 SDOperand &Base, SDOperand &Scale,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001010 SDOperand &Index, SDOperand &Disp) {
1011 X86ISelAddressMode AM;
1012 if (MatchAddress(N, AM))
1013 return false;
1014
Evan Cheng25ab6902006-09-08 06:48:29 +00001015 MVT::ValueType VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001016 unsigned Complexity = 0;
1017 if (AM.BaseType == X86ISelAddressMode::RegBase)
1018 if (AM.Base.Reg.Val)
1019 Complexity = 1;
1020 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001021 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001022 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1023 Complexity = 4;
1024
1025 if (AM.IndexReg.Val)
1026 Complexity++;
1027 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001028 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001029
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001030 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1031 // a simple shift.
1032 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001033 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001034
1035 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1036 // to a LEA. This is determined with some expermentation but is by no means
1037 // optimal (especially for code size consideration). LEA is nice because of
1038 // its three-address nature. Tweak the cost function again when we can run
1039 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +00001040 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
1041 // For X86-64, we should always use lea to materialize RIP relative
1042 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001043 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001044 Complexity = 4;
1045 else
1046 Complexity += 2;
1047 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001048
1049 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
1050 Complexity++;
1051
1052 if (Complexity > 2) {
1053 getAddressOperands(AM, Base, Scale, Index, Disp);
1054 return true;
1055 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001056 return false;
1057}
1058
Evan Cheng5e351682006-02-06 06:02:33 +00001059bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
1060 SDOperand &Base, SDOperand &Scale,
1061 SDOperand &Index, SDOperand &Disp) {
Evan Cheng466685d2006-10-09 20:57:25 +00001062 if (ISD::isNON_EXTLoad(N.Val) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001063 N.hasOneUse() &&
Evan Cheng27e1fe92006-10-14 08:33:25 +00001064 CanBeFoldedBy(N.Val, P.Val, P.Val))
Evan Cheng0d538262006-11-08 20:34:28 +00001065 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
Evan Cheng0114e942006-01-06 20:36:21 +00001066 return false;
1067}
1068
Evan Cheng7ccced62006-02-18 00:15:05 +00001069/// getGlobalBaseReg - Output the instructions required to put the
1070/// base address to use for accessing globals into a register.
1071///
Evan Cheng9ade2182006-08-26 05:34:46 +00001072SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng25ab6902006-09-08 06:48:29 +00001073 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Evan Cheng7ccced62006-02-18 00:15:05 +00001074 if (!GlobalBaseReg) {
1075 // Insert the set of GlobalBaseReg into the first MBB of the function
Evan Cheng0475ab52008-01-05 00:41:47 +00001076 MachineFunction *MF = BB->getParent();
1077 MachineBasicBlock &FirstMBB = MF->front();
Evan Cheng7ccced62006-02-18 00:15:05 +00001078 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Evan Cheng0475ab52008-01-05 00:41:47 +00001079 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Chris Lattner84bc5422007-12-31 04:13:23 +00001080 unsigned PC = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001081
Evan Chengc0f64ff2006-11-27 23:37:22 +00001082 const TargetInstrInfo *TII = TM.getInstrInfo();
Evan Chengf02ca692007-12-22 02:26:46 +00001083 // Operand of MovePCtoStack is completely ignored by asm printer. It's
1084 // only used in JIT code emission as displacement to pc.
Evan Cheng0475ab52008-01-05 00:41:47 +00001085 BuildMI(FirstMBB, MBBI, TII->get(X86::MOVPC32r), PC).addImm(0);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001086
1087 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
1088 // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
Evan Cheng706535d2007-01-22 21:34:25 +00001089 if (TM.getRelocationModel() == Reloc::PIC_ &&
1090 Subtarget->isPICStyleGOT()) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001091 GlobalBaseReg = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng0475ab52008-01-05 00:41:47 +00001092 BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg)
1093 .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001094 } else {
1095 GlobalBaseReg = PC;
1096 }
1097
Evan Cheng7ccced62006-02-18 00:15:05 +00001098 }
Evan Cheng25ab6902006-09-08 06:48:29 +00001099 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
Evan Cheng7ccced62006-02-18 00:15:05 +00001100}
1101
Evan Chengb245d922006-05-20 01:36:52 +00001102static SDNode *FindCallStartFromCall(SDNode *Node) {
1103 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1104 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1105 "Node doesn't have a token chain argument!");
1106 return FindCallStartFromCall(Node->getOperand(0).Val);
1107}
1108
Christopher Lambc59e5212007-08-10 21:48:46 +00001109SDNode *X86DAGToDAGISel::getTruncate(SDOperand N0, MVT::ValueType VT) {
1110 SDOperand SRIdx;
1111 switch (VT) {
1112 case MVT::i8:
1113 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1114 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1115 if (!Subtarget->is64Bit()) {
1116 unsigned Opc;
1117 MVT::ValueType VT;
1118 switch (N0.getValueType()) {
1119 default: assert(0 && "Unknown truncate!");
1120 case MVT::i16:
1121 Opc = X86::MOV16to16_;
1122 VT = MVT::i16;
1123 break;
1124 case MVT::i32:
1125 Opc = X86::MOV32to32_;
1126 VT = MVT::i32;
1127 break;
1128 }
Evan Cheng96aaa542007-10-12 07:55:53 +00001129 N0 = SDOperand(CurDAG->getTargetNode(Opc, VT, MVT::Flag, N0), 0);
1130 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1131 VT, N0, SRIdx, N0.getValue(1));
Christopher Lambc59e5212007-08-10 21:48:46 +00001132 }
1133 break;
1134 case MVT::i16:
1135 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1136 break;
1137 case MVT::i32:
1138 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1139 break;
Evan Cheng96aaa542007-10-12 07:55:53 +00001140 default: assert(0 && "Unknown truncate!"); break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001141 }
Evan Cheng96aaa542007-10-12 07:55:53 +00001142 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, VT, N0, SRIdx);
Christopher Lambc59e5212007-08-10 21:48:46 +00001143}
1144
1145
Evan Cheng9ade2182006-08-26 05:34:46 +00001146SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Chengdef941b2005-12-15 01:02:48 +00001147 SDNode *Node = N.Val;
1148 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001149 unsigned Opc, MOpc;
1150 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +00001151
Evan Chengf597dc72006-02-10 22:24:32 +00001152#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001153 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001154 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001155 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001156 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001157#endif
1158
Evan Cheng34167212006-02-09 00:37:58 +00001159 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengf597dc72006-02-10 22:24:32 +00001160#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001161 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001162 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001163 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001164 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001165#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001166 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001167 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001168
Evan Cheng0114e942006-01-06 20:36:21 +00001169 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001170 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001171 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001172 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001173
Chris Lattner447ff682008-03-11 03:23:40 +00001174 // FIXME: This is a workaround for a tblgen problem: rdar://5791600
1175 case X86ISD::RET_FLAG:
1176 if (ConstantSDNode *Amt = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1177 if (Amt->getSignExtended() != 0) break;
1178
1179 // Match (X86retflag 0).
1180 SDOperand Chain = N.getOperand(0);
1181 bool HasInFlag = N.getOperand(N.getNumOperands()-1).getValueType()
1182 == MVT::Flag;
1183 SmallVector<SDOperand, 8> Ops0;
1184 AddToISelQueue(Chain);
1185 SDOperand InFlag(0, 0);
1186 if (HasInFlag) {
1187 InFlag = N.getOperand(N.getNumOperands()-1);
1188 AddToISelQueue(InFlag);
1189 }
1190 for (unsigned i = 2, e = N.getNumOperands()-(HasInFlag?1:0); i != e;
1191 ++i) {
1192 AddToISelQueue(N.getOperand(i));
1193 Ops0.push_back(N.getOperand(i));
1194 }
1195 Ops0.push_back(Chain);
1196 if (HasInFlag)
1197 Ops0.push_back(InFlag);
1198 return CurDAG->getTargetNode(X86::RET, MVT::Other,
1199 &Ops0[0], Ops0.size());
1200 }
1201 break;
1202
Evan Cheng51a9ed92006-02-25 10:09:08 +00001203 case ISD::ADD: {
1204 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1205 // code and is matched first so to prevent it from being turned into
1206 // LEA32r X+c.
Evan Chengb1a9aec2008-01-08 02:06:11 +00001207 // In 64-bit small code size mode, use LEA to take advantage of
1208 // RIP-relative addressing.
1209 if (TM.getCodeModel() != CodeModel::Small)
1210 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001211 MVT::ValueType PtrVT = TLI.getPointerTy();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001212 SDOperand N0 = N.getOperand(0);
1213 SDOperand N1 = N.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00001214 if (N.Val->getValueType(0) == PtrVT &&
Evan Cheng19f2ffc2006-12-05 04:01:03 +00001215 N0.getOpcode() == X86ISD::Wrapper &&
Evan Cheng51a9ed92006-02-25 10:09:08 +00001216 N1.getOpcode() == ISD::Constant) {
1217 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1218 SDOperand C(0, 0);
1219 // TODO: handle ExternalSymbolSDNode.
1220 if (GlobalAddressSDNode *G =
1221 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001222 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001223 G->getOffset() + Offset);
1224 } else if (ConstantPoolSDNode *CP =
1225 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Chengc356a572006-09-12 21:04:05 +00001226 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001227 CP->getAlignment(),
1228 CP->getOffset()+Offset);
1229 }
1230
Evan Cheng25ab6902006-09-08 06:48:29 +00001231 if (C.Val) {
1232 if (Subtarget->is64Bit()) {
1233 SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
1234 CurDAG->getRegister(0, PtrVT), C };
1235 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1236 } else
1237 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1238 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001239 }
1240
1241 // Other cases are handled by auto-generated code.
1242 break;
Evan Chenga0ea0532006-02-23 02:43:52 +00001243 }
Evan Cheng020d2e82006-02-23 20:41:18 +00001244
Dan Gohman525178c2007-10-08 18:33:35 +00001245 case ISD::SMUL_LOHI:
1246 case ISD::UMUL_LOHI: {
1247 SDOperand N0 = Node->getOperand(0);
1248 SDOperand N1 = Node->getOperand(1);
1249
Dan Gohman525178c2007-10-08 18:33:35 +00001250 bool isSigned = Opcode == ISD::SMUL_LOHI;
1251 if (!isSigned)
Evan Cheng0114e942006-01-06 20:36:21 +00001252 switch (NVT) {
1253 default: assert(0 && "Unsupported VT!");
1254 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1255 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1256 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001257 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001258 }
1259 else
1260 switch (NVT) {
1261 default: assert(0 && "Unsupported VT!");
1262 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1263 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1264 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001265 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001266 }
1267
1268 unsigned LoReg, HiReg;
1269 switch (NVT) {
1270 default: assert(0 && "Unsupported VT!");
1271 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1272 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1273 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001274 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001275 }
1276
Evan Cheng0114e942006-01-06 20:36:21 +00001277 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng7afa1662007-08-02 05:48:35 +00001278 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman525178c2007-10-08 18:33:35 +00001279 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001280 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +00001281 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng7afa1662007-08-02 05:48:35 +00001282 if (foldedLoad)
1283 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001284 }
1285
Evan Cheng04699902006-08-26 01:05:16 +00001286 AddToISelQueue(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001287 SDOperand InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1288 N0, SDOperand()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001289
1290 if (foldedLoad) {
Dan Gohman525178c2007-10-08 18:33:35 +00001291 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001292 AddToISelQueue(Tmp0);
1293 AddToISelQueue(Tmp1);
1294 AddToISelQueue(Tmp2);
1295 AddToISelQueue(Tmp3);
Dan Gohman525178c2007-10-08 18:33:35 +00001296 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001297 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001298 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001299 InFlag = SDOperand(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001300 // Update the chain.
1301 ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001302 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001303 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001304 InFlag =
1305 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001306 }
1307
Dan Gohman525178c2007-10-08 18:33:35 +00001308 // Copy the low half of the result, if it is needed.
1309 if (!N.getValue(0).use_empty()) {
1310 SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1311 LoReg, NVT, InFlag);
1312 InFlag = Result.getValue(2);
1313 ReplaceUses(N.getValue(0), Result);
1314#ifndef NDEBUG
1315 DOUT << std::string(Indent-2, ' ') << "=> ";
1316 DEBUG(Result.Val->dump(CurDAG));
1317 DOUT << "\n";
1318#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001319 }
Dan Gohman525178c2007-10-08 18:33:35 +00001320 // Copy the high half of the result, if it is needed.
1321 if (!N.getValue(1).use_empty()) {
1322 SDOperand Result;
1323 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1324 // Prevent use of AH in a REX instruction by referencing AX instead.
1325 // Shift it down 8 bits.
1326 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1327 X86::AX, MVT::i16, InFlag);
1328 InFlag = Result.getValue(2);
1329 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1330 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1331 // Then truncate it down to i8.
1332 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1333 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1334 MVT::i8, Result, SRIdx), 0);
1335 } else {
1336 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1337 HiReg, NVT, InFlag);
1338 InFlag = Result.getValue(2);
1339 }
1340 ReplaceUses(N.getValue(1), Result);
1341#ifndef NDEBUG
1342 DOUT << std::string(Indent-2, ' ') << "=> ";
1343 DEBUG(Result.Val->dump(CurDAG));
1344 DOUT << "\n";
1345#endif
1346 }
Evan Cheng34167212006-02-09 00:37:58 +00001347
Evan Chengf597dc72006-02-10 22:24:32 +00001348#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001349 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001350#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001351
Evan Cheng64a752f2006-08-11 09:08:15 +00001352 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001353 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001354
Dan Gohman525178c2007-10-08 18:33:35 +00001355 case ISD::SDIVREM:
1356 case ISD::UDIVREM: {
1357 SDOperand N0 = Node->getOperand(0);
1358 SDOperand N1 = Node->getOperand(1);
1359
1360 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001361 if (!isSigned)
1362 switch (NVT) {
1363 default: assert(0 && "Unsupported VT!");
1364 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1365 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1366 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001367 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001368 }
1369 else
1370 switch (NVT) {
1371 default: assert(0 && "Unsupported VT!");
1372 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1373 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1374 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001375 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001376 }
1377
1378 unsigned LoReg, HiReg;
1379 unsigned ClrOpcode, SExtOpcode;
1380 switch (NVT) {
1381 default: assert(0 && "Unsupported VT!");
1382 case MVT::i8:
1383 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001384 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001385 SExtOpcode = X86::CBW;
1386 break;
1387 case MVT::i16:
1388 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001389 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001390 SExtOpcode = X86::CWD;
1391 break;
1392 case MVT::i32:
1393 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001394 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001395 SExtOpcode = X86::CDQ;
1396 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001397 case MVT::i64:
1398 LoReg = X86::RAX; HiReg = X86::RDX;
1399 ClrOpcode = X86::MOV64r0;
1400 SExtOpcode = X86::CQO;
1401 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001402 }
1403
Dan Gohman525178c2007-10-08 18:33:35 +00001404 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
1405 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1406
1407 SDOperand InFlag;
Evan Chengb1409ce2006-11-17 22:10:14 +00001408 if (NVT == MVT::i8 && !isSigned) {
1409 // Special case for div8, just use a move with zero extension to AX to
1410 // clear the upper 8 bits (AH).
1411 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
1412 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
1413 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
1414 AddToISelQueue(N0.getOperand(0));
1415 AddToISelQueue(Tmp0);
1416 AddToISelQueue(Tmp1);
1417 AddToISelQueue(Tmp2);
1418 AddToISelQueue(Tmp3);
1419 Move =
1420 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
1421 Ops, 5), 0);
1422 Chain = Move.getValue(1);
1423 ReplaceUses(N0.getValue(1), Chain);
1424 } else {
1425 AddToISelQueue(N0);
1426 Move =
1427 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
1428 Chain = CurDAG->getEntryNode();
1429 }
Dan Gohman525178c2007-10-08 18:33:35 +00001430 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDOperand());
Evan Cheng948f3432006-01-06 23:19:29 +00001431 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001432 } else {
1433 AddToISelQueue(N0);
1434 InFlag =
Dan Gohman525178c2007-10-08 18:33:35 +00001435 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
1436 LoReg, N0, SDOperand()).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001437 if (isSigned) {
1438 // Sign extend the low part into the high part.
1439 InFlag =
1440 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
1441 } else {
1442 // Zero out the high part, effectively zero extending the input.
1443 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001444 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1445 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001446 }
Evan Cheng948f3432006-01-06 23:19:29 +00001447 }
1448
1449 if (foldedLoad) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001450 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001451 AddToISelQueue(Tmp0);
1452 AddToISelQueue(Tmp1);
1453 AddToISelQueue(Tmp2);
1454 AddToISelQueue(Tmp3);
Evan Chengb1409ce2006-11-17 22:10:14 +00001455 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001456 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001457 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001458 InFlag = SDOperand(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001459 // Update the chain.
1460 ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001461 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001462 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001463 InFlag =
1464 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001465 }
1466
Dan Gohmana37c9f72007-09-25 18:23:27 +00001467 // Copy the division (low) result, if it is needed.
1468 if (!N.getValue(0).use_empty()) {
Dan Gohman525178c2007-10-08 18:33:35 +00001469 SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1470 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001471 InFlag = Result.getValue(2);
1472 ReplaceUses(N.getValue(0), Result);
1473#ifndef NDEBUG
1474 DOUT << std::string(Indent-2, ' ') << "=> ";
1475 DEBUG(Result.Val->dump(CurDAG));
1476 DOUT << "\n";
1477#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001478 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001479 // Copy the remainder (high) result, if it is needed.
1480 if (!N.getValue(1).use_empty()) {
1481 SDOperand Result;
1482 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1483 // Prevent use of AH in a REX instruction by referencing AX instead.
1484 // Shift it down 8 bits.
Dan Gohman525178c2007-10-08 18:33:35 +00001485 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1486 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001487 InFlag = Result.getValue(2);
1488 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1489 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1490 // Then truncate it down to i8.
1491 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1492 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1493 MVT::i8, Result, SRIdx), 0);
1494 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00001495 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1496 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001497 InFlag = Result.getValue(2);
1498 }
1499 ReplaceUses(N.getValue(1), Result);
1500#ifndef NDEBUG
1501 DOUT << std::string(Indent-2, ' ') << "=> ";
1502 DEBUG(Result.Val->dump(CurDAG));
1503 DOUT << "\n";
1504#endif
1505 }
Evan Chengf597dc72006-02-10 22:24:32 +00001506
1507#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001508 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001509#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001510
1511 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001512 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001513
1514 case ISD::ANY_EXTEND: {
Christopher Lambc9298232008-03-16 03:12:01 +00001515 // Check if the type extended to supports subregs.
1516 if (NVT == MVT::i8)
1517 break;
1518
Christopher Lamba1eb1552007-08-10 22:22:41 +00001519 SDOperand N0 = Node->getOperand(0);
Christopher Lambc9298232008-03-16 03:12:01 +00001520 // Get the subregsiter index for the type to extend.
1521 MVT::ValueType N0VT = N0.getValueType();
1522 unsigned Idx = (N0VT == MVT::i32) ? X86::SUBREG_32BIT :
1523 (N0VT == MVT::i16) ? X86::SUBREG_16BIT :
1524 (Subtarget->is64Bit()) ? X86::SUBREG_8BIT : 0;
1525
1526 // If we don't have a subreg Idx, let generated ISel have a try.
1527 if (Idx == 0)
1528 break;
1529
1530 // If we have an index, generate an insert_subreg into undef.
Christopher Lamba1eb1552007-08-10 22:22:41 +00001531 AddToISelQueue(N0);
Christopher Lambc9298232008-03-16 03:12:01 +00001532 SDOperand Undef =
Evan Cheng90ce87b2008-04-03 07:45:18 +00001533 SDOperand(CurDAG->getTargetNode(X86::IMPLICIT_DEF, NVT), 0);
Christopher Lambc9298232008-03-16 03:12:01 +00001534 SDOperand SRIdx = CurDAG->getTargetConstant(Idx, MVT::i32);
1535 SDNode *ResNode = CurDAG->getTargetNode(X86::INSERT_SUBREG,
Evan Cheng90ce87b2008-04-03 07:45:18 +00001536 NVT, Undef, N0, SRIdx);
Christopher Lamba1eb1552007-08-10 22:22:41 +00001537
1538#ifndef NDEBUG
Christopher Lambc9298232008-03-16 03:12:01 +00001539 DOUT << std::string(Indent-2, ' ') << "=> ";
1540 DEBUG(ResNode->dump(CurDAG));
1541 DOUT << "\n";
1542 Indent -= 2;
Christopher Lamba1eb1552007-08-10 22:22:41 +00001543#endif
Christopher Lambc9298232008-03-16 03:12:01 +00001544 return ResNode;
Christopher Lamba1eb1552007-08-10 22:22:41 +00001545 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001546
1547 case ISD::SIGN_EXTEND_INREG: {
1548 SDOperand N0 = Node->getOperand(0);
1549 AddToISelQueue(N0);
Evan Cheng403be7e2006-05-08 08:01:26 +00001550
Christopher Lambc59e5212007-08-10 21:48:46 +00001551 MVT::ValueType SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
1552 SDOperand TruncOp = SDOperand(getTruncate(N0, SVT), 0);
Bill Wendling0d642872007-11-01 08:51:44 +00001553 unsigned Opc = 0;
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001554 switch (NVT) {
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001555 case MVT::i16:
Christopher Lambc59e5212007-08-10 21:48:46 +00001556 if (SVT == MVT::i8) Opc = X86::MOVSX16rr8;
1557 else assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001558 break;
1559 case MVT::i32:
Christopher Lambc59e5212007-08-10 21:48:46 +00001560 switch (SVT) {
1561 case MVT::i8: Opc = X86::MOVSX32rr8; break;
1562 case MVT::i16: Opc = X86::MOVSX32rr16; break;
1563 default: assert(0 && "Unknown sign_extend_inreg!");
1564 }
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001565 break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001566 case MVT::i64:
1567 switch (SVT) {
1568 case MVT::i8: Opc = X86::MOVSX64rr8; break;
1569 case MVT::i16: Opc = X86::MOVSX64rr16; break;
1570 case MVT::i32: Opc = X86::MOVSX64rr32; break;
1571 default: assert(0 && "Unknown sign_extend_inreg!");
1572 }
1573 break;
1574 default: assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001575 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001576
1577 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
1578
1579#ifndef NDEBUG
1580 DOUT << std::string(Indent-2, ' ') << "=> ";
1581 DEBUG(TruncOp.Val->dump(CurDAG));
1582 DOUT << "\n";
1583 DOUT << std::string(Indent-2, ' ') << "=> ";
1584 DEBUG(ResNode->dump(CurDAG));
1585 DOUT << "\n";
1586 Indent -= 2;
1587#endif
1588 return ResNode;
1589 break;
1590 }
1591
1592 case ISD::TRUNCATE: {
1593 SDOperand Input = Node->getOperand(0);
1594 AddToISelQueue(Node->getOperand(0));
1595 SDNode *ResNode = getTruncate(Input, NVT);
1596
Evan Cheng403be7e2006-05-08 08:01:26 +00001597#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001598 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001599 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001600 DOUT << "\n";
Evan Cheng403be7e2006-05-08 08:01:26 +00001601 Indent -= 2;
1602#endif
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001603 return ResNode;
Evan Cheng6b2e2542006-05-20 07:44:28 +00001604 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001605 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001606 }
1607
Evan Cheng9ade2182006-08-26 05:34:46 +00001608 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001609
Evan Chengf597dc72006-02-10 22:24:32 +00001610#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001611 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001612 if (ResNode == NULL || ResNode == N.Val)
1613 DEBUG(N.Val->dump(CurDAG));
1614 else
1615 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001616 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001617 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001618#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001619
1620 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001621}
1622
Chris Lattnerc0bad572006-06-08 18:03:49 +00001623bool X86DAGToDAGISel::
1624SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1625 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1626 SDOperand Op0, Op1, Op2, Op3;
1627 switch (ConstraintCode) {
1628 case 'o': // offsetable ??
1629 case 'v': // not offsetable ??
1630 default: return true;
1631 case 'm': // memory
Evan Cheng0d538262006-11-08 20:34:28 +00001632 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001633 return true;
1634 break;
1635 }
1636
Evan Cheng04699902006-08-26 01:05:16 +00001637 OutOps.push_back(Op0);
1638 OutOps.push_back(Op1);
1639 OutOps.push_back(Op2);
1640 OutOps.push_back(Op3);
1641 AddToISelQueue(Op0);
1642 AddToISelQueue(Op1);
1643 AddToISelQueue(Op2);
1644 AddToISelQueue(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001645 return false;
1646}
1647
Chris Lattnerc961eea2005-11-16 01:54:32 +00001648/// createX86ISelDag - This pass converts a legalized DAG into a
1649/// X86-specific DAG, ready for instruction scheduling.
1650///
Evan Chenge50794a2006-08-29 18:28:33 +00001651FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1652 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001653}