blob: c5ffb06feec0c40837b55230298b59590258a6c0 [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
42//===----------------------------------------------------------------------===//
43// Pattern Matcher Implementation
44//===----------------------------------------------------------------------===//
45
46namespace {
Chris Lattner3f0f71b2005-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 Lattneraa2372562006-05-24 17:04:05 +000053 FrameIndexBase
Chris Lattner3f0f71b2005-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
61 unsigned Scale;
62 SDOperand IndexReg;
63 unsigned Disp;
64 GlobalValue *GV;
Evan Cheng77d86ff2006-02-25 10:09:08 +000065 Constant *CP;
66 unsigned Align; // CP alignment.
Chris Lattner3f0f71b2005-11-19 02:11:08 +000067
68 X86ISelAddressMode()
Evan Cheng77d86ff2006-02-25 10:09:08 +000069 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0), GV(0),
70 CP(0), Align(0) {
Chris Lattner3f0f71b2005-11-19 02:11:08 +000071 }
72 };
73}
74
75namespace {
Chris Lattner655e7df2005-11-16 01:54:32 +000076 Statistic<>
77 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
78
Evan Chengc07feb142006-08-29 06:44:17 +000079 Statistic<>
80 NumLoadMoved("x86-codegen", "Number of loads moved below TokenFactor");
81
Chris Lattner655e7df2005-11-16 01:54:32 +000082 //===--------------------------------------------------------------------===//
83 /// ISel - X86 specific code to select X86 machine instructions for
84 /// SelectionDAG operations.
85 ///
Chris Lattner0cc59072006-06-28 23:27:49 +000086 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattner655e7df2005-11-16 01:54:32 +000087 /// ContainsFPCode - Every instruction we select that uses or defines a FP
88 /// register should set this to true.
89 bool ContainsFPCode;
90
Evan Cheng358b9ed2006-08-29 18:28:33 +000091 /// FastISel - Enable fast(er) instruction selection.
92 ///
93 bool FastISel;
94
Chris Lattner655e7df2005-11-16 01:54:32 +000095 /// X86Lowering - This object fully describes how to lower LLVM code to an
96 /// X86-specific SelectionDAG.
97 X86TargetLowering X86Lowering;
98
99 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
100 /// make the right decision when generating code for different targets.
101 const X86Subtarget *Subtarget;
Evan Cheng5588de92006-02-18 00:15:05 +0000102
103 unsigned GlobalBaseReg;
Evan Cheng691a63d2006-07-27 16:44:36 +0000104
Chris Lattner655e7df2005-11-16 01:54:32 +0000105 public:
Evan Cheng358b9ed2006-08-29 18:28:33 +0000106 X86DAGToDAGISel(X86TargetMachine &TM, bool fast)
Evan Cheng2dd2c652006-03-13 23:20:37 +0000107 : SelectionDAGISel(X86Lowering),
Evan Cheng358b9ed2006-08-29 18:28:33 +0000108 ContainsFPCode(false), FastISel(fast),
Evan Cheng691a63d2006-07-27 16:44:36 +0000109 X86Lowering(*TM.getTargetLowering()),
Evan Cheng72bb66a2006-08-08 00:31:00 +0000110 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
Chris Lattner655e7df2005-11-16 01:54:32 +0000111
Evan Cheng5588de92006-02-18 00:15:05 +0000112 virtual bool runOnFunction(Function &Fn) {
113 // Make sure we re-emit a set of the global base reg if necessary
114 GlobalBaseReg = 0;
115 return SelectionDAGISel::runOnFunction(Fn);
116 }
117
Chris Lattner655e7df2005-11-16 01:54:32 +0000118 virtual const char *getPassName() const {
119 return "X86 DAG->DAG Instruction Selection";
120 }
121
122 /// InstructionSelectBasicBlock - This callback is invoked by
123 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
124 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
125
Evan Chengbc7a0f442006-01-11 06:09:51 +0000126 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
127
Evan Chenge2a3f702006-07-28 01:03:48 +0000128 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U);
Evan Cheng691a63d2006-07-27 16:44:36 +0000129
Chris Lattner655e7df2005-11-16 01:54:32 +0000130// Include the pieces autogenerated from the target description.
131#include "X86GenDAGISel.inc"
132
133 private:
Evan Cheng61413a32006-08-26 05:34:46 +0000134 SDNode *Select(SDOperand N);
Chris Lattner655e7df2005-11-16 01:54:32 +0000135
Evan Chenga86ba852006-02-11 02:05:36 +0000136 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM, bool isRoot = true);
Evan Chengc9fab312005-12-08 02:01:35 +0000137 bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
138 SDOperand &Index, SDOperand &Disp);
139 bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
140 SDOperand &Index, SDOperand &Disp);
Evan Chengd5f2ba02006-02-06 06:02:33 +0000141 bool TryFoldLoad(SDOperand P, SDOperand N,
142 SDOperand &Base, SDOperand &Scale,
Evan Cheng10d27902006-01-06 20:36:21 +0000143 SDOperand &Index, SDOperand &Disp);
Evan Cheng64a9e282006-08-28 20:10:17 +0000144 void InstructionSelectPreprocess(SelectionDAG &DAG);
Evan Chengb9d34bd2006-08-07 22:28:20 +0000145
Chris Lattnerba1ed582006-06-08 18:03:49 +0000146 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
147 /// inline asm expressions.
148 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
149 char ConstraintCode,
150 std::vector<SDOperand> &OutOps,
151 SelectionDAG &DAG);
152
Evan Chenge8a42362006-06-02 22:38:37 +0000153 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
154
Evan Cheng67ed58e2005-12-12 21:49:40 +0000155 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
156 SDOperand &Scale, SDOperand &Index,
157 SDOperand &Disp) {
158 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
159 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, MVT::i32) : AM.Base.Reg;
Evan Cheng1d712482005-12-17 09:13:43 +0000160 Scale = getI8Imm(AM.Scale);
Evan Cheng67ed58e2005-12-12 21:49:40 +0000161 Index = AM.IndexReg;
162 Disp = AM.GV ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp)
Evan Cheng77d86ff2006-02-25 10:09:08 +0000163 : (AM.CP ?
164 CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp)
165 : getI32Imm(AM.Disp));
Evan Cheng67ed58e2005-12-12 21:49:40 +0000166 }
167
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000168 /// getI8Imm - Return a target constant with the specified value, of type
169 /// i8.
170 inline SDOperand getI8Imm(unsigned Imm) {
171 return CurDAG->getTargetConstant(Imm, MVT::i8);
172 }
173
Chris Lattner655e7df2005-11-16 01:54:32 +0000174 /// getI16Imm - Return a target constant with the specified value, of type
175 /// i16.
176 inline SDOperand getI16Imm(unsigned Imm) {
177 return CurDAG->getTargetConstant(Imm, MVT::i16);
178 }
179
180 /// getI32Imm - Return a target constant with the specified value, of type
181 /// i32.
182 inline SDOperand getI32Imm(unsigned Imm) {
183 return CurDAG->getTargetConstant(Imm, MVT::i32);
184 }
Evan Chengd49cc362006-02-10 22:24:32 +0000185
Evan Cheng5588de92006-02-18 00:15:05 +0000186 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
187 /// base register. Return the virtual register that holds this value.
Evan Cheng61413a32006-08-26 05:34:46 +0000188 SDNode *getGlobalBaseReg();
Evan Cheng5588de92006-02-18 00:15:05 +0000189
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000190#ifndef NDEBUG
191 unsigned Indent;
192#endif
Chris Lattner655e7df2005-11-16 01:54:32 +0000193 };
194}
195
Evan Cheng72bb66a2006-08-08 00:31:00 +0000196static void findNonImmUse(SDNode* Use, SDNode* Def, bool &found,
197 std::set<SDNode *> &Visited) {
198 if (found ||
199 Use->getNodeId() > Def->getNodeId() ||
200 !Visited.insert(Use).second)
201 return;
202
203 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
204 SDNode *N = Use->getOperand(i).Val;
205 if (N != Def) {
206 findNonImmUse(N, Def, found, Visited);
207 } else {
208 found = true;
209 break;
210 }
211 }
212}
213
214static inline bool isNonImmUse(SDNode* Use, SDNode* Def) {
215 std::set<SDNode *> Visited;
216 bool found = false;
217 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
218 SDNode *N = Use->getOperand(i).Val;
219 if (N != Def) {
220 findNonImmUse(N, Def, found, Visited);
221 if (found) break;
222 }
223 }
224 return found;
225}
226
227
Evan Chenge2a3f702006-07-28 01:03:48 +0000228bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U) {
Evan Cheng691a63d2006-07-27 16:44:36 +0000229 // If U use can somehow reach N through another path then U can't fold N or
230 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Chenge8071ec2006-07-28 06:33:41 +0000231 // through X. If N is folded into into U, then X is both a predecessor and
Evan Cheng691a63d2006-07-27 16:44:36 +0000232 // a successor of U.
233 //
234 // [ N ]
235 // ^ ^
236 // | |
237 // / \---
238 // / [X]
239 // | ^
240 // [U]--------|
Evan Cheng358b9ed2006-08-29 18:28:33 +0000241 return !FastISel && !isNonImmUse(U, N);
Evan Cheng691a63d2006-07-27 16:44:36 +0000242}
243
Evan Cheng64a9e282006-08-28 20:10:17 +0000244/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
245/// and move load below the TokenFactor. Replace store's chain operand with
246/// load's chain result.
247static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load,
248 SDOperand Store, SDOperand TF) {
249 std::vector<SDOperand> Ops;
250 for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i)
251 if (Load.Val == TF.Val->getOperand(i).Val)
252 Ops.push_back(Load.Val->getOperand(0));
253 else
254 Ops.push_back(TF.Val->getOperand(i));
255 DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size());
256 DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2));
257 DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1),
258 Store.getOperand(2), Store.getOperand(3));
259}
260
261/// InstructionSelectPreprocess - Preprocess the DAG to allow the instruction
262/// selector to pick more load-modify-store instructions. This is a common
263/// case:
264///
265/// [Load chain]
266/// ^
267/// |
268/// [Load]
269/// ^ ^
270/// | |
271/// / \-
272/// / |
273/// [TokenFactor] [Op]
274/// ^ ^
275/// | |
276/// \ /
277/// \ /
278/// [Store]
279///
280/// The fact the store's chain operand != load's chain will prevent the
281/// (store (op (load))) instruction from being selected. We can transform it to:
282///
283/// [Load chain]
284/// ^
285/// |
286/// [TokenFactor]
287/// ^
288/// |
289/// [Load]
290/// ^ ^
291/// | |
292/// | \-
293/// | |
294/// | [Op]
295/// | ^
296/// | |
297/// \ /
298/// \ /
299/// [Store]
300void X86DAGToDAGISel::InstructionSelectPreprocess(SelectionDAG &DAG) {
301 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
302 E = DAG.allnodes_end(); I != E; ++I) {
303 if (I->getOpcode() != ISD::STORE)
304 continue;
305 SDOperand Chain = I->getOperand(0);
306 if (Chain.Val->getOpcode() != ISD::TokenFactor)
307 continue;
308
309 SDOperand N1 = I->getOperand(1);
310 SDOperand N2 = I->getOperand(2);
Evan Cheng2c4e0f12006-09-01 22:52:28 +0000311 if (MVT::isFloatingPoint(N1.getValueType()) ||
312 MVT::isVector(N1.getValueType()) ||
Evan Chengdfb85152006-08-29 18:37:37 +0000313 !N1.hasOneUse())
Evan Cheng64a9e282006-08-28 20:10:17 +0000314 continue;
315
316 bool RModW = false;
317 SDOperand Load;
318 unsigned Opcode = N1.Val->getOpcode();
319 switch (Opcode) {
320 case ISD::ADD:
321 case ISD::MUL:
Evan Cheng64a9e282006-08-28 20:10:17 +0000322 case ISD::AND:
323 case ISD::OR:
324 case ISD::XOR:
325 case ISD::ADDC:
326 case ISD::ADDE: {
327 SDOperand N10 = N1.getOperand(0);
328 SDOperand N11 = N1.getOperand(1);
329 if (N10.Val->getOpcode() == ISD::LOAD)
330 RModW = true;
331 else if (N11.Val->getOpcode() == ISD::LOAD) {
332 RModW = true;
333 std::swap(N10, N11);
334 }
335 RModW = RModW && N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Chengc07feb142006-08-29 06:44:17 +0000336 (N10.getOperand(1) == N2) &&
337 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng64a9e282006-08-28 20:10:17 +0000338 if (RModW)
339 Load = N10;
340 break;
341 }
342 case ISD::SUB:
343 case ISD::SHL:
344 case ISD::SRA:
345 case ISD::SRL:
346 case ISD::ROTL:
347 case ISD::ROTR:
348 case ISD::SUBC:
349 case ISD::SUBE:
350 case X86ISD::SHLD:
351 case X86ISD::SHRD: {
352 SDOperand N10 = N1.getOperand(0);
353 if (N10.Val->getOpcode() == ISD::LOAD)
354 RModW = N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Chengc07feb142006-08-29 06:44:17 +0000355 (N10.getOperand(1) == N2) &&
356 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng64a9e282006-08-28 20:10:17 +0000357 if (RModW)
358 Load = N10;
359 break;
360 }
361 }
362
Evan Chengc07feb142006-08-29 06:44:17 +0000363 if (RModW) {
Evan Cheng64a9e282006-08-28 20:10:17 +0000364 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
Evan Chengc07feb142006-08-29 06:44:17 +0000365 ++NumLoadMoved;
366 }
Evan Cheng64a9e282006-08-28 20:10:17 +0000367 }
368}
369
Chris Lattner655e7df2005-11-16 01:54:32 +0000370/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
371/// when it has created a SelectionDAG for us to codegen.
372void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
373 DEBUG(BB->dump());
Chris Lattner7c551262006-01-11 01:15:34 +0000374 MachineFunction::iterator FirstMBB = BB;
Chris Lattner655e7df2005-11-16 01:54:32 +0000375
Evan Cheng358b9ed2006-08-29 18:28:33 +0000376 if (!FastISel)
Evan Cheng64a9e282006-08-28 20:10:17 +0000377 InstructionSelectPreprocess(DAG);
378
Chris Lattner655e7df2005-11-16 01:54:32 +0000379 // Codegen the basic block.
Evan Chengd49cc362006-02-10 22:24:32 +0000380#ifndef NDEBUG
381 DEBUG(std::cerr << "===== Instruction selection begins:\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000382 Indent = 0;
Evan Chengd49cc362006-02-10 22:24:32 +0000383#endif
Evan Cheng54cb1832006-02-05 06:46:41 +0000384 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengd49cc362006-02-10 22:24:32 +0000385#ifndef NDEBUG
386 DEBUG(std::cerr << "===== Instruction selection ends:\n");
387#endif
Evan Cheng3b5e0ca2006-07-28 00:10:59 +0000388
Chris Lattner655e7df2005-11-16 01:54:32 +0000389 DAG.RemoveDeadNodes();
390
391 // Emit machine code to BB.
392 ScheduleAndEmitDAG(DAG);
Chris Lattner7c551262006-01-11 01:15:34 +0000393
394 // If we are emitting FP stack code, scan the basic block to determine if this
395 // block defines any FP values. If so, put an FP_REG_KILL instruction before
396 // the terminator of the block.
Evan Chengcde9e302006-01-27 08:10:46 +0000397 if (!Subtarget->hasSSE2()) {
Chris Lattner7c551262006-01-11 01:15:34 +0000398 // Note that FP stack instructions *are* used in SSE code when returning
399 // values, but these are not live out of the basic block, so we don't need
400 // an FP_REG_KILL in this case either.
401 bool ContainsFPCode = false;
402
403 // Scan all of the machine instructions in these MBBs, checking for FP
404 // stores.
405 MachineFunction::iterator MBBI = FirstMBB;
406 do {
407 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
408 !ContainsFPCode && I != E; ++I) {
409 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
410 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
411 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
412 RegMap->getRegClass(I->getOperand(0).getReg()) ==
413 X86::RFPRegisterClass) {
414 ContainsFPCode = true;
415 break;
416 }
417 }
418 }
419 } while (!ContainsFPCode && &*(MBBI++) != BB);
420
421 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
422 // a copy of the input value in this block.
423 if (!ContainsFPCode) {
424 // Final check, check LLVM BB's that are successors to the LLVM BB
425 // corresponding to BB for FP PHI nodes.
426 const BasicBlock *LLVMBB = BB->getBasicBlock();
427 const PHINode *PN;
428 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
429 !ContainsFPCode && SI != E; ++SI) {
430 for (BasicBlock::const_iterator II = SI->begin();
431 (PN = dyn_cast<PHINode>(II)); ++II) {
432 if (PN->getType()->isFloatingPoint()) {
433 ContainsFPCode = true;
434 break;
435 }
436 }
437 }
438 }
439
440 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
441 if (ContainsFPCode) {
442 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
443 ++NumFPKill;
444 }
445 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000446}
447
Evan Chengbc7a0f442006-01-11 06:09:51 +0000448/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
449/// the main function.
Evan Chenge8a42362006-06-02 22:38:37 +0000450void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
451 MachineFrameInfo *MFI) {
452 if (Subtarget->TargetType == X86Subtarget::isCygwin)
453 BuildMI(BB, X86::CALLpcrel32, 1).addExternalSymbol("__main");
454
Evan Chengbc7a0f442006-01-11 06:09:51 +0000455 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
456 int CWFrameIdx = MFI->CreateStackObject(2, 2);
457 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
458
459 // Set the high part to be 64-bit precision.
460 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
461 CWFrameIdx, 1).addImm(2);
462
463 // Reload the modified control word now.
464 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
465}
466
467void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
468 // If this is main, emit special code for main.
469 MachineBasicBlock *BB = MF.begin();
470 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
471 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
472}
473
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000474/// MatchAddress - Add the specified node to the specified addressing mode,
475/// returning true if it cannot be done. This just pattern matches for the
476/// addressing mode
Evan Chenga86ba852006-02-11 02:05:36 +0000477bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
478 bool isRoot) {
Evan Chengb9d34bd2006-08-07 22:28:20 +0000479 int id = N.Val->getNodeId();
480 bool Available = isSelected(id);
Evan Chenga86ba852006-02-11 02:05:36 +0000481
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000482 switch (N.getOpcode()) {
483 default: break;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000484 case ISD::Constant:
485 AM.Disp += cast<ConstantSDNode>(N)->getValue();
486 return false;
487
488 case X86ISD::Wrapper:
489 // If both base and index components have been picked, we can't fit
490 // the result available in the register in the addressing mode. Duplicate
491 // GlobalAddress or ConstantPool as displacement.
492 if (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
493 if (ConstantPoolSDNode *CP =
494 dyn_cast<ConstantPoolSDNode>(N.getOperand(0))) {
495 if (AM.CP == 0) {
496 AM.CP = CP->get();
497 AM.Align = CP->getAlignment();
498 AM.Disp += CP->getOffset();
499 return false;
500 }
501 } else if (GlobalAddressSDNode *G =
502 dyn_cast<GlobalAddressSDNode>(N.getOperand(0))) {
503 if (AM.GV == 0) {
504 AM.GV = G->getGlobal();
505 AM.Disp += G->getOffset();
506 return false;
507 }
508 }
509 }
510 break;
511
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000512 case ISD::FrameIndex:
513 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
514 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
515 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
516 return false;
517 }
518 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000519
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000520 case ISD::SHL:
Evan Cheng77d86ff2006-02-25 10:09:08 +0000521 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000522 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
523 unsigned Val = CN->getValue();
524 if (Val == 1 || Val == 2 || Val == 3) {
525 AM.Scale = 1 << Val;
526 SDOperand ShVal = N.Val->getOperand(0);
527
528 // Okay, we know that we have a scale by now. However, if the scaled
529 // value is an add of something and a constant, we can fold the
530 // constant into the disp field here.
531 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
532 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
533 AM.IndexReg = ShVal.Val->getOperand(0);
534 ConstantSDNode *AddVal =
535 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
536 AM.Disp += AddVal->getValue() << Val;
537 } else {
538 AM.IndexReg = ShVal;
539 }
540 return false;
541 }
542 }
543 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000544
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000545 case ISD::MUL:
546 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng77d86ff2006-02-25 10:09:08 +0000547 if (!Available &&
548 AM.BaseType == X86ISelAddressMode::RegBase &&
549 AM.Base.Reg.Val == 0 &&
550 AM.IndexReg.Val == 0)
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000551 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
552 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
553 AM.Scale = unsigned(CN->getValue())-1;
554
555 SDOperand MulVal = N.Val->getOperand(0);
556 SDOperand Reg;
557
558 // Okay, we know that we have a scale by now. However, if the scaled
559 // value is an add of something and a constant, we can fold the
560 // constant into the disp field here.
561 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
562 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
563 Reg = MulVal.Val->getOperand(0);
564 ConstantSDNode *AddVal =
565 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
566 AM.Disp += AddVal->getValue() * CN->getValue();
567 } else {
568 Reg = N.Val->getOperand(0);
569 }
570
571 AM.IndexReg = AM.Base.Reg = Reg;
572 return false;
573 }
574 break;
575
576 case ISD::ADD: {
Evan Cheng77d86ff2006-02-25 10:09:08 +0000577 if (!Available) {
Evan Chenga86ba852006-02-11 02:05:36 +0000578 X86ISelAddressMode Backup = AM;
579 if (!MatchAddress(N.Val->getOperand(0), AM, false) &&
580 !MatchAddress(N.Val->getOperand(1), AM, false))
581 return false;
582 AM = Backup;
583 if (!MatchAddress(N.Val->getOperand(1), AM, false) &&
584 !MatchAddress(N.Val->getOperand(0), AM, false))
585 return false;
586 AM = Backup;
587 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000588 break;
589 }
Evan Cheng734e1e22006-05-30 06:59:36 +0000590
591 case ISD::OR: {
592 if (!Available) {
593 X86ISelAddressMode Backup = AM;
594 // Look for (x << c1) | c2 where (c2 < c1)
595 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(0));
596 if (CN && !MatchAddress(N.Val->getOperand(1), AM, false)) {
597 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
598 AM.Disp = CN->getValue();
599 return false;
600 }
601 }
602 AM = Backup;
603 CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1));
604 if (CN && !MatchAddress(N.Val->getOperand(0), AM, false)) {
605 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
606 AM.Disp = CN->getValue();
607 return false;
608 }
609 }
610 AM = Backup;
611 }
612 break;
613 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000614 }
615
616 // Is the base register already occupied?
617 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
618 // If so, check to see if the scale index register is set.
619 if (AM.IndexReg.Val == 0) {
620 AM.IndexReg = N;
621 AM.Scale = 1;
622 return false;
623 }
624
625 // Otherwise, we cannot select it.
626 return true;
627 }
628
629 // Default, generate it as a register.
630 AM.BaseType = X86ISelAddressMode::RegBase;
631 AM.Base.Reg = N;
632 return false;
633}
634
Evan Chengc9fab312005-12-08 02:01:35 +0000635/// SelectAddr - returns true if it is able pattern match an addressing mode.
636/// It returns the operands which make up the maximal addressing mode it can
637/// match by reference.
638bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
639 SDOperand &Index, SDOperand &Disp) {
640 X86ISelAddressMode AM;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000641 if (MatchAddress(N, AM))
642 return false;
Evan Chengc9fab312005-12-08 02:01:35 +0000643
Evan Chengbc7a0f442006-01-11 06:09:51 +0000644 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Chengd19d51f2006-02-05 05:25:07 +0000645 if (!AM.Base.Reg.Val)
Evan Chengbc7a0f442006-01-11 06:09:51 +0000646 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
Evan Chengc9fab312005-12-08 02:01:35 +0000647 }
Evan Chengbc7a0f442006-01-11 06:09:51 +0000648
Evan Chengd19d51f2006-02-05 05:25:07 +0000649 if (!AM.IndexReg.Val)
Evan Chengbc7a0f442006-01-11 06:09:51 +0000650 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
651
652 getAddressOperands(AM, Base, Scale, Index, Disp);
653 return true;
Evan Chengc9fab312005-12-08 02:01:35 +0000654}
655
Evan Cheng77d86ff2006-02-25 10:09:08 +0000656/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
657/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng77d86ff2006-02-25 10:09:08 +0000658bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base,
659 SDOperand &Scale,
660 SDOperand &Index, SDOperand &Disp) {
661 X86ISelAddressMode AM;
662 if (MatchAddress(N, AM))
663 return false;
664
665 unsigned Complexity = 0;
666 if (AM.BaseType == X86ISelAddressMode::RegBase)
667 if (AM.Base.Reg.Val)
668 Complexity = 1;
669 else
670 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
671 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
672 Complexity = 4;
673
674 if (AM.IndexReg.Val)
675 Complexity++;
676 else
677 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
678
Evan Cheng990c3602006-02-28 21:13:57 +0000679 if (AM.Scale > 2)
Evan Cheng77d86ff2006-02-25 10:09:08 +0000680 Complexity += 2;
Evan Cheng990c3602006-02-28 21:13:57 +0000681 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg
682 else if (AM.Scale > 1)
683 Complexity++;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000684
685 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
686 // to a LEA. This is determined with some expermentation but is by no means
687 // optimal (especially for code size consideration). LEA is nice because of
688 // its three-address nature. Tweak the cost function again when we can run
689 // convertToThreeAddress() at register allocation time.
690 if (AM.GV || AM.CP)
691 Complexity += 2;
692
693 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
694 Complexity++;
695
696 if (Complexity > 2) {
697 getAddressOperands(AM, Base, Scale, Index, Disp);
698 return true;
699 }
Evan Cheng77d86ff2006-02-25 10:09:08 +0000700 return false;
701}
702
Evan Chengd5f2ba02006-02-06 06:02:33 +0000703bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
704 SDOperand &Base, SDOperand &Scale,
705 SDOperand &Index, SDOperand &Disp) {
706 if (N.getOpcode() == ISD::LOAD &&
707 N.hasOneUse() &&
Evan Cheng29ab7c42006-08-16 23:59:00 +0000708 P.Val->isOnlyUse(N.Val) &&
709 CanBeFoldedBy(N.Val, P.Val))
Evan Cheng10d27902006-01-06 20:36:21 +0000710 return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp);
711 return false;
712}
713
714static bool isRegister0(SDOperand Op) {
Evan Chengc9fab312005-12-08 02:01:35 +0000715 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
716 return (R->getReg() == 0);
717 return false;
718}
719
Evan Cheng5588de92006-02-18 00:15:05 +0000720/// getGlobalBaseReg - Output the instructions required to put the
721/// base address to use for accessing globals into a register.
722///
Evan Cheng61413a32006-08-26 05:34:46 +0000723SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng5588de92006-02-18 00:15:05 +0000724 if (!GlobalBaseReg) {
725 // Insert the set of GlobalBaseReg into the first MBB of the function
726 MachineBasicBlock &FirstMBB = BB->getParent()->front();
727 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
728 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
729 // FIXME: when we get to LP64, we will need to create the appropriate
730 // type of register here.
Evan Cheng9fee4422006-05-16 07:21:53 +0000731 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng5588de92006-02-18 00:15:05 +0000732 BuildMI(FirstMBB, MBBI, X86::MovePCtoStack, 0);
733 BuildMI(FirstMBB, MBBI, X86::POP32r, 1, GlobalBaseReg);
734 }
Evan Cheng61413a32006-08-26 05:34:46 +0000735 return CurDAG->getRegister(GlobalBaseReg, MVT::i32).Val;
Evan Cheng5588de92006-02-18 00:15:05 +0000736}
737
Evan Chengf838cfc2006-05-20 01:36:52 +0000738static SDNode *FindCallStartFromCall(SDNode *Node) {
739 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
740 assert(Node->getOperand(0).getValueType() == MVT::Other &&
741 "Node doesn't have a token chain argument!");
742 return FindCallStartFromCall(Node->getOperand(0).Val);
743}
744
Evan Cheng61413a32006-08-26 05:34:46 +0000745SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Cheng00fcb002005-12-15 01:02:48 +0000746 SDNode *Node = N.Val;
747 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng10d27902006-01-06 20:36:21 +0000748 unsigned Opc, MOpc;
749 unsigned Opcode = Node->getOpcode();
Chris Lattner655e7df2005-11-16 01:54:32 +0000750
Evan Chengd49cc362006-02-10 22:24:32 +0000751#ifndef NDEBUG
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000752 DEBUG(std::cerr << std::string(Indent, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000753 DEBUG(std::cerr << "Selecting: ");
754 DEBUG(Node->dump(CurDAG));
755 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000756 Indent += 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000757#endif
758
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000759 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengd49cc362006-02-10 22:24:32 +0000760#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000761 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000762 DEBUG(std::cerr << "== ");
763 DEBUG(Node->dump(CurDAG));
764 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000765 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000766#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +0000767 return NULL; // Already selected.
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000768 }
Evan Cheng2ae799a2006-01-11 22:15:18 +0000769
Evan Cheng10d27902006-01-06 20:36:21 +0000770 switch (Opcode) {
Chris Lattner655e7df2005-11-16 01:54:32 +0000771 default: break;
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000772 case X86ISD::GlobalBaseReg:
Evan Cheng61413a32006-08-26 05:34:46 +0000773 return getGlobalBaseReg();
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000774
Evan Cheng77d86ff2006-02-25 10:09:08 +0000775 case ISD::ADD: {
776 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
777 // code and is matched first so to prevent it from being turned into
778 // LEA32r X+c.
779 SDOperand N0 = N.getOperand(0);
780 SDOperand N1 = N.getOperand(1);
781 if (N.Val->getValueType(0) == MVT::i32 &&
782 N0.getOpcode() == X86ISD::Wrapper &&
783 N1.getOpcode() == ISD::Constant) {
784 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
785 SDOperand C(0, 0);
786 // TODO: handle ExternalSymbolSDNode.
787 if (GlobalAddressSDNode *G =
788 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
789 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), MVT::i32,
790 G->getOffset() + Offset);
791 } else if (ConstantPoolSDNode *CP =
792 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
793 C = CurDAG->getTargetConstantPool(CP->get(), MVT::i32,
794 CP->getAlignment(),
795 CP->getOffset()+Offset);
796 }
797
Evan Cheng2d487222006-08-26 01:05:16 +0000798 if (C.Val)
Evan Cheng34b70ee2006-08-26 08:00:10 +0000799 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, MVT::i32, C);
Evan Cheng77d86ff2006-02-25 10:09:08 +0000800 }
801
802 // Other cases are handled by auto-generated code.
803 break;
Evan Cheng1f342c22006-02-23 02:43:52 +0000804 }
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000805
Evan Cheng10d27902006-01-06 20:36:21 +0000806 case ISD::MULHU:
807 case ISD::MULHS: {
808 if (Opcode == ISD::MULHU)
809 switch (NVT) {
810 default: assert(0 && "Unsupported VT!");
811 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
812 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
813 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
814 }
815 else
816 switch (NVT) {
817 default: assert(0 && "Unsupported VT!");
818 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
819 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
820 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
821 }
822
823 unsigned LoReg, HiReg;
824 switch (NVT) {
825 default: assert(0 && "Unsupported VT!");
826 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
827 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
828 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
829 }
830
831 SDOperand N0 = Node->getOperand(0);
832 SDOperand N1 = Node->getOperand(1);
833
834 bool foldedLoad = false;
835 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Chengd5f2ba02006-02-06 06:02:33 +0000836 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng92e27972006-01-06 23:19:29 +0000837 // MULHU and MULHS are commmutative
838 if (!foldedLoad) {
Evan Chengd5f2ba02006-02-06 06:02:33 +0000839 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng92e27972006-01-06 23:19:29 +0000840 if (foldedLoad) {
841 N0 = Node->getOperand(1);
842 N1 = Node->getOperand(0);
843 }
844 }
845
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000846 SDOperand Chain;
Evan Cheng2d487222006-08-26 01:05:16 +0000847 if (foldedLoad) {
848 Chain = N1.getOperand(0);
849 AddToISelQueue(Chain);
850 } else
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000851 Chain = CurDAG->getEntryNode();
Evan Cheng10d27902006-01-06 20:36:21 +0000852
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000853 SDOperand InFlag(0, 0);
Evan Cheng2d487222006-08-26 01:05:16 +0000854 AddToISelQueue(N0);
Evan Cheng10d27902006-01-06 20:36:21 +0000855 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000856 N0, InFlag);
Evan Cheng10d27902006-01-06 20:36:21 +0000857 InFlag = Chain.getValue(1);
858
859 if (foldedLoad) {
Evan Cheng2d487222006-08-26 01:05:16 +0000860 AddToISelQueue(Tmp0);
861 AddToISelQueue(Tmp1);
862 AddToISelQueue(Tmp2);
863 AddToISelQueue(Tmp3);
Evan Chengc3acfc02006-08-27 08:14:06 +0000864 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
Evan Chengd1b82d82006-02-09 07:17:49 +0000865 SDNode *CNode =
Evan Chengc3acfc02006-08-27 08:14:06 +0000866 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Chengd1b82d82006-02-09 07:17:49 +0000867 Chain = SDOperand(CNode, 0);
868 InFlag = SDOperand(CNode, 1);
Evan Cheng10d27902006-01-06 20:36:21 +0000869 } else {
Evan Cheng2d487222006-08-26 01:05:16 +0000870 AddToISelQueue(N1);
Evan Chengd1b82d82006-02-09 07:17:49 +0000871 InFlag =
872 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng10d27902006-01-06 20:36:21 +0000873 }
874
Evan Cheng61413a32006-08-26 05:34:46 +0000875 SDOperand Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
Evan Chengb9d34bd2006-08-07 22:28:20 +0000876 ReplaceUses(N.getValue(0), Result);
877 if (foldedLoad)
878 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000879
Evan Chengd49cc362006-02-10 22:24:32 +0000880#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000881 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengb9d34bd2006-08-07 22:28:20 +0000882 DEBUG(std::cerr << "=> ");
Evan Chengd49cc362006-02-10 22:24:32 +0000883 DEBUG(Result.Val->dump(CurDAG));
884 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000885 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000886#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +0000887 return NULL;
Evan Cheng92e27972006-01-06 23:19:29 +0000888 }
Evan Cheng5588de92006-02-18 00:15:05 +0000889
Evan Cheng92e27972006-01-06 23:19:29 +0000890 case ISD::SDIV:
891 case ISD::UDIV:
892 case ISD::SREM:
893 case ISD::UREM: {
894 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
895 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
896 if (!isSigned)
897 switch (NVT) {
898 default: assert(0 && "Unsupported VT!");
899 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
900 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
901 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
902 }
903 else
904 switch (NVT) {
905 default: assert(0 && "Unsupported VT!");
906 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
907 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
908 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
909 }
910
911 unsigned LoReg, HiReg;
912 unsigned ClrOpcode, SExtOpcode;
913 switch (NVT) {
914 default: assert(0 && "Unsupported VT!");
915 case MVT::i8:
916 LoReg = X86::AL; HiReg = X86::AH;
Evan Chenga2efb9f2006-06-02 21:20:34 +0000917 ClrOpcode = X86::MOV8r0;
Evan Cheng92e27972006-01-06 23:19:29 +0000918 SExtOpcode = X86::CBW;
919 break;
920 case MVT::i16:
921 LoReg = X86::AX; HiReg = X86::DX;
Evan Chenga2efb9f2006-06-02 21:20:34 +0000922 ClrOpcode = X86::MOV16r0;
Evan Cheng92e27972006-01-06 23:19:29 +0000923 SExtOpcode = X86::CWD;
924 break;
925 case MVT::i32:
926 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chenga2efb9f2006-06-02 21:20:34 +0000927 ClrOpcode = X86::MOV32r0;
Evan Cheng92e27972006-01-06 23:19:29 +0000928 SExtOpcode = X86::CDQ;
929 break;
930 }
931
932 SDOperand N0 = Node->getOperand(0);
933 SDOperand N1 = Node->getOperand(1);
934
935 bool foldedLoad = false;
936 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Chengd5f2ba02006-02-06 06:02:33 +0000937 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000938 SDOperand Chain;
Evan Cheng2d487222006-08-26 01:05:16 +0000939 if (foldedLoad) {
940 Chain = N1.getOperand(0);
941 AddToISelQueue(Chain);
942 } else
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000943 Chain = CurDAG->getEntryNode();
Evan Cheng92e27972006-01-06 23:19:29 +0000944
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000945 SDOperand InFlag(0, 0);
Evan Cheng2d487222006-08-26 01:05:16 +0000946 AddToISelQueue(N0);
Evan Cheng92e27972006-01-06 23:19:29 +0000947 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000948 N0, InFlag);
Evan Cheng92e27972006-01-06 23:19:29 +0000949 InFlag = Chain.getValue(1);
950
951 if (isSigned) {
952 // Sign extend the low part into the high part.
Evan Chengd1b82d82006-02-09 07:17:49 +0000953 InFlag =
954 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000955 } else {
956 // Zero out the high part, effectively zero extending the input.
Evan Chenga2efb9f2006-06-02 21:20:34 +0000957 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000958 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT),
959 ClrNode, InFlag);
960 InFlag = Chain.getValue(1);
961 }
962
963 if (foldedLoad) {
Evan Cheng2d487222006-08-26 01:05:16 +0000964 AddToISelQueue(Tmp0);
965 AddToISelQueue(Tmp1);
966 AddToISelQueue(Tmp2);
967 AddToISelQueue(Tmp3);
Evan Chengc3acfc02006-08-27 08:14:06 +0000968 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
Evan Chengd1b82d82006-02-09 07:17:49 +0000969 SDNode *CNode =
Evan Chengc3acfc02006-08-27 08:14:06 +0000970 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Chengd1b82d82006-02-09 07:17:49 +0000971 Chain = SDOperand(CNode, 0);
972 InFlag = SDOperand(CNode, 1);
Evan Cheng92e27972006-01-06 23:19:29 +0000973 } else {
Evan Cheng2d487222006-08-26 01:05:16 +0000974 AddToISelQueue(N1);
Evan Chengd1b82d82006-02-09 07:17:49 +0000975 InFlag =
976 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000977 }
978
Evan Cheng61413a32006-08-26 05:34:46 +0000979 SDOperand Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg,
980 NVT, InFlag);
Evan Chengb9d34bd2006-08-07 22:28:20 +0000981 ReplaceUses(N.getValue(0), Result);
982 if (foldedLoad)
983 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Chengd49cc362006-02-10 22:24:32 +0000984
985#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000986 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengb9d34bd2006-08-07 22:28:20 +0000987 DEBUG(std::cerr << "=> ");
Evan Chengd49cc362006-02-10 22:24:32 +0000988 DEBUG(Result.Val->dump(CurDAG));
989 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000990 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000991#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +0000992
993 return NULL;
Evan Cheng10d27902006-01-06 20:36:21 +0000994 }
Evan Cheng9733bde2006-05-08 08:01:26 +0000995
996 case ISD::TRUNCATE: {
997 if (NVT == MVT::i8) {
998 unsigned Opc2;
999 MVT::ValueType VT;
1000 switch (Node->getOperand(0).getValueType()) {
1001 default: assert(0 && "Unknown truncate!");
1002 case MVT::i16:
1003 Opc = X86::MOV16to16_;
1004 VT = MVT::i16;
Evan Cheng9fee4422006-05-16 07:21:53 +00001005 Opc2 = X86::TRUNC_GR16_GR8;
Evan Cheng9733bde2006-05-08 08:01:26 +00001006 break;
1007 case MVT::i32:
1008 Opc = X86::MOV32to32_;
1009 VT = MVT::i32;
Evan Cheng9fee4422006-05-16 07:21:53 +00001010 Opc2 = X86::TRUNC_GR32_GR8;
Evan Cheng9733bde2006-05-08 08:01:26 +00001011 break;
1012 }
1013
Evan Cheng2d487222006-08-26 01:05:16 +00001014 AddToISelQueue(Node->getOperand(0));
1015 SDOperand Tmp =
1016 SDOperand(CurDAG->getTargetNode(Opc, VT, Node->getOperand(0)), 0);
Evan Cheng61413a32006-08-26 05:34:46 +00001017 SDNode *ResNode = CurDAG->getTargetNode(Opc2, NVT, Tmp);
Evan Cheng9733bde2006-05-08 08:01:26 +00001018
1019#ifndef NDEBUG
1020 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengb9d34bd2006-08-07 22:28:20 +00001021 DEBUG(std::cerr << "=> ");
Evan Cheng61413a32006-08-26 05:34:46 +00001022 DEBUG(ResNode->dump(CurDAG));
Evan Cheng9733bde2006-05-08 08:01:26 +00001023 DEBUG(std::cerr << "\n");
1024 Indent -= 2;
1025#endif
Evan Cheng61413a32006-08-26 05:34:46 +00001026 return ResNode;
Evan Cheng9733bde2006-05-08 08:01:26 +00001027 }
Evan Chenga26c4512006-05-20 07:44:28 +00001028
1029 break;
Evan Cheng9733bde2006-05-08 08:01:26 +00001030 }
Chris Lattner655e7df2005-11-16 01:54:32 +00001031 }
1032
Evan Cheng61413a32006-08-26 05:34:46 +00001033 SDNode *ResNode = SelectCode(N);
Evan Chengbd1c5a82006-08-11 09:08:15 +00001034
Evan Chengd49cc362006-02-10 22:24:32 +00001035#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +00001036 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +00001037 DEBUG(std::cerr << "=> ");
Evan Cheng61413a32006-08-26 05:34:46 +00001038 if (ResNode == NULL || ResNode == N.Val)
1039 DEBUG(N.Val->dump(CurDAG));
1040 else
1041 DEBUG(ResNode->dump(CurDAG));
Evan Chengd49cc362006-02-10 22:24:32 +00001042 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +00001043 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +00001044#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +00001045
1046 return ResNode;
Chris Lattner655e7df2005-11-16 01:54:32 +00001047}
1048
Chris Lattnerba1ed582006-06-08 18:03:49 +00001049bool X86DAGToDAGISel::
1050SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1051 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1052 SDOperand Op0, Op1, Op2, Op3;
1053 switch (ConstraintCode) {
1054 case 'o': // offsetable ??
1055 case 'v': // not offsetable ??
1056 default: return true;
1057 case 'm': // memory
1058 if (!SelectAddr(Op, Op0, Op1, Op2, Op3))
1059 return true;
1060 break;
1061 }
1062
Evan Cheng2d487222006-08-26 01:05:16 +00001063 OutOps.push_back(Op0);
1064 OutOps.push_back(Op1);
1065 OutOps.push_back(Op2);
1066 OutOps.push_back(Op3);
1067 AddToISelQueue(Op0);
1068 AddToISelQueue(Op1);
1069 AddToISelQueue(Op2);
1070 AddToISelQueue(Op3);
Chris Lattnerba1ed582006-06-08 18:03:49 +00001071 return false;
1072}
1073
Chris Lattner655e7df2005-11-16 01:54:32 +00001074/// createX86ISelDag - This pass converts a legalized DAG into a
1075/// X86-specific DAG, ready for instruction scheduling.
1076///
Evan Cheng358b9ed2006-08-29 18:28:33 +00001077FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1078 return new X86DAGToDAGISel(TM, Fast);
Chris Lattner655e7df2005-11-16 01:54:32 +00001079}