blob: 1d88f1ff6ff3b3da2b208372b107b10c3891baaa [file] [log] [blame]
Chris Lattner7a125372005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattnerc961eea2005-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 Chengf597dc72006-02-10 22:24:32 +000015#define DEBUG_TYPE "isel"
Chris Lattnerc961eea2005-11-16 01:54:32 +000016#include "X86.h"
Evan Cheng8700e142006-01-11 06:09:51 +000017#include "X86InstrBuilder.h"
Evan Chengc4c62572006-03-13 23:20:37 +000018#include "X86ISelLowering.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000019#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000020#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000021#include "X86TargetMachine.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000022#include "llvm/GlobalValue.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000023#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000024#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000025#include "llvm/Support/CFG.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000027#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000029#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000031#include "llvm/CodeGen/SelectionDAGISel.h"
32#include "llvm/Target/TargetMachine.h"
33#include "llvm/Support/Debug.h"
Chris Lattner2c79de82006-06-28 23:27:49 +000034#include "llvm/Support/Visibility.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000035#include "llvm/ADT/Statistic.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000036#include <iostream>
Evan Chengba2f0a92006-02-05 06:46:41 +000037#include <set>
Chris Lattnerc961eea2005-11-16 01:54:32 +000038using namespace llvm;
39
40//===----------------------------------------------------------------------===//
41// Pattern Matcher Implementation
42//===----------------------------------------------------------------------===//
43
44namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000045 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
46 /// SDOperand's instead of register numbers for the leaves of the matched
47 /// tree.
48 struct X86ISelAddressMode {
49 enum {
50 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000051 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000052 } BaseType;
53
54 struct { // This is really a union, discriminated by BaseType!
55 SDOperand Reg;
56 int FrameIndex;
57 } Base;
58
59 unsigned Scale;
60 SDOperand IndexReg;
61 unsigned Disp;
62 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000063 Constant *CP;
64 unsigned Align; // CP alignment.
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000065
66 X86ISelAddressMode()
Evan Cheng51a9ed92006-02-25 10:09:08 +000067 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0), GV(0),
68 CP(0), Align(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000069 }
70 };
71}
72
73namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +000074 Statistic<>
75 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
76
77 //===--------------------------------------------------------------------===//
78 /// ISel - X86 specific code to select X86 machine instructions for
79 /// SelectionDAG operations.
80 ///
Chris Lattner2c79de82006-06-28 23:27:49 +000081 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +000082 /// ContainsFPCode - Every instruction we select that uses or defines a FP
83 /// register should set this to true.
84 bool ContainsFPCode;
85
86 /// X86Lowering - This object fully describes how to lower LLVM code to an
87 /// X86-specific SelectionDAG.
88 X86TargetLowering X86Lowering;
89
90 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
91 /// make the right decision when generating code for different targets.
92 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +000093
94 unsigned GlobalBaseReg;
Chris Lattnerc961eea2005-11-16 01:54:32 +000095 public:
Evan Chengc4c62572006-03-13 23:20:37 +000096 X86DAGToDAGISel(X86TargetMachine &TM)
97 : SelectionDAGISel(X86Lowering),
98 X86Lowering(*TM.getTargetLowering()) {
Chris Lattnerc961eea2005-11-16 01:54:32 +000099 Subtarget = &TM.getSubtarget<X86Subtarget>();
100 }
101
Evan Cheng7ccced62006-02-18 00:15:05 +0000102 virtual bool runOnFunction(Function &Fn) {
103 // Make sure we re-emit a set of the global base reg if necessary
104 GlobalBaseReg = 0;
105 return SelectionDAGISel::runOnFunction(Fn);
106 }
107
Chris Lattnerc961eea2005-11-16 01:54:32 +0000108 virtual const char *getPassName() const {
109 return "X86 DAG->DAG Instruction Selection";
110 }
111
112 /// InstructionSelectBasicBlock - This callback is invoked by
113 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
114 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
115
Evan Cheng8700e142006-01-11 06:09:51 +0000116 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
117
Chris Lattnerc961eea2005-11-16 01:54:32 +0000118// Include the pieces autogenerated from the target description.
119#include "X86GenDAGISel.inc"
120
121 private:
Evan Cheng34167212006-02-09 00:37:58 +0000122 void Select(SDOperand &Result, SDOperand N);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000123
Evan Cheng2486af12006-02-11 02:05:36 +0000124 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM, bool isRoot = true);
Evan Chengec693f72005-12-08 02:01:35 +0000125 bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
126 SDOperand &Index, SDOperand &Disp);
127 bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
128 SDOperand &Index, SDOperand &Disp);
Evan Cheng5e351682006-02-06 06:02:33 +0000129 bool TryFoldLoad(SDOperand P, SDOperand N,
130 SDOperand &Base, SDOperand &Scale,
Evan Cheng0114e942006-01-06 20:36:21 +0000131 SDOperand &Index, SDOperand &Disp);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000132 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
133 /// inline asm expressions.
134 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
135 char ConstraintCode,
136 std::vector<SDOperand> &OutOps,
137 SelectionDAG &DAG);
138
Evan Cheng3649b0e2006-06-02 22:38:37 +0000139 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
140
Evan Chenge5280532005-12-12 21:49:40 +0000141 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
142 SDOperand &Scale, SDOperand &Index,
143 SDOperand &Disp) {
144 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
145 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, MVT::i32) : AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000146 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000147 Index = AM.IndexReg;
148 Disp = AM.GV ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp)
Evan Cheng51a9ed92006-02-25 10:09:08 +0000149 : (AM.CP ?
150 CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp)
151 : getI32Imm(AM.Disp));
Evan Chenge5280532005-12-12 21:49:40 +0000152 }
153
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000154 /// getI8Imm - Return a target constant with the specified value, of type
155 /// i8.
156 inline SDOperand getI8Imm(unsigned Imm) {
157 return CurDAG->getTargetConstant(Imm, MVT::i8);
158 }
159
Chris Lattnerc961eea2005-11-16 01:54:32 +0000160 /// getI16Imm - Return a target constant with the specified value, of type
161 /// i16.
162 inline SDOperand getI16Imm(unsigned Imm) {
163 return CurDAG->getTargetConstant(Imm, MVT::i16);
164 }
165
166 /// getI32Imm - Return a target constant with the specified value, of type
167 /// i32.
168 inline SDOperand getI32Imm(unsigned Imm) {
169 return CurDAG->getTargetConstant(Imm, MVT::i32);
170 }
Evan Chengf597dc72006-02-10 22:24:32 +0000171
Evan Cheng7ccced62006-02-18 00:15:05 +0000172 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
173 /// base register. Return the virtual register that holds this value.
174 SDOperand getGlobalBaseReg();
175
Evan Cheng23addc02006-02-10 22:46:26 +0000176#ifndef NDEBUG
177 unsigned Indent;
178#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000179 };
180}
181
182/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
183/// when it has created a SelectionDAG for us to codegen.
184void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
185 DEBUG(BB->dump());
Chris Lattner92cb0af2006-01-11 01:15:34 +0000186 MachineFunction::iterator FirstMBB = BB;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000187
188 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000189#ifndef NDEBUG
190 DEBUG(std::cerr << "===== Instruction selection begins:\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000191 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000192#endif
Evan Chengba2f0a92006-02-05 06:46:41 +0000193 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Cheng6a3d5a62006-05-25 00:24:28 +0000194 assert(InFlightSet.empty() && "ISel InFlightSet has not been emptied!");
Evan Chengf597dc72006-02-10 22:24:32 +0000195#ifndef NDEBUG
196 DEBUG(std::cerr << "===== Instruction selection ends:\n");
197#endif
Evan Chengfcaa9952005-12-19 22:36:02 +0000198 CodeGenMap.clear();
Evan Chengafe358e2006-05-24 20:46:25 +0000199 HandleMap.clear();
200 ReplaceMap.clear();
Chris Lattnerc961eea2005-11-16 01:54:32 +0000201 DAG.RemoveDeadNodes();
202
203 // Emit machine code to BB.
204 ScheduleAndEmitDAG(DAG);
Chris Lattner92cb0af2006-01-11 01:15:34 +0000205
206 // If we are emitting FP stack code, scan the basic block to determine if this
207 // block defines any FP values. If so, put an FP_REG_KILL instruction before
208 // the terminator of the block.
Evan Cheng559806f2006-01-27 08:10:46 +0000209 if (!Subtarget->hasSSE2()) {
Chris Lattner92cb0af2006-01-11 01:15:34 +0000210 // Note that FP stack instructions *are* used in SSE code when returning
211 // values, but these are not live out of the basic block, so we don't need
212 // an FP_REG_KILL in this case either.
213 bool ContainsFPCode = false;
214
215 // Scan all of the machine instructions in these MBBs, checking for FP
216 // stores.
217 MachineFunction::iterator MBBI = FirstMBB;
218 do {
219 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
220 !ContainsFPCode && I != E; ++I) {
221 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
222 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
223 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
224 RegMap->getRegClass(I->getOperand(0).getReg()) ==
225 X86::RFPRegisterClass) {
226 ContainsFPCode = true;
227 break;
228 }
229 }
230 }
231 } while (!ContainsFPCode && &*(MBBI++) != BB);
232
233 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
234 // a copy of the input value in this block.
235 if (!ContainsFPCode) {
236 // Final check, check LLVM BB's that are successors to the LLVM BB
237 // corresponding to BB for FP PHI nodes.
238 const BasicBlock *LLVMBB = BB->getBasicBlock();
239 const PHINode *PN;
240 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
241 !ContainsFPCode && SI != E; ++SI) {
242 for (BasicBlock::const_iterator II = SI->begin();
243 (PN = dyn_cast<PHINode>(II)); ++II) {
244 if (PN->getType()->isFloatingPoint()) {
245 ContainsFPCode = true;
246 break;
247 }
248 }
249 }
250 }
251
252 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
253 if (ContainsFPCode) {
254 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
255 ++NumFPKill;
256 }
257 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000258}
259
Evan Cheng8700e142006-01-11 06:09:51 +0000260/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
261/// the main function.
Evan Cheng3649b0e2006-06-02 22:38:37 +0000262void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
263 MachineFrameInfo *MFI) {
264 if (Subtarget->TargetType == X86Subtarget::isCygwin)
265 BuildMI(BB, X86::CALLpcrel32, 1).addExternalSymbol("__main");
266
Evan Cheng8700e142006-01-11 06:09:51 +0000267 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
268 int CWFrameIdx = MFI->CreateStackObject(2, 2);
269 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
270
271 // Set the high part to be 64-bit precision.
272 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
273 CWFrameIdx, 1).addImm(2);
274
275 // Reload the modified control word now.
276 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
277}
278
279void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
280 // If this is main, emit special code for main.
281 MachineBasicBlock *BB = MF.begin();
282 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
283 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
284}
285
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000286/// MatchAddress - Add the specified node to the specified addressing mode,
287/// returning true if it cannot be done. This just pattern matches for the
288/// addressing mode
Evan Cheng2486af12006-02-11 02:05:36 +0000289bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM,
290 bool isRoot) {
Evan Cheng51a9ed92006-02-25 10:09:08 +0000291 bool Available = false;
292 // If N has already been selected, reuse the result unless in some very
293 // specific cases.
Evan Cheng2486af12006-02-11 02:05:36 +0000294 std::map<SDOperand, SDOperand>::iterator CGMI= CodeGenMap.find(N.getValue(0));
295 if (CGMI != CodeGenMap.end()) {
Evan Cheng51a9ed92006-02-25 10:09:08 +0000296 Available = true;
Evan Cheng2486af12006-02-11 02:05:36 +0000297 }
298
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000299 switch (N.getOpcode()) {
300 default: break;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000301 case ISD::Constant:
302 AM.Disp += cast<ConstantSDNode>(N)->getValue();
303 return false;
304
305 case X86ISD::Wrapper:
306 // If both base and index components have been picked, we can't fit
307 // the result available in the register in the addressing mode. Duplicate
308 // GlobalAddress or ConstantPool as displacement.
309 if (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val)) {
310 if (ConstantPoolSDNode *CP =
311 dyn_cast<ConstantPoolSDNode>(N.getOperand(0))) {
312 if (AM.CP == 0) {
313 AM.CP = CP->get();
314 AM.Align = CP->getAlignment();
315 AM.Disp += CP->getOffset();
316 return false;
317 }
318 } else if (GlobalAddressSDNode *G =
319 dyn_cast<GlobalAddressSDNode>(N.getOperand(0))) {
320 if (AM.GV == 0) {
321 AM.GV = G->getGlobal();
322 AM.Disp += G->getOffset();
323 return false;
324 }
325 }
326 }
327 break;
328
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000329 case ISD::FrameIndex:
330 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
331 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
332 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
333 return false;
334 }
335 break;
Evan Chengec693f72005-12-08 02:01:35 +0000336
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000337 case ISD::SHL:
Evan Cheng51a9ed92006-02-25 10:09:08 +0000338 if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000339 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
340 unsigned Val = CN->getValue();
341 if (Val == 1 || Val == 2 || Val == 3) {
342 AM.Scale = 1 << Val;
343 SDOperand ShVal = N.Val->getOperand(0);
344
345 // Okay, we know that we have a scale by now. However, if the scaled
346 // value is an add of something and a constant, we can fold the
347 // constant into the disp field here.
348 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
349 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
350 AM.IndexReg = ShVal.Val->getOperand(0);
351 ConstantSDNode *AddVal =
352 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
353 AM.Disp += AddVal->getValue() << Val;
354 } else {
355 AM.IndexReg = ShVal;
356 }
357 return false;
358 }
359 }
360 break;
Evan Chengec693f72005-12-08 02:01:35 +0000361
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000362 case ISD::MUL:
363 // X*[3,5,9] -> X+X*[2,4,8]
Evan Cheng51a9ed92006-02-25 10:09:08 +0000364 if (!Available &&
365 AM.BaseType == X86ISelAddressMode::RegBase &&
366 AM.Base.Reg.Val == 0 &&
367 AM.IndexReg.Val == 0)
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000368 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
369 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
370 AM.Scale = unsigned(CN->getValue())-1;
371
372 SDOperand MulVal = N.Val->getOperand(0);
373 SDOperand Reg;
374
375 // Okay, we know that we have a scale by now. However, if the scaled
376 // value is an add of something and a constant, we can fold the
377 // constant into the disp field here.
378 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
379 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
380 Reg = MulVal.Val->getOperand(0);
381 ConstantSDNode *AddVal =
382 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
383 AM.Disp += AddVal->getValue() * CN->getValue();
384 } else {
385 Reg = N.Val->getOperand(0);
386 }
387
388 AM.IndexReg = AM.Base.Reg = Reg;
389 return false;
390 }
391 break;
392
393 case ISD::ADD: {
Evan Cheng51a9ed92006-02-25 10:09:08 +0000394 if (!Available) {
Evan Cheng2486af12006-02-11 02:05:36 +0000395 X86ISelAddressMode Backup = AM;
396 if (!MatchAddress(N.Val->getOperand(0), AM, false) &&
397 !MatchAddress(N.Val->getOperand(1), AM, false))
398 return false;
399 AM = Backup;
400 if (!MatchAddress(N.Val->getOperand(1), AM, false) &&
401 !MatchAddress(N.Val->getOperand(0), AM, false))
402 return false;
403 AM = Backup;
404 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000405 break;
406 }
Evan Chenge6ad27e2006-05-30 06:59:36 +0000407
408 case ISD::OR: {
409 if (!Available) {
410 X86ISelAddressMode Backup = AM;
411 // Look for (x << c1) | c2 where (c2 < c1)
412 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(0));
413 if (CN && !MatchAddress(N.Val->getOperand(1), AM, false)) {
414 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
415 AM.Disp = CN->getValue();
416 return false;
417 }
418 }
419 AM = Backup;
420 CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1));
421 if (CN && !MatchAddress(N.Val->getOperand(0), AM, false)) {
422 if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) {
423 AM.Disp = CN->getValue();
424 return false;
425 }
426 }
427 AM = Backup;
428 }
429 break;
430 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000431 }
432
433 // Is the base register already occupied?
434 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
435 // If so, check to see if the scale index register is set.
436 if (AM.IndexReg.Val == 0) {
437 AM.IndexReg = N;
438 AM.Scale = 1;
439 return false;
440 }
441
442 // Otherwise, we cannot select it.
443 return true;
444 }
445
446 // Default, generate it as a register.
447 AM.BaseType = X86ISelAddressMode::RegBase;
448 AM.Base.Reg = N;
449 return false;
450}
451
Evan Chengec693f72005-12-08 02:01:35 +0000452/// SelectAddr - returns true if it is able pattern match an addressing mode.
453/// It returns the operands which make up the maximal addressing mode it can
454/// match by reference.
455bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
456 SDOperand &Index, SDOperand &Disp) {
457 X86ISelAddressMode AM;
Evan Cheng8700e142006-01-11 06:09:51 +0000458 if (MatchAddress(N, AM))
459 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000460
Evan Cheng8700e142006-01-11 06:09:51 +0000461 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Cheng7dd281b2006-02-05 05:25:07 +0000462 if (!AM.Base.Reg.Val)
Evan Cheng8700e142006-01-11 06:09:51 +0000463 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
Evan Chengec693f72005-12-08 02:01:35 +0000464 }
Evan Cheng8700e142006-01-11 06:09:51 +0000465
Evan Cheng7dd281b2006-02-05 05:25:07 +0000466 if (!AM.IndexReg.Val)
Evan Cheng8700e142006-01-11 06:09:51 +0000467 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
468
469 getAddressOperands(AM, Base, Scale, Index, Disp);
Evan Cheng51a9ed92006-02-25 10:09:08 +0000470
Evan Cheng8700e142006-01-11 06:09:51 +0000471 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000472}
473
Evan Cheng51a9ed92006-02-25 10:09:08 +0000474/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
475/// mode it matches can be cost effectively emitted as an LEA instruction.
Evan Cheng51a9ed92006-02-25 10:09:08 +0000476bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base,
477 SDOperand &Scale,
478 SDOperand &Index, SDOperand &Disp) {
479 X86ISelAddressMode AM;
480 if (MatchAddress(N, AM))
481 return false;
482
483 unsigned Complexity = 0;
484 if (AM.BaseType == X86ISelAddressMode::RegBase)
485 if (AM.Base.Reg.Val)
486 Complexity = 1;
487 else
488 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
489 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
490 Complexity = 4;
491
492 if (AM.IndexReg.Val)
493 Complexity++;
494 else
495 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
496
Evan Cheng8c03fe42006-02-28 21:13:57 +0000497 if (AM.Scale > 2)
Evan Cheng51a9ed92006-02-25 10:09:08 +0000498 Complexity += 2;
Evan Cheng8c03fe42006-02-28 21:13:57 +0000499 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg
500 else if (AM.Scale > 1)
501 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000502
503 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
504 // to a LEA. This is determined with some expermentation but is by no means
505 // optimal (especially for code size consideration). LEA is nice because of
506 // its three-address nature. Tweak the cost function again when we can run
507 // convertToThreeAddress() at register allocation time.
508 if (AM.GV || AM.CP)
509 Complexity += 2;
510
511 if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val))
512 Complexity++;
513
514 if (Complexity > 2) {
515 getAddressOperands(AM, Base, Scale, Index, Disp);
516 return true;
517 }
518
519 return false;
520}
521
Evan Cheng5e351682006-02-06 06:02:33 +0000522bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N,
523 SDOperand &Base, SDOperand &Scale,
524 SDOperand &Index, SDOperand &Disp) {
525 if (N.getOpcode() == ISD::LOAD &&
526 N.hasOneUse() &&
527 !CodeGenMap.count(N.getValue(0)) &&
528 (P.getNumOperands() == 1 || !isNonImmUse(P.Val, N.Val)))
Evan Cheng0114e942006-01-06 20:36:21 +0000529 return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp);
530 return false;
531}
532
533static bool isRegister0(SDOperand Op) {
Evan Chengec693f72005-12-08 02:01:35 +0000534 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
535 return (R->getReg() == 0);
536 return false;
537}
538
Evan Cheng7ccced62006-02-18 00:15:05 +0000539/// getGlobalBaseReg - Output the instructions required to put the
540/// base address to use for accessing globals into a register.
541///
542SDOperand X86DAGToDAGISel::getGlobalBaseReg() {
543 if (!GlobalBaseReg) {
544 // Insert the set of GlobalBaseReg into the first MBB of the function
545 MachineBasicBlock &FirstMBB = BB->getParent()->front();
546 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
547 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
548 // FIXME: when we get to LP64, we will need to create the appropriate
549 // type of register here.
Evan Cheng069287d2006-05-16 07:21:53 +0000550 GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass);
Evan Cheng7ccced62006-02-18 00:15:05 +0000551 BuildMI(FirstMBB, MBBI, X86::MovePCtoStack, 0);
552 BuildMI(FirstMBB, MBBI, X86::POP32r, 1, GlobalBaseReg);
553 }
554 return CurDAG->getRegister(GlobalBaseReg, MVT::i32);
555}
556
Evan Chengb245d922006-05-20 01:36:52 +0000557static SDNode *FindCallStartFromCall(SDNode *Node) {
558 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
559 assert(Node->getOperand(0).getValueType() == MVT::Other &&
560 "Node doesn't have a token chain argument!");
561 return FindCallStartFromCall(Node->getOperand(0).Val);
562}
563
Evan Cheng34167212006-02-09 00:37:58 +0000564void X86DAGToDAGISel::Select(SDOperand &Result, SDOperand N) {
Evan Chengdef941b2005-12-15 01:02:48 +0000565 SDNode *Node = N.Val;
566 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +0000567 unsigned Opc, MOpc;
568 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +0000569
Evan Chengf597dc72006-02-10 22:24:32 +0000570#ifndef NDEBUG
Evan Cheng23addc02006-02-10 22:46:26 +0000571 DEBUG(std::cerr << std::string(Indent, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +0000572 DEBUG(std::cerr << "Selecting: ");
573 DEBUG(Node->dump(CurDAG));
574 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000575 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000576#endif
577
Evan Cheng34167212006-02-09 00:37:58 +0000578 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) {
579 Result = N;
Evan Chengf597dc72006-02-10 22:24:32 +0000580#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +0000581 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +0000582 DEBUG(std::cerr << "== ");
583 DEBUG(Node->dump(CurDAG));
584 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000585 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000586#endif
Evan Cheng34167212006-02-09 00:37:58 +0000587 return; // Already selected.
588 }
Evan Cheng38262ca2006-01-11 22:15:18 +0000589
590 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(N);
Evan Cheng34167212006-02-09 00:37:58 +0000591 if (CGMI != CodeGenMap.end()) {
592 Result = CGMI->second;
Evan Chengf597dc72006-02-10 22:24:32 +0000593#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +0000594 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +0000595 DEBUG(std::cerr << "== ");
596 DEBUG(Result.Val->dump(CurDAG));
597 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000598 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000599#endif
Evan Cheng34167212006-02-09 00:37:58 +0000600 return;
601 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000602
Evan Cheng0114e942006-01-06 20:36:21 +0000603 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000604 default: break;
Evan Cheng020d2e82006-02-23 20:41:18 +0000605 case X86ISD::GlobalBaseReg:
606 Result = getGlobalBaseReg();
607 return;
608
Evan Cheng51a9ed92006-02-25 10:09:08 +0000609 case ISD::ADD: {
610 // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd
611 // code and is matched first so to prevent it from being turned into
612 // LEA32r X+c.
613 SDOperand N0 = N.getOperand(0);
614 SDOperand N1 = N.getOperand(1);
615 if (N.Val->getValueType(0) == MVT::i32 &&
616 N0.getOpcode() == X86ISD::Wrapper &&
617 N1.getOpcode() == ISD::Constant) {
618 unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue();
619 SDOperand C(0, 0);
620 // TODO: handle ExternalSymbolSDNode.
621 if (GlobalAddressSDNode *G =
622 dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) {
623 C = CurDAG->getTargetGlobalAddress(G->getGlobal(), MVT::i32,
624 G->getOffset() + Offset);
625 } else if (ConstantPoolSDNode *CP =
626 dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) {
627 C = CurDAG->getTargetConstantPool(CP->get(), MVT::i32,
628 CP->getAlignment(),
629 CP->getOffset()+Offset);
630 }
631
632 if (C.Val) {
633 if (N.Val->hasOneUse()) {
634 Result = CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, MVT::i32, C);
635 } else {
636 SDNode *ResNode = CurDAG->getTargetNode(X86::MOV32ri, MVT::i32, C);
637 Result = CodeGenMap[N] = SDOperand(ResNode, 0);
638 }
639 return;
640 }
641 }
642
643 // Other cases are handled by auto-generated code.
644 break;
Evan Chenga0ea0532006-02-23 02:43:52 +0000645 }
Evan Cheng020d2e82006-02-23 20:41:18 +0000646
Evan Cheng0114e942006-01-06 20:36:21 +0000647 case ISD::MULHU:
648 case ISD::MULHS: {
649 if (Opcode == ISD::MULHU)
650 switch (NVT) {
651 default: assert(0 && "Unsupported VT!");
652 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
653 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
654 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
655 }
656 else
657 switch (NVT) {
658 default: assert(0 && "Unsupported VT!");
659 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
660 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
661 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
662 }
663
664 unsigned LoReg, HiReg;
665 switch (NVT) {
666 default: assert(0 && "Unsupported VT!");
667 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
668 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
669 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
670 }
671
672 SDOperand N0 = Node->getOperand(0);
673 SDOperand N1 = Node->getOperand(1);
674
675 bool foldedLoad = false;
676 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng5e351682006-02-06 06:02:33 +0000677 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +0000678 // MULHU and MULHS are commmutative
679 if (!foldedLoad) {
Evan Cheng5e351682006-02-06 06:02:33 +0000680 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +0000681 if (foldedLoad) {
682 N0 = Node->getOperand(1);
683 N1 = Node->getOperand(0);
684 }
685 }
686
Evan Cheng34167212006-02-09 00:37:58 +0000687 SDOperand Chain;
688 if (foldedLoad)
689 Select(Chain, N1.getOperand(0));
690 else
691 Chain = CurDAG->getEntryNode();
Evan Cheng0114e942006-01-06 20:36:21 +0000692
Evan Cheng34167212006-02-09 00:37:58 +0000693 SDOperand InFlag(0, 0);
694 Select(N0, N0);
Evan Cheng0114e942006-01-06 20:36:21 +0000695 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng34167212006-02-09 00:37:58 +0000696 N0, InFlag);
Evan Cheng0114e942006-01-06 20:36:21 +0000697 InFlag = Chain.getValue(1);
698
699 if (foldedLoad) {
Evan Cheng34167212006-02-09 00:37:58 +0000700 Select(Tmp0, Tmp0);
701 Select(Tmp1, Tmp1);
702 Select(Tmp2, Tmp2);
703 Select(Tmp3, Tmp3);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000704 SDNode *CNode =
705 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
706 Tmp2, Tmp3, Chain, InFlag);
707 Chain = SDOperand(CNode, 0);
708 InFlag = SDOperand(CNode, 1);
Evan Cheng0114e942006-01-06 20:36:21 +0000709 } else {
Evan Cheng34167212006-02-09 00:37:58 +0000710 Select(N1, N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000711 InFlag =
712 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng0114e942006-01-06 20:36:21 +0000713 }
714
Evan Cheng34167212006-02-09 00:37:58 +0000715 Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
Evan Cheng0114e942006-01-06 20:36:21 +0000716 CodeGenMap[N.getValue(0)] = Result;
Evan Cheng5e351682006-02-06 06:02:33 +0000717 if (foldedLoad) {
Evan Cheng948f3432006-01-06 23:19:29 +0000718 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
Evan Cheng7d82d602006-02-09 22:12:53 +0000719 AddHandleReplacement(N1.Val, 1, Result.Val, 1);
Evan Cheng5e351682006-02-06 06:02:33 +0000720 }
Evan Cheng34167212006-02-09 00:37:58 +0000721
Evan Chengf597dc72006-02-10 22:24:32 +0000722#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +0000723 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +0000724 DEBUG(std::cerr << "== ");
725 DEBUG(Result.Val->dump(CurDAG));
726 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000727 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000728#endif
Evan Cheng34167212006-02-09 00:37:58 +0000729 return;
Evan Cheng948f3432006-01-06 23:19:29 +0000730 }
Evan Cheng7ccced62006-02-18 00:15:05 +0000731
Evan Cheng948f3432006-01-06 23:19:29 +0000732 case ISD::SDIV:
733 case ISD::UDIV:
734 case ISD::SREM:
735 case ISD::UREM: {
736 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
737 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
738 if (!isSigned)
739 switch (NVT) {
740 default: assert(0 && "Unsupported VT!");
741 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
742 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
743 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
744 }
745 else
746 switch (NVT) {
747 default: assert(0 && "Unsupported VT!");
748 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
749 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
750 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
751 }
752
753 unsigned LoReg, HiReg;
754 unsigned ClrOpcode, SExtOpcode;
755 switch (NVT) {
756 default: assert(0 && "Unsupported VT!");
757 case MVT::i8:
758 LoReg = X86::AL; HiReg = X86::AH;
Evan Chengaede9b92006-06-02 21:20:34 +0000759 ClrOpcode = X86::MOV8r0;
Evan Cheng948f3432006-01-06 23:19:29 +0000760 SExtOpcode = X86::CBW;
761 break;
762 case MVT::i16:
763 LoReg = X86::AX; HiReg = X86::DX;
Evan Chengaede9b92006-06-02 21:20:34 +0000764 ClrOpcode = X86::MOV16r0;
Evan Cheng948f3432006-01-06 23:19:29 +0000765 SExtOpcode = X86::CWD;
766 break;
767 case MVT::i32:
768 LoReg = X86::EAX; HiReg = X86::EDX;
Evan Chengaede9b92006-06-02 21:20:34 +0000769 ClrOpcode = X86::MOV32r0;
Evan Cheng948f3432006-01-06 23:19:29 +0000770 SExtOpcode = X86::CDQ;
771 break;
772 }
773
774 SDOperand N0 = Node->getOperand(0);
775 SDOperand N1 = Node->getOperand(1);
776
777 bool foldedLoad = false;
778 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
Evan Cheng5e351682006-02-06 06:02:33 +0000779 foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng34167212006-02-09 00:37:58 +0000780 SDOperand Chain;
781 if (foldedLoad)
782 Select(Chain, N1.getOperand(0));
783 else
784 Chain = CurDAG->getEntryNode();
Evan Cheng948f3432006-01-06 23:19:29 +0000785
Evan Cheng34167212006-02-09 00:37:58 +0000786 SDOperand InFlag(0, 0);
787 Select(N0, N0);
Evan Cheng948f3432006-01-06 23:19:29 +0000788 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
Evan Cheng34167212006-02-09 00:37:58 +0000789 N0, InFlag);
Evan Cheng948f3432006-01-06 23:19:29 +0000790 InFlag = Chain.getValue(1);
791
792 if (isSigned) {
793 // Sign extend the low part into the high part.
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000794 InFlag =
795 SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +0000796 } else {
797 // Zero out the high part, effectively zero extending the input.
Evan Chengaede9b92006-06-02 21:20:34 +0000798 SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0);
Evan Cheng948f3432006-01-06 23:19:29 +0000799 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT),
800 ClrNode, InFlag);
801 InFlag = Chain.getValue(1);
802 }
803
804 if (foldedLoad) {
Evan Cheng34167212006-02-09 00:37:58 +0000805 Select(Tmp0, Tmp0);
806 Select(Tmp1, Tmp1);
807 Select(Tmp2, Tmp2);
808 Select(Tmp3, Tmp3);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000809 SDNode *CNode =
810 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
811 Tmp2, Tmp3, Chain, InFlag);
812 Chain = SDOperand(CNode, 0);
813 InFlag = SDOperand(CNode, 1);
Evan Cheng948f3432006-01-06 23:19:29 +0000814 } else {
Evan Cheng34167212006-02-09 00:37:58 +0000815 Select(N1, N1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000816 InFlag =
817 SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0);
Evan Cheng948f3432006-01-06 23:19:29 +0000818 }
819
Evan Cheng34167212006-02-09 00:37:58 +0000820 Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg,
821 NVT, InFlag);
Evan Cheng948f3432006-01-06 23:19:29 +0000822 CodeGenMap[N.getValue(0)] = Result;
Evan Cheng5e351682006-02-06 06:02:33 +0000823 if (foldedLoad) {
Evan Cheng948f3432006-01-06 23:19:29 +0000824 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
Evan Cheng7d82d602006-02-09 22:12:53 +0000825 AddHandleReplacement(N1.Val, 1, Result.Val, 1);
Evan Cheng5e351682006-02-06 06:02:33 +0000826 }
Evan Chengf597dc72006-02-10 22:24:32 +0000827
828#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +0000829 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +0000830 DEBUG(std::cerr << "== ");
831 DEBUG(Result.Val->dump(CurDAG));
832 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000833 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000834#endif
Evan Cheng34167212006-02-09 00:37:58 +0000835 return;
Evan Cheng0114e942006-01-06 20:36:21 +0000836 }
Evan Cheng403be7e2006-05-08 08:01:26 +0000837
838 case ISD::TRUNCATE: {
839 if (NVT == MVT::i8) {
840 unsigned Opc2;
841 MVT::ValueType VT;
842 switch (Node->getOperand(0).getValueType()) {
843 default: assert(0 && "Unknown truncate!");
844 case MVT::i16:
845 Opc = X86::MOV16to16_;
846 VT = MVT::i16;
Evan Cheng069287d2006-05-16 07:21:53 +0000847 Opc2 = X86::TRUNC_GR16_GR8;
Evan Cheng403be7e2006-05-08 08:01:26 +0000848 break;
849 case MVT::i32:
850 Opc = X86::MOV32to32_;
851 VT = MVT::i32;
Evan Cheng069287d2006-05-16 07:21:53 +0000852 Opc2 = X86::TRUNC_GR32_GR8;
Evan Cheng403be7e2006-05-08 08:01:26 +0000853 break;
854 }
855
856 SDOperand Tmp0, Tmp1;
857 Select(Tmp0, Node->getOperand(0));
858 Tmp1 = SDOperand(CurDAG->getTargetNode(Opc, VT, Tmp0), 0);
859 Result = CodeGenMap[N] =
860 SDOperand(CurDAG->getTargetNode(Opc2, NVT, Tmp1), 0);
861
862#ifndef NDEBUG
863 DEBUG(std::cerr << std::string(Indent-2, ' '));
864 DEBUG(std::cerr << "== ");
865 DEBUG(Result.Val->dump(CurDAG));
866 DEBUG(std::cerr << "\n");
867 Indent -= 2;
868#endif
869 return;
870 }
Evan Cheng6b2e2542006-05-20 07:44:28 +0000871
872 break;
Evan Cheng403be7e2006-05-08 08:01:26 +0000873 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000874 }
875
Evan Cheng34167212006-02-09 00:37:58 +0000876 SelectCode(Result, N);
Evan Chengf597dc72006-02-10 22:24:32 +0000877#ifndef NDEBUG
Evan Cheng2486af12006-02-11 02:05:36 +0000878 DEBUG(std::cerr << std::string(Indent-2, ' '));
Evan Chengf597dc72006-02-10 22:24:32 +0000879 DEBUG(std::cerr << "=> ");
880 DEBUG(Result.Val->dump(CurDAG));
881 DEBUG(std::cerr << "\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000882 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +0000883#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000884}
885
Chris Lattnerc0bad572006-06-08 18:03:49 +0000886bool X86DAGToDAGISel::
887SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode,
888 std::vector<SDOperand> &OutOps, SelectionDAG &DAG){
889 SDOperand Op0, Op1, Op2, Op3;
890 switch (ConstraintCode) {
891 case 'o': // offsetable ??
892 case 'v': // not offsetable ??
893 default: return true;
894 case 'm': // memory
895 if (!SelectAddr(Op, Op0, Op1, Op2, Op3))
896 return true;
897 break;
898 }
899
900 OutOps.resize(4);
901 Select(OutOps[0], Op0);
902 Select(OutOps[1], Op1);
903 Select(OutOps[2], Op2);
904 Select(OutOps[3], Op3);
905 return false;
906}
907
Chris Lattnerc961eea2005-11-16 01:54:32 +0000908/// createX86ISelDag - This pass converts a legalized DAG into a
909/// X86-specific DAG, ready for instruction scheduling.
910///
Evan Chengc4c62572006-03-13 23:20:37 +0000911FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000912 return new X86DAGToDAGISel(TM);
913}