blob: 3238c7450b1ea3ec84df12f933f300e703fcc339 [file] [log] [blame]
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
Chris Lattner24aad1b2005-01-10 22:10:13 +00002//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for X86.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
15#include "X86InstrBuilder.h"
16#include "X86RegisterInfo.h"
Chris Lattner590d8002005-01-09 18:52:44 +000017#include "llvm/Constants.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000018#include "llvm/Function.h"
Chris Lattner590d8002005-01-09 18:52:44 +000019#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000020#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/ADT/Statistic.h"
29#include <set>
30using namespace llvm;
31
32//===----------------------------------------------------------------------===//
33// X86TargetLowering - X86 Implementation of the TargetLowering interface
34namespace {
35 class X86TargetLowering : public TargetLowering {
36 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000037 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000038 public:
39 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
40 // Set up the TargetLowering object.
41 addRegisterClass(MVT::i8, X86::R8RegisterClass);
42 addRegisterClass(MVT::i16, X86::R16RegisterClass);
43 addRegisterClass(MVT::i32, X86::R32RegisterClass);
44 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
45
46 // FIXME: Eliminate these two classes when legalize can handle promotions
47 // well.
48 addRegisterClass(MVT::i1, X86::R8RegisterClass);
49 addRegisterClass(MVT::f32, X86::RFPRegisterClass);
50
51 computeRegisterProperties();
Chris Lattner795069d2005-01-11 05:57:36 +000052
Chris Lattner795069d2005-01-11 05:57:36 +000053 setOperationUnsupported(ISD::MEMMOVE, MVT::Other);
54
Chris Lattner8acb1ba2005-01-07 07:49:41 +000055 setOperationUnsupported(ISD::MUL, MVT::i8);
56 setOperationUnsupported(ISD::SELECT, MVT::i1);
57 setOperationUnsupported(ISD::SELECT, MVT::i8);
58
59 addLegalFPImmediate(+0.0); // FLD0
60 addLegalFPImmediate(+1.0); // FLD1
61 addLegalFPImmediate(-0.0); // FLD0/FCHS
62 addLegalFPImmediate(-1.0); // FLD1/FCHS
63 }
64
65 /// LowerArguments - This hook must be implemented to indicate how we should
66 /// lower the arguments for the specified function, into the specified DAG.
67 virtual std::vector<SDOperand>
68 LowerArguments(Function &F, SelectionDAG &DAG);
69
70 /// LowerCallTo - This hook lowers an abstract call to a function into an
71 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000072 virtual std::pair<SDOperand, SDOperand>
73 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
74 ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000075
76 virtual std::pair<SDOperand, SDOperand>
77 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
78
79 virtual std::pair<SDOperand,SDOperand>
80 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
81 const Type *ArgTy, SelectionDAG &DAG);
82
83 virtual std::pair<SDOperand, SDOperand>
84 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
85 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +000086 };
87}
88
89
90std::vector<SDOperand>
91X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
92 std::vector<SDOperand> ArgValues;
93
94 // Add DAG nodes to load the arguments... On entry to a function on the X86,
95 // the stack frame looks like this:
96 //
97 // [ESP] -- return address
98 // [ESP + 4] -- first argument (leftmost lexically)
99 // [ESP + 8] -- second argument, if first argument is four bytes in size
100 // ...
101 //
102 MachineFunction &MF = DAG.getMachineFunction();
103 MachineFrameInfo *MFI = MF.getFrameInfo();
104
105 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
106 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I) {
107 MVT::ValueType ObjectVT = getValueType(I->getType());
108 unsigned ArgIncrement = 4;
109 unsigned ObjSize;
110 switch (ObjectVT) {
111 default: assert(0 && "Unhandled argument type!");
112 case MVT::i1:
113 case MVT::i8: ObjSize = 1; break;
114 case MVT::i16: ObjSize = 2; break;
115 case MVT::i32: ObjSize = 4; break;
116 case MVT::i64: ObjSize = ArgIncrement = 8; break;
117 case MVT::f32: ObjSize = 4; break;
118 case MVT::f64: ObjSize = ArgIncrement = 8; break;
119 }
120 // Create the frame index object for this incoming parameter...
121 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
122
123 // Create the SelectionDAG nodes corresponding to a load from this parameter
124 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
125
126 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
127 // dead loads.
128 SDOperand ArgValue;
129 if (!I->use_empty())
130 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
131 else {
132 if (MVT::isInteger(ObjectVT))
133 ArgValue = DAG.getConstant(0, ObjectVT);
134 else
135 ArgValue = DAG.getConstantFP(0, ObjectVT);
136 }
137 ArgValues.push_back(ArgValue);
138
139 ArgOffset += ArgIncrement; // Move on to the next argument...
140 }
141
142 // If the function takes variable number of arguments, make a frame index for
143 // the start of the first vararg value... for expansion of llvm.va_start.
144 if (F.isVarArg())
145 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000146 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000147 return ArgValues;
148}
149
Chris Lattner5188ad72005-01-08 19:28:19 +0000150std::pair<SDOperand, SDOperand>
151X86TargetLowering::LowerCallTo(SDOperand Chain,
152 const Type *RetTy, SDOperand Callee,
153 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000154 // Count how many bytes are to be pushed on the stack.
155 unsigned NumBytes = 0;
156
157 if (Args.empty()) {
158 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000159 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
160 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000161 } else {
162 for (unsigned i = 0, e = Args.size(); i != e; ++i)
163 switch (getValueType(Args[i].second)) {
164 default: assert(0 && "Unknown value type!");
165 case MVT::i1:
166 case MVT::i8:
167 case MVT::i16:
168 case MVT::i32:
169 case MVT::f32:
170 NumBytes += 4;
171 break;
172 case MVT::i64:
173 case MVT::f64:
174 NumBytes += 8;
175 break;
176 }
177
Chris Lattner5188ad72005-01-08 19:28:19 +0000178 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
179 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000180
181 // Arguments go on the stack in reverse order, as specified by the ABI.
182 unsigned ArgOffset = 0;
183 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32);
184 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
185 unsigned ArgReg;
186 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
187 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
188
189 switch (getValueType(Args[i].second)) {
190 default: assert(0 && "Unexpected ValueType for argument!");
191 case MVT::i1:
192 case MVT::i8:
193 case MVT::i16:
194 // Promote the integer to 32 bits. If the input type is signed use a
195 // sign extend, otherwise use a zero extend.
196 if (Args[i].second->isSigned())
197 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
198 else
199 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
200
201 // FALL THROUGH
202 case MVT::i32:
203 case MVT::f32:
204 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000205 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
206 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000207 ArgOffset += 4;
208 break;
209 case MVT::i64:
210 case MVT::f64:
211 // FIXME: Note that all of these stores are independent of each other.
Chris Lattner5188ad72005-01-08 19:28:19 +0000212 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
213 Args[i].first, PtrOff);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000214 ArgOffset += 8;
215 break;
216 }
217 }
218 }
219
220 std::vector<MVT::ValueType> RetVals;
221 MVT::ValueType RetTyVT = getValueType(RetTy);
222 if (RetTyVT != MVT::isVoid)
223 RetVals.push_back(RetTyVT);
224 RetVals.push_back(MVT::Other);
225
Chris Lattner5188ad72005-01-08 19:28:19 +0000226 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000227 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000228 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
229 DAG.getConstant(NumBytes, getPointerTy()));
230 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000231}
232
Chris Lattner14824582005-01-09 00:01:27 +0000233std::pair<SDOperand, SDOperand>
234X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
235 // vastart just returns the address of the VarArgsFrameIndex slot.
236 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
237}
238
239std::pair<SDOperand,SDOperand> X86TargetLowering::
240LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
241 const Type *ArgTy, SelectionDAG &DAG) {
242 MVT::ValueType ArgVT = getValueType(ArgTy);
243 SDOperand Result;
244 if (!isVANext) {
245 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
246 } else {
247 unsigned Amt;
248 if (ArgVT == MVT::i32)
249 Amt = 4;
250 else {
251 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
252 "Other types should have been promoted for varargs!");
253 Amt = 8;
254 }
255 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
256 DAG.getConstant(Amt, VAList.getValueType()));
257 }
258 return std::make_pair(Result, Chain);
259}
260
261
262std::pair<SDOperand, SDOperand> X86TargetLowering::
263LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
264 SelectionDAG &DAG) {
265 SDOperand Result;
266 if (Depth) // Depths > 0 not supported yet!
267 Result = DAG.getConstant(0, getPointerTy());
268 else {
269 if (ReturnAddrIndex == 0) {
270 // Set up a frame object for the return address.
271 MachineFunction &MF = DAG.getMachineFunction();
272 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
273 }
274
275 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
276
277 if (!isFrameAddress)
278 // Just load the return address
279 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI);
280 else
281 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
282 DAG.getConstant(4, MVT::i32));
283 }
284 return std::make_pair(Result, Chain);
285}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000286
287
288
289
290
291namespace {
292 Statistic<>
293 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
294
295 //===--------------------------------------------------------------------===//
296 /// ISel - X86 specific code to select X86 machine instructions for
297 /// SelectionDAG operations.
298 ///
299 class ISel : public SelectionDAGISel {
300 /// ContainsFPCode - Every instruction we select that uses or defines a FP
301 /// register should set this to true.
302 bool ContainsFPCode;
303
304 /// X86Lowering - This object fully describes how to lower LLVM code to an
305 /// X86-specific SelectionDAG.
306 X86TargetLowering X86Lowering;
307
Chris Lattner11333092005-01-11 03:11:44 +0000308 /// RegPressureMap - This keeps an approximate count of the number of
309 /// registers required to evaluate each node in the graph.
310 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000311
312 /// ExprMap - As shared expressions are codegen'd, we keep track of which
313 /// vreg the value is produced in, so we only emit one copy of each compiled
314 /// tree.
315 std::map<SDOperand, unsigned> ExprMap;
316 std::set<SDOperand> LoweredTokens;
317
318 public:
319 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
320 }
321
Chris Lattner11333092005-01-11 03:11:44 +0000322 unsigned getRegPressure(SDOperand O) {
323 return RegPressureMap[O.Val];
324 }
325 unsigned ComputeRegPressure(SDOperand O);
326
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000327 /// InstructionSelectBasicBlock - This callback is invoked by
328 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
329 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
330 // While we're doing this, keep track of whether we see any FP code for
331 // FP_REG_KILL insertion.
332 ContainsFPCode = false;
333
Chris Lattnerdb8c3682005-01-12 02:57:10 +0000334 // Scan the PHI nodes that already are inserted into this basic block. If
335 // any of them is a PHI of a floating point value, we need to insert an
336 // FP_REG_KILL.
337 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
338 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
339 I != E; ++I) {
340 assert(I->getOpcode() == X86::PHI &&
341 "Isn't just PHI nodes?");
342 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
343 X86::RFPRegisterClass) {
344 ContainsFPCode = true;
345 break;
346 }
347 }
348
Chris Lattner11333092005-01-11 03:11:44 +0000349 // Compute the RegPressureMap, which is an approximation for the number of
350 // registers required to compute each node.
351 ComputeRegPressure(DAG.getRoot());
352
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000353 // Codegen the basic block.
354 Select(DAG.getRoot());
355
356 // Insert FP_REG_KILL instructions into basic blocks that need them. This
357 // only occurs due to the floating point stackifier not being aggressive
358 // enough to handle arbitrary global stackification.
359 //
360 // Currently we insert an FP_REG_KILL instruction into each block that
361 // uses or defines a floating point virtual register.
362 //
363 // When the global register allocators (like linear scan) finally update
364 // live variable analysis, we can keep floating point values in registers
365 // across basic blocks. This will be a huge win, but we are waiting on
366 // the global allocators before we can do this.
367 //
368 if (ContainsFPCode && BB->succ_size()) {
369 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
370 ++NumFPKill;
371 }
372
373 // Clear state used for selection.
374 ExprMap.clear();
375 LoweredTokens.clear();
Chris Lattner11333092005-01-11 03:11:44 +0000376 RegPressureMap.clear();
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000377 }
378
Chris Lattnera5ade062005-01-11 21:19:59 +0000379 bool isFoldableLoad(SDOperand Op);
380 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
381
382
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000383 void EmitCMP(SDOperand LHS, SDOperand RHS);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000384 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000385 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
386 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000387 unsigned SelectExpr(SDOperand N);
388 bool SelectAddress(SDOperand N, X86AddressMode &AM);
389 void Select(SDOperand N);
390 };
391}
392
Chris Lattner11333092005-01-11 03:11:44 +0000393// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
394// for the number of registers required to compute each node. This is basically
395// computing a generalized form of the Sethi-Ullman number for each node.
396unsigned ISel::ComputeRegPressure(SDOperand O) {
397 SDNode *N = O.Val;
398 unsigned &Result = RegPressureMap[N];
399 if (Result) return Result;
400
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000401 // FIXME: Should operations like CALL (which clobber lots o regs) have a
402 // higher fixed cost??
403
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000404 if (N->getNumOperands() == 0) {
405 Result = 1;
406 } else {
407 unsigned MaxRegUse = 0;
408 unsigned NumExtraMaxRegUsers = 0;
409 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
410 unsigned Regs;
411 if (N->getOperand(i).getOpcode() == ISD::Constant)
412 Regs = 0;
413 else
414 Regs = ComputeRegPressure(N->getOperand(i));
415 if (Regs > MaxRegUse) {
416 MaxRegUse = Regs;
417 NumExtraMaxRegUsers = 0;
418 } else if (Regs == MaxRegUse &&
419 N->getOperand(i).getValueType() != MVT::Other) {
420 ++NumExtraMaxRegUsers;
421 }
Chris Lattner11333092005-01-11 03:11:44 +0000422 }
Chris Lattner11333092005-01-11 03:11:44 +0000423
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000424 Result = MaxRegUse+NumExtraMaxRegUsers;
425 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000426
Chris Lattner837caa72005-01-11 23:21:30 +0000427 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000428 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000429}
430
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000431/// SelectAddress - Add the specified node to the specified addressing mode,
432/// returning true if it cannot be done.
433bool ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
434 switch (N.getOpcode()) {
435 default: break;
436 case ISD::FrameIndex:
437 if (AM.BaseType == X86AddressMode::RegBase && AM.Base.Reg == 0) {
438 AM.BaseType = X86AddressMode::FrameIndexBase;
439 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
440 return false;
441 }
442 break;
443 case ISD::GlobalAddress:
444 if (AM.GV == 0) {
445 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
446 return false;
447 }
448 break;
449 case ISD::Constant:
450 AM.Disp += cast<ConstantSDNode>(N)->getValue();
451 return false;
452 case ISD::SHL:
453 if (AM.IndexReg == 0 || AM.Scale == 1)
454 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
455 unsigned Val = CN->getValue();
456 if (Val == 1 || Val == 2 || Val == 3) {
457 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000458 SDOperand ShVal = N.Val->getOperand(0);
459
460 // Okay, we know that we have a scale by now. However, if the scaled
461 // value is an add of something and a constant, we can fold the
462 // constant into the disp field here.
463 if (ShVal.Val->getOpcode() == ISD::ADD &&
464 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
465 AM.IndexReg = SelectExpr(ShVal.Val->getOperand(0));
466 ConstantSDNode *AddVal =
467 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
468 AM.Disp += AddVal->getValue() << Val;
469 } else {
470 AM.IndexReg = SelectExpr(ShVal);
471 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000472 return false;
473 }
474 }
475 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000476 case ISD::MUL:
477 // X*[3,5,9] -> X+X*[2,4,8]
478 if (AM.IndexReg == 0 && AM.BaseType == X86AddressMode::RegBase &&
479 AM.Base.Reg == 0)
480 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
481 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
482 AM.Scale = unsigned(CN->getValue())-1;
483
484 SDOperand MulVal = N.Val->getOperand(0);
485 unsigned Reg;
486
487 // Okay, we know that we have a scale by now. However, if the scaled
488 // value is an add of something and a constant, we can fold the
489 // constant into the disp field here.
490 if (MulVal.Val->getOpcode() == ISD::ADD &&
491 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
492 Reg = SelectExpr(MulVal.Val->getOperand(0));
493 ConstantSDNode *AddVal =
494 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
495 AM.Disp += AddVal->getValue() * CN->getValue();
496 } else {
497 Reg = SelectExpr(N.Val->getOperand(0));
498 }
499
500 AM.IndexReg = AM.Base.Reg = Reg;
501 return false;
502 }
503 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000504
505 case ISD::ADD: {
506 X86AddressMode Backup = AM;
507 if (!SelectAddress(N.Val->getOperand(0), AM) &&
508 !SelectAddress(N.Val->getOperand(1), AM))
509 return false;
510 AM = Backup;
511 break;
512 }
513 }
514
Chris Lattnera95589b2005-01-11 04:40:19 +0000515 // Is the base register already occupied?
516 if (AM.BaseType != X86AddressMode::RegBase || AM.Base.Reg) {
517 // If so, check to see if the scale index register is set.
518 if (AM.IndexReg == 0) {
519 AM.IndexReg = SelectExpr(N);
520 AM.Scale = 1;
521 return false;
522 }
523
524 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000525 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000526 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000527
528 // Default, generate it as a register.
529 AM.BaseType = X86AddressMode::RegBase;
530 AM.Base.Reg = SelectExpr(N);
531 return false;
532}
533
534/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
535/// assuming that the temporary registers are in the 8-bit register class.
536///
537/// Tmp1 = setcc1
538/// Tmp2 = setcc2
539/// DestReg = logicalop Tmp1, Tmp2
540///
541static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
542 unsigned SetCC2, unsigned LogicalOp,
543 unsigned DestReg) {
544 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
545 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
546 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
547 BuildMI(BB, SetCC1, 0, Tmp1);
548 BuildMI(BB, SetCC2, 0, Tmp2);
549 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
550}
551
552/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
553/// condition codes match the specified SetCCOpcode. Note that some conditions
554/// require multiple instructions to generate the correct value.
555static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
556 ISD::CondCode SetCCOpcode, bool isFP) {
557 unsigned Opc;
558 if (!isFP) {
559 switch (SetCCOpcode) {
560 default: assert(0 && "Illegal integer SetCC!");
561 case ISD::SETEQ: Opc = X86::SETEr; break;
562 case ISD::SETGT: Opc = X86::SETGr; break;
563 case ISD::SETGE: Opc = X86::SETGEr; break;
564 case ISD::SETLT: Opc = X86::SETLr; break;
565 case ISD::SETLE: Opc = X86::SETLEr; break;
566 case ISD::SETNE: Opc = X86::SETNEr; break;
567 case ISD::SETULT: Opc = X86::SETBr; break;
568 case ISD::SETUGT: Opc = X86::SETAr; break;
569 case ISD::SETULE: Opc = X86::SETBEr; break;
570 case ISD::SETUGE: Opc = X86::SETAEr; break;
571 }
572 } else {
573 // On a floating point condition, the flags are set as follows:
574 // ZF PF CF op
575 // 0 | 0 | 0 | X > Y
576 // 0 | 0 | 1 | X < Y
577 // 1 | 0 | 0 | X == Y
578 // 1 | 1 | 1 | unordered
579 //
580 switch (SetCCOpcode) {
581 default: assert(0 && "Invalid FP setcc!");
582 case ISD::SETUEQ:
583 case ISD::SETEQ:
584 Opc = X86::SETEr; // True if ZF = 1
585 break;
586 case ISD::SETOGT:
587 case ISD::SETGT:
588 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
589 break;
590 case ISD::SETOGE:
591 case ISD::SETGE:
592 Opc = X86::SETAEr; // True if CF = 0
593 break;
594 case ISD::SETULT:
595 case ISD::SETLT:
596 Opc = X86::SETBr; // True if CF = 1
597 break;
598 case ISD::SETULE:
599 case ISD::SETLE:
600 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
601 break;
602 case ISD::SETONE:
603 case ISD::SETNE:
604 Opc = X86::SETNEr; // True if ZF = 0
605 break;
606 case ISD::SETUO:
607 Opc = X86::SETPr; // True if PF = 1
608 break;
609 case ISD::SETO:
610 Opc = X86::SETNPr; // True if PF = 0
611 break;
612 case ISD::SETOEQ: // !PF & ZF
613 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
614 return;
615 case ISD::SETOLT: // !PF & CF
616 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
617 return;
618 case ISD::SETOLE: // !PF & (CF || ZF)
619 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
620 return;
621 case ISD::SETUGT: // PF | (!ZF & !CF)
622 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
623 return;
624 case ISD::SETUGE: // PF | !CF
625 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
626 return;
627 case ISD::SETUNE: // PF | !ZF
628 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
629 return;
630 }
631 }
632 BuildMI(BB, Opc, 0, DestReg);
633}
634
635
636/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
637/// the Dest block if the Cond condition is true. If we cannot fold this
638/// condition into the branch, return true.
639///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000640bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
641 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000642 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
643 // B) using two conditional branches instead of one condbr, two setcc's, and
644 // an or.
645 if ((Cond.getOpcode() == ISD::OR ||
646 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
647 // And and or set the flags for us, so there is no need to emit a TST of the
648 // result. It is only safe to do this if there is only a single use of the
649 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000650 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000651 SelectExpr(Cond);
652 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
653 return false;
654 }
655
656 // Codegen br not C -> JE.
657 if (Cond.getOpcode() == ISD::XOR)
658 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
659 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000660 unsigned CondR;
661 if (getRegPressure(Chain) > getRegPressure(Cond)) {
662 Select(Chain);
663 CondR = SelectExpr(Cond.Val->getOperand(0));
664 } else {
665 CondR = SelectExpr(Cond.Val->getOperand(0));
666 Select(Chain);
667 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000668 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
669 BuildMI(BB, X86::JE, 1).addMBB(Dest);
670 return false;
671 }
672
673 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
674 if (SetCC == 0)
675 return true; // Can only handle simple setcc's so far.
676
677 unsigned Opc;
678
679 // Handle integer conditions first.
680 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
681 switch (SetCC->getCondition()) {
682 default: assert(0 && "Illegal integer SetCC!");
683 case ISD::SETEQ: Opc = X86::JE; break;
684 case ISD::SETGT: Opc = X86::JG; break;
685 case ISD::SETGE: Opc = X86::JGE; break;
686 case ISD::SETLT: Opc = X86::JL; break;
687 case ISD::SETLE: Opc = X86::JLE; break;
688 case ISD::SETNE: Opc = X86::JNE; break;
689 case ISD::SETULT: Opc = X86::JB; break;
690 case ISD::SETUGT: Opc = X86::JA; break;
691 case ISD::SETULE: Opc = X86::JBE; break;
692 case ISD::SETUGE: Opc = X86::JAE; break;
693 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000694 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000695 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
696 BuildMI(BB, Opc, 1).addMBB(Dest);
697 return false;
698 }
699
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000700 unsigned Opc2 = 0; // Second branch if needed.
701
702 // On a floating point condition, the flags are set as follows:
703 // ZF PF CF op
704 // 0 | 0 | 0 | X > Y
705 // 0 | 0 | 1 | X < Y
706 // 1 | 0 | 0 | X == Y
707 // 1 | 1 | 1 | unordered
708 //
709 switch (SetCC->getCondition()) {
710 default: assert(0 && "Invalid FP setcc!");
711 case ISD::SETUEQ:
712 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
713 case ISD::SETOGT:
714 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
715 case ISD::SETOGE:
716 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
717 case ISD::SETULT:
718 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
719 case ISD::SETULE:
720 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
721 case ISD::SETONE:
722 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
723 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
724 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
725 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
726 Opc = X86::JA; // ZF = 0 & CF = 0
727 Opc2 = X86::JP; // PF = 1
728 break;
729 case ISD::SETUGE: // PF = 1 | CF = 0
730 Opc = X86::JAE; // CF = 0
731 Opc2 = X86::JP; // PF = 1
732 break;
733 case ISD::SETUNE: // PF = 1 | ZF = 0
734 Opc = X86::JNE; // ZF = 0
735 Opc2 = X86::JP; // PF = 1
736 break;
737 case ISD::SETOEQ: // PF = 0 & ZF = 1
738 //X86::JNP, X86::JE
739 //X86::AND8rr
740 return true; // FIXME: Emit more efficient code for this branch.
741 case ISD::SETOLT: // PF = 0 & CF = 1
742 //X86::JNP, X86::JB
743 //X86::AND8rr
744 return true; // FIXME: Emit more efficient code for this branch.
745 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
746 //X86::JNP, X86::JBE
747 //X86::AND8rr
748 return true; // FIXME: Emit more efficient code for this branch.
749 }
750
Chris Lattner6c07aee2005-01-11 04:06:27 +0000751 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000752 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1));
753 BuildMI(BB, Opc, 1).addMBB(Dest);
754 if (Opc2)
755 BuildMI(BB, Opc2, 1).addMBB(Dest);
756 return false;
757}
758
Chris Lattner24aad1b2005-01-10 22:10:13 +0000759/// EmitSelectCC - Emit code into BB that performs a select operation between
760/// the two registers RTrue and RFalse, generating a result into RDest. Return
761/// true if the fold cannot be performed.
762///
763void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
764 unsigned RTrue, unsigned RFalse, unsigned RDest) {
765 enum Condition {
766 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
767 NOT_SET
768 } CondCode = NOT_SET;
769
770 static const unsigned CMOVTAB16[] = {
771 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
772 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
773 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
774 };
775 static const unsigned CMOVTAB32[] = {
776 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
777 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
778 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
779 };
780 static const unsigned CMOVTABFP[] = {
781 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
782 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
783 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
784 };
785
786 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
787 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
788 switch (SetCC->getCondition()) {
789 default: assert(0 && "Unknown integer comparison!");
790 case ISD::SETEQ: CondCode = EQ; break;
791 case ISD::SETGT: CondCode = GT; break;
792 case ISD::SETGE: CondCode = GE; break;
793 case ISD::SETLT: CondCode = LT; break;
794 case ISD::SETLE: CondCode = LE; break;
795 case ISD::SETNE: CondCode = NE; break;
796 case ISD::SETULT: CondCode = B; break;
797 case ISD::SETUGT: CondCode = A; break;
798 case ISD::SETULE: CondCode = BE; break;
799 case ISD::SETUGE: CondCode = AE; break;
800 }
801 } else {
802 // On a floating point condition, the flags are set as follows:
803 // ZF PF CF op
804 // 0 | 0 | 0 | X > Y
805 // 0 | 0 | 1 | X < Y
806 // 1 | 0 | 0 | X == Y
807 // 1 | 1 | 1 | unordered
808 //
809 switch (SetCC->getCondition()) {
810 default: assert(0 && "Unknown FP comparison!");
811 case ISD::SETUEQ:
812 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
813 case ISD::SETOGT:
814 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
815 case ISD::SETOGE:
816 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
817 case ISD::SETULT:
818 case ISD::SETLT: CondCode = B; break; // True if CF = 1
819 case ISD::SETULE:
820 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
821 case ISD::SETONE:
822 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
823 case ISD::SETUO: CondCode = P; break; // True if PF = 1
824 case ISD::SETO: CondCode = NP; break; // True if PF = 0
825 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
826 case ISD::SETUGE: // PF = 1 | CF = 0
827 case ISD::SETUNE: // PF = 1 | ZF = 0
828 case ISD::SETOEQ: // PF = 0 & ZF = 1
829 case ISD::SETOLT: // PF = 0 & CF = 1
830 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
831 // We cannot emit this comparison as a single cmov.
832 break;
833 }
834 }
835 }
836
837 unsigned Opc = 0;
838 if (CondCode != NOT_SET) {
839 switch (SVT) {
840 default: assert(0 && "Cannot select this type!");
841 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
842 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
843 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000844 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000845 }
846 }
847
848 // Finally, if we weren't able to fold this, just emit the condition and test
849 // it.
850 if (CondCode == NOT_SET || Opc == 0) {
851 // Get the condition into the zero flag.
852 unsigned CondReg = SelectExpr(Cond);
853 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
854
855 switch (SVT) {
856 default: assert(0 && "Cannot select this type!");
857 case MVT::i16: Opc = X86::CMOVE16rr; break;
858 case MVT::i32: Opc = X86::CMOVE32rr; break;
859 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000860 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +0000861 }
862 } else {
863 // FIXME: CMP R, 0 -> TEST R, R
864 EmitCMP(Cond.getOperand(0), Cond.getOperand(1));
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000865 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000866 }
867 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
868}
869
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000870void ISel::EmitCMP(SDOperand LHS, SDOperand RHS) {
Chris Lattner11333092005-01-11 03:11:44 +0000871 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000872 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
873 Opc = 0;
Chris Lattneref6806c2005-01-12 02:02:48 +0000874 if (isFoldableLoad(LHS)) {
875 switch (RHS.getValueType()) {
876 default: break;
877 case MVT::i1:
878 case MVT::i8: Opc = X86::CMP8mi; break;
879 case MVT::i16: Opc = X86::CMP16mi; break;
880 case MVT::i32: Opc = X86::CMP32mi; break;
881 }
882 if (Opc) {
883 X86AddressMode AM;
884 EmitFoldedLoad(LHS, AM);
885 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
886 return;
887 }
888 }
889
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000890 switch (RHS.getValueType()) {
891 default: break;
892 case MVT::i1:
893 case MVT::i8: Opc = X86::CMP8ri; break;
894 case MVT::i16: Opc = X86::CMP16ri; break;
895 case MVT::i32: Opc = X86::CMP32ri; break;
896 }
897 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +0000898 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000899 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
900 return;
901 }
902 }
903
Chris Lattneref6806c2005-01-12 02:02:48 +0000904 Opc = 0;
905 if (isFoldableLoad(LHS)) {
906 switch (RHS.getValueType()) {
907 default: break;
908 case MVT::i1:
909 case MVT::i8: Opc = X86::CMP8mr; break;
910 case MVT::i16: Opc = X86::CMP16mr; break;
911 case MVT::i32: Opc = X86::CMP32mr; break;
912 }
913 if (Opc) {
914 X86AddressMode AM;
915 unsigned Reg;
916 if (getRegPressure(LHS) > getRegPressure(RHS)) {
917 EmitFoldedLoad(LHS, AM);
918 Reg = SelectExpr(RHS);
919 } else {
920 Reg = SelectExpr(RHS);
921 EmitFoldedLoad(LHS, AM);
922 }
923 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
924 return;
925 }
926 }
927
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000928 switch (LHS.getValueType()) {
929 default: assert(0 && "Cannot compare this value!");
930 case MVT::i1:
931 case MVT::i8: Opc = X86::CMP8rr; break;
932 case MVT::i16: Opc = X86::CMP16rr; break;
933 case MVT::i32: Opc = X86::CMP32rr; break;
934 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +0000935 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000936 }
Chris Lattner11333092005-01-11 03:11:44 +0000937 unsigned Tmp1, Tmp2;
938 if (getRegPressure(LHS) > getRegPressure(RHS)) {
939 Tmp1 = SelectExpr(LHS);
940 Tmp2 = SelectExpr(RHS);
941 } else {
942 Tmp2 = SelectExpr(RHS);
943 Tmp1 = SelectExpr(LHS);
944 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000945 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
946}
947
Chris Lattnera5ade062005-01-11 21:19:59 +0000948/// isFoldableLoad - Return true if this is a load instruction that can safely
949/// be folded into an operation that uses it.
950bool ISel::isFoldableLoad(SDOperand Op) {
951 if (Op.getOpcode() != ISD::LOAD ||
952 // FIXME: currently can't fold constant pool indexes.
953 isa<ConstantPoolSDNode>(Op.getOperand(1)))
954 return false;
955
956 // If this load has already been emitted, we clearly can't fold it.
957 if (ExprMap.count(Op)) return false;
958
959 return Op.Val->use_size() == 2;
960}
961
962/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
963/// and compute the address being loaded into AM.
964void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
965 SDOperand Chain = Op.getOperand(0);
966 SDOperand Address = Op.getOperand(1);
967 if (getRegPressure(Chain) > getRegPressure(Address)) {
968 Select(Chain);
969 SelectAddress(Address, AM);
970 } else {
971 SelectAddress(Address, AM);
972 Select(Chain);
973 }
974
975 // The chain for this load is now lowered.
976 LoweredTokens.insert(SDOperand(Op.Val, 1));
977 ExprMap[SDOperand(Op.Val, 1)] = 1;
978}
979
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000980unsigned ISel::SelectExpr(SDOperand N) {
981 unsigned Result;
982 unsigned Tmp1, Tmp2, Tmp3;
983 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +0000984 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +0000985 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +0000986
Chris Lattner590d8002005-01-09 18:52:44 +0000987 if (Node->getOpcode() == ISD::CopyFromReg)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000988 // Just use the specified register as our input.
Chris Lattner590d8002005-01-09 18:52:44 +0000989 return dyn_cast<CopyRegSDNode>(Node)->getReg();
Chris Lattnera5ade062005-01-11 21:19:59 +0000990
991 unsigned &Reg = ExprMap[N];
992 if (Reg) return Reg;
993
994 if (N.getOpcode() != ISD::CALL)
995 Reg = Result = (N.getValueType() != MVT::Other) ?
996 MakeReg(N.getValueType()) : 1;
997 else {
998 // If this is a call instruction, make sure to prepare ALL of the result
999 // values as well as the chain.
1000 if (Node->getNumValues() == 1)
1001 Reg = Result = 1; // Void call, just a chain.
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001002 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001003 Result = MakeReg(Node->getValueType(0));
1004 ExprMap[N.getValue(0)] = Result;
1005 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1006 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1007 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001008 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001009 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001010
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001011 switch (N.getOpcode()) {
1012 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001013 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001014 assert(0 && "Node not handled!\n");
1015 case ISD::FrameIndex:
1016 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1017 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1018 return Result;
1019 case ISD::ConstantPool:
1020 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1021 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1022 return Result;
1023 case ISD::ConstantFP:
1024 ContainsFPCode = true;
1025 Tmp1 = Result; // Intermediate Register
1026 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1027 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1028 Tmp1 = MakeReg(MVT::f64);
1029
1030 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1031 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1032 BuildMI(BB, X86::FLD0, 0, Tmp1);
1033 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1034 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1035 BuildMI(BB, X86::FLD1, 0, Tmp1);
1036 else
1037 assert(0 && "Unexpected constant!");
1038 if (Tmp1 != Result)
1039 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1040 return Result;
1041 case ISD::Constant:
1042 switch (N.getValueType()) {
1043 default: assert(0 && "Cannot use constants of this type!");
1044 case MVT::i1:
1045 case MVT::i8: Opc = X86::MOV8ri; break;
1046 case MVT::i16: Opc = X86::MOV16ri; break;
1047 case MVT::i32: Opc = X86::MOV32ri; break;
1048 }
1049 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1050 return Result;
1051 case ISD::GlobalAddress: {
1052 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1053 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1054 return Result;
1055 }
1056 case ISD::ExternalSymbol: {
1057 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1058 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1059 return Result;
1060 }
1061 case ISD::FP_EXTEND:
1062 Tmp1 = SelectExpr(N.getOperand(0));
1063 BuildMI(BB, X86::FpMOV, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001064 return Result;
1065 case ISD::ZERO_EXTEND: {
1066 int DestIs16 = N.getValueType() == MVT::i16;
1067 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001068
1069 // FIXME: This hack is here for zero extension casts from bool to i8. This
1070 // would not be needed if bools were promoted by Legalize.
1071 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001072 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001073 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1074 return Result;
1075 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001076
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001077 if (isFoldableLoad(N.getOperand(0))) {
1078 static const unsigned Opc[3] = {
1079 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1080 };
1081
1082 X86AddressMode AM;
1083 EmitFoldedLoad(N.getOperand(0), AM);
1084 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1085
1086 return Result;
1087 }
1088
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001089 static const unsigned Opc[3] = {
1090 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1091 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001092 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001093 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1094 return Result;
1095 }
1096 case ISD::SIGN_EXTEND: {
1097 int DestIs16 = N.getValueType() == MVT::i16;
1098 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1099
Chris Lattner590d8002005-01-09 18:52:44 +00001100 // FIXME: Legalize should promote bools to i8!
1101 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1102 "Sign extend from bool not implemented!");
1103
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001104 if (isFoldableLoad(N.getOperand(0))) {
1105 static const unsigned Opc[3] = {
1106 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1107 };
1108
1109 X86AddressMode AM;
1110 EmitFoldedLoad(N.getOperand(0), AM);
1111 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1112 return Result;
1113 }
1114
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001115 static const unsigned Opc[3] = {
1116 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1117 };
1118 Tmp1 = SelectExpr(N.getOperand(0));
1119 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1120 return Result;
1121 }
1122 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001123 // Fold TRUNCATE (LOAD P) into a smaller load from P.
1124 if (isFoldableLoad(N.getOperand(0))) {
1125 switch (N.getValueType()) {
1126 default: assert(0 && "Unknown truncate!");
1127 case MVT::i1:
1128 case MVT::i8: Opc = X86::MOV8rm; break;
1129 case MVT::i16: Opc = X86::MOV16rm; break;
1130 }
1131 X86AddressMode AM;
1132 EmitFoldedLoad(N.getOperand(0), AM);
1133 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1134 return Result;
1135 }
1136
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001137 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1138 // a move out of AX or AL.
1139 switch (N.getOperand(0).getValueType()) {
1140 default: assert(0 && "Unknown truncate!");
1141 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1142 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1143 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1144 }
1145 Tmp1 = SelectExpr(N.getOperand(0));
1146 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1147
1148 switch (N.getValueType()) {
1149 default: assert(0 && "Unknown truncate!");
1150 case MVT::i1:
1151 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1152 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1153 }
1154 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1155 return Result;
1156
1157 case ISD::FP_ROUND:
1158 // Truncate from double to float by storing to memory as float,
1159 // then reading it back into a register.
1160
1161 // Create as stack slot to use.
Chris Lattner590d8002005-01-09 18:52:44 +00001162 // FIXME: This should automatically be made by the Legalizer!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001163 Tmp1 = TLI.getTargetData().getFloatAlignment();
1164 Tmp2 = BB->getParent()->getFrameInfo()->CreateStackObject(4, Tmp1);
1165
1166 // Codegen the input.
1167 Tmp1 = SelectExpr(N.getOperand(0));
1168
1169 // Emit the store, then the reload.
1170 addFrameReference(BuildMI(BB, X86::FST32m, 5), Tmp2).addReg(Tmp1);
1171 addFrameReference(BuildMI(BB, X86::FLD32m, 5, Result), Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001172 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00001173
1174 case ISD::SINT_TO_FP:
1175 case ISD::UINT_TO_FP: {
1176 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001177 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001178
1179 // Promote the integer to a type supported by FLD. We do this because there
1180 // are no unsigned FLD instructions, so we must promote an unsigned value to
1181 // a larger signed value, then use FLD on the larger value.
1182 //
1183 MVT::ValueType PromoteType = MVT::Other;
1184 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1185 unsigned PromoteOpcode = 0;
1186 unsigned RealDestReg = Result;
1187 switch (SrcTy) {
1188 case MVT::i1:
1189 case MVT::i8:
1190 // We don't have the facilities for directly loading byte sized data from
1191 // memory (even signed). Promote it to 16 bits.
1192 PromoteType = MVT::i16;
1193 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1194 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1195 break;
1196 case MVT::i16:
1197 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1198 PromoteType = MVT::i32;
1199 PromoteOpcode = X86::MOVZX32rr16;
1200 }
1201 break;
1202 default:
1203 // Don't fild into the real destination.
1204 if (Node->getOpcode() == ISD::UINT_TO_FP)
1205 Result = MakeReg(Node->getValueType(0));
1206 break;
1207 }
1208
1209 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1210
1211 if (PromoteType != MVT::Other) {
1212 Tmp2 = MakeReg(PromoteType);
1213 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1214 SrcTy = PromoteType;
1215 Tmp1 = Tmp2;
1216 }
1217
1218 // Spill the integer to memory and reload it from there.
1219 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1220 MachineFunction *F = BB->getParent();
1221 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1222
1223 switch (SrcTy) {
1224 case MVT::i64:
1225 // FIXME: this won't work for cast [u]long to FP
1226 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1227 FrameIdx).addReg(Tmp1);
1228 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1229 FrameIdx, 4).addReg(Tmp1+1);
1230 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1231 break;
1232 case MVT::i32:
1233 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1234 FrameIdx).addReg(Tmp1);
1235 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1236 break;
1237 case MVT::i16:
1238 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1239 FrameIdx).addReg(Tmp1);
1240 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1241 break;
1242 default: break; // No promotion required.
1243 }
1244
Chris Lattner085c9952005-01-12 04:00:00 +00001245 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001246 // If this is a cast from uint -> double, we need to be careful when if
1247 // the "sign" bit is set. If so, we don't want to make a negative number,
1248 // we want to make a positive number. Emit code to add an offset if the
1249 // sign bit is set.
1250
1251 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1252 unsigned IsNeg = MakeReg(MVT::i32);
1253 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1254
1255 // Create a CP value that has the offset in one word and 0 in the other.
1256 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1257 0x4f80000000000000ULL);
1258 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1259 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1260 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1261
1262 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1263 // We need special handling for unsigned 64-bit integer sources. If the
1264 // input number has the "sign bit" set, then we loaded it incorrectly as a
1265 // negative 64-bit number. In this case, add an offset value.
1266
1267 // Emit a test instruction to see if the dynamic input value was signed.
1268 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1269
1270 // If the sign bit is set, get a pointer to an offset, otherwise get a
1271 // pointer to a zero.
1272 MachineConstantPool *CP = F->getConstantPool();
1273 unsigned Zero = MakeReg(MVT::i32);
1274 Constant *Null = Constant::getNullValue(Type::UIntTy);
1275 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
1276 CP->getConstantPoolIndex(Null));
1277 unsigned Offset = MakeReg(MVT::i32);
1278 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
1279
1280 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1281 CP->getConstantPoolIndex(OffsetCst));
1282 unsigned Addr = MakeReg(MVT::i32);
1283 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1284
1285 // Load the constant for an add. FIXME: this could make an 'fadd' that
1286 // reads directly from memory, but we don't support these yet.
1287 unsigned ConstReg = MakeReg(MVT::f64);
1288 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1289
1290 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1291 }
1292 return RealDestReg;
1293 }
1294 case ISD::FP_TO_SINT:
1295 case ISD::FP_TO_UINT: {
1296 // FIXME: Most of this grunt work should be done by legalize!
1297 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1298
1299 // Change the floating point control register to use "round towards zero"
1300 // mode when truncating to an integer value.
1301 //
1302 MachineFunction *F = BB->getParent();
1303 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1304 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1305
1306 // Load the old value of the high byte of the control word...
1307 unsigned HighPartOfCW = MakeReg(MVT::i8);
1308 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1309 CWFrameIdx, 1);
1310
1311 // Set the high part to be round to zero...
1312 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1313 CWFrameIdx, 1).addImm(12);
1314
1315 // Reload the modified control word now...
1316 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1317
1318 // Restore the memory image of control word to original value
1319 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1320 CWFrameIdx, 1).addReg(HighPartOfCW);
1321
1322 // We don't have the facilities for directly storing byte sized data to
1323 // memory. Promote it to 16 bits. We also must promote unsigned values to
1324 // larger classes because we only have signed FP stores.
1325 MVT::ValueType StoreClass = Node->getValueType(0);
1326 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1327 switch (StoreClass) {
1328 case MVT::i8: StoreClass = MVT::i16; break;
1329 case MVT::i16: StoreClass = MVT::i32; break;
1330 case MVT::i32: StoreClass = MVT::i64; break;
1331 // The following treatment of cLong may not be perfectly right,
1332 // but it survives chains of casts of the form
1333 // double->ulong->double.
1334 case MVT::i64: StoreClass = MVT::i64; break;
1335 default: assert(0 && "Unknown store class!");
1336 }
1337
1338 // Spill the integer to memory and reload it from there.
1339 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1340 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1341
1342 switch (StoreClass) {
1343 default: assert(0 && "Unknown store class!");
1344 case MVT::i16:
1345 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1346 break;
1347 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001348 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001349 break;
1350 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001351 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001352 break;
1353 }
1354
1355 switch (Node->getValueType(0)) {
1356 default:
1357 assert(0 && "Unknown integer type!");
1358 case MVT::i64:
1359 // FIXME: this isn't gunna work.
1360 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1361 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1362 case MVT::i32:
1363 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1364 break;
1365 case MVT::i16:
1366 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1367 break;
1368 case MVT::i8:
1369 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1370 break;
1371 }
1372
1373 // Reload the original control word now.
1374 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1375 return Result;
1376 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001377 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001378 Op0 = N.getOperand(0);
1379 Op1 = N.getOperand(1);
1380
1381 if (isFoldableLoad(Op0))
1382 std::swap(Op0, Op1);
1383
1384 if (isFoldableLoad(Op1)) {
1385 switch (N.getValueType()) {
1386 default: assert(0 && "Cannot add this type!");
1387 case MVT::i1:
1388 case MVT::i8: Opc = X86::ADD8rm; break;
1389 case MVT::i16: Opc = X86::ADD16rm; break;
1390 case MVT::i32: Opc = X86::ADD32rm; break;
1391 case MVT::f32: Opc = X86::FADD32m; break;
1392 case MVT::f64: Opc = X86::FADD64m; break;
1393 }
1394 X86AddressMode AM;
1395 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1396 Tmp1 = SelectExpr(Op0);
1397 EmitFoldedLoad(Op1, AM);
1398 } else {
1399 EmitFoldedLoad(Op1, AM);
1400 Tmp1 = SelectExpr(Op0);
1401 }
1402 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1403 return Result;
1404 }
1405
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001406 // See if we can codegen this as an LEA to fold operations together.
1407 if (N.getValueType() == MVT::i32) {
1408 X86AddressMode AM;
Chris Lattnera5ade062005-01-11 21:19:59 +00001409 if (!SelectAddress(Op0, AM) && !SelectAddress(Op1, AM)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001410 // If this is not just an add, emit the LEA. For a simple add (like
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001411 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001412 // leave this as LEA, then peephole it to 'ADD' after two address elim
1413 // happens.
1414 if (AM.Scale != 1 || AM.BaseType == X86AddressMode::FrameIndexBase ||
Chris Lattnerbd9f0ee2005-01-09 20:20:29 +00001415 AM.GV || (AM.Base.Reg && AM.IndexReg && AM.Disp)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001416 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1417 return Result;
1418 }
1419 }
1420 }
Chris Lattner11333092005-01-11 03:11:44 +00001421
Chris Lattnera5ade062005-01-11 21:19:59 +00001422 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001423 Opc = 0;
1424 if (CN->getValue() == 1) { // add X, 1 -> inc X
1425 switch (N.getValueType()) {
1426 default: assert(0 && "Cannot integer add this type!");
1427 case MVT::i8: Opc = X86::INC8r; break;
1428 case MVT::i16: Opc = X86::INC16r; break;
1429 case MVT::i32: Opc = X86::INC32r; break;
1430 }
1431 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1432 switch (N.getValueType()) {
1433 default: assert(0 && "Cannot integer add this type!");
1434 case MVT::i8: Opc = X86::DEC8r; break;
1435 case MVT::i16: Opc = X86::DEC16r; break;
1436 case MVT::i32: Opc = X86::DEC32r; break;
1437 }
1438 }
1439
1440 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001441 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001442 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1443 return Result;
1444 }
1445
1446 switch (N.getValueType()) {
1447 default: assert(0 && "Cannot add this type!");
1448 case MVT::i8: Opc = X86::ADD8ri; break;
1449 case MVT::i16: Opc = X86::ADD16ri; break;
1450 case MVT::i32: Opc = X86::ADD32ri; break;
1451 }
1452 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001453 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001454 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1455 return Result;
1456 }
1457 }
1458
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001459 switch (N.getValueType()) {
1460 default: assert(0 && "Cannot add this type!");
1461 case MVT::i8: Opc = X86::ADD8rr; break;
1462 case MVT::i16: Opc = X86::ADD16rr; break;
1463 case MVT::i32: Opc = X86::ADD32rr; break;
1464 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00001465 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001466 }
Chris Lattner11333092005-01-11 03:11:44 +00001467
Chris Lattnera5ade062005-01-11 21:19:59 +00001468 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1469 Tmp1 = SelectExpr(Op0);
1470 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001471 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001472 Tmp2 = SelectExpr(Op1);
1473 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001474 }
1475
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001476 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1477 return Result;
1478 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001479 case ISD::MUL:
1480 case ISD::AND:
1481 case ISD::OR:
1482 case ISD::XOR:
1483 static const unsigned SUBTab[] = {
1484 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1485 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1486 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1487 };
1488 static const unsigned MULTab[] = {
1489 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1490 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1491 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1492 };
1493 static const unsigned ANDTab[] = {
1494 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1495 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
1496 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
1497 };
1498 static const unsigned ORTab[] = {
1499 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1500 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1501 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1502 };
1503 static const unsigned XORTab[] = {
1504 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1505 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1506 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1507 };
1508
1509 Op0 = Node->getOperand(0);
1510 Op1 = Node->getOperand(1);
1511
1512 if (Node->getOpcode() == ISD::SUB && MVT::isInteger(N.getValueType()))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001513 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1514 if (CN->isNullValue()) { // 0 - N -> neg N
1515 switch (N.getValueType()) {
1516 default: assert(0 && "Cannot sub this type!");
1517 case MVT::i1:
1518 case MVT::i8: Opc = X86::NEG8r; break;
1519 case MVT::i16: Opc = X86::NEG16r; break;
1520 case MVT::i32: Opc = X86::NEG32r; break;
1521 }
1522 Tmp1 = SelectExpr(N.getOperand(1));
1523 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1524 return Result;
1525 }
1526
Chris Lattnera5ade062005-01-11 21:19:59 +00001527 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1528 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001529 switch (N.getValueType()) {
1530 default: assert(0 && "Cannot add this type!");
1531 case MVT::i1:
1532 case MVT::i8: Opc = X86::NOT8r; break;
1533 case MVT::i16: Opc = X86::NOT16r; break;
1534 case MVT::i32: Opc = X86::NOT32r; break;
1535 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001536 Tmp1 = SelectExpr(Op0);
Chris Lattnerd4dab922005-01-11 04:31:30 +00001537 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1538 return Result;
1539 }
1540
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001541 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001542 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001543 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001544 case MVT::i8: Opc = 0; break;
1545 case MVT::i16: Opc = 1; break;
1546 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001547 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001548 switch (Node->getOpcode()) {
1549 default: assert(0 && "Unreachable!");
1550 case ISD::SUB: Opc = SUBTab[Opc]; break;
1551 case ISD::MUL: Opc = MULTab[Opc]; break;
1552 case ISD::AND: Opc = ANDTab[Opc]; break;
1553 case ISD::OR: Opc = ORTab[Opc]; break;
1554 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001555 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001556 if (Opc) { // Can't fold MUL:i8 R, imm
1557 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001558 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1559 return Result;
1560 }
1561 }
Chris Lattner11333092005-01-11 03:11:44 +00001562
Chris Lattnera5ade062005-01-11 21:19:59 +00001563 if (isFoldableLoad(Op0))
1564 if (Node->getOpcode() != ISD::SUB) {
1565 std::swap(Op0, Op1);
1566 } else {
1567 // Emit 'reverse' subract, with a memory operand.
1568 switch (N.getValueType()) {
1569 default: Opc = 0; break;
1570 case MVT::f32: Opc = X86::FSUBR32m; break;
1571 case MVT::f64: Opc = X86::FSUBR64m; break;
1572 }
1573 if (Opc) {
1574 X86AddressMode AM;
1575 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1576 EmitFoldedLoad(Op0, AM);
1577 Tmp1 = SelectExpr(Op1);
1578 } else {
1579 Tmp1 = SelectExpr(Op1);
1580 EmitFoldedLoad(Op0, AM);
1581 }
1582 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1583 return Result;
1584 }
1585 }
1586
1587 if (isFoldableLoad(Op1)) {
1588 switch (N.getValueType()) {
1589 default: assert(0 && "Cannot operate on this type!");
1590 case MVT::i1:
1591 case MVT::i8: Opc = 5; break;
1592 case MVT::i16: Opc = 6; break;
1593 case MVT::i32: Opc = 7; break;
1594 case MVT::f32: Opc = 8; break;
1595 case MVT::f64: Opc = 9; break;
1596 }
1597 switch (Node->getOpcode()) {
1598 default: assert(0 && "Unreachable!");
1599 case ISD::SUB: Opc = SUBTab[Opc]; break;
1600 case ISD::MUL: Opc = MULTab[Opc]; break;
1601 case ISD::AND: Opc = ANDTab[Opc]; break;
1602 case ISD::OR: Opc = ORTab[Opc]; break;
1603 case ISD::XOR: Opc = XORTab[Opc]; break;
1604 }
1605
1606 X86AddressMode AM;
1607 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1608 Tmp1 = SelectExpr(Op0);
1609 EmitFoldedLoad(Op1, AM);
1610 } else {
1611 EmitFoldedLoad(Op1, AM);
1612 Tmp1 = SelectExpr(Op0);
1613 }
1614 if (Opc) {
1615 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1616 } else {
1617 assert(Node->getOpcode() == ISD::MUL &&
1618 N.getValueType() == MVT::i8 && "Unexpected situation!");
1619 // Must use the MUL instruction, which forces use of AL.
1620 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1621 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
1622 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1623 }
1624 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001625 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001626
1627 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1628 Tmp1 = SelectExpr(Op0);
1629 Tmp2 = SelectExpr(Op1);
1630 } else {
1631 Tmp2 = SelectExpr(Op1);
1632 Tmp1 = SelectExpr(Op0);
1633 }
1634
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001635 switch (N.getValueType()) {
1636 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001637 case MVT::i1:
1638 case MVT::i8: Opc = 10; break;
1639 case MVT::i16: Opc = 11; break;
1640 case MVT::i32: Opc = 12; break;
1641 case MVT::f32: Opc = 13; break;
1642 case MVT::f64: Opc = 14; break;
1643 }
1644 switch (Node->getOpcode()) {
1645 default: assert(0 && "Unreachable!");
1646 case ISD::SUB: Opc = SUBTab[Opc]; break;
1647 case ISD::MUL: Opc = MULTab[Opc]; break;
1648 case ISD::AND: Opc = ANDTab[Opc]; break;
1649 case ISD::OR: Opc = ORTab[Opc]; break;
1650 case ISD::XOR: Opc = XORTab[Opc]; break;
1651 }
1652 if (Opc) {
1653 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1654 } else {
1655 assert(Node->getOpcode() == ISD::MUL &&
1656 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00001657 // Must use the MUL instruction, which forces use of AL.
1658 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
1659 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
1660 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001661 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001662 return Result;
1663
1664 case ISD::SELECT:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001665 if (N.getValueType() != MVT::i1 && N.getValueType() != MVT::i8) {
Chris Lattner11333092005-01-11 03:11:44 +00001666 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1667 Tmp2 = SelectExpr(N.getOperand(1));
1668 Tmp3 = SelectExpr(N.getOperand(2));
1669 } else {
1670 Tmp3 = SelectExpr(N.getOperand(2));
1671 Tmp2 = SelectExpr(N.getOperand(1));
1672 }
Chris Lattner24aad1b2005-01-10 22:10:13 +00001673 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001674 return Result;
1675 } else {
1676 // FIXME: This should not be implemented here, it should be in the generic
1677 // code!
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001678 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
1679 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1680 N.getOperand(1)));
1681 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1682 N.getOperand(2)));
1683 } else {
1684 Tmp3 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1685 N.getOperand(2)));
1686 Tmp2 = SelectExpr(CurDAG->getNode(ISD::ZERO_EXTEND, MVT::i16,
1687 N.getOperand(1)));
1688 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001689 unsigned TmpReg = MakeReg(MVT::i16);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001690 EmitSelectCC(N.getOperand(0), MVT::i16, Tmp2, Tmp3, TmpReg);
1691 // FIXME: need subregs to do better than this!
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001692 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(TmpReg);
1693 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
1694 return Result;
1695 }
1696
1697 case ISD::SDIV:
1698 case ISD::UDIV:
1699 case ISD::SREM:
1700 case ISD::UREM: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001701 if (N.getOpcode() == ISD::SDIV)
1702 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1703 // FIXME: These special cases should be handled by the lowering impl!
1704 unsigned RHS = CN->getValue();
1705 bool isNeg = false;
1706 if ((int)RHS < 0) {
1707 isNeg = true;
1708 RHS = -RHS;
1709 }
1710 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
1711 unsigned Log = log2(RHS);
1712 unsigned TmpReg = MakeReg(N.getValueType());
1713 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
1714 switch (N.getValueType()) {
1715 default: assert("Unknown type to signed divide!");
1716 case MVT::i8:
1717 SAROpc = X86::SAR8ri;
1718 SHROpc = X86::SHR8ri;
1719 ADDOpc = X86::ADD8rr;
1720 NEGOpc = X86::NEG8r;
1721 break;
1722 case MVT::i16:
1723 SAROpc = X86::SAR16ri;
1724 SHROpc = X86::SHR16ri;
1725 ADDOpc = X86::ADD16rr;
1726 NEGOpc = X86::NEG16r;
1727 break;
1728 case MVT::i32:
1729 SAROpc = X86::SAR32ri;
1730 SHROpc = X86::SHR32ri;
1731 ADDOpc = X86::ADD32rr;
1732 NEGOpc = X86::NEG32r;
1733 break;
1734 }
Chris Lattner11333092005-01-11 03:11:44 +00001735 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001736 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
1737 unsigned TmpReg2 = MakeReg(N.getValueType());
1738 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
1739 unsigned TmpReg3 = MakeReg(N.getValueType());
1740 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
1741
1742 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
1743 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
1744 if (isNeg)
1745 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
1746 return Result;
1747 }
1748 }
1749
Chris Lattner11333092005-01-11 03:11:44 +00001750 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1751 Tmp1 = SelectExpr(N.getOperand(0));
1752 Tmp2 = SelectExpr(N.getOperand(1));
1753 } else {
1754 Tmp2 = SelectExpr(N.getOperand(1));
1755 Tmp1 = SelectExpr(N.getOperand(0));
1756 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001757
1758 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
1759 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
1760 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
1761 switch (N.getValueType()) {
1762 default: assert(0 && "Cannot sdiv this type!");
1763 case MVT::i8:
1764 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
1765 LoReg = X86::AL;
1766 HiReg = X86::AH;
1767 MovOpcode = X86::MOV8rr;
1768 ClrOpcode = X86::MOV8ri;
1769 SExtOpcode = X86::CBW;
1770 break;
1771 case MVT::i16:
1772 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
1773 LoReg = X86::AX;
1774 HiReg = X86::DX;
1775 MovOpcode = X86::MOV16rr;
1776 ClrOpcode = X86::MOV16ri;
1777 SExtOpcode = X86::CWD;
1778 break;
1779 case MVT::i32:
1780 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00001781 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001782 HiReg = X86::EDX;
1783 MovOpcode = X86::MOV32rr;
1784 ClrOpcode = X86::MOV32ri;
1785 SExtOpcode = X86::CDQ;
1786 break;
1787 case MVT::i64: assert(0 && "FIXME: implement i64 DIV/REM libcalls!");
1788 case MVT::f32:
1789 case MVT::f64:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001790 if (N.getOpcode() == ISD::SDIV)
1791 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1792 else
1793 assert(0 && "FIXME: Emit frem libcall to fmod!");
1794 return Result;
1795 }
1796
1797 // Set up the low part.
1798 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
1799
1800 if (isSigned) {
1801 // Sign extend the low part into the high part.
1802 BuildMI(BB, SExtOpcode, 0);
1803 } else {
1804 // Zero out the high part, effectively zero extending the input.
1805 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
1806 }
1807
1808 // Emit the DIV/IDIV instruction.
1809 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
1810
1811 // Get the result of the divide or rem.
1812 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
1813 return Result;
1814 }
1815
1816 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001817 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001818 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
1819 switch (N.getValueType()) {
1820 default: assert(0 && "Cannot shift this type!");
1821 case MVT::i8: Opc = X86::ADD8rr; break;
1822 case MVT::i16: Opc = X86::ADD16rr; break;
1823 case MVT::i32: Opc = X86::ADD32rr; break;
1824 }
1825 Tmp1 = SelectExpr(N.getOperand(0));
1826 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
1827 return Result;
1828 }
1829
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001830 switch (N.getValueType()) {
1831 default: assert(0 && "Cannot shift this type!");
1832 case MVT::i8: Opc = X86::SHL8ri; break;
1833 case MVT::i16: Opc = X86::SHL16ri; break;
1834 case MVT::i32: Opc = X86::SHL32ri; break;
1835 }
Chris Lattner11333092005-01-11 03:11:44 +00001836 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001837 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1838 return Result;
1839 }
Chris Lattner11333092005-01-11 03:11:44 +00001840
1841 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1842 Tmp1 = SelectExpr(N.getOperand(0));
1843 Tmp2 = SelectExpr(N.getOperand(1));
1844 } else {
1845 Tmp2 = SelectExpr(N.getOperand(1));
1846 Tmp1 = SelectExpr(N.getOperand(0));
1847 }
1848
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001849 switch (N.getValueType()) {
1850 default: assert(0 && "Cannot shift this type!");
1851 case MVT::i8 : Opc = X86::SHL8rCL; break;
1852 case MVT::i16: Opc = X86::SHL16rCL; break;
1853 case MVT::i32: Opc = X86::SHL32rCL; break;
1854 }
1855 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1856 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1857 return Result;
1858 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001859 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1860 switch (N.getValueType()) {
1861 default: assert(0 && "Cannot shift this type!");
1862 case MVT::i8: Opc = X86::SHR8ri; break;
1863 case MVT::i16: Opc = X86::SHR16ri; break;
1864 case MVT::i32: Opc = X86::SHR32ri; break;
1865 }
Chris Lattner11333092005-01-11 03:11:44 +00001866 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001867 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1868 return Result;
1869 }
Chris Lattner11333092005-01-11 03:11:44 +00001870
1871 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1872 Tmp1 = SelectExpr(N.getOperand(0));
1873 Tmp2 = SelectExpr(N.getOperand(1));
1874 } else {
1875 Tmp2 = SelectExpr(N.getOperand(1));
1876 Tmp1 = SelectExpr(N.getOperand(0));
1877 }
1878
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001879 switch (N.getValueType()) {
1880 default: assert(0 && "Cannot shift this type!");
1881 case MVT::i8 : Opc = X86::SHR8rCL; break;
1882 case MVT::i16: Opc = X86::SHR16rCL; break;
1883 case MVT::i32: Opc = X86::SHR32rCL; break;
1884 }
1885 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1886 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1887 return Result;
1888 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001889 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1890 switch (N.getValueType()) {
1891 default: assert(0 && "Cannot shift this type!");
1892 case MVT::i8: Opc = X86::SAR8ri; break;
1893 case MVT::i16: Opc = X86::SAR16ri; break;
1894 case MVT::i32: Opc = X86::SAR32ri; break;
1895 }
Chris Lattner11333092005-01-11 03:11:44 +00001896 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001897 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1898 return Result;
1899 }
Chris Lattner11333092005-01-11 03:11:44 +00001900
1901 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1902 Tmp1 = SelectExpr(N.getOperand(0));
1903 Tmp2 = SelectExpr(N.getOperand(1));
1904 } else {
1905 Tmp2 = SelectExpr(N.getOperand(1));
1906 Tmp1 = SelectExpr(N.getOperand(0));
1907 }
1908
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001909 switch (N.getValueType()) {
1910 default: assert(0 && "Cannot shift this type!");
1911 case MVT::i8 : Opc = X86::SAR8rCL; break;
1912 case MVT::i16: Opc = X86::SAR16rCL; break;
1913 case MVT::i32: Opc = X86::SAR32rCL; break;
1914 }
1915 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
1916 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1917 return Result;
1918
1919 case ISD::SETCC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001920 EmitCMP(N.getOperand(0), N.getOperand(1));
1921 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
1922 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
1923 return Result;
1924 case ISD::LOAD: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001925 // Make sure we generate both values.
1926 if (Result != 1)
1927 ExprMap[N.getValue(1)] = 1; // Generate the token
1928 else
1929 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1930
Chris Lattner5188ad72005-01-08 19:28:19 +00001931 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001932 default: assert(0 && "Cannot load this type!");
1933 case MVT::i1:
1934 case MVT::i8: Opc = X86::MOV8rm; break;
1935 case MVT::i16: Opc = X86::MOV16rm; break;
1936 case MVT::i32: Opc = X86::MOV32rm; break;
1937 case MVT::f32: Opc = X86::FLD32m; ContainsFPCode = true; break;
1938 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
1939 }
Chris Lattner11333092005-01-11 03:11:44 +00001940
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001941 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00001942 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001943 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
1944 } else {
1945 X86AddressMode AM;
Chris Lattnera5ade062005-01-11 21:19:59 +00001946 EmitFoldedLoad(N, AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001947 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1948 }
1949 return Result;
1950 }
1951 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001952 // Generate both result values.
1953 if (Result != 1)
1954 ExprMap[N.getValue(1)] = 1; // Generate the token
1955 else
1956 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1957
1958 // FIXME: We are currently ignoring the requested alignment for handling
1959 // greater than the stack alignment. This will need to be revisited at some
1960 // point. Align = N.getOperand(2);
1961
1962 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1963 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1964 std::cerr << "Cannot allocate stack object with greater alignment than"
1965 << " the stack alignment yet!";
1966 abort();
1967 }
1968
1969 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001970 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001971 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
1972 .addImm(CN->getValue());
1973 } else {
Chris Lattner11333092005-01-11 03:11:44 +00001974 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
1975 Select(N.getOperand(0));
1976 Tmp1 = SelectExpr(N.getOperand(1));
1977 } else {
1978 Tmp1 = SelectExpr(N.getOperand(1));
1979 Select(N.getOperand(0));
1980 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001981
1982 // Subtract size from stack pointer, thereby allocating some space.
1983 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
1984 }
1985
1986 // Put a pointer to the space into the result register, by copying the stack
1987 // pointer.
1988 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
1989 return Result;
1990
1991 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00001992 // The chain for this call is now lowered.
1993 LoweredTokens.insert(N.getValue(Node->getNumValues()-1));
1994
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001995 if (GlobalAddressSDNode *GASD =
1996 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00001997 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001998 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
1999 } else if (ExternalSymbolSDNode *ESSDN =
2000 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002001 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002002 BuildMI(BB, X86::CALLpcrel32,
2003 1).addExternalSymbol(ESSDN->getSymbol(), true);
2004 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002005 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2006 Select(N.getOperand(0));
2007 Tmp1 = SelectExpr(N.getOperand(1));
2008 } else {
2009 Tmp1 = SelectExpr(N.getOperand(1));
2010 Select(N.getOperand(0));
2011 }
2012
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002013 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2014 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002015 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002016 default: assert(0 && "Unknown value type for call result!");
2017 case MVT::Other: return 1;
2018 case MVT::i1:
2019 case MVT::i8:
2020 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2021 break;
2022 case MVT::i16:
2023 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2024 break;
2025 case MVT::i32:
2026 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002027 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002028 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2029 break;
2030 case MVT::f32:
2031 case MVT::f64: // Floating-point return values live in %ST(0)
2032 ContainsFPCode = true;
2033 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2034 break;
2035 }
2036 return Result+N.ResNo;
2037 }
2038
2039 return 0;
2040}
2041
2042void ISel::Select(SDOperand N) {
2043 unsigned Tmp1, Tmp2, Opc;
2044
2045 // FIXME: Disable for our current expansion model!
2046 if (/*!N->hasOneUse() &&*/ !LoweredTokens.insert(N).second)
2047 return; // Already selected.
2048
Chris Lattner989de032005-01-11 06:14:36 +00002049 SDNode *Node = N.Val;
2050
2051 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002052 default:
Chris Lattner989de032005-01-11 06:14:36 +00002053 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002054 assert(0 && "Node not handled yet!");
2055 case ISD::EntryToken: return; // Noop
2056 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002057 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2058 Select(N.getOperand(0));
2059 Tmp1 = SelectExpr(N.getOperand(1));
2060 } else {
2061 Tmp1 = SelectExpr(N.getOperand(1));
2062 Select(N.getOperand(0));
2063 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002064 Tmp2 = cast<CopyRegSDNode>(N)->getReg();
2065
2066 if (Tmp1 != Tmp2) {
2067 switch (N.getOperand(1).getValueType()) {
2068 default: assert(0 && "Invalid type for operation!");
2069 case MVT::i1:
2070 case MVT::i8: Opc = X86::MOV8rr; break;
2071 case MVT::i16: Opc = X86::MOV16rr; break;
2072 case MVT::i32: Opc = X86::MOV32rr; break;
2073 case MVT::f32:
Chris Lattneref7ba072005-01-11 03:50:45 +00002074 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002075 }
2076 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2077 }
2078 return;
2079 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002080 switch (N.getNumOperands()) {
2081 default:
2082 assert(0 && "Unknown return instruction!");
2083 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002084 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2085 N.getOperand(2).getValueType() == MVT::i32 &&
2086 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002087 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2088 Tmp1 = SelectExpr(N.getOperand(1));
2089 Tmp2 = SelectExpr(N.getOperand(2));
2090 } else {
2091 Tmp2 = SelectExpr(N.getOperand(2));
2092 Tmp1 = SelectExpr(N.getOperand(1));
2093 }
2094 Select(N.getOperand(0));
2095
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002096 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2097 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
2098 // Declare that EAX & EDX are live on exit.
2099 BuildMI(BB, X86::IMPLICIT_USE, 3).addReg(X86::EAX).addReg(X86::EDX)
2100 .addReg(X86::ESP);
2101 break;
2102 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002103 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2104 Select(N.getOperand(0));
2105 Tmp1 = SelectExpr(N.getOperand(1));
2106 } else {
2107 Tmp1 = SelectExpr(N.getOperand(1));
2108 Select(N.getOperand(0));
2109 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002110 switch (N.getOperand(1).getValueType()) {
2111 default: assert(0 && "All other types should have been promoted!!");
2112 case MVT::f64:
2113 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
2114 // Declare that top-of-stack is live on exit
2115 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::ST0).addReg(X86::ESP);
2116 break;
2117 case MVT::i32:
2118 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2119 BuildMI(BB, X86::IMPLICIT_USE, 2).addReg(X86::EAX).addReg(X86::ESP);
2120 break;
2121 }
2122 break;
2123 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002124 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002125 break;
2126 }
2127 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2128 return;
2129 case ISD::BR: {
2130 Select(N.getOperand(0));
2131 MachineBasicBlock *Dest =
2132 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2133 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2134 return;
2135 }
2136
2137 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002138 MachineBasicBlock *Dest =
2139 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002140
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002141 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2142 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002143 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2144 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2145 Select(N.getOperand(0));
2146 Tmp1 = SelectExpr(N.getOperand(1));
2147 } else {
2148 Tmp1 = SelectExpr(N.getOperand(1));
2149 Select(N.getOperand(0));
2150 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002151 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2152 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2153 }
Chris Lattner11333092005-01-11 03:11:44 +00002154
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002155 return;
2156 }
2157 case ISD::LOAD:
2158 case ISD::CALL:
2159 case ISD::DYNAMIC_STACKALLOC:
2160 SelectExpr(N);
2161 return;
2162 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002163 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002164
2165 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2166 Opc = 0;
2167 switch (CN->getValueType(0)) {
2168 default: assert(0 && "Invalid type for operation!");
2169 case MVT::i1:
2170 case MVT::i8: Opc = X86::MOV8mi; break;
2171 case MVT::i16: Opc = X86::MOV16mi; break;
2172 case MVT::i32: Opc = X86::MOV32mi; break;
2173 case MVT::f32:
2174 case MVT::f64: break;
2175 }
2176 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002177 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
2178 Select(N.getOperand(0));
2179 SelectAddress(N.getOperand(2), AM);
2180 } else {
2181 SelectAddress(N.getOperand(2), AM);
2182 Select(N.getOperand(0));
2183 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002184 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
2185 return;
2186 }
2187 }
Chris Lattner837caa72005-01-11 23:21:30 +00002188
2189 // Check to see if this is a load/op/store combination.
2190 if (N.getOperand(1).Val->hasOneUse() &&
Chris Lattner42928302005-01-12 03:16:09 +00002191 isFoldableLoad(N.getOperand(0).getValue(0)) &&
2192 !MVT::isFloatingPoint(N.getOperand(0).getValue(0).getValueType())) {
Chris Lattner837caa72005-01-11 23:21:30 +00002193 SDOperand TheLoad = N.getOperand(0).getValue(0);
2194 // Check to see if we are loading the same pointer that we're storing to.
2195 if (TheLoad.getOperand(1) == N.getOperand(2)) {
2196 // See if the stored value is a simple binary operator that uses the
2197 // load as one of its operands.
2198 SDOperand Op = N.getOperand(1);
2199 if (Op.Val->getNumOperands() == 2 &&
2200 (Op.getOperand(0) == TheLoad || Op.getOperand(1) == TheLoad)) {
2201 // Finally, check to see if this is one of the ops we can handle!
2202 static const unsigned ADDTAB[] = {
2203 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002204 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
Chris Lattner837caa72005-01-11 23:21:30 +00002205 };
Chris Lattner7ea64f52005-01-12 01:28:00 +00002206 static const unsigned SUBTAB[] = {
2207 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002208 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002209 };
2210 static const unsigned ANDTAB[] = {
2211 X86::AND8mi, X86::AND16mi, X86::AND32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002212 X86::AND8mr, X86::AND16mr, X86::AND32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002213 };
2214 static const unsigned ORTAB[] = {
2215 X86::OR8mi, X86::OR16mi, X86::OR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002216 X86::OR8mr, X86::OR16mr, X86::OR32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002217 };
2218 static const unsigned XORTAB[] = {
2219 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002220 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002221 };
2222 static const unsigned SHLTAB[] = {
2223 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002224 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002225 };
2226 static const unsigned SARTAB[] = {
2227 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002228 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002229 };
2230 static const unsigned SHRTAB[] = {
2231 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
Chris Lattner42928302005-01-12 03:16:09 +00002232 /*Have to put the reg in CL*/0, 0, 0,
Chris Lattner7ea64f52005-01-12 01:28:00 +00002233 };
Chris Lattner837caa72005-01-11 23:21:30 +00002234
2235 const unsigned *TabPtr = 0;
2236 switch (Op.getOpcode()) {
Chris Lattner7ea64f52005-01-12 01:28:00 +00002237 default: std::cerr << "CANNOT [mem] op= val: "; Op.Val->dump(); std::cerr << "\n"; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002238 case ISD::ADD: TabPtr = ADDTAB; break;
Chris Lattner7ea64f52005-01-12 01:28:00 +00002239 case ISD::SUB: TabPtr = SUBTAB; break;
2240 case ISD::AND: TabPtr = ANDTAB; break;
2241 case ISD:: OR: TabPtr = ORTAB; break;
2242 case ISD::XOR: TabPtr = XORTAB; break;
2243 case ISD::SHL: TabPtr = SHLTAB; break;
2244 case ISD::SRA: TabPtr = SARTAB; break;
2245 case ISD::SRL: TabPtr = SHRTAB; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002246 }
2247
2248 if (TabPtr) {
2249 // Handle: [mem] op= CST
2250 SDOperand Op0 = Op.getOperand(0);
2251 SDOperand Op1 = Op.getOperand(1);
2252 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2253 switch (CN->getValueType(0)) {
2254 default: break;
2255 case MVT::i1:
2256 case MVT::i8: Opc = TabPtr[0]; break;
2257 case MVT::i16: Opc = TabPtr[1]; break;
2258 case MVT::i32: Opc = TabPtr[2]; break;
2259 }
2260
2261 if (Opc) {
2262 if (getRegPressure(TheLoad.getOperand(0)) >
2263 getRegPressure(TheLoad.getOperand(1))) {
2264 Select(TheLoad.getOperand(0));
2265 SelectAddress(TheLoad.getOperand(1), AM);
2266 } else {
2267 SelectAddress(TheLoad.getOperand(1), AM);
2268 Select(TheLoad.getOperand(0));
2269 }
2270
2271 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2272 return;
2273 }
2274 }
2275
2276 // If we have [mem] = V op [mem], try to turn it into:
2277 // [mem] = [mem] op V.
Chris Lattner7ea64f52005-01-12 01:28:00 +00002278 if (Op1 == TheLoad && Op.getOpcode() != ISD::SUB &&
2279 Op.getOpcode() != ISD::SHL && Op.getOpcode() != ISD::SRA &&
2280 Op.getOpcode() != ISD::SRL)
Chris Lattner837caa72005-01-11 23:21:30 +00002281 std::swap(Op0, Op1);
2282
2283 if (Op0 == TheLoad) {
2284 switch (Op0.getValueType()) {
2285 default: break;
2286 case MVT::i1:
2287 case MVT::i8: Opc = TabPtr[3]; break;
2288 case MVT::i16: Opc = TabPtr[4]; break;
2289 case MVT::i32: Opc = TabPtr[5]; break;
Chris Lattner837caa72005-01-11 23:21:30 +00002290 }
2291
2292 if (Opc) {
2293 Select(TheLoad.getOperand(0));
2294 SelectAddress(TheLoad.getOperand(1), AM);
2295 unsigned Reg = SelectExpr(Op1);
2296 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addReg(Reg);
2297 return;
2298 }
2299 }
Chris Lattner837caa72005-01-11 23:21:30 +00002300 }
2301 }
2302 }
2303 }
2304
2305
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002306 switch (N.getOperand(1).getValueType()) {
2307 default: assert(0 && "Cannot store this type!");
2308 case MVT::i1:
2309 case MVT::i8: Opc = X86::MOV8mr; break;
2310 case MVT::i16: Opc = X86::MOV16mr; break;
2311 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002312 case MVT::f32: Opc = X86::FST32m; break;
2313 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002314 }
Chris Lattner11333092005-01-11 03:11:44 +00002315
2316 std::vector<std::pair<unsigned, unsigned> > RP;
2317 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
2318 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
2319 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
2320 std::sort(RP.begin(), RP.end());
2321
2322 for (unsigned i = 0; i != 3; ++i)
2323 switch (RP[2-i].second) {
2324 default: assert(0 && "Unknown operand number!");
2325 case 0: Select(N.getOperand(0)); break;
2326 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00002327 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00002328 }
2329
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002330 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
2331 return;
2332 }
2333 case ISD::ADJCALLSTACKDOWN:
2334 case ISD::ADJCALLSTACKUP:
2335 Select(N.getOperand(0));
2336 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2337
2338 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
2339 X86::ADJCALLSTACKUP;
2340 BuildMI(BB, Opc, 1).addImm(Tmp1);
2341 return;
Chris Lattner989de032005-01-11 06:14:36 +00002342 case ISD::MEMSET: {
2343 Select(N.getOperand(0)); // Select the chain.
2344 unsigned Align =
2345 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2346 if (Align == 0) Align = 1;
2347
2348 // Turn the byte code into # iterations
2349 unsigned CountReg;
2350 unsigned Opcode;
2351 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
2352 unsigned Val = ValC->getValue() & 255;
2353
2354 // If the value is a constant, then we can potentially use larger sets.
2355 switch (Align & 3) {
2356 case 2: // WORD aligned
2357 CountReg = MakeReg(MVT::i32);
2358 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2359 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2360 } else {
2361 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2362 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2363 }
2364 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
2365 Opcode = X86::REP_STOSW;
2366 break;
2367 case 0: // DWORD aligned
2368 CountReg = MakeReg(MVT::i32);
2369 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2370 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2371 } else {
2372 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2373 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2374 }
2375 Val = (Val << 8) | Val;
2376 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
2377 Opcode = X86::REP_STOSD;
2378 break;
2379 default: // BYTE aligned
2380 CountReg = SelectExpr(Node->getOperand(3));
2381 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
2382 Opcode = X86::REP_STOSB;
2383 break;
2384 }
2385 } else {
2386 // If it's not a constant value we are storing, just fall back. We could
2387 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
2388 unsigned ValReg = SelectExpr(Node->getOperand(2));
2389 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
2390 CountReg = SelectExpr(Node->getOperand(3));
2391 Opcode = X86::REP_STOSB;
2392 }
2393
2394 // No matter what the alignment is, we put the source in ESI, the
2395 // destination in EDI, and the count in ECX.
2396 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2397 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2398 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2399 BuildMI(BB, Opcode, 0);
2400 return;
2401 }
Chris Lattner31805bf2005-01-11 06:19:26 +00002402 case ISD::MEMCPY:
2403 Select(N.getOperand(0)); // Select the chain.
2404 unsigned Align =
2405 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
2406 if (Align == 0) Align = 1;
2407
2408 // Turn the byte code into # iterations
2409 unsigned CountReg;
2410 unsigned Opcode;
2411 switch (Align & 3) {
2412 case 2: // WORD aligned
2413 CountReg = MakeReg(MVT::i32);
2414 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2415 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
2416 } else {
2417 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2418 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
2419 }
2420 Opcode = X86::REP_MOVSW;
2421 break;
2422 case 0: // DWORD aligned
2423 CountReg = MakeReg(MVT::i32);
2424 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
2425 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
2426 } else {
2427 unsigned ByteReg = SelectExpr(Node->getOperand(3));
2428 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
2429 }
2430 Opcode = X86::REP_MOVSD;
2431 break;
2432 default: // BYTE aligned
2433 CountReg = SelectExpr(Node->getOperand(3));
2434 Opcode = X86::REP_MOVSB;
2435 break;
2436 }
2437
2438 // No matter what the alignment is, we put the source in ESI, the
2439 // destination in EDI, and the count in ECX.
2440 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
2441 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
2442 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
2443 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
2444 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
2445 BuildMI(BB, Opcode, 0);
2446 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002447 }
2448 assert(0 && "Should not be reached!");
2449}
2450
2451
2452/// createX86PatternInstructionSelector - This pass converts an LLVM function
2453/// into a machine code representation using pattern matching and a machine
2454/// description file.
2455///
2456FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
2457 return new ISel(TM);
2458}