blob: 9c1dcf93b256f04e3d344ecf6973138f85a17272 [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 &&
Chris Lattner62412262007-02-04 20:18:17 +0000680 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 }
Chris Lattner62412262007-02-04 20:18:17 +0000708 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000709 break;
710
Chris Lattner62412262007-02-04 20:18:17 +0000711 case ISD::ADD:
Evan Cheng51a9ed92006-02-25 10:09:08 +0000712 if (!Available) {
Evan Cheng2486af12006-02-11 02:05:36 +0000713 X86ISelAddressMode Backup = AM;
714 if (!MatchAddress(N.Val->getOperand(0), AM, false) &&
715 !MatchAddress(N.Val->getOperand(1), AM, false))
716 return false;
717 AM = Backup;
718 if (!MatchAddress(N.Val->getOperand(1), AM, false) &&
719 !MatchAddress(N.Val->getOperand(0), AM, false))
720 return false;
721 AM = Backup;
722 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000723 break;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000724
Chris Lattner62412262007-02-04 20:18:17 +0000725 case ISD::OR:
726 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Evan Chenge6ad27e2006-05-30 06:59:36 +0000727 if (!Available) {
Chris Lattner62412262007-02-04 20:18:17 +0000728 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
729 X86ISelAddressMode Backup = AM;
730 // Start with the LHS as an addr mode.
731 if (!MatchAddress(N.getOperand(0), AM, false) &&
732 // Address could not have picked a GV address for the displacement.
733 AM.GV == NULL &&
734 // On x86-64, the resultant disp must fit in 32-bits.
735 isInt32(AM.Disp + CN->getSignExtended()) &&
736 // Check to see if the LHS & C is zero.
737 TLI.MaskedValueIsZero(N.getOperand(0), CN->getValue())) {
738 AM.Disp += CN->getValue();
Evan Chenge6ad27e2006-05-30 06:59:36 +0000739 return false;
740 }
Chris Lattner62412262007-02-04 20:18:17 +0000741 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +0000742 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000743 }
744 break;
745 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000746
747 // Is the base register already occupied?
748 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
749 // If so, check to see if the scale index register is set.
750 if (AM.IndexReg.Val == 0) {
751 AM.IndexReg = N;
752 AM.Scale = 1;
753 return false;
754 }
755
756 // Otherwise, we cannot select it.
757 return true;
758 }
759
760 // Default, generate it as a register.
761 AM.BaseType = X86ISelAddressMode::RegBase;
762 AM.Base.Reg = N;
763 return false;
764}
765
Evan Chengec693f72005-12-08 02:01:35 +0000766/// SelectAddr - returns true if it is able pattern match an addressing mode.
767/// It returns the operands which make up the maximal addressing mode it can
768/// match by reference.
Evan Cheng0d538262006-11-08 20:34:28 +0000769bool X86DAGToDAGISel::SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
770 SDOperand &Scale, SDOperand &Index,
771 SDOperand &Disp) {
Evan Chengec693f72005-12-08 02:01:35 +0000772 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +0000773 if (MatchAddress(N, AM))
774 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000775
Evan Cheng25ab6902006-09-08 06:48:29 +0000776 MVT::ValueType VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +0000777 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Cheng7dd281b2006-02-05 05:25:07 +0000778 if (!AM.Base.Reg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000779 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +0000780 }
Evan Cheng8700e142006-01-11 06:09:51 +0000781
Evan Cheng7dd281b2006-02-05 05:25:07 +0000782 if (!AM.IndexReg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000783 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +0000784
785 getAddressOperands(AM, Base, Scale, Index, Disp);
786 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000787}
788
Chris Lattner4fe4f252006-10-11 22:09:58 +0000789/// isZeroNode - Returns true if Elt is a constant zero or a floating point
790/// constant +0.0.
791static inline bool isZeroNode(SDOperand Elt) {
792 return ((isa<ConstantSDNode>(Elt) &&
793 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
794 (isa<ConstantFPSDNode>(Elt) &&
795 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
796}
797
798
Chris Lattner3a7cd952006-10-07 21:55:32 +0000799/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
800/// match a load whose top elements are either undef or zeros. The load flavor
801/// is derived from the type of N, which is either v4f32 or v2f64.
Evan Cheng0d538262006-11-08 20:34:28 +0000802bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Cheng07e4b002006-10-16 06:34:55 +0000803 SDOperand N, SDOperand &Base,
Evan Cheng82a91642006-10-11 21:06:01 +0000804 SDOperand &Scale, SDOperand &Index,
805 SDOperand &Disp, SDOperand &InChain,
806 SDOperand &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +0000807 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +0000808 InChain = N.getOperand(0).getValue(1);
Evan Cheng07e4b002006-10-16 06:34:55 +0000809 if (ISD::isNON_EXTLoad(InChain.Val) &&
810 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +0000811 N.hasOneUse() &&
Evan Cheng0d538262006-11-08 20:34:28 +0000812 CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
Evan Cheng82a91642006-10-11 21:06:01 +0000813 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Evan Cheng0d538262006-11-08 20:34:28 +0000814 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner3a7cd952006-10-07 21:55:32 +0000815 return false;
Evan Cheng82a91642006-10-11 21:06:01 +0000816 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +0000817 return true;
818 }
819 }
Chris Lattner4fe4f252006-10-11 22:09:58 +0000820
821 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +0000822 // elements. This is a vector shuffle from the zero vector.
Chris Lattner4fe4f252006-10-11 22:09:58 +0000823 if (N.getOpcode() == ISD::VECTOR_SHUFFLE && N.Val->hasOneUse() &&
824 N.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
825 N.getOperand(1).getOpcode() == ISD::SCALAR_TO_VECTOR &&
826 N.getOperand(1).Val->hasOneUse() &&
827 ISD::isNON_EXTLoad(N.getOperand(1).getOperand(0).Val) &&
828 N.getOperand(1).getOperand(0).hasOneUse()) {
829 // Check to see if the BUILD_VECTOR is building a zero vector.
830 SDOperand BV = N.getOperand(0);
831 for (unsigned i = 0, e = BV.getNumOperands(); i != e; ++i)
832 if (!isZeroNode(BV.getOperand(i)) &&
833 BV.getOperand(i).getOpcode() != ISD::UNDEF)
834 return false; // Not a zero/undef vector.
835 // Check to see if the shuffle mask is 4/L/L/L or 2/L, where L is something
836 // from the LHS.
837 unsigned VecWidth = BV.getNumOperands();
838 SDOperand ShufMask = N.getOperand(2);
839 assert(ShufMask.getOpcode() == ISD::BUILD_VECTOR && "Invalid shuf mask!");
840 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(ShufMask.getOperand(0))) {
841 if (C->getValue() == VecWidth) {
842 for (unsigned i = 1; i != VecWidth; ++i) {
843 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF) {
844 // ok.
845 } else {
846 ConstantSDNode *C = cast<ConstantSDNode>(ShufMask.getOperand(i));
847 if (C->getValue() >= VecWidth) return false;
848 }
849 }
850 }
851
852 // Okay, this is a zero extending load. Fold it.
853 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(1).getOperand(0));
Evan Cheng0d538262006-11-08 20:34:28 +0000854 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner4fe4f252006-10-11 22:09:58 +0000855 return false;
856 OutChain = LD->getChain();
857 InChain = SDOperand(LD, 1);
858 return true;
859 }
860 }
Chris Lattner3a7cd952006-10-07 21:55:32 +0000861 return false;
862}
863
864
Evan Cheng51a9ed92006-02-25 10:09:08 +0000865/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
866/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng0d538262006-11-08 20:34:28 +0000867bool X86DAGToDAGISel::SelectLEAAddr(SDOperand Op, SDOperand N,
868 SDOperand &Base, SDOperand &Scale,
Evan Cheng51a9ed92006-02-25 10:09:08 +0000869 SDOperand &Index, SDOperand &Disp) {
870 X86ISelAddressMode AM;
871 if (MatchAddress(N, AM))
872 return false;
873
Evan Cheng25ab6902006-09-08 06:48:29 +0000874 MVT::ValueType VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +0000875 unsigned Complexity = 0;
876 if (AM.BaseType == X86ISelAddressMode::RegBase)
877 if (AM.Base.Reg.Val)
878 Complexity = 1;
879 else
Evan Cheng25ab6902006-09-08 06:48:29 +0000880 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +0000881 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
882 Complexity = 4;
883
884 if (AM.IndexReg.Val)
885 Complexity++;
886 else
Evan Cheng25ab6902006-09-08 06:48:29 +0000887 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +0000888
Chris Lattnera16b7cb2007-03-20 06:08:29 +0000889 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
890 // a simple shift.
891 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +0000892 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000893
894 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
895 // to a LEA. This is determined with some expermentation but is by no means
896 // optimal (especially for code size consideration). LEA is nice because of
897 // its three-address nature. Tweak the cost function again when we can run
898 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +0000899 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
900 // For X86-64, we should always use lea to materialize RIP relative
901 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +0000902 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +0000903 Complexity = 4;
904 else
905 Complexity += 2;
906 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000907
908 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
909 Complexity++;
910
911 if (Complexity > 2) {
912 getAddressOperands(AM, Base, Scale, Index, Disp);
913 return true;
914 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000915 return false;
916}
917
Evan Cheng5e351682006-02-06 06:02:33 +0000918bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
919 SDOperand &Base, SDOperand &Scale,
920 SDOperand &Index, SDOperand &Disp) {
Evan Cheng466685d2006-10-09 20:57:25 +0000921 if (ISD::isNON_EXTLoad(N.Val) &&
Evan Cheng5e351682006-02-06 06:02:33 +0000922 N.hasOneUse() &&
Evan Cheng27e1fe92006-10-14 08:33:25 +0000923 CanBeFoldedBy(N.Val, P.Val, P.Val))
Evan Cheng0d538262006-11-08 20:34:28 +0000924 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
Evan Cheng0114e942006-01-06 20:36:21 +0000925 return false;
926}
927
Evan Cheng7ccced62006-02-18 00:15:05 +0000928/// getGlobalBaseReg - Output the instructions required to put the
929/// base address to use for accessing globals into a register.
930///
Evan Cheng9ade2182006-08-26 05:34:46 +0000931SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng25ab6902006-09-08 06:48:29 +0000932 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Evan Cheng7ccced62006-02-18 00:15:05 +0000933 if (!GlobalBaseReg) {
934 // Insert the set of GlobalBaseReg into the first MBB of the function
935 MachineBasicBlock &FirstMBB = BB->getParent()->front();
936 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
937 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000938 unsigned PC = RegMap->createVirtualRegister(X86::GR32RegisterClass);
939
Evan Chengc0f64ff2006-11-27 23:37:22 +0000940 const TargetInstrInfo *TII = TM.getInstrInfo();
941 BuildMI(FirstMBB, MBBI, TII->get(X86::MovePCtoStack));
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000942 BuildMI(FirstMBB, MBBI, TII->get(X86::POP32r), PC);
943
944 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
945 // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
Evan Cheng706535d2007-01-22 21:34:25 +0000946 if (TM.getRelocationModel() == Reloc::PIC_ &&
947 Subtarget->isPICStyleGOT()) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000948 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
949 BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg).
950 addReg(PC).
951 addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
952 } else {
953 GlobalBaseReg = PC;
954 }
955
Evan Cheng7ccced62006-02-18 00:15:05 +0000956 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000957 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
Evan Cheng7ccced62006-02-18 00:15:05 +0000958}
959
Evan Chengb245d922006-05-20 01:36:52 +0000960static SDNode *FindCallStartFromCall(SDNode *Node) {
961 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
962 assert(Node->getOperand(0).getValueType() == MVT::Other &&
963 "Node doesn't have a token chain argument!");
964 return FindCallStartFromCall(Node->getOperand(0).Val);
965}
966
Evan Cheng9ade2182006-08-26 05:34:46 +0000967SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Chengdef941b2005-12-15 01:02:48 +0000968 SDNode *Node = N.Val;
969 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +0000970 unsigned Opc, MOpc;
971 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +0000972
Evan Chengf597dc72006-02-10 22:24:32 +0000973#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000974 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +0000975 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +0000976 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000977 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000978#endif
979
Evan Cheng34167212006-02-09 00:37:58 +0000980 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengf597dc72006-02-10 22:24:32 +0000981#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000982 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +0000983 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +0000984 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000985 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000986#endif
Evan Cheng64a752f2006-08-11 09:08:15 +0000987 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +0000988 }
Evan Cheng38262ca2006-01-11 22:15:18 +0000989
Evan Cheng0114e942006-01-06 20:36:21 +0000990 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000991 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +0000992 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +0000993 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +0000994
Evan Cheng51a9ed92006-02-25 10:09:08 +0000995 case ISD::ADD: {
996 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
997 // code and is matched first so to prevent it from being turned into
998 // LEA32r X+c.
Evan Cheng25ab6902006-09-08 06:48:29 +0000999 // In 64-bit mode, use LEA to take advantage of RIP-relative addressing.
1000 MVT::ValueType PtrVT = TLI.getPointerTy();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001001 SDOperand N0 = N.getOperand(0);
1002 SDOperand N1 = N.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00001003 if (N.Val->getValueType(0) == PtrVT &&
Evan Cheng19f2ffc2006-12-05 04:01:03 +00001004 N0.getOpcode() == X86ISD::Wrapper &&
Evan Cheng51a9ed92006-02-25 10:09:08 +00001005 N1.getOpcode() == ISD::Constant) {
1006 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1007 SDOperand C(0, 0);
1008 // TODO: handle ExternalSymbolSDNode.
1009 if (GlobalAddressSDNode *G =
1010 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001011 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001012 G->getOffset() + Offset);
1013 } else if (ConstantPoolSDNode *CP =
1014 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Chengc356a572006-09-12 21:04:05 +00001015 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001016 CP->getAlignment(),
1017 CP->getOffset()+Offset);
1018 }
1019
Evan Cheng25ab6902006-09-08 06:48:29 +00001020 if (C.Val) {
1021 if (Subtarget->is64Bit()) {
1022 SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
1023 CurDAG->getRegister(0, PtrVT), C };
1024 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1025 } else
1026 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1027 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001028 }
1029
1030 // Other cases are handled by auto-generated code.
1031 break;
Evan Chenga0ea0532006-02-23 02:43:52 +00001032 }
Evan Cheng020d2e82006-02-23 20:41:18 +00001033
Evan Cheng0114e942006-01-06 20:36:21 +00001034 case ISD::MULHU:
1035 case ISD::MULHS: {
1036 if (Opcode == ISD::MULHU)
1037 switch (NVT) {
1038 default: assert(0 && "Unsupported VT!");
1039 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1040 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1041 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001042 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001043 }
1044 else
1045 switch (NVT) {
1046 default: assert(0 && "Unsupported VT!");
1047 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1048 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1049 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001050 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001051 }
1052
1053 unsigned LoReg, HiReg;
1054 switch (NVT) {
1055 default: assert(0 && "Unsupported VT!");
1056 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1057 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1058 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001059 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001060 }
1061
1062 SDOperand N0 = Node->getOperand(0);
1063 SDOperand N1 = Node->getOperand(1);
1064
1065 bool foldedLoad = false;
1066 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng5e351682006-02-06 06:02:33 +00001067 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +00001068 // MULHU and MULHS are commmutative
1069 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +00001070 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +00001071 if (foldedLoad) {
1072 N0 = Node->getOperand(1);
1073 N1 = Node->getOperand(0);
1074 }
1075 }
1076
Evan Cheng34167212006-02-09 00:37:58 +00001077 SDOperand Chain;
Evan Cheng04699902006-08-26 01:05:16 +00001078 if (foldedLoad) {
1079 Chain = N1.getOperand(0);
1080 AddToISelQueue(Chain);
1081 } else
Evan Cheng34167212006-02-09 00:37:58 +00001082 Chain = CurDAG->getEntryNode();
Evan Cheng0114e942006-01-06 20:36:21 +00001083
Evan Cheng34167212006-02-09 00:37:58 +00001084 SDOperand InFlag(0, 0);
Evan Cheng04699902006-08-26 01:05:16 +00001085 AddToISelQueue(N0);
Evan Cheng0114e942006-01-06 20:36:21 +00001086 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng34167212006-02-09 00:37:58 +00001087 N0, InFlag);
Evan Cheng0114e942006-01-06 20:36:21 +00001088 InFlag = Chain.getValue(1);
1089
1090 if (foldedLoad) {
Evan Cheng04699902006-08-26 01:05:16 +00001091 AddToISelQueue(Tmp0);
1092 AddToISelQueue(Tmp1);
1093 AddToISelQueue(Tmp2);
1094 AddToISelQueue(Tmp3);
Evan Cheng0b828e02006-08-27 08:14:06 +00001095 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001096 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001097 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001098 Chain = SDOperand(CNode, 0);
1099 InFlag = SDOperand(CNode, 1);
Evan Cheng0114e942006-01-06 20:36:21 +00001100 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001101 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001102 InFlag =
1103 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001104 }
1105
Evan Cheng9ade2182006-08-26 05:34:46 +00001106 SDOperand Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
Evan Cheng2ef88a02006-08-07 22:28:20 +00001107 ReplaceUses(N.getValue(0), Result);
1108 if (foldedLoad)
1109 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Cheng34167212006-02-09 00:37:58 +00001110
Evan Chengf597dc72006-02-10 22:24:32 +00001111#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001112 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Chengf597dc72006-02-10 22:24:32 +00001113 DEBUG(Result.Val->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001114 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001115 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001116#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001117 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001118 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001119
Evan Cheng948f3432006-01-06 23:19:29 +00001120 case ISD::SDIV:
1121 case ISD::UDIV:
1122 case ISD::SREM:
1123 case ISD::UREM: {
1124 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
1125 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
1126 if (!isSigned)
1127 switch (NVT) {
1128 default: assert(0 && "Unsupported VT!");
1129 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1130 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1131 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001132 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001133 }
1134 else
1135 switch (NVT) {
1136 default: assert(0 && "Unsupported VT!");
1137 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1138 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1139 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001140 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001141 }
1142
1143 unsigned LoReg, HiReg;
1144 unsigned ClrOpcode, SExtOpcode;
1145 switch (NVT) {
1146 default: assert(0 && "Unsupported VT!");
1147 case MVT::i8:
1148 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001149 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001150 SExtOpcode = X86::CBW;
1151 break;
1152 case MVT::i16:
1153 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001154 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001155 SExtOpcode = X86::CWD;
1156 break;
1157 case MVT::i32:
1158 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001159 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001160 SExtOpcode = X86::CDQ;
1161 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001162 case MVT::i64:
1163 LoReg = X86::RAX; HiReg = X86::RDX;
1164 ClrOpcode = X86::MOV64r0;
1165 SExtOpcode = X86::CQO;
1166 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001167 }
1168
1169 SDOperand N0 = Node->getOperand(0);
1170 SDOperand N1 = Node->getOperand(1);
Evan Cheng34167212006-02-09 00:37:58 +00001171 SDOperand InFlag(0, 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001172 if (NVT == MVT::i8 && !isSigned) {
1173 // Special case for div8, just use a move with zero extension to AX to
1174 // clear the upper 8 bits (AH).
1175 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
1176 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
1177 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
1178 AddToISelQueue(N0.getOperand(0));
1179 AddToISelQueue(Tmp0);
1180 AddToISelQueue(Tmp1);
1181 AddToISelQueue(Tmp2);
1182 AddToISelQueue(Tmp3);
1183 Move =
1184 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
1185 Ops, 5), 0);
1186 Chain = Move.getValue(1);
1187 ReplaceUses(N0.getValue(1), Chain);
1188 } else {
1189 AddToISelQueue(N0);
1190 Move =
1191 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
1192 Chain = CurDAG->getEntryNode();
1193 }
1194 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, InFlag);
Evan Cheng948f3432006-01-06 23:19:29 +00001195 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001196 } else {
1197 AddToISelQueue(N0);
1198 InFlag =
1199 CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg, N0,
1200 InFlag).getValue(1);
1201 if (isSigned) {
1202 // Sign extend the low part into the high part.
1203 InFlag =
1204 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
1205 } else {
1206 // Zero out the high part, effectively zero extending the input.
1207 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
1208 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg, ClrNode,
1209 InFlag).getValue(1);
1210 }
Evan Cheng948f3432006-01-06 23:19:29 +00001211 }
1212
Evan Chengb1409ce2006-11-17 22:10:14 +00001213 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Chain;
1214 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +00001215 if (foldedLoad) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001216 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001217 AddToISelQueue(Tmp0);
1218 AddToISelQueue(Tmp1);
1219 AddToISelQueue(Tmp2);
1220 AddToISelQueue(Tmp3);
Evan Chengb1409ce2006-11-17 22:10:14 +00001221 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001222 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001223 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001224 Chain = SDOperand(CNode, 0);
1225 InFlag = SDOperand(CNode, 1);
Evan Cheng948f3432006-01-06 23:19:29 +00001226 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001227 AddToISelQueue(N1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001228 Chain = CurDAG->getEntryNode();
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001229 InFlag =
1230 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001231 }
1232
Evan Chengb1409ce2006-11-17 22:10:14 +00001233 SDOperand Result =
1234 CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg, NVT, InFlag);
Evan Cheng2ef88a02006-08-07 22:28:20 +00001235 ReplaceUses(N.getValue(0), Result);
1236 if (foldedLoad)
1237 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Chengf597dc72006-02-10 22:24:32 +00001238
1239#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001240 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Chengf597dc72006-02-10 22:24:32 +00001241 DEBUG(Result.Val->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001242 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001243 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001244#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001245
1246 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001247 }
Evan Cheng403be7e2006-05-08 08:01:26 +00001248
1249 case ISD::TRUNCATE: {
Evan Cheng25ab6902006-09-08 06:48:29 +00001250 if (!Subtarget->is64Bit() && NVT == MVT::i8) {
Evan Cheng403be7e2006-05-08 08:01:26 +00001251 unsigned Opc2;
1252 MVT::ValueType VT;
1253 switch (Node->getOperand(0).getValueType()) {
1254 default: assert(0 && "Unknown truncate!");
1255 case MVT::i16:
1256 Opc = X86::MOV16to16_;
1257 VT = MVT::i16;
Evan Cheng25ab6902006-09-08 06:48:29 +00001258 Opc2 = X86::TRUNC_16_to8;
Evan Cheng403be7e2006-05-08 08:01:26 +00001259 break;
1260 case MVT::i32:
1261 Opc = X86::MOV32to32_;
1262 VT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00001263 Opc2 = X86::TRUNC_32_to8;
Evan Cheng403be7e2006-05-08 08:01:26 +00001264 break;
1265 }
1266
Evan Cheng04699902006-08-26 01:05:16 +00001267 AddToISelQueue(Node->getOperand(0));
1268 SDOperand Tmp =
1269 SDOperand(CurDAG->getTargetNode(Opc, VT, Node->getOperand(0)), 0);
Evan Cheng9ade2182006-08-26 05:34:46 +00001270 SDNode *ResNode = CurDAG->getTargetNode(Opc2, NVT, Tmp);
Evan Cheng403be7e2006-05-08 08:01:26 +00001271
1272#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001273 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001274 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001275 DOUT << "\n";
Evan Cheng403be7e2006-05-08 08:01:26 +00001276 Indent -= 2;
1277#endif
Evan Cheng9ade2182006-08-26 05:34:46 +00001278 return ResNode;
Evan Cheng403be7e2006-05-08 08:01:26 +00001279 }
Evan Cheng6b2e2542006-05-20 07:44:28 +00001280
1281 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001282 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001283 }
1284
Evan Cheng9ade2182006-08-26 05:34:46 +00001285 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001286
Evan Chengf597dc72006-02-10 22:24:32 +00001287#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001288 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001289 if (ResNode == NULL || ResNode == N.Val)
1290 DEBUG(N.Val->dump(CurDAG));
1291 else
1292 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001293 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001294 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001295#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001296
1297 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001298}
1299
Chris Lattnerc0bad572006-06-08 18:03:49 +00001300bool X86DAGToDAGISel::
1301SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1302 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1303 SDOperand Op0, Op1, Op2, Op3;
1304 switch (ConstraintCode) {
1305 case 'o': // offsetable ??
1306 case 'v': // not offsetable ??
1307 default: return true;
1308 case 'm': // memory
Evan Cheng0d538262006-11-08 20:34:28 +00001309 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001310 return true;
1311 break;
1312 }
1313
Evan Cheng04699902006-08-26 01:05:16 +00001314 OutOps.push_back(Op0);
1315 OutOps.push_back(Op1);
1316 OutOps.push_back(Op2);
1317 OutOps.push_back(Op3);
1318 AddToISelQueue(Op0);
1319 AddToISelQueue(Op1);
1320 AddToISelQueue(Op2);
1321 AddToISelQueue(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001322 return false;
1323}
1324
Chris Lattnerc961eea2005-11-16 01:54:32 +00001325/// createX86ISelDag - This pass converts a legalized DAG into a
1326/// X86-specific DAG, ready for instruction scheduling.
1327///
Evan Chenge50794a2006-08-29 18:28:33 +00001328FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1329 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001330}