blob: 34d13564707ff4aed743e09c0a79767f5a0bf70d [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
Chris Lattnera5ade062005-01-11 21:19:59 +0000366 bool isFoldableLoad(SDOperand Op);
367 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
368
369
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000370 void EmitCMP(SDOperand LHS, SDOperand RHS);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000371 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000372 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
373 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000374 unsigned SelectExpr(SDOperand N);
375 bool SelectAddress(SDOperand N, X86AddressMode &AM);
376 void Select(SDOperand N);
377 };
378}
379
Chris Lattner11333092005-01-11 03:11:44 +0000380// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
381// for the number of registers required to compute each node. This is basically
382// computing a generalized form of the Sethi-Ullman number for each node.
383unsigned ISel::ComputeRegPressure(SDOperand O) {
384 SDNode *N = O.Val;
385 unsigned &Result = RegPressureMap[N];
386 if (Result) return Result;
387
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000388 // FIXME: Should operations like CALL (which clobber lots o regs) have a
389 // higher fixed cost??
390
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000391 if (N->getNumOperands() == 0) {
392 Result = 1;
393 } else {
394 unsigned MaxRegUse = 0;
395 unsigned NumExtraMaxRegUsers = 0;
396 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
397 unsigned Regs;
398 if (N->getOperand(i).getOpcode() == ISD::Constant)
399 Regs = 0;
400 else
401 Regs = ComputeRegPressure(N->getOperand(i));
402 if (Regs > MaxRegUse) {
403 MaxRegUse = Regs;
404 NumExtraMaxRegUsers = 0;
405 } else if (Regs == MaxRegUse &&
406 N->getOperand(i).getValueType() != MVT::Other) {
407 ++NumExtraMaxRegUsers;
408 }
Chris Lattner11333092005-01-11 03:11:44 +0000409 }
Chris Lattner11333092005-01-11 03:11:44 +0000410
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000411 Result = MaxRegUse+NumExtraMaxRegUsers;
412 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000413
Chris Lattner837caa72005-01-11 23:21:30 +0000414 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000415 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000416}
417
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000418/// SelectAddress - Add the specified node to the specified addressing mode,
419/// returning true if it cannot be done.
420bool ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
421 switch (N.getOpcode()) {
422 default: break;
423 case ISD::FrameIndex:
424 if (AM.BaseType == X86AddressMode::RegBase && AM.Base.Reg == 0) {
425 AM.BaseType = X86AddressMode::FrameIndexBase;
426 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
427 return false;
428 }
429 break;
430 case ISD::GlobalAddress:
431 if (AM.GV == 0) {
432 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
433 return false;
434 }
435 break;
436 case ISD::Constant:
437 AM.Disp += cast<ConstantSDNode>(N)->getValue();
438 return false;
439 case ISD::SHL:
440 if (AM.IndexReg == 0 || AM.Scale == 1)
441 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
442 unsigned Val = CN->getValue();
443 if (Val == 1 || Val == 2 || Val == 3) {
444 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000445 SDOperand ShVal = N.Val->getOperand(0);
446
447 // Okay, we know that we have a scale by now. However, if the scaled
448 // value is an add of something and a constant, we can fold the
449 // constant into the disp field here.
450 if (ShVal.Val->getOpcode() == ISD::ADD &&
451 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
452 AM.IndexReg = SelectExpr(ShVal.Val->getOperand(0));
453 ConstantSDNode *AddVal =
454 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
455 AM.Disp += AddVal->getValue() << Val;
456 } else {
457 AM.IndexReg = SelectExpr(ShVal);
458 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000459 return false;
460 }
461 }
462 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000463 case ISD::MUL:
464 // X*[3,5,9] -> X+X*[2,4,8]
465 if (AM.IndexReg == 0 && AM.BaseType == X86AddressMode::RegBase &&
466 AM.Base.Reg == 0)
467 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
468 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
469 AM.Scale = unsigned(CN->getValue())-1;
470
471 SDOperand MulVal = N.Val->getOperand(0);
472 unsigned Reg;
473
474 // Okay, we know that we have a scale by now. However, if the scaled
475 // value is an add of something and a constant, we can fold the
476 // constant into the disp field here.
477 if (MulVal.Val->getOpcode() == ISD::ADD &&
478 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
479 Reg = SelectExpr(MulVal.Val->getOperand(0));
480 ConstantSDNode *AddVal =
481 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
482 AM.Disp += AddVal->getValue() * CN->getValue();
483 } else {
484 Reg = SelectExpr(N.Val->getOperand(0));
485 }
486
487 AM.IndexReg = AM.Base.Reg = Reg;
488 return false;
489 }
490 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000491
492 case ISD::ADD: {
493 X86AddressMode Backup = AM;
494 if (!SelectAddress(N.Val->getOperand(0), AM) &&
495 !SelectAddress(N.Val->getOperand(1), AM))
496 return false;
497 AM = Backup;
498 break;
499 }
500 }
501
Chris Lattnera95589b2005-01-11 04:40:19 +0000502 // Is the base register already occupied?
503 if (AM.BaseType != X86AddressMode::RegBase || AM.Base.Reg) {
504 // If so, check to see if the scale index register is set.
505 if (AM.IndexReg == 0) {
506 AM.IndexReg = SelectExpr(N);
507 AM.Scale = 1;
508 return false;
509 }
510
511 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000512 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000513 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000514
515 // Default, generate it as a register.
516 AM.BaseType = X86AddressMode::RegBase;
517 AM.Base.Reg = SelectExpr(N);
518 return false;
519}
520
521/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
522/// assuming that the temporary registers are in the 8-bit register class.
523///
524/// Tmp1 = setcc1
525/// Tmp2 = setcc2
526/// DestReg = logicalop Tmp1, Tmp2
527///
528static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
529 unsigned SetCC2, unsigned LogicalOp,
530 unsigned DestReg) {
531 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
532 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
533 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
534 BuildMI(BB, SetCC1, 0, Tmp1);
535 BuildMI(BB, SetCC2, 0, Tmp2);
536 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
537}
538
539/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
540/// condition codes match the specified SetCCOpcode. Note that some conditions
541/// require multiple instructions to generate the correct value.
542static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
543 ISD::CondCode SetCCOpcode, bool isFP) {
544 unsigned Opc;
545 if (!isFP) {
546 switch (SetCCOpcode) {
547 default: assert(0 && "Illegal integer SetCC!");
548 case ISD::SETEQ: Opc = X86::SETEr; break;
549 case ISD::SETGT: Opc = X86::SETGr; break;
550 case ISD::SETGE: Opc = X86::SETGEr; break;
551 case ISD::SETLT: Opc = X86::SETLr; break;
552 case ISD::SETLE: Opc = X86::SETLEr; break;
553 case ISD::SETNE: Opc = X86::SETNEr; break;
554 case ISD::SETULT: Opc = X86::SETBr; break;
555 case ISD::SETUGT: Opc = X86::SETAr; break;
556 case ISD::SETULE: Opc = X86::SETBEr; break;
557 case ISD::SETUGE: Opc = X86::SETAEr; break;
558 }
559 } else {
560 // On a floating point condition, the flags are set as follows:
561 // ZF PF CF op
562 // 0 | 0 | 0 | X > Y
563 // 0 | 0 | 1 | X < Y
564 // 1 | 0 | 0 | X == Y
565 // 1 | 1 | 1 | unordered
566 //
567 switch (SetCCOpcode) {
568 default: assert(0 && "Invalid FP setcc!");
569 case ISD::SETUEQ:
570 case ISD::SETEQ:
571 Opc = X86::SETEr; // True if ZF = 1
572 break;
573 case ISD::SETOGT:
574 case ISD::SETGT:
575 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
576 break;
577 case ISD::SETOGE:
578 case ISD::SETGE:
579 Opc = X86::SETAEr; // True if CF = 0
580 break;
581 case ISD::SETULT:
582 case ISD::SETLT:
583 Opc = X86::SETBr; // True if CF = 1
584 break;
585 case ISD::SETULE:
586 case ISD::SETLE:
587 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
588 break;
589 case ISD::SETONE:
590 case ISD::SETNE:
591 Opc = X86::SETNEr; // True if ZF = 0
592 break;
593 case ISD::SETUO:
594 Opc = X86::SETPr; // True if PF = 1
595 break;
596 case ISD::SETO:
597 Opc = X86::SETNPr; // True if PF = 0
598 break;
599 case ISD::SETOEQ: // !PF & ZF
600 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
601 return;
602 case ISD::SETOLT: // !PF & CF
603 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
604 return;
605 case ISD::SETOLE: // !PF & (CF || ZF)
606 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
607 return;
608 case ISD::SETUGT: // PF | (!ZF & !CF)
609 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
610 return;
611 case ISD::SETUGE: // PF | !CF
612 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
613 return;
614 case ISD::SETUNE: // PF | !ZF
615 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
616 return;
617 }
618 }
619 BuildMI(BB, Opc, 0, DestReg);
620}
621
622
623/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
624/// the Dest block if the Cond condition is true. If we cannot fold this
625/// condition into the branch, return true.
626///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000627bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
628 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000629 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
630 // B) using two conditional branches instead of one condbr, two setcc's, and
631 // an or.
632 if ((Cond.getOpcode() == ISD::OR ||
633 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
634 // And and or set the flags for us, so there is no need to emit a TST of the
635 // result. It is only safe to do this if there is only a single use of the
636 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000637 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000638 SelectExpr(Cond);
639 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
640 return false;
641 }
642
643 // Codegen br not C -> JE.
644 if (Cond.getOpcode() == ISD::XOR)
645 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
646 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000647 unsigned CondR;
648 if (getRegPressure(Chain) > getRegPressure(Cond)) {
649 Select(Chain);
650 CondR = SelectExpr(Cond.Val->getOperand(0));
651 } else {
652 CondR = SelectExpr(Cond.Val->getOperand(0));
653 Select(Chain);
654 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000655 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
656 BuildMI(BB, X86::JE, 1).addMBB(Dest);
657 return false;
658 }
659
660 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
661 if (SetCC == 0)
662 return true; // Can only handle simple setcc's so far.
663
664 unsigned Opc;
665
666 // Handle integer conditions first.
667 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
668 switch (SetCC->getCondition()) {
669 default: assert(0 && "Illegal integer SetCC!");
670 case ISD::SETEQ: Opc = X86::JE; break;
671 case ISD::SETGT: Opc = X86::JG; break;
672 case ISD::SETGE: Opc = X86::JGE; break;
673 case ISD::SETLT: Opc = X86::JL; break;
674 case ISD::SETLE: Opc = X86::JLE; break;
675 case ISD::SETNE: Opc = X86::JNE; break;
676 case ISD::SETULT: Opc = X86::JB; break;
677 case ISD::SETUGT: Opc = X86::JA; break;
678 case ISD::SETULE: Opc = X86::JBE; break;
679 case ISD::SETUGE: Opc = X86::JAE; break;
680 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000681 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000682 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
683 BuildMI(BB, Opc, 1).addMBB(Dest);
684 return false;
685 }
686
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000687 unsigned Opc2 = 0; // Second branch if needed.
688
689 // On a floating point condition, the flags are set as follows:
690 // ZF PF CF op
691 // 0 | 0 | 0 | X > Y
692 // 0 | 0 | 1 | X < Y
693 // 1 | 0 | 0 | X == Y
694 // 1 | 1 | 1 | unordered
695 //
696 switch (SetCC->getCondition()) {
697 default: assert(0 && "Invalid FP setcc!");
698 case ISD::SETUEQ:
699 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
700 case ISD::SETOGT:
701 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
702 case ISD::SETOGE:
703 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
704 case ISD::SETULT:
705 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
706 case ISD::SETULE:
707 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
708 case ISD::SETONE:
709 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
710 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
711 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
712 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
713 Opc = X86::JA; // ZF = 0 & CF = 0
714 Opc2 = X86::JP; // PF = 1
715 break;
716 case ISD::SETUGE: // PF = 1 | CF = 0
717 Opc = X86::JAE; // CF = 0
718 Opc2 = X86::JP; // PF = 1
719 break;
720 case ISD::SETUNE: // PF = 1 | ZF = 0
721 Opc = X86::JNE; // ZF = 0
722 Opc2 = X86::JP; // PF = 1
723 break;
724 case ISD::SETOEQ: // PF = 0 & ZF = 1
725 //X86::JNP, X86::JE
726 //X86::AND8rr
727 return true; // FIXME: Emit more efficient code for this branch.
728 case ISD::SETOLT: // PF = 0 & CF = 1
729 //X86::JNP, X86::JB
730 //X86::AND8rr
731 return true; // FIXME: Emit more efficient code for this branch.
732 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
733 //X86::JNP, X86::JBE
734 //X86::AND8rr
735 return true; // FIXME: Emit more efficient code for this branch.
736 }
737
Chris Lattner6c07aee2005-01-11 04:06:27 +0000738 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000739 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
740 BuildMI(BB, Opc, 1).addMBB(Dest);
741 if (Opc2)
742 BuildMI(BB, Opc2, 1).addMBB(Dest);
743 return false;
744}
745
Chris Lattner24aad1b2005-01-10 22:10:13 +0000746/// EmitSelectCC - Emit code into BB that performs a select operation between
747/// the two registers RTrue and RFalse, generating a result into RDest. Return
748/// true if the fold cannot be performed.
749///
750void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
751 unsigned RTrue, unsigned RFalse, unsigned RDest) {
752 enum Condition {
753 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
754 NOT_SET
755 } CondCode = NOT_SET;
756
757 static const unsigned CMOVTAB16[] = {
758 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
759 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
760 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
761 };
762 static const unsigned CMOVTAB32[] = {
763 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
764 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
765 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
766 };
767 static const unsigned CMOVTABFP[] = {
768 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
769 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
770 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
771 };
772
773 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
774 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
775 switch (SetCC->getCondition()) {
776 default: assert(0 && "Unknown integer comparison!");
777 case ISD::SETEQ: CondCode = EQ; break;
778 case ISD::SETGT: CondCode = GT; break;
779 case ISD::SETGE: CondCode = GE; break;
780 case ISD::SETLT: CondCode = LT; break;
781 case ISD::SETLE: CondCode = LE; break;
782 case ISD::SETNE: CondCode = NE; break;
783 case ISD::SETULT: CondCode = B; break;
784 case ISD::SETUGT: CondCode = A; break;
785 case ISD::SETULE: CondCode = BE; break;
786 case ISD::SETUGE: CondCode = AE; break;
787 }
788 } else {
789 // On a floating point condition, the flags are set as follows:
790 // ZF PF CF op
791 // 0 | 0 | 0 | X > Y
792 // 0 | 0 | 1 | X < Y
793 // 1 | 0 | 0 | X == Y
794 // 1 | 1 | 1 | unordered
795 //
796 switch (SetCC->getCondition()) {
797 default: assert(0 && "Unknown FP comparison!");
798 case ISD::SETUEQ:
799 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
800 case ISD::SETOGT:
801 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
802 case ISD::SETOGE:
803 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
804 case ISD::SETULT:
805 case ISD::SETLT: CondCode = B; break; // True if CF = 1
806 case ISD::SETULE:
807 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
808 case ISD::SETONE:
809 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
810 case ISD::SETUO: CondCode = P; break; // True if PF = 1
811 case ISD::SETO: CondCode = NP; break; // True if PF = 0
812 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
813 case ISD::SETUGE: // PF = 1 | CF = 0
814 case ISD::SETUNE: // PF = 1 | ZF = 0
815 case ISD::SETOEQ: // PF = 0 & ZF = 1
816 case ISD::SETOLT: // PF = 0 & CF = 1
817 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
818 // We cannot emit this comparison as a single cmov.
819 break;
820 }
821 }
822 }
823
824 unsigned Opc = 0;
825 if (CondCode != NOT_SET) {
826 switch (SVT) {
827 default: assert(0 && "Cannot select this type!");
828 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
829 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
830 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000831 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000832 }
833 }
834
835 // Finally, if we weren't able to fold this, just emit the condition and test
836 // it.
837 if (CondCode == NOT_SET || Opc == 0) {
838 // Get the condition into the zero flag.
839 unsigned CondReg = SelectExpr(Cond);
840 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
841
842 switch (SVT) {
843 default: assert(0 && "Cannot select this type!");
844 case MVT::i16: Opc = X86::CMOVE16rr; break;
845 case MVT::i32: Opc = X86::CMOVE32rr; break;
846 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000847 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000848 }
849 } else {
850 // FIXME: CMP R, 0 -> TEST R, R
851 EmitCMP(Cond.getOperand(0), Cond.getOperand(1));
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000852 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000853 }
854 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
855}
856
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000857void ISel::EmitCMP(SDOperand LHS, SDOperand RHS) {
Chris Lattner11333092005-01-11 03:11:44 +0000858 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000859 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
860 Opc = 0;
Chris Lattneref6806c2005-01-12 02:02:48 +0000861 if (isFoldableLoad(LHS)) {
862 switch (RHS.getValueType()) {
863 default: break;
864 case MVT::i1:
865 case MVT::i8: Opc = X86::CMP8mi; break;
866 case MVT::i16: Opc = X86::CMP16mi; break;
867 case MVT::i32: Opc = X86::CMP32mi; break;
868 }
869 if (Opc) {
870 X86AddressMode AM;
871 EmitFoldedLoad(LHS, AM);
872 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
873 return;
874 }
875 }
876
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000877 switch (RHS.getValueType()) {
878 default: break;
879 case MVT::i1:
880 case MVT::i8: Opc = X86::CMP8ri; break;
881 case MVT::i16: Opc = X86::CMP16ri; break;
882 case MVT::i32: Opc = X86::CMP32ri; break;
883 }
884 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +0000885 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000886 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
887 return;
888 }
889 }
890
Chris Lattneref6806c2005-01-12 02:02:48 +0000891 Opc = 0;
892 if (isFoldableLoad(LHS)) {
893 switch (RHS.getValueType()) {
894 default: break;
895 case MVT::i1:
896 case MVT::i8: Opc = X86::CMP8mr; break;
897 case MVT::i16: Opc = X86::CMP16mr; break;
898 case MVT::i32: Opc = X86::CMP32mr; break;
899 }
900 if (Opc) {
901 X86AddressMode AM;
902 unsigned Reg;
903 if (getRegPressure(LHS) > getRegPressure(RHS)) {
904 EmitFoldedLoad(LHS, AM);
905 Reg = SelectExpr(RHS);
906 } else {
907 Reg = SelectExpr(RHS);
908 EmitFoldedLoad(LHS, AM);
909 }
910 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
911 return;
912 }
913 }
914
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000915 switch (LHS.getValueType()) {
916 default: assert(0 && "Cannot compare this value!");
917 case MVT::i1:
918 case MVT::i8: Opc = X86::CMP8rr; break;
919 case MVT::i16: Opc = X86::CMP16rr; break;
920 case MVT::i32: Opc = X86::CMP32rr; break;
921 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000922 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000923 }
Chris Lattner11333092005-01-11 03:11:44 +0000924 unsigned Tmp1, Tmp2;
925 if (getRegPressure(LHS) > getRegPressure(RHS)) {
926 Tmp1 = SelectExpr(LHS);
927 Tmp2 = SelectExpr(RHS);
928 } else {
929 Tmp2 = SelectExpr(RHS);
930 Tmp1 = SelectExpr(LHS);
931 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000932 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
933}
934
Chris Lattnera5ade062005-01-11 21:19:59 +0000935/// isFoldableLoad - Return true if this is a load instruction that can safely
936/// be folded into an operation that uses it.
937bool ISel::isFoldableLoad(SDOperand Op) {
938 if (Op.getOpcode() != ISD::LOAD ||
939 // FIXME: currently can't fold constant pool indexes.
940 isa<ConstantPoolSDNode>(Op.getOperand(1)))
941 return false;
942
943 // If this load has already been emitted, we clearly can't fold it.
944 if (ExprMap.count(Op)) return false;
945
946 return Op.Val->use_size() == 2;
947}
948
949/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
950/// and compute the address being loaded into AM.
951void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
952 SDOperand Chain = Op.getOperand(0);
953 SDOperand Address = Op.getOperand(1);
954 if (getRegPressure(Chain) > getRegPressure(Address)) {
955 Select(Chain);
956 SelectAddress(Address, AM);
957 } else {
958 SelectAddress(Address, AM);
959 Select(Chain);
960 }
961
962 // The chain for this load is now lowered.
963 LoweredTokens.insert(SDOperand(Op.Val, 1));
964 ExprMap[SDOperand(Op.Val, 1)] = 1;
965}
966
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000967unsigned ISel::SelectExpr(SDOperand N) {
968 unsigned Result;
969 unsigned Tmp1, Tmp2, Tmp3;
970 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +0000971 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +0000972 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +0000973
Chris Lattner590d8002005-01-09 18:52:44 +0000974 if (Node->getOpcode() == ISD::CopyFromReg)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000975 // Just use the specified register as our input.
Chris Lattner590d8002005-01-09 18:52:44 +0000976 return dyn_cast<CopyRegSDNode>(Node)->getReg();
Chris Lattnera5ade062005-01-11 21:19:59 +0000977
978 unsigned &Reg = ExprMap[N];
979 if (Reg) return Reg;
980
981 if (N.getOpcode() != ISD::CALL)
982 Reg = Result = (N.getValueType() != MVT::Other) ?
983 MakeReg(N.getValueType()) : 1;
984 else {
985 // If this is a call instruction, make sure to prepare ALL of the result
986 // values as well as the chain.
987 if (Node->getNumValues() == 1)
988 Reg = Result = 1; // Void call, just a chain.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000989 else {
Chris Lattnera5ade062005-01-11 21:19:59 +0000990 Result = MakeReg(Node->getValueType(0));
991 ExprMap[N.getValue(0)] = Result;
992 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
993 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
994 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000995 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000996 }
Chris Lattnera5ade062005-01-11 21:19:59 +0000997
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000998 switch (N.getOpcode()) {
999 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001000 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001001 assert(0 && "Node not handled!\n");
1002 case ISD::FrameIndex:
1003 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1004 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1005 return Result;
1006 case ISD::ConstantPool:
1007 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1008 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1009 return Result;
1010 case ISD::ConstantFP:
1011 ContainsFPCode = true;
1012 Tmp1 = Result; // Intermediate Register
1013 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1014 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1015 Tmp1 = MakeReg(MVT::f64);
1016
1017 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1018 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1019 BuildMI(BB, X86::FLD0, 0, Tmp1);
1020 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1021 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1022 BuildMI(BB, X86::FLD1, 0, Tmp1);
1023 else
1024 assert(0 && "Unexpected constant!");
1025 if (Tmp1 != Result)
1026 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1027 return Result;
1028 case ISD::Constant:
1029 switch (N.getValueType()) {
1030 default: assert(0 && "Cannot use constants of this type!");
1031 case MVT::i1:
1032 case MVT::i8: Opc = X86::MOV8ri; break;
1033 case MVT::i16: Opc = X86::MOV16ri; break;
1034 case MVT::i32: Opc = X86::MOV32ri; break;
1035 }
1036 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1037 return Result;
1038 case ISD::GlobalAddress: {
1039 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1040 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1041 return Result;
1042 }
1043 case ISD::ExternalSymbol: {
1044 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1045 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1046 return Result;
1047 }
1048 case ISD::FP_EXTEND:
1049 Tmp1 = SelectExpr(N.getOperand(0));
1050 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001051 return Result;
1052 case ISD::ZERO_EXTEND: {
1053 int DestIs16 = N.getValueType() == MVT::i16;
1054 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001055
1056 // FIXME: This hack is here for zero extension casts from bool to i8. This
1057 // would not be needed if bools were promoted by Legalize.
1058 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001059 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001060 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1061 return Result;
1062 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001063
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001064 if (isFoldableLoad(N.getOperand(0))) {
1065 static const unsigned Opc[3] = {
1066 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1067 };
1068
1069 X86AddressMode AM;
1070 EmitFoldedLoad(N.getOperand(0), AM);
1071 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1072
1073 return Result;
1074 }
1075
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001076 static const unsigned Opc[3] = {
1077 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1078 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001079 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001080 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1081 return Result;
1082 }
1083 case ISD::SIGN_EXTEND: {
1084 int DestIs16 = N.getValueType() == MVT::i16;
1085 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1086
Chris Lattner590d8002005-01-09 18:52:44 +00001087 // FIXME: Legalize should promote bools to i8!
1088 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1089 "Sign extend from bool not implemented!");
1090
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001091 if (isFoldableLoad(N.getOperand(0))) {
1092 static const unsigned Opc[3] = {
1093 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1094 };
1095
1096 X86AddressMode AM;
1097 EmitFoldedLoad(N.getOperand(0), AM);
1098 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1099 return Result;
1100 }
1101
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001102 static const unsigned Opc[3] = {
1103 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1104 };
1105 Tmp1 = SelectExpr(N.getOperand(0));
1106 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1107 return Result;
1108 }
1109 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001110 // Fold TRUNCATE (LOAD P) into a smaller load from P.
1111 if (isFoldableLoad(N.getOperand(0))) {
1112 switch (N.getValueType()) {
1113 default: assert(0 && "Unknown truncate!");
1114 case MVT::i1:
1115 case MVT::i8: Opc = X86::MOV8rm; break;
1116 case MVT::i16: Opc = X86::MOV16rm; break;
1117 }
1118 X86AddressMode AM;
1119 EmitFoldedLoad(N.getOperand(0), AM);
1120 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1121 return Result;
1122 }
1123
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001124 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1125 // a move out of AX or AL.
1126 switch (N.getOperand(0).getValueType()) {
1127 default: assert(0 && "Unknown truncate!");
1128 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1129 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1130 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1131 }
1132 Tmp1 = SelectExpr(N.getOperand(0));
1133 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1134
1135 switch (N.getValueType()) {
1136 default: assert(0 && "Unknown truncate!");
1137 case MVT::i1:
1138 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1139 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1140 }
1141 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1142 return Result;
1143
1144 case ISD::FP_ROUND:
1145 // Truncate from double to float by storing to memory as float,
1146 // then reading it back into a register.
1147
1148 // Create as stack slot to use.
Chris Lattner590d8002005-01-09 18:52:44 +00001149 // FIXME: This should automatically be made by the Legalizer!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001150 Tmp1 = TLI.getTargetData().getFloatAlignment();
1151 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
1152
1153 // Codegen the input.
1154 Tmp1 = SelectExpr(N.getOperand(0));
1155
1156 // Emit the store, then the reload.
1157 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
1158 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001159 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00001160
1161 case ISD::SINT_TO_FP:
1162 case ISD::UINT_TO_FP: {
1163 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001164 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001165
1166 // Promote the integer to a type supported by FLD. We do this because there
1167 // are no unsigned FLD instructions, so we must promote an unsigned value to
1168 // a larger signed value, then use FLD on the larger value.
1169 //
1170 MVT::ValueType PromoteType = MVT::Other;
1171 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1172 unsigned PromoteOpcode = 0;
1173 unsigned RealDestReg = Result;
1174 switch (SrcTy) {
1175 case MVT::i1:
1176 case MVT::i8:
1177 // We don't have the facilities for directly loading byte sized data from
1178 // memory (even signed). Promote it to 16 bits.
1179 PromoteType = MVT::i16;
1180 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1181 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1182 break;
1183 case MVT::i16:
1184 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1185 PromoteType = MVT::i32;
1186 PromoteOpcode = X86::MOVZX32rr16;
1187 }
1188 break;
1189 default:
1190 // Don't fild into the real destination.
1191 if (Node->getOpcode() == ISD::UINT_TO_FP)
1192 Result = MakeReg(Node->getValueType(0));
1193 break;
1194 }
1195
1196 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1197
1198 if (PromoteType != MVT::Other) {
1199 Tmp2 = MakeReg(PromoteType);
1200 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1201 SrcTy = PromoteType;
1202 Tmp1 = Tmp2;
1203 }
1204
1205 // Spill the integer to memory and reload it from there.
1206 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1207 MachineFunction *F = BB->getParent();
1208 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1209
1210 switch (SrcTy) {
1211 case MVT::i64:
1212 // FIXME: this won't work for cast [u]long to FP
1213 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1214 FrameIdx).addReg(Tmp1);
1215 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1216 FrameIdx, 4).addReg(Tmp1+1);
1217 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1218 break;
1219 case MVT::i32:
1220 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1221 FrameIdx).addReg(Tmp1);
1222 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1223 break;
1224 case MVT::i16:
1225 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1226 FrameIdx).addReg(Tmp1);
1227 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1228 break;
1229 default: break; // No promotion required.
1230 }
1231
1232 if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i32) {
1233 // If this is a cast from uint -> double, we need to be careful when if
1234 // the "sign" bit is set. If so, we don't want to make a negative number,
1235 // we want to make a positive number. Emit code to add an offset if the
1236 // sign bit is set.
1237
1238 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1239 unsigned IsNeg = MakeReg(MVT::i32);
1240 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1241
1242 // Create a CP value that has the offset in one word and 0 in the other.
1243 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1244 0x4f80000000000000ULL);
1245 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1246 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1247 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1248
1249 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1250 // We need special handling for unsigned 64-bit integer sources. If the
1251 // input number has the "sign bit" set, then we loaded it incorrectly as a
1252 // negative 64-bit number. In this case, add an offset value.
1253
1254 // Emit a test instruction to see if the dynamic input value was signed.
1255 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1256
1257 // If the sign bit is set, get a pointer to an offset, otherwise get a
1258 // pointer to a zero.
1259 MachineConstantPool *CP = F->getConstantPool();
1260 unsigned Zero = MakeReg(MVT::i32);
1261 Constant *Null = Constant::getNullValue(Type::UIntTy);
1262 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1263 CP->getConstantPoolIndex(Null));
1264 unsigned Offset = MakeReg(MVT::i32);
1265 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1266
1267 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1268 CP->getConstantPoolIndex(OffsetCst));
1269 unsigned Addr = MakeReg(MVT::i32);
1270 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1271
1272 // Load the constant for an add. FIXME: this could make an 'fadd' that
1273 // reads directly from memory, but we don't support these yet.
1274 unsigned ConstReg = MakeReg(MVT::f64);
1275 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1276
1277 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1278 }
1279 return RealDestReg;
1280 }
1281 case ISD::FP_TO_SINT:
1282 case ISD::FP_TO_UINT: {
1283 // FIXME: Most of this grunt work should be done by legalize!
1284 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1285
1286 // Change the floating point control register to use "round towards zero"
1287 // mode when truncating to an integer value.
1288 //
1289 MachineFunction *F = BB->getParent();
1290 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1291 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1292
1293 // Load the old value of the high byte of the control word...
1294 unsigned HighPartOfCW = MakeReg(MVT::i8);
1295 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1296 CWFrameIdx, 1);
1297
1298 // Set the high part to be round to zero...
1299 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1300 CWFrameIdx, 1).addImm(12);
1301
1302 // Reload the modified control word now...
1303 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1304
1305 // Restore the memory image of control word to original value
1306 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1307 CWFrameIdx, 1).addReg(HighPartOfCW);
1308
1309 // We don't have the facilities for directly storing byte sized data to
1310 // memory. Promote it to 16 bits. We also must promote unsigned values to
1311 // larger classes because we only have signed FP stores.
1312 MVT::ValueType StoreClass = Node->getValueType(0);
1313 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1314 switch (StoreClass) {
1315 case MVT::i8: StoreClass = MVT::i16; break;
1316 case MVT::i16: StoreClass = MVT::i32; break;
1317 case MVT::i32: StoreClass = MVT::i64; break;
1318 // The following treatment of cLong may not be perfectly right,
1319 // but it survives chains of casts of the form
1320 // double->ulong->double.
1321 case MVT::i64: StoreClass = MVT::i64; break;
1322 default: assert(0 && "Unknown store class!");
1323 }
1324
1325 // Spill the integer to memory and reload it from there.
1326 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1327 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1328
1329 switch (StoreClass) {
1330 default: assert(0 && "Unknown store class!");
1331 case MVT::i16:
1332 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1333 break;
1334 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001335 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001336 break;
1337 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001338 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001339 break;
1340 }
1341
1342 switch (Node->getValueType(0)) {
1343 default:
1344 assert(0 && "Unknown integer type!");
1345 case MVT::i64:
1346 // FIXME: this isn't gunna work.
1347 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1348 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1349 case MVT::i32:
1350 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1351 break;
1352 case MVT::i16:
1353 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1354 break;
1355 case MVT::i8:
1356 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1357 break;
1358 }
1359
1360 // Reload the original control word now.
1361 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1362 return Result;
1363 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001364 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001365 Op0 = N.getOperand(0);
1366 Op1 = N.getOperand(1);
1367
1368 if (isFoldableLoad(Op0))
1369 std::swap(Op0, Op1);
1370
1371 if (isFoldableLoad(Op1)) {
1372 switch (N.getValueType()) {
1373 default: assert(0 && "Cannot add this type!");
1374 case MVT::i1:
1375 case MVT::i8: Opc = X86::ADD8rm; break;
1376 case MVT::i16: Opc = X86::ADD16rm; break;
1377 case MVT::i32: Opc = X86::ADD32rm; break;
1378 case MVT::f32: Opc = X86::FADD32m; break;
1379 case MVT::f64: Opc = X86::FADD64m; break;
1380 }
1381 X86AddressMode AM;
1382 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1383 Tmp1 = SelectExpr(Op0);
1384 EmitFoldedLoad(Op1, AM);
1385 } else {
1386 EmitFoldedLoad(Op1, AM);
1387 Tmp1 = SelectExpr(Op0);
1388 }
1389 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1390 return Result;
1391 }
1392
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001393 // See if we can codegen this as an LEA to fold operations together.
1394 if (N.getValueType() == MVT::i32) {
1395 X86AddressMode AM;
Chris Lattnera5ade062005-01-11 21:19:59 +00001396 if (!SelectAddress(Op0, AM) && !SelectAddress(Op1, AM)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001397 // If this is not just an add, emit the LEA. For a simple add (like
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001398 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001399 // leave this as LEA, then peephole it to 'ADD' after two address elim
1400 // happens.
1401 if (AM.Scale != 1 || AM.BaseType == X86AddressMode::FrameIndexBase ||
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001402 AM.GV || (AM.Base.Reg && AM.IndexReg && AM.Disp)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001403 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1404 return Result;
1405 }
1406 }
1407 }
Chris Lattner11333092005-01-11 03:11:44 +00001408
Chris Lattnera5ade062005-01-11 21:19:59 +00001409 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001410 Opc = 0;
1411 if (CN->getValue() == 1) { // add X, 1 -> inc X
1412 switch (N.getValueType()) {
1413 default: assert(0 && "Cannot integer add this type!");
1414 case MVT::i8: Opc = X86::INC8r; break;
1415 case MVT::i16: Opc = X86::INC16r; break;
1416 case MVT::i32: Opc = X86::INC32r; break;
1417 }
1418 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1419 switch (N.getValueType()) {
1420 default: assert(0 && "Cannot integer add this type!");
1421 case MVT::i8: Opc = X86::DEC8r; break;
1422 case MVT::i16: Opc = X86::DEC16r; break;
1423 case MVT::i32: Opc = X86::DEC32r; break;
1424 }
1425 }
1426
1427 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001428 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001429 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1430 return Result;
1431 }
1432
1433 switch (N.getValueType()) {
1434 default: assert(0 && "Cannot add this type!");
1435 case MVT::i8: Opc = X86::ADD8ri; break;
1436 case MVT::i16: Opc = X86::ADD16ri; break;
1437 case MVT::i32: Opc = X86::ADD32ri; break;
1438 }
1439 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001440 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001441 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1442 return Result;
1443 }
1444 }
1445
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001446 switch (N.getValueType()) {
1447 default: assert(0 && "Cannot add this type!");
1448 case MVT::i8: Opc = X86::ADD8rr; break;
1449 case MVT::i16: Opc = X86::ADD16rr; break;
1450 case MVT::i32: Opc = X86::ADD32rr; break;
1451 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001452 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001453 }
Chris Lattner11333092005-01-11 03:11:44 +00001454
Chris Lattnera5ade062005-01-11 21:19:59 +00001455 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1456 Tmp1 = SelectExpr(Op0);
1457 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001458 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001459 Tmp2 = SelectExpr(Op1);
1460 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001461 }
1462
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001463 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1464 return Result;
1465 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001466 case ISD::MUL:
1467 case ISD::AND:
1468 case ISD::OR:
1469 case ISD::XOR:
1470 static const unsigned SUBTab[] = {
1471 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1472 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1473 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1474 };
1475 static const unsigned MULTab[] = {
1476 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1477 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1478 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1479 };
1480 static const unsigned ANDTab[] = {
1481 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1482 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1483 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1484 };
1485 static const unsigned ORTab[] = {
1486 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1487 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1488 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1489 };
1490 static const unsigned XORTab[] = {
1491 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1492 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1493 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1494 };
1495
1496 Op0 = Node->getOperand(0);
1497 Op1 = Node->getOperand(1);
1498
1499 if (Node->getOpcode() == ISD::SUB && MVT::isInteger(N.getValueType()))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001500 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1501 if (CN->isNullValue()) { // 0 - N -> neg N
1502 switch (N.getValueType()) {
1503 default: assert(0 && "Cannot sub this type!");
1504 case MVT::i1:
1505 case MVT::i8: Opc = X86::NEG8r; break;
1506 case MVT::i16: Opc = X86::NEG16r; break;
1507 case MVT::i32: Opc = X86::NEG32r; break;
1508 }
1509 Tmp1 = SelectExpr(N.getOperand(1));
1510 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1511 return Result;
1512 }
1513
Chris Lattnera5ade062005-01-11 21:19:59 +00001514 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1515 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001516 switch (N.getValueType()) {
1517 default: assert(0 && "Cannot add this type!");
1518 case MVT::i1:
1519 case MVT::i8: Opc = X86::NOT8r; break;
1520 case MVT::i16: Opc = X86::NOT16r; break;
1521 case MVT::i32: Opc = X86::NOT32r; break;
1522 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001523 Tmp1 = SelectExpr(Op0);
Chris Lattnerd4dab922005-01-11 04:31:30 +00001524 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1525 return Result;
1526 }
1527
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001528 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001529 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001530 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001531 case MVT::i8: Opc = 0; break;
1532 case MVT::i16: Opc = 1; break;
1533 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001534 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001535 switch (Node->getOpcode()) {
1536 default: assert(0 && "Unreachable!");
1537 case ISD::SUB: Opc = SUBTab[Opc]; break;
1538 case ISD::MUL: Opc = MULTab[Opc]; break;
1539 case ISD::AND: Opc = ANDTab[Opc]; break;
1540 case ISD::OR: Opc = ORTab[Opc]; break;
1541 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001542 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001543 if (Opc) { // Can't fold MUL:i8 R, imm
1544 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001545 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1546 return Result;
1547 }
1548 }
Chris Lattner11333092005-01-11 03:11:44 +00001549
Chris Lattnera5ade062005-01-11 21:19:59 +00001550 if (isFoldableLoad(Op0))
1551 if (Node->getOpcode() != ISD::SUB) {
1552 std::swap(Op0, Op1);
1553 } else {
1554 // Emit 'reverse' subract, with a memory operand.
1555 switch (N.getValueType()) {
1556 default: Opc = 0; break;
1557 case MVT::f32: Opc = X86::FSUBR32m; break;
1558 case MVT::f64: Opc = X86::FSUBR64m; break;
1559 }
1560 if (Opc) {
1561 X86AddressMode AM;
1562 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1563 EmitFoldedLoad(Op0, AM);
1564 Tmp1 = SelectExpr(Op1);
1565 } else {
1566 Tmp1 = SelectExpr(Op1);
1567 EmitFoldedLoad(Op0, AM);
1568 }
1569 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1570 return Result;
1571 }
1572 }
1573
1574 if (isFoldableLoad(Op1)) {
1575 switch (N.getValueType()) {
1576 default: assert(0 && "Cannot operate on this type!");
1577 case MVT::i1:
1578 case MVT::i8: Opc = 5; break;
1579 case MVT::i16: Opc = 6; break;
1580 case MVT::i32: Opc = 7; break;
1581 case MVT::f32: Opc = 8; break;
1582 case MVT::f64: Opc = 9; break;
1583 }
1584 switch (Node->getOpcode()) {
1585 default: assert(0 && "Unreachable!");
1586 case ISD::SUB: Opc = SUBTab[Opc]; break;
1587 case ISD::MUL: Opc = MULTab[Opc]; break;
1588 case ISD::AND: Opc = ANDTab[Opc]; break;
1589 case ISD::OR: Opc = ORTab[Opc]; break;
1590 case ISD::XOR: Opc = XORTab[Opc]; break;
1591 }
1592
1593 X86AddressMode AM;
1594 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1595 Tmp1 = SelectExpr(Op0);
1596 EmitFoldedLoad(Op1, AM);
1597 } else {
1598 EmitFoldedLoad(Op1, AM);
1599 Tmp1 = SelectExpr(Op0);
1600 }
1601 if (Opc) {
1602 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1603 } else {
1604 assert(Node->getOpcode() == ISD::MUL &&
1605 N.getValueType() == MVT::i8 && "Unexpected situation!");
1606 // Must use the MUL instruction, which forces use of AL.
1607 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1608 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1609 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1610 }
1611 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001612 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001613
1614 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1615 Tmp1 = SelectExpr(Op0);
1616 Tmp2 = SelectExpr(Op1);
1617 } else {
1618 Tmp2 = SelectExpr(Op1);
1619 Tmp1 = SelectExpr(Op0);
1620 }
1621
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001622 switch (N.getValueType()) {
1623 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001624 case MVT::i1:
1625 case MVT::i8: Opc = 10; break;
1626 case MVT::i16: Opc = 11; break;
1627 case MVT::i32: Opc = 12; break;
1628 case MVT::f32: Opc = 13; break;
1629 case MVT::f64: Opc = 14; break;
1630 }
1631 switch (Node->getOpcode()) {
1632 default: assert(0 && "Unreachable!");
1633 case ISD::SUB: Opc = SUBTab[Opc]; break;
1634 case ISD::MUL: Opc = MULTab[Opc]; break;
1635 case ISD::AND: Opc = ANDTab[Opc]; break;
1636 case ISD::OR: Opc = ORTab[Opc]; break;
1637 case ISD::XOR: Opc = XORTab[Opc]; break;
1638 }
1639 if (Opc) {
1640 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1641 } else {
1642 assert(Node->getOpcode() == ISD::MUL &&
1643 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001644 // Must use the MUL instruction, which forces use of AL.
1645 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1646 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1647 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001648 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001649 return Result;
1650
1651 case ISD::SELECT:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001652 if (N.getValueType() != MVT::i1 && N.getValueType() != MVT::i8) {
Chris Lattner11333092005-01-11 03:11:44 +00001653 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1654 Tmp2 = SelectExpr(N.getOperand(1));
1655 Tmp3 = SelectExpr(N.getOperand(2));
1656 } else {
1657 Tmp3 = SelectExpr(N.getOperand(2));
1658 Tmp2 = SelectExpr(N.getOperand(1));
1659 }
Chris Lattner24aad1b2005-01-10 22:10:13 +00001660 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001661 return Result;
1662 } else {
1663 // FIXME: This should not be implemented here, it should be in the generic
1664 // code!
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001665 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1666 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1667 N.getOperand(1)));
1668 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1669 N.getOperand(2)));
1670 } else {
1671 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1672 N.getOperand(2)));
1673 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1674 N.getOperand(1)));
1675 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001676 unsigned TmpReg = MakeReg(MVT::i16);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001677 EmitSelectCC(N.getOperand(0), MVT::i16, Tmp2, Tmp3, TmpReg);
1678 // FIXME: need subregs to do better than this!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001679 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(TmpReg);
1680 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1681 return Result;
1682 }
1683
1684 case ISD::SDIV:
1685 case ISD::UDIV:
1686 case ISD::SREM:
1687 case ISD::UREM: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001688 if (N.getOpcode() == ISD::SDIV)
1689 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1690 // FIXME: These special cases should be handled by the lowering impl!
1691 unsigned RHS = CN->getValue();
1692 bool isNeg = false;
1693 if ((int)RHS < 0) {
1694 isNeg = true;
1695 RHS = -RHS;
1696 }
1697 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1698 unsigned Log = log2(RHS);
1699 unsigned TmpReg = MakeReg(N.getValueType());
1700 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1701 switch (N.getValueType()) {
1702 default: assert("Unknown type to signed divide!");
1703 case MVT::i8:
1704 SAROpc = X86::SAR8ri;
1705 SHROpc = X86::SHR8ri;
1706 ADDOpc = X86::ADD8rr;
1707 NEGOpc = X86::NEG8r;
1708 break;
1709 case MVT::i16:
1710 SAROpc = X86::SAR16ri;
1711 SHROpc = X86::SHR16ri;
1712 ADDOpc = X86::ADD16rr;
1713 NEGOpc = X86::NEG16r;
1714 break;
1715 case MVT::i32:
1716 SAROpc = X86::SAR32ri;
1717 SHROpc = X86::SHR32ri;
1718 ADDOpc = X86::ADD32rr;
1719 NEGOpc = X86::NEG32r;
1720 break;
1721 }
Chris Lattner11333092005-01-11 03:11:44 +00001722 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001723 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1724 unsigned TmpReg2 = MakeReg(N.getValueType());
1725 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1726 unsigned TmpReg3 = MakeReg(N.getValueType());
1727 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1728
1729 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1730 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1731 if (isNeg)
1732 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1733 return Result;
1734 }
1735 }
1736
Chris Lattner11333092005-01-11 03:11:44 +00001737 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1738 Tmp1 = SelectExpr(N.getOperand(0));
1739 Tmp2 = SelectExpr(N.getOperand(1));
1740 } else {
1741 Tmp2 = SelectExpr(N.getOperand(1));
1742 Tmp1 = SelectExpr(N.getOperand(0));
1743 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001744
1745 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1746 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1747 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1748 switch (N.getValueType()) {
1749 default: assert(0 && "Cannot sdiv this type!");
1750 case MVT::i8:
1751 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1752 LoReg = X86::AL;
1753 HiReg = X86::AH;
1754 MovOpcode = X86::MOV8rr;
1755 ClrOpcode = X86::MOV8ri;
1756 SExtOpcode = X86::CBW;
1757 break;
1758 case MVT::i16:
1759 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1760 LoReg = X86::AX;
1761 HiReg = X86::DX;
1762 MovOpcode = X86::MOV16rr;
1763 ClrOpcode = X86::MOV16ri;
1764 SExtOpcode = X86::CWD;
1765 break;
1766 case MVT::i32:
1767 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
1768 LoReg =X86::EAX;
1769 HiReg = X86::EDX;
1770 MovOpcode = X86::MOV32rr;
1771 ClrOpcode = X86::MOV32ri;
1772 SExtOpcode = X86::CDQ;
1773 break;
1774 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1775 case MVT::f32:
1776 case MVT::f64:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001777 if (N.getOpcode() == ISD::SDIV)
1778 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1779 else
1780 assert(0 && "FIXME: Emit frem libcall to fmod!");
1781 return Result;
1782 }
1783
1784 // Set up the low part.
1785 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1786
1787 if (isSigned) {
1788 // Sign extend the low part into the high part.
1789 BuildMI(BB, SExtOpcode, 0);
1790 } else {
1791 // Zero out the high part, effectively zero extending the input.
1792 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1793 }
1794
1795 // Emit the DIV/IDIV instruction.
1796 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1797
1798 // Get the result of the divide or rem.
1799 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1800 return Result;
1801 }
1802
1803 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001804 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001805 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
1806 switch (N.getValueType()) {
1807 default: assert(0 && "Cannot shift this type!");
1808 case MVT::i8: Opc = X86::ADD8rr; break;
1809 case MVT::i16: Opc = X86::ADD16rr; break;
1810 case MVT::i32: Opc = X86::ADD32rr; break;
1811 }
1812 Tmp1 = SelectExpr(N.getOperand(0));
1813 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
1814 return Result;
1815 }
1816
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001817 switch (N.getValueType()) {
1818 default: assert(0 && "Cannot shift this type!");
1819 case MVT::i8: Opc = X86::SHL8ri; break;
1820 case MVT::i16: Opc = X86::SHL16ri; break;
1821 case MVT::i32: Opc = X86::SHL32ri; break;
1822 }
Chris Lattner11333092005-01-11 03:11:44 +00001823 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001824 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1825 return Result;
1826 }
Chris Lattner11333092005-01-11 03:11:44 +00001827
1828 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1829 Tmp1 = SelectExpr(N.getOperand(0));
1830 Tmp2 = SelectExpr(N.getOperand(1));
1831 } else {
1832 Tmp2 = SelectExpr(N.getOperand(1));
1833 Tmp1 = SelectExpr(N.getOperand(0));
1834 }
1835
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001836 switch (N.getValueType()) {
1837 default: assert(0 && "Cannot shift this type!");
1838 case MVT::i8 : Opc = X86::SHL8rCL; break;
1839 case MVT::i16: Opc = X86::SHL16rCL; break;
1840 case MVT::i32: Opc = X86::SHL32rCL; break;
1841 }
1842 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1843 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1844 return Result;
1845 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001846 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1847 switch (N.getValueType()) {
1848 default: assert(0 && "Cannot shift this type!");
1849 case MVT::i8: Opc = X86::SHR8ri; break;
1850 case MVT::i16: Opc = X86::SHR16ri; break;
1851 case MVT::i32: Opc = X86::SHR32ri; break;
1852 }
Chris Lattner11333092005-01-11 03:11:44 +00001853 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001854 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1855 return Result;
1856 }
Chris Lattner11333092005-01-11 03:11:44 +00001857
1858 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1859 Tmp1 = SelectExpr(N.getOperand(0));
1860 Tmp2 = SelectExpr(N.getOperand(1));
1861 } else {
1862 Tmp2 = SelectExpr(N.getOperand(1));
1863 Tmp1 = SelectExpr(N.getOperand(0));
1864 }
1865
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001866 switch (N.getValueType()) {
1867 default: assert(0 && "Cannot shift this type!");
1868 case MVT::i8 : Opc = X86::SHR8rCL; break;
1869 case MVT::i16: Opc = X86::SHR16rCL; break;
1870 case MVT::i32: Opc = X86::SHR32rCL; break;
1871 }
1872 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1873 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1874 return Result;
1875 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001876 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1877 switch (N.getValueType()) {
1878 default: assert(0 && "Cannot shift this type!");
1879 case MVT::i8: Opc = X86::SAR8ri; break;
1880 case MVT::i16: Opc = X86::SAR16ri; break;
1881 case MVT::i32: Opc = X86::SAR32ri; break;
1882 }
Chris Lattner11333092005-01-11 03:11:44 +00001883 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001884 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1885 return Result;
1886 }
Chris Lattner11333092005-01-11 03:11:44 +00001887
1888 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1889 Tmp1 = SelectExpr(N.getOperand(0));
1890 Tmp2 = SelectExpr(N.getOperand(1));
1891 } else {
1892 Tmp2 = SelectExpr(N.getOperand(1));
1893 Tmp1 = SelectExpr(N.getOperand(0));
1894 }
1895
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001896 switch (N.getValueType()) {
1897 default: assert(0 && "Cannot shift this type!");
1898 case MVT::i8 : Opc = X86::SAR8rCL; break;
1899 case MVT::i16: Opc = X86::SAR16rCL; break;
1900 case MVT::i32: Opc = X86::SAR32rCL; break;
1901 }
1902 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1903 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1904 return Result;
1905
1906 case ISD::SETCC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001907 EmitCMP(N.getOperand(0), N.getOperand(1));
1908 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
1909 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
1910 return Result;
1911 case ISD::LOAD: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001912 // Make sure we generate both values.
1913 if (Result != 1)
1914 ExprMap[N.getValue(1)] = 1; // Generate the token
1915 else
1916 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1917
Chris Lattner5188ad72005-01-08 19:28:19 +00001918 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001919 default: assert(0 && "Cannot load this type!");
1920 case MVT::i1:
1921 case MVT::i8: Opc = X86::MOV8rm; break;
1922 case MVT::i16: Opc = X86::MOV16rm; break;
1923 case MVT::i32: Opc = X86::MOV32rm; break;
1924 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
1925 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
1926 }
Chris Lattner11333092005-01-11 03:11:44 +00001927
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001928 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00001929 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001930 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
1931 } else {
1932 X86AddressMode AM;
Chris Lattnera5ade062005-01-11 21:19:59 +00001933 EmitFoldedLoad(N, AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001934 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1935 }
1936 return Result;
1937 }
1938 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001939 // Generate both result values.
1940 if (Result != 1)
1941 ExprMap[N.getValue(1)] = 1; // Generate the token
1942 else
1943 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1944
1945 // FIXME: We are currently ignoring the requested alignment for handling
1946 // greater than the stack alignment. This will need to be revisited at some
1947 // point. Align = N.getOperand(2);
1948
1949 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1950 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1951 std::cerr << "Cannot allocate stack object with greater alignment than"
1952 << " the stack alignment yet!";
1953 abort();
1954 }
1955
1956 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001957 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001958 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
1959 .addImm(CN->getValue());
1960 } else {
Chris Lattner11333092005-01-11 03:11:44 +00001961 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1962 Select(N.getOperand(0));
1963 Tmp1 = SelectExpr(N.getOperand(1));
1964 } else {
1965 Tmp1 = SelectExpr(N.getOperand(1));
1966 Select(N.getOperand(0));
1967 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001968
1969 // Subtract size from stack pointer, thereby allocating some space.
1970 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
1971 }
1972
1973 // Put a pointer to the space into the result register, by copying the stack
1974 // pointer.
1975 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
1976 return Result;
1977
1978 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00001979 // The chain for this call is now lowered.
1980 LoweredTokens.insert(N.getValue(Node->getNumValues()-1));
1981
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001982 if (GlobalAddressSDNode *GASD =
1983 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001984 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001985 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
1986 } else if (ExternalSymbolSDNode *ESSDN =
1987 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001988 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001989 BuildMI(BB, X86::CALLpcrel32,
1990 1).addExternalSymbol(ESSDN->getSymbol(), true);
1991 } else {
Chris Lattner11333092005-01-11 03:11:44 +00001992 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1993 Select(N.getOperand(0));
1994 Tmp1 = SelectExpr(N.getOperand(1));
1995 } else {
1996 Tmp1 = SelectExpr(N.getOperand(1));
1997 Select(N.getOperand(0));
1998 }
1999
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002000 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2001 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002002 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002003 default: assert(0 && "Unknown value type for call result!");
2004 case MVT::Other: return 1;
2005 case MVT::i1:
2006 case MVT::i8:
2007 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2008 break;
2009 case MVT::i16:
2010 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2011 break;
2012 case MVT::i32:
2013 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002014 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002015 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2016 break;
2017 case MVT::f32:
2018 case MVT::f64: // Floating-point return values live in %ST(0)
2019 ContainsFPCode = true;
2020 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2021 break;
2022 }
2023 return Result+N.ResNo;
2024 }
2025
2026 return 0;
2027}
2028
2029void ISel::Select(SDOperand N) {
2030 unsigned Tmp1, Tmp2, Opc;
2031
2032 // FIXME: Disable for our current expansion model!
2033 if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second)
2034 return; // Already selected.
2035
Chris Lattner989de032005-01-11 06:14:36 +00002036 SDNode *Node = N.Val;
2037
2038 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002039 default:
Chris Lattner989de032005-01-11 06:14:36 +00002040 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002041 assert(0 && "Node not handled yet!");
2042 case ISD::EntryToken: return; // Noop
2043 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002044 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2045 Select(N.getOperand(0));
2046 Tmp1 = SelectExpr(N.getOperand(1));
2047 } else {
2048 Tmp1 = SelectExpr(N.getOperand(1));
2049 Select(N.getOperand(0));
2050 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002051 Tmp2 = cast<CopyRegSDNode>(N)->getReg();
2052
2053 if (Tmp1 != Tmp2) {
2054 switch (N.getOperand(1).getValueType()) {
2055 default: assert(0 && "Invalid type for operation!");
2056 case MVT::i1:
2057 case MVT::i8: Opc = X86::MOV8rr; break;
2058 case MVT::i16: Opc = X86::MOV16rr; break;
2059 case MVT::i32: Opc = X86::MOV32rr; break;
2060 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00002061 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002062 }
2063 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2064 }
2065 return;
2066 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002067 switch (N.getNumOperands()) {
2068 default:
2069 assert(0 && "Unknown return instruction!");
2070 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002071 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2072 N.getOperand(2).getValueType() == MVT::i32 &&
2073 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002074 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2075 Tmp1 = SelectExpr(N.getOperand(1));
2076 Tmp2 = SelectExpr(N.getOperand(2));
2077 } else {
2078 Tmp2 = SelectExpr(N.getOperand(2));
2079 Tmp1 = SelectExpr(N.getOperand(1));
2080 }
2081 Select(N.getOperand(0));
2082
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002083 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2084 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2085 // Declare that EAX & EDX are live on exit.
2086 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2087 .addReg(X86::ESP);
2088 break;
2089 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002090 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2091 Select(N.getOperand(0));
2092 Tmp1 = SelectExpr(N.getOperand(1));
2093 } else {
2094 Tmp1 = SelectExpr(N.getOperand(1));
2095 Select(N.getOperand(0));
2096 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002097 switch (N.getOperand(1).getValueType()) {
2098 default: assert(0 && "All other types should have been promoted!!");
2099 case MVT::f64:
2100 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2101 // Declare that top-of-stack is live on exit
2102 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2103 break;
2104 case MVT::i32:
2105 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2106 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2107 break;
2108 }
2109 break;
2110 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002111 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002112 break;
2113 }
2114 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2115 return;
2116 case ISD::BR: {
2117 Select(N.getOperand(0));
2118 MachineBasicBlock *Dest =
2119 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2120 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2121 return;
2122 }
2123
2124 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002125 MachineBasicBlock *Dest =
2126 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002127
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002128 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2129 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002130 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2131 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2132 Select(N.getOperand(0));
2133 Tmp1 = SelectExpr(N.getOperand(1));
2134 } else {
2135 Tmp1 = SelectExpr(N.getOperand(1));
2136 Select(N.getOperand(0));
2137 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002138 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2139 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2140 }
Chris Lattner11333092005-01-11 03:11:44 +00002141
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002142 return;
2143 }
2144 case ISD::LOAD:
2145 case ISD::CALL:
2146 case ISD::DYNAMIC_STACKALLOC:
2147 SelectExpr(N);
2148 return;
2149 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002150 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002151
2152 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2153 Opc = 0;
2154 switch (CN->getValueType(0)) {
2155 default: assert(0 && "Invalid type for operation!");
2156 case MVT::i1:
2157 case MVT::i8: Opc = X86::MOV8mi; break;
2158 case MVT::i16: Opc = X86::MOV16mi; break;
2159 case MVT::i32: Opc = X86::MOV32mi; break;
2160 case MVT::f32:
2161 case MVT::f64: break;
2162 }
2163 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002164 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2165 Select(N.getOperand(0));
2166 SelectAddress(N.getOperand(2), AM);
2167 } else {
2168 SelectAddress(N.getOperand(2), AM);
2169 Select(N.getOperand(0));
2170 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002171 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2172 return;
2173 }
2174 }
Chris Lattner837caa72005-01-11 23:21:30 +00002175
2176 // Check to see if this is a load/op/store combination.
2177 if (N.getOperand(1).Val->hasOneUse() &&
2178 isFoldableLoad(N.getOperand(0).getValue(0))) {
2179 SDOperand TheLoad = N.getOperand(0).getValue(0);
2180 // Check to see if we are loading the same pointer that we're storing to.
2181 if (TheLoad.getOperand(1) == N.getOperand(2)) {
2182 // See if the stored value is a simple binary operator that uses the
2183 // load as one of its operands.
2184 SDOperand Op = N.getOperand(1);
2185 if (Op.Val->getNumOperands() == 2 &&
2186 (Op.getOperand(0) == TheLoad || Op.getOperand(1) == TheLoad)) {
2187 // Finally, check to see if this is one of the ops we can handle!
2188 static const unsigned ADDTAB[] = {
2189 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2190 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr, 0, 0,
2191 };
Chris Lattner7ea64f52005-01-12 01:28:00 +00002192 static const unsigned SUBTAB[] = {
2193 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2194 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr, 0, 0,
2195 };
2196 static const unsigned ANDTAB[] = {
2197 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2198 X86::AND8mr, X86::AND16mr, X86::AND32mr, 0, 0,
2199 };
2200 static const unsigned ORTAB[] = {
2201 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2202 X86::OR8mr, X86::OR16mr, X86::OR32mr, 0, 0,
2203 };
2204 static const unsigned XORTAB[] = {
2205 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2206 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr, 0, 0,
2207 };
2208 static const unsigned SHLTAB[] = {
2209 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2210 /*Have to put the reg in CL*/0, 0, 0, 0, 0,
2211 };
2212 static const unsigned SARTAB[] = {
2213 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2214 /*Have to put the reg in CL*/0, 0, 0, 0, 0,
2215 };
2216 static const unsigned SHRTAB[] = {
2217 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2218 /*Have to put the reg in CL*/0, 0, 0, 0, 0,
2219 };
Chris Lattner837caa72005-01-11 23:21:30 +00002220
2221 const unsigned *TabPtr = 0;
2222 switch (Op.getOpcode()) {
Chris Lattner7ea64f52005-01-12 01:28:00 +00002223 default: std::cerr << "CANNOT [mem] op= val: "; Op.Val->dump(); std::cerr << "\n"; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002224 case ISD::ADD: TabPtr = ADDTAB; break;
Chris Lattner7ea64f52005-01-12 01:28:00 +00002225 case ISD::SUB: TabPtr = SUBTAB; break;
2226 case ISD::AND: TabPtr = ANDTAB; break;
2227 case ISD:: OR: TabPtr = ORTAB; break;
2228 case ISD::XOR: TabPtr = XORTAB; break;
2229 case ISD::SHL: TabPtr = SHLTAB; break;
2230 case ISD::SRA: TabPtr = SARTAB; break;
2231 case ISD::SRL: TabPtr = SHRTAB; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002232 }
2233
2234 if (TabPtr) {
2235 // Handle: [mem] op= CST
2236 SDOperand Op0 = Op.getOperand(0);
2237 SDOperand Op1 = Op.getOperand(1);
2238 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2239 switch (CN->getValueType(0)) {
2240 default: break;
2241 case MVT::i1:
2242 case MVT::i8: Opc = TabPtr[0]; break;
2243 case MVT::i16: Opc = TabPtr[1]; break;
2244 case MVT::i32: Opc = TabPtr[2]; break;
2245 }
2246
2247 if (Opc) {
2248 if (getRegPressure(TheLoad.getOperand(0)) >
2249 getRegPressure(TheLoad.getOperand(1))) {
2250 Select(TheLoad.getOperand(0));
2251 SelectAddress(TheLoad.getOperand(1), AM);
2252 } else {
2253 SelectAddress(TheLoad.getOperand(1), AM);
2254 Select(TheLoad.getOperand(0));
2255 }
2256
2257 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2258 return;
2259 }
2260 }
2261
2262 // If we have [mem] = V op [mem], try to turn it into:
2263 // [mem] = [mem] op V.
Chris Lattner7ea64f52005-01-12 01:28:00 +00002264 if (Op1 == TheLoad && Op.getOpcode() != ISD::SUB &&
2265 Op.getOpcode() != ISD::SHL && Op.getOpcode() != ISD::SRA &&
2266 Op.getOpcode() != ISD::SRL)
Chris Lattner837caa72005-01-11 23:21:30 +00002267 std::swap(Op0, Op1);
2268
2269 if (Op0 == TheLoad) {
2270 switch (Op0.getValueType()) {
2271 default: break;
2272 case MVT::i1:
2273 case MVT::i8: Opc = TabPtr[3]; break;
2274 case MVT::i16: Opc = TabPtr[4]; break;
2275 case MVT::i32: Opc = TabPtr[5]; break;
2276 case MVT::f32: Opc = TabPtr[6]; break;
2277 case MVT::f64: Opc = TabPtr[7]; break;
2278 }
2279
2280 if (Opc) {
2281 Select(TheLoad.getOperand(0));
2282 SelectAddress(TheLoad.getOperand(1), AM);
2283 unsigned Reg = SelectExpr(Op1);
2284 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addReg(Reg);
2285 return;
2286 }
2287 }
Chris Lattner837caa72005-01-11 23:21:30 +00002288 }
2289 }
2290 }
2291 }
2292
2293
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002294 switch (N.getOperand(1).getValueType()) {
2295 default: assert(0 && "Cannot store this type!");
2296 case MVT::i1:
2297 case MVT::i8: Opc = X86::MOV8mr; break;
2298 case MVT::i16: Opc = X86::MOV16mr; break;
2299 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002300 case MVT::f32: Opc = X86::FST32m; break;
2301 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002302 }
Chris Lattner11333092005-01-11 03:11:44 +00002303
2304 std::vector<std::pair<unsigned, unsigned> > RP;
2305 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2306 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2307 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2308 std::sort(RP.begin(), RP.end());
2309
2310 for (unsigned i = 0; i != 3; ++i)
2311 switch (RP[2-i].second) {
2312 default: assert(0 && "Unknown operand number!");
2313 case 0: Select(N.getOperand(0)); break;
2314 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002315 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002316 }
2317
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002318 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2319 return;
2320 }
2321 case ISD::ADJCALLSTACKDOWN:
2322 case ISD::ADJCALLSTACKUP:
2323 Select(N.getOperand(0));
2324 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2325
2326 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2327 X86::ADJCALLSTACKUP;
2328 BuildMI(BB, Opc, 1).addImm(Tmp1);
2329 return;
Chris Lattner989de032005-01-11 06:14:36 +00002330 case ISD::MEMSET: {
2331 Select(N.getOperand(0)); // Select the chain.
2332 unsigned Align =
2333 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2334 if (Align == 0) Align = 1;
2335
2336 // Turn the byte code into # iterations
2337 unsigned CountReg;
2338 unsigned Opcode;
2339 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2340 unsigned Val = ValC->getValue() & 255;
2341
2342 // If the value is a constant, then we can potentially use larger sets.
2343 switch (Align & 3) {
2344 case 2: // WORD aligned
2345 CountReg = MakeReg(MVT::i32);
2346 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2347 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2348 } else {
2349 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2350 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2351 }
2352 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2353 Opcode = X86::REP_STOSW;
2354 break;
2355 case 0: // DWORD aligned
2356 CountReg = MakeReg(MVT::i32);
2357 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2358 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2359 } else {
2360 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2361 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2362 }
2363 Val = (Val << 8) | Val;
2364 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
2365 Opcode = X86::REP_STOSD;
2366 break;
2367 default: // BYTE aligned
2368 CountReg = SelectExpr(Node->getOperand(3));
2369 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
2370 Opcode = X86::REP_STOSB;
2371 break;
2372 }
2373 } else {
2374 // If it's not a constant value we are storing, just fall back. We could
2375 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
2376 unsigned ValReg = SelectExpr(Node->getOperand(2));
2377 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
2378 CountReg = SelectExpr(Node->getOperand(3));
2379 Opcode = X86::REP_STOSB;
2380 }
2381
2382 // No matter what the alignment is, we put the source in ESI, the
2383 // destination in EDI, and the count in ECX.
2384 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2385 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2386 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2387 BuildMI(BB, Opcode, 0);
2388 return;
2389 }
Chris Lattner31805bf2005-01-11 06:19:26 +00002390 case ISD::MEMCPY:
2391 Select(N.getOperand(0)); // Select the chain.
2392 unsigned Align =
2393 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2394 if (Align == 0) Align = 1;
2395
2396 // Turn the byte code into # iterations
2397 unsigned CountReg;
2398 unsigned Opcode;
2399 switch (Align & 3) {
2400 case 2: // WORD aligned
2401 CountReg = MakeReg(MVT::i32);
2402 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2403 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2404 } else {
2405 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2406 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2407 }
2408 Opcode = X86::REP_MOVSW;
2409 break;
2410 case 0: // DWORD aligned
2411 CountReg = MakeReg(MVT::i32);
2412 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2413 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2414 } else {
2415 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2416 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2417 }
2418 Opcode = X86::REP_MOVSD;
2419 break;
2420 default: // BYTE aligned
2421 CountReg = SelectExpr(Node->getOperand(3));
2422 Opcode = X86::REP_MOVSB;
2423 break;
2424 }
2425
2426 // No matter what the alignment is, we put the source in ESI, the
2427 // destination in EDI, and the count in ECX.
2428 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2429 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
2430 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2431 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2432 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
2433 BuildMI(BB, Opcode, 0);
2434 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002435 }
2436 assert(0 && "Should not be reached!");
2437}
2438
2439
2440/// createX86PatternInstructionSelector - This pass converts an LLVM function
2441/// into a machine code representation using pattern matching and a machine
2442/// description file.
2443///
2444FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
2445 return new ISel(TM);
2446}