blob: 40e4c414ff9a637bc1f357adb0e4434d35914fe7 [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;
Chris Lattner51a26342005-01-11 06:36:20 +0000432 SDOperand ShVal = N.Val->getOperand(0);
433
434 // Okay, we know that we have a scale by now. However, if the scaled
435 // value is an add of something and a constant, we can fold the
436 // constant into the disp field here.
437 if (ShVal.Val->getOpcode() == ISD::ADD &&
438 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
439 AM.IndexReg = SelectExpr(ShVal.Val->getOperand(0));
440 ConstantSDNode *AddVal =
441 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
442 AM.Disp += AddVal->getValue() << Val;
443 } else {
444 AM.IndexReg = SelectExpr(ShVal);
445 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000446 return false;
447 }
448 }
449 break;
450
451 case ISD::ADD: {
452 X86AddressMode Backup = AM;
453 if (!SelectAddress(N.Val->getOperand(0), AM) &&
454 !SelectAddress(N.Val->getOperand(1), AM))
455 return false;
456 AM = Backup;
457 break;
458 }
459 }
460
Chris Lattnera95589b2005-01-11 04:40:19 +0000461 // Is the base register already occupied?
462 if (AM.BaseType != X86AddressMode::RegBase || AM.Base.Reg) {
463 // If so, check to see if the scale index register is set.
464 if (AM.IndexReg == 0) {
465 AM.IndexReg = SelectExpr(N);
466 AM.Scale = 1;
467 return false;
468 }
469
470 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000471 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000472 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000473
474 // Default, generate it as a register.
475 AM.BaseType = X86AddressMode::RegBase;
476 AM.Base.Reg = SelectExpr(N);
477 return false;
478}
479
480/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
481/// assuming that the temporary registers are in the 8-bit register class.
482///
483/// Tmp1 = setcc1
484/// Tmp2 = setcc2
485/// DestReg = logicalop Tmp1, Tmp2
486///
487static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
488 unsigned SetCC2, unsigned LogicalOp,
489 unsigned DestReg) {
490 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
491 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
492 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
493 BuildMI(BB, SetCC1, 0, Tmp1);
494 BuildMI(BB, SetCC2, 0, Tmp2);
495 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
496}
497
498/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
499/// condition codes match the specified SetCCOpcode. Note that some conditions
500/// require multiple instructions to generate the correct value.
501static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
502 ISD::CondCode SetCCOpcode, bool isFP) {
503 unsigned Opc;
504 if (!isFP) {
505 switch (SetCCOpcode) {
506 default: assert(0 && "Illegal integer SetCC!");
507 case ISD::SETEQ: Opc = X86::SETEr; break;
508 case ISD::SETGT: Opc = X86::SETGr; break;
509 case ISD::SETGE: Opc = X86::SETGEr; break;
510 case ISD::SETLT: Opc = X86::SETLr; break;
511 case ISD::SETLE: Opc = X86::SETLEr; break;
512 case ISD::SETNE: Opc = X86::SETNEr; break;
513 case ISD::SETULT: Opc = X86::SETBr; break;
514 case ISD::SETUGT: Opc = X86::SETAr; break;
515 case ISD::SETULE: Opc = X86::SETBEr; break;
516 case ISD::SETUGE: Opc = X86::SETAEr; break;
517 }
518 } else {
519 // On a floating point condition, the flags are set as follows:
520 // ZF PF CF op
521 // 0 | 0 | 0 | X > Y
522 // 0 | 0 | 1 | X < Y
523 // 1 | 0 | 0 | X == Y
524 // 1 | 1 | 1 | unordered
525 //
526 switch (SetCCOpcode) {
527 default: assert(0 && "Invalid FP setcc!");
528 case ISD::SETUEQ:
529 case ISD::SETEQ:
530 Opc = X86::SETEr; // True if ZF = 1
531 break;
532 case ISD::SETOGT:
533 case ISD::SETGT:
534 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
535 break;
536 case ISD::SETOGE:
537 case ISD::SETGE:
538 Opc = X86::SETAEr; // True if CF = 0
539 break;
540 case ISD::SETULT:
541 case ISD::SETLT:
542 Opc = X86::SETBr; // True if CF = 1
543 break;
544 case ISD::SETULE:
545 case ISD::SETLE:
546 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
547 break;
548 case ISD::SETONE:
549 case ISD::SETNE:
550 Opc = X86::SETNEr; // True if ZF = 0
551 break;
552 case ISD::SETUO:
553 Opc = X86::SETPr; // True if PF = 1
554 break;
555 case ISD::SETO:
556 Opc = X86::SETNPr; // True if PF = 0
557 break;
558 case ISD::SETOEQ: // !PF & ZF
559 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
560 return;
561 case ISD::SETOLT: // !PF & CF
562 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
563 return;
564 case ISD::SETOLE: // !PF & (CF || ZF)
565 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
566 return;
567 case ISD::SETUGT: // PF | (!ZF & !CF)
568 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
569 return;
570 case ISD::SETUGE: // PF | !CF
571 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
572 return;
573 case ISD::SETUNE: // PF | !ZF
574 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
575 return;
576 }
577 }
578 BuildMI(BB, Opc, 0, DestReg);
579}
580
581
582/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
583/// the Dest block if the Cond condition is true. If we cannot fold this
584/// condition into the branch, return true.
585///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000586bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
587 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000588 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
589 // B) using two conditional branches instead of one condbr, two setcc's, and
590 // an or.
591 if ((Cond.getOpcode() == ISD::OR ||
592 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
593 // And and or set the flags for us, so there is no need to emit a TST of the
594 // result. It is only safe to do this if there is only a single use of the
595 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000596 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000597 SelectExpr(Cond);
598 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
599 return false;
600 }
601
602 // Codegen br not C -> JE.
603 if (Cond.getOpcode() == ISD::XOR)
604 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
605 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000606 unsigned CondR;
607 if (getRegPressure(Chain) > getRegPressure(Cond)) {
608 Select(Chain);
609 CondR = SelectExpr(Cond.Val->getOperand(0));
610 } else {
611 CondR = SelectExpr(Cond.Val->getOperand(0));
612 Select(Chain);
613 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000614 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
615 BuildMI(BB, X86::JE, 1).addMBB(Dest);
616 return false;
617 }
618
619 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
620 if (SetCC == 0)
621 return true; // Can only handle simple setcc's so far.
622
623 unsigned Opc;
624
625 // Handle integer conditions first.
626 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
627 switch (SetCC->getCondition()) {
628 default: assert(0 && "Illegal integer SetCC!");
629 case ISD::SETEQ: Opc = X86::JE; break;
630 case ISD::SETGT: Opc = X86::JG; break;
631 case ISD::SETGE: Opc = X86::JGE; break;
632 case ISD::SETLT: Opc = X86::JL; break;
633 case ISD::SETLE: Opc = X86::JLE; break;
634 case ISD::SETNE: Opc = X86::JNE; break;
635 case ISD::SETULT: Opc = X86::JB; break;
636 case ISD::SETUGT: Opc = X86::JA; break;
637 case ISD::SETULE: Opc = X86::JBE; break;
638 case ISD::SETUGE: Opc = X86::JAE; break;
639 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000640 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000641 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
642 BuildMI(BB, Opc, 1).addMBB(Dest);
643 return false;
644 }
645
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000646 unsigned Opc2 = 0; // Second branch if needed.
647
648 // On a floating point condition, the flags are set as follows:
649 // ZF PF CF op
650 // 0 | 0 | 0 | X > Y
651 // 0 | 0 | 1 | X < Y
652 // 1 | 0 | 0 | X == Y
653 // 1 | 1 | 1 | unordered
654 //
655 switch (SetCC->getCondition()) {
656 default: assert(0 && "Invalid FP setcc!");
657 case ISD::SETUEQ:
658 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
659 case ISD::SETOGT:
660 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
661 case ISD::SETOGE:
662 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
663 case ISD::SETULT:
664 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
665 case ISD::SETULE:
666 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
667 case ISD::SETONE:
668 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
669 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
670 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
671 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
672 Opc = X86::JA; // ZF = 0 & CF = 0
673 Opc2 = X86::JP; // PF = 1
674 break;
675 case ISD::SETUGE: // PF = 1 | CF = 0
676 Opc = X86::JAE; // CF = 0
677 Opc2 = X86::JP; // PF = 1
678 break;
679 case ISD::SETUNE: // PF = 1 | ZF = 0
680 Opc = X86::JNE; // ZF = 0
681 Opc2 = X86::JP; // PF = 1
682 break;
683 case ISD::SETOEQ: // PF = 0 & ZF = 1
684 //X86::JNP, X86::JE
685 //X86::AND8rr
686 return true; // FIXME: Emit more efficient code for this branch.
687 case ISD::SETOLT: // PF = 0 & CF = 1
688 //X86::JNP, X86::JB
689 //X86::AND8rr
690 return true; // FIXME: Emit more efficient code for this branch.
691 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
692 //X86::JNP, X86::JBE
693 //X86::AND8rr
694 return true; // FIXME: Emit more efficient code for this branch.
695 }
696
Chris Lattner6c07aee2005-01-11 04:06:27 +0000697 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000698 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
699 BuildMI(BB, Opc, 1).addMBB(Dest);
700 if (Opc2)
701 BuildMI(BB, Opc2, 1).addMBB(Dest);
702 return false;
703}
704
Chris Lattner24aad1b2005-01-10 22:10:13 +0000705/// EmitSelectCC - Emit code into BB that performs a select operation between
706/// the two registers RTrue and RFalse, generating a result into RDest. Return
707/// true if the fold cannot be performed.
708///
709void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
710 unsigned RTrue, unsigned RFalse, unsigned RDest) {
711 enum Condition {
712 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
713 NOT_SET
714 } CondCode = NOT_SET;
715
716 static const unsigned CMOVTAB16[] = {
717 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
718 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
719 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
720 };
721 static const unsigned CMOVTAB32[] = {
722 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
723 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
724 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
725 };
726 static const unsigned CMOVTABFP[] = {
727 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
728 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
729 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
730 };
731
732 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
733 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
734 switch (SetCC->getCondition()) {
735 default: assert(0 && "Unknown integer comparison!");
736 case ISD::SETEQ: CondCode = EQ; break;
737 case ISD::SETGT: CondCode = GT; break;
738 case ISD::SETGE: CondCode = GE; break;
739 case ISD::SETLT: CondCode = LT; break;
740 case ISD::SETLE: CondCode = LE; break;
741 case ISD::SETNE: CondCode = NE; break;
742 case ISD::SETULT: CondCode = B; break;
743 case ISD::SETUGT: CondCode = A; break;
744 case ISD::SETULE: CondCode = BE; break;
745 case ISD::SETUGE: CondCode = AE; break;
746 }
747 } else {
748 // On a floating point condition, the flags are set as follows:
749 // ZF PF CF op
750 // 0 | 0 | 0 | X > Y
751 // 0 | 0 | 1 | X < Y
752 // 1 | 0 | 0 | X == Y
753 // 1 | 1 | 1 | unordered
754 //
755 switch (SetCC->getCondition()) {
756 default: assert(0 && "Unknown FP comparison!");
757 case ISD::SETUEQ:
758 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
759 case ISD::SETOGT:
760 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
761 case ISD::SETOGE:
762 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
763 case ISD::SETULT:
764 case ISD::SETLT: CondCode = B; break; // True if CF = 1
765 case ISD::SETULE:
766 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
767 case ISD::SETONE:
768 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
769 case ISD::SETUO: CondCode = P; break; // True if PF = 1
770 case ISD::SETO: CondCode = NP; break; // True if PF = 0
771 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
772 case ISD::SETUGE: // PF = 1 | CF = 0
773 case ISD::SETUNE: // PF = 1 | ZF = 0
774 case ISD::SETOEQ: // PF = 0 & ZF = 1
775 case ISD::SETOLT: // PF = 0 & CF = 1
776 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
777 // We cannot emit this comparison as a single cmov.
778 break;
779 }
780 }
781 }
782
783 unsigned Opc = 0;
784 if (CondCode != NOT_SET) {
785 switch (SVT) {
786 default: assert(0 && "Cannot select this type!");
787 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
788 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
789 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000790 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000791 }
792 }
793
794 // Finally, if we weren't able to fold this, just emit the condition and test
795 // it.
796 if (CondCode == NOT_SET || Opc == 0) {
797 // Get the condition into the zero flag.
798 unsigned CondReg = SelectExpr(Cond);
799 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
800
801 switch (SVT) {
802 default: assert(0 && "Cannot select this type!");
803 case MVT::i16: Opc = X86::CMOVE16rr; break;
804 case MVT::i32: Opc = X86::CMOVE32rr; break;
805 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000806 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000807 }
808 } else {
809 // FIXME: CMP R, 0 -> TEST R, R
810 EmitCMP(Cond.getOperand(0), Cond.getOperand(1));
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000811 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000812 }
813 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
814}
815
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000816void ISel::EmitCMP(SDOperand LHS, SDOperand RHS) {
Chris Lattner11333092005-01-11 03:11:44 +0000817 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000818 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
819 Opc = 0;
820 switch (RHS.getValueType()) {
821 default: break;
822 case MVT::i1:
823 case MVT::i8: Opc = X86::CMP8ri; break;
824 case MVT::i16: Opc = X86::CMP16ri; break;
825 case MVT::i32: Opc = X86::CMP32ri; break;
826 }
827 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +0000828 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000829 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
830 return;
831 }
832 }
833
834 switch (LHS.getValueType()) {
835 default: assert(0 && "Cannot compare this value!");
836 case MVT::i1:
837 case MVT::i8: Opc = X86::CMP8rr; break;
838 case MVT::i16: Opc = X86::CMP16rr; break;
839 case MVT::i32: Opc = X86::CMP32rr; break;
840 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000841 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000842 }
Chris Lattner11333092005-01-11 03:11:44 +0000843 unsigned Tmp1, Tmp2;
844 if (getRegPressure(LHS) > getRegPressure(RHS)) {
845 Tmp1 = SelectExpr(LHS);
846 Tmp2 = SelectExpr(RHS);
847 } else {
848 Tmp2 = SelectExpr(RHS);
849 Tmp1 = SelectExpr(LHS);
850 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000851 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
852}
853
854unsigned ISel::SelectExpr(SDOperand N) {
855 unsigned Result;
856 unsigned Tmp1, Tmp2, Tmp3;
857 unsigned Opc = 0;
858
Chris Lattner5188ad72005-01-08 19:28:19 +0000859 SDNode *Node = N.Val;
860
Chris Lattner590d8002005-01-09 18:52:44 +0000861 if (Node->getOpcode() == ISD::CopyFromReg)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000862 // Just use the specified register as our input.
Chris Lattner590d8002005-01-09 18:52:44 +0000863 return dyn_cast<CopyRegSDNode>(Node)->getReg();
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000864
865 // If there are multiple uses of this expression, memorize the
866 // register it is code generated in, instead of emitting it multiple
867 // times.
868 // FIXME: Disabled for our current selection model.
Chris Lattner5188ad72005-01-08 19:28:19 +0000869 if (1 || !Node->hasOneUse()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000870 unsigned &Reg = ExprMap[N];
871 if (Reg) return Reg;
872
873 if (N.getOpcode() != ISD::CALL)
874 Reg = Result = (N.getValueType() != MVT::Other) ?
875 MakeReg(N.getValueType()) : 1;
876 else {
877 // If this is a call instruction, make sure to prepare ALL of the result
878 // values as well as the chain.
Chris Lattner5188ad72005-01-08 19:28:19 +0000879 if (Node->getNumValues() == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000880 Reg = Result = 1; // Void call, just a chain.
881 else {
Chris Lattner5188ad72005-01-08 19:28:19 +0000882 Result = MakeReg(Node->getValueType(0));
883 ExprMap[N.getValue(0)] = Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000884 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Chris Lattner5188ad72005-01-08 19:28:19 +0000885 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
886 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000887 }
888 }
889 } else {
890 Result = MakeReg(N.getValueType());
891 }
892
893 switch (N.getOpcode()) {
894 default:
Chris Lattner5188ad72005-01-08 19:28:19 +0000895 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000896 assert(0 && "Node not handled!\n");
897 case ISD::FrameIndex:
898 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
899 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
900 return Result;
901 case ISD::ConstantPool:
902 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
903 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
904 return Result;
905 case ISD::ConstantFP:
906 ContainsFPCode = true;
907 Tmp1 = Result; // Intermediate Register
908 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
909 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
910 Tmp1 = MakeReg(MVT::f64);
911
912 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
913 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
914 BuildMI(BB, X86::FLD0, 0, Tmp1);
915 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
916 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
917 BuildMI(BB, X86::FLD1, 0, Tmp1);
918 else
919 assert(0 && "Unexpected constant!");
920 if (Tmp1 != Result)
921 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
922 return Result;
923 case ISD::Constant:
924 switch (N.getValueType()) {
925 default: assert(0 && "Cannot use constants of this type!");
926 case MVT::i1:
927 case MVT::i8: Opc = X86::MOV8ri; break;
928 case MVT::i16: Opc = X86::MOV16ri; break;
929 case MVT::i32: Opc = X86::MOV32ri; break;
930 }
931 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
932 return Result;
933 case ISD::GlobalAddress: {
934 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
935 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
936 return Result;
937 }
938 case ISD::ExternalSymbol: {
939 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
940 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
941 return Result;
942 }
943 case ISD::FP_EXTEND:
944 Tmp1 = SelectExpr(N.getOperand(0));
945 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000946 return Result;
947 case ISD::ZERO_EXTEND: {
948 int DestIs16 = N.getValueType() == MVT::i16;
949 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +0000950 Tmp1 = SelectExpr(N.getOperand(0));
951
952 // FIXME: This hack is here for zero extension casts from bool to i8. This
953 // would not be needed if bools were promoted by Legalize.
954 if (N.getValueType() == MVT::i8) {
955 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
956 return Result;
957 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000958
959 static const unsigned Opc[3] = {
960 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
961 };
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000962 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
963 return Result;
964 }
965 case ISD::SIGN_EXTEND: {
966 int DestIs16 = N.getValueType() == MVT::i16;
967 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
968
Chris Lattner590d8002005-01-09 18:52:44 +0000969 // FIXME: Legalize should promote bools to i8!
970 assert(N.getOperand(0).getValueType() != MVT::i1 &&
971 "Sign extend from bool not implemented!");
972
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000973 static const unsigned Opc[3] = {
974 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
975 };
976 Tmp1 = SelectExpr(N.getOperand(0));
977 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
978 return Result;
979 }
980 case ISD::TRUNCATE:
981 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
982 // a move out of AX or AL.
983 switch (N.getOperand(0).getValueType()) {
984 default: assert(0 && "Unknown truncate!");
985 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
986 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
987 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
988 }
989 Tmp1 = SelectExpr(N.getOperand(0));
990 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
991
992 switch (N.getValueType()) {
993 default: assert(0 && "Unknown truncate!");
994 case MVT::i1:
995 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
996 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
997 }
998 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
999 return Result;
1000
1001 case ISD::FP_ROUND:
1002 // Truncate from double to float by storing to memory as float,
1003 // then reading it back into a register.
1004
1005 // Create as stack slot to use.
Chris Lattner590d8002005-01-09 18:52:44 +00001006 // FIXME: This should automatically be made by the Legalizer!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001007 Tmp1 = TLI.getTargetData().getFloatAlignment();
1008 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
1009
1010 // Codegen the input.
1011 Tmp1 = SelectExpr(N.getOperand(0));
1012
1013 // Emit the store, then the reload.
1014 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
1015 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001016 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00001017
1018 case ISD::SINT_TO_FP:
1019 case ISD::UINT_TO_FP: {
1020 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001021 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001022
1023 // Promote the integer to a type supported by FLD. We do this because there
1024 // are no unsigned FLD instructions, so we must promote an unsigned value to
1025 // a larger signed value, then use FLD on the larger value.
1026 //
1027 MVT::ValueType PromoteType = MVT::Other;
1028 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1029 unsigned PromoteOpcode = 0;
1030 unsigned RealDestReg = Result;
1031 switch (SrcTy) {
1032 case MVT::i1:
1033 case MVT::i8:
1034 // We don't have the facilities for directly loading byte sized data from
1035 // memory (even signed). Promote it to 16 bits.
1036 PromoteType = MVT::i16;
1037 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1038 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1039 break;
1040 case MVT::i16:
1041 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1042 PromoteType = MVT::i32;
1043 PromoteOpcode = X86::MOVZX32rr16;
1044 }
1045 break;
1046 default:
1047 // Don't fild into the real destination.
1048 if (Node->getOpcode() == ISD::UINT_TO_FP)
1049 Result = MakeReg(Node->getValueType(0));
1050 break;
1051 }
1052
1053 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1054
1055 if (PromoteType != MVT::Other) {
1056 Tmp2 = MakeReg(PromoteType);
1057 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1058 SrcTy = PromoteType;
1059 Tmp1 = Tmp2;
1060 }
1061
1062 // Spill the integer to memory and reload it from there.
1063 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1064 MachineFunction *F = BB->getParent();
1065 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1066
1067 switch (SrcTy) {
1068 case MVT::i64:
1069 // FIXME: this won't work for cast [u]long to FP
1070 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1071 FrameIdx).addReg(Tmp1);
1072 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1073 FrameIdx, 4).addReg(Tmp1+1);
1074 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1075 break;
1076 case MVT::i32:
1077 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1078 FrameIdx).addReg(Tmp1);
1079 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1080 break;
1081 case MVT::i16:
1082 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1083 FrameIdx).addReg(Tmp1);
1084 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1085 break;
1086 default: break; // No promotion required.
1087 }
1088
1089 if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i32) {
1090 // If this is a cast from uint -> double, we need to be careful when if
1091 // the "sign" bit is set. If so, we don't want to make a negative number,
1092 // we want to make a positive number. Emit code to add an offset if the
1093 // sign bit is set.
1094
1095 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1096 unsigned IsNeg = MakeReg(MVT::i32);
1097 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1098
1099 // Create a CP value that has the offset in one word and 0 in the other.
1100 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1101 0x4f80000000000000ULL);
1102 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1103 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1104 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1105
1106 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1107 // We need special handling for unsigned 64-bit integer sources. If the
1108 // input number has the "sign bit" set, then we loaded it incorrectly as a
1109 // negative 64-bit number. In this case, add an offset value.
1110
1111 // Emit a test instruction to see if the dynamic input value was signed.
1112 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1113
1114 // If the sign bit is set, get a pointer to an offset, otherwise get a
1115 // pointer to a zero.
1116 MachineConstantPool *CP = F->getConstantPool();
1117 unsigned Zero = MakeReg(MVT::i32);
1118 Constant *Null = Constant::getNullValue(Type::UIntTy);
1119 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1120 CP->getConstantPoolIndex(Null));
1121 unsigned Offset = MakeReg(MVT::i32);
1122 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1123
1124 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1125 CP->getConstantPoolIndex(OffsetCst));
1126 unsigned Addr = MakeReg(MVT::i32);
1127 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1128
1129 // Load the constant for an add. FIXME: this could make an 'fadd' that
1130 // reads directly from memory, but we don't support these yet.
1131 unsigned ConstReg = MakeReg(MVT::f64);
1132 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1133
1134 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1135 }
1136 return RealDestReg;
1137 }
1138 case ISD::FP_TO_SINT:
1139 case ISD::FP_TO_UINT: {
1140 // FIXME: Most of this grunt work should be done by legalize!
1141 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1142
1143 // Change the floating point control register to use "round towards zero"
1144 // mode when truncating to an integer value.
1145 //
1146 MachineFunction *F = BB->getParent();
1147 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1148 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1149
1150 // Load the old value of the high byte of the control word...
1151 unsigned HighPartOfCW = MakeReg(MVT::i8);
1152 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1153 CWFrameIdx, 1);
1154
1155 // Set the high part to be round to zero...
1156 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1157 CWFrameIdx, 1).addImm(12);
1158
1159 // Reload the modified control word now...
1160 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1161
1162 // Restore the memory image of control word to original value
1163 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1164 CWFrameIdx, 1).addReg(HighPartOfCW);
1165
1166 // We don't have the facilities for directly storing byte sized data to
1167 // memory. Promote it to 16 bits. We also must promote unsigned values to
1168 // larger classes because we only have signed FP stores.
1169 MVT::ValueType StoreClass = Node->getValueType(0);
1170 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1171 switch (StoreClass) {
1172 case MVT::i8: StoreClass = MVT::i16; break;
1173 case MVT::i16: StoreClass = MVT::i32; break;
1174 case MVT::i32: StoreClass = MVT::i64; break;
1175 // The following treatment of cLong may not be perfectly right,
1176 // but it survives chains of casts of the form
1177 // double->ulong->double.
1178 case MVT::i64: StoreClass = MVT::i64; break;
1179 default: assert(0 && "Unknown store class!");
1180 }
1181
1182 // Spill the integer to memory and reload it from there.
1183 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1184 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1185
1186 switch (StoreClass) {
1187 default: assert(0 && "Unknown store class!");
1188 case MVT::i16:
1189 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1190 break;
1191 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001192 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001193 break;
1194 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001195 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001196 break;
1197 }
1198
1199 switch (Node->getValueType(0)) {
1200 default:
1201 assert(0 && "Unknown integer type!");
1202 case MVT::i64:
1203 // FIXME: this isn't gunna work.
1204 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1205 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1206 case MVT::i32:
1207 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1208 break;
1209 case MVT::i16:
1210 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1211 break;
1212 case MVT::i8:
1213 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1214 break;
1215 }
1216
1217 // Reload the original control word now.
1218 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1219 return Result;
1220 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001221 case ISD::ADD:
1222 // See if we can codegen this as an LEA to fold operations together.
1223 if (N.getValueType() == MVT::i32) {
1224 X86AddressMode AM;
1225 if (!SelectAddress(N.getOperand(0), AM) &&
1226 !SelectAddress(N.getOperand(1), AM)) {
1227 // If this is not just an add, emit the LEA. For a simple add (like
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001228 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001229 // leave this as LEA, then peephole it to 'ADD' after two address elim
1230 // happens.
1231 if (AM.Scale != 1 || AM.BaseType == X86AddressMode::FrameIndexBase ||
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001232 AM.GV || (AM.Base.Reg && AM.IndexReg && AM.Disp)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001233 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1234 return Result;
1235 }
1236 }
1237 }
Chris Lattner11333092005-01-11 03:11:44 +00001238
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001239 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1240 Opc = 0;
1241 if (CN->getValue() == 1) { // add X, 1 -> inc X
1242 switch (N.getValueType()) {
1243 default: assert(0 && "Cannot integer add this type!");
1244 case MVT::i8: Opc = X86::INC8r; break;
1245 case MVT::i16: Opc = X86::INC16r; break;
1246 case MVT::i32: Opc = X86::INC32r; break;
1247 }
1248 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1249 switch (N.getValueType()) {
1250 default: assert(0 && "Cannot integer add this type!");
1251 case MVT::i8: Opc = X86::DEC8r; break;
1252 case MVT::i16: Opc = X86::DEC16r; break;
1253 case MVT::i32: Opc = X86::DEC32r; break;
1254 }
1255 }
1256
1257 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001258 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001259 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1260 return Result;
1261 }
1262
1263 switch (N.getValueType()) {
1264 default: assert(0 && "Cannot add this type!");
1265 case MVT::i8: Opc = X86::ADD8ri; break;
1266 case MVT::i16: Opc = X86::ADD16ri; break;
1267 case MVT::i32: Opc = X86::ADD32ri; break;
1268 }
1269 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001270 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001271 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1272 return Result;
1273 }
1274 }
1275
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001276 switch (N.getValueType()) {
1277 default: assert(0 && "Cannot add this type!");
1278 case MVT::i8: Opc = X86::ADD8rr; break;
1279 case MVT::i16: Opc = X86::ADD16rr; break;
1280 case MVT::i32: Opc = X86::ADD32rr; break;
1281 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001282 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001283 }
Chris Lattner11333092005-01-11 03:11:44 +00001284
1285 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1286 Tmp1 = SelectExpr(N.getOperand(0));
1287 Tmp2 = SelectExpr(N.getOperand(1));
1288 } else {
1289 Tmp2 = SelectExpr(N.getOperand(1));
1290 Tmp1 = SelectExpr(N.getOperand(0));
1291 }
1292
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001293 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1294 return Result;
1295 case ISD::SUB:
1296 if (MVT::isInteger(N.getValueType()))
1297 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1298 if (CN->isNullValue()) { // 0 - N -> neg N
1299 switch (N.getValueType()) {
1300 default: assert(0 && "Cannot sub this type!");
1301 case MVT::i1:
1302 case MVT::i8: Opc = X86::NEG8r; break;
1303 case MVT::i16: Opc = X86::NEG16r; break;
1304 case MVT::i32: Opc = X86::NEG32r; break;
1305 }
1306 Tmp1 = SelectExpr(N.getOperand(1));
1307 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1308 return Result;
1309 }
1310
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001311 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1312 switch (N.getValueType()) {
1313 default: assert(0 && "Cannot sub this type!");
1314 case MVT::i1:
1315 case MVT::i8: Opc = X86::SUB8ri; break;
1316 case MVT::i16: Opc = X86::SUB16ri; break;
1317 case MVT::i32: Opc = X86::SUB32ri; break;
1318 }
Chris Lattner11333092005-01-11 03:11:44 +00001319 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001320 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1321 return Result;
1322 }
Chris Lattner11333092005-01-11 03:11:44 +00001323
1324 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1325 Tmp1 = SelectExpr(N.getOperand(0));
1326 Tmp2 = SelectExpr(N.getOperand(1));
1327 } else {
1328 Tmp2 = SelectExpr(N.getOperand(1));
1329 Tmp1 = SelectExpr(N.getOperand(0));
1330 }
1331
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001332 switch (N.getValueType()) {
1333 default: assert(0 && "Cannot add this type!");
1334 case MVT::i1:
1335 case MVT::i8: Opc = X86::SUB8rr; break;
1336 case MVT::i16: Opc = X86::SUB16rr; break;
1337 case MVT::i32: Opc = X86::SUB32rr; break;
1338 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001339 case MVT::f64: Opc = X86::FpSUB; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001340 }
1341 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1342 return Result;
1343
1344 case ISD::AND:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001345 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1346 switch (N.getValueType()) {
1347 default: assert(0 && "Cannot add this type!");
1348 case MVT::i1:
1349 case MVT::i8: Opc = X86::AND8ri; break;
1350 case MVT::i16: Opc = X86::AND16ri; break;
1351 case MVT::i32: Opc = X86::AND32ri; break;
1352 }
Chris Lattner11333092005-01-11 03:11:44 +00001353 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001354 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1355 return Result;
1356 }
Chris Lattner11333092005-01-11 03:11:44 +00001357
1358 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1359 Tmp1 = SelectExpr(N.getOperand(0));
1360 Tmp2 = SelectExpr(N.getOperand(1));
1361 } else {
1362 Tmp2 = SelectExpr(N.getOperand(1));
1363 Tmp1 = SelectExpr(N.getOperand(0));
1364 }
1365
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001366 switch (N.getValueType()) {
1367 default: assert(0 && "Cannot add this type!");
1368 case MVT::i1:
1369 case MVT::i8: Opc = X86::AND8rr; break;
1370 case MVT::i16: Opc = X86::AND16rr; break;
1371 case MVT::i32: Opc = X86::AND32rr; break;
1372 }
1373 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1374 return Result;
1375 case ISD::OR:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001376 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001377 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001378 switch (N.getValueType()) {
1379 default: assert(0 && "Cannot add this type!");
1380 case MVT::i1:
1381 case MVT::i8: Opc = X86::OR8ri; break;
1382 case MVT::i16: Opc = X86::OR16ri; break;
1383 case MVT::i32: Opc = X86::OR32ri; break;
1384 }
1385 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1386 return Result;
1387 }
Chris Lattner11333092005-01-11 03:11:44 +00001388
1389 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1390 Tmp1 = SelectExpr(N.getOperand(0));
1391 Tmp2 = SelectExpr(N.getOperand(1));
1392 } else {
1393 Tmp2 = SelectExpr(N.getOperand(1));
1394 Tmp1 = SelectExpr(N.getOperand(0));
1395 }
1396
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001397 switch (N.getValueType()) {
1398 default: assert(0 && "Cannot add this type!");
1399 case MVT::i1:
1400 case MVT::i8: Opc = X86::OR8rr; break;
1401 case MVT::i16: Opc = X86::OR16rr; break;
1402 case MVT::i32: Opc = X86::OR32rr; break;
1403 }
1404 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1405 return Result;
1406 case ISD::XOR:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001407 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001408 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerd4dab922005-01-11 04:31:30 +00001409
1410 if (CN->isAllOnesValue()) {
1411 switch (N.getValueType()) {
1412 default: assert(0 && "Cannot add this type!");
1413 case MVT::i1:
1414 case MVT::i8: Opc = X86::NOT8r; break;
1415 case MVT::i16: Opc = X86::NOT16r; break;
1416 case MVT::i32: Opc = X86::NOT32r; break;
1417 }
1418 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1419 return Result;
1420 }
1421
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001422 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001423 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001424 case MVT::i1:
1425 case MVT::i8: Opc = X86::XOR8ri; break;
1426 case MVT::i16: Opc = X86::XOR16ri; break;
1427 case MVT::i32: Opc = X86::XOR32ri; break;
1428 }
1429 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1430 return Result;
1431 }
Chris Lattner11333092005-01-11 03:11:44 +00001432
1433 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1434 Tmp1 = SelectExpr(N.getOperand(0));
1435 Tmp2 = SelectExpr(N.getOperand(1));
1436 } else {
1437 Tmp2 = SelectExpr(N.getOperand(1));
1438 Tmp1 = SelectExpr(N.getOperand(0));
1439 }
1440
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001441 switch (N.getValueType()) {
1442 default: assert(0 && "Cannot add this type!");
1443 case MVT::i1:
1444 case MVT::i8: Opc = X86::XOR8rr; break;
1445 case MVT::i16: Opc = X86::XOR16rr; break;
1446 case MVT::i32: Opc = X86::XOR32rr; break;
1447 }
1448 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1449 return Result;
1450
1451 case ISD::MUL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001452 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1453 Opc = 0;
1454 switch (N.getValueType()) {
1455 default: assert(0 && "Cannot multiply this type!");
1456 case MVT::i8: break;
1457 case MVT::i16: Opc = X86::IMUL16rri; break;
1458 case MVT::i32: Opc = X86::IMUL32rri; break;
1459 }
1460 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001461 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001462 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1463 return Result;
1464 }
1465 }
Chris Lattner11333092005-01-11 03:11:44 +00001466
1467 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1468 Tmp1 = SelectExpr(N.getOperand(0));
1469 Tmp2 = SelectExpr(N.getOperand(1));
1470 } else {
1471 Tmp2 = SelectExpr(N.getOperand(1));
1472 Tmp1 = SelectExpr(N.getOperand(0));
1473 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001474 switch (N.getValueType()) {
1475 default: assert(0 && "Cannot add this type!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001476 case MVT::i8:
1477 // Must use the MUL instruction, which forces use of AL.
1478 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1479 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1480 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1481 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001482 case MVT::i16: Opc = X86::IMUL16rr; break;
1483 case MVT::i32: Opc = X86::IMUL32rr; break;
1484 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001485 case MVT::f64: Opc = X86::FpMUL; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001486 }
1487 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1488 return Result;
1489
1490 case ISD::SELECT:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001491 if (N.getValueType() != MVT::i1 && N.getValueType() != MVT::i8) {
Chris Lattner11333092005-01-11 03:11:44 +00001492 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1493 Tmp2 = SelectExpr(N.getOperand(1));
1494 Tmp3 = SelectExpr(N.getOperand(2));
1495 } else {
1496 Tmp3 = SelectExpr(N.getOperand(2));
1497 Tmp2 = SelectExpr(N.getOperand(1));
1498 }
Chris Lattner24aad1b2005-01-10 22:10:13 +00001499 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001500 return Result;
1501 } else {
1502 // FIXME: This should not be implemented here, it should be in the generic
1503 // code!
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001504 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1505 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1506 N.getOperand(1)));
1507 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1508 N.getOperand(2)));
1509 } else {
1510 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1511 N.getOperand(2)));
1512 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1513 N.getOperand(1)));
1514 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001515 unsigned TmpReg = MakeReg(MVT::i16);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001516 EmitSelectCC(N.getOperand(0), MVT::i16, Tmp2, Tmp3, TmpReg);
1517 // FIXME: need subregs to do better than this!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001518 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(TmpReg);
1519 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1520 return Result;
1521 }
1522
1523 case ISD::SDIV:
1524 case ISD::UDIV:
1525 case ISD::SREM:
1526 case ISD::UREM: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001527 if (N.getOpcode() == ISD::SDIV)
1528 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1529 // FIXME: These special cases should be handled by the lowering impl!
1530 unsigned RHS = CN->getValue();
1531 bool isNeg = false;
1532 if ((int)RHS < 0) {
1533 isNeg = true;
1534 RHS = -RHS;
1535 }
1536 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1537 unsigned Log = log2(RHS);
1538 unsigned TmpReg = MakeReg(N.getValueType());
1539 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1540 switch (N.getValueType()) {
1541 default: assert("Unknown type to signed divide!");
1542 case MVT::i8:
1543 SAROpc = X86::SAR8ri;
1544 SHROpc = X86::SHR8ri;
1545 ADDOpc = X86::ADD8rr;
1546 NEGOpc = X86::NEG8r;
1547 break;
1548 case MVT::i16:
1549 SAROpc = X86::SAR16ri;
1550 SHROpc = X86::SHR16ri;
1551 ADDOpc = X86::ADD16rr;
1552 NEGOpc = X86::NEG16r;
1553 break;
1554 case MVT::i32:
1555 SAROpc = X86::SAR32ri;
1556 SHROpc = X86::SHR32ri;
1557 ADDOpc = X86::ADD32rr;
1558 NEGOpc = X86::NEG32r;
1559 break;
1560 }
Chris Lattner11333092005-01-11 03:11:44 +00001561 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001562 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1563 unsigned TmpReg2 = MakeReg(N.getValueType());
1564 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1565 unsigned TmpReg3 = MakeReg(N.getValueType());
1566 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1567
1568 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1569 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1570 if (isNeg)
1571 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1572 return Result;
1573 }
1574 }
1575
Chris Lattner11333092005-01-11 03:11:44 +00001576 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1577 Tmp1 = SelectExpr(N.getOperand(0));
1578 Tmp2 = SelectExpr(N.getOperand(1));
1579 } else {
1580 Tmp2 = SelectExpr(N.getOperand(1));
1581 Tmp1 = SelectExpr(N.getOperand(0));
1582 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001583
1584 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1585 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1586 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1587 switch (N.getValueType()) {
1588 default: assert(0 && "Cannot sdiv this type!");
1589 case MVT::i8:
1590 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1591 LoReg = X86::AL;
1592 HiReg = X86::AH;
1593 MovOpcode = X86::MOV8rr;
1594 ClrOpcode = X86::MOV8ri;
1595 SExtOpcode = X86::CBW;
1596 break;
1597 case MVT::i16:
1598 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1599 LoReg = X86::AX;
1600 HiReg = X86::DX;
1601 MovOpcode = X86::MOV16rr;
1602 ClrOpcode = X86::MOV16ri;
1603 SExtOpcode = X86::CWD;
1604 break;
1605 case MVT::i32:
1606 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
1607 LoReg =X86::EAX;
1608 HiReg = X86::EDX;
1609 MovOpcode = X86::MOV32rr;
1610 ClrOpcode = X86::MOV32ri;
1611 SExtOpcode = X86::CDQ;
1612 break;
1613 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1614 case MVT::f32:
1615 case MVT::f64:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001616 if (N.getOpcode() == ISD::SDIV)
1617 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1618 else
1619 assert(0 && "FIXME: Emit frem libcall to fmod!");
1620 return Result;
1621 }
1622
1623 // Set up the low part.
1624 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1625
1626 if (isSigned) {
1627 // Sign extend the low part into the high part.
1628 BuildMI(BB, SExtOpcode, 0);
1629 } else {
1630 // Zero out the high part, effectively zero extending the input.
1631 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1632 }
1633
1634 // Emit the DIV/IDIV instruction.
1635 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1636
1637 // Get the result of the divide or rem.
1638 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1639 return Result;
1640 }
1641
1642 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001643 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1644 switch (N.getValueType()) {
1645 default: assert(0 && "Cannot shift this type!");
1646 case MVT::i8: Opc = X86::SHL8ri; break;
1647 case MVT::i16: Opc = X86::SHL16ri; break;
1648 case MVT::i32: Opc = X86::SHL32ri; break;
1649 }
Chris Lattner11333092005-01-11 03:11:44 +00001650 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001651 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1652 return Result;
1653 }
Chris Lattner11333092005-01-11 03:11:44 +00001654
1655 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1656 Tmp1 = SelectExpr(N.getOperand(0));
1657 Tmp2 = SelectExpr(N.getOperand(1));
1658 } else {
1659 Tmp2 = SelectExpr(N.getOperand(1));
1660 Tmp1 = SelectExpr(N.getOperand(0));
1661 }
1662
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001663 switch (N.getValueType()) {
1664 default: assert(0 && "Cannot shift this type!");
1665 case MVT::i8 : Opc = X86::SHL8rCL; break;
1666 case MVT::i16: Opc = X86::SHL16rCL; break;
1667 case MVT::i32: Opc = X86::SHL32rCL; break;
1668 }
1669 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1670 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1671 return Result;
1672 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001673 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1674 switch (N.getValueType()) {
1675 default: assert(0 && "Cannot shift this type!");
1676 case MVT::i8: Opc = X86::SHR8ri; break;
1677 case MVT::i16: Opc = X86::SHR16ri; break;
1678 case MVT::i32: Opc = X86::SHR32ri; break;
1679 }
Chris Lattner11333092005-01-11 03:11:44 +00001680 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001681 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1682 return Result;
1683 }
Chris Lattner11333092005-01-11 03:11:44 +00001684
1685 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1686 Tmp1 = SelectExpr(N.getOperand(0));
1687 Tmp2 = SelectExpr(N.getOperand(1));
1688 } else {
1689 Tmp2 = SelectExpr(N.getOperand(1));
1690 Tmp1 = SelectExpr(N.getOperand(0));
1691 }
1692
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001693 switch (N.getValueType()) {
1694 default: assert(0 && "Cannot shift this type!");
1695 case MVT::i8 : Opc = X86::SHR8rCL; break;
1696 case MVT::i16: Opc = X86::SHR16rCL; break;
1697 case MVT::i32: Opc = X86::SHR32rCL; break;
1698 }
1699 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1700 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1701 return Result;
1702 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001703 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1704 switch (N.getValueType()) {
1705 default: assert(0 && "Cannot shift this type!");
1706 case MVT::i8: Opc = X86::SAR8ri; break;
1707 case MVT::i16: Opc = X86::SAR16ri; break;
1708 case MVT::i32: Opc = X86::SAR32ri; break;
1709 }
Chris Lattner11333092005-01-11 03:11:44 +00001710 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001711 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1712 return Result;
1713 }
Chris Lattner11333092005-01-11 03:11:44 +00001714
1715 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1716 Tmp1 = SelectExpr(N.getOperand(0));
1717 Tmp2 = SelectExpr(N.getOperand(1));
1718 } else {
1719 Tmp2 = SelectExpr(N.getOperand(1));
1720 Tmp1 = SelectExpr(N.getOperand(0));
1721 }
1722
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001723 switch (N.getValueType()) {
1724 default: assert(0 && "Cannot shift this type!");
1725 case MVT::i8 : Opc = X86::SAR8rCL; break;
1726 case MVT::i16: Opc = X86::SAR16rCL; break;
1727 case MVT::i32: Opc = X86::SAR32rCL; break;
1728 }
1729 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1730 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1731 return Result;
1732
1733 case ISD::SETCC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001734 EmitCMP(N.getOperand(0), N.getOperand(1));
1735 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
1736 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
1737 return Result;
1738 case ISD::LOAD: {
Chris Lattner5188ad72005-01-08 19:28:19 +00001739 // The chain for this load is now lowered.
1740 LoweredTokens.insert(SDOperand(Node, 1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001741
1742 // Make sure we generate both values.
1743 if (Result != 1)
1744 ExprMap[N.getValue(1)] = 1; // Generate the token
1745 else
1746 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1747
Chris Lattner5188ad72005-01-08 19:28:19 +00001748 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001749 default: assert(0 && "Cannot load this type!");
1750 case MVT::i1:
1751 case MVT::i8: Opc = X86::MOV8rm; break;
1752 case MVT::i16: Opc = X86::MOV16rm; break;
1753 case MVT::i32: Opc = X86::MOV32rm; break;
1754 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
1755 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
1756 }
Chris Lattner11333092005-01-11 03:11:44 +00001757
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001758 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00001759 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001760 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
1761 } else {
1762 X86AddressMode AM;
Chris Lattner11333092005-01-11 03:11:44 +00001763 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1764 Select(N.getOperand(0));
1765 SelectAddress(N.getOperand(1), AM);
1766 } else {
1767 SelectAddress(N.getOperand(1), AM);
1768 Select(N.getOperand(0));
1769 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001770 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1771 }
1772 return Result;
1773 }
1774 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001775 // Generate both result values.
1776 if (Result != 1)
1777 ExprMap[N.getValue(1)] = 1; // Generate the token
1778 else
1779 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1780
1781 // FIXME: We are currently ignoring the requested alignment for handling
1782 // greater than the stack alignment. This will need to be revisited at some
1783 // point. Align = N.getOperand(2);
1784
1785 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1786 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1787 std::cerr << "Cannot allocate stack object with greater alignment than"
1788 << " the stack alignment yet!";
1789 abort();
1790 }
1791
1792 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001793 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001794 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
1795 .addImm(CN->getValue());
1796 } else {
Chris Lattner11333092005-01-11 03:11:44 +00001797 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1798 Select(N.getOperand(0));
1799 Tmp1 = SelectExpr(N.getOperand(1));
1800 } else {
1801 Tmp1 = SelectExpr(N.getOperand(1));
1802 Select(N.getOperand(0));
1803 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001804
1805 // Subtract size from stack pointer, thereby allocating some space.
1806 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
1807 }
1808
1809 // Put a pointer to the space into the result register, by copying the stack
1810 // pointer.
1811 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
1812 return Result;
1813
1814 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00001815 // The chain for this call is now lowered.
1816 LoweredTokens.insert(N.getValue(Node->getNumValues()-1));
1817
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001818 if (GlobalAddressSDNode *GASD =
1819 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001820 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001821 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
1822 } else if (ExternalSymbolSDNode *ESSDN =
1823 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001824 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001825 BuildMI(BB, X86::CALLpcrel32,
1826 1).addExternalSymbol(ESSDN->getSymbol(), true);
1827 } else {
Chris Lattner11333092005-01-11 03:11:44 +00001828 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1829 Select(N.getOperand(0));
1830 Tmp1 = SelectExpr(N.getOperand(1));
1831 } else {
1832 Tmp1 = SelectExpr(N.getOperand(1));
1833 Select(N.getOperand(0));
1834 }
1835
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001836 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
1837 }
Chris Lattner5188ad72005-01-08 19:28:19 +00001838 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001839 default: assert(0 && "Unknown value type for call result!");
1840 case MVT::Other: return 1;
1841 case MVT::i1:
1842 case MVT::i8:
1843 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1844 break;
1845 case MVT::i16:
1846 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
1847 break;
1848 case MVT::i32:
1849 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00001850 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001851 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
1852 break;
1853 case MVT::f32:
1854 case MVT::f64: // Floating-point return values live in %ST(0)
1855 ContainsFPCode = true;
1856 BuildMI(BB, X86::FpGETRESULT, 1, Result);
1857 break;
1858 }
1859 return Result+N.ResNo;
1860 }
1861
1862 return 0;
1863}
1864
1865void ISel::Select(SDOperand N) {
1866 unsigned Tmp1, Tmp2, Opc;
1867
1868 // FIXME: Disable for our current expansion model!
1869 if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second)
1870 return; // Already selected.
1871
Chris Lattner989de032005-01-11 06:14:36 +00001872 SDNode *Node = N.Val;
1873
1874 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001875 default:
Chris Lattner989de032005-01-11 06:14:36 +00001876 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001877 assert(0 && "Node not handled yet!");
1878 case ISD::EntryToken: return; // Noop
1879 case ISD::CopyToReg:
1880 Select(N.getOperand(0));
1881 Tmp1 = SelectExpr(N.getOperand(1));
1882 Tmp2 = cast<CopyRegSDNode>(N)->getReg();
1883
1884 if (Tmp1 != Tmp2) {
1885 switch (N.getOperand(1).getValueType()) {
1886 default: assert(0 && "Invalid type for operation!");
1887 case MVT::i1:
1888 case MVT::i8: Opc = X86::MOV8rr; break;
1889 case MVT::i16: Opc = X86::MOV16rr; break;
1890 case MVT::i32: Opc = X86::MOV32rr; break;
1891 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001892 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001893 }
1894 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1895 }
1896 return;
1897 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001898 switch (N.getNumOperands()) {
1899 default:
1900 assert(0 && "Unknown return instruction!");
1901 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001902 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1903 N.getOperand(2).getValueType() == MVT::i32 &&
1904 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00001905 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1906 Tmp1 = SelectExpr(N.getOperand(1));
1907 Tmp2 = SelectExpr(N.getOperand(2));
1908 } else {
1909 Tmp2 = SelectExpr(N.getOperand(2));
1910 Tmp1 = SelectExpr(N.getOperand(1));
1911 }
1912 Select(N.getOperand(0));
1913
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001914 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
1915 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
1916 // Declare that EAX & EDX are live on exit.
1917 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
1918 .addReg(X86::ESP);
1919 break;
1920 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00001921 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1922 Select(N.getOperand(0));
1923 Tmp1 = SelectExpr(N.getOperand(1));
1924 } else {
1925 Tmp1 = SelectExpr(N.getOperand(1));
1926 Select(N.getOperand(0));
1927 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001928 switch (N.getOperand(1).getValueType()) {
1929 default: assert(0 && "All other types should have been promoted!!");
1930 case MVT::f64:
1931 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
1932 // Declare that top-of-stack is live on exit
1933 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
1934 break;
1935 case MVT::i32:
1936 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
1937 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
1938 break;
1939 }
1940 break;
1941 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00001942 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001943 break;
1944 }
1945 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
1946 return;
1947 case ISD::BR: {
1948 Select(N.getOperand(0));
1949 MachineBasicBlock *Dest =
1950 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1951 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
1952 return;
1953 }
1954
1955 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001956 MachineBasicBlock *Dest =
1957 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00001958
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001959 // Try to fold a setcc into the branch. If this fails, emit a test/jne
1960 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00001961 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
1962 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1963 Select(N.getOperand(0));
1964 Tmp1 = SelectExpr(N.getOperand(1));
1965 } else {
1966 Tmp1 = SelectExpr(N.getOperand(1));
1967 Select(N.getOperand(0));
1968 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001969 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
1970 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
1971 }
Chris Lattner11333092005-01-11 03:11:44 +00001972
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001973 return;
1974 }
1975 case ISD::LOAD:
1976 case ISD::CALL:
1977 case ISD::DYNAMIC_STACKALLOC:
1978 SelectExpr(N);
1979 return;
1980 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001981 // Select the address.
1982 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001983
1984 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1985 Opc = 0;
1986 switch (CN->getValueType(0)) {
1987 default: assert(0 && "Invalid type for operation!");
1988 case MVT::i1:
1989 case MVT::i8: Opc = X86::MOV8mi; break;
1990 case MVT::i16: Opc = X86::MOV16mi; break;
1991 case MVT::i32: Opc = X86::MOV32mi; break;
1992 case MVT::f32:
1993 case MVT::f64: break;
1994 }
1995 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001996 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
1997 Select(N.getOperand(0));
1998 SelectAddress(N.getOperand(2), AM);
1999 } else {
2000 SelectAddress(N.getOperand(2), AM);
2001 Select(N.getOperand(0));
2002 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002003 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2004 return;
2005 }
2006 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002007 switch (N.getOperand(1).getValueType()) {
2008 default: assert(0 && "Cannot store this type!");
2009 case MVT::i1:
2010 case MVT::i8: Opc = X86::MOV8mr; break;
2011 case MVT::i16: Opc = X86::MOV16mr; break;
2012 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002013 case MVT::f32: Opc = X86::FST32m; break;
2014 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002015 }
Chris Lattner11333092005-01-11 03:11:44 +00002016
2017 std::vector<std::pair<unsigned, unsigned> > RP;
2018 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2019 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2020 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2021 std::sort(RP.begin(), RP.end());
2022
2023 for (unsigned i = 0; i != 3; ++i)
2024 switch (RP[2-i].second) {
2025 default: assert(0 && "Unknown operand number!");
2026 case 0: Select(N.getOperand(0)); break;
2027 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002028 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002029 }
2030
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002031 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2032 return;
2033 }
2034 case ISD::ADJCALLSTACKDOWN:
2035 case ISD::ADJCALLSTACKUP:
2036 Select(N.getOperand(0));
2037 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2038
2039 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2040 X86::ADJCALLSTACKUP;
2041 BuildMI(BB, Opc, 1).addImm(Tmp1);
2042 return;
Chris Lattner989de032005-01-11 06:14:36 +00002043 case ISD::MEMSET: {
2044 Select(N.getOperand(0)); // Select the chain.
2045 unsigned Align =
2046 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2047 if (Align == 0) Align = 1;
2048
2049 // Turn the byte code into # iterations
2050 unsigned CountReg;
2051 unsigned Opcode;
2052 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2053 unsigned Val = ValC->getValue() & 255;
2054
2055 // If the value is a constant, then we can potentially use larger sets.
2056 switch (Align & 3) {
2057 case 2: // WORD aligned
2058 CountReg = MakeReg(MVT::i32);
2059 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2060 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2061 } else {
2062 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2063 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2064 }
2065 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2066 Opcode = X86::REP_STOSW;
2067 break;
2068 case 0: // DWORD aligned
2069 CountReg = MakeReg(MVT::i32);
2070 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2071 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2072 } else {
2073 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2074 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2075 }
2076 Val = (Val << 8) | Val;
2077 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
2078 Opcode = X86::REP_STOSD;
2079 break;
2080 default: // BYTE aligned
2081 CountReg = SelectExpr(Node->getOperand(3));
2082 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
2083 Opcode = X86::REP_STOSB;
2084 break;
2085 }
2086 } else {
2087 // If it's not a constant value we are storing, just fall back. We could
2088 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
2089 unsigned ValReg = SelectExpr(Node->getOperand(2));
2090 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
2091 CountReg = SelectExpr(Node->getOperand(3));
2092 Opcode = X86::REP_STOSB;
2093 }
2094
2095 // No matter what the alignment is, we put the source in ESI, the
2096 // destination in EDI, and the count in ECX.
2097 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2098 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2099 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2100 BuildMI(BB, Opcode, 0);
2101 return;
2102 }
Chris Lattner31805bf2005-01-11 06:19:26 +00002103 case ISD::MEMCPY:
2104 Select(N.getOperand(0)); // Select the chain.
2105 unsigned Align =
2106 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2107 if (Align == 0) Align = 1;
2108
2109 // Turn the byte code into # iterations
2110 unsigned CountReg;
2111 unsigned Opcode;
2112 switch (Align & 3) {
2113 case 2: // WORD aligned
2114 CountReg = MakeReg(MVT::i32);
2115 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2116 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2117 } else {
2118 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2119 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2120 }
2121 Opcode = X86::REP_MOVSW;
2122 break;
2123 case 0: // DWORD aligned
2124 CountReg = MakeReg(MVT::i32);
2125 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2126 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2127 } else {
2128 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2129 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2130 }
2131 Opcode = X86::REP_MOVSD;
2132 break;
2133 default: // BYTE aligned
2134 CountReg = SelectExpr(Node->getOperand(3));
2135 Opcode = X86::REP_MOVSB;
2136 break;
2137 }
2138
2139 // No matter what the alignment is, we put the source in ESI, the
2140 // destination in EDI, and the count in ECX.
2141 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2142 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
2143 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2144 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2145 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
2146 BuildMI(BB, Opcode, 0);
2147 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002148 }
2149 assert(0 && "Should not be reached!");
2150}
2151
2152
2153/// createX86PatternInstructionSelector - This pass converts an LLVM function
2154/// into a machine code representation using pattern matching and a machine
2155/// description file.
2156///
2157FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
2158 return new ISel(TM);
2159}