blob: a1512beb4d3ac86528de03bdebc1caeda4ba464c [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 Chengd49cc362006-02-10 22:24:32 +000015#define DEBUG_TYPE "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"
34#include "llvm/ADT/Statistic.h"
Chris Lattnerde02d772006-01-22 23:41:00 +000035#include <iostream>
Evan Cheng54cb1832006-02-05 06:46:41 +000036#include <set>
Chris Lattner655e7df2005-11-16 01:54:32 +000037using namespace llvm;
38
39//===----------------------------------------------------------------------===//
40// Pattern Matcher Implementation
41//===----------------------------------------------------------------------===//
42
43namespace {
Chris Lattner3f0f71b2005-11-19 02:11:08 +000044 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
45 /// SDOperand's instead of register numbers for the leaves of the matched
46 /// tree.
47 struct X86ISelAddressMode {
48 enum {
49 RegBase,
Chris Lattneraa2372562006-05-24 17:04:05 +000050 FrameIndexBase
Chris Lattner3f0f71b2005-11-19 02:11:08 +000051 } BaseType;
52
53 struct { // This is really a union, discriminated by BaseType!
54 SDOperand Reg;
55 int FrameIndex;
56 } Base;
57
58 unsigned Scale;
59 SDOperand IndexReg;
60 unsigned Disp;
61 GlobalValue *GV;
Evan Cheng77d86ff2006-02-25 10:09:08 +000062 Constant *CP;
63 unsigned Align; // CP alignment.
Chris Lattner3f0f71b2005-11-19 02:11:08 +000064
65 X86ISelAddressMode()
Evan Cheng77d86ff2006-02-25 10:09:08 +000066 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0), GV(0),
67 CP(0), Align(0) {
Chris Lattner3f0f71b2005-11-19 02:11:08 +000068 }
69 };
70}
71
72namespace {
Chris Lattner655e7df2005-11-16 01:54:32 +000073 Statistic<>
74 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
75
76 //===--------------------------------------------------------------------===//
77 /// ISel - X86 specific code to select X86 machine instructions for
78 /// SelectionDAG operations.
79 ///
80 class X86DAGToDAGISel : public SelectionDAGISel {
81 /// ContainsFPCode - Every instruction we select that uses or defines a FP
82 /// register should set this to true.
83 bool ContainsFPCode;
84
85 /// X86Lowering - This object fully describes how to lower LLVM code to an
86 /// X86-specific SelectionDAG.
87 X86TargetLowering X86Lowering;
88
89 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
90 /// make the right decision when generating code for different targets.
91 const X86Subtarget *Subtarget;
Evan Cheng5588de92006-02-18 00:15:05 +000092
93 unsigned GlobalBaseReg;
Chris Lattner655e7df2005-11-16 01:54:32 +000094 public:
Evan Cheng2dd2c652006-03-13 23:20:37 +000095 X86DAGToDAGISel(X86TargetMachine &TM)
96 : SelectionDAGISel(X86Lowering),
97 X86Lowering(*TM.getTargetLowering()) {
Chris Lattner655e7df2005-11-16 01:54:32 +000098 Subtarget = &TM.getSubtarget<X86Subtarget>();
99 }
100
Evan Cheng5588de92006-02-18 00:15:05 +0000101 virtual bool runOnFunction(Function &Fn) {
102 // Make sure we re-emit a set of the global base reg if necessary
103 GlobalBaseReg = 0;
104 return SelectionDAGISel::runOnFunction(Fn);
105 }
106
Chris Lattner655e7df2005-11-16 01:54:32 +0000107 virtual const char *getPassName() const {
108 return "X86 DAG->DAG Instruction Selection";
109 }
110
111 /// InstructionSelectBasicBlock - This callback is invoked by
112 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
113 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
114
Evan Chengbc7a0f442006-01-11 06:09:51 +0000115 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
116
Chris Lattner655e7df2005-11-16 01:54:32 +0000117// Include the pieces autogenerated from the target description.
118#include "X86GenDAGISel.inc"
119
120 private:
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000121 void Select(SDOperand &Result, SDOperand N);
Chris Lattner655e7df2005-11-16 01:54:32 +0000122
Evan Chenga86ba852006-02-11 02:05:36 +0000123 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM, bool isRoot = true);
Evan Chengc9fab312005-12-08 02:01:35 +0000124 bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
125 SDOperand &Index, SDOperand &Disp);
126 bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
127 SDOperand &Index, SDOperand &Disp);
Evan Chengd5f2ba02006-02-06 06:02:33 +0000128 bool TryFoldLoad(SDOperand P, SDOperand N,
129 SDOperand &Base, SDOperand &Scale,
Evan Cheng10d27902006-01-06 20:36:21 +0000130 SDOperand &Index, SDOperand &Disp);
Chris Lattnerba1ed582006-06-08 18:03:49 +0000131 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
132 /// inline asm expressions.
133 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
134 char ConstraintCode,
135 std::vector<SDOperand> &OutOps,
136 SelectionDAG &DAG);
137
Evan Chenge8a42362006-06-02 22:38:37 +0000138 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
139
Evan Cheng67ed58e2005-12-12 21:49:40 +0000140 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
141 SDOperand &Scale, SDOperand &Index,
142 SDOperand &Disp) {
143 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
144 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, MVT::i32) : AM.Base.Reg;
Evan Cheng1d712482005-12-17 09:13:43 +0000145 Scale = getI8Imm(AM.Scale);
Evan Cheng67ed58e2005-12-12 21:49:40 +0000146 Index = AM.IndexReg;
147 Disp = AM.GV ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp)
Evan Cheng77d86ff2006-02-25 10:09:08 +0000148 : (AM.CP ?
149 CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp)
150 : getI32Imm(AM.Disp));
Evan Cheng67ed58e2005-12-12 21:49:40 +0000151 }
152
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000153 /// getI8Imm - Return a target constant with the specified value, of type
154 /// i8.
155 inline SDOperand getI8Imm(unsigned Imm) {
156 return CurDAG->getTargetConstant(Imm, MVT::i8);
157 }
158
Chris Lattner655e7df2005-11-16 01:54:32 +0000159 /// getI16Imm - Return a target constant with the specified value, of type
160 /// i16.
161 inline SDOperand getI16Imm(unsigned Imm) {
162 return CurDAG->getTargetConstant(Imm, MVT::i16);
163 }
164
165 /// getI32Imm - Return a target constant with the specified value, of type
166 /// i32.
167 inline SDOperand getI32Imm(unsigned Imm) {
168 return CurDAG->getTargetConstant(Imm, MVT::i32);
169 }
Evan Chengd49cc362006-02-10 22:24:32 +0000170
Evan Cheng5588de92006-02-18 00:15:05 +0000171 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
172 /// base register. Return the virtual register that holds this value.
173 SDOperand getGlobalBaseReg();
174
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000175#ifndef NDEBUG
176 unsigned Indent;
177#endif
Chris Lattner655e7df2005-11-16 01:54:32 +0000178 };
179}
180
181/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
182/// when it has created a SelectionDAG for us to codegen.
183void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
184 DEBUG(BB->dump());
Chris Lattner7c551262006-01-11 01:15:34 +0000185 MachineFunction::iterator FirstMBB = BB;
Chris Lattner655e7df2005-11-16 01:54:32 +0000186
187 // Codegen the basic block.
Evan Chengd49cc362006-02-10 22:24:32 +0000188#ifndef NDEBUG
189 DEBUG(std::cerr << "===== Instruction selection begins:\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000190 Indent = 0;
Evan Chengd49cc362006-02-10 22:24:32 +0000191#endif
Evan Cheng54cb1832006-02-05 06:46:41 +0000192 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Cheng4af59da2006-05-25 00:24:28 +0000193 assert(InFlightSet.empty() && "ISel InFlightSet has not been emptied!");
Evan Chengd49cc362006-02-10 22:24:32 +0000194#ifndef NDEBUG
195 DEBUG(std::cerr << "===== Instruction selection ends:\n");
196#endif
Evan Cheng1d9b6712005-12-19 22:36:02 +0000197 CodeGenMap.clear();
Evan Cheng1a8e74d2006-05-24 20:46:25 +0000198 HandleMap.clear();
199 ReplaceMap.clear();
Chris Lattner655e7df2005-11-16 01:54:32 +0000200 DAG.RemoveDeadNodes();
201
202 // Emit machine code to BB.
203 ScheduleAndEmitDAG(DAG);
Chris Lattner7c551262006-01-11 01:15:34 +0000204
205 // If we are emitting FP stack code, scan the basic block to determine if this
206 // block defines any FP values. If so, put an FP_REG_KILL instruction before
207 // the terminator of the block.
Evan Chengcde9e302006-01-27 08:10:46 +0000208 if (!Subtarget->hasSSE2()) {
Chris Lattner7c551262006-01-11 01:15:34 +0000209 // Note that FP stack instructions *are* used in SSE code when returning
210 // values, but these are not live out of the basic block, so we don't need
211 // an FP_REG_KILL in this case either.
212 bool ContainsFPCode = false;
213
214 // Scan all of the machine instructions in these MBBs, checking for FP
215 // stores.
216 MachineFunction::iterator MBBI = FirstMBB;
217 do {
218 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
219 !ContainsFPCode && I != E; ++I) {
220 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
221 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
222 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
223 RegMap->getRegClass(I->getOperand(0).getReg()) ==
224 X86::RFPRegisterClass) {
225 ContainsFPCode = true;
226 break;
227 }
228 }
229 }
230 } while (!ContainsFPCode && &*(MBBI++) != BB);
231
232 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
233 // a copy of the input value in this block.
234 if (!ContainsFPCode) {
235 // Final check, check LLVM BB's that are successors to the LLVM BB
236 // corresponding to BB for FP PHI nodes.
237 const BasicBlock *LLVMBB = BB->getBasicBlock();
238 const PHINode *PN;
239 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
240 !ContainsFPCode && SI != E; ++SI) {
241 for (BasicBlock::const_iterator II = SI->begin();
242 (PN = dyn_cast<PHINode>(II)); ++II) {
243 if (PN->getType()->isFloatingPoint()) {
244 ContainsFPCode = true;
245 break;
246 }
247 }
248 }
249 }
250
251 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
252 if (ContainsFPCode) {
253 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
254 ++NumFPKill;
255 }
256 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000257}
258
Evan Chengbc7a0f442006-01-11 06:09:51 +0000259/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
260/// the main function.
Evan Chenge8a42362006-06-02 22:38:37 +0000261void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
262 MachineFrameInfo *MFI) {
263 if (Subtarget->TargetType == X86Subtarget::isCygwin)
264 BuildMI(BB, X86::CALLpcrel32, 1).addExternalSymbol("__main");
265
Evan Chengbc7a0f442006-01-11 06:09:51 +0000266 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
267 int CWFrameIdx = MFI->CreateStackObject(2, 2);
268 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
269
270 // Set the high part to be 64-bit precision.
271 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
272 CWFrameIdx, 1).addImm(2);
273
274 // Reload the modified control word now.
275 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
276}
277
278void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
279 // If this is main, emit special code for main.
280 MachineBasicBlock *BB = MF.begin();
281 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
282 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
283}
284
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000285/// MatchAddress - Add the specified node to the specified addressing mode,
286/// returning true if it cannot be done. This just pattern matches for the
287/// addressing mode
Evan Chenga86ba852006-02-11 02:05:36 +0000288bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
289 bool isRoot) {
Evan Cheng77d86ff2006-02-25 10:09:08 +0000290 bool Available = false;
291 // If N has already been selected, reuse the result unless in some very
292 // specific cases.
Evan Chenga86ba852006-02-11 02:05:36 +0000293 std::map<SDOperand, SDOperand>::iterator CGMI= CodeGenMap.find(N.getValue(0));
294 if (CGMI != CodeGenMap.end()) {
Evan Cheng77d86ff2006-02-25 10:09:08 +0000295 Available = true;
Evan Chenga86ba852006-02-11 02:05:36 +0000296 }
297
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000298 switch (N.getOpcode()) {
299 default: break;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000300 case ISD::Constant:
301 AM.Disp += cast<ConstantSDNode>(N)->getValue();
302 return false;
303
304 case X86ISD::Wrapper:
305 // If both base and index components have been picked, we can't fit
306 // the result available in the register in the addressing mode. Duplicate
307 // GlobalAddress or ConstantPool as displacement.
308 if (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
309 if (ConstantPoolSDNode *CP =
310 dyn_cast<ConstantPoolSDNode>(N.getOperand(0))) {
311 if (AM.CP == 0) {
312 AM.CP = CP->get();
313 AM.Align = CP->getAlignment();
314 AM.Disp += CP->getOffset();
315 return false;
316 }
317 } else if (GlobalAddressSDNode *G =
318 dyn_cast<GlobalAddressSDNode>(N.getOperand(0))) {
319 if (AM.GV == 0) {
320 AM.GV = G->getGlobal();
321 AM.Disp += G->getOffset();
322 return false;
323 }
324 }
325 }
326 break;
327
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000328 case ISD::FrameIndex:
329 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
330 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
331 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
332 return false;
333 }
334 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000335
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000336 case ISD::SHL:
Evan Cheng77d86ff2006-02-25 10:09:08 +0000337 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000338 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
339 unsigned Val = CN->getValue();
340 if (Val == 1 || Val == 2 || Val == 3) {
341 AM.Scale = 1 << Val;
342 SDOperand ShVal = N.Val->getOperand(0);
343
344 // Okay, we know that we have a scale by now. However, if the scaled
345 // value is an add of something and a constant, we can fold the
346 // constant into the disp field here.
347 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
348 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
349 AM.IndexReg = ShVal.Val->getOperand(0);
350 ConstantSDNode *AddVal =
351 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
352 AM.Disp += AddVal->getValue() << Val;
353 } else {
354 AM.IndexReg = ShVal;
355 }
356 return false;
357 }
358 }
359 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000360
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000361 case ISD::MUL:
362 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng77d86ff2006-02-25 10:09:08 +0000363 if (!Available &&
364 AM.BaseType == X86ISelAddressMode::RegBase &&
365 AM.Base.Reg.Val == 0 &&
366 AM.IndexReg.Val == 0)
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000367 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
368 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
369 AM.Scale = unsigned(CN->getValue())-1;
370
371 SDOperand MulVal = N.Val->getOperand(0);
372 SDOperand Reg;
373
374 // Okay, we know that we have a scale by now. However, if the scaled
375 // value is an add of something and a constant, we can fold the
376 // constant into the disp field here.
377 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
378 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
379 Reg = MulVal.Val->getOperand(0);
380 ConstantSDNode *AddVal =
381 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
382 AM.Disp += AddVal->getValue() * CN->getValue();
383 } else {
384 Reg = N.Val->getOperand(0);
385 }
386
387 AM.IndexReg = AM.Base.Reg = Reg;
388 return false;
389 }
390 break;
391
392 case ISD::ADD: {
Evan Cheng77d86ff2006-02-25 10:09:08 +0000393 if (!Available) {
Evan Chenga86ba852006-02-11 02:05:36 +0000394 X86ISelAddressMode Backup = AM;
395 if (!MatchAddress(N.Val->getOperand(0), AM, false) &&
396 !MatchAddress(N.Val->getOperand(1), AM, false))
397 return false;
398 AM = Backup;
399 if (!MatchAddress(N.Val->getOperand(1), AM, false) &&
400 !MatchAddress(N.Val->getOperand(0), AM, false))
401 return false;
402 AM = Backup;
403 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000404 break;
405 }
Evan Cheng734e1e22006-05-30 06:59:36 +0000406
407 case ISD::OR: {
408 if (!Available) {
409 X86ISelAddressMode Backup = AM;
410 // Look for (x << c1) | c2 where (c2 < c1)
411 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(0));
412 if (CN && !MatchAddress(N.Val->getOperand(1), AM, false)) {
413 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
414 AM.Disp = CN->getValue();
415 return false;
416 }
417 }
418 AM = Backup;
419 CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1));
420 if (CN && !MatchAddress(N.Val->getOperand(0), AM, false)) {
421 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
422 AM.Disp = CN->getValue();
423 return false;
424 }
425 }
426 AM = Backup;
427 }
428 break;
429 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000430 }
431
432 // Is the base register already occupied?
433 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
434 // If so, check to see if the scale index register is set.
435 if (AM.IndexReg.Val == 0) {
436 AM.IndexReg = N;
437 AM.Scale = 1;
438 return false;
439 }
440
441 // Otherwise, we cannot select it.
442 return true;
443 }
444
445 // Default, generate it as a register.
446 AM.BaseType = X86ISelAddressMode::RegBase;
447 AM.Base.Reg = N;
448 return false;
449}
450
Evan Chengc9fab312005-12-08 02:01:35 +0000451/// SelectAddr - returns true if it is able pattern match an addressing mode.
452/// It returns the operands which make up the maximal addressing mode it can
453/// match by reference.
454bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
455 SDOperand &Index, SDOperand &Disp) {
456 X86ISelAddressMode AM;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000457 if (MatchAddress(N, AM))
458 return false;
Evan Chengc9fab312005-12-08 02:01:35 +0000459
Evan Chengbc7a0f442006-01-11 06:09:51 +0000460 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Chengd19d51f2006-02-05 05:25:07 +0000461 if (!AM.Base.Reg.Val)
Evan Chengbc7a0f442006-01-11 06:09:51 +0000462 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
Evan Chengc9fab312005-12-08 02:01:35 +0000463 }
Evan Chengbc7a0f442006-01-11 06:09:51 +0000464
Evan Chengd19d51f2006-02-05 05:25:07 +0000465 if (!AM.IndexReg.Val)
Evan Chengbc7a0f442006-01-11 06:09:51 +0000466 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
467
468 getAddressOperands(AM, Base, Scale, Index, Disp);
Evan Cheng77d86ff2006-02-25 10:09:08 +0000469
Evan Chengbc7a0f442006-01-11 06:09:51 +0000470 return true;
Evan Chengc9fab312005-12-08 02:01:35 +0000471}
472
Evan Cheng77d86ff2006-02-25 10:09:08 +0000473/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
474/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng77d86ff2006-02-25 10:09:08 +0000475bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base,
476 SDOperand &Scale,
477 SDOperand &Index, SDOperand &Disp) {
478 X86ISelAddressMode AM;
479 if (MatchAddress(N, AM))
480 return false;
481
482 unsigned Complexity = 0;
483 if (AM.BaseType == X86ISelAddressMode::RegBase)
484 if (AM.Base.Reg.Val)
485 Complexity = 1;
486 else
487 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
488 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
489 Complexity = 4;
490
491 if (AM.IndexReg.Val)
492 Complexity++;
493 else
494 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
495
Evan Cheng990c3602006-02-28 21:13:57 +0000496 if (AM.Scale > 2)
Evan Cheng77d86ff2006-02-25 10:09:08 +0000497 Complexity += 2;
Evan Cheng990c3602006-02-28 21:13:57 +0000498 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg
499 else if (AM.Scale > 1)
500 Complexity++;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000501
502 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
503 // to a LEA. This is determined with some expermentation but is by no means
504 // optimal (especially for code size consideration). LEA is nice because of
505 // its three-address nature. Tweak the cost function again when we can run
506 // convertToThreeAddress() at register allocation time.
507 if (AM.GV || AM.CP)
508 Complexity += 2;
509
510 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
511 Complexity++;
512
513 if (Complexity > 2) {
514 getAddressOperands(AM, Base, Scale, Index, Disp);
515 return true;
516 }
517
518 return false;
519}
520
Evan Chengd5f2ba02006-02-06 06:02:33 +0000521bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
522 SDOperand &Base, SDOperand &Scale,
523 SDOperand &Index, SDOperand &Disp) {
524 if (N.getOpcode() == ISD::LOAD &&
525 N.hasOneUse() &&
526 !CodeGenMap.count(N.getValue(0)) &&
527 (P.getNumOperands() == 1 || !isNonImmUse(P.Val, N.Val)))
Evan Cheng10d27902006-01-06 20:36:21 +0000528 return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp);
529 return false;
530}
531
532static bool isRegister0(SDOperand Op) {
Evan Chengc9fab312005-12-08 02:01:35 +0000533 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
534 return (R->getReg() == 0);
535 return false;
536}
537
Evan Cheng5588de92006-02-18 00:15:05 +0000538/// getGlobalBaseReg - Output the instructions required to put the
539/// base address to use for accessing globals into a register.
540///
541SDOperand X86DAGToDAGISel::getGlobalBaseReg() {
542 if (!GlobalBaseReg) {
543 // Insert the set of GlobalBaseReg into the first MBB of the function
544 MachineBasicBlock &FirstMBB = BB->getParent()->front();
545 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
546 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
547 // FIXME: when we get to LP64, we will need to create the appropriate
548 // type of register here.
Evan Cheng9fee4422006-05-16 07:21:53 +0000549 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng5588de92006-02-18 00:15:05 +0000550 BuildMI(FirstMBB, MBBI, X86::MovePCtoStack, 0);
551 BuildMI(FirstMBB, MBBI, X86::POP32r, 1, GlobalBaseReg);
552 }
553 return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
554}
555
Evan Chengf838cfc2006-05-20 01:36:52 +0000556static SDNode *FindCallStartFromCall(SDNode *Node) {
557 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
558 assert(Node->getOperand(0).getValueType() == MVT::Other &&
559 "Node doesn't have a token chain argument!");
560 return FindCallStartFromCall(Node->getOperand(0).Val);
561}
562
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000563void X86DAGToDAGISel::Select(SDOperand &Result, SDOperand N) {
Evan Cheng00fcb002005-12-15 01:02:48 +0000564 SDNode *Node = N.Val;
565 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng10d27902006-01-06 20:36:21 +0000566 unsigned Opc, MOpc;
567 unsigned Opcode = Node->getOpcode();
Chris Lattner655e7df2005-11-16 01:54:32 +0000568
Evan Chengd49cc362006-02-10 22:24:32 +0000569#ifndef NDEBUG
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000570 DEBUG(std::cerr << std::string(Indent, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000571 DEBUG(std::cerr << "Selecting: ");
572 DEBUG(Node->dump(CurDAG));
573 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000574 Indent += 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000575#endif
576
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000577 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
578 Result = N;
Evan Chengd49cc362006-02-10 22:24:32 +0000579#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000580 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000581 DEBUG(std::cerr << "== ");
582 DEBUG(Node->dump(CurDAG));
583 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000584 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000585#endif
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000586 return; // Already selected.
587 }
Evan Cheng2ae799a2006-01-11 22:15:18 +0000588
589 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(N);
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000590 if (CGMI != CodeGenMap.end()) {
591 Result = CGMI->second;
Evan Chengd49cc362006-02-10 22:24:32 +0000592#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000593 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000594 DEBUG(std::cerr << "== ");
595 DEBUG(Result.Val->dump(CurDAG));
596 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000597 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000598#endif
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000599 return;
600 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000601
Evan Cheng10d27902006-01-06 20:36:21 +0000602 switch (Opcode) {
Chris Lattner655e7df2005-11-16 01:54:32 +0000603 default: break;
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000604 case X86ISD::GlobalBaseReg:
605 Result = getGlobalBaseReg();
606 return;
607
Evan Cheng77d86ff2006-02-25 10:09:08 +0000608 case ISD::ADD: {
609 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
610 // code and is matched first so to prevent it from being turned into
611 // LEA32r X+c.
612 SDOperand N0 = N.getOperand(0);
613 SDOperand N1 = N.getOperand(1);
614 if (N.Val->getValueType(0) == MVT::i32 &&
615 N0.getOpcode() == X86ISD::Wrapper &&
616 N1.getOpcode() == ISD::Constant) {
617 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
618 SDOperand C(0, 0);
619 // TODO: handle ExternalSymbolSDNode.
620 if (GlobalAddressSDNode *G =
621 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
622 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), MVT::i32,
623 G->getOffset() + Offset);
624 } else if (ConstantPoolSDNode *CP =
625 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
626 C = CurDAG->getTargetConstantPool(CP->get(), MVT::i32,
627 CP->getAlignment(),
628 CP->getOffset()+Offset);
629 }
630
631 if (C.Val) {
632 if (N.Val->hasOneUse()) {
633 Result = CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, MVT::i32, C);
634 } else {
635 SDNode *ResNode = CurDAG->getTargetNode(X86::MOV32ri, MVT::i32, C);
636 Result = CodeGenMap[N] = SDOperand(ResNode, 0);
637 }
638 return;
639 }
640 }
641
642 // Other cases are handled by auto-generated code.
643 break;
Evan Cheng1f342c22006-02-23 02:43:52 +0000644 }
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000645
Evan Cheng10d27902006-01-06 20:36:21 +0000646 case ISD::MULHU:
647 case ISD::MULHS: {
648 if (Opcode == ISD::MULHU)
649 switch (NVT) {
650 default: assert(0 && "Unsupported VT!");
651 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
652 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
653 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
654 }
655 else
656 switch (NVT) {
657 default: assert(0 && "Unsupported VT!");
658 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
659 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
660 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
661 }
662
663 unsigned LoReg, HiReg;
664 switch (NVT) {
665 default: assert(0 && "Unsupported VT!");
666 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
667 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
668 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
669 }
670
671 SDOperand N0 = Node->getOperand(0);
672 SDOperand N1 = Node->getOperand(1);
673
674 bool foldedLoad = false;
675 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Chengd5f2ba02006-02-06 06:02:33 +0000676 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng92e27972006-01-06 23:19:29 +0000677 // MULHU and MULHS are commmutative
678 if (!foldedLoad) {
Evan Chengd5f2ba02006-02-06 06:02:33 +0000679 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng92e27972006-01-06 23:19:29 +0000680 if (foldedLoad) {
681 N0 = Node->getOperand(1);
682 N1 = Node->getOperand(0);
683 }
684 }
685
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000686 SDOperand Chain;
687 if (foldedLoad)
688 Select(Chain, N1.getOperand(0));
689 else
690 Chain = CurDAG->getEntryNode();
Evan Cheng10d27902006-01-06 20:36:21 +0000691
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000692 SDOperand InFlag(0, 0);
693 Select(N0, N0);
Evan Cheng10d27902006-01-06 20:36:21 +0000694 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000695 N0, InFlag);
Evan Cheng10d27902006-01-06 20:36:21 +0000696 InFlag = Chain.getValue(1);
697
698 if (foldedLoad) {
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000699 Select(Tmp0, Tmp0);
700 Select(Tmp1, Tmp1);
701 Select(Tmp2, Tmp2);
702 Select(Tmp3, Tmp3);
Evan Chengd1b82d82006-02-09 07:17:49 +0000703 SDNode *CNode =
704 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
705 Tmp2, Tmp3, Chain, InFlag);
706 Chain = SDOperand(CNode, 0);
707 InFlag = SDOperand(CNode, 1);
Evan Cheng10d27902006-01-06 20:36:21 +0000708 } else {
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000709 Select(N1, N1);
Evan Chengd1b82d82006-02-09 07:17:49 +0000710 InFlag =
711 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng10d27902006-01-06 20:36:21 +0000712 }
713
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000714 Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
Evan Cheng10d27902006-01-06 20:36:21 +0000715 CodeGenMap[N.getValue(0)] = Result;
Evan Chengd5f2ba02006-02-06 06:02:33 +0000716 if (foldedLoad) {
Evan Cheng92e27972006-01-06 23:19:29 +0000717 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
Evan Cheng101e4b92006-02-09 22:12:53 +0000718 AddHandleReplacement(N1.Val, 1, Result.Val, 1);
Evan Chengd5f2ba02006-02-06 06:02:33 +0000719 }
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000720
Evan Chengd49cc362006-02-10 22:24:32 +0000721#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000722 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000723 DEBUG(std::cerr << "== ");
724 DEBUG(Result.Val->dump(CurDAG));
725 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000726 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000727#endif
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000728 return;
Evan Cheng92e27972006-01-06 23:19:29 +0000729 }
Evan Cheng5588de92006-02-18 00:15:05 +0000730
Evan Cheng92e27972006-01-06 23:19:29 +0000731 case ISD::SDIV:
732 case ISD::UDIV:
733 case ISD::SREM:
734 case ISD::UREM: {
735 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
736 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
737 if (!isSigned)
738 switch (NVT) {
739 default: assert(0 && "Unsupported VT!");
740 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
741 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
742 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
743 }
744 else
745 switch (NVT) {
746 default: assert(0 && "Unsupported VT!");
747 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
748 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
749 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
750 }
751
752 unsigned LoReg, HiReg;
753 unsigned ClrOpcode, SExtOpcode;
754 switch (NVT) {
755 default: assert(0 && "Unsupported VT!");
756 case MVT::i8:
757 LoReg = X86::AL; HiReg = X86::AH;
Evan Chenga2efb9f2006-06-02 21:20:34 +0000758 ClrOpcode = X86::MOV8r0;
Evan Cheng92e27972006-01-06 23:19:29 +0000759 SExtOpcode = X86::CBW;
760 break;
761 case MVT::i16:
762 LoReg = X86::AX; HiReg = X86::DX;
Evan Chenga2efb9f2006-06-02 21:20:34 +0000763 ClrOpcode = X86::MOV16r0;
Evan Cheng92e27972006-01-06 23:19:29 +0000764 SExtOpcode = X86::CWD;
765 break;
766 case MVT::i32:
767 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chenga2efb9f2006-06-02 21:20:34 +0000768 ClrOpcode = X86::MOV32r0;
Evan Cheng92e27972006-01-06 23:19:29 +0000769 SExtOpcode = X86::CDQ;
770 break;
771 }
772
773 SDOperand N0 = Node->getOperand(0);
774 SDOperand N1 = Node->getOperand(1);
775
776 bool foldedLoad = false;
777 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Chengd5f2ba02006-02-06 06:02:33 +0000778 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000779 SDOperand Chain;
780 if (foldedLoad)
781 Select(Chain, N1.getOperand(0));
782 else
783 Chain = CurDAG->getEntryNode();
Evan Cheng92e27972006-01-06 23:19:29 +0000784
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000785 SDOperand InFlag(0, 0);
786 Select(N0, N0);
Evan Cheng92e27972006-01-06 23:19:29 +0000787 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000788 N0, InFlag);
Evan Cheng92e27972006-01-06 23:19:29 +0000789 InFlag = Chain.getValue(1);
790
791 if (isSigned) {
792 // Sign extend the low part into the high part.
Evan Chengd1b82d82006-02-09 07:17:49 +0000793 InFlag =
794 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000795 } else {
796 // Zero out the high part, effectively zero extending the input.
Evan Chenga2efb9f2006-06-02 21:20:34 +0000797 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000798 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT),
799 ClrNode, InFlag);
800 InFlag = Chain.getValue(1);
801 }
802
803 if (foldedLoad) {
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000804 Select(Tmp0, Tmp0);
805 Select(Tmp1, Tmp1);
806 Select(Tmp2, Tmp2);
807 Select(Tmp3, Tmp3);
Evan Chengd1b82d82006-02-09 07:17:49 +0000808 SDNode *CNode =
809 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
810 Tmp2, Tmp3, Chain, InFlag);
811 Chain = SDOperand(CNode, 0);
812 InFlag = SDOperand(CNode, 1);
Evan Cheng92e27972006-01-06 23:19:29 +0000813 } else {
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000814 Select(N1, N1);
Evan Chengd1b82d82006-02-09 07:17:49 +0000815 InFlag =
816 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000817 }
818
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000819 Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg,
820 NVT, InFlag);
Evan Cheng92e27972006-01-06 23:19:29 +0000821 CodeGenMap[N.getValue(0)] = Result;
Evan Chengd5f2ba02006-02-06 06:02:33 +0000822 if (foldedLoad) {
Evan Cheng92e27972006-01-06 23:19:29 +0000823 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
Evan Cheng101e4b92006-02-09 22:12:53 +0000824 AddHandleReplacement(N1.Val, 1, Result.Val, 1);
Evan Chengd5f2ba02006-02-06 06:02:33 +0000825 }
Evan Chengd49cc362006-02-10 22:24:32 +0000826
827#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000828 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000829 DEBUG(std::cerr << "== ");
830 DEBUG(Result.Val->dump(CurDAG));
831 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000832 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000833#endif
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000834 return;
Evan Cheng10d27902006-01-06 20:36:21 +0000835 }
Evan Cheng9733bde2006-05-08 08:01:26 +0000836
837 case ISD::TRUNCATE: {
838 if (NVT == MVT::i8) {
839 unsigned Opc2;
840 MVT::ValueType VT;
841 switch (Node->getOperand(0).getValueType()) {
842 default: assert(0 && "Unknown truncate!");
843 case MVT::i16:
844 Opc = X86::MOV16to16_;
845 VT = MVT::i16;
Evan Cheng9fee4422006-05-16 07:21:53 +0000846 Opc2 = X86::TRUNC_GR16_GR8;
Evan Cheng9733bde2006-05-08 08:01:26 +0000847 break;
848 case MVT::i32:
849 Opc = X86::MOV32to32_;
850 VT = MVT::i32;
Evan Cheng9fee4422006-05-16 07:21:53 +0000851 Opc2 = X86::TRUNC_GR32_GR8;
Evan Cheng9733bde2006-05-08 08:01:26 +0000852 break;
853 }
854
855 SDOperand Tmp0, Tmp1;
856 Select(Tmp0, Node->getOperand(0));
857 Tmp1 = SDOperand(CurDAG->getTargetNode(Opc, VT, Tmp0), 0);
858 Result = CodeGenMap[N] =
859 SDOperand(CurDAG->getTargetNode(Opc2, NVT, Tmp1), 0);
860
861#ifndef NDEBUG
862 DEBUG(std::cerr << std::string(Indent-2, ' '));
863 DEBUG(std::cerr << "== ");
864 DEBUG(Result.Val->dump(CurDAG));
865 DEBUG(std::cerr << "\n");
866 Indent -= 2;
867#endif
868 return;
869 }
Evan Chenga26c4512006-05-20 07:44:28 +0000870
871 break;
Evan Cheng9733bde2006-05-08 08:01:26 +0000872 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000873 }
874
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000875 SelectCode(Result, N);
Evan Chengd49cc362006-02-10 22:24:32 +0000876#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000877 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000878 DEBUG(std::cerr << "=> ");
879 DEBUG(Result.Val->dump(CurDAG));
880 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000881 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000882#endif
Chris Lattner655e7df2005-11-16 01:54:32 +0000883}
884
Chris Lattnerba1ed582006-06-08 18:03:49 +0000885bool X86DAGToDAGISel::
886SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
887 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
888 SDOperand Op0, Op1, Op2, Op3;
889 switch (ConstraintCode) {
890 case 'o': // offsetable ??
891 case 'v': // not offsetable ??
892 default: return true;
893 case 'm': // memory
894 if (!SelectAddr(Op, Op0, Op1, Op2, Op3))
895 return true;
896 break;
897 }
898
899 OutOps.resize(4);
900 Select(OutOps[0], Op0);
901 Select(OutOps[1], Op1);
902 Select(OutOps[2], Op2);
903 Select(OutOps[3], Op3);
904 return false;
905}
906
Chris Lattner655e7df2005-11-16 01:54:32 +0000907/// createX86ISelDag - This pass converts a legalized DAG into a
908/// X86-specific DAG, ready for instruction scheduling.
909///
Evan Cheng2dd2c652006-03-13 23:20:37 +0000910FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM) {
Chris Lattner655e7df2005-11-16 01:54:32 +0000911 return new X86DAGToDAGISel(TM);
912}