blob: f4956b44cc537dccdaf66cb7f8c8875012bbd81b [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
15#include "X86.h"
16#include "X86Subtarget.h"
17#include "X86ISelLowering.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000018#include "llvm/GlobalValue.h"
19#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000020#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000022#include "llvm/CodeGen/SelectionDAGISel.h"
23#include "llvm/Target/TargetMachine.h"
24#include "llvm/Support/Debug.h"
25#include "llvm/ADT/Statistic.h"
26using namespace llvm;
27
28//===----------------------------------------------------------------------===//
29// Pattern Matcher Implementation
30//===----------------------------------------------------------------------===//
31
32namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000033 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
34 /// SDOperand's instead of register numbers for the leaves of the matched
35 /// tree.
36 struct X86ISelAddressMode {
37 enum {
38 RegBase,
39 FrameIndexBase,
Evan Chengec693f72005-12-08 02:01:35 +000040 ConstantPoolBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000041 } BaseType;
42
43 struct { // This is really a union, discriminated by BaseType!
44 SDOperand Reg;
45 int FrameIndex;
46 } Base;
47
48 unsigned Scale;
49 SDOperand IndexReg;
50 unsigned Disp;
51 GlobalValue *GV;
52
53 X86ISelAddressMode()
Evan Chengbd3d25c2005-11-30 02:51:20 +000054 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0), GV(0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000055 }
56 };
57}
58
59namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +000060 Statistic<>
61 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
62
63 //===--------------------------------------------------------------------===//
64 /// ISel - X86 specific code to select X86 machine instructions for
65 /// SelectionDAG operations.
66 ///
67 class X86DAGToDAGISel : public SelectionDAGISel {
68 /// ContainsFPCode - Every instruction we select that uses or defines a FP
69 /// register should set this to true.
70 bool ContainsFPCode;
71
72 /// X86Lowering - This object fully describes how to lower LLVM code to an
73 /// X86-specific SelectionDAG.
74 X86TargetLowering X86Lowering;
75
76 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
77 /// make the right decision when generating code for different targets.
78 const X86Subtarget *Subtarget;
79 public:
80 X86DAGToDAGISel(TargetMachine &TM)
81 : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
82 Subtarget = &TM.getSubtarget<X86Subtarget>();
83 }
84
85 virtual const char *getPassName() const {
86 return "X86 DAG->DAG Instruction Selection";
87 }
88
89 /// InstructionSelectBasicBlock - This callback is invoked by
90 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
91 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
92
93// Include the pieces autogenerated from the target description.
94#include "X86GenDAGISel.inc"
95
96 private:
97 SDOperand Select(SDOperand N);
98
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000099 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
Evan Chengec693f72005-12-08 02:01:35 +0000100 bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
101 SDOperand &Index, SDOperand &Disp);
102 bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
103 SDOperand &Index, SDOperand &Disp);
Evan Cheng0114e942006-01-06 20:36:21 +0000104 bool TryFoldLoad(SDOperand N, SDOperand &Base, SDOperand &Scale,
105 SDOperand &Index, SDOperand &Disp);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000106
Evan Chenge5280532005-12-12 21:49:40 +0000107 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
108 SDOperand &Scale, SDOperand &Index,
109 SDOperand &Disp) {
110 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
111 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, MVT::i32) : AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000112 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000113 Index = AM.IndexReg;
114 Disp = AM.GV ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp)
115 : getI32Imm(AM.Disp);
116 }
117
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000118 /// getI8Imm - Return a target constant with the specified value, of type
119 /// i8.
120 inline SDOperand getI8Imm(unsigned Imm) {
121 return CurDAG->getTargetConstant(Imm, MVT::i8);
122 }
123
Chris Lattnerc961eea2005-11-16 01:54:32 +0000124 /// getI16Imm - Return a target constant with the specified value, of type
125 /// i16.
126 inline SDOperand getI16Imm(unsigned Imm) {
127 return CurDAG->getTargetConstant(Imm, MVT::i16);
128 }
129
130 /// getI32Imm - Return a target constant with the specified value, of type
131 /// i32.
132 inline SDOperand getI32Imm(unsigned Imm) {
133 return CurDAG->getTargetConstant(Imm, MVT::i32);
134 }
135 };
136}
137
138/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
139/// when it has created a SelectionDAG for us to codegen.
140void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
141 DEBUG(BB->dump());
142
143 // Codegen the basic block.
144 DAG.setRoot(Select(DAG.getRoot()));
Evan Chengfcaa9952005-12-19 22:36:02 +0000145 CodeGenMap.clear();
Chris Lattnerc961eea2005-11-16 01:54:32 +0000146 DAG.RemoveDeadNodes();
147
148 // Emit machine code to BB.
149 ScheduleAndEmitDAG(DAG);
150}
151
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000152/// FIXME: copied from X86ISelPattern.cpp
153/// MatchAddress - Add the specified node to the specified addressing mode,
154/// returning true if it cannot be done. This just pattern matches for the
155/// addressing mode
156bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
157 switch (N.getOpcode()) {
158 default: break;
159 case ISD::FrameIndex:
160 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
161 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
162 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
163 return false;
164 }
165 break;
Evan Chengec693f72005-12-08 02:01:35 +0000166
167 case ISD::ConstantPool:
168 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
169 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N)) {
170 AM.BaseType = X86ISelAddressMode::ConstantPoolBase;
171 AM.Base.Reg = CurDAG->getTargetConstantPool(CP->get(), MVT::i32);
172 return false;
173 }
174 }
175 break;
176
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000177 case ISD::GlobalAddress:
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000178 case ISD::TargetGlobalAddress:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000179 if (AM.GV == 0) {
Evan Chengb077b842005-12-21 02:39:21 +0000180 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Evan Chengbdce7b42005-12-17 09:13:43 +0000181 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000182 }
183 break;
Evan Chengec693f72005-12-08 02:01:35 +0000184
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000185 case ISD::Constant:
186 AM.Disp += cast<ConstantSDNode>(N)->getValue();
187 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000188
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000189 case ISD::SHL:
190 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
191 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
192 unsigned Val = CN->getValue();
193 if (Val == 1 || Val == 2 || Val == 3) {
194 AM.Scale = 1 << Val;
195 SDOperand ShVal = N.Val->getOperand(0);
196
197 // Okay, we know that we have a scale by now. However, if the scaled
198 // value is an add of something and a constant, we can fold the
199 // constant into the disp field here.
200 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
201 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
202 AM.IndexReg = ShVal.Val->getOperand(0);
203 ConstantSDNode *AddVal =
204 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
205 AM.Disp += AddVal->getValue() << Val;
206 } else {
207 AM.IndexReg = ShVal;
208 }
209 return false;
210 }
211 }
212 break;
Evan Chengec693f72005-12-08 02:01:35 +0000213
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000214 case ISD::MUL:
215 // X*[3,5,9] -> X+X*[2,4,8]
216 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
217 AM.Base.Reg.Val == 0)
218 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
219 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
220 AM.Scale = unsigned(CN->getValue())-1;
221
222 SDOperand MulVal = N.Val->getOperand(0);
223 SDOperand Reg;
224
225 // Okay, we know that we have a scale by now. However, if the scaled
226 // value is an add of something and a constant, we can fold the
227 // constant into the disp field here.
228 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
229 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
230 Reg = MulVal.Val->getOperand(0);
231 ConstantSDNode *AddVal =
232 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
233 AM.Disp += AddVal->getValue() * CN->getValue();
234 } else {
235 Reg = N.Val->getOperand(0);
236 }
237
238 AM.IndexReg = AM.Base.Reg = Reg;
239 return false;
240 }
241 break;
242
243 case ISD::ADD: {
244 X86ISelAddressMode Backup = AM;
245 if (!MatchAddress(N.Val->getOperand(0), AM) &&
246 !MatchAddress(N.Val->getOperand(1), AM))
247 return false;
248 AM = Backup;
249 if (!MatchAddress(N.Val->getOperand(1), AM) &&
250 !MatchAddress(N.Val->getOperand(0), AM))
251 return false;
252 AM = Backup;
253 break;
254 }
255 }
256
257 // Is the base register already occupied?
258 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
259 // If so, check to see if the scale index register is set.
260 if (AM.IndexReg.Val == 0) {
261 AM.IndexReg = N;
262 AM.Scale = 1;
263 return false;
264 }
265
266 // Otherwise, we cannot select it.
267 return true;
268 }
269
270 // Default, generate it as a register.
271 AM.BaseType = X86ISelAddressMode::RegBase;
272 AM.Base.Reg = N;
273 return false;
274}
275
Evan Chengec693f72005-12-08 02:01:35 +0000276/// SelectAddr - returns true if it is able pattern match an addressing mode.
277/// It returns the operands which make up the maximal addressing mode it can
278/// match by reference.
279bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
280 SDOperand &Index, SDOperand &Disp) {
281 X86ISelAddressMode AM;
282 if (!MatchAddress(N, AM)) {
283 if (AM.BaseType == X86ISelAddressMode::RegBase) {
284 if (AM.Base.Reg.Val)
285 AM.Base.Reg = Select(AM.Base.Reg);
286 else
287 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
288 }
289 if (AM.IndexReg.Val)
290 AM.IndexReg = Select(AM.IndexReg);
291 else
292 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
293
Evan Chenge5280532005-12-12 21:49:40 +0000294 getAddressOperands(AM, Base, Scale, Index, Disp);
Evan Chengec693f72005-12-08 02:01:35 +0000295 return true;
296 }
297 return false;
298}
299
Evan Cheng0114e942006-01-06 20:36:21 +0000300bool X86DAGToDAGISel::TryFoldLoad(SDOperand N, SDOperand &Base,
301 SDOperand &Scale, SDOperand &Index,
302 SDOperand &Disp) {
303 if (N.getOpcode() == ISD::LOAD && N.hasOneUse() &&
Evan Cheng948f3432006-01-06 23:19:29 +0000304 CodeGenMap.count(N.getValue(1)) == 0)
Evan Cheng0114e942006-01-06 20:36:21 +0000305 return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp);
306 return false;
307}
308
309static bool isRegister0(SDOperand Op) {
Evan Chengec693f72005-12-08 02:01:35 +0000310 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
311 return (R->getReg() == 0);
312 return false;
313}
314
315/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
316/// mode it matches can be cost effectively emitted as an LEA instruction.
317/// For X86, it always is unless it's just a (Reg + const).
Chris Lattnera2b694c2006-01-11 00:46:55 +0000318bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base,
319 SDOperand &Scale,
Evan Chengec693f72005-12-08 02:01:35 +0000320 SDOperand &Index, SDOperand &Disp) {
Evan Chenge5280532005-12-12 21:49:40 +0000321 X86ISelAddressMode AM;
322 if (!MatchAddress(N, AM)) {
323 bool SelectBase = false;
324 bool SelectIndex = false;
325 bool Check = false;
326 if (AM.BaseType == X86ISelAddressMode::RegBase) {
327 if (AM.Base.Reg.Val) {
328 Check = true;
329 SelectBase = true;
Evan Chengec693f72005-12-08 02:01:35 +0000330 } else {
Evan Chenge5280532005-12-12 21:49:40 +0000331 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
Evan Chengec693f72005-12-08 02:01:35 +0000332 }
Evan Chengec693f72005-12-08 02:01:35 +0000333 }
Evan Chenge5280532005-12-12 21:49:40 +0000334
335 if (AM.IndexReg.Val) {
336 SelectIndex = true;
337 } else {
338 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
339 }
340
341 if (Check) {
342 unsigned Complexity = 0;
343 if (AM.Scale > 1)
344 Complexity++;
345 if (SelectIndex)
346 Complexity++;
347 if (AM.GV)
348 Complexity++;
349 else if (AM.Disp > 1)
350 Complexity++;
351 if (Complexity <= 1)
352 return false;
353 }
354
355 if (SelectBase)
356 AM.Base.Reg = Select(AM.Base.Reg);
357 if (SelectIndex)
358 AM.IndexReg = Select(AM.IndexReg);
359
360 getAddressOperands(AM, Base, Scale, Index, Disp);
Evan Chengec693f72005-12-08 02:01:35 +0000361 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000362 }
Evan Chenge5280532005-12-12 21:49:40 +0000363 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000364}
365
Evan Chengdef941b2005-12-15 01:02:48 +0000366SDOperand X86DAGToDAGISel::Select(SDOperand N) {
367 SDNode *Node = N.Val;
368 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +0000369 unsigned Opc, MOpc;
370 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +0000371
Evan Cheng0114e942006-01-06 20:36:21 +0000372 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER)
Evan Chengdef941b2005-12-15 01:02:48 +0000373 return N; // Already selected.
Chris Lattnerc961eea2005-11-16 01:54:32 +0000374
Evan Cheng0114e942006-01-06 20:36:21 +0000375 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000376 default: break;
Evan Cheng0114e942006-01-06 20:36:21 +0000377 case ISD::MULHU:
378 case ISD::MULHS: {
379 if (Opcode == ISD::MULHU)
380 switch (NVT) {
381 default: assert(0 && "Unsupported VT!");
382 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
383 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
384 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
385 }
386 else
387 switch (NVT) {
388 default: assert(0 && "Unsupported VT!");
389 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
390 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
391 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
392 }
393
394 unsigned LoReg, HiReg;
395 switch (NVT) {
396 default: assert(0 && "Unsupported VT!");
397 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
398 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
399 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
400 }
401
402 SDOperand N0 = Node->getOperand(0);
403 SDOperand N1 = Node->getOperand(1);
404
405 bool foldedLoad = false;
406 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
407 foldedLoad = TryFoldLoad(N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +0000408 // MULHU and MULHS are commmutative
409 if (!foldedLoad) {
410 foldedLoad = TryFoldLoad(N0, Tmp0, Tmp1, Tmp2, Tmp3);
411 if (foldedLoad) {
412 N0 = Node->getOperand(1);
413 N1 = Node->getOperand(0);
414 }
415 }
416
Evan Cheng0114e942006-01-06 20:36:21 +0000417 SDOperand Chain = foldedLoad ? Select(N1.getOperand(0))
418 : CurDAG->getEntryNode();
419
420 SDOperand InFlag;
421 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
422 Select(N0), InFlag);
423 InFlag = Chain.getValue(1);
424
425 if (foldedLoad) {
426 Chain = CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
427 Tmp2, Tmp3, Chain, InFlag);
428 InFlag = Chain.getValue(1);
429 } else {
430 InFlag = CurDAG->getTargetNode(Opc, MVT::Flag, Select(N1), InFlag);
431 }
432
433 SDOperand Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
434 CodeGenMap[N.getValue(0)] = Result;
Evan Cheng948f3432006-01-06 23:19:29 +0000435 if (foldedLoad)
436 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
437 return Result;
438 }
439
440 case ISD::SDIV:
441 case ISD::UDIV:
442 case ISD::SREM:
443 case ISD::UREM: {
444 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
445 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
446 if (!isSigned)
447 switch (NVT) {
448 default: assert(0 && "Unsupported VT!");
449 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
450 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
451 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
452 }
453 else
454 switch (NVT) {
455 default: assert(0 && "Unsupported VT!");
456 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
457 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
458 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
459 }
460
461 unsigned LoReg, HiReg;
462 unsigned ClrOpcode, SExtOpcode;
463 switch (NVT) {
464 default: assert(0 && "Unsupported VT!");
465 case MVT::i8:
466 LoReg = X86::AL; HiReg = X86::AH;
467 ClrOpcode = X86::MOV8ri;
468 SExtOpcode = X86::CBW;
469 break;
470 case MVT::i16:
471 LoReg = X86::AX; HiReg = X86::DX;
472 ClrOpcode = X86::MOV16ri;
473 SExtOpcode = X86::CWD;
474 break;
475 case MVT::i32:
476 LoReg = X86::EAX; HiReg = X86::EDX;
477 ClrOpcode = X86::MOV32ri;
478 SExtOpcode = X86::CDQ;
479 break;
480 }
481
482 SDOperand N0 = Node->getOperand(0);
483 SDOperand N1 = Node->getOperand(1);
484
485 bool foldedLoad = false;
486 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
487 foldedLoad = TryFoldLoad(N1, Tmp0, Tmp1, Tmp2, Tmp3);
488 SDOperand Chain = foldedLoad ? Select(N1.getOperand(0))
489 : CurDAG->getEntryNode();
490
491 SDOperand InFlag;
492 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
493 Select(N0), InFlag);
494 InFlag = Chain.getValue(1);
495
496 if (isSigned) {
497 // Sign extend the low part into the high part.
498 InFlag = CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag);
499 } else {
500 // Zero out the high part, effectively zero extending the input.
501 SDOperand ClrNode =
502 CurDAG->getTargetNode(ClrOpcode, NVT,
503 CurDAG->getTargetConstant(0, NVT));
504 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT),
505 ClrNode, InFlag);
506 InFlag = Chain.getValue(1);
507 }
508
509 if (foldedLoad) {
510 Chain = CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
511 Tmp2, Tmp3, Chain, InFlag);
512 InFlag = Chain.getValue(1);
513 } else {
514 InFlag = CurDAG->getTargetNode(Opc, MVT::Flag, Select(N1), InFlag);
515 }
516
517 SDOperand Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg,
518 NVT, InFlag);
519 CodeGenMap[N.getValue(0)] = Result;
520 if (foldedLoad)
521 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
522 return Result;
Evan Cheng0114e942006-01-06 20:36:21 +0000523 }
Evan Chengbd3d25c2005-11-30 02:51:20 +0000524
Evan Cheng45f37bc2005-12-17 02:02:50 +0000525 case ISD::TRUNCATE: {
526 unsigned Reg;
527 MVT::ValueType VT;
528 switch (Node->getOperand(0).getValueType()) {
529 default: assert(0 && "Unknown truncate!");
530 case MVT::i16: Reg = X86::AX; Opc = X86::MOV16rr; VT = MVT::i16; break;
531 case MVT::i32: Reg = X86::EAX; Opc = X86::MOV32rr; VT = MVT::i32; break;
532 }
533 SDOperand Tmp0 = Select(Node->getOperand(0));
534 SDOperand Tmp1 = CurDAG->getTargetNode(Opc, VT, Tmp0);
535 SDOperand InFlag = SDOperand(0,0);
536 SDOperand Result = CurDAG->getCopyToReg(CurDAG->getEntryNode(),
537 Reg, Tmp1, InFlag).getValue(1);
538 SDOperand Chain = Result.getValue(0);
539 InFlag = Result.getValue(1);
540
541 switch (NVT) {
542 default: assert(0 && "Unknown truncate!");
543 case MVT::i8: Reg = X86::AL; Opc = X86::MOV8rr; VT = MVT::i8; break;
544 case MVT::i16: Reg = X86::AX; Opc = X86::MOV16rr; VT = MVT::i16; break;
545 }
546
547 Result = CurDAG->getCopyFromReg(Chain,
548 Reg, VT, InFlag);
Evan Cheng0114e942006-01-06 20:36:21 +0000549 if (N.Val->hasOneUse())
550 return CurDAG->SelectNodeTo(N.Val, Opc, VT, Result);
551 else
552 return CodeGenMap[N] = CurDAG->getTargetNode(Opc, VT, Result);
Evan Cheng45f37bc2005-12-17 02:02:50 +0000553 break;
554 }
Evan Chengaaca22c2006-01-10 20:26:56 +0000555
556 case X86ISD::FP_TO_INT16_IN_MEM:
557 case X86ISD::FP_TO_INT32_IN_MEM:
558 case X86ISD::FP_TO_INT64_IN_MEM: {
559 assert(N.getOperand(1).getValueType() == MVT::f64);
560
561 // Change the floating point control register to use "round towards zero"
562 // mode when truncating to an integer value.
563 MachineFunction &MF = CurDAG->getMachineFunction();
564 int CWFI = MF.getFrameInfo()->CreateStackObject(2, 2);
565 SDOperand CWSlot = CurDAG->getFrameIndex(CWFI, MVT::i32);
566 SDOperand Base, Scale, Index, Disp;
567 (void)SelectAddr(CWSlot, Base, Scale, Index, Disp);
568 SDOperand Chain = N.getOperand(0);
569
570 // Save the control word.
571 Chain = CurDAG->getTargetNode(X86::FNSTCW16m, MVT::Other,
572 Base, Scale, Index, Disp, Chain);
573
574 // Load the old value of the high byte of the control word.
575 SDOperand OldCW =
576 CurDAG->getTargetNode(X86::MOV16rm, MVT::i16, MVT::Other,
577 Base, Scale, Index, Disp, Chain);
578 Chain = OldCW.getValue(1);
579
580 // Set the high part to be round to zero...
581 Chain = CurDAG->getTargetNode(X86::MOV16mi, MVT::Other,
582 Base, Scale, Index, Disp,
583 CurDAG->getConstant(0xC7F, MVT::i16),
584 Chain);
585
586 // Reload the modified control word now...
587 Chain = CurDAG->getTargetNode(X86::FLDCW16m, MVT::Other,
588 Base, Scale, Index, Disp, Chain);
589
590 // Restore the memory image of control word to original value
591 Chain = CurDAG->getTargetNode(X86::MOV16mr, MVT::Other,
592 Base, Scale, Index, Disp, OldCW, Chain);
593
594 switch (Opcode) {
595 case X86ISD::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
596 case X86ISD::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
597 case X86ISD::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
598 }
599
600 SDOperand N1 = Select(N.getOperand(1));
601 SDOperand Base2, Scale2, Index2, Disp2;
602 (void)SelectAddr(N.getOperand(2), Base2, Scale2, Index2, Disp2);
603 Chain = CurDAG->getTargetNode(Opc, MVT::Other,
604 Base2, Scale2, Index2, Disp2, N1, Chain);
605
606 // Reload the modified control word now...
607 CodeGenMap[N] =
608 Chain = CurDAG->getTargetNode(X86::FLDCW16m, MVT::Other,
609 Base, Scale, Index, Disp, Chain);
610 return Chain;
611 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000612 }
613
Evan Chengdef941b2005-12-15 01:02:48 +0000614 return SelectCode(N);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000615}
616
617/// createX86ISelDag - This pass converts a legalized DAG into a
618/// X86-specific DAG, ready for instruction scheduling.
619///
620FunctionPass *llvm::createX86ISelDag(TargetMachine &TM) {
621 return new X86DAGToDAGISel(TM);
622}