blob: 8073342895a35b64a7cdb1ee56682aa786fa4720 [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();
Chris Lattner795069d2005-01-11 05:57:36 +000052
Chris Lattner795069d2005-01-11 05:57:36 +000053 setOperationUnsupported(ISD::MEMMOVE, MVT::Other);
54
Chris Lattner8acb1ba2005-01-07 07:49:41 +000055 setOperationUnsupported(ISD::MUL, MVT::i8);
56 setOperationUnsupported(ISD::SELECT, MVT::i1);
57 setOperationUnsupported(ISD::SELECT, MVT::i8);
58
59 addLegalFPImmediate(+0.0); // FLD0
60 addLegalFPImmediate(+1.0); // FLD1
61 addLegalFPImmediate(-0.0); // FLD0/FCHS
62 addLegalFPImmediate(-1.0); // FLD1/FCHS
63 }
64
65 /// LowerArguments - This hook must be implemented to indicate how we should
66 /// lower the arguments for the specified function, into the specified DAG.
67 virtual std::vector<SDOperand>
68 LowerArguments(Function &F, SelectionDAG &DAG);
69
70 /// LowerCallTo - This hook lowers an abstract call to a function into an
71 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000072 virtual std::pair<SDOperand, SDOperand>
73 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
74 ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000075
76 virtual std::pair<SDOperand, SDOperand>
77 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
78
79 virtual std::pair<SDOperand,SDOperand>
80 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
81 const Type *ArgTy, SelectionDAG &DAG);
82
83 virtual std::pair<SDOperand, SDOperand>
84 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
85 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000086 };
87}
88
89
90std::vector<SDOperand>
91X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
92 std::vector<SDOperand> ArgValues;
93
94 // Add DAG nodes to load the arguments... On entry to a function on the X86,
95 // the stack frame looks like this:
96 //
97 // [ESP] -- return address
98 // [ESP + 4] -- first argument (leftmost lexically)
99 // [ESP + 8] -- second argument, if first argument is four bytes in size
100 // ...
101 //
102 MachineFunction &MF = DAG.getMachineFunction();
103 MachineFrameInfo *MFI = MF.getFrameInfo();
104
105 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
106 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I) {
107 MVT::ValueType ObjectVT = getValueType(I->getType());
108 unsigned ArgIncrement = 4;
109 unsigned ObjSize;
110 switch (ObjectVT) {
111 default: assert(0 && "Unhandled argument type!");
112 case MVT::i1:
113 case MVT::i8: ObjSize = 1; break;
114 case MVT::i16: ObjSize = 2; break;
115 case MVT::i32: ObjSize = 4; break;
116 case MVT::i64: ObjSize = ArgIncrement = 8; break;
117 case MVT::f32: ObjSize = 4; break;
118 case MVT::f64: ObjSize = ArgIncrement = 8; break;
119 }
120 // Create the frame index object for this incoming parameter...
121 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
122
123 // Create the SelectionDAG nodes corresponding to a load from this parameter
124 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
125
126 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
127 // dead loads.
128 SDOperand ArgValue;
129 if (!I->use_empty())
130 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
131 else {
132 if (MVT::isInteger(ObjectVT))
133 ArgValue = DAG.getConstant(0, ObjectVT);
134 else
135 ArgValue = DAG.getConstantFP(0, ObjectVT);
136 }
137 ArgValues.push_back(ArgValue);
138
139 ArgOffset += ArgIncrement; // Move on to the next argument...
140 }
141
142 // If the function takes variable number of arguments, make a frame index for
143 // the start of the first vararg value... for expansion of llvm.va_start.
144 if (F.isVarArg())
145 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000146 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000147 return ArgValues;
148}
149
Chris Lattner5188ad72005-01-08 19:28:19 +0000150std::pair<SDOperand, SDOperand>
151X86TargetLowering::LowerCallTo(SDOperand Chain,
152 const Type *RetTy, SDOperand Callee,
153 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000154 // Count how many bytes are to be pushed on the stack.
155 unsigned NumBytes = 0;
156
157 if (Args.empty()) {
158 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000159 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
160 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000161 } else {
162 for (unsigned i = 0, e = Args.size(); i != e; ++i)
163 switch (getValueType(Args[i].second)) {
164 default: assert(0 && "Unknown value type!");
165 case MVT::i1:
166 case MVT::i8:
167 case MVT::i16:
168 case MVT::i32:
169 case MVT::f32:
170 NumBytes += 4;
171 break;
172 case MVT::i64:
173 case MVT::f64:
174 NumBytes += 8;
175 break;
176 }
177
Chris Lattner5188ad72005-01-08 19:28:19 +0000178 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
179 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000180
181 // Arguments go on the stack in reverse order, as specified by the ABI.
182 unsigned ArgOffset = 0;
183 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32);
184 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
185 unsigned ArgReg;
186 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
187 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
188
189 switch (getValueType(Args[i].second)) {
190 default: assert(0 && "Unexpected ValueType for argument!");
191 case MVT::i1:
192 case MVT::i8:
193 case MVT::i16:
194 // Promote the integer to 32 bits. If the input type is signed use a
195 // sign extend, otherwise use a zero extend.
196 if (Args[i].second->isSigned())
197 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
198 else
199 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
200
201 // FALL THROUGH
202 case MVT::i32:
203 case MVT::f32:
204 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000205 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
206 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000207 ArgOffset += 4;
208 break;
209 case MVT::i64:
210 case MVT::f64:
211 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000212 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
213 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000214 ArgOffset += 8;
215 break;
216 }
217 }
218 }
219
220 std::vector<MVT::ValueType> RetVals;
221 MVT::ValueType RetTyVT = getValueType(RetTy);
222 if (RetTyVT != MVT::isVoid)
223 RetVals.push_back(RetTyVT);
224 RetVals.push_back(MVT::Other);
225
Chris Lattner5188ad72005-01-08 19:28:19 +0000226 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000227 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000228 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
229 DAG.getConstant(NumBytes, getPointerTy()));
230 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000231}
232
Chris Lattner14824582005-01-09 00:01:27 +0000233std::pair<SDOperand, SDOperand>
234X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
235 // vastart just returns the address of the VarArgsFrameIndex slot.
236 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
237}
238
239std::pair<SDOperand,SDOperand> X86TargetLowering::
240LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
241 const Type *ArgTy, SelectionDAG &DAG) {
242 MVT::ValueType ArgVT = getValueType(ArgTy);
243 SDOperand Result;
244 if (!isVANext) {
245 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
246 } else {
247 unsigned Amt;
248 if (ArgVT == MVT::i32)
249 Amt = 4;
250 else {
251 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
252 "Other types should have been promoted for varargs!");
253 Amt = 8;
254 }
255 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
256 DAG.getConstant(Amt, VAList.getValueType()));
257 }
258 return std::make_pair(Result, Chain);
259}
260
261
262std::pair<SDOperand, SDOperand> X86TargetLowering::
263LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
264 SelectionDAG &DAG) {
265 SDOperand Result;
266 if (Depth) // Depths > 0 not supported yet!
267 Result = DAG.getConstant(0, getPointerTy());
268 else {
269 if (ReturnAddrIndex == 0) {
270 // Set up a frame object for the return address.
271 MachineFunction &MF = DAG.getMachineFunction();
272 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
273 }
274
275 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
276
277 if (!isFrameAddress)
278 // Just load the return address
279 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI);
280 else
281 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
282 DAG.getConstant(4, MVT::i32));
283 }
284 return std::make_pair(Result, Chain);
285}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000286
287
288
289
290
291namespace {
292 Statistic<>
293 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
294
295 //===--------------------------------------------------------------------===//
296 /// ISel - X86 specific code to select X86 machine instructions for
297 /// SelectionDAG operations.
298 ///
299 class ISel : public SelectionDAGISel {
300 /// ContainsFPCode - Every instruction we select that uses or defines a FP
301 /// register should set this to true.
302 bool ContainsFPCode;
303
304 /// X86Lowering - This object fully describes how to lower LLVM code to an
305 /// X86-specific SelectionDAG.
306 X86TargetLowering X86Lowering;
307
Chris Lattner11333092005-01-11 03:11:44 +0000308 /// RegPressureMap - This keeps an approximate count of the number of
309 /// registers required to evaluate each node in the graph.
310 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000311
312 /// ExprMap - As shared expressions are codegen'd, we keep track of which
313 /// vreg the value is produced in, so we only emit one copy of each compiled
314 /// tree.
315 std::map<SDOperand, unsigned> ExprMap;
316 std::set<SDOperand> LoweredTokens;
317
318 public:
319 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
320 }
321
Chris Lattner11333092005-01-11 03:11:44 +0000322 unsigned getRegPressure(SDOperand O) {
323 return RegPressureMap[O.Val];
324 }
325 unsigned ComputeRegPressure(SDOperand O);
326
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000327 /// InstructionSelectBasicBlock - This callback is invoked by
328 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
329 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
330 // While we're doing this, keep track of whether we see any FP code for
331 // FP_REG_KILL insertion.
332 ContainsFPCode = false;
333
Chris Lattner11333092005-01-11 03:11:44 +0000334 // Compute the RegPressureMap, which is an approximation for the number of
335 // registers required to compute each node.
336 ComputeRegPressure(DAG.getRoot());
337
338 //DAG.viewGraph();
339
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000340 // Codegen the basic block.
341 Select(DAG.getRoot());
342
343 // Insert FP_REG_KILL instructions into basic blocks that need them. This
344 // only occurs due to the floating point stackifier not being aggressive
345 // enough to handle arbitrary global stackification.
346 //
347 // Currently we insert an FP_REG_KILL instruction into each block that
348 // uses or defines a floating point virtual register.
349 //
350 // When the global register allocators (like linear scan) finally update
351 // live variable analysis, we can keep floating point values in registers
352 // across basic blocks. This will be a huge win, but we are waiting on
353 // the global allocators before we can do this.
354 //
355 if (ContainsFPCode && BB->succ_size()) {
356 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
357 ++NumFPKill;
358 }
359
360 // Clear state used for selection.
361 ExprMap.clear();
362 LoweredTokens.clear();
Chris Lattner11333092005-01-11 03:11:44 +0000363 RegPressureMap.clear();
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000364 }
365
366 void EmitCMP(SDOperand LHS, SDOperand RHS);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000367 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000368 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
369 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000370 unsigned SelectExpr(SDOperand N);
371 bool SelectAddress(SDOperand N, X86AddressMode &AM);
372 void Select(SDOperand N);
373 };
374}
375
Chris Lattner11333092005-01-11 03:11:44 +0000376// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
377// for the number of registers required to compute each node. This is basically
378// computing a generalized form of the Sethi-Ullman number for each node.
379unsigned ISel::ComputeRegPressure(SDOperand O) {
380 SDNode *N = O.Val;
381 unsigned &Result = RegPressureMap[N];
382 if (Result) return Result;
383
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000384 // FIXME: Should operations like CALL (which clobber lots o regs) have a
385 // higher fixed cost??
386
Chris Lattner11333092005-01-11 03:11:44 +0000387 if (N->getNumOperands() == 0)
388 return Result = 1;
389
390 unsigned MaxRegUse = 0;
391 unsigned NumExtraMaxRegUsers = 0;
392 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
393 unsigned Regs = ComputeRegPressure(N->getOperand(i));
394 if (Regs > MaxRegUse) {
395 MaxRegUse = Regs;
396 NumExtraMaxRegUsers = 0;
397 } else if (Regs == MaxRegUse) {
398 ++NumExtraMaxRegUsers;
399 }
400 }
401
402 return Result = MaxRegUse+NumExtraMaxRegUsers;
403}
404
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000405/// SelectAddress - Add the specified node to the specified addressing mode,
406/// returning true if it cannot be done.
407bool ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
408 switch (N.getOpcode()) {
409 default: break;
410 case ISD::FrameIndex:
411 if (AM.BaseType == X86AddressMode::RegBase && AM.Base.Reg == 0) {
412 AM.BaseType = X86AddressMode::FrameIndexBase;
413 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
414 return false;
415 }
416 break;
417 case ISD::GlobalAddress:
418 if (AM.GV == 0) {
419 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
420 return false;
421 }
422 break;
423 case ISD::Constant:
424 AM.Disp += cast<ConstantSDNode>(N)->getValue();
425 return false;
426 case ISD::SHL:
427 if (AM.IndexReg == 0 || AM.Scale == 1)
428 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
429 unsigned Val = CN->getValue();
430 if (Val == 1 || Val == 2 || Val == 3) {
431 AM.Scale = 1 << Val;
432 AM.IndexReg = SelectExpr(N.Val->getOperand(0));
433 return false;
434 }
435 }
436 break;
437
438 case ISD::ADD: {
439 X86AddressMode Backup = AM;
440 if (!SelectAddress(N.Val->getOperand(0), AM) &&
441 !SelectAddress(N.Val->getOperand(1), AM))
442 return false;
443 AM = Backup;
444 break;
445 }
446 }
447
Chris Lattnera95589b2005-01-11 04:40:19 +0000448 // Is the base register already occupied?
449 if (AM.BaseType != X86AddressMode::RegBase || AM.Base.Reg) {
450 // If so, check to see if the scale index register is set.
451 if (AM.IndexReg == 0) {
452 AM.IndexReg = SelectExpr(N);
453 AM.Scale = 1;
454 return false;
455 }
456
457 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000458 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000459 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000460
461 // Default, generate it as a register.
462 AM.BaseType = X86AddressMode::RegBase;
463 AM.Base.Reg = SelectExpr(N);
464 return false;
465}
466
467/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
468/// assuming that the temporary registers are in the 8-bit register class.
469///
470/// Tmp1 = setcc1
471/// Tmp2 = setcc2
472/// DestReg = logicalop Tmp1, Tmp2
473///
474static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
475 unsigned SetCC2, unsigned LogicalOp,
476 unsigned DestReg) {
477 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
478 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
479 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
480 BuildMI(BB, SetCC1, 0, Tmp1);
481 BuildMI(BB, SetCC2, 0, Tmp2);
482 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
483}
484
485/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
486/// condition codes match the specified SetCCOpcode. Note that some conditions
487/// require multiple instructions to generate the correct value.
488static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
489 ISD::CondCode SetCCOpcode, bool isFP) {
490 unsigned Opc;
491 if (!isFP) {
492 switch (SetCCOpcode) {
493 default: assert(0 && "Illegal integer SetCC!");
494 case ISD::SETEQ: Opc = X86::SETEr; break;
495 case ISD::SETGT: Opc = X86::SETGr; break;
496 case ISD::SETGE: Opc = X86::SETGEr; break;
497 case ISD::SETLT: Opc = X86::SETLr; break;
498 case ISD::SETLE: Opc = X86::SETLEr; break;
499 case ISD::SETNE: Opc = X86::SETNEr; break;
500 case ISD::SETULT: Opc = X86::SETBr; break;
501 case ISD::SETUGT: Opc = X86::SETAr; break;
502 case ISD::SETULE: Opc = X86::SETBEr; break;
503 case ISD::SETUGE: Opc = X86::SETAEr; break;
504 }
505 } else {
506 // On a floating point condition, the flags are set as follows:
507 // ZF PF CF op
508 // 0 | 0 | 0 | X > Y
509 // 0 | 0 | 1 | X < Y
510 // 1 | 0 | 0 | X == Y
511 // 1 | 1 | 1 | unordered
512 //
513 switch (SetCCOpcode) {
514 default: assert(0 && "Invalid FP setcc!");
515 case ISD::SETUEQ:
516 case ISD::SETEQ:
517 Opc = X86::SETEr; // True if ZF = 1
518 break;
519 case ISD::SETOGT:
520 case ISD::SETGT:
521 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
522 break;
523 case ISD::SETOGE:
524 case ISD::SETGE:
525 Opc = X86::SETAEr; // True if CF = 0
526 break;
527 case ISD::SETULT:
528 case ISD::SETLT:
529 Opc = X86::SETBr; // True if CF = 1
530 break;
531 case ISD::SETULE:
532 case ISD::SETLE:
533 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
534 break;
535 case ISD::SETONE:
536 case ISD::SETNE:
537 Opc = X86::SETNEr; // True if ZF = 0
538 break;
539 case ISD::SETUO:
540 Opc = X86::SETPr; // True if PF = 1
541 break;
542 case ISD::SETO:
543 Opc = X86::SETNPr; // True if PF = 0
544 break;
545 case ISD::SETOEQ: // !PF & ZF
546 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
547 return;
548 case ISD::SETOLT: // !PF & CF
549 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
550 return;
551 case ISD::SETOLE: // !PF & (CF || ZF)
552 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
553 return;
554 case ISD::SETUGT: // PF | (!ZF & !CF)
555 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
556 return;
557 case ISD::SETUGE: // PF | !CF
558 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
559 return;
560 case ISD::SETUNE: // PF | !ZF
561 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
562 return;
563 }
564 }
565 BuildMI(BB, Opc, 0, DestReg);
566}
567
568
569/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
570/// the Dest block if the Cond condition is true. If we cannot fold this
571/// condition into the branch, return true.
572///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000573bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
574 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000575 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
576 // B) using two conditional branches instead of one condbr, two setcc's, and
577 // an or.
578 if ((Cond.getOpcode() == ISD::OR ||
579 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
580 // And and or set the flags for us, so there is no need to emit a TST of the
581 // result. It is only safe to do this if there is only a single use of the
582 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000583 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000584 SelectExpr(Cond);
585 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
586 return false;
587 }
588
589 // Codegen br not C -> JE.
590 if (Cond.getOpcode() == ISD::XOR)
591 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
592 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000593 unsigned CondR;
594 if (getRegPressure(Chain) > getRegPressure(Cond)) {
595 Select(Chain);
596 CondR = SelectExpr(Cond.Val->getOperand(0));
597 } else {
598 CondR = SelectExpr(Cond.Val->getOperand(0));
599 Select(Chain);
600 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000601 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
602 BuildMI(BB, X86::JE, 1).addMBB(Dest);
603 return false;
604 }
605
606 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
607 if (SetCC == 0)
608 return true; // Can only handle simple setcc's so far.
609
610 unsigned Opc;
611
612 // Handle integer conditions first.
613 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
614 switch (SetCC->getCondition()) {
615 default: assert(0 && "Illegal integer SetCC!");
616 case ISD::SETEQ: Opc = X86::JE; break;
617 case ISD::SETGT: Opc = X86::JG; break;
618 case ISD::SETGE: Opc = X86::JGE; break;
619 case ISD::SETLT: Opc = X86::JL; break;
620 case ISD::SETLE: Opc = X86::JLE; break;
621 case ISD::SETNE: Opc = X86::JNE; break;
622 case ISD::SETULT: Opc = X86::JB; break;
623 case ISD::SETUGT: Opc = X86::JA; break;
624 case ISD::SETULE: Opc = X86::JBE; break;
625 case ISD::SETUGE: Opc = X86::JAE; break;
626 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000627 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000628 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
629 BuildMI(BB, Opc, 1).addMBB(Dest);
630 return false;
631 }
632
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000633 unsigned Opc2 = 0; // Second branch if needed.
634
635 // On a floating point condition, the flags are set as follows:
636 // ZF PF CF op
637 // 0 | 0 | 0 | X > Y
638 // 0 | 0 | 1 | X < Y
639 // 1 | 0 | 0 | X == Y
640 // 1 | 1 | 1 | unordered
641 //
642 switch (SetCC->getCondition()) {
643 default: assert(0 && "Invalid FP setcc!");
644 case ISD::SETUEQ:
645 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
646 case ISD::SETOGT:
647 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
648 case ISD::SETOGE:
649 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
650 case ISD::SETULT:
651 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
652 case ISD::SETULE:
653 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
654 case ISD::SETONE:
655 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
656 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
657 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
658 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
659 Opc = X86::JA; // ZF = 0 & CF = 0
660 Opc2 = X86::JP; // PF = 1
661 break;
662 case ISD::SETUGE: // PF = 1 | CF = 0
663 Opc = X86::JAE; // CF = 0
664 Opc2 = X86::JP; // PF = 1
665 break;
666 case ISD::SETUNE: // PF = 1 | ZF = 0
667 Opc = X86::JNE; // ZF = 0
668 Opc2 = X86::JP; // PF = 1
669 break;
670 case ISD::SETOEQ: // PF = 0 & ZF = 1
671 //X86::JNP, X86::JE
672 //X86::AND8rr
673 return true; // FIXME: Emit more efficient code for this branch.
674 case ISD::SETOLT: // PF = 0 & CF = 1
675 //X86::JNP, X86::JB
676 //X86::AND8rr
677 return true; // FIXME: Emit more efficient code for this branch.
678 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
679 //X86::JNP, X86::JBE
680 //X86::AND8rr
681 return true; // FIXME: Emit more efficient code for this branch.
682 }
683
Chris Lattner6c07aee2005-01-11 04:06:27 +0000684 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000685 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
686 BuildMI(BB, Opc, 1).addMBB(Dest);
687 if (Opc2)
688 BuildMI(BB, Opc2, 1).addMBB(Dest);
689 return false;
690}
691
Chris Lattner24aad1b2005-01-10 22:10:13 +0000692/// EmitSelectCC - Emit code into BB that performs a select operation between
693/// the two registers RTrue and RFalse, generating a result into RDest. Return
694/// true if the fold cannot be performed.
695///
696void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
697 unsigned RTrue, unsigned RFalse, unsigned RDest) {
698 enum Condition {
699 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
700 NOT_SET
701 } CondCode = NOT_SET;
702
703 static const unsigned CMOVTAB16[] = {
704 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
705 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
706 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
707 };
708 static const unsigned CMOVTAB32[] = {
709 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
710 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
711 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
712 };
713 static const unsigned CMOVTABFP[] = {
714 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
715 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
716 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
717 };
718
719 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
720 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
721 switch (SetCC->getCondition()) {
722 default: assert(0 && "Unknown integer comparison!");
723 case ISD::SETEQ: CondCode = EQ; break;
724 case ISD::SETGT: CondCode = GT; break;
725 case ISD::SETGE: CondCode = GE; break;
726 case ISD::SETLT: CondCode = LT; break;
727 case ISD::SETLE: CondCode = LE; break;
728 case ISD::SETNE: CondCode = NE; break;
729 case ISD::SETULT: CondCode = B; break;
730 case ISD::SETUGT: CondCode = A; break;
731 case ISD::SETULE: CondCode = BE; break;
732 case ISD::SETUGE: CondCode = AE; break;
733 }
734 } else {
735 // On a floating point condition, the flags are set as follows:
736 // ZF PF CF op
737 // 0 | 0 | 0 | X > Y
738 // 0 | 0 | 1 | X < Y
739 // 1 | 0 | 0 | X == Y
740 // 1 | 1 | 1 | unordered
741 //
742 switch (SetCC->getCondition()) {
743 default: assert(0 && "Unknown FP comparison!");
744 case ISD::SETUEQ:
745 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
746 case ISD::SETOGT:
747 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
748 case ISD::SETOGE:
749 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
750 case ISD::SETULT:
751 case ISD::SETLT: CondCode = B; break; // True if CF = 1
752 case ISD::SETULE:
753 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
754 case ISD::SETONE:
755 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
756 case ISD::SETUO: CondCode = P; break; // True if PF = 1
757 case ISD::SETO: CondCode = NP; break; // True if PF = 0
758 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
759 case ISD::SETUGE: // PF = 1 | CF = 0
760 case ISD::SETUNE: // PF = 1 | ZF = 0
761 case ISD::SETOEQ: // PF = 0 & ZF = 1
762 case ISD::SETOLT: // PF = 0 & CF = 1
763 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
764 // We cannot emit this comparison as a single cmov.
765 break;
766 }
767 }
768 }
769
770 unsigned Opc = 0;
771 if (CondCode != NOT_SET) {
772 switch (SVT) {
773 default: assert(0 && "Cannot select this type!");
774 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
775 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
776 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000777 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000778 }
779 }
780
781 // Finally, if we weren't able to fold this, just emit the condition and test
782 // it.
783 if (CondCode == NOT_SET || Opc == 0) {
784 // Get the condition into the zero flag.
785 unsigned CondReg = SelectExpr(Cond);
786 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
787
788 switch (SVT) {
789 default: assert(0 && "Cannot select this type!");
790 case MVT::i16: Opc = X86::CMOVE16rr; break;
791 case MVT::i32: Opc = X86::CMOVE32rr; break;
792 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000793 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000794 }
795 } else {
796 // FIXME: CMP R, 0 -> TEST R, R
797 EmitCMP(Cond.getOperand(0), Cond.getOperand(1));
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000798 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000799 }
800 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
801}
802
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000803void ISel::EmitCMP(SDOperand LHS, SDOperand RHS) {
Chris Lattner11333092005-01-11 03:11:44 +0000804 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000805 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
806 Opc = 0;
807 switch (RHS.getValueType()) {
808 default: break;
809 case MVT::i1:
810 case MVT::i8: Opc = X86::CMP8ri; break;
811 case MVT::i16: Opc = X86::CMP16ri; break;
812 case MVT::i32: Opc = X86::CMP32ri; break;
813 }
814 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +0000815 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000816 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
817 return;
818 }
819 }
820
821 switch (LHS.getValueType()) {
822 default: assert(0 && "Cannot compare this value!");
823 case MVT::i1:
824 case MVT::i8: Opc = X86::CMP8rr; break;
825 case MVT::i16: Opc = X86::CMP16rr; break;
826 case MVT::i32: Opc = X86::CMP32rr; break;
827 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000828 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000829 }
Chris Lattner11333092005-01-11 03:11:44 +0000830 unsigned Tmp1, Tmp2;
831 if (getRegPressure(LHS) > getRegPressure(RHS)) {
832 Tmp1 = SelectExpr(LHS);
833 Tmp2 = SelectExpr(RHS);
834 } else {
835 Tmp2 = SelectExpr(RHS);
836 Tmp1 = SelectExpr(LHS);
837 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000838 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
839}
840
841unsigned ISel::SelectExpr(SDOperand N) {
842 unsigned Result;
843 unsigned Tmp1, Tmp2, Tmp3;
844 unsigned Opc = 0;
845
Chris Lattner5188ad72005-01-08 19:28:19 +0000846 SDNode *Node = N.Val;
847
Chris Lattner590d8002005-01-09 18:52:44 +0000848 if (Node->getOpcode() == ISD::CopyFromReg)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000849 // Just use the specified register as our input.
Chris Lattner590d8002005-01-09 18:52:44 +0000850 return dyn_cast<CopyRegSDNode>(Node)->getReg();
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000851
852 // If there are multiple uses of this expression, memorize the
853 // register it is code generated in, instead of emitting it multiple
854 // times.
855 // FIXME: Disabled for our current selection model.
Chris Lattner5188ad72005-01-08 19:28:19 +0000856 if (1 || !Node->hasOneUse()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000857 unsigned &Reg = ExprMap[N];
858 if (Reg) return Reg;
859
860 if (N.getOpcode() != ISD::CALL)
861 Reg = Result = (N.getValueType() != MVT::Other) ?
862 MakeReg(N.getValueType()) : 1;
863 else {
864 // If this is a call instruction, make sure to prepare ALL of the result
865 // values as well as the chain.
Chris Lattner5188ad72005-01-08 19:28:19 +0000866 if (Node->getNumValues() == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000867 Reg = Result = 1; // Void call, just a chain.
868 else {
Chris Lattner5188ad72005-01-08 19:28:19 +0000869 Result = MakeReg(Node->getValueType(0));
870 ExprMap[N.getValue(0)] = Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000871 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Chris Lattner5188ad72005-01-08 19:28:19 +0000872 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
873 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000874 }
875 }
876 } else {
877 Result = MakeReg(N.getValueType());
878 }
879
880 switch (N.getOpcode()) {
881 default:
Chris Lattner5188ad72005-01-08 19:28:19 +0000882 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000883 assert(0 && "Node not handled!\n");
884 case ISD::FrameIndex:
885 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
886 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
887 return Result;
888 case ISD::ConstantPool:
889 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
890 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
891 return Result;
892 case ISD::ConstantFP:
893 ContainsFPCode = true;
894 Tmp1 = Result; // Intermediate Register
895 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
896 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
897 Tmp1 = MakeReg(MVT::f64);
898
899 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
900 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
901 BuildMI(BB, X86::FLD0, 0, Tmp1);
902 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
903 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
904 BuildMI(BB, X86::FLD1, 0, Tmp1);
905 else
906 assert(0 && "Unexpected constant!");
907 if (Tmp1 != Result)
908 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
909 return Result;
910 case ISD::Constant:
911 switch (N.getValueType()) {
912 default: assert(0 && "Cannot use constants of this type!");
913 case MVT::i1:
914 case MVT::i8: Opc = X86::MOV8ri; break;
915 case MVT::i16: Opc = X86::MOV16ri; break;
916 case MVT::i32: Opc = X86::MOV32ri; break;
917 }
918 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
919 return Result;
920 case ISD::GlobalAddress: {
921 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
922 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
923 return Result;
924 }
925 case ISD::ExternalSymbol: {
926 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
927 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
928 return Result;
929 }
930 case ISD::FP_EXTEND:
931 Tmp1 = SelectExpr(N.getOperand(0));
932 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000933 return Result;
934 case ISD::ZERO_EXTEND: {
935 int DestIs16 = N.getValueType() == MVT::i16;
936 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +0000937 Tmp1 = SelectExpr(N.getOperand(0));
938
939 // FIXME: This hack is here for zero extension casts from bool to i8. This
940 // would not be needed if bools were promoted by Legalize.
941 if (N.getValueType() == MVT::i8) {
942 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
943 return Result;
944 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000945
946 static const unsigned Opc[3] = {
947 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
948 };
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000949 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
950 return Result;
951 }
952 case ISD::SIGN_EXTEND: {
953 int DestIs16 = N.getValueType() == MVT::i16;
954 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
955
Chris Lattner590d8002005-01-09 18:52:44 +0000956 // FIXME: Legalize should promote bools to i8!
957 assert(N.getOperand(0).getValueType() != MVT::i1 &&
958 "Sign extend from bool not implemented!");
959
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000960 static const unsigned Opc[3] = {
961 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
962 };
963 Tmp1 = SelectExpr(N.getOperand(0));
964 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
965 return Result;
966 }
967 case ISD::TRUNCATE:
968 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
969 // a move out of AX or AL.
970 switch (N.getOperand(0).getValueType()) {
971 default: assert(0 && "Unknown truncate!");
972 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
973 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
974 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
975 }
976 Tmp1 = SelectExpr(N.getOperand(0));
977 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
978
979 switch (N.getValueType()) {
980 default: assert(0 && "Unknown truncate!");
981 case MVT::i1:
982 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
983 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
984 }
985 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
986 return Result;
987
988 case ISD::FP_ROUND:
989 // Truncate from double to float by storing to memory as float,
990 // then reading it back into a register.
991
992 // Create as stack slot to use.
Chris Lattner590d8002005-01-09 18:52:44 +0000993 // FIXME: This should automatically be made by the Legalizer!
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000994 Tmp1 = TLI.getTargetData().getFloatAlignment();
995 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
996
997 // Codegen the input.
998 Tmp1 = SelectExpr(N.getOperand(0));
999
1000 // Emit the store, then the reload.
1001 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
1002 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001003 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00001004
1005 case ISD::SINT_TO_FP:
1006 case ISD::UINT_TO_FP: {
1007 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001008 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001009
1010 // Promote the integer to a type supported by FLD. We do this because there
1011 // are no unsigned FLD instructions, so we must promote an unsigned value to
1012 // a larger signed value, then use FLD on the larger value.
1013 //
1014 MVT::ValueType PromoteType = MVT::Other;
1015 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1016 unsigned PromoteOpcode = 0;
1017 unsigned RealDestReg = Result;
1018 switch (SrcTy) {
1019 case MVT::i1:
1020 case MVT::i8:
1021 // We don't have the facilities for directly loading byte sized data from
1022 // memory (even signed). Promote it to 16 bits.
1023 PromoteType = MVT::i16;
1024 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1025 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1026 break;
1027 case MVT::i16:
1028 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1029 PromoteType = MVT::i32;
1030 PromoteOpcode = X86::MOVZX32rr16;
1031 }
1032 break;
1033 default:
1034 // Don't fild into the real destination.
1035 if (Node->getOpcode() == ISD::UINT_TO_FP)
1036 Result = MakeReg(Node->getValueType(0));
1037 break;
1038 }
1039
1040 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1041
1042 if (PromoteType != MVT::Other) {
1043 Tmp2 = MakeReg(PromoteType);
1044 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1045 SrcTy = PromoteType;
1046 Tmp1 = Tmp2;
1047 }
1048
1049 // Spill the integer to memory and reload it from there.
1050 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1051 MachineFunction *F = BB->getParent();
1052 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1053
1054 switch (SrcTy) {
1055 case MVT::i64:
1056 // FIXME: this won't work for cast [u]long to FP
1057 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1058 FrameIdx).addReg(Tmp1);
1059 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1060 FrameIdx, 4).addReg(Tmp1+1);
1061 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1062 break;
1063 case MVT::i32:
1064 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1065 FrameIdx).addReg(Tmp1);
1066 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1067 break;
1068 case MVT::i16:
1069 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1070 FrameIdx).addReg(Tmp1);
1071 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1072 break;
1073 default: break; // No promotion required.
1074 }
1075
1076 if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i32) {
1077 // If this is a cast from uint -> double, we need to be careful when if
1078 // the "sign" bit is set. If so, we don't want to make a negative number,
1079 // we want to make a positive number. Emit code to add an offset if the
1080 // sign bit is set.
1081
1082 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1083 unsigned IsNeg = MakeReg(MVT::i32);
1084 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1085
1086 // Create a CP value that has the offset in one word and 0 in the other.
1087 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1088 0x4f80000000000000ULL);
1089 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1090 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1091 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1092
1093 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1094 // We need special handling for unsigned 64-bit integer sources. If the
1095 // input number has the "sign bit" set, then we loaded it incorrectly as a
1096 // negative 64-bit number. In this case, add an offset value.
1097
1098 // Emit a test instruction to see if the dynamic input value was signed.
1099 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1100
1101 // If the sign bit is set, get a pointer to an offset, otherwise get a
1102 // pointer to a zero.
1103 MachineConstantPool *CP = F->getConstantPool();
1104 unsigned Zero = MakeReg(MVT::i32);
1105 Constant *Null = Constant::getNullValue(Type::UIntTy);
1106 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1107 CP->getConstantPoolIndex(Null));
1108 unsigned Offset = MakeReg(MVT::i32);
1109 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1110
1111 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1112 CP->getConstantPoolIndex(OffsetCst));
1113 unsigned Addr = MakeReg(MVT::i32);
1114 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1115
1116 // Load the constant for an add. FIXME: this could make an 'fadd' that
1117 // reads directly from memory, but we don't support these yet.
1118 unsigned ConstReg = MakeReg(MVT::f64);
1119 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1120
1121 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1122 }
1123 return RealDestReg;
1124 }
1125 case ISD::FP_TO_SINT:
1126 case ISD::FP_TO_UINT: {
1127 // FIXME: Most of this grunt work should be done by legalize!
1128 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1129
1130 // Change the floating point control register to use "round towards zero"
1131 // mode when truncating to an integer value.
1132 //
1133 MachineFunction *F = BB->getParent();
1134 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1135 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1136
1137 // Load the old value of the high byte of the control word...
1138 unsigned HighPartOfCW = MakeReg(MVT::i8);
1139 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1140 CWFrameIdx, 1);
1141
1142 // Set the high part to be round to zero...
1143 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1144 CWFrameIdx, 1).addImm(12);
1145
1146 // Reload the modified control word now...
1147 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1148
1149 // Restore the memory image of control word to original value
1150 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1151 CWFrameIdx, 1).addReg(HighPartOfCW);
1152
1153 // We don't have the facilities for directly storing byte sized data to
1154 // memory. Promote it to 16 bits. We also must promote unsigned values to
1155 // larger classes because we only have signed FP stores.
1156 MVT::ValueType StoreClass = Node->getValueType(0);
1157 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1158 switch (StoreClass) {
1159 case MVT::i8: StoreClass = MVT::i16; break;
1160 case MVT::i16: StoreClass = MVT::i32; break;
1161 case MVT::i32: StoreClass = MVT::i64; break;
1162 // The following treatment of cLong may not be perfectly right,
1163 // but it survives chains of casts of the form
1164 // double->ulong->double.
1165 case MVT::i64: StoreClass = MVT::i64; break;
1166 default: assert(0 && "Unknown store class!");
1167 }
1168
1169 // Spill the integer to memory and reload it from there.
1170 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1171 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1172
1173 switch (StoreClass) {
1174 default: assert(0 && "Unknown store class!");
1175 case MVT::i16:
1176 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1177 break;
1178 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001179 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001180 break;
1181 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001182 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001183 break;
1184 }
1185
1186 switch (Node->getValueType(0)) {
1187 default:
1188 assert(0 && "Unknown integer type!");
1189 case MVT::i64:
1190 // FIXME: this isn't gunna work.
1191 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1192 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1193 case MVT::i32:
1194 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1195 break;
1196 case MVT::i16:
1197 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1198 break;
1199 case MVT::i8:
1200 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1201 break;
1202 }
1203
1204 // Reload the original control word now.
1205 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1206 return Result;
1207 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001208 case ISD::ADD:
1209 // See if we can codegen this as an LEA to fold operations together.
1210 if (N.getValueType() == MVT::i32) {
1211 X86AddressMode AM;
1212 if (!SelectAddress(N.getOperand(0), AM) &&
1213 !SelectAddress(N.getOperand(1), AM)) {
1214 // If this is not just an add, emit the LEA. For a simple add (like
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001215 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001216 // leave this as LEA, then peephole it to 'ADD' after two address elim
1217 // happens.
1218 if (AM.Scale != 1 || AM.BaseType == X86AddressMode::FrameIndexBase ||
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001219 AM.GV || (AM.Base.Reg && AM.IndexReg && AM.Disp)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001220 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1221 return Result;
1222 }
1223 }
1224 }
Chris Lattner11333092005-01-11 03:11:44 +00001225
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001226 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1227 Opc = 0;
1228 if (CN->getValue() == 1) { // add X, 1 -> inc X
1229 switch (N.getValueType()) {
1230 default: assert(0 && "Cannot integer add this type!");
1231 case MVT::i8: Opc = X86::INC8r; break;
1232 case MVT::i16: Opc = X86::INC16r; break;
1233 case MVT::i32: Opc = X86::INC32r; break;
1234 }
1235 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1236 switch (N.getValueType()) {
1237 default: assert(0 && "Cannot integer add this type!");
1238 case MVT::i8: Opc = X86::DEC8r; break;
1239 case MVT::i16: Opc = X86::DEC16r; break;
1240 case MVT::i32: Opc = X86::DEC32r; break;
1241 }
1242 }
1243
1244 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001245 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001246 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1247 return Result;
1248 }
1249
1250 switch (N.getValueType()) {
1251 default: assert(0 && "Cannot add this type!");
1252 case MVT::i8: Opc = X86::ADD8ri; break;
1253 case MVT::i16: Opc = X86::ADD16ri; break;
1254 case MVT::i32: Opc = X86::ADD32ri; break;
1255 }
1256 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001257 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001258 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1259 return Result;
1260 }
1261 }
1262
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001263 switch (N.getValueType()) {
1264 default: assert(0 && "Cannot add this type!");
1265 case MVT::i8: Opc = X86::ADD8rr; break;
1266 case MVT::i16: Opc = X86::ADD16rr; break;
1267 case MVT::i32: Opc = X86::ADD32rr; break;
1268 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001269 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001270 }
Chris Lattner11333092005-01-11 03:11:44 +00001271
1272 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1273 Tmp1 = SelectExpr(N.getOperand(0));
1274 Tmp2 = SelectExpr(N.getOperand(1));
1275 } else {
1276 Tmp2 = SelectExpr(N.getOperand(1));
1277 Tmp1 = SelectExpr(N.getOperand(0));
1278 }
1279
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001280 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1281 return Result;
1282 case ISD::SUB:
1283 if (MVT::isInteger(N.getValueType()))
1284 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1285 if (CN->isNullValue()) { // 0 - N -> neg N
1286 switch (N.getValueType()) {
1287 default: assert(0 && "Cannot sub this type!");
1288 case MVT::i1:
1289 case MVT::i8: Opc = X86::NEG8r; break;
1290 case MVT::i16: Opc = X86::NEG16r; break;
1291 case MVT::i32: Opc = X86::NEG32r; break;
1292 }
1293 Tmp1 = SelectExpr(N.getOperand(1));
1294 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1295 return Result;
1296 }
1297
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001298 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1299 switch (N.getValueType()) {
1300 default: assert(0 && "Cannot sub this type!");
1301 case MVT::i1:
1302 case MVT::i8: Opc = X86::SUB8ri; break;
1303 case MVT::i16: Opc = X86::SUB16ri; break;
1304 case MVT::i32: Opc = X86::SUB32ri; break;
1305 }
Chris Lattner11333092005-01-11 03:11:44 +00001306 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001307 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1308 return Result;
1309 }
Chris Lattner11333092005-01-11 03:11:44 +00001310
1311 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1312 Tmp1 = SelectExpr(N.getOperand(0));
1313 Tmp2 = SelectExpr(N.getOperand(1));
1314 } else {
1315 Tmp2 = SelectExpr(N.getOperand(1));
1316 Tmp1 = SelectExpr(N.getOperand(0));
1317 }
1318
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001319 switch (N.getValueType()) {
1320 default: assert(0 && "Cannot add this type!");
1321 case MVT::i1:
1322 case MVT::i8: Opc = X86::SUB8rr; break;
1323 case MVT::i16: Opc = X86::SUB16rr; break;
1324 case MVT::i32: Opc = X86::SUB32rr; break;
1325 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001326 case MVT::f64: Opc = X86::FpSUB; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001327 }
1328 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1329 return Result;
1330
1331 case ISD::AND:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001332 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1333 switch (N.getValueType()) {
1334 default: assert(0 && "Cannot add this type!");
1335 case MVT::i1:
1336 case MVT::i8: Opc = X86::AND8ri; break;
1337 case MVT::i16: Opc = X86::AND16ri; break;
1338 case MVT::i32: Opc = X86::AND32ri; break;
1339 }
Chris Lattner11333092005-01-11 03:11:44 +00001340 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001341 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1342 return Result;
1343 }
Chris Lattner11333092005-01-11 03:11:44 +00001344
1345 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1346 Tmp1 = SelectExpr(N.getOperand(0));
1347 Tmp2 = SelectExpr(N.getOperand(1));
1348 } else {
1349 Tmp2 = SelectExpr(N.getOperand(1));
1350 Tmp1 = SelectExpr(N.getOperand(0));
1351 }
1352
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001353 switch (N.getValueType()) {
1354 default: assert(0 && "Cannot add this type!");
1355 case MVT::i1:
1356 case MVT::i8: Opc = X86::AND8rr; break;
1357 case MVT::i16: Opc = X86::AND16rr; break;
1358 case MVT::i32: Opc = X86::AND32rr; break;
1359 }
1360 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1361 return Result;
1362 case ISD::OR:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001363 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001364 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001365 switch (N.getValueType()) {
1366 default: assert(0 && "Cannot add this type!");
1367 case MVT::i1:
1368 case MVT::i8: Opc = X86::OR8ri; break;
1369 case MVT::i16: Opc = X86::OR16ri; break;
1370 case MVT::i32: Opc = X86::OR32ri; break;
1371 }
1372 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1373 return Result;
1374 }
Chris Lattner11333092005-01-11 03:11:44 +00001375
1376 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1377 Tmp1 = SelectExpr(N.getOperand(0));
1378 Tmp2 = SelectExpr(N.getOperand(1));
1379 } else {
1380 Tmp2 = SelectExpr(N.getOperand(1));
1381 Tmp1 = SelectExpr(N.getOperand(0));
1382 }
1383
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001384 switch (N.getValueType()) {
1385 default: assert(0 && "Cannot add this type!");
1386 case MVT::i1:
1387 case MVT::i8: Opc = X86::OR8rr; break;
1388 case MVT::i16: Opc = X86::OR16rr; break;
1389 case MVT::i32: Opc = X86::OR32rr; break;
1390 }
1391 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1392 return Result;
1393 case ISD::XOR:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001394 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001395 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerd4dab922005-01-11 04:31:30 +00001396
1397 if (CN->isAllOnesValue()) {
1398 switch (N.getValueType()) {
1399 default: assert(0 && "Cannot add this type!");
1400 case MVT::i1:
1401 case MVT::i8: Opc = X86::NOT8r; break;
1402 case MVT::i16: Opc = X86::NOT16r; break;
1403 case MVT::i32: Opc = X86::NOT32r; break;
1404 }
1405 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1406 return Result;
1407 }
1408
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001409 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001410 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001411 case MVT::i1:
1412 case MVT::i8: Opc = X86::XOR8ri; break;
1413 case MVT::i16: Opc = X86::XOR16ri; break;
1414 case MVT::i32: Opc = X86::XOR32ri; break;
1415 }
1416 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1417 return Result;
1418 }
Chris Lattner11333092005-01-11 03:11:44 +00001419
1420 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1421 Tmp1 = SelectExpr(N.getOperand(0));
1422 Tmp2 = SelectExpr(N.getOperand(1));
1423 } else {
1424 Tmp2 = SelectExpr(N.getOperand(1));
1425 Tmp1 = SelectExpr(N.getOperand(0));
1426 }
1427
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001428 switch (N.getValueType()) {
1429 default: assert(0 && "Cannot add this type!");
1430 case MVT::i1:
1431 case MVT::i8: Opc = X86::XOR8rr; break;
1432 case MVT::i16: Opc = X86::XOR16rr; break;
1433 case MVT::i32: Opc = X86::XOR32rr; break;
1434 }
1435 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1436 return Result;
1437
1438 case ISD::MUL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001439 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1440 Opc = 0;
1441 switch (N.getValueType()) {
1442 default: assert(0 && "Cannot multiply this type!");
1443 case MVT::i8: break;
1444 case MVT::i16: Opc = X86::IMUL16rri; break;
1445 case MVT::i32: Opc = X86::IMUL32rri; break;
1446 }
1447 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001448 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001449 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1450 return Result;
1451 }
1452 }
Chris Lattner11333092005-01-11 03:11:44 +00001453
1454 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1455 Tmp1 = SelectExpr(N.getOperand(0));
1456 Tmp2 = SelectExpr(N.getOperand(1));
1457 } else {
1458 Tmp2 = SelectExpr(N.getOperand(1));
1459 Tmp1 = SelectExpr(N.getOperand(0));
1460 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001461 switch (N.getValueType()) {
1462 default: assert(0 && "Cannot add this type!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001463 case MVT::i8:
1464 // Must use the MUL instruction, which forces use of AL.
1465 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1466 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1467 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1468 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001469 case MVT::i16: Opc = X86::IMUL16rr; break;
1470 case MVT::i32: Opc = X86::IMUL32rr; break;
1471 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001472 case MVT::f64: Opc = X86::FpMUL; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001473 }
1474 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1475 return Result;
1476
1477 case ISD::SELECT:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001478 if (N.getValueType() != MVT::i1 && N.getValueType() != MVT::i8) {
Chris Lattner11333092005-01-11 03:11:44 +00001479 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1480 Tmp2 = SelectExpr(N.getOperand(1));
1481 Tmp3 = SelectExpr(N.getOperand(2));
1482 } else {
1483 Tmp3 = SelectExpr(N.getOperand(2));
1484 Tmp2 = SelectExpr(N.getOperand(1));
1485 }
Chris Lattner24aad1b2005-01-10 22:10:13 +00001486 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001487 return Result;
1488 } else {
1489 // FIXME: This should not be implemented here, it should be in the generic
1490 // code!
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001491 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1492 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1493 N.getOperand(1)));
1494 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1495 N.getOperand(2)));
1496 } else {
1497 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1498 N.getOperand(2)));
1499 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1500 N.getOperand(1)));
1501 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001502 unsigned TmpReg = MakeReg(MVT::i16);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001503 EmitSelectCC(N.getOperand(0), MVT::i16, Tmp2, Tmp3, TmpReg);
1504 // FIXME: need subregs to do better than this!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001505 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(TmpReg);
1506 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1507 return Result;
1508 }
1509
1510 case ISD::SDIV:
1511 case ISD::UDIV:
1512 case ISD::SREM:
1513 case ISD::UREM: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001514 if (N.getOpcode() == ISD::SDIV)
1515 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1516 // FIXME: These special cases should be handled by the lowering impl!
1517 unsigned RHS = CN->getValue();
1518 bool isNeg = false;
1519 if ((int)RHS < 0) {
1520 isNeg = true;
1521 RHS = -RHS;
1522 }
1523 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1524 unsigned Log = log2(RHS);
1525 unsigned TmpReg = MakeReg(N.getValueType());
1526 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1527 switch (N.getValueType()) {
1528 default: assert("Unknown type to signed divide!");
1529 case MVT::i8:
1530 SAROpc = X86::SAR8ri;
1531 SHROpc = X86::SHR8ri;
1532 ADDOpc = X86::ADD8rr;
1533 NEGOpc = X86::NEG8r;
1534 break;
1535 case MVT::i16:
1536 SAROpc = X86::SAR16ri;
1537 SHROpc = X86::SHR16ri;
1538 ADDOpc = X86::ADD16rr;
1539 NEGOpc = X86::NEG16r;
1540 break;
1541 case MVT::i32:
1542 SAROpc = X86::SAR32ri;
1543 SHROpc = X86::SHR32ri;
1544 ADDOpc = X86::ADD32rr;
1545 NEGOpc = X86::NEG32r;
1546 break;
1547 }
Chris Lattner11333092005-01-11 03:11:44 +00001548 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001549 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1550 unsigned TmpReg2 = MakeReg(N.getValueType());
1551 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1552 unsigned TmpReg3 = MakeReg(N.getValueType());
1553 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1554
1555 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1556 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1557 if (isNeg)
1558 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1559 return Result;
1560 }
1561 }
1562
Chris Lattner11333092005-01-11 03:11:44 +00001563 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1564 Tmp1 = SelectExpr(N.getOperand(0));
1565 Tmp2 = SelectExpr(N.getOperand(1));
1566 } else {
1567 Tmp2 = SelectExpr(N.getOperand(1));
1568 Tmp1 = SelectExpr(N.getOperand(0));
1569 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001570
1571 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1572 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1573 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1574 switch (N.getValueType()) {
1575 default: assert(0 && "Cannot sdiv this type!");
1576 case MVT::i8:
1577 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1578 LoReg = X86::AL;
1579 HiReg = X86::AH;
1580 MovOpcode = X86::MOV8rr;
1581 ClrOpcode = X86::MOV8ri;
1582 SExtOpcode = X86::CBW;
1583 break;
1584 case MVT::i16:
1585 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1586 LoReg = X86::AX;
1587 HiReg = X86::DX;
1588 MovOpcode = X86::MOV16rr;
1589 ClrOpcode = X86::MOV16ri;
1590 SExtOpcode = X86::CWD;
1591 break;
1592 case MVT::i32:
1593 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
1594 LoReg =X86::EAX;
1595 HiReg = X86::EDX;
1596 MovOpcode = X86::MOV32rr;
1597 ClrOpcode = X86::MOV32ri;
1598 SExtOpcode = X86::CDQ;
1599 break;
1600 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1601 case MVT::f32:
1602 case MVT::f64:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001603 if (N.getOpcode() == ISD::SDIV)
1604 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1605 else
1606 assert(0 && "FIXME: Emit frem libcall to fmod!");
1607 return Result;
1608 }
1609
1610 // Set up the low part.
1611 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1612
1613 if (isSigned) {
1614 // Sign extend the low part into the high part.
1615 BuildMI(BB, SExtOpcode, 0);
1616 } else {
1617 // Zero out the high part, effectively zero extending the input.
1618 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1619 }
1620
1621 // Emit the DIV/IDIV instruction.
1622 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1623
1624 // Get the result of the divide or rem.
1625 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1626 return Result;
1627 }
1628
1629 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001630 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1631 switch (N.getValueType()) {
1632 default: assert(0 && "Cannot shift this type!");
1633 case MVT::i8: Opc = X86::SHL8ri; break;
1634 case MVT::i16: Opc = X86::SHL16ri; break;
1635 case MVT::i32: Opc = X86::SHL32ri; break;
1636 }
Chris Lattner11333092005-01-11 03:11:44 +00001637 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001638 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1639 return Result;
1640 }
Chris Lattner11333092005-01-11 03:11:44 +00001641
1642 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1643 Tmp1 = SelectExpr(N.getOperand(0));
1644 Tmp2 = SelectExpr(N.getOperand(1));
1645 } else {
1646 Tmp2 = SelectExpr(N.getOperand(1));
1647 Tmp1 = SelectExpr(N.getOperand(0));
1648 }
1649
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001650 switch (N.getValueType()) {
1651 default: assert(0 && "Cannot shift this type!");
1652 case MVT::i8 : Opc = X86::SHL8rCL; break;
1653 case MVT::i16: Opc = X86::SHL16rCL; break;
1654 case MVT::i32: Opc = X86::SHL32rCL; break;
1655 }
1656 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1657 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1658 return Result;
1659 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001660 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1661 switch (N.getValueType()) {
1662 default: assert(0 && "Cannot shift this type!");
1663 case MVT::i8: Opc = X86::SHR8ri; break;
1664 case MVT::i16: Opc = X86::SHR16ri; break;
1665 case MVT::i32: Opc = X86::SHR32ri; break;
1666 }
Chris Lattner11333092005-01-11 03:11:44 +00001667 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001668 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1669 return Result;
1670 }
Chris Lattner11333092005-01-11 03:11:44 +00001671
1672 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1673 Tmp1 = SelectExpr(N.getOperand(0));
1674 Tmp2 = SelectExpr(N.getOperand(1));
1675 } else {
1676 Tmp2 = SelectExpr(N.getOperand(1));
1677 Tmp1 = SelectExpr(N.getOperand(0));
1678 }
1679
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001680 switch (N.getValueType()) {
1681 default: assert(0 && "Cannot shift this type!");
1682 case MVT::i8 : Opc = X86::SHR8rCL; break;
1683 case MVT::i16: Opc = X86::SHR16rCL; break;
1684 case MVT::i32: Opc = X86::SHR32rCL; break;
1685 }
1686 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1687 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1688 return Result;
1689 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001690 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1691 switch (N.getValueType()) {
1692 default: assert(0 && "Cannot shift this type!");
1693 case MVT::i8: Opc = X86::SAR8ri; break;
1694 case MVT::i16: Opc = X86::SAR16ri; break;
1695 case MVT::i32: Opc = X86::SAR32ri; break;
1696 }
Chris Lattner11333092005-01-11 03:11:44 +00001697 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001698 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1699 return Result;
1700 }
Chris Lattner11333092005-01-11 03:11:44 +00001701
1702 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1703 Tmp1 = SelectExpr(N.getOperand(0));
1704 Tmp2 = SelectExpr(N.getOperand(1));
1705 } else {
1706 Tmp2 = SelectExpr(N.getOperand(1));
1707 Tmp1 = SelectExpr(N.getOperand(0));
1708 }
1709
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001710 switch (N.getValueType()) {
1711 default: assert(0 && "Cannot shift this type!");
1712 case MVT::i8 : Opc = X86::SAR8rCL; break;
1713 case MVT::i16: Opc = X86::SAR16rCL; break;
1714 case MVT::i32: Opc = X86::SAR32rCL; break;
1715 }
1716 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1717 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1718 return Result;
1719
1720 case ISD::SETCC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001721 EmitCMP(N.getOperand(0), N.getOperand(1));
1722 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
1723 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
1724 return Result;
1725 case ISD::LOAD: {
Chris Lattner5188ad72005-01-08 19:28:19 +00001726 // The chain for this load is now lowered.
1727 LoweredTokens.insert(SDOperand(Node, 1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001728
1729 // Make sure we generate both values.
1730 if (Result != 1)
1731 ExprMap[N.getValue(1)] = 1; // Generate the token
1732 else
1733 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1734
Chris Lattner5188ad72005-01-08 19:28:19 +00001735 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001736 default: assert(0 && "Cannot load this type!");
1737 case MVT::i1:
1738 case MVT::i8: Opc = X86::MOV8rm; break;
1739 case MVT::i16: Opc = X86::MOV16rm; break;
1740 case MVT::i32: Opc = X86::MOV32rm; break;
1741 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
1742 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
1743 }
Chris Lattner11333092005-01-11 03:11:44 +00001744
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001745 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00001746 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001747 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
1748 } else {
1749 X86AddressMode AM;
Chris Lattner11333092005-01-11 03:11:44 +00001750 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1751 Select(N.getOperand(0));
1752 SelectAddress(N.getOperand(1), AM);
1753 } else {
1754 SelectAddress(N.getOperand(1), AM);
1755 Select(N.getOperand(0));
1756 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001757 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1758 }
1759 return Result;
1760 }
1761 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001762 // Generate both result values.
1763 if (Result != 1)
1764 ExprMap[N.getValue(1)] = 1; // Generate the token
1765 else
1766 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1767
1768 // FIXME: We are currently ignoring the requested alignment for handling
1769 // greater than the stack alignment. This will need to be revisited at some
1770 // point. Align = N.getOperand(2);
1771
1772 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1773 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1774 std::cerr << "Cannot allocate stack object with greater alignment than"
1775 << " the stack alignment yet!";
1776 abort();
1777 }
1778
1779 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001780 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001781 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
1782 .addImm(CN->getValue());
1783 } else {
Chris Lattner11333092005-01-11 03:11:44 +00001784 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1785 Select(N.getOperand(0));
1786 Tmp1 = SelectExpr(N.getOperand(1));
1787 } else {
1788 Tmp1 = SelectExpr(N.getOperand(1));
1789 Select(N.getOperand(0));
1790 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001791
1792 // Subtract size from stack pointer, thereby allocating some space.
1793 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
1794 }
1795
1796 // Put a pointer to the space into the result register, by copying the stack
1797 // pointer.
1798 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
1799 return Result;
1800
1801 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00001802 // The chain for this call is now lowered.
1803 LoweredTokens.insert(N.getValue(Node->getNumValues()-1));
1804
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001805 if (GlobalAddressSDNode *GASD =
1806 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001807 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001808 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
1809 } else if (ExternalSymbolSDNode *ESSDN =
1810 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001811 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001812 BuildMI(BB, X86::CALLpcrel32,
1813 1).addExternalSymbol(ESSDN->getSymbol(), true);
1814 } else {
Chris Lattner11333092005-01-11 03:11:44 +00001815 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1816 Select(N.getOperand(0));
1817 Tmp1 = SelectExpr(N.getOperand(1));
1818 } else {
1819 Tmp1 = SelectExpr(N.getOperand(1));
1820 Select(N.getOperand(0));
1821 }
1822
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001823 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
1824 }
Chris Lattner5188ad72005-01-08 19:28:19 +00001825 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001826 default: assert(0 && "Unknown value type for call result!");
1827 case MVT::Other: return 1;
1828 case MVT::i1:
1829 case MVT::i8:
1830 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1831 break;
1832 case MVT::i16:
1833 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
1834 break;
1835 case MVT::i32:
1836 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00001837 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001838 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
1839 break;
1840 case MVT::f32:
1841 case MVT::f64: // Floating-point return values live in %ST(0)
1842 ContainsFPCode = true;
1843 BuildMI(BB, X86::FpGETRESULT, 1, Result);
1844 break;
1845 }
1846 return Result+N.ResNo;
1847 }
1848
1849 return 0;
1850}
1851
1852void ISel::Select(SDOperand N) {
1853 unsigned Tmp1, Tmp2, Opc;
1854
1855 // FIXME: Disable for our current expansion model!
1856 if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second)
1857 return; // Already selected.
1858
Chris Lattner989de032005-01-11 06:14:36 +00001859 SDNode *Node = N.Val;
1860
1861 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001862 default:
Chris Lattner989de032005-01-11 06:14:36 +00001863 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001864 assert(0 && "Node not handled yet!");
1865 case ISD::EntryToken: return; // Noop
1866 case ISD::CopyToReg:
1867 Select(N.getOperand(0));
1868 Tmp1 = SelectExpr(N.getOperand(1));
1869 Tmp2 = cast<CopyRegSDNode>(N)->getReg();
1870
1871 if (Tmp1 != Tmp2) {
1872 switch (N.getOperand(1).getValueType()) {
1873 default: assert(0 && "Invalid type for operation!");
1874 case MVT::i1:
1875 case MVT::i8: Opc = X86::MOV8rr; break;
1876 case MVT::i16: Opc = X86::MOV16rr; break;
1877 case MVT::i32: Opc = X86::MOV32rr; break;
1878 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001879 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001880 }
1881 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1882 }
1883 return;
1884 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001885 switch (N.getNumOperands()) {
1886 default:
1887 assert(0 && "Unknown return instruction!");
1888 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001889 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1890 N.getOperand(2).getValueType() == MVT::i32 &&
1891 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00001892 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1893 Tmp1 = SelectExpr(N.getOperand(1));
1894 Tmp2 = SelectExpr(N.getOperand(2));
1895 } else {
1896 Tmp2 = SelectExpr(N.getOperand(2));
1897 Tmp1 = SelectExpr(N.getOperand(1));
1898 }
1899 Select(N.getOperand(0));
1900
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001901 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
1902 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
1903 // Declare that EAX & EDX are live on exit.
1904 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
1905 .addReg(X86::ESP);
1906 break;
1907 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00001908 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1909 Select(N.getOperand(0));
1910 Tmp1 = SelectExpr(N.getOperand(1));
1911 } else {
1912 Tmp1 = SelectExpr(N.getOperand(1));
1913 Select(N.getOperand(0));
1914 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001915 switch (N.getOperand(1).getValueType()) {
1916 default: assert(0 && "All other types should have been promoted!!");
1917 case MVT::f64:
1918 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
1919 // Declare that top-of-stack is live on exit
1920 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
1921 break;
1922 case MVT::i32:
1923 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
1924 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
1925 break;
1926 }
1927 break;
1928 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00001929 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001930 break;
1931 }
1932 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
1933 return;
1934 case ISD::BR: {
1935 Select(N.getOperand(0));
1936 MachineBasicBlock *Dest =
1937 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1938 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
1939 return;
1940 }
1941
1942 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001943 MachineBasicBlock *Dest =
1944 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00001945
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001946 // Try to fold a setcc into the branch. If this fails, emit a test/jne
1947 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00001948 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
1949 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1950 Select(N.getOperand(0));
1951 Tmp1 = SelectExpr(N.getOperand(1));
1952 } else {
1953 Tmp1 = SelectExpr(N.getOperand(1));
1954 Select(N.getOperand(0));
1955 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001956 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
1957 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
1958 }
Chris Lattner11333092005-01-11 03:11:44 +00001959
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001960 return;
1961 }
1962 case ISD::LOAD:
1963 case ISD::CALL:
1964 case ISD::DYNAMIC_STACKALLOC:
1965 SelectExpr(N);
1966 return;
1967 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001968 // Select the address.
1969 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001970
1971 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1972 Opc = 0;
1973 switch (CN->getValueType(0)) {
1974 default: assert(0 && "Invalid type for operation!");
1975 case MVT::i1:
1976 case MVT::i8: Opc = X86::MOV8mi; break;
1977 case MVT::i16: Opc = X86::MOV16mi; break;
1978 case MVT::i32: Opc = X86::MOV32mi; break;
1979 case MVT::f32:
1980 case MVT::f64: break;
1981 }
1982 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001983 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
1984 Select(N.getOperand(0));
1985 SelectAddress(N.getOperand(2), AM);
1986 } else {
1987 SelectAddress(N.getOperand(2), AM);
1988 Select(N.getOperand(0));
1989 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001990 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
1991 return;
1992 }
1993 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001994 switch (N.getOperand(1).getValueType()) {
1995 default: assert(0 && "Cannot store this type!");
1996 case MVT::i1:
1997 case MVT::i8: Opc = X86::MOV8mr; break;
1998 case MVT::i16: Opc = X86::MOV16mr; break;
1999 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002000 case MVT::f32: Opc = X86::FST32m; break;
2001 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002002 }
Chris Lattner11333092005-01-11 03:11:44 +00002003
2004 std::vector<std::pair<unsigned, unsigned> > RP;
2005 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2006 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2007 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2008 std::sort(RP.begin(), RP.end());
2009
2010 for (unsigned i = 0; i != 3; ++i)
2011 switch (RP[2-i].second) {
2012 default: assert(0 && "Unknown operand number!");
2013 case 0: Select(N.getOperand(0)); break;
2014 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002015 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002016 }
2017
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002018 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2019 return;
2020 }
2021 case ISD::ADJCALLSTACKDOWN:
2022 case ISD::ADJCALLSTACKUP:
2023 Select(N.getOperand(0));
2024 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2025
2026 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2027 X86::ADJCALLSTACKUP;
2028 BuildMI(BB, Opc, 1).addImm(Tmp1);
2029 return;
Chris Lattner989de032005-01-11 06:14:36 +00002030 case ISD::MEMSET: {
2031 Select(N.getOperand(0)); // Select the chain.
2032 unsigned Align =
2033 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2034 if (Align == 0) Align = 1;
2035
2036 // Turn the byte code into # iterations
2037 unsigned CountReg;
2038 unsigned Opcode;
2039 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2040 unsigned Val = ValC->getValue() & 255;
2041
2042 // If the value is a constant, then we can potentially use larger sets.
2043 switch (Align & 3) {
2044 case 2: // WORD aligned
2045 CountReg = MakeReg(MVT::i32);
2046 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2047 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2048 } else {
2049 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2050 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2051 }
2052 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2053 Opcode = X86::REP_STOSW;
2054 break;
2055 case 0: // DWORD aligned
2056 CountReg = MakeReg(MVT::i32);
2057 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2058 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2059 } else {
2060 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2061 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2062 }
2063 Val = (Val << 8) | Val;
2064 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
2065 Opcode = X86::REP_STOSD;
2066 break;
2067 default: // BYTE aligned
2068 CountReg = SelectExpr(Node->getOperand(3));
2069 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
2070 Opcode = X86::REP_STOSB;
2071 break;
2072 }
2073 } else {
2074 // If it's not a constant value we are storing, just fall back. We could
2075 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
2076 unsigned ValReg = SelectExpr(Node->getOperand(2));
2077 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
2078 CountReg = SelectExpr(Node->getOperand(3));
2079 Opcode = X86::REP_STOSB;
2080 }
2081
2082 // No matter what the alignment is, we put the source in ESI, the
2083 // destination in EDI, and the count in ECX.
2084 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2085 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2086 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2087 BuildMI(BB, Opcode, 0);
2088 return;
2089 }
Chris Lattner31805bf2005-01-11 06:19:26 +00002090 case ISD::MEMCPY:
2091 Select(N.getOperand(0)); // Select the chain.
2092 unsigned Align =
2093 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2094 if (Align == 0) Align = 1;
2095
2096 // Turn the byte code into # iterations
2097 unsigned CountReg;
2098 unsigned Opcode;
2099 switch (Align & 3) {
2100 case 2: // WORD aligned
2101 CountReg = MakeReg(MVT::i32);
2102 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2103 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2104 } else {
2105 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2106 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2107 }
2108 Opcode = X86::REP_MOVSW;
2109 break;
2110 case 0: // DWORD aligned
2111 CountReg = MakeReg(MVT::i32);
2112 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2113 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2114 } else {
2115 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2116 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2117 }
2118 Opcode = X86::REP_MOVSD;
2119 break;
2120 default: // BYTE aligned
2121 CountReg = SelectExpr(Node->getOperand(3));
2122 Opcode = X86::REP_MOVSB;
2123 break;
2124 }
2125
2126 // No matter what the alignment is, we put the source in ESI, the
2127 // destination in EDI, and the count in ECX.
2128 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2129 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
2130 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2131 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2132 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
2133 BuildMI(BB, Opcode, 0);
2134 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002135 }
2136 assert(0 && "Should not be reached!");
2137}
2138
2139
2140/// createX86PatternInstructionSelector - This pass converts an LLVM function
2141/// into a machine code representation using pattern matching and a machine
2142/// description file.
2143///
2144FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
2145 return new ISel(TM);
2146}