blob: c6d4e6727e7e4a783d2d4771e6aa0f32860e3576 [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
15#include "X86.h"
Chris Lattner7c551262006-01-11 01:15:34 +000016#include "X86RegisterInfo.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000017#include "X86Subtarget.h"
18#include "X86ISelLowering.h"
Chris Lattner3f0f71b2005-11-19 02:11:08 +000019#include "llvm/GlobalValue.h"
Chris Lattner7c551262006-01-11 01:15:34 +000020#include "llvm/Instructions.h"
21#include "llvm/Support/CFG.h"
Chris Lattner3f0f71b2005-11-19 02:11:08 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000023#include "llvm/CodeGen/MachineFunction.h"
Evan Cheng73a1ad92006-01-10 20:26:56 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner7c551262006-01-11 01:15:34 +000025#include "llvm/CodeGen/MachineInstrBuilder.h"
26#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000027#include "llvm/CodeGen/SelectionDAGISel.h"
28#include "llvm/Target/TargetMachine.h"
29#include "llvm/Support/Debug.h"
30#include "llvm/ADT/Statistic.h"
31using namespace llvm;
32
33//===----------------------------------------------------------------------===//
34// Pattern Matcher Implementation
35//===----------------------------------------------------------------------===//
36
37namespace {
Chris Lattner3f0f71b2005-11-19 02:11:08 +000038 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
39 /// SDOperand's instead of register numbers for the leaves of the matched
40 /// tree.
41 struct X86ISelAddressMode {
42 enum {
43 RegBase,
44 FrameIndexBase,
Evan Chengc9fab312005-12-08 02:01:35 +000045 ConstantPoolBase
Chris Lattner3f0f71b2005-11-19 02:11:08 +000046 } BaseType;
47
48 struct { // This is really a union, discriminated by BaseType!
49 SDOperand Reg;
50 int FrameIndex;
51 } Base;
52
53 unsigned Scale;
54 SDOperand IndexReg;
55 unsigned Disp;
56 GlobalValue *GV;
57
58 X86ISelAddressMode()
Evan Cheng4eb7af92005-11-30 02:51:20 +000059 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0), GV(0) {
Chris Lattner3f0f71b2005-11-19 02:11:08 +000060 }
61 };
62}
63
64namespace {
Chris Lattner655e7df2005-11-16 01:54:32 +000065 Statistic<>
66 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
67
68 //===--------------------------------------------------------------------===//
69 /// ISel - X86 specific code to select X86 machine instructions for
70 /// SelectionDAG operations.
71 ///
72 class X86DAGToDAGISel : public SelectionDAGISel {
73 /// ContainsFPCode - Every instruction we select that uses or defines a FP
74 /// register should set this to true.
75 bool ContainsFPCode;
76
77 /// X86Lowering - This object fully describes how to lower LLVM code to an
78 /// X86-specific SelectionDAG.
79 X86TargetLowering X86Lowering;
80
81 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
82 /// make the right decision when generating code for different targets.
83 const X86Subtarget *Subtarget;
84 public:
85 X86DAGToDAGISel(TargetMachine &TM)
86 : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
87 Subtarget = &TM.getSubtarget<X86Subtarget>();
88 }
89
90 virtual const char *getPassName() const {
91 return "X86 DAG->DAG Instruction Selection";
92 }
93
94 /// InstructionSelectBasicBlock - This callback is invoked by
95 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
96 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
97
98// Include the pieces autogenerated from the target description.
99#include "X86GenDAGISel.inc"
100
101 private:
102 SDOperand Select(SDOperand N);
103
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000104 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
Evan Chengc9fab312005-12-08 02:01:35 +0000105 bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
106 SDOperand &Index, SDOperand &Disp);
107 bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
108 SDOperand &Index, SDOperand &Disp);
Evan Cheng10d27902006-01-06 20:36:21 +0000109 bool TryFoldLoad(SDOperand N, SDOperand &Base, SDOperand &Scale,
110 SDOperand &Index, SDOperand &Disp);
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000111
Evan Cheng67ed58e2005-12-12 21:49:40 +0000112 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
113 SDOperand &Scale, SDOperand &Index,
114 SDOperand &Disp) {
115 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
116 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, MVT::i32) : AM.Base.Reg;
Evan Cheng1d712482005-12-17 09:13:43 +0000117 Scale = getI8Imm(AM.Scale);
Evan Cheng67ed58e2005-12-12 21:49:40 +0000118 Index = AM.IndexReg;
119 Disp = AM.GV ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp)
120 : getI32Imm(AM.Disp);
121 }
122
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000123 /// getI8Imm - Return a target constant with the specified value, of type
124 /// i8.
125 inline SDOperand getI8Imm(unsigned Imm) {
126 return CurDAG->getTargetConstant(Imm, MVT::i8);
127 }
128
Chris Lattner655e7df2005-11-16 01:54:32 +0000129 /// getI16Imm - Return a target constant with the specified value, of type
130 /// i16.
131 inline SDOperand getI16Imm(unsigned Imm) {
132 return CurDAG->getTargetConstant(Imm, MVT::i16);
133 }
134
135 /// getI32Imm - Return a target constant with the specified value, of type
136 /// i32.
137 inline SDOperand getI32Imm(unsigned Imm) {
138 return CurDAG->getTargetConstant(Imm, MVT::i32);
139 }
140 };
141}
142
143/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
144/// when it has created a SelectionDAG for us to codegen.
145void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
146 DEBUG(BB->dump());
Chris Lattner7c551262006-01-11 01:15:34 +0000147 MachineFunction::iterator FirstMBB = BB;
Chris Lattner655e7df2005-11-16 01:54:32 +0000148
149 // Codegen the basic block.
150 DAG.setRoot(Select(DAG.getRoot()));
Evan Cheng1d9b6712005-12-19 22:36:02 +0000151 CodeGenMap.clear();
Chris Lattner655e7df2005-11-16 01:54:32 +0000152 DAG.RemoveDeadNodes();
153
154 // Emit machine code to BB.
155 ScheduleAndEmitDAG(DAG);
Chris Lattner7c551262006-01-11 01:15:34 +0000156
157 // If we are emitting FP stack code, scan the basic block to determine if this
158 // block defines any FP values. If so, put an FP_REG_KILL instruction before
159 // the terminator of the block.
160 if (X86Vector < SSE2) {
161 // Note that FP stack instructions *are* used in SSE code when returning
162 // values, but these are not live out of the basic block, so we don't need
163 // an FP_REG_KILL in this case either.
164 bool ContainsFPCode = false;
165
166 // Scan all of the machine instructions in these MBBs, checking for FP
167 // stores.
168 MachineFunction::iterator MBBI = FirstMBB;
169 do {
170 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
171 !ContainsFPCode && I != E; ++I) {
172 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
173 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
174 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
175 RegMap->getRegClass(I->getOperand(0).getReg()) ==
176 X86::RFPRegisterClass) {
177 ContainsFPCode = true;
178 break;
179 }
180 }
181 }
182 } while (!ContainsFPCode && &*(MBBI++) != BB);
183
184 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
185 // a copy of the input value in this block.
186 if (!ContainsFPCode) {
187 // Final check, check LLVM BB's that are successors to the LLVM BB
188 // corresponding to BB for FP PHI nodes.
189 const BasicBlock *LLVMBB = BB->getBasicBlock();
190 const PHINode *PN;
191 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
192 !ContainsFPCode && SI != E; ++SI) {
193 for (BasicBlock::const_iterator II = SI->begin();
194 (PN = dyn_cast<PHINode>(II)); ++II) {
195 if (PN->getType()->isFloatingPoint()) {
196 ContainsFPCode = true;
197 break;
198 }
199 }
200 }
201 }
202
203 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
204 if (ContainsFPCode) {
205 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
206 ++NumFPKill;
207 }
208 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000209}
210
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000211/// FIXME: copied from X86ISelPattern.cpp
212/// MatchAddress - Add the specified node to the specified addressing mode,
213/// returning true if it cannot be done. This just pattern matches for the
214/// addressing mode
215bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
216 switch (N.getOpcode()) {
217 default: break;
218 case ISD::FrameIndex:
219 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
220 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
221 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
222 return false;
223 }
224 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000225
226 case ISD::ConstantPool:
227 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
228 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N)) {
229 AM.BaseType = X86ISelAddressMode::ConstantPoolBase;
230 AM.Base.Reg = CurDAG->getTargetConstantPool(CP->get(), MVT::i32);
231 return false;
232 }
233 }
234 break;
235
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000236 case ISD::GlobalAddress:
Evan Cheng9cdc16c2005-12-21 23:05:39 +0000237 case ISD::TargetGlobalAddress:
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000238 if (AM.GV == 0) {
Evan Chenga74ce622005-12-21 02:39:21 +0000239 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Evan Cheng1d712482005-12-17 09:13:43 +0000240 return false;
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000241 }
242 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000243
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000244 case ISD::Constant:
245 AM.Disp += cast<ConstantSDNode>(N)->getValue();
246 return false;
Evan Chengc9fab312005-12-08 02:01:35 +0000247
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000248 case ISD::SHL:
249 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
250 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
251 unsigned Val = CN->getValue();
252 if (Val == 1 || Val == 2 || Val == 3) {
253 AM.Scale = 1 << Val;
254 SDOperand ShVal = N.Val->getOperand(0);
255
256 // Okay, we know that we have a scale by now. However, if the scaled
257 // value is an add of something and a constant, we can fold the
258 // constant into the disp field here.
259 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
260 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
261 AM.IndexReg = ShVal.Val->getOperand(0);
262 ConstantSDNode *AddVal =
263 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
264 AM.Disp += AddVal->getValue() << Val;
265 } else {
266 AM.IndexReg = ShVal;
267 }
268 return false;
269 }
270 }
271 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000272
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000273 case ISD::MUL:
274 // X*[3,5,9] -> X+X*[2,4,8]
275 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
276 AM.Base.Reg.Val == 0)
277 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
278 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
279 AM.Scale = unsigned(CN->getValue())-1;
280
281 SDOperand MulVal = N.Val->getOperand(0);
282 SDOperand Reg;
283
284 // Okay, we know that we have a scale by now. However, if the scaled
285 // value is an add of something and a constant, we can fold the
286 // constant into the disp field here.
287 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
288 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
289 Reg = MulVal.Val->getOperand(0);
290 ConstantSDNode *AddVal =
291 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
292 AM.Disp += AddVal->getValue() * CN->getValue();
293 } else {
294 Reg = N.Val->getOperand(0);
295 }
296
297 AM.IndexReg = AM.Base.Reg = Reg;
298 return false;
299 }
300 break;
301
302 case ISD::ADD: {
303 X86ISelAddressMode Backup = AM;
304 if (!MatchAddress(N.Val->getOperand(0), AM) &&
305 !MatchAddress(N.Val->getOperand(1), AM))
306 return false;
307 AM = Backup;
308 if (!MatchAddress(N.Val->getOperand(1), AM) &&
309 !MatchAddress(N.Val->getOperand(0), AM))
310 return false;
311 AM = Backup;
312 break;
313 }
314 }
315
316 // Is the base register already occupied?
317 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
318 // If so, check to see if the scale index register is set.
319 if (AM.IndexReg.Val == 0) {
320 AM.IndexReg = N;
321 AM.Scale = 1;
322 return false;
323 }
324
325 // Otherwise, we cannot select it.
326 return true;
327 }
328
329 // Default, generate it as a register.
330 AM.BaseType = X86ISelAddressMode::RegBase;
331 AM.Base.Reg = N;
332 return false;
333}
334
Evan Chengc9fab312005-12-08 02:01:35 +0000335/// SelectAddr - returns true if it is able pattern match an addressing mode.
336/// It returns the operands which make up the maximal addressing mode it can
337/// match by reference.
338bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
339 SDOperand &Index, SDOperand &Disp) {
340 X86ISelAddressMode AM;
341 if (!MatchAddress(N, AM)) {
342 if (AM.BaseType == X86ISelAddressMode::RegBase) {
343 if (AM.Base.Reg.Val)
344 AM.Base.Reg = Select(AM.Base.Reg);
345 else
346 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
347 }
348 if (AM.IndexReg.Val)
349 AM.IndexReg = Select(AM.IndexReg);
350 else
351 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
352
Evan Cheng67ed58e2005-12-12 21:49:40 +0000353 getAddressOperands(AM, Base, Scale, Index, Disp);
Evan Chengc9fab312005-12-08 02:01:35 +0000354 return true;
355 }
356 return false;
357}
358
Evan Cheng10d27902006-01-06 20:36:21 +0000359bool X86DAGToDAGISel::TryFoldLoad(SDOperand N, SDOperand &Base,
360 SDOperand &Scale, SDOperand &Index,
361 SDOperand &Disp) {
362 if (N.getOpcode() == ISD::LOAD && N.hasOneUse() &&
Evan Cheng92e27972006-01-06 23:19:29 +0000363 CodeGenMap.count(N.getValue(1)) == 0)
Evan Cheng10d27902006-01-06 20:36:21 +0000364 return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp);
365 return false;
366}
367
368static bool isRegister0(SDOperand Op) {
Evan Chengc9fab312005-12-08 02:01:35 +0000369 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
370 return (R->getReg() == 0);
371 return false;
372}
373
374/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
375/// mode it matches can be cost effectively emitted as an LEA instruction.
376/// For X86, it always is unless it's just a (Reg + const).
Chris Lattner29852a582006-01-11 00:46:55 +0000377bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base,
378 SDOperand &Scale,
Evan Chengc9fab312005-12-08 02:01:35 +0000379 SDOperand &Index, SDOperand &Disp) {
Evan Cheng67ed58e2005-12-12 21:49:40 +0000380 X86ISelAddressMode AM;
381 if (!MatchAddress(N, AM)) {
382 bool SelectBase = false;
383 bool SelectIndex = false;
384 bool Check = false;
385 if (AM.BaseType == X86ISelAddressMode::RegBase) {
386 if (AM.Base.Reg.Val) {
387 Check = true;
388 SelectBase = true;
Evan Chengc9fab312005-12-08 02:01:35 +0000389 } else {
Evan Cheng67ed58e2005-12-12 21:49:40 +0000390 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
Evan Chengc9fab312005-12-08 02:01:35 +0000391 }
Evan Chengc9fab312005-12-08 02:01:35 +0000392 }
Evan Cheng67ed58e2005-12-12 21:49:40 +0000393
394 if (AM.IndexReg.Val) {
395 SelectIndex = true;
396 } else {
397 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
398 }
399
400 if (Check) {
401 unsigned Complexity = 0;
402 if (AM.Scale > 1)
403 Complexity++;
404 if (SelectIndex)
405 Complexity++;
406 if (AM.GV)
407 Complexity++;
408 else if (AM.Disp > 1)
409 Complexity++;
410 if (Complexity <= 1)
411 return false;
412 }
413
414 if (SelectBase)
415 AM.Base.Reg = Select(AM.Base.Reg);
416 if (SelectIndex)
417 AM.IndexReg = Select(AM.IndexReg);
418
419 getAddressOperands(AM, Base, Scale, Index, Disp);
Evan Chengc9fab312005-12-08 02:01:35 +0000420 return true;
Evan Chengc9fab312005-12-08 02:01:35 +0000421 }
Evan Cheng67ed58e2005-12-12 21:49:40 +0000422 return false;
Evan Chengc9fab312005-12-08 02:01:35 +0000423}
424
Evan Cheng00fcb002005-12-15 01:02:48 +0000425SDOperand X86DAGToDAGISel::Select(SDOperand N) {
426 SDNode *Node = N.Val;
427 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng10d27902006-01-06 20:36:21 +0000428 unsigned Opc, MOpc;
429 unsigned Opcode = Node->getOpcode();
Chris Lattner655e7df2005-11-16 01:54:32 +0000430
Evan Cheng10d27902006-01-06 20:36:21 +0000431 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER)
Evan Cheng00fcb002005-12-15 01:02:48 +0000432 return N; // Already selected.
Chris Lattner655e7df2005-11-16 01:54:32 +0000433
Evan Cheng10d27902006-01-06 20:36:21 +0000434 switch (Opcode) {
Chris Lattner655e7df2005-11-16 01:54:32 +0000435 default: break;
Evan Cheng10d27902006-01-06 20:36:21 +0000436 case ISD::MULHU:
437 case ISD::MULHS: {
438 if (Opcode == ISD::MULHU)
439 switch (NVT) {
440 default: assert(0 && "Unsupported VT!");
441 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
442 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
443 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
444 }
445 else
446 switch (NVT) {
447 default: assert(0 && "Unsupported VT!");
448 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
449 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
450 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
451 }
452
453 unsigned LoReg, HiReg;
454 switch (NVT) {
455 default: assert(0 && "Unsupported VT!");
456 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
457 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
458 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
459 }
460
461 SDOperand N0 = Node->getOperand(0);
462 SDOperand N1 = Node->getOperand(1);
463
464 bool foldedLoad = false;
465 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
466 foldedLoad = TryFoldLoad(N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng92e27972006-01-06 23:19:29 +0000467 // MULHU and MULHS are commmutative
468 if (!foldedLoad) {
469 foldedLoad = TryFoldLoad(N0, Tmp0, Tmp1, Tmp2, Tmp3);
470 if (foldedLoad) {
471 N0 = Node->getOperand(1);
472 N1 = Node->getOperand(0);
473 }
474 }
475
Evan Cheng10d27902006-01-06 20:36:21 +0000476 SDOperand Chain = foldedLoad ? Select(N1.getOperand(0))
477 : CurDAG->getEntryNode();
478
479 SDOperand InFlag;
480 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
481 Select(N0), InFlag);
482 InFlag = Chain.getValue(1);
483
484 if (foldedLoad) {
485 Chain = CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
486 Tmp2, Tmp3, Chain, InFlag);
487 InFlag = Chain.getValue(1);
488 } else {
489 InFlag = CurDAG->getTargetNode(Opc, MVT::Flag, Select(N1), InFlag);
490 }
491
492 SDOperand Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
493 CodeGenMap[N.getValue(0)] = Result;
Evan Cheng92e27972006-01-06 23:19:29 +0000494 if (foldedLoad)
495 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
496 return Result;
497 }
498
499 case ISD::SDIV:
500 case ISD::UDIV:
501 case ISD::SREM:
502 case ISD::UREM: {
503 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
504 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
505 if (!isSigned)
506 switch (NVT) {
507 default: assert(0 && "Unsupported VT!");
508 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
509 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
510 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
511 }
512 else
513 switch (NVT) {
514 default: assert(0 && "Unsupported VT!");
515 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
516 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
517 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
518 }
519
520 unsigned LoReg, HiReg;
521 unsigned ClrOpcode, SExtOpcode;
522 switch (NVT) {
523 default: assert(0 && "Unsupported VT!");
524 case MVT::i8:
525 LoReg = X86::AL; HiReg = X86::AH;
526 ClrOpcode = X86::MOV8ri;
527 SExtOpcode = X86::CBW;
528 break;
529 case MVT::i16:
530 LoReg = X86::AX; HiReg = X86::DX;
531 ClrOpcode = X86::MOV16ri;
532 SExtOpcode = X86::CWD;
533 break;
534 case MVT::i32:
535 LoReg = X86::EAX; HiReg = X86::EDX;
536 ClrOpcode = X86::MOV32ri;
537 SExtOpcode = X86::CDQ;
538 break;
539 }
540
541 SDOperand N0 = Node->getOperand(0);
542 SDOperand N1 = Node->getOperand(1);
543
544 bool foldedLoad = false;
545 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
546 foldedLoad = TryFoldLoad(N1, Tmp0, Tmp1, Tmp2, Tmp3);
547 SDOperand Chain = foldedLoad ? Select(N1.getOperand(0))
548 : CurDAG->getEntryNode();
549
550 SDOperand InFlag;
551 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
552 Select(N0), InFlag);
553 InFlag = Chain.getValue(1);
554
555 if (isSigned) {
556 // Sign extend the low part into the high part.
557 InFlag = CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag);
558 } else {
559 // Zero out the high part, effectively zero extending the input.
560 SDOperand ClrNode =
561 CurDAG->getTargetNode(ClrOpcode, NVT,
562 CurDAG->getTargetConstant(0, NVT));
563 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT),
564 ClrNode, InFlag);
565 InFlag = Chain.getValue(1);
566 }
567
568 if (foldedLoad) {
569 Chain = CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
570 Tmp2, Tmp3, Chain, InFlag);
571 InFlag = Chain.getValue(1);
572 } else {
573 InFlag = CurDAG->getTargetNode(Opc, MVT::Flag, Select(N1), InFlag);
574 }
575
576 SDOperand Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg,
577 NVT, InFlag);
578 CodeGenMap[N.getValue(0)] = Result;
579 if (foldedLoad)
580 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
581 return Result;
Evan Cheng10d27902006-01-06 20:36:21 +0000582 }
Evan Cheng4eb7af92005-11-30 02:51:20 +0000583
Evan Chengbc7708c2005-12-17 02:02:50 +0000584 case ISD::TRUNCATE: {
585 unsigned Reg;
586 MVT::ValueType VT;
587 switch (Node->getOperand(0).getValueType()) {
588 default: assert(0 && "Unknown truncate!");
589 case MVT::i16: Reg = X86::AX; Opc = X86::MOV16rr; VT = MVT::i16; break;
590 case MVT::i32: Reg = X86::EAX; Opc = X86::MOV32rr; VT = MVT::i32; break;
591 }
592 SDOperand Tmp0 = Select(Node->getOperand(0));
593 SDOperand Tmp1 = CurDAG->getTargetNode(Opc, VT, Tmp0);
594 SDOperand InFlag = SDOperand(0,0);
595 SDOperand Result = CurDAG->getCopyToReg(CurDAG->getEntryNode(),
596 Reg, Tmp1, InFlag).getValue(1);
597 SDOperand Chain = Result.getValue(0);
598 InFlag = Result.getValue(1);
599
600 switch (NVT) {
601 default: assert(0 && "Unknown truncate!");
602 case MVT::i8: Reg = X86::AL; Opc = X86::MOV8rr; VT = MVT::i8; break;
603 case MVT::i16: Reg = X86::AX; Opc = X86::MOV16rr; VT = MVT::i16; break;
604 }
605
606 Result = CurDAG->getCopyFromReg(Chain,
607 Reg, VT, InFlag);
Evan Cheng10d27902006-01-06 20:36:21 +0000608 if (N.Val->hasOneUse())
609 return CurDAG->SelectNodeTo(N.Val, Opc, VT, Result);
610 else
611 return CodeGenMap[N] = CurDAG->getTargetNode(Opc, VT, Result);
Evan Chengbc7708c2005-12-17 02:02:50 +0000612 break;
613 }
Evan Cheng73a1ad92006-01-10 20:26:56 +0000614
615 case X86ISD::FP_TO_INT16_IN_MEM:
616 case X86ISD::FP_TO_INT32_IN_MEM:
617 case X86ISD::FP_TO_INT64_IN_MEM: {
618 assert(N.getOperand(1).getValueType() == MVT::f64);
619
620 // Change the floating point control register to use "round towards zero"
621 // mode when truncating to an integer value.
622 MachineFunction &MF = CurDAG->getMachineFunction();
623 int CWFI = MF.getFrameInfo()->CreateStackObject(2, 2);
624 SDOperand CWSlot = CurDAG->getFrameIndex(CWFI, MVT::i32);
625 SDOperand Base, Scale, Index, Disp;
626 (void)SelectAddr(CWSlot, Base, Scale, Index, Disp);
627 SDOperand Chain = N.getOperand(0);
628
629 // Save the control word.
630 Chain = CurDAG->getTargetNode(X86::FNSTCW16m, MVT::Other,
631 Base, Scale, Index, Disp, Chain);
632
633 // Load the old value of the high byte of the control word.
634 SDOperand OldCW =
635 CurDAG->getTargetNode(X86::MOV16rm, MVT::i16, MVT::Other,
636 Base, Scale, Index, Disp, Chain);
637 Chain = OldCW.getValue(1);
638
639 // Set the high part to be round to zero...
640 Chain = CurDAG->getTargetNode(X86::MOV16mi, MVT::Other,
641 Base, Scale, Index, Disp,
642 CurDAG->getConstant(0xC7F, MVT::i16),
643 Chain);
644
645 // Reload the modified control word now...
646 Chain = CurDAG->getTargetNode(X86::FLDCW16m, MVT::Other,
647 Base, Scale, Index, Disp, Chain);
648
649 // Restore the memory image of control word to original value
650 Chain = CurDAG->getTargetNode(X86::MOV16mr, MVT::Other,
651 Base, Scale, Index, Disp, OldCW, Chain);
652
653 switch (Opcode) {
654 case X86ISD::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
655 case X86ISD::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
656 case X86ISD::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
657 }
658
659 SDOperand N1 = Select(N.getOperand(1));
660 SDOperand Base2, Scale2, Index2, Disp2;
661 (void)SelectAddr(N.getOperand(2), Base2, Scale2, Index2, Disp2);
662 Chain = CurDAG->getTargetNode(Opc, MVT::Other,
663 Base2, Scale2, Index2, Disp2, N1, Chain);
664
665 // Reload the modified control word now...
666 CodeGenMap[N] =
667 Chain = CurDAG->getTargetNode(X86::FLDCW16m, MVT::Other,
668 Base, Scale, Index, Disp, Chain);
669 return Chain;
670 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000671 }
672
Evan Cheng00fcb002005-12-15 01:02:48 +0000673 return SelectCode(N);
Chris Lattner655e7df2005-11-16 01:54:32 +0000674}
675
676/// createX86ISelDag - This pass converts a legalized DAG into a
677/// X86-specific DAG, ready for instruction scheduling.
678///
679FunctionPass *llvm::createX86ISelDag(TargetMachine &TM) {
680 return new X86DAGToDAGISel(TM);
681}