blob: 0e236637e309c38524bbdeda776f0dc0a98885d3 [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"
Evan Chengbc7a0f442006-01-11 06:09:51 +000016#include "X86InstrBuilder.h"
Chris Lattner7c551262006-01-11 01:15:34 +000017#include "X86RegisterInfo.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000018#include "X86Subtarget.h"
19#include "X86ISelLowering.h"
Chris Lattner3f0f71b2005-11-19 02:11:08 +000020#include "llvm/GlobalValue.h"
Chris Lattner7c551262006-01-11 01:15:34 +000021#include "llvm/Instructions.h"
22#include "llvm/Support/CFG.h"
Chris Lattner3f0f71b2005-11-19 02:11:08 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000024#include "llvm/CodeGen/MachineFunction.h"
Evan Cheng73a1ad92006-01-10 20:26:56 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner7c551262006-01-11 01:15:34 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
27#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000028#include "llvm/CodeGen/SelectionDAGISel.h"
29#include "llvm/Target/TargetMachine.h"
30#include "llvm/Support/Debug.h"
31#include "llvm/ADT/Statistic.h"
Chris Lattnerde02d772006-01-22 23:41:00 +000032#include <iostream>
Chris Lattner655e7df2005-11-16 01:54:32 +000033using namespace llvm;
34
35//===----------------------------------------------------------------------===//
36// Pattern Matcher Implementation
37//===----------------------------------------------------------------------===//
38
39namespace {
Chris Lattner3f0f71b2005-11-19 02:11:08 +000040 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
41 /// SDOperand's instead of register numbers for the leaves of the matched
42 /// tree.
43 struct X86ISelAddressMode {
44 enum {
45 RegBase,
46 FrameIndexBase,
Evan Chengc9fab312005-12-08 02:01:35 +000047 ConstantPoolBase
Chris Lattner3f0f71b2005-11-19 02:11:08 +000048 } BaseType;
49
50 struct { // This is really a union, discriminated by BaseType!
51 SDOperand Reg;
52 int FrameIndex;
53 } Base;
54
55 unsigned Scale;
56 SDOperand IndexReg;
57 unsigned Disp;
58 GlobalValue *GV;
59
60 X86ISelAddressMode()
Evan Cheng4eb7af92005-11-30 02:51:20 +000061 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0), GV(0) {
Chris Lattner3f0f71b2005-11-19 02:11:08 +000062 }
63 };
64}
65
66namespace {
Chris Lattner655e7df2005-11-16 01:54:32 +000067 Statistic<>
68 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
69
70 //===--------------------------------------------------------------------===//
71 /// ISel - X86 specific code to select X86 machine instructions for
72 /// SelectionDAG operations.
73 ///
74 class X86DAGToDAGISel : public SelectionDAGISel {
75 /// ContainsFPCode - Every instruction we select that uses or defines a FP
76 /// register should set this to true.
77 bool ContainsFPCode;
78
79 /// X86Lowering - This object fully describes how to lower LLVM code to an
80 /// X86-specific SelectionDAG.
81 X86TargetLowering X86Lowering;
82
83 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
84 /// make the right decision when generating code for different targets.
85 const X86Subtarget *Subtarget;
86 public:
87 X86DAGToDAGISel(TargetMachine &TM)
88 : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
89 Subtarget = &TM.getSubtarget<X86Subtarget>();
90 }
91
92 virtual const char *getPassName() const {
93 return "X86 DAG->DAG Instruction Selection";
94 }
95
96 /// InstructionSelectBasicBlock - This callback is invoked by
97 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
98 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
99
Evan Chengbc7a0f442006-01-11 06:09:51 +0000100 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
101
Chris Lattner655e7df2005-11-16 01:54:32 +0000102// Include the pieces autogenerated from the target description.
103#include "X86GenDAGISel.inc"
104
105 private:
106 SDOperand Select(SDOperand N);
107
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000108 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
Evan Chengc9fab312005-12-08 02:01:35 +0000109 bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
110 SDOperand &Index, SDOperand &Disp);
111 bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
112 SDOperand &Index, SDOperand &Disp);
Evan Cheng10d27902006-01-06 20:36:21 +0000113 bool TryFoldLoad(SDOperand N, SDOperand &Base, SDOperand &Scale,
114 SDOperand &Index, SDOperand &Disp);
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000115
Evan Cheng67ed58e2005-12-12 21:49:40 +0000116 inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base,
117 SDOperand &Scale, SDOperand &Index,
118 SDOperand &Disp) {
119 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
120 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, MVT::i32) : AM.Base.Reg;
Evan Cheng1d712482005-12-17 09:13:43 +0000121 Scale = getI8Imm(AM.Scale);
Evan Cheng67ed58e2005-12-12 21:49:40 +0000122 Index = AM.IndexReg;
123 Disp = AM.GV ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp)
124 : getI32Imm(AM.Disp);
125 }
126
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000127 /// getI8Imm - Return a target constant with the specified value, of type
128 /// i8.
129 inline SDOperand getI8Imm(unsigned Imm) {
130 return CurDAG->getTargetConstant(Imm, MVT::i8);
131 }
132
Chris Lattner655e7df2005-11-16 01:54:32 +0000133 /// getI16Imm - Return a target constant with the specified value, of type
134 /// i16.
135 inline SDOperand getI16Imm(unsigned Imm) {
136 return CurDAG->getTargetConstant(Imm, MVT::i16);
137 }
138
139 /// getI32Imm - Return a target constant with the specified value, of type
140 /// i32.
141 inline SDOperand getI32Imm(unsigned Imm) {
142 return CurDAG->getTargetConstant(Imm, MVT::i32);
143 }
144 };
145}
146
147/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
148/// when it has created a SelectionDAG for us to codegen.
149void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
150 DEBUG(BB->dump());
Chris Lattner7c551262006-01-11 01:15:34 +0000151 MachineFunction::iterator FirstMBB = BB;
Chris Lattner655e7df2005-11-16 01:54:32 +0000152
153 // Codegen the basic block.
154 DAG.setRoot(Select(DAG.getRoot()));
Evan Cheng1d9b6712005-12-19 22:36:02 +0000155 CodeGenMap.clear();
Chris Lattner655e7df2005-11-16 01:54:32 +0000156 DAG.RemoveDeadNodes();
157
158 // Emit machine code to BB.
159 ScheduleAndEmitDAG(DAG);
Chris Lattner7c551262006-01-11 01:15:34 +0000160
161 // If we are emitting FP stack code, scan the basic block to determine if this
162 // block defines any FP values. If so, put an FP_REG_KILL instruction before
163 // the terminator of the block.
Evan Chengcde9e302006-01-27 08:10:46 +0000164 if (!Subtarget->hasSSE2()) {
Chris Lattner7c551262006-01-11 01:15:34 +0000165 // Note that FP stack instructions *are* used in SSE code when returning
166 // values, but these are not live out of the basic block, so we don't need
167 // an FP_REG_KILL in this case either.
168 bool ContainsFPCode = false;
169
170 // Scan all of the machine instructions in these MBBs, checking for FP
171 // stores.
172 MachineFunction::iterator MBBI = FirstMBB;
173 do {
174 for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end();
175 !ContainsFPCode && I != E; ++I) {
176 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
177 if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() &&
178 MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
179 RegMap->getRegClass(I->getOperand(0).getReg()) ==
180 X86::RFPRegisterClass) {
181 ContainsFPCode = true;
182 break;
183 }
184 }
185 }
186 } while (!ContainsFPCode && &*(MBBI++) != BB);
187
188 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
189 // a copy of the input value in this block.
190 if (!ContainsFPCode) {
191 // Final check, check LLVM BB's that are successors to the LLVM BB
192 // corresponding to BB for FP PHI nodes.
193 const BasicBlock *LLVMBB = BB->getBasicBlock();
194 const PHINode *PN;
195 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
196 !ContainsFPCode && SI != E; ++SI) {
197 for (BasicBlock::const_iterator II = SI->begin();
198 (PN = dyn_cast<PHINode>(II)); ++II) {
199 if (PN->getType()->isFloatingPoint()) {
200 ContainsFPCode = true;
201 break;
202 }
203 }
204 }
205 }
206
207 // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
208 if (ContainsFPCode) {
209 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
210 ++NumFPKill;
211 }
212 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000213}
214
Evan Chengbc7a0f442006-01-11 06:09:51 +0000215/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
216/// the main function.
217static void EmitSpecialCodeForMain(MachineBasicBlock *BB,
218 MachineFrameInfo *MFI) {
219 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
220 int CWFrameIdx = MFI->CreateStackObject(2, 2);
221 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
222
223 // Set the high part to be 64-bit precision.
224 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
225 CWFrameIdx, 1).addImm(2);
226
227 // Reload the modified control word now.
228 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
229}
230
231void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
232 // If this is main, emit special code for main.
233 MachineBasicBlock *BB = MF.begin();
234 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
235 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
236}
237
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000238/// MatchAddress - Add the specified node to the specified addressing mode,
239/// returning true if it cannot be done. This just pattern matches for the
240/// addressing mode
241bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
242 switch (N.getOpcode()) {
243 default: break;
244 case ISD::FrameIndex:
245 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
246 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
247 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
248 return false;
249 }
250 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000251
252 case ISD::ConstantPool:
253 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
254 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N)) {
255 AM.BaseType = X86ISelAddressMode::ConstantPoolBase;
Evan Cheng72d5c252006-01-31 22:28:30 +0000256 AM.Base.Reg = CurDAG->getTargetConstantPool(CP->get(), MVT::i32,
257 CP->getAlignment());
Evan Chengc9fab312005-12-08 02:01:35 +0000258 return false;
259 }
260 }
261 break;
262
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000263 case ISD::GlobalAddress:
Evan Cheng9cdc16c2005-12-21 23:05:39 +0000264 case ISD::TargetGlobalAddress:
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000265 if (AM.GV == 0) {
Evan Chenga74ce622005-12-21 02:39:21 +0000266 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Evan Cheng1d712482005-12-17 09:13:43 +0000267 return false;
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000268 }
269 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000270
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000271 case ISD::Constant:
272 AM.Disp += cast<ConstantSDNode>(N)->getValue();
273 return false;
Evan Chengc9fab312005-12-08 02:01:35 +0000274
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000275 case ISD::SHL:
276 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
277 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
278 unsigned Val = CN->getValue();
279 if (Val == 1 || Val == 2 || Val == 3) {
280 AM.Scale = 1 << Val;
281 SDOperand ShVal = N.Val->getOperand(0);
282
283 // Okay, we know that we have a scale by now. However, if the scaled
284 // value is an add of something and a constant, we can fold the
285 // constant into the disp field here.
286 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
287 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
288 AM.IndexReg = ShVal.Val->getOperand(0);
289 ConstantSDNode *AddVal =
290 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
291 AM.Disp += AddVal->getValue() << Val;
292 } else {
293 AM.IndexReg = ShVal;
294 }
295 return false;
296 }
297 }
298 break;
Evan Chengc9fab312005-12-08 02:01:35 +0000299
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000300 case ISD::MUL:
301 // X*[3,5,9] -> X+X*[2,4,8]
302 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
303 AM.Base.Reg.Val == 0)
304 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
305 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
306 AM.Scale = unsigned(CN->getValue())-1;
307
308 SDOperand MulVal = N.Val->getOperand(0);
309 SDOperand Reg;
310
311 // Okay, we know that we have a scale by now. However, if the scaled
312 // value is an add of something and a constant, we can fold the
313 // constant into the disp field here.
314 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
315 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
316 Reg = MulVal.Val->getOperand(0);
317 ConstantSDNode *AddVal =
318 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
319 AM.Disp += AddVal->getValue() * CN->getValue();
320 } else {
321 Reg = N.Val->getOperand(0);
322 }
323
324 AM.IndexReg = AM.Base.Reg = Reg;
325 return false;
326 }
327 break;
328
329 case ISD::ADD: {
330 X86ISelAddressMode Backup = AM;
331 if (!MatchAddress(N.Val->getOperand(0), AM) &&
332 !MatchAddress(N.Val->getOperand(1), AM))
333 return false;
334 AM = Backup;
335 if (!MatchAddress(N.Val->getOperand(1), AM) &&
336 !MatchAddress(N.Val->getOperand(0), AM))
337 return false;
338 AM = Backup;
339 break;
340 }
341 }
342
343 // Is the base register already occupied?
344 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
345 // If so, check to see if the scale index register is set.
346 if (AM.IndexReg.Val == 0) {
347 AM.IndexReg = N;
348 AM.Scale = 1;
349 return false;
350 }
351
352 // Otherwise, we cannot select it.
353 return true;
354 }
355
356 // Default, generate it as a register.
357 AM.BaseType = X86ISelAddressMode::RegBase;
358 AM.Base.Reg = N;
359 return false;
360}
361
Evan Chengc9fab312005-12-08 02:01:35 +0000362/// SelectAddr - returns true if it is able pattern match an addressing mode.
363/// It returns the operands which make up the maximal addressing mode it can
364/// match by reference.
365bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale,
366 SDOperand &Index, SDOperand &Disp) {
367 X86ISelAddressMode AM;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000368 if (MatchAddress(N, AM))
369 return false;
Evan Chengc9fab312005-12-08 02:01:35 +0000370
Evan Chengbc7a0f442006-01-11 06:09:51 +0000371 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Evan Chengd19d51f2006-02-05 05:25:07 +0000372 if (!AM.Base.Reg.Val)
Evan Chengbc7a0f442006-01-11 06:09:51 +0000373 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
Evan Chengc9fab312005-12-08 02:01:35 +0000374 }
Evan Chengbc7a0f442006-01-11 06:09:51 +0000375
Evan Chengd19d51f2006-02-05 05:25:07 +0000376 if (!AM.IndexReg.Val)
Evan Chengbc7a0f442006-01-11 06:09:51 +0000377 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
378
379 getAddressOperands(AM, Base, Scale, Index, Disp);
380 return true;
Evan Chengc9fab312005-12-08 02:01:35 +0000381}
382
Evan Cheng10d27902006-01-06 20:36:21 +0000383bool X86DAGToDAGISel::TryFoldLoad(SDOperand N, SDOperand &Base,
384 SDOperand &Scale, SDOperand &Index,
385 SDOperand &Disp) {
386 if (N.getOpcode() == ISD::LOAD && N.hasOneUse() &&
Evan Cheng92e27972006-01-06 23:19:29 +0000387 CodeGenMap.count(N.getValue(1)) == 0)
Evan Cheng10d27902006-01-06 20:36:21 +0000388 return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp);
389 return false;
390}
391
392static bool isRegister0(SDOperand Op) {
Evan Chengc9fab312005-12-08 02:01:35 +0000393 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op))
394 return (R->getReg() == 0);
395 return false;
396}
397
398/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
399/// mode it matches can be cost effectively emitted as an LEA instruction.
400/// For X86, it always is unless it's just a (Reg + const).
Chris Lattner29852a582006-01-11 00:46:55 +0000401bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base,
402 SDOperand &Scale,
Evan Chengc9fab312005-12-08 02:01:35 +0000403 SDOperand &Index, SDOperand &Disp) {
Evan Cheng67ed58e2005-12-12 21:49:40 +0000404 X86ISelAddressMode AM;
405 if (!MatchAddress(N, AM)) {
406 bool SelectBase = false;
407 bool SelectIndex = false;
408 bool Check = false;
409 if (AM.BaseType == X86ISelAddressMode::RegBase) {
410 if (AM.Base.Reg.Val) {
411 Check = true;
412 SelectBase = true;
Evan Chengc9fab312005-12-08 02:01:35 +0000413 } else {
Evan Cheng67ed58e2005-12-12 21:49:40 +0000414 AM.Base.Reg = CurDAG->getRegister(0, MVT::i32);
Evan Chengc9fab312005-12-08 02:01:35 +0000415 }
Evan Chengc9fab312005-12-08 02:01:35 +0000416 }
Evan Cheng67ed58e2005-12-12 21:49:40 +0000417
418 if (AM.IndexReg.Val) {
419 SelectIndex = true;
420 } else {
421 AM.IndexReg = CurDAG->getRegister(0, MVT::i32);
422 }
423
424 if (Check) {
425 unsigned Complexity = 0;
426 if (AM.Scale > 1)
427 Complexity++;
428 if (SelectIndex)
429 Complexity++;
430 if (AM.GV)
431 Complexity++;
432 else if (AM.Disp > 1)
433 Complexity++;
434 if (Complexity <= 1)
435 return false;
436 }
437
Evan Cheng67ed58e2005-12-12 21:49:40 +0000438 getAddressOperands(AM, Base, Scale, Index, Disp);
Evan Chengc9fab312005-12-08 02:01:35 +0000439 return true;
Evan Chengc9fab312005-12-08 02:01:35 +0000440 }
Evan Cheng67ed58e2005-12-12 21:49:40 +0000441 return false;
Evan Chengc9fab312005-12-08 02:01:35 +0000442}
443
Evan Cheng00fcb002005-12-15 01:02:48 +0000444SDOperand X86DAGToDAGISel::Select(SDOperand N) {
445 SDNode *Node = N.Val;
446 MVT::ValueType NVT = Node->getValueType(0);
Evan Cheng10d27902006-01-06 20:36:21 +0000447 unsigned Opc, MOpc;
448 unsigned Opcode = Node->getOpcode();
Chris Lattner655e7df2005-11-16 01:54:32 +0000449
Evan Cheng10d27902006-01-06 20:36:21 +0000450 if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER)
Evan Cheng00fcb002005-12-15 01:02:48 +0000451 return N; // Already selected.
Evan Cheng2ae799a2006-01-11 22:15:18 +0000452
453 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(N);
454 if (CGMI != CodeGenMap.end()) return CGMI->second;
Chris Lattner655e7df2005-11-16 01:54:32 +0000455
Evan Cheng10d27902006-01-06 20:36:21 +0000456 switch (Opcode) {
Chris Lattner655e7df2005-11-16 01:54:32 +0000457 default: break;
Evan Cheng10d27902006-01-06 20:36:21 +0000458 case ISD::MULHU:
459 case ISD::MULHS: {
460 if (Opcode == ISD::MULHU)
461 switch (NVT) {
462 default: assert(0 && "Unsupported VT!");
463 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
464 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
465 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
466 }
467 else
468 switch (NVT) {
469 default: assert(0 && "Unsupported VT!");
470 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
471 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
472 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
473 }
474
475 unsigned LoReg, HiReg;
476 switch (NVT) {
477 default: assert(0 && "Unsupported VT!");
478 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
479 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
480 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
481 }
482
483 SDOperand N0 = Node->getOperand(0);
484 SDOperand N1 = Node->getOperand(1);
485
486 bool foldedLoad = false;
487 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
488 foldedLoad = TryFoldLoad(N1, Tmp0, Tmp1, Tmp2, Tmp3);
Evan Cheng92e27972006-01-06 23:19:29 +0000489 // MULHU and MULHS are commmutative
490 if (!foldedLoad) {
491 foldedLoad = TryFoldLoad(N0, Tmp0, Tmp1, Tmp2, Tmp3);
492 if (foldedLoad) {
493 N0 = Node->getOperand(1);
494 N1 = Node->getOperand(0);
495 }
496 }
497
Evan Cheng10d27902006-01-06 20:36:21 +0000498 SDOperand Chain = foldedLoad ? Select(N1.getOperand(0))
499 : CurDAG->getEntryNode();
500
501 SDOperand InFlag;
502 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
503 Select(N0), InFlag);
504 InFlag = Chain.getValue(1);
505
506 if (foldedLoad) {
507 Chain = CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
508 Tmp2, Tmp3, Chain, InFlag);
509 InFlag = Chain.getValue(1);
510 } else {
511 InFlag = CurDAG->getTargetNode(Opc, MVT::Flag, Select(N1), InFlag);
512 }
513
514 SDOperand Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag);
515 CodeGenMap[N.getValue(0)] = Result;
Evan Cheng92e27972006-01-06 23:19:29 +0000516 if (foldedLoad)
517 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
518 return Result;
519 }
520
521 case ISD::SDIV:
522 case ISD::UDIV:
523 case ISD::SREM:
524 case ISD::UREM: {
525 bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM;
526 bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV;
527 if (!isSigned)
528 switch (NVT) {
529 default: assert(0 && "Unsupported VT!");
530 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
531 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
532 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
533 }
534 else
535 switch (NVT) {
536 default: assert(0 && "Unsupported VT!");
537 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
538 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
539 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
540 }
541
542 unsigned LoReg, HiReg;
543 unsigned ClrOpcode, SExtOpcode;
544 switch (NVT) {
545 default: assert(0 && "Unsupported VT!");
546 case MVT::i8:
547 LoReg = X86::AL; HiReg = X86::AH;
548 ClrOpcode = X86::MOV8ri;
549 SExtOpcode = X86::CBW;
550 break;
551 case MVT::i16:
552 LoReg = X86::AX; HiReg = X86::DX;
553 ClrOpcode = X86::MOV16ri;
554 SExtOpcode = X86::CWD;
555 break;
556 case MVT::i32:
557 LoReg = X86::EAX; HiReg = X86::EDX;
558 ClrOpcode = X86::MOV32ri;
559 SExtOpcode = X86::CDQ;
560 break;
561 }
562
563 SDOperand N0 = Node->getOperand(0);
564 SDOperand N1 = Node->getOperand(1);
565
566 bool foldedLoad = false;
567 SDOperand Tmp0, Tmp1, Tmp2, Tmp3;
568 foldedLoad = TryFoldLoad(N1, Tmp0, Tmp1, Tmp2, Tmp3);
569 SDOperand Chain = foldedLoad ? Select(N1.getOperand(0))
570 : CurDAG->getEntryNode();
571
572 SDOperand InFlag;
573 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT),
574 Select(N0), InFlag);
575 InFlag = Chain.getValue(1);
576
577 if (isSigned) {
578 // Sign extend the low part into the high part.
579 InFlag = CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag);
580 } else {
581 // Zero out the high part, effectively zero extending the input.
582 SDOperand ClrNode =
583 CurDAG->getTargetNode(ClrOpcode, NVT,
584 CurDAG->getTargetConstant(0, NVT));
585 Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT),
586 ClrNode, InFlag);
587 InFlag = Chain.getValue(1);
588 }
589
590 if (foldedLoad) {
591 Chain = CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1,
592 Tmp2, Tmp3, Chain, InFlag);
593 InFlag = Chain.getValue(1);
594 } else {
595 InFlag = CurDAG->getTargetNode(Opc, MVT::Flag, Select(N1), InFlag);
596 }
597
598 SDOperand Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg,
599 NVT, InFlag);
600 CodeGenMap[N.getValue(0)] = Result;
601 if (foldedLoad)
602 CodeGenMap[N1.getValue(1)] = Result.getValue(1);
603 return Result;
Evan Cheng10d27902006-01-06 20:36:21 +0000604 }
Evan Cheng4eb7af92005-11-30 02:51:20 +0000605
Evan Chengbc7708c2005-12-17 02:02:50 +0000606 case ISD::TRUNCATE: {
607 unsigned Reg;
608 MVT::ValueType VT;
609 switch (Node->getOperand(0).getValueType()) {
610 default: assert(0 && "Unknown truncate!");
611 case MVT::i16: Reg = X86::AX; Opc = X86::MOV16rr; VT = MVT::i16; break;
612 case MVT::i32: Reg = X86::EAX; Opc = X86::MOV32rr; VT = MVT::i32; break;
613 }
614 SDOperand Tmp0 = Select(Node->getOperand(0));
615 SDOperand Tmp1 = CurDAG->getTargetNode(Opc, VT, Tmp0);
616 SDOperand InFlag = SDOperand(0,0);
617 SDOperand Result = CurDAG->getCopyToReg(CurDAG->getEntryNode(),
Evan Cheng2ae799a2006-01-11 22:15:18 +0000618 Reg, Tmp1, InFlag);
Evan Chengbc7708c2005-12-17 02:02:50 +0000619 SDOperand Chain = Result.getValue(0);
620 InFlag = Result.getValue(1);
621
622 switch (NVT) {
623 default: assert(0 && "Unknown truncate!");
624 case MVT::i8: Reg = X86::AL; Opc = X86::MOV8rr; VT = MVT::i8; break;
625 case MVT::i16: Reg = X86::AX; Opc = X86::MOV16rr; VT = MVT::i16; break;
626 }
627
628 Result = CurDAG->getCopyFromReg(Chain,
629 Reg, VT, InFlag);
Evan Cheng10d27902006-01-06 20:36:21 +0000630 if (N.Val->hasOneUse())
631 return CurDAG->SelectNodeTo(N.Val, Opc, VT, Result);
632 else
633 return CodeGenMap[N] = CurDAG->getTargetNode(Opc, VT, Result);
Evan Chengbc7708c2005-12-17 02:02:50 +0000634 break;
635 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000636 }
637
Evan Cheng00fcb002005-12-15 01:02:48 +0000638 return SelectCode(N);
Chris Lattner655e7df2005-11-16 01:54:32 +0000639}
640
641/// createX86ISelDag - This pass converts a legalized DAG into a
642/// X86-specific DAG, ready for instruction scheduling.
643///
644FunctionPass *llvm::createX86ISelDag(TargetMachine &TM) {
645 return new X86DAGToDAGISel(TM);
646}