blob: 7c61febf00ee0d233aaa89c8ba0dbcd053429cae [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//
5// This file was developed by the Evan Cheng and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
Chris Lattner92cb0af2006-01-11 01:15:34 +000019#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000020#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000021#include "X86TargetMachine.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000022#include "llvm/GlobalValue.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000023#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000024#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000025#include "llvm/Support/CFG.h"
Reid Spencer7aa8a452007-01-12 23:22:14 +000026#include "llvm/Type.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000027#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000028#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000030#include "llvm/CodeGen/MachineInstrBuilder.h"
31#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000032#include "llvm/CodeGen/SelectionDAGISel.h"
33#include "llvm/Target/TargetMachine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000034#include "llvm/Support/Compiler.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000035#include "llvm/Support/Debug.h"
36#include "llvm/Support/MathExtras.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000037#include "llvm/ADT/Statistic.h"
Evan Cheng2ef88a02006-08-07 22:28:20 +000038#include <queue>
Evan Chengba2f0a92006-02-05 06:46:41 +000039#include <set>
Chris Lattnerc961eea2005-11-16 01:54:32 +000040using namespace llvm;
41
Chris Lattner95b2c7d2006-12-19 22:59:26 +000042STATISTIC(NumFPKill , "Number of FP_REG_KILL instructions added");
43STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
44
45
Chris Lattnerc961eea2005-11-16 01:54:32 +000046//===----------------------------------------------------------------------===//
47// Pattern Matcher Implementation
48//===----------------------------------------------------------------------===//
49
50namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000051 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
52 /// SDOperand's instead of register numbers for the leaves of the matched
53 /// tree.
54 struct X86ISelAddressMode {
55 enum {
56 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000057 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000058 } BaseType;
59
60 struct { // This is really a union, discriminated by BaseType!
61 SDOperand Reg;
62 int FrameIndex;
63 } Base;
64
Evan Cheng25ab6902006-09-08 06:48:29 +000065 bool isRIPRel; // RIP relative?
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000066 unsigned Scale;
67 SDOperand IndexReg;
68 unsigned Disp;
69 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000070 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000071 const char *ES;
72 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000073 unsigned Align; // CP alignment.
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000074
75 X86ISelAddressMode()
Evan Cheng25ab6902006-09-08 06:48:29 +000076 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
77 GV(0), CP(0), ES(0), JT(-1), Align(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000078 }
79 };
80}
81
82namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +000083 //===--------------------------------------------------------------------===//
84 /// ISel - X86 specific code to select X86 machine instructions for
85 /// SelectionDAG operations.
86 ///
Chris Lattner2c79de82006-06-28 23:27:49 +000087 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +000088 /// ContainsFPCode - Every instruction we select that uses or defines a FP
89 /// register should set this to true.
90 bool ContainsFPCode;
91
Evan Chenge50794a2006-08-29 18:28:33 +000092 /// FastISel - Enable fast(er) instruction selection.
93 ///
94 bool FastISel;
95
Evan Cheng25ab6902006-09-08 06:48:29 +000096 /// TM - Keep a reference to X86TargetMachine.
97 ///
98 X86TargetMachine &TM;
99
Chris Lattnerc961eea2005-11-16 01:54:32 +0000100 /// X86Lowering - This object fully describes how to lower LLVM code to an
101 /// X86-specific SelectionDAG.
102 X86TargetLowering X86Lowering;
103
104 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
105 /// make the right decision when generating code for different targets.
106 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000107
Evan Cheng25ab6902006-09-08 06:48:29 +0000108 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
109 /// base register.
Evan Cheng7ccced62006-02-18 00:15:05 +0000110 unsigned GlobalBaseReg;
Evan Chenga8df1b42006-07-27 16:44:36 +0000111
Chris Lattnerc961eea2005-11-16 01:54:32 +0000112 public:
Evan Cheng25ab6902006-09-08 06:48:29 +0000113 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Evan Chengc4c62572006-03-13 23:20:37 +0000114 : SelectionDAGISel(X86Lowering),
Evan Cheng25ab6902006-09-08 06:48:29 +0000115 ContainsFPCode(false), FastISel(fast), TM(tm),
Evan Chenga8df1b42006-07-27 16:44:36 +0000116 X86Lowering(*TM.getTargetLowering()),
Evan Chengf4b4c412006-08-08 00:31:00 +0000117 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000118
Evan Cheng7ccced62006-02-18 00:15:05 +0000119 virtual bool runOnFunction(Function &Fn) {
120 // Make sure we re-emit a set of the global base reg if necessary
121 GlobalBaseReg = 0;
122 return SelectionDAGISel::runOnFunction(Fn);
123 }
124
Chris Lattnerc961eea2005-11-16 01:54:32 +0000125 virtual const char *getPassName() const {
126 return "X86 DAG->DAG Instruction Selection";
127 }
128
129 /// InstructionSelectBasicBlock - This callback is invoked by
130 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
131 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
132
Evan Cheng8700e142006-01-11 06:09:51 +0000133 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
134
Evan Cheng27e1fe92006-10-14 08:33:25 +0000135 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root);
Evan Chenga8df1b42006-07-27 16:44:36 +0000136
Chris Lattnerc961eea2005-11-16 01:54:32 +0000137// Include the pieces autogenerated from the target description.
138#include "X86GenDAGISel.inc"
139
140 private:
Evan Cheng9ade2182006-08-26 05:34:46 +0000141 SDNode *Select(SDOperand N);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000142
Evan Cheng2486af12006-02-11 02:05:36 +0000143 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM, bool isRoot = true);
Evan Cheng0d538262006-11-08 20:34:28 +0000144 bool SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
145 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
146 bool SelectLEAAddr(SDOperand Op, SDOperand N, SDOperand &Base,
147 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
148 bool SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Cheng07e4b002006-10-16 06:34:55 +0000149 SDOperand N, SDOperand &Base, SDOperand &Scale,
Evan Cheng82a91642006-10-11 21:06:01 +0000150 SDOperand &Index, SDOperand &Disp,
151 SDOperand &InChain, SDOperand &OutChain);
Evan Cheng5e351682006-02-06 06:02:33 +0000152 bool TryFoldLoad(SDOperand P, SDOperand N,
153 SDOperand &Base, SDOperand &Scale,
Evan Cheng0114e942006-01-06 20:36:21 +0000154 SDOperand &Index, SDOperand &Disp);
Evan Cheng70e674e2006-08-28 20:10:17 +0000155 void InstructionSelectPreprocess(SelectionDAG &DAG);
Evan Cheng2ef88a02006-08-07 22:28:20 +0000156
Chris Lattnerc0bad572006-06-08 18:03:49 +0000157 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
158 /// inline asm expressions.
159 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
160 char ConstraintCode,
161 std::vector<SDOperand> &OutOps,
162 SelectionDAG &DAG);
163
Evan Cheng3649b0e2006-06-02 22:38:37 +0000164 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
165
Evan Chenge5280532005-12-12 21:49:40 +0000166 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
167 SDOperand &Scale, SDOperand &Index,
168 SDOperand &Disp) {
169 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000170 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
171 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000172 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000173 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000174 // These are 32-bit even in 64-bit mode since RIP relative offset
175 // is 32-bit.
176 if (AM.GV)
177 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
178 else if (AM.CP)
179 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp);
180 else if (AM.ES)
181 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
182 else if (AM.JT != -1)
183 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
184 else
185 Disp = getI32Imm(AM.Disp);
Evan Chenge5280532005-12-12 21:49:40 +0000186 }
187
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000188 /// getI8Imm - Return a target constant with the specified value, of type
189 /// i8.
190 inline SDOperand getI8Imm(unsigned Imm) {
191 return CurDAG->getTargetConstant(Imm, MVT::i8);
192 }
193
Chris Lattnerc961eea2005-11-16 01:54:32 +0000194 /// getI16Imm - Return a target constant with the specified value, of type
195 /// i16.
196 inline SDOperand getI16Imm(unsigned Imm) {
197 return CurDAG->getTargetConstant(Imm, MVT::i16);
198 }
199
200 /// getI32Imm - Return a target constant with the specified value, of type
201 /// i32.
202 inline SDOperand getI32Imm(unsigned Imm) {
203 return CurDAG->getTargetConstant(Imm, MVT::i32);
204 }
Evan Chengf597dc72006-02-10 22:24:32 +0000205
Evan Cheng7ccced62006-02-18 00:15:05 +0000206 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
207 /// base register. Return the virtual register that holds this value.
Evan Cheng9ade2182006-08-26 05:34:46 +0000208 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000209
Evan Cheng23addc02006-02-10 22:46:26 +0000210#ifndef NDEBUG
211 unsigned Indent;
212#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000213 };
214}
215
Evan Chenga275ecb2006-10-10 01:46:56 +0000216static SDNode *findFlagUse(SDNode *N) {
217 unsigned FlagResNo = N->getNumValues()-1;
218 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
219 SDNode *User = *I;
220 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
221 SDOperand Op = User->getOperand(i);
Evan Cheng494cec62006-10-12 19:13:59 +0000222 if (Op.Val == N && Op.ResNo == FlagResNo)
Evan Chenga275ecb2006-10-10 01:46:56 +0000223 return User;
224 }
225 }
226 return NULL;
227}
228
Evan Cheng27e1fe92006-10-14 08:33:25 +0000229static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
230 SDNode *Root, SDNode *Skip, bool &found,
Evan Chengf4b4c412006-08-08 00:31:00 +0000231 std::set<SDNode *> &Visited) {
232 if (found ||
233 Use->getNodeId() > Def->getNodeId() ||
234 !Visited.insert(Use).second)
235 return;
236
Evan Cheng27e1fe92006-10-14 08:33:25 +0000237 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000238 SDNode *N = Use->getOperand(i).Val;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000239 if (N == Skip)
Evan Chenga275ecb2006-10-10 01:46:56 +0000240 continue;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000241 if (N == Def) {
242 if (Use == ImmedUse)
243 continue; // Immediate use is ok.
244 if (Use == Root) {
245 assert(Use->getOpcode() == ISD::STORE ||
246 Use->getOpcode() == X86ISD::CMP);
247 continue;
248 }
Evan Chengf4b4c412006-08-08 00:31:00 +0000249 found = true;
250 break;
251 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000252 findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000253 }
254}
255
Evan Cheng27e1fe92006-10-14 08:33:25 +0000256/// isNonImmUse - Start searching from Root up the DAG to check is Def can
257/// be reached. Return true if that's the case. However, ignore direct uses
258/// by ImmedUse (which would be U in the example illustrated in
259/// CanBeFoldedBy) and by Root (which can happen in the store case).
260/// FIXME: to be really generic, we should allow direct use by any node
261/// that is being folded. But realisticly since we only fold loads which
262/// have one non-chain use, we only need to watch out for load/op/store
263/// and load/op/cmp case where the root (store / cmp) may reach the load via
264/// its chain operand.
265static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
266 SDNode *Skip = NULL) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000267 std::set<SDNode *> Visited;
268 bool found = false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000269 findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000270 return found;
271}
272
273
Evan Cheng27e1fe92006-10-14 08:33:25 +0000274bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) {
275 if (FastISel) return false;
276
Evan Chenga8df1b42006-07-27 16:44:36 +0000277 // If U use can somehow reach N through another path then U can't fold N or
278 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Cheng37e18032006-07-28 06:33:41 +0000279 // through X. If N is folded into into U, then X is both a predecessor and
Evan Chenga8df1b42006-07-27 16:44:36 +0000280 // a successor of U.
281 //
282 // [ N ]
283 // ^ ^
284 // | |
285 // / \---
286 // / [X]
287 // | ^
288 // [U]--------|
Evan Cheng27e1fe92006-10-14 08:33:25 +0000289
290 if (isNonImmUse(Root, N, U))
291 return false;
292
293 // If U produces a flag, then it gets (even more) interesting. Since it
294 // would have been "glued" together with its flag use, we need to check if
295 // it might reach N:
296 //
297 // [ N ]
298 // ^ ^
299 // | |
300 // [U] \--
301 // ^ [TF]
302 // | ^
303 // | |
304 // \ /
305 // [FU]
306 //
307 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
308 // NU), then TF is a predecessor of FU and a successor of NU. But since
309 // NU and FU are flagged together, this effectively creates a cycle.
310 bool HasFlagUse = false;
311 MVT::ValueType VT = Root->getValueType(Root->getNumValues()-1);
312 while ((VT == MVT::Flag && !Root->use_empty())) {
313 SDNode *FU = findFlagUse(Root);
314 if (FU == NULL)
315 break;
316 else {
317 Root = FU;
318 HasFlagUse = true;
Evan Chenga275ecb2006-10-10 01:46:56 +0000319 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000320 VT = Root->getValueType(Root->getNumValues()-1);
Evan Chenga275ecb2006-10-10 01:46:56 +0000321 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000322
323 if (HasFlagUse)
324 return !isNonImmUse(Root, N, Root, U);
325 return true;
Evan Chenga8df1b42006-07-27 16:44:36 +0000326}
327
Evan Cheng70e674e2006-08-28 20:10:17 +0000328/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
329/// and move load below the TokenFactor. Replace store's chain operand with
330/// load's chain result.
331static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
332 SDOperand Store, SDOperand TF) {
333 std::vector<SDOperand> Ops;
334 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
335 if (Load.Val == TF.Val->getOperand(i).Val)
336 Ops.push_back(Load.Val->getOperand(0));
337 else
338 Ops.push_back(TF.Val->getOperand(i));
339 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
340 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
341 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
342 Store.getOperand(2), Store.getOperand(3));
343}
344
345/// InstructionSelectPreprocess - Preprocess the DAG to allow the instruction
346/// selector to pick more load-modify-store instructions. This is a common
347/// case:
348///
349/// [Load chain]
350/// ^
351/// |
352/// [Load]
353/// ^ ^
354/// | |
355/// / \-
356/// / |
357/// [TokenFactor] [Op]
358/// ^ ^
359/// | |
360/// \ /
361/// \ /
362/// [Store]
363///
364/// The fact the store's chain operand != load's chain will prevent the
365/// (store (op (load))) instruction from being selected. We can transform it to:
366///
367/// [Load chain]
368/// ^
369/// |
370/// [TokenFactor]
371/// ^
372/// |
373/// [Load]
374/// ^ ^
375/// | |
376/// | \-
377/// | |
378/// | [Op]
379/// | ^
380/// | |
381/// \ /
382/// \ /
383/// [Store]
384void X86DAGToDAGISel::InstructionSelectPreprocess(SelectionDAG &DAG) {
385 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
386 E = DAG.allnodes_end(); I != E; ++I) {
Evan Cheng8b2794a2006-10-13 21:14:26 +0000387 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000388 continue;
389 SDOperand Chain = I->getOperand(0);
390 if (Chain.Val->getOpcode() != ISD::TokenFactor)
391 continue;
392
393 SDOperand N1 = I->getOperand(1);
394 SDOperand N2 = I->getOperand(2);
Evan Cheng1453de52006-09-01 22:52:28 +0000395 if (MVT::isFloatingPoint(N1.getValueType()) ||
396 MVT::isVector(N1.getValueType()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000397 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000398 continue;
399
400 bool RModW = false;
401 SDOperand Load;
402 unsigned Opcode = N1.Val->getOpcode();
403 switch (Opcode) {
404 case ISD::ADD:
405 case ISD::MUL:
Evan Cheng70e674e2006-08-28 20:10:17 +0000406 case ISD::AND:
407 case ISD::OR:
408 case ISD::XOR:
409 case ISD::ADDC:
410 case ISD::ADDE: {
411 SDOperand N10 = N1.getOperand(0);
412 SDOperand N11 = N1.getOperand(1);
Evan Cheng466685d2006-10-09 20:57:25 +0000413 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng70e674e2006-08-28 20:10:17 +0000414 RModW = true;
Evan Cheng466685d2006-10-09 20:57:25 +0000415 else if (ISD::isNON_EXTLoad(N11.Val)) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000416 RModW = true;
417 std::swap(N10, N11);
418 }
419 RModW = RModW && N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000420 (N10.getOperand(1) == N2) &&
421 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000422 if (RModW)
423 Load = N10;
424 break;
425 }
426 case ISD::SUB:
427 case ISD::SHL:
428 case ISD::SRA:
429 case ISD::SRL:
430 case ISD::ROTL:
431 case ISD::ROTR:
432 case ISD::SUBC:
433 case ISD::SUBE:
434 case X86ISD::SHLD:
435 case X86ISD::SHRD: {
436 SDOperand N10 = N1.getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +0000437 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng70e674e2006-08-28 20:10:17 +0000438 RModW = N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000439 (N10.getOperand(1) == N2) &&
440 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000441 if (RModW)
442 Load = N10;
443 break;
444 }
445 }
446
Evan Cheng82a35b32006-08-29 06:44:17 +0000447 if (RModW) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000448 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000449 ++NumLoadMoved;
450 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000451 }
452}
453
Chris Lattnerc961eea2005-11-16 01:54:32 +0000454/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
455/// when it has created a SelectionDAG for us to codegen.
456void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
457 DEBUG(BB->dump());
Chris Lattner92cb0af2006-01-11 01:15:34 +0000458 MachineFunction::iterator FirstMBB = BB;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000459
Evan Chenge50794a2006-08-29 18:28:33 +0000460 if (!FastISel)
Evan Cheng70e674e2006-08-28 20:10:17 +0000461 InstructionSelectPreprocess(DAG);
462
Chris Lattnerc961eea2005-11-16 01:54:32 +0000463 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000464#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000465 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000466 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000467#endif
Evan Chengba2f0a92006-02-05 06:46:41 +0000468 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengf597dc72006-02-10 22:24:32 +0000469#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000470 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000471#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000472
Chris Lattnerc961eea2005-11-16 01:54:32 +0000473 DAG.RemoveDeadNodes();
474
475 // Emit machine code to BB.
476 ScheduleAndEmitDAG(DAG);
Chris Lattner92cb0af2006-01-11 01:15:34 +0000477
478 // If we are emitting FP stack code, scan the basic block to determine if this
479 // block defines any FP values. If so, put an FP_REG_KILL instruction before
480 // the terminator of the block.
Evan Cheng559806f2006-01-27 08:10:46 +0000481 if (!Subtarget->hasSSE2()) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000482 // Note that FP stack instructions *are* used in SSE code when returning
483 // values, but these are not live out of the basic block, so we don't need
484 // an FP_REG_KILL in this case either.
485 bool ContainsFPCode = false;
486
487 // Scan all of the machine instructions in these MBBs, checking for FP
488 // stores.
489 MachineFunction::iterator MBBI = FirstMBB;
490 do {
491 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
492 !ContainsFPCode && I != E; ++I) {
493 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
494 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
495 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
496 RegMap->getRegClass(I->getOperand(0).getReg()) ==
497 X86::RFPRegisterClass) {
498 ContainsFPCode = true;
499 break;
500 }
501 }
502 }
503 } while (!ContainsFPCode && &*(MBBI++) != BB);
504
505 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
506 // a copy of the input value in this block.
507 if (!ContainsFPCode) {
508 // Final check, check LLVM BB's that are successors to the LLVM BB
509 // corresponding to BB for FP PHI nodes.
510 const BasicBlock *LLVMBB = BB->getBasicBlock();
511 const PHINode *PN;
512 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
513 !ContainsFPCode && SI != E; ++SI) {
514 for (BasicBlock::const_iterator II = SI->begin();
515 (PN = dyn_cast<PHINode>(II)); ++II) {
516 if (PN->getType()->isFloatingPoint()) {
517 ContainsFPCode = true;
518 break;
519 }
520 }
521 }
522 }
523
524 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
525 if (ContainsFPCode) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000526 BuildMI(*BB, BB->getFirstTerminator(),
527 TM.getInstrInfo()->get(X86::FP_REG_KILL));
Chris Lattner92cb0af2006-01-11 01:15:34 +0000528 ++NumFPKill;
529 }
530 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000531}
532
Evan Cheng8700e142006-01-11 06:09:51 +0000533/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
534/// the main function.
Evan Cheng3649b0e2006-06-02 22:38:37 +0000535void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
536 MachineFrameInfo *MFI) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000537 const TargetInstrInfo *TII = TM.getInstrInfo();
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000538 if (Subtarget->isTargetCygMing())
Evan Chengc0f64ff2006-11-27 23:37:22 +0000539 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Evan Cheng3649b0e2006-06-02 22:38:37 +0000540
Evan Cheng8700e142006-01-11 06:09:51 +0000541 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
542 int CWFrameIdx = MFI->CreateStackObject(2, 2);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000543 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng8700e142006-01-11 06:09:51 +0000544
545 // Set the high part to be 64-bit precision.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000546 addFrameReference(BuildMI(BB, TII->get(X86::MOV8mi)),
Evan Cheng8700e142006-01-11 06:09:51 +0000547 CWFrameIdx, 1).addImm(2);
548
549 // Reload the modified control word now.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000550 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng8700e142006-01-11 06:09:51 +0000551}
552
553void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
554 // If this is main, emit special code for main.
555 MachineBasicBlock *BB = MF.begin();
556 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
557 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
558}
559
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000560/// MatchAddress - Add the specified node to the specified addressing mode,
561/// returning true if it cannot be done. This just pattern matches for the
562/// addressing mode
Evan Cheng2486af12006-02-11 02:05:36 +0000563bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
564 bool isRoot) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000565 // RIP relative addressing: %rip + 32-bit displacement!
566 if (AM.isRIPRel) {
567 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000568 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000569 if (isInt32(AM.Disp + Val)) {
570 AM.Disp += Val;
571 return false;
572 }
573 }
574 return true;
575 }
576
Evan Cheng2ef88a02006-08-07 22:28:20 +0000577 int id = N.Val->getNodeId();
578 bool Available = isSelected(id);
Evan Cheng2486af12006-02-11 02:05:36 +0000579
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000580 switch (N.getOpcode()) {
581 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000582 case ISD::Constant: {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000583 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000584 if (isInt32(AM.Disp + Val)) {
585 AM.Disp += Val;
586 return false;
587 }
588 break;
589 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000590
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000591 case X86ISD::Wrapper: {
592 bool is64Bit = Subtarget->is64Bit();
Evan Cheng0085a282006-11-30 21:55:46 +0000593 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000594 if (is64Bit && TM.getCodeModel() != CodeModel::Small)
Evan Cheng0085a282006-11-30 21:55:46 +0000595 break;
Evan Cheng28b514392006-12-05 19:50:18 +0000596 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
597 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000598 // If value is available in a register both base and index components have
599 // been picked, we can't fit the result available in the register in the
600 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Evan Cheng49463992006-11-29 23:46:27 +0000601 if (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
Evan Cheng28b514392006-12-05 19:50:18 +0000602 bool isStatic = TM.getRelocationModel() == Reloc::Static;
603 SDOperand N0 = N.getOperand(0);
604 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
605 GlobalValue *GV = G->getGlobal();
Evan Chenga70d14b2006-12-19 21:31:42 +0000606 bool isAbs32 = !is64Bit || isStatic;
Evan Cheng28b514392006-12-05 19:50:18 +0000607 if (isAbs32 || isRoot) {
Evan Chenga70d14b2006-12-19 21:31:42 +0000608 AM.GV = GV;
Evan Cheng28b514392006-12-05 19:50:18 +0000609 AM.Disp += G->getOffset();
610 AM.isRIPRel = !isAbs32;
611 return false;
612 }
613 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
614 if (!is64Bit || isStatic || isRoot) {
Evan Chengc356a572006-09-12 21:04:05 +0000615 AM.CP = CP->getConstVal();
Evan Cheng51a9ed92006-02-25 10:09:08 +0000616 AM.Align = CP->getAlignment();
617 AM.Disp += CP->getOffset();
Evan Cheng28b514392006-12-05 19:50:18 +0000618 AM.isRIPRel = !isStatic;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000619 return false;
620 }
Evan Cheng28b514392006-12-05 19:50:18 +0000621 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
622 if (isStatic || isRoot) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000623 AM.ES = S->getSymbol();
Evan Cheng28b514392006-12-05 19:50:18 +0000624 AM.isRIPRel = !isStatic;
Evan Cheng25ab6902006-09-08 06:48:29 +0000625 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000626 }
627 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
628 if (isStatic || isRoot) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000629 AM.JT = J->getIndex();
Evan Cheng28b514392006-12-05 19:50:18 +0000630 AM.isRIPRel = !isStatic;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000631 return false;
632 }
633 }
634 }
635 break;
Evan Cheng0085a282006-11-30 21:55:46 +0000636 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000637
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000638 case ISD::FrameIndex:
639 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
640 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
641 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
642 return false;
643 }
644 break;
Evan Chengec693f72005-12-08 02:01:35 +0000645
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000646 case ISD::SHL:
Evan Cheng51a9ed92006-02-25 10:09:08 +0000647 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000648 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
649 unsigned Val = CN->getValue();
650 if (Val == 1 || Val == 2 || Val == 3) {
651 AM.Scale = 1 << Val;
652 SDOperand ShVal = N.Val->getOperand(0);
653
654 // Okay, we know that we have a scale by now. However, if the scaled
655 // value is an add of something and a constant, we can fold the
656 // constant into the disp field here.
657 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
658 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
659 AM.IndexReg = ShVal.Val->getOperand(0);
660 ConstantSDNode *AddVal =
661 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
Jeff Cohend41b30d2006-11-05 19:31:28 +0000662 uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
Evan Cheng25ab6902006-09-08 06:48:29 +0000663 if (isInt32(Disp))
664 AM.Disp = Disp;
665 else
666 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000667 } else {
668 AM.IndexReg = ShVal;
669 }
670 return false;
671 }
672 }
673 break;
Evan Chengec693f72005-12-08 02:01:35 +0000674
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000675 case ISD::MUL:
676 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng51a9ed92006-02-25 10:09:08 +0000677 if (!Available &&
678 AM.BaseType == X86ISelAddressMode::RegBase &&
679 AM.Base.Reg.Val == 0 &&
680 AM.IndexReg.Val == 0)
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000681 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
682 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
683 AM.Scale = unsigned(CN->getValue())-1;
684
685 SDOperand MulVal = N.Val->getOperand(0);
686 SDOperand Reg;
687
688 // Okay, we know that we have a scale by now. However, if the scaled
689 // value is an add of something and a constant, we can fold the
690 // constant into the disp field here.
691 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
692 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
693 Reg = MulVal.Val->getOperand(0);
694 ConstantSDNode *AddVal =
695 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
Evan Cheng25ab6902006-09-08 06:48:29 +0000696 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
697 if (isInt32(Disp))
698 AM.Disp = Disp;
699 else
700 Reg = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000701 } else {
702 Reg = N.Val->getOperand(0);
703 }
704
705 AM.IndexReg = AM.Base.Reg = Reg;
706 return false;
707 }
708 break;
709
710 case ISD::ADD: {
Evan Cheng51a9ed92006-02-25 10:09:08 +0000711 if (!Available) {
Evan Cheng2486af12006-02-11 02:05:36 +0000712 X86ISelAddressMode Backup = AM;
713 if (!MatchAddress(N.Val->getOperand(0), AM, false) &&
714 !MatchAddress(N.Val->getOperand(1), AM, false))
715 return false;
716 AM = Backup;
717 if (!MatchAddress(N.Val->getOperand(1), AM, false) &&
718 !MatchAddress(N.Val->getOperand(0), AM, false))
719 return false;
720 AM = Backup;
721 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000722 break;
723 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000724
725 case ISD::OR: {
726 if (!Available) {
727 X86ISelAddressMode Backup = AM;
728 // Look for (x << c1) | c2 where (c2 < c1)
729 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(0));
730 if (CN && !MatchAddress(N.Val->getOperand(1), AM, false)) {
731 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
732 AM.Disp = CN->getValue();
733 return false;
734 }
735 }
736 AM = Backup;
737 CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1));
738 if (CN && !MatchAddress(N.Val->getOperand(0), AM, false)) {
739 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
740 AM.Disp = CN->getValue();
741 return false;
742 }
743 }
744 AM = Backup;
745 }
746 break;
747 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000748 }
749
750 // Is the base register already occupied?
751 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
752 // If so, check to see if the scale index register is set.
753 if (AM.IndexReg.Val == 0) {
754 AM.IndexReg = N;
755 AM.Scale = 1;
756 return false;
757 }
758
759 // Otherwise, we cannot select it.
760 return true;
761 }
762
763 // Default, generate it as a register.
764 AM.BaseType = X86ISelAddressMode::RegBase;
765 AM.Base.Reg = N;
766 return false;
767}
768
Evan Chengec693f72005-12-08 02:01:35 +0000769/// SelectAddr - returns true if it is able pattern match an addressing mode.
770/// It returns the operands which make up the maximal addressing mode it can
771/// match by reference.
Evan Cheng0d538262006-11-08 20:34:28 +0000772bool X86DAGToDAGISel::SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
773 SDOperand &Scale, SDOperand &Index,
774 SDOperand &Disp) {
Evan Chengec693f72005-12-08 02:01:35 +0000775 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +0000776 if (MatchAddress(N, AM))
777 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000778
Evan Cheng25ab6902006-09-08 06:48:29 +0000779 MVT::ValueType VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +0000780 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Cheng7dd281b2006-02-05 05:25:07 +0000781 if (!AM.Base.Reg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000782 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +0000783 }
Evan Cheng8700e142006-01-11 06:09:51 +0000784
Evan Cheng7dd281b2006-02-05 05:25:07 +0000785 if (!AM.IndexReg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000786 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +0000787
788 getAddressOperands(AM, Base, Scale, Index, Disp);
789 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000790}
791
Chris Lattner4fe4f252006-10-11 22:09:58 +0000792/// isZeroNode - Returns true if Elt is a constant zero or a floating point
793/// constant +0.0.
794static inline bool isZeroNode(SDOperand Elt) {
795 return ((isa<ConstantSDNode>(Elt) &&
796 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
797 (isa<ConstantFPSDNode>(Elt) &&
798 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
799}
800
801
Chris Lattner3a7cd952006-10-07 21:55:32 +0000802/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
803/// match a load whose top elements are either undef or zeros. The load flavor
804/// is derived from the type of N, which is either v4f32 or v2f64.
Evan Cheng0d538262006-11-08 20:34:28 +0000805bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Cheng07e4b002006-10-16 06:34:55 +0000806 SDOperand N, SDOperand &Base,
Evan Cheng82a91642006-10-11 21:06:01 +0000807 SDOperand &Scale, SDOperand &Index,
808 SDOperand &Disp, SDOperand &InChain,
809 SDOperand &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +0000810 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +0000811 InChain = N.getOperand(0).getValue(1);
Evan Cheng07e4b002006-10-16 06:34:55 +0000812 if (ISD::isNON_EXTLoad(InChain.Val) &&
813 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +0000814 N.hasOneUse() &&
Evan Cheng0d538262006-11-08 20:34:28 +0000815 CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
Evan Cheng82a91642006-10-11 21:06:01 +0000816 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Evan Cheng0d538262006-11-08 20:34:28 +0000817 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner3a7cd952006-10-07 21:55:32 +0000818 return false;
Evan Cheng82a91642006-10-11 21:06:01 +0000819 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +0000820 return true;
821 }
822 }
Chris Lattner4fe4f252006-10-11 22:09:58 +0000823
824 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +0000825 // elements. This is a vector shuffle from the zero vector.
Chris Lattner4fe4f252006-10-11 22:09:58 +0000826 if (N.getOpcode() == ISD::VECTOR_SHUFFLE && N.Val->hasOneUse() &&
827 N.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
828 N.getOperand(1).getOpcode() == ISD::SCALAR_TO_VECTOR &&
829 N.getOperand(1).Val->hasOneUse() &&
830 ISD::isNON_EXTLoad(N.getOperand(1).getOperand(0).Val) &&
831 N.getOperand(1).getOperand(0).hasOneUse()) {
832 // Check to see if the BUILD_VECTOR is building a zero vector.
833 SDOperand BV = N.getOperand(0);
834 for (unsigned i = 0, e = BV.getNumOperands(); i != e; ++i)
835 if (!isZeroNode(BV.getOperand(i)) &&
836 BV.getOperand(i).getOpcode() != ISD::UNDEF)
837 return false; // Not a zero/undef vector.
838 // Check to see if the shuffle mask is 4/L/L/L or 2/L, where L is something
839 // from the LHS.
840 unsigned VecWidth = BV.getNumOperands();
841 SDOperand ShufMask = N.getOperand(2);
842 assert(ShufMask.getOpcode() == ISD::BUILD_VECTOR && "Invalid shuf mask!");
843 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(ShufMask.getOperand(0))) {
844 if (C->getValue() == VecWidth) {
845 for (unsigned i = 1; i != VecWidth; ++i) {
846 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF) {
847 // ok.
848 } else {
849 ConstantSDNode *C = cast<ConstantSDNode>(ShufMask.getOperand(i));
850 if (C->getValue() >= VecWidth) return false;
851 }
852 }
853 }
854
855 // Okay, this is a zero extending load. Fold it.
856 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(1).getOperand(0));
Evan Cheng0d538262006-11-08 20:34:28 +0000857 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner4fe4f252006-10-11 22:09:58 +0000858 return false;
859 OutChain = LD->getChain();
860 InChain = SDOperand(LD, 1);
861 return true;
862 }
863 }
Chris Lattner3a7cd952006-10-07 21:55:32 +0000864 return false;
865}
866
867
Evan Cheng51a9ed92006-02-25 10:09:08 +0000868/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
869/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng0d538262006-11-08 20:34:28 +0000870bool X86DAGToDAGISel::SelectLEAAddr(SDOperand Op, SDOperand N,
871 SDOperand &Base, SDOperand &Scale,
Evan Cheng51a9ed92006-02-25 10:09:08 +0000872 SDOperand &Index, SDOperand &Disp) {
873 X86ISelAddressMode AM;
874 if (MatchAddress(N, AM))
875 return false;
876
Evan Cheng25ab6902006-09-08 06:48:29 +0000877 MVT::ValueType VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +0000878 unsigned Complexity = 0;
879 if (AM.BaseType == X86ISelAddressMode::RegBase)
880 if (AM.Base.Reg.Val)
881 Complexity = 1;
882 else
Evan Cheng25ab6902006-09-08 06:48:29 +0000883 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +0000884 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
885 Complexity = 4;
886
887 if (AM.IndexReg.Val)
888 Complexity++;
889 else
Evan Cheng25ab6902006-09-08 06:48:29 +0000890 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +0000891
Evan Cheng8c03fe42006-02-28 21:13:57 +0000892 if (AM.Scale > 2)
Evan Cheng51a9ed92006-02-25 10:09:08 +0000893 Complexity += 2;
Evan Cheng8c03fe42006-02-28 21:13:57 +0000894 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg
895 else if (AM.Scale > 1)
896 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000897
898 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
899 // to a LEA. This is determined with some expermentation but is by no means
900 // optimal (especially for code size consideration). LEA is nice because of
901 // its three-address nature. Tweak the cost function again when we can run
902 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +0000903 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
904 // For X86-64, we should always use lea to materialize RIP relative
905 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +0000906 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +0000907 Complexity = 4;
908 else
909 Complexity += 2;
910 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000911
912 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
913 Complexity++;
914
915 if (Complexity > 2) {
916 getAddressOperands(AM, Base, Scale, Index, Disp);
917 return true;
918 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000919 return false;
920}
921
Evan Cheng5e351682006-02-06 06:02:33 +0000922bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
923 SDOperand &Base, SDOperand &Scale,
924 SDOperand &Index, SDOperand &Disp) {
Evan Cheng466685d2006-10-09 20:57:25 +0000925 if (ISD::isNON_EXTLoad(N.Val) &&
Evan Cheng5e351682006-02-06 06:02:33 +0000926 N.hasOneUse() &&
Evan Cheng27e1fe92006-10-14 08:33:25 +0000927 CanBeFoldedBy(N.Val, P.Val, P.Val))
Evan Cheng0d538262006-11-08 20:34:28 +0000928 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
Evan Cheng0114e942006-01-06 20:36:21 +0000929 return false;
930}
931
Evan Cheng7ccced62006-02-18 00:15:05 +0000932/// getGlobalBaseReg - Output the instructions required to put the
933/// base address to use for accessing globals into a register.
934///
Evan Cheng9ade2182006-08-26 05:34:46 +0000935SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng25ab6902006-09-08 06:48:29 +0000936 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Evan Cheng7ccced62006-02-18 00:15:05 +0000937 if (!GlobalBaseReg) {
938 // Insert the set of GlobalBaseReg into the first MBB of the function
939 MachineBasicBlock &FirstMBB = BB->getParent()->front();
940 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
941 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000942 unsigned PC = RegMap->createVirtualRegister(X86::GR32RegisterClass);
943
Evan Chengc0f64ff2006-11-27 23:37:22 +0000944 const TargetInstrInfo *TII = TM.getInstrInfo();
945 BuildMI(FirstMBB, MBBI, TII->get(X86::MovePCtoStack));
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000946 BuildMI(FirstMBB, MBBI, TII->get(X86::POP32r), PC);
947
948 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
949 // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
Evan Cheng706535d2007-01-22 21:34:25 +0000950 if (TM.getRelocationModel() == Reloc::PIC_ &&
951 Subtarget->isPICStyleGOT()) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000952 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
953 BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg).
954 addReg(PC).
955 addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
956 } else {
957 GlobalBaseReg = PC;
958 }
959
Evan Cheng7ccced62006-02-18 00:15:05 +0000960 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000961 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
Evan Cheng7ccced62006-02-18 00:15:05 +0000962}
963
Evan Chengb245d922006-05-20 01:36:52 +0000964static SDNode *FindCallStartFromCall(SDNode *Node) {
965 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
966 assert(Node->getOperand(0).getValueType() == MVT::Other &&
967 "Node doesn't have a token chain argument!");
968 return FindCallStartFromCall(Node->getOperand(0).Val);
969}
970
Evan Cheng9ade2182006-08-26 05:34:46 +0000971SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Chengdef941b2005-12-15 01:02:48 +0000972 SDNode *Node = N.Val;
973 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +0000974 unsigned Opc, MOpc;
975 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +0000976
Evan Chengf597dc72006-02-10 22:24:32 +0000977#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000978 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +0000979 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +0000980 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000981 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000982#endif
983
Evan Cheng34167212006-02-09 00:37:58 +0000984 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengf597dc72006-02-10 22:24:32 +0000985#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000986 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +0000987 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +0000988 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000989 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000990#endif
Evan Cheng64a752f2006-08-11 09:08:15 +0000991 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +0000992 }
Evan Cheng38262ca2006-01-11 22:15:18 +0000993
Evan Cheng0114e942006-01-06 20:36:21 +0000994 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000995 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +0000996 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +0000997 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +0000998
Evan Cheng51a9ed92006-02-25 10:09:08 +0000999 case ISD::ADD: {
1000 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
1001 // code and is matched first so to prevent it from being turned into
1002 // LEA32r X+c.
Evan Cheng25ab6902006-09-08 06:48:29 +00001003 // In 64-bit mode, use LEA to take advantage of RIP-relative addressing.
1004 MVT::ValueType PtrVT = TLI.getPointerTy();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001005 SDOperand N0 = N.getOperand(0);
1006 SDOperand N1 = N.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00001007 if (N.Val->getValueType(0) == PtrVT &&
Evan Cheng19f2ffc2006-12-05 04:01:03 +00001008 N0.getOpcode() == X86ISD::Wrapper &&
Evan Cheng51a9ed92006-02-25 10:09:08 +00001009 N1.getOpcode() == ISD::Constant) {
1010 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1011 SDOperand C(0, 0);
1012 // TODO: handle ExternalSymbolSDNode.
1013 if (GlobalAddressSDNode *G =
1014 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001015 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001016 G->getOffset() + Offset);
1017 } else if (ConstantPoolSDNode *CP =
1018 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Chengc356a572006-09-12 21:04:05 +00001019 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001020 CP->getAlignment(),
1021 CP->getOffset()+Offset);
1022 }
1023
Evan Cheng25ab6902006-09-08 06:48:29 +00001024 if (C.Val) {
1025 if (Subtarget->is64Bit()) {
1026 SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
1027 CurDAG->getRegister(0, PtrVT), C };
1028 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1029 } else
1030 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1031 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001032 }
1033
1034 // Other cases are handled by auto-generated code.
1035 break;
Evan Chenga0ea0532006-02-23 02:43:52 +00001036 }
Evan Cheng020d2e82006-02-23 20:41:18 +00001037
Evan Cheng0114e942006-01-06 20:36:21 +00001038 case ISD::MULHU:
1039 case ISD::MULHS: {
1040 if (Opcode == ISD::MULHU)
1041 switch (NVT) {
1042 default: assert(0 && "Unsupported VT!");
1043 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1044 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1045 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001046 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001047 }
1048 else
1049 switch (NVT) {
1050 default: assert(0 && "Unsupported VT!");
1051 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1052 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1053 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001054 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001055 }
1056
1057 unsigned LoReg, HiReg;
1058 switch (NVT) {
1059 default: assert(0 && "Unsupported VT!");
1060 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1061 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1062 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001063 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001064 }
1065
1066 SDOperand N0 = Node->getOperand(0);
1067 SDOperand N1 = Node->getOperand(1);
1068
1069 bool foldedLoad = false;
1070 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng5e351682006-02-06 06:02:33 +00001071 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +00001072 // MULHU and MULHS are commmutative
1073 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +00001074 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +00001075 if (foldedLoad) {
1076 N0 = Node->getOperand(1);
1077 N1 = Node->getOperand(0);
1078 }
1079 }
1080
Evan Cheng34167212006-02-09 00:37:58 +00001081 SDOperand Chain;
Evan Cheng04699902006-08-26 01:05:16 +00001082 if (foldedLoad) {
1083 Chain = N1.getOperand(0);
1084 AddToISelQueue(Chain);
1085 } else
Evan Cheng34167212006-02-09 00:37:58 +00001086 Chain = CurDAG->getEntryNode();
Evan Cheng0114e942006-01-06 20:36:21 +00001087
Evan Cheng34167212006-02-09 00:37:58 +00001088 SDOperand InFlag(0, 0);
Evan Cheng04699902006-08-26 01:05:16 +00001089 AddToISelQueue(N0);
Evan Cheng0114e942006-01-06 20:36:21 +00001090 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng34167212006-02-09 00:37:58 +00001091 N0, InFlag);
Evan Cheng0114e942006-01-06 20:36:21 +00001092 InFlag = Chain.getValue(1);
1093
1094 if (foldedLoad) {
Evan Cheng04699902006-08-26 01:05:16 +00001095 AddToISelQueue(Tmp0);
1096 AddToISelQueue(Tmp1);
1097 AddToISelQueue(Tmp2);
1098 AddToISelQueue(Tmp3);
Evan Cheng0b828e02006-08-27 08:14:06 +00001099 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001100 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001101 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001102 Chain = SDOperand(CNode, 0);
1103 InFlag = SDOperand(CNode, 1);
Evan Cheng0114e942006-01-06 20:36:21 +00001104 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001105 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001106 InFlag =
1107 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001108 }
1109
Evan Cheng9ade2182006-08-26 05:34:46 +00001110 SDOperand Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
Evan Cheng2ef88a02006-08-07 22:28:20 +00001111 ReplaceUses(N.getValue(0), Result);
1112 if (foldedLoad)
1113 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Cheng34167212006-02-09 00:37:58 +00001114
Evan Chengf597dc72006-02-10 22:24:32 +00001115#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001116 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Chengf597dc72006-02-10 22:24:32 +00001117 DEBUG(Result.Val->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001118 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001119 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001120#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001121 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001122 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001123
Evan Cheng948f3432006-01-06 23:19:29 +00001124 case ISD::SDIV:
1125 case ISD::UDIV:
1126 case ISD::SREM:
1127 case ISD::UREM: {
1128 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
1129 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
1130 if (!isSigned)
1131 switch (NVT) {
1132 default: assert(0 && "Unsupported VT!");
1133 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1134 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1135 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001136 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001137 }
1138 else
1139 switch (NVT) {
1140 default: assert(0 && "Unsupported VT!");
1141 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1142 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1143 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001144 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001145 }
1146
1147 unsigned LoReg, HiReg;
1148 unsigned ClrOpcode, SExtOpcode;
1149 switch (NVT) {
1150 default: assert(0 && "Unsupported VT!");
1151 case MVT::i8:
1152 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001153 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001154 SExtOpcode = X86::CBW;
1155 break;
1156 case MVT::i16:
1157 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001158 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001159 SExtOpcode = X86::CWD;
1160 break;
1161 case MVT::i32:
1162 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001163 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001164 SExtOpcode = X86::CDQ;
1165 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001166 case MVT::i64:
1167 LoReg = X86::RAX; HiReg = X86::RDX;
1168 ClrOpcode = X86::MOV64r0;
1169 SExtOpcode = X86::CQO;
1170 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001171 }
1172
1173 SDOperand N0 = Node->getOperand(0);
1174 SDOperand N1 = Node->getOperand(1);
Evan Cheng34167212006-02-09 00:37:58 +00001175 SDOperand InFlag(0, 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001176 if (NVT == MVT::i8 && !isSigned) {
1177 // Special case for div8, just use a move with zero extension to AX to
1178 // clear the upper 8 bits (AH).
1179 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
1180 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
1181 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
1182 AddToISelQueue(N0.getOperand(0));
1183 AddToISelQueue(Tmp0);
1184 AddToISelQueue(Tmp1);
1185 AddToISelQueue(Tmp2);
1186 AddToISelQueue(Tmp3);
1187 Move =
1188 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
1189 Ops, 5), 0);
1190 Chain = Move.getValue(1);
1191 ReplaceUses(N0.getValue(1), Chain);
1192 } else {
1193 AddToISelQueue(N0);
1194 Move =
1195 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
1196 Chain = CurDAG->getEntryNode();
1197 }
1198 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, InFlag);
Evan Cheng948f3432006-01-06 23:19:29 +00001199 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001200 } else {
1201 AddToISelQueue(N0);
1202 InFlag =
1203 CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg, N0,
1204 InFlag).getValue(1);
1205 if (isSigned) {
1206 // Sign extend the low part into the high part.
1207 InFlag =
1208 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
1209 } else {
1210 // Zero out the high part, effectively zero extending the input.
1211 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
1212 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg, ClrNode,
1213 InFlag).getValue(1);
1214 }
Evan Cheng948f3432006-01-06 23:19:29 +00001215 }
1216
Evan Chengb1409ce2006-11-17 22:10:14 +00001217 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Chain;
1218 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +00001219 if (foldedLoad) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001220 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001221 AddToISelQueue(Tmp0);
1222 AddToISelQueue(Tmp1);
1223 AddToISelQueue(Tmp2);
1224 AddToISelQueue(Tmp3);
Evan Chengb1409ce2006-11-17 22:10:14 +00001225 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001226 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001227 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001228 Chain = SDOperand(CNode, 0);
1229 InFlag = SDOperand(CNode, 1);
Evan Cheng948f3432006-01-06 23:19:29 +00001230 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001231 AddToISelQueue(N1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001232 Chain = CurDAG->getEntryNode();
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001233 InFlag =
1234 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001235 }
1236
Evan Chengb1409ce2006-11-17 22:10:14 +00001237 SDOperand Result =
1238 CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg, NVT, InFlag);
Evan Cheng2ef88a02006-08-07 22:28:20 +00001239 ReplaceUses(N.getValue(0), Result);
1240 if (foldedLoad)
1241 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Chengf597dc72006-02-10 22:24:32 +00001242
1243#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001244 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Chengf597dc72006-02-10 22:24:32 +00001245 DEBUG(Result.Val->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001246 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001247 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001248#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001249
1250 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001251 }
Evan Cheng403be7e2006-05-08 08:01:26 +00001252
1253 case ISD::TRUNCATE: {
Evan Cheng25ab6902006-09-08 06:48:29 +00001254 if (!Subtarget->is64Bit() && NVT == MVT::i8) {
Evan Cheng403be7e2006-05-08 08:01:26 +00001255 unsigned Opc2;
1256 MVT::ValueType VT;
1257 switch (Node->getOperand(0).getValueType()) {
1258 default: assert(0 && "Unknown truncate!");
1259 case MVT::i16:
1260 Opc = X86::MOV16to16_;
1261 VT = MVT::i16;
Evan Cheng25ab6902006-09-08 06:48:29 +00001262 Opc2 = X86::TRUNC_16_to8;
Evan Cheng403be7e2006-05-08 08:01:26 +00001263 break;
1264 case MVT::i32:
1265 Opc = X86::MOV32to32_;
1266 VT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00001267 Opc2 = X86::TRUNC_32_to8;
Evan Cheng403be7e2006-05-08 08:01:26 +00001268 break;
1269 }
1270
Evan Cheng04699902006-08-26 01:05:16 +00001271 AddToISelQueue(Node->getOperand(0));
1272 SDOperand Tmp =
1273 SDOperand(CurDAG->getTargetNode(Opc, VT, Node->getOperand(0)), 0);
Evan Cheng9ade2182006-08-26 05:34:46 +00001274 SDNode *ResNode = CurDAG->getTargetNode(Opc2, NVT, Tmp);
Evan Cheng403be7e2006-05-08 08:01:26 +00001275
1276#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001277 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001278 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001279 DOUT << "\n";
Evan Cheng403be7e2006-05-08 08:01:26 +00001280 Indent -= 2;
1281#endif
Evan Cheng9ade2182006-08-26 05:34:46 +00001282 return ResNode;
Evan Cheng403be7e2006-05-08 08:01:26 +00001283 }
Evan Cheng6b2e2542006-05-20 07:44:28 +00001284
1285 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001286 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001287 }
1288
Evan Cheng9ade2182006-08-26 05:34:46 +00001289 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001290
Evan Chengf597dc72006-02-10 22:24:32 +00001291#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001292 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001293 if (ResNode == NULL || ResNode == N.Val)
1294 DEBUG(N.Val->dump(CurDAG));
1295 else
1296 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001297 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001298 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001299#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001300
1301 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001302}
1303
Chris Lattnerc0bad572006-06-08 18:03:49 +00001304bool X86DAGToDAGISel::
1305SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1306 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1307 SDOperand Op0, Op1, Op2, Op3;
1308 switch (ConstraintCode) {
1309 case 'o': // offsetable ??
1310 case 'v': // not offsetable ??
1311 default: return true;
1312 case 'm': // memory
Evan Cheng0d538262006-11-08 20:34:28 +00001313 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001314 return true;
1315 break;
1316 }
1317
Evan Cheng04699902006-08-26 01:05:16 +00001318 OutOps.push_back(Op0);
1319 OutOps.push_back(Op1);
1320 OutOps.push_back(Op2);
1321 OutOps.push_back(Op3);
1322 AddToISelQueue(Op0);
1323 AddToISelQueue(Op1);
1324 AddToISelQueue(Op2);
1325 AddToISelQueue(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001326 return false;
1327}
1328
Chris Lattnerc961eea2005-11-16 01:54:32 +00001329/// createX86ISelDag - This pass converts a legalized DAG into a
1330/// X86-specific DAG, ready for instruction scheduling.
1331///
Evan Chenge50794a2006-08-29 18:28:33 +00001332FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1333 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001334}