blob: 9697f2591314f9a174c09458f95dc18f4e5dcfa8 [file] [log] [blame]
Chris Lattner5930d3d2005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattner655e7df2005-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 Chengb9d34bd2006-08-07 22:28:20 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattner655e7df2005-11-16 01:54:32 +000016#include "X86.h"
Evan Chengbc7a0f442006-01-11 06:09:51 +000017#include "X86InstrBuilder.h"
Evan Cheng2dd2c652006-03-13 23:20:37 +000018#include "X86ISelLowering.h"
Chris Lattner7c551262006-01-11 01:15:34 +000019#include "X86RegisterInfo.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000020#include "X86Subtarget.h"
Evan Cheng2dd2c652006-03-13 23:20:37 +000021#include "X86TargetMachine.h"
Chris Lattner3f0f71b2005-11-19 02:11:08 +000022#include "llvm/GlobalValue.h"
Chris Lattner7c551262006-01-11 01:15:34 +000023#include "llvm/Instructions.h"
Chris Lattner5d70a7c2006-03-25 06:47:10 +000024#include "llvm/Intrinsics.h"
Chris Lattner7c551262006-01-11 01:15:34 +000025#include "llvm/Support/CFG.h"
Chris Lattner3f0f71b2005-11-19 02:11:08 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000027#include "llvm/CodeGen/MachineFunction.h"
Evan Cheng73a1ad92006-01-10 20:26:56 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner7c551262006-01-11 01:15:34 +000029#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000031#include "llvm/CodeGen/SelectionDAGISel.h"
32#include "llvm/Target/TargetMachine.h"
33#include "llvm/Support/Debug.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000034#include "llvm/Support/Compiler.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000035#include "llvm/ADT/Statistic.h"
Evan Cheng2e945382006-07-28 06:05:06 +000036#include <deque>
Chris Lattnerde02d772006-01-22 23:41:00 +000037#include <iostream>
Evan Chengb9d34bd2006-08-07 22:28:20 +000038#include <queue>
Evan Cheng54cb1832006-02-05 06:46:41 +000039#include <set>
Chris Lattner655e7df2005-11-16 01:54:32 +000040using namespace llvm;
41
Evan Cheng64a9e282006-08-28 20:10:17 +000042#include "llvm/Support/CommandLine.h"
43static cl::opt<bool> X86ISelPreproc("enable-x86-isel-preprocessing", cl::Hidden,
44 cl::desc("Enable isel preprocessing on X86"));
45
Chris Lattner655e7df2005-11-16 01:54:32 +000046//===----------------------------------------------------------------------===//
47// Pattern Matcher Implementation
48//===----------------------------------------------------------------------===//
49
50namespace {
Chris Lattner3f0f71b2005-11-19 02:11:08 +000051 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
52 /// SDOperand's instead of register numbers for the leaves of the matched
53 /// tree.
54 struct X86ISelAddressMode {
55 enum {
56 RegBase,
Chris Lattneraa2372562006-05-24 17:04:05 +000057 FrameIndexBase
Chris Lattner3f0f71b2005-11-19 02:11:08 +000058 } BaseType;
59
60 struct { // This is really a union, discriminated by BaseType!
61 SDOperand Reg;
62 int FrameIndex;
63 } Base;
64
65 unsigned Scale;
66 SDOperand IndexReg;
67 unsigned Disp;
68 GlobalValue *GV;
Evan Cheng77d86ff2006-02-25 10:09:08 +000069 Constant *CP;
70 unsigned Align; // CP alignment.
Chris Lattner3f0f71b2005-11-19 02:11:08 +000071
72 X86ISelAddressMode()
Evan Cheng77d86ff2006-02-25 10:09:08 +000073 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0), GV(0),
74 CP(0), Align(0) {
Chris Lattner3f0f71b2005-11-19 02:11:08 +000075 }
76 };
77}
78
79namespace {
Chris Lattner655e7df2005-11-16 01:54:32 +000080 Statistic<>
81 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
82
Evan Chengc07feb142006-08-29 06:44:17 +000083 Statistic<>
84 NumLoadMoved("x86-codegen", "Number of loads moved below TokenFactor");
85
Chris Lattner655e7df2005-11-16 01:54:32 +000086 //===--------------------------------------------------------------------===//
87 /// ISel - X86 specific code to select X86 machine instructions for
88 /// SelectionDAG operations.
89 ///
Chris Lattner0cc59072006-06-28 23:27:49 +000090 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattner655e7df2005-11-16 01:54:32 +000091 /// ContainsFPCode - Every instruction we select that uses or defines a FP
92 /// register should set this to true.
93 bool ContainsFPCode;
94
Evan Cheng358b9ed2006-08-29 18:28:33 +000095 /// FastISel - Enable fast(er) instruction selection.
96 ///
97 bool FastISel;
98
Chris Lattner655e7df2005-11-16 01:54:32 +000099 /// X86Lowering - This object fully describes how to lower LLVM code to an
100 /// X86-specific SelectionDAG.
101 X86TargetLowering X86Lowering;
102
103 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
104 /// make the right decision when generating code for different targets.
105 const X86Subtarget *Subtarget;
Evan Cheng5588de92006-02-18 00:15:05 +0000106
107 unsigned GlobalBaseReg;
Evan Cheng691a63d2006-07-27 16:44:36 +0000108
Chris Lattner655e7df2005-11-16 01:54:32 +0000109 public:
Evan Cheng358b9ed2006-08-29 18:28:33 +0000110 X86DAGToDAGISel(X86TargetMachine &TM, bool fast)
Evan Cheng2dd2c652006-03-13 23:20:37 +0000111 : SelectionDAGISel(X86Lowering),
Evan Cheng358b9ed2006-08-29 18:28:33 +0000112 ContainsFPCode(false), FastISel(fast),
Evan Cheng691a63d2006-07-27 16:44:36 +0000113 X86Lowering(*TM.getTargetLowering()),
Evan Cheng72bb66a2006-08-08 00:31:00 +0000114 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
Chris Lattner655e7df2005-11-16 01:54:32 +0000115
Evan Cheng5588de92006-02-18 00:15:05 +0000116 virtual bool runOnFunction(Function &Fn) {
117 // Make sure we re-emit a set of the global base reg if necessary
118 GlobalBaseReg = 0;
119 return SelectionDAGISel::runOnFunction(Fn);
120 }
121
Chris Lattner655e7df2005-11-16 01:54:32 +0000122 virtual const char *getPassName() const {
123 return "X86 DAG->DAG Instruction Selection";
124 }
125
126 /// InstructionSelectBasicBlock - This callback is invoked by
127 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
128 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
129
Evan Chengbc7a0f442006-01-11 06:09:51 +0000130 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
131
Evan Chenge2a3f702006-07-28 01:03:48 +0000132 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U);
Evan Cheng691a63d2006-07-27 16:44:36 +0000133
Chris Lattner655e7df2005-11-16 01:54:32 +0000134// Include the pieces autogenerated from the target description.
135#include "X86GenDAGISel.inc"
136
137 private:
Evan Cheng61413a32006-08-26 05:34:46 +0000138 SDNode *Select(SDOperand N);
Chris Lattner655e7df2005-11-16 01:54:32 +0000139
Evan Chenga86ba852006-02-11 02:05:36 +0000140 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM, bool isRoot = true);
Evan Chengc9fab312005-12-08 02:01:35 +0000141 bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
142 SDOperand &Index, SDOperand &Disp);
143 bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
144 SDOperand &Index, SDOperand &Disp);
Evan Chengd5f2ba02006-02-06 06:02:33 +0000145 bool TryFoldLoad(SDOperand P, SDOperand N,
146 SDOperand &Base, SDOperand &Scale,
Evan Cheng10d27902006-01-06 20:36:21 +0000147 SDOperand &Index, SDOperand &Disp);
Evan Cheng64a9e282006-08-28 20:10:17 +0000148 void InstructionSelectPreprocess(SelectionDAG &DAG);
Evan Chengb9d34bd2006-08-07 22:28:20 +0000149
Chris Lattnerba1ed582006-06-08 18:03:49 +0000150 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
151 /// inline asm expressions.
152 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
153 char ConstraintCode,
154 std::vector<SDOperand> &OutOps,
155 SelectionDAG &DAG);
156
Evan Chenge8a42362006-06-02 22:38:37 +0000157 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
158
Evan Cheng67ed58e2005-12-12 21:49:40 +0000159 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
160 SDOperand &Scale, SDOperand &Index,
161 SDOperand &Disp) {
162 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
163 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, MVT::i32) : AM.Base.Reg;
Evan Cheng1d712482005-12-17 09:13:43 +0000164 Scale = getI8Imm(AM.Scale);
Evan Cheng67ed58e2005-12-12 21:49:40 +0000165 Index = AM.IndexReg;
166 Disp = AM.GV ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp)
Evan Cheng77d86ff2006-02-25 10:09:08 +0000167 : (AM.CP ?
168 CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp)
169 : getI32Imm(AM.Disp));
Evan Cheng67ed58e2005-12-12 21:49:40 +0000170 }
171
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000172 /// getI8Imm - Return a target constant with the specified value, of type
173 /// i8.
174 inline SDOperand getI8Imm(unsigned Imm) {
175 return CurDAG->getTargetConstant(Imm, MVT::i8);
176 }
177
Chris Lattner655e7df2005-11-16 01:54:32 +0000178 /// getI16Imm - Return a target constant with the specified value, of type
179 /// i16.
180 inline SDOperand getI16Imm(unsigned Imm) {
181 return CurDAG->getTargetConstant(Imm, MVT::i16);
182 }
183
184 /// getI32Imm - Return a target constant with the specified value, of type
185 /// i32.
186 inline SDOperand getI32Imm(unsigned Imm) {
187 return CurDAG->getTargetConstant(Imm, MVT::i32);
188 }
Evan Chengd49cc362006-02-10 22:24:32 +0000189
Evan Cheng5588de92006-02-18 00:15:05 +0000190 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
191 /// base register. Return the virtual register that holds this value.
Evan Cheng61413a32006-08-26 05:34:46 +0000192 SDNode *getGlobalBaseReg();
Evan Cheng5588de92006-02-18 00:15:05 +0000193
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000194#ifndef NDEBUG
195 unsigned Indent;
196#endif
Chris Lattner655e7df2005-11-16 01:54:32 +0000197 };
198}
199
Evan Cheng72bb66a2006-08-08 00:31:00 +0000200static void findNonImmUse(SDNode* Use, SDNode* Def, bool &found,
201 std::set<SDNode *> &Visited) {
202 if (found ||
203 Use->getNodeId() > Def->getNodeId() ||
204 !Visited.insert(Use).second)
205 return;
206
207 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
208 SDNode *N = Use->getOperand(i).Val;
209 if (N != Def) {
210 findNonImmUse(N, Def, found, Visited);
211 } else {
212 found = true;
213 break;
214 }
215 }
216}
217
218static inline bool isNonImmUse(SDNode* Use, SDNode* Def) {
219 std::set<SDNode *> Visited;
220 bool found = false;
221 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
222 SDNode *N = Use->getOperand(i).Val;
223 if (N != Def) {
224 findNonImmUse(N, Def, found, Visited);
225 if (found) break;
226 }
227 }
228 return found;
229}
230
231
Evan Chenge2a3f702006-07-28 01:03:48 +0000232bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U) {
Evan Cheng691a63d2006-07-27 16:44:36 +0000233 // If U use can somehow reach N through another path then U can't fold N or
234 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Chenge8071ec2006-07-28 06:33:41 +0000235 // through X. If N is folded into into U, then X is both a predecessor and
Evan Cheng691a63d2006-07-27 16:44:36 +0000236 // a successor of U.
237 //
238 // [ N ]
239 // ^ ^
240 // | |
241 // / \---
242 // / [X]
243 // | ^
244 // [U]--------|
Evan Cheng358b9ed2006-08-29 18:28:33 +0000245 return !FastISel && !isNonImmUse(U, N);
Evan Cheng691a63d2006-07-27 16:44:36 +0000246}
247
Evan Cheng64a9e282006-08-28 20:10:17 +0000248/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
249/// and move load below the TokenFactor. Replace store's chain operand with
250/// load's chain result.
251static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
252 SDOperand Store, SDOperand TF) {
253 std::vector<SDOperand> Ops;
254 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
255 if (Load.Val == TF.Val->getOperand(i).Val)
256 Ops.push_back(Load.Val->getOperand(0));
257 else
258 Ops.push_back(TF.Val->getOperand(i));
259 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
260 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
261 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
262 Store.getOperand(2), Store.getOperand(3));
263}
264
265/// InstructionSelectPreprocess - Preprocess the DAG to allow the instruction
266/// selector to pick more load-modify-store instructions. This is a common
267/// case:
268///
269/// [Load chain]
270/// ^
271/// |
272/// [Load]
273/// ^ ^
274/// | |
275/// / \-
276/// / |
277/// [TokenFactor] [Op]
278/// ^ ^
279/// | |
280/// \ /
281/// \ /
282/// [Store]
283///
284/// The fact the store's chain operand != load's chain will prevent the
285/// (store (op (load))) instruction from being selected. We can transform it to:
286///
287/// [Load chain]
288/// ^
289/// |
290/// [TokenFactor]
291/// ^
292/// |
293/// [Load]
294/// ^ ^
295/// | |
296/// | \-
297/// | |
298/// | [Op]
299/// | ^
300/// | |
301/// \ /
302/// \ /
303/// [Store]
304void X86DAGToDAGISel::InstructionSelectPreprocess(SelectionDAG &DAG) {
305 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
306 E = DAG.allnodes_end(); I != E; ++I) {
307 if (I->getOpcode() != ISD::STORE)
308 continue;
309 SDOperand Chain = I->getOperand(0);
310 if (Chain.Val->getOpcode() != ISD::TokenFactor)
311 continue;
312
313 SDOperand N1 = I->getOperand(1);
314 SDOperand N2 = I->getOperand(2);
315 if (!N1.hasOneUse())
316 continue;
317
318 bool RModW = false;
319 SDOperand Load;
320 unsigned Opcode = N1.Val->getOpcode();
321 switch (Opcode) {
322 case ISD::ADD:
323 case ISD::MUL:
Evan Cheng64a9e282006-08-28 20:10:17 +0000324 case ISD::AND:
325 case ISD::OR:
326 case ISD::XOR:
327 case ISD::ADDC:
328 case ISD::ADDE: {
329 SDOperand N10 = N1.getOperand(0);
330 SDOperand N11 = N1.getOperand(1);
331 if (N10.Val->getOpcode() == ISD::LOAD)
332 RModW = true;
333 else if (N11.Val->getOpcode() == ISD::LOAD) {
334 RModW = true;
335 std::swap(N10, N11);
336 }
337 RModW = RModW && N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Chengc07feb142006-08-29 06:44:17 +0000338 (N10.getOperand(1) == N2) &&
339 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng64a9e282006-08-28 20:10:17 +0000340 if (RModW)
341 Load = N10;
342 break;
343 }
344 case ISD::SUB:
345 case ISD::SHL:
346 case ISD::SRA:
347 case ISD::SRL:
348 case ISD::ROTL:
349 case ISD::ROTR:
350 case ISD::SUBC:
351 case ISD::SUBE:
352 case X86ISD::SHLD:
353 case X86ISD::SHRD: {
354 SDOperand N10 = N1.getOperand(0);
355 if (N10.Val->getOpcode() == ISD::LOAD)
356 RModW = N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Chengc07feb142006-08-29 06:44:17 +0000357 (N10.getOperand(1) == N2) &&
358 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng64a9e282006-08-28 20:10:17 +0000359 if (RModW)
360 Load = N10;
361 break;
362 }
363 }
364
Evan Chengc07feb142006-08-29 06:44:17 +0000365 if (RModW) {
Evan Cheng64a9e282006-08-28 20:10:17 +0000366 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
Evan Chengc07feb142006-08-29 06:44:17 +0000367 ++NumLoadMoved;
368 }
Evan Cheng64a9e282006-08-28 20:10:17 +0000369 }
370}
371
Chris Lattner655e7df2005-11-16 01:54:32 +0000372/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
373/// when it has created a SelectionDAG for us to codegen.
374void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
375 DEBUG(BB->dump());
Chris Lattner7c551262006-01-11 01:15:34 +0000376 MachineFunction::iterator FirstMBB = BB;
Chris Lattner655e7df2005-11-16 01:54:32 +0000377
Evan Cheng358b9ed2006-08-29 18:28:33 +0000378 if (!FastISel)
Evan Cheng64a9e282006-08-28 20:10:17 +0000379 InstructionSelectPreprocess(DAG);
380
Chris Lattner655e7df2005-11-16 01:54:32 +0000381 // Codegen the basic block.
Evan Chengd49cc362006-02-10 22:24:32 +0000382#ifndef NDEBUG
383 DEBUG(std::cerr << "===== Instruction selection begins:\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000384 Indent = 0;
Evan Chengd49cc362006-02-10 22:24:32 +0000385#endif
Evan Cheng54cb1832006-02-05 06:46:41 +0000386 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengd49cc362006-02-10 22:24:32 +0000387#ifndef NDEBUG
388 DEBUG(std::cerr << "===== Instruction selection ends:\n");
389#endif
Evan Cheng3b5e0ca2006-07-28 00:10:59 +0000390
Chris Lattner655e7df2005-11-16 01:54:32 +0000391 DAG.RemoveDeadNodes();
392
393 // Emit machine code to BB.
394 ScheduleAndEmitDAG(DAG);
Chris Lattner7c551262006-01-11 01:15:34 +0000395
396 // If we are emitting FP stack code, scan the basic block to determine if this
397 // block defines any FP values. If so, put an FP_REG_KILL instruction before
398 // the terminator of the block.
Evan Chengcde9e302006-01-27 08:10:46 +0000399 if (!Subtarget->hasSSE2()) {
Chris Lattner7c551262006-01-11 01:15:34 +0000400 // Note that FP stack instructions *are* used in SSE code when returning
401 // values, but these are not live out of the basic block, so we don't need
402 // an FP_REG_KILL in this case either.
403 bool ContainsFPCode = false;
404
405 // Scan all of the machine instructions in these MBBs, checking for FP
406 // stores.
407 MachineFunction::iterator MBBI = FirstMBB;
408 do {
409 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
410 !ContainsFPCode && I != E; ++I) {
411 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
412 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
413 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
414 RegMap->getRegClass(I->getOperand(0).getReg()) ==
415 X86::RFPRegisterClass) {
416 ContainsFPCode = true;
417 break;
418 }
419 }
420 }
421 } while (!ContainsFPCode && &*(MBBI++) != BB);
422
423 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
424 // a copy of the input value in this block.
425 if (!ContainsFPCode) {
426 // Final check, check LLVM BB's that are successors to the LLVM BB
427 // corresponding to BB for FP PHI nodes.
428 const BasicBlock *LLVMBB = BB->getBasicBlock();
429 const PHINode *PN;
430 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
431 !ContainsFPCode && SI != E; ++SI) {
432 for (BasicBlock::const_iterator II = SI->begin();
433 (PN = dyn_cast<PHINode>(II)); ++II) {
434 if (PN->getType()->isFloatingPoint()) {
435 ContainsFPCode = true;
436 break;
437 }
438 }
439 }
440 }
441
442 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
443 if (ContainsFPCode) {
444 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
445 ++NumFPKill;
446 }
447 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000448}
449
Evan Chengbc7a0f442006-01-11 06:09:51 +0000450/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
451/// the main function.
Evan Chenge8a42362006-06-02 22:38:37 +0000452void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
453 MachineFrameInfo *MFI) {
454 if (Subtarget->TargetType == X86Subtarget::isCygwin)
455 BuildMI(BB, X86::CALLpcrel32, 1).addExternalSymbol("__main");
456
Evan Chengbc7a0f442006-01-11 06:09:51 +0000457 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
458 int CWFrameIdx = MFI->CreateStackObject(2, 2);
459 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
460
461 // Set the high part to be 64-bit precision.
462 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
463 CWFrameIdx, 1).addImm(2);
464
465 // Reload the modified control word now.
466 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
467}
468
469void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
470 // If this is main, emit special code for main.
471 MachineBasicBlock *BB = MF.begin();
472 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
473 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
474}
475
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000476/// MatchAddress - Add the specified node to the specified addressing mode,
477/// returning true if it cannot be done. This just pattern matches for the
478/// addressing mode
Evan Chenga86ba852006-02-11 02:05:36 +0000479bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
480 bool isRoot) {
Evan Chengb9d34bd2006-08-07 22:28:20 +0000481 int id = N.Val->getNodeId();
482 bool Available = isSelected(id);
Evan Chenga86ba852006-02-11 02:05:36 +0000483
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000484 switch (N.getOpcode()) {
485 default: break;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000486 case ISD::Constant:
487 AM.Disp += cast<ConstantSDNode>(N)->getValue();
488 return false;
489
490 case X86ISD::Wrapper:
491 // If both base and index components have been picked, we can't fit
492 // the result available in the register in the addressing mode. Duplicate
493 // GlobalAddress or ConstantPool as displacement.
494 if (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
495 if (ConstantPoolSDNode *CP =
496 dyn_cast<ConstantPoolSDNode>(N.getOperand(0))) {
497 if (AM.CP == 0) {
498 AM.CP = CP->get();
499 AM.Align = CP->getAlignment();
500 AM.Disp += CP->getOffset();
501 return false;
502 }
503 } else if (GlobalAddressSDNode *G =
504 dyn_cast<GlobalAddressSDNode>(N.getOperand(0))) {
505 if (AM.GV == 0) {
506 AM.GV = G->getGlobal();
507 AM.Disp += G->getOffset();
508 return false;
509 }
510 }
511 }
512 break;
513
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000514 case ISD::FrameIndex:
515 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
516 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
517 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
518 return false;
519 }
520 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000521
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000522 case ISD::SHL:
Evan Cheng77d86ff2006-02-25 10:09:08 +0000523 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000524 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
525 unsigned Val = CN->getValue();
526 if (Val == 1 || Val == 2 || Val == 3) {
527 AM.Scale = 1 << Val;
528 SDOperand ShVal = N.Val->getOperand(0);
529
530 // Okay, we know that we have a scale by now. However, if the scaled
531 // value is an add of something and a constant, we can fold the
532 // constant into the disp field here.
533 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
534 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
535 AM.IndexReg = ShVal.Val->getOperand(0);
536 ConstantSDNode *AddVal =
537 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
538 AM.Disp += AddVal->getValue() << Val;
539 } else {
540 AM.IndexReg = ShVal;
541 }
542 return false;
543 }
544 }
545 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000546
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000547 case ISD::MUL:
548 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng77d86ff2006-02-25 10:09:08 +0000549 if (!Available &&
550 AM.BaseType == X86ISelAddressMode::RegBase &&
551 AM.Base.Reg.Val == 0 &&
552 AM.IndexReg.Val == 0)
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000553 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
554 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
555 AM.Scale = unsigned(CN->getValue())-1;
556
557 SDOperand MulVal = N.Val->getOperand(0);
558 SDOperand Reg;
559
560 // Okay, we know that we have a scale by now. However, if the scaled
561 // value is an add of something and a constant, we can fold the
562 // constant into the disp field here.
563 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
564 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
565 Reg = MulVal.Val->getOperand(0);
566 ConstantSDNode *AddVal =
567 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
568 AM.Disp += AddVal->getValue() * CN->getValue();
569 } else {
570 Reg = N.Val->getOperand(0);
571 }
572
573 AM.IndexReg = AM.Base.Reg = Reg;
574 return false;
575 }
576 break;
577
578 case ISD::ADD: {
Evan Cheng77d86ff2006-02-25 10:09:08 +0000579 if (!Available) {
Evan Chenga86ba852006-02-11 02:05:36 +0000580 X86ISelAddressMode Backup = AM;
581 if (!MatchAddress(N.Val->getOperand(0), AM, false) &&
582 !MatchAddress(N.Val->getOperand(1), AM, false))
583 return false;
584 AM = Backup;
585 if (!MatchAddress(N.Val->getOperand(1), AM, false) &&
586 !MatchAddress(N.Val->getOperand(0), AM, false))
587 return false;
588 AM = Backup;
589 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000590 break;
591 }
Evan Cheng734e1e22006-05-30 06:59:36 +0000592
593 case ISD::OR: {
594 if (!Available) {
595 X86ISelAddressMode Backup = AM;
596 // Look for (x << c1) | c2 where (c2 < c1)
597 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(0));
598 if (CN && !MatchAddress(N.Val->getOperand(1), AM, false)) {
599 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
600 AM.Disp = CN->getValue();
601 return false;
602 }
603 }
604 AM = Backup;
605 CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1));
606 if (CN && !MatchAddress(N.Val->getOperand(0), AM, false)) {
607 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
608 AM.Disp = CN->getValue();
609 return false;
610 }
611 }
612 AM = Backup;
613 }
614 break;
615 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000616 }
617
618 // Is the base register already occupied?
619 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
620 // If so, check to see if the scale index register is set.
621 if (AM.IndexReg.Val == 0) {
622 AM.IndexReg = N;
623 AM.Scale = 1;
624 return false;
625 }
626
627 // Otherwise, we cannot select it.
628 return true;
629 }
630
631 // Default, generate it as a register.
632 AM.BaseType = X86ISelAddressMode::RegBase;
633 AM.Base.Reg = N;
634 return false;
635}
636
Evan Chengc9fab312005-12-08 02:01:35 +0000637/// SelectAddr - returns true if it is able pattern match an addressing mode.
638/// It returns the operands which make up the maximal addressing mode it can
639/// match by reference.
640bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
641 SDOperand &Index, SDOperand &Disp) {
642 X86ISelAddressMode AM;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000643 if (MatchAddress(N, AM))
644 return false;
Evan Chengc9fab312005-12-08 02:01:35 +0000645
Evan Chengbc7a0f442006-01-11 06:09:51 +0000646 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Chengd19d51f2006-02-05 05:25:07 +0000647 if (!AM.Base.Reg.Val)
Evan Chengbc7a0f442006-01-11 06:09:51 +0000648 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
Evan Chengc9fab312005-12-08 02:01:35 +0000649 }
Evan Chengbc7a0f442006-01-11 06:09:51 +0000650
Evan Chengd19d51f2006-02-05 05:25:07 +0000651 if (!AM.IndexReg.Val)
Evan Chengbc7a0f442006-01-11 06:09:51 +0000652 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
653
654 getAddressOperands(AM, Base, Scale, Index, Disp);
655 return true;
Evan Chengc9fab312005-12-08 02:01:35 +0000656}
657
Evan Cheng77d86ff2006-02-25 10:09:08 +0000658/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
659/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng77d86ff2006-02-25 10:09:08 +0000660bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base,
661 SDOperand &Scale,
662 SDOperand &Index, SDOperand &Disp) {
663 X86ISelAddressMode AM;
664 if (MatchAddress(N, AM))
665 return false;
666
667 unsigned Complexity = 0;
668 if (AM.BaseType == X86ISelAddressMode::RegBase)
669 if (AM.Base.Reg.Val)
670 Complexity = 1;
671 else
672 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
673 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
674 Complexity = 4;
675
676 if (AM.IndexReg.Val)
677 Complexity++;
678 else
679 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
680
Evan Cheng990c3602006-02-28 21:13:57 +0000681 if (AM.Scale > 2)
Evan Cheng77d86ff2006-02-25 10:09:08 +0000682 Complexity += 2;
Evan Cheng990c3602006-02-28 21:13:57 +0000683 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg
684 else if (AM.Scale > 1)
685 Complexity++;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000686
687 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
688 // to a LEA. This is determined with some expermentation but is by no means
689 // optimal (especially for code size consideration). LEA is nice because of
690 // its three-address nature. Tweak the cost function again when we can run
691 // convertToThreeAddress() at register allocation time.
692 if (AM.GV || AM.CP)
693 Complexity += 2;
694
695 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
696 Complexity++;
697
698 if (Complexity > 2) {
699 getAddressOperands(AM, Base, Scale, Index, Disp);
700 return true;
701 }
Evan Cheng77d86ff2006-02-25 10:09:08 +0000702 return false;
703}
704
Evan Chengd5f2ba02006-02-06 06:02:33 +0000705bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
706 SDOperand &Base, SDOperand &Scale,
707 SDOperand &Index, SDOperand &Disp) {
708 if (N.getOpcode() == ISD::LOAD &&
709 N.hasOneUse() &&
Evan Cheng29ab7c42006-08-16 23:59:00 +0000710 P.Val->isOnlyUse(N.Val) &&
711 CanBeFoldedBy(N.Val, P.Val))
Evan Cheng10d27902006-01-06 20:36:21 +0000712 return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp);
713 return false;
714}
715
716static bool isRegister0(SDOperand Op) {
Evan Chengc9fab312005-12-08 02:01:35 +0000717 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
718 return (R->getReg() == 0);
719 return false;
720}
721
Evan Cheng5588de92006-02-18 00:15:05 +0000722/// getGlobalBaseReg - Output the instructions required to put the
723/// base address to use for accessing globals into a register.
724///
Evan Cheng61413a32006-08-26 05:34:46 +0000725SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng5588de92006-02-18 00:15:05 +0000726 if (!GlobalBaseReg) {
727 // Insert the set of GlobalBaseReg into the first MBB of the function
728 MachineBasicBlock &FirstMBB = BB->getParent()->front();
729 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
730 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
731 // FIXME: when we get to LP64, we will need to create the appropriate
732 // type of register here.
Evan Cheng9fee4422006-05-16 07:21:53 +0000733 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng5588de92006-02-18 00:15:05 +0000734 BuildMI(FirstMBB, MBBI, X86::MovePCtoStack, 0);
735 BuildMI(FirstMBB, MBBI, X86::POP32r, 1, GlobalBaseReg);
736 }
Evan Cheng61413a32006-08-26 05:34:46 +0000737 return CurDAG->getRegister(GlobalBaseReg, MVT::i32).Val;
Evan Cheng5588de92006-02-18 00:15:05 +0000738}
739
Evan Chengf838cfc2006-05-20 01:36:52 +0000740static SDNode *FindCallStartFromCall(SDNode *Node) {
741 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
742 assert(Node->getOperand(0).getValueType() == MVT::Other &&
743 "Node doesn't have a token chain argument!");
744 return FindCallStartFromCall(Node->getOperand(0).Val);
745}
746
Evan Cheng61413a32006-08-26 05:34:46 +0000747SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Cheng00fcb002005-12-15 01:02:48 +0000748 SDNode *Node = N.Val;
749 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng10d27902006-01-06 20:36:21 +0000750 unsigned Opc, MOpc;
751 unsigned Opcode = Node->getOpcode();
Chris Lattner655e7df2005-11-16 01:54:32 +0000752
Evan Chengd49cc362006-02-10 22:24:32 +0000753#ifndef NDEBUG
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000754 DEBUG(std::cerr << std::string(Indent, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000755 DEBUG(std::cerr << "Selecting: ");
756 DEBUG(Node->dump(CurDAG));
757 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000758 Indent += 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000759#endif
760
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000761 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengd49cc362006-02-10 22:24:32 +0000762#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000763 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000764 DEBUG(std::cerr << "== ");
765 DEBUG(Node->dump(CurDAG));
766 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000767 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000768#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +0000769 return NULL; // Already selected.
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000770 }
Evan Cheng2ae799a2006-01-11 22:15:18 +0000771
Evan Cheng10d27902006-01-06 20:36:21 +0000772 switch (Opcode) {
Chris Lattner655e7df2005-11-16 01:54:32 +0000773 default: break;
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000774 case X86ISD::GlobalBaseReg:
Evan Cheng61413a32006-08-26 05:34:46 +0000775 return getGlobalBaseReg();
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000776
Evan Cheng77d86ff2006-02-25 10:09:08 +0000777 case ISD::ADD: {
778 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
779 // code and is matched first so to prevent it from being turned into
780 // LEA32r X+c.
781 SDOperand N0 = N.getOperand(0);
782 SDOperand N1 = N.getOperand(1);
783 if (N.Val->getValueType(0) == MVT::i32 &&
784 N0.getOpcode() == X86ISD::Wrapper &&
785 N1.getOpcode() == ISD::Constant) {
786 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
787 SDOperand C(0, 0);
788 // TODO: handle ExternalSymbolSDNode.
789 if (GlobalAddressSDNode *G =
790 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
791 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), MVT::i32,
792 G->getOffset() + Offset);
793 } else if (ConstantPoolSDNode *CP =
794 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
795 C = CurDAG->getTargetConstantPool(CP->get(), MVT::i32,
796 CP->getAlignment(),
797 CP->getOffset()+Offset);
798 }
799
Evan Cheng2d487222006-08-26 01:05:16 +0000800 if (C.Val)
Evan Cheng34b70ee2006-08-26 08:00:10 +0000801 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, MVT::i32, C);
Evan Cheng77d86ff2006-02-25 10:09:08 +0000802 }
803
804 // Other cases are handled by auto-generated code.
805 break;
Evan Cheng1f342c22006-02-23 02:43:52 +0000806 }
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000807
Evan Cheng10d27902006-01-06 20:36:21 +0000808 case ISD::MULHU:
809 case ISD::MULHS: {
810 if (Opcode == ISD::MULHU)
811 switch (NVT) {
812 default: assert(0 && "Unsupported VT!");
813 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
814 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
815 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
816 }
817 else
818 switch (NVT) {
819 default: assert(0 && "Unsupported VT!");
820 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
821 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
822 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
823 }
824
825 unsigned LoReg, HiReg;
826 switch (NVT) {
827 default: assert(0 && "Unsupported VT!");
828 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
829 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
830 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
831 }
832
833 SDOperand N0 = Node->getOperand(0);
834 SDOperand N1 = Node->getOperand(1);
835
836 bool foldedLoad = false;
837 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Chengd5f2ba02006-02-06 06:02:33 +0000838 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng92e27972006-01-06 23:19:29 +0000839 // MULHU and MULHS are commmutative
840 if (!foldedLoad) {
Evan Chengd5f2ba02006-02-06 06:02:33 +0000841 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng92e27972006-01-06 23:19:29 +0000842 if (foldedLoad) {
843 N0 = Node->getOperand(1);
844 N1 = Node->getOperand(0);
845 }
846 }
847
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000848 SDOperand Chain;
Evan Cheng2d487222006-08-26 01:05:16 +0000849 if (foldedLoad) {
850 Chain = N1.getOperand(0);
851 AddToISelQueue(Chain);
852 } else
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000853 Chain = CurDAG->getEntryNode();
Evan Cheng10d27902006-01-06 20:36:21 +0000854
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000855 SDOperand InFlag(0, 0);
Evan Cheng2d487222006-08-26 01:05:16 +0000856 AddToISelQueue(N0);
Evan Cheng10d27902006-01-06 20:36:21 +0000857 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000858 N0, InFlag);
Evan Cheng10d27902006-01-06 20:36:21 +0000859 InFlag = Chain.getValue(1);
860
861 if (foldedLoad) {
Evan Cheng2d487222006-08-26 01:05:16 +0000862 AddToISelQueue(Tmp0);
863 AddToISelQueue(Tmp1);
864 AddToISelQueue(Tmp2);
865 AddToISelQueue(Tmp3);
Evan Chengc3acfc02006-08-27 08:14:06 +0000866 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
Evan Chengd1b82d82006-02-09 07:17:49 +0000867 SDNode *CNode =
Evan Chengc3acfc02006-08-27 08:14:06 +0000868 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Chengd1b82d82006-02-09 07:17:49 +0000869 Chain = SDOperand(CNode, 0);
870 InFlag = SDOperand(CNode, 1);
Evan Cheng10d27902006-01-06 20:36:21 +0000871 } else {
Evan Cheng2d487222006-08-26 01:05:16 +0000872 AddToISelQueue(N1);
Evan Chengd1b82d82006-02-09 07:17:49 +0000873 InFlag =
874 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng10d27902006-01-06 20:36:21 +0000875 }
876
Evan Cheng61413a32006-08-26 05:34:46 +0000877 SDOperand Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
Evan Chengb9d34bd2006-08-07 22:28:20 +0000878 ReplaceUses(N.getValue(0), Result);
879 if (foldedLoad)
880 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000881
Evan Chengd49cc362006-02-10 22:24:32 +0000882#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000883 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengb9d34bd2006-08-07 22:28:20 +0000884 DEBUG(std::cerr << "=> ");
Evan Chengd49cc362006-02-10 22:24:32 +0000885 DEBUG(Result.Val->dump(CurDAG));
886 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000887 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000888#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +0000889 return NULL;
Evan Cheng92e27972006-01-06 23:19:29 +0000890 }
Evan Cheng5588de92006-02-18 00:15:05 +0000891
Evan Cheng92e27972006-01-06 23:19:29 +0000892 case ISD::SDIV:
893 case ISD::UDIV:
894 case ISD::SREM:
895 case ISD::UREM: {
896 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
897 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
898 if (!isSigned)
899 switch (NVT) {
900 default: assert(0 && "Unsupported VT!");
901 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
902 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
903 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
904 }
905 else
906 switch (NVT) {
907 default: assert(0 && "Unsupported VT!");
908 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
909 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
910 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
911 }
912
913 unsigned LoReg, HiReg;
914 unsigned ClrOpcode, SExtOpcode;
915 switch (NVT) {
916 default: assert(0 && "Unsupported VT!");
917 case MVT::i8:
918 LoReg = X86::AL; HiReg = X86::AH;
Evan Chenga2efb9f2006-06-02 21:20:34 +0000919 ClrOpcode = X86::MOV8r0;
Evan Cheng92e27972006-01-06 23:19:29 +0000920 SExtOpcode = X86::CBW;
921 break;
922 case MVT::i16:
923 LoReg = X86::AX; HiReg = X86::DX;
Evan Chenga2efb9f2006-06-02 21:20:34 +0000924 ClrOpcode = X86::MOV16r0;
Evan Cheng92e27972006-01-06 23:19:29 +0000925 SExtOpcode = X86::CWD;
926 break;
927 case MVT::i32:
928 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chenga2efb9f2006-06-02 21:20:34 +0000929 ClrOpcode = X86::MOV32r0;
Evan Cheng92e27972006-01-06 23:19:29 +0000930 SExtOpcode = X86::CDQ;
931 break;
932 }
933
934 SDOperand N0 = Node->getOperand(0);
935 SDOperand N1 = Node->getOperand(1);
936
937 bool foldedLoad = false;
938 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Chengd5f2ba02006-02-06 06:02:33 +0000939 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000940 SDOperand Chain;
Evan Cheng2d487222006-08-26 01:05:16 +0000941 if (foldedLoad) {
942 Chain = N1.getOperand(0);
943 AddToISelQueue(Chain);
944 } else
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000945 Chain = CurDAG->getEntryNode();
Evan Cheng92e27972006-01-06 23:19:29 +0000946
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000947 SDOperand InFlag(0, 0);
Evan Cheng2d487222006-08-26 01:05:16 +0000948 AddToISelQueue(N0);
Evan Cheng92e27972006-01-06 23:19:29 +0000949 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000950 N0, InFlag);
Evan Cheng92e27972006-01-06 23:19:29 +0000951 InFlag = Chain.getValue(1);
952
953 if (isSigned) {
954 // Sign extend the low part into the high part.
Evan Chengd1b82d82006-02-09 07:17:49 +0000955 InFlag =
956 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000957 } else {
958 // Zero out the high part, effectively zero extending the input.
Evan Chenga2efb9f2006-06-02 21:20:34 +0000959 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000960 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT),
961 ClrNode, InFlag);
962 InFlag = Chain.getValue(1);
963 }
964
965 if (foldedLoad) {
Evan Cheng2d487222006-08-26 01:05:16 +0000966 AddToISelQueue(Tmp0);
967 AddToISelQueue(Tmp1);
968 AddToISelQueue(Tmp2);
969 AddToISelQueue(Tmp3);
Evan Chengc3acfc02006-08-27 08:14:06 +0000970 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
Evan Chengd1b82d82006-02-09 07:17:49 +0000971 SDNode *CNode =
Evan Chengc3acfc02006-08-27 08:14:06 +0000972 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Chengd1b82d82006-02-09 07:17:49 +0000973 Chain = SDOperand(CNode, 0);
974 InFlag = SDOperand(CNode, 1);
Evan Cheng92e27972006-01-06 23:19:29 +0000975 } else {
Evan Cheng2d487222006-08-26 01:05:16 +0000976 AddToISelQueue(N1);
Evan Chengd1b82d82006-02-09 07:17:49 +0000977 InFlag =
978 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000979 }
980
Evan Cheng61413a32006-08-26 05:34:46 +0000981 SDOperand Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg,
982 NVT, InFlag);
Evan Chengb9d34bd2006-08-07 22:28:20 +0000983 ReplaceUses(N.getValue(0), Result);
984 if (foldedLoad)
985 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Chengd49cc362006-02-10 22:24:32 +0000986
987#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000988 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengb9d34bd2006-08-07 22:28:20 +0000989 DEBUG(std::cerr << "=> ");
Evan Chengd49cc362006-02-10 22:24:32 +0000990 DEBUG(Result.Val->dump(CurDAG));
991 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000992 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000993#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +0000994
995 return NULL;
Evan Cheng10d27902006-01-06 20:36:21 +0000996 }
Evan Cheng9733bde2006-05-08 08:01:26 +0000997
998 case ISD::TRUNCATE: {
999 if (NVT == MVT::i8) {
1000 unsigned Opc2;
1001 MVT::ValueType VT;
1002 switch (Node->getOperand(0).getValueType()) {
1003 default: assert(0 && "Unknown truncate!");
1004 case MVT::i16:
1005 Opc = X86::MOV16to16_;
1006 VT = MVT::i16;
Evan Cheng9fee4422006-05-16 07:21:53 +00001007 Opc2 = X86::TRUNC_GR16_GR8;
Evan Cheng9733bde2006-05-08 08:01:26 +00001008 break;
1009 case MVT::i32:
1010 Opc = X86::MOV32to32_;
1011 VT = MVT::i32;
Evan Cheng9fee4422006-05-16 07:21:53 +00001012 Opc2 = X86::TRUNC_GR32_GR8;
Evan Cheng9733bde2006-05-08 08:01:26 +00001013 break;
1014 }
1015
Evan Cheng2d487222006-08-26 01:05:16 +00001016 AddToISelQueue(Node->getOperand(0));
1017 SDOperand Tmp =
1018 SDOperand(CurDAG->getTargetNode(Opc, VT, Node->getOperand(0)), 0);
Evan Cheng61413a32006-08-26 05:34:46 +00001019 SDNode *ResNode = CurDAG->getTargetNode(Opc2, NVT, Tmp);
Evan Cheng9733bde2006-05-08 08:01:26 +00001020
1021#ifndef NDEBUG
1022 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengb9d34bd2006-08-07 22:28:20 +00001023 DEBUG(std::cerr << "=> ");
Evan Cheng61413a32006-08-26 05:34:46 +00001024 DEBUG(ResNode->dump(CurDAG));
Evan Cheng9733bde2006-05-08 08:01:26 +00001025 DEBUG(std::cerr << "\n");
1026 Indent -= 2;
1027#endif
Evan Cheng61413a32006-08-26 05:34:46 +00001028 return ResNode;
Evan Cheng9733bde2006-05-08 08:01:26 +00001029 }
Evan Chenga26c4512006-05-20 07:44:28 +00001030
1031 break;
Evan Cheng9733bde2006-05-08 08:01:26 +00001032 }
Chris Lattner655e7df2005-11-16 01:54:32 +00001033 }
1034
Evan Cheng61413a32006-08-26 05:34:46 +00001035 SDNode *ResNode = SelectCode(N);
Evan Chengbd1c5a82006-08-11 09:08:15 +00001036
Evan Chengd49cc362006-02-10 22:24:32 +00001037#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +00001038 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +00001039 DEBUG(std::cerr << "=> ");
Evan Cheng61413a32006-08-26 05:34:46 +00001040 if (ResNode == NULL || ResNode == N.Val)
1041 DEBUG(N.Val->dump(CurDAG));
1042 else
1043 DEBUG(ResNode->dump(CurDAG));
Evan Chengd49cc362006-02-10 22:24:32 +00001044 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +00001045 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +00001046#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +00001047
1048 return ResNode;
Chris Lattner655e7df2005-11-16 01:54:32 +00001049}
1050
Chris Lattnerba1ed582006-06-08 18:03:49 +00001051bool X86DAGToDAGISel::
1052SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1053 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1054 SDOperand Op0, Op1, Op2, Op3;
1055 switch (ConstraintCode) {
1056 case 'o': // offsetable ??
1057 case 'v': // not offsetable ??
1058 default: return true;
1059 case 'm': // memory
1060 if (!SelectAddr(Op, Op0, Op1, Op2, Op3))
1061 return true;
1062 break;
1063 }
1064
Evan Cheng2d487222006-08-26 01:05:16 +00001065 OutOps.push_back(Op0);
1066 OutOps.push_back(Op1);
1067 OutOps.push_back(Op2);
1068 OutOps.push_back(Op3);
1069 AddToISelQueue(Op0);
1070 AddToISelQueue(Op1);
1071 AddToISelQueue(Op2);
1072 AddToISelQueue(Op3);
Chris Lattnerba1ed582006-06-08 18:03:49 +00001073 return false;
1074}
1075
Chris Lattner655e7df2005-11-16 01:54:32 +00001076/// createX86ISelDag - This pass converts a legalized DAG into a
1077/// X86-specific DAG, ready for instruction scheduling.
1078///
Evan Cheng358b9ed2006-08-29 18:28:33 +00001079FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1080 return new X86DAGToDAGISel(TM, Fast);
Chris Lattner655e7df2005-11-16 01:54:32 +00001081}