blob: 6ec294c3f3a4f2f0b82c55cb0ec38750ec32014a [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 Lattner0cc59072006-06-28 23:27:49 +000034#include "llvm/Support/Visibility.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
79 //===--------------------------------------------------------------------===//
80 /// ISel - X86 specific code to select X86 machine instructions for
81 /// SelectionDAG operations.
82 ///
Chris Lattner0cc59072006-06-28 23:27:49 +000083 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattner655e7df2005-11-16 01:54:32 +000084 /// ContainsFPCode - Every instruction we select that uses or defines a FP
85 /// register should set this to true.
86 bool ContainsFPCode;
87
88 /// X86Lowering - This object fully describes how to lower LLVM code to an
89 /// X86-specific SelectionDAG.
90 X86TargetLowering X86Lowering;
91
92 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
93 /// make the right decision when generating code for different targets.
94 const X86Subtarget *Subtarget;
Evan Cheng5588de92006-02-18 00:15:05 +000095
96 unsigned GlobalBaseReg;
Evan Cheng691a63d2006-07-27 16:44:36 +000097
Chris Lattner655e7df2005-11-16 01:54:32 +000098 public:
Evan Cheng2dd2c652006-03-13 23:20:37 +000099 X86DAGToDAGISel(X86TargetMachine &TM)
100 : SelectionDAGISel(X86Lowering),
Evan Cheng691a63d2006-07-27 16:44:36 +0000101 X86Lowering(*TM.getTargetLowering()),
Evan Cheng72bb66a2006-08-08 00:31:00 +0000102 Subtarget(&TM.getSubtarget<X86Subtarget>()) {}
Chris Lattner655e7df2005-11-16 01:54:32 +0000103
Evan Cheng5588de92006-02-18 00:15:05 +0000104 virtual bool runOnFunction(Function &Fn) {
105 // Make sure we re-emit a set of the global base reg if necessary
106 GlobalBaseReg = 0;
107 return SelectionDAGISel::runOnFunction(Fn);
108 }
109
Chris Lattner655e7df2005-11-16 01:54:32 +0000110 virtual const char *getPassName() const {
111 return "X86 DAG->DAG Instruction Selection";
112 }
113
114 /// InstructionSelectBasicBlock - This callback is invoked by
115 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
116 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
117
Evan Chengbc7a0f442006-01-11 06:09:51 +0000118 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
119
Evan Chenge2a3f702006-07-28 01:03:48 +0000120 virtual bool CanBeFoldedBy(SDNode *N, SDNode *U);
Evan Cheng691a63d2006-07-27 16:44:36 +0000121
Chris Lattner655e7df2005-11-16 01:54:32 +0000122// Include the pieces autogenerated from the target description.
123#include "X86GenDAGISel.inc"
124
125 private:
Evan Cheng61413a32006-08-26 05:34:46 +0000126 SDNode *Select(SDOperand N);
Chris Lattner655e7df2005-11-16 01:54:32 +0000127
Evan Chenga86ba852006-02-11 02:05:36 +0000128 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM, bool isRoot = true);
Evan Chengc9fab312005-12-08 02:01:35 +0000129 bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
130 SDOperand &Index, SDOperand &Disp);
131 bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
132 SDOperand &Index, SDOperand &Disp);
Evan Chengd5f2ba02006-02-06 06:02:33 +0000133 bool TryFoldLoad(SDOperand P, SDOperand N,
134 SDOperand &Base, SDOperand &Scale,
Evan Cheng10d27902006-01-06 20:36:21 +0000135 SDOperand &Index, SDOperand &Disp);
Evan Chengb9d34bd2006-08-07 22:28:20 +0000136
Chris Lattnerba1ed582006-06-08 18:03:49 +0000137 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
138 /// inline asm expressions.
139 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
140 char ConstraintCode,
141 std::vector<SDOperand> &OutOps,
142 SelectionDAG &DAG);
143
Evan Chenge8a42362006-06-02 22:38:37 +0000144 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
145
Evan Cheng67ed58e2005-12-12 21:49:40 +0000146 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
147 SDOperand &Scale, SDOperand &Index,
148 SDOperand &Disp) {
149 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
150 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, MVT::i32) : AM.Base.Reg;
Evan Cheng1d712482005-12-17 09:13:43 +0000151 Scale = getI8Imm(AM.Scale);
Evan Cheng67ed58e2005-12-12 21:49:40 +0000152 Index = AM.IndexReg;
153 Disp = AM.GV ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp)
Evan Cheng77d86ff2006-02-25 10:09:08 +0000154 : (AM.CP ?
155 CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp)
156 : getI32Imm(AM.Disp));
Evan Cheng67ed58e2005-12-12 21:49:40 +0000157 }
158
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000159 /// getI8Imm - Return a target constant with the specified value, of type
160 /// i8.
161 inline SDOperand getI8Imm(unsigned Imm) {
162 return CurDAG->getTargetConstant(Imm, MVT::i8);
163 }
164
Chris Lattner655e7df2005-11-16 01:54:32 +0000165 /// getI16Imm - Return a target constant with the specified value, of type
166 /// i16.
167 inline SDOperand getI16Imm(unsigned Imm) {
168 return CurDAG->getTargetConstant(Imm, MVT::i16);
169 }
170
171 /// getI32Imm - Return a target constant with the specified value, of type
172 /// i32.
173 inline SDOperand getI32Imm(unsigned Imm) {
174 return CurDAG->getTargetConstant(Imm, MVT::i32);
175 }
Evan Chengd49cc362006-02-10 22:24:32 +0000176
Evan Cheng5588de92006-02-18 00:15:05 +0000177 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
178 /// base register. Return the virtual register that holds this value.
Evan Cheng61413a32006-08-26 05:34:46 +0000179 SDNode *getGlobalBaseReg();
Evan Cheng5588de92006-02-18 00:15:05 +0000180
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000181#ifndef NDEBUG
182 unsigned Indent;
183#endif
Chris Lattner655e7df2005-11-16 01:54:32 +0000184 };
185}
186
Evan Cheng72bb66a2006-08-08 00:31:00 +0000187static void findNonImmUse(SDNode* Use, SDNode* Def, bool &found,
188 std::set<SDNode *> &Visited) {
189 if (found ||
190 Use->getNodeId() > Def->getNodeId() ||
191 !Visited.insert(Use).second)
192 return;
193
194 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
195 SDNode *N = Use->getOperand(i).Val;
196 if (N != Def) {
197 findNonImmUse(N, Def, found, Visited);
198 } else {
199 found = true;
200 break;
201 }
202 }
203}
204
205static inline bool isNonImmUse(SDNode* Use, SDNode* Def) {
206 std::set<SDNode *> Visited;
207 bool found = false;
208 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
209 SDNode *N = Use->getOperand(i).Val;
210 if (N != Def) {
211 findNonImmUse(N, Def, found, Visited);
212 if (found) break;
213 }
214 }
215 return found;
216}
217
218
Evan Chenge2a3f702006-07-28 01:03:48 +0000219bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U) {
Evan Cheng691a63d2006-07-27 16:44:36 +0000220 // If U use can somehow reach N through another path then U can't fold N or
221 // it will create a cycle. e.g. In the following diagram, U can reach N
Evan Chenge8071ec2006-07-28 06:33:41 +0000222 // through X. If N is folded into into U, then X is both a predecessor and
Evan Cheng691a63d2006-07-27 16:44:36 +0000223 // a successor of U.
224 //
225 // [ N ]
226 // ^ ^
227 // | |
228 // / \---
229 // / [X]
230 // | ^
231 // [U]--------|
Evan Cheng72bb66a2006-08-08 00:31:00 +0000232 return !isNonImmUse(U, N);
Evan Cheng691a63d2006-07-27 16:44:36 +0000233}
234
Chris Lattner655e7df2005-11-16 01:54:32 +0000235/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
236/// when it has created a SelectionDAG for us to codegen.
237void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
238 DEBUG(BB->dump());
Chris Lattner7c551262006-01-11 01:15:34 +0000239 MachineFunction::iterator FirstMBB = BB;
Chris Lattner655e7df2005-11-16 01:54:32 +0000240
241 // Codegen the basic block.
Evan Chengd49cc362006-02-10 22:24:32 +0000242#ifndef NDEBUG
243 DEBUG(std::cerr << "===== Instruction selection begins:\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000244 Indent = 0;
Evan Chengd49cc362006-02-10 22:24:32 +0000245#endif
Evan Cheng54cb1832006-02-05 06:46:41 +0000246 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Chengd49cc362006-02-10 22:24:32 +0000247#ifndef NDEBUG
248 DEBUG(std::cerr << "===== Instruction selection ends:\n");
249#endif
Evan Cheng3b5e0ca2006-07-28 00:10:59 +0000250
Chris Lattner655e7df2005-11-16 01:54:32 +0000251 DAG.RemoveDeadNodes();
252
253 // Emit machine code to BB.
254 ScheduleAndEmitDAG(DAG);
Chris Lattner7c551262006-01-11 01:15:34 +0000255
256 // If we are emitting FP stack code, scan the basic block to determine if this
257 // block defines any FP values. If so, put an FP_REG_KILL instruction before
258 // the terminator of the block.
Evan Chengcde9e302006-01-27 08:10:46 +0000259 if (!Subtarget->hasSSE2()) {
Chris Lattner7c551262006-01-11 01:15:34 +0000260 // Note that FP stack instructions *are* used in SSE code when returning
261 // values, but these are not live out of the basic block, so we don't need
262 // an FP_REG_KILL in this case either.
263 bool ContainsFPCode = false;
264
265 // Scan all of the machine instructions in these MBBs, checking for FP
266 // stores.
267 MachineFunction::iterator MBBI = FirstMBB;
268 do {
269 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
270 !ContainsFPCode && I != E; ++I) {
271 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
272 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
273 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
274 RegMap->getRegClass(I->getOperand(0).getReg()) ==
275 X86::RFPRegisterClass) {
276 ContainsFPCode = true;
277 break;
278 }
279 }
280 }
281 } while (!ContainsFPCode && &*(MBBI++) != BB);
282
283 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
284 // a copy of the input value in this block.
285 if (!ContainsFPCode) {
286 // Final check, check LLVM BB's that are successors to the LLVM BB
287 // corresponding to BB for FP PHI nodes.
288 const BasicBlock *LLVMBB = BB->getBasicBlock();
289 const PHINode *PN;
290 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
291 !ContainsFPCode && SI != E; ++SI) {
292 for (BasicBlock::const_iterator II = SI->begin();
293 (PN = dyn_cast<PHINode>(II)); ++II) {
294 if (PN->getType()->isFloatingPoint()) {
295 ContainsFPCode = true;
296 break;
297 }
298 }
299 }
300 }
301
302 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
303 if (ContainsFPCode) {
304 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
305 ++NumFPKill;
306 }
307 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000308}
309
Evan Chengbc7a0f442006-01-11 06:09:51 +0000310/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
311/// the main function.
Evan Chenge8a42362006-06-02 22:38:37 +0000312void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
313 MachineFrameInfo *MFI) {
314 if (Subtarget->TargetType == X86Subtarget::isCygwin)
315 BuildMI(BB, X86::CALLpcrel32, 1).addExternalSymbol("__main");
316
Evan Chengbc7a0f442006-01-11 06:09:51 +0000317 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
318 int CWFrameIdx = MFI->CreateStackObject(2, 2);
319 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
320
321 // Set the high part to be 64-bit precision.
322 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
323 CWFrameIdx, 1).addImm(2);
324
325 // Reload the modified control word now.
326 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
327}
328
329void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
330 // If this is main, emit special code for main.
331 MachineBasicBlock *BB = MF.begin();
332 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
333 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
334}
335
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000336/// MatchAddress - Add the specified node to the specified addressing mode,
337/// returning true if it cannot be done. This just pattern matches for the
338/// addressing mode
Evan Chenga86ba852006-02-11 02:05:36 +0000339bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
340 bool isRoot) {
Evan Chengb9d34bd2006-08-07 22:28:20 +0000341 int id = N.Val->getNodeId();
342 bool Available = isSelected(id);
Evan Chenga86ba852006-02-11 02:05:36 +0000343
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000344 switch (N.getOpcode()) {
345 default: break;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000346 case ISD::Constant:
347 AM.Disp += cast<ConstantSDNode>(N)->getValue();
348 return false;
349
350 case X86ISD::Wrapper:
351 // If both base and index components have been picked, we can't fit
352 // the result available in the register in the addressing mode. Duplicate
353 // GlobalAddress or ConstantPool as displacement.
354 if (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
355 if (ConstantPoolSDNode *CP =
356 dyn_cast<ConstantPoolSDNode>(N.getOperand(0))) {
357 if (AM.CP == 0) {
358 AM.CP = CP->get();
359 AM.Align = CP->getAlignment();
360 AM.Disp += CP->getOffset();
361 return false;
362 }
363 } else if (GlobalAddressSDNode *G =
364 dyn_cast<GlobalAddressSDNode>(N.getOperand(0))) {
365 if (AM.GV == 0) {
366 AM.GV = G->getGlobal();
367 AM.Disp += G->getOffset();
368 return false;
369 }
370 }
371 }
372 break;
373
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000374 case ISD::FrameIndex:
375 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
376 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
377 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
378 return false;
379 }
380 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000381
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000382 case ISD::SHL:
Evan Cheng77d86ff2006-02-25 10:09:08 +0000383 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000384 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
385 unsigned Val = CN->getValue();
386 if (Val == 1 || Val == 2 || Val == 3) {
387 AM.Scale = 1 << Val;
388 SDOperand ShVal = N.Val->getOperand(0);
389
390 // Okay, we know that we have a scale by now. However, if the scaled
391 // value is an add of something and a constant, we can fold the
392 // constant into the disp field here.
393 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
394 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
395 AM.IndexReg = ShVal.Val->getOperand(0);
396 ConstantSDNode *AddVal =
397 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
398 AM.Disp += AddVal->getValue() << Val;
399 } else {
400 AM.IndexReg = ShVal;
401 }
402 return false;
403 }
404 }
405 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000406
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000407 case ISD::MUL:
408 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng77d86ff2006-02-25 10:09:08 +0000409 if (!Available &&
410 AM.BaseType == X86ISelAddressMode::RegBase &&
411 AM.Base.Reg.Val == 0 &&
412 AM.IndexReg.Val == 0)
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000413 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
414 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
415 AM.Scale = unsigned(CN->getValue())-1;
416
417 SDOperand MulVal = N.Val->getOperand(0);
418 SDOperand Reg;
419
420 // Okay, we know that we have a scale by now. However, if the scaled
421 // value is an add of something and a constant, we can fold the
422 // constant into the disp field here.
423 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
424 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
425 Reg = MulVal.Val->getOperand(0);
426 ConstantSDNode *AddVal =
427 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
428 AM.Disp += AddVal->getValue() * CN->getValue();
429 } else {
430 Reg = N.Val->getOperand(0);
431 }
432
433 AM.IndexReg = AM.Base.Reg = Reg;
434 return false;
435 }
436 break;
437
438 case ISD::ADD: {
Evan Cheng77d86ff2006-02-25 10:09:08 +0000439 if (!Available) {
Evan Chenga86ba852006-02-11 02:05:36 +0000440 X86ISelAddressMode Backup = AM;
441 if (!MatchAddress(N.Val->getOperand(0), AM, false) &&
442 !MatchAddress(N.Val->getOperand(1), AM, false))
443 return false;
444 AM = Backup;
445 if (!MatchAddress(N.Val->getOperand(1), AM, false) &&
446 !MatchAddress(N.Val->getOperand(0), AM, false))
447 return false;
448 AM = Backup;
449 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000450 break;
451 }
Evan Cheng734e1e22006-05-30 06:59:36 +0000452
453 case ISD::OR: {
454 if (!Available) {
455 X86ISelAddressMode Backup = AM;
456 // Look for (x << c1) | c2 where (c2 < c1)
457 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(0));
458 if (CN && !MatchAddress(N.Val->getOperand(1), AM, false)) {
459 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
460 AM.Disp = CN->getValue();
461 return false;
462 }
463 }
464 AM = Backup;
465 CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1));
466 if (CN && !MatchAddress(N.Val->getOperand(0), AM, false)) {
467 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
468 AM.Disp = CN->getValue();
469 return false;
470 }
471 }
472 AM = Backup;
473 }
474 break;
475 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000476 }
477
478 // Is the base register already occupied?
479 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
480 // If so, check to see if the scale index register is set.
481 if (AM.IndexReg.Val == 0) {
482 AM.IndexReg = N;
483 AM.Scale = 1;
484 return false;
485 }
486
487 // Otherwise, we cannot select it.
488 return true;
489 }
490
491 // Default, generate it as a register.
492 AM.BaseType = X86ISelAddressMode::RegBase;
493 AM.Base.Reg = N;
494 return false;
495}
496
Evan Chengc9fab312005-12-08 02:01:35 +0000497/// SelectAddr - returns true if it is able pattern match an addressing mode.
498/// It returns the operands which make up the maximal addressing mode it can
499/// match by reference.
500bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
501 SDOperand &Index, SDOperand &Disp) {
502 X86ISelAddressMode AM;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000503 if (MatchAddress(N, AM))
504 return false;
Evan Chengc9fab312005-12-08 02:01:35 +0000505
Evan Chengbc7a0f442006-01-11 06:09:51 +0000506 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Chengd19d51f2006-02-05 05:25:07 +0000507 if (!AM.Base.Reg.Val)
Evan Chengbc7a0f442006-01-11 06:09:51 +0000508 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
Evan Chengc9fab312005-12-08 02:01:35 +0000509 }
Evan Chengbc7a0f442006-01-11 06:09:51 +0000510
Evan Chengd19d51f2006-02-05 05:25:07 +0000511 if (!AM.IndexReg.Val)
Evan Chengbc7a0f442006-01-11 06:09:51 +0000512 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
513
514 getAddressOperands(AM, Base, Scale, Index, Disp);
515 return true;
Evan Chengc9fab312005-12-08 02:01:35 +0000516}
517
Evan Cheng77d86ff2006-02-25 10:09:08 +0000518/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
519/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng77d86ff2006-02-25 10:09:08 +0000520bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base,
521 SDOperand &Scale,
522 SDOperand &Index, SDOperand &Disp) {
523 X86ISelAddressMode AM;
524 if (MatchAddress(N, AM))
525 return false;
526
527 unsigned Complexity = 0;
528 if (AM.BaseType == X86ISelAddressMode::RegBase)
529 if (AM.Base.Reg.Val)
530 Complexity = 1;
531 else
532 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
533 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
534 Complexity = 4;
535
536 if (AM.IndexReg.Val)
537 Complexity++;
538 else
539 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
540
Evan Cheng990c3602006-02-28 21:13:57 +0000541 if (AM.Scale > 2)
Evan Cheng77d86ff2006-02-25 10:09:08 +0000542 Complexity += 2;
Evan Cheng990c3602006-02-28 21:13:57 +0000543 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg
544 else if (AM.Scale > 1)
545 Complexity++;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000546
547 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
548 // to a LEA. This is determined with some expermentation but is by no means
549 // optimal (especially for code size consideration). LEA is nice because of
550 // its three-address nature. Tweak the cost function again when we can run
551 // convertToThreeAddress() at register allocation time.
552 if (AM.GV || AM.CP)
553 Complexity += 2;
554
555 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
556 Complexity++;
557
558 if (Complexity > 2) {
559 getAddressOperands(AM, Base, Scale, Index, Disp);
560 return true;
561 }
Evan Cheng77d86ff2006-02-25 10:09:08 +0000562 return false;
563}
564
Evan Chengd5f2ba02006-02-06 06:02:33 +0000565bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
566 SDOperand &Base, SDOperand &Scale,
567 SDOperand &Index, SDOperand &Disp) {
568 if (N.getOpcode() == ISD::LOAD &&
569 N.hasOneUse() &&
Evan Cheng29ab7c42006-08-16 23:59:00 +0000570 P.Val->isOnlyUse(N.Val) &&
571 CanBeFoldedBy(N.Val, P.Val))
Evan Cheng10d27902006-01-06 20:36:21 +0000572 return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp);
573 return false;
574}
575
576static bool isRegister0(SDOperand Op) {
Evan Chengc9fab312005-12-08 02:01:35 +0000577 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
578 return (R->getReg() == 0);
579 return false;
580}
581
Evan Cheng5588de92006-02-18 00:15:05 +0000582/// getGlobalBaseReg - Output the instructions required to put the
583/// base address to use for accessing globals into a register.
584///
Evan Cheng61413a32006-08-26 05:34:46 +0000585SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Evan Cheng5588de92006-02-18 00:15:05 +0000586 if (!GlobalBaseReg) {
587 // Insert the set of GlobalBaseReg into the first MBB of the function
588 MachineBasicBlock &FirstMBB = BB->getParent()->front();
589 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
590 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
591 // FIXME: when we get to LP64, we will need to create the appropriate
592 // type of register here.
Evan Cheng9fee4422006-05-16 07:21:53 +0000593 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng5588de92006-02-18 00:15:05 +0000594 BuildMI(FirstMBB, MBBI, X86::MovePCtoStack, 0);
595 BuildMI(FirstMBB, MBBI, X86::POP32r, 1, GlobalBaseReg);
596 }
Evan Cheng61413a32006-08-26 05:34:46 +0000597 return CurDAG->getRegister(GlobalBaseReg, MVT::i32).Val;
Evan Cheng5588de92006-02-18 00:15:05 +0000598}
599
Evan Chengf838cfc2006-05-20 01:36:52 +0000600static SDNode *FindCallStartFromCall(SDNode *Node) {
601 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
602 assert(Node->getOperand(0).getValueType() == MVT::Other &&
603 "Node doesn't have a token chain argument!");
604 return FindCallStartFromCall(Node->getOperand(0).Val);
605}
606
Evan Cheng61413a32006-08-26 05:34:46 +0000607SDNode *X86DAGToDAGISel::Select(SDOperand N) {
Evan Cheng00fcb002005-12-15 01:02:48 +0000608 SDNode *Node = N.Val;
609 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng10d27902006-01-06 20:36:21 +0000610 unsigned Opc, MOpc;
611 unsigned Opcode = Node->getOpcode();
Chris Lattner655e7df2005-11-16 01:54:32 +0000612
Evan Chengd49cc362006-02-10 22:24:32 +0000613#ifndef NDEBUG
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000614 DEBUG(std::cerr << std::string(Indent, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000615 DEBUG(std::cerr << "Selecting: ");
616 DEBUG(Node->dump(CurDAG));
617 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000618 Indent += 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000619#endif
620
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000621 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
Evan Chengd49cc362006-02-10 22:24:32 +0000622#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000623 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000624 DEBUG(std::cerr << "== ");
625 DEBUG(Node->dump(CurDAG));
626 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000627 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000628#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +0000629 return NULL; // Already selected.
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000630 }
Evan Cheng2ae799a2006-01-11 22:15:18 +0000631
Evan Cheng10d27902006-01-06 20:36:21 +0000632 switch (Opcode) {
Chris Lattner655e7df2005-11-16 01:54:32 +0000633 default: break;
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000634 case X86ISD::GlobalBaseReg:
Evan Cheng61413a32006-08-26 05:34:46 +0000635 return getGlobalBaseReg();
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000636
Evan Cheng77d86ff2006-02-25 10:09:08 +0000637 case ISD::ADD: {
638 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
639 // code and is matched first so to prevent it from being turned into
640 // LEA32r X+c.
641 SDOperand N0 = N.getOperand(0);
642 SDOperand N1 = N.getOperand(1);
643 if (N.Val->getValueType(0) == MVT::i32 &&
644 N0.getOpcode() == X86ISD::Wrapper &&
645 N1.getOpcode() == ISD::Constant) {
646 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
647 SDOperand C(0, 0);
648 // TODO: handle ExternalSymbolSDNode.
649 if (GlobalAddressSDNode *G =
650 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
651 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), MVT::i32,
652 G->getOffset() + Offset);
653 } else if (ConstantPoolSDNode *CP =
654 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
655 C = CurDAG->getTargetConstantPool(CP->get(), MVT::i32,
656 CP->getAlignment(),
657 CP->getOffset()+Offset);
658 }
659
Evan Cheng2d487222006-08-26 01:05:16 +0000660 if (C.Val)
661 return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, MVT::i32, C).Val;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000662 }
663
664 // Other cases are handled by auto-generated code.
665 break;
Evan Cheng1f342c22006-02-23 02:43:52 +0000666 }
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000667
Evan Cheng10d27902006-01-06 20:36:21 +0000668 case ISD::MULHU:
669 case ISD::MULHS: {
670 if (Opcode == ISD::MULHU)
671 switch (NVT) {
672 default: assert(0 && "Unsupported VT!");
673 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
674 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
675 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
676 }
677 else
678 switch (NVT) {
679 default: assert(0 && "Unsupported VT!");
680 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
681 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
682 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
683 }
684
685 unsigned LoReg, HiReg;
686 switch (NVT) {
687 default: assert(0 && "Unsupported VT!");
688 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
689 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
690 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
691 }
692
693 SDOperand N0 = Node->getOperand(0);
694 SDOperand N1 = Node->getOperand(1);
695
696 bool foldedLoad = false;
697 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Chengd5f2ba02006-02-06 06:02:33 +0000698 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng92e27972006-01-06 23:19:29 +0000699 // MULHU and MULHS are commmutative
700 if (!foldedLoad) {
Evan Chengd5f2ba02006-02-06 06:02:33 +0000701 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng92e27972006-01-06 23:19:29 +0000702 if (foldedLoad) {
703 N0 = Node->getOperand(1);
704 N1 = Node->getOperand(0);
705 }
706 }
707
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000708 SDOperand Chain;
Evan Cheng2d487222006-08-26 01:05:16 +0000709 if (foldedLoad) {
710 Chain = N1.getOperand(0);
711 AddToISelQueue(Chain);
712 } else
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000713 Chain = CurDAG->getEntryNode();
Evan Cheng10d27902006-01-06 20:36:21 +0000714
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000715 SDOperand InFlag(0, 0);
Evan Cheng2d487222006-08-26 01:05:16 +0000716 AddToISelQueue(N0);
Evan Cheng10d27902006-01-06 20:36:21 +0000717 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000718 N0, InFlag);
Evan Cheng10d27902006-01-06 20:36:21 +0000719 InFlag = Chain.getValue(1);
720
721 if (foldedLoad) {
Evan Cheng2d487222006-08-26 01:05:16 +0000722 AddToISelQueue(Tmp0);
723 AddToISelQueue(Tmp1);
724 AddToISelQueue(Tmp2);
725 AddToISelQueue(Tmp3);
Evan Chengd1b82d82006-02-09 07:17:49 +0000726 SDNode *CNode =
727 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
728 Tmp2, Tmp3, Chain, InFlag);
729 Chain = SDOperand(CNode, 0);
730 InFlag = SDOperand(CNode, 1);
Evan Cheng10d27902006-01-06 20:36:21 +0000731 } else {
Evan Cheng2d487222006-08-26 01:05:16 +0000732 AddToISelQueue(N1);
Evan Chengd1b82d82006-02-09 07:17:49 +0000733 InFlag =
734 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng10d27902006-01-06 20:36:21 +0000735 }
736
Evan Cheng61413a32006-08-26 05:34:46 +0000737 SDOperand Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
Evan Chengb9d34bd2006-08-07 22:28:20 +0000738 ReplaceUses(N.getValue(0), Result);
739 if (foldedLoad)
740 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000741
Evan Chengd49cc362006-02-10 22:24:32 +0000742#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000743 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengb9d34bd2006-08-07 22:28:20 +0000744 DEBUG(std::cerr << "=> ");
Evan Chengd49cc362006-02-10 22:24:32 +0000745 DEBUG(Result.Val->dump(CurDAG));
746 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000747 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000748#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +0000749 return NULL;
Evan Cheng92e27972006-01-06 23:19:29 +0000750 }
Evan Cheng5588de92006-02-18 00:15:05 +0000751
Evan Cheng92e27972006-01-06 23:19:29 +0000752 case ISD::SDIV:
753 case ISD::UDIV:
754 case ISD::SREM:
755 case ISD::UREM: {
756 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
757 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
758 if (!isSigned)
759 switch (NVT) {
760 default: assert(0 && "Unsupported VT!");
761 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
762 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
763 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
764 }
765 else
766 switch (NVT) {
767 default: assert(0 && "Unsupported VT!");
768 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
769 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
770 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
771 }
772
773 unsigned LoReg, HiReg;
774 unsigned ClrOpcode, SExtOpcode;
775 switch (NVT) {
776 default: assert(0 && "Unsupported VT!");
777 case MVT::i8:
778 LoReg = X86::AL; HiReg = X86::AH;
Evan Chenga2efb9f2006-06-02 21:20:34 +0000779 ClrOpcode = X86::MOV8r0;
Evan Cheng92e27972006-01-06 23:19:29 +0000780 SExtOpcode = X86::CBW;
781 break;
782 case MVT::i16:
783 LoReg = X86::AX; HiReg = X86::DX;
Evan Chenga2efb9f2006-06-02 21:20:34 +0000784 ClrOpcode = X86::MOV16r0;
Evan Cheng92e27972006-01-06 23:19:29 +0000785 SExtOpcode = X86::CWD;
786 break;
787 case MVT::i32:
788 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chenga2efb9f2006-06-02 21:20:34 +0000789 ClrOpcode = X86::MOV32r0;
Evan Cheng92e27972006-01-06 23:19:29 +0000790 SExtOpcode = X86::CDQ;
791 break;
792 }
793
794 SDOperand N0 = Node->getOperand(0);
795 SDOperand N1 = Node->getOperand(1);
796
797 bool foldedLoad = false;
798 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Chengd5f2ba02006-02-06 06:02:33 +0000799 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000800 SDOperand Chain;
Evan Cheng2d487222006-08-26 01:05:16 +0000801 if (foldedLoad) {
802 Chain = N1.getOperand(0);
803 AddToISelQueue(Chain);
804 } else
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000805 Chain = CurDAG->getEntryNode();
Evan Cheng92e27972006-01-06 23:19:29 +0000806
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000807 SDOperand InFlag(0, 0);
Evan Cheng2d487222006-08-26 01:05:16 +0000808 AddToISelQueue(N0);
Evan Cheng92e27972006-01-06 23:19:29 +0000809 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000810 N0, InFlag);
Evan Cheng92e27972006-01-06 23:19:29 +0000811 InFlag = Chain.getValue(1);
812
813 if (isSigned) {
814 // Sign extend the low part into the high part.
Evan Chengd1b82d82006-02-09 07:17:49 +0000815 InFlag =
816 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000817 } else {
818 // Zero out the high part, effectively zero extending the input.
Evan Chenga2efb9f2006-06-02 21:20:34 +0000819 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000820 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT),
821 ClrNode, InFlag);
822 InFlag = Chain.getValue(1);
823 }
824
825 if (foldedLoad) {
Evan Cheng2d487222006-08-26 01:05:16 +0000826 AddToISelQueue(Tmp0);
827 AddToISelQueue(Tmp1);
828 AddToISelQueue(Tmp2);
829 AddToISelQueue(Tmp3);
Evan Chengd1b82d82006-02-09 07:17:49 +0000830 SDNode *CNode =
831 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
832 Tmp2, Tmp3, Chain, InFlag);
833 Chain = SDOperand(CNode, 0);
834 InFlag = SDOperand(CNode, 1);
Evan Cheng92e27972006-01-06 23:19:29 +0000835 } else {
Evan Cheng2d487222006-08-26 01:05:16 +0000836 AddToISelQueue(N1);
Evan Chengd1b82d82006-02-09 07:17:49 +0000837 InFlag =
838 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000839 }
840
Evan Cheng61413a32006-08-26 05:34:46 +0000841 SDOperand Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg,
842 NVT, InFlag);
Evan Chengb9d34bd2006-08-07 22:28:20 +0000843 ReplaceUses(N.getValue(0), Result);
844 if (foldedLoad)
845 ReplaceUses(N1.getValue(1), Result.getValue(1));
Evan Chengd49cc362006-02-10 22:24:32 +0000846
847#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000848 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengb9d34bd2006-08-07 22:28:20 +0000849 DEBUG(std::cerr << "=> ");
Evan Chengd49cc362006-02-10 22:24:32 +0000850 DEBUG(Result.Val->dump(CurDAG));
851 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000852 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000853#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +0000854
855 return NULL;
Evan Cheng10d27902006-01-06 20:36:21 +0000856 }
Evan Cheng9733bde2006-05-08 08:01:26 +0000857
858 case ISD::TRUNCATE: {
859 if (NVT == MVT::i8) {
860 unsigned Opc2;
861 MVT::ValueType VT;
862 switch (Node->getOperand(0).getValueType()) {
863 default: assert(0 && "Unknown truncate!");
864 case MVT::i16:
865 Opc = X86::MOV16to16_;
866 VT = MVT::i16;
Evan Cheng9fee4422006-05-16 07:21:53 +0000867 Opc2 = X86::TRUNC_GR16_GR8;
Evan Cheng9733bde2006-05-08 08:01:26 +0000868 break;
869 case MVT::i32:
870 Opc = X86::MOV32to32_;
871 VT = MVT::i32;
Evan Cheng9fee4422006-05-16 07:21:53 +0000872 Opc2 = X86::TRUNC_GR32_GR8;
Evan Cheng9733bde2006-05-08 08:01:26 +0000873 break;
874 }
875
Evan Cheng2d487222006-08-26 01:05:16 +0000876 AddToISelQueue(Node->getOperand(0));
877 SDOperand Tmp =
878 SDOperand(CurDAG->getTargetNode(Opc, VT, Node->getOperand(0)), 0);
Evan Cheng61413a32006-08-26 05:34:46 +0000879 SDNode *ResNode = CurDAG->getTargetNode(Opc2, NVT, Tmp);
Evan Cheng9733bde2006-05-08 08:01:26 +0000880
881#ifndef NDEBUG
882 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengb9d34bd2006-08-07 22:28:20 +0000883 DEBUG(std::cerr << "=> ");
Evan Cheng61413a32006-08-26 05:34:46 +0000884 DEBUG(ResNode->dump(CurDAG));
Evan Cheng9733bde2006-05-08 08:01:26 +0000885 DEBUG(std::cerr << "\n");
886 Indent -= 2;
887#endif
Evan Cheng61413a32006-08-26 05:34:46 +0000888 return ResNode;
Evan Cheng9733bde2006-05-08 08:01:26 +0000889 }
Evan Chenga26c4512006-05-20 07:44:28 +0000890
891 break;
Evan Cheng9733bde2006-05-08 08:01:26 +0000892 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000893 }
894
Evan Cheng61413a32006-08-26 05:34:46 +0000895 SDNode *ResNode = SelectCode(N);
Evan Chengbd1c5a82006-08-11 09:08:15 +0000896
Evan Chengd49cc362006-02-10 22:24:32 +0000897#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000898 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000899 DEBUG(std::cerr << "=> ");
Evan Cheng61413a32006-08-26 05:34:46 +0000900 if (ResNode == NULL || ResNode == N.Val)
901 DEBUG(N.Val->dump(CurDAG));
902 else
903 DEBUG(ResNode->dump(CurDAG));
Evan Chengd49cc362006-02-10 22:24:32 +0000904 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000905 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000906#endif
Evan Chengbd1c5a82006-08-11 09:08:15 +0000907
908 return ResNode;
Chris Lattner655e7df2005-11-16 01:54:32 +0000909}
910
Chris Lattnerba1ed582006-06-08 18:03:49 +0000911bool X86DAGToDAGISel::
912SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
913 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
914 SDOperand Op0, Op1, Op2, Op3;
915 switch (ConstraintCode) {
916 case 'o': // offsetable ??
917 case 'v': // not offsetable ??
918 default: return true;
919 case 'm': // memory
920 if (!SelectAddr(Op, Op0, Op1, Op2, Op3))
921 return true;
922 break;
923 }
924
Evan Cheng2d487222006-08-26 01:05:16 +0000925 OutOps.push_back(Op0);
926 OutOps.push_back(Op1);
927 OutOps.push_back(Op2);
928 OutOps.push_back(Op3);
929 AddToISelQueue(Op0);
930 AddToISelQueue(Op1);
931 AddToISelQueue(Op2);
932 AddToISelQueue(Op3);
Chris Lattnerba1ed582006-06-08 18:03:49 +0000933 return false;
934}
935
Chris Lattner655e7df2005-11-16 01:54:32 +0000936/// createX86ISelDag - This pass converts a legalized DAG into a
937/// X86-specific DAG, ready for instruction scheduling.
938///
Evan Cheng2dd2c652006-03-13 23:20:37 +0000939FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM) {
Chris Lattner655e7df2005-11-16 01:54:32 +0000940 return new X86DAGToDAGISel(TM);
941}