blob: 863e6d8a14de18063e3bd8556c48965fc056ae84 [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"
Evan Cheng2ef88a02006-08-07 22:28:20 +000037#include <queue>
Evan Chengba2f0a92006-02-05 06:46:41 +000038#include <set>
Chris Lattnerc961eea2005-11-16 01:54:32 +000039using namespace llvm;
40
41//===----------------------------------------------------------------------===//
42// Pattern Matcher Implementation
43//===----------------------------------------------------------------------===//
44
45namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000046 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
47 /// SDOperand's instead of register numbers for the leaves of the matched
48 /// tree.
49 struct X86ISelAddressMode {
50 enum {
51 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000052 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000053 } BaseType;
54
55 struct { // This is really a union, discriminated by BaseType!
56 SDOperand Reg;
57 int FrameIndex;
58 } Base;
59
Evan Cheng25ab6902006-09-08 06:48:29 +000060 bool isRIPRel; // RIP relative?
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000061 unsigned Scale;
62 SDOperand IndexReg;
63 unsigned Disp;
64 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000065 Constant *CP;
Evan Cheng25ab6902006-09-08 06:48:29 +000066 const char *ES;
67 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000068 unsigned Align; // CP alignment.
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000069
70 X86ISelAddressMode()
Evan Cheng25ab6902006-09-08 06:48:29 +000071 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0),
72 GV(0), CP(0), ES(0), JT(-1), Align(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000073 }
74 };
75}
76
77namespace {
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000078 Statistic
Chris Lattnerc961eea2005-11-16 01:54:32 +000079 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
80
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000081 Statistic
Evan Cheng82a35b32006-08-29 06:44:17 +000082 NumLoadMoved("x86-codegen", "Number of loads moved below TokenFactor");
83
Chris Lattnerc961eea2005-11-16 01:54:32 +000084 //===--------------------------------------------------------------------===//
85 /// ISel - X86 specific code to select X86 machine instructions for
86 /// SelectionDAG operations.
87 ///
Chris Lattner2c79de82006-06-28 23:27:49 +000088 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +000089 /// ContainsFPCode - Every instruction we select that uses or defines a FP
90 /// register should set this to true.
91 bool ContainsFPCode;
92
Evan Chenge50794a2006-08-29 18:28:33 +000093 /// FastISel - Enable fast(er) instruction selection.
94 ///
95 bool FastISel;
96
Evan Cheng25ab6902006-09-08 06:48:29 +000097 /// TM - Keep a reference to X86TargetMachine.
98 ///
99 X86TargetMachine &TM;
100
Chris Lattnerc961eea2005-11-16 01:54:32 +0000101 /// X86Lowering - This object fully describes how to lower LLVM code to an
102 /// X86-specific SelectionDAG.
103 X86TargetLowering X86Lowering;
104
105 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
106 /// make the right decision when generating code for different targets.
107 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000108
Evan Cheng25ab6902006-09-08 06:48:29 +0000109 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
110 /// base register.
Evan Cheng7ccced62006-02-18 00:15:05 +0000111 unsigned GlobalBaseReg;
Evan Chenga8df1b42006-07-27 16:44:36 +0000112
Chris Lattnerc961eea2005-11-16 01:54:32 +0000113 public:
Evan Cheng25ab6902006-09-08 06:48:29 +0000114 X86DAGToDAGISel(X86TargetMachine &tm, bool fast)
Evan Chengc4c62572006-03-13 23:20:37 +0000115 : SelectionDAGISel(X86Lowering),
Evan Cheng25ab6902006-09-08 06:48:29 +0000116 ContainsFPCode(false), FastISel(fast), TM(tm),
Evan Chenga8df1b42006-07-27 16:44:36 +0000117 X86Lowering(*TM.getTargetLowering()),
Evan Chengf4b4c412006-08-08 00:31:00 +0000118 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000119
Evan Cheng7ccced62006-02-18 00:15:05 +0000120 virtual bool runOnFunction(Function &Fn) {
121 // Make sure we re-emit a set of the global base reg if necessary
122 GlobalBaseReg = 0;
123 return SelectionDAGISel::runOnFunction(Fn);
124 }
125
Chris Lattnerc961eea2005-11-16 01:54:32 +0000126 virtual const char *getPassName() const {
127 return "X86 DAG->DAG Instruction Selection";
128 }
129
130 /// InstructionSelectBasicBlock - This callback is invoked by
131 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
132 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
133
Evan Cheng8700e142006-01-11 06:09:51 +0000134 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
135
Evan Cheng27e1fe92006-10-14 08:33:25 +0000136 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root);
Evan Chenga8df1b42006-07-27 16:44:36 +0000137
Chris Lattnerc961eea2005-11-16 01:54:32 +0000138// Include the pieces autogenerated from the target description.
139#include "X86GenDAGISel.inc"
140
141 private:
Evan Cheng9ade2182006-08-26 05:34:46 +0000142 SDNode *Select(SDOperand N);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000143
Evan Cheng2486af12006-02-11 02:05:36 +0000144 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM, bool isRoot = true);
Evan Cheng0d538262006-11-08 20:34:28 +0000145 bool SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
146 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
147 bool SelectLEAAddr(SDOperand Op, SDOperand N, SDOperand &Base,
148 SDOperand &Scale, SDOperand &Index, SDOperand &Disp);
149 bool SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Cheng07e4b002006-10-16 06:34:55 +0000150 SDOperand N, SDOperand &Base, SDOperand &Scale,
Evan Cheng82a91642006-10-11 21:06:01 +0000151 SDOperand &Index, SDOperand &Disp,
152 SDOperand &InChain, SDOperand &OutChain);
Evan Cheng5e351682006-02-06 06:02:33 +0000153 bool TryFoldLoad(SDOperand P, SDOperand N,
154 SDOperand &Base, SDOperand &Scale,
Evan Cheng0114e942006-01-06 20:36:21 +0000155 SDOperand &Index, SDOperand &Disp);
Evan Cheng70e674e2006-08-28 20:10:17 +0000156 void InstructionSelectPreprocess(SelectionDAG &DAG);
Evan Cheng2ef88a02006-08-07 22:28:20 +0000157
Chris Lattnerc0bad572006-06-08 18:03:49 +0000158 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
159 /// inline asm expressions.
160 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
161 char ConstraintCode,
162 std::vector<SDOperand> &OutOps,
163 SelectionDAG &DAG);
164
Evan Cheng3649b0e2006-06-02 22:38:37 +0000165 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
166
Evan Chenge5280532005-12-12 21:49:40 +0000167 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
168 SDOperand &Scale, SDOperand &Index,
169 SDOperand &Disp) {
170 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000171 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
172 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000173 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000174 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000175 // These are 32-bit even in 64-bit mode since RIP relative offset
176 // is 32-bit.
177 if (AM.GV)
178 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp);
179 else if (AM.CP)
180 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp);
181 else if (AM.ES)
182 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32);
183 else if (AM.JT != -1)
184 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32);
185 else
186 Disp = getI32Imm(AM.Disp);
Evan Chenge5280532005-12-12 21:49:40 +0000187 }
188
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000189 /// getI8Imm - Return a target constant with the specified value, of type
190 /// i8.
191 inline SDOperand getI8Imm(unsigned Imm) {
192 return CurDAG->getTargetConstant(Imm, MVT::i8);
193 }
194
Chris Lattnerc961eea2005-11-16 01:54:32 +0000195 /// getI16Imm - Return a target constant with the specified value, of type
196 /// i16.
197 inline SDOperand getI16Imm(unsigned Imm) {
198 return CurDAG->getTargetConstant(Imm, MVT::i16);
199 }
200
201 /// getI32Imm - Return a target constant with the specified value, of type
202 /// i32.
203 inline SDOperand getI32Imm(unsigned Imm) {
204 return CurDAG->getTargetConstant(Imm, MVT::i32);
205 }
Evan Chengf597dc72006-02-10 22:24:32 +0000206
Evan Cheng7ccced62006-02-18 00:15:05 +0000207 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
208 /// base register. Return the virtual register that holds this value.
Evan Cheng9ade2182006-08-26 05:34:46 +0000209 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000210
Evan Cheng23addc02006-02-10 22:46:26 +0000211#ifndef NDEBUG
212 unsigned Indent;
213#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000214 };
215}
216
Evan Chenga275ecb2006-10-10 01:46:56 +0000217static SDNode *findFlagUse(SDNode *N) {
218 unsigned FlagResNo = N->getNumValues()-1;
219 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
220 SDNode *User = *I;
221 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
222 SDOperand Op = User->getOperand(i);
Evan Cheng494cec62006-10-12 19:13:59 +0000223 if (Op.Val == N && Op.ResNo == FlagResNo)
Evan Chenga275ecb2006-10-10 01:46:56 +0000224 return User;
225 }
226 }
227 return NULL;
228}
229
Evan Cheng27e1fe92006-10-14 08:33:25 +0000230static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse,
231 SDNode *Root, SDNode *Skip, bool &found,
Evan Chengf4b4c412006-08-08 00:31:00 +0000232 std::set<SDNode *> &Visited) {
233 if (found ||
234 Use->getNodeId() > Def->getNodeId() ||
235 !Visited.insert(Use).second)
236 return;
237
Evan Cheng27e1fe92006-10-14 08:33:25 +0000238 for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000239 SDNode *N = Use->getOperand(i).Val;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000240 if (N == Skip)
Evan Chenga275ecb2006-10-10 01:46:56 +0000241 continue;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000242 if (N == Def) {
243 if (Use == ImmedUse)
244 continue; // Immediate use is ok.
245 if (Use == Root) {
246 assert(Use->getOpcode() == ISD::STORE ||
247 Use->getOpcode() == X86ISD::CMP);
248 continue;
249 }
Evan Chengf4b4c412006-08-08 00:31:00 +0000250 found = true;
251 break;
252 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000253 findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000254 }
255}
256
Evan Cheng27e1fe92006-10-14 08:33:25 +0000257/// isNonImmUse - Start searching from Root up the DAG to check is Def can
258/// be reached. Return true if that's the case. However, ignore direct uses
259/// by ImmedUse (which would be U in the example illustrated in
260/// CanBeFoldedBy) and by Root (which can happen in the store case).
261/// FIXME: to be really generic, we should allow direct use by any node
262/// that is being folded. But realisticly since we only fold loads which
263/// have one non-chain use, we only need to watch out for load/op/store
264/// and load/op/cmp case where the root (store / cmp) may reach the load via
265/// its chain operand.
266static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse,
267 SDNode *Skip = NULL) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000268 std::set<SDNode *> Visited;
269 bool found = false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000270 findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000271 return found;
272}
273
274
Evan Cheng27e1fe92006-10-14 08:33:25 +0000275bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) {
276 if (FastISel) return false;
277
Evan Chenga8df1b42006-07-27 16:44:36 +0000278 // If U use can somehow reach N through another path then U can't fold N or
279 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Cheng37e18032006-07-28 06:33:41 +0000280 // through X. If N is folded into into U, then X is both a predecessor and
Evan Chenga8df1b42006-07-27 16:44:36 +0000281 // a successor of U.
282 //
283 // [ N ]
284 // ^ ^
285 // | |
286 // / \---
287 // / [X]
288 // | ^
289 // [U]--------|
Evan Cheng27e1fe92006-10-14 08:33:25 +0000290
291 if (isNonImmUse(Root, N, U))
292 return false;
293
294 // If U produces a flag, then it gets (even more) interesting. Since it
295 // would have been "glued" together with its flag use, we need to check if
296 // it might reach N:
297 //
298 // [ N ]
299 // ^ ^
300 // | |
301 // [U] \--
302 // ^ [TF]
303 // | ^
304 // | |
305 // \ /
306 // [FU]
307 //
308 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
309 // NU), then TF is a predecessor of FU and a successor of NU. But since
310 // NU and FU are flagged together, this effectively creates a cycle.
311 bool HasFlagUse = false;
312 MVT::ValueType VT = Root->getValueType(Root->getNumValues()-1);
313 while ((VT == MVT::Flag && !Root->use_empty())) {
314 SDNode *FU = findFlagUse(Root);
315 if (FU == NULL)
316 break;
317 else {
318 Root = FU;
319 HasFlagUse = true;
Evan Chenga275ecb2006-10-10 01:46:56 +0000320 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000321 VT = Root->getValueType(Root->getNumValues()-1);
Evan Chenga275ecb2006-10-10 01:46:56 +0000322 }
Evan Cheng27e1fe92006-10-14 08:33:25 +0000323
324 if (HasFlagUse)
325 return !isNonImmUse(Root, N, Root, U);
326 return true;
Evan Chenga8df1b42006-07-27 16:44:36 +0000327}
328
Evan Cheng70e674e2006-08-28 20:10:17 +0000329/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
330/// and move load below the TokenFactor. Replace store's chain operand with
331/// load's chain result.
332static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
333 SDOperand Store, SDOperand TF) {
334 std::vector<SDOperand> Ops;
335 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
336 if (Load.Val == TF.Val->getOperand(i).Val)
337 Ops.push_back(Load.Val->getOperand(0));
338 else
339 Ops.push_back(TF.Val->getOperand(i));
340 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
341 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
342 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
343 Store.getOperand(2), Store.getOperand(3));
344}
345
346/// InstructionSelectPreprocess - Preprocess the DAG to allow the instruction
347/// selector to pick more load-modify-store instructions. This is a common
348/// case:
349///
350/// [Load chain]
351/// ^
352/// |
353/// [Load]
354/// ^ ^
355/// | |
356/// / \-
357/// / |
358/// [TokenFactor] [Op]
359/// ^ ^
360/// | |
361/// \ /
362/// \ /
363/// [Store]
364///
365/// The fact the store's chain operand != load's chain will prevent the
366/// (store (op (load))) instruction from being selected. We can transform it to:
367///
368/// [Load chain]
369/// ^
370/// |
371/// [TokenFactor]
372/// ^
373/// |
374/// [Load]
375/// ^ ^
376/// | |
377/// | \-
378/// | |
379/// | [Op]
380/// | ^
381/// | |
382/// \ /
383/// \ /
384/// [Store]
385void X86DAGToDAGISel::InstructionSelectPreprocess(SelectionDAG &DAG) {
386 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
387 E = DAG.allnodes_end(); I != E; ++I) {
Evan Cheng8b2794a2006-10-13 21:14:26 +0000388 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000389 continue;
390 SDOperand Chain = I->getOperand(0);
391 if (Chain.Val->getOpcode() != ISD::TokenFactor)
392 continue;
393
394 SDOperand N1 = I->getOperand(1);
395 SDOperand N2 = I->getOperand(2);
Evan Cheng1453de52006-09-01 22:52:28 +0000396 if (MVT::isFloatingPoint(N1.getValueType()) ||
397 MVT::isVector(N1.getValueType()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000398 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000399 continue;
400
401 bool RModW = false;
402 SDOperand Load;
403 unsigned Opcode = N1.Val->getOpcode();
404 switch (Opcode) {
405 case ISD::ADD:
406 case ISD::MUL:
Evan Cheng70e674e2006-08-28 20:10:17 +0000407 case ISD::AND:
408 case ISD::OR:
409 case ISD::XOR:
410 case ISD::ADDC:
411 case ISD::ADDE: {
412 SDOperand N10 = N1.getOperand(0);
413 SDOperand N11 = N1.getOperand(1);
Evan Cheng466685d2006-10-09 20:57:25 +0000414 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng70e674e2006-08-28 20:10:17 +0000415 RModW = true;
Evan Cheng466685d2006-10-09 20:57:25 +0000416 else if (ISD::isNON_EXTLoad(N11.Val)) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000417 RModW = true;
418 std::swap(N10, N11);
419 }
420 RModW = RModW && N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000421 (N10.getOperand(1) == N2) &&
422 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000423 if (RModW)
424 Load = N10;
425 break;
426 }
427 case ISD::SUB:
428 case ISD::SHL:
429 case ISD::SRA:
430 case ISD::SRL:
431 case ISD::ROTL:
432 case ISD::ROTR:
433 case ISD::SUBC:
434 case ISD::SUBE:
435 case X86ISD::SHLD:
436 case X86ISD::SHRD: {
437 SDOperand N10 = N1.getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +0000438 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng70e674e2006-08-28 20:10:17 +0000439 RModW = N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000440 (N10.getOperand(1) == N2) &&
441 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000442 if (RModW)
443 Load = N10;
444 break;
445 }
446 }
447
Evan Cheng82a35b32006-08-29 06:44:17 +0000448 if (RModW) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000449 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000450 ++NumLoadMoved;
451 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000452 }
453}
454
Chris Lattnerc961eea2005-11-16 01:54:32 +0000455/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
456/// when it has created a SelectionDAG for us to codegen.
457void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
458 DEBUG(BB->dump());
Chris Lattner92cb0af2006-01-11 01:15:34 +0000459 MachineFunction::iterator FirstMBB = BB;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000460
Evan Chenge50794a2006-08-29 18:28:33 +0000461 if (!FastISel)
Evan Cheng70e674e2006-08-28 20:10:17 +0000462 InstructionSelectPreprocess(DAG);
463
Chris Lattnerc961eea2005-11-16 01:54:32 +0000464 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000465#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000466 DOUT << "===== Instruction selection begins:\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000467 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000468#endif
Evan Chengba2f0a92006-02-05 06:46:41 +0000469 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengf597dc72006-02-10 22:24:32 +0000470#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000471 DOUT << "===== Instruction selection ends:\n";
Evan Chengf597dc72006-02-10 22:24:32 +0000472#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000473
Chris Lattnerc961eea2005-11-16 01:54:32 +0000474 DAG.RemoveDeadNodes();
475
476 // Emit machine code to BB.
477 ScheduleAndEmitDAG(DAG);
Chris Lattner92cb0af2006-01-11 01:15:34 +0000478
479 // If we are emitting FP stack code, scan the basic block to determine if this
480 // block defines any FP values. If so, put an FP_REG_KILL instruction before
481 // the terminator of the block.
Evan Cheng559806f2006-01-27 08:10:46 +0000482 if (!Subtarget->hasSSE2()) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000483 // Note that FP stack instructions *are* used in SSE code when returning
484 // values, but these are not live out of the basic block, so we don't need
485 // an FP_REG_KILL in this case either.
486 bool ContainsFPCode = false;
487
488 // Scan all of the machine instructions in these MBBs, checking for FP
489 // stores.
490 MachineFunction::iterator MBBI = FirstMBB;
491 do {
492 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
493 !ContainsFPCode && I != E; ++I) {
494 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
495 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
496 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
497 RegMap->getRegClass(I->getOperand(0).getReg()) ==
498 X86::RFPRegisterClass) {
499 ContainsFPCode = true;
500 break;
501 }
502 }
503 }
504 } while (!ContainsFPCode && &*(MBBI++) != BB);
505
506 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
507 // a copy of the input value in this block.
508 if (!ContainsFPCode) {
509 // Final check, check LLVM BB's that are successors to the LLVM BB
510 // corresponding to BB for FP PHI nodes.
511 const BasicBlock *LLVMBB = BB->getBasicBlock();
512 const PHINode *PN;
513 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
514 !ContainsFPCode && SI != E; ++SI) {
515 for (BasicBlock::const_iterator II = SI->begin();
516 (PN = dyn_cast<PHINode>(II)); ++II) {
517 if (PN->getType()->isFloatingPoint()) {
518 ContainsFPCode = true;
519 break;
520 }
521 }
522 }
523 }
524
525 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
526 if (ContainsFPCode) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000527 BuildMI(*BB, BB->getFirstTerminator(),
528 TM.getInstrInfo()->get(X86::FP_REG_KILL));
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) {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000538 const TargetInstrInfo *TII = TM.getInstrInfo();
Anton Korobeynikovbcb97702006-09-17 20:25:45 +0000539 if (Subtarget->isTargetCygwin())
Evan Chengc0f64ff2006-11-27 23:37:22 +0000540 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Evan Cheng3649b0e2006-06-02 22:38:37 +0000541
Evan Cheng8700e142006-01-11 06:09:51 +0000542 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
543 int CWFrameIdx = MFI->CreateStackObject(2, 2);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000544 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng8700e142006-01-11 06:09:51 +0000545
546 // Set the high part to be 64-bit precision.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000547 addFrameReference(BuildMI(BB, TII->get(X86::MOV8mi)),
Evan Cheng8700e142006-01-11 06:09:51 +0000548 CWFrameIdx, 1).addImm(2);
549
550 // Reload the modified control word now.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000551 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng8700e142006-01-11 06:09:51 +0000552}
553
554void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
555 // If this is main, emit special code for main.
556 MachineBasicBlock *BB = MF.begin();
557 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
558 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
559}
560
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000561/// MatchAddress - Add the specified node to the specified addressing mode,
562/// returning true if it cannot be done. This just pattern matches for the
563/// addressing mode
Evan Cheng2486af12006-02-11 02:05:36 +0000564bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
565 bool isRoot) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000566 // RIP relative addressing: %rip + 32-bit displacement!
567 if (AM.isRIPRel) {
568 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000569 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000570 if (isInt32(AM.Disp + Val)) {
571 AM.Disp += Val;
572 return false;
573 }
574 }
575 return true;
576 }
577
Evan Cheng2ef88a02006-08-07 22:28:20 +0000578 int id = N.Val->getNodeId();
579 bool Available = isSelected(id);
Evan Cheng2486af12006-02-11 02:05:36 +0000580
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000581 switch (N.getOpcode()) {
582 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000583 case ISD::Constant: {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000584 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000585 if (isInt32(AM.Disp + Val)) {
586 AM.Disp += Val;
587 return false;
588 }
589 break;
590 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000591
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000592 case X86ISD::Wrapper: {
593 bool is64Bit = Subtarget->is64Bit();
Evan Cheng0085a282006-11-30 21:55:46 +0000594 // Under X86-64 non-small code model, GV (and friends) are 64-bits.
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000595 if (is64Bit && TM.getCodeModel() != CodeModel::Small)
Evan Cheng0085a282006-11-30 21:55:46 +0000596 break;
Evan Cheng28b514392006-12-05 19:50:18 +0000597 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1)
598 break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000599 // If value is available in a register both base and index components have
600 // been picked, we can't fit the result available in the register in the
601 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
Evan Cheng49463992006-11-29 23:46:27 +0000602 if (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
Evan Cheng28b514392006-12-05 19:50:18 +0000603 bool isStatic = TM.getRelocationModel() == Reloc::Static;
604 SDOperand N0 = N.getOperand(0);
605 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
606 GlobalValue *GV = G->getGlobal();
Evan Chenga70d14b2006-12-19 21:31:42 +0000607 bool isAbs32 = !is64Bit || isStatic;
Evan Cheng28b514392006-12-05 19:50:18 +0000608 if (isAbs32 || isRoot) {
Evan Chenga70d14b2006-12-19 21:31:42 +0000609 AM.GV = GV;
Evan Cheng28b514392006-12-05 19:50:18 +0000610 AM.Disp += G->getOffset();
611 AM.isRIPRel = !isAbs32;
612 return false;
613 }
614 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
615 if (!is64Bit || isStatic || isRoot) {
Evan Chengc356a572006-09-12 21:04:05 +0000616 AM.CP = CP->getConstVal();
Evan Cheng51a9ed92006-02-25 10:09:08 +0000617 AM.Align = CP->getAlignment();
618 AM.Disp += CP->getOffset();
Evan Cheng28b514392006-12-05 19:50:18 +0000619 AM.isRIPRel = !isStatic;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000620 return false;
621 }
Evan Cheng28b514392006-12-05 19:50:18 +0000622 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) {
623 if (isStatic || isRoot) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000624 AM.ES = S->getSymbol();
Evan Cheng28b514392006-12-05 19:50:18 +0000625 AM.isRIPRel = !isStatic;
Evan Cheng25ab6902006-09-08 06:48:29 +0000626 return false;
Evan Cheng28b514392006-12-05 19:50:18 +0000627 }
628 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
629 if (isStatic || isRoot) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000630 AM.JT = J->getIndex();
Evan Cheng28b514392006-12-05 19:50:18 +0000631 AM.isRIPRel = !isStatic;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000632 return false;
633 }
634 }
635 }
636 break;
Evan Cheng0085a282006-11-30 21:55:46 +0000637 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000638
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000639 case ISD::FrameIndex:
640 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
641 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
642 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
643 return false;
644 }
645 break;
Evan Chengec693f72005-12-08 02:01:35 +0000646
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000647 case ISD::SHL:
Evan Cheng51a9ed92006-02-25 10:09:08 +0000648 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000649 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
650 unsigned Val = CN->getValue();
651 if (Val == 1 || Val == 2 || Val == 3) {
652 AM.Scale = 1 << Val;
653 SDOperand ShVal = N.Val->getOperand(0);
654
655 // Okay, we know that we have a scale by now. However, if the scaled
656 // value is an add of something and a constant, we can fold the
657 // constant into the disp field here.
658 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
659 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
660 AM.IndexReg = ShVal.Val->getOperand(0);
661 ConstantSDNode *AddVal =
662 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
Jeff Cohend41b30d2006-11-05 19:31:28 +0000663 uint64_t Disp = AM.Disp + (AddVal->getValue() << Val);
Evan Cheng25ab6902006-09-08 06:48:29 +0000664 if (isInt32(Disp))
665 AM.Disp = Disp;
666 else
667 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000668 } else {
669 AM.IndexReg = ShVal;
670 }
671 return false;
672 }
673 }
674 break;
Evan Chengec693f72005-12-08 02:01:35 +0000675
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000676 case ISD::MUL:
677 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng51a9ed92006-02-25 10:09:08 +0000678 if (!Available &&
679 AM.BaseType == X86ISelAddressMode::RegBase &&
680 AM.Base.Reg.Val == 0 &&
681 AM.IndexReg.Val == 0)
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000682 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
683 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
684 AM.Scale = unsigned(CN->getValue())-1;
685
686 SDOperand MulVal = N.Val->getOperand(0);
687 SDOperand Reg;
688
689 // Okay, we know that we have a scale by now. However, if the scaled
690 // value is an add of something and a constant, we can fold the
691 // constant into the disp field here.
692 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
693 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
694 Reg = MulVal.Val->getOperand(0);
695 ConstantSDNode *AddVal =
696 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
Evan Cheng25ab6902006-09-08 06:48:29 +0000697 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
698 if (isInt32(Disp))
699 AM.Disp = Disp;
700 else
701 Reg = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000702 } else {
703 Reg = N.Val->getOperand(0);
704 }
705
706 AM.IndexReg = AM.Base.Reg = Reg;
707 return false;
708 }
709 break;
710
711 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;
724 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000725
726 case ISD::OR: {
727 if (!Available) {
728 X86ISelAddressMode Backup = AM;
729 // Look for (x << c1) | c2 where (c2 < c1)
730 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(0));
731 if (CN && !MatchAddress(N.Val->getOperand(1), AM, false)) {
732 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
733 AM.Disp = CN->getValue();
734 return false;
735 }
736 }
737 AM = Backup;
738 CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1));
739 if (CN && !MatchAddress(N.Val->getOperand(0), AM, false)) {
740 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
741 AM.Disp = CN->getValue();
742 return false;
743 }
744 }
745 AM = Backup;
746 }
747 break;
748 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000749 }
750
751 // Is the base register already occupied?
752 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
753 // If so, check to see if the scale index register is set.
754 if (AM.IndexReg.Val == 0) {
755 AM.IndexReg = N;
756 AM.Scale = 1;
757 return false;
758 }
759
760 // Otherwise, we cannot select it.
761 return true;
762 }
763
764 // Default, generate it as a register.
765 AM.BaseType = X86ISelAddressMode::RegBase;
766 AM.Base.Reg = N;
767 return false;
768}
769
Evan Chengec693f72005-12-08 02:01:35 +0000770/// SelectAddr - returns true if it is able pattern match an addressing mode.
771/// It returns the operands which make up the maximal addressing mode it can
772/// match by reference.
Evan Cheng0d538262006-11-08 20:34:28 +0000773bool X86DAGToDAGISel::SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base,
774 SDOperand &Scale, SDOperand &Index,
775 SDOperand &Disp) {
Evan Chengec693f72005-12-08 02:01:35 +0000776 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +0000777 if (MatchAddress(N, AM))
778 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000779
Evan Cheng25ab6902006-09-08 06:48:29 +0000780 MVT::ValueType VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +0000781 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Cheng7dd281b2006-02-05 05:25:07 +0000782 if (!AM.Base.Reg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000783 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +0000784 }
Evan Cheng8700e142006-01-11 06:09:51 +0000785
Evan Cheng7dd281b2006-02-05 05:25:07 +0000786 if (!AM.IndexReg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000787 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +0000788
789 getAddressOperands(AM, Base, Scale, Index, Disp);
790 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000791}
792
Chris Lattner4fe4f252006-10-11 22:09:58 +0000793/// isZeroNode - Returns true if Elt is a constant zero or a floating point
794/// constant +0.0.
795static inline bool isZeroNode(SDOperand Elt) {
796 return ((isa<ConstantSDNode>(Elt) &&
797 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
798 (isa<ConstantFPSDNode>(Elt) &&
799 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
800}
801
802
Chris Lattner3a7cd952006-10-07 21:55:32 +0000803/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
804/// match a load whose top elements are either undef or zeros. The load flavor
805/// is derived from the type of N, which is either v4f32 or v2f64.
Evan Cheng0d538262006-11-08 20:34:28 +0000806bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand Op, SDOperand Pred,
Evan Cheng07e4b002006-10-16 06:34:55 +0000807 SDOperand N, SDOperand &Base,
Evan Cheng82a91642006-10-11 21:06:01 +0000808 SDOperand &Scale, SDOperand &Index,
809 SDOperand &Disp, SDOperand &InChain,
810 SDOperand &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +0000811 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +0000812 InChain = N.getOperand(0).getValue(1);
Evan Cheng07e4b002006-10-16 06:34:55 +0000813 if (ISD::isNON_EXTLoad(InChain.Val) &&
814 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +0000815 N.hasOneUse() &&
Evan Cheng0d538262006-11-08 20:34:28 +0000816 CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) {
Evan Cheng82a91642006-10-11 21:06:01 +0000817 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Evan Cheng0d538262006-11-08 20:34:28 +0000818 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner3a7cd952006-10-07 21:55:32 +0000819 return false;
Evan Cheng82a91642006-10-11 21:06:01 +0000820 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +0000821 return true;
822 }
823 }
Chris Lattner4fe4f252006-10-11 22:09:58 +0000824
825 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +0000826 // elements. This is a vector shuffle from the zero vector.
Chris Lattner4fe4f252006-10-11 22:09:58 +0000827 if (N.getOpcode() == ISD::VECTOR_SHUFFLE && N.Val->hasOneUse() &&
828 N.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
829 N.getOperand(1).getOpcode() == ISD::SCALAR_TO_VECTOR &&
830 N.getOperand(1).Val->hasOneUse() &&
831 ISD::isNON_EXTLoad(N.getOperand(1).getOperand(0).Val) &&
832 N.getOperand(1).getOperand(0).hasOneUse()) {
833 // Check to see if the BUILD_VECTOR is building a zero vector.
834 SDOperand BV = N.getOperand(0);
835 for (unsigned i = 0, e = BV.getNumOperands(); i != e; ++i)
836 if (!isZeroNode(BV.getOperand(i)) &&
837 BV.getOperand(i).getOpcode() != ISD::UNDEF)
838 return false; // Not a zero/undef vector.
839 // Check to see if the shuffle mask is 4/L/L/L or 2/L, where L is something
840 // from the LHS.
841 unsigned VecWidth = BV.getNumOperands();
842 SDOperand ShufMask = N.getOperand(2);
843 assert(ShufMask.getOpcode() == ISD::BUILD_VECTOR && "Invalid shuf mask!");
844 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(ShufMask.getOperand(0))) {
845 if (C->getValue() == VecWidth) {
846 for (unsigned i = 1; i != VecWidth; ++i) {
847 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF) {
848 // ok.
849 } else {
850 ConstantSDNode *C = cast<ConstantSDNode>(ShufMask.getOperand(i));
851 if (C->getValue() >= VecWidth) return false;
852 }
853 }
854 }
855
856 // Okay, this is a zero extending load. Fold it.
857 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(1).getOperand(0));
Evan Cheng0d538262006-11-08 20:34:28 +0000858 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner4fe4f252006-10-11 22:09:58 +0000859 return false;
860 OutChain = LD->getChain();
861 InChain = SDOperand(LD, 1);
862 return true;
863 }
864 }
Chris Lattner3a7cd952006-10-07 21:55:32 +0000865 return false;
866}
867
868
Evan Cheng51a9ed92006-02-25 10:09:08 +0000869/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
870/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng0d538262006-11-08 20:34:28 +0000871bool X86DAGToDAGISel::SelectLEAAddr(SDOperand Op, SDOperand N,
872 SDOperand &Base, SDOperand &Scale,
Evan Cheng51a9ed92006-02-25 10:09:08 +0000873 SDOperand &Index, SDOperand &Disp) {
874 X86ISelAddressMode AM;
875 if (MatchAddress(N, AM))
876 return false;
877
Evan Cheng25ab6902006-09-08 06:48:29 +0000878 MVT::ValueType VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +0000879 unsigned Complexity = 0;
880 if (AM.BaseType == X86ISelAddressMode::RegBase)
881 if (AM.Base.Reg.Val)
882 Complexity = 1;
883 else
Evan Cheng25ab6902006-09-08 06:48:29 +0000884 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +0000885 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
886 Complexity = 4;
887
888 if (AM.IndexReg.Val)
889 Complexity++;
890 else
Evan Cheng25ab6902006-09-08 06:48:29 +0000891 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +0000892
Evan Cheng8c03fe42006-02-28 21:13:57 +0000893 if (AM.Scale > 2)
Evan Cheng51a9ed92006-02-25 10:09:08 +0000894 Complexity += 2;
Evan Cheng8c03fe42006-02-28 21:13:57 +0000895 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg
896 else if (AM.Scale > 1)
897 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000898
899 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
900 // to a LEA. This is determined with some expermentation but is by no means
901 // optimal (especially for code size consideration). LEA is nice because of
902 // its three-address nature. Tweak the cost function again when we can run
903 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +0000904 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
905 // For X86-64, we should always use lea to materialize RIP relative
906 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +0000907 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +0000908 Complexity = 4;
909 else
910 Complexity += 2;
911 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000912
913 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
914 Complexity++;
915
916 if (Complexity > 2) {
917 getAddressOperands(AM, Base, Scale, Index, Disp);
918 return true;
919 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000920 return false;
921}
922
Evan Cheng5e351682006-02-06 06:02:33 +0000923bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
924 SDOperand &Base, SDOperand &Scale,
925 SDOperand &Index, SDOperand &Disp) {
Evan Cheng466685d2006-10-09 20:57:25 +0000926 if (ISD::isNON_EXTLoad(N.Val) &&
Evan Cheng5e351682006-02-06 06:02:33 +0000927 N.hasOneUse() &&
Evan Cheng27e1fe92006-10-14 08:33:25 +0000928 CanBeFoldedBy(N.Val, P.Val, P.Val))
Evan Cheng0d538262006-11-08 20:34:28 +0000929 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp);
Evan Cheng0114e942006-01-06 20:36:21 +0000930 return false;
931}
932
Evan Cheng7ccced62006-02-18 00:15:05 +0000933/// getGlobalBaseReg - Output the instructions required to put the
934/// base address to use for accessing globals into a register.
935///
Evan Cheng9ade2182006-08-26 05:34:46 +0000936SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng25ab6902006-09-08 06:48:29 +0000937 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Evan Cheng7ccced62006-02-18 00:15:05 +0000938 if (!GlobalBaseReg) {
939 // Insert the set of GlobalBaseReg into the first MBB of the function
940 MachineBasicBlock &FirstMBB = BB->getParent()->front();
941 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
942 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
Evan Cheng069287d2006-05-16 07:21:53 +0000943 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000944 const TargetInstrInfo *TII = TM.getInstrInfo();
945 BuildMI(FirstMBB, MBBI, TII->get(X86::MovePCtoStack));
946 BuildMI(FirstMBB, MBBI, TII->get(X86::POP32r), GlobalBaseReg);
Evan Cheng7ccced62006-02-18 00:15:05 +0000947 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000948 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
Evan Cheng7ccced62006-02-18 00:15:05 +0000949}
950
Evan Chengb245d922006-05-20 01:36:52 +0000951static SDNode *FindCallStartFromCall(SDNode *Node) {
952 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
953 assert(Node->getOperand(0).getValueType() == MVT::Other &&
954 "Node doesn't have a token chain argument!");
955 return FindCallStartFromCall(Node->getOperand(0).Val);
956}
957
Evan Cheng9ade2182006-08-26 05:34:46 +0000958SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Chengdef941b2005-12-15 01:02:48 +0000959 SDNode *Node = N.Val;
960 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +0000961 unsigned Opc, MOpc;
962 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +0000963
Evan Chengf597dc72006-02-10 22:24:32 +0000964#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000965 DOUT << std::string(Indent, ' ') << "Selecting: ";
Evan Chengf597dc72006-02-10 22:24:32 +0000966 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +0000967 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000968 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000969#endif
970
Evan Cheng34167212006-02-09 00:37:58 +0000971 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengf597dc72006-02-10 22:24:32 +0000972#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +0000973 DOUT << std::string(Indent-2, ' ') << "== ";
Evan Chengf597dc72006-02-10 22:24:32 +0000974 DEBUG(Node->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +0000975 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +0000976 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000977#endif
Evan Cheng64a752f2006-08-11 09:08:15 +0000978 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +0000979 }
Evan Cheng38262ca2006-01-11 22:15:18 +0000980
Evan Cheng0114e942006-01-06 20:36:21 +0000981 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000982 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +0000983 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +0000984 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +0000985
Evan Cheng51a9ed92006-02-25 10:09:08 +0000986 case ISD::ADD: {
987 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
988 // code and is matched first so to prevent it from being turned into
989 // LEA32r X+c.
Evan Cheng25ab6902006-09-08 06:48:29 +0000990 // In 64-bit mode, use LEA to take advantage of RIP-relative addressing.
991 MVT::ValueType PtrVT = TLI.getPointerTy();
Evan Cheng51a9ed92006-02-25 10:09:08 +0000992 SDOperand N0 = N.getOperand(0);
993 SDOperand N1 = N.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +0000994 if (N.Val->getValueType(0) == PtrVT &&
Evan Cheng19f2ffc2006-12-05 04:01:03 +0000995 N0.getOpcode() == X86ISD::Wrapper &&
Evan Cheng51a9ed92006-02-25 10:09:08 +0000996 N1.getOpcode() == ISD::Constant) {
997 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
998 SDOperand C(0, 0);
999 // TODO: handle ExternalSymbolSDNode.
1000 if (GlobalAddressSDNode *G =
1001 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001002 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001003 G->getOffset() + Offset);
1004 } else if (ConstantPoolSDNode *CP =
1005 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Chengc356a572006-09-12 21:04:05 +00001006 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001007 CP->getAlignment(),
1008 CP->getOffset()+Offset);
1009 }
1010
Evan Cheng25ab6902006-09-08 06:48:29 +00001011 if (C.Val) {
1012 if (Subtarget->is64Bit()) {
1013 SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
1014 CurDAG->getRegister(0, PtrVT), C };
1015 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1016 } else
1017 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1018 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001019 }
1020
1021 // Other cases are handled by auto-generated code.
1022 break;
Evan Chenga0ea0532006-02-23 02:43:52 +00001023 }
Evan Cheng020d2e82006-02-23 20:41:18 +00001024
Evan Cheng0114e942006-01-06 20:36:21 +00001025 case ISD::MULHU:
1026 case ISD::MULHS: {
1027 if (Opcode == ISD::MULHU)
1028 switch (NVT) {
1029 default: assert(0 && "Unsupported VT!");
1030 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1031 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1032 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001033 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001034 }
1035 else
1036 switch (NVT) {
1037 default: assert(0 && "Unsupported VT!");
1038 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1039 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1040 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001041 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001042 }
1043
1044 unsigned LoReg, HiReg;
1045 switch (NVT) {
1046 default: assert(0 && "Unsupported VT!");
1047 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1048 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1049 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001050 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001051 }
1052
1053 SDOperand N0 = Node->getOperand(0);
1054 SDOperand N1 = Node->getOperand(1);
1055
1056 bool foldedLoad = false;
1057 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng5e351682006-02-06 06:02:33 +00001058 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +00001059 // MULHU and MULHS are commmutative
1060 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +00001061 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +00001062 if (foldedLoad) {
1063 N0 = Node->getOperand(1);
1064 N1 = Node->getOperand(0);
1065 }
1066 }
1067
Evan Cheng34167212006-02-09 00:37:58 +00001068 SDOperand Chain;
Evan Cheng04699902006-08-26 01:05:16 +00001069 if (foldedLoad) {
1070 Chain = N1.getOperand(0);
1071 AddToISelQueue(Chain);
1072 } else
Evan Cheng34167212006-02-09 00:37:58 +00001073 Chain = CurDAG->getEntryNode();
Evan Cheng0114e942006-01-06 20:36:21 +00001074
Evan Cheng34167212006-02-09 00:37:58 +00001075 SDOperand InFlag(0, 0);
Evan Cheng04699902006-08-26 01:05:16 +00001076 AddToISelQueue(N0);
Evan Cheng0114e942006-01-06 20:36:21 +00001077 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng34167212006-02-09 00:37:58 +00001078 N0, InFlag);
Evan Cheng0114e942006-01-06 20:36:21 +00001079 InFlag = Chain.getValue(1);
1080
1081 if (foldedLoad) {
Evan Cheng04699902006-08-26 01:05:16 +00001082 AddToISelQueue(Tmp0);
1083 AddToISelQueue(Tmp1);
1084 AddToISelQueue(Tmp2);
1085 AddToISelQueue(Tmp3);
Evan Cheng0b828e02006-08-27 08:14:06 +00001086 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001087 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001088 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001089 Chain = SDOperand(CNode, 0);
1090 InFlag = SDOperand(CNode, 1);
Evan Cheng0114e942006-01-06 20:36:21 +00001091 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001092 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001093 InFlag =
1094 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001095 }
1096
Evan Cheng9ade2182006-08-26 05:34:46 +00001097 SDOperand Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
Evan Cheng2ef88a02006-08-07 22:28:20 +00001098 ReplaceUses(N.getValue(0), Result);
1099 if (foldedLoad)
1100 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Cheng34167212006-02-09 00:37:58 +00001101
Evan Chengf597dc72006-02-10 22:24:32 +00001102#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001103 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Chengf597dc72006-02-10 22:24:32 +00001104 DEBUG(Result.Val->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001105 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001106 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001107#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001108 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001109 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001110
Evan Cheng948f3432006-01-06 23:19:29 +00001111 case ISD::SDIV:
1112 case ISD::UDIV:
1113 case ISD::SREM:
1114 case ISD::UREM: {
1115 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
1116 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
1117 if (!isSigned)
1118 switch (NVT) {
1119 default: assert(0 && "Unsupported VT!");
1120 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1121 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1122 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001123 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001124 }
1125 else
1126 switch (NVT) {
1127 default: assert(0 && "Unsupported VT!");
1128 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1129 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1130 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001131 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001132 }
1133
1134 unsigned LoReg, HiReg;
1135 unsigned ClrOpcode, SExtOpcode;
1136 switch (NVT) {
1137 default: assert(0 && "Unsupported VT!");
1138 case MVT::i8:
1139 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengb1409ce2006-11-17 22:10:14 +00001140 ClrOpcode = 0;
Evan Cheng948f3432006-01-06 23:19:29 +00001141 SExtOpcode = X86::CBW;
1142 break;
1143 case MVT::i16:
1144 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001145 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001146 SExtOpcode = X86::CWD;
1147 break;
1148 case MVT::i32:
1149 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001150 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001151 SExtOpcode = X86::CDQ;
1152 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001153 case MVT::i64:
1154 LoReg = X86::RAX; HiReg = X86::RDX;
1155 ClrOpcode = X86::MOV64r0;
1156 SExtOpcode = X86::CQO;
1157 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001158 }
1159
1160 SDOperand N0 = Node->getOperand(0);
1161 SDOperand N1 = Node->getOperand(1);
Evan Cheng34167212006-02-09 00:37:58 +00001162 SDOperand InFlag(0, 0);
Evan Chengb1409ce2006-11-17 22:10:14 +00001163 if (NVT == MVT::i8 && !isSigned) {
1164 // Special case for div8, just use a move with zero extension to AX to
1165 // clear the upper 8 bits (AH).
1166 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain;
1167 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) {
1168 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) };
1169 AddToISelQueue(N0.getOperand(0));
1170 AddToISelQueue(Tmp0);
1171 AddToISelQueue(Tmp1);
1172 AddToISelQueue(Tmp2);
1173 AddToISelQueue(Tmp3);
1174 Move =
1175 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other,
1176 Ops, 5), 0);
1177 Chain = Move.getValue(1);
1178 ReplaceUses(N0.getValue(1), Chain);
1179 } else {
1180 AddToISelQueue(N0);
1181 Move =
1182 SDOperand(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0);
1183 Chain = CurDAG->getEntryNode();
1184 }
1185 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, InFlag);
Evan Cheng948f3432006-01-06 23:19:29 +00001186 InFlag = Chain.getValue(1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001187 } else {
1188 AddToISelQueue(N0);
1189 InFlag =
1190 CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg, N0,
1191 InFlag).getValue(1);
1192 if (isSigned) {
1193 // Sign extend the low part into the high part.
1194 InFlag =
1195 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
1196 } else {
1197 // Zero out the high part, effectively zero extending the input.
1198 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
1199 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg, ClrNode,
1200 InFlag).getValue(1);
1201 }
Evan Cheng948f3432006-01-06 23:19:29 +00001202 }
1203
Evan Chengb1409ce2006-11-17 22:10:14 +00001204 SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Chain;
1205 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +00001206 if (foldedLoad) {
Evan Chengb1409ce2006-11-17 22:10:14 +00001207 AddToISelQueue(N1.getOperand(0));
Evan Cheng04699902006-08-26 01:05:16 +00001208 AddToISelQueue(Tmp0);
1209 AddToISelQueue(Tmp1);
1210 AddToISelQueue(Tmp2);
1211 AddToISelQueue(Tmp3);
Evan Chengb1409ce2006-11-17 22:10:14 +00001212 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001213 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001214 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001215 Chain = SDOperand(CNode, 0);
1216 InFlag = SDOperand(CNode, 1);
Evan Cheng948f3432006-01-06 23:19:29 +00001217 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001218 AddToISelQueue(N1);
Evan Chengb1409ce2006-11-17 22:10:14 +00001219 Chain = CurDAG->getEntryNode();
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001220 InFlag =
1221 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001222 }
1223
Evan Chengb1409ce2006-11-17 22:10:14 +00001224 SDOperand Result =
1225 CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg, NVT, InFlag);
Evan Cheng2ef88a02006-08-07 22:28:20 +00001226 ReplaceUses(N.getValue(0), Result);
1227 if (foldedLoad)
1228 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Chengf597dc72006-02-10 22:24:32 +00001229
1230#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001231 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Chengf597dc72006-02-10 22:24:32 +00001232 DEBUG(Result.Val->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001233 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001234 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001235#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001236
1237 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001238 }
Evan Cheng403be7e2006-05-08 08:01:26 +00001239
1240 case ISD::TRUNCATE: {
Evan Cheng25ab6902006-09-08 06:48:29 +00001241 if (!Subtarget->is64Bit() && NVT == MVT::i8) {
Evan Cheng403be7e2006-05-08 08:01:26 +00001242 unsigned Opc2;
1243 MVT::ValueType VT;
1244 switch (Node->getOperand(0).getValueType()) {
1245 default: assert(0 && "Unknown truncate!");
1246 case MVT::i16:
1247 Opc = X86::MOV16to16_;
1248 VT = MVT::i16;
Evan Cheng25ab6902006-09-08 06:48:29 +00001249 Opc2 = X86::TRUNC_16_to8;
Evan Cheng403be7e2006-05-08 08:01:26 +00001250 break;
1251 case MVT::i32:
1252 Opc = X86::MOV32to32_;
1253 VT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00001254 Opc2 = X86::TRUNC_32_to8;
Evan Cheng403be7e2006-05-08 08:01:26 +00001255 break;
1256 }
1257
Evan Cheng04699902006-08-26 01:05:16 +00001258 AddToISelQueue(Node->getOperand(0));
1259 SDOperand Tmp =
1260 SDOperand(CurDAG->getTargetNode(Opc, VT, Node->getOperand(0)), 0);
Evan Cheng9ade2182006-08-26 05:34:46 +00001261 SDNode *ResNode = CurDAG->getTargetNode(Opc2, NVT, Tmp);
Evan Cheng403be7e2006-05-08 08:01:26 +00001262
1263#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001264 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001265 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001266 DOUT << "\n";
Evan Cheng403be7e2006-05-08 08:01:26 +00001267 Indent -= 2;
1268#endif
Evan Cheng9ade2182006-08-26 05:34:46 +00001269 return ResNode;
Evan Cheng403be7e2006-05-08 08:01:26 +00001270 }
Evan Cheng6b2e2542006-05-20 07:44:28 +00001271
1272 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001273 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001274 }
1275
Evan Cheng9ade2182006-08-26 05:34:46 +00001276 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001277
Evan Chengf597dc72006-02-10 22:24:32 +00001278#ifndef NDEBUG
Bill Wendling6345d752006-11-17 07:52:03 +00001279 DOUT << std::string(Indent-2, ' ') << "=> ";
Evan Cheng9ade2182006-08-26 05:34:46 +00001280 if (ResNode == NULL || ResNode == N.Val)
1281 DEBUG(N.Val->dump(CurDAG));
1282 else
1283 DEBUG(ResNode->dump(CurDAG));
Bill Wendling6345d752006-11-17 07:52:03 +00001284 DOUT << "\n";
Evan Cheng23addc02006-02-10 22:46:26 +00001285 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001286#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001287
1288 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001289}
1290
Chris Lattnerc0bad572006-06-08 18:03:49 +00001291bool X86DAGToDAGISel::
1292SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1293 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1294 SDOperand Op0, Op1, Op2, Op3;
1295 switch (ConstraintCode) {
1296 case 'o': // offsetable ??
1297 case 'v': // not offsetable ??
1298 default: return true;
1299 case 'm': // memory
Evan Cheng0d538262006-11-08 20:34:28 +00001300 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3))
Chris Lattnerc0bad572006-06-08 18:03:49 +00001301 return true;
1302 break;
1303 }
1304
Evan Cheng04699902006-08-26 01:05:16 +00001305 OutOps.push_back(Op0);
1306 OutOps.push_back(Op1);
1307 OutOps.push_back(Op2);
1308 OutOps.push_back(Op3);
1309 AddToISelQueue(Op0);
1310 AddToISelQueue(Op1);
1311 AddToISelQueue(Op2);
1312 AddToISelQueue(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001313 return false;
1314}
1315
Chris Lattnerc961eea2005-11-16 01:54:32 +00001316/// createX86ISelDag - This pass converts a legalized DAG into a
1317/// X86-specific DAG, ready for instruction scheduling.
1318///
Evan Chenge50794a2006-08-29 18:28:33 +00001319FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1320 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001321}