blob: 3a4496f002557785552e7c623630fa341a747027 [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"
Evan Chengcdda25d2008-04-25 08:22:20 +000039#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000040#include "llvm/ADT/Statistic.h"
Evan Cheng2ef88a02006-08-07 22:28:20 +000041#include <queue>
Evan Chengba2f0a92006-02-05 06:46:41 +000042#include <set>
Chris Lattnerc961eea2005-11-16 01:54:32 +000043using namespace llvm;
44
Chris Lattner95b2c7d2006-12-19 22:59:26 +000045STATISTIC(NumFPKill , "Number of FP_REG_KILL instructions added");
46STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
47
Chris Lattnerc961eea2005-11-16 01:54:32 +000048//===----------------------------------------------------------------------===//
49// Pattern Matcher Implementation
50//===----------------------------------------------------------------------===//
51
52namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000053 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
54 /// SDOperand's instead of register numbers for the leaves of the matched
55 /// tree.
56 struct X86ISelAddressMode {
57 enum {
58 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000059 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000060 } BaseType;
61
62 struct { // This is really a union, discriminated by BaseType!
63 SDOperand Reg;
64 int FrameIndex;
65 } Base;
66
Evan Chengbe3bf422008-02-07 08:53:49 +000067 bool isRIPRel; // RIP as base?
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000068 unsigned Scale;
69 SDOperand IndexReg;
70 unsigned Disp;
71 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000072 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000073 const char *ES;
74 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000075 unsigned Align; // CP alignment.
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000076
77 X86ISelAddressMode()
Evan Cheng25ab6902006-09-08 06:48:29 +000078 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
79 GV(0), CP(0), ES(0), JT(-1), Align(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000080 }
81 };
82}
83
84namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +000085 //===--------------------------------------------------------------------===//
86 /// ISel - X86 specific code to select X86 machine instructions for
87 /// SelectionDAG operations.
88 ///
Chris Lattner2c79de82006-06-28 23:27:49 +000089 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +000090 /// ContainsFPCode - Every instruction we select that uses or defines a FP
91 /// register should set this to true.
92 bool ContainsFPCode;
93
Evan Chenge50794a2006-08-29 18:28:33 +000094 /// FastISel - Enable fast(er) instruction selection.
95 ///
96 bool FastISel;
97
Evan Cheng25ab6902006-09-08 06:48:29 +000098 /// TM - Keep a reference to X86TargetMachine.
99 ///
100 X86TargetMachine &TM;
101
Chris Lattnerc961eea2005-11-16 01:54:32 +0000102 /// X86Lowering - This object fully describes how to lower LLVM code to an
103 /// X86-specific SelectionDAG.
104 X86TargetLowering X86Lowering;
105
106 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
107 /// make the right decision when generating code for different targets.
108 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000109
Evan Cheng25ab6902006-09-08 06:48:29 +0000110 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
111 /// base register.
Evan Cheng7ccced62006-02-18 00:15:05 +0000112 unsigned GlobalBaseReg;
Evan Chenga8df1b42006-07-27 16:44:36 +0000113
Chris Lattnerc961eea2005-11-16 01:54:32 +0000114 public:
Evan Cheng25ab6902006-09-08 06:48:29 +0000115 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Evan Chengc4c62572006-03-13 23:20:37 +0000116 : SelectionDAGISel(X86Lowering),
Evan Cheng25ab6902006-09-08 06:48:29 +0000117 ContainsFPCode(false), FastISel(fast), TM(tm),
Evan Chenga8df1b42006-07-27 16:44:36 +0000118 X86Lowering(*TM.getTargetLowering()),
Evan Chengf4b4c412006-08-08 00:31:00 +0000119 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000120
Evan Cheng7ccced62006-02-18 00:15:05 +0000121 virtual bool runOnFunction(Function &Fn) {
122 // Make sure we re-emit a set of the global base reg if necessary
123 GlobalBaseReg = 0;
124 return SelectionDAGISel::runOnFunction(Fn);
125 }
126
Chris Lattnerc961eea2005-11-16 01:54:32 +0000127 virtual const char *getPassName() const {
128 return "X86 DAG->DAG Instruction Selection";
129 }
130
131 /// InstructionSelectBasicBlock - This callback is invoked by
132 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
133 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
134
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000135 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
136
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000137 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000138
Chris Lattnerc961eea2005-11-16 01:54:32 +0000139// Include the pieces autogenerated from the target description.
140#include "X86GenDAGISel.inc"
141
142 private:
Evan Cheng9ade2182006-08-26 05:34:46 +0000143 SDNode *Select(SDOperand N);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000144
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000145 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM,
Anton Korobeynikovf6e93532007-03-28 18:38:33 +0000146 bool isRoot = true, unsigned Depth = 0);
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000147 bool MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
148 bool isRoot, unsigned Depth);
Evan Cheng0d538262006-11-08 20:34:28 +0000149 bool SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
150 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
151 bool SelectLEAAddr(SDOperand Op, SDOperand N, SDOperand &Base,
152 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
153 bool SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Cheng07e4b002006-10-16 06:34:55 +0000154 SDOperand N, SDOperand &Base, SDOperand &Scale,
Evan Cheng82a91642006-10-11 21:06:01 +0000155 SDOperand &Index, SDOperand &Disp,
156 SDOperand &InChain, SDOperand &OutChain);
Evan Cheng5e351682006-02-06 06:02:33 +0000157 bool TryFoldLoad(SDOperand P, SDOperand N,
158 SDOperand &Base, SDOperand &Scale,
Evan Cheng0114e942006-01-06 20:36:21 +0000159 SDOperand &Index, SDOperand &Disp);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000160 void PreprocessForRMW(SelectionDAG &DAG);
161 void PreprocessForFPConvert(SelectionDAG &DAG);
Evan Cheng2ef88a02006-08-07 22:28:20 +0000162
Chris Lattnerc0bad572006-06-08 18:03:49 +0000163 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
164 /// inline asm expressions.
165 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
166 char ConstraintCode,
167 std::vector<SDOperand> &OutOps,
168 SelectionDAG &DAG);
169
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000170 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
171
Evan Chenge5280532005-12-12 21:49:40 +0000172 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
173 SDOperand &Scale, SDOperand &Index,
174 SDOperand &Disp) {
175 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000176 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
177 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000178 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000179 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000180 // These are 32-bit even in 64-bit mode since RIP relative offset
181 // is 32-bit.
182 if (AM.GV)
183 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
184 else if (AM.CP)
185 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp);
186 else if (AM.ES)
187 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
188 else if (AM.JT != -1)
189 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
190 else
191 Disp = getI32Imm(AM.Disp);
Evan Chenge5280532005-12-12 21:49:40 +0000192 }
193
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000194 /// getI8Imm - Return a target constant with the specified value, of type
195 /// i8.
196 inline SDOperand getI8Imm(unsigned Imm) {
197 return CurDAG->getTargetConstant(Imm, MVT::i8);
198 }
199
Chris Lattnerc961eea2005-11-16 01:54:32 +0000200 /// getI16Imm - Return a target constant with the specified value, of type
201 /// i16.
202 inline SDOperand getI16Imm(unsigned Imm) {
203 return CurDAG->getTargetConstant(Imm, MVT::i16);
204 }
205
206 /// getI32Imm - Return a target constant with the specified value, of type
207 /// i32.
208 inline SDOperand getI32Imm(unsigned Imm) {
209 return CurDAG->getTargetConstant(Imm, MVT::i32);
210 }
Evan Chengf597dc72006-02-10 22:24:32 +0000211
Evan Cheng7ccced62006-02-18 00:15:05 +0000212 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
213 /// base register. Return the virtual register that holds this value.
Evan Cheng9ade2182006-08-26 05:34:46 +0000214 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000215
Christopher Lambc59e5212007-08-10 21:48:46 +0000216 /// getTruncate - return an SDNode that implements a subreg based truncate
217 /// of the specified operand to the the specified value type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000218 SDNode *getTruncate(SDOperand N0, MVT VT);
Christopher Lambc59e5212007-08-10 21:48:46 +0000219
Evan Cheng23addc02006-02-10 22:46:26 +0000220#ifndef NDEBUG
221 unsigned Indent;
222#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000223 };
224}
225
Evan Chengcdda25d2008-04-25 08:22:20 +0000226/// findFlagUse - Return use of MVT::Flag value produced by the specified SDNode.
227///
Evan Chenga275ecb2006-10-10 01:46:56 +0000228static SDNode *findFlagUse(SDNode *N) {
229 unsigned FlagResNo = N->getNumValues()-1;
230 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +0000231 SDNode *User = I->getUser();
Evan Chenga275ecb2006-10-10 01:46:56 +0000232 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
233 SDOperand Op = User->getOperand(i);
Evan Cheng494cec62006-10-12 19:13:59 +0000234 if (Op.Val == N && Op.ResNo == FlagResNo)
Evan Chenga275ecb2006-10-10 01:46:56 +0000235 return User;
236 }
237 }
238 return NULL;
239}
240
Evan Chengcdda25d2008-04-25 08:22:20 +0000241/// findNonImmUse - Return true by reference in "found" if "Use" is an
242/// non-immediate use of "Def". This function recursively traversing
243/// up the operand chain ignoring certain nodes.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000244static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
245 SDNode *Root, SDNode *Skip, bool &found,
Evan Chengcdda25d2008-04-25 08:22:20 +0000246 SmallPtrSet<SDNode*, 16> &Visited) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000247 if (found ||
248 Use->getNodeId() > Def->getNodeId() ||
Evan Chengcdda25d2008-04-25 08:22:20 +0000249 !Visited.insert(Use))
Evan Chengf4b4c412006-08-08 00:31:00 +0000250 return;
Evan Chengcdda25d2008-04-25 08:22:20 +0000251
Evan Cheng27e1fe92006-10-14 08:33:25 +0000252 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000253 SDNode *N = Use->getOperand(i).Val;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000254 if (N == Skip)
Evan Chenga275ecb2006-10-10 01:46:56 +0000255 continue;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000256 if (N == Def) {
257 if (Use == ImmedUse)
Evan Cheng419ace92008-04-25 08:55:28 +0000258 continue; // We are not looking for immediate use.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000259 if (Use == Root) {
Evan Cheng419ace92008-04-25 08:55:28 +0000260 // Must be a chain reading node where it is possible to reach its own
261 // chain operand through a path started from another operand.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000262 assert(Use->getOpcode() == ISD::STORE ||
Chris Lattner25453ea2008-04-25 05:13:01 +0000263 Use->getOpcode() == X86ISD::CMP ||
Chris Lattner25453ea2008-04-25 05:13:01 +0000264 Use->getOpcode() == ISD::INTRINSIC_W_CHAIN ||
265 Use->getOpcode() == ISD::INTRINSIC_VOID);
Evan Cheng27e1fe92006-10-14 08:33:25 +0000266 continue;
267 }
Evan Chengf4b4c412006-08-08 00:31:00 +0000268 found = true;
269 break;
270 }
Evan Chengcdda25d2008-04-25 08:22:20 +0000271
272 // Traverse up the operand chain.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000273 findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000274 }
275}
276
Evan Cheng27e1fe92006-10-14 08:33:25 +0000277/// isNonImmUse - Start searching from Root up the DAG to check is Def can
278/// be reached. Return true if that's the case. However, ignore direct uses
279/// by ImmedUse (which would be U in the example illustrated in
280/// CanBeFoldedBy) and by Root (which can happen in the store case).
281/// FIXME: to be really generic, we should allow direct use by any node
282/// that is being folded. But realisticly since we only fold loads which
283/// have one non-chain use, we only need to watch out for load/op/store
284/// and load/op/cmp case where the root (store / cmp) may reach the load via
285/// its chain operand.
286static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
287 SDNode *Skip = NULL) {
Evan Chengcdda25d2008-04-25 08:22:20 +0000288 SmallPtrSet<SDNode*, 16> Visited;
Evan Chengf4b4c412006-08-08 00:31:00 +0000289 bool found = false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000290 findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000291 return found;
292}
293
294
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000295bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
Evan Cheng27e1fe92006-10-14 08:33:25 +0000296 if (FastISel) return false;
297
Evan Chenga8df1b42006-07-27 16:44:36 +0000298 // If U use can somehow reach N through another path then U can't fold N or
299 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Cheng37e18032006-07-28 06:33:41 +0000300 // through X. If N is folded into into U, then X is both a predecessor and
Evan Chenga8df1b42006-07-27 16:44:36 +0000301 // a successor of U.
302 //
303 // [ N ]
304 // ^ ^
305 // | |
306 // / \---
307 // / [X]
308 // | ^
309 // [U]--------|
Evan Cheng27e1fe92006-10-14 08:33:25 +0000310
311 if (isNonImmUse(Root, N, U))
312 return false;
313
314 // If U produces a flag, then it gets (even more) interesting. Since it
315 // would have been "glued" together with its flag use, we need to check if
316 // it might reach N:
317 //
318 // [ N ]
319 // ^ ^
320 // | |
321 // [U] \--
322 // ^ [TF]
323 // | ^
324 // | |
325 // \ /
326 // [FU]
327 //
328 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
329 // NU), then TF is a predecessor of FU and a successor of NU. But since
330 // NU and FU are flagged together, this effectively creates a cycle.
331 bool HasFlagUse = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000332 MVT VT = Root->getValueType(Root->getNumValues()-1);
Evan Cheng27e1fe92006-10-14 08:33:25 +0000333 while ((VT == MVT::Flag && !Root->use_empty())) {
334 SDNode *FU = findFlagUse(Root);
335 if (FU == NULL)
336 break;
337 else {
338 Root = FU;
339 HasFlagUse = true;
Evan Chenga275ecb2006-10-10 01:46:56 +0000340 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000341 VT = Root->getValueType(Root->getNumValues()-1);
Evan Chenga275ecb2006-10-10 01:46:56 +0000342 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000343
344 if (HasFlagUse)
345 return !isNonImmUse(Root, N, Root, U);
346 return true;
Evan Chenga8df1b42006-07-27 16:44:36 +0000347}
348
Evan Cheng70e674e2006-08-28 20:10:17 +0000349/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
350/// and move load below the TokenFactor. Replace store's chain operand with
351/// load's chain result.
352static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
353 SDOperand Store, SDOperand TF) {
354 std::vector<SDOperand> Ops;
355 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
356 if (Load.Val == TF.Val->getOperand(i).Val)
357 Ops.push_back(Load.Val->getOperand(0));
358 else
359 Ops.push_back(TF.Val->getOperand(i));
360 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
361 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
362 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
363 Store.getOperand(2), Store.getOperand(3));
364}
365
Evan Chengcd0baf22008-05-23 21:23:16 +0000366/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
367///
368static bool isRMWLoad(SDOperand N, SDOperand Chain, SDOperand Address,
369 SDOperand &Load) {
370 if (N.getOpcode() == ISD::BIT_CONVERT)
371 N = N.getOperand(0);
372
373 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
374 if (!LD || LD->isVolatile())
375 return false;
376 if (LD->getAddressingMode() != ISD::UNINDEXED)
377 return false;
378
379 ISD::LoadExtType ExtType = LD->getExtensionType();
380 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
381 return false;
382
383 if (N.hasOneUse() &&
384 N.getOperand(1) == Address &&
385 N.Val->isOperandOf(Chain.Val)) {
386 Load = N;
387 return true;
388 }
389 return false;
390}
391
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000392/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
393/// This is only run if not in -fast mode (aka -O0).
394/// This allows the instruction selector to pick more read-modify-write
395/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000396///
397/// [Load chain]
398/// ^
399/// |
400/// [Load]
401/// ^ ^
402/// | |
403/// / \-
404/// / |
405/// [TokenFactor] [Op]
406/// ^ ^
407/// | |
408/// \ /
409/// \ /
410/// [Store]
411///
412/// The fact the store's chain operand != load's chain will prevent the
413/// (store (op (load))) instruction from being selected. We can transform it to:
414///
415/// [Load chain]
416/// ^
417/// |
418/// [TokenFactor]
419/// ^
420/// |
421/// [Load]
422/// ^ ^
423/// | |
424/// | \-
425/// | |
426/// | [Op]
427/// | ^
428/// | |
429/// \ /
430/// \ /
431/// [Store]
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000432void X86DAGToDAGISel::PreprocessForRMW(SelectionDAG &DAG) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000433 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
434 E = DAG.allnodes_end(); I != E; ++I) {
Evan Cheng8b2794a2006-10-13 21:14:26 +0000435 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000436 continue;
437 SDOperand Chain = I->getOperand(0);
438 if (Chain.Val->getOpcode() != ISD::TokenFactor)
439 continue;
440
441 SDOperand N1 = I->getOperand(1);
442 SDOperand N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000443 if ((N1.getValueType().isFloatingPoint() &&
444 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000445 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000446 continue;
447
448 bool RModW = false;
449 SDOperand Load;
450 unsigned Opcode = N1.Val->getOpcode();
451 switch (Opcode) {
452 case ISD::ADD:
453 case ISD::MUL:
Evan Cheng70e674e2006-08-28 20:10:17 +0000454 case ISD::AND:
455 case ISD::OR:
456 case ISD::XOR:
457 case ISD::ADDC:
Evan Chengcd0baf22008-05-23 21:23:16 +0000458 case ISD::ADDE:
459 case ISD::VECTOR_SHUFFLE: {
Evan Cheng70e674e2006-08-28 20:10:17 +0000460 SDOperand N10 = N1.getOperand(0);
461 SDOperand N11 = N1.getOperand(1);
Evan Chengcd0baf22008-05-23 21:23:16 +0000462 RModW = isRMWLoad(N10, Chain, N2, Load);
463 if (!RModW)
464 RModW = isRMWLoad(N11, Chain, N2, Load);
Evan Cheng70e674e2006-08-28 20:10:17 +0000465 break;
466 }
467 case ISD::SUB:
468 case ISD::SHL:
469 case ISD::SRA:
470 case ISD::SRL:
471 case ISD::ROTL:
472 case ISD::ROTR:
473 case ISD::SUBC:
474 case ISD::SUBE:
475 case X86ISD::SHLD:
476 case X86ISD::SHRD: {
477 SDOperand N10 = N1.getOperand(0);
Evan Chengcd0baf22008-05-23 21:23:16 +0000478 RModW = isRMWLoad(N10, Chain, N2, Load);
Evan Cheng70e674e2006-08-28 20:10:17 +0000479 break;
480 }
481 }
482
Evan Cheng82a35b32006-08-29 06:44:17 +0000483 if (RModW) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000484 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000485 ++NumLoadMoved;
486 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000487 }
488}
489
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000490
491/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
492/// nodes that target the FP stack to be store and load to the stack. This is a
493/// gross hack. We would like to simply mark these as being illegal, but when
494/// we do that, legalize produces these when it expands calls, then expands
495/// these in the same legalize pass. We would like dag combine to be able to
496/// hack on these between the call expansion and the node legalization. As such
497/// this pass basically does "really late" legalization of these inline with the
498/// X86 isel pass.
499void X86DAGToDAGISel::PreprocessForFPConvert(SelectionDAG &DAG) {
500 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
501 E = DAG.allnodes_end(); I != E; ) {
502 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
503 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
504 continue;
505
506 // If the source and destination are SSE registers, then this is a legal
507 // conversion that should not be lowered.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000508 MVT SrcVT = N->getOperand(0).getValueType();
509 MVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000510 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
511 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
512 if (SrcIsSSE && DstIsSSE)
513 continue;
514
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000515 if (!SrcIsSSE && !DstIsSSE) {
516 // If this is an FPStack extension, it is a noop.
517 if (N->getOpcode() == ISD::FP_EXTEND)
518 continue;
519 // If this is a value-preserving FPStack truncation, it is a noop.
520 if (N->getConstantOperandVal(1))
521 continue;
522 }
523
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000524 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
525 // FPStack has extload and truncstore. SSE can fold direct loads into other
526 // operations. Based on this, decide what we want to do.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000527 MVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000528 if (N->getOpcode() == ISD::FP_ROUND)
529 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
530 else
531 MemVT = SrcIsSSE ? SrcVT : DstVT;
532
533 SDOperand MemTmp = DAG.CreateStackTemporary(MemVT);
534
535 // FIXME: optimize the case where the src/dest is a load or store?
536 SDOperand Store = DAG.getTruncStore(DAG.getEntryNode(), N->getOperand(0),
537 MemTmp, NULL, 0, MemVT);
538 SDOperand Result = DAG.getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp,
539 NULL, 0, MemVT);
540
541 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
542 // extload we created. This will cause general havok on the dag because
543 // anything below the conversion could be folded into other existing nodes.
544 // To avoid invalidating 'I', back it up to the convert node.
545 --I;
546 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result);
547
548 // Now that we did that, the node is dead. Increment the iterator to the
549 // next node to process, then delete N.
550 ++I;
551 DAG.DeleteNode(N);
552 }
553}
554
Chris Lattnerc961eea2005-11-16 01:54:32 +0000555/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
556/// when it has created a SelectionDAG for us to codegen.
557void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
558 DEBUG(BB->dump());
Chris Lattner92cb0af2006-01-11 01:15:34 +0000559 MachineFunction::iterator FirstMBB = BB;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000560
Evan Chenge50794a2006-08-29 18:28:33 +0000561 if (!FastISel)
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000562 PreprocessForRMW(DAG);
563
564 // FIXME: This should only happen when not -fast.
565 PreprocessForFPConvert(DAG);
Evan Cheng70e674e2006-08-28 20:10:17 +0000566
Chris Lattnerc961eea2005-11-16 01:54:32 +0000567 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000568#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000569 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000570 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000571#endif
Evan Chengba2f0a92006-02-05 06:46:41 +0000572 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengf597dc72006-02-10 22:24:32 +0000573#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000574 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000575#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000576
Chris Lattnerc961eea2005-11-16 01:54:32 +0000577 DAG.RemoveDeadNodes();
578
Chris Lattner03fdec02008-03-10 23:34:12 +0000579 // Emit machine code to BB. This can change 'BB' to the last block being
580 // inserted into.
Chris Lattnerc961eea2005-11-16 01:54:32 +0000581 ScheduleAndEmitDAG(DAG);
Chris Lattner92cb0af2006-01-11 01:15:34 +0000582
583 // If we are emitting FP stack code, scan the basic block to determine if this
584 // block defines any FP values. If so, put an FP_REG_KILL instruction before
585 // the terminator of the block.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000586
Dale Johannesen48d1e452007-09-24 22:52:39 +0000587 // Note that FP stack instructions are used in all modes for long double,
588 // so we always need to do this check.
589 // Also note that it's possible for an FP stack register to be live across
590 // an instruction that produces multiple basic blocks (SSE CMOV) so we
591 // must check all the generated basic blocks.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000592
593 // Scan all of the machine instructions in these MBBs, checking for FP
594 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
595 MachineFunction::iterator MBBI = FirstMBB;
Chris Lattner03fdec02008-03-10 23:34:12 +0000596 MachineFunction::iterator EndMBB = BB; ++EndMBB;
597 for (; MBBI != EndMBB; ++MBBI) {
598 MachineBasicBlock *MBB = MBBI;
599
600 // If this block returns, ignore it. We don't want to insert an FP_REG_KILL
601 // before the return.
602 if (!MBB->empty()) {
603 MachineBasicBlock::iterator EndI = MBB->end();
604 --EndI;
605 if (EndI->getDesc().isReturn())
606 continue;
607 }
608
Dale Johannesen48d1e452007-09-24 22:52:39 +0000609 bool ContainsFPCode = false;
Chris Lattner03fdec02008-03-10 23:34:12 +0000610 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000611 !ContainsFPCode && I != E; ++I) {
612 if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) {
613 const TargetRegisterClass *clas;
614 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
615 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
Chris Lattner03fdec02008-03-10 23:34:12 +0000616 TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
Chris Lattner84bc5422007-12-31 04:13:23 +0000617 ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) ==
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000618 X86::RFP32RegisterClass ||
619 clas == X86::RFP64RegisterClass ||
620 clas == X86::RFP80RegisterClass)) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000621 ContainsFPCode = true;
622 break;
623 }
624 }
625 }
626 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000627 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
628 // a copy of the input value in this block. In SSE mode, we only care about
629 // 80-bit values.
630 if (!ContainsFPCode) {
631 // Final check, check LLVM BB's that are successors to the LLVM BB
632 // corresponding to BB for FP PHI nodes.
633 const BasicBlock *LLVMBB = BB->getBasicBlock();
634 const PHINode *PN;
635 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
636 !ContainsFPCode && SI != E; ++SI) {
637 for (BasicBlock::const_iterator II = SI->begin();
638 (PN = dyn_cast<PHINode>(II)); ++II) {
639 if (PN->getType()==Type::X86_FP80Ty ||
640 (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) ||
641 (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) {
642 ContainsFPCode = true;
643 break;
644 }
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000645 }
646 }
Chris Lattner92cb0af2006-01-11 01:15:34 +0000647 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000648 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
649 if (ContainsFPCode) {
Chris Lattner03fdec02008-03-10 23:34:12 +0000650 BuildMI(*MBB, MBBI->getFirstTerminator(),
Dale Johannesen48d1e452007-09-24 22:52:39 +0000651 TM.getInstrInfo()->get(X86::FP_REG_KILL));
652 ++NumFPKill;
653 }
Chris Lattner03fdec02008-03-10 23:34:12 +0000654 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000655}
656
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000657/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
658/// the main function.
659void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
660 MachineFrameInfo *MFI) {
661 const TargetInstrInfo *TII = TM.getInstrInfo();
662 if (Subtarget->isTargetCygMing())
663 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
664}
665
666void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
667 // If this is main, emit special code for main.
668 MachineBasicBlock *BB = MF.begin();
669 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
670 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
671}
672
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000673/// MatchAddress - Add the specified node to the specified addressing mode,
674/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000675/// addressing mode.
Evan Cheng2486af12006-02-11 02:05:36 +0000676bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000677 bool isRoot, unsigned Depth) {
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000678 // Limit recursion.
679 if (Depth > 5)
680 return MatchAddressBase(N, AM, isRoot, Depth);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000681
Evan Cheng25ab6902006-09-08 06:48:29 +0000682 // RIP relative addressing: %rip + 32-bit displacement!
683 if (AM.isRIPRel) {
684 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000685 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000686 if (isInt32(AM.Disp + Val)) {
687 AM.Disp += Val;
688 return false;
689 }
690 }
691 return true;
692 }
693
Evan Cheng2ef88a02006-08-07 22:28:20 +0000694 int id = N.Val->getNodeId();
Evan Cheng1314b002007-12-13 00:43:27 +0000695 bool AlreadySelected = isSelected(id); // Already selected, not yet replaced.
Evan Cheng2486af12006-02-11 02:05:36 +0000696
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000697 switch (N.getOpcode()) {
698 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000699 case ISD::Constant: {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000700 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000701 if (isInt32(AM.Disp + Val)) {
702 AM.Disp += Val;
703 return false;
704 }
705 break;
706 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000707
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000708 case X86ISD::Wrapper: {
709 bool is64Bit = Subtarget->is64Bit();
Evan Cheng0085a282006-11-30 21:55:46 +0000710 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Chengbe3bf422008-02-07 08:53:49 +0000711 // Also, base and index reg must be 0 in order to use rip as base.
712 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
713 AM.Base.Reg.Val || AM.IndexReg.Val))
Evan Cheng0085a282006-11-30 21:55:46 +0000714 break;
Evan Cheng28b514392006-12-05 19:50:18 +0000715 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
716 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000717 // If value is available in a register both base and index components have
718 // been picked, we can't fit the result available in the register in the
719 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Evan Cheng1314b002007-12-13 00:43:27 +0000720 if (!AlreadySelected || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
Evan Cheng28b514392006-12-05 19:50:18 +0000721 SDOperand N0 = N.getOperand(0);
722 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
723 GlobalValue *GV = G->getGlobal();
Evan Chengbe3bf422008-02-07 08:53:49 +0000724 AM.GV = GV;
725 AM.Disp += G->getOffset();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000726 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
727 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000728 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000729 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000730 AM.CP = CP->getConstVal();
731 AM.Align = CP->getAlignment();
732 AM.Disp += CP->getOffset();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000733 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
734 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000735 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000736 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000737 AM.ES = S->getSymbol();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000738 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
739 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000740 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000741 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000742 AM.JT = J->getIndex();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000743 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
744 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000745 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000746 }
747 }
748 break;
Evan Cheng0085a282006-11-30 21:55:46 +0000749 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000750
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000751 case ISD::FrameIndex:
752 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
753 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
754 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
755 return false;
756 }
757 break;
Evan Chengec693f72005-12-08 02:01:35 +0000758
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000759 case ISD::SHL:
Evan Chengbe3bf422008-02-07 08:53:49 +0000760 if (AlreadySelected || AM.IndexReg.Val != 0 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000761 break;
762
763 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
764 unsigned Val = CN->getValue();
765 if (Val == 1 || Val == 2 || Val == 3) {
766 AM.Scale = 1 << Val;
767 SDOperand ShVal = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000768
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000769 // Okay, we know that we have a scale by now. However, if the scaled
770 // value is an add of something and a constant, we can fold the
771 // constant into the disp field here.
772 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
773 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
774 AM.IndexReg = ShVal.Val->getOperand(0);
775 ConstantSDNode *AddVal =
776 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
777 uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
778 if (isInt32(Disp))
779 AM.Disp = Disp;
780 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000781 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000782 } else {
783 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000784 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000785 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000786 }
787 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000788 }
Evan Chengec693f72005-12-08 02:01:35 +0000789
Dan Gohman83688052007-10-22 20:22:24 +0000790 case ISD::SMUL_LOHI:
791 case ISD::UMUL_LOHI:
792 // A mul_lohi where we need the low part can be folded as a plain multiply.
793 if (N.ResNo != 0) break;
794 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000795 case ISD::MUL:
796 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng1314b002007-12-13 00:43:27 +0000797 if (!AlreadySelected &&
Evan Cheng51a9ed92006-02-25 10:09:08 +0000798 AM.BaseType == X86ISelAddressMode::RegBase &&
799 AM.Base.Reg.Val == 0 &&
Evan Chengbe3bf422008-02-07 08:53:49 +0000800 AM.IndexReg.Val == 0 &&
801 !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000802 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
803 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
804 AM.Scale = unsigned(CN->getValue())-1;
805
806 SDOperand MulVal = N.Val->getOperand(0);
807 SDOperand Reg;
808
809 // Okay, we know that we have a scale by now. However, if the scaled
810 // value is an add of something and a constant, we can fold the
811 // constant into the disp field here.
812 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
813 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
814 Reg = MulVal.Val->getOperand(0);
815 ConstantSDNode *AddVal =
816 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
Evan Cheng25ab6902006-09-08 06:48:29 +0000817 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
818 if (isInt32(Disp))
819 AM.Disp = Disp;
820 else
821 Reg = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000822 } else {
823 Reg = N.Val->getOperand(0);
824 }
825
826 AM.IndexReg = AM.Base.Reg = Reg;
827 return false;
828 }
Chris Lattner62412262007-02-04 20:18:17 +0000829 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000830 break;
831
Chris Lattner62412262007-02-04 20:18:17 +0000832 case ISD::ADD:
Evan Cheng1314b002007-12-13 00:43:27 +0000833 if (!AlreadySelected) {
Evan Cheng2486af12006-02-11 02:05:36 +0000834 X86ISelAddressMode Backup = AM;
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000835 if (!MatchAddress(N.Val->getOperand(0), AM, false, Depth+1) &&
836 !MatchAddress(N.Val->getOperand(1), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000837 return false;
838 AM = Backup;
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000839 if (!MatchAddress(N.Val->getOperand(1), AM, false, Depth+1) &&
840 !MatchAddress(N.Val->getOperand(0), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000841 return false;
842 AM = Backup;
843 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000844 break;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000845
Chris Lattner62412262007-02-04 20:18:17 +0000846 case ISD::OR:
847 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Evan Cheng1314b002007-12-13 00:43:27 +0000848 if (AlreadySelected) break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000849
850 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
851 X86ISelAddressMode Backup = AM;
852 // Start with the LHS as an addr mode.
853 if (!MatchAddress(N.getOperand(0), AM, false) &&
854 // Address could not have picked a GV address for the displacement.
855 AM.GV == NULL &&
856 // On x86-64, the resultant disp must fit in 32-bits.
857 isInt32(AM.Disp + CN->getSignExtended()) &&
858 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000859 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000860 AM.Disp += CN->getValue();
861 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000862 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000863 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000864 }
865 break;
Evan Cheng1314b002007-12-13 00:43:27 +0000866
867 case ISD::AND: {
868 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
869 // allows us to fold the shift into this addressing mode.
870 if (AlreadySelected) break;
871 SDOperand Shift = N.getOperand(0);
872 if (Shift.getOpcode() != ISD::SHL) break;
873
874 // Scale must not be used already.
875 if (AM.IndexReg.Val != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +0000876
877 // Not when RIP is used as the base.
878 if (AM.isRIPRel) break;
Evan Cheng1314b002007-12-13 00:43:27 +0000879
880 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
881 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
882 if (!C1 || !C2) break;
883
884 // Not likely to be profitable if either the AND or SHIFT node has more
885 // than one use (unless all uses are for address computation). Besides,
886 // isel mechanism requires their node ids to be reused.
887 if (!N.hasOneUse() || !Shift.hasOneUse())
888 break;
889
890 // Verify that the shift amount is something we can fold.
891 unsigned ShiftCst = C1->getValue();
892 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
893 break;
894
895 // Get the new AND mask, this folds to a constant.
896 SDOperand NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
897 SDOperand(C2, 0), SDOperand(C1, 0));
898 SDOperand NewAND = CurDAG->getNode(ISD::AND, N.getValueType(),
899 Shift.getOperand(0), NewANDMask);
900 NewANDMask.Val->setNodeId(Shift.Val->getNodeId());
901 NewAND.Val->setNodeId(N.Val->getNodeId());
902
903 AM.Scale = 1 << ShiftCst;
904 AM.IndexReg = NewAND;
905 return false;
906 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000907 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000908
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000909 return MatchAddressBase(N, AM, isRoot, Depth);
910}
911
912/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
913/// specified addressing mode without any further recursion.
914bool X86DAGToDAGISel::MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
915 bool isRoot, unsigned Depth) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000916 // Is the base register already occupied?
917 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
918 // If so, check to see if the scale index register is set.
Evan Chengbe3bf422008-02-07 08:53:49 +0000919 if (AM.IndexReg.Val == 0 && !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000920 AM.IndexReg = N;
921 AM.Scale = 1;
922 return false;
923 }
924
925 // Otherwise, we cannot select it.
926 return true;
927 }
928
929 // Default, generate it as a register.
930 AM.BaseType = X86ISelAddressMode::RegBase;
931 AM.Base.Reg = N;
932 return false;
933}
934
Evan Chengec693f72005-12-08 02:01:35 +0000935/// SelectAddr - returns true if it is able pattern match an addressing mode.
936/// It returns the operands which make up the maximal addressing mode it can
937/// match by reference.
Evan Cheng0d538262006-11-08 20:34:28 +0000938bool X86DAGToDAGISel::SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
939 SDOperand &Scale, SDOperand &Index,
940 SDOperand &Disp) {
Evan Chengec693f72005-12-08 02:01:35 +0000941 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +0000942 if (MatchAddress(N, AM))
943 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000944
Duncan Sands83ec4b62008-06-06 12:08:01 +0000945 MVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +0000946 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Cheng7dd281b2006-02-05 05:25:07 +0000947 if (!AM.Base.Reg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000948 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +0000949 }
Evan Cheng8700e142006-01-11 06:09:51 +0000950
Evan Cheng7dd281b2006-02-05 05:25:07 +0000951 if (!AM.IndexReg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000952 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +0000953
954 getAddressOperands(AM, Base, Scale, Index, Disp);
955 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000956}
957
Chris Lattner4fe4f252006-10-11 22:09:58 +0000958/// isZeroNode - Returns true if Elt is a constant zero or a floating point
959/// constant +0.0.
960static inline bool isZeroNode(SDOperand Elt) {
961 return ((isa<ConstantSDNode>(Elt) &&
962 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
963 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +0000964 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Chris Lattner4fe4f252006-10-11 22:09:58 +0000965}
966
967
Chris Lattner3a7cd952006-10-07 21:55:32 +0000968/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
969/// match a load whose top elements are either undef or zeros. The load flavor
970/// is derived from the type of N, which is either v4f32 or v2f64.
Evan Cheng0d538262006-11-08 20:34:28 +0000971bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Cheng07e4b002006-10-16 06:34:55 +0000972 SDOperand N, SDOperand &Base,
Evan Cheng82a91642006-10-11 21:06:01 +0000973 SDOperand &Scale, SDOperand &Index,
974 SDOperand &Disp, SDOperand &InChain,
975 SDOperand &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +0000976 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +0000977 InChain = N.getOperand(0).getValue(1);
Evan Cheng07e4b002006-10-16 06:34:55 +0000978 if (ISD::isNON_EXTLoad(InChain.Val) &&
979 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +0000980 N.hasOneUse() &&
Evan Cheng0d538262006-11-08 20:34:28 +0000981 CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
Evan Cheng82a91642006-10-11 21:06:01 +0000982 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Evan Cheng0d538262006-11-08 20:34:28 +0000983 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner3a7cd952006-10-07 21:55:32 +0000984 return false;
Evan Cheng82a91642006-10-11 21:06:01 +0000985 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +0000986 return true;
987 }
988 }
Chris Lattner4fe4f252006-10-11 22:09:58 +0000989
990 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +0000991 // elements. This is a vector shuffle from the zero vector.
Evan Chengd880b972008-05-09 21:53:03 +0000992 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.Val->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +0000993 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +0000994 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
995 N.getOperand(0).Val->hasOneUse() &&
996 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).Val) &&
997 N.getOperand(0).getOperand(0).hasOneUse()) {
998 // Okay, this is a zero extending load. Fold it.
999 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
1000 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1001 return false;
1002 OutChain = LD->getChain();
1003 InChain = SDOperand(LD, 1);
1004 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001005 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001006 return false;
1007}
1008
1009
Evan Cheng51a9ed92006-02-25 10:09:08 +00001010/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1011/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng0d538262006-11-08 20:34:28 +00001012bool X86DAGToDAGISel::SelectLEAAddr(SDOperand Op, SDOperand N,
1013 SDOperand &Base, SDOperand &Scale,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001014 SDOperand &Index, SDOperand &Disp) {
1015 X86ISelAddressMode AM;
1016 if (MatchAddress(N, AM))
1017 return false;
1018
Duncan Sands83ec4b62008-06-06 12:08:01 +00001019 MVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001020 unsigned Complexity = 0;
1021 if (AM.BaseType == X86ISelAddressMode::RegBase)
1022 if (AM.Base.Reg.Val)
1023 Complexity = 1;
1024 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001025 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001026 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1027 Complexity = 4;
1028
1029 if (AM.IndexReg.Val)
1030 Complexity++;
1031 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001032 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001033
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001034 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1035 // a simple shift.
1036 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001037 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001038
1039 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1040 // to a LEA. This is determined with some expermentation but is by no means
1041 // optimal (especially for code size consideration). LEA is nice because of
1042 // its three-address nature. Tweak the cost function again when we can run
1043 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +00001044 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
1045 // For X86-64, we should always use lea to materialize RIP relative
1046 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001047 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001048 Complexity = 4;
1049 else
1050 Complexity += 2;
1051 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001052
1053 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
1054 Complexity++;
1055
1056 if (Complexity > 2) {
1057 getAddressOperands(AM, Base, Scale, Index, Disp);
1058 return true;
1059 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001060 return false;
1061}
1062
Evan Cheng5e351682006-02-06 06:02:33 +00001063bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
1064 SDOperand &Base, SDOperand &Scale,
1065 SDOperand &Index, SDOperand &Disp) {
Evan Cheng466685d2006-10-09 20:57:25 +00001066 if (ISD::isNON_EXTLoad(N.Val) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001067 N.hasOneUse() &&
Evan Cheng27e1fe92006-10-14 08:33:25 +00001068 CanBeFoldedBy(N.Val, P.Val, P.Val))
Evan Cheng0d538262006-11-08 20:34:28 +00001069 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
Evan Cheng0114e942006-01-06 20:36:21 +00001070 return false;
1071}
1072
Evan Cheng7ccced62006-02-18 00:15:05 +00001073/// getGlobalBaseReg - Output the instructions required to put the
1074/// base address to use for accessing globals into a register.
1075///
Evan Cheng9ade2182006-08-26 05:34:46 +00001076SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng25ab6902006-09-08 06:48:29 +00001077 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Evan Cheng7ccced62006-02-18 00:15:05 +00001078 if (!GlobalBaseReg) {
1079 // Insert the set of GlobalBaseReg into the first MBB of the function
Evan Cheng0475ab52008-01-05 00:41:47 +00001080 MachineFunction *MF = BB->getParent();
1081 MachineBasicBlock &FirstMBB = MF->front();
Evan Cheng7ccced62006-02-18 00:15:05 +00001082 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Evan Cheng0475ab52008-01-05 00:41:47 +00001083 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Chris Lattner84bc5422007-12-31 04:13:23 +00001084 unsigned PC = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001085
Evan Chengc0f64ff2006-11-27 23:37:22 +00001086 const TargetInstrInfo *TII = TM.getInstrInfo();
Evan Chengf02ca692007-12-22 02:26:46 +00001087 // Operand of MovePCtoStack is completely ignored by asm printer. It's
1088 // only used in JIT code emission as displacement to pc.
Evan Cheng0475ab52008-01-05 00:41:47 +00001089 BuildMI(FirstMBB, MBBI, TII->get(X86::MOVPC32r), PC).addImm(0);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001090
1091 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
1092 // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
Evan Cheng706535d2007-01-22 21:34:25 +00001093 if (TM.getRelocationModel() == Reloc::PIC_ &&
1094 Subtarget->isPICStyleGOT()) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001095 GlobalBaseReg = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng0475ab52008-01-05 00:41:47 +00001096 BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg)
1097 .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001098 } else {
1099 GlobalBaseReg = PC;
1100 }
1101
Evan Cheng7ccced62006-02-18 00:15:05 +00001102 }
Evan Cheng25ab6902006-09-08 06:48:29 +00001103 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
Evan Cheng7ccced62006-02-18 00:15:05 +00001104}
1105
Evan Chengb245d922006-05-20 01:36:52 +00001106static SDNode *FindCallStartFromCall(SDNode *Node) {
1107 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1108 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1109 "Node doesn't have a token chain argument!");
1110 return FindCallStartFromCall(Node->getOperand(0).Val);
1111}
1112
Duncan Sands83ec4b62008-06-06 12:08:01 +00001113SDNode *X86DAGToDAGISel::getTruncate(SDOperand N0, MVT VT) {
Christopher Lambc59e5212007-08-10 21:48:46 +00001114 SDOperand SRIdx;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001115 switch (VT.getSimpleVT()) {
1116 default: assert(0 && "Unknown truncate!");
Christopher Lambc59e5212007-08-10 21:48:46 +00001117 case MVT::i8:
1118 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1119 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1120 if (!Subtarget->is64Bit()) {
1121 unsigned Opc;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001122 MVT VT;
1123 switch (N0.getValueType().getSimpleVT()) {
Christopher Lambc59e5212007-08-10 21:48:46 +00001124 default: assert(0 && "Unknown truncate!");
1125 case MVT::i16:
1126 Opc = X86::MOV16to16_;
1127 VT = MVT::i16;
1128 break;
1129 case MVT::i32:
1130 Opc = X86::MOV32to32_;
1131 VT = MVT::i32;
1132 break;
1133 }
Evan Cheng96aaa542007-10-12 07:55:53 +00001134 N0 = SDOperand(CurDAG->getTargetNode(Opc, VT, MVT::Flag, N0), 0);
1135 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1136 VT, N0, SRIdx, N0.getValue(1));
Christopher Lambc59e5212007-08-10 21:48:46 +00001137 }
1138 break;
1139 case MVT::i16:
1140 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1141 break;
1142 case MVT::i32:
1143 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1144 break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001145 }
Evan Cheng96aaa542007-10-12 07:55:53 +00001146 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, VT, N0, SRIdx);
Christopher Lambc59e5212007-08-10 21:48:46 +00001147}
1148
1149
Evan Cheng9ade2182006-08-26 05:34:46 +00001150SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Chengdef941b2005-12-15 01:02:48 +00001151 SDNode *Node = N.Val;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001152 MVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001153 unsigned Opc, MOpc;
1154 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +00001155
Evan Chengf597dc72006-02-10 22:24:32 +00001156#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001157 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001158 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001159 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001160 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001161#endif
1162
Evan Cheng34167212006-02-09 00:37:58 +00001163 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengf597dc72006-02-10 22:24:32 +00001164#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001165 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001166 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001167 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001168 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001169#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001170 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001171 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001172
Evan Cheng0114e942006-01-06 20:36:21 +00001173 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001174 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001175 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001176 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001177
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;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001186 MVT 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)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001227 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001228 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
Duncan Sands83ec4b62008-06-06 12:08:01 +00001235 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001236 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;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001244 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001245 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)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001337 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001338 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
Duncan Sands83ec4b62008-06-06 12:08:01 +00001345 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001346 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;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001355 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001356 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: {
Christopher Lambc9298232008-03-16 03:12:01 +00001490 // Check if the type extended to supports subregs.
1491 if (NVT == MVT::i8)
1492 break;
1493
Christopher Lamba1eb1552007-08-10 22:22:41 +00001494 SDOperand N0 = Node->getOperand(0);
Christopher Lambc9298232008-03-16 03:12:01 +00001495 // Get the subregsiter index for the type to extend.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001496 MVT N0VT = N0.getValueType();
Christopher Lambc9298232008-03-16 03:12:01 +00001497 unsigned Idx = (N0VT == MVT::i32) ? X86::SUBREG_32BIT :
1498 (N0VT == MVT::i16) ? X86::SUBREG_16BIT :
1499 (Subtarget->is64Bit()) ? X86::SUBREG_8BIT : 0;
1500
1501 // If we don't have a subreg Idx, let generated ISel have a try.
1502 if (Idx == 0)
1503 break;
1504
1505 // If we have an index, generate an insert_subreg into undef.
Christopher Lamba1eb1552007-08-10 22:22:41 +00001506 AddToISelQueue(N0);
Christopher Lambc9298232008-03-16 03:12:01 +00001507 SDOperand Undef =
Evan Cheng90ce87b2008-04-03 07:45:18 +00001508 SDOperand(CurDAG->getTargetNode(X86::IMPLICIT_DEF, NVT), 0);
Christopher Lambc9298232008-03-16 03:12:01 +00001509 SDOperand SRIdx = CurDAG->getTargetConstant(Idx, MVT::i32);
1510 SDNode *ResNode = CurDAG->getTargetNode(X86::INSERT_SUBREG,
Evan Cheng90ce87b2008-04-03 07:45:18 +00001511 NVT, Undef, N0, SRIdx);
Christopher Lamba1eb1552007-08-10 22:22:41 +00001512
1513#ifndef NDEBUG
Christopher Lambc9298232008-03-16 03:12:01 +00001514 DOUT << std::string(Indent-2, ' ') << "=> ";
1515 DEBUG(ResNode->dump(CurDAG));
1516 DOUT << "\n";
1517 Indent -= 2;
Christopher Lamba1eb1552007-08-10 22:22:41 +00001518#endif
Christopher Lambc9298232008-03-16 03:12:01 +00001519 return ResNode;
Christopher Lamba1eb1552007-08-10 22:22:41 +00001520 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001521
1522 case ISD::SIGN_EXTEND_INREG: {
1523 SDOperand N0 = Node->getOperand(0);
1524 AddToISelQueue(N0);
Evan Cheng403be7e2006-05-08 08:01:26 +00001525
Duncan Sands83ec4b62008-06-06 12:08:01 +00001526 MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Christopher Lambc59e5212007-08-10 21:48:46 +00001527 SDOperand TruncOp = SDOperand(getTruncate(N0, SVT), 0);
Bill Wendling0d642872007-11-01 08:51:44 +00001528 unsigned Opc = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001529 switch (NVT.getSimpleVT()) {
1530 default: assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001531 case MVT::i16:
Christopher Lambc59e5212007-08-10 21:48:46 +00001532 if (SVT == MVT::i8) Opc = X86::MOVSX16rr8;
1533 else assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001534 break;
1535 case MVT::i32:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001536 switch (SVT.getSimpleVT()) {
1537 default: assert(0 && "Unknown sign_extend_inreg!");
Christopher Lambc59e5212007-08-10 21:48:46 +00001538 case MVT::i8: Opc = X86::MOVSX32rr8; break;
1539 case MVT::i16: Opc = X86::MOVSX32rr16; break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001540 }
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001541 break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001542 case MVT::i64:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001543 switch (SVT.getSimpleVT()) {
1544 default: assert(0 && "Unknown sign_extend_inreg!");
Christopher Lambc59e5212007-08-10 21:48:46 +00001545 case MVT::i8: Opc = X86::MOVSX64rr8; break;
1546 case MVT::i16: Opc = X86::MOVSX64rr16; break;
1547 case MVT::i32: Opc = X86::MOVSX64rr32; break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001548 }
1549 break;
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001550 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001551
1552 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
1553
1554#ifndef NDEBUG
1555 DOUT << std::string(Indent-2, ' ') << "=> ";
1556 DEBUG(TruncOp.Val->dump(CurDAG));
1557 DOUT << "\n";
1558 DOUT << std::string(Indent-2, ' ') << "=> ";
1559 DEBUG(ResNode->dump(CurDAG));
1560 DOUT << "\n";
1561 Indent -= 2;
1562#endif
1563 return ResNode;
1564 break;
1565 }
1566
1567 case ISD::TRUNCATE: {
1568 SDOperand Input = Node->getOperand(0);
1569 AddToISelQueue(Node->getOperand(0));
1570 SDNode *ResNode = getTruncate(Input, NVT);
1571
Evan Cheng403be7e2006-05-08 08:01:26 +00001572#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001573 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001574 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001575 DOUT << "\n";
Evan Cheng403be7e2006-05-08 08:01:26 +00001576 Indent -= 2;
1577#endif
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001578 return ResNode;
Evan Cheng6b2e2542006-05-20 07:44:28 +00001579 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001580 }
Evan Cheng851bc042008-06-17 02:01:22 +00001581
1582 case ISD::DECLARE: {
1583 // Handle DECLARE nodes here because the second operand may have been
1584 // wrapped in X86ISD::Wrapper.
1585 SDOperand Chain = Node->getOperand(0);
1586 SDOperand N1 = Node->getOperand(1);
1587 SDOperand N2 = Node->getOperand(2);
Evan Chengfab83872008-06-18 02:48:27 +00001588 if (!isa<FrameIndexSDNode>(N1))
1589 break;
1590 int FI = cast<FrameIndexSDNode>(N1)->getIndex();
1591 if (N2.getOpcode() == ISD::ADD &&
1592 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1593 N2 = N2.getOperand(1);
1594 if (N2.getOpcode() == X86ISD::Wrapper &&
Evan Cheng851bc042008-06-17 02:01:22 +00001595 isa<GlobalAddressSDNode>(N2.getOperand(0))) {
Evan Cheng851bc042008-06-17 02:01:22 +00001596 GlobalValue *GV =
1597 cast<GlobalAddressSDNode>(N2.getOperand(0))->getGlobal();
1598 SDOperand Tmp1 = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1599 SDOperand Tmp2 = CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());
1600 AddToISelQueue(Chain);
1601 SDOperand Ops[] = { Tmp1, Tmp2, Chain };
1602 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE,
1603 MVT::Other, Ops, 3);
1604 }
1605 break;
1606 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001607 }
1608
Evan Cheng9ade2182006-08-26 05:34:46 +00001609 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001610
Evan Chengf597dc72006-02-10 22:24:32 +00001611#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001612 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001613 if (ResNode == NULL || ResNode == N.Val)
1614 DEBUG(N.Val->dump(CurDAG));
1615 else
1616 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001617 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001618 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001619#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001620
1621 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001622}
1623
Chris Lattnerc0bad572006-06-08 18:03:49 +00001624bool X86DAGToDAGISel::
1625SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1626 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1627 SDOperand Op0, Op1, Op2, Op3;
1628 switch (ConstraintCode) {
1629 case 'o': // offsetable ??
1630 case 'v': // not offsetable ??
1631 default: return true;
1632 case 'm': // memory
Evan Cheng0d538262006-11-08 20:34:28 +00001633 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001634 return true;
1635 break;
1636 }
1637
Evan Cheng04699902006-08-26 01:05:16 +00001638 OutOps.push_back(Op0);
1639 OutOps.push_back(Op1);
1640 OutOps.push_back(Op2);
1641 OutOps.push_back(Op3);
1642 AddToISelQueue(Op0);
1643 AddToISelQueue(Op1);
1644 AddToISelQueue(Op2);
1645 AddToISelQueue(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001646 return false;
1647}
1648
Chris Lattnerc961eea2005-11-16 01:54:32 +00001649/// createX86ISelDag - This pass converts a legalized DAG into a
1650/// X86-specific DAG, ready for instruction scheduling.
1651///
Evan Chenge50794a2006-08-29 18:28:33 +00001652FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1653 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001654}