blob: 786e92030abb1ac913013ad051c6190355558a22 [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"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000035#include "llvm/Support/Compiler.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000036#include "llvm/Support/Debug.h"
37#include "llvm/Support/MathExtras.h"
Evan Chengcdda25d2008-04-25 08:22:20 +000038#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000039#include "llvm/ADT/Statistic.h"
Evan Cheng2ef88a02006-08-07 22:28:20 +000040#include <queue>
Evan Chengba2f0a92006-02-05 06:46:41 +000041#include <set>
Chris Lattnerc961eea2005-11-16 01:54:32 +000042using namespace llvm;
43
Chris Lattner95b2c7d2006-12-19 22:59:26 +000044STATISTIC(NumFPKill , "Number of FP_REG_KILL instructions added");
45STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
46
Chris Lattnerc961eea2005-11-16 01:54:32 +000047//===----------------------------------------------------------------------===//
48// Pattern Matcher Implementation
49//===----------------------------------------------------------------------===//
50
51namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000052 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
53 /// SDOperand's instead of register numbers for the leaves of the matched
54 /// tree.
55 struct X86ISelAddressMode {
56 enum {
57 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000058 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000059 } BaseType;
60
61 struct { // This is really a union, discriminated by BaseType!
62 SDOperand Reg;
63 int FrameIndex;
64 } Base;
65
Evan Chengbe3bf422008-02-07 08:53:49 +000066 bool isRIPRel; // RIP as base?
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000067 unsigned Scale;
68 SDOperand IndexReg;
69 unsigned Disp;
70 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000071 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000072 const char *ES;
73 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000074 unsigned Align; // CP alignment.
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000075
76 X86ISelAddressMode()
Evan Cheng25ab6902006-09-08 06:48:29 +000077 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
78 GV(0), CP(0), ES(0), JT(-1), Align(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000079 }
80 };
81}
82
83namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +000084 //===--------------------------------------------------------------------===//
85 /// ISel - X86 specific code to select X86 machine instructions for
86 /// SelectionDAG operations.
87 ///
Chris Lattner2c79de82006-06-28 23:27:49 +000088 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +000089 /// ContainsFPCode - Every instruction we select that uses or defines a FP
90 /// register should set this to true.
91 bool ContainsFPCode;
92
Evan Cheng25ab6902006-09-08 06:48:29 +000093 /// TM - Keep a reference to X86TargetMachine.
94 ///
95 X86TargetMachine &TM;
96
Chris Lattnerc961eea2005-11-16 01:54:32 +000097 /// X86Lowering - This object fully describes how to lower LLVM code to an
98 /// X86-specific SelectionDAG.
99 X86TargetLowering X86Lowering;
100
101 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
102 /// make the right decision when generating code for different targets.
103 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000104
Evan Cheng25ab6902006-09-08 06:48:29 +0000105 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
106 /// base register.
Evan Cheng7ccced62006-02-18 00:15:05 +0000107 unsigned GlobalBaseReg;
Evan Chenga8df1b42006-07-27 16:44:36 +0000108
Evan Chengdb8d56b2008-06-30 20:45:06 +0000109 /// CurBB - Current BB being isel'd.
110 ///
111 MachineBasicBlock *CurBB;
112
Chris Lattnerc961eea2005-11-16 01:54:32 +0000113 public:
Evan Cheng25ab6902006-09-08 06:48:29 +0000114 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Evan Cheng4576f6d2008-07-01 18:05:03 +0000115 : SelectionDAGISel(X86Lowering, fast),
116 ContainsFPCode(false), TM(tm),
Evan Chenga8df1b42006-07-27 16:44:36 +0000117 X86Lowering(*TM.getTargetLowering()),
Evan Chengf4b4c412006-08-08 00:31:00 +0000118 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000119
Evan Cheng7ccced62006-02-18 00:15:05 +0000120 virtual bool runOnFunction(Function &Fn) {
121 // Make sure we re-emit a set of the global base reg if necessary
122 GlobalBaseReg = 0;
123 return SelectionDAGISel::runOnFunction(Fn);
124 }
125
Chris Lattnerc961eea2005-11-16 01:54:32 +0000126 virtual const char *getPassName() const {
127 return "X86 DAG->DAG Instruction Selection";
128 }
129
Evan Chengdb8d56b2008-06-30 20:45:06 +0000130 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000131 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Evan Chengdb8d56b2008-06-30 20:45:06 +0000132 virtual void InstructionSelect(SelectionDAG &DAG);
133
134 /// InstructionSelectPostProcessing - Post processing of selected and
135 /// scheduled basic blocks.
136 virtual void InstructionSelectPostProcessing(SelectionDAG &DAG);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000137
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000138 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
139
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000140 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000141
Chris Lattnerc961eea2005-11-16 01:54:32 +0000142// Include the pieces autogenerated from the target description.
143#include "X86GenDAGISel.inc"
144
145 private:
Evan Cheng9ade2182006-08-26 05:34:46 +0000146 SDNode *Select(SDOperand N);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000147
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000148 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM,
Anton Korobeynikovf6e93532007-03-28 18:38:33 +0000149 bool isRoot = true, unsigned Depth = 0);
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000150 bool MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
151 bool isRoot, unsigned Depth);
Evan Cheng0d538262006-11-08 20:34:28 +0000152 bool SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
153 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
154 bool SelectLEAAddr(SDOperand Op, SDOperand N, SDOperand &Base,
155 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
156 bool SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Cheng07e4b002006-10-16 06:34:55 +0000157 SDOperand N, SDOperand &Base, SDOperand &Scale,
Evan Cheng82a91642006-10-11 21:06:01 +0000158 SDOperand &Index, SDOperand &Disp,
159 SDOperand &InChain, SDOperand &OutChain);
Evan Cheng5e351682006-02-06 06:02:33 +0000160 bool TryFoldLoad(SDOperand P, SDOperand N,
161 SDOperand &Base, SDOperand &Scale,
Evan Cheng0114e942006-01-06 20:36:21 +0000162 SDOperand &Index, SDOperand &Disp);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000163 void PreprocessForRMW(SelectionDAG &DAG);
164 void PreprocessForFPConvert(SelectionDAG &DAG);
Evan Cheng2ef88a02006-08-07 22:28:20 +0000165
Chris Lattnerc0bad572006-06-08 18:03:49 +0000166 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
167 /// inline asm expressions.
168 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
169 char ConstraintCode,
170 std::vector<SDOperand> &OutOps,
171 SelectionDAG &DAG);
172
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000173 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
174
Evan Chenge5280532005-12-12 21:49:40 +0000175 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
176 SDOperand &Scale, SDOperand &Index,
177 SDOperand &Disp) {
178 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000179 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
180 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000181 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000182 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000183 // These are 32-bit even in 64-bit mode since RIP relative offset
184 // is 32-bit.
185 if (AM.GV)
186 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
187 else if (AM.CP)
188 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp);
189 else if (AM.ES)
190 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
191 else if (AM.JT != -1)
192 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
193 else
194 Disp = getI32Imm(AM.Disp);
Evan Chenge5280532005-12-12 21:49:40 +0000195 }
196
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000197 /// getI8Imm - Return a target constant with the specified value, of type
198 /// i8.
199 inline SDOperand getI8Imm(unsigned Imm) {
200 return CurDAG->getTargetConstant(Imm, MVT::i8);
201 }
202
Chris Lattnerc961eea2005-11-16 01:54:32 +0000203 /// getI16Imm - Return a target constant with the specified value, of type
204 /// i16.
205 inline SDOperand getI16Imm(unsigned Imm) {
206 return CurDAG->getTargetConstant(Imm, MVT::i16);
207 }
208
209 /// getI32Imm - Return a target constant with the specified value, of type
210 /// i32.
211 inline SDOperand getI32Imm(unsigned Imm) {
212 return CurDAG->getTargetConstant(Imm, MVT::i32);
213 }
Evan Chengf597dc72006-02-10 22:24:32 +0000214
Evan Cheng7ccced62006-02-18 00:15:05 +0000215 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
216 /// base register. Return the virtual register that holds this value.
Evan Cheng9ade2182006-08-26 05:34:46 +0000217 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000218
Christopher Lambc59e5212007-08-10 21:48:46 +0000219 /// getTruncate - return an SDNode that implements a subreg based truncate
220 /// of the specified operand to the the specified value type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000221 SDNode *getTruncate(SDOperand N0, MVT VT);
Christopher Lambc59e5212007-08-10 21:48:46 +0000222
Evan Cheng23addc02006-02-10 22:46:26 +0000223#ifndef NDEBUG
224 unsigned Indent;
225#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000226 };
227}
228
Evan Chengcdda25d2008-04-25 08:22:20 +0000229/// findFlagUse - Return use of MVT::Flag value produced by the specified SDNode.
230///
Evan Chenga275ecb2006-10-10 01:46:56 +0000231static SDNode *findFlagUse(SDNode *N) {
232 unsigned FlagResNo = N->getNumValues()-1;
233 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +0000234 SDNode *User = I->getUser();
Evan Chenga275ecb2006-10-10 01:46:56 +0000235 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
236 SDOperand Op = User->getOperand(i);
Evan Cheng494cec62006-10-12 19:13:59 +0000237 if (Op.Val == N && Op.ResNo == FlagResNo)
Evan Chenga275ecb2006-10-10 01:46:56 +0000238 return User;
239 }
240 }
241 return NULL;
242}
243
Evan Chengcdda25d2008-04-25 08:22:20 +0000244/// findNonImmUse - Return true by reference in "found" if "Use" is an
245/// non-immediate use of "Def". This function recursively traversing
246/// up the operand chain ignoring certain nodes.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000247static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
248 SDNode *Root, SDNode *Skip, bool &found,
Evan Chengcdda25d2008-04-25 08:22:20 +0000249 SmallPtrSet<SDNode*, 16> &Visited) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000250 if (found ||
251 Use->getNodeId() > Def->getNodeId() ||
Evan Chengcdda25d2008-04-25 08:22:20 +0000252 !Visited.insert(Use))
Evan Chengf4b4c412006-08-08 00:31:00 +0000253 return;
Evan Chengcdda25d2008-04-25 08:22:20 +0000254
Evan Cheng27e1fe92006-10-14 08:33:25 +0000255 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000256 SDNode *N = Use->getOperand(i).Val;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000257 if (N == Skip)
Evan Chenga275ecb2006-10-10 01:46:56 +0000258 continue;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000259 if (N == Def) {
260 if (Use == ImmedUse)
Evan Cheng419ace92008-04-25 08:55:28 +0000261 continue; // We are not looking for immediate use.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000262 if (Use == Root) {
Evan Cheng419ace92008-04-25 08:55:28 +0000263 // Must be a chain reading node where it is possible to reach its own
264 // chain operand through a path started from another operand.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000265 assert(Use->getOpcode() == ISD::STORE ||
Chris Lattner25453ea2008-04-25 05:13:01 +0000266 Use->getOpcode() == X86ISD::CMP ||
Chris Lattner25453ea2008-04-25 05:13:01 +0000267 Use->getOpcode() == ISD::INTRINSIC_W_CHAIN ||
268 Use->getOpcode() == ISD::INTRINSIC_VOID);
Evan Cheng27e1fe92006-10-14 08:33:25 +0000269 continue;
270 }
Evan Chengf4b4c412006-08-08 00:31:00 +0000271 found = true;
272 break;
273 }
Evan Chengcdda25d2008-04-25 08:22:20 +0000274
275 // Traverse up the operand chain.
Evan Cheng27e1fe92006-10-14 08:33:25 +0000276 findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000277 }
278}
279
Evan Cheng27e1fe92006-10-14 08:33:25 +0000280/// isNonImmUse - Start searching from Root up the DAG to check is Def can
281/// be reached. Return true if that's the case. However, ignore direct uses
282/// by ImmedUse (which would be U in the example illustrated in
283/// CanBeFoldedBy) and by Root (which can happen in the store case).
284/// FIXME: to be really generic, we should allow direct use by any node
285/// that is being folded. But realisticly since we only fold loads which
286/// have one non-chain use, we only need to watch out for load/op/store
287/// and load/op/cmp case where the root (store / cmp) may reach the load via
288/// its chain operand.
289static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
290 SDNode *Skip = NULL) {
Evan Chengcdda25d2008-04-25 08:22:20 +0000291 SmallPtrSet<SDNode*, 16> Visited;
Evan Chengf4b4c412006-08-08 00:31:00 +0000292 bool found = false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000293 findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000294 return found;
295}
296
297
Dan Gohmandc9b3d02007-07-24 23:00:27 +0000298bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const {
Evan Cheng27e1fe92006-10-14 08:33:25 +0000299 if (FastISel) return false;
300
Evan Chenga8df1b42006-07-27 16:44:36 +0000301 // If U use can somehow reach N through another path then U can't fold N or
302 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Cheng37e18032006-07-28 06:33:41 +0000303 // through X. If N is folded into into U, then X is both a predecessor and
Evan Chenga8df1b42006-07-27 16:44:36 +0000304 // a successor of U.
305 //
306 // [ N ]
307 // ^ ^
308 // | |
309 // / \---
310 // / [X]
311 // | ^
312 // [U]--------|
Evan Cheng27e1fe92006-10-14 08:33:25 +0000313
314 if (isNonImmUse(Root, N, U))
315 return false;
316
317 // If U produces a flag, then it gets (even more) interesting. Since it
318 // would have been "glued" together with its flag use, we need to check if
319 // it might reach N:
320 //
321 // [ N ]
322 // ^ ^
323 // | |
324 // [U] \--
325 // ^ [TF]
326 // | ^
327 // | |
328 // \ /
329 // [FU]
330 //
331 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
332 // NU), then TF is a predecessor of FU and a successor of NU. But since
333 // NU and FU are flagged together, this effectively creates a cycle.
334 bool HasFlagUse = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000335 MVT VT = Root->getValueType(Root->getNumValues()-1);
Evan Cheng27e1fe92006-10-14 08:33:25 +0000336 while ((VT == MVT::Flag && !Root->use_empty())) {
337 SDNode *FU = findFlagUse(Root);
338 if (FU == NULL)
339 break;
340 else {
341 Root = FU;
342 HasFlagUse = true;
Evan Chenga275ecb2006-10-10 01:46:56 +0000343 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000344 VT = Root->getValueType(Root->getNumValues()-1);
Evan Chenga275ecb2006-10-10 01:46:56 +0000345 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000346
347 if (HasFlagUse)
348 return !isNonImmUse(Root, N, Root, U);
349 return true;
Evan Chenga8df1b42006-07-27 16:44:36 +0000350}
351
Evan Cheng70e674e2006-08-28 20:10:17 +0000352/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
353/// and move load below the TokenFactor. Replace store's chain operand with
354/// load's chain result.
355static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
356 SDOperand Store, SDOperand TF) {
357 std::vector<SDOperand> Ops;
358 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
359 if (Load.Val == TF.Val->getOperand(i).Val)
360 Ops.push_back(Load.Val->getOperand(0));
361 else
362 Ops.push_back(TF.Val->getOperand(i));
363 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
364 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
365 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
366 Store.getOperand(2), Store.getOperand(3));
367}
368
Evan Chengcd0baf22008-05-23 21:23:16 +0000369/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG.
370///
371static bool isRMWLoad(SDOperand N, SDOperand Chain, SDOperand Address,
372 SDOperand &Load) {
373 if (N.getOpcode() == ISD::BIT_CONVERT)
374 N = N.getOperand(0);
375
376 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
377 if (!LD || LD->isVolatile())
378 return false;
379 if (LD->getAddressingMode() != ISD::UNINDEXED)
380 return false;
381
382 ISD::LoadExtType ExtType = LD->getExtensionType();
383 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
384 return false;
385
386 if (N.hasOneUse() &&
387 N.getOperand(1) == Address &&
388 N.Val->isOperandOf(Chain.Val)) {
389 Load = N;
390 return true;
391 }
392 return false;
393}
394
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000395/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
396/// This is only run if not in -fast mode (aka -O0).
397/// This allows the instruction selector to pick more read-modify-write
398/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000399///
400/// [Load chain]
401/// ^
402/// |
403/// [Load]
404/// ^ ^
405/// | |
406/// / \-
407/// / |
408/// [TokenFactor] [Op]
409/// ^ ^
410/// | |
411/// \ /
412/// \ /
413/// [Store]
414///
415/// The fact the store's chain operand != load's chain will prevent the
416/// (store (op (load))) instruction from being selected. We can transform it to:
417///
418/// [Load chain]
419/// ^
420/// |
421/// [TokenFactor]
422/// ^
423/// |
424/// [Load]
425/// ^ ^
426/// | |
427/// | \-
428/// | |
429/// | [Op]
430/// | ^
431/// | |
432/// \ /
433/// \ /
434/// [Store]
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000435void X86DAGToDAGISel::PreprocessForRMW(SelectionDAG &DAG) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000436 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
437 E = DAG.allnodes_end(); I != E; ++I) {
Evan Cheng8b2794a2006-10-13 21:14:26 +0000438 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000439 continue;
440 SDOperand Chain = I->getOperand(0);
441 if (Chain.Val->getOpcode() != ISD::TokenFactor)
442 continue;
443
444 SDOperand N1 = I->getOperand(1);
445 SDOperand N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000446 if ((N1.getValueType().isFloatingPoint() &&
447 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000448 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000449 continue;
450
451 bool RModW = false;
452 SDOperand Load;
453 unsigned Opcode = N1.Val->getOpcode();
454 switch (Opcode) {
455 case ISD::ADD:
456 case ISD::MUL:
Evan Cheng70e674e2006-08-28 20:10:17 +0000457 case ISD::AND:
458 case ISD::OR:
459 case ISD::XOR:
460 case ISD::ADDC:
Evan Chengcd0baf22008-05-23 21:23:16 +0000461 case ISD::ADDE:
462 case ISD::VECTOR_SHUFFLE: {
Evan Cheng70e674e2006-08-28 20:10:17 +0000463 SDOperand N10 = N1.getOperand(0);
464 SDOperand N11 = N1.getOperand(1);
Evan Chengcd0baf22008-05-23 21:23:16 +0000465 RModW = isRMWLoad(N10, Chain, N2, Load);
466 if (!RModW)
467 RModW = isRMWLoad(N11, Chain, N2, Load);
Evan Cheng70e674e2006-08-28 20:10:17 +0000468 break;
469 }
470 case ISD::SUB:
471 case ISD::SHL:
472 case ISD::SRA:
473 case ISD::SRL:
474 case ISD::ROTL:
475 case ISD::ROTR:
476 case ISD::SUBC:
477 case ISD::SUBE:
478 case X86ISD::SHLD:
479 case X86ISD::SHRD: {
480 SDOperand N10 = N1.getOperand(0);
Evan Chengcd0baf22008-05-23 21:23:16 +0000481 RModW = isRMWLoad(N10, Chain, N2, Load);
Evan Cheng70e674e2006-08-28 20:10:17 +0000482 break;
483 }
484 }
485
Evan Cheng82a35b32006-08-29 06:44:17 +0000486 if (RModW) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000487 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000488 ++NumLoadMoved;
489 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000490 }
491}
492
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000493
494/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
495/// nodes that target the FP stack to be store and load to the stack. This is a
496/// gross hack. We would like to simply mark these as being illegal, but when
497/// we do that, legalize produces these when it expands calls, then expands
498/// these in the same legalize pass. We would like dag combine to be able to
499/// hack on these between the call expansion and the node legalization. As such
500/// this pass basically does "really late" legalization of these inline with the
501/// X86 isel pass.
502void X86DAGToDAGISel::PreprocessForFPConvert(SelectionDAG &DAG) {
503 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
504 E = DAG.allnodes_end(); I != E; ) {
505 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
506 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
507 continue;
508
509 // If the source and destination are SSE registers, then this is a legal
510 // conversion that should not be lowered.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000511 MVT SrcVT = N->getOperand(0).getValueType();
512 MVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000513 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
514 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
515 if (SrcIsSSE && DstIsSSE)
516 continue;
517
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000518 if (!SrcIsSSE && !DstIsSSE) {
519 // If this is an FPStack extension, it is a noop.
520 if (N->getOpcode() == ISD::FP_EXTEND)
521 continue;
522 // If this is a value-preserving FPStack truncation, it is a noop.
523 if (N->getConstantOperandVal(1))
524 continue;
525 }
526
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000527 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
528 // FPStack has extload and truncstore. SSE can fold direct loads into other
529 // operations. Based on this, decide what we want to do.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000530 MVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000531 if (N->getOpcode() == ISD::FP_ROUND)
532 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
533 else
534 MemVT = SrcIsSSE ? SrcVT : DstVT;
535
536 SDOperand MemTmp = DAG.CreateStackTemporary(MemVT);
537
538 // FIXME: optimize the case where the src/dest is a load or store?
539 SDOperand Store = DAG.getTruncStore(DAG.getEntryNode(), N->getOperand(0),
540 MemTmp, NULL, 0, MemVT);
541 SDOperand Result = DAG.getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp,
542 NULL, 0, MemVT);
543
544 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
545 // extload we created. This will cause general havok on the dag because
546 // anything below the conversion could be folded into other existing nodes.
547 // To avoid invalidating 'I', back it up to the convert node.
548 --I;
549 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result);
550
551 // Now that we did that, the node is dead. Increment the iterator to the
552 // next node to process, then delete N.
553 ++I;
554 DAG.DeleteNode(N);
555 }
556}
557
Chris Lattnerc961eea2005-11-16 01:54:32 +0000558/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
559/// when it has created a SelectionDAG for us to codegen.
Evan Chengdb8d56b2008-06-30 20:45:06 +0000560void X86DAGToDAGISel::InstructionSelect(SelectionDAG &DAG) {
561 CurBB = BB; // BB can change as result of isel.
Chris Lattnerc961eea2005-11-16 01:54:32 +0000562
Evan Chengdb8d56b2008-06-30 20:45:06 +0000563 DEBUG(BB->dump());
Evan Chenge50794a2006-08-29 18:28:33 +0000564 if (!FastISel)
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000565 PreprocessForRMW(DAG);
566
567 // FIXME: This should only happen when not -fast.
568 PreprocessForFPConvert(DAG);
Evan Cheng70e674e2006-08-28 20:10:17 +0000569
Chris Lattnerc961eea2005-11-16 01:54:32 +0000570 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000571#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000572 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000573 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000574#endif
Evan Chengba2f0a92006-02-05 06:46:41 +0000575 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengf597dc72006-02-10 22:24:32 +0000576#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000577 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000578#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000579
Chris Lattnerc961eea2005-11-16 01:54:32 +0000580 DAG.RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000581}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000582
Evan Chengdb8d56b2008-06-30 20:45:06 +0000583void X86DAGToDAGISel::InstructionSelectPostProcessing(SelectionDAG &DAG) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000584 // If we are emitting FP stack code, scan the basic block to determine if this
585 // block defines any FP values. If so, put an FP_REG_KILL instruction before
586 // the terminator of the block.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000587
Dale Johannesen48d1e452007-09-24 22:52:39 +0000588 // Note that FP stack instructions are used in all modes for long double,
589 // so we always need to do this check.
590 // Also note that it's possible for an FP stack register to be live across
591 // an instruction that produces multiple basic blocks (SSE CMOV) so we
592 // must check all the generated basic blocks.
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000593
594 // Scan all of the machine instructions in these MBBs, checking for FP
595 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
Evan Chengdb8d56b2008-06-30 20:45:06 +0000596 MachineFunction::iterator MBBI = CurBB;
Chris Lattner03fdec02008-03-10 23:34:12 +0000597 MachineFunction::iterator EndMBB = BB; ++EndMBB;
598 for (; MBBI != EndMBB; ++MBBI) {
599 MachineBasicBlock *MBB = MBBI;
600
601 // If this block returns, ignore it. We don't want to insert an FP_REG_KILL
602 // before the return.
603 if (!MBB->empty()) {
604 MachineBasicBlock::iterator EndI = MBB->end();
605 --EndI;
606 if (EndI->getDesc().isReturn())
607 continue;
608 }
609
Dale Johannesen48d1e452007-09-24 22:52:39 +0000610 bool ContainsFPCode = false;
Chris Lattner03fdec02008-03-10 23:34:12 +0000611 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000612 !ContainsFPCode && I != E; ++I) {
613 if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) {
614 const TargetRegisterClass *clas;
615 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
616 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
Chris Lattner03fdec02008-03-10 23:34:12 +0000617 TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
Chris Lattner84bc5422007-12-31 04:13:23 +0000618 ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) ==
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000619 X86::RFP32RegisterClass ||
620 clas == X86::RFP64RegisterClass ||
621 clas == X86::RFP80RegisterClass)) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000622 ContainsFPCode = true;
623 break;
624 }
625 }
626 }
627 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000628 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
629 // a copy of the input value in this block. In SSE mode, we only care about
630 // 80-bit values.
631 if (!ContainsFPCode) {
632 // Final check, check LLVM BB's that are successors to the LLVM BB
633 // corresponding to BB for FP PHI nodes.
634 const BasicBlock *LLVMBB = BB->getBasicBlock();
635 const PHINode *PN;
636 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
637 !ContainsFPCode && SI != E; ++SI) {
638 for (BasicBlock::const_iterator II = SI->begin();
639 (PN = dyn_cast<PHINode>(II)); ++II) {
640 if (PN->getType()==Type::X86_FP80Ty ||
641 (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) ||
642 (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) {
643 ContainsFPCode = true;
644 break;
645 }
Dale Johannesencdbe4d32007-08-07 20:29:26 +0000646 }
647 }
Chris Lattner92cb0af2006-01-11 01:15:34 +0000648 }
Dale Johannesen48d1e452007-09-24 22:52:39 +0000649 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
650 if (ContainsFPCode) {
Chris Lattner03fdec02008-03-10 23:34:12 +0000651 BuildMI(*MBB, MBBI->getFirstTerminator(),
Dale Johannesen48d1e452007-09-24 22:52:39 +0000652 TM.getInstrInfo()->get(X86::FP_REG_KILL));
653 ++NumFPKill;
654 }
Chris Lattner03fdec02008-03-10 23:34:12 +0000655 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000656}
657
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000658/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
659/// the main function.
660void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
661 MachineFrameInfo *MFI) {
662 const TargetInstrInfo *TII = TM.getInstrInfo();
663 if (Subtarget->isTargetCygMing())
664 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
665}
666
667void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
668 // If this is main, emit special code for main.
669 MachineBasicBlock *BB = MF.begin();
670 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
671 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
672}
673
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000674/// MatchAddress - Add the specified node to the specified addressing mode,
675/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000676/// addressing mode.
Evan Cheng2486af12006-02-11 02:05:36 +0000677bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000678 bool isRoot, unsigned Depth) {
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000679 // Limit recursion.
680 if (Depth > 5)
681 return MatchAddressBase(N, AM, isRoot, Depth);
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000682
Evan Cheng25ab6902006-09-08 06:48:29 +0000683 // RIP relative addressing: %rip + 32-bit displacement!
684 if (AM.isRIPRel) {
685 if (!AM.ES && AM.JT != -1 && N.getOpcode() == 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 }
692 return true;
693 }
694
Evan Cheng2ef88a02006-08-07 22:28:20 +0000695 int id = N.Val->getNodeId();
Evan Cheng1314b002007-12-13 00:43:27 +0000696 bool AlreadySelected = isSelected(id); // Already selected, not yet replaced.
Evan Cheng2486af12006-02-11 02:05:36 +0000697
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000698 switch (N.getOpcode()) {
699 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000700 case ISD::Constant: {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000701 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000702 if (isInt32(AM.Disp + Val)) {
703 AM.Disp += Val;
704 return false;
705 }
706 break;
707 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000708
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000709 case X86ISD::Wrapper: {
710 bool is64Bit = Subtarget->is64Bit();
Evan Cheng0085a282006-11-30 21:55:46 +0000711 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Chengbe3bf422008-02-07 08:53:49 +0000712 // Also, base and index reg must be 0 in order to use rip as base.
713 if (is64Bit && (TM.getCodeModel() != CodeModel::Small ||
714 AM.Base.Reg.Val || AM.IndexReg.Val))
Evan Cheng0085a282006-11-30 21:55:46 +0000715 break;
Evan Cheng28b514392006-12-05 19:50:18 +0000716 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
717 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000718 // If value is available in a register both base and index components have
719 // been picked, we can't fit the result available in the register in the
720 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Evan Cheng1314b002007-12-13 00:43:27 +0000721 if (!AlreadySelected || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
Evan Cheng28b514392006-12-05 19:50:18 +0000722 SDOperand N0 = N.getOperand(0);
723 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
724 GlobalValue *GV = G->getGlobal();
Evan Chengbe3bf422008-02-07 08:53:49 +0000725 AM.GV = GV;
726 AM.Disp += G->getOffset();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000727 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
728 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000729 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000730 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000731 AM.CP = CP->getConstVal();
732 AM.Align = CP->getAlignment();
733 AM.Disp += CP->getOffset();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000734 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
735 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000736 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000737 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000738 AM.ES = S->getSymbol();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000739 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
740 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000741 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000742 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Evan Chengbe3bf422008-02-07 08:53:49 +0000743 AM.JT = J->getIndex();
Evan Cheng9f143ce2008-02-12 19:20:46 +0000744 AM.isRIPRel = TM.getRelocationModel() != Reloc::Static &&
745 Subtarget->isPICStyleRIPRel();
Evan Chengbe3bf422008-02-07 08:53:49 +0000746 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000747 }
748 }
749 break;
Evan Cheng0085a282006-11-30 21:55:46 +0000750 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000751
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000752 case ISD::FrameIndex:
753 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
754 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
755 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
756 return false;
757 }
758 break;
Evan Chengec693f72005-12-08 02:01:35 +0000759
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000760 case ISD::SHL:
Evan Chengbe3bf422008-02-07 08:53:49 +0000761 if (AlreadySelected || AM.IndexReg.Val != 0 || AM.Scale != 1 || AM.isRIPRel)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000762 break;
763
764 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
765 unsigned Val = CN->getValue();
766 if (Val == 1 || Val == 2 || Val == 3) {
767 AM.Scale = 1 << Val;
768 SDOperand ShVal = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000769
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000770 // Okay, we know that we have a scale by now. However, if the scaled
771 // value is an add of something and a constant, we can fold the
772 // constant into the disp field here.
773 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
774 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
775 AM.IndexReg = ShVal.Val->getOperand(0);
776 ConstantSDNode *AddVal =
777 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
778 uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
779 if (isInt32(Disp))
780 AM.Disp = Disp;
781 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000782 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000783 } else {
784 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000785 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000786 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000787 }
788 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000789 }
Evan Chengec693f72005-12-08 02:01:35 +0000790
Dan Gohman83688052007-10-22 20:22:24 +0000791 case ISD::SMUL_LOHI:
792 case ISD::UMUL_LOHI:
793 // A mul_lohi where we need the low part can be folded as a plain multiply.
794 if (N.ResNo != 0) break;
795 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000796 case ISD::MUL:
797 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng1314b002007-12-13 00:43:27 +0000798 if (!AlreadySelected &&
Evan Cheng51a9ed92006-02-25 10:09:08 +0000799 AM.BaseType == X86ISelAddressMode::RegBase &&
800 AM.Base.Reg.Val == 0 &&
Evan Chengbe3bf422008-02-07 08:53:49 +0000801 AM.IndexReg.Val == 0 &&
802 !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000803 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
804 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
805 AM.Scale = unsigned(CN->getValue())-1;
806
807 SDOperand MulVal = N.Val->getOperand(0);
808 SDOperand Reg;
809
810 // Okay, we know that we have a scale by now. However, if the scaled
811 // value is an add of something and a constant, we can fold the
812 // constant into the disp field here.
813 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
814 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
815 Reg = MulVal.Val->getOperand(0);
816 ConstantSDNode *AddVal =
817 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
Evan Cheng25ab6902006-09-08 06:48:29 +0000818 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
819 if (isInt32(Disp))
820 AM.Disp = Disp;
821 else
822 Reg = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000823 } else {
824 Reg = N.Val->getOperand(0);
825 }
826
827 AM.IndexReg = AM.Base.Reg = Reg;
828 return false;
829 }
Chris Lattner62412262007-02-04 20:18:17 +0000830 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000831 break;
832
Chris Lattner62412262007-02-04 20:18:17 +0000833 case ISD::ADD:
Evan Cheng1314b002007-12-13 00:43:27 +0000834 if (!AlreadySelected) {
Evan Cheng2486af12006-02-11 02:05:36 +0000835 X86ISelAddressMode Backup = AM;
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000836 if (!MatchAddress(N.Val->getOperand(0), AM, false, Depth+1) &&
837 !MatchAddress(N.Val->getOperand(1), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000838 return false;
839 AM = Backup;
Anton Korobeynikov33bf8c42007-03-28 18:36:33 +0000840 if (!MatchAddress(N.Val->getOperand(1), AM, false, Depth+1) &&
841 !MatchAddress(N.Val->getOperand(0), AM, false, Depth+1))
Evan Cheng2486af12006-02-11 02:05:36 +0000842 return false;
843 AM = Backup;
844 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000845 break;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000846
Chris Lattner62412262007-02-04 20:18:17 +0000847 case ISD::OR:
848 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Evan Cheng1314b002007-12-13 00:43:27 +0000849 if (AlreadySelected) break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000850
851 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
852 X86ISelAddressMode Backup = AM;
853 // Start with the LHS as an addr mode.
854 if (!MatchAddress(N.getOperand(0), AM, false) &&
855 // Address could not have picked a GV address for the displacement.
856 AM.GV == NULL &&
857 // On x86-64, the resultant disp must fit in 32-bits.
858 isInt32(AM.Disp + CN->getSignExtended()) &&
859 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000860 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000861 AM.Disp += CN->getValue();
862 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000863 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000864 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000865 }
866 break;
Evan Cheng1314b002007-12-13 00:43:27 +0000867
868 case ISD::AND: {
869 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
870 // allows us to fold the shift into this addressing mode.
871 if (AlreadySelected) break;
872 SDOperand Shift = N.getOperand(0);
873 if (Shift.getOpcode() != ISD::SHL) break;
874
875 // Scale must not be used already.
876 if (AM.IndexReg.Val != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +0000877
878 // Not when RIP is used as the base.
879 if (AM.isRIPRel) break;
Evan Cheng1314b002007-12-13 00:43:27 +0000880
881 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
882 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
883 if (!C1 || !C2) break;
884
885 // Not likely to be profitable if either the AND or SHIFT node has more
886 // than one use (unless all uses are for address computation). Besides,
887 // isel mechanism requires their node ids to be reused.
888 if (!N.hasOneUse() || !Shift.hasOneUse())
889 break;
890
891 // Verify that the shift amount is something we can fold.
892 unsigned ShiftCst = C1->getValue();
893 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
894 break;
895
896 // Get the new AND mask, this folds to a constant.
897 SDOperand NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(),
898 SDOperand(C2, 0), SDOperand(C1, 0));
899 SDOperand NewAND = CurDAG->getNode(ISD::AND, N.getValueType(),
900 Shift.getOperand(0), NewANDMask);
901 NewANDMask.Val->setNodeId(Shift.Val->getNodeId());
902 NewAND.Val->setNodeId(N.Val->getNodeId());
903
904 AM.Scale = 1 << ShiftCst;
905 AM.IndexReg = NewAND;
906 return false;
907 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000908 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000909
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000910 return MatchAddressBase(N, AM, isRoot, Depth);
911}
912
913/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
914/// specified addressing mode without any further recursion.
915bool X86DAGToDAGISel::MatchAddressBase(SDOperand N, X86ISelAddressMode &AM,
916 bool isRoot, unsigned Depth) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000917 // Is the base register already occupied?
918 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
919 // If so, check to see if the scale index register is set.
Evan Chengbe3bf422008-02-07 08:53:49 +0000920 if (AM.IndexReg.Val == 0 && !AM.isRIPRel) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000921 AM.IndexReg = N;
922 AM.Scale = 1;
923 return false;
924 }
925
926 // Otherwise, we cannot select it.
927 return true;
928 }
929
930 // Default, generate it as a register.
931 AM.BaseType = X86ISelAddressMode::RegBase;
932 AM.Base.Reg = N;
933 return false;
934}
935
Evan Chengec693f72005-12-08 02:01:35 +0000936/// SelectAddr - returns true if it is able pattern match an addressing mode.
937/// It returns the operands which make up the maximal addressing mode it can
938/// match by reference.
Evan Cheng0d538262006-11-08 20:34:28 +0000939bool X86DAGToDAGISel::SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
940 SDOperand &Scale, SDOperand &Index,
941 SDOperand &Disp) {
Evan Chengec693f72005-12-08 02:01:35 +0000942 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +0000943 if (MatchAddress(N, AM))
944 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000945
Duncan Sands83ec4b62008-06-06 12:08:01 +0000946 MVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +0000947 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Cheng7dd281b2006-02-05 05:25:07 +0000948 if (!AM.Base.Reg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000949 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +0000950 }
Evan Cheng8700e142006-01-11 06:09:51 +0000951
Evan Cheng7dd281b2006-02-05 05:25:07 +0000952 if (!AM.IndexReg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000953 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +0000954
955 getAddressOperands(AM, Base, Scale, Index, Disp);
956 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000957}
958
Chris Lattner4fe4f252006-10-11 22:09:58 +0000959/// isZeroNode - Returns true if Elt is a constant zero or a floating point
960/// constant +0.0.
961static inline bool isZeroNode(SDOperand Elt) {
962 return ((isa<ConstantSDNode>(Elt) &&
963 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
964 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +0000965 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Chris Lattner4fe4f252006-10-11 22:09:58 +0000966}
967
968
Chris Lattner3a7cd952006-10-07 21:55:32 +0000969/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
970/// match a load whose top elements are either undef or zeros. The load flavor
971/// is derived from the type of N, which is either v4f32 or v2f64.
Evan Cheng0d538262006-11-08 20:34:28 +0000972bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Cheng07e4b002006-10-16 06:34:55 +0000973 SDOperand N, SDOperand &Base,
Evan Cheng82a91642006-10-11 21:06:01 +0000974 SDOperand &Scale, SDOperand &Index,
975 SDOperand &Disp, SDOperand &InChain,
976 SDOperand &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +0000977 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +0000978 InChain = N.getOperand(0).getValue(1);
Evan Cheng07e4b002006-10-16 06:34:55 +0000979 if (ISD::isNON_EXTLoad(InChain.Val) &&
980 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +0000981 N.hasOneUse() &&
Evan Cheng0d538262006-11-08 20:34:28 +0000982 CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
Evan Cheng82a91642006-10-11 21:06:01 +0000983 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Evan Cheng0d538262006-11-08 20:34:28 +0000984 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner3a7cd952006-10-07 21:55:32 +0000985 return false;
Evan Cheng82a91642006-10-11 21:06:01 +0000986 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +0000987 return true;
988 }
989 }
Chris Lattner4fe4f252006-10-11 22:09:58 +0000990
991 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +0000992 // elements. This is a vector shuffle from the zero vector.
Evan Chengd880b972008-05-09 21:53:03 +0000993 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.Val->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +0000994 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +0000995 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
996 N.getOperand(0).Val->hasOneUse() &&
997 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).Val) &&
998 N.getOperand(0).getOperand(0).hasOneUse()) {
999 // Okay, this is a zero extending load. Fold it.
1000 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
1001 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
1002 return false;
1003 OutChain = LD->getChain();
1004 InChain = SDOperand(LD, 1);
1005 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001006 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001007 return false;
1008}
1009
1010
Evan Cheng51a9ed92006-02-25 10:09:08 +00001011/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1012/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng0d538262006-11-08 20:34:28 +00001013bool X86DAGToDAGISel::SelectLEAAddr(SDOperand Op, SDOperand N,
1014 SDOperand &Base, SDOperand &Scale,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001015 SDOperand &Index, SDOperand &Disp) {
1016 X86ISelAddressMode AM;
1017 if (MatchAddress(N, AM))
1018 return false;
1019
Duncan Sands83ec4b62008-06-06 12:08:01 +00001020 MVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001021 unsigned Complexity = 0;
1022 if (AM.BaseType == X86ISelAddressMode::RegBase)
1023 if (AM.Base.Reg.Val)
1024 Complexity = 1;
1025 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001026 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001027 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1028 Complexity = 4;
1029
1030 if (AM.IndexReg.Val)
1031 Complexity++;
1032 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001033 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001034
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001035 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1036 // a simple shift.
1037 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001038 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001039
1040 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1041 // to a LEA. This is determined with some expermentation but is by no means
1042 // optimal (especially for code size consideration). LEA is nice because of
1043 // its three-address nature. Tweak the cost function again when we can run
1044 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +00001045 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
1046 // For X86-64, we should always use lea to materialize RIP relative
1047 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001048 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001049 Complexity = 4;
1050 else
1051 Complexity += 2;
1052 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001053
1054 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
1055 Complexity++;
1056
1057 if (Complexity > 2) {
1058 getAddressOperands(AM, Base, Scale, Index, Disp);
1059 return true;
1060 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001061 return false;
1062}
1063
Evan Cheng5e351682006-02-06 06:02:33 +00001064bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
1065 SDOperand &Base, SDOperand &Scale,
1066 SDOperand &Index, SDOperand &Disp) {
Evan Cheng466685d2006-10-09 20:57:25 +00001067 if (ISD::isNON_EXTLoad(N.Val) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001068 N.hasOneUse() &&
Evan Cheng27e1fe92006-10-14 08:33:25 +00001069 CanBeFoldedBy(N.Val, P.Val, P.Val))
Evan Cheng0d538262006-11-08 20:34:28 +00001070 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
Evan Cheng0114e942006-01-06 20:36:21 +00001071 return false;
1072}
1073
Evan Cheng7ccced62006-02-18 00:15:05 +00001074/// getGlobalBaseReg - Output the instructions required to put the
1075/// base address to use for accessing globals into a register.
1076///
Evan Cheng9ade2182006-08-26 05:34:46 +00001077SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng25ab6902006-09-08 06:48:29 +00001078 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Evan Cheng7ccced62006-02-18 00:15:05 +00001079 if (!GlobalBaseReg) {
1080 // Insert the set of GlobalBaseReg into the first MBB of the function
Evan Cheng0475ab52008-01-05 00:41:47 +00001081 MachineFunction *MF = BB->getParent();
1082 MachineBasicBlock &FirstMBB = MF->front();
Evan Cheng7ccced62006-02-18 00:15:05 +00001083 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Evan Cheng0475ab52008-01-05 00:41:47 +00001084 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Chris Lattner84bc5422007-12-31 04:13:23 +00001085 unsigned PC = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001086
Evan Chengc0f64ff2006-11-27 23:37:22 +00001087 const TargetInstrInfo *TII = TM.getInstrInfo();
Evan Chengf02ca692007-12-22 02:26:46 +00001088 // Operand of MovePCtoStack is completely ignored by asm printer. It's
1089 // only used in JIT code emission as displacement to pc.
Evan Cheng0475ab52008-01-05 00:41:47 +00001090 BuildMI(FirstMBB, MBBI, TII->get(X86::MOVPC32r), PC).addImm(0);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001091
1092 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
1093 // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
Evan Cheng706535d2007-01-22 21:34:25 +00001094 if (TM.getRelocationModel() == Reloc::PIC_ &&
1095 Subtarget->isPICStyleGOT()) {
Chris Lattner84bc5422007-12-31 04:13:23 +00001096 GlobalBaseReg = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng0475ab52008-01-05 00:41:47 +00001097 BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg)
1098 .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001099 } else {
1100 GlobalBaseReg = PC;
1101 }
1102
Evan Cheng7ccced62006-02-18 00:15:05 +00001103 }
Evan Cheng25ab6902006-09-08 06:48:29 +00001104 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
Evan Cheng7ccced62006-02-18 00:15:05 +00001105}
1106
Evan Chengb245d922006-05-20 01:36:52 +00001107static SDNode *FindCallStartFromCall(SDNode *Node) {
1108 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
1109 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1110 "Node doesn't have a token chain argument!");
1111 return FindCallStartFromCall(Node->getOperand(0).Val);
1112}
1113
Duncan Sands83ec4b62008-06-06 12:08:01 +00001114SDNode *X86DAGToDAGISel::getTruncate(SDOperand N0, MVT VT) {
Christopher Lambc59e5212007-08-10 21:48:46 +00001115 SDOperand SRIdx;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001116 switch (VT.getSimpleVT()) {
1117 default: assert(0 && "Unknown truncate!");
Christopher Lambc59e5212007-08-10 21:48:46 +00001118 case MVT::i8:
1119 SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1120 // Ensure that the source register has an 8-bit subreg on 32-bit targets
1121 if (!Subtarget->is64Bit()) {
1122 unsigned Opc;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001123 MVT VT;
1124 switch (N0.getValueType().getSimpleVT()) {
Christopher Lambc59e5212007-08-10 21:48:46 +00001125 default: assert(0 && "Unknown truncate!");
1126 case MVT::i16:
1127 Opc = X86::MOV16to16_;
1128 VT = MVT::i16;
1129 break;
1130 case MVT::i32:
1131 Opc = X86::MOV32to32_;
1132 VT = MVT::i32;
1133 break;
1134 }
Evan Cheng96aaa542007-10-12 07:55:53 +00001135 N0 = SDOperand(CurDAG->getTargetNode(Opc, VT, MVT::Flag, N0), 0);
1136 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1137 VT, N0, SRIdx, N0.getValue(1));
Christopher Lambc59e5212007-08-10 21:48:46 +00001138 }
1139 break;
1140 case MVT::i16:
1141 SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2
1142 break;
1143 case MVT::i32:
1144 SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3
1145 break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001146 }
Evan Cheng96aaa542007-10-12 07:55:53 +00001147 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, VT, N0, SRIdx);
Christopher Lambc59e5212007-08-10 21:48:46 +00001148}
1149
1150
Evan Cheng9ade2182006-08-26 05:34:46 +00001151SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Chengdef941b2005-12-15 01:02:48 +00001152 SDNode *Node = N.Val;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001153 MVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001154 unsigned Opc, MOpc;
1155 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +00001156
Evan Chengf597dc72006-02-10 22:24:32 +00001157#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001158 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +00001159 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001160 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001161 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001162#endif
1163
Evan Cheng34167212006-02-09 00:37:58 +00001164 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengf597dc72006-02-10 22:24:32 +00001165#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001166 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +00001167 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001168 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001169 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001170#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001171 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001172 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001173
Evan Cheng0114e942006-01-06 20:36:21 +00001174 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +00001175 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +00001176 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +00001177 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001178
Evan Cheng51a9ed92006-02-25 10:09:08 +00001179 case ISD::ADD: {
1180 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1181 // code and is matched first so to prevent it from being turned into
1182 // LEA32r X+c.
Evan Chengb1a9aec2008-01-08 02:06:11 +00001183 // In 64-bit small code size mode, use LEA to take advantage of
1184 // RIP-relative addressing.
1185 if (TM.getCodeModel() != CodeModel::Small)
1186 break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001187 MVT PtrVT = TLI.getPointerTy();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001188 SDOperand N0 = N.getOperand(0);
1189 SDOperand N1 = N.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00001190 if (N.Val->getValueType(0) == PtrVT &&
Evan Cheng19f2ffc2006-12-05 04:01:03 +00001191 N0.getOpcode() == X86ISD::Wrapper &&
Evan Cheng51a9ed92006-02-25 10:09:08 +00001192 N1.getOpcode() == ISD::Constant) {
1193 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1194 SDOperand C(0, 0);
1195 // TODO: handle ExternalSymbolSDNode.
1196 if (GlobalAddressSDNode *G =
1197 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001198 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001199 G->getOffset() + Offset);
1200 } else if (ConstantPoolSDNode *CP =
1201 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Chengc356a572006-09-12 21:04:05 +00001202 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001203 CP->getAlignment(),
1204 CP->getOffset()+Offset);
1205 }
1206
Evan Cheng25ab6902006-09-08 06:48:29 +00001207 if (C.Val) {
1208 if (Subtarget->is64Bit()) {
1209 SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
1210 CurDAG->getRegister(0, PtrVT), C };
1211 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1212 } else
1213 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1214 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001215 }
1216
1217 // Other cases are handled by auto-generated code.
1218 break;
Evan Chenga0ea0532006-02-23 02:43:52 +00001219 }
Evan Cheng020d2e82006-02-23 20:41:18 +00001220
Dan Gohman525178c2007-10-08 18:33:35 +00001221 case ISD::SMUL_LOHI:
1222 case ISD::UMUL_LOHI: {
1223 SDOperand N0 = Node->getOperand(0);
1224 SDOperand N1 = Node->getOperand(1);
1225
Dan Gohman525178c2007-10-08 18:33:35 +00001226 bool isSigned = Opcode == ISD::SMUL_LOHI;
1227 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001228 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001229 default: assert(0 && "Unsupported VT!");
1230 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1231 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1232 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001233 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001234 }
1235 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001236 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001237 default: assert(0 && "Unsupported VT!");
1238 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1239 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1240 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001241 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001242 }
1243
1244 unsigned LoReg, HiReg;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001245 switch (NVT.getSimpleVT()) {
Evan Cheng0114e942006-01-06 20:36:21 +00001246 default: assert(0 && "Unsupported VT!");
1247 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1248 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1249 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001250 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001251 }
1252
Evan Cheng0114e942006-01-06 20:36:21 +00001253 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng7afa1662007-08-02 05:48:35 +00001254 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Dan Gohman525178c2007-10-08 18:33:35 +00001255 // multiplty is commmutative
Evan Cheng948f3432006-01-06 23:19:29 +00001256 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +00001257 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng7afa1662007-08-02 05:48:35 +00001258 if (foldedLoad)
1259 std::swap(N0, N1);
Evan Cheng948f3432006-01-06 23:19:29 +00001260 }
1261
Evan Cheng04699902006-08-26 01:05:16 +00001262 AddToISelQueue(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001263 SDOperand InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg,
1264 N0, SDOperand()).getValue(1);
Evan Cheng0114e942006-01-06 20:36:21 +00001265
1266 if (foldedLoad) {
Dan Gohman525178c2007-10-08 18:33:35 +00001267 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001268 AddToISelQueue(Tmp0);
1269 AddToISelQueue(Tmp1);
1270 AddToISelQueue(Tmp2);
1271 AddToISelQueue(Tmp3);
Dan Gohman525178c2007-10-08 18:33:35 +00001272 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001273 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001274 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001275 InFlag = SDOperand(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001276 // Update the chain.
1277 ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
Evan Cheng0114e942006-01-06 20:36:21 +00001278 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001279 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001280 InFlag =
1281 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001282 }
1283
Dan Gohman525178c2007-10-08 18:33:35 +00001284 // Copy the low half of the result, if it is needed.
1285 if (!N.getValue(0).use_empty()) {
1286 SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1287 LoReg, NVT, InFlag);
1288 InFlag = Result.getValue(2);
1289 ReplaceUses(N.getValue(0), Result);
1290#ifndef NDEBUG
1291 DOUT << std::string(Indent-2, ' ') << "=> ";
1292 DEBUG(Result.Val->dump(CurDAG));
1293 DOUT << "\n";
1294#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001295 }
Dan Gohman525178c2007-10-08 18:33:35 +00001296 // Copy the high half of the result, if it is needed.
1297 if (!N.getValue(1).use_empty()) {
1298 SDOperand Result;
1299 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1300 // Prevent use of AH in a REX instruction by referencing AX instead.
1301 // Shift it down 8 bits.
1302 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1303 X86::AX, MVT::i16, InFlag);
1304 InFlag = Result.getValue(2);
1305 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1306 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1307 // Then truncate it down to i8.
1308 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1309 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1310 MVT::i8, Result, SRIdx), 0);
1311 } else {
1312 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1313 HiReg, NVT, InFlag);
1314 InFlag = Result.getValue(2);
1315 }
1316 ReplaceUses(N.getValue(1), Result);
1317#ifndef NDEBUG
1318 DOUT << std::string(Indent-2, ' ') << "=> ";
1319 DEBUG(Result.Val->dump(CurDAG));
1320 DOUT << "\n";
1321#endif
1322 }
Evan Cheng34167212006-02-09 00:37:58 +00001323
Evan Chengf597dc72006-02-10 22:24:32 +00001324#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001325 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001326#endif
Dan Gohman525178c2007-10-08 18:33:35 +00001327
Evan Cheng64a752f2006-08-11 09:08:15 +00001328 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001329 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001330
Dan Gohman525178c2007-10-08 18:33:35 +00001331 case ISD::SDIVREM:
1332 case ISD::UDIVREM: {
1333 SDOperand N0 = Node->getOperand(0);
1334 SDOperand N1 = Node->getOperand(1);
1335
1336 bool isSigned = Opcode == ISD::SDIVREM;
Evan Cheng948f3432006-01-06 23:19:29 +00001337 if (!isSigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00001338 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001339 default: assert(0 && "Unsupported VT!");
1340 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1341 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1342 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001343 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001344 }
1345 else
Duncan Sands83ec4b62008-06-06 12:08:01 +00001346 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001347 default: assert(0 && "Unsupported VT!");
1348 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1349 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1350 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001351 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001352 }
1353
1354 unsigned LoReg, HiReg;
1355 unsigned ClrOpcode, SExtOpcode;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001356 switch (NVT.getSimpleVT()) {
Evan Cheng948f3432006-01-06 23:19:29 +00001357 default: assert(0 && "Unsupported VT!");
1358 case MVT::i8:
1359 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001360 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001361 SExtOpcode = X86::CBW;
1362 break;
1363 case MVT::i16:
1364 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001365 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001366 SExtOpcode = X86::CWD;
1367 break;
1368 case MVT::i32:
1369 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001370 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001371 SExtOpcode = X86::CDQ;
1372 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001373 case MVT::i64:
1374 LoReg = X86::RAX; HiReg = X86::RDX;
1375 ClrOpcode = X86::MOV64r0;
1376 SExtOpcode = X86::CQO;
1377 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001378 }
1379
Dan Gohman525178c2007-10-08 18:33:35 +00001380 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
1381 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
1382
1383 SDOperand InFlag;
Evan Chengb1409ce2006-11-17 22:10:14 +00001384 if (NVT == MVT::i8 && !isSigned) {
1385 // Special case for div8, just use a move with zero extension to AX to
1386 // clear the upper 8 bits (AH).
1387 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
1388 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
1389 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
1390 AddToISelQueue(N0.getOperand(0));
1391 AddToISelQueue(Tmp0);
1392 AddToISelQueue(Tmp1);
1393 AddToISelQueue(Tmp2);
1394 AddToISelQueue(Tmp3);
1395 Move =
1396 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
1397 Ops, 5), 0);
1398 Chain = Move.getValue(1);
1399 ReplaceUses(N0.getValue(1), Chain);
1400 } else {
1401 AddToISelQueue(N0);
1402 Move =
1403 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
1404 Chain = CurDAG->getEntryNode();
1405 }
Dan Gohman525178c2007-10-08 18:33:35 +00001406 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDOperand());
Evan Cheng948f3432006-01-06 23:19:29 +00001407 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001408 } else {
1409 AddToISelQueue(N0);
1410 InFlag =
Dan Gohman525178c2007-10-08 18:33:35 +00001411 CurDAG->getCopyToReg(CurDAG->getEntryNode(),
1412 LoReg, N0, SDOperand()).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001413 if (isSigned) {
1414 // Sign extend the low part into the high part.
1415 InFlag =
1416 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
1417 } else {
1418 // Zero out the high part, effectively zero extending the input.
1419 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001420 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg,
1421 ClrNode, InFlag).getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001422 }
Evan Cheng948f3432006-01-06 23:19:29 +00001423 }
1424
1425 if (foldedLoad) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001426 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001427 AddToISelQueue(Tmp0);
1428 AddToISelQueue(Tmp1);
1429 AddToISelQueue(Tmp2);
1430 AddToISelQueue(Tmp3);
Evan Chengb1409ce2006-11-17 22:10:14 +00001431 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001432 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001433 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001434 InFlag = SDOperand(CNode, 1);
Dan Gohman525178c2007-10-08 18:33:35 +00001435 // Update the chain.
1436 ReplaceUses(N1.getValue(1), SDOperand(CNode, 0));
Evan Cheng948f3432006-01-06 23:19:29 +00001437 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001438 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001439 InFlag =
1440 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001441 }
1442
Dan Gohmana37c9f72007-09-25 18:23:27 +00001443 // Copy the division (low) result, if it is needed.
1444 if (!N.getValue(0).use_empty()) {
Dan Gohman525178c2007-10-08 18:33:35 +00001445 SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1446 LoReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001447 InFlag = Result.getValue(2);
1448 ReplaceUses(N.getValue(0), Result);
1449#ifndef NDEBUG
1450 DOUT << std::string(Indent-2, ' ') << "=> ";
1451 DEBUG(Result.Val->dump(CurDAG));
1452 DOUT << "\n";
1453#endif
Evan Chengf7ef26e2007-08-09 21:59:35 +00001454 }
Dan Gohmana37c9f72007-09-25 18:23:27 +00001455 // Copy the remainder (high) result, if it is needed.
1456 if (!N.getValue(1).use_empty()) {
1457 SDOperand Result;
1458 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1459 // Prevent use of AH in a REX instruction by referencing AX instead.
1460 // Shift it down 8 bits.
Dan Gohman525178c2007-10-08 18:33:35 +00001461 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1462 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001463 InFlag = Result.getValue(2);
1464 Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result,
1465 CurDAG->getTargetConstant(8, MVT::i8)), 0);
1466 // Then truncate it down to i8.
1467 SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1
1468 Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG,
1469 MVT::i8, Result, SRIdx), 0);
1470 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00001471 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
1472 HiReg, NVT, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00001473 InFlag = Result.getValue(2);
1474 }
1475 ReplaceUses(N.getValue(1), Result);
1476#ifndef NDEBUG
1477 DOUT << std::string(Indent-2, ' ') << "=> ";
1478 DEBUG(Result.Val->dump(CurDAG));
1479 DOUT << "\n";
1480#endif
1481 }
Evan Chengf597dc72006-02-10 22:24:32 +00001482
1483#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +00001484 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001485#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001486
1487 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001488 }
Christopher Lamba1eb1552007-08-10 22:22:41 +00001489
1490 case ISD::ANY_EXTEND: {
Christopher Lambc9298232008-03-16 03:12:01 +00001491 // Check if the type extended to supports subregs.
1492 if (NVT == MVT::i8)
1493 break;
1494
Christopher Lamba1eb1552007-08-10 22:22:41 +00001495 SDOperand N0 = Node->getOperand(0);
Christopher Lambc9298232008-03-16 03:12:01 +00001496 // Get the subregsiter index for the type to extend.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001497 MVT N0VT = N0.getValueType();
Christopher Lambc9298232008-03-16 03:12:01 +00001498 unsigned Idx = (N0VT == MVT::i32) ? X86::SUBREG_32BIT :
1499 (N0VT == MVT::i16) ? X86::SUBREG_16BIT :
1500 (Subtarget->is64Bit()) ? X86::SUBREG_8BIT : 0;
1501
1502 // If we don't have a subreg Idx, let generated ISel have a try.
1503 if (Idx == 0)
1504 break;
1505
1506 // If we have an index, generate an insert_subreg into undef.
Christopher Lamba1eb1552007-08-10 22:22:41 +00001507 AddToISelQueue(N0);
Christopher Lambc9298232008-03-16 03:12:01 +00001508 SDOperand Undef =
Evan Cheng90ce87b2008-04-03 07:45:18 +00001509 SDOperand(CurDAG->getTargetNode(X86::IMPLICIT_DEF, NVT), 0);
Christopher Lambc9298232008-03-16 03:12:01 +00001510 SDOperand SRIdx = CurDAG->getTargetConstant(Idx, MVT::i32);
1511 SDNode *ResNode = CurDAG->getTargetNode(X86::INSERT_SUBREG,
Evan Cheng90ce87b2008-04-03 07:45:18 +00001512 NVT, Undef, N0, SRIdx);
Christopher Lamba1eb1552007-08-10 22:22:41 +00001513
1514#ifndef NDEBUG
Christopher Lambc9298232008-03-16 03:12:01 +00001515 DOUT << std::string(Indent-2, ' ') << "=> ";
1516 DEBUG(ResNode->dump(CurDAG));
1517 DOUT << "\n";
1518 Indent -= 2;
Christopher Lamba1eb1552007-08-10 22:22:41 +00001519#endif
Christopher Lambc9298232008-03-16 03:12:01 +00001520 return ResNode;
Christopher Lamba1eb1552007-08-10 22:22:41 +00001521 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001522
1523 case ISD::SIGN_EXTEND_INREG: {
1524 SDOperand N0 = Node->getOperand(0);
1525 AddToISelQueue(N0);
Evan Cheng403be7e2006-05-08 08:01:26 +00001526
Duncan Sands83ec4b62008-06-06 12:08:01 +00001527 MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Christopher Lambc59e5212007-08-10 21:48:46 +00001528 SDOperand TruncOp = SDOperand(getTruncate(N0, SVT), 0);
Bill Wendling0d642872007-11-01 08:51:44 +00001529 unsigned Opc = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001530 switch (NVT.getSimpleVT()) {
1531 default: assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001532 case MVT::i16:
Christopher Lambc59e5212007-08-10 21:48:46 +00001533 if (SVT == MVT::i8) Opc = X86::MOVSX16rr8;
1534 else assert(0 && "Unknown sign_extend_inreg!");
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001535 break;
1536 case MVT::i32:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001537 switch (SVT.getSimpleVT()) {
1538 default: assert(0 && "Unknown sign_extend_inreg!");
Christopher Lambc59e5212007-08-10 21:48:46 +00001539 case MVT::i8: Opc = X86::MOVSX32rr8; break;
1540 case MVT::i16: Opc = X86::MOVSX32rr16; break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001541 }
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001542 break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001543 case MVT::i64:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001544 switch (SVT.getSimpleVT()) {
1545 default: assert(0 && "Unknown sign_extend_inreg!");
Christopher Lambc59e5212007-08-10 21:48:46 +00001546 case MVT::i8: Opc = X86::MOVSX64rr8; break;
1547 case MVT::i16: Opc = X86::MOVSX64rr16; break;
1548 case MVT::i32: Opc = X86::MOVSX64rr32; break;
Christopher Lambc59e5212007-08-10 21:48:46 +00001549 }
1550 break;
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001551 }
Christopher Lambc59e5212007-08-10 21:48:46 +00001552
1553 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp);
1554
1555#ifndef NDEBUG
1556 DOUT << std::string(Indent-2, ' ') << "=> ";
1557 DEBUG(TruncOp.Val->dump(CurDAG));
1558 DOUT << "\n";
1559 DOUT << std::string(Indent-2, ' ') << "=> ";
1560 DEBUG(ResNode->dump(CurDAG));
1561 DOUT << "\n";
1562 Indent -= 2;
1563#endif
1564 return ResNode;
1565 break;
1566 }
1567
1568 case ISD::TRUNCATE: {
1569 SDOperand Input = Node->getOperand(0);
1570 AddToISelQueue(Node->getOperand(0));
1571 SDNode *ResNode = getTruncate(Input, NVT);
1572
Evan Cheng403be7e2006-05-08 08:01:26 +00001573#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001574 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001575 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001576 DOUT << "\n";
Evan Cheng403be7e2006-05-08 08:01:26 +00001577 Indent -= 2;
1578#endif
Christopher Lamb2dc6dc62007-07-29 01:24:57 +00001579 return ResNode;
Evan Cheng6b2e2542006-05-20 07:44:28 +00001580 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001581 }
Evan Cheng851bc042008-06-17 02:01:22 +00001582
1583 case ISD::DECLARE: {
1584 // Handle DECLARE nodes here because the second operand may have been
1585 // wrapped in X86ISD::Wrapper.
1586 SDOperand Chain = Node->getOperand(0);
1587 SDOperand N1 = Node->getOperand(1);
1588 SDOperand N2 = Node->getOperand(2);
Evan Chengfab83872008-06-18 02:48:27 +00001589 if (!isa<FrameIndexSDNode>(N1))
1590 break;
1591 int FI = cast<FrameIndexSDNode>(N1)->getIndex();
1592 if (N2.getOpcode() == ISD::ADD &&
1593 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg)
1594 N2 = N2.getOperand(1);
1595 if (N2.getOpcode() == X86ISD::Wrapper &&
Evan Cheng851bc042008-06-17 02:01:22 +00001596 isa<GlobalAddressSDNode>(N2.getOperand(0))) {
Evan Cheng851bc042008-06-17 02:01:22 +00001597 GlobalValue *GV =
1598 cast<GlobalAddressSDNode>(N2.getOperand(0))->getGlobal();
1599 SDOperand Tmp1 = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
1600 SDOperand Tmp2 = CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());
1601 AddToISelQueue(Chain);
1602 SDOperand Ops[] = { Tmp1, Tmp2, Chain };
1603 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE,
1604 MVT::Other, Ops, 3);
1605 }
1606 break;
1607 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001608 }
1609
Evan Cheng9ade2182006-08-26 05:34:46 +00001610 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001611
Evan Chengf597dc72006-02-10 22:24:32 +00001612#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001613 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001614 if (ResNode == NULL || ResNode == N.Val)
1615 DEBUG(N.Val->dump(CurDAG));
1616 else
1617 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001618 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001619 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001620#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001621
1622 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001623}
1624
Chris Lattnerc0bad572006-06-08 18:03:49 +00001625bool X86DAGToDAGISel::
1626SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1627 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1628 SDOperand Op0, Op1, Op2, Op3;
1629 switch (ConstraintCode) {
1630 case 'o': // offsetable ??
1631 case 'v': // not offsetable ??
1632 default: return true;
1633 case 'm': // memory
Evan Cheng0d538262006-11-08 20:34:28 +00001634 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001635 return true;
1636 break;
1637 }
1638
Evan Cheng04699902006-08-26 01:05:16 +00001639 OutOps.push_back(Op0);
1640 OutOps.push_back(Op1);
1641 OutOps.push_back(Op2);
1642 OutOps.push_back(Op3);
1643 AddToISelQueue(Op0);
1644 AddToISelQueue(Op1);
1645 AddToISelQueue(Op2);
1646 AddToISelQueue(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001647 return false;
1648}
1649
Chris Lattnerc961eea2005-11-16 01:54:32 +00001650/// createX86ISelDag - This pass converts a legalized DAG into a
1651/// X86-specific DAG, ready for instruction scheduling.
1652///
Evan Chenge50794a2006-08-29 18:28:33 +00001653FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1654 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001655}