blob: 1e9a0da9c6889bd0bf661b8465676b3b2b4fca5f [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.
218 SDNode *getTruncate(SDOperand N0, MVT::ValueType VT);
219
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;
332 MVT::ValueType VT = Root->getValueType(Root->getNumValues()-1);
333 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
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000366/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
367/// This is only run if not in -fast mode (aka -O0).
368/// This allows the instruction selector to pick more read-modify-write
369/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000370///
371/// [Load chain]
372/// ^
373/// |
374/// [Load]
375/// ^ ^
376/// | |
377/// / \-
378/// / |
379/// [TokenFactor] [Op]
380/// ^ ^
381/// | |
382/// \ /
383/// \ /
384/// [Store]
385///
386/// The fact the store's chain operand != load's chain will prevent the
387/// (store (op (load))) instruction from being selected. We can transform it to:
388///
389/// [Load chain]
390/// ^
391/// |
392/// [TokenFactor]
393/// ^
394/// |
395/// [Load]
396/// ^ ^
397/// | |
398/// | \-
399/// | |
400/// | [Op]
401/// | ^
402/// | |
403/// \ /
404/// \ /
405/// [Store]
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000406void X86DAGToDAGISel::PreprocessForRMW(SelectionDAG &DAG) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000407 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
408 E = DAG.allnodes_end(); I != E; ++I) {
Evan Cheng8b2794a2006-10-13 21:14:26 +0000409 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000410 continue;
411 SDOperand Chain = I->getOperand(0);
412 if (Chain.Val->getOpcode() != ISD::TokenFactor)
413 continue;
414
415 SDOperand N1 = I->getOperand(1);
416 SDOperand N2 = I->getOperand(2);
Evan Cheng1453de52006-09-01 22:52:28 +0000417 if (MVT::isFloatingPoint(N1.getValueType()) ||
418 MVT::isVector(N1.getValueType()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000419 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000420 continue;
421
422 bool RModW = false;
423 SDOperand Load;
424 unsigned Opcode = N1.Val->getOpcode();
425 switch (Opcode) {
426 case ISD::ADD:
427 case ISD::MUL:
Evan Cheng70e674e2006-08-28 20:10:17 +0000428 case ISD::AND:
429 case ISD::OR:
430 case ISD::XOR:
431 case ISD::ADDC:
432 case ISD::ADDE: {
433 SDOperand N10 = N1.getOperand(0);
434 SDOperand N11 = N1.getOperand(1);
Evan Cheng466685d2006-10-09 20:57:25 +0000435 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng70e674e2006-08-28 20:10:17 +0000436 RModW = true;
Evan Cheng466685d2006-10-09 20:57:25 +0000437 else if (ISD::isNON_EXTLoad(N11.Val)) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000438 RModW = true;
439 std::swap(N10, N11);
440 }
Evan Cheng07b7ea12008-03-04 00:40:35 +0000441 RModW = RModW && N10.Val->isOperandOf(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000442 (N10.getOperand(1) == N2) &&
443 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000444 if (RModW)
445 Load = N10;
446 break;
447 }
448 case ISD::SUB:
449 case ISD::SHL:
450 case ISD::SRA:
451 case ISD::SRL:
452 case ISD::ROTL:
453 case ISD::ROTR:
454 case ISD::SUBC:
455 case ISD::SUBE:
456 case X86ISD::SHLD:
457 case X86ISD::SHRD: {
458 SDOperand N10 = N1.getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +0000459 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng07b7ea12008-03-04 00:40:35 +0000460 RModW = N10.Val->isOperandOf(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000461 (N10.getOperand(1) == N2) &&
462 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000463 if (RModW)
464 Load = N10;
465 break;
466 }
467 }
468
Evan Cheng82a35b32006-08-29 06:44:17 +0000469 if (RModW) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000470 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000471 ++NumLoadMoved;
472 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000473 }
474}
475
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000476
477/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
478/// nodes that target the FP stack to be store and load to the stack. This is a
479/// gross hack. We would like to simply mark these as being illegal, but when
480/// we do that, legalize produces these when it expands calls, then expands
481/// these in the same legalize pass. We would like dag combine to be able to
482/// hack on these between the call expansion and the node legalization. As such
483/// this pass basically does "really late" legalization of these inline with the
484/// X86 isel pass.
485void X86DAGToDAGISel::PreprocessForFPConvert(SelectionDAG &DAG) {
486 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
487 E = DAG.allnodes_end(); I != E; ) {
488 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
489 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
490 continue;
491
492 // If the source and destination are SSE registers, then this is a legal
493 // conversion that should not be lowered.
494 MVT::ValueType SrcVT = N->getOperand(0).getValueType();
495 MVT::ValueType DstVT = N->getValueType(0);
496 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
497 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
498 if (SrcIsSSE && DstIsSSE)
499 continue;
500
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000501 if (!SrcIsSSE && !DstIsSSE) {
502 // If this is an FPStack extension, it is a noop.
503 if (N->getOpcode() == ISD::FP_EXTEND)
504 continue;
505 // If this is a value-preserving FPStack truncation, it is a noop.
506 if (N->getConstantOperandVal(1))
507 continue;
508 }
509
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000510 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
511 // FPStack has extload and truncstore. SSE can fold direct loads into other
512 // operations. Based on this, decide what we want to do.
513 MVT::ValueType MemVT;
514 if (N->getOpcode() == ISD::FP_ROUND)
515 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
516 else
517 MemVT = SrcIsSSE ? SrcVT : DstVT;
518
519 SDOperand MemTmp = DAG.CreateStackTemporary(MemVT);
520
521 // FIXME: optimize the case where the src/dest is a load or store?
522 SDOperand Store = DAG.getTruncStore(DAG.getEntryNode(), N->getOperand(0),
523 MemTmp, NULL, 0, MemVT);
524 SDOperand Result = DAG.getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp,
525 NULL, 0, MemVT);
526
527 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
528 // extload we created. This will cause general havok on the dag because
529 // anything below the conversion could be folded into other existing nodes.
530 // To avoid invalidating 'I', back it up to the convert node.
531 --I;
532 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result);
533
534 // Now that we did that, the node is dead. Increment the iterator to the
535 // next node to process, then delete N.
536 ++I;
537 DAG.DeleteNode(N);
538 }
539}
540
Chris Lattnerc961eea2005-11-16 01:54:32 +0000541/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
542/// when it has created a SelectionDAG for us to codegen.
543void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
544 DEBUG(BB->dump());
Chris Lattner92cb0af2006-01-11 01:15:34 +0000545 MachineFunction::iterator FirstMBB = BB;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000546
Evan Chenge50794a2006-08-29 18:28:33 +0000547 if (!FastISel)
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000548 PreprocessForRMW(DAG);
549
550 // FIXME: This should only happen when not -fast.
551 PreprocessForFPConvert(DAG);
Evan Cheng70e674e2006-08-28 20:10:17 +0000552
Chris Lattnerc961eea2005-11-16 01:54:32 +0000553 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000554#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000555 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000556 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000557#endif
Evan Chengba2f0a92006-02-05 06:46:41 +0000558 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengf597dc72006-02-10 22:24:32 +0000559#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000560 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000561#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000562
Chris Lattnerc961eea2005-11-16 01:54:32 +0000563 DAG.RemoveDeadNodes();
564
Chris Lattner03fdec02008-03-10 23:34:12 +0000565 // Emit machine code to BB. This can change 'BB' to the last block being
566 // inserted into.
Chris Lattnerc961eea2005-11-16 01:54:32 +0000567 ScheduleAndEmitDAG(DAG);
Chris Lattner92cb0af2006-01-11 01:15:34 +0000568
569 // If we are emitting FP stack code, scan the basic block to determine if this
570 // block defines any FP values. If so, put an FP_REG_KILL instruction before
571 // the terminator of the block.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000572
Dale Johannesen48d1e452007-09-24 22:52:39 +0000573 // Note that FP stack instructions are used in all modes for long double,
574 // so we always need to do this check.
575 // Also note that it's possible for an FP stack register to be live across
576 // an instruction that produces multiple basic blocks (SSE CMOV) so we
577 // must check all the generated basic blocks.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000578
579 // Scan all of the machine instructions in these MBBs, checking for FP
580 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
581 MachineFunction::iterator MBBI = FirstMBB;
Chris Lattner03fdec02008-03-10 23:34:12 +0000582 MachineFunction::iterator EndMBB = BB; ++EndMBB;
583 for (; MBBI != EndMBB; ++MBBI) {
584 MachineBasicBlock *MBB = MBBI;
585
586 // If this block returns, ignore it. We don't want to insert an FP_REG_KILL
587 // before the return.
588 if (!MBB->empty()) {
589 MachineBasicBlock::iterator EndI = MBB->end();
590 --EndI;
591 if (EndI->getDesc().isReturn())
592 continue;
593 }
594
Dale Johannesen48d1e452007-09-24 22:52:39 +0000595 bool ContainsFPCode = false;
Chris Lattner03fdec02008-03-10 23:34:12 +0000596 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000597 !ContainsFPCode && I != E; ++I) {
598 if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) {
599 const TargetRegisterClass *clas;
600 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
601 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
Chris Lattner03fdec02008-03-10 23:34:12 +0000602 TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
Chris Lattner84bc5422007-12-31 04:13:23 +0000603 ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) ==
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000604 X86::RFP32RegisterClass ||
605 clas == X86::RFP64RegisterClass ||
606 clas == X86::RFP80RegisterClass)) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000607 ContainsFPCode = true;
608 break;
609 }
610 }
611 }
612 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000613 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
614 // a copy of the input value in this block. In SSE mode, we only care about
615 // 80-bit values.
616 if (!ContainsFPCode) {
617 // Final check, check LLVM BB's that are successors to the LLVM BB
618 // corresponding to BB for FP PHI nodes.
619 const BasicBlock *LLVMBB = BB->getBasicBlock();
620 const PHINode *PN;
621 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
622 !ContainsFPCode && SI != E; ++SI) {
623 for (BasicBlock::const_iterator II = SI->begin();
624 (PN = dyn_cast<PHINode>(II)); ++II) {
625 if (PN->getType()==Type::X86_FP80Ty ||
626 (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) ||
627 (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) {
628 ContainsFPCode = true;
629 break;
630 }
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000631 }
632 }
Chris Lattner92cb0af2006-01-11 01:15:34 +0000633 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000634 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
635 if (ContainsFPCode) {
Chris Lattner03fdec02008-03-10 23:34:12 +0000636 BuildMI(*MBB, MBBI->getFirstTerminator(),
Dale Johannesen48d1e452007-09-24 22:52:39 +0000637 TM.getInstrInfo()->get(X86::FP_REG_KILL));
638 ++NumFPKill;
639 }
Chris Lattner03fdec02008-03-10 23:34:12 +0000640 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000641}
642
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000643/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
644/// the main function.
645void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
646 MachineFrameInfo *MFI) {
647 const TargetInstrInfo *TII = TM.getInstrInfo();
648 if (Subtarget->isTargetCygMing())
649 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
650}
651
652void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
653 // If this is main, emit special code for main.
654 MachineBasicBlock *BB = MF.begin();
655 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
656 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
657}
658
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000659/// MatchAddress - Add the specified node to the specified addressing mode,
660/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000661/// addressing mode.
Evan Cheng2486af12006-02-11 02:05:36 +0000662bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000663 bool isRoot, unsigned Depth) {
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000664 // Limit recursion.
665 if (Depth > 5)
666 return MatchAddressBase(N, AM, isRoot, Depth);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000667
Evan Cheng25ab6902006-09-08 06:48:29 +0000668 // RIP relative addressing: %rip + 32-bit displacement!
669 if (AM.isRIPRel) {
670 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000671 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000672 if (isInt32(AM.Disp + Val)) {
673 AM.Disp += Val;
674 return false;
675 }
676 }
677 return true;
678 }
679
Evan Cheng2ef88a02006-08-07 22:28:20 +0000680 int id = N.Val->getNodeId();
Evan Cheng1314b002007-12-13 00:43:27 +0000681 bool AlreadySelected = isSelected(id); // Already selected, not yet replaced.
Evan Cheng2486af12006-02-11 02:05:36 +0000682
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000683 switch (N.getOpcode()) {
684 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000685 case ISD::Constant: {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000686 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000687 if (isInt32(AM.Disp + Val)) {
688 AM.Disp += Val;
689 return false;
690 }
691 break;
692 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000693
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000694 case X86ISD::Wrapper: {
695 bool is64Bit = Subtarget->is64Bit();
Evan Cheng0085a282006-11-30 21:55:46 +0000696 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Chengbe3bf422008-02-07 08:53:49 +0000697 // Also, base and index reg must be 0 in order to use rip as base.
698 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
699 AM.Base.Reg.Val || AM.IndexReg.Val))
Evan Cheng0085a282006-11-30 21:55:46 +0000700 break;
Evan Cheng28b514392006-12-05 19:50:18 +0000701 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
702 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000703 // If value is available in a register both base and index components have
704 // been picked, we can't fit the result available in the register in the
705 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Evan Cheng1314b002007-12-13 00:43:27 +0000706 if (!AlreadySelected || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
Evan Cheng28b514392006-12-05 19:50:18 +0000707 SDOperand N0 = N.getOperand(0);
708 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
709 GlobalValue *GV = G->getGlobal();
Evan Chengbe3bf422008-02-07 08:53:49 +0000710 AM.GV = GV;
711 AM.Disp += G->getOffset();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000712 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
713 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000714 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000715 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000716 AM.CP = CP->getConstVal();
717 AM.Align = CP->getAlignment();
718 AM.Disp += CP->getOffset();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000719 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
720 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000721 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000722 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000723 AM.ES = S->getSymbol();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000724 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
725 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000726 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000727 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000728 AM.JT = J->getIndex();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000729 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
730 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000731 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000732 }
733 }
734 break;
Evan Cheng0085a282006-11-30 21:55:46 +0000735 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000736
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000737 case ISD::FrameIndex:
738 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
739 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
740 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
741 return false;
742 }
743 break;
Evan Chengec693f72005-12-08 02:01:35 +0000744
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000745 case ISD::SHL:
Evan Chengbe3bf422008-02-07 08:53:49 +0000746 if (AlreadySelected || AM.IndexReg.Val != 0 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000747 break;
748
749 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
750 unsigned Val = CN->getValue();
751 if (Val == 1 || Val == 2 || Val == 3) {
752 AM.Scale = 1 << Val;
753 SDOperand ShVal = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000754
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000755 // Okay, we know that we have a scale by now. However, if the scaled
756 // value is an add of something and a constant, we can fold the
757 // constant into the disp field here.
758 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
759 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
760 AM.IndexReg = ShVal.Val->getOperand(0);
761 ConstantSDNode *AddVal =
762 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
763 uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
764 if (isInt32(Disp))
765 AM.Disp = Disp;
766 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000767 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000768 } else {
769 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000770 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000771 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000772 }
773 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000774 }
Evan Chengec693f72005-12-08 02:01:35 +0000775
Dan Gohman83688052007-10-22 20:22:24 +0000776 case ISD::SMUL_LOHI:
777 case ISD::UMUL_LOHI:
778 // A mul_lohi where we need the low part can be folded as a plain multiply.
779 if (N.ResNo != 0) break;
780 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000781 case ISD::MUL:
782 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng1314b002007-12-13 00:43:27 +0000783 if (!AlreadySelected &&
Evan Cheng51a9ed92006-02-25 10:09:08 +0000784 AM.BaseType == X86ISelAddressMode::RegBase &&
785 AM.Base.Reg.Val == 0 &&
Evan Chengbe3bf422008-02-07 08:53:49 +0000786 AM.IndexReg.Val == 0 &&
787 !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000788 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
789 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
790 AM.Scale = unsigned(CN->getValue())-1;
791
792 SDOperand MulVal = N.Val->getOperand(0);
793 SDOperand Reg;
794
795 // Okay, we know that we have a scale by now. However, if the scaled
796 // value is an add of something and a constant, we can fold the
797 // constant into the disp field here.
798 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
799 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
800 Reg = MulVal.Val->getOperand(0);
801 ConstantSDNode *AddVal =
802 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
Evan Cheng25ab6902006-09-08 06:48:29 +0000803 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
804 if (isInt32(Disp))
805 AM.Disp = Disp;
806 else
807 Reg = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000808 } else {
809 Reg = N.Val->getOperand(0);
810 }
811
812 AM.IndexReg = AM.Base.Reg = Reg;
813 return false;
814 }
Chris Lattner62412262007-02-04 20:18:17 +0000815 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000816 break;
817
Chris Lattner62412262007-02-04 20:18:17 +0000818 case ISD::ADD:
Evan Cheng1314b002007-12-13 00:43:27 +0000819 if (!AlreadySelected) {
Evan Cheng2486af12006-02-11 02:05:36 +0000820 X86ISelAddressMode Backup = AM;
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000821 if (!MatchAddress(N.Val->getOperand(0), AM, false, Depth+1) &&
822 !MatchAddress(N.Val->getOperand(1), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000823 return false;
824 AM = Backup;
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000825 if (!MatchAddress(N.Val->getOperand(1), AM, false, Depth+1) &&
826 !MatchAddress(N.Val->getOperand(0), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000827 return false;
828 AM = Backup;
829 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000830 break;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000831
Chris Lattner62412262007-02-04 20:18:17 +0000832 case ISD::OR:
833 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Evan Cheng1314b002007-12-13 00:43:27 +0000834 if (AlreadySelected) break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000835
836 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
837 X86ISelAddressMode Backup = AM;
838 // Start with the LHS as an addr mode.
839 if (!MatchAddress(N.getOperand(0), AM, false) &&
840 // Address could not have picked a GV address for the displacement.
841 AM.GV == NULL &&
842 // On x86-64, the resultant disp must fit in 32-bits.
843 isInt32(AM.Disp + CN->getSignExtended()) &&
844 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000845 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000846 AM.Disp += CN->getValue();
847 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000848 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000849 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000850 }
851 break;
Evan Cheng1314b002007-12-13 00:43:27 +0000852
853 case ISD::AND: {
854 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
855 // allows us to fold the shift into this addressing mode.
856 if (AlreadySelected) break;
857 SDOperand Shift = N.getOperand(0);
858 if (Shift.getOpcode() != ISD::SHL) break;
859
860 // Scale must not be used already.
861 if (AM.IndexReg.Val != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +0000862
863 // Not when RIP is used as the base.
864 if (AM.isRIPRel) break;
Evan Cheng1314b002007-12-13 00:43:27 +0000865
866 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
867 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
868 if (!C1 || !C2) break;
869
870 // Not likely to be profitable if either the AND or SHIFT node has more
871 // than one use (unless all uses are for address computation). Besides,
872 // isel mechanism requires their node ids to be reused.
873 if (!N.hasOneUse() || !Shift.hasOneUse())
874 break;
875
876 // Verify that the shift amount is something we can fold.
877 unsigned ShiftCst = C1->getValue();
878 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
879 break;
880
881 // Get the new AND mask, this folds to a constant.
882 SDOperand NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
883 SDOperand(C2, 0), SDOperand(C1, 0));
884 SDOperand NewAND = CurDAG->getNode(ISD::AND, N.getValueType(),
885 Shift.getOperand(0), NewANDMask);
886 NewANDMask.Val->setNodeId(Shift.Val->getNodeId());
887 NewAND.Val->setNodeId(N.Val->getNodeId());
888
889 AM.Scale = 1 << ShiftCst;
890 AM.IndexReg = NewAND;
891 return false;
892 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000893 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000894
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000895 return MatchAddressBase(N, AM, isRoot, Depth);
896}
897
898/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
899/// specified addressing mode without any further recursion.
900bool X86DAGToDAGISel::MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
901 bool isRoot, unsigned Depth) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000902 // Is the base register already occupied?
903 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
904 // If so, check to see if the scale index register is set.
Evan Chengbe3bf422008-02-07 08:53:49 +0000905 if (AM.IndexReg.Val == 0 && !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000906 AM.IndexReg = N;
907 AM.Scale = 1;
908 return false;
909 }
910
911 // Otherwise, we cannot select it.
912 return true;
913 }
914
915 // Default, generate it as a register.
916 AM.BaseType = X86ISelAddressMode::RegBase;
917 AM.Base.Reg = N;
918 return false;
919}
920
Evan Chengec693f72005-12-08 02:01:35 +0000921/// SelectAddr - returns true if it is able pattern match an addressing mode.
922/// It returns the operands which make up the maximal addressing mode it can
923/// match by reference.
Evan Cheng0d538262006-11-08 20:34:28 +0000924bool X86DAGToDAGISel::SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
925 SDOperand &Scale, SDOperand &Index,
926 SDOperand &Disp) {
Evan Chengec693f72005-12-08 02:01:35 +0000927 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +0000928 if (MatchAddress(N, AM))
929 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000930
Evan Cheng25ab6902006-09-08 06:48:29 +0000931 MVT::ValueType VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +0000932 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Cheng7dd281b2006-02-05 05:25:07 +0000933 if (!AM.Base.Reg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000934 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +0000935 }
Evan Cheng8700e142006-01-11 06:09:51 +0000936
Evan Cheng7dd281b2006-02-05 05:25:07 +0000937 if (!AM.IndexReg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000938 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +0000939
940 getAddressOperands(AM, Base, Scale, Index, Disp);
941 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000942}
943
Chris Lattner4fe4f252006-10-11 22:09:58 +0000944/// isZeroNode - Returns true if Elt is a constant zero or a floating point
945/// constant +0.0.
946static inline bool isZeroNode(SDOperand Elt) {
947 return ((isa<ConstantSDNode>(Elt) &&
948 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
949 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +0000950 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Chris Lattner4fe4f252006-10-11 22:09:58 +0000951}
952
953
Chris Lattner3a7cd952006-10-07 21:55:32 +0000954/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
955/// match a load whose top elements are either undef or zeros. The load flavor
956/// is derived from the type of N, which is either v4f32 or v2f64.
Evan Cheng0d538262006-11-08 20:34:28 +0000957bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Cheng07e4b002006-10-16 06:34:55 +0000958 SDOperand N, SDOperand &Base,
Evan Cheng82a91642006-10-11 21:06:01 +0000959 SDOperand &Scale, SDOperand &Index,
960 SDOperand &Disp, SDOperand &InChain,
961 SDOperand &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +0000962 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +0000963 InChain = N.getOperand(0).getValue(1);
Evan Cheng07e4b002006-10-16 06:34:55 +0000964 if (ISD::isNON_EXTLoad(InChain.Val) &&
965 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +0000966 N.hasOneUse() &&
Evan Cheng0d538262006-11-08 20:34:28 +0000967 CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
Evan Cheng82a91642006-10-11 21:06:01 +0000968 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Evan Cheng0d538262006-11-08 20:34:28 +0000969 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner3a7cd952006-10-07 21:55:32 +0000970 return false;
Evan Cheng82a91642006-10-11 21:06:01 +0000971 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +0000972 return true;
973 }
974 }
Chris Lattner4fe4f252006-10-11 22:09:58 +0000975
976 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +0000977 // elements. This is a vector shuffle from the zero vector.
Evan Cheng7e2ff772008-05-08 00:57:18 +0000978 if (N.getOpcode() == X86ISD::ZEXT_VMOVL && N.Val->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +0000979 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +0000980 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
981 N.getOperand(0).Val->hasOneUse() &&
982 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).Val) &&
983 N.getOperand(0).getOperand(0).hasOneUse()) {
984 // Okay, this is a zero extending load. Fold it.
985 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
986 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
987 return false;
988 OutChain = LD->getChain();
989 InChain = SDOperand(LD, 1);
990 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +0000991 }
Chris Lattner3a7cd952006-10-07 21:55:32 +0000992 return false;
993}
994
995
Evan Cheng51a9ed92006-02-25 10:09:08 +0000996/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
997/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng0d538262006-11-08 20:34:28 +0000998bool X86DAGToDAGISel::SelectLEAAddr(SDOperand Op, SDOperand N,
999 SDOperand &Base, SDOperand &Scale,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001000 SDOperand &Index, SDOperand &Disp) {
1001 X86ISelAddressMode AM;
1002 if (MatchAddress(N, AM))
1003 return false;
1004
Evan Cheng25ab6902006-09-08 06:48:29 +00001005 MVT::ValueType VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001006 unsigned Complexity = 0;
1007 if (AM.BaseType == X86ISelAddressMode::RegBase)
1008 if (AM.Base.Reg.Val)
1009 Complexity = 1;
1010 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001011 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001012 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1013 Complexity = 4;
1014
1015 if (AM.IndexReg.Val)
1016 Complexity++;
1017 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001018 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001019
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001020 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1021 // a simple shift.
1022 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001023 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001024
1025 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1026 // to a LEA. This is determined with some expermentation but is by no means
1027 // optimal (especially for code size consideration). LEA is nice because of
1028 // its three-address nature. Tweak the cost function again when we can run
1029 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +00001030 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
1031 // For X86-64, we should always use lea to materialize RIP relative
1032 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001033 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001034 Complexity = 4;
1035 else
1036 Complexity += 2;
1037 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001038
1039 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
1040 Complexity++;
1041
1042 if (Complexity > 2) {
1043 getAddressOperands(AM, Base, Scale, Index, Disp);
1044 return true;
1045 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001046 return false;
1047}
1048
Evan Cheng5e351682006-02-06 06:02:33 +00001049bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
1050 SDOperand &Base, SDOperand &Scale,
1051 SDOperand &Index, SDOperand &Disp) {
Evan Cheng466685d2006-10-09 20:57:25 +00001052 if (ISD::isNON_EXTLoad(N.Val) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001053 N.hasOneUse() &&
Evan Cheng27e1fe92006-10-14 08:33:25 +00001054 CanBeFoldedBy(N.Val, P.Val, P.Val))
Evan Cheng0d538262006-11-08 20:34:28 +00001055 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
Evan Cheng0114e942006-01-06 20:36:21 +00001056 return false;
1057}
1058
Evan Cheng7ccced62006-02-18 00:15:05 +00001059/// getGlobalBaseReg - Output the instructions required to put the
1060/// base address to use for accessing globals into a register.
1061///
Evan Cheng9ade2182006-08-26 05:34:46 +00001062SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng25ab6902006-09-08 06:48:29 +00001063 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Evan Cheng7ccced62006-02-18 00:15:05 +00001064 if (!GlobalBaseReg) {
1065 // Insert the set of GlobalBaseReg into the first MBB of the function
Evan Cheng0475ab52008-01-05 00:41:47 +00001066 MachineFunction *MF = BB->getParent();
1067 MachineBasicBlock &FirstMBB = MF->front();
Evan Cheng7ccced62006-02-18 00:15:05 +00001068 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Evan Cheng0475ab52008-01-05 00:41:47 +00001069 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Chris Lattner84bc5422007-12-31 04:13:23 +00001070 unsigned PC = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001071
Evan Chengc0f64ff2006-11-27 23:37:22 +00001072 const TargetInstrInfo *TII = TM.getInstrInfo();
Evan Chengf02ca692007-12-22 02:26:46 +00001073 // Operand of MovePCtoStack is completely ignored by asm printer. It's
1074 // only used in JIT code emission as displacement to pc.
Evan Cheng0475ab52008-01-05 00:41:47 +00001075 BuildMI(FirstMBB, MBBI, TII->get(X86::MOVPC32r), PC).addImm(0);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001076
1077 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
1078 // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
Evan Cheng706535d2007-01-22 21:34:25 +00001079 if (TM.getRelocationModel() == Reloc::PIC_ &&
1080 Subtarget->isPICStyleGOT()) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001081 GlobalBaseReg = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng0475ab52008-01-05 00:41:47 +00001082 BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg)
1083 .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001084 } else {
1085 GlobalBaseReg = PC;
1086 }
1087
Evan Cheng7ccced62006-02-18 00:15:05 +00001088 }
Evan Cheng25ab6902006-09-08 06:48:29 +00001089 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
Evan Cheng7ccced62006-02-18 00:15:05 +00001090}
1091
Evan Chengb245d922006-05-20 01:36:52 +00001092static SDNode *FindCallStartFromCall(SDNode *Node) {
1093 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1094 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1095 "Node doesn't have a token chain argument!");
1096 return FindCallStartFromCall(Node->getOperand(0).Val);
1097}
1098
Christopher Lambc59e5212007-08-10 21:48:46 +00001099SDNode *X86DAGToDAGISel::getTruncate(SDOperand N0, MVT::ValueType VT) {
1100 SDOperand SRIdx;
1101 switch (VT) {
1102 case MVT::i8:
1103 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1104 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1105 if (!Subtarget->is64Bit()) {
1106 unsigned Opc;
1107 MVT::ValueType VT;
1108 switch (N0.getValueType()) {
1109 default: assert(0 && "Unknown truncate!");
1110 case MVT::i16:
1111 Opc = X86::MOV16to16_;
1112 VT = MVT::i16;
1113 break;
1114 case MVT::i32:
1115 Opc = X86::MOV32to32_;
1116 VT = MVT::i32;
1117 break;
1118 }
Evan Cheng96aaa542007-10-12 07:55:53 +00001119 N0 = SDOperand(CurDAG->getTargetNode(Opc, VT, MVT::Flag, N0), 0);
1120 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1121 VT, N0, SRIdx, N0.getValue(1));
Christopher Lambc59e5212007-08-10 21:48:46 +00001122 }
1123 break;
1124 case MVT::i16:
1125 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1126 break;
1127 case MVT::i32:
1128 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1129 break;
Evan Cheng96aaa542007-10-12 07:55:53 +00001130 default: assert(0 && "Unknown truncate!"); break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001131 }
Evan Cheng96aaa542007-10-12 07:55:53 +00001132 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, VT, N0, SRIdx);
Christopher Lambc59e5212007-08-10 21:48:46 +00001133}
1134
1135
Evan Cheng9ade2182006-08-26 05:34:46 +00001136SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Chengdef941b2005-12-15 01:02:48 +00001137 SDNode *Node = N.Val;
1138 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001139 unsigned Opc, MOpc;
1140 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +00001141
Evan Chengf597dc72006-02-10 22:24:32 +00001142#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001143 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001144 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001145 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001146 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001147#endif
1148
Evan Cheng34167212006-02-09 00:37:58 +00001149 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengf597dc72006-02-10 22:24:32 +00001150#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001151 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001152 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001153 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001154 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001155#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001156 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001157 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001158
Evan Cheng0114e942006-01-06 20:36:21 +00001159 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001160 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001161 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001162 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001163
Chris Lattner447ff682008-03-11 03:23:40 +00001164 // FIXME: This is a workaround for a tblgen problem: rdar://5791600
1165 case X86ISD::RET_FLAG:
1166 if (ConstantSDNode *Amt = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1167 if (Amt->getSignExtended() != 0) break;
1168
1169 // Match (X86retflag 0).
1170 SDOperand Chain = N.getOperand(0);
1171 bool HasInFlag = N.getOperand(N.getNumOperands()-1).getValueType()
1172 == MVT::Flag;
1173 SmallVector<SDOperand, 8> Ops0;
1174 AddToISelQueue(Chain);
1175 SDOperand InFlag(0, 0);
1176 if (HasInFlag) {
1177 InFlag = N.getOperand(N.getNumOperands()-1);
1178 AddToISelQueue(InFlag);
1179 }
1180 for (unsigned i = 2, e = N.getNumOperands()-(HasInFlag?1:0); i != e;
1181 ++i) {
1182 AddToISelQueue(N.getOperand(i));
1183 Ops0.push_back(N.getOperand(i));
1184 }
1185 Ops0.push_back(Chain);
1186 if (HasInFlag)
1187 Ops0.push_back(InFlag);
1188 return CurDAG->getTargetNode(X86::RET, MVT::Other,
1189 &Ops0[0], Ops0.size());
1190 }
1191 break;
1192
Evan Cheng51a9ed92006-02-25 10:09:08 +00001193 case ISD::ADD: {
1194 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1195 // code and is matched first so to prevent it from being turned into
1196 // LEA32r X+c.
Evan Chengb1a9aec2008-01-08 02:06:11 +00001197 // In 64-bit small code size mode, use LEA to take advantage of
1198 // RIP-relative addressing.
1199 if (TM.getCodeModel() != CodeModel::Small)
1200 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001201 MVT::ValueType PtrVT = TLI.getPointerTy();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001202 SDOperand N0 = N.getOperand(0);
1203 SDOperand N1 = N.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00001204 if (N.Val->getValueType(0) == PtrVT &&
Evan Cheng19f2ffc2006-12-05 04:01:03 +00001205 N0.getOpcode() == X86ISD::Wrapper &&
Evan Cheng51a9ed92006-02-25 10:09:08 +00001206 N1.getOpcode() == ISD::Constant) {
1207 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1208 SDOperand C(0, 0);
1209 // TODO: handle ExternalSymbolSDNode.
1210 if (GlobalAddressSDNode *G =
1211 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001212 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001213 G->getOffset() + Offset);
1214 } else if (ConstantPoolSDNode *CP =
1215 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Chengc356a572006-09-12 21:04:05 +00001216 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001217 CP->getAlignment(),
1218 CP->getOffset()+Offset);
1219 }
1220
Evan Cheng25ab6902006-09-08 06:48:29 +00001221 if (C.Val) {
1222 if (Subtarget->is64Bit()) {
1223 SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
1224 CurDAG->getRegister(0, PtrVT), C };
1225 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1226 } else
1227 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1228 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001229 }
1230
1231 // Other cases are handled by auto-generated code.
1232 break;
Evan Chenga0ea0532006-02-23 02:43:52 +00001233 }
Evan Cheng020d2e82006-02-23 20:41:18 +00001234
Dan Gohman525178c2007-10-08 18:33:35 +00001235 case ISD::SMUL_LOHI:
1236 case ISD::UMUL_LOHI: {
1237 SDOperand N0 = Node->getOperand(0);
1238 SDOperand N1 = Node->getOperand(1);
1239
Dan Gohman525178c2007-10-08 18:33:35 +00001240 bool isSigned = Opcode == ISD::SMUL_LOHI;
1241 if (!isSigned)
Evan Cheng0114e942006-01-06 20:36:21 +00001242 switch (NVT) {
1243 default: assert(0 && "Unsupported VT!");
1244 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1245 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1246 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001247 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001248 }
1249 else
1250 switch (NVT) {
1251 default: assert(0 && "Unsupported VT!");
1252 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1253 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1254 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001255 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001256 }
1257
1258 unsigned LoReg, HiReg;
1259 switch (NVT) {
1260 default: assert(0 && "Unsupported VT!");
1261 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1262 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1263 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001264 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001265 }
1266
Evan Cheng0114e942006-01-06 20:36:21 +00001267 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng7afa1662007-08-02 05:48:35 +00001268 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman525178c2007-10-08 18:33:35 +00001269 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001270 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +00001271 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng7afa1662007-08-02 05:48:35 +00001272 if (foldedLoad)
1273 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001274 }
1275
Evan Cheng04699902006-08-26 01:05:16 +00001276 AddToISelQueue(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001277 SDOperand InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1278 N0, SDOperand()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001279
1280 if (foldedLoad) {
Dan Gohman525178c2007-10-08 18:33:35 +00001281 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001282 AddToISelQueue(Tmp0);
1283 AddToISelQueue(Tmp1);
1284 AddToISelQueue(Tmp2);
1285 AddToISelQueue(Tmp3);
Dan Gohman525178c2007-10-08 18:33:35 +00001286 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001287 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001288 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001289 InFlag = SDOperand(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001290 // Update the chain.
1291 ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001292 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001293 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001294 InFlag =
1295 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001296 }
1297
Dan Gohman525178c2007-10-08 18:33:35 +00001298 // Copy the low half of the result, if it is needed.
1299 if (!N.getValue(0).use_empty()) {
1300 SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1301 LoReg, NVT, InFlag);
1302 InFlag = Result.getValue(2);
1303 ReplaceUses(N.getValue(0), Result);
1304#ifndef NDEBUG
1305 DOUT << std::string(Indent-2, ' ') << "=> ";
1306 DEBUG(Result.Val->dump(CurDAG));
1307 DOUT << "\n";
1308#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001309 }
Dan Gohman525178c2007-10-08 18:33:35 +00001310 // Copy the high half of the result, if it is needed.
1311 if (!N.getValue(1).use_empty()) {
1312 SDOperand Result;
1313 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1314 // Prevent use of AH in a REX instruction by referencing AX instead.
1315 // Shift it down 8 bits.
1316 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1317 X86::AX, MVT::i16, InFlag);
1318 InFlag = Result.getValue(2);
1319 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1320 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1321 // Then truncate it down to i8.
1322 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1323 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1324 MVT::i8, Result, SRIdx), 0);
1325 } else {
1326 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1327 HiReg, NVT, InFlag);
1328 InFlag = Result.getValue(2);
1329 }
1330 ReplaceUses(N.getValue(1), Result);
1331#ifndef NDEBUG
1332 DOUT << std::string(Indent-2, ' ') << "=> ";
1333 DEBUG(Result.Val->dump(CurDAG));
1334 DOUT << "\n";
1335#endif
1336 }
Evan Cheng34167212006-02-09 00:37:58 +00001337
Evan Chengf597dc72006-02-10 22:24:32 +00001338#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001339 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001340#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001341
Evan Cheng64a752f2006-08-11 09:08:15 +00001342 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001343 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001344
Dan Gohman525178c2007-10-08 18:33:35 +00001345 case ISD::SDIVREM:
1346 case ISD::UDIVREM: {
1347 SDOperand N0 = Node->getOperand(0);
1348 SDOperand N1 = Node->getOperand(1);
1349
1350 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001351 if (!isSigned)
1352 switch (NVT) {
1353 default: assert(0 && "Unsupported VT!");
1354 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1355 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1356 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001357 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001358 }
1359 else
1360 switch (NVT) {
1361 default: assert(0 && "Unsupported VT!");
1362 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1363 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1364 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001365 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001366 }
1367
1368 unsigned LoReg, HiReg;
1369 unsigned ClrOpcode, SExtOpcode;
1370 switch (NVT) {
1371 default: assert(0 && "Unsupported VT!");
1372 case MVT::i8:
1373 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001374 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001375 SExtOpcode = X86::CBW;
1376 break;
1377 case MVT::i16:
1378 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001379 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001380 SExtOpcode = X86::CWD;
1381 break;
1382 case MVT::i32:
1383 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001384 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001385 SExtOpcode = X86::CDQ;
1386 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001387 case MVT::i64:
1388 LoReg = X86::RAX; HiReg = X86::RDX;
1389 ClrOpcode = X86::MOV64r0;
1390 SExtOpcode = X86::CQO;
1391 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001392 }
1393
Dan Gohman525178c2007-10-08 18:33:35 +00001394 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
1395 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1396
1397 SDOperand InFlag;
Evan Chengb1409ce2006-11-17 22:10:14 +00001398 if (NVT == MVT::i8 && !isSigned) {
1399 // Special case for div8, just use a move with zero extension to AX to
1400 // clear the upper 8 bits (AH).
1401 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
1402 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
1403 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
1404 AddToISelQueue(N0.getOperand(0));
1405 AddToISelQueue(Tmp0);
1406 AddToISelQueue(Tmp1);
1407 AddToISelQueue(Tmp2);
1408 AddToISelQueue(Tmp3);
1409 Move =
1410 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
1411 Ops, 5), 0);
1412 Chain = Move.getValue(1);
1413 ReplaceUses(N0.getValue(1), Chain);
1414 } else {
1415 AddToISelQueue(N0);
1416 Move =
1417 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
1418 Chain = CurDAG->getEntryNode();
1419 }
Dan Gohman525178c2007-10-08 18:33:35 +00001420 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDOperand());
Evan Cheng948f3432006-01-06 23:19:29 +00001421 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001422 } else {
1423 AddToISelQueue(N0);
1424 InFlag =
Dan Gohman525178c2007-10-08 18:33:35 +00001425 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
1426 LoReg, N0, SDOperand()).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001427 if (isSigned) {
1428 // Sign extend the low part into the high part.
1429 InFlag =
1430 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
1431 } else {
1432 // Zero out the high part, effectively zero extending the input.
1433 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001434 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1435 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001436 }
Evan Cheng948f3432006-01-06 23:19:29 +00001437 }
1438
1439 if (foldedLoad) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001440 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001441 AddToISelQueue(Tmp0);
1442 AddToISelQueue(Tmp1);
1443 AddToISelQueue(Tmp2);
1444 AddToISelQueue(Tmp3);
Evan Chengb1409ce2006-11-17 22:10:14 +00001445 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001446 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001447 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001448 InFlag = SDOperand(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001449 // Update the chain.
1450 ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001451 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001452 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001453 InFlag =
1454 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001455 }
1456
Dan Gohmana37c9f72007-09-25 18:23:27 +00001457 // Copy the division (low) result, if it is needed.
1458 if (!N.getValue(0).use_empty()) {
Dan Gohman525178c2007-10-08 18:33:35 +00001459 SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1460 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001461 InFlag = Result.getValue(2);
1462 ReplaceUses(N.getValue(0), Result);
1463#ifndef NDEBUG
1464 DOUT << std::string(Indent-2, ' ') << "=> ";
1465 DEBUG(Result.Val->dump(CurDAG));
1466 DOUT << "\n";
1467#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001468 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001469 // Copy the remainder (high) result, if it is needed.
1470 if (!N.getValue(1).use_empty()) {
1471 SDOperand Result;
1472 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1473 // Prevent use of AH in a REX instruction by referencing AX instead.
1474 // Shift it down 8 bits.
Dan Gohman525178c2007-10-08 18:33:35 +00001475 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1476 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001477 InFlag = Result.getValue(2);
1478 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1479 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1480 // Then truncate it down to i8.
1481 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1482 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1483 MVT::i8, Result, SRIdx), 0);
1484 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00001485 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1486 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001487 InFlag = Result.getValue(2);
1488 }
1489 ReplaceUses(N.getValue(1), Result);
1490#ifndef NDEBUG
1491 DOUT << std::string(Indent-2, ' ') << "=> ";
1492 DEBUG(Result.Val->dump(CurDAG));
1493 DOUT << "\n";
1494#endif
1495 }
Evan Chengf597dc72006-02-10 22:24:32 +00001496
1497#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001498 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001499#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001500
1501 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001502 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001503
1504 case ISD::ANY_EXTEND: {
Christopher Lambc9298232008-03-16 03:12:01 +00001505 // Check if the type extended to supports subregs.
1506 if (NVT == MVT::i8)
1507 break;
1508
Christopher Lamba1eb1552007-08-10 22:22:41 +00001509 SDOperand N0 = Node->getOperand(0);
Christopher Lambc9298232008-03-16 03:12:01 +00001510 // Get the subregsiter index for the type to extend.
1511 MVT::ValueType N0VT = N0.getValueType();
1512 unsigned Idx = (N0VT == MVT::i32) ? X86::SUBREG_32BIT :
1513 (N0VT == MVT::i16) ? X86::SUBREG_16BIT :
1514 (Subtarget->is64Bit()) ? X86::SUBREG_8BIT : 0;
1515
1516 // If we don't have a subreg Idx, let generated ISel have a try.
1517 if (Idx == 0)
1518 break;
1519
1520 // If we have an index, generate an insert_subreg into undef.
Christopher Lamba1eb1552007-08-10 22:22:41 +00001521 AddToISelQueue(N0);
Christopher Lambc9298232008-03-16 03:12:01 +00001522 SDOperand Undef =
Evan Cheng90ce87b2008-04-03 07:45:18 +00001523 SDOperand(CurDAG->getTargetNode(X86::IMPLICIT_DEF, NVT), 0);
Christopher Lambc9298232008-03-16 03:12:01 +00001524 SDOperand SRIdx = CurDAG->getTargetConstant(Idx, MVT::i32);
1525 SDNode *ResNode = CurDAG->getTargetNode(X86::INSERT_SUBREG,
Evan Cheng90ce87b2008-04-03 07:45:18 +00001526 NVT, Undef, N0, SRIdx);
Christopher Lamba1eb1552007-08-10 22:22:41 +00001527
1528#ifndef NDEBUG
Christopher Lambc9298232008-03-16 03:12:01 +00001529 DOUT << std::string(Indent-2, ' ') << "=> ";
1530 DEBUG(ResNode->dump(CurDAG));
1531 DOUT << "\n";
1532 Indent -= 2;
Christopher Lamba1eb1552007-08-10 22:22:41 +00001533#endif
Christopher Lambc9298232008-03-16 03:12:01 +00001534 return ResNode;
Christopher Lamba1eb1552007-08-10 22:22:41 +00001535 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001536
1537 case ISD::SIGN_EXTEND_INREG: {
1538 SDOperand N0 = Node->getOperand(0);
1539 AddToISelQueue(N0);
Evan Cheng403be7e2006-05-08 08:01:26 +00001540
Christopher Lambc59e5212007-08-10 21:48:46 +00001541 MVT::ValueType SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
1542 SDOperand TruncOp = SDOperand(getTruncate(N0, SVT), 0);
Bill Wendling0d642872007-11-01 08:51:44 +00001543 unsigned Opc = 0;
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001544 switch (NVT) {
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001545 case MVT::i16:
Christopher Lambc59e5212007-08-10 21:48:46 +00001546 if (SVT == MVT::i8) Opc = X86::MOVSX16rr8;
1547 else assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001548 break;
1549 case MVT::i32:
Christopher Lambc59e5212007-08-10 21:48:46 +00001550 switch (SVT) {
1551 case MVT::i8: Opc = X86::MOVSX32rr8; break;
1552 case MVT::i16: Opc = X86::MOVSX32rr16; break;
1553 default: assert(0 && "Unknown sign_extend_inreg!");
1554 }
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001555 break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001556 case MVT::i64:
1557 switch (SVT) {
1558 case MVT::i8: Opc = X86::MOVSX64rr8; break;
1559 case MVT::i16: Opc = X86::MOVSX64rr16; break;
1560 case MVT::i32: Opc = X86::MOVSX64rr32; break;
1561 default: assert(0 && "Unknown sign_extend_inreg!");
1562 }
1563 break;
1564 default: assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001565 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001566
1567 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
1568
1569#ifndef NDEBUG
1570 DOUT << std::string(Indent-2, ' ') << "=> ";
1571 DEBUG(TruncOp.Val->dump(CurDAG));
1572 DOUT << "\n";
1573 DOUT << std::string(Indent-2, ' ') << "=> ";
1574 DEBUG(ResNode->dump(CurDAG));
1575 DOUT << "\n";
1576 Indent -= 2;
1577#endif
1578 return ResNode;
1579 break;
1580 }
1581
1582 case ISD::TRUNCATE: {
1583 SDOperand Input = Node->getOperand(0);
1584 AddToISelQueue(Node->getOperand(0));
1585 SDNode *ResNode = getTruncate(Input, NVT);
1586
Evan Cheng403be7e2006-05-08 08:01:26 +00001587#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001588 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001589 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001590 DOUT << "\n";
Evan Cheng403be7e2006-05-08 08:01:26 +00001591 Indent -= 2;
1592#endif
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001593 return ResNode;
Evan Cheng6b2e2542006-05-20 07:44:28 +00001594 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001595 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001596 }
1597
Evan Cheng9ade2182006-08-26 05:34:46 +00001598 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001599
Evan Chengf597dc72006-02-10 22:24:32 +00001600#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001601 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001602 if (ResNode == NULL || ResNode == N.Val)
1603 DEBUG(N.Val->dump(CurDAG));
1604 else
1605 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001606 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001607 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001608#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001609
1610 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001611}
1612
Chris Lattnerc0bad572006-06-08 18:03:49 +00001613bool X86DAGToDAGISel::
1614SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1615 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1616 SDOperand Op0, Op1, Op2, Op3;
1617 switch (ConstraintCode) {
1618 case 'o': // offsetable ??
1619 case 'v': // not offsetable ??
1620 default: return true;
1621 case 'm': // memory
Evan Cheng0d538262006-11-08 20:34:28 +00001622 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001623 return true;
1624 break;
1625 }
1626
Evan Cheng04699902006-08-26 01:05:16 +00001627 OutOps.push_back(Op0);
1628 OutOps.push_back(Op1);
1629 OutOps.push_back(Op2);
1630 OutOps.push_back(Op3);
1631 AddToISelQueue(Op0);
1632 AddToISelQueue(Op1);
1633 AddToISelQueue(Op2);
1634 AddToISelQueue(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001635 return false;
1636}
1637
Chris Lattnerc961eea2005-11-16 01:54:32 +00001638/// createX86ISelDag - This pass converts a legalized DAG into a
1639/// X86-specific DAG, ready for instruction scheduling.
1640///
Evan Chenge50794a2006-08-29 18:28:33 +00001641FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1642 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001643}