blob: 6605473f17a49ed4113a31bf6c5f8c02f447ce88 [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);
Evan Chengdfb85152006-08-29 18:37:37 +0000315 if (MVT::isFloatingPoint(N1.getValueType()) &&
316 MVT::isVector(N1.getValueType()) &&
317 !N1.hasOneUse())
Evan Cheng64a9e282006-08-28 20:10:17 +0000318 continue;
319
320 bool RModW = false;
321 SDOperand Load;
322 unsigned Opcode = N1.Val->getOpcode();
323 switch (Opcode) {
324 case ISD::ADD:
325 case ISD::MUL:
Evan Cheng64a9e282006-08-28 20:10:17 +0000326 case ISD::AND:
327 case ISD::OR:
328 case ISD::XOR:
329 case ISD::ADDC:
330 case ISD::ADDE: {
331 SDOperand N10 = N1.getOperand(0);
332 SDOperand N11 = N1.getOperand(1);
333 if (N10.Val->getOpcode() == ISD::LOAD)
334 RModW = true;
335 else if (N11.Val->getOpcode() == ISD::LOAD) {
336 RModW = true;
337 std::swap(N10, N11);
338 }
339 RModW = RModW && N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Chengc07feb142006-08-29 06:44:17 +0000340 (N10.getOperand(1) == N2) &&
341 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng64a9e282006-08-28 20:10:17 +0000342 if (RModW)
343 Load = N10;
344 break;
345 }
346 case ISD::SUB:
347 case ISD::SHL:
348 case ISD::SRA:
349 case ISD::SRL:
350 case ISD::ROTL:
351 case ISD::ROTR:
352 case ISD::SUBC:
353 case ISD::SUBE:
354 case X86ISD::SHLD:
355 case X86ISD::SHRD: {
356 SDOperand N10 = N1.getOperand(0);
357 if (N10.Val->getOpcode() == ISD::LOAD)
358 RModW = N10.Val->isOperand(Chain.Val) && N10.hasOneUse() &&
Evan Chengc07feb142006-08-29 06:44:17 +0000359 (N10.getOperand(1) == N2) &&
360 (N10.Val->getValueType(0) == N1.getValueType());
Evan Cheng64a9e282006-08-28 20:10:17 +0000361 if (RModW)
362 Load = N10;
363 break;
364 }
365 }
366
Evan Chengc07feb142006-08-29 06:44:17 +0000367 if (RModW) {
Evan Cheng64a9e282006-08-28 20:10:17 +0000368 MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain);
Evan Chengc07feb142006-08-29 06:44:17 +0000369 ++NumLoadMoved;
370 }
Evan Cheng64a9e282006-08-28 20:10:17 +0000371 }
372}
373
Chris Lattner655e7df2005-11-16 01:54:32 +0000374/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
375/// when it has created a SelectionDAG for us to codegen.
376void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
377 DEBUG(BB->dump());
Chris Lattner7c551262006-01-11 01:15:34 +0000378 MachineFunction::iterator FirstMBB = BB;
Chris Lattner655e7df2005-11-16 01:54:32 +0000379
Evan Cheng358b9ed2006-08-29 18:28:33 +0000380 if (!FastISel)
Evan Cheng64a9e282006-08-28 20:10:17 +0000381 InstructionSelectPreprocess(DAG);
382
Chris Lattner655e7df2005-11-16 01:54:32 +0000383 // Codegen the basic block.
Evan Chengd49cc362006-02-10 22:24:32 +0000384#ifndef NDEBUG
385 DEBUG(std::cerr << "===== Instruction selection begins:\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000386 Indent = 0;
Evan Chengd49cc362006-02-10 22:24:32 +0000387#endif
Evan Cheng54cb1832006-02-05 06:46:41 +0000388 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengd49cc362006-02-10 22:24:32 +0000389#ifndef NDEBUG
390 DEBUG(std::cerr << "===== Instruction selection ends:\n");
391#endif
Evan Cheng3b5e0ca2006-07-28 00:10:59 +0000392
Chris Lattner655e7df2005-11-16 01:54:32 +0000393 DAG.RemoveDeadNodes();
394
395 // Emit machine code to BB.
396 ScheduleAndEmitDAG(DAG);
Chris Lattner7c551262006-01-11 01:15:34 +0000397
398 // If we are emitting FP stack code, scan the basic block to determine if this
399 // block defines any FP values. If so, put an FP_REG_KILL instruction before
400 // the terminator of the block.
Evan Chengcde9e302006-01-27 08:10:46 +0000401 if (!Subtarget->hasSSE2()) {
Chris Lattner7c551262006-01-11 01:15:34 +0000402 // Note that FP stack instructions *are* used in SSE code when returning
403 // values, but these are not live out of the basic block, so we don't need
404 // an FP_REG_KILL in this case either.
405 bool ContainsFPCode = false;
406
407 // Scan all of the machine instructions in these MBBs, checking for FP
408 // stores.
409 MachineFunction::iterator MBBI = FirstMBB;
410 do {
411 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
412 !ContainsFPCode && I != E; ++I) {
413 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
414 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
415 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
416 RegMap->getRegClass(I->getOperand(0).getReg()) ==
417 X86::RFPRegisterClass) {
418 ContainsFPCode = true;
419 break;
420 }
421 }
422 }
423 } while (!ContainsFPCode && &*(MBBI++) != BB);
424
425 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
426 // a copy of the input value in this block.
427 if (!ContainsFPCode) {
428 // Final check, check LLVM BB's that are successors to the LLVM BB
429 // corresponding to BB for FP PHI nodes.
430 const BasicBlock *LLVMBB = BB->getBasicBlock();
431 const PHINode *PN;
432 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
433 !ContainsFPCode && SI != E; ++SI) {
434 for (BasicBlock::const_iterator II = SI->begin();
435 (PN = dyn_cast<PHINode>(II)); ++II) {
436 if (PN->getType()->isFloatingPoint()) {
437 ContainsFPCode = true;
438 break;
439 }
440 }
441 }
442 }
443
444 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
445 if (ContainsFPCode) {
446 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
447 ++NumFPKill;
448 }
449 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000450}
451
Evan Chengbc7a0f442006-01-11 06:09:51 +0000452/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
453/// the main function.
Evan Chenge8a42362006-06-02 22:38:37 +0000454void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
455 MachineFrameInfo *MFI) {
456 if (Subtarget->TargetType == X86Subtarget::isCygwin)
457 BuildMI(BB, X86::CALLpcrel32, 1).addExternalSymbol("__main");
458
Evan Chengbc7a0f442006-01-11 06:09:51 +0000459 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
460 int CWFrameIdx = MFI->CreateStackObject(2, 2);
461 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
462
463 // Set the high part to be 64-bit precision.
464 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
465 CWFrameIdx, 1).addImm(2);
466
467 // Reload the modified control word now.
468 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
469}
470
471void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
472 // If this is main, emit special code for main.
473 MachineBasicBlock *BB = MF.begin();
474 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
475 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
476}
477
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000478/// MatchAddress - Add the specified node to the specified addressing mode,
479/// returning true if it cannot be done. This just pattern matches for the
480/// addressing mode
Evan Chenga86ba852006-02-11 02:05:36 +0000481bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
482 bool isRoot) {
Evan Chengb9d34bd2006-08-07 22:28:20 +0000483 int id = N.Val->getNodeId();
484 bool Available = isSelected(id);
Evan Chenga86ba852006-02-11 02:05:36 +0000485
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000486 switch (N.getOpcode()) {
487 default: break;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000488 case ISD::Constant:
489 AM.Disp += cast<ConstantSDNode>(N)->getValue();
490 return false;
491
492 case X86ISD::Wrapper:
493 // If both base and index components have been picked, we can't fit
494 // the result available in the register in the addressing mode. Duplicate
495 // GlobalAddress or ConstantPool as displacement.
496 if (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
497 if (ConstantPoolSDNode *CP =
498 dyn_cast<ConstantPoolSDNode>(N.getOperand(0))) {
499 if (AM.CP == 0) {
500 AM.CP = CP->get();
501 AM.Align = CP->getAlignment();
502 AM.Disp += CP->getOffset();
503 return false;
504 }
505 } else if (GlobalAddressSDNode *G =
506 dyn_cast<GlobalAddressSDNode>(N.getOperand(0))) {
507 if (AM.GV == 0) {
508 AM.GV = G->getGlobal();
509 AM.Disp += G->getOffset();
510 return false;
511 }
512 }
513 }
514 break;
515
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000516 case ISD::FrameIndex:
517 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
518 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
519 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
520 return false;
521 }
522 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000523
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000524 case ISD::SHL:
Evan Cheng77d86ff2006-02-25 10:09:08 +0000525 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000526 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
527 unsigned Val = CN->getValue();
528 if (Val == 1 || Val == 2 || Val == 3) {
529 AM.Scale = 1 << Val;
530 SDOperand ShVal = N.Val->getOperand(0);
531
532 // Okay, we know that we have a scale by now. However, if the scaled
533 // value is an add of something and a constant, we can fold the
534 // constant into the disp field here.
535 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
536 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
537 AM.IndexReg = ShVal.Val->getOperand(0);
538 ConstantSDNode *AddVal =
539 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
540 AM.Disp += AddVal->getValue() << Val;
541 } else {
542 AM.IndexReg = ShVal;
543 }
544 return false;
545 }
546 }
547 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000548
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000549 case ISD::MUL:
550 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng77d86ff2006-02-25 10:09:08 +0000551 if (!Available &&
552 AM.BaseType == X86ISelAddressMode::RegBase &&
553 AM.Base.Reg.Val == 0 &&
554 AM.IndexReg.Val == 0)
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000555 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
556 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
557 AM.Scale = unsigned(CN->getValue())-1;
558
559 SDOperand MulVal = N.Val->getOperand(0);
560 SDOperand Reg;
561
562 // Okay, we know that we have a scale by now. However, if the scaled
563 // value is an add of something and a constant, we can fold the
564 // constant into the disp field here.
565 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
566 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
567 Reg = MulVal.Val->getOperand(0);
568 ConstantSDNode *AddVal =
569 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
570 AM.Disp += AddVal->getValue() * CN->getValue();
571 } else {
572 Reg = N.Val->getOperand(0);
573 }
574
575 AM.IndexReg = AM.Base.Reg = Reg;
576 return false;
577 }
578 break;
579
580 case ISD::ADD: {
Evan Cheng77d86ff2006-02-25 10:09:08 +0000581 if (!Available) {
Evan Chenga86ba852006-02-11 02:05:36 +0000582 X86ISelAddressMode Backup = AM;
583 if (!MatchAddress(N.Val->getOperand(0), AM, false) &&
584 !MatchAddress(N.Val->getOperand(1), AM, false))
585 return false;
586 AM = Backup;
587 if (!MatchAddress(N.Val->getOperand(1), AM, false) &&
588 !MatchAddress(N.Val->getOperand(0), AM, false))
589 return false;
590 AM = Backup;
591 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000592 break;
593 }
Evan Cheng734e1e22006-05-30 06:59:36 +0000594
595 case ISD::OR: {
596 if (!Available) {
597 X86ISelAddressMode Backup = AM;
598 // Look for (x << c1) | c2 where (c2 < c1)
599 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(0));
600 if (CN && !MatchAddress(N.Val->getOperand(1), AM, false)) {
601 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
602 AM.Disp = CN->getValue();
603 return false;
604 }
605 }
606 AM = Backup;
607 CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1));
608 if (CN && !MatchAddress(N.Val->getOperand(0), AM, false)) {
609 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
610 AM.Disp = CN->getValue();
611 return false;
612 }
613 }
614 AM = Backup;
615 }
616 break;
617 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000618 }
619
620 // Is the base register already occupied?
621 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
622 // If so, check to see if the scale index register is set.
623 if (AM.IndexReg.Val == 0) {
624 AM.IndexReg = N;
625 AM.Scale = 1;
626 return false;
627 }
628
629 // Otherwise, we cannot select it.
630 return true;
631 }
632
633 // Default, generate it as a register.
634 AM.BaseType = X86ISelAddressMode::RegBase;
635 AM.Base.Reg = N;
636 return false;
637}
638
Evan Chengc9fab312005-12-08 02:01:35 +0000639/// SelectAddr - returns true if it is able pattern match an addressing mode.
640/// It returns the operands which make up the maximal addressing mode it can
641/// match by reference.
642bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
643 SDOperand &Index, SDOperand &Disp) {
644 X86ISelAddressMode AM;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000645 if (MatchAddress(N, AM))
646 return false;
Evan Chengc9fab312005-12-08 02:01:35 +0000647
Evan Chengbc7a0f442006-01-11 06:09:51 +0000648 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Chengd19d51f2006-02-05 05:25:07 +0000649 if (!AM.Base.Reg.Val)
Evan Chengbc7a0f442006-01-11 06:09:51 +0000650 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
Evan Chengc9fab312005-12-08 02:01:35 +0000651 }
Evan Chengbc7a0f442006-01-11 06:09:51 +0000652
Evan Chengd19d51f2006-02-05 05:25:07 +0000653 if (!AM.IndexReg.Val)
Evan Chengbc7a0f442006-01-11 06:09:51 +0000654 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
655
656 getAddressOperands(AM, Base, Scale, Index, Disp);
657 return true;
Evan Chengc9fab312005-12-08 02:01:35 +0000658}
659
Evan Cheng77d86ff2006-02-25 10:09:08 +0000660/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
661/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng77d86ff2006-02-25 10:09:08 +0000662bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base,
663 SDOperand &Scale,
664 SDOperand &Index, SDOperand &Disp) {
665 X86ISelAddressMode AM;
666 if (MatchAddress(N, AM))
667 return false;
668
669 unsigned Complexity = 0;
670 if (AM.BaseType == X86ISelAddressMode::RegBase)
671 if (AM.Base.Reg.Val)
672 Complexity = 1;
673 else
674 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
675 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
676 Complexity = 4;
677
678 if (AM.IndexReg.Val)
679 Complexity++;
680 else
681 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
682
Evan Cheng990c3602006-02-28 21:13:57 +0000683 if (AM.Scale > 2)
Evan Cheng77d86ff2006-02-25 10:09:08 +0000684 Complexity += 2;
Evan Cheng990c3602006-02-28 21:13:57 +0000685 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg
686 else if (AM.Scale > 1)
687 Complexity++;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000688
689 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
690 // to a LEA. This is determined with some expermentation but is by no means
691 // optimal (especially for code size consideration). LEA is nice because of
692 // its three-address nature. Tweak the cost function again when we can run
693 // convertToThreeAddress() at register allocation time.
694 if (AM.GV || AM.CP)
695 Complexity += 2;
696
697 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
698 Complexity++;
699
700 if (Complexity > 2) {
701 getAddressOperands(AM, Base, Scale, Index, Disp);
702 return true;
703 }
Evan Cheng77d86ff2006-02-25 10:09:08 +0000704 return false;
705}
706
Evan Chengd5f2ba02006-02-06 06:02:33 +0000707bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
708 SDOperand &Base, SDOperand &Scale,
709 SDOperand &Index, SDOperand &Disp) {
710 if (N.getOpcode() == ISD::LOAD &&
711 N.hasOneUse() &&
Evan Cheng29ab7c42006-08-16 23:59:00 +0000712 P.Val->isOnlyUse(N.Val) &&
713 CanBeFoldedBy(N.Val, P.Val))
Evan Cheng10d27902006-01-06 20:36:21 +0000714 return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp);
715 return false;
716}
717
718static bool isRegister0(SDOperand Op) {
Evan Chengc9fab312005-12-08 02:01:35 +0000719 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
720 return (R->getReg() == 0);
721 return false;
722}
723
Evan Cheng5588de92006-02-18 00:15:05 +0000724/// getGlobalBaseReg - Output the instructions required to put the
725/// base address to use for accessing globals into a register.
726///
Evan Cheng61413a32006-08-26 05:34:46 +0000727SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng5588de92006-02-18 00:15:05 +0000728 if (!GlobalBaseReg) {
729 // Insert the set of GlobalBaseReg into the first MBB of the function
730 MachineBasicBlock &FirstMBB = BB->getParent()->front();
731 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
732 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
733 // FIXME: when we get to LP64, we will need to create the appropriate
734 // type of register here.
Evan Cheng9fee4422006-05-16 07:21:53 +0000735 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng5588de92006-02-18 00:15:05 +0000736 BuildMI(FirstMBB, MBBI, X86::MovePCtoStack, 0);
737 BuildMI(FirstMBB, MBBI, X86::POP32r, 1, GlobalBaseReg);
738 }
Evan Cheng61413a32006-08-26 05:34:46 +0000739 return CurDAG->getRegister(GlobalBaseReg, MVT::i32).Val;
Evan Cheng5588de92006-02-18 00:15:05 +0000740}
741
Evan Chengf838cfc2006-05-20 01:36:52 +0000742static SDNode *FindCallStartFromCall(SDNode *Node) {
743 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
744 assert(Node->getOperand(0).getValueType() == MVT::Other &&
745 "Node doesn't have a token chain argument!");
746 return FindCallStartFromCall(Node->getOperand(0).Val);
747}
748
Evan Cheng61413a32006-08-26 05:34:46 +0000749SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Cheng00fcb002005-12-15 01:02:48 +0000750 SDNode *Node = N.Val;
751 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng10d27902006-01-06 20:36:21 +0000752 unsigned Opc, MOpc;
753 unsigned Opcode = Node->getOpcode();
Chris Lattner655e7df2005-11-16 01:54:32 +0000754
Evan Chengd49cc362006-02-10 22:24:32 +0000755#ifndef NDEBUG
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000756 DEBUG(std::cerr << std::string(Indent, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000757 DEBUG(std::cerr << "Selecting: ");
758 DEBUG(Node->dump(CurDAG));
759 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000760 Indent += 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000761#endif
762
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000763 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengd49cc362006-02-10 22:24:32 +0000764#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000765 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000766 DEBUG(std::cerr << "== ");
767 DEBUG(Node->dump(CurDAG));
768 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000769 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000770#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +0000771 return NULL; // Already selected.
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000772 }
Evan Cheng2ae799a2006-01-11 22:15:18 +0000773
Evan Cheng10d27902006-01-06 20:36:21 +0000774 switch (Opcode) {
Chris Lattner655e7df2005-11-16 01:54:32 +0000775 default: break;
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000776 case X86ISD::GlobalBaseReg:
Evan Cheng61413a32006-08-26 05:34:46 +0000777 return getGlobalBaseReg();
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000778
Evan Cheng77d86ff2006-02-25 10:09:08 +0000779 case ISD::ADD: {
780 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
781 // code and is matched first so to prevent it from being turned into
782 // LEA32r X+c.
783 SDOperand N0 = N.getOperand(0);
784 SDOperand N1 = N.getOperand(1);
785 if (N.Val->getValueType(0) == MVT::i32 &&
786 N0.getOpcode() == X86ISD::Wrapper &&
787 N1.getOpcode() == ISD::Constant) {
788 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
789 SDOperand C(0, 0);
790 // TODO: handle ExternalSymbolSDNode.
791 if (GlobalAddressSDNode *G =
792 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
793 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), MVT::i32,
794 G->getOffset() + Offset);
795 } else if (ConstantPoolSDNode *CP =
796 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
797 C = CurDAG->getTargetConstantPool(CP->get(), MVT::i32,
798 CP->getAlignment(),
799 CP->getOffset()+Offset);
800 }
801
Evan Cheng2d487222006-08-26 01:05:16 +0000802 if (C.Val)
Evan Cheng34b70ee2006-08-26 08:00:10 +0000803 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, MVT::i32, C);
Evan Cheng77d86ff2006-02-25 10:09:08 +0000804 }
805
806 // Other cases are handled by auto-generated code.
807 break;
Evan Cheng1f342c22006-02-23 02:43:52 +0000808 }
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000809
Evan Cheng10d27902006-01-06 20:36:21 +0000810 case ISD::MULHU:
811 case ISD::MULHS: {
812 if (Opcode == ISD::MULHU)
813 switch (NVT) {
814 default: assert(0 && "Unsupported VT!");
815 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
816 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
817 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
818 }
819 else
820 switch (NVT) {
821 default: assert(0 && "Unsupported VT!");
822 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
823 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
824 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
825 }
826
827 unsigned LoReg, HiReg;
828 switch (NVT) {
829 default: assert(0 && "Unsupported VT!");
830 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
831 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
832 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
833 }
834
835 SDOperand N0 = Node->getOperand(0);
836 SDOperand N1 = Node->getOperand(1);
837
838 bool foldedLoad = false;
839 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Chengd5f2ba02006-02-06 06:02:33 +0000840 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng92e27972006-01-06 23:19:29 +0000841 // MULHU and MULHS are commmutative
842 if (!foldedLoad) {
Evan Chengd5f2ba02006-02-06 06:02:33 +0000843 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng92e27972006-01-06 23:19:29 +0000844 if (foldedLoad) {
845 N0 = Node->getOperand(1);
846 N1 = Node->getOperand(0);
847 }
848 }
849
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000850 SDOperand Chain;
Evan Cheng2d487222006-08-26 01:05:16 +0000851 if (foldedLoad) {
852 Chain = N1.getOperand(0);
853 AddToISelQueue(Chain);
854 } else
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000855 Chain = CurDAG->getEntryNode();
Evan Cheng10d27902006-01-06 20:36:21 +0000856
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000857 SDOperand InFlag(0, 0);
Evan Cheng2d487222006-08-26 01:05:16 +0000858 AddToISelQueue(N0);
Evan Cheng10d27902006-01-06 20:36:21 +0000859 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000860 N0, InFlag);
Evan Cheng10d27902006-01-06 20:36:21 +0000861 InFlag = Chain.getValue(1);
862
863 if (foldedLoad) {
Evan Cheng2d487222006-08-26 01:05:16 +0000864 AddToISelQueue(Tmp0);
865 AddToISelQueue(Tmp1);
866 AddToISelQueue(Tmp2);
867 AddToISelQueue(Tmp3);
Evan Chengc3acfc02006-08-27 08:14:06 +0000868 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
Evan Chengd1b82d82006-02-09 07:17:49 +0000869 SDNode *CNode =
Evan Chengc3acfc02006-08-27 08:14:06 +0000870 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Chengd1b82d82006-02-09 07:17:49 +0000871 Chain = SDOperand(CNode, 0);
872 InFlag = SDOperand(CNode, 1);
Evan Cheng10d27902006-01-06 20:36:21 +0000873 } else {
Evan Cheng2d487222006-08-26 01:05:16 +0000874 AddToISelQueue(N1);
Evan Chengd1b82d82006-02-09 07:17:49 +0000875 InFlag =
876 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng10d27902006-01-06 20:36:21 +0000877 }
878
Evan Cheng61413a32006-08-26 05:34:46 +0000879 SDOperand Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
Evan Chengb9d34bd2006-08-07 22:28:20 +0000880 ReplaceUses(N.getValue(0), Result);
881 if (foldedLoad)
882 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000883
Evan Chengd49cc362006-02-10 22:24:32 +0000884#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000885 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengb9d34bd2006-08-07 22:28:20 +0000886 DEBUG(std::cerr << "=> ");
Evan Chengd49cc362006-02-10 22:24:32 +0000887 DEBUG(Result.Val->dump(CurDAG));
888 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000889 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000890#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +0000891 return NULL;
Evan Cheng92e27972006-01-06 23:19:29 +0000892 }
Evan Cheng5588de92006-02-18 00:15:05 +0000893
Evan Cheng92e27972006-01-06 23:19:29 +0000894 case ISD::SDIV:
895 case ISD::UDIV:
896 case ISD::SREM:
897 case ISD::UREM: {
898 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
899 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
900 if (!isSigned)
901 switch (NVT) {
902 default: assert(0 && "Unsupported VT!");
903 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
904 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
905 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
906 }
907 else
908 switch (NVT) {
909 default: assert(0 && "Unsupported VT!");
910 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
911 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
912 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
913 }
914
915 unsigned LoReg, HiReg;
916 unsigned ClrOpcode, SExtOpcode;
917 switch (NVT) {
918 default: assert(0 && "Unsupported VT!");
919 case MVT::i8:
920 LoReg = X86::AL; HiReg = X86::AH;
Evan Chenga2efb9f2006-06-02 21:20:34 +0000921 ClrOpcode = X86::MOV8r0;
Evan Cheng92e27972006-01-06 23:19:29 +0000922 SExtOpcode = X86::CBW;
923 break;
924 case MVT::i16:
925 LoReg = X86::AX; HiReg = X86::DX;
Evan Chenga2efb9f2006-06-02 21:20:34 +0000926 ClrOpcode = X86::MOV16r0;
Evan Cheng92e27972006-01-06 23:19:29 +0000927 SExtOpcode = X86::CWD;
928 break;
929 case MVT::i32:
930 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chenga2efb9f2006-06-02 21:20:34 +0000931 ClrOpcode = X86::MOV32r0;
Evan Cheng92e27972006-01-06 23:19:29 +0000932 SExtOpcode = X86::CDQ;
933 break;
934 }
935
936 SDOperand N0 = Node->getOperand(0);
937 SDOperand N1 = Node->getOperand(1);
938
939 bool foldedLoad = false;
940 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Chengd5f2ba02006-02-06 06:02:33 +0000941 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000942 SDOperand Chain;
Evan Cheng2d487222006-08-26 01:05:16 +0000943 if (foldedLoad) {
944 Chain = N1.getOperand(0);
945 AddToISelQueue(Chain);
946 } else
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000947 Chain = CurDAG->getEntryNode();
Evan Cheng92e27972006-01-06 23:19:29 +0000948
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000949 SDOperand InFlag(0, 0);
Evan Cheng2d487222006-08-26 01:05:16 +0000950 AddToISelQueue(N0);
Evan Cheng92e27972006-01-06 23:19:29 +0000951 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000952 N0, InFlag);
Evan Cheng92e27972006-01-06 23:19:29 +0000953 InFlag = Chain.getValue(1);
954
955 if (isSigned) {
956 // Sign extend the low part into the high part.
Evan Chengd1b82d82006-02-09 07:17:49 +0000957 InFlag =
958 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000959 } else {
960 // Zero out the high part, effectively zero extending the input.
Evan Chenga2efb9f2006-06-02 21:20:34 +0000961 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000962 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT),
963 ClrNode, InFlag);
964 InFlag = Chain.getValue(1);
965 }
966
967 if (foldedLoad) {
Evan Cheng2d487222006-08-26 01:05:16 +0000968 AddToISelQueue(Tmp0);
969 AddToISelQueue(Tmp1);
970 AddToISelQueue(Tmp2);
971 AddToISelQueue(Tmp3);
Evan Chengc3acfc02006-08-27 08:14:06 +0000972 SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag };
Evan Chengd1b82d82006-02-09 07:17:49 +0000973 SDNode *CNode =
Evan Chengc3acfc02006-08-27 08:14:06 +0000974 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6);
Evan Chengd1b82d82006-02-09 07:17:49 +0000975 Chain = SDOperand(CNode, 0);
976 InFlag = SDOperand(CNode, 1);
Evan Cheng92e27972006-01-06 23:19:29 +0000977 } else {
Evan Cheng2d487222006-08-26 01:05:16 +0000978 AddToISelQueue(N1);
Evan Chengd1b82d82006-02-09 07:17:49 +0000979 InFlag =
980 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000981 }
982
Evan Cheng61413a32006-08-26 05:34:46 +0000983 SDOperand Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg,
984 NVT, InFlag);
Evan Chengb9d34bd2006-08-07 22:28:20 +0000985 ReplaceUses(N.getValue(0), Result);
986 if (foldedLoad)
987 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Chengd49cc362006-02-10 22:24:32 +0000988
989#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000990 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengb9d34bd2006-08-07 22:28:20 +0000991 DEBUG(std::cerr << "=> ");
Evan Chengd49cc362006-02-10 22:24:32 +0000992 DEBUG(Result.Val->dump(CurDAG));
993 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000994 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000995#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +0000996
997 return NULL;
Evan Cheng10d27902006-01-06 20:36:21 +0000998 }
Evan Cheng9733bde2006-05-08 08:01:26 +0000999
1000 case ISD::TRUNCATE: {
1001 if (NVT == MVT::i8) {
1002 unsigned Opc2;
1003 MVT::ValueType VT;
1004 switch (Node->getOperand(0).getValueType()) {
1005 default: assert(0 && "Unknown truncate!");
1006 case MVT::i16:
1007 Opc = X86::MOV16to16_;
1008 VT = MVT::i16;
Evan Cheng9fee4422006-05-16 07:21:53 +00001009 Opc2 = X86::TRUNC_GR16_GR8;
Evan Cheng9733bde2006-05-08 08:01:26 +00001010 break;
1011 case MVT::i32:
1012 Opc = X86::MOV32to32_;
1013 VT = MVT::i32;
Evan Cheng9fee4422006-05-16 07:21:53 +00001014 Opc2 = X86::TRUNC_GR32_GR8;
Evan Cheng9733bde2006-05-08 08:01:26 +00001015 break;
1016 }
1017
Evan Cheng2d487222006-08-26 01:05:16 +00001018 AddToISelQueue(Node->getOperand(0));
1019 SDOperand Tmp =
1020 SDOperand(CurDAG->getTargetNode(Opc, VT, Node->getOperand(0)), 0);
Evan Cheng61413a32006-08-26 05:34:46 +00001021 SDNode *ResNode = CurDAG->getTargetNode(Opc2, NVT, Tmp);
Evan Cheng9733bde2006-05-08 08:01:26 +00001022
1023#ifndef NDEBUG
1024 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengb9d34bd2006-08-07 22:28:20 +00001025 DEBUG(std::cerr << "=> ");
Evan Cheng61413a32006-08-26 05:34:46 +00001026 DEBUG(ResNode->dump(CurDAG));
Evan Cheng9733bde2006-05-08 08:01:26 +00001027 DEBUG(std::cerr << "\n");
1028 Indent -= 2;
1029#endif
Evan Cheng61413a32006-08-26 05:34:46 +00001030 return ResNode;
Evan Cheng9733bde2006-05-08 08:01:26 +00001031 }
Evan Chenga26c4512006-05-20 07:44:28 +00001032
1033 break;
Evan Cheng9733bde2006-05-08 08:01:26 +00001034 }
Chris Lattner655e7df2005-11-16 01:54:32 +00001035 }
1036
Evan Cheng61413a32006-08-26 05:34:46 +00001037 SDNode *ResNode = SelectCode(N);
Evan Chengbd1c5a82006-08-11 09:08:15 +00001038
Evan Chengd49cc362006-02-10 22:24:32 +00001039#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +00001040 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +00001041 DEBUG(std::cerr << "=> ");
Evan Cheng61413a32006-08-26 05:34:46 +00001042 if (ResNode == NULL || ResNode == N.Val)
1043 DEBUG(N.Val->dump(CurDAG));
1044 else
1045 DEBUG(ResNode->dump(CurDAG));
Evan Chengd49cc362006-02-10 22:24:32 +00001046 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +00001047 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +00001048#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +00001049
1050 return ResNode;
Chris Lattner655e7df2005-11-16 01:54:32 +00001051}
1052
Chris Lattnerba1ed582006-06-08 18:03:49 +00001053bool X86DAGToDAGISel::
1054SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
1055 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
1056 SDOperand Op0, Op1, Op2, Op3;
1057 switch (ConstraintCode) {
1058 case 'o': // offsetable ??
1059 case 'v': // not offsetable ??
1060 default: return true;
1061 case 'm': // memory
1062 if (!SelectAddr(Op, Op0, Op1, Op2, Op3))
1063 return true;
1064 break;
1065 }
1066
Evan Cheng2d487222006-08-26 01:05:16 +00001067 OutOps.push_back(Op0);
1068 OutOps.push_back(Op1);
1069 OutOps.push_back(Op2);
1070 OutOps.push_back(Op3);
1071 AddToISelQueue(Op0);
1072 AddToISelQueue(Op1);
1073 AddToISelQueue(Op2);
1074 AddToISelQueue(Op3);
Chris Lattnerba1ed582006-06-08 18:03:49 +00001075 return false;
1076}
1077
Chris Lattner655e7df2005-11-16 01:54:32 +00001078/// createX86ISelDag - This pass converts a legalized DAG into a
1079/// X86-specific DAG, ready for instruction scheduling.
1080///
Evan Cheng358b9ed2006-08-29 18:28:33 +00001081FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) {
1082 return new X86DAGToDAGISel(TM, Fast);
Chris Lattner655e7df2005-11-16 01:54:32 +00001083}