blob: 72fb33195d5ccdf991519fcdb608a73125442bb7 [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"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000027#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000029#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000031#include "llvm/CodeGen/SelectionDAGISel.h"
32#include "llvm/Target/TargetMachine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000033#include "llvm/Support/Compiler.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000034#include "llvm/Support/Debug.h"
35#include "llvm/Support/MathExtras.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000036#include "llvm/ADT/Statistic.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000037#include <iostream>
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
42//===----------------------------------------------------------------------===//
43// Pattern Matcher Implementation
44//===----------------------------------------------------------------------===//
45
46namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000047 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
48 /// SDOperand's instead of register numbers for the leaves of the matched
49 /// tree.
50 struct X86ISelAddressMode {
51 enum {
52 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000053 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000054 } BaseType;
55
56 struct { // This is really a union, discriminated by BaseType!
57 SDOperand Reg;
58 int FrameIndex;
59 } Base;
60
Evan Cheng25ab6902006-09-08 06:48:29 +000061 bool isRIPRel; // RIP relative?
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000062 unsigned Scale;
63 SDOperand IndexReg;
64 unsigned Disp;
65 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000066 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000067 const char *ES;
68 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000069 unsigned Align; // CP alignment.
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000070
71 X86ISelAddressMode()
Evan Cheng25ab6902006-09-08 06:48:29 +000072 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
73 GV(0), CP(0), ES(0), JT(-1), Align(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000074 }
75 };
76}
77
78namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +000079 Statistic<>
80 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
81
Evan Cheng82a35b32006-08-29 06:44:17 +000082 Statistic<>
83 NumLoadMoved("x86-codegen", "Number of loads moved below TokenFactor");
84
Chris Lattnerc961eea2005-11-16 01:54:32 +000085 //===--------------------------------------------------------------------===//
86 /// ISel - X86 specific code to select X86 machine instructions for
87 /// SelectionDAG operations.
88 ///
Chris Lattner2c79de82006-06-28 23:27:49 +000089 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +000090 /// ContainsFPCode - Every instruction we select that uses or defines a FP
91 /// register should set this to true.
92 bool ContainsFPCode;
93
Evan Chenge50794a2006-08-29 18:28:33 +000094 /// FastISel - Enable fast(er) instruction selection.
95 ///
96 bool FastISel;
97
Evan Cheng25ab6902006-09-08 06:48:29 +000098 /// TM - Keep a reference to X86TargetMachine.
99 ///
100 X86TargetMachine &TM;
101
Chris Lattnerc961eea2005-11-16 01:54:32 +0000102 /// X86Lowering - This object fully describes how to lower LLVM code to an
103 /// X86-specific SelectionDAG.
104 X86TargetLowering X86Lowering;
105
106 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
107 /// make the right decision when generating code for different targets.
108 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000109
Evan Cheng25ab6902006-09-08 06:48:29 +0000110 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
111 /// base register.
Evan Cheng7ccced62006-02-18 00:15:05 +0000112 unsigned GlobalBaseReg;
Evan Chenga8df1b42006-07-27 16:44:36 +0000113
Chris Lattnerc961eea2005-11-16 01:54:32 +0000114 public:
Evan Cheng25ab6902006-09-08 06:48:29 +0000115 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Evan Chengc4c62572006-03-13 23:20:37 +0000116 : SelectionDAGISel(X86Lowering),
Evan Cheng25ab6902006-09-08 06:48:29 +0000117 ContainsFPCode(false), FastISel(fast), TM(tm),
Evan Chenga8df1b42006-07-27 16:44:36 +0000118 X86Lowering(*TM.getTargetLowering()),
Evan Chengf4b4c412006-08-08 00:31:00 +0000119 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000120
Evan Cheng7ccced62006-02-18 00:15:05 +0000121 virtual bool runOnFunction(Function &Fn) {
122 // Make sure we re-emit a set of the global base reg if necessary
123 GlobalBaseReg = 0;
124 return SelectionDAGISel::runOnFunction(Fn);
125 }
126
Chris Lattnerc961eea2005-11-16 01:54:32 +0000127 virtual const char *getPassName() const {
128 return "X86 DAG->DAG Instruction Selection";
129 }
130
131 /// InstructionSelectBasicBlock - This callback is invoked by
132 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
133 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
134
Evan Cheng8700e142006-01-11 06:09:51 +0000135 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
136
Evan Cheng27e1fe92006-10-14 08:33:25 +0000137 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root);
Evan Chenga8df1b42006-07-27 16:44:36 +0000138
Chris Lattnerc961eea2005-11-16 01:54:32 +0000139// Include the pieces autogenerated from the target description.
140#include "X86GenDAGISel.inc"
141
142 private:
Evan Cheng9ade2182006-08-26 05:34:46 +0000143 SDNode *Select(SDOperand N);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000144
Evan Cheng2486af12006-02-11 02:05:36 +0000145 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM, bool isRoot = true);
Evan Cheng0d538262006-11-08 20:34:28 +0000146 bool SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
147 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
148 bool SelectLEAAddr(SDOperand Op, SDOperand N, SDOperand &Base,
149 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
150 bool SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Cheng07e4b002006-10-16 06:34:55 +0000151 SDOperand N, SDOperand &Base, SDOperand &Scale,
Evan Cheng82a91642006-10-11 21:06:01 +0000152 SDOperand &Index, SDOperand &Disp,
153 SDOperand &InChain, SDOperand &OutChain);
Evan Cheng5e351682006-02-06 06:02:33 +0000154 bool TryFoldLoad(SDOperand P, SDOperand N,
155 SDOperand &Base, SDOperand &Scale,
Evan Cheng0114e942006-01-06 20:36:21 +0000156 SDOperand &Index, SDOperand &Disp);
Evan Cheng70e674e2006-08-28 20:10:17 +0000157 void InstructionSelectPreprocess(SelectionDAG &DAG);
Evan Cheng2ef88a02006-08-07 22:28:20 +0000158
Chris Lattnerc0bad572006-06-08 18:03:49 +0000159 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
160 /// inline asm expressions.
161 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
162 char ConstraintCode,
163 std::vector<SDOperand> &OutOps,
164 SelectionDAG &DAG);
165
Evan Cheng3649b0e2006-06-02 22:38:37 +0000166 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
167
Evan Chenge5280532005-12-12 21:49:40 +0000168 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
169 SDOperand &Scale, SDOperand &Index,
170 SDOperand &Disp) {
171 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000172 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
173 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000174 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000175 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000176 // These are 32-bit even in 64-bit mode since RIP relative offset
177 // is 32-bit.
178 if (AM.GV)
179 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
180 else if (AM.CP)
181 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp);
182 else if (AM.ES)
183 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
184 else if (AM.JT != -1)
185 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
186 else
187 Disp = getI32Imm(AM.Disp);
Evan Chenge5280532005-12-12 21:49:40 +0000188 }
189
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000190 /// getI8Imm - Return a target constant with the specified value, of type
191 /// i8.
192 inline SDOperand getI8Imm(unsigned Imm) {
193 return CurDAG->getTargetConstant(Imm, MVT::i8);
194 }
195
Chris Lattnerc961eea2005-11-16 01:54:32 +0000196 /// getI16Imm - Return a target constant with the specified value, of type
197 /// i16.
198 inline SDOperand getI16Imm(unsigned Imm) {
199 return CurDAG->getTargetConstant(Imm, MVT::i16);
200 }
201
202 /// getI32Imm - Return a target constant with the specified value, of type
203 /// i32.
204 inline SDOperand getI32Imm(unsigned Imm) {
205 return CurDAG->getTargetConstant(Imm, MVT::i32);
206 }
Evan Chengf597dc72006-02-10 22:24:32 +0000207
Evan Cheng7ccced62006-02-18 00:15:05 +0000208 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
209 /// base register. Return the virtual register that holds this value.
Evan Cheng9ade2182006-08-26 05:34:46 +0000210 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000211
Evan Cheng23addc02006-02-10 22:46:26 +0000212#ifndef NDEBUG
213 unsigned Indent;
214#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000215 };
216}
217
Evan Chenga275ecb2006-10-10 01:46:56 +0000218static SDNode *findFlagUse(SDNode *N) {
219 unsigned FlagResNo = N->getNumValues()-1;
220 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
221 SDNode *User = *I;
222 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
223 SDOperand Op = User->getOperand(i);
Evan Cheng494cec62006-10-12 19:13:59 +0000224 if (Op.Val == N && Op.ResNo == FlagResNo)
Evan Chenga275ecb2006-10-10 01:46:56 +0000225 return User;
226 }
227 }
228 return NULL;
229}
230
Evan Cheng27e1fe92006-10-14 08:33:25 +0000231static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
232 SDNode *Root, SDNode *Skip, bool &found,
Evan Chengf4b4c412006-08-08 00:31:00 +0000233 std::set<SDNode *> &Visited) {
234 if (found ||
235 Use->getNodeId() > Def->getNodeId() ||
236 !Visited.insert(Use).second)
237 return;
238
Evan Cheng27e1fe92006-10-14 08:33:25 +0000239 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000240 SDNode *N = Use->getOperand(i).Val;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000241 if (N == Skip)
Evan Chenga275ecb2006-10-10 01:46:56 +0000242 continue;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000243 if (N == Def) {
244 if (Use == ImmedUse)
245 continue; // Immediate use is ok.
246 if (Use == Root) {
247 assert(Use->getOpcode() == ISD::STORE ||
248 Use->getOpcode() == X86ISD::CMP);
249 continue;
250 }
Evan Chengf4b4c412006-08-08 00:31:00 +0000251 found = true;
252 break;
253 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000254 findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000255 }
256}
257
Evan Cheng27e1fe92006-10-14 08:33:25 +0000258/// isNonImmUse - Start searching from Root up the DAG to check is Def can
259/// be reached. Return true if that's the case. However, ignore direct uses
260/// by ImmedUse (which would be U in the example illustrated in
261/// CanBeFoldedBy) and by Root (which can happen in the store case).
262/// FIXME: to be really generic, we should allow direct use by any node
263/// that is being folded. But realisticly since we only fold loads which
264/// have one non-chain use, we only need to watch out for load/op/store
265/// and load/op/cmp case where the root (store / cmp) may reach the load via
266/// its chain operand.
267static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
268 SDNode *Skip = NULL) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000269 std::set<SDNode *> Visited;
270 bool found = false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000271 findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000272 return found;
273}
274
275
Evan Cheng27e1fe92006-10-14 08:33:25 +0000276bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) {
277 if (FastISel) return false;
278
Evan Chenga8df1b42006-07-27 16:44:36 +0000279 // If U use can somehow reach N through another path then U can't fold N or
280 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Cheng37e18032006-07-28 06:33:41 +0000281 // through X. If N is folded into into U, then X is both a predecessor and
Evan Chenga8df1b42006-07-27 16:44:36 +0000282 // a successor of U.
283 //
284 // [ N ]
285 // ^ ^
286 // | |
287 // / \---
288 // / [X]
289 // | ^
290 // [U]--------|
Evan Cheng27e1fe92006-10-14 08:33:25 +0000291
292 if (isNonImmUse(Root, N, U))
293 return false;
294
295 // If U produces a flag, then it gets (even more) interesting. Since it
296 // would have been "glued" together with its flag use, we need to check if
297 // it might reach N:
298 //
299 // [ N ]
300 // ^ ^
301 // | |
302 // [U] \--
303 // ^ [TF]
304 // | ^
305 // | |
306 // \ /
307 // [FU]
308 //
309 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
310 // NU), then TF is a predecessor of FU and a successor of NU. But since
311 // NU and FU are flagged together, this effectively creates a cycle.
312 bool HasFlagUse = false;
313 MVT::ValueType VT = Root->getValueType(Root->getNumValues()-1);
314 while ((VT == MVT::Flag && !Root->use_empty())) {
315 SDNode *FU = findFlagUse(Root);
316 if (FU == NULL)
317 break;
318 else {
319 Root = FU;
320 HasFlagUse = true;
Evan Chenga275ecb2006-10-10 01:46:56 +0000321 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000322 VT = Root->getValueType(Root->getNumValues()-1);
Evan Chenga275ecb2006-10-10 01:46:56 +0000323 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000324
325 if (HasFlagUse)
326 return !isNonImmUse(Root, N, Root, U);
327 return true;
Evan Chenga8df1b42006-07-27 16:44:36 +0000328}
329
Evan Cheng70e674e2006-08-28 20:10:17 +0000330/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
331/// and move load below the TokenFactor. Replace store's chain operand with
332/// load's chain result.
333static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
334 SDOperand Store, SDOperand TF) {
335 std::vector<SDOperand> Ops;
336 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
337 if (Load.Val == TF.Val->getOperand(i).Val)
338 Ops.push_back(Load.Val->getOperand(0));
339 else
340 Ops.push_back(TF.Val->getOperand(i));
341 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
342 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
343 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
344 Store.getOperand(2), Store.getOperand(3));
345}
346
347/// InstructionSelectPreprocess - Preprocess the DAG to allow the instruction
348/// selector to pick more load-modify-store instructions. This is a common
349/// case:
350///
351/// [Load chain]
352/// ^
353/// |
354/// [Load]
355/// ^ ^
356/// | |
357/// / \-
358/// / |
359/// [TokenFactor] [Op]
360/// ^ ^
361/// | |
362/// \ /
363/// \ /
364/// [Store]
365///
366/// The fact the store's chain operand != load's chain will prevent the
367/// (store (op (load))) instruction from being selected. We can transform it to:
368///
369/// [Load chain]
370/// ^
371/// |
372/// [TokenFactor]
373/// ^
374/// |
375/// [Load]
376/// ^ ^
377/// | |
378/// | \-
379/// | |
380/// | [Op]
381/// | ^
382/// | |
383/// \ /
384/// \ /
385/// [Store]
386void X86DAGToDAGISel::InstructionSelectPreprocess(SelectionDAG &DAG) {
387 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
388 E = DAG.allnodes_end(); I != E; ++I) {
Evan Cheng8b2794a2006-10-13 21:14:26 +0000389 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000390 continue;
391 SDOperand Chain = I->getOperand(0);
392 if (Chain.Val->getOpcode() != ISD::TokenFactor)
393 continue;
394
395 SDOperand N1 = I->getOperand(1);
396 SDOperand N2 = I->getOperand(2);
Evan Cheng1453de52006-09-01 22:52:28 +0000397 if (MVT::isFloatingPoint(N1.getValueType()) ||
398 MVT::isVector(N1.getValueType()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000399 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000400 continue;
401
402 bool RModW = false;
403 SDOperand Load;
404 unsigned Opcode = N1.Val->getOpcode();
405 switch (Opcode) {
406 case ISD::ADD:
407 case ISD::MUL:
Evan Cheng70e674e2006-08-28 20:10:17 +0000408 case ISD::AND:
409 case ISD::OR:
410 case ISD::XOR:
411 case ISD::ADDC:
412 case ISD::ADDE: {
413 SDOperand N10 = N1.getOperand(0);
414 SDOperand N11 = N1.getOperand(1);
Evan Cheng466685d2006-10-09 20:57:25 +0000415 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng70e674e2006-08-28 20:10:17 +0000416 RModW = true;
Evan Cheng466685d2006-10-09 20:57:25 +0000417 else if (ISD::isNON_EXTLoad(N11.Val)) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000418 RModW = true;
419 std::swap(N10, N11);
420 }
421 RModW = RModW && N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000422 (N10.getOperand(1) == N2) &&
423 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000424 if (RModW)
425 Load = N10;
426 break;
427 }
428 case ISD::SUB:
429 case ISD::SHL:
430 case ISD::SRA:
431 case ISD::SRL:
432 case ISD::ROTL:
433 case ISD::ROTR:
434 case ISD::SUBC:
435 case ISD::SUBE:
436 case X86ISD::SHLD:
437 case X86ISD::SHRD: {
438 SDOperand N10 = N1.getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +0000439 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng70e674e2006-08-28 20:10:17 +0000440 RModW = N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000441 (N10.getOperand(1) == N2) &&
442 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000443 if (RModW)
444 Load = N10;
445 break;
446 }
447 }
448
Evan Cheng82a35b32006-08-29 06:44:17 +0000449 if (RModW) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000450 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000451 ++NumLoadMoved;
452 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000453 }
454}
455
Chris Lattnerc961eea2005-11-16 01:54:32 +0000456/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
457/// when it has created a SelectionDAG for us to codegen.
458void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
459 DEBUG(BB->dump());
Chris Lattner92cb0af2006-01-11 01:15:34 +0000460 MachineFunction::iterator FirstMBB = BB;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000461
Evan Chenge50794a2006-08-29 18:28:33 +0000462 if (!FastISel)
Evan Cheng70e674e2006-08-28 20:10:17 +0000463 InstructionSelectPreprocess(DAG);
464
Chris Lattnerc961eea2005-11-16 01:54:32 +0000465 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000466#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000467 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000468 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000469#endif
Evan Chengba2f0a92006-02-05 06:46:41 +0000470 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengf597dc72006-02-10 22:24:32 +0000471#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000472 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000473#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000474
Chris Lattnerc961eea2005-11-16 01:54:32 +0000475 DAG.RemoveDeadNodes();
476
477 // Emit machine code to BB.
478 ScheduleAndEmitDAG(DAG);
Chris Lattner92cb0af2006-01-11 01:15:34 +0000479
480 // If we are emitting FP stack code, scan the basic block to determine if this
481 // block defines any FP values. If so, put an FP_REG_KILL instruction before
482 // the terminator of the block.
Evan Cheng559806f2006-01-27 08:10:46 +0000483 if (!Subtarget->hasSSE2()) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000484 // Note that FP stack instructions *are* used in SSE code when returning
485 // values, but these are not live out of the basic block, so we don't need
486 // an FP_REG_KILL in this case either.
487 bool ContainsFPCode = false;
488
489 // Scan all of the machine instructions in these MBBs, checking for FP
490 // stores.
491 MachineFunction::iterator MBBI = FirstMBB;
492 do {
493 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
494 !ContainsFPCode && I != E; ++I) {
495 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
496 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
497 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
498 RegMap->getRegClass(I->getOperand(0).getReg()) ==
499 X86::RFPRegisterClass) {
500 ContainsFPCode = true;
501 break;
502 }
503 }
504 }
505 } while (!ContainsFPCode && &*(MBBI++) != BB);
506
507 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
508 // a copy of the input value in this block.
509 if (!ContainsFPCode) {
510 // Final check, check LLVM BB's that are successors to the LLVM BB
511 // corresponding to BB for FP PHI nodes.
512 const BasicBlock *LLVMBB = BB->getBasicBlock();
513 const PHINode *PN;
514 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
515 !ContainsFPCode && SI != E; ++SI) {
516 for (BasicBlock::const_iterator II = SI->begin();
517 (PN = dyn_cast<PHINode>(II)); ++II) {
518 if (PN->getType()->isFloatingPoint()) {
519 ContainsFPCode = true;
520 break;
521 }
522 }
523 }
524 }
525
526 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
527 if (ContainsFPCode) {
Evan Cheng7ce45782006-11-13 23:36:35 +0000528 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
Chris Lattner92cb0af2006-01-11 01:15:34 +0000529 ++NumFPKill;
530 }
531 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000532}
533
Evan Cheng8700e142006-01-11 06:09:51 +0000534/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
535/// the main function.
Evan Cheng3649b0e2006-06-02 22:38:37 +0000536void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
537 MachineFrameInfo *MFI) {
Anton Korobeynikovbcb97702006-09-17 20:25:45 +0000538 if (Subtarget->isTargetCygwin())
Evan Cheng7ce45782006-11-13 23:36:35 +0000539 BuildMI(BB, X86::CALLpcrel32, 1).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);
543 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
544
545 // Set the high part to be 64-bit precision.
546 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
547 CWFrameIdx, 1).addImm(2);
548
549 // Reload the modified control word now.
550 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
551}
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
591 case X86ISD::Wrapper:
Evan Cheng25ab6902006-09-08 06:48:29 +0000592 // If value is available in a register both base and index components have
593 // been picked, we can't fit the result available in the register in the
594 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
595
596 // Can't fit GV or CP in addressing mode for X86-64 medium or large code
597 // model since the displacement field is 32-bit. Ok for small code model.
598
599 // For X86-64 PIC code, only allow GV / CP + displacement so we can use RIP
600 // relative addressing mode.
601 if ((!Subtarget->is64Bit() || TM.getCodeModel() == CodeModel::Small) &&
602 (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val))) {
603 bool isRIP = Subtarget->is64Bit();
604 if (isRIP && (AM.Base.Reg.Val || AM.Scale > 1 || AM.IndexReg.Val ||
605 AM.BaseType == X86ISelAddressMode::FrameIndexBase))
606 break;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000607 if (ConstantPoolSDNode *CP =
608 dyn_cast<ConstantPoolSDNode>(N.getOperand(0))) {
609 if (AM.CP == 0) {
Evan Chengc356a572006-09-12 21:04:05 +0000610 AM.CP = CP->getConstVal();
Evan Cheng51a9ed92006-02-25 10:09:08 +0000611 AM.Align = CP->getAlignment();
612 AM.Disp += CP->getOffset();
Evan Cheng25ab6902006-09-08 06:48:29 +0000613 if (isRIP)
614 AM.isRIPRel = true;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000615 return false;
616 }
617 } else if (GlobalAddressSDNode *G =
618 dyn_cast<GlobalAddressSDNode>(N.getOperand(0))) {
619 if (AM.GV == 0) {
620 AM.GV = G->getGlobal();
621 AM.Disp += G->getOffset();
Evan Cheng25ab6902006-09-08 06:48:29 +0000622 if (isRIP)
623 AM.isRIPRel = true;
624 return false;
625 }
626 } else if (isRoot && isRIP) {
627 if (ExternalSymbolSDNode *S =
628 dyn_cast<ExternalSymbolSDNode>(N.getOperand(0))) {
629 AM.ES = S->getSymbol();
630 AM.isRIPRel = true;
631 return false;
632 } else if (JumpTableSDNode *J =
633 dyn_cast<JumpTableSDNode>(N.getOperand(0))) {
634 AM.JT = J->getIndex();
635 AM.isRIPRel = true;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000636 return false;
637 }
638 }
639 }
640 break;
641
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000642 case ISD::FrameIndex:
643 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
644 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
645 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
646 return false;
647 }
648 break;
Evan Chengec693f72005-12-08 02:01:35 +0000649
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000650 case ISD::SHL:
Evan Cheng51a9ed92006-02-25 10:09:08 +0000651 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000652 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
653 unsigned Val = CN->getValue();
654 if (Val == 1 || Val == 2 || Val == 3) {
655 AM.Scale = 1 << Val;
656 SDOperand ShVal = N.Val->getOperand(0);
657
658 // Okay, we know that we have a scale by now. However, if the scaled
659 // value is an add of something and a constant, we can fold the
660 // constant into the disp field here.
661 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
662 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
663 AM.IndexReg = ShVal.Val->getOperand(0);
664 ConstantSDNode *AddVal =
665 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
Jeff Cohend41b30d2006-11-05 19:31:28 +0000666 uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
Evan Cheng25ab6902006-09-08 06:48:29 +0000667 if (isInt32(Disp))
668 AM.Disp = Disp;
669 else
670 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000671 } else {
672 AM.IndexReg = ShVal;
673 }
674 return false;
675 }
676 }
677 break;
Evan Chengec693f72005-12-08 02:01:35 +0000678
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000679 case ISD::MUL:
680 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng51a9ed92006-02-25 10:09:08 +0000681 if (!Available &&
682 AM.BaseType == X86ISelAddressMode::RegBase &&
683 AM.Base.Reg.Val == 0 &&
684 AM.IndexReg.Val == 0)
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000685 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
686 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
687 AM.Scale = unsigned(CN->getValue())-1;
688
689 SDOperand MulVal = N.Val->getOperand(0);
690 SDOperand Reg;
691
692 // Okay, we know that we have a scale by now. However, if the scaled
693 // value is an add of something and a constant, we can fold the
694 // constant into the disp field here.
695 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
696 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
697 Reg = MulVal.Val->getOperand(0);
698 ConstantSDNode *AddVal =
699 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
Evan Cheng25ab6902006-09-08 06:48:29 +0000700 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
701 if (isInt32(Disp))
702 AM.Disp = Disp;
703 else
704 Reg = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000705 } else {
706 Reg = N.Val->getOperand(0);
707 }
708
709 AM.IndexReg = AM.Base.Reg = Reg;
710 return false;
711 }
712 break;
713
714 case ISD::ADD: {
Evan Cheng51a9ed92006-02-25 10:09:08 +0000715 if (!Available) {
Evan Cheng2486af12006-02-11 02:05:36 +0000716 X86ISelAddressMode Backup = AM;
717 if (!MatchAddress(N.Val->getOperand(0), AM, false) &&
718 !MatchAddress(N.Val->getOperand(1), AM, false))
719 return false;
720 AM = Backup;
721 if (!MatchAddress(N.Val->getOperand(1), AM, false) &&
722 !MatchAddress(N.Val->getOperand(0), AM, false))
723 return false;
724 AM = Backup;
725 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000726 break;
727 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000728
729 case ISD::OR: {
730 if (!Available) {
731 X86ISelAddressMode Backup = AM;
732 // Look for (x << c1) | c2 where (c2 < c1)
733 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(0));
734 if (CN && !MatchAddress(N.Val->getOperand(1), AM, false)) {
735 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
736 AM.Disp = CN->getValue();
737 return false;
738 }
739 }
740 AM = Backup;
741 CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1));
742 if (CN && !MatchAddress(N.Val->getOperand(0), AM, false)) {
743 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
744 AM.Disp = CN->getValue();
745 return false;
746 }
747 }
748 AM = Backup;
749 }
750 break;
751 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000752 }
753
754 // Is the base register already occupied?
755 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
756 // If so, check to see if the scale index register is set.
757 if (AM.IndexReg.Val == 0) {
758 AM.IndexReg = N;
759 AM.Scale = 1;
760 return false;
761 }
762
763 // Otherwise, we cannot select it.
764 return true;
765 }
766
767 // Default, generate it as a register.
768 AM.BaseType = X86ISelAddressMode::RegBase;
769 AM.Base.Reg = N;
770 return false;
771}
772
Evan Chengec693f72005-12-08 02:01:35 +0000773/// SelectAddr - returns true if it is able pattern match an addressing mode.
774/// It returns the operands which make up the maximal addressing mode it can
775/// match by reference.
Evan Cheng0d538262006-11-08 20:34:28 +0000776bool X86DAGToDAGISel::SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
777 SDOperand &Scale, SDOperand &Index,
778 SDOperand &Disp) {
Evan Chengec693f72005-12-08 02:01:35 +0000779 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +0000780 if (MatchAddress(N, AM))
781 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000782
Evan Cheng25ab6902006-09-08 06:48:29 +0000783 MVT::ValueType VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +0000784 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Cheng7dd281b2006-02-05 05:25:07 +0000785 if (!AM.Base.Reg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000786 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +0000787 }
Evan Cheng8700e142006-01-11 06:09:51 +0000788
Evan Cheng7dd281b2006-02-05 05:25:07 +0000789 if (!AM.IndexReg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000790 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +0000791
792 getAddressOperands(AM, Base, Scale, Index, Disp);
793 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000794}
795
Chris Lattner4fe4f252006-10-11 22:09:58 +0000796/// isZeroNode - Returns true if Elt is a constant zero or a floating point
797/// constant +0.0.
798static inline bool isZeroNode(SDOperand Elt) {
799 return ((isa<ConstantSDNode>(Elt) &&
800 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
801 (isa<ConstantFPSDNode>(Elt) &&
802 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
803}
804
805
Chris Lattner3a7cd952006-10-07 21:55:32 +0000806/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
807/// match a load whose top elements are either undef or zeros. The load flavor
808/// is derived from the type of N, which is either v4f32 or v2f64.
Evan Cheng0d538262006-11-08 20:34:28 +0000809bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Cheng07e4b002006-10-16 06:34:55 +0000810 SDOperand N, SDOperand &Base,
Evan Cheng82a91642006-10-11 21:06:01 +0000811 SDOperand &Scale, SDOperand &Index,
812 SDOperand &Disp, SDOperand &InChain,
813 SDOperand &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +0000814 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +0000815 InChain = N.getOperand(0).getValue(1);
Evan Cheng07e4b002006-10-16 06:34:55 +0000816 if (ISD::isNON_EXTLoad(InChain.Val) &&
817 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +0000818 N.hasOneUse() &&
Evan Cheng0d538262006-11-08 20:34:28 +0000819 CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
Evan Cheng82a91642006-10-11 21:06:01 +0000820 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Evan Cheng0d538262006-11-08 20:34:28 +0000821 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner3a7cd952006-10-07 21:55:32 +0000822 return false;
Evan Cheng82a91642006-10-11 21:06:01 +0000823 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +0000824 return true;
825 }
826 }
Chris Lattner4fe4f252006-10-11 22:09:58 +0000827
828 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +0000829 // elements. This is a vector shuffle from the zero vector.
Chris Lattner4fe4f252006-10-11 22:09:58 +0000830 if (N.getOpcode() == ISD::VECTOR_SHUFFLE && N.Val->hasOneUse() &&
831 N.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
832 N.getOperand(1).getOpcode() == ISD::SCALAR_TO_VECTOR &&
833 N.getOperand(1).Val->hasOneUse() &&
834 ISD::isNON_EXTLoad(N.getOperand(1).getOperand(0).Val) &&
835 N.getOperand(1).getOperand(0).hasOneUse()) {
836 // Check to see if the BUILD_VECTOR is building a zero vector.
837 SDOperand BV = N.getOperand(0);
838 for (unsigned i = 0, e = BV.getNumOperands(); i != e; ++i)
839 if (!isZeroNode(BV.getOperand(i)) &&
840 BV.getOperand(i).getOpcode() != ISD::UNDEF)
841 return false; // Not a zero/undef vector.
842 // Check to see if the shuffle mask is 4/L/L/L or 2/L, where L is something
843 // from the LHS.
844 unsigned VecWidth = BV.getNumOperands();
845 SDOperand ShufMask = N.getOperand(2);
846 assert(ShufMask.getOpcode() == ISD::BUILD_VECTOR && "Invalid shuf mask!");
847 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(ShufMask.getOperand(0))) {
848 if (C->getValue() == VecWidth) {
849 for (unsigned i = 1; i != VecWidth; ++i) {
850 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF) {
851 // ok.
852 } else {
853 ConstantSDNode *C = cast<ConstantSDNode>(ShufMask.getOperand(i));
854 if (C->getValue() >= VecWidth) return false;
855 }
856 }
857 }
858
859 // Okay, this is a zero extending load. Fold it.
860 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(1).getOperand(0));
Evan Cheng0d538262006-11-08 20:34:28 +0000861 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner4fe4f252006-10-11 22:09:58 +0000862 return false;
863 OutChain = LD->getChain();
864 InChain = SDOperand(LD, 1);
865 return true;
866 }
867 }
Chris Lattner3a7cd952006-10-07 21:55:32 +0000868 return false;
869}
870
871
Evan Cheng51a9ed92006-02-25 10:09:08 +0000872/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
873/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng0d538262006-11-08 20:34:28 +0000874bool X86DAGToDAGISel::SelectLEAAddr(SDOperand Op, SDOperand N,
875 SDOperand &Base, SDOperand &Scale,
Evan Cheng51a9ed92006-02-25 10:09:08 +0000876 SDOperand &Index, SDOperand &Disp) {
877 X86ISelAddressMode AM;
878 if (MatchAddress(N, AM))
879 return false;
880
Evan Cheng25ab6902006-09-08 06:48:29 +0000881 MVT::ValueType VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +0000882 unsigned Complexity = 0;
883 if (AM.BaseType == X86ISelAddressMode::RegBase)
884 if (AM.Base.Reg.Val)
885 Complexity = 1;
886 else
Evan Cheng25ab6902006-09-08 06:48:29 +0000887 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +0000888 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
889 Complexity = 4;
890
891 if (AM.IndexReg.Val)
892 Complexity++;
893 else
Evan Cheng25ab6902006-09-08 06:48:29 +0000894 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +0000895
Evan Cheng8c03fe42006-02-28 21:13:57 +0000896 if (AM.Scale > 2)
Evan Cheng51a9ed92006-02-25 10:09:08 +0000897 Complexity += 2;
Evan Cheng8c03fe42006-02-28 21:13:57 +0000898 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg
899 else if (AM.Scale > 1)
900 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000901
902 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
903 // to a LEA. This is determined with some expermentation but is by no means
904 // optimal (especially for code size consideration). LEA is nice because of
905 // its three-address nature. Tweak the cost function again when we can run
906 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +0000907 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
908 // For X86-64, we should always use lea to materialize RIP relative
909 // addresses.
910 if (Subtarget->is64Bit())
911 Complexity = 4;
912 else
913 Complexity += 2;
914 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000915
916 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
917 Complexity++;
918
919 if (Complexity > 2) {
920 getAddressOperands(AM, Base, Scale, Index, Disp);
921 return true;
922 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000923 return false;
924}
925
Evan Cheng5e351682006-02-06 06:02:33 +0000926bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
927 SDOperand &Base, SDOperand &Scale,
928 SDOperand &Index, SDOperand &Disp) {
Evan Cheng466685d2006-10-09 20:57:25 +0000929 if (ISD::isNON_EXTLoad(N.Val) &&
Evan Cheng5e351682006-02-06 06:02:33 +0000930 N.hasOneUse() &&
Evan Cheng27e1fe92006-10-14 08:33:25 +0000931 CanBeFoldedBy(N.Val, P.Val, P.Val))
Evan Cheng0d538262006-11-08 20:34:28 +0000932 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
Evan Cheng0114e942006-01-06 20:36:21 +0000933 return false;
934}
935
Evan Cheng7ccced62006-02-18 00:15:05 +0000936/// getGlobalBaseReg - Output the instructions required to put the
937/// base address to use for accessing globals into a register.
938///
Evan Cheng9ade2182006-08-26 05:34:46 +0000939SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng25ab6902006-09-08 06:48:29 +0000940 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Evan Cheng7ccced62006-02-18 00:15:05 +0000941 if (!GlobalBaseReg) {
942 // Insert the set of GlobalBaseReg into the first MBB of the function
943 MachineBasicBlock &FirstMBB = BB->getParent()->front();
944 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
945 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
946 // FIXME: when we get to LP64, we will need to create the appropriate
947 // type of register here.
Evan Cheng069287d2006-05-16 07:21:53 +0000948 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng7ccced62006-02-18 00:15:05 +0000949 BuildMI(FirstMBB, MBBI, X86::MovePCtoStack, 0);
Evan Cheng7ce45782006-11-13 23:36:35 +0000950 BuildMI(FirstMBB, MBBI, X86::POP32r, 1, GlobalBaseReg);
Evan Cheng7ccced62006-02-18 00:15:05 +0000951 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000952 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
Evan Cheng7ccced62006-02-18 00:15:05 +0000953}
954
Evan Chengb245d922006-05-20 01:36:52 +0000955static SDNode *FindCallStartFromCall(SDNode *Node) {
956 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
957 assert(Node->getOperand(0).getValueType() == MVT::Other &&
958 "Node doesn't have a token chain argument!");
959 return FindCallStartFromCall(Node->getOperand(0).Val);
960}
961
Evan Cheng9ade2182006-08-26 05:34:46 +0000962SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Chengdef941b2005-12-15 01:02:48 +0000963 SDNode *Node = N.Val;
964 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +0000965 unsigned Opc, MOpc;
966 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +0000967
Evan Chengf597dc72006-02-10 22:24:32 +0000968#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000969 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +0000970 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +0000971 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000972 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000973#endif
974
Evan Cheng34167212006-02-09 00:37:58 +0000975 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengf597dc72006-02-10 22:24:32 +0000976#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000977 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +0000978 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +0000979 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000980 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000981#endif
Evan Cheng64a752f2006-08-11 09:08:15 +0000982 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +0000983 }
Evan Cheng38262ca2006-01-11 22:15:18 +0000984
Evan Cheng0114e942006-01-06 20:36:21 +0000985 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000986 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +0000987 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +0000988 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +0000989
Evan Cheng51a9ed92006-02-25 10:09:08 +0000990 case ISD::ADD: {
991 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
992 // code and is matched first so to prevent it from being turned into
993 // LEA32r X+c.
Evan Cheng25ab6902006-09-08 06:48:29 +0000994 // In 64-bit mode, use LEA to take advantage of RIP-relative addressing.
995 MVT::ValueType PtrVT = TLI.getPointerTy();
Evan Cheng51a9ed92006-02-25 10:09:08 +0000996 SDOperand N0 = N.getOperand(0);
997 SDOperand N1 = N.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +0000998 if (N.Val->getValueType(0) == PtrVT &&
Evan Cheng51a9ed92006-02-25 10:09:08 +0000999 N0.getOpcode() == X86ISD::Wrapper &&
1000 N1.getOpcode() == ISD::Constant) {
1001 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1002 SDOperand C(0, 0);
1003 // TODO: handle ExternalSymbolSDNode.
1004 if (GlobalAddressSDNode *G =
1005 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001006 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001007 G->getOffset() + Offset);
1008 } else if (ConstantPoolSDNode *CP =
1009 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Chengc356a572006-09-12 21:04:05 +00001010 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001011 CP->getAlignment(),
1012 CP->getOffset()+Offset);
1013 }
1014
Evan Cheng25ab6902006-09-08 06:48:29 +00001015 if (C.Val) {
1016 if (Subtarget->is64Bit()) {
1017 SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
1018 CurDAG->getRegister(0, PtrVT), C };
1019 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1020 } else
1021 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1022 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001023 }
1024
1025 // Other cases are handled by auto-generated code.
1026 break;
Evan Chenga0ea0532006-02-23 02:43:52 +00001027 }
Evan Cheng020d2e82006-02-23 20:41:18 +00001028
Evan Cheng0114e942006-01-06 20:36:21 +00001029 case ISD::MULHU:
1030 case ISD::MULHS: {
1031 if (Opcode == ISD::MULHU)
1032 switch (NVT) {
1033 default: assert(0 && "Unsupported VT!");
1034 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1035 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1036 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001037 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001038 }
1039 else
1040 switch (NVT) {
1041 default: assert(0 && "Unsupported VT!");
1042 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1043 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1044 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001045 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001046 }
1047
1048 unsigned LoReg, HiReg;
1049 switch (NVT) {
1050 default: assert(0 && "Unsupported VT!");
1051 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1052 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1053 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001054 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001055 }
1056
1057 SDOperand N0 = Node->getOperand(0);
1058 SDOperand N1 = Node->getOperand(1);
1059
1060 bool foldedLoad = false;
1061 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng5e351682006-02-06 06:02:33 +00001062 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +00001063 // MULHU and MULHS are commmutative
1064 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +00001065 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +00001066 if (foldedLoad) {
1067 N0 = Node->getOperand(1);
1068 N1 = Node->getOperand(0);
1069 }
1070 }
1071
Evan Cheng34167212006-02-09 00:37:58 +00001072 SDOperand Chain;
Evan Cheng04699902006-08-26 01:05:16 +00001073 if (foldedLoad) {
1074 Chain = N1.getOperand(0);
1075 AddToISelQueue(Chain);
1076 } else
Evan Cheng34167212006-02-09 00:37:58 +00001077 Chain = CurDAG->getEntryNode();
Evan Cheng0114e942006-01-06 20:36:21 +00001078
Evan Cheng34167212006-02-09 00:37:58 +00001079 SDOperand InFlag(0, 0);
Evan Cheng04699902006-08-26 01:05:16 +00001080 AddToISelQueue(N0);
Evan Cheng0114e942006-01-06 20:36:21 +00001081 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng34167212006-02-09 00:37:58 +00001082 N0, InFlag);
Evan Cheng0114e942006-01-06 20:36:21 +00001083 InFlag = Chain.getValue(1);
1084
1085 if (foldedLoad) {
Evan Cheng04699902006-08-26 01:05:16 +00001086 AddToISelQueue(Tmp0);
1087 AddToISelQueue(Tmp1);
1088 AddToISelQueue(Tmp2);
1089 AddToISelQueue(Tmp3);
Evan Cheng0b828e02006-08-27 08:14:06 +00001090 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001091 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001092 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001093 Chain = SDOperand(CNode, 0);
1094 InFlag = SDOperand(CNode, 1);
Evan Cheng0114e942006-01-06 20:36:21 +00001095 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001096 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001097 InFlag =
1098 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001099 }
1100
Evan Cheng9ade2182006-08-26 05:34:46 +00001101 SDOperand Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
Evan Cheng2ef88a02006-08-07 22:28:20 +00001102 ReplaceUses(N.getValue(0), Result);
1103 if (foldedLoad)
1104 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Cheng34167212006-02-09 00:37:58 +00001105
Evan Chengf597dc72006-02-10 22:24:32 +00001106#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001107 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Chengf597dc72006-02-10 22:24:32 +00001108 DEBUG(Result.Val->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001109 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001110 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001111#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001112 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001113 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001114
Evan Cheng948f3432006-01-06 23:19:29 +00001115 case ISD::SDIV:
1116 case ISD::UDIV:
1117 case ISD::SREM:
1118 case ISD::UREM: {
1119 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
1120 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
1121 if (!isSigned)
1122 switch (NVT) {
1123 default: assert(0 && "Unsupported VT!");
1124 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1125 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1126 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001127 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001128 }
1129 else
1130 switch (NVT) {
1131 default: assert(0 && "Unsupported VT!");
1132 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1133 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1134 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001135 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001136 }
1137
1138 unsigned LoReg, HiReg;
1139 unsigned ClrOpcode, SExtOpcode;
1140 switch (NVT) {
1141 default: assert(0 && "Unsupported VT!");
1142 case MVT::i8:
1143 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001144 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001145 SExtOpcode = X86::CBW;
1146 break;
1147 case MVT::i16:
1148 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001149 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001150 SExtOpcode = X86::CWD;
1151 break;
1152 case MVT::i32:
1153 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001154 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001155 SExtOpcode = X86::CDQ;
1156 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001157 case MVT::i64:
1158 LoReg = X86::RAX; HiReg = X86::RDX;
1159 ClrOpcode = X86::MOV64r0;
1160 SExtOpcode = X86::CQO;
1161 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001162 }
1163
1164 SDOperand N0 = Node->getOperand(0);
1165 SDOperand N1 = Node->getOperand(1);
Evan Cheng34167212006-02-09 00:37:58 +00001166 SDOperand InFlag(0, 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001167 if (NVT == MVT::i8 && !isSigned) {
1168 // Special case for div8, just use a move with zero extension to AX to
1169 // clear the upper 8 bits (AH).
1170 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
1171 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
1172 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
1173 AddToISelQueue(N0.getOperand(0));
1174 AddToISelQueue(Tmp0);
1175 AddToISelQueue(Tmp1);
1176 AddToISelQueue(Tmp2);
1177 AddToISelQueue(Tmp3);
1178 Move =
1179 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
1180 Ops, 5), 0);
1181 Chain = Move.getValue(1);
1182 ReplaceUses(N0.getValue(1), Chain);
1183 } else {
1184 AddToISelQueue(N0);
1185 Move =
1186 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
1187 Chain = CurDAG->getEntryNode();
1188 }
1189 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, InFlag);
Evan Cheng948f3432006-01-06 23:19:29 +00001190 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001191 } else {
1192 AddToISelQueue(N0);
1193 InFlag =
1194 CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg, N0,
1195 InFlag).getValue(1);
1196 if (isSigned) {
1197 // Sign extend the low part into the high part.
1198 InFlag =
1199 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
1200 } else {
1201 // Zero out the high part, effectively zero extending the input.
1202 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
1203 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg, ClrNode,
1204 InFlag).getValue(1);
1205 }
Evan Cheng948f3432006-01-06 23:19:29 +00001206 }
1207
Evan Chengb1409ce2006-11-17 22:10:14 +00001208 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Chain;
1209 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +00001210 if (foldedLoad) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001211 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001212 AddToISelQueue(Tmp0);
1213 AddToISelQueue(Tmp1);
1214 AddToISelQueue(Tmp2);
1215 AddToISelQueue(Tmp3);
Evan Chengb1409ce2006-11-17 22:10:14 +00001216 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001217 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001218 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001219 Chain = SDOperand(CNode, 0);
1220 InFlag = SDOperand(CNode, 1);
Evan Cheng948f3432006-01-06 23:19:29 +00001221 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001222 AddToISelQueue(N1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001223 Chain = CurDAG->getEntryNode();
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001224 InFlag =
1225 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001226 }
1227
Evan Chengb1409ce2006-11-17 22:10:14 +00001228 SDOperand Result =
1229 CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg, NVT, InFlag);
Evan Cheng2ef88a02006-08-07 22:28:20 +00001230 ReplaceUses(N.getValue(0), Result);
1231 if (foldedLoad)
1232 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Chengf597dc72006-02-10 22:24:32 +00001233
1234#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001235 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Chengf597dc72006-02-10 22:24:32 +00001236 DEBUG(Result.Val->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001237 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001238 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001239#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001240
1241 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001242 }
Evan Cheng403be7e2006-05-08 08:01:26 +00001243
1244 case ISD::TRUNCATE: {
Evan Cheng25ab6902006-09-08 06:48:29 +00001245 if (!Subtarget->is64Bit() && NVT == MVT::i8) {
Evan Cheng403be7e2006-05-08 08:01:26 +00001246 unsigned Opc2;
1247 MVT::ValueType VT;
1248 switch (Node->getOperand(0).getValueType()) {
1249 default: assert(0 && "Unknown truncate!");
1250 case MVT::i16:
1251 Opc = X86::MOV16to16_;
1252 VT = MVT::i16;
Evan Cheng25ab6902006-09-08 06:48:29 +00001253 Opc2 = X86::TRUNC_16_to8;
Evan Cheng403be7e2006-05-08 08:01:26 +00001254 break;
1255 case MVT::i32:
1256 Opc = X86::MOV32to32_;
1257 VT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00001258 Opc2 = X86::TRUNC_32_to8;
Evan Cheng403be7e2006-05-08 08:01:26 +00001259 break;
1260 }
1261
Evan Cheng04699902006-08-26 01:05:16 +00001262 AddToISelQueue(Node->getOperand(0));
1263 SDOperand Tmp =
1264 SDOperand(CurDAG->getTargetNode(Opc, VT, Node->getOperand(0)), 0);
Evan Cheng9ade2182006-08-26 05:34:46 +00001265 SDNode *ResNode = CurDAG->getTargetNode(Opc2, NVT, Tmp);
Evan Cheng403be7e2006-05-08 08:01:26 +00001266
1267#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001268 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001269 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001270 DOUT << "\n";
Evan Cheng403be7e2006-05-08 08:01:26 +00001271 Indent -= 2;
1272#endif
Evan Cheng9ade2182006-08-26 05:34:46 +00001273 return ResNode;
Evan Cheng403be7e2006-05-08 08:01:26 +00001274 }
Evan Cheng6b2e2542006-05-20 07:44:28 +00001275
1276 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001277 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001278 }
1279
Evan Cheng9ade2182006-08-26 05:34:46 +00001280 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001281
Evan Chengf597dc72006-02-10 22:24:32 +00001282#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001283 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001284 if (ResNode == NULL || ResNode == N.Val)
1285 DEBUG(N.Val->dump(CurDAG));
1286 else
1287 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001288 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001289 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001290#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001291
1292 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001293}
1294
Chris Lattnerc0bad572006-06-08 18:03:49 +00001295bool X86DAGToDAGISel::
1296SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1297 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1298 SDOperand Op0, Op1, Op2, Op3;
1299 switch (ConstraintCode) {
1300 case 'o': // offsetable ??
1301 case 'v': // not offsetable ??
1302 default: return true;
1303 case 'm': // memory
Evan Cheng0d538262006-11-08 20:34:28 +00001304 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001305 return true;
1306 break;
1307 }
1308
Evan Cheng04699902006-08-26 01:05:16 +00001309 OutOps.push_back(Op0);
1310 OutOps.push_back(Op1);
1311 OutOps.push_back(Op2);
1312 OutOps.push_back(Op3);
1313 AddToISelQueue(Op0);
1314 AddToISelQueue(Op1);
1315 AddToISelQueue(Op2);
1316 AddToISelQueue(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001317 return false;
1318}
1319
Chris Lattnerc961eea2005-11-16 01:54:32 +00001320/// createX86ISelDag - This pass converts a legalized DAG into a
1321/// X86-specific DAG, ready for instruction scheduling.
1322///
Evan Chenge50794a2006-08-29 18:28:33 +00001323FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1324 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001325}