blob: 1b9212b5bf97a3977cba01a78f23675d48586ff3 [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 Chengf2dfafc2006-07-28 01:03:48 +0000137 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U);
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 Chengec693f72005-12-08 02:01:35 +0000146 bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
147 SDOperand &Index, SDOperand &Disp);
148 bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
149 SDOperand &Index, SDOperand &Disp);
Chris Lattner3a7cd952006-10-07 21:55:32 +0000150 bool SelectScalarSSELoad(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
230static void findNonImmUse(SDNode* Use, SDNode* Def, SDNode *Ignore, bool &found,
Evan Chengf4b4c412006-08-08 00:31:00 +0000231 std::set<SDNode *> &Visited) {
232 if (found ||
233 Use->getNodeId() > Def->getNodeId() ||
234 !Visited.insert(Use).second)
235 return;
236
237 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
238 SDNode *N = Use->getOperand(i).Val;
Evan Chenga275ecb2006-10-10 01:46:56 +0000239 if (N == Ignore)
240 continue;
Evan Chengf4b4c412006-08-08 00:31:00 +0000241 if (N != Def) {
Evan Chenga275ecb2006-10-10 01:46:56 +0000242 findNonImmUse(N, Def, Ignore, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000243 } else {
244 found = true;
245 break;
246 }
247 }
248}
249
Evan Chenga275ecb2006-10-10 01:46:56 +0000250static inline bool isNonImmUse(SDNode* Use, SDNode* Def, SDNode *Ignore=NULL) {
Evan Chengf4b4c412006-08-08 00:31:00 +0000251 std::set<SDNode *> Visited;
252 bool found = false;
253 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
254 SDNode *N = Use->getOperand(i).Val;
Evan Chenga275ecb2006-10-10 01:46:56 +0000255 if (N != Def && N != Ignore) {
256 findNonImmUse(N, Def, Ignore, found, Visited);
Evan Chengf4b4c412006-08-08 00:31:00 +0000257 if (found) break;
258 }
259 }
Evan Chenga275ecb2006-10-10 01:46:56 +0000260
261 if (!found && Ignore) {
262 // We must be checking for reachability between Def and a flag use. Go down
263 // recursively if Use also produces a flag.
264 MVT::ValueType VT = Use->getValueType(Use->getNumValues()-1);
265 if (VT == MVT::Flag && !Use->use_empty()) {
266 SDNode *FU = findFlagUse(Use);
267 if (FU)
268 return !isNonImmUse(FU, Def, Use);
269 }
270 }
Evan Chengf4b4c412006-08-08 00:31:00 +0000271 return found;
272}
273
274
Evan Chengf2dfafc2006-07-28 01:03:48 +0000275bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U) {
Evan Chenga8df1b42006-07-27 16:44:36 +0000276 // If U use can somehow reach N through another path then U can't fold N or
277 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Cheng37e18032006-07-28 06:33:41 +0000278 // through X. If N is folded into into U, then X is both a predecessor and
Evan Chenga8df1b42006-07-27 16:44:36 +0000279 // a successor of U.
280 //
281 // [ N ]
282 // ^ ^
283 // | |
284 // / \---
285 // / [X]
286 // | ^
287 // [U]--------|
Evan Chenga275ecb2006-10-10 01:46:56 +0000288 if (!FastISel && !isNonImmUse(U, N)) {
289 // If U produces a flag, then it gets (even more) interesting. Since it
290 // would have been "glued" together with its flag use, we need to check if
291 // it might reach N:
292 //
293 // [ N ]
294 // ^ ^
295 // | |
296 // [U] \--
297 // ^ [TF]
298 // | |
299 // \ /
300 // [FU]
301 //
302 // If FU (flag use) indirectly reach N (the load), and U fold N (call it
303 // NU), then TF is a predecessor of FU and a successor of NU. But since
304 // NU and FU are flagged together, this effectively creates a cycle.
305 MVT::ValueType VT = U->getValueType(U->getNumValues()-1);
306 if (VT == MVT::Flag && !U->use_empty()) {
307 SDNode *FU = findFlagUse(U);
308 if (FU)
309 return !isNonImmUse(FU, N, U);
310 }
311 return true;
312 }
313 return false;
Evan Chenga8df1b42006-07-27 16:44:36 +0000314}
315
Evan Cheng70e674e2006-08-28 20:10:17 +0000316/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
317/// and move load below the TokenFactor. Replace store's chain operand with
318/// load's chain result.
319static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
320 SDOperand Store, SDOperand TF) {
321 std::vector<SDOperand> Ops;
322 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
323 if (Load.Val == TF.Val->getOperand(i).Val)
324 Ops.push_back(Load.Val->getOperand(0));
325 else
326 Ops.push_back(TF.Val->getOperand(i));
327 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
328 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
329 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
330 Store.getOperand(2), Store.getOperand(3));
331}
332
333/// InstructionSelectPreprocess - Preprocess the DAG to allow the instruction
334/// selector to pick more load-modify-store instructions. This is a common
335/// case:
336///
337/// [Load chain]
338/// ^
339/// |
340/// [Load]
341/// ^ ^
342/// | |
343/// / \-
344/// / |
345/// [TokenFactor] [Op]
346/// ^ ^
347/// | |
348/// \ /
349/// \ /
350/// [Store]
351///
352/// The fact the store's chain operand != load's chain will prevent the
353/// (store (op (load))) instruction from being selected. We can transform it to:
354///
355/// [Load chain]
356/// ^
357/// |
358/// [TokenFactor]
359/// ^
360/// |
361/// [Load]
362/// ^ ^
363/// | |
364/// | \-
365/// | |
366/// | [Op]
367/// | ^
368/// | |
369/// \ /
370/// \ /
371/// [Store]
372void X86DAGToDAGISel::InstructionSelectPreprocess(SelectionDAG &DAG) {
373 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
374 E = DAG.allnodes_end(); I != E; ++I) {
375 if (I->getOpcode() != ISD::STORE)
376 continue;
377 SDOperand Chain = I->getOperand(0);
378 if (Chain.Val->getOpcode() != ISD::TokenFactor)
379 continue;
380
381 SDOperand N1 = I->getOperand(1);
382 SDOperand N2 = I->getOperand(2);
Evan Cheng1453de52006-09-01 22:52:28 +0000383 if (MVT::isFloatingPoint(N1.getValueType()) ||
384 MVT::isVector(N1.getValueType()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000385 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000386 continue;
387
388 bool RModW = false;
389 SDOperand Load;
390 unsigned Opcode = N1.Val->getOpcode();
391 switch (Opcode) {
392 case ISD::ADD:
393 case ISD::MUL:
Evan Cheng70e674e2006-08-28 20:10:17 +0000394 case ISD::AND:
395 case ISD::OR:
396 case ISD::XOR:
397 case ISD::ADDC:
398 case ISD::ADDE: {
399 SDOperand N10 = N1.getOperand(0);
400 SDOperand N11 = N1.getOperand(1);
Evan Cheng466685d2006-10-09 20:57:25 +0000401 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng70e674e2006-08-28 20:10:17 +0000402 RModW = true;
Evan Cheng466685d2006-10-09 20:57:25 +0000403 else if (ISD::isNON_EXTLoad(N11.Val)) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000404 RModW = true;
405 std::swap(N10, N11);
406 }
407 RModW = RModW && N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000408 (N10.getOperand(1) == N2) &&
409 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000410 if (RModW)
411 Load = N10;
412 break;
413 }
414 case ISD::SUB:
415 case ISD::SHL:
416 case ISD::SRA:
417 case ISD::SRL:
418 case ISD::ROTL:
419 case ISD::ROTR:
420 case ISD::SUBC:
421 case ISD::SUBE:
422 case X86ISD::SHLD:
423 case X86ISD::SHRD: {
424 SDOperand N10 = N1.getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +0000425 if (ISD::isNON_EXTLoad(N10.Val))
Evan Cheng70e674e2006-08-28 20:10:17 +0000426 RModW = N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Cheng82a35b32006-08-29 06:44:17 +0000427 (N10.getOperand(1) == N2) &&
428 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng70e674e2006-08-28 20:10:17 +0000429 if (RModW)
430 Load = N10;
431 break;
432 }
433 }
434
Evan Cheng82a35b32006-08-29 06:44:17 +0000435 if (RModW) {
Evan Cheng70e674e2006-08-28 20:10:17 +0000436 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000437 ++NumLoadMoved;
438 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000439 }
440}
441
Chris Lattnerc961eea2005-11-16 01:54:32 +0000442/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
443/// when it has created a SelectionDAG for us to codegen.
444void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
445 DEBUG(BB->dump());
Chris Lattner92cb0af2006-01-11 01:15:34 +0000446 MachineFunction::iterator FirstMBB = BB;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000447
Evan Chenge50794a2006-08-29 18:28:33 +0000448 if (!FastISel)
Evan Cheng70e674e2006-08-28 20:10:17 +0000449 InstructionSelectPreprocess(DAG);
450
Chris Lattnerc961eea2005-11-16 01:54:32 +0000451 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000452#ifndef NDEBUG
453 DEBUG(std::cerr << "===== Instruction selection begins:\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000454 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000455#endif
Evan Chengba2f0a92006-02-05 06:46:41 +0000456 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengf597dc72006-02-10 22:24:32 +0000457#ifndef NDEBUG
458 DEBUG(std::cerr << "===== Instruction selection ends:\n");
459#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000460
Chris Lattnerc961eea2005-11-16 01:54:32 +0000461 DAG.RemoveDeadNodes();
462
463 // Emit machine code to BB.
464 ScheduleAndEmitDAG(DAG);
Chris Lattner92cb0af2006-01-11 01:15:34 +0000465
466 // If we are emitting FP stack code, scan the basic block to determine if this
467 // block defines any FP values. If so, put an FP_REG_KILL instruction before
468 // the terminator of the block.
Evan Cheng559806f2006-01-27 08:10:46 +0000469 if (!Subtarget->hasSSE2()) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000470 // Note that FP stack instructions *are* used in SSE code when returning
471 // values, but these are not live out of the basic block, so we don't need
472 // an FP_REG_KILL in this case either.
473 bool ContainsFPCode = false;
474
475 // Scan all of the machine instructions in these MBBs, checking for FP
476 // stores.
477 MachineFunction::iterator MBBI = FirstMBB;
478 do {
479 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
480 !ContainsFPCode && I != E; ++I) {
481 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
482 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
483 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
484 RegMap->getRegClass(I->getOperand(0).getReg()) ==
485 X86::RFPRegisterClass) {
486 ContainsFPCode = true;
487 break;
488 }
489 }
490 }
491 } while (!ContainsFPCode && &*(MBBI++) != BB);
492
493 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
494 // a copy of the input value in this block.
495 if (!ContainsFPCode) {
496 // Final check, check LLVM BB's that are successors to the LLVM BB
497 // corresponding to BB for FP PHI nodes.
498 const BasicBlock *LLVMBB = BB->getBasicBlock();
499 const PHINode *PN;
500 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
501 !ContainsFPCode && SI != E; ++SI) {
502 for (BasicBlock::const_iterator II = SI->begin();
503 (PN = dyn_cast<PHINode>(II)); ++II) {
504 if (PN->getType()->isFloatingPoint()) {
505 ContainsFPCode = true;
506 break;
507 }
508 }
509 }
510 }
511
512 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
513 if (ContainsFPCode) {
514 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
515 ++NumFPKill;
516 }
517 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000518}
519
Evan Cheng8700e142006-01-11 06:09:51 +0000520/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
521/// the main function.
Evan Cheng3649b0e2006-06-02 22:38:37 +0000522void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
523 MachineFrameInfo *MFI) {
Anton Korobeynikovbcb97702006-09-17 20:25:45 +0000524 if (Subtarget->isTargetCygwin())
Evan Cheng3649b0e2006-06-02 22:38:37 +0000525 BuildMI(BB, X86::CALLpcrel32, 1).addExternalSymbol("__main");
526
Evan Cheng8700e142006-01-11 06:09:51 +0000527 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
528 int CWFrameIdx = MFI->CreateStackObject(2, 2);
529 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
530
531 // Set the high part to be 64-bit precision.
532 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
533 CWFrameIdx, 1).addImm(2);
534
535 // Reload the modified control word now.
536 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
537}
538
539void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
540 // If this is main, emit special code for main.
541 MachineBasicBlock *BB = MF.begin();
542 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
543 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
544}
545
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000546/// MatchAddress - Add the specified node to the specified addressing mode,
547/// returning true if it cannot be done. This just pattern matches for the
548/// addressing mode
Evan Cheng2486af12006-02-11 02:05:36 +0000549bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
550 bool isRoot) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000551 // RIP relative addressing: %rip + 32-bit displacement!
552 if (AM.isRIPRel) {
553 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) {
Chris Lattner0f27fc32006-09-13 04:45:25 +0000554 int64_t Val = cast<ConstantSDNode>(N)->getSignExtended();
Evan Cheng25ab6902006-09-08 06:48:29 +0000555 if (isInt32(AM.Disp + Val)) {
556 AM.Disp += Val;
557 return false;
558 }
559 }
560 return true;
561 }
562
Evan Cheng2ef88a02006-08-07 22:28:20 +0000563 int id = N.Val->getNodeId();
564 bool Available = isSelected(id);
Evan Cheng2486af12006-02-11 02:05:36 +0000565
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000566 switch (N.getOpcode()) {
567 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000568 case 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 break;
575 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000576
577 case X86ISD::Wrapper:
Evan Cheng25ab6902006-09-08 06:48:29 +0000578 // If value is available in a register both base and index components have
579 // been picked, we can't fit the result available in the register in the
580 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement.
581
582 // Can't fit GV or CP in addressing mode for X86-64 medium or large code
583 // model since the displacement field is 32-bit. Ok for small code model.
584
585 // For X86-64 PIC code, only allow GV / CP + displacement so we can use RIP
586 // relative addressing mode.
587 if ((!Subtarget->is64Bit() || TM.getCodeModel() == CodeModel::Small) &&
588 (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val))) {
589 bool isRIP = Subtarget->is64Bit();
590 if (isRIP && (AM.Base.Reg.Val || AM.Scale > 1 || AM.IndexReg.Val ||
591 AM.BaseType == X86ISelAddressMode::FrameIndexBase))
592 break;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000593 if (ConstantPoolSDNode *CP =
594 dyn_cast<ConstantPoolSDNode>(N.getOperand(0))) {
595 if (AM.CP == 0) {
Evan Chengc356a572006-09-12 21:04:05 +0000596 AM.CP = CP->getConstVal();
Evan Cheng51a9ed92006-02-25 10:09:08 +0000597 AM.Align = CP->getAlignment();
598 AM.Disp += CP->getOffset();
Evan Cheng25ab6902006-09-08 06:48:29 +0000599 if (isRIP)
600 AM.isRIPRel = true;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000601 return false;
602 }
603 } else if (GlobalAddressSDNode *G =
604 dyn_cast<GlobalAddressSDNode>(N.getOperand(0))) {
605 if (AM.GV == 0) {
606 AM.GV = G->getGlobal();
607 AM.Disp += G->getOffset();
Evan Cheng25ab6902006-09-08 06:48:29 +0000608 if (isRIP)
609 AM.isRIPRel = true;
610 return false;
611 }
612 } else if (isRoot && isRIP) {
613 if (ExternalSymbolSDNode *S =
614 dyn_cast<ExternalSymbolSDNode>(N.getOperand(0))) {
615 AM.ES = S->getSymbol();
616 AM.isRIPRel = true;
617 return false;
618 } else if (JumpTableSDNode *J =
619 dyn_cast<JumpTableSDNode>(N.getOperand(0))) {
620 AM.JT = J->getIndex();
621 AM.isRIPRel = true;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000622 return false;
623 }
624 }
625 }
626 break;
627
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000628 case ISD::FrameIndex:
629 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
630 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
631 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
632 return false;
633 }
634 break;
Evan Chengec693f72005-12-08 02:01:35 +0000635
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000636 case ISD::SHL:
Evan Cheng51a9ed92006-02-25 10:09:08 +0000637 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000638 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
639 unsigned Val = CN->getValue();
640 if (Val == 1 || Val == 2 || Val == 3) {
641 AM.Scale = 1 << Val;
642 SDOperand ShVal = N.Val->getOperand(0);
643
644 // Okay, we know that we have a scale by now. However, if the scaled
645 // value is an add of something and a constant, we can fold the
646 // constant into the disp field here.
647 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
648 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
649 AM.IndexReg = ShVal.Val->getOperand(0);
650 ConstantSDNode *AddVal =
651 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
Evan Cheng25ab6902006-09-08 06:48:29 +0000652 uint64_t Disp = AM.Disp + AddVal->getValue() << Val;
653 if (isInt32(Disp))
654 AM.Disp = Disp;
655 else
656 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000657 } else {
658 AM.IndexReg = ShVal;
659 }
660 return false;
661 }
662 }
663 break;
Evan Chengec693f72005-12-08 02:01:35 +0000664
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000665 case ISD::MUL:
666 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng51a9ed92006-02-25 10:09:08 +0000667 if (!Available &&
668 AM.BaseType == X86ISelAddressMode::RegBase &&
669 AM.Base.Reg.Val == 0 &&
670 AM.IndexReg.Val == 0)
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000671 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
672 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
673 AM.Scale = unsigned(CN->getValue())-1;
674
675 SDOperand MulVal = N.Val->getOperand(0);
676 SDOperand Reg;
677
678 // Okay, we know that we have a scale by now. However, if the scaled
679 // value is an add of something and a constant, we can fold the
680 // constant into the disp field here.
681 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
682 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
683 Reg = MulVal.Val->getOperand(0);
684 ConstantSDNode *AddVal =
685 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
Evan Cheng25ab6902006-09-08 06:48:29 +0000686 uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue();
687 if (isInt32(Disp))
688 AM.Disp = Disp;
689 else
690 Reg = N.Val->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000691 } else {
692 Reg = N.Val->getOperand(0);
693 }
694
695 AM.IndexReg = AM.Base.Reg = Reg;
696 return false;
697 }
698 break;
699
700 case ISD::ADD: {
Evan Cheng51a9ed92006-02-25 10:09:08 +0000701 if (!Available) {
Evan Cheng2486af12006-02-11 02:05:36 +0000702 X86ISelAddressMode Backup = AM;
703 if (!MatchAddress(N.Val->getOperand(0), AM, false) &&
704 !MatchAddress(N.Val->getOperand(1), AM, false))
705 return false;
706 AM = Backup;
707 if (!MatchAddress(N.Val->getOperand(1), AM, false) &&
708 !MatchAddress(N.Val->getOperand(0), AM, false))
709 return false;
710 AM = Backup;
711 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000712 break;
713 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000714
715 case ISD::OR: {
716 if (!Available) {
717 X86ISelAddressMode Backup = AM;
718 // Look for (x << c1) | c2 where (c2 < c1)
719 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(0));
720 if (CN && !MatchAddress(N.Val->getOperand(1), AM, false)) {
721 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
722 AM.Disp = CN->getValue();
723 return false;
724 }
725 }
726 AM = Backup;
727 CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1));
728 if (CN && !MatchAddress(N.Val->getOperand(0), AM, false)) {
729 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
730 AM.Disp = CN->getValue();
731 return false;
732 }
733 }
734 AM = Backup;
735 }
736 break;
737 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000738 }
739
740 // Is the base register already occupied?
741 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
742 // If so, check to see if the scale index register is set.
743 if (AM.IndexReg.Val == 0) {
744 AM.IndexReg = N;
745 AM.Scale = 1;
746 return false;
747 }
748
749 // Otherwise, we cannot select it.
750 return true;
751 }
752
753 // Default, generate it as a register.
754 AM.BaseType = X86ISelAddressMode::RegBase;
755 AM.Base.Reg = N;
756 return false;
757}
758
Evan Chengec693f72005-12-08 02:01:35 +0000759/// SelectAddr - returns true if it is able pattern match an addressing mode.
760/// It returns the operands which make up the maximal addressing mode it can
761/// match by reference.
762bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
763 SDOperand &Index, SDOperand &Disp) {
764 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +0000765 if (MatchAddress(N, AM))
766 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000767
Evan Cheng25ab6902006-09-08 06:48:29 +0000768 MVT::ValueType VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +0000769 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Cheng7dd281b2006-02-05 05:25:07 +0000770 if (!AM.Base.Reg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000771 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +0000772 }
Evan Cheng8700e142006-01-11 06:09:51 +0000773
Evan Cheng7dd281b2006-02-05 05:25:07 +0000774 if (!AM.IndexReg.Val)
Evan Cheng25ab6902006-09-08 06:48:29 +0000775 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +0000776
777 getAddressOperands(AM, Base, Scale, Index, Disp);
778 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000779}
780
Chris Lattner4fe4f252006-10-11 22:09:58 +0000781/// isZeroNode - Returns true if Elt is a constant zero or a floating point
782/// constant +0.0.
783static inline bool isZeroNode(SDOperand Elt) {
784 return ((isa<ConstantSDNode>(Elt) &&
785 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
786 (isa<ConstantFPSDNode>(Elt) &&
787 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
788}
789
790
Chris Lattner3a7cd952006-10-07 21:55:32 +0000791/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
792/// match a load whose top elements are either undef or zeros. The load flavor
793/// is derived from the type of N, which is either v4f32 or v2f64.
794bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand N, SDOperand &Base,
Evan Cheng82a91642006-10-11 21:06:01 +0000795 SDOperand &Scale, SDOperand &Index,
796 SDOperand &Disp, SDOperand &InChain,
797 SDOperand &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +0000798 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +0000799 InChain = N.getOperand(0).getValue(1);
Chris Lattnerf78ae9e2006-10-12 03:55:48 +0000800 if (ISD::isNON_EXTLoad(InChain.Val) && InChain.getValue(0).hasOneUse()) {
Evan Cheng82a91642006-10-11 21:06:01 +0000801 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Chris Lattner4fe4f252006-10-11 22:09:58 +0000802 if (!SelectAddr(LD->getBasePtr(), Base, Scale, Index, Disp))
Chris Lattner3a7cd952006-10-07 21:55:32 +0000803 return false;
Evan Cheng82a91642006-10-11 21:06:01 +0000804 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +0000805 return true;
806 }
807 }
Chris Lattner4fe4f252006-10-11 22:09:58 +0000808
809 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +0000810 // elements. This is a vector shuffle from the zero vector.
Chris Lattner4fe4f252006-10-11 22:09:58 +0000811 if (N.getOpcode() == ISD::VECTOR_SHUFFLE && N.Val->hasOneUse() &&
812 N.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
813 N.getOperand(1).getOpcode() == ISD::SCALAR_TO_VECTOR &&
814 N.getOperand(1).Val->hasOneUse() &&
815 ISD::isNON_EXTLoad(N.getOperand(1).getOperand(0).Val) &&
816 N.getOperand(1).getOperand(0).hasOneUse()) {
817 // Check to see if the BUILD_VECTOR is building a zero vector.
818 SDOperand BV = N.getOperand(0);
819 for (unsigned i = 0, e = BV.getNumOperands(); i != e; ++i)
820 if (!isZeroNode(BV.getOperand(i)) &&
821 BV.getOperand(i).getOpcode() != ISD::UNDEF)
822 return false; // Not a zero/undef vector.
823 // Check to see if the shuffle mask is 4/L/L/L or 2/L, where L is something
824 // from the LHS.
825 unsigned VecWidth = BV.getNumOperands();
826 SDOperand ShufMask = N.getOperand(2);
827 assert(ShufMask.getOpcode() == ISD::BUILD_VECTOR && "Invalid shuf mask!");
828 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(ShufMask.getOperand(0))) {
829 if (C->getValue() == VecWidth) {
830 for (unsigned i = 1; i != VecWidth; ++i) {
831 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF) {
832 // ok.
833 } else {
834 ConstantSDNode *C = cast<ConstantSDNode>(ShufMask.getOperand(i));
835 if (C->getValue() >= VecWidth) return false;
836 }
837 }
838 }
839
840 // Okay, this is a zero extending load. Fold it.
841 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(1).getOperand(0));
842 if (!SelectAddr(LD->getBasePtr(), Base, Scale, Index, Disp))
843 return false;
844 OutChain = LD->getChain();
845 InChain = SDOperand(LD, 1);
846 return true;
847 }
848 }
Chris Lattner3a7cd952006-10-07 21:55:32 +0000849 return false;
850}
851
852
Evan Cheng51a9ed92006-02-25 10:09:08 +0000853/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
854/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng51a9ed92006-02-25 10:09:08 +0000855bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base,
856 SDOperand &Scale,
857 SDOperand &Index, SDOperand &Disp) {
858 X86ISelAddressMode AM;
859 if (MatchAddress(N, AM))
860 return false;
861
Evan Cheng25ab6902006-09-08 06:48:29 +0000862 MVT::ValueType VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +0000863 unsigned Complexity = 0;
864 if (AM.BaseType == X86ISelAddressMode::RegBase)
865 if (AM.Base.Reg.Val)
866 Complexity = 1;
867 else
Evan Cheng25ab6902006-09-08 06:48:29 +0000868 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +0000869 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
870 Complexity = 4;
871
872 if (AM.IndexReg.Val)
873 Complexity++;
874 else
Evan Cheng25ab6902006-09-08 06:48:29 +0000875 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +0000876
Evan Cheng8c03fe42006-02-28 21:13:57 +0000877 if (AM.Scale > 2)
Evan Cheng51a9ed92006-02-25 10:09:08 +0000878 Complexity += 2;
Evan Cheng8c03fe42006-02-28 21:13:57 +0000879 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg
880 else if (AM.Scale > 1)
881 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000882
883 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
884 // to a LEA. This is determined with some expermentation but is by no means
885 // optimal (especially for code size consideration). LEA is nice because of
886 // its three-address nature. Tweak the cost function again when we can run
887 // convertToThreeAddress() at register allocation time.
Evan Cheng25ab6902006-09-08 06:48:29 +0000888 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) {
889 // For X86-64, we should always use lea to materialize RIP relative
890 // addresses.
891 if (Subtarget->is64Bit())
892 Complexity = 4;
893 else
894 Complexity += 2;
895 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000896
897 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
898 Complexity++;
899
900 if (Complexity > 2) {
901 getAddressOperands(AM, Base, Scale, Index, Disp);
902 return true;
903 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000904 return false;
905}
906
Evan Cheng5e351682006-02-06 06:02:33 +0000907bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
908 SDOperand &Base, SDOperand &Scale,
909 SDOperand &Index, SDOperand &Disp) {
Evan Cheng466685d2006-10-09 20:57:25 +0000910 if (ISD::isNON_EXTLoad(N.Val) &&
Evan Cheng5e351682006-02-06 06:02:33 +0000911 N.hasOneUse() &&
Evan Chengeb8730d2006-08-16 23:59:00 +0000912 CanBeFoldedBy(N.Val, P.Val))
Evan Cheng0114e942006-01-06 20:36:21 +0000913 return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp);
914 return false;
915}
916
917static bool isRegister0(SDOperand Op) {
Evan Chengec693f72005-12-08 02:01:35 +0000918 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
919 return (R->getReg() == 0);
920 return false;
921}
922
Evan Cheng7ccced62006-02-18 00:15:05 +0000923/// getGlobalBaseReg - Output the instructions required to put the
924/// base address to use for accessing globals into a register.
925///
Evan Cheng9ade2182006-08-26 05:34:46 +0000926SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng25ab6902006-09-08 06:48:29 +0000927 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
Evan Cheng7ccced62006-02-18 00:15:05 +0000928 if (!GlobalBaseReg) {
929 // Insert the set of GlobalBaseReg into the first MBB of the function
930 MachineBasicBlock &FirstMBB = BB->getParent()->front();
931 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
932 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
933 // FIXME: when we get to LP64, we will need to create the appropriate
934 // type of register here.
Evan Cheng069287d2006-05-16 07:21:53 +0000935 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng7ccced62006-02-18 00:15:05 +0000936 BuildMI(FirstMBB, MBBI, X86::MovePCtoStack, 0);
937 BuildMI(FirstMBB, MBBI, X86::POP32r, 1, GlobalBaseReg);
938 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000939 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val;
Evan Cheng7ccced62006-02-18 00:15:05 +0000940}
941
Evan Chengb245d922006-05-20 01:36:52 +0000942static SDNode *FindCallStartFromCall(SDNode *Node) {
943 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
944 assert(Node->getOperand(0).getValueType() == MVT::Other &&
945 "Node doesn't have a token chain argument!");
946 return FindCallStartFromCall(Node->getOperand(0).Val);
947}
948
Evan Cheng9ade2182006-08-26 05:34:46 +0000949SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Chengdef941b2005-12-15 01:02:48 +0000950 SDNode *Node = N.Val;
951 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +0000952 unsigned Opc, MOpc;
953 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +0000954
Evan Chengf597dc72006-02-10 22:24:32 +0000955#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +0000956 DEBUG(std::cerr << std::string(Indent, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +0000957 DEBUG(std::cerr << "Selecting: ");
958 DEBUG(Node->dump(CurDAG));
959 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000960 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000961#endif
962
Evan Cheng34167212006-02-09 00:37:58 +0000963 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengf597dc72006-02-10 22:24:32 +0000964#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +0000965 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +0000966 DEBUG(std::cerr << "== ");
967 DEBUG(Node->dump(CurDAG));
968 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000969 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000970#endif
Evan Cheng64a752f2006-08-11 09:08:15 +0000971 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +0000972 }
Evan Cheng38262ca2006-01-11 22:15:18 +0000973
Evan Cheng0114e942006-01-06 20:36:21 +0000974 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000975 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +0000976 case X86ISD::GlobalBaseReg:
Evan Cheng9ade2182006-08-26 05:34:46 +0000977 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +0000978
Evan Cheng51a9ed92006-02-25 10:09:08 +0000979 case ISD::ADD: {
980 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
981 // code and is matched first so to prevent it from being turned into
982 // LEA32r X+c.
Evan Cheng25ab6902006-09-08 06:48:29 +0000983 // In 64-bit mode, use LEA to take advantage of RIP-relative addressing.
984 MVT::ValueType PtrVT = TLI.getPointerTy();
Evan Cheng51a9ed92006-02-25 10:09:08 +0000985 SDOperand N0 = N.getOperand(0);
986 SDOperand N1 = N.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +0000987 if (N.Val->getValueType(0) == PtrVT &&
Evan Cheng51a9ed92006-02-25 10:09:08 +0000988 N0.getOpcode() == X86ISD::Wrapper &&
989 N1.getOpcode() == ISD::Constant) {
990 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
991 SDOperand C(0, 0);
992 // TODO: handle ExternalSymbolSDNode.
993 if (GlobalAddressSDNode *G =
994 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000995 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +0000996 G->getOffset() + Offset);
997 } else if (ConstantPoolSDNode *CP =
998 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
Evan Chengc356a572006-09-12 21:04:05 +0000999 C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT,
Evan Cheng51a9ed92006-02-25 10:09:08 +00001000 CP->getAlignment(),
1001 CP->getOffset()+Offset);
1002 }
1003
Evan Cheng25ab6902006-09-08 06:48:29 +00001004 if (C.Val) {
1005 if (Subtarget->is64Bit()) {
1006 SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1),
1007 CurDAG->getRegister(0, PtrVT), C };
1008 return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4);
1009 } else
1010 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C);
1011 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001012 }
1013
1014 // Other cases are handled by auto-generated code.
1015 break;
Evan Chenga0ea0532006-02-23 02:43:52 +00001016 }
Evan Cheng020d2e82006-02-23 20:41:18 +00001017
Evan Cheng0114e942006-01-06 20:36:21 +00001018 case ISD::MULHU:
1019 case ISD::MULHS: {
1020 if (Opcode == ISD::MULHU)
1021 switch (NVT) {
1022 default: assert(0 && "Unsupported VT!");
1023 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1024 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1025 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001026 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001027 }
1028 else
1029 switch (NVT) {
1030 default: assert(0 && "Unsupported VT!");
1031 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1032 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1033 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001034 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001035 }
1036
1037 unsigned LoReg, HiReg;
1038 switch (NVT) {
1039 default: assert(0 && "Unsupported VT!");
1040 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1041 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1042 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001043 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Evan Cheng0114e942006-01-06 20:36:21 +00001044 }
1045
1046 SDOperand N0 = Node->getOperand(0);
1047 SDOperand N1 = Node->getOperand(1);
1048
1049 bool foldedLoad = false;
1050 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng5e351682006-02-06 06:02:33 +00001051 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +00001052 // MULHU and MULHS are commmutative
1053 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +00001054 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +00001055 if (foldedLoad) {
1056 N0 = Node->getOperand(1);
1057 N1 = Node->getOperand(0);
1058 }
1059 }
1060
Evan Cheng34167212006-02-09 00:37:58 +00001061 SDOperand Chain;
Evan Cheng04699902006-08-26 01:05:16 +00001062 if (foldedLoad) {
1063 Chain = N1.getOperand(0);
1064 AddToISelQueue(Chain);
1065 } else
Evan Cheng34167212006-02-09 00:37:58 +00001066 Chain = CurDAG->getEntryNode();
Evan Cheng0114e942006-01-06 20:36:21 +00001067
Evan Cheng34167212006-02-09 00:37:58 +00001068 SDOperand InFlag(0, 0);
Evan Cheng04699902006-08-26 01:05:16 +00001069 AddToISelQueue(N0);
Evan Cheng0114e942006-01-06 20:36:21 +00001070 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng34167212006-02-09 00:37:58 +00001071 N0, InFlag);
Evan Cheng0114e942006-01-06 20:36:21 +00001072 InFlag = Chain.getValue(1);
1073
1074 if (foldedLoad) {
Evan Cheng04699902006-08-26 01:05:16 +00001075 AddToISelQueue(Tmp0);
1076 AddToISelQueue(Tmp1);
1077 AddToISelQueue(Tmp2);
1078 AddToISelQueue(Tmp3);
Evan Cheng0b828e02006-08-27 08:14:06 +00001079 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001080 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001081 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001082 Chain = SDOperand(CNode, 0);
1083 InFlag = SDOperand(CNode, 1);
Evan Cheng0114e942006-01-06 20:36:21 +00001084 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001085 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001086 InFlag =
1087 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +00001088 }
1089
Evan Cheng9ade2182006-08-26 05:34:46 +00001090 SDOperand Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
Evan Cheng2ef88a02006-08-07 22:28:20 +00001091 ReplaceUses(N.getValue(0), Result);
1092 if (foldedLoad)
1093 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Cheng34167212006-02-09 00:37:58 +00001094
Evan Chengf597dc72006-02-10 22:24:32 +00001095#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +00001096 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Cheng2ef88a02006-08-07 22:28:20 +00001097 DEBUG(std::cerr << "=> ");
Evan Chengf597dc72006-02-10 22:24:32 +00001098 DEBUG(Result.Val->dump(CurDAG));
1099 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +00001100 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001101#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001102 return NULL;
Evan Cheng948f3432006-01-06 23:19:29 +00001103 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001104
Evan Cheng948f3432006-01-06 23:19:29 +00001105 case ISD::SDIV:
1106 case ISD::UDIV:
1107 case ISD::SREM:
1108 case ISD::UREM: {
1109 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
1110 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
1111 if (!isSigned)
1112 switch (NVT) {
1113 default: assert(0 && "Unsupported VT!");
1114 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1115 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1116 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001117 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001118 }
1119 else
1120 switch (NVT) {
1121 default: assert(0 && "Unsupported VT!");
1122 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1123 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1124 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001125 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Evan Cheng948f3432006-01-06 23:19:29 +00001126 }
1127
1128 unsigned LoReg, HiReg;
1129 unsigned ClrOpcode, SExtOpcode;
1130 switch (NVT) {
1131 default: assert(0 && "Unsupported VT!");
1132 case MVT::i8:
1133 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengaede9b92006-06-02 21:20:34 +00001134 ClrOpcode = X86::MOV8r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001135 SExtOpcode = X86::CBW;
1136 break;
1137 case MVT::i16:
1138 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +00001139 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001140 SExtOpcode = X86::CWD;
1141 break;
1142 case MVT::i32:
1143 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +00001144 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +00001145 SExtOpcode = X86::CDQ;
1146 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00001147 case MVT::i64:
1148 LoReg = X86::RAX; HiReg = X86::RDX;
1149 ClrOpcode = X86::MOV64r0;
1150 SExtOpcode = X86::CQO;
1151 break;
Evan Cheng948f3432006-01-06 23:19:29 +00001152 }
1153
1154 SDOperand N0 = Node->getOperand(0);
1155 SDOperand N1 = Node->getOperand(1);
1156
1157 bool foldedLoad = false;
1158 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng5e351682006-02-06 06:02:33 +00001159 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng34167212006-02-09 00:37:58 +00001160 SDOperand Chain;
Evan Cheng04699902006-08-26 01:05:16 +00001161 if (foldedLoad) {
1162 Chain = N1.getOperand(0);
1163 AddToISelQueue(Chain);
1164 } else
Evan Cheng34167212006-02-09 00:37:58 +00001165 Chain = CurDAG->getEntryNode();
Evan Cheng948f3432006-01-06 23:19:29 +00001166
Evan Cheng34167212006-02-09 00:37:58 +00001167 SDOperand InFlag(0, 0);
Evan Cheng04699902006-08-26 01:05:16 +00001168 AddToISelQueue(N0);
Evan Cheng948f3432006-01-06 23:19:29 +00001169 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng34167212006-02-09 00:37:58 +00001170 N0, InFlag);
Evan Cheng948f3432006-01-06 23:19:29 +00001171 InFlag = Chain.getValue(1);
1172
1173 if (isSigned) {
1174 // Sign extend the low part into the high part.
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001175 InFlag =
1176 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001177 } else {
1178 // Zero out the high part, effectively zero extending the input.
Evan Chengaede9b92006-06-02 21:20:34 +00001179 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001180 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT),
1181 ClrNode, InFlag);
1182 InFlag = Chain.getValue(1);
1183 }
1184
1185 if (foldedLoad) {
Evan Cheng04699902006-08-26 01:05:16 +00001186 AddToISelQueue(Tmp0);
1187 AddToISelQueue(Tmp1);
1188 AddToISelQueue(Tmp2);
1189 AddToISelQueue(Tmp3);
Evan Cheng0b828e02006-08-27 08:14:06 +00001190 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001191 SDNode *CNode =
Evan Cheng0b828e02006-08-27 08:14:06 +00001192 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001193 Chain = SDOperand(CNode, 0);
1194 InFlag = SDOperand(CNode, 1);
Evan Cheng948f3432006-01-06 23:19:29 +00001195 } else {
Evan Cheng04699902006-08-26 01:05:16 +00001196 AddToISelQueue(N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001197 InFlag =
1198 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +00001199 }
1200
Evan Cheng9ade2182006-08-26 05:34:46 +00001201 SDOperand Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg,
1202 NVT, InFlag);
Evan Cheng2ef88a02006-08-07 22:28:20 +00001203 ReplaceUses(N.getValue(0), Result);
1204 if (foldedLoad)
1205 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Chengf597dc72006-02-10 22:24:32 +00001206
1207#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +00001208 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Cheng2ef88a02006-08-07 22:28:20 +00001209 DEBUG(std::cerr << "=> ");
Evan Chengf597dc72006-02-10 22:24:32 +00001210 DEBUG(Result.Val->dump(CurDAG));
1211 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +00001212 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001213#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001214
1215 return NULL;
Evan Cheng0114e942006-01-06 20:36:21 +00001216 }
Evan Cheng403be7e2006-05-08 08:01:26 +00001217
1218 case ISD::TRUNCATE: {
Evan Cheng25ab6902006-09-08 06:48:29 +00001219 if (!Subtarget->is64Bit() && NVT == MVT::i8) {
Evan Cheng403be7e2006-05-08 08:01:26 +00001220 unsigned Opc2;
1221 MVT::ValueType VT;
1222 switch (Node->getOperand(0).getValueType()) {
1223 default: assert(0 && "Unknown truncate!");
1224 case MVT::i16:
1225 Opc = X86::MOV16to16_;
1226 VT = MVT::i16;
Evan Cheng25ab6902006-09-08 06:48:29 +00001227 Opc2 = X86::TRUNC_16_to8;
Evan Cheng403be7e2006-05-08 08:01:26 +00001228 break;
1229 case MVT::i32:
1230 Opc = X86::MOV32to32_;
1231 VT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00001232 Opc2 = X86::TRUNC_32_to8;
Evan Cheng403be7e2006-05-08 08:01:26 +00001233 break;
1234 }
1235
Evan Cheng04699902006-08-26 01:05:16 +00001236 AddToISelQueue(Node->getOperand(0));
1237 SDOperand Tmp =
1238 SDOperand(CurDAG->getTargetNode(Opc, VT, Node->getOperand(0)), 0);
Evan Cheng9ade2182006-08-26 05:34:46 +00001239 SDNode *ResNode = CurDAG->getTargetNode(Opc2, NVT, Tmp);
Evan Cheng403be7e2006-05-08 08:01:26 +00001240
1241#ifndef NDEBUG
1242 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Cheng2ef88a02006-08-07 22:28:20 +00001243 DEBUG(std::cerr << "=> ");
Evan Cheng9ade2182006-08-26 05:34:46 +00001244 DEBUG(ResNode->dump(CurDAG));
Evan Cheng403be7e2006-05-08 08:01:26 +00001245 DEBUG(std::cerr << "\n");
1246 Indent -= 2;
1247#endif
Evan Cheng9ade2182006-08-26 05:34:46 +00001248 return ResNode;
Evan Cheng403be7e2006-05-08 08:01:26 +00001249 }
Evan Cheng6b2e2542006-05-20 07:44:28 +00001250
1251 break;
Evan Cheng403be7e2006-05-08 08:01:26 +00001252 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00001253 }
1254
Evan Cheng9ade2182006-08-26 05:34:46 +00001255 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00001256
Evan Chengf597dc72006-02-10 22:24:32 +00001257#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +00001258 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +00001259 DEBUG(std::cerr << "=> ");
Evan Cheng9ade2182006-08-26 05:34:46 +00001260 if (ResNode == NULL || ResNode == N.Val)
1261 DEBUG(N.Val->dump(CurDAG));
1262 else
1263 DEBUG(ResNode->dump(CurDAG));
Evan Chengf597dc72006-02-10 22:24:32 +00001264 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +00001265 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001266#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001267
1268 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00001269}
1270
Chris Lattnerc0bad572006-06-08 18:03:49 +00001271bool X86DAGToDAGISel::
1272SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1273 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1274 SDOperand Op0, Op1, Op2, Op3;
1275 switch (ConstraintCode) {
1276 case 'o': // offsetable ??
1277 case 'v': // not offsetable ??
1278 default: return true;
1279 case 'm': // memory
1280 if (!SelectAddr(Op, Op0, Op1, Op2, Op3))
1281 return true;
1282 break;
1283 }
1284
Evan Cheng04699902006-08-26 01:05:16 +00001285 OutOps.push_back(Op0);
1286 OutOps.push_back(Op1);
1287 OutOps.push_back(Op2);
1288 OutOps.push_back(Op3);
1289 AddToISelQueue(Op0);
1290 AddToISelQueue(Op1);
1291 AddToISelQueue(Op2);
1292 AddToISelQueue(Op3);
Chris Lattnerc0bad572006-06-08 18:03:49 +00001293 return false;
1294}
1295
Chris Lattnerc961eea2005-11-16 01:54:32 +00001296/// createX86ISelDag - This pass converts a legalized DAG into a
1297/// X86-specific DAG, ready for instruction scheduling.
1298///
Evan Chenge50794a2006-08-29 18:28:33 +00001299FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1300 return new X86DAGToDAGISel(TM, Fast);
Chris Lattnerc961eea2005-11-16 01:54:32 +00001301}