blob: 1ca1e311449f43fc525867bf4a9e63af911db5f7 [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 Chengcdda25d2008-04-25 08:22:20 +0000258 continue; // We are not looking for immediate use.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000259 if (Use == Root) {
260 assert(Use->getOpcode() == ISD::STORE ||
Chris Lattner25453ea2008-04-25 05:13:01 +0000261 Use->getOpcode() == X86ISD::CMP ||
262 Use->getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
263 Use->getOpcode() == ISD::INTRINSIC_W_CHAIN ||
264 Use->getOpcode() == ISD::INTRINSIC_VOID);
Evan Cheng27e1fe92006-10-14 08:33:25 +0000265 continue;
266 }
Evan Chengf4b4c412006-08-08 00:31:00 +0000267 found = true;
268 break;
269 }
Evan Chengcdda25d2008-04-25 08:22:20 +0000270
271 // Traverse up the operand chain.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000272 findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000273 }
274}
275
Evan Cheng27e1fe92006-10-14 08:33:25 +0000276/// isNonImmUse - Start searching from Root up the DAG to check is Def can
277/// be reached. Return true if that's the case. However, ignore direct uses
278/// by ImmedUse (which would be U in the example illustrated in
279/// CanBeFoldedBy) and by Root (which can happen in the store case).
280/// FIXME: to be really generic, we should allow direct use by any node
281/// that is being folded. But realisticly since we only fold loads which
282/// have one non-chain use, we only need to watch out for load/op/store
283/// and load/op/cmp case where the root (store / cmp) may reach the load via
284/// its chain operand.
285static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
286 SDNode *Skip = NULL) {
Evan Chengcdda25d2008-04-25 08:22:20 +0000287 SmallPtrSet<SDNode*, 16> Visited;
Evan Chengf4b4c412006-08-08 00:31:00 +0000288 bool found = false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000289 findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000290 return found;
291}
292
293
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000294bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
Evan Cheng27e1fe92006-10-14 08:33:25 +0000295 if (FastISel) return false;
296
Evan Chenga8df1b42006-07-27 16:44:36 +0000297 // If U use can somehow reach N through another path then U can't fold N or
298 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Cheng37e18032006-07-28 06:33:41 +0000299 // through X. If N is folded into into U, then X is both a predecessor and
Evan Chenga8df1b42006-07-27 16:44:36 +0000300 // a successor of U.
301 //
302 // [ N ]
303 // ^ ^
304 // | |
305 // / \---
306 // / [X]
307 // | ^
308 // [U]--------|
Evan Cheng27e1fe92006-10-14 08:33:25 +0000309
310 if (isNonImmUse(Root, N, U))
311 return false;
312
313 // If U produces a flag, then it gets (even more) interesting. Since it
314 // would have been "glued" together with its flag use, we need to check if
315 // it might reach N:
316 //
317 // [ N ]
318 // ^ ^
319 // | |
320 // [U] \--
321 // ^ [TF]
322 // | ^
323 // | |
324 // \ /
325 // [FU]
326 //
327 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
328 // NU), then TF is a predecessor of FU and a successor of NU. But since
329 // NU and FU are flagged together, this effectively creates a cycle.
330 bool HasFlagUse = false;
331 MVT::ValueType VT = Root->getValueType(Root->getNumValues()-1);
332 while ((VT == MVT::Flag && !Root->use_empty())) {
333 SDNode *FU = findFlagUse(Root);
334 if (FU == NULL)
335 break;
336 else {
337 Root = FU;
338 HasFlagUse = true;
Evan Chenga275ecb2006-10-10 01:46:56 +0000339 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000340 VT = Root->getValueType(Root->getNumValues()-1);
Evan Chenga275ecb2006-10-10 01:46:56 +0000341 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000342
343 if (HasFlagUse)
344 return !isNonImmUse(Root, N, Root, U);
345 return true;
Evan Chenga8df1b42006-07-27 16:44:36 +0000346}
347
Evan Cheng70e674e2006-08-28 20:10:17 +0000348/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
349/// and move load below the TokenFactor. Replace store's chain operand with
350/// load's chain result.
351static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
352 SDOperand Store, SDOperand TF) {
353 std::vector<SDOperand> Ops;
354 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
355 if (Load.Val == TF.Val->getOperand(i).Val)
356 Ops.push_back(Load.Val->getOperand(0));
357 else
358 Ops.push_back(TF.Val->getOperand(i));
359 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
360 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
361 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
362 Store.getOperand(2), Store.getOperand(3));
363}
364
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000365/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
366/// This is only run if not in -fast mode (aka -O0).
367/// This allows the instruction selector to pick more read-modify-write
368/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000369///
370/// [Load chain]
371/// ^
372/// |
373/// [Load]
374/// ^ ^
375/// | |
376/// / \-
377/// / |
378/// [TokenFactor] [Op]
379/// ^ ^
380/// | |
381/// \ /
382/// \ /
383/// [Store]
384///
385/// The fact the store's chain operand != load's chain will prevent the
386/// (store (op (load))) instruction from being selected. We can transform it to:
387///
388/// [Load chain]
389/// ^
390/// |
391/// [TokenFactor]
392/// ^
393/// |
394/// [Load]
395/// ^ ^
396/// | |
397/// | \-
398/// | |
399/// | [Op]
400/// | ^
401/// | |
402/// \ /
403/// \ /
404/// [Store]
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000405void X86DAGToDAGISel::PreprocessForRMW(SelectionDAG &DAG) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000406 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
407 E = DAG.allnodes_end(); I != E; ++I) {
Evan Cheng8b2794a2006-10-13 21:14:26 +0000408 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000409 continue;
410 SDOperand Chain = I->getOperand(0);
411 if (Chain.Val->getOpcode() != ISD::TokenFactor)
412 continue;
413
414 SDOperand N1 = I->getOperand(1);
415 SDOperand N2 = I->getOperand(2);
Evan Cheng1453de52006-09-01 22:52:28 +0000416 if (MVT::isFloatingPoint(N1.getValueType()) ||
417 MVT::isVector(N1.getValueType()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000418 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000419 continue;
420
421 bool RModW = false;
422 SDOperand Load;
423 unsigned Opcode = N1.Val->getOpcode();
424 switch (Opcode) {
425 case ISD::ADD:
426 case ISD::MUL:
Evan Cheng70e674e2006-08-28 20:10:17 +0000427 case ISD::AND:
428 case ISD::OR:
429 case ISD::XOR:
430 case ISD::ADDC:
431 case ISD::ADDE: {
432 SDOperand N10 = N1.getOperand(0);
433 SDOperand N11 = N1.getOperand(1);
Evan Cheng466685d2006-10-09 20:57:25 +0000434 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng70e674e2006-08-28 20:10:17 +0000435 RModW = true;
Evan Cheng466685d2006-10-09 20:57:25 +0000436 else if (ISD::isNON_EXTLoad(N11.Val)) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000437 RModW = true;
438 std::swap(N10, N11);
439 }
Evan Cheng07b7ea12008-03-04 00:40:35 +0000440 RModW = RModW && N10.Val->isOperandOf(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000441 (N10.getOperand(1) == N2) &&
442 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000443 if (RModW)
444 Load = N10;
445 break;
446 }
447 case ISD::SUB:
448 case ISD::SHL:
449 case ISD::SRA:
450 case ISD::SRL:
451 case ISD::ROTL:
452 case ISD::ROTR:
453 case ISD::SUBC:
454 case ISD::SUBE:
455 case X86ISD::SHLD:
456 case X86ISD::SHRD: {
457 SDOperand N10 = N1.getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +0000458 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng07b7ea12008-03-04 00:40:35 +0000459 RModW = N10.Val->isOperandOf(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000460 (N10.getOperand(1) == N2) &&
461 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000462 if (RModW)
463 Load = N10;
464 break;
465 }
466 }
467
Evan Cheng82a35b32006-08-29 06:44:17 +0000468 if (RModW) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000469 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000470 ++NumLoadMoved;
471 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000472 }
473}
474
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000475
476/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
477/// nodes that target the FP stack to be store and load to the stack. This is a
478/// gross hack. We would like to simply mark these as being illegal, but when
479/// we do that, legalize produces these when it expands calls, then expands
480/// these in the same legalize pass. We would like dag combine to be able to
481/// hack on these between the call expansion and the node legalization. As such
482/// this pass basically does "really late" legalization of these inline with the
483/// X86 isel pass.
484void X86DAGToDAGISel::PreprocessForFPConvert(SelectionDAG &DAG) {
485 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
486 E = DAG.allnodes_end(); I != E; ) {
487 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
488 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
489 continue;
490
491 // If the source and destination are SSE registers, then this is a legal
492 // conversion that should not be lowered.
493 MVT::ValueType SrcVT = N->getOperand(0).getValueType();
494 MVT::ValueType DstVT = N->getValueType(0);
495 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
496 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
497 if (SrcIsSSE && DstIsSSE)
498 continue;
499
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000500 if (!SrcIsSSE && !DstIsSSE) {
501 // If this is an FPStack extension, it is a noop.
502 if (N->getOpcode() == ISD::FP_EXTEND)
503 continue;
504 // If this is a value-preserving FPStack truncation, it is a noop.
505 if (N->getConstantOperandVal(1))
506 continue;
507 }
508
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000509 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
510 // FPStack has extload and truncstore. SSE can fold direct loads into other
511 // operations. Based on this, decide what we want to do.
512 MVT::ValueType MemVT;
513 if (N->getOpcode() == ISD::FP_ROUND)
514 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
515 else
516 MemVT = SrcIsSSE ? SrcVT : DstVT;
517
518 SDOperand MemTmp = DAG.CreateStackTemporary(MemVT);
519
520 // FIXME: optimize the case where the src/dest is a load or store?
521 SDOperand Store = DAG.getTruncStore(DAG.getEntryNode(), N->getOperand(0),
522 MemTmp, NULL, 0, MemVT);
523 SDOperand Result = DAG.getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp,
524 NULL, 0, MemVT);
525
526 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
527 // extload we created. This will cause general havok on the dag because
528 // anything below the conversion could be folded into other existing nodes.
529 // To avoid invalidating 'I', back it up to the convert node.
530 --I;
531 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result);
532
533 // Now that we did that, the node is dead. Increment the iterator to the
534 // next node to process, then delete N.
535 ++I;
536 DAG.DeleteNode(N);
537 }
538}
539
Chris Lattnerc961eea2005-11-16 01:54:32 +0000540/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
541/// when it has created a SelectionDAG for us to codegen.
542void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
543 DEBUG(BB->dump());
Chris Lattner92cb0af2006-01-11 01:15:34 +0000544 MachineFunction::iterator FirstMBB = BB;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000545
Evan Chenge50794a2006-08-29 18:28:33 +0000546 if (!FastISel)
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000547 PreprocessForRMW(DAG);
548
549 // FIXME: This should only happen when not -fast.
550 PreprocessForFPConvert(DAG);
Evan Cheng70e674e2006-08-28 20:10:17 +0000551
Chris Lattnerc961eea2005-11-16 01:54:32 +0000552 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000553#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000554 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000555 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000556#endif
Evan Chengba2f0a92006-02-05 06:46:41 +0000557 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengf597dc72006-02-10 22:24:32 +0000558#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000559 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000560#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000561
Chris Lattnerc961eea2005-11-16 01:54:32 +0000562 DAG.RemoveDeadNodes();
563
Chris Lattner03fdec02008-03-10 23:34:12 +0000564 // Emit machine code to BB. This can change 'BB' to the last block being
565 // inserted into.
Chris Lattnerc961eea2005-11-16 01:54:32 +0000566 ScheduleAndEmitDAG(DAG);
Chris Lattner92cb0af2006-01-11 01:15:34 +0000567
568 // If we are emitting FP stack code, scan the basic block to determine if this
569 // block defines any FP values. If so, put an FP_REG_KILL instruction before
570 // the terminator of the block.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000571
Dale Johannesen48d1e452007-09-24 22:52:39 +0000572 // Note that FP stack instructions are used in all modes for long double,
573 // so we always need to do this check.
574 // Also note that it's possible for an FP stack register to be live across
575 // an instruction that produces multiple basic blocks (SSE CMOV) so we
576 // must check all the generated basic blocks.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000577
578 // Scan all of the machine instructions in these MBBs, checking for FP
579 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
580 MachineFunction::iterator MBBI = FirstMBB;
Chris Lattner03fdec02008-03-10 23:34:12 +0000581 MachineFunction::iterator EndMBB = BB; ++EndMBB;
582 for (; MBBI != EndMBB; ++MBBI) {
583 MachineBasicBlock *MBB = MBBI;
584
585 // If this block returns, ignore it. We don't want to insert an FP_REG_KILL
586 // before the return.
587 if (!MBB->empty()) {
588 MachineBasicBlock::iterator EndI = MBB->end();
589 --EndI;
590 if (EndI->getDesc().isReturn())
591 continue;
592 }
593
Dale Johannesen48d1e452007-09-24 22:52:39 +0000594 bool ContainsFPCode = false;
Chris Lattner03fdec02008-03-10 23:34:12 +0000595 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000596 !ContainsFPCode && I != E; ++I) {
597 if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) {
598 const TargetRegisterClass *clas;
599 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
600 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
Chris Lattner03fdec02008-03-10 23:34:12 +0000601 TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
Chris Lattner84bc5422007-12-31 04:13:23 +0000602 ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) ==
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000603 X86::RFP32RegisterClass ||
604 clas == X86::RFP64RegisterClass ||
605 clas == X86::RFP80RegisterClass)) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000606 ContainsFPCode = true;
607 break;
608 }
609 }
610 }
611 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000612 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
613 // a copy of the input value in this block. In SSE mode, we only care about
614 // 80-bit values.
615 if (!ContainsFPCode) {
616 // Final check, check LLVM BB's that are successors to the LLVM BB
617 // corresponding to BB for FP PHI nodes.
618 const BasicBlock *LLVMBB = BB->getBasicBlock();
619 const PHINode *PN;
620 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
621 !ContainsFPCode && SI != E; ++SI) {
622 for (BasicBlock::const_iterator II = SI->begin();
623 (PN = dyn_cast<PHINode>(II)); ++II) {
624 if (PN->getType()==Type::X86_FP80Ty ||
625 (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) ||
626 (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) {
627 ContainsFPCode = true;
628 break;
629 }
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000630 }
631 }
Chris Lattner92cb0af2006-01-11 01:15:34 +0000632 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000633 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
634 if (ContainsFPCode) {
Chris Lattner03fdec02008-03-10 23:34:12 +0000635 BuildMI(*MBB, MBBI->getFirstTerminator(),
Dale Johannesen48d1e452007-09-24 22:52:39 +0000636 TM.getInstrInfo()->get(X86::FP_REG_KILL));
637 ++NumFPKill;
638 }
Chris Lattner03fdec02008-03-10 23:34:12 +0000639 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000640}
641
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000642/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
643/// the main function.
644void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
645 MachineFrameInfo *MFI) {
646 const TargetInstrInfo *TII = TM.getInstrInfo();
647 if (Subtarget->isTargetCygMing())
648 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
649}
650
651void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
652 // If this is main, emit special code for main.
653 MachineBasicBlock *BB = MF.begin();
654 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
655 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
656}
657
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000658/// MatchAddress - Add the specified node to the specified addressing mode,
659/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000660/// addressing mode.
Evan Cheng2486af12006-02-11 02:05:36 +0000661bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000662 bool isRoot, unsigned Depth) {
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000663 // Limit recursion.
664 if (Depth > 5)
665 return MatchAddressBase(N, AM, isRoot, Depth);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000666
Evan Cheng25ab6902006-09-08 06:48:29 +0000667 // RIP relative addressing: %rip + 32-bit displacement!
668 if (AM.isRIPRel) {
669 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000670 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000671 if (isInt32(AM.Disp + Val)) {
672 AM.Disp += Val;
673 return false;
674 }
675 }
676 return true;
677 }
678
Evan Cheng2ef88a02006-08-07 22:28:20 +0000679 int id = N.Val->getNodeId();
Evan Cheng1314b002007-12-13 00:43:27 +0000680 bool AlreadySelected = isSelected(id); // Already selected, not yet replaced.
Evan Cheng2486af12006-02-11 02:05:36 +0000681
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000682 switch (N.getOpcode()) {
683 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000684 case 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 break;
691 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000692
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000693 case X86ISD::Wrapper: {
694 bool is64Bit = Subtarget->is64Bit();
Evan Cheng0085a282006-11-30 21:55:46 +0000695 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Chengbe3bf422008-02-07 08:53:49 +0000696 // Also, base and index reg must be 0 in order to use rip as base.
697 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
698 AM.Base.Reg.Val || AM.IndexReg.Val))
Evan Cheng0085a282006-11-30 21:55:46 +0000699 break;
Evan Cheng28b514392006-12-05 19:50:18 +0000700 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
701 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000702 // If value is available in a register both base and index components have
703 // been picked, we can't fit the result available in the register in the
704 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Evan Cheng1314b002007-12-13 00:43:27 +0000705 if (!AlreadySelected || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
Evan Cheng28b514392006-12-05 19:50:18 +0000706 SDOperand N0 = N.getOperand(0);
707 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
708 GlobalValue *GV = G->getGlobal();
Evan Chengbe3bf422008-02-07 08:53:49 +0000709 AM.GV = GV;
710 AM.Disp += G->getOffset();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000711 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
712 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000713 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000714 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000715 AM.CP = CP->getConstVal();
716 AM.Align = CP->getAlignment();
717 AM.Disp += CP->getOffset();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000718 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
719 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000720 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000721 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000722 AM.ES = S->getSymbol();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000723 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
724 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000725 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000726 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000727 AM.JT = J->getIndex();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000728 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
729 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000730 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000731 }
732 }
733 break;
Evan Cheng0085a282006-11-30 21:55:46 +0000734 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000735
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000736 case ISD::FrameIndex:
737 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
738 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
739 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
740 return false;
741 }
742 break;
Evan Chengec693f72005-12-08 02:01:35 +0000743
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000744 case ISD::SHL:
Evan Chengbe3bf422008-02-07 08:53:49 +0000745 if (AlreadySelected || AM.IndexReg.Val != 0 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000746 break;
747
748 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
749 unsigned Val = CN->getValue();
750 if (Val == 1 || Val == 2 || Val == 3) {
751 AM.Scale = 1 << Val;
752 SDOperand ShVal = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000753
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000754 // Okay, we know that we have a scale by now. However, if the scaled
755 // value is an add of something and a constant, we can fold the
756 // constant into the disp field here.
757 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
758 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
759 AM.IndexReg = ShVal.Val->getOperand(0);
760 ConstantSDNode *AddVal =
761 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
762 uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
763 if (isInt32(Disp))
764 AM.Disp = Disp;
765 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000766 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000767 } else {
768 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000769 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000770 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000771 }
772 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000773 }
Evan Chengec693f72005-12-08 02:01:35 +0000774
Dan Gohman83688052007-10-22 20:22:24 +0000775 case ISD::SMUL_LOHI:
776 case ISD::UMUL_LOHI:
777 // A mul_lohi where we need the low part can be folded as a plain multiply.
778 if (N.ResNo != 0) break;
779 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000780 case ISD::MUL:
781 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng1314b002007-12-13 00:43:27 +0000782 if (!AlreadySelected &&
Evan Cheng51a9ed92006-02-25 10:09:08 +0000783 AM.BaseType == X86ISelAddressMode::RegBase &&
784 AM.Base.Reg.Val == 0 &&
Evan Chengbe3bf422008-02-07 08:53:49 +0000785 AM.IndexReg.Val == 0 &&
786 !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000787 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
788 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
789 AM.Scale = unsigned(CN->getValue())-1;
790
791 SDOperand MulVal = N.Val->getOperand(0);
792 SDOperand Reg;
793
794 // Okay, we know that we have a scale by now. However, if the scaled
795 // value is an add of something and a constant, we can fold the
796 // constant into the disp field here.
797 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
798 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
799 Reg = MulVal.Val->getOperand(0);
800 ConstantSDNode *AddVal =
801 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
Evan Cheng25ab6902006-09-08 06:48:29 +0000802 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
803 if (isInt32(Disp))
804 AM.Disp = Disp;
805 else
806 Reg = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000807 } else {
808 Reg = N.Val->getOperand(0);
809 }
810
811 AM.IndexReg = AM.Base.Reg = Reg;
812 return false;
813 }
Chris Lattner62412262007-02-04 20:18:17 +0000814 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000815 break;
816
Chris Lattner62412262007-02-04 20:18:17 +0000817 case ISD::ADD:
Evan Cheng1314b002007-12-13 00:43:27 +0000818 if (!AlreadySelected) {
Evan Cheng2486af12006-02-11 02:05:36 +0000819 X86ISelAddressMode Backup = AM;
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000820 if (!MatchAddress(N.Val->getOperand(0), AM, false, Depth+1) &&
821 !MatchAddress(N.Val->getOperand(1), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000822 return false;
823 AM = Backup;
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000824 if (!MatchAddress(N.Val->getOperand(1), AM, false, Depth+1) &&
825 !MatchAddress(N.Val->getOperand(0), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000826 return false;
827 AM = Backup;
828 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000829 break;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000830
Chris Lattner62412262007-02-04 20:18:17 +0000831 case ISD::OR:
832 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Evan Cheng1314b002007-12-13 00:43:27 +0000833 if (AlreadySelected) break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000834
835 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
836 X86ISelAddressMode Backup = AM;
837 // Start with the LHS as an addr mode.
838 if (!MatchAddress(N.getOperand(0), AM, false) &&
839 // Address could not have picked a GV address for the displacement.
840 AM.GV == NULL &&
841 // On x86-64, the resultant disp must fit in 32-bits.
842 isInt32(AM.Disp + CN->getSignExtended()) &&
843 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000844 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000845 AM.Disp += CN->getValue();
846 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000847 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000848 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000849 }
850 break;
Evan Cheng1314b002007-12-13 00:43:27 +0000851
852 case ISD::AND: {
853 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
854 // allows us to fold the shift into this addressing mode.
855 if (AlreadySelected) break;
856 SDOperand Shift = N.getOperand(0);
857 if (Shift.getOpcode() != ISD::SHL) break;
858
859 // Scale must not be used already.
860 if (AM.IndexReg.Val != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +0000861
862 // Not when RIP is used as the base.
863 if (AM.isRIPRel) break;
Evan Cheng1314b002007-12-13 00:43:27 +0000864
865 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
866 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
867 if (!C1 || !C2) break;
868
869 // Not likely to be profitable if either the AND or SHIFT node has more
870 // than one use (unless all uses are for address computation). Besides,
871 // isel mechanism requires their node ids to be reused.
872 if (!N.hasOneUse() || !Shift.hasOneUse())
873 break;
874
875 // Verify that the shift amount is something we can fold.
876 unsigned ShiftCst = C1->getValue();
877 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
878 break;
879
880 // Get the new AND mask, this folds to a constant.
881 SDOperand NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
882 SDOperand(C2, 0), SDOperand(C1, 0));
883 SDOperand NewAND = CurDAG->getNode(ISD::AND, N.getValueType(),
884 Shift.getOperand(0), NewANDMask);
885 NewANDMask.Val->setNodeId(Shift.Val->getNodeId());
886 NewAND.Val->setNodeId(N.Val->getNodeId());
887
888 AM.Scale = 1 << ShiftCst;
889 AM.IndexReg = NewAND;
890 return false;
891 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000892 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000893
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000894 return MatchAddressBase(N, AM, isRoot, Depth);
895}
896
897/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
898/// specified addressing mode without any further recursion.
899bool X86DAGToDAGISel::MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
900 bool isRoot, unsigned Depth) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000901 // Is the base register already occupied?
902 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
903 // If so, check to see if the scale index register is set.
Evan Chengbe3bf422008-02-07 08:53:49 +0000904 if (AM.IndexReg.Val == 0 && !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000905 AM.IndexReg = N;
906 AM.Scale = 1;
907 return false;
908 }
909
910 // Otherwise, we cannot select it.
911 return true;
912 }
913
914 // Default, generate it as a register.
915 AM.BaseType = X86ISelAddressMode::RegBase;
916 AM.Base.Reg = N;
917 return false;
918}
919
Evan Chengec693f72005-12-08 02:01:35 +0000920/// SelectAddr - returns true if it is able pattern match an addressing mode.
921/// It returns the operands which make up the maximal addressing mode it can
922/// match by reference.
Evan Cheng0d538262006-11-08 20:34:28 +0000923bool X86DAGToDAGISel::SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
924 SDOperand &Scale, SDOperand &Index,
925 SDOperand &Disp) {
Evan Chengec693f72005-12-08 02:01:35 +0000926 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +0000927 if (MatchAddress(N, AM))
928 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000929
Evan Cheng25ab6902006-09-08 06:48:29 +0000930 MVT::ValueType VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +0000931 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Cheng7dd281b2006-02-05 05:25:07 +0000932 if (!AM.Base.Reg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000933 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +0000934 }
Evan Cheng8700e142006-01-11 06:09:51 +0000935
Evan Cheng7dd281b2006-02-05 05:25:07 +0000936 if (!AM.IndexReg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000937 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +0000938
939 getAddressOperands(AM, Base, Scale, Index, Disp);
940 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000941}
942
Chris Lattner4fe4f252006-10-11 22:09:58 +0000943/// isZeroNode - Returns true if Elt is a constant zero or a floating point
944/// constant +0.0.
945static inline bool isZeroNode(SDOperand Elt) {
946 return ((isa<ConstantSDNode>(Elt) &&
947 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
948 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +0000949 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Chris Lattner4fe4f252006-10-11 22:09:58 +0000950}
951
952
Chris Lattner3a7cd952006-10-07 21:55:32 +0000953/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
954/// match a load whose top elements are either undef or zeros. The load flavor
955/// is derived from the type of N, which is either v4f32 or v2f64.
Evan Cheng0d538262006-11-08 20:34:28 +0000956bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Cheng07e4b002006-10-16 06:34:55 +0000957 SDOperand N, SDOperand &Base,
Evan Cheng82a91642006-10-11 21:06:01 +0000958 SDOperand &Scale, SDOperand &Index,
959 SDOperand &Disp, SDOperand &InChain,
960 SDOperand &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +0000961 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +0000962 InChain = N.getOperand(0).getValue(1);
Evan Cheng07e4b002006-10-16 06:34:55 +0000963 if (ISD::isNON_EXTLoad(InChain.Val) &&
964 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +0000965 N.hasOneUse() &&
Evan Cheng0d538262006-11-08 20:34:28 +0000966 CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
Evan Cheng82a91642006-10-11 21:06:01 +0000967 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Evan Cheng0d538262006-11-08 20:34:28 +0000968 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner3a7cd952006-10-07 21:55:32 +0000969 return false;
Evan Cheng82a91642006-10-11 21:06:01 +0000970 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +0000971 return true;
972 }
973 }
Chris Lattner4fe4f252006-10-11 22:09:58 +0000974
975 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +0000976 // elements. This is a vector shuffle from the zero vector.
Chris Lattner4fe4f252006-10-11 22:09:58 +0000977 if (N.getOpcode() == ISD::VECTOR_SHUFFLE && N.Val->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +0000978 // Check to see if the top elements are all zeros (or bitcast of zeros).
979 ISD::isBuildVectorAllZeros(N.getOperand(0).Val) &&
Chris Lattner4fe4f252006-10-11 22:09:58 +0000980 N.getOperand(1).getOpcode() == ISD::SCALAR_TO_VECTOR &&
981 N.getOperand(1).Val->hasOneUse() &&
982 ISD::isNON_EXTLoad(N.getOperand(1).getOperand(0).Val) &&
983 N.getOperand(1).getOperand(0).hasOneUse()) {
Chris Lattner4fe4f252006-10-11 22:09:58 +0000984 // Check to see if the shuffle mask is 4/L/L/L or 2/L, where L is something
985 // from the LHS.
Chris Lattner8a594482007-11-25 00:24:49 +0000986 unsigned VecWidth=MVT::getVectorNumElements(N.getOperand(0).getValueType());
Chris Lattner4fe4f252006-10-11 22:09:58 +0000987 SDOperand ShufMask = N.getOperand(2);
988 assert(ShufMask.getOpcode() == ISD::BUILD_VECTOR && "Invalid shuf mask!");
989 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(ShufMask.getOperand(0))) {
990 if (C->getValue() == VecWidth) {
991 for (unsigned i = 1; i != VecWidth; ++i) {
992 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF) {
993 // ok.
994 } else {
995 ConstantSDNode *C = cast<ConstantSDNode>(ShufMask.getOperand(i));
996 if (C->getValue() >= VecWidth) return false;
997 }
998 }
999 }
1000
1001 // Okay, this is a zero extending load. Fold it.
1002 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(1).getOperand(0));
Evan Cheng0d538262006-11-08 20:34:28 +00001003 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner4fe4f252006-10-11 22:09:58 +00001004 return false;
1005 OutChain = LD->getChain();
1006 InChain = SDOperand(LD, 1);
1007 return true;
1008 }
1009 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001010 return false;
1011}
1012
1013
Evan Cheng51a9ed92006-02-25 10:09:08 +00001014/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1015/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng0d538262006-11-08 20:34:28 +00001016bool X86DAGToDAGISel::SelectLEAAddr(SDOperand Op, SDOperand N,
1017 SDOperand &Base, SDOperand &Scale,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001018 SDOperand &Index, SDOperand &Disp) {
1019 X86ISelAddressMode AM;
1020 if (MatchAddress(N, AM))
1021 return false;
1022
Evan Cheng25ab6902006-09-08 06:48:29 +00001023 MVT::ValueType VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001024 unsigned Complexity = 0;
1025 if (AM.BaseType == X86ISelAddressMode::RegBase)
1026 if (AM.Base.Reg.Val)
1027 Complexity = 1;
1028 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001029 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001030 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1031 Complexity = 4;
1032
1033 if (AM.IndexReg.Val)
1034 Complexity++;
1035 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001036 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001037
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001038 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1039 // a simple shift.
1040 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001041 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001042
1043 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1044 // to a LEA. This is determined with some expermentation but is by no means
1045 // optimal (especially for code size consideration). LEA is nice because of
1046 // its three-address nature. Tweak the cost function again when we can run
1047 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +00001048 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
1049 // For X86-64, we should always use lea to materialize RIP relative
1050 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001051 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001052 Complexity = 4;
1053 else
1054 Complexity += 2;
1055 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001056
1057 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
1058 Complexity++;
1059
1060 if (Complexity > 2) {
1061 getAddressOperands(AM, Base, Scale, Index, Disp);
1062 return true;
1063 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001064 return false;
1065}
1066
Evan Cheng5e351682006-02-06 06:02:33 +00001067bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
1068 SDOperand &Base, SDOperand &Scale,
1069 SDOperand &Index, SDOperand &Disp) {
Evan Cheng466685d2006-10-09 20:57:25 +00001070 if (ISD::isNON_EXTLoad(N.Val) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001071 N.hasOneUse() &&
Evan Cheng27e1fe92006-10-14 08:33:25 +00001072 CanBeFoldedBy(N.Val, P.Val, P.Val))
Evan Cheng0d538262006-11-08 20:34:28 +00001073 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
Evan Cheng0114e942006-01-06 20:36:21 +00001074 return false;
1075}
1076
Evan Cheng7ccced62006-02-18 00:15:05 +00001077/// getGlobalBaseReg - Output the instructions required to put the
1078/// base address to use for accessing globals into a register.
1079///
Evan Cheng9ade2182006-08-26 05:34:46 +00001080SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng25ab6902006-09-08 06:48:29 +00001081 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Evan Cheng7ccced62006-02-18 00:15:05 +00001082 if (!GlobalBaseReg) {
1083 // Insert the set of GlobalBaseReg into the first MBB of the function
Evan Cheng0475ab52008-01-05 00:41:47 +00001084 MachineFunction *MF = BB->getParent();
1085 MachineBasicBlock &FirstMBB = MF->front();
Evan Cheng7ccced62006-02-18 00:15:05 +00001086 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Evan Cheng0475ab52008-01-05 00:41:47 +00001087 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Chris Lattner84bc5422007-12-31 04:13:23 +00001088 unsigned PC = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001089
Evan Chengc0f64ff2006-11-27 23:37:22 +00001090 const TargetInstrInfo *TII = TM.getInstrInfo();
Evan Chengf02ca692007-12-22 02:26:46 +00001091 // Operand of MovePCtoStack is completely ignored by asm printer. It's
1092 // only used in JIT code emission as displacement to pc.
Evan Cheng0475ab52008-01-05 00:41:47 +00001093 BuildMI(FirstMBB, MBBI, TII->get(X86::MOVPC32r), PC).addImm(0);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001094
1095 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
1096 // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
Evan Cheng706535d2007-01-22 21:34:25 +00001097 if (TM.getRelocationModel() == Reloc::PIC_ &&
1098 Subtarget->isPICStyleGOT()) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001099 GlobalBaseReg = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng0475ab52008-01-05 00:41:47 +00001100 BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg)
1101 .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001102 } else {
1103 GlobalBaseReg = PC;
1104 }
1105
Evan Cheng7ccced62006-02-18 00:15:05 +00001106 }
Evan Cheng25ab6902006-09-08 06:48:29 +00001107 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
Evan Cheng7ccced62006-02-18 00:15:05 +00001108}
1109
Evan Chengb245d922006-05-20 01:36:52 +00001110static SDNode *FindCallStartFromCall(SDNode *Node) {
1111 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1112 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1113 "Node doesn't have a token chain argument!");
1114 return FindCallStartFromCall(Node->getOperand(0).Val);
1115}
1116
Christopher Lambc59e5212007-08-10 21:48:46 +00001117SDNode *X86DAGToDAGISel::getTruncate(SDOperand N0, MVT::ValueType VT) {
1118 SDOperand SRIdx;
1119 switch (VT) {
1120 case MVT::i8:
1121 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1122 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1123 if (!Subtarget->is64Bit()) {
1124 unsigned Opc;
1125 MVT::ValueType VT;
1126 switch (N0.getValueType()) {
1127 default: assert(0 && "Unknown truncate!");
1128 case MVT::i16:
1129 Opc = X86::MOV16to16_;
1130 VT = MVT::i16;
1131 break;
1132 case MVT::i32:
1133 Opc = X86::MOV32to32_;
1134 VT = MVT::i32;
1135 break;
1136 }
Evan Cheng96aaa542007-10-12 07:55:53 +00001137 N0 = SDOperand(CurDAG->getTargetNode(Opc, VT, MVT::Flag, N0), 0);
1138 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1139 VT, N0, SRIdx, N0.getValue(1));
Christopher Lambc59e5212007-08-10 21:48:46 +00001140 }
1141 break;
1142 case MVT::i16:
1143 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1144 break;
1145 case MVT::i32:
1146 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1147 break;
Evan Cheng96aaa542007-10-12 07:55:53 +00001148 default: assert(0 && "Unknown truncate!"); break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001149 }
Evan Cheng96aaa542007-10-12 07:55:53 +00001150 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, VT, N0, SRIdx);
Christopher Lambc59e5212007-08-10 21:48:46 +00001151}
1152
1153
Evan Cheng9ade2182006-08-26 05:34:46 +00001154SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Chengdef941b2005-12-15 01:02:48 +00001155 SDNode *Node = N.Val;
1156 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001157 unsigned Opc, MOpc;
1158 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +00001159
Evan Chengf597dc72006-02-10 22:24:32 +00001160#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001161 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001162 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001163 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001164 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001165#endif
1166
Evan Cheng34167212006-02-09 00:37:58 +00001167 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengf597dc72006-02-10 22:24:32 +00001168#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001169 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001170 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001171 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001172 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001173#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001174 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001175 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001176
Evan Cheng0114e942006-01-06 20:36:21 +00001177 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001178 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001179 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001180 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001181
Chris Lattner447ff682008-03-11 03:23:40 +00001182 // FIXME: This is a workaround for a tblgen problem: rdar://5791600
1183 case X86ISD::RET_FLAG:
1184 if (ConstantSDNode *Amt = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1185 if (Amt->getSignExtended() != 0) break;
1186
1187 // Match (X86retflag 0).
1188 SDOperand Chain = N.getOperand(0);
1189 bool HasInFlag = N.getOperand(N.getNumOperands()-1).getValueType()
1190 == MVT::Flag;
1191 SmallVector<SDOperand, 8> Ops0;
1192 AddToISelQueue(Chain);
1193 SDOperand InFlag(0, 0);
1194 if (HasInFlag) {
1195 InFlag = N.getOperand(N.getNumOperands()-1);
1196 AddToISelQueue(InFlag);
1197 }
1198 for (unsigned i = 2, e = N.getNumOperands()-(HasInFlag?1:0); i != e;
1199 ++i) {
1200 AddToISelQueue(N.getOperand(i));
1201 Ops0.push_back(N.getOperand(i));
1202 }
1203 Ops0.push_back(Chain);
1204 if (HasInFlag)
1205 Ops0.push_back(InFlag);
1206 return CurDAG->getTargetNode(X86::RET, MVT::Other,
1207 &Ops0[0], Ops0.size());
1208 }
1209 break;
1210
Evan Cheng51a9ed92006-02-25 10:09:08 +00001211 case ISD::ADD: {
1212 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1213 // code and is matched first so to prevent it from being turned into
1214 // LEA32r X+c.
Evan Chengb1a9aec2008-01-08 02:06:11 +00001215 // In 64-bit small code size mode, use LEA to take advantage of
1216 // RIP-relative addressing.
1217 if (TM.getCodeModel() != CodeModel::Small)
1218 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001219 MVT::ValueType PtrVT = TLI.getPointerTy();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001220 SDOperand N0 = N.getOperand(0);
1221 SDOperand N1 = N.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00001222 if (N.Val->getValueType(0) == PtrVT &&
Evan Cheng19f2ffc2006-12-05 04:01:03 +00001223 N0.getOpcode() == X86ISD::Wrapper &&
Evan Cheng51a9ed92006-02-25 10:09:08 +00001224 N1.getOpcode() == ISD::Constant) {
1225 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1226 SDOperand C(0, 0);
1227 // TODO: handle ExternalSymbolSDNode.
1228 if (GlobalAddressSDNode *G =
1229 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001230 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001231 G->getOffset() + Offset);
1232 } else if (ConstantPoolSDNode *CP =
1233 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Chengc356a572006-09-12 21:04:05 +00001234 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001235 CP->getAlignment(),
1236 CP->getOffset()+Offset);
1237 }
1238
Evan Cheng25ab6902006-09-08 06:48:29 +00001239 if (C.Val) {
1240 if (Subtarget->is64Bit()) {
1241 SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
1242 CurDAG->getRegister(0, PtrVT), C };
1243 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1244 } else
1245 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1246 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001247 }
1248
1249 // Other cases are handled by auto-generated code.
1250 break;
Evan Chenga0ea0532006-02-23 02:43:52 +00001251 }
Evan Cheng020d2e82006-02-23 20:41:18 +00001252
Dan Gohman525178c2007-10-08 18:33:35 +00001253 case ISD::SMUL_LOHI:
1254 case ISD::UMUL_LOHI: {
1255 SDOperand N0 = Node->getOperand(0);
1256 SDOperand N1 = Node->getOperand(1);
1257
Dan Gohman525178c2007-10-08 18:33:35 +00001258 bool isSigned = Opcode == ISD::SMUL_LOHI;
1259 if (!isSigned)
Evan Cheng0114e942006-01-06 20:36:21 +00001260 switch (NVT) {
1261 default: assert(0 && "Unsupported VT!");
1262 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1263 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1264 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001265 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001266 }
1267 else
1268 switch (NVT) {
1269 default: assert(0 && "Unsupported VT!");
1270 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1271 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1272 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001273 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001274 }
1275
1276 unsigned LoReg, HiReg;
1277 switch (NVT) {
1278 default: assert(0 && "Unsupported VT!");
1279 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1280 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1281 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001282 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001283 }
1284
Evan Cheng0114e942006-01-06 20:36:21 +00001285 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng7afa1662007-08-02 05:48:35 +00001286 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman525178c2007-10-08 18:33:35 +00001287 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001288 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +00001289 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng7afa1662007-08-02 05:48:35 +00001290 if (foldedLoad)
1291 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001292 }
1293
Evan Cheng04699902006-08-26 01:05:16 +00001294 AddToISelQueue(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001295 SDOperand InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1296 N0, SDOperand()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001297
1298 if (foldedLoad) {
Dan Gohman525178c2007-10-08 18:33:35 +00001299 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001300 AddToISelQueue(Tmp0);
1301 AddToISelQueue(Tmp1);
1302 AddToISelQueue(Tmp2);
1303 AddToISelQueue(Tmp3);
Dan Gohman525178c2007-10-08 18:33:35 +00001304 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001305 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001306 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001307 InFlag = SDOperand(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001308 // Update the chain.
1309 ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001310 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001311 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001312 InFlag =
1313 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001314 }
1315
Dan Gohman525178c2007-10-08 18:33:35 +00001316 // Copy the low half of the result, if it is needed.
1317 if (!N.getValue(0).use_empty()) {
1318 SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1319 LoReg, NVT, InFlag);
1320 InFlag = Result.getValue(2);
1321 ReplaceUses(N.getValue(0), Result);
1322#ifndef NDEBUG
1323 DOUT << std::string(Indent-2, ' ') << "=> ";
1324 DEBUG(Result.Val->dump(CurDAG));
1325 DOUT << "\n";
1326#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001327 }
Dan Gohman525178c2007-10-08 18:33:35 +00001328 // Copy the high half of the result, if it is needed.
1329 if (!N.getValue(1).use_empty()) {
1330 SDOperand Result;
1331 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1332 // Prevent use of AH in a REX instruction by referencing AX instead.
1333 // Shift it down 8 bits.
1334 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1335 X86::AX, MVT::i16, InFlag);
1336 InFlag = Result.getValue(2);
1337 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1338 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1339 // Then truncate it down to i8.
1340 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1341 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1342 MVT::i8, Result, SRIdx), 0);
1343 } else {
1344 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1345 HiReg, NVT, InFlag);
1346 InFlag = Result.getValue(2);
1347 }
1348 ReplaceUses(N.getValue(1), Result);
1349#ifndef NDEBUG
1350 DOUT << std::string(Indent-2, ' ') << "=> ";
1351 DEBUG(Result.Val->dump(CurDAG));
1352 DOUT << "\n";
1353#endif
1354 }
Evan Cheng34167212006-02-09 00:37:58 +00001355
Evan Chengf597dc72006-02-10 22:24:32 +00001356#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001357 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001358#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001359
Evan Cheng64a752f2006-08-11 09:08:15 +00001360 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001361 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001362
Dan Gohman525178c2007-10-08 18:33:35 +00001363 case ISD::SDIVREM:
1364 case ISD::UDIVREM: {
1365 SDOperand N0 = Node->getOperand(0);
1366 SDOperand N1 = Node->getOperand(1);
1367
1368 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001369 if (!isSigned)
1370 switch (NVT) {
1371 default: assert(0 && "Unsupported VT!");
1372 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1373 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1374 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001375 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001376 }
1377 else
1378 switch (NVT) {
1379 default: assert(0 && "Unsupported VT!");
1380 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1381 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1382 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001383 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001384 }
1385
1386 unsigned LoReg, HiReg;
1387 unsigned ClrOpcode, SExtOpcode;
1388 switch (NVT) {
1389 default: assert(0 && "Unsupported VT!");
1390 case MVT::i8:
1391 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001392 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001393 SExtOpcode = X86::CBW;
1394 break;
1395 case MVT::i16:
1396 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001397 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001398 SExtOpcode = X86::CWD;
1399 break;
1400 case MVT::i32:
1401 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001402 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001403 SExtOpcode = X86::CDQ;
1404 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001405 case MVT::i64:
1406 LoReg = X86::RAX; HiReg = X86::RDX;
1407 ClrOpcode = X86::MOV64r0;
1408 SExtOpcode = X86::CQO;
1409 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001410 }
1411
Dan Gohman525178c2007-10-08 18:33:35 +00001412 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
1413 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1414
1415 SDOperand InFlag;
Evan Chengb1409ce2006-11-17 22:10:14 +00001416 if (NVT == MVT::i8 && !isSigned) {
1417 // Special case for div8, just use a move with zero extension to AX to
1418 // clear the upper 8 bits (AH).
1419 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
1420 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
1421 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
1422 AddToISelQueue(N0.getOperand(0));
1423 AddToISelQueue(Tmp0);
1424 AddToISelQueue(Tmp1);
1425 AddToISelQueue(Tmp2);
1426 AddToISelQueue(Tmp3);
1427 Move =
1428 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
1429 Ops, 5), 0);
1430 Chain = Move.getValue(1);
1431 ReplaceUses(N0.getValue(1), Chain);
1432 } else {
1433 AddToISelQueue(N0);
1434 Move =
1435 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
1436 Chain = CurDAG->getEntryNode();
1437 }
Dan Gohman525178c2007-10-08 18:33:35 +00001438 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDOperand());
Evan Cheng948f3432006-01-06 23:19:29 +00001439 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001440 } else {
1441 AddToISelQueue(N0);
1442 InFlag =
Dan Gohman525178c2007-10-08 18:33:35 +00001443 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
1444 LoReg, N0, SDOperand()).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001445 if (isSigned) {
1446 // Sign extend the low part into the high part.
1447 InFlag =
1448 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
1449 } else {
1450 // Zero out the high part, effectively zero extending the input.
1451 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001452 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1453 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001454 }
Evan Cheng948f3432006-01-06 23:19:29 +00001455 }
1456
1457 if (foldedLoad) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001458 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001459 AddToISelQueue(Tmp0);
1460 AddToISelQueue(Tmp1);
1461 AddToISelQueue(Tmp2);
1462 AddToISelQueue(Tmp3);
Evan Chengb1409ce2006-11-17 22:10:14 +00001463 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001464 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001465 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001466 InFlag = SDOperand(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001467 // Update the chain.
1468 ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001469 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001470 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001471 InFlag =
1472 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001473 }
1474
Dan Gohmana37c9f72007-09-25 18:23:27 +00001475 // Copy the division (low) result, if it is needed.
1476 if (!N.getValue(0).use_empty()) {
Dan Gohman525178c2007-10-08 18:33:35 +00001477 SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1478 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001479 InFlag = Result.getValue(2);
1480 ReplaceUses(N.getValue(0), Result);
1481#ifndef NDEBUG
1482 DOUT << std::string(Indent-2, ' ') << "=> ";
1483 DEBUG(Result.Val->dump(CurDAG));
1484 DOUT << "\n";
1485#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001486 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001487 // Copy the remainder (high) result, if it is needed.
1488 if (!N.getValue(1).use_empty()) {
1489 SDOperand Result;
1490 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1491 // Prevent use of AH in a REX instruction by referencing AX instead.
1492 // Shift it down 8 bits.
Dan Gohman525178c2007-10-08 18:33:35 +00001493 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1494 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001495 InFlag = Result.getValue(2);
1496 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1497 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1498 // Then truncate it down to i8.
1499 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1500 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1501 MVT::i8, Result, SRIdx), 0);
1502 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00001503 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1504 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001505 InFlag = Result.getValue(2);
1506 }
1507 ReplaceUses(N.getValue(1), Result);
1508#ifndef NDEBUG
1509 DOUT << std::string(Indent-2, ' ') << "=> ";
1510 DEBUG(Result.Val->dump(CurDAG));
1511 DOUT << "\n";
1512#endif
1513 }
Evan Chengf597dc72006-02-10 22:24:32 +00001514
1515#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001516 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001517#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001518
1519 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001520 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001521
1522 case ISD::ANY_EXTEND: {
Christopher Lambc9298232008-03-16 03:12:01 +00001523 // Check if the type extended to supports subregs.
1524 if (NVT == MVT::i8)
1525 break;
1526
Christopher Lamba1eb1552007-08-10 22:22:41 +00001527 SDOperand N0 = Node->getOperand(0);
Christopher Lambc9298232008-03-16 03:12:01 +00001528 // Get the subregsiter index for the type to extend.
1529 MVT::ValueType N0VT = N0.getValueType();
1530 unsigned Idx = (N0VT == MVT::i32) ? X86::SUBREG_32BIT :
1531 (N0VT == MVT::i16) ? X86::SUBREG_16BIT :
1532 (Subtarget->is64Bit()) ? X86::SUBREG_8BIT : 0;
1533
1534 // If we don't have a subreg Idx, let generated ISel have a try.
1535 if (Idx == 0)
1536 break;
1537
1538 // If we have an index, generate an insert_subreg into undef.
Christopher Lamba1eb1552007-08-10 22:22:41 +00001539 AddToISelQueue(N0);
Christopher Lambc9298232008-03-16 03:12:01 +00001540 SDOperand Undef =
Evan Cheng90ce87b2008-04-03 07:45:18 +00001541 SDOperand(CurDAG->getTargetNode(X86::IMPLICIT_DEF, NVT), 0);
Christopher Lambc9298232008-03-16 03:12:01 +00001542 SDOperand SRIdx = CurDAG->getTargetConstant(Idx, MVT::i32);
1543 SDNode *ResNode = CurDAG->getTargetNode(X86::INSERT_SUBREG,
Evan Cheng90ce87b2008-04-03 07:45:18 +00001544 NVT, Undef, N0, SRIdx);
Christopher Lamba1eb1552007-08-10 22:22:41 +00001545
1546#ifndef NDEBUG
Christopher Lambc9298232008-03-16 03:12:01 +00001547 DOUT << std::string(Indent-2, ' ') << "=> ";
1548 DEBUG(ResNode->dump(CurDAG));
1549 DOUT << "\n";
1550 Indent -= 2;
Christopher Lamba1eb1552007-08-10 22:22:41 +00001551#endif
Christopher Lambc9298232008-03-16 03:12:01 +00001552 return ResNode;
Christopher Lamba1eb1552007-08-10 22:22:41 +00001553 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001554
1555 case ISD::SIGN_EXTEND_INREG: {
1556 SDOperand N0 = Node->getOperand(0);
1557 AddToISelQueue(N0);
Evan Cheng403be7e2006-05-08 08:01:26 +00001558
Christopher Lambc59e5212007-08-10 21:48:46 +00001559 MVT::ValueType SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
1560 SDOperand TruncOp = SDOperand(getTruncate(N0, SVT), 0);
Bill Wendling0d642872007-11-01 08:51:44 +00001561 unsigned Opc = 0;
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001562 switch (NVT) {
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001563 case MVT::i16:
Christopher Lambc59e5212007-08-10 21:48:46 +00001564 if (SVT == MVT::i8) Opc = X86::MOVSX16rr8;
1565 else assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001566 break;
1567 case MVT::i32:
Christopher Lambc59e5212007-08-10 21:48:46 +00001568 switch (SVT) {
1569 case MVT::i8: Opc = X86::MOVSX32rr8; break;
1570 case MVT::i16: Opc = X86::MOVSX32rr16; break;
1571 default: assert(0 && "Unknown sign_extend_inreg!");
1572 }
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001573 break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001574 case MVT::i64:
1575 switch (SVT) {
1576 case MVT::i8: Opc = X86::MOVSX64rr8; break;
1577 case MVT::i16: Opc = X86::MOVSX64rr16; break;
1578 case MVT::i32: Opc = X86::MOVSX64rr32; break;
1579 default: assert(0 && "Unknown sign_extend_inreg!");
1580 }
1581 break;
1582 default: assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001583 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001584
1585 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
1586
1587#ifndef NDEBUG
1588 DOUT << std::string(Indent-2, ' ') << "=> ";
1589 DEBUG(TruncOp.Val->dump(CurDAG));
1590 DOUT << "\n";
1591 DOUT << std::string(Indent-2, ' ') << "=> ";
1592 DEBUG(ResNode->dump(CurDAG));
1593 DOUT << "\n";
1594 Indent -= 2;
1595#endif
1596 return ResNode;
1597 break;
1598 }
1599
1600 case ISD::TRUNCATE: {
1601 SDOperand Input = Node->getOperand(0);
1602 AddToISelQueue(Node->getOperand(0));
1603 SDNode *ResNode = getTruncate(Input, NVT);
1604
Evan Cheng403be7e2006-05-08 08:01:26 +00001605#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001606 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001607 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001608 DOUT << "\n";
Evan Cheng403be7e2006-05-08 08:01:26 +00001609 Indent -= 2;
1610#endif
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001611 return ResNode;
Evan Cheng6b2e2542006-05-20 07:44:28 +00001612 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001613 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001614 }
1615
Evan Cheng9ade2182006-08-26 05:34:46 +00001616 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001617
Evan Chengf597dc72006-02-10 22:24:32 +00001618#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001619 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001620 if (ResNode == NULL || ResNode == N.Val)
1621 DEBUG(N.Val->dump(CurDAG));
1622 else
1623 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001624 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001625 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001626#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001627
1628 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001629}
1630
Chris Lattnerc0bad572006-06-08 18:03:49 +00001631bool X86DAGToDAGISel::
1632SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1633 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1634 SDOperand Op0, Op1, Op2, Op3;
1635 switch (ConstraintCode) {
1636 case 'o': // offsetable ??
1637 case 'v': // not offsetable ??
1638 default: return true;
1639 case 'm': // memory
Evan Cheng0d538262006-11-08 20:34:28 +00001640 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001641 return true;
1642 break;
1643 }
1644
Evan Cheng04699902006-08-26 01:05:16 +00001645 OutOps.push_back(Op0);
1646 OutOps.push_back(Op1);
1647 OutOps.push_back(Op2);
1648 OutOps.push_back(Op3);
1649 AddToISelQueue(Op0);
1650 AddToISelQueue(Op1);
1651 AddToISelQueue(Op2);
1652 AddToISelQueue(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001653 return false;
1654}
1655
Chris Lattnerc961eea2005-11-16 01:54:32 +00001656/// createX86ISelDag - This pass converts a legalized DAG into a
1657/// X86-specific DAG, ready for instruction scheduling.
1658///
Evan Chenge50794a2006-08-29 18:28:33 +00001659FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1660 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001661}