blob: 7f7e069b081ca53e5aadc7da1c4173b242370616 [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 Lattner3f0f71b2005-11-19 02:11:08 +0000131
Evan Chenge8a42362006-06-02 22:38:37 +0000132 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
133
Evan Cheng67ed58e2005-12-12 21:49:40 +0000134 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
135 SDOperand &Scale, SDOperand &Index,
136 SDOperand &Disp) {
137 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
138 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, MVT::i32) : AM.Base.Reg;
Evan Cheng1d712482005-12-17 09:13:43 +0000139 Scale = getI8Imm(AM.Scale);
Evan Cheng67ed58e2005-12-12 21:49:40 +0000140 Index = AM.IndexReg;
141 Disp = AM.GV ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp)
Evan Cheng77d86ff2006-02-25 10:09:08 +0000142 : (AM.CP ?
143 CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp)
144 : getI32Imm(AM.Disp));
Evan Cheng67ed58e2005-12-12 21:49:40 +0000145 }
146
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000147 /// getI8Imm - Return a target constant with the specified value, of type
148 /// i8.
149 inline SDOperand getI8Imm(unsigned Imm) {
150 return CurDAG->getTargetConstant(Imm, MVT::i8);
151 }
152
Chris Lattner655e7df2005-11-16 01:54:32 +0000153 /// getI16Imm - Return a target constant with the specified value, of type
154 /// i16.
155 inline SDOperand getI16Imm(unsigned Imm) {
156 return CurDAG->getTargetConstant(Imm, MVT::i16);
157 }
158
159 /// getI32Imm - Return a target constant with the specified value, of type
160 /// i32.
161 inline SDOperand getI32Imm(unsigned Imm) {
162 return CurDAG->getTargetConstant(Imm, MVT::i32);
163 }
Evan Chengd49cc362006-02-10 22:24:32 +0000164
Evan Cheng5588de92006-02-18 00:15:05 +0000165 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
166 /// base register. Return the virtual register that holds this value.
167 SDOperand getGlobalBaseReg();
168
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000169#ifndef NDEBUG
170 unsigned Indent;
171#endif
Chris Lattner655e7df2005-11-16 01:54:32 +0000172 };
173}
174
175/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
176/// when it has created a SelectionDAG for us to codegen.
177void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
178 DEBUG(BB->dump());
Chris Lattner7c551262006-01-11 01:15:34 +0000179 MachineFunction::iterator FirstMBB = BB;
Chris Lattner655e7df2005-11-16 01:54:32 +0000180
181 // Codegen the basic block.
Evan Chengd49cc362006-02-10 22:24:32 +0000182#ifndef NDEBUG
183 DEBUG(std::cerr << "===== Instruction selection begins:\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000184 Indent = 0;
Evan Chengd49cc362006-02-10 22:24:32 +0000185#endif
Evan Cheng54cb1832006-02-05 06:46:41 +0000186 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Cheng4af59da2006-05-25 00:24:28 +0000187 assert(InFlightSet.empty() && "ISel InFlightSet has not been emptied!");
Evan Chengd49cc362006-02-10 22:24:32 +0000188#ifndef NDEBUG
189 DEBUG(std::cerr << "===== Instruction selection ends:\n");
190#endif
Evan Cheng1d9b6712005-12-19 22:36:02 +0000191 CodeGenMap.clear();
Evan Cheng1a8e74d2006-05-24 20:46:25 +0000192 HandleMap.clear();
193 ReplaceMap.clear();
Chris Lattner655e7df2005-11-16 01:54:32 +0000194 DAG.RemoveDeadNodes();
195
196 // Emit machine code to BB.
197 ScheduleAndEmitDAG(DAG);
Chris Lattner7c551262006-01-11 01:15:34 +0000198
199 // If we are emitting FP stack code, scan the basic block to determine if this
200 // block defines any FP values. If so, put an FP_REG_KILL instruction before
201 // the terminator of the block.
Evan Chengcde9e302006-01-27 08:10:46 +0000202 if (!Subtarget->hasSSE2()) {
Chris Lattner7c551262006-01-11 01:15:34 +0000203 // Note that FP stack instructions *are* used in SSE code when returning
204 // values, but these are not live out of the basic block, so we don't need
205 // an FP_REG_KILL in this case either.
206 bool ContainsFPCode = false;
207
208 // Scan all of the machine instructions in these MBBs, checking for FP
209 // stores.
210 MachineFunction::iterator MBBI = FirstMBB;
211 do {
212 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
213 !ContainsFPCode && I != E; ++I) {
214 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
215 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
216 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
217 RegMap->getRegClass(I->getOperand(0).getReg()) ==
218 X86::RFPRegisterClass) {
219 ContainsFPCode = true;
220 break;
221 }
222 }
223 }
224 } while (!ContainsFPCode && &*(MBBI++) != BB);
225
226 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
227 // a copy of the input value in this block.
228 if (!ContainsFPCode) {
229 // Final check, check LLVM BB's that are successors to the LLVM BB
230 // corresponding to BB for FP PHI nodes.
231 const BasicBlock *LLVMBB = BB->getBasicBlock();
232 const PHINode *PN;
233 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
234 !ContainsFPCode && SI != E; ++SI) {
235 for (BasicBlock::const_iterator II = SI->begin();
236 (PN = dyn_cast<PHINode>(II)); ++II) {
237 if (PN->getType()->isFloatingPoint()) {
238 ContainsFPCode = true;
239 break;
240 }
241 }
242 }
243 }
244
245 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
246 if (ContainsFPCode) {
247 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
248 ++NumFPKill;
249 }
250 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000251}
252
Evan Chengbc7a0f442006-01-11 06:09:51 +0000253/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
254/// the main function.
Evan Chenge8a42362006-06-02 22:38:37 +0000255void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
256 MachineFrameInfo *MFI) {
257 if (Subtarget->TargetType == X86Subtarget::isCygwin)
258 BuildMI(BB, X86::CALLpcrel32, 1).addExternalSymbol("__main");
259
Evan Chengbc7a0f442006-01-11 06:09:51 +0000260 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
261 int CWFrameIdx = MFI->CreateStackObject(2, 2);
262 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
263
264 // Set the high part to be 64-bit precision.
265 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
266 CWFrameIdx, 1).addImm(2);
267
268 // Reload the modified control word now.
269 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
270}
271
272void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
273 // If this is main, emit special code for main.
274 MachineBasicBlock *BB = MF.begin();
275 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
276 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
277}
278
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000279/// MatchAddress - Add the specified node to the specified addressing mode,
280/// returning true if it cannot be done. This just pattern matches for the
281/// addressing mode
Evan Chenga86ba852006-02-11 02:05:36 +0000282bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
283 bool isRoot) {
Evan Cheng77d86ff2006-02-25 10:09:08 +0000284 bool Available = false;
285 // If N has already been selected, reuse the result unless in some very
286 // specific cases.
Evan Chenga86ba852006-02-11 02:05:36 +0000287 std::map<SDOperand, SDOperand>::iterator CGMI= CodeGenMap.find(N.getValue(0));
288 if (CGMI != CodeGenMap.end()) {
Evan Cheng77d86ff2006-02-25 10:09:08 +0000289 Available = true;
Evan Chenga86ba852006-02-11 02:05:36 +0000290 }
291
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000292 switch (N.getOpcode()) {
293 default: break;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000294 case ISD::Constant:
295 AM.Disp += cast<ConstantSDNode>(N)->getValue();
296 return false;
297
298 case X86ISD::Wrapper:
299 // If both base and index components have been picked, we can't fit
300 // the result available in the register in the addressing mode. Duplicate
301 // GlobalAddress or ConstantPool as displacement.
302 if (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
303 if (ConstantPoolSDNode *CP =
304 dyn_cast<ConstantPoolSDNode>(N.getOperand(0))) {
305 if (AM.CP == 0) {
306 AM.CP = CP->get();
307 AM.Align = CP->getAlignment();
308 AM.Disp += CP->getOffset();
309 return false;
310 }
311 } else if (GlobalAddressSDNode *G =
312 dyn_cast<GlobalAddressSDNode>(N.getOperand(0))) {
313 if (AM.GV == 0) {
314 AM.GV = G->getGlobal();
315 AM.Disp += G->getOffset();
316 return false;
317 }
318 }
319 }
320 break;
321
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000322 case ISD::FrameIndex:
323 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
324 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
325 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
326 return false;
327 }
328 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000329
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000330 case ISD::SHL:
Evan Cheng77d86ff2006-02-25 10:09:08 +0000331 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000332 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
333 unsigned Val = CN->getValue();
334 if (Val == 1 || Val == 2 || Val == 3) {
335 AM.Scale = 1 << Val;
336 SDOperand ShVal = N.Val->getOperand(0);
337
338 // Okay, we know that we have a scale by now. However, if the scaled
339 // value is an add of something and a constant, we can fold the
340 // constant into the disp field here.
341 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
342 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
343 AM.IndexReg = ShVal.Val->getOperand(0);
344 ConstantSDNode *AddVal =
345 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
346 AM.Disp += AddVal->getValue() << Val;
347 } else {
348 AM.IndexReg = ShVal;
349 }
350 return false;
351 }
352 }
353 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000354
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000355 case ISD::MUL:
356 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng77d86ff2006-02-25 10:09:08 +0000357 if (!Available &&
358 AM.BaseType == X86ISelAddressMode::RegBase &&
359 AM.Base.Reg.Val == 0 &&
360 AM.IndexReg.Val == 0)
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000361 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
362 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
363 AM.Scale = unsigned(CN->getValue())-1;
364
365 SDOperand MulVal = N.Val->getOperand(0);
366 SDOperand Reg;
367
368 // Okay, we know that we have a scale by now. However, if the scaled
369 // value is an add of something and a constant, we can fold the
370 // constant into the disp field here.
371 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
372 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
373 Reg = MulVal.Val->getOperand(0);
374 ConstantSDNode *AddVal =
375 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
376 AM.Disp += AddVal->getValue() * CN->getValue();
377 } else {
378 Reg = N.Val->getOperand(0);
379 }
380
381 AM.IndexReg = AM.Base.Reg = Reg;
382 return false;
383 }
384 break;
385
386 case ISD::ADD: {
Evan Cheng77d86ff2006-02-25 10:09:08 +0000387 if (!Available) {
Evan Chenga86ba852006-02-11 02:05:36 +0000388 X86ISelAddressMode Backup = AM;
389 if (!MatchAddress(N.Val->getOperand(0), AM, false) &&
390 !MatchAddress(N.Val->getOperand(1), AM, false))
391 return false;
392 AM = Backup;
393 if (!MatchAddress(N.Val->getOperand(1), AM, false) &&
394 !MatchAddress(N.Val->getOperand(0), AM, false))
395 return false;
396 AM = Backup;
397 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000398 break;
399 }
Evan Cheng734e1e22006-05-30 06:59:36 +0000400
401 case ISD::OR: {
402 if (!Available) {
403 X86ISelAddressMode Backup = AM;
404 // Look for (x << c1) | c2 where (c2 < c1)
405 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(0));
406 if (CN && !MatchAddress(N.Val->getOperand(1), AM, false)) {
407 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
408 AM.Disp = CN->getValue();
409 return false;
410 }
411 }
412 AM = Backup;
413 CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1));
414 if (CN && !MatchAddress(N.Val->getOperand(0), AM, false)) {
415 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
416 AM.Disp = CN->getValue();
417 return false;
418 }
419 }
420 AM = Backup;
421 }
422 break;
423 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000424 }
425
426 // Is the base register already occupied?
427 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
428 // If so, check to see if the scale index register is set.
429 if (AM.IndexReg.Val == 0) {
430 AM.IndexReg = N;
431 AM.Scale = 1;
432 return false;
433 }
434
435 // Otherwise, we cannot select it.
436 return true;
437 }
438
439 // Default, generate it as a register.
440 AM.BaseType = X86ISelAddressMode::RegBase;
441 AM.Base.Reg = N;
442 return false;
443}
444
Evan Chengc9fab312005-12-08 02:01:35 +0000445/// SelectAddr - returns true if it is able pattern match an addressing mode.
446/// It returns the operands which make up the maximal addressing mode it can
447/// match by reference.
448bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
449 SDOperand &Index, SDOperand &Disp) {
450 X86ISelAddressMode AM;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000451 if (MatchAddress(N, AM))
452 return false;
Evan Chengc9fab312005-12-08 02:01:35 +0000453
Evan Chengbc7a0f442006-01-11 06:09:51 +0000454 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Chengd19d51f2006-02-05 05:25:07 +0000455 if (!AM.Base.Reg.Val)
Evan Chengbc7a0f442006-01-11 06:09:51 +0000456 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
Evan Chengc9fab312005-12-08 02:01:35 +0000457 }
Evan Chengbc7a0f442006-01-11 06:09:51 +0000458
Evan Chengd19d51f2006-02-05 05:25:07 +0000459 if (!AM.IndexReg.Val)
Evan Chengbc7a0f442006-01-11 06:09:51 +0000460 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
461
462 getAddressOperands(AM, Base, Scale, Index, Disp);
Evan Cheng77d86ff2006-02-25 10:09:08 +0000463
Evan Chengbc7a0f442006-01-11 06:09:51 +0000464 return true;
Evan Chengc9fab312005-12-08 02:01:35 +0000465}
466
Evan Cheng77d86ff2006-02-25 10:09:08 +0000467/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
468/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng77d86ff2006-02-25 10:09:08 +0000469bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base,
470 SDOperand &Scale,
471 SDOperand &Index, SDOperand &Disp) {
472 X86ISelAddressMode AM;
473 if (MatchAddress(N, AM))
474 return false;
475
476 unsigned Complexity = 0;
477 if (AM.BaseType == X86ISelAddressMode::RegBase)
478 if (AM.Base.Reg.Val)
479 Complexity = 1;
480 else
481 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
482 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
483 Complexity = 4;
484
485 if (AM.IndexReg.Val)
486 Complexity++;
487 else
488 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
489
Evan Cheng990c3602006-02-28 21:13:57 +0000490 if (AM.Scale > 2)
Evan Cheng77d86ff2006-02-25 10:09:08 +0000491 Complexity += 2;
Evan Cheng990c3602006-02-28 21:13:57 +0000492 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg
493 else if (AM.Scale > 1)
494 Complexity++;
Evan Cheng77d86ff2006-02-25 10:09:08 +0000495
496 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
497 // to a LEA. This is determined with some expermentation but is by no means
498 // optimal (especially for code size consideration). LEA is nice because of
499 // its three-address nature. Tweak the cost function again when we can run
500 // convertToThreeAddress() at register allocation time.
501 if (AM.GV || AM.CP)
502 Complexity += 2;
503
504 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
505 Complexity++;
506
507 if (Complexity > 2) {
508 getAddressOperands(AM, Base, Scale, Index, Disp);
509 return true;
510 }
511
512 return false;
513}
514
Evan Chengd5f2ba02006-02-06 06:02:33 +0000515bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
516 SDOperand &Base, SDOperand &Scale,
517 SDOperand &Index, SDOperand &Disp) {
518 if (N.getOpcode() == ISD::LOAD &&
519 N.hasOneUse() &&
520 !CodeGenMap.count(N.getValue(0)) &&
521 (P.getNumOperands() == 1 || !isNonImmUse(P.Val, N.Val)))
Evan Cheng10d27902006-01-06 20:36:21 +0000522 return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp);
523 return false;
524}
525
526static bool isRegister0(SDOperand Op) {
Evan Chengc9fab312005-12-08 02:01:35 +0000527 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
528 return (R->getReg() == 0);
529 return false;
530}
531
Evan Cheng5588de92006-02-18 00:15:05 +0000532/// getGlobalBaseReg - Output the instructions required to put the
533/// base address to use for accessing globals into a register.
534///
535SDOperand X86DAGToDAGISel::getGlobalBaseReg() {
536 if (!GlobalBaseReg) {
537 // Insert the set of GlobalBaseReg into the first MBB of the function
538 MachineBasicBlock &FirstMBB = BB->getParent()->front();
539 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
540 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
541 // FIXME: when we get to LP64, we will need to create the appropriate
542 // type of register here.
Evan Cheng9fee4422006-05-16 07:21:53 +0000543 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng5588de92006-02-18 00:15:05 +0000544 BuildMI(FirstMBB, MBBI, X86::MovePCtoStack, 0);
545 BuildMI(FirstMBB, MBBI, X86::POP32r, 1, GlobalBaseReg);
546 }
547 return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
548}
549
Evan Chengf838cfc2006-05-20 01:36:52 +0000550static SDNode *FindCallStartFromCall(SDNode *Node) {
551 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
552 assert(Node->getOperand(0).getValueType() == MVT::Other &&
553 "Node doesn't have a token chain argument!");
554 return FindCallStartFromCall(Node->getOperand(0).Val);
555}
556
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000557void X86DAGToDAGISel::Select(SDOperand &Result, SDOperand N) {
Evan Cheng00fcb002005-12-15 01:02:48 +0000558 SDNode *Node = N.Val;
559 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng10d27902006-01-06 20:36:21 +0000560 unsigned Opc, MOpc;
561 unsigned Opcode = Node->getOpcode();
Chris Lattner655e7df2005-11-16 01:54:32 +0000562
Evan Chengd49cc362006-02-10 22:24:32 +0000563#ifndef NDEBUG
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000564 DEBUG(std::cerr << std::string(Indent, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000565 DEBUG(std::cerr << "Selecting: ");
566 DEBUG(Node->dump(CurDAG));
567 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000568 Indent += 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000569#endif
570
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000571 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
572 Result = N;
Evan Chengd49cc362006-02-10 22:24:32 +0000573#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000574 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000575 DEBUG(std::cerr << "== ");
576 DEBUG(Node->dump(CurDAG));
577 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000578 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000579#endif
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000580 return; // Already selected.
581 }
Evan Cheng2ae799a2006-01-11 22:15:18 +0000582
583 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(N);
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000584 if (CGMI != CodeGenMap.end()) {
585 Result = CGMI->second;
Evan Chengd49cc362006-02-10 22:24:32 +0000586#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000587 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000588 DEBUG(std::cerr << "== ");
589 DEBUG(Result.Val->dump(CurDAG));
590 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000591 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000592#endif
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000593 return;
594 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000595
Evan Cheng10d27902006-01-06 20:36:21 +0000596 switch (Opcode) {
Chris Lattner655e7df2005-11-16 01:54:32 +0000597 default: break;
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000598 case X86ISD::GlobalBaseReg:
599 Result = getGlobalBaseReg();
600 return;
601
Evan Cheng77d86ff2006-02-25 10:09:08 +0000602 case ISD::ADD: {
603 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
604 // code and is matched first so to prevent it from being turned into
605 // LEA32r X+c.
606 SDOperand N0 = N.getOperand(0);
607 SDOperand N1 = N.getOperand(1);
608 if (N.Val->getValueType(0) == MVT::i32 &&
609 N0.getOpcode() == X86ISD::Wrapper &&
610 N1.getOpcode() == ISD::Constant) {
611 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
612 SDOperand C(0, 0);
613 // TODO: handle ExternalSymbolSDNode.
614 if (GlobalAddressSDNode *G =
615 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
616 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), MVT::i32,
617 G->getOffset() + Offset);
618 } else if (ConstantPoolSDNode *CP =
619 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
620 C = CurDAG->getTargetConstantPool(CP->get(), MVT::i32,
621 CP->getAlignment(),
622 CP->getOffset()+Offset);
623 }
624
625 if (C.Val) {
626 if (N.Val->hasOneUse()) {
627 Result = CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, MVT::i32, C);
628 } else {
629 SDNode *ResNode = CurDAG->getTargetNode(X86::MOV32ri, MVT::i32, C);
630 Result = CodeGenMap[N] = SDOperand(ResNode, 0);
631 }
632 return;
633 }
634 }
635
636 // Other cases are handled by auto-generated code.
637 break;
Evan Cheng1f342c22006-02-23 02:43:52 +0000638 }
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000639
Evan Cheng10d27902006-01-06 20:36:21 +0000640 case ISD::MULHU:
641 case ISD::MULHS: {
642 if (Opcode == ISD::MULHU)
643 switch (NVT) {
644 default: assert(0 && "Unsupported VT!");
645 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
646 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
647 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
648 }
649 else
650 switch (NVT) {
651 default: assert(0 && "Unsupported VT!");
652 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
653 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
654 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
655 }
656
657 unsigned LoReg, HiReg;
658 switch (NVT) {
659 default: assert(0 && "Unsupported VT!");
660 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
661 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
662 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
663 }
664
665 SDOperand N0 = Node->getOperand(0);
666 SDOperand N1 = Node->getOperand(1);
667
668 bool foldedLoad = false;
669 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Chengd5f2ba02006-02-06 06:02:33 +0000670 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng92e27972006-01-06 23:19:29 +0000671 // MULHU and MULHS are commmutative
672 if (!foldedLoad) {
Evan Chengd5f2ba02006-02-06 06:02:33 +0000673 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng92e27972006-01-06 23:19:29 +0000674 if (foldedLoad) {
675 N0 = Node->getOperand(1);
676 N1 = Node->getOperand(0);
677 }
678 }
679
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000680 SDOperand Chain;
681 if (foldedLoad)
682 Select(Chain, N1.getOperand(0));
683 else
684 Chain = CurDAG->getEntryNode();
Evan Cheng10d27902006-01-06 20:36:21 +0000685
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000686 SDOperand InFlag(0, 0);
687 Select(N0, N0);
Evan Cheng10d27902006-01-06 20:36:21 +0000688 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000689 N0, InFlag);
Evan Cheng10d27902006-01-06 20:36:21 +0000690 InFlag = Chain.getValue(1);
691
692 if (foldedLoad) {
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000693 Select(Tmp0, Tmp0);
694 Select(Tmp1, Tmp1);
695 Select(Tmp2, Tmp2);
696 Select(Tmp3, Tmp3);
Evan Chengd1b82d82006-02-09 07:17:49 +0000697 SDNode *CNode =
698 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
699 Tmp2, Tmp3, Chain, InFlag);
700 Chain = SDOperand(CNode, 0);
701 InFlag = SDOperand(CNode, 1);
Evan Cheng10d27902006-01-06 20:36:21 +0000702 } else {
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000703 Select(N1, N1);
Evan Chengd1b82d82006-02-09 07:17:49 +0000704 InFlag =
705 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng10d27902006-01-06 20:36:21 +0000706 }
707
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000708 Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
Evan Cheng10d27902006-01-06 20:36:21 +0000709 CodeGenMap[N.getValue(0)] = Result;
Evan Chengd5f2ba02006-02-06 06:02:33 +0000710 if (foldedLoad) {
Evan Cheng92e27972006-01-06 23:19:29 +0000711 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
Evan Cheng101e4b92006-02-09 22:12:53 +0000712 AddHandleReplacement(N1.Val, 1, Result.Val, 1);
Evan Chengd5f2ba02006-02-06 06:02:33 +0000713 }
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000714
Evan Chengd49cc362006-02-10 22:24:32 +0000715#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000716 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000717 DEBUG(std::cerr << "== ");
718 DEBUG(Result.Val->dump(CurDAG));
719 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000720 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000721#endif
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000722 return;
Evan Cheng92e27972006-01-06 23:19:29 +0000723 }
Evan Cheng5588de92006-02-18 00:15:05 +0000724
Evan Cheng92e27972006-01-06 23:19:29 +0000725 case ISD::SDIV:
726 case ISD::UDIV:
727 case ISD::SREM:
728 case ISD::UREM: {
729 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
730 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
731 if (!isSigned)
732 switch (NVT) {
733 default: assert(0 && "Unsupported VT!");
734 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
735 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
736 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
737 }
738 else
739 switch (NVT) {
740 default: assert(0 && "Unsupported VT!");
741 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
742 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
743 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
744 }
745
746 unsigned LoReg, HiReg;
747 unsigned ClrOpcode, SExtOpcode;
748 switch (NVT) {
749 default: assert(0 && "Unsupported VT!");
750 case MVT::i8:
751 LoReg = X86::AL; HiReg = X86::AH;
Evan Chenga2efb9f2006-06-02 21:20:34 +0000752 ClrOpcode = X86::MOV8r0;
Evan Cheng92e27972006-01-06 23:19:29 +0000753 SExtOpcode = X86::CBW;
754 break;
755 case MVT::i16:
756 LoReg = X86::AX; HiReg = X86::DX;
Evan Chenga2efb9f2006-06-02 21:20:34 +0000757 ClrOpcode = X86::MOV16r0;
Evan Cheng92e27972006-01-06 23:19:29 +0000758 SExtOpcode = X86::CWD;
759 break;
760 case MVT::i32:
761 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chenga2efb9f2006-06-02 21:20:34 +0000762 ClrOpcode = X86::MOV32r0;
Evan Cheng92e27972006-01-06 23:19:29 +0000763 SExtOpcode = X86::CDQ;
764 break;
765 }
766
767 SDOperand N0 = Node->getOperand(0);
768 SDOperand N1 = Node->getOperand(1);
769
770 bool foldedLoad = false;
771 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Chengd5f2ba02006-02-06 06:02:33 +0000772 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000773 SDOperand Chain;
774 if (foldedLoad)
775 Select(Chain, N1.getOperand(0));
776 else
777 Chain = CurDAG->getEntryNode();
Evan Cheng92e27972006-01-06 23:19:29 +0000778
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000779 SDOperand InFlag(0, 0);
780 Select(N0, N0);
Evan Cheng92e27972006-01-06 23:19:29 +0000781 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000782 N0, InFlag);
Evan Cheng92e27972006-01-06 23:19:29 +0000783 InFlag = Chain.getValue(1);
784
785 if (isSigned) {
786 // Sign extend the low part into the high part.
Evan Chengd1b82d82006-02-09 07:17:49 +0000787 InFlag =
788 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000789 } else {
790 // Zero out the high part, effectively zero extending the input.
Evan Chenga2efb9f2006-06-02 21:20:34 +0000791 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000792 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT),
793 ClrNode, InFlag);
794 InFlag = Chain.getValue(1);
795 }
796
797 if (foldedLoad) {
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000798 Select(Tmp0, Tmp0);
799 Select(Tmp1, Tmp1);
800 Select(Tmp2, Tmp2);
801 Select(Tmp3, Tmp3);
Evan Chengd1b82d82006-02-09 07:17:49 +0000802 SDNode *CNode =
803 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
804 Tmp2, Tmp3, Chain, InFlag);
805 Chain = SDOperand(CNode, 0);
806 InFlag = SDOperand(CNode, 1);
Evan Cheng92e27972006-01-06 23:19:29 +0000807 } else {
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000808 Select(N1, N1);
Evan Chengd1b82d82006-02-09 07:17:49 +0000809 InFlag =
810 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng92e27972006-01-06 23:19:29 +0000811 }
812
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000813 Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg,
814 NVT, InFlag);
Evan Cheng92e27972006-01-06 23:19:29 +0000815 CodeGenMap[N.getValue(0)] = Result;
Evan Chengd5f2ba02006-02-06 06:02:33 +0000816 if (foldedLoad) {
Evan Cheng92e27972006-01-06 23:19:29 +0000817 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
Evan Cheng101e4b92006-02-09 22:12:53 +0000818 AddHandleReplacement(N1.Val, 1, Result.Val, 1);
Evan Chengd5f2ba02006-02-06 06:02:33 +0000819 }
Evan Chengd49cc362006-02-10 22:24:32 +0000820
821#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000822 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000823 DEBUG(std::cerr << "== ");
824 DEBUG(Result.Val->dump(CurDAG));
825 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000826 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000827#endif
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000828 return;
Evan Cheng10d27902006-01-06 20:36:21 +0000829 }
Evan Cheng9733bde2006-05-08 08:01:26 +0000830
831 case ISD::TRUNCATE: {
832 if (NVT == MVT::i8) {
833 unsigned Opc2;
834 MVT::ValueType VT;
835 switch (Node->getOperand(0).getValueType()) {
836 default: assert(0 && "Unknown truncate!");
837 case MVT::i16:
838 Opc = X86::MOV16to16_;
839 VT = MVT::i16;
Evan Cheng9fee4422006-05-16 07:21:53 +0000840 Opc2 = X86::TRUNC_GR16_GR8;
Evan Cheng9733bde2006-05-08 08:01:26 +0000841 break;
842 case MVT::i32:
843 Opc = X86::MOV32to32_;
844 VT = MVT::i32;
Evan Cheng9fee4422006-05-16 07:21:53 +0000845 Opc2 = X86::TRUNC_GR32_GR8;
Evan Cheng9733bde2006-05-08 08:01:26 +0000846 break;
847 }
848
849 SDOperand Tmp0, Tmp1;
850 Select(Tmp0, Node->getOperand(0));
851 Tmp1 = SDOperand(CurDAG->getTargetNode(Opc, VT, Tmp0), 0);
852 Result = CodeGenMap[N] =
853 SDOperand(CurDAG->getTargetNode(Opc2, NVT, Tmp1), 0);
854
855#ifndef NDEBUG
856 DEBUG(std::cerr << std::string(Indent-2, ' '));
857 DEBUG(std::cerr << "== ");
858 DEBUG(Result.Val->dump(CurDAG));
859 DEBUG(std::cerr << "\n");
860 Indent -= 2;
861#endif
862 return;
863 }
Evan Chenga26c4512006-05-20 07:44:28 +0000864
865 break;
Evan Cheng9733bde2006-05-08 08:01:26 +0000866 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000867 }
868
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000869 SelectCode(Result, N);
Evan Chengd49cc362006-02-10 22:24:32 +0000870#ifndef NDEBUG
Evan Chenga86ba852006-02-11 02:05:36 +0000871 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengd49cc362006-02-10 22:24:32 +0000872 DEBUG(std::cerr << "=> ");
873 DEBUG(Result.Val->dump(CurDAG));
874 DEBUG(std::cerr << "\n");
Evan Cheng2b6f78b2006-02-10 22:46:26 +0000875 Indent -= 2;
Evan Chengd49cc362006-02-10 22:24:32 +0000876#endif
Chris Lattner655e7df2005-11-16 01:54:32 +0000877}
878
879/// createX86ISelDag - This pass converts a legalized DAG into a
880/// X86-specific DAG, ready for instruction scheduling.
881///
Evan Cheng2dd2c652006-03-13 23:20:37 +0000882FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM) {
Chris Lattner655e7df2005-11-16 01:54:32 +0000883 return new X86DAGToDAGISel(TM);
884}