blob: e4cd61191ccadb41fbde9d69d871b5ed9e1d0950 [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).
318bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
319 SDOperand &Index, SDOperand &Disp) {
Evan Chenge5280532005-12-12 21:49:40 +0000320 X86ISelAddressMode AM;
321 if (!MatchAddress(N, AM)) {
322 bool SelectBase = false;
323 bool SelectIndex = false;
324 bool Check = false;
325 if (AM.BaseType == X86ISelAddressMode::RegBase) {
326 if (AM.Base.Reg.Val) {
327 Check = true;
328 SelectBase = true;
Evan Chengec693f72005-12-08 02:01:35 +0000329 } else {
Evan Chenge5280532005-12-12 21:49:40 +0000330 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
Evan Chengec693f72005-12-08 02:01:35 +0000331 }
Evan Chengec693f72005-12-08 02:01:35 +0000332 }
Evan Chenge5280532005-12-12 21:49:40 +0000333
334 if (AM.IndexReg.Val) {
335 SelectIndex = true;
336 } else {
337 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
338 }
339
340 if (Check) {
341 unsigned Complexity = 0;
342 if (AM.Scale > 1)
343 Complexity++;
344 if (SelectIndex)
345 Complexity++;
346 if (AM.GV)
347 Complexity++;
348 else if (AM.Disp > 1)
349 Complexity++;
350 if (Complexity <= 1)
351 return false;
352 }
353
354 if (SelectBase)
355 AM.Base.Reg = Select(AM.Base.Reg);
356 if (SelectIndex)
357 AM.IndexReg = Select(AM.IndexReg);
358
359 getAddressOperands(AM, Base, Scale, Index, Disp);
Evan Chengec693f72005-12-08 02:01:35 +0000360 return true;
Evan Chengec693f72005-12-08 02:01:35 +0000361 }
Evan Chenge5280532005-12-12 21:49:40 +0000362 return false;
Evan Chengec693f72005-12-08 02:01:35 +0000363}
364
Evan Chengdef941b2005-12-15 01:02:48 +0000365SDOperand X86DAGToDAGISel::Select(SDOperand N) {
366 SDNode *Node = N.Val;
367 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +0000368 unsigned Opc, MOpc;
369 unsigned Opcode = Node->getOpcode();
Chris Lattnerc961eea2005-11-16 01:54:32 +0000370
Evan Cheng0114e942006-01-06 20:36:21 +0000371 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER)
Evan Chengdef941b2005-12-15 01:02:48 +0000372 return N; // Already selected.
Chris Lattnerc961eea2005-11-16 01:54:32 +0000373
Evan Cheng0114e942006-01-06 20:36:21 +0000374 switch (Opcode) {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000375 default: break;
Evan Cheng0114e942006-01-06 20:36:21 +0000376 case ISD::MULHU:
377 case ISD::MULHS: {
378 if (Opcode == ISD::MULHU)
379 switch (NVT) {
380 default: assert(0 && "Unsupported VT!");
381 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
382 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
383 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
384 }
385 else
386 switch (NVT) {
387 default: assert(0 && "Unsupported VT!");
388 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
389 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
390 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
391 }
392
393 unsigned LoReg, HiReg;
394 switch (NVT) {
395 default: assert(0 && "Unsupported VT!");
396 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
397 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
398 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
399 }
400
401 SDOperand N0 = Node->getOperand(0);
402 SDOperand N1 = Node->getOperand(1);
403
404 bool foldedLoad = false;
405 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
406 foldedLoad = TryFoldLoad(N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng948f3432006-01-06 23:19:29 +0000407 // MULHU and MULHS are commmutative
408 if (!foldedLoad) {
409 foldedLoad = TryFoldLoad(N0, Tmp0, Tmp1, Tmp2, Tmp3);
410 if (foldedLoad) {
411 N0 = Node->getOperand(1);
412 N1 = Node->getOperand(0);
413 }
414 }
415
Evan Cheng0114e942006-01-06 20:36:21 +0000416 SDOperand Chain = foldedLoad ? Select(N1.getOperand(0))
417 : CurDAG->getEntryNode();
418
419 SDOperand InFlag;
420 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
421 Select(N0), InFlag);
422 InFlag = Chain.getValue(1);
423
424 if (foldedLoad) {
425 Chain = CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
426 Tmp2, Tmp3, Chain, InFlag);
427 InFlag = Chain.getValue(1);
428 } else {
429 InFlag = CurDAG->getTargetNode(Opc, MVT::Flag, Select(N1), InFlag);
430 }
431
432 SDOperand Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
433 CodeGenMap[N.getValue(0)] = Result;
Evan Cheng948f3432006-01-06 23:19:29 +0000434 if (foldedLoad)
435 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
436 return Result;
437 }
438
439 case ISD::SDIV:
440 case ISD::UDIV:
441 case ISD::SREM:
442 case ISD::UREM: {
443 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
444 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
445 if (!isSigned)
446 switch (NVT) {
447 default: assert(0 && "Unsupported VT!");
448 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
449 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
450 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
451 }
452 else
453 switch (NVT) {
454 default: assert(0 && "Unsupported VT!");
455 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
456 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
457 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
458 }
459
460 unsigned LoReg, HiReg;
461 unsigned ClrOpcode, SExtOpcode;
462 switch (NVT) {
463 default: assert(0 && "Unsupported VT!");
464 case MVT::i8:
465 LoReg = X86::AL; HiReg = X86::AH;
466 ClrOpcode = X86::MOV8ri;
467 SExtOpcode = X86::CBW;
468 break;
469 case MVT::i16:
470 LoReg = X86::AX; HiReg = X86::DX;
471 ClrOpcode = X86::MOV16ri;
472 SExtOpcode = X86::CWD;
473 break;
474 case MVT::i32:
475 LoReg = X86::EAX; HiReg = X86::EDX;
476 ClrOpcode = X86::MOV32ri;
477 SExtOpcode = X86::CDQ;
478 break;
479 }
480
481 SDOperand N0 = Node->getOperand(0);
482 SDOperand N1 = Node->getOperand(1);
483
484 bool foldedLoad = false;
485 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
486 foldedLoad = TryFoldLoad(N1, Tmp0, Tmp1, Tmp2, Tmp3);
487 SDOperand Chain = foldedLoad ? Select(N1.getOperand(0))
488 : CurDAG->getEntryNode();
489
490 SDOperand InFlag;
491 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
492 Select(N0), InFlag);
493 InFlag = Chain.getValue(1);
494
495 if (isSigned) {
496 // Sign extend the low part into the high part.
497 InFlag = CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag);
498 } else {
499 // Zero out the high part, effectively zero extending the input.
500 SDOperand ClrNode =
501 CurDAG->getTargetNode(ClrOpcode, NVT,
502 CurDAG->getTargetConstant(0, NVT));
503 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT),
504 ClrNode, InFlag);
505 InFlag = Chain.getValue(1);
506 }
507
508 if (foldedLoad) {
509 Chain = CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
510 Tmp2, Tmp3, Chain, InFlag);
511 InFlag = Chain.getValue(1);
512 } else {
513 InFlag = CurDAG->getTargetNode(Opc, MVT::Flag, Select(N1), InFlag);
514 }
515
516 SDOperand Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg,
517 NVT, InFlag);
518 CodeGenMap[N.getValue(0)] = Result;
519 if (foldedLoad)
520 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
521 return Result;
Evan Cheng0114e942006-01-06 20:36:21 +0000522 }
Evan Chengbd3d25c2005-11-30 02:51:20 +0000523
Evan Cheng45f37bc2005-12-17 02:02:50 +0000524 case ISD::TRUNCATE: {
525 unsigned Reg;
526 MVT::ValueType VT;
527 switch (Node->getOperand(0).getValueType()) {
528 default: assert(0 && "Unknown truncate!");
529 case MVT::i16: Reg = X86::AX; Opc = X86::MOV16rr; VT = MVT::i16; break;
530 case MVT::i32: Reg = X86::EAX; Opc = X86::MOV32rr; VT = MVT::i32; break;
531 }
532 SDOperand Tmp0 = Select(Node->getOperand(0));
533 SDOperand Tmp1 = CurDAG->getTargetNode(Opc, VT, Tmp0);
534 SDOperand InFlag = SDOperand(0,0);
535 SDOperand Result = CurDAG->getCopyToReg(CurDAG->getEntryNode(),
536 Reg, Tmp1, InFlag).getValue(1);
537 SDOperand Chain = Result.getValue(0);
538 InFlag = Result.getValue(1);
539
540 switch (NVT) {
541 default: assert(0 && "Unknown truncate!");
542 case MVT::i8: Reg = X86::AL; Opc = X86::MOV8rr; VT = MVT::i8; break;
543 case MVT::i16: Reg = X86::AX; Opc = X86::MOV16rr; VT = MVT::i16; break;
544 }
545
546 Result = CurDAG->getCopyFromReg(Chain,
547 Reg, VT, InFlag);
Evan Cheng0114e942006-01-06 20:36:21 +0000548 if (N.Val->hasOneUse())
549 return CurDAG->SelectNodeTo(N.Val, Opc, VT, Result);
550 else
551 return CodeGenMap[N] = CurDAG->getTargetNode(Opc, VT, Result);
Evan Cheng45f37bc2005-12-17 02:02:50 +0000552 break;
553 }
Evan Chengaaca22c2006-01-10 20:26:56 +0000554
555 case X86ISD::FP_TO_INT16_IN_MEM:
556 case X86ISD::FP_TO_INT32_IN_MEM:
557 case X86ISD::FP_TO_INT64_IN_MEM: {
558 assert(N.getOperand(1).getValueType() == MVT::f64);
559
560 // Change the floating point control register to use "round towards zero"
561 // mode when truncating to an integer value.
562 MachineFunction &MF = CurDAG->getMachineFunction();
563 int CWFI = MF.getFrameInfo()->CreateStackObject(2, 2);
564 SDOperand CWSlot = CurDAG->getFrameIndex(CWFI, MVT::i32);
565 SDOperand Base, Scale, Index, Disp;
566 (void)SelectAddr(CWSlot, Base, Scale, Index, Disp);
567 SDOperand Chain = N.getOperand(0);
568
569 // Save the control word.
570 Chain = CurDAG->getTargetNode(X86::FNSTCW16m, MVT::Other,
571 Base, Scale, Index, Disp, Chain);
572
573 // Load the old value of the high byte of the control word.
574 SDOperand OldCW =
575 CurDAG->getTargetNode(X86::MOV16rm, MVT::i16, MVT::Other,
576 Base, Scale, Index, Disp, Chain);
577 Chain = OldCW.getValue(1);
578
579 // Set the high part to be round to zero...
580 Chain = CurDAG->getTargetNode(X86::MOV16mi, MVT::Other,
581 Base, Scale, Index, Disp,
582 CurDAG->getConstant(0xC7F, MVT::i16),
583 Chain);
584
585 // Reload the modified control word now...
586 Chain = CurDAG->getTargetNode(X86::FLDCW16m, MVT::Other,
587 Base, Scale, Index, Disp, Chain);
588
589 // Restore the memory image of control word to original value
590 Chain = CurDAG->getTargetNode(X86::MOV16mr, MVT::Other,
591 Base, Scale, Index, Disp, OldCW, Chain);
592
593 switch (Opcode) {
594 case X86ISD::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
595 case X86ISD::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
596 case X86ISD::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
597 }
598
599 SDOperand N1 = Select(N.getOperand(1));
600 SDOperand Base2, Scale2, Index2, Disp2;
601 (void)SelectAddr(N.getOperand(2), Base2, Scale2, Index2, Disp2);
602 Chain = CurDAG->getTargetNode(Opc, MVT::Other,
603 Base2, Scale2, Index2, Disp2, N1, Chain);
604
605 // Reload the modified control word now...
606 CodeGenMap[N] =
607 Chain = CurDAG->getTargetNode(X86::FLDCW16m, MVT::Other,
608 Base, Scale, Index, Disp, Chain);
609 return Chain;
610 }
Chris Lattnerc961eea2005-11-16 01:54:32 +0000611 }
612
Evan Chengdef941b2005-12-15 01:02:48 +0000613 return SelectCode(N);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000614}
615
616/// createX86ISelDag - This pass converts a legalized DAG into a
617/// X86-specific DAG, ready for instruction scheduling.
618///
619FunctionPass *llvm::createX86ISelDag(TargetMachine &TM) {
620 return new X86DAGToDAGISel(TM);
621}