blob: cc22506295fef88c60b8551f3a0b7ed462d95609 [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>
Jeff Cohen603fea92005-01-12 04:29:05 +000030#include <algorithm>
Chris Lattner8acb1ba2005-01-07 07:49:41 +000031using namespace llvm;
32
33//===----------------------------------------------------------------------===//
34// X86TargetLowering - X86 Implementation of the TargetLowering interface
35namespace {
36 class X86TargetLowering : public TargetLowering {
37 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000038 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000039 public:
40 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
41 // Set up the TargetLowering object.
42 addRegisterClass(MVT::i8, X86::R8RegisterClass);
43 addRegisterClass(MVT::i16, X86::R16RegisterClass);
44 addRegisterClass(MVT::i32, X86::R32RegisterClass);
45 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
46
47 // FIXME: Eliminate these two classes when legalize can handle promotions
48 // well.
49 addRegisterClass(MVT::i1, X86::R8RegisterClass);
50 addRegisterClass(MVT::f32, X86::RFPRegisterClass);
51
52 computeRegisterProperties();
Chris Lattner795069d2005-01-11 05:57:36 +000053
Chris Lattner795069d2005-01-11 05:57:36 +000054 setOperationUnsupported(ISD::MEMMOVE, MVT::Other);
55
Chris Lattner8acb1ba2005-01-07 07:49:41 +000056 setOperationUnsupported(ISD::MUL, MVT::i8);
57 setOperationUnsupported(ISD::SELECT, MVT::i1);
58 setOperationUnsupported(ISD::SELECT, MVT::i8);
59
60 addLegalFPImmediate(+0.0); // FLD0
61 addLegalFPImmediate(+1.0); // FLD1
62 addLegalFPImmediate(-0.0); // FLD0/FCHS
63 addLegalFPImmediate(-1.0); // FLD1/FCHS
64 }
65
66 /// LowerArguments - This hook must be implemented to indicate how we should
67 /// lower the arguments for the specified function, into the specified DAG.
68 virtual std::vector<SDOperand>
69 LowerArguments(Function &F, SelectionDAG &DAG);
70
71 /// LowerCallTo - This hook lowers an abstract call to a function into an
72 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000073 virtual std::pair<SDOperand, SDOperand>
74 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
75 ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000076
77 virtual std::pair<SDOperand, SDOperand>
78 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
79
80 virtual std::pair<SDOperand,SDOperand>
81 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
82 const Type *ArgTy, SelectionDAG &DAG);
83
84 virtual std::pair<SDOperand, SDOperand>
85 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
86 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000087 };
88}
89
90
91std::vector<SDOperand>
92X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
93 std::vector<SDOperand> ArgValues;
94
95 // Add DAG nodes to load the arguments... On entry to a function on the X86,
96 // the stack frame looks like this:
97 //
98 // [ESP] -- return address
99 // [ESP + 4] -- first argument (leftmost lexically)
100 // [ESP + 8] -- second argument, if first argument is four bytes in size
101 // ...
102 //
103 MachineFunction &MF = DAG.getMachineFunction();
104 MachineFrameInfo *MFI = MF.getFrameInfo();
105
106 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
107 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I) {
108 MVT::ValueType ObjectVT = getValueType(I->getType());
109 unsigned ArgIncrement = 4;
110 unsigned ObjSize;
111 switch (ObjectVT) {
112 default: assert(0 && "Unhandled argument type!");
113 case MVT::i1:
114 case MVT::i8: ObjSize = 1; break;
115 case MVT::i16: ObjSize = 2; break;
116 case MVT::i32: ObjSize = 4; break;
117 case MVT::i64: ObjSize = ArgIncrement = 8; break;
118 case MVT::f32: ObjSize = 4; break;
119 case MVT::f64: ObjSize = ArgIncrement = 8; break;
120 }
121 // Create the frame index object for this incoming parameter...
122 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
123
124 // Create the SelectionDAG nodes corresponding to a load from this parameter
125 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
126
127 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
128 // dead loads.
129 SDOperand ArgValue;
130 if (!I->use_empty())
131 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
132 else {
133 if (MVT::isInteger(ObjectVT))
134 ArgValue = DAG.getConstant(0, ObjectVT);
135 else
136 ArgValue = DAG.getConstantFP(0, ObjectVT);
137 }
138 ArgValues.push_back(ArgValue);
139
140 ArgOffset += ArgIncrement; // Move on to the next argument...
141 }
142
143 // If the function takes variable number of arguments, make a frame index for
144 // the start of the first vararg value... for expansion of llvm.va_start.
145 if (F.isVarArg())
146 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000147 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000148 return ArgValues;
149}
150
Chris Lattner5188ad72005-01-08 19:28:19 +0000151std::pair<SDOperand, SDOperand>
152X86TargetLowering::LowerCallTo(SDOperand Chain,
153 const Type *RetTy, SDOperand Callee,
154 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000155 // Count how many bytes are to be pushed on the stack.
156 unsigned NumBytes = 0;
157
158 if (Args.empty()) {
159 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000160 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
161 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000162 } else {
163 for (unsigned i = 0, e = Args.size(); i != e; ++i)
164 switch (getValueType(Args[i].second)) {
165 default: assert(0 && "Unknown value type!");
166 case MVT::i1:
167 case MVT::i8:
168 case MVT::i16:
169 case MVT::i32:
170 case MVT::f32:
171 NumBytes += 4;
172 break;
173 case MVT::i64:
174 case MVT::f64:
175 NumBytes += 8;
176 break;
177 }
178
Chris Lattner5188ad72005-01-08 19:28:19 +0000179 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
180 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000181
182 // Arguments go on the stack in reverse order, as specified by the ABI.
183 unsigned ArgOffset = 0;
184 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32);
185 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
186 unsigned ArgReg;
187 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
188 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
189
190 switch (getValueType(Args[i].second)) {
191 default: assert(0 && "Unexpected ValueType for argument!");
192 case MVT::i1:
193 case MVT::i8:
194 case MVT::i16:
195 // Promote the integer to 32 bits. If the input type is signed use a
196 // sign extend, otherwise use a zero extend.
197 if (Args[i].second->isSigned())
198 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
199 else
200 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
201
202 // FALL THROUGH
203 case MVT::i32:
204 case MVT::f32:
205 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000206 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
207 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000208 ArgOffset += 4;
209 break;
210 case MVT::i64:
211 case MVT::f64:
212 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000213 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
214 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000215 ArgOffset += 8;
216 break;
217 }
218 }
219 }
220
221 std::vector<MVT::ValueType> RetVals;
222 MVT::ValueType RetTyVT = getValueType(RetTy);
223 if (RetTyVT != MVT::isVoid)
224 RetVals.push_back(RetTyVT);
225 RetVals.push_back(MVT::Other);
226
Chris Lattner5188ad72005-01-08 19:28:19 +0000227 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000228 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000229 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
230 DAG.getConstant(NumBytes, getPointerTy()));
231 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000232}
233
Chris Lattner14824582005-01-09 00:01:27 +0000234std::pair<SDOperand, SDOperand>
235X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
236 // vastart just returns the address of the VarArgsFrameIndex slot.
237 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
238}
239
240std::pair<SDOperand,SDOperand> X86TargetLowering::
241LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
242 const Type *ArgTy, SelectionDAG &DAG) {
243 MVT::ValueType ArgVT = getValueType(ArgTy);
244 SDOperand Result;
245 if (!isVANext) {
246 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
247 } else {
248 unsigned Amt;
249 if (ArgVT == MVT::i32)
250 Amt = 4;
251 else {
252 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
253 "Other types should have been promoted for varargs!");
254 Amt = 8;
255 }
256 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
257 DAG.getConstant(Amt, VAList.getValueType()));
258 }
259 return std::make_pair(Result, Chain);
260}
261
262
263std::pair<SDOperand, SDOperand> X86TargetLowering::
264LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
265 SelectionDAG &DAG) {
266 SDOperand Result;
267 if (Depth) // Depths > 0 not supported yet!
268 Result = DAG.getConstant(0, getPointerTy());
269 else {
270 if (ReturnAddrIndex == 0) {
271 // Set up a frame object for the return address.
272 MachineFunction &MF = DAG.getMachineFunction();
273 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
274 }
275
276 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
277
278 if (!isFrameAddress)
279 // Just load the return address
280 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI);
281 else
282 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
283 DAG.getConstant(4, MVT::i32));
284 }
285 return std::make_pair(Result, Chain);
286}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000287
288
289
290
291
292namespace {
293 Statistic<>
294 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
295
296 //===--------------------------------------------------------------------===//
297 /// ISel - X86 specific code to select X86 machine instructions for
298 /// SelectionDAG operations.
299 ///
300 class ISel : public SelectionDAGISel {
301 /// ContainsFPCode - Every instruction we select that uses or defines a FP
302 /// register should set this to true.
303 bool ContainsFPCode;
304
305 /// X86Lowering - This object fully describes how to lower LLVM code to an
306 /// X86-specific SelectionDAG.
307 X86TargetLowering X86Lowering;
308
Chris Lattner11333092005-01-11 03:11:44 +0000309 /// RegPressureMap - This keeps an approximate count of the number of
310 /// registers required to evaluate each node in the graph.
311 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000312
313 /// ExprMap - As shared expressions are codegen'd, we keep track of which
314 /// vreg the value is produced in, so we only emit one copy of each compiled
315 /// tree.
316 std::map<SDOperand, unsigned> ExprMap;
317 std::set<SDOperand> LoweredTokens;
318
319 public:
320 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
321 }
322
Chris Lattner11333092005-01-11 03:11:44 +0000323 unsigned getRegPressure(SDOperand O) {
324 return RegPressureMap[O.Val];
325 }
326 unsigned ComputeRegPressure(SDOperand O);
327
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000328 /// InstructionSelectBasicBlock - This callback is invoked by
329 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000330 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000331
Chris Lattnera5ade062005-01-11 21:19:59 +0000332 bool isFoldableLoad(SDOperand Op);
333 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
334
335
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000336 void EmitCMP(SDOperand LHS, SDOperand RHS);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000337 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000338 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
339 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000340 unsigned SelectExpr(SDOperand N);
341 bool SelectAddress(SDOperand N, X86AddressMode &AM);
342 void Select(SDOperand N);
343 };
344}
345
Chris Lattner7dbcb752005-01-12 04:21:28 +0000346/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
347/// when it has created a SelectionDAG for us to codegen.
348void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
349 // While we're doing this, keep track of whether we see any FP code for
350 // FP_REG_KILL insertion.
351 ContainsFPCode = false;
352
353 // Scan the PHI nodes that already are inserted into this basic block. If any
354 // of them is a PHI of a floating point value, we need to insert an
355 // FP_REG_KILL.
356 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
357 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
358 I != E; ++I) {
359 assert(I->getOpcode() == X86::PHI &&
360 "Isn't just PHI nodes?");
361 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
362 X86::RFPRegisterClass) {
363 ContainsFPCode = true;
364 break;
365 }
366 }
367
368 // Compute the RegPressureMap, which is an approximation for the number of
369 // registers required to compute each node.
370 ComputeRegPressure(DAG.getRoot());
371
372 // Codegen the basic block.
373 Select(DAG.getRoot());
374
375 // Finally, look at all of the successors of this block. If any contain a PHI
376 // node of FP type, we need to insert an FP_REG_KILL in this block.
377 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
378 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
379 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
380 I != E && I->getOpcode() == X86::PHI; ++I) {
381 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
382 X86::RFPRegisterClass) {
383 ContainsFPCode = true;
384 break;
385 }
386 }
387
388 // Insert FP_REG_KILL instructions into basic blocks that need them. This
389 // only occurs due to the floating point stackifier not being aggressive
390 // enough to handle arbitrary global stackification.
391 //
392 // Currently we insert an FP_REG_KILL instruction into each block that uses or
393 // defines a floating point virtual register.
394 //
395 // When the global register allocators (like linear scan) finally update live
396 // variable analysis, we can keep floating point values in registers across
397 // basic blocks. This will be a huge win, but we are waiting on the global
398 // allocators before we can do this.
399 //
400 if (ContainsFPCode && BB->succ_size()) {
401 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
402 ++NumFPKill;
403 }
404
405 // Clear state used for selection.
406 ExprMap.clear();
407 LoweredTokens.clear();
408 RegPressureMap.clear();
409}
410
411
Chris Lattner11333092005-01-11 03:11:44 +0000412// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
413// for the number of registers required to compute each node. This is basically
414// computing a generalized form of the Sethi-Ullman number for each node.
415unsigned ISel::ComputeRegPressure(SDOperand O) {
416 SDNode *N = O.Val;
417 unsigned &Result = RegPressureMap[N];
418 if (Result) return Result;
419
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000420 // FIXME: Should operations like CALL (which clobber lots o regs) have a
421 // higher fixed cost??
422
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000423 if (N->getNumOperands() == 0) {
424 Result = 1;
425 } else {
426 unsigned MaxRegUse = 0;
427 unsigned NumExtraMaxRegUsers = 0;
428 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
429 unsigned Regs;
430 if (N->getOperand(i).getOpcode() == ISD::Constant)
431 Regs = 0;
432 else
433 Regs = ComputeRegPressure(N->getOperand(i));
434 if (Regs > MaxRegUse) {
435 MaxRegUse = Regs;
436 NumExtraMaxRegUsers = 0;
437 } else if (Regs == MaxRegUse &&
438 N->getOperand(i).getValueType() != MVT::Other) {
439 ++NumExtraMaxRegUsers;
440 }
Chris Lattner11333092005-01-11 03:11:44 +0000441 }
Chris Lattner11333092005-01-11 03:11:44 +0000442
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000443 Result = MaxRegUse+NumExtraMaxRegUsers;
444 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000445
Chris Lattner837caa72005-01-11 23:21:30 +0000446 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000447 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000448}
449
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000450/// SelectAddress - Add the specified node to the specified addressing mode,
451/// returning true if it cannot be done.
452bool ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
453 switch (N.getOpcode()) {
454 default: break;
455 case ISD::FrameIndex:
456 if (AM.BaseType == X86AddressMode::RegBase && AM.Base.Reg == 0) {
457 AM.BaseType = X86AddressMode::FrameIndexBase;
458 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
459 return false;
460 }
461 break;
462 case ISD::GlobalAddress:
463 if (AM.GV == 0) {
464 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
465 return false;
466 }
467 break;
468 case ISD::Constant:
469 AM.Disp += cast<ConstantSDNode>(N)->getValue();
470 return false;
471 case ISD::SHL:
Chris Lattner2b937862005-01-12 07:33:20 +0000472 if (AM.IndexReg == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000473 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
474 unsigned Val = CN->getValue();
475 if (Val == 1 || Val == 2 || Val == 3) {
476 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000477 SDOperand ShVal = N.Val->getOperand(0);
478
479 // Okay, we know that we have a scale by now. However, if the scaled
480 // value is an add of something and a constant, we can fold the
481 // constant into the disp field here.
482 if (ShVal.Val->getOpcode() == ISD::ADD &&
483 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
484 AM.IndexReg = SelectExpr(ShVal.Val->getOperand(0));
485 ConstantSDNode *AddVal =
486 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
487 AM.Disp += AddVal->getValue() << Val;
488 } else {
489 AM.IndexReg = SelectExpr(ShVal);
490 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000491 return false;
492 }
493 }
494 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000495 case ISD::MUL:
496 // X*[3,5,9] -> X+X*[2,4,8]
497 if (AM.IndexReg == 0 && AM.BaseType == X86AddressMode::RegBase &&
498 AM.Base.Reg == 0)
499 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
500 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
501 AM.Scale = unsigned(CN->getValue())-1;
502
503 SDOperand MulVal = N.Val->getOperand(0);
504 unsigned Reg;
505
506 // Okay, we know that we have a scale by now. However, if the scaled
507 // value is an add of something and a constant, we can fold the
508 // constant into the disp field here.
509 if (MulVal.Val->getOpcode() == ISD::ADD &&
510 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
511 Reg = SelectExpr(MulVal.Val->getOperand(0));
512 ConstantSDNode *AddVal =
513 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
514 AM.Disp += AddVal->getValue() * CN->getValue();
515 } else {
516 Reg = SelectExpr(N.Val->getOperand(0));
517 }
518
519 AM.IndexReg = AM.Base.Reg = Reg;
520 return false;
521 }
522 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000523
524 case ISD::ADD: {
525 X86AddressMode Backup = AM;
526 if (!SelectAddress(N.Val->getOperand(0), AM) &&
527 !SelectAddress(N.Val->getOperand(1), AM))
528 return false;
529 AM = Backup;
Chris Lattner9bbd9922005-01-12 18:08:53 +0000530 if (!SelectAddress(N.Val->getOperand(1), AM) &&
531 !SelectAddress(N.Val->getOperand(0), AM))
532 return false;
533 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000534 break;
535 }
536 }
537
Chris Lattnera95589b2005-01-11 04:40:19 +0000538 // Is the base register already occupied?
539 if (AM.BaseType != X86AddressMode::RegBase || AM.Base.Reg) {
540 // If so, check to see if the scale index register is set.
541 if (AM.IndexReg == 0) {
542 AM.IndexReg = SelectExpr(N);
543 AM.Scale = 1;
544 return false;
545 }
546
547 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000548 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000549 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000550
551 // Default, generate it as a register.
552 AM.BaseType = X86AddressMode::RegBase;
553 AM.Base.Reg = SelectExpr(N);
554 return false;
555}
556
557/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
558/// assuming that the temporary registers are in the 8-bit register class.
559///
560/// Tmp1 = setcc1
561/// Tmp2 = setcc2
562/// DestReg = logicalop Tmp1, Tmp2
563///
564static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
565 unsigned SetCC2, unsigned LogicalOp,
566 unsigned DestReg) {
567 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
568 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
569 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
570 BuildMI(BB, SetCC1, 0, Tmp1);
571 BuildMI(BB, SetCC2, 0, Tmp2);
572 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
573}
574
575/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
576/// condition codes match the specified SetCCOpcode. Note that some conditions
577/// require multiple instructions to generate the correct value.
578static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
579 ISD::CondCode SetCCOpcode, bool isFP) {
580 unsigned Opc;
581 if (!isFP) {
582 switch (SetCCOpcode) {
583 default: assert(0 && "Illegal integer SetCC!");
584 case ISD::SETEQ: Opc = X86::SETEr; break;
585 case ISD::SETGT: Opc = X86::SETGr; break;
586 case ISD::SETGE: Opc = X86::SETGEr; break;
587 case ISD::SETLT: Opc = X86::SETLr; break;
588 case ISD::SETLE: Opc = X86::SETLEr; break;
589 case ISD::SETNE: Opc = X86::SETNEr; break;
590 case ISD::SETULT: Opc = X86::SETBr; break;
591 case ISD::SETUGT: Opc = X86::SETAr; break;
592 case ISD::SETULE: Opc = X86::SETBEr; break;
593 case ISD::SETUGE: Opc = X86::SETAEr; break;
594 }
595 } else {
596 // On a floating point condition, the flags are set as follows:
597 // ZF PF CF op
598 // 0 | 0 | 0 | X > Y
599 // 0 | 0 | 1 | X < Y
600 // 1 | 0 | 0 | X == Y
601 // 1 | 1 | 1 | unordered
602 //
603 switch (SetCCOpcode) {
604 default: assert(0 && "Invalid FP setcc!");
605 case ISD::SETUEQ:
606 case ISD::SETEQ:
607 Opc = X86::SETEr; // True if ZF = 1
608 break;
609 case ISD::SETOGT:
610 case ISD::SETGT:
611 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
612 break;
613 case ISD::SETOGE:
614 case ISD::SETGE:
615 Opc = X86::SETAEr; // True if CF = 0
616 break;
617 case ISD::SETULT:
618 case ISD::SETLT:
619 Opc = X86::SETBr; // True if CF = 1
620 break;
621 case ISD::SETULE:
622 case ISD::SETLE:
623 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
624 break;
625 case ISD::SETONE:
626 case ISD::SETNE:
627 Opc = X86::SETNEr; // True if ZF = 0
628 break;
629 case ISD::SETUO:
630 Opc = X86::SETPr; // True if PF = 1
631 break;
632 case ISD::SETO:
633 Opc = X86::SETNPr; // True if PF = 0
634 break;
635 case ISD::SETOEQ: // !PF & ZF
636 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
637 return;
638 case ISD::SETOLT: // !PF & CF
639 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
640 return;
641 case ISD::SETOLE: // !PF & (CF || ZF)
642 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
643 return;
644 case ISD::SETUGT: // PF | (!ZF & !CF)
645 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
646 return;
647 case ISD::SETUGE: // PF | !CF
648 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
649 return;
650 case ISD::SETUNE: // PF | !ZF
651 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
652 return;
653 }
654 }
655 BuildMI(BB, Opc, 0, DestReg);
656}
657
658
659/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
660/// the Dest block if the Cond condition is true. If we cannot fold this
661/// condition into the branch, return true.
662///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000663bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
664 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000665 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
666 // B) using two conditional branches instead of one condbr, two setcc's, and
667 // an or.
668 if ((Cond.getOpcode() == ISD::OR ||
669 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
670 // And and or set the flags for us, so there is no need to emit a TST of the
671 // result. It is only safe to do this if there is only a single use of the
672 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000673 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000674 SelectExpr(Cond);
675 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
676 return false;
677 }
678
679 // Codegen br not C -> JE.
680 if (Cond.getOpcode() == ISD::XOR)
681 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
682 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000683 unsigned CondR;
684 if (getRegPressure(Chain) > getRegPressure(Cond)) {
685 Select(Chain);
686 CondR = SelectExpr(Cond.Val->getOperand(0));
687 } else {
688 CondR = SelectExpr(Cond.Val->getOperand(0));
689 Select(Chain);
690 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000691 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
692 BuildMI(BB, X86::JE, 1).addMBB(Dest);
693 return false;
694 }
695
696 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
697 if (SetCC == 0)
698 return true; // Can only handle simple setcc's so far.
699
700 unsigned Opc;
701
702 // Handle integer conditions first.
703 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
704 switch (SetCC->getCondition()) {
705 default: assert(0 && "Illegal integer SetCC!");
706 case ISD::SETEQ: Opc = X86::JE; break;
707 case ISD::SETGT: Opc = X86::JG; break;
708 case ISD::SETGE: Opc = X86::JGE; break;
709 case ISD::SETLT: Opc = X86::JL; break;
710 case ISD::SETLE: Opc = X86::JLE; break;
711 case ISD::SETNE: Opc = X86::JNE; break;
712 case ISD::SETULT: Opc = X86::JB; break;
713 case ISD::SETUGT: Opc = X86::JA; break;
714 case ISD::SETULE: Opc = X86::JBE; break;
715 case ISD::SETUGE: Opc = X86::JAE; break;
716 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000717 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000718 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
719 BuildMI(BB, Opc, 1).addMBB(Dest);
720 return false;
721 }
722
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000723 unsigned Opc2 = 0; // Second branch if needed.
724
725 // On a floating point condition, the flags are set as follows:
726 // ZF PF CF op
727 // 0 | 0 | 0 | X > Y
728 // 0 | 0 | 1 | X < Y
729 // 1 | 0 | 0 | X == Y
730 // 1 | 1 | 1 | unordered
731 //
732 switch (SetCC->getCondition()) {
733 default: assert(0 && "Invalid FP setcc!");
734 case ISD::SETUEQ:
735 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
736 case ISD::SETOGT:
737 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
738 case ISD::SETOGE:
739 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
740 case ISD::SETULT:
741 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
742 case ISD::SETULE:
743 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
744 case ISD::SETONE:
745 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
746 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
747 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
748 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
749 Opc = X86::JA; // ZF = 0 & CF = 0
750 Opc2 = X86::JP; // PF = 1
751 break;
752 case ISD::SETUGE: // PF = 1 | CF = 0
753 Opc = X86::JAE; // CF = 0
754 Opc2 = X86::JP; // PF = 1
755 break;
756 case ISD::SETUNE: // PF = 1 | ZF = 0
757 Opc = X86::JNE; // ZF = 0
758 Opc2 = X86::JP; // PF = 1
759 break;
760 case ISD::SETOEQ: // PF = 0 & ZF = 1
761 //X86::JNP, X86::JE
762 //X86::AND8rr
763 return true; // FIXME: Emit more efficient code for this branch.
764 case ISD::SETOLT: // PF = 0 & CF = 1
765 //X86::JNP, X86::JB
766 //X86::AND8rr
767 return true; // FIXME: Emit more efficient code for this branch.
768 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
769 //X86::JNP, X86::JBE
770 //X86::AND8rr
771 return true; // FIXME: Emit more efficient code for this branch.
772 }
773
Chris Lattner6c07aee2005-01-11 04:06:27 +0000774 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000775 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
776 BuildMI(BB, Opc, 1).addMBB(Dest);
777 if (Opc2)
778 BuildMI(BB, Opc2, 1).addMBB(Dest);
779 return false;
780}
781
Chris Lattner24aad1b2005-01-10 22:10:13 +0000782/// EmitSelectCC - Emit code into BB that performs a select operation between
783/// the two registers RTrue and RFalse, generating a result into RDest. Return
784/// true if the fold cannot be performed.
785///
786void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
787 unsigned RTrue, unsigned RFalse, unsigned RDest) {
788 enum Condition {
789 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
790 NOT_SET
791 } CondCode = NOT_SET;
792
793 static const unsigned CMOVTAB16[] = {
794 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
795 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
796 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
797 };
798 static const unsigned CMOVTAB32[] = {
799 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
800 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
801 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
802 };
803 static const unsigned CMOVTABFP[] = {
804 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
805 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
806 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
807 };
808
809 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
810 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
811 switch (SetCC->getCondition()) {
812 default: assert(0 && "Unknown integer comparison!");
813 case ISD::SETEQ: CondCode = EQ; break;
814 case ISD::SETGT: CondCode = GT; break;
815 case ISD::SETGE: CondCode = GE; break;
816 case ISD::SETLT: CondCode = LT; break;
817 case ISD::SETLE: CondCode = LE; break;
818 case ISD::SETNE: CondCode = NE; break;
819 case ISD::SETULT: CondCode = B; break;
820 case ISD::SETUGT: CondCode = A; break;
821 case ISD::SETULE: CondCode = BE; break;
822 case ISD::SETUGE: CondCode = AE; break;
823 }
824 } else {
825 // On a floating point condition, the flags are set as follows:
826 // ZF PF CF op
827 // 0 | 0 | 0 | X > Y
828 // 0 | 0 | 1 | X < Y
829 // 1 | 0 | 0 | X == Y
830 // 1 | 1 | 1 | unordered
831 //
832 switch (SetCC->getCondition()) {
833 default: assert(0 && "Unknown FP comparison!");
834 case ISD::SETUEQ:
835 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
836 case ISD::SETOGT:
837 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
838 case ISD::SETOGE:
839 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
840 case ISD::SETULT:
841 case ISD::SETLT: CondCode = B; break; // True if CF = 1
842 case ISD::SETULE:
843 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
844 case ISD::SETONE:
845 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
846 case ISD::SETUO: CondCode = P; break; // True if PF = 1
847 case ISD::SETO: CondCode = NP; break; // True if PF = 0
848 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
849 case ISD::SETUGE: // PF = 1 | CF = 0
850 case ISD::SETUNE: // PF = 1 | ZF = 0
851 case ISD::SETOEQ: // PF = 0 & ZF = 1
852 case ISD::SETOLT: // PF = 0 & CF = 1
853 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
854 // We cannot emit this comparison as a single cmov.
855 break;
856 }
857 }
858 }
859
860 unsigned Opc = 0;
861 if (CondCode != NOT_SET) {
862 switch (SVT) {
863 default: assert(0 && "Cannot select this type!");
864 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
865 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
866 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000867 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000868 }
869 }
870
871 // Finally, if we weren't able to fold this, just emit the condition and test
872 // it.
873 if (CondCode == NOT_SET || Opc == 0) {
874 // Get the condition into the zero flag.
875 unsigned CondReg = SelectExpr(Cond);
876 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
877
878 switch (SVT) {
879 default: assert(0 && "Cannot select this type!");
880 case MVT::i16: Opc = X86::CMOVE16rr; break;
881 case MVT::i32: Opc = X86::CMOVE32rr; break;
882 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000883 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000884 }
885 } else {
886 // FIXME: CMP R, 0 -> TEST R, R
887 EmitCMP(Cond.getOperand(0), Cond.getOperand(1));
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000888 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000889 }
890 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
891}
892
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000893void ISel::EmitCMP(SDOperand LHS, SDOperand RHS) {
Chris Lattner11333092005-01-11 03:11:44 +0000894 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000895 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
896 Opc = 0;
Chris Lattneref6806c2005-01-12 02:02:48 +0000897 if (isFoldableLoad(LHS)) {
898 switch (RHS.getValueType()) {
899 default: break;
900 case MVT::i1:
901 case MVT::i8: Opc = X86::CMP8mi; break;
902 case MVT::i16: Opc = X86::CMP16mi; break;
903 case MVT::i32: Opc = X86::CMP32mi; break;
904 }
905 if (Opc) {
906 X86AddressMode AM;
907 EmitFoldedLoad(LHS, AM);
908 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
909 return;
910 }
911 }
912
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000913 switch (RHS.getValueType()) {
914 default: break;
915 case MVT::i1:
916 case MVT::i8: Opc = X86::CMP8ri; break;
917 case MVT::i16: Opc = X86::CMP16ri; break;
918 case MVT::i32: Opc = X86::CMP32ri; break;
919 }
920 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +0000921 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000922 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
923 return;
924 }
925 }
926
Chris Lattneref6806c2005-01-12 02:02:48 +0000927 Opc = 0;
928 if (isFoldableLoad(LHS)) {
929 switch (RHS.getValueType()) {
930 default: break;
931 case MVT::i1:
932 case MVT::i8: Opc = X86::CMP8mr; break;
933 case MVT::i16: Opc = X86::CMP16mr; break;
934 case MVT::i32: Opc = X86::CMP32mr; break;
935 }
936 if (Opc) {
937 X86AddressMode AM;
938 unsigned Reg;
939 if (getRegPressure(LHS) > getRegPressure(RHS)) {
940 EmitFoldedLoad(LHS, AM);
941 Reg = SelectExpr(RHS);
942 } else {
943 Reg = SelectExpr(RHS);
944 EmitFoldedLoad(LHS, AM);
945 }
946 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
947 return;
948 }
949 }
950
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000951 switch (LHS.getValueType()) {
952 default: assert(0 && "Cannot compare this value!");
953 case MVT::i1:
954 case MVT::i8: Opc = X86::CMP8rr; break;
955 case MVT::i16: Opc = X86::CMP16rr; break;
956 case MVT::i32: Opc = X86::CMP32rr; break;
957 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000958 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000959 }
Chris Lattner11333092005-01-11 03:11:44 +0000960 unsigned Tmp1, Tmp2;
961 if (getRegPressure(LHS) > getRegPressure(RHS)) {
962 Tmp1 = SelectExpr(LHS);
963 Tmp2 = SelectExpr(RHS);
964 } else {
965 Tmp2 = SelectExpr(RHS);
966 Tmp1 = SelectExpr(LHS);
967 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000968 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
969}
970
Chris Lattnera5ade062005-01-11 21:19:59 +0000971/// isFoldableLoad - Return true if this is a load instruction that can safely
972/// be folded into an operation that uses it.
973bool ISel::isFoldableLoad(SDOperand Op) {
974 if (Op.getOpcode() != ISD::LOAD ||
975 // FIXME: currently can't fold constant pool indexes.
976 isa<ConstantPoolSDNode>(Op.getOperand(1)))
977 return false;
978
979 // If this load has already been emitted, we clearly can't fold it.
980 if (ExprMap.count(Op)) return false;
981
Chris Lattnera0bb6922005-01-12 18:38:26 +0000982 // Finally, there can only be one use of its value.
983 return Op.Val->hasNUsesOfValue(1, 0);
Chris Lattnera5ade062005-01-11 21:19:59 +0000984}
985
986/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
987/// and compute the address being loaded into AM.
988void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
989 SDOperand Chain = Op.getOperand(0);
990 SDOperand Address = Op.getOperand(1);
991 if (getRegPressure(Chain) > getRegPressure(Address)) {
992 Select(Chain);
993 SelectAddress(Address, AM);
994 } else {
995 SelectAddress(Address, AM);
996 Select(Chain);
997 }
998
999 // The chain for this load is now lowered.
1000 LoweredTokens.insert(SDOperand(Op.Val, 1));
1001 ExprMap[SDOperand(Op.Val, 1)] = 1;
1002}
1003
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001004unsigned ISel::SelectExpr(SDOperand N) {
1005 unsigned Result;
1006 unsigned Tmp1, Tmp2, Tmp3;
1007 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001008 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001009 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001010
Chris Lattner590d8002005-01-09 18:52:44 +00001011 if (Node->getOpcode() == ISD::CopyFromReg)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001012 // Just use the specified register as our input.
Chris Lattner590d8002005-01-09 18:52:44 +00001013 return dyn_cast<CopyRegSDNode>(Node)->getReg();
Chris Lattnera5ade062005-01-11 21:19:59 +00001014
1015 unsigned &Reg = ExprMap[N];
1016 if (Reg) return Reg;
1017
1018 if (N.getOpcode() != ISD::CALL)
1019 Reg = Result = (N.getValueType() != MVT::Other) ?
1020 MakeReg(N.getValueType()) : 1;
1021 else {
1022 // If this is a call instruction, make sure to prepare ALL of the result
1023 // values as well as the chain.
1024 if (Node->getNumValues() == 1)
1025 Reg = Result = 1; // Void call, just a chain.
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001026 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001027 Result = MakeReg(Node->getValueType(0));
1028 ExprMap[N.getValue(0)] = Result;
1029 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1030 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1031 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001032 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001033 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001034
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001035 switch (N.getOpcode()) {
1036 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001037 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001038 assert(0 && "Node not handled!\n");
1039 case ISD::FrameIndex:
1040 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1041 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1042 return Result;
1043 case ISD::ConstantPool:
1044 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1045 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1046 return Result;
1047 case ISD::ConstantFP:
1048 ContainsFPCode = true;
1049 Tmp1 = Result; // Intermediate Register
1050 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1051 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1052 Tmp1 = MakeReg(MVT::f64);
1053
1054 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1055 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1056 BuildMI(BB, X86::FLD0, 0, Tmp1);
1057 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1058 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1059 BuildMI(BB, X86::FLD1, 0, Tmp1);
1060 else
1061 assert(0 && "Unexpected constant!");
1062 if (Tmp1 != Result)
1063 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1064 return Result;
1065 case ISD::Constant:
1066 switch (N.getValueType()) {
1067 default: assert(0 && "Cannot use constants of this type!");
1068 case MVT::i1:
1069 case MVT::i8: Opc = X86::MOV8ri; break;
1070 case MVT::i16: Opc = X86::MOV16ri; break;
1071 case MVT::i32: Opc = X86::MOV32ri; break;
1072 }
1073 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1074 return Result;
1075 case ISD::GlobalAddress: {
1076 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1077 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1078 return Result;
1079 }
1080 case ISD::ExternalSymbol: {
1081 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1082 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1083 return Result;
1084 }
1085 case ISD::FP_EXTEND:
1086 Tmp1 = SelectExpr(N.getOperand(0));
1087 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001088 return Result;
1089 case ISD::ZERO_EXTEND: {
1090 int DestIs16 = N.getValueType() == MVT::i16;
1091 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001092
1093 // FIXME: This hack is here for zero extension casts from bool to i8. This
1094 // would not be needed if bools were promoted by Legalize.
1095 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001096 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001097 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1098 return Result;
1099 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001100
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001101 if (isFoldableLoad(N.getOperand(0))) {
1102 static const unsigned Opc[3] = {
1103 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1104 };
1105
1106 X86AddressMode AM;
1107 EmitFoldedLoad(N.getOperand(0), AM);
1108 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1109
1110 return Result;
1111 }
1112
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001113 static const unsigned Opc[3] = {
1114 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1115 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001116 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001117 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1118 return Result;
1119 }
1120 case ISD::SIGN_EXTEND: {
1121 int DestIs16 = N.getValueType() == MVT::i16;
1122 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1123
Chris Lattner590d8002005-01-09 18:52:44 +00001124 // FIXME: Legalize should promote bools to i8!
1125 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1126 "Sign extend from bool not implemented!");
1127
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001128 if (isFoldableLoad(N.getOperand(0))) {
1129 static const unsigned Opc[3] = {
1130 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1131 };
1132
1133 X86AddressMode AM;
1134 EmitFoldedLoad(N.getOperand(0), AM);
1135 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1136 return Result;
1137 }
1138
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001139 static const unsigned Opc[3] = {
1140 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1141 };
1142 Tmp1 = SelectExpr(N.getOperand(0));
1143 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1144 return Result;
1145 }
1146 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001147 // Fold TRUNCATE (LOAD P) into a smaller load from P.
1148 if (isFoldableLoad(N.getOperand(0))) {
1149 switch (N.getValueType()) {
1150 default: assert(0 && "Unknown truncate!");
1151 case MVT::i1:
1152 case MVT::i8: Opc = X86::MOV8rm; break;
1153 case MVT::i16: Opc = X86::MOV16rm; break;
1154 }
1155 X86AddressMode AM;
1156 EmitFoldedLoad(N.getOperand(0), AM);
1157 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1158 return Result;
1159 }
1160
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001161 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1162 // a move out of AX or AL.
1163 switch (N.getOperand(0).getValueType()) {
1164 default: assert(0 && "Unknown truncate!");
1165 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1166 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1167 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1168 }
1169 Tmp1 = SelectExpr(N.getOperand(0));
1170 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1171
1172 switch (N.getValueType()) {
1173 default: assert(0 && "Unknown truncate!");
1174 case MVT::i1:
1175 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1176 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1177 }
1178 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1179 return Result;
1180
1181 case ISD::FP_ROUND:
1182 // Truncate from double to float by storing to memory as float,
1183 // then reading it back into a register.
1184
1185 // Create as stack slot to use.
Chris Lattner590d8002005-01-09 18:52:44 +00001186 // FIXME: This should automatically be made by the Legalizer!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001187 Tmp1 = TLI.getTargetData().getFloatAlignment();
1188 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
1189
1190 // Codegen the input.
1191 Tmp1 = SelectExpr(N.getOperand(0));
1192
1193 // Emit the store, then the reload.
1194 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
1195 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001196 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00001197
1198 case ISD::SINT_TO_FP:
1199 case ISD::UINT_TO_FP: {
1200 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001201 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001202
1203 // Promote the integer to a type supported by FLD. We do this because there
1204 // are no unsigned FLD instructions, so we must promote an unsigned value to
1205 // a larger signed value, then use FLD on the larger value.
1206 //
1207 MVT::ValueType PromoteType = MVT::Other;
1208 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1209 unsigned PromoteOpcode = 0;
1210 unsigned RealDestReg = Result;
1211 switch (SrcTy) {
1212 case MVT::i1:
1213 case MVT::i8:
1214 // We don't have the facilities for directly loading byte sized data from
1215 // memory (even signed). Promote it to 16 bits.
1216 PromoteType = MVT::i16;
1217 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1218 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1219 break;
1220 case MVT::i16:
1221 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1222 PromoteType = MVT::i32;
1223 PromoteOpcode = X86::MOVZX32rr16;
1224 }
1225 break;
1226 default:
1227 // Don't fild into the real destination.
1228 if (Node->getOpcode() == ISD::UINT_TO_FP)
1229 Result = MakeReg(Node->getValueType(0));
1230 break;
1231 }
1232
1233 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1234
1235 if (PromoteType != MVT::Other) {
1236 Tmp2 = MakeReg(PromoteType);
1237 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1238 SrcTy = PromoteType;
1239 Tmp1 = Tmp2;
1240 }
1241
1242 // Spill the integer to memory and reload it from there.
1243 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1244 MachineFunction *F = BB->getParent();
1245 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1246
1247 switch (SrcTy) {
1248 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001249 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001250 // FIXME: this won't work for cast [u]long to FP
1251 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1252 FrameIdx).addReg(Tmp1);
1253 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1254 FrameIdx, 4).addReg(Tmp1+1);
1255 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1256 break;
1257 case MVT::i32:
1258 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1259 FrameIdx).addReg(Tmp1);
1260 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1261 break;
1262 case MVT::i16:
1263 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1264 FrameIdx).addReg(Tmp1);
1265 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1266 break;
1267 default: break; // No promotion required.
1268 }
1269
Chris Lattner085c9952005-01-12 04:00:00 +00001270 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001271 // If this is a cast from uint -> double, we need to be careful when if
1272 // the "sign" bit is set. If so, we don't want to make a negative number,
1273 // we want to make a positive number. Emit code to add an offset if the
1274 // sign bit is set.
1275
1276 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1277 unsigned IsNeg = MakeReg(MVT::i32);
1278 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1279
1280 // Create a CP value that has the offset in one word and 0 in the other.
1281 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1282 0x4f80000000000000ULL);
1283 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1284 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1285 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1286
1287 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1288 // We need special handling for unsigned 64-bit integer sources. If the
1289 // input number has the "sign bit" set, then we loaded it incorrectly as a
1290 // negative 64-bit number. In this case, add an offset value.
1291
1292 // Emit a test instruction to see if the dynamic input value was signed.
1293 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1294
1295 // If the sign bit is set, get a pointer to an offset, otherwise get a
1296 // pointer to a zero.
1297 MachineConstantPool *CP = F->getConstantPool();
1298 unsigned Zero = MakeReg(MVT::i32);
1299 Constant *Null = Constant::getNullValue(Type::UIntTy);
1300 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1301 CP->getConstantPoolIndex(Null));
1302 unsigned Offset = MakeReg(MVT::i32);
1303 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1304
1305 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1306 CP->getConstantPoolIndex(OffsetCst));
1307 unsigned Addr = MakeReg(MVT::i32);
1308 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1309
1310 // Load the constant for an add. FIXME: this could make an 'fadd' that
1311 // reads directly from memory, but we don't support these yet.
1312 unsigned ConstReg = MakeReg(MVT::f64);
1313 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1314
1315 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1316 }
1317 return RealDestReg;
1318 }
1319 case ISD::FP_TO_SINT:
1320 case ISD::FP_TO_UINT: {
1321 // FIXME: Most of this grunt work should be done by legalize!
1322 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1323
1324 // Change the floating point control register to use "round towards zero"
1325 // mode when truncating to an integer value.
1326 //
1327 MachineFunction *F = BB->getParent();
1328 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1329 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1330
1331 // Load the old value of the high byte of the control word...
1332 unsigned HighPartOfCW = MakeReg(MVT::i8);
1333 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1334 CWFrameIdx, 1);
1335
1336 // Set the high part to be round to zero...
1337 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1338 CWFrameIdx, 1).addImm(12);
1339
1340 // Reload the modified control word now...
1341 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1342
1343 // Restore the memory image of control word to original value
1344 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1345 CWFrameIdx, 1).addReg(HighPartOfCW);
1346
1347 // We don't have the facilities for directly storing byte sized data to
1348 // memory. Promote it to 16 bits. We also must promote unsigned values to
1349 // larger classes because we only have signed FP stores.
1350 MVT::ValueType StoreClass = Node->getValueType(0);
1351 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1352 switch (StoreClass) {
1353 case MVT::i8: StoreClass = MVT::i16; break;
1354 case MVT::i16: StoreClass = MVT::i32; break;
1355 case MVT::i32: StoreClass = MVT::i64; break;
1356 // The following treatment of cLong may not be perfectly right,
1357 // but it survives chains of casts of the form
1358 // double->ulong->double.
1359 case MVT::i64: StoreClass = MVT::i64; break;
1360 default: assert(0 && "Unknown store class!");
1361 }
1362
1363 // Spill the integer to memory and reload it from there.
1364 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1365 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1366
1367 switch (StoreClass) {
1368 default: assert(0 && "Unknown store class!");
1369 case MVT::i16:
1370 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1371 break;
1372 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001373 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001374 break;
1375 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001376 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001377 break;
1378 }
1379
1380 switch (Node->getValueType(0)) {
1381 default:
1382 assert(0 && "Unknown integer type!");
1383 case MVT::i64:
1384 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001385 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001386 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1387 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1388 case MVT::i32:
1389 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1390 break;
1391 case MVT::i16:
1392 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1393 break;
1394 case MVT::i8:
1395 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1396 break;
1397 }
1398
1399 // Reload the original control word now.
1400 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1401 return Result;
1402 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001403 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001404 Op0 = N.getOperand(0);
1405 Op1 = N.getOperand(1);
1406
1407 if (isFoldableLoad(Op0))
1408 std::swap(Op0, Op1);
1409
1410 if (isFoldableLoad(Op1)) {
1411 switch (N.getValueType()) {
1412 default: assert(0 && "Cannot add this type!");
1413 case MVT::i1:
1414 case MVT::i8: Opc = X86::ADD8rm; break;
1415 case MVT::i16: Opc = X86::ADD16rm; break;
1416 case MVT::i32: Opc = X86::ADD32rm; break;
1417 case MVT::f32: Opc = X86::FADD32m; break;
1418 case MVT::f64: Opc = X86::FADD64m; break;
1419 }
1420 X86AddressMode AM;
1421 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1422 Tmp1 = SelectExpr(Op0);
1423 EmitFoldedLoad(Op1, AM);
1424 } else {
1425 EmitFoldedLoad(Op1, AM);
1426 Tmp1 = SelectExpr(Op0);
1427 }
1428 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1429 return Result;
1430 }
1431
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001432 // See if we can codegen this as an LEA to fold operations together.
1433 if (N.getValueType() == MVT::i32) {
1434 X86AddressMode AM;
Chris Lattnera5ade062005-01-11 21:19:59 +00001435 if (!SelectAddress(Op0, AM) && !SelectAddress(Op1, AM)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001436 // If this is not just an add, emit the LEA. For a simple add (like
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001437 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001438 // leave this as LEA, then peephole it to 'ADD' after two address elim
1439 // happens.
1440 if (AM.Scale != 1 || AM.BaseType == X86AddressMode::FrameIndexBase ||
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001441 AM.GV || (AM.Base.Reg && AM.IndexReg && AM.Disp)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001442 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1443 return Result;
1444 }
1445 }
1446 }
Chris Lattner11333092005-01-11 03:11:44 +00001447
Chris Lattnera5ade062005-01-11 21:19:59 +00001448 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001449 Opc = 0;
1450 if (CN->getValue() == 1) { // add X, 1 -> inc X
1451 switch (N.getValueType()) {
1452 default: assert(0 && "Cannot integer add this type!");
1453 case MVT::i8: Opc = X86::INC8r; break;
1454 case MVT::i16: Opc = X86::INC16r; break;
1455 case MVT::i32: Opc = X86::INC32r; break;
1456 }
1457 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1458 switch (N.getValueType()) {
1459 default: assert(0 && "Cannot integer add this type!");
1460 case MVT::i8: Opc = X86::DEC8r; break;
1461 case MVT::i16: Opc = X86::DEC16r; break;
1462 case MVT::i32: Opc = X86::DEC32r; break;
1463 }
1464 }
1465
1466 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001467 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001468 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1469 return Result;
1470 }
1471
1472 switch (N.getValueType()) {
1473 default: assert(0 && "Cannot add this type!");
1474 case MVT::i8: Opc = X86::ADD8ri; break;
1475 case MVT::i16: Opc = X86::ADD16ri; break;
1476 case MVT::i32: Opc = X86::ADD32ri; break;
1477 }
1478 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001479 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001480 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1481 return Result;
1482 }
1483 }
1484
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001485 switch (N.getValueType()) {
1486 default: assert(0 && "Cannot add this type!");
1487 case MVT::i8: Opc = X86::ADD8rr; break;
1488 case MVT::i16: Opc = X86::ADD16rr; break;
1489 case MVT::i32: Opc = X86::ADD32rr; break;
1490 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001491 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001492 }
Chris Lattner11333092005-01-11 03:11:44 +00001493
Chris Lattnera5ade062005-01-11 21:19:59 +00001494 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1495 Tmp1 = SelectExpr(Op0);
1496 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001497 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001498 Tmp2 = SelectExpr(Op1);
1499 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001500 }
1501
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001502 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1503 return Result;
1504 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001505 case ISD::MUL:
1506 case ISD::AND:
1507 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001508 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001509 static const unsigned SUBTab[] = {
1510 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1511 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1512 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1513 };
1514 static const unsigned MULTab[] = {
1515 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1516 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1517 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1518 };
1519 static const unsigned ANDTab[] = {
1520 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1521 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1522 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1523 };
1524 static const unsigned ORTab[] = {
1525 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1526 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1527 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1528 };
1529 static const unsigned XORTab[] = {
1530 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1531 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1532 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1533 };
1534
1535 Op0 = Node->getOperand(0);
1536 Op1 = Node->getOperand(1);
1537
1538 if (Node->getOpcode() == ISD::SUB && MVT::isInteger(N.getValueType()))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001539 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1540 if (CN->isNullValue()) { // 0 - N -> neg N
1541 switch (N.getValueType()) {
1542 default: assert(0 && "Cannot sub this type!");
1543 case MVT::i1:
1544 case MVT::i8: Opc = X86::NEG8r; break;
1545 case MVT::i16: Opc = X86::NEG16r; break;
1546 case MVT::i32: Opc = X86::NEG32r; break;
1547 }
1548 Tmp1 = SelectExpr(N.getOperand(1));
1549 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1550 return Result;
1551 }
1552
Chris Lattnera5ade062005-01-11 21:19:59 +00001553 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1554 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001555 switch (N.getValueType()) {
1556 default: assert(0 && "Cannot add this type!");
1557 case MVT::i1:
1558 case MVT::i8: Opc = X86::NOT8r; break;
1559 case MVT::i16: Opc = X86::NOT16r; break;
1560 case MVT::i32: Opc = X86::NOT32r; break;
1561 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001562 Tmp1 = SelectExpr(Op0);
Chris Lattnerd4dab922005-01-11 04:31:30 +00001563 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1564 return Result;
1565 }
1566
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001567 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001568 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001569 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001570 case MVT::i8: Opc = 0; break;
1571 case MVT::i16: Opc = 1; break;
1572 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001573 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001574 switch (Node->getOpcode()) {
1575 default: assert(0 && "Unreachable!");
1576 case ISD::SUB: Opc = SUBTab[Opc]; break;
1577 case ISD::MUL: Opc = MULTab[Opc]; break;
1578 case ISD::AND: Opc = ANDTab[Opc]; break;
1579 case ISD::OR: Opc = ORTab[Opc]; break;
1580 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001581 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001582 if (Opc) { // Can't fold MUL:i8 R, imm
1583 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001584 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1585 return Result;
1586 }
1587 }
Chris Lattner11333092005-01-11 03:11:44 +00001588
Chris Lattnera5ade062005-01-11 21:19:59 +00001589 if (isFoldableLoad(Op0))
1590 if (Node->getOpcode() != ISD::SUB) {
1591 std::swap(Op0, Op1);
1592 } else {
1593 // Emit 'reverse' subract, with a memory operand.
1594 switch (N.getValueType()) {
1595 default: Opc = 0; break;
1596 case MVT::f32: Opc = X86::FSUBR32m; break;
1597 case MVT::f64: Opc = X86::FSUBR64m; break;
1598 }
1599 if (Opc) {
1600 X86AddressMode AM;
1601 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1602 EmitFoldedLoad(Op0, AM);
1603 Tmp1 = SelectExpr(Op1);
1604 } else {
1605 Tmp1 = SelectExpr(Op1);
1606 EmitFoldedLoad(Op0, AM);
1607 }
1608 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1609 return Result;
1610 }
1611 }
1612
1613 if (isFoldableLoad(Op1)) {
1614 switch (N.getValueType()) {
1615 default: assert(0 && "Cannot operate on this type!");
1616 case MVT::i1:
1617 case MVT::i8: Opc = 5; break;
1618 case MVT::i16: Opc = 6; break;
1619 case MVT::i32: Opc = 7; break;
1620 case MVT::f32: Opc = 8; break;
1621 case MVT::f64: Opc = 9; break;
1622 }
1623 switch (Node->getOpcode()) {
1624 default: assert(0 && "Unreachable!");
1625 case ISD::SUB: Opc = SUBTab[Opc]; break;
1626 case ISD::MUL: Opc = MULTab[Opc]; break;
1627 case ISD::AND: Opc = ANDTab[Opc]; break;
1628 case ISD::OR: Opc = ORTab[Opc]; break;
1629 case ISD::XOR: Opc = XORTab[Opc]; break;
1630 }
1631
1632 X86AddressMode AM;
1633 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1634 Tmp1 = SelectExpr(Op0);
1635 EmitFoldedLoad(Op1, AM);
1636 } else {
1637 EmitFoldedLoad(Op1, AM);
1638 Tmp1 = SelectExpr(Op0);
1639 }
1640 if (Opc) {
1641 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1642 } else {
1643 assert(Node->getOpcode() == ISD::MUL &&
1644 N.getValueType() == MVT::i8 && "Unexpected situation!");
1645 // Must use the MUL instruction, which forces use of AL.
1646 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1647 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1648 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1649 }
1650 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001651 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001652
1653 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1654 Tmp1 = SelectExpr(Op0);
1655 Tmp2 = SelectExpr(Op1);
1656 } else {
1657 Tmp2 = SelectExpr(Op1);
1658 Tmp1 = SelectExpr(Op0);
1659 }
1660
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001661 switch (N.getValueType()) {
1662 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001663 case MVT::i1:
1664 case MVT::i8: Opc = 10; break;
1665 case MVT::i16: Opc = 11; break;
1666 case MVT::i32: Opc = 12; break;
1667 case MVT::f32: Opc = 13; break;
1668 case MVT::f64: Opc = 14; break;
1669 }
1670 switch (Node->getOpcode()) {
1671 default: assert(0 && "Unreachable!");
1672 case ISD::SUB: Opc = SUBTab[Opc]; break;
1673 case ISD::MUL: Opc = MULTab[Opc]; break;
1674 case ISD::AND: Opc = ANDTab[Opc]; break;
1675 case ISD::OR: Opc = ORTab[Opc]; break;
1676 case ISD::XOR: Opc = XORTab[Opc]; break;
1677 }
1678 if (Opc) {
1679 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1680 } else {
1681 assert(Node->getOpcode() == ISD::MUL &&
1682 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001683 // Must use the MUL instruction, which forces use of AL.
1684 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1685 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1686 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001687 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001688 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00001689 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001690 case ISD::SELECT:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001691 if (N.getValueType() != MVT::i1 && N.getValueType() != MVT::i8) {
Chris Lattner11333092005-01-11 03:11:44 +00001692 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1693 Tmp2 = SelectExpr(N.getOperand(1));
1694 Tmp3 = SelectExpr(N.getOperand(2));
1695 } else {
1696 Tmp3 = SelectExpr(N.getOperand(2));
1697 Tmp2 = SelectExpr(N.getOperand(1));
1698 }
Chris Lattner24aad1b2005-01-10 22:10:13 +00001699 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001700 return Result;
1701 } else {
1702 // FIXME: This should not be implemented here, it should be in the generic
1703 // code!
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001704 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1705 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1706 N.getOperand(1)));
1707 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1708 N.getOperand(2)));
1709 } else {
1710 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1711 N.getOperand(2)));
1712 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1713 N.getOperand(1)));
1714 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001715 unsigned TmpReg = MakeReg(MVT::i16);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001716 EmitSelectCC(N.getOperand(0), MVT::i16, Tmp2, Tmp3, TmpReg);
1717 // FIXME: need subregs to do better than this!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001718 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(TmpReg);
1719 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1720 return Result;
1721 }
1722
1723 case ISD::SDIV:
1724 case ISD::UDIV:
1725 case ISD::SREM:
1726 case ISD::UREM: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001727 if (N.getOpcode() == ISD::SDIV)
1728 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1729 // FIXME: These special cases should be handled by the lowering impl!
1730 unsigned RHS = CN->getValue();
1731 bool isNeg = false;
1732 if ((int)RHS < 0) {
1733 isNeg = true;
1734 RHS = -RHS;
1735 }
1736 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1737 unsigned Log = log2(RHS);
1738 unsigned TmpReg = MakeReg(N.getValueType());
1739 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1740 switch (N.getValueType()) {
1741 default: assert("Unknown type to signed divide!");
1742 case MVT::i8:
1743 SAROpc = X86::SAR8ri;
1744 SHROpc = X86::SHR8ri;
1745 ADDOpc = X86::ADD8rr;
1746 NEGOpc = X86::NEG8r;
1747 break;
1748 case MVT::i16:
1749 SAROpc = X86::SAR16ri;
1750 SHROpc = X86::SHR16ri;
1751 ADDOpc = X86::ADD16rr;
1752 NEGOpc = X86::NEG16r;
1753 break;
1754 case MVT::i32:
1755 SAROpc = X86::SAR32ri;
1756 SHROpc = X86::SHR32ri;
1757 ADDOpc = X86::ADD32rr;
1758 NEGOpc = X86::NEG32r;
1759 break;
1760 }
Chris Lattner11333092005-01-11 03:11:44 +00001761 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001762 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1763 unsigned TmpReg2 = MakeReg(N.getValueType());
1764 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1765 unsigned TmpReg3 = MakeReg(N.getValueType());
1766 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1767
1768 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1769 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1770 if (isNeg)
1771 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1772 return Result;
1773 }
1774 }
1775
Chris Lattner11333092005-01-11 03:11:44 +00001776 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1777 Tmp1 = SelectExpr(N.getOperand(0));
1778 Tmp2 = SelectExpr(N.getOperand(1));
1779 } else {
1780 Tmp2 = SelectExpr(N.getOperand(1));
1781 Tmp1 = SelectExpr(N.getOperand(0));
1782 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001783
1784 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1785 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1786 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1787 switch (N.getValueType()) {
1788 default: assert(0 && "Cannot sdiv this type!");
1789 case MVT::i8:
1790 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1791 LoReg = X86::AL;
1792 HiReg = X86::AH;
1793 MovOpcode = X86::MOV8rr;
1794 ClrOpcode = X86::MOV8ri;
1795 SExtOpcode = X86::CBW;
1796 break;
1797 case MVT::i16:
1798 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1799 LoReg = X86::AX;
1800 HiReg = X86::DX;
1801 MovOpcode = X86::MOV16rr;
1802 ClrOpcode = X86::MOV16ri;
1803 SExtOpcode = X86::CWD;
1804 break;
1805 case MVT::i32:
1806 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00001807 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001808 HiReg = X86::EDX;
1809 MovOpcode = X86::MOV32rr;
1810 ClrOpcode = X86::MOV32ri;
1811 SExtOpcode = X86::CDQ;
1812 break;
1813 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1814 case MVT::f32:
1815 case MVT::f64:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001816 if (N.getOpcode() == ISD::SDIV)
1817 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1818 else
1819 assert(0 && "FIXME: Emit frem libcall to fmod!");
1820 return Result;
1821 }
1822
1823 // Set up the low part.
1824 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1825
1826 if (isSigned) {
1827 // Sign extend the low part into the high part.
1828 BuildMI(BB, SExtOpcode, 0);
1829 } else {
1830 // Zero out the high part, effectively zero extending the input.
1831 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1832 }
1833
1834 // Emit the DIV/IDIV instruction.
1835 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1836
1837 // Get the result of the divide or rem.
1838 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1839 return Result;
1840 }
1841
1842 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001843 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001844 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
1845 switch (N.getValueType()) {
1846 default: assert(0 && "Cannot shift this type!");
1847 case MVT::i8: Opc = X86::ADD8rr; break;
1848 case MVT::i16: Opc = X86::ADD16rr; break;
1849 case MVT::i32: Opc = X86::ADD32rr; break;
1850 }
1851 Tmp1 = SelectExpr(N.getOperand(0));
1852 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
1853 return Result;
1854 }
1855
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001856 switch (N.getValueType()) {
1857 default: assert(0 && "Cannot shift this type!");
1858 case MVT::i8: Opc = X86::SHL8ri; break;
1859 case MVT::i16: Opc = X86::SHL16ri; break;
1860 case MVT::i32: Opc = X86::SHL32ri; break;
1861 }
Chris Lattner11333092005-01-11 03:11:44 +00001862 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001863 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1864 return Result;
1865 }
Chris Lattner11333092005-01-11 03:11:44 +00001866
1867 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1868 Tmp1 = SelectExpr(N.getOperand(0));
1869 Tmp2 = SelectExpr(N.getOperand(1));
1870 } else {
1871 Tmp2 = SelectExpr(N.getOperand(1));
1872 Tmp1 = SelectExpr(N.getOperand(0));
1873 }
1874
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001875 switch (N.getValueType()) {
1876 default: assert(0 && "Cannot shift this type!");
1877 case MVT::i8 : Opc = X86::SHL8rCL; break;
1878 case MVT::i16: Opc = X86::SHL16rCL; break;
1879 case MVT::i32: Opc = X86::SHL32rCL; break;
1880 }
1881 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1882 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1883 return Result;
1884 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001885 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1886 switch (N.getValueType()) {
1887 default: assert(0 && "Cannot shift this type!");
1888 case MVT::i8: Opc = X86::SHR8ri; break;
1889 case MVT::i16: Opc = X86::SHR16ri; break;
1890 case MVT::i32: Opc = X86::SHR32ri; break;
1891 }
Chris Lattner11333092005-01-11 03:11:44 +00001892 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001893 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1894 return Result;
1895 }
Chris Lattner11333092005-01-11 03:11:44 +00001896
1897 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1898 Tmp1 = SelectExpr(N.getOperand(0));
1899 Tmp2 = SelectExpr(N.getOperand(1));
1900 } else {
1901 Tmp2 = SelectExpr(N.getOperand(1));
1902 Tmp1 = SelectExpr(N.getOperand(0));
1903 }
1904
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001905 switch (N.getValueType()) {
1906 default: assert(0 && "Cannot shift this type!");
1907 case MVT::i8 : Opc = X86::SHR8rCL; break;
1908 case MVT::i16: Opc = X86::SHR16rCL; break;
1909 case MVT::i32: Opc = X86::SHR32rCL; break;
1910 }
1911 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1912 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1913 return Result;
1914 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001915 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1916 switch (N.getValueType()) {
1917 default: assert(0 && "Cannot shift this type!");
1918 case MVT::i8: Opc = X86::SAR8ri; break;
1919 case MVT::i16: Opc = X86::SAR16ri; break;
1920 case MVT::i32: Opc = X86::SAR32ri; break;
1921 }
Chris Lattner11333092005-01-11 03:11:44 +00001922 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001923 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1924 return Result;
1925 }
Chris Lattner11333092005-01-11 03:11:44 +00001926
1927 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1928 Tmp1 = SelectExpr(N.getOperand(0));
1929 Tmp2 = SelectExpr(N.getOperand(1));
1930 } else {
1931 Tmp2 = SelectExpr(N.getOperand(1));
1932 Tmp1 = SelectExpr(N.getOperand(0));
1933 }
1934
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001935 switch (N.getValueType()) {
1936 default: assert(0 && "Cannot shift this type!");
1937 case MVT::i8 : Opc = X86::SAR8rCL; break;
1938 case MVT::i16: Opc = X86::SAR16rCL; break;
1939 case MVT::i32: Opc = X86::SAR32rCL; break;
1940 }
1941 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1942 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1943 return Result;
1944
1945 case ISD::SETCC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001946 EmitCMP(N.getOperand(0), N.getOperand(1));
1947 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
1948 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
1949 return Result;
1950 case ISD::LOAD: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001951 // Make sure we generate both values.
1952 if (Result != 1)
1953 ExprMap[N.getValue(1)] = 1; // Generate the token
1954 else
1955 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1956
Chris Lattner5188ad72005-01-08 19:28:19 +00001957 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001958 default: assert(0 && "Cannot load this type!");
1959 case MVT::i1:
1960 case MVT::i8: Opc = X86::MOV8rm; break;
1961 case MVT::i16: Opc = X86::MOV16rm; break;
1962 case MVT::i32: Opc = X86::MOV32rm; break;
1963 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
1964 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
1965 }
Chris Lattner11333092005-01-11 03:11:44 +00001966
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001967 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00001968 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001969 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
1970 } else {
1971 X86AddressMode AM;
Chris Lattnera5ade062005-01-11 21:19:59 +00001972 EmitFoldedLoad(N, AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001973 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1974 }
1975 return Result;
1976 }
1977 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001978 // Generate both result values.
1979 if (Result != 1)
1980 ExprMap[N.getValue(1)] = 1; // Generate the token
1981 else
1982 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1983
1984 // FIXME: We are currently ignoring the requested alignment for handling
1985 // greater than the stack alignment. This will need to be revisited at some
1986 // point. Align = N.getOperand(2);
1987
1988 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1989 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1990 std::cerr << "Cannot allocate stack object with greater alignment than"
1991 << " the stack alignment yet!";
1992 abort();
1993 }
1994
1995 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001996 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001997 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
1998 .addImm(CN->getValue());
1999 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002000 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2001 Select(N.getOperand(0));
2002 Tmp1 = SelectExpr(N.getOperand(1));
2003 } else {
2004 Tmp1 = SelectExpr(N.getOperand(1));
2005 Select(N.getOperand(0));
2006 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002007
2008 // Subtract size from stack pointer, thereby allocating some space.
2009 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2010 }
2011
2012 // Put a pointer to the space into the result register, by copying the stack
2013 // pointer.
2014 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2015 return Result;
2016
2017 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002018 // The chain for this call is now lowered.
2019 LoweredTokens.insert(N.getValue(Node->getNumValues()-1));
2020
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002021 if (GlobalAddressSDNode *GASD =
2022 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002023 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002024 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2025 } else if (ExternalSymbolSDNode *ESSDN =
2026 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002027 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002028 BuildMI(BB, X86::CALLpcrel32,
2029 1).addExternalSymbol(ESSDN->getSymbol(), true);
2030 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002031 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2032 Select(N.getOperand(0));
2033 Tmp1 = SelectExpr(N.getOperand(1));
2034 } else {
2035 Tmp1 = SelectExpr(N.getOperand(1));
2036 Select(N.getOperand(0));
2037 }
2038
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002039 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2040 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002041 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002042 default: assert(0 && "Unknown value type for call result!");
2043 case MVT::Other: return 1;
2044 case MVT::i1:
2045 case MVT::i8:
2046 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2047 break;
2048 case MVT::i16:
2049 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2050 break;
2051 case MVT::i32:
2052 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002053 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002054 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2055 break;
2056 case MVT::f32:
2057 case MVT::f64: // Floating-point return values live in %ST(0)
2058 ContainsFPCode = true;
2059 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2060 break;
2061 }
2062 return Result+N.ResNo;
2063 }
2064
2065 return 0;
2066}
2067
2068void ISel::Select(SDOperand N) {
2069 unsigned Tmp1, Tmp2, Opc;
2070
2071 // FIXME: Disable for our current expansion model!
2072 if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second)
2073 return; // Already selected.
2074
Chris Lattner989de032005-01-11 06:14:36 +00002075 SDNode *Node = N.Val;
2076
2077 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002078 default:
Chris Lattner989de032005-01-11 06:14:36 +00002079 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002080 assert(0 && "Node not handled yet!");
2081 case ISD::EntryToken: return; // Noop
2082 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002083 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2084 Select(N.getOperand(0));
2085 Tmp1 = SelectExpr(N.getOperand(1));
2086 } else {
2087 Tmp1 = SelectExpr(N.getOperand(1));
2088 Select(N.getOperand(0));
2089 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002090 Tmp2 = cast<CopyRegSDNode>(N)->getReg();
2091
2092 if (Tmp1 != Tmp2) {
2093 switch (N.getOperand(1).getValueType()) {
2094 default: assert(0 && "Invalid type for operation!");
2095 case MVT::i1:
2096 case MVT::i8: Opc = X86::MOV8rr; break;
2097 case MVT::i16: Opc = X86::MOV16rr; break;
2098 case MVT::i32: Opc = X86::MOV32rr; break;
2099 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00002100 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002101 }
2102 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2103 }
2104 return;
2105 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002106 switch (N.getNumOperands()) {
2107 default:
2108 assert(0 && "Unknown return instruction!");
2109 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002110 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2111 N.getOperand(2).getValueType() == MVT::i32 &&
2112 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002113 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2114 Tmp1 = SelectExpr(N.getOperand(1));
2115 Tmp2 = SelectExpr(N.getOperand(2));
2116 } else {
2117 Tmp2 = SelectExpr(N.getOperand(2));
2118 Tmp1 = SelectExpr(N.getOperand(1));
2119 }
2120 Select(N.getOperand(0));
2121
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002122 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2123 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2124 // Declare that EAX & EDX are live on exit.
2125 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2126 .addReg(X86::ESP);
2127 break;
2128 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002129 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2130 Select(N.getOperand(0));
2131 Tmp1 = SelectExpr(N.getOperand(1));
2132 } else {
2133 Tmp1 = SelectExpr(N.getOperand(1));
2134 Select(N.getOperand(0));
2135 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002136 switch (N.getOperand(1).getValueType()) {
2137 default: assert(0 && "All other types should have been promoted!!");
2138 case MVT::f64:
2139 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2140 // Declare that top-of-stack is live on exit
2141 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2142 break;
2143 case MVT::i32:
2144 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2145 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2146 break;
2147 }
2148 break;
2149 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002150 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002151 break;
2152 }
2153 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2154 return;
2155 case ISD::BR: {
2156 Select(N.getOperand(0));
2157 MachineBasicBlock *Dest =
2158 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2159 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2160 return;
2161 }
2162
2163 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002164 MachineBasicBlock *Dest =
2165 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002166
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002167 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2168 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002169 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2170 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2171 Select(N.getOperand(0));
2172 Tmp1 = SelectExpr(N.getOperand(1));
2173 } else {
2174 Tmp1 = SelectExpr(N.getOperand(1));
2175 Select(N.getOperand(0));
2176 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002177 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2178 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2179 }
Chris Lattner11333092005-01-11 03:11:44 +00002180
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002181 return;
2182 }
2183 case ISD::LOAD:
2184 case ISD::CALL:
2185 case ISD::DYNAMIC_STACKALLOC:
2186 SelectExpr(N);
2187 return;
2188 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002189 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002190
2191 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2192 Opc = 0;
2193 switch (CN->getValueType(0)) {
2194 default: assert(0 && "Invalid type for operation!");
2195 case MVT::i1:
2196 case MVT::i8: Opc = X86::MOV8mi; break;
2197 case MVT::i16: Opc = X86::MOV16mi; break;
2198 case MVT::i32: Opc = X86::MOV32mi; break;
2199 case MVT::f32:
2200 case MVT::f64: break;
2201 }
2202 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002203 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2204 Select(N.getOperand(0));
2205 SelectAddress(N.getOperand(2), AM);
2206 } else {
2207 SelectAddress(N.getOperand(2), AM);
2208 Select(N.getOperand(0));
2209 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002210 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2211 return;
2212 }
2213 }
Chris Lattner837caa72005-01-11 23:21:30 +00002214
2215 // Check to see if this is a load/op/store combination.
2216 if (N.getOperand(1).Val->hasOneUse() &&
Chris Lattner42928302005-01-12 03:16:09 +00002217 isFoldableLoad(N.getOperand(0).getValue(0)) &&
2218 !MVT::isFloatingPoint(N.getOperand(0).getValue(0).getValueType())) {
Chris Lattner837caa72005-01-11 23:21:30 +00002219 SDOperand TheLoad = N.getOperand(0).getValue(0);
2220 // Check to see if we are loading the same pointer that we're storing to.
2221 if (TheLoad.getOperand(1) == N.getOperand(2)) {
2222 // See if the stored value is a simple binary operator that uses the
2223 // load as one of its operands.
2224 SDOperand Op = N.getOperand(1);
2225 if (Op.Val->getNumOperands() == 2 &&
2226 (Op.getOperand(0) == TheLoad || Op.getOperand(1) == TheLoad)) {
2227 // Finally, check to see if this is one of the ops we can handle!
2228 static const unsigned ADDTAB[] = {
2229 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002230 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
Chris Lattner837caa72005-01-11 23:21:30 +00002231 };
Chris Lattner7ea64f52005-01-12 01:28:00 +00002232 static const unsigned SUBTAB[] = {
2233 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002234 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002235 };
2236 static const unsigned ANDTAB[] = {
2237 X86::AND8mi, X86::AND16mi, X86::AND32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002238 X86::AND8mr, X86::AND16mr, X86::AND32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002239 };
2240 static const unsigned ORTAB[] = {
2241 X86::OR8mi, X86::OR16mi, X86::OR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002242 X86::OR8mr, X86::OR16mr, X86::OR32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002243 };
2244 static const unsigned XORTAB[] = {
2245 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002246 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002247 };
2248 static const unsigned SHLTAB[] = {
2249 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002250 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002251 };
2252 static const unsigned SARTAB[] = {
2253 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002254 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002255 };
2256 static const unsigned SHRTAB[] = {
2257 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002258 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002259 };
Chris Lattner837caa72005-01-11 23:21:30 +00002260
2261 const unsigned *TabPtr = 0;
2262 switch (Op.getOpcode()) {
Chris Lattner7ea64f52005-01-12 01:28:00 +00002263 default: std::cerr << "CANNOT [mem] op= val: "; Op.Val->dump(); std::cerr << "\n"; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002264 case ISD::ADD: TabPtr = ADDTAB; break;
Chris Lattner7ea64f52005-01-12 01:28:00 +00002265 case ISD::SUB: TabPtr = SUBTAB; break;
2266 case ISD::AND: TabPtr = ANDTAB; break;
2267 case ISD:: OR: TabPtr = ORTAB; break;
2268 case ISD::XOR: TabPtr = XORTAB; break;
2269 case ISD::SHL: TabPtr = SHLTAB; break;
2270 case ISD::SRA: TabPtr = SARTAB; break;
2271 case ISD::SRL: TabPtr = SHRTAB; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002272 }
2273
2274 if (TabPtr) {
2275 // Handle: [mem] op= CST
2276 SDOperand Op0 = Op.getOperand(0);
2277 SDOperand Op1 = Op.getOperand(1);
2278 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner48034fd2005-01-12 05:22:07 +00002279 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
Chris Lattner837caa72005-01-11 23:21:30 +00002280 default: break;
2281 case MVT::i1:
2282 case MVT::i8: Opc = TabPtr[0]; break;
2283 case MVT::i16: Opc = TabPtr[1]; break;
2284 case MVT::i32: Opc = TabPtr[2]; break;
2285 }
2286
2287 if (Opc) {
2288 if (getRegPressure(TheLoad.getOperand(0)) >
2289 getRegPressure(TheLoad.getOperand(1))) {
2290 Select(TheLoad.getOperand(0));
2291 SelectAddress(TheLoad.getOperand(1), AM);
2292 } else {
2293 SelectAddress(TheLoad.getOperand(1), AM);
2294 Select(TheLoad.getOperand(0));
2295 }
2296
2297 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2298 return;
2299 }
2300 }
2301
2302 // If we have [mem] = V op [mem], try to turn it into:
2303 // [mem] = [mem] op V.
Chris Lattner7ea64f52005-01-12 01:28:00 +00002304 if (Op1 == TheLoad && Op.getOpcode() != ISD::SUB &&
2305 Op.getOpcode() != ISD::SHL && Op.getOpcode() != ISD::SRA &&
2306 Op.getOpcode() != ISD::SRL)
Chris Lattner837caa72005-01-11 23:21:30 +00002307 std::swap(Op0, Op1);
2308
2309 if (Op0 == TheLoad) {
2310 switch (Op0.getValueType()) {
2311 default: break;
2312 case MVT::i1:
2313 case MVT::i8: Opc = TabPtr[3]; break;
2314 case MVT::i16: Opc = TabPtr[4]; break;
2315 case MVT::i32: Opc = TabPtr[5]; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002316 }
2317
2318 if (Opc) {
2319 Select(TheLoad.getOperand(0));
2320 SelectAddress(TheLoad.getOperand(1), AM);
2321 unsigned Reg = SelectExpr(Op1);
2322 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addReg(Reg);
2323 return;
2324 }
2325 }
Chris Lattner837caa72005-01-11 23:21:30 +00002326 }
2327 }
2328 }
2329 }
2330
2331
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002332 switch (N.getOperand(1).getValueType()) {
2333 default: assert(0 && "Cannot store this type!");
2334 case MVT::i1:
2335 case MVT::i8: Opc = X86::MOV8mr; break;
2336 case MVT::i16: Opc = X86::MOV16mr; break;
2337 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002338 case MVT::f32: Opc = X86::FST32m; break;
2339 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002340 }
Chris Lattner11333092005-01-11 03:11:44 +00002341
2342 std::vector<std::pair<unsigned, unsigned> > RP;
2343 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2344 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2345 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2346 std::sort(RP.begin(), RP.end());
2347
2348 for (unsigned i = 0; i != 3; ++i)
2349 switch (RP[2-i].second) {
2350 default: assert(0 && "Unknown operand number!");
2351 case 0: Select(N.getOperand(0)); break;
2352 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002353 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002354 }
2355
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002356 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2357 return;
2358 }
2359 case ISD::ADJCALLSTACKDOWN:
2360 case ISD::ADJCALLSTACKUP:
2361 Select(N.getOperand(0));
2362 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2363
2364 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2365 X86::ADJCALLSTACKUP;
2366 BuildMI(BB, Opc, 1).addImm(Tmp1);
2367 return;
Chris Lattner989de032005-01-11 06:14:36 +00002368 case ISD::MEMSET: {
2369 Select(N.getOperand(0)); // Select the chain.
2370 unsigned Align =
2371 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2372 if (Align == 0) Align = 1;
2373
2374 // Turn the byte code into # iterations
2375 unsigned CountReg;
2376 unsigned Opcode;
2377 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2378 unsigned Val = ValC->getValue() & 255;
2379
2380 // If the value is a constant, then we can potentially use larger sets.
2381 switch (Align & 3) {
2382 case 2: // WORD aligned
2383 CountReg = MakeReg(MVT::i32);
2384 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2385 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2386 } else {
2387 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2388 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2389 }
2390 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2391 Opcode = X86::REP_STOSW;
2392 break;
2393 case 0: // DWORD aligned
2394 CountReg = MakeReg(MVT::i32);
2395 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2396 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2397 } else {
2398 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2399 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2400 }
2401 Val = (Val << 8) | Val;
2402 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
2403 Opcode = X86::REP_STOSD;
2404 break;
2405 default: // BYTE aligned
2406 CountReg = SelectExpr(Node->getOperand(3));
2407 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
2408 Opcode = X86::REP_STOSB;
2409 break;
2410 }
2411 } else {
2412 // If it's not a constant value we are storing, just fall back. We could
2413 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
2414 unsigned ValReg = SelectExpr(Node->getOperand(2));
2415 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
2416 CountReg = SelectExpr(Node->getOperand(3));
2417 Opcode = X86::REP_STOSB;
2418 }
2419
2420 // No matter what the alignment is, we put the source in ESI, the
2421 // destination in EDI, and the count in ECX.
2422 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2423 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2424 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2425 BuildMI(BB, Opcode, 0);
2426 return;
2427 }
Chris Lattner31805bf2005-01-11 06:19:26 +00002428 case ISD::MEMCPY:
2429 Select(N.getOperand(0)); // Select the chain.
2430 unsigned Align =
2431 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2432 if (Align == 0) Align = 1;
2433
2434 // Turn the byte code into # iterations
2435 unsigned CountReg;
2436 unsigned Opcode;
2437 switch (Align & 3) {
2438 case 2: // WORD aligned
2439 CountReg = MakeReg(MVT::i32);
2440 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2441 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2442 } else {
2443 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2444 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2445 }
2446 Opcode = X86::REP_MOVSW;
2447 break;
2448 case 0: // DWORD aligned
2449 CountReg = MakeReg(MVT::i32);
2450 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2451 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2452 } else {
2453 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2454 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2455 }
2456 Opcode = X86::REP_MOVSD;
2457 break;
2458 default: // BYTE aligned
2459 CountReg = SelectExpr(Node->getOperand(3));
2460 Opcode = X86::REP_MOVSB;
2461 break;
2462 }
2463
2464 // No matter what the alignment is, we put the source in ESI, the
2465 // destination in EDI, and the count in ECX.
2466 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2467 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
2468 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2469 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2470 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
2471 BuildMI(BB, Opcode, 0);
2472 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002473 }
2474 assert(0 && "Should not be reached!");
2475}
2476
2477
2478/// createX86PatternInstructionSelector - This pass converts an LLVM function
2479/// into a machine code representation using pattern matching and a machine
2480/// description file.
2481///
2482FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
2483 return new ISel(TM);
2484}