blob: e9531c5db9f8f13f0ced3b18c3a12e05f38f4b5e [file] [log] [blame]
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
Chris Lattner24aad1b2005-01-10 22:10:13 +00002//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group 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 pattern matching instruction selector for X86.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
15#include "X86InstrBuilder.h"
16#include "X86RegisterInfo.h"
Chris Lattner590d8002005-01-09 18:52:44 +000017#include "llvm/Constants.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000018#include "llvm/Function.h"
Chris Lattner590d8002005-01-09 18:52:44 +000019#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000020#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/ADT/Statistic.h"
29#include <set>
30using namespace llvm;
31
32//===----------------------------------------------------------------------===//
33// X86TargetLowering - X86 Implementation of the TargetLowering interface
34namespace {
35 class X86TargetLowering : public TargetLowering {
36 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000037 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000038 public:
39 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
40 // Set up the TargetLowering object.
41 addRegisterClass(MVT::i8, X86::R8RegisterClass);
42 addRegisterClass(MVT::i16, X86::R16RegisterClass);
43 addRegisterClass(MVT::i32, X86::R32RegisterClass);
44 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
45
46 // FIXME: Eliminate these two classes when legalize can handle promotions
47 // well.
48 addRegisterClass(MVT::i1, X86::R8RegisterClass);
49 addRegisterClass(MVT::f32, X86::RFPRegisterClass);
50
51 computeRegisterProperties();
52
53 setOperationUnsupported(ISD::MUL, MVT::i8);
54 setOperationUnsupported(ISD::SELECT, MVT::i1);
55 setOperationUnsupported(ISD::SELECT, MVT::i8);
56
57 addLegalFPImmediate(+0.0); // FLD0
58 addLegalFPImmediate(+1.0); // FLD1
59 addLegalFPImmediate(-0.0); // FLD0/FCHS
60 addLegalFPImmediate(-1.0); // FLD1/FCHS
61 }
62
63 /// LowerArguments - This hook must be implemented to indicate how we should
64 /// lower the arguments for the specified function, into the specified DAG.
65 virtual std::vector<SDOperand>
66 LowerArguments(Function &F, SelectionDAG &DAG);
67
68 /// LowerCallTo - This hook lowers an abstract call to a function into an
69 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000070 virtual std::pair<SDOperand, SDOperand>
71 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
72 ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000073
74 virtual std::pair<SDOperand, SDOperand>
75 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
76
77 virtual std::pair<SDOperand,SDOperand>
78 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
79 const Type *ArgTy, SelectionDAG &DAG);
80
81 virtual std::pair<SDOperand, SDOperand>
82 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
83 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000084 };
85}
86
87
88std::vector<SDOperand>
89X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
90 std::vector<SDOperand> ArgValues;
91
92 // Add DAG nodes to load the arguments... On entry to a function on the X86,
93 // the stack frame looks like this:
94 //
95 // [ESP] -- return address
96 // [ESP + 4] -- first argument (leftmost lexically)
97 // [ESP + 8] -- second argument, if first argument is four bytes in size
98 // ...
99 //
100 MachineFunction &MF = DAG.getMachineFunction();
101 MachineFrameInfo *MFI = MF.getFrameInfo();
102
103 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
104 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I) {
105 MVT::ValueType ObjectVT = getValueType(I->getType());
106 unsigned ArgIncrement = 4;
107 unsigned ObjSize;
108 switch (ObjectVT) {
109 default: assert(0 && "Unhandled argument type!");
110 case MVT::i1:
111 case MVT::i8: ObjSize = 1; break;
112 case MVT::i16: ObjSize = 2; break;
113 case MVT::i32: ObjSize = 4; break;
114 case MVT::i64: ObjSize = ArgIncrement = 8; break;
115 case MVT::f32: ObjSize = 4; break;
116 case MVT::f64: ObjSize = ArgIncrement = 8; break;
117 }
118 // Create the frame index object for this incoming parameter...
119 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
120
121 // Create the SelectionDAG nodes corresponding to a load from this parameter
122 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
123
124 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
125 // dead loads.
126 SDOperand ArgValue;
127 if (!I->use_empty())
128 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
129 else {
130 if (MVT::isInteger(ObjectVT))
131 ArgValue = DAG.getConstant(0, ObjectVT);
132 else
133 ArgValue = DAG.getConstantFP(0, ObjectVT);
134 }
135 ArgValues.push_back(ArgValue);
136
137 ArgOffset += ArgIncrement; // Move on to the next argument...
138 }
139
140 // If the function takes variable number of arguments, make a frame index for
141 // the start of the first vararg value... for expansion of llvm.va_start.
142 if (F.isVarArg())
143 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000144 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000145 return ArgValues;
146}
147
Chris Lattner5188ad72005-01-08 19:28:19 +0000148std::pair<SDOperand, SDOperand>
149X86TargetLowering::LowerCallTo(SDOperand Chain,
150 const Type *RetTy, SDOperand Callee,
151 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000152 // Count how many bytes are to be pushed on the stack.
153 unsigned NumBytes = 0;
154
155 if (Args.empty()) {
156 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000157 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
158 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000159 } else {
160 for (unsigned i = 0, e = Args.size(); i != e; ++i)
161 switch (getValueType(Args[i].second)) {
162 default: assert(0 && "Unknown value type!");
163 case MVT::i1:
164 case MVT::i8:
165 case MVT::i16:
166 case MVT::i32:
167 case MVT::f32:
168 NumBytes += 4;
169 break;
170 case MVT::i64:
171 case MVT::f64:
172 NumBytes += 8;
173 break;
174 }
175
Chris Lattner5188ad72005-01-08 19:28:19 +0000176 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
177 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000178
179 // Arguments go on the stack in reverse order, as specified by the ABI.
180 unsigned ArgOffset = 0;
181 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32);
182 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
183 unsigned ArgReg;
184 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
185 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
186
187 switch (getValueType(Args[i].second)) {
188 default: assert(0 && "Unexpected ValueType for argument!");
189 case MVT::i1:
190 case MVT::i8:
191 case MVT::i16:
192 // Promote the integer to 32 bits. If the input type is signed use a
193 // sign extend, otherwise use a zero extend.
194 if (Args[i].second->isSigned())
195 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
196 else
197 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
198
199 // FALL THROUGH
200 case MVT::i32:
201 case MVT::f32:
202 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000203 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
204 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000205 ArgOffset += 4;
206 break;
207 case MVT::i64:
208 case MVT::f64:
209 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000210 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
211 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000212 ArgOffset += 8;
213 break;
214 }
215 }
216 }
217
218 std::vector<MVT::ValueType> RetVals;
219 MVT::ValueType RetTyVT = getValueType(RetTy);
220 if (RetTyVT != MVT::isVoid)
221 RetVals.push_back(RetTyVT);
222 RetVals.push_back(MVT::Other);
223
Chris Lattner5188ad72005-01-08 19:28:19 +0000224 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000225 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000226 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
227 DAG.getConstant(NumBytes, getPointerTy()));
228 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000229}
230
Chris Lattner14824582005-01-09 00:01:27 +0000231std::pair<SDOperand, SDOperand>
232X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
233 // vastart just returns the address of the VarArgsFrameIndex slot.
234 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
235}
236
237std::pair<SDOperand,SDOperand> X86TargetLowering::
238LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
239 const Type *ArgTy, SelectionDAG &DAG) {
240 MVT::ValueType ArgVT = getValueType(ArgTy);
241 SDOperand Result;
242 if (!isVANext) {
243 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
244 } else {
245 unsigned Amt;
246 if (ArgVT == MVT::i32)
247 Amt = 4;
248 else {
249 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
250 "Other types should have been promoted for varargs!");
251 Amt = 8;
252 }
253 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
254 DAG.getConstant(Amt, VAList.getValueType()));
255 }
256 return std::make_pair(Result, Chain);
257}
258
259
260std::pair<SDOperand, SDOperand> X86TargetLowering::
261LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
262 SelectionDAG &DAG) {
263 SDOperand Result;
264 if (Depth) // Depths > 0 not supported yet!
265 Result = DAG.getConstant(0, getPointerTy());
266 else {
267 if (ReturnAddrIndex == 0) {
268 // Set up a frame object for the return address.
269 MachineFunction &MF = DAG.getMachineFunction();
270 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
271 }
272
273 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
274
275 if (!isFrameAddress)
276 // Just load the return address
277 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI);
278 else
279 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
280 DAG.getConstant(4, MVT::i32));
281 }
282 return std::make_pair(Result, Chain);
283}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000284
285
286
287
288
289namespace {
290 Statistic<>
291 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
292
293 //===--------------------------------------------------------------------===//
294 /// ISel - X86 specific code to select X86 machine instructions for
295 /// SelectionDAG operations.
296 ///
297 class ISel : public SelectionDAGISel {
298 /// ContainsFPCode - Every instruction we select that uses or defines a FP
299 /// register should set this to true.
300 bool ContainsFPCode;
301
302 /// X86Lowering - This object fully describes how to lower LLVM code to an
303 /// X86-specific SelectionDAG.
304 X86TargetLowering X86Lowering;
305
Chris Lattner11333092005-01-11 03:11:44 +0000306 /// RegPressureMap - This keeps an approximate count of the number of
307 /// registers required to evaluate each node in the graph.
308 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000309
310 /// ExprMap - As shared expressions are codegen'd, we keep track of which
311 /// vreg the value is produced in, so we only emit one copy of each compiled
312 /// tree.
313 std::map<SDOperand, unsigned> ExprMap;
314 std::set<SDOperand> LoweredTokens;
315
316 public:
317 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
318 }
319
Chris Lattner11333092005-01-11 03:11:44 +0000320 unsigned getRegPressure(SDOperand O) {
321 return RegPressureMap[O.Val];
322 }
323 unsigned ComputeRegPressure(SDOperand O);
324
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000325 /// InstructionSelectBasicBlock - This callback is invoked by
326 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
327 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
328 // While we're doing this, keep track of whether we see any FP code for
329 // FP_REG_KILL insertion.
330 ContainsFPCode = false;
331
Chris Lattner11333092005-01-11 03:11:44 +0000332 // Compute the RegPressureMap, which is an approximation for the number of
333 // registers required to compute each node.
334 ComputeRegPressure(DAG.getRoot());
335
336 //DAG.viewGraph();
337
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000338 // Codegen the basic block.
339 Select(DAG.getRoot());
340
341 // Insert FP_REG_KILL instructions into basic blocks that need them. This
342 // only occurs due to the floating point stackifier not being aggressive
343 // enough to handle arbitrary global stackification.
344 //
345 // Currently we insert an FP_REG_KILL instruction into each block that
346 // uses or defines a floating point virtual register.
347 //
348 // When the global register allocators (like linear scan) finally update
349 // live variable analysis, we can keep floating point values in registers
350 // across basic blocks. This will be a huge win, but we are waiting on
351 // the global allocators before we can do this.
352 //
353 if (ContainsFPCode && BB->succ_size()) {
354 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
355 ++NumFPKill;
356 }
357
358 // Clear state used for selection.
359 ExprMap.clear();
360 LoweredTokens.clear();
Chris Lattner11333092005-01-11 03:11:44 +0000361 RegPressureMap.clear();
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000362 }
363
364 void EmitCMP(SDOperand LHS, SDOperand RHS);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000365 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000366 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
367 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000368 unsigned SelectExpr(SDOperand N);
369 bool SelectAddress(SDOperand N, X86AddressMode &AM);
370 void Select(SDOperand N);
371 };
372}
373
Chris Lattner11333092005-01-11 03:11:44 +0000374// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
375// for the number of registers required to compute each node. This is basically
376// computing a generalized form of the Sethi-Ullman number for each node.
377unsigned ISel::ComputeRegPressure(SDOperand O) {
378 SDNode *N = O.Val;
379 unsigned &Result = RegPressureMap[N];
380 if (Result) return Result;
381
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000382 // FIXME: Should operations like CALL (which clobber lots o regs) have a
383 // higher fixed cost??
384
Chris Lattner11333092005-01-11 03:11:44 +0000385 if (N->getNumOperands() == 0)
386 return Result = 1;
387
388 unsigned MaxRegUse = 0;
389 unsigned NumExtraMaxRegUsers = 0;
390 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
391 unsigned Regs = ComputeRegPressure(N->getOperand(i));
392 if (Regs > MaxRegUse) {
393 MaxRegUse = Regs;
394 NumExtraMaxRegUsers = 0;
395 } else if (Regs == MaxRegUse) {
396 ++NumExtraMaxRegUsers;
397 }
398 }
399
400 return Result = MaxRegUse+NumExtraMaxRegUsers;
401}
402
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000403/// SelectAddress - Add the specified node to the specified addressing mode,
404/// returning true if it cannot be done.
405bool ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
406 switch (N.getOpcode()) {
407 default: break;
408 case ISD::FrameIndex:
409 if (AM.BaseType == X86AddressMode::RegBase && AM.Base.Reg == 0) {
410 AM.BaseType = X86AddressMode::FrameIndexBase;
411 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
412 return false;
413 }
414 break;
415 case ISD::GlobalAddress:
416 if (AM.GV == 0) {
417 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
418 return false;
419 }
420 break;
421 case ISD::Constant:
422 AM.Disp += cast<ConstantSDNode>(N)->getValue();
423 return false;
424 case ISD::SHL:
425 if (AM.IndexReg == 0 || AM.Scale == 1)
426 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
427 unsigned Val = CN->getValue();
428 if (Val == 1 || Val == 2 || Val == 3) {
429 AM.Scale = 1 << Val;
430 AM.IndexReg = SelectExpr(N.Val->getOperand(0));
431 return false;
432 }
433 }
434 break;
435
436 case ISD::ADD: {
437 X86AddressMode Backup = AM;
438 if (!SelectAddress(N.Val->getOperand(0), AM) &&
439 !SelectAddress(N.Val->getOperand(1), AM))
440 return false;
441 AM = Backup;
442 break;
443 }
444 }
445
446 if (AM.BaseType != X86AddressMode::RegBase ||
447 AM.Base.Reg)
448 return true;
449
450 // Default, generate it as a register.
451 AM.BaseType = X86AddressMode::RegBase;
452 AM.Base.Reg = SelectExpr(N);
453 return false;
454}
455
456/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
457/// assuming that the temporary registers are in the 8-bit register class.
458///
459/// Tmp1 = setcc1
460/// Tmp2 = setcc2
461/// DestReg = logicalop Tmp1, Tmp2
462///
463static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
464 unsigned SetCC2, unsigned LogicalOp,
465 unsigned DestReg) {
466 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
467 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
468 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
469 BuildMI(BB, SetCC1, 0, Tmp1);
470 BuildMI(BB, SetCC2, 0, Tmp2);
471 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
472}
473
474/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
475/// condition codes match the specified SetCCOpcode. Note that some conditions
476/// require multiple instructions to generate the correct value.
477static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
478 ISD::CondCode SetCCOpcode, bool isFP) {
479 unsigned Opc;
480 if (!isFP) {
481 switch (SetCCOpcode) {
482 default: assert(0 && "Illegal integer SetCC!");
483 case ISD::SETEQ: Opc = X86::SETEr; break;
484 case ISD::SETGT: Opc = X86::SETGr; break;
485 case ISD::SETGE: Opc = X86::SETGEr; break;
486 case ISD::SETLT: Opc = X86::SETLr; break;
487 case ISD::SETLE: Opc = X86::SETLEr; break;
488 case ISD::SETNE: Opc = X86::SETNEr; break;
489 case ISD::SETULT: Opc = X86::SETBr; break;
490 case ISD::SETUGT: Opc = X86::SETAr; break;
491 case ISD::SETULE: Opc = X86::SETBEr; break;
492 case ISD::SETUGE: Opc = X86::SETAEr; break;
493 }
494 } else {
495 // On a floating point condition, the flags are set as follows:
496 // ZF PF CF op
497 // 0 | 0 | 0 | X > Y
498 // 0 | 0 | 1 | X < Y
499 // 1 | 0 | 0 | X == Y
500 // 1 | 1 | 1 | unordered
501 //
502 switch (SetCCOpcode) {
503 default: assert(0 && "Invalid FP setcc!");
504 case ISD::SETUEQ:
505 case ISD::SETEQ:
506 Opc = X86::SETEr; // True if ZF = 1
507 break;
508 case ISD::SETOGT:
509 case ISD::SETGT:
510 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
511 break;
512 case ISD::SETOGE:
513 case ISD::SETGE:
514 Opc = X86::SETAEr; // True if CF = 0
515 break;
516 case ISD::SETULT:
517 case ISD::SETLT:
518 Opc = X86::SETBr; // True if CF = 1
519 break;
520 case ISD::SETULE:
521 case ISD::SETLE:
522 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
523 break;
524 case ISD::SETONE:
525 case ISD::SETNE:
526 Opc = X86::SETNEr; // True if ZF = 0
527 break;
528 case ISD::SETUO:
529 Opc = X86::SETPr; // True if PF = 1
530 break;
531 case ISD::SETO:
532 Opc = X86::SETNPr; // True if PF = 0
533 break;
534 case ISD::SETOEQ: // !PF & ZF
535 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
536 return;
537 case ISD::SETOLT: // !PF & CF
538 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
539 return;
540 case ISD::SETOLE: // !PF & (CF || ZF)
541 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
542 return;
543 case ISD::SETUGT: // PF | (!ZF & !CF)
544 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
545 return;
546 case ISD::SETUGE: // PF | !CF
547 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
548 return;
549 case ISD::SETUNE: // PF | !ZF
550 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
551 return;
552 }
553 }
554 BuildMI(BB, Opc, 0, DestReg);
555}
556
557
558/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
559/// the Dest block if the Cond condition is true. If we cannot fold this
560/// condition into the branch, return true.
561///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000562bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
563 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000564 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
565 // B) using two conditional branches instead of one condbr, two setcc's, and
566 // an or.
567 if ((Cond.getOpcode() == ISD::OR ||
568 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
569 // And and or set the flags for us, so there is no need to emit a TST of the
570 // result. It is only safe to do this if there is only a single use of the
571 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000572 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000573 SelectExpr(Cond);
574 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
575 return false;
576 }
577
578 // Codegen br not C -> JE.
579 if (Cond.getOpcode() == ISD::XOR)
580 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
581 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000582 unsigned CondR;
583 if (getRegPressure(Chain) > getRegPressure(Cond)) {
584 Select(Chain);
585 CondR = SelectExpr(Cond.Val->getOperand(0));
586 } else {
587 CondR = SelectExpr(Cond.Val->getOperand(0));
588 Select(Chain);
589 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000590 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
591 BuildMI(BB, X86::JE, 1).addMBB(Dest);
592 return false;
593 }
594
595 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
596 if (SetCC == 0)
597 return true; // Can only handle simple setcc's so far.
598
599 unsigned Opc;
600
601 // Handle integer conditions first.
602 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
603 switch (SetCC->getCondition()) {
604 default: assert(0 && "Illegal integer SetCC!");
605 case ISD::SETEQ: Opc = X86::JE; break;
606 case ISD::SETGT: Opc = X86::JG; break;
607 case ISD::SETGE: Opc = X86::JGE; break;
608 case ISD::SETLT: Opc = X86::JL; break;
609 case ISD::SETLE: Opc = X86::JLE; break;
610 case ISD::SETNE: Opc = X86::JNE; break;
611 case ISD::SETULT: Opc = X86::JB; break;
612 case ISD::SETUGT: Opc = X86::JA; break;
613 case ISD::SETULE: Opc = X86::JBE; break;
614 case ISD::SETUGE: Opc = X86::JAE; break;
615 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000616 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000617 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
618 BuildMI(BB, Opc, 1).addMBB(Dest);
619 return false;
620 }
621
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000622 unsigned Opc2 = 0; // Second branch if needed.
623
624 // On a floating point condition, the flags are set as follows:
625 // ZF PF CF op
626 // 0 | 0 | 0 | X > Y
627 // 0 | 0 | 1 | X < Y
628 // 1 | 0 | 0 | X == Y
629 // 1 | 1 | 1 | unordered
630 //
631 switch (SetCC->getCondition()) {
632 default: assert(0 && "Invalid FP setcc!");
633 case ISD::SETUEQ:
634 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
635 case ISD::SETOGT:
636 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
637 case ISD::SETOGE:
638 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
639 case ISD::SETULT:
640 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
641 case ISD::SETULE:
642 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
643 case ISD::SETONE:
644 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
645 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
646 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
647 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
648 Opc = X86::JA; // ZF = 0 & CF = 0
649 Opc2 = X86::JP; // PF = 1
650 break;
651 case ISD::SETUGE: // PF = 1 | CF = 0
652 Opc = X86::JAE; // CF = 0
653 Opc2 = X86::JP; // PF = 1
654 break;
655 case ISD::SETUNE: // PF = 1 | ZF = 0
656 Opc = X86::JNE; // ZF = 0
657 Opc2 = X86::JP; // PF = 1
658 break;
659 case ISD::SETOEQ: // PF = 0 & ZF = 1
660 //X86::JNP, X86::JE
661 //X86::AND8rr
662 return true; // FIXME: Emit more efficient code for this branch.
663 case ISD::SETOLT: // PF = 0 & CF = 1
664 //X86::JNP, X86::JB
665 //X86::AND8rr
666 return true; // FIXME: Emit more efficient code for this branch.
667 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
668 //X86::JNP, X86::JBE
669 //X86::AND8rr
670 return true; // FIXME: Emit more efficient code for this branch.
671 }
672
Chris Lattner6c07aee2005-01-11 04:06:27 +0000673 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000674 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
675 BuildMI(BB, Opc, 1).addMBB(Dest);
676 if (Opc2)
677 BuildMI(BB, Opc2, 1).addMBB(Dest);
678 return false;
679}
680
Chris Lattner24aad1b2005-01-10 22:10:13 +0000681/// EmitSelectCC - Emit code into BB that performs a select operation between
682/// the two registers RTrue and RFalse, generating a result into RDest. Return
683/// true if the fold cannot be performed.
684///
685void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
686 unsigned RTrue, unsigned RFalse, unsigned RDest) {
687 enum Condition {
688 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
689 NOT_SET
690 } CondCode = NOT_SET;
691
692 static const unsigned CMOVTAB16[] = {
693 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
694 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
695 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
696 };
697 static const unsigned CMOVTAB32[] = {
698 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
699 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
700 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
701 };
702 static const unsigned CMOVTABFP[] = {
703 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
704 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
705 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
706 };
707
708 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
709 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
710 switch (SetCC->getCondition()) {
711 default: assert(0 && "Unknown integer comparison!");
712 case ISD::SETEQ: CondCode = EQ; break;
713 case ISD::SETGT: CondCode = GT; break;
714 case ISD::SETGE: CondCode = GE; break;
715 case ISD::SETLT: CondCode = LT; break;
716 case ISD::SETLE: CondCode = LE; break;
717 case ISD::SETNE: CondCode = NE; break;
718 case ISD::SETULT: CondCode = B; break;
719 case ISD::SETUGT: CondCode = A; break;
720 case ISD::SETULE: CondCode = BE; break;
721 case ISD::SETUGE: CondCode = AE; break;
722 }
723 } else {
724 // On a floating point condition, the flags are set as follows:
725 // ZF PF CF op
726 // 0 | 0 | 0 | X > Y
727 // 0 | 0 | 1 | X < Y
728 // 1 | 0 | 0 | X == Y
729 // 1 | 1 | 1 | unordered
730 //
731 switch (SetCC->getCondition()) {
732 default: assert(0 && "Unknown FP comparison!");
733 case ISD::SETUEQ:
734 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
735 case ISD::SETOGT:
736 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
737 case ISD::SETOGE:
738 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
739 case ISD::SETULT:
740 case ISD::SETLT: CondCode = B; break; // True if CF = 1
741 case ISD::SETULE:
742 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
743 case ISD::SETONE:
744 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
745 case ISD::SETUO: CondCode = P; break; // True if PF = 1
746 case ISD::SETO: CondCode = NP; break; // True if PF = 0
747 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
748 case ISD::SETUGE: // PF = 1 | CF = 0
749 case ISD::SETUNE: // PF = 1 | ZF = 0
750 case ISD::SETOEQ: // PF = 0 & ZF = 1
751 case ISD::SETOLT: // PF = 0 & CF = 1
752 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
753 // We cannot emit this comparison as a single cmov.
754 break;
755 }
756 }
757 }
758
759 unsigned Opc = 0;
760 if (CondCode != NOT_SET) {
761 switch (SVT) {
762 default: assert(0 && "Cannot select this type!");
763 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
764 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
765 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000766 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000767 }
768 }
769
770 // Finally, if we weren't able to fold this, just emit the condition and test
771 // it.
772 if (CondCode == NOT_SET || Opc == 0) {
773 // Get the condition into the zero flag.
774 unsigned CondReg = SelectExpr(Cond);
775 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
776
777 switch (SVT) {
778 default: assert(0 && "Cannot select this type!");
779 case MVT::i16: Opc = X86::CMOVE16rr; break;
780 case MVT::i32: Opc = X86::CMOVE32rr; break;
781 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000782 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000783 }
784 } else {
785 // FIXME: CMP R, 0 -> TEST R, R
786 EmitCMP(Cond.getOperand(0), Cond.getOperand(1));
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000787 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000788 }
789 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
790}
791
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000792void ISel::EmitCMP(SDOperand LHS, SDOperand RHS) {
Chris Lattner11333092005-01-11 03:11:44 +0000793 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000794 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
795 Opc = 0;
796 switch (RHS.getValueType()) {
797 default: break;
798 case MVT::i1:
799 case MVT::i8: Opc = X86::CMP8ri; break;
800 case MVT::i16: Opc = X86::CMP16ri; break;
801 case MVT::i32: Opc = X86::CMP32ri; break;
802 }
803 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +0000804 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000805 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
806 return;
807 }
808 }
809
810 switch (LHS.getValueType()) {
811 default: assert(0 && "Cannot compare this value!");
812 case MVT::i1:
813 case MVT::i8: Opc = X86::CMP8rr; break;
814 case MVT::i16: Opc = X86::CMP16rr; break;
815 case MVT::i32: Opc = X86::CMP32rr; break;
816 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000817 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000818 }
Chris Lattner11333092005-01-11 03:11:44 +0000819 unsigned Tmp1, Tmp2;
820 if (getRegPressure(LHS) > getRegPressure(RHS)) {
821 Tmp1 = SelectExpr(LHS);
822 Tmp2 = SelectExpr(RHS);
823 } else {
824 Tmp2 = SelectExpr(RHS);
825 Tmp1 = SelectExpr(LHS);
826 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000827 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
828}
829
830unsigned ISel::SelectExpr(SDOperand N) {
831 unsigned Result;
832 unsigned Tmp1, Tmp2, Tmp3;
833 unsigned Opc = 0;
834
Chris Lattner5188ad72005-01-08 19:28:19 +0000835 SDNode *Node = N.Val;
836
Chris Lattner590d8002005-01-09 18:52:44 +0000837 if (Node->getOpcode() == ISD::CopyFromReg)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000838 // Just use the specified register as our input.
Chris Lattner590d8002005-01-09 18:52:44 +0000839 return dyn_cast<CopyRegSDNode>(Node)->getReg();
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000840
841 // If there are multiple uses of this expression, memorize the
842 // register it is code generated in, instead of emitting it multiple
843 // times.
844 // FIXME: Disabled for our current selection model.
Chris Lattner5188ad72005-01-08 19:28:19 +0000845 if (1 || !Node->hasOneUse()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000846 unsigned &Reg = ExprMap[N];
847 if (Reg) return Reg;
848
849 if (N.getOpcode() != ISD::CALL)
850 Reg = Result = (N.getValueType() != MVT::Other) ?
851 MakeReg(N.getValueType()) : 1;
852 else {
853 // If this is a call instruction, make sure to prepare ALL of the result
854 // values as well as the chain.
Chris Lattner5188ad72005-01-08 19:28:19 +0000855 if (Node->getNumValues() == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000856 Reg = Result = 1; // Void call, just a chain.
857 else {
Chris Lattner5188ad72005-01-08 19:28:19 +0000858 Result = MakeReg(Node->getValueType(0));
859 ExprMap[N.getValue(0)] = Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000860 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Chris Lattner5188ad72005-01-08 19:28:19 +0000861 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
862 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000863 }
864 }
865 } else {
866 Result = MakeReg(N.getValueType());
867 }
868
869 switch (N.getOpcode()) {
870 default:
Chris Lattner5188ad72005-01-08 19:28:19 +0000871 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000872 assert(0 && "Node not handled!\n");
873 case ISD::FrameIndex:
874 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
875 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
876 return Result;
877 case ISD::ConstantPool:
878 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
879 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
880 return Result;
881 case ISD::ConstantFP:
882 ContainsFPCode = true;
883 Tmp1 = Result; // Intermediate Register
884 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
885 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
886 Tmp1 = MakeReg(MVT::f64);
887
888 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
889 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
890 BuildMI(BB, X86::FLD0, 0, Tmp1);
891 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
892 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
893 BuildMI(BB, X86::FLD1, 0, Tmp1);
894 else
895 assert(0 && "Unexpected constant!");
896 if (Tmp1 != Result)
897 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
898 return Result;
899 case ISD::Constant:
900 switch (N.getValueType()) {
901 default: assert(0 && "Cannot use constants of this type!");
902 case MVT::i1:
903 case MVT::i8: Opc = X86::MOV8ri; break;
904 case MVT::i16: Opc = X86::MOV16ri; break;
905 case MVT::i32: Opc = X86::MOV32ri; break;
906 }
907 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
908 return Result;
909 case ISD::GlobalAddress: {
910 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
911 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
912 return Result;
913 }
914 case ISD::ExternalSymbol: {
915 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
916 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
917 return Result;
918 }
919 case ISD::FP_EXTEND:
920 Tmp1 = SelectExpr(N.getOperand(0));
921 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000922 return Result;
923 case ISD::ZERO_EXTEND: {
924 int DestIs16 = N.getValueType() == MVT::i16;
925 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +0000926 Tmp1 = SelectExpr(N.getOperand(0));
927
928 // FIXME: This hack is here for zero extension casts from bool to i8. This
929 // would not be needed if bools were promoted by Legalize.
930 if (N.getValueType() == MVT::i8) {
931 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
932 return Result;
933 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000934
935 static const unsigned Opc[3] = {
936 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
937 };
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000938 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
939 return Result;
940 }
941 case ISD::SIGN_EXTEND: {
942 int DestIs16 = N.getValueType() == MVT::i16;
943 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
944
Chris Lattner590d8002005-01-09 18:52:44 +0000945 // FIXME: Legalize should promote bools to i8!
946 assert(N.getOperand(0).getValueType() != MVT::i1 &&
947 "Sign extend from bool not implemented!");
948
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000949 static const unsigned Opc[3] = {
950 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
951 };
952 Tmp1 = SelectExpr(N.getOperand(0));
953 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
954 return Result;
955 }
956 case ISD::TRUNCATE:
957 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
958 // a move out of AX or AL.
959 switch (N.getOperand(0).getValueType()) {
960 default: assert(0 && "Unknown truncate!");
961 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
962 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
963 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
964 }
965 Tmp1 = SelectExpr(N.getOperand(0));
966 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
967
968 switch (N.getValueType()) {
969 default: assert(0 && "Unknown truncate!");
970 case MVT::i1:
971 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
972 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
973 }
974 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
975 return Result;
976
977 case ISD::FP_ROUND:
978 // Truncate from double to float by storing to memory as float,
979 // then reading it back into a register.
980
981 // Create as stack slot to use.
Chris Lattner590d8002005-01-09 18:52:44 +0000982 // FIXME: This should automatically be made by the Legalizer!
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000983 Tmp1 = TLI.getTargetData().getFloatAlignment();
984 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
985
986 // Codegen the input.
987 Tmp1 = SelectExpr(N.getOperand(0));
988
989 // Emit the store, then the reload.
990 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
991 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000992 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +0000993
994 case ISD::SINT_TO_FP:
995 case ISD::UINT_TO_FP: {
996 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +0000997 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +0000998
999 // Promote the integer to a type supported by FLD. We do this because there
1000 // are no unsigned FLD instructions, so we must promote an unsigned value to
1001 // a larger signed value, then use FLD on the larger value.
1002 //
1003 MVT::ValueType PromoteType = MVT::Other;
1004 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1005 unsigned PromoteOpcode = 0;
1006 unsigned RealDestReg = Result;
1007 switch (SrcTy) {
1008 case MVT::i1:
1009 case MVT::i8:
1010 // We don't have the facilities for directly loading byte sized data from
1011 // memory (even signed). Promote it to 16 bits.
1012 PromoteType = MVT::i16;
1013 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1014 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1015 break;
1016 case MVT::i16:
1017 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1018 PromoteType = MVT::i32;
1019 PromoteOpcode = X86::MOVZX32rr16;
1020 }
1021 break;
1022 default:
1023 // Don't fild into the real destination.
1024 if (Node->getOpcode() == ISD::UINT_TO_FP)
1025 Result = MakeReg(Node->getValueType(0));
1026 break;
1027 }
1028
1029 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1030
1031 if (PromoteType != MVT::Other) {
1032 Tmp2 = MakeReg(PromoteType);
1033 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1034 SrcTy = PromoteType;
1035 Tmp1 = Tmp2;
1036 }
1037
1038 // Spill the integer to memory and reload it from there.
1039 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1040 MachineFunction *F = BB->getParent();
1041 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1042
1043 switch (SrcTy) {
1044 case MVT::i64:
1045 // FIXME: this won't work for cast [u]long to FP
1046 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1047 FrameIdx).addReg(Tmp1);
1048 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1049 FrameIdx, 4).addReg(Tmp1+1);
1050 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1051 break;
1052 case MVT::i32:
1053 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1054 FrameIdx).addReg(Tmp1);
1055 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1056 break;
1057 case MVT::i16:
1058 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1059 FrameIdx).addReg(Tmp1);
1060 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1061 break;
1062 default: break; // No promotion required.
1063 }
1064
1065 if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i32) {
1066 // If this is a cast from uint -> double, we need to be careful when if
1067 // the "sign" bit is set. If so, we don't want to make a negative number,
1068 // we want to make a positive number. Emit code to add an offset if the
1069 // sign bit is set.
1070
1071 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1072 unsigned IsNeg = MakeReg(MVT::i32);
1073 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1074
1075 // Create a CP value that has the offset in one word and 0 in the other.
1076 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1077 0x4f80000000000000ULL);
1078 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1079 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1080 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1081
1082 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1083 // We need special handling for unsigned 64-bit integer sources. If the
1084 // input number has the "sign bit" set, then we loaded it incorrectly as a
1085 // negative 64-bit number. In this case, add an offset value.
1086
1087 // Emit a test instruction to see if the dynamic input value was signed.
1088 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1089
1090 // If the sign bit is set, get a pointer to an offset, otherwise get a
1091 // pointer to a zero.
1092 MachineConstantPool *CP = F->getConstantPool();
1093 unsigned Zero = MakeReg(MVT::i32);
1094 Constant *Null = Constant::getNullValue(Type::UIntTy);
1095 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1096 CP->getConstantPoolIndex(Null));
1097 unsigned Offset = MakeReg(MVT::i32);
1098 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1099
1100 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1101 CP->getConstantPoolIndex(OffsetCst));
1102 unsigned Addr = MakeReg(MVT::i32);
1103 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1104
1105 // Load the constant for an add. FIXME: this could make an 'fadd' that
1106 // reads directly from memory, but we don't support these yet.
1107 unsigned ConstReg = MakeReg(MVT::f64);
1108 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1109
1110 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1111 }
1112 return RealDestReg;
1113 }
1114 case ISD::FP_TO_SINT:
1115 case ISD::FP_TO_UINT: {
1116 // FIXME: Most of this grunt work should be done by legalize!
1117 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1118
1119 // Change the floating point control register to use "round towards zero"
1120 // mode when truncating to an integer value.
1121 //
1122 MachineFunction *F = BB->getParent();
1123 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1124 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1125
1126 // Load the old value of the high byte of the control word...
1127 unsigned HighPartOfCW = MakeReg(MVT::i8);
1128 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1129 CWFrameIdx, 1);
1130
1131 // Set the high part to be round to zero...
1132 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1133 CWFrameIdx, 1).addImm(12);
1134
1135 // Reload the modified control word now...
1136 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1137
1138 // Restore the memory image of control word to original value
1139 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1140 CWFrameIdx, 1).addReg(HighPartOfCW);
1141
1142 // We don't have the facilities for directly storing byte sized data to
1143 // memory. Promote it to 16 bits. We also must promote unsigned values to
1144 // larger classes because we only have signed FP stores.
1145 MVT::ValueType StoreClass = Node->getValueType(0);
1146 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1147 switch (StoreClass) {
1148 case MVT::i8: StoreClass = MVT::i16; break;
1149 case MVT::i16: StoreClass = MVT::i32; break;
1150 case MVT::i32: StoreClass = MVT::i64; break;
1151 // The following treatment of cLong may not be perfectly right,
1152 // but it survives chains of casts of the form
1153 // double->ulong->double.
1154 case MVT::i64: StoreClass = MVT::i64; break;
1155 default: assert(0 && "Unknown store class!");
1156 }
1157
1158 // Spill the integer to memory and reload it from there.
1159 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1160 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1161
1162 switch (StoreClass) {
1163 default: assert(0 && "Unknown store class!");
1164 case MVT::i16:
1165 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1166 break;
1167 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001168 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001169 break;
1170 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001171 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001172 break;
1173 }
1174
1175 switch (Node->getValueType(0)) {
1176 default:
1177 assert(0 && "Unknown integer type!");
1178 case MVT::i64:
1179 // FIXME: this isn't gunna work.
1180 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1181 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1182 case MVT::i32:
1183 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1184 break;
1185 case MVT::i16:
1186 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1187 break;
1188 case MVT::i8:
1189 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1190 break;
1191 }
1192
1193 // Reload the original control word now.
1194 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1195 return Result;
1196 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001197 case ISD::ADD:
1198 // See if we can codegen this as an LEA to fold operations together.
1199 if (N.getValueType() == MVT::i32) {
1200 X86AddressMode AM;
1201 if (!SelectAddress(N.getOperand(0), AM) &&
1202 !SelectAddress(N.getOperand(1), AM)) {
1203 // If this is not just an add, emit the LEA. For a simple add (like
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001204 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001205 // leave this as LEA, then peephole it to 'ADD' after two address elim
1206 // happens.
1207 if (AM.Scale != 1 || AM.BaseType == X86AddressMode::FrameIndexBase ||
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001208 AM.GV || (AM.Base.Reg && AM.IndexReg && AM.Disp)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001209 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1210 return Result;
1211 }
1212 }
1213 }
Chris Lattner11333092005-01-11 03:11:44 +00001214
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001215 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1216 Opc = 0;
1217 if (CN->getValue() == 1) { // add X, 1 -> inc X
1218 switch (N.getValueType()) {
1219 default: assert(0 && "Cannot integer add this type!");
1220 case MVT::i8: Opc = X86::INC8r; break;
1221 case MVT::i16: Opc = X86::INC16r; break;
1222 case MVT::i32: Opc = X86::INC32r; break;
1223 }
1224 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1225 switch (N.getValueType()) {
1226 default: assert(0 && "Cannot integer add this type!");
1227 case MVT::i8: Opc = X86::DEC8r; break;
1228 case MVT::i16: Opc = X86::DEC16r; break;
1229 case MVT::i32: Opc = X86::DEC32r; break;
1230 }
1231 }
1232
1233 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001234 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001235 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1236 return Result;
1237 }
1238
1239 switch (N.getValueType()) {
1240 default: assert(0 && "Cannot add this type!");
1241 case MVT::i8: Opc = X86::ADD8ri; break;
1242 case MVT::i16: Opc = X86::ADD16ri; break;
1243 case MVT::i32: Opc = X86::ADD32ri; break;
1244 }
1245 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001246 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001247 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1248 return Result;
1249 }
1250 }
1251
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001252 switch (N.getValueType()) {
1253 default: assert(0 && "Cannot add this type!");
1254 case MVT::i8: Opc = X86::ADD8rr; break;
1255 case MVT::i16: Opc = X86::ADD16rr; break;
1256 case MVT::i32: Opc = X86::ADD32rr; break;
1257 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001258 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001259 }
Chris Lattner11333092005-01-11 03:11:44 +00001260
1261 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1262 Tmp1 = SelectExpr(N.getOperand(0));
1263 Tmp2 = SelectExpr(N.getOperand(1));
1264 } else {
1265 Tmp2 = SelectExpr(N.getOperand(1));
1266 Tmp1 = SelectExpr(N.getOperand(0));
1267 }
1268
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001269 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1270 return Result;
1271 case ISD::SUB:
1272 if (MVT::isInteger(N.getValueType()))
1273 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1274 if (CN->isNullValue()) { // 0 - N -> neg N
1275 switch (N.getValueType()) {
1276 default: assert(0 && "Cannot sub this type!");
1277 case MVT::i1:
1278 case MVT::i8: Opc = X86::NEG8r; break;
1279 case MVT::i16: Opc = X86::NEG16r; break;
1280 case MVT::i32: Opc = X86::NEG32r; break;
1281 }
1282 Tmp1 = SelectExpr(N.getOperand(1));
1283 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1284 return Result;
1285 }
1286
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001287 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1288 switch (N.getValueType()) {
1289 default: assert(0 && "Cannot sub this type!");
1290 case MVT::i1:
1291 case MVT::i8: Opc = X86::SUB8ri; break;
1292 case MVT::i16: Opc = X86::SUB16ri; break;
1293 case MVT::i32: Opc = X86::SUB32ri; break;
1294 }
Chris Lattner11333092005-01-11 03:11:44 +00001295 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001296 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1297 return Result;
1298 }
Chris Lattner11333092005-01-11 03:11:44 +00001299
1300 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1301 Tmp1 = SelectExpr(N.getOperand(0));
1302 Tmp2 = SelectExpr(N.getOperand(1));
1303 } else {
1304 Tmp2 = SelectExpr(N.getOperand(1));
1305 Tmp1 = SelectExpr(N.getOperand(0));
1306 }
1307
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001308 switch (N.getValueType()) {
1309 default: assert(0 && "Cannot add this type!");
1310 case MVT::i1:
1311 case MVT::i8: Opc = X86::SUB8rr; break;
1312 case MVT::i16: Opc = X86::SUB16rr; break;
1313 case MVT::i32: Opc = X86::SUB32rr; break;
1314 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001315 case MVT::f64: Opc = X86::FpSUB; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001316 }
1317 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1318 return Result;
1319
1320 case ISD::AND:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001321 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1322 switch (N.getValueType()) {
1323 default: assert(0 && "Cannot add this type!");
1324 case MVT::i1:
1325 case MVT::i8: Opc = X86::AND8ri; break;
1326 case MVT::i16: Opc = X86::AND16ri; break;
1327 case MVT::i32: Opc = X86::AND32ri; break;
1328 }
Chris Lattner11333092005-01-11 03:11:44 +00001329 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001330 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1331 return Result;
1332 }
Chris Lattner11333092005-01-11 03:11:44 +00001333
1334 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1335 Tmp1 = SelectExpr(N.getOperand(0));
1336 Tmp2 = SelectExpr(N.getOperand(1));
1337 } else {
1338 Tmp2 = SelectExpr(N.getOperand(1));
1339 Tmp1 = SelectExpr(N.getOperand(0));
1340 }
1341
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001342 switch (N.getValueType()) {
1343 default: assert(0 && "Cannot add this type!");
1344 case MVT::i1:
1345 case MVT::i8: Opc = X86::AND8rr; break;
1346 case MVT::i16: Opc = X86::AND16rr; break;
1347 case MVT::i32: Opc = X86::AND32rr; break;
1348 }
1349 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1350 return Result;
1351 case ISD::OR:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001352 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001353 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001354 switch (N.getValueType()) {
1355 default: assert(0 && "Cannot add this type!");
1356 case MVT::i1:
1357 case MVT::i8: Opc = X86::OR8ri; break;
1358 case MVT::i16: Opc = X86::OR16ri; break;
1359 case MVT::i32: Opc = X86::OR32ri; break;
1360 }
1361 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1362 return Result;
1363 }
Chris Lattner11333092005-01-11 03:11:44 +00001364
1365 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1366 Tmp1 = SelectExpr(N.getOperand(0));
1367 Tmp2 = SelectExpr(N.getOperand(1));
1368 } else {
1369 Tmp2 = SelectExpr(N.getOperand(1));
1370 Tmp1 = SelectExpr(N.getOperand(0));
1371 }
1372
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001373 switch (N.getValueType()) {
1374 default: assert(0 && "Cannot add this type!");
1375 case MVT::i1:
1376 case MVT::i8: Opc = X86::OR8rr; break;
1377 case MVT::i16: Opc = X86::OR16rr; break;
1378 case MVT::i32: Opc = X86::OR32rr; break;
1379 }
1380 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1381 return Result;
1382 case ISD::XOR:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001383 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001384 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001385 switch (N.getValueType()) {
1386 default: assert(0 && "Cannot add this type!");
1387 case MVT::i1:
1388 case MVT::i8: Opc = X86::XOR8ri; break;
1389 case MVT::i16: Opc = X86::XOR16ri; break;
1390 case MVT::i32: Opc = X86::XOR32ri; break;
1391 }
1392 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1393 return Result;
1394 }
Chris Lattner11333092005-01-11 03:11:44 +00001395
1396 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1397 Tmp1 = SelectExpr(N.getOperand(0));
1398 Tmp2 = SelectExpr(N.getOperand(1));
1399 } else {
1400 Tmp2 = SelectExpr(N.getOperand(1));
1401 Tmp1 = SelectExpr(N.getOperand(0));
1402 }
1403
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001404 switch (N.getValueType()) {
1405 default: assert(0 && "Cannot add this type!");
1406 case MVT::i1:
1407 case MVT::i8: Opc = X86::XOR8rr; break;
1408 case MVT::i16: Opc = X86::XOR16rr; break;
1409 case MVT::i32: Opc = X86::XOR32rr; break;
1410 }
1411 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1412 return Result;
1413
1414 case ISD::MUL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001415 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1416 Opc = 0;
1417 switch (N.getValueType()) {
1418 default: assert(0 && "Cannot multiply this type!");
1419 case MVT::i8: break;
1420 case MVT::i16: Opc = X86::IMUL16rri; break;
1421 case MVT::i32: Opc = X86::IMUL32rri; break;
1422 }
1423 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001424 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001425 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1426 return Result;
1427 }
1428 }
Chris Lattner11333092005-01-11 03:11:44 +00001429
1430 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1431 Tmp1 = SelectExpr(N.getOperand(0));
1432 Tmp2 = SelectExpr(N.getOperand(1));
1433 } else {
1434 Tmp2 = SelectExpr(N.getOperand(1));
1435 Tmp1 = SelectExpr(N.getOperand(0));
1436 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001437 switch (N.getValueType()) {
1438 default: assert(0 && "Cannot add this type!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001439 case MVT::i8:
1440 // Must use the MUL instruction, which forces use of AL.
1441 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1442 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1443 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1444 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001445 case MVT::i16: Opc = X86::IMUL16rr; break;
1446 case MVT::i32: Opc = X86::IMUL32rr; break;
1447 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001448 case MVT::f64: Opc = X86::FpMUL; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001449 }
1450 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1451 return Result;
1452
1453 case ISD::SELECT:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001454 if (N.getValueType() != MVT::i1 && N.getValueType() != MVT::i8) {
Chris Lattner11333092005-01-11 03:11:44 +00001455 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1456 Tmp2 = SelectExpr(N.getOperand(1));
1457 Tmp3 = SelectExpr(N.getOperand(2));
1458 } else {
1459 Tmp3 = SelectExpr(N.getOperand(2));
1460 Tmp2 = SelectExpr(N.getOperand(1));
1461 }
Chris Lattner24aad1b2005-01-10 22:10:13 +00001462 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001463 return Result;
1464 } else {
1465 // FIXME: This should not be implemented here, it should be in the generic
1466 // code!
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001467 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1468 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1469 N.getOperand(1)));
1470 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1471 N.getOperand(2)));
1472 } else {
1473 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1474 N.getOperand(2)));
1475 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1476 N.getOperand(1)));
1477 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001478 unsigned TmpReg = MakeReg(MVT::i16);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001479 EmitSelectCC(N.getOperand(0), MVT::i16, Tmp2, Tmp3, TmpReg);
1480 // FIXME: need subregs to do better than this!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001481 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(TmpReg);
1482 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1483 return Result;
1484 }
1485
1486 case ISD::SDIV:
1487 case ISD::UDIV:
1488 case ISD::SREM:
1489 case ISD::UREM: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001490 if (N.getOpcode() == ISD::SDIV)
1491 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1492 // FIXME: These special cases should be handled by the lowering impl!
1493 unsigned RHS = CN->getValue();
1494 bool isNeg = false;
1495 if ((int)RHS < 0) {
1496 isNeg = true;
1497 RHS = -RHS;
1498 }
1499 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1500 unsigned Log = log2(RHS);
1501 unsigned TmpReg = MakeReg(N.getValueType());
1502 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1503 switch (N.getValueType()) {
1504 default: assert("Unknown type to signed divide!");
1505 case MVT::i8:
1506 SAROpc = X86::SAR8ri;
1507 SHROpc = X86::SHR8ri;
1508 ADDOpc = X86::ADD8rr;
1509 NEGOpc = X86::NEG8r;
1510 break;
1511 case MVT::i16:
1512 SAROpc = X86::SAR16ri;
1513 SHROpc = X86::SHR16ri;
1514 ADDOpc = X86::ADD16rr;
1515 NEGOpc = X86::NEG16r;
1516 break;
1517 case MVT::i32:
1518 SAROpc = X86::SAR32ri;
1519 SHROpc = X86::SHR32ri;
1520 ADDOpc = X86::ADD32rr;
1521 NEGOpc = X86::NEG32r;
1522 break;
1523 }
Chris Lattner11333092005-01-11 03:11:44 +00001524 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001525 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1526 unsigned TmpReg2 = MakeReg(N.getValueType());
1527 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1528 unsigned TmpReg3 = MakeReg(N.getValueType());
1529 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1530
1531 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1532 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1533 if (isNeg)
1534 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1535 return Result;
1536 }
1537 }
1538
Chris Lattner11333092005-01-11 03:11:44 +00001539 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1540 Tmp1 = SelectExpr(N.getOperand(0));
1541 Tmp2 = SelectExpr(N.getOperand(1));
1542 } else {
1543 Tmp2 = SelectExpr(N.getOperand(1));
1544 Tmp1 = SelectExpr(N.getOperand(0));
1545 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001546
1547 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1548 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1549 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1550 switch (N.getValueType()) {
1551 default: assert(0 && "Cannot sdiv this type!");
1552 case MVT::i8:
1553 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1554 LoReg = X86::AL;
1555 HiReg = X86::AH;
1556 MovOpcode = X86::MOV8rr;
1557 ClrOpcode = X86::MOV8ri;
1558 SExtOpcode = X86::CBW;
1559 break;
1560 case MVT::i16:
1561 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1562 LoReg = X86::AX;
1563 HiReg = X86::DX;
1564 MovOpcode = X86::MOV16rr;
1565 ClrOpcode = X86::MOV16ri;
1566 SExtOpcode = X86::CWD;
1567 break;
1568 case MVT::i32:
1569 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
1570 LoReg =X86::EAX;
1571 HiReg = X86::EDX;
1572 MovOpcode = X86::MOV32rr;
1573 ClrOpcode = X86::MOV32ri;
1574 SExtOpcode = X86::CDQ;
1575 break;
1576 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1577 case MVT::f32:
1578 case MVT::f64:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001579 if (N.getOpcode() == ISD::SDIV)
1580 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1581 else
1582 assert(0 && "FIXME: Emit frem libcall to fmod!");
1583 return Result;
1584 }
1585
1586 // Set up the low part.
1587 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1588
1589 if (isSigned) {
1590 // Sign extend the low part into the high part.
1591 BuildMI(BB, SExtOpcode, 0);
1592 } else {
1593 // Zero out the high part, effectively zero extending the input.
1594 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1595 }
1596
1597 // Emit the DIV/IDIV instruction.
1598 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1599
1600 // Get the result of the divide or rem.
1601 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1602 return Result;
1603 }
1604
1605 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001606 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1607 switch (N.getValueType()) {
1608 default: assert(0 && "Cannot shift this type!");
1609 case MVT::i8: Opc = X86::SHL8ri; break;
1610 case MVT::i16: Opc = X86::SHL16ri; break;
1611 case MVT::i32: Opc = X86::SHL32ri; break;
1612 }
Chris Lattner11333092005-01-11 03:11:44 +00001613 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001614 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1615 return Result;
1616 }
Chris Lattner11333092005-01-11 03:11:44 +00001617
1618 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1619 Tmp1 = SelectExpr(N.getOperand(0));
1620 Tmp2 = SelectExpr(N.getOperand(1));
1621 } else {
1622 Tmp2 = SelectExpr(N.getOperand(1));
1623 Tmp1 = SelectExpr(N.getOperand(0));
1624 }
1625
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001626 switch (N.getValueType()) {
1627 default: assert(0 && "Cannot shift this type!");
1628 case MVT::i8 : Opc = X86::SHL8rCL; break;
1629 case MVT::i16: Opc = X86::SHL16rCL; break;
1630 case MVT::i32: Opc = X86::SHL32rCL; break;
1631 }
1632 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1633 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1634 return Result;
1635 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001636 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1637 switch (N.getValueType()) {
1638 default: assert(0 && "Cannot shift this type!");
1639 case MVT::i8: Opc = X86::SHR8ri; break;
1640 case MVT::i16: Opc = X86::SHR16ri; break;
1641 case MVT::i32: Opc = X86::SHR32ri; break;
1642 }
Chris Lattner11333092005-01-11 03:11:44 +00001643 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001644 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1645 return Result;
1646 }
Chris Lattner11333092005-01-11 03:11:44 +00001647
1648 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1649 Tmp1 = SelectExpr(N.getOperand(0));
1650 Tmp2 = SelectExpr(N.getOperand(1));
1651 } else {
1652 Tmp2 = SelectExpr(N.getOperand(1));
1653 Tmp1 = SelectExpr(N.getOperand(0));
1654 }
1655
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001656 switch (N.getValueType()) {
1657 default: assert(0 && "Cannot shift this type!");
1658 case MVT::i8 : Opc = X86::SHR8rCL; break;
1659 case MVT::i16: Opc = X86::SHR16rCL; break;
1660 case MVT::i32: Opc = X86::SHR32rCL; break;
1661 }
1662 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1663 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1664 return Result;
1665 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001666 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1667 switch (N.getValueType()) {
1668 default: assert(0 && "Cannot shift this type!");
1669 case MVT::i8: Opc = X86::SAR8ri; break;
1670 case MVT::i16: Opc = X86::SAR16ri; break;
1671 case MVT::i32: Opc = X86::SAR32ri; break;
1672 }
Chris Lattner11333092005-01-11 03:11:44 +00001673 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001674 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1675 return Result;
1676 }
Chris Lattner11333092005-01-11 03:11:44 +00001677
1678 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1679 Tmp1 = SelectExpr(N.getOperand(0));
1680 Tmp2 = SelectExpr(N.getOperand(1));
1681 } else {
1682 Tmp2 = SelectExpr(N.getOperand(1));
1683 Tmp1 = SelectExpr(N.getOperand(0));
1684 }
1685
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001686 switch (N.getValueType()) {
1687 default: assert(0 && "Cannot shift this type!");
1688 case MVT::i8 : Opc = X86::SAR8rCL; break;
1689 case MVT::i16: Opc = X86::SAR16rCL; break;
1690 case MVT::i32: Opc = X86::SAR32rCL; break;
1691 }
1692 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1693 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1694 return Result;
1695
1696 case ISD::SETCC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001697 EmitCMP(N.getOperand(0), N.getOperand(1));
1698 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
1699 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
1700 return Result;
1701 case ISD::LOAD: {
Chris Lattner5188ad72005-01-08 19:28:19 +00001702 // The chain for this load is now lowered.
1703 LoweredTokens.insert(SDOperand(Node, 1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001704
1705 // Make sure we generate both values.
1706 if (Result != 1)
1707 ExprMap[N.getValue(1)] = 1; // Generate the token
1708 else
1709 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1710
Chris Lattner5188ad72005-01-08 19:28:19 +00001711 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001712 default: assert(0 && "Cannot load this type!");
1713 case MVT::i1:
1714 case MVT::i8: Opc = X86::MOV8rm; break;
1715 case MVT::i16: Opc = X86::MOV16rm; break;
1716 case MVT::i32: Opc = X86::MOV32rm; break;
1717 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
1718 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
1719 }
Chris Lattner11333092005-01-11 03:11:44 +00001720
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001721 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00001722 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001723 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
1724 } else {
1725 X86AddressMode AM;
Chris Lattner11333092005-01-11 03:11:44 +00001726 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1727 Select(N.getOperand(0));
1728 SelectAddress(N.getOperand(1), AM);
1729 } else {
1730 SelectAddress(N.getOperand(1), AM);
1731 Select(N.getOperand(0));
1732 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001733 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1734 }
1735 return Result;
1736 }
1737 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001738 // Generate both result values.
1739 if (Result != 1)
1740 ExprMap[N.getValue(1)] = 1; // Generate the token
1741 else
1742 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1743
1744 // FIXME: We are currently ignoring the requested alignment for handling
1745 // greater than the stack alignment. This will need to be revisited at some
1746 // point. Align = N.getOperand(2);
1747
1748 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1749 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1750 std::cerr << "Cannot allocate stack object with greater alignment than"
1751 << " the stack alignment yet!";
1752 abort();
1753 }
1754
1755 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001756 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001757 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
1758 .addImm(CN->getValue());
1759 } else {
Chris Lattner11333092005-01-11 03:11:44 +00001760 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1761 Select(N.getOperand(0));
1762 Tmp1 = SelectExpr(N.getOperand(1));
1763 } else {
1764 Tmp1 = SelectExpr(N.getOperand(1));
1765 Select(N.getOperand(0));
1766 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001767
1768 // Subtract size from stack pointer, thereby allocating some space.
1769 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
1770 }
1771
1772 // Put a pointer to the space into the result register, by copying the stack
1773 // pointer.
1774 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
1775 return Result;
1776
1777 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00001778 // The chain for this call is now lowered.
1779 LoweredTokens.insert(N.getValue(Node->getNumValues()-1));
1780
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001781 if (GlobalAddressSDNode *GASD =
1782 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001783 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001784 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
1785 } else if (ExternalSymbolSDNode *ESSDN =
1786 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001787 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001788 BuildMI(BB, X86::CALLpcrel32,
1789 1).addExternalSymbol(ESSDN->getSymbol(), true);
1790 } else {
Chris Lattner11333092005-01-11 03:11:44 +00001791 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1792 Select(N.getOperand(0));
1793 Tmp1 = SelectExpr(N.getOperand(1));
1794 } else {
1795 Tmp1 = SelectExpr(N.getOperand(1));
1796 Select(N.getOperand(0));
1797 }
1798
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001799 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
1800 }
Chris Lattner5188ad72005-01-08 19:28:19 +00001801 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001802 default: assert(0 && "Unknown value type for call result!");
1803 case MVT::Other: return 1;
1804 case MVT::i1:
1805 case MVT::i8:
1806 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1807 break;
1808 case MVT::i16:
1809 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
1810 break;
1811 case MVT::i32:
1812 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00001813 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001814 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
1815 break;
1816 case MVT::f32:
1817 case MVT::f64: // Floating-point return values live in %ST(0)
1818 ContainsFPCode = true;
1819 BuildMI(BB, X86::FpGETRESULT, 1, Result);
1820 break;
1821 }
1822 return Result+N.ResNo;
1823 }
1824
1825 return 0;
1826}
1827
1828void ISel::Select(SDOperand N) {
1829 unsigned Tmp1, Tmp2, Opc;
1830
1831 // FIXME: Disable for our current expansion model!
1832 if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second)
1833 return; // Already selected.
1834
1835 switch (N.getOpcode()) {
1836 default:
1837 N.Val->dump(); std::cerr << "\n";
1838 assert(0 && "Node not handled yet!");
1839 case ISD::EntryToken: return; // Noop
1840 case ISD::CopyToReg:
1841 Select(N.getOperand(0));
1842 Tmp1 = SelectExpr(N.getOperand(1));
1843 Tmp2 = cast<CopyRegSDNode>(N)->getReg();
1844
1845 if (Tmp1 != Tmp2) {
1846 switch (N.getOperand(1).getValueType()) {
1847 default: assert(0 && "Invalid type for operation!");
1848 case MVT::i1:
1849 case MVT::i8: Opc = X86::MOV8rr; break;
1850 case MVT::i16: Opc = X86::MOV16rr; break;
1851 case MVT::i32: Opc = X86::MOV32rr; break;
1852 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001853 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001854 }
1855 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1856 }
1857 return;
1858 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001859 switch (N.getNumOperands()) {
1860 default:
1861 assert(0 && "Unknown return instruction!");
1862 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001863 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1864 N.getOperand(2).getValueType() == MVT::i32 &&
1865 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00001866 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1867 Tmp1 = SelectExpr(N.getOperand(1));
1868 Tmp2 = SelectExpr(N.getOperand(2));
1869 } else {
1870 Tmp2 = SelectExpr(N.getOperand(2));
1871 Tmp1 = SelectExpr(N.getOperand(1));
1872 }
1873 Select(N.getOperand(0));
1874
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001875 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
1876 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
1877 // Declare that EAX & EDX are live on exit.
1878 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
1879 .addReg(X86::ESP);
1880 break;
1881 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00001882 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1883 Select(N.getOperand(0));
1884 Tmp1 = SelectExpr(N.getOperand(1));
1885 } else {
1886 Tmp1 = SelectExpr(N.getOperand(1));
1887 Select(N.getOperand(0));
1888 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001889 switch (N.getOperand(1).getValueType()) {
1890 default: assert(0 && "All other types should have been promoted!!");
1891 case MVT::f64:
1892 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
1893 // Declare that top-of-stack is live on exit
1894 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
1895 break;
1896 case MVT::i32:
1897 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
1898 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
1899 break;
1900 }
1901 break;
1902 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00001903 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001904 break;
1905 }
1906 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
1907 return;
1908 case ISD::BR: {
1909 Select(N.getOperand(0));
1910 MachineBasicBlock *Dest =
1911 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1912 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
1913 return;
1914 }
1915
1916 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001917 MachineBasicBlock *Dest =
1918 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00001919
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001920 // Try to fold a setcc into the branch. If this fails, emit a test/jne
1921 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00001922 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
1923 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1924 Select(N.getOperand(0));
1925 Tmp1 = SelectExpr(N.getOperand(1));
1926 } else {
1927 Tmp1 = SelectExpr(N.getOperand(1));
1928 Select(N.getOperand(0));
1929 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001930 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
1931 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
1932 }
Chris Lattner11333092005-01-11 03:11:44 +00001933
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001934 return;
1935 }
1936 case ISD::LOAD:
1937 case ISD::CALL:
1938 case ISD::DYNAMIC_STACKALLOC:
1939 SelectExpr(N);
1940 return;
1941 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001942 // Select the address.
1943 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001944
1945 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1946 Opc = 0;
1947 switch (CN->getValueType(0)) {
1948 default: assert(0 && "Invalid type for operation!");
1949 case MVT::i1:
1950 case MVT::i8: Opc = X86::MOV8mi; break;
1951 case MVT::i16: Opc = X86::MOV16mi; break;
1952 case MVT::i32: Opc = X86::MOV32mi; break;
1953 case MVT::f32:
1954 case MVT::f64: break;
1955 }
1956 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001957 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
1958 Select(N.getOperand(0));
1959 SelectAddress(N.getOperand(2), AM);
1960 } else {
1961 SelectAddress(N.getOperand(2), AM);
1962 Select(N.getOperand(0));
1963 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001964 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
1965 return;
1966 }
1967 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001968 switch (N.getOperand(1).getValueType()) {
1969 default: assert(0 && "Cannot store this type!");
1970 case MVT::i1:
1971 case MVT::i8: Opc = X86::MOV8mr; break;
1972 case MVT::i16: Opc = X86::MOV16mr; break;
1973 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001974 case MVT::f32: Opc = X86::FST32m; break;
1975 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001976 }
Chris Lattner11333092005-01-11 03:11:44 +00001977
1978 std::vector<std::pair<unsigned, unsigned> > RP;
1979 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
1980 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
1981 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
1982 std::sort(RP.begin(), RP.end());
1983
1984 for (unsigned i = 0; i != 3; ++i)
1985 switch (RP[2-i].second) {
1986 default: assert(0 && "Unknown operand number!");
1987 case 0: Select(N.getOperand(0)); break;
1988 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001989 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00001990 }
1991
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001992 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
1993 return;
1994 }
1995 case ISD::ADJCALLSTACKDOWN:
1996 case ISD::ADJCALLSTACKUP:
1997 Select(N.getOperand(0));
1998 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1999
2000 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2001 X86::ADJCALLSTACKUP;
2002 BuildMI(BB, Opc, 1).addImm(Tmp1);
2003 return;
2004 }
2005 assert(0 && "Should not be reached!");
2006}
2007
2008
2009/// createX86PatternInstructionSelector - This pass converts an LLVM function
2010/// into a machine code representation using pattern matching and a machine
2011/// description file.
2012///
2013FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
2014 return new ISel(TM);
2015}