blob: 29e088ef764c2822c03ae57ec756a9a54eed6939 [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.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00007//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00008//===----------------------------------------------------------------------===//
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"
Nate Begemanfb5792f2005-07-12 01:41:54 +000017#include "X86Subtarget.h"
Chris Lattnerc6f41812005-05-12 23:06:28 +000018#include "llvm/CallingConv.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000019#include "llvm/Constants.h"
20#include "llvm/Instructions.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000021#include "llvm/Function.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000023#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
27#include "llvm/CodeGen/SSARegMap.h"
28#include "llvm/Target/TargetData.h"
29#include "llvm/Target/TargetLowering.h"
Nate Begemanfb5792f2005-07-12 01:41:54 +000030#include "llvm/Target/TargetMachine.h"
Chris Lattnerc5dcb532005-04-30 04:25:35 +000031#include "llvm/Target/TargetOptions.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000032#include "llvm/Support/CFG.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000033#include "llvm/Support/MathExtras.h"
34#include "llvm/ADT/Statistic.h"
35#include <set>
Jeff Cohen603fea92005-01-12 04:29:05 +000036#include <algorithm>
Chris Lattner8acb1ba2005-01-07 07:49:41 +000037using namespace llvm;
38
Chris Lattnerc6f41812005-05-12 23:06:28 +000039// FIXME: temporary.
40#include "llvm/Support/CommandLine.h"
41static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
42 cl::desc("Enable fastcc on X86"));
43
Chris Lattner67649df2005-05-14 06:52:07 +000044namespace {
45 // X86 Specific DAG Nodes
46 namespace X86ISD {
47 enum NodeType {
48 // Start the numbering where the builtin ops leave off.
49 FIRST_NUMBER = ISD::BUILTIN_OP_END,
50
51 /// FILD64m - This instruction implements SINT_TO_FP with a
52 /// 64-bit source in memory and a FP reg result. This corresponds to
53 /// the X86::FILD64m instruction. It has two inputs (token chain and
54 /// address) and two outputs (FP value and token chain).
55 FILD64m,
Chris Lattner239738a2005-05-14 08:48:15 +000056
57 /// CALL/TAILCALL - These operations represent an abstract X86 call
58 /// instruction, which includes a bunch of information. In particular the
59 /// operands of these node are:
60 ///
61 /// #0 - The incoming token chain
62 /// #1 - The callee
63 /// #2 - The number of arg bytes the caller pushes on the stack.
64 /// #3 - The number of arg bytes the callee pops off the stack.
65 /// #4 - The value to pass in AL/AX/EAX (optional)
66 /// #5 - The value to pass in DL/DX/EDX (optional)
67 ///
68 /// The result values of these nodes are:
69 ///
70 /// #0 - The outgoing token chain
71 /// #1 - The first register result value (optional)
72 /// #2 - The second register result value (optional)
73 ///
74 /// The CALL vs TAILCALL distinction boils down to whether the callee is
75 /// known not to modify the caller's stack frame, as is standard with
76 /// LLVM.
77 CALL,
78 TAILCALL,
Chris Lattner67649df2005-05-14 06:52:07 +000079 };
80 }
81}
82
Chris Lattner8acb1ba2005-01-07 07:49:41 +000083//===----------------------------------------------------------------------===//
84// X86TargetLowering - X86 Implementation of the TargetLowering interface
85namespace {
86 class X86TargetLowering : public TargetLowering {
87 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000088 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner381e8872005-05-15 05:46:45 +000089 int BytesToPopOnReturn; // Number of arg bytes ret should pop.
90 int BytesCallerReserves; // Number of arg bytes caller makes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000091 public:
92 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
93 // Set up the TargetLowering object.
Chris Lattner4df0de92005-01-17 00:00:33 +000094
Chris Lattner653f7232005-05-13 22:46:57 +000095 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Chris Lattner4df0de92005-01-17 00:00:33 +000096 setShiftAmountType(MVT::i8);
97 setSetCCResultType(MVT::i8);
Chris Lattner6659bd72005-04-07 19:41:46 +000098 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner009b55b2005-01-19 03:36:30 +000099 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner4df0de92005-01-17 00:00:33 +0000100
101 // Set up the register classes.
Nate Begemanf63be7d2005-07-06 18:59:04 +0000102 // FIXME: Eliminate these two classes when legalize can handle promotions
103 // well.
104 addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000105 addRegisterClass(MVT::i8, X86::R8RegisterClass);
106 addRegisterClass(MVT::i16, X86::R16RegisterClass);
107 addRegisterClass(MVT::i32, X86::R32RegisterClass);
Nate Begemanf63be7d2005-07-06 18:59:04 +0000108
Chris Lattnera28381c2005-07-16 00:28:20 +0000109 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
110 // operation.
111 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
112 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
113 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
114 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000115
116 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
117 // this operation.
118 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
119 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Chris Lattnera28381c2005-07-16 00:28:20 +0000120
121 // We can handle SINT_TO_FP from i64 even though i64 isn't legal.
Chris Lattner67649df2005-05-14 06:52:07 +0000122 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Chris Lattnera28381c2005-07-16 00:28:20 +0000123
Chris Lattnerda4d4692005-04-09 03:22:37 +0000124 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000125 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
126 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000127 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000128 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
129 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
130 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Chris Lattnerc610d422005-05-11 05:00:34 +0000131 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
132 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
133 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
134 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
135 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
136 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000137 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
138 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +0000139 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Nate Begemanf63be7d2005-07-06 18:59:04 +0000140
Chris Lattner4e6ce5f2005-05-09 20:37:29 +0000141 setOperationAction(ISD::READIO , MVT::i1 , Expand);
142 setOperationAction(ISD::READIO , MVT::i8 , Expand);
143 setOperationAction(ISD::READIO , MVT::i16 , Expand);
144 setOperationAction(ISD::READIO , MVT::i32 , Expand);
145 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
146 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
147 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
148 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
Nate Begemanf63be7d2005-07-06 18:59:04 +0000149
Chris Lattnerda2ce112005-01-16 07:34:08 +0000150 // These should be promoted to a larger select which is supported.
Nate Begemanf63be7d2005-07-06 18:59:04 +0000151 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000152 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begemanf63be7d2005-07-06 18:59:04 +0000153
154 if (X86ScalarSSE) {
155 // Set up the FP register classes.
156 addRegisterClass(MVT::f32, X86::RXMMRegisterClass);
157 addRegisterClass(MVT::f64, X86::RXMMRegisterClass);
158
Nate Begeman5a8441e2005-07-16 02:02:34 +0000159 // SSE has no load+extend ops
Nate Begemanf63be7d2005-07-06 18:59:04 +0000160 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
161 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000162
163 // SSE has no i16 to fp conversion, only i32
164 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
165
Nate Begemanf63be7d2005-07-06 18:59:04 +0000166 // We don't support sin/cos/sqrt/fmod
167 setOperationAction(ISD::FSIN , MVT::f64, Expand);
168 setOperationAction(ISD::FCOS , MVT::f64, Expand);
169 setOperationAction(ISD::FABS , MVT::f64, Expand);
170 setOperationAction(ISD::FNEG , MVT::f64, Expand);
171 setOperationAction(ISD::SREM , MVT::f64, Expand);
172 setOperationAction(ISD::FSIN , MVT::f32, Expand);
173 setOperationAction(ISD::FCOS , MVT::f32, Expand);
174 setOperationAction(ISD::FABS , MVT::f32, Expand);
175 setOperationAction(ISD::FNEG , MVT::f32, Expand);
176 setOperationAction(ISD::SREM , MVT::f32, Expand);
177 } else {
178 // Set up the FP register classes.
179 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
180
181 if (!UnsafeFPMath) {
182 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
183 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
184 }
185
186 addLegalFPImmediate(+0.0); // FLD0
187 addLegalFPImmediate(+1.0); // FLD1
188 addLegalFPImmediate(-0.0); // FLD0/FCHS
189 addLegalFPImmediate(-1.0); // FLD1/FCHS
190 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000191 computeRegisterProperties();
Reid Spencera0f5bf32005-07-19 04:52:44 +0000192
193 maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores
194 maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores
195 maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores
196 allowUnalignedStores = true; // x86 supports it!
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000197 }
Nate Begemanf63be7d2005-07-06 18:59:04 +0000198
Chris Lattner3648c672005-05-13 21:44:04 +0000199 // Return the number of bytes that a function should pop when it returns (in
200 // addition to the space used by the return address).
201 //
202 unsigned getBytesToPopOnReturn() const { return BytesToPopOnReturn; }
203
Chris Lattner381e8872005-05-15 05:46:45 +0000204 // Return the number of bytes that the caller reserves for arguments passed
205 // to this function.
206 unsigned getBytesCallerReserves() const { return BytesCallerReserves; }
207
Chris Lattner67649df2005-05-14 06:52:07 +0000208 /// LowerOperation - Provide custom lowering hooks for some operations.
209 ///
210 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
211
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000212 /// LowerArguments - This hook must be implemented to indicate how we should
213 /// lower the arguments for the specified function, into the specified DAG.
214 virtual std::vector<SDOperand>
215 LowerArguments(Function &F, SelectionDAG &DAG);
216
217 /// LowerCallTo - This hook lowers an abstract call to a function into an
218 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +0000219 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000220 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000221 bool isTailCall, SDOperand Callee, ArgListTy &Args,
222 SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000223
Chris Lattnere0fe2252005-07-05 19:58:54 +0000224 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
225 Value *VAListV, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000226 virtual std::pair<SDOperand,SDOperand>
Chris Lattnere0fe2252005-07-05 19:58:54 +0000227 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
228 const Type *ArgTy, SelectionDAG &DAG);
229
Chris Lattner14824582005-01-09 00:01:27 +0000230 virtual std::pair<SDOperand, SDOperand>
231 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
232 SelectionDAG &DAG);
Chris Lattner381e8872005-05-15 05:46:45 +0000233
234 SDOperand getReturnAddressFrameIndex(SelectionDAG &DAG);
235
Chris Lattnerc6f41812005-05-12 23:06:28 +0000236 private:
237 // C Calling Convention implementation.
238 std::vector<SDOperand> LowerCCCArguments(Function &F, SelectionDAG &DAG);
239 std::pair<SDOperand, SDOperand>
240 LowerCCCCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
Chris Lattner2e7714a2005-05-13 20:29:13 +0000241 bool isTailCall,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000242 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
243
244 // Fast Calling Convention implementation.
245 std::vector<SDOperand> LowerFastCCArguments(Function &F, SelectionDAG &DAG);
246 std::pair<SDOperand, SDOperand>
Chris Lattner2e7714a2005-05-13 20:29:13 +0000247 LowerFastCCCallTo(SDOperand Chain, const Type *RetTy, bool isTailCall,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000248 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000249 };
250}
251
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000252std::vector<SDOperand>
253X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattnerc6f41812005-05-12 23:06:28 +0000254 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
255 return LowerFastCCArguments(F, DAG);
256 return LowerCCCArguments(F, DAG);
257}
258
259std::pair<SDOperand, SDOperand>
260X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
261 bool isVarArg, unsigned CallingConv,
Chris Lattneradf6a962005-05-13 18:50:42 +0000262 bool isTailCall,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000263 SDOperand Callee, ArgListTy &Args,
264 SelectionDAG &DAG) {
265 assert((!isVarArg || CallingConv == CallingConv::C) &&
266 "Only C takes varargs!");
267 if (CallingConv == CallingConv::Fast && EnableFastCC)
Chris Lattner2e7714a2005-05-13 20:29:13 +0000268 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
269 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000270}
271
272//===----------------------------------------------------------------------===//
Chris Lattner653f7232005-05-13 22:46:57 +0000273// C Calling Convention implementation
Chris Lattnerc6f41812005-05-12 23:06:28 +0000274//===----------------------------------------------------------------------===//
275
276std::vector<SDOperand>
277X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000278 std::vector<SDOperand> ArgValues;
279
Chris Lattner6415bb42005-05-10 03:53:18 +0000280 MachineFunction &MF = DAG.getMachineFunction();
281 MachineFrameInfo *MFI = MF.getFrameInfo();
282
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000283 // Add DAG nodes to load the arguments... On entry to a function on the X86,
284 // the stack frame looks like this:
285 //
286 // [ESP] -- return address
287 // [ESP + 4] -- first argument (leftmost lexically)
288 // [ESP + 8] -- second argument, if first argument is four bytes in size
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000289 // ...
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000290 //
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000291 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattnere4d5c442005-03-15 04:54:21 +0000292 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000293 MVT::ValueType ObjectVT = getValueType(I->getType());
294 unsigned ArgIncrement = 4;
295 unsigned ObjSize;
296 switch (ObjectVT) {
297 default: assert(0 && "Unhandled argument type!");
298 case MVT::i1:
299 case MVT::i8: ObjSize = 1; break;
300 case MVT::i16: ObjSize = 2; break;
301 case MVT::i32: ObjSize = 4; break;
302 case MVT::i64: ObjSize = ArgIncrement = 8; break;
303 case MVT::f32: ObjSize = 4; break;
304 case MVT::f64: ObjSize = ArgIncrement = 8; break;
305 }
306 // Create the frame index object for this incoming parameter...
307 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000308
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000309 // Create the SelectionDAG nodes corresponding to a load from this parameter
310 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
311
312 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
313 // dead loads.
314 SDOperand ArgValue;
315 if (!I->use_empty())
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000316 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
317 DAG.getSrcValue(NULL));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000318 else {
319 if (MVT::isInteger(ObjectVT))
320 ArgValue = DAG.getConstant(0, ObjectVT);
321 else
322 ArgValue = DAG.getConstantFP(0, ObjectVT);
323 }
324 ArgValues.push_back(ArgValue);
325
326 ArgOffset += ArgIncrement; // Move on to the next argument...
327 }
328
329 // If the function takes variable number of arguments, make a frame index for
330 // the start of the first vararg value... for expansion of llvm.va_start.
331 if (F.isVarArg())
332 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner3648c672005-05-13 21:44:04 +0000333 ReturnAddrIndex = 0; // No return address slot generated yet.
334 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattner381e8872005-05-15 05:46:45 +0000335 BytesCallerReserves = ArgOffset;
Chris Lattner4c52f0e2005-04-09 15:23:56 +0000336
337 // Finally, inform the code generator which regs we return values in.
338 switch (getValueType(F.getReturnType())) {
339 default: assert(0 && "Unknown type!");
340 case MVT::isVoid: break;
341 case MVT::i1:
342 case MVT::i8:
343 case MVT::i16:
344 case MVT::i32:
345 MF.addLiveOut(X86::EAX);
346 break;
347 case MVT::i64:
348 MF.addLiveOut(X86::EAX);
349 MF.addLiveOut(X86::EDX);
350 break;
351 case MVT::f32:
352 case MVT::f64:
353 MF.addLiveOut(X86::ST0);
354 break;
355 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000356 return ArgValues;
357}
358
Chris Lattner5188ad72005-01-08 19:28:19 +0000359std::pair<SDOperand, SDOperand>
Chris Lattnerc6f41812005-05-12 23:06:28 +0000360X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
Chris Lattner2e7714a2005-05-13 20:29:13 +0000361 bool isVarArg, bool isTailCall,
362 SDOperand Callee, ArgListTy &Args,
363 SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000364 // Count how many bytes are to be pushed on the stack.
365 unsigned NumBytes = 0;
366
367 if (Args.empty()) {
368 // Save zero bytes.
Chris Lattner16cd04d2005-05-12 23:24:06 +0000369 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattner5188ad72005-01-08 19:28:19 +0000370 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000371 } else {
372 for (unsigned i = 0, e = Args.size(); i != e; ++i)
373 switch (getValueType(Args[i].second)) {
374 default: assert(0 && "Unknown value type!");
375 case MVT::i1:
376 case MVT::i8:
377 case MVT::i16:
378 case MVT::i32:
379 case MVT::f32:
380 NumBytes += 4;
381 break;
382 case MVT::i64:
383 case MVT::f64:
384 NumBytes += 8;
385 break;
386 }
387
Chris Lattner16cd04d2005-05-12 23:24:06 +0000388 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattner5188ad72005-01-08 19:28:19 +0000389 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000390
391 // Arguments go on the stack in reverse order, as specified by the ABI.
392 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000393 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
394 DAG.getEntryNode());
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000395 std::vector<SDOperand> Stores;
396
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000397 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000398 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
399 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
400
401 switch (getValueType(Args[i].second)) {
402 default: assert(0 && "Unexpected ValueType for argument!");
403 case MVT::i1:
404 case MVT::i8:
405 case MVT::i16:
406 // Promote the integer to 32 bits. If the input type is signed use a
407 // sign extend, otherwise use a zero extend.
408 if (Args[i].second->isSigned())
409 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
410 else
411 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
412
413 // FALL THROUGH
414 case MVT::i32:
415 case MVT::f32:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000416 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000417 Args[i].first, PtrOff,
418 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000419 ArgOffset += 4;
420 break;
421 case MVT::i64:
422 case MVT::f64:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000423 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000424 Args[i].first, PtrOff,
425 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000426 ArgOffset += 8;
427 break;
428 }
429 }
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000430 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000431 }
432
433 std::vector<MVT::ValueType> RetVals;
434 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000435 RetVals.push_back(MVT::Other);
436
Chris Lattner239738a2005-05-14 08:48:15 +0000437 // The result values produced have to be legal. Promote the result.
438 switch (RetTyVT) {
439 case MVT::isVoid: break;
440 default:
441 RetVals.push_back(RetTyVT);
442 break;
443 case MVT::i1:
444 case MVT::i8:
445 case MVT::i16:
446 RetVals.push_back(MVT::i32);
447 break;
448 case MVT::f32:
Nate Begemanf63be7d2005-07-06 18:59:04 +0000449 if (X86ScalarSSE)
450 RetVals.push_back(MVT::f32);
451 else
452 RetVals.push_back(MVT::f64);
Chris Lattner239738a2005-05-14 08:48:15 +0000453 break;
454 case MVT::i64:
455 RetVals.push_back(MVT::i32);
456 RetVals.push_back(MVT::i32);
457 break;
458 }
459 std::vector<SDOperand> Ops;
460 Ops.push_back(Chain);
461 Ops.push_back(Callee);
462 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
463 Ops.push_back(DAG.getConstant(0, getPointerTy()));
464 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
465 RetVals, Ops);
466 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
467
468 SDOperand ResultVal;
469 switch (RetTyVT) {
470 case MVT::isVoid: break;
471 default:
472 ResultVal = TheCall.getValue(1);
473 break;
474 case MVT::i1:
475 case MVT::i8:
476 case MVT::i16:
477 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
478 break;
479 case MVT::f32:
480 // FIXME: we would really like to remember that this FP_ROUND operation is
481 // okay to eliminate if we allow excess FP precision.
482 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
483 break;
484 case MVT::i64:
485 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
486 TheCall.getValue(2));
487 break;
488 }
489
490 return std::make_pair(ResultVal, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000491}
492
Chris Lattnere0fe2252005-07-05 19:58:54 +0000493SDOperand
494X86TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
495 Value *VAListV, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000496 // vastart just stores the address of the VarArgsFrameIndex slot.
497 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000498 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
499 DAG.getSrcValue(VAListV));
Chris Lattner14824582005-01-09 00:01:27 +0000500}
501
Chris Lattnere0fe2252005-07-05 19:58:54 +0000502
503std::pair<SDOperand,SDOperand>
504X86TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP,
505 Value *VAListV, const Type *ArgTy,
506 SelectionDAG &DAG) {
Chris Lattner14824582005-01-09 00:01:27 +0000507 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000508 SDOperand Val = DAG.getLoad(MVT::i32, Chain,
509 VAListP, DAG.getSrcValue(VAListV));
510 SDOperand Result = DAG.getLoad(ArgVT, Chain, Val,
Chris Lattner08568cf2005-07-05 17:50:16 +0000511 DAG.getSrcValue(NULL));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000512 unsigned Amt;
513 if (ArgVT == MVT::i32)
514 Amt = 4;
515 else {
516 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
517 "Other types should have been promoted for varargs!");
518 Amt = 8;
Chris Lattner14824582005-01-09 00:01:27 +0000519 }
Andrew Lenharth558bc882005-06-18 18:34:52 +0000520 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
521 DAG.getConstant(Amt, Val.getValueType()));
522 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000523 Val, VAListP, DAG.getSrcValue(VAListV));
Chris Lattner14824582005-01-09 00:01:27 +0000524 return std::make_pair(Result, Chain);
525}
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000526
Chris Lattnerc6f41812005-05-12 23:06:28 +0000527//===----------------------------------------------------------------------===//
Chris Lattner653f7232005-05-13 22:46:57 +0000528// Fast Calling Convention implementation
Chris Lattnerc6f41812005-05-12 23:06:28 +0000529//===----------------------------------------------------------------------===//
530//
531// The X86 'fast' calling convention passes up to two integer arguments in
532// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
533// and requires that the callee pop its arguments off the stack (allowing proper
534// tail calls), and has the same return value conventions as C calling convs.
535//
Chris Lattner10d26452005-05-13 23:49:10 +0000536// This calling convention always arranges for the callee pop value to be 8n+4
537// bytes, which is needed for tail recursion elimination and stack alignment
538// reasons.
539//
Chris Lattnerc6f41812005-05-12 23:06:28 +0000540// Note that this can be enhanced in the future to pass fp vals in registers
541// (when we have a global fp allocator) and do other tricks.
542//
Chris Lattner63602fb2005-05-13 07:38:09 +0000543
544/// AddLiveIn - This helper function adds the specified physical register to the
545/// MachineFunction as a live in value. It also creates a corresponding virtual
546/// register for it.
547static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
548 TargetRegisterClass *RC) {
549 assert(RC->contains(PReg) && "Not the correct regclass!");
550 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
551 MF.addLiveIn(PReg, VReg);
552 return VReg;
553}
554
555
Chris Lattnerc6f41812005-05-12 23:06:28 +0000556std::vector<SDOperand>
557X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
558 std::vector<SDOperand> ArgValues;
559
560 MachineFunction &MF = DAG.getMachineFunction();
561 MachineFrameInfo *MFI = MF.getFrameInfo();
562
563 // Add DAG nodes to load the arguments... On entry to a function the stack
564 // frame looks like this:
565 //
566 // [ESP] -- return address
567 // [ESP + 4] -- first nonreg argument (leftmost lexically)
568 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
569 // ...
570 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
571
572 // Keep track of the number of integer regs passed so far. This can be either
573 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
574 // used).
575 unsigned NumIntRegs = 0;
576
577 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
578 MVT::ValueType ObjectVT = getValueType(I->getType());
579 unsigned ArgIncrement = 4;
580 unsigned ObjSize = 0;
581 SDOperand ArgValue;
582
583 switch (ObjectVT) {
584 default: assert(0 && "Unhandled argument type!");
585 case MVT::i1:
586 case MVT::i8:
587 if (NumIntRegs < 2) {
588 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000589 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
590 X86::R8RegisterClass);
591 ArgValue = DAG.getCopyFromReg(VReg, MVT::i8, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000592 DAG.setRoot(ArgValue.getValue(1));
593 }
594 ++NumIntRegs;
595 break;
596 }
597
598 ObjSize = 1;
599 break;
600 case MVT::i16:
601 if (NumIntRegs < 2) {
602 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000603 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
604 X86::R16RegisterClass);
605 ArgValue = DAG.getCopyFromReg(VReg, MVT::i16, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000606 DAG.setRoot(ArgValue.getValue(1));
607 }
608 ++NumIntRegs;
609 break;
610 }
611 ObjSize = 2;
612 break;
613 case MVT::i32:
614 if (NumIntRegs < 2) {
615 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000616 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
617 X86::R32RegisterClass);
618 ArgValue = DAG.getCopyFromReg(VReg, MVT::i32, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000619 DAG.setRoot(ArgValue.getValue(1));
620 }
621 ++NumIntRegs;
622 break;
623 }
624 ObjSize = 4;
625 break;
626 case MVT::i64:
627 if (NumIntRegs == 0) {
628 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000629 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
630 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000631
Chris Lattner63602fb2005-05-13 07:38:09 +0000632 SDOperand Low=DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot());
633 SDOperand Hi =DAG.getCopyFromReg(TopReg, MVT::i32, Low.getValue(1));
Chris Lattnerc6f41812005-05-12 23:06:28 +0000634 DAG.setRoot(Hi.getValue(1));
635
636 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
637 }
638 NumIntRegs = 2;
639 break;
640 } else if (NumIntRegs == 1) {
641 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000642 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
643 SDOperand Low = DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000644 DAG.setRoot(Low.getValue(1));
645
646 // Load the high part from memory.
647 // Create the frame index object for this incoming parameter...
648 int FI = MFI->CreateFixedObject(4, ArgOffset);
649 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
650 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
651 DAG.getSrcValue(NULL));
652 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
653 }
654 ArgOffset += 4;
655 NumIntRegs = 2;
656 break;
657 }
658 ObjSize = ArgIncrement = 8;
659 break;
660 case MVT::f32: ObjSize = 4; break;
661 case MVT::f64: ObjSize = ArgIncrement = 8; break;
662 }
663
664 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
665 // dead loads.
666 if (ObjSize && !I->use_empty()) {
667 // Create the frame index object for this incoming parameter...
668 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
669
670 // Create the SelectionDAG nodes corresponding to a load from this
671 // parameter.
672 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
673
674 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
675 DAG.getSrcValue(NULL));
676 } else if (ArgValue.Val == 0) {
677 if (MVT::isInteger(ObjectVT))
678 ArgValue = DAG.getConstant(0, ObjectVT);
679 else
680 ArgValue = DAG.getConstantFP(0, ObjectVT);
681 }
682 ArgValues.push_back(ArgValue);
683
684 if (ObjSize)
685 ArgOffset += ArgIncrement; // Move on to the next argument.
686 }
687
Chris Lattner10d26452005-05-13 23:49:10 +0000688 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
689 // arguments and the arguments after the retaddr has been pushed are aligned.
690 if ((ArgOffset & 7) == 0)
691 ArgOffset += 4;
692
Chris Lattner3648c672005-05-13 21:44:04 +0000693 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
694 ReturnAddrIndex = 0; // No return address slot generated yet.
695 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
Chris Lattner381e8872005-05-15 05:46:45 +0000696 BytesCallerReserves = 0;
Chris Lattnerc6f41812005-05-12 23:06:28 +0000697
698 // Finally, inform the code generator which regs we return values in.
699 switch (getValueType(F.getReturnType())) {
700 default: assert(0 && "Unknown type!");
701 case MVT::isVoid: break;
702 case MVT::i1:
703 case MVT::i8:
704 case MVT::i16:
705 case MVT::i32:
706 MF.addLiveOut(X86::EAX);
707 break;
708 case MVT::i64:
709 MF.addLiveOut(X86::EAX);
710 MF.addLiveOut(X86::EDX);
711 break;
712 case MVT::f32:
713 case MVT::f64:
714 MF.addLiveOut(X86::ST0);
715 break;
716 }
717 return ArgValues;
718}
719
720std::pair<SDOperand, SDOperand>
721X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
Chris Lattner2e7714a2005-05-13 20:29:13 +0000722 bool isTailCall, SDOperand Callee,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000723 ArgListTy &Args, SelectionDAG &DAG) {
724 // Count how many bytes are to be pushed on the stack.
725 unsigned NumBytes = 0;
726
727 // Keep track of the number of integer regs passed so far. This can be either
728 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
729 // used).
730 unsigned NumIntRegs = 0;
731
732 for (unsigned i = 0, e = Args.size(); i != e; ++i)
733 switch (getValueType(Args[i].second)) {
734 default: assert(0 && "Unknown value type!");
735 case MVT::i1:
736 case MVT::i8:
737 case MVT::i16:
738 case MVT::i32:
739 if (NumIntRegs < 2) {
740 ++NumIntRegs;
741 break;
742 }
743 // fall through
744 case MVT::f32:
745 NumBytes += 4;
746 break;
747 case MVT::i64:
748 if (NumIntRegs == 0) {
749 NumIntRegs = 2;
750 break;
751 } else if (NumIntRegs == 1) {
752 NumIntRegs = 2;
753 NumBytes += 4;
754 break;
755 }
756
757 // fall through
758 case MVT::f64:
759 NumBytes += 8;
760 break;
761 }
762
Chris Lattner10d26452005-05-13 23:49:10 +0000763 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
764 // arguments and the arguments after the retaddr has been pushed are aligned.
765 if ((NumBytes & 7) == 0)
766 NumBytes += 4;
767
Chris Lattner16cd04d2005-05-12 23:24:06 +0000768 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000769 DAG.getConstant(NumBytes, getPointerTy()));
770
771 // Arguments go on the stack in reverse order, as specified by the ABI.
772 unsigned ArgOffset = 0;
773 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
774 DAG.getEntryNode());
775 NumIntRegs = 0;
776 std::vector<SDOperand> Stores;
777 std::vector<SDOperand> RegValuesToPass;
778 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
779 switch (getValueType(Args[i].second)) {
780 default: assert(0 && "Unexpected ValueType for argument!");
781 case MVT::i1:
782 case MVT::i8:
783 case MVT::i16:
784 case MVT::i32:
785 if (NumIntRegs < 2) {
786 RegValuesToPass.push_back(Args[i].first);
787 ++NumIntRegs;
788 break;
789 }
790 // Fall through
791 case MVT::f32: {
792 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
793 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
794 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
795 Args[i].first, PtrOff,
796 DAG.getSrcValue(NULL)));
797 ArgOffset += 4;
798 break;
799 }
800 case MVT::i64:
801 if (NumIntRegs < 2) { // Can pass part of it in regs?
802 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
803 Args[i].first, DAG.getConstant(1, MVT::i32));
804 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
805 Args[i].first, DAG.getConstant(0, MVT::i32));
806 RegValuesToPass.push_back(Lo);
807 ++NumIntRegs;
808 if (NumIntRegs < 2) { // Pass both parts in regs?
809 RegValuesToPass.push_back(Hi);
810 ++NumIntRegs;
811 } else {
812 // Pass the high part in memory.
813 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
814 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
815 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner920c0aa2005-05-14 12:03:10 +0000816 Hi, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattnerc6f41812005-05-12 23:06:28 +0000817 ArgOffset += 4;
818 }
819 break;
820 }
821 // Fall through
822 case MVT::f64:
823 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
824 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
825 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
826 Args[i].first, PtrOff,
827 DAG.getSrcValue(NULL)));
828 ArgOffset += 8;
829 break;
830 }
831 }
832 if (!Stores.empty())
833 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
834
Chris Lattner10d26452005-05-13 23:49:10 +0000835 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
836 // arguments and the arguments after the retaddr has been pushed are aligned.
837 if ((ArgOffset & 7) == 0)
838 ArgOffset += 4;
839
Chris Lattner239738a2005-05-14 08:48:15 +0000840 std::vector<MVT::ValueType> RetVals;
841 MVT::ValueType RetTyVT = getValueType(RetTy);
842
843 RetVals.push_back(MVT::Other);
844
845 // The result values produced have to be legal. Promote the result.
846 switch (RetTyVT) {
847 case MVT::isVoid: break;
848 default:
849 RetVals.push_back(RetTyVT);
850 break;
851 case MVT::i1:
852 case MVT::i8:
853 case MVT::i16:
854 RetVals.push_back(MVT::i32);
855 break;
856 case MVT::f32:
Nate Begemanf63be7d2005-07-06 18:59:04 +0000857 if (X86ScalarSSE)
858 RetVals.push_back(MVT::f32);
859 else
860 RetVals.push_back(MVT::f64);
Chris Lattner239738a2005-05-14 08:48:15 +0000861 break;
862 case MVT::i64:
863 RetVals.push_back(MVT::i32);
864 RetVals.push_back(MVT::i32);
865 break;
866 }
867
868 std::vector<SDOperand> Ops;
869 Ops.push_back(Chain);
870 Ops.push_back(Callee);
871 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
872 // Callee pops all arg values on the stack.
873 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
874
875 // Pass register arguments as needed.
876 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
877
878 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
879 RetVals, Ops);
880 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
881
882 SDOperand ResultVal;
883 switch (RetTyVT) {
884 case MVT::isVoid: break;
885 default:
886 ResultVal = TheCall.getValue(1);
887 break;
888 case MVT::i1:
889 case MVT::i8:
890 case MVT::i16:
891 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
892 break;
893 case MVT::f32:
894 // FIXME: we would really like to remember that this FP_ROUND operation is
895 // okay to eliminate if we allow excess FP precision.
896 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
897 break;
898 case MVT::i64:
899 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
900 TheCall.getValue(2));
901 break;
902 }
903
904 return std::make_pair(ResultVal, Chain);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000905}
906
Chris Lattner381e8872005-05-15 05:46:45 +0000907SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
908 if (ReturnAddrIndex == 0) {
909 // Set up a frame object for the return address.
910 MachineFunction &MF = DAG.getMachineFunction();
911 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
912 }
913
914 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
915}
Chris Lattnerc6f41812005-05-12 23:06:28 +0000916
917
Chris Lattner14824582005-01-09 00:01:27 +0000918
919std::pair<SDOperand, SDOperand> X86TargetLowering::
920LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
921 SelectionDAG &DAG) {
922 SDOperand Result;
923 if (Depth) // Depths > 0 not supported yet!
924 Result = DAG.getConstant(0, getPointerTy());
925 else {
Chris Lattner381e8872005-05-15 05:46:45 +0000926 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000927 if (!isFrameAddress)
928 // Just load the return address
Chris Lattnerc6f41812005-05-12 23:06:28 +0000929 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
930 DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000931 else
932 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
933 DAG.getConstant(4, MVT::i32));
934 }
935 return std::make_pair(Result, Chain);
936}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000937
Chris Lattnera28381c2005-07-16 00:28:20 +0000938//===----------------------------------------------------------------------===//
939// X86 Custom Lowering Hooks
940//===----------------------------------------------------------------------===//
941
Chris Lattner67649df2005-05-14 06:52:07 +0000942/// LowerOperation - Provide custom lowering hooks for some operations.
943///
944SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
945 switch (Op.getOpcode()) {
946 default: assert(0 && "Should not custom lower this!");
947 case ISD::SINT_TO_FP:
948 assert(Op.getValueType() == MVT::f64 &&
949 Op.getOperand(0).getValueType() == MVT::i64 &&
950 "Unknown SINT_TO_FP to lower!");
951 // We lower sint64->FP into a store to a temporary stack slot, followed by a
952 // FILD64m node.
953 MachineFunction &MF = DAG.getMachineFunction();
954 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
955 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
956 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
957 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
958 std::vector<MVT::ValueType> RTs;
959 RTs.push_back(MVT::f64);
960 RTs.push_back(MVT::Other);
961 std::vector<SDOperand> Ops;
962 Ops.push_back(Store);
963 Ops.push_back(StackSlot);
964 return DAG.getNode(X86ISD::FILD64m, RTs, Ops);
965 }
966}
967
968
969//===----------------------------------------------------------------------===//
970// Pattern Matcher Implementation
971//===----------------------------------------------------------------------===//
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000972
Chris Lattner98a8ba02005-01-18 01:06:26 +0000973namespace {
974 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
975 /// SDOperand's instead of register numbers for the leaves of the matched
976 /// tree.
977 struct X86ISelAddressMode {
978 enum {
979 RegBase,
980 FrameIndexBase,
981 } BaseType;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000982
Chris Lattner98a8ba02005-01-18 01:06:26 +0000983 struct { // This is really a union, discriminated by BaseType!
984 SDOperand Reg;
985 int FrameIndex;
986 } Base;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000987
Chris Lattner98a8ba02005-01-18 01:06:26 +0000988 unsigned Scale;
989 SDOperand IndexReg;
990 unsigned Disp;
991 GlobalValue *GV;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000992
Chris Lattner98a8ba02005-01-18 01:06:26 +0000993 X86ISelAddressMode()
994 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
995 }
996 };
997}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000998
999
1000namespace {
1001 Statistic<>
1002 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
1003
1004 //===--------------------------------------------------------------------===//
1005 /// ISel - X86 specific code to select X86 machine instructions for
1006 /// SelectionDAG operations.
1007 ///
1008 class ISel : public SelectionDAGISel {
1009 /// ContainsFPCode - Every instruction we select that uses or defines a FP
1010 /// register should set this to true.
1011 bool ContainsFPCode;
1012
1013 /// X86Lowering - This object fully describes how to lower LLVM code to an
1014 /// X86-specific SelectionDAG.
1015 X86TargetLowering X86Lowering;
1016
Chris Lattner11333092005-01-11 03:11:44 +00001017 /// RegPressureMap - This keeps an approximate count of the number of
1018 /// registers required to evaluate each node in the graph.
1019 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001020
1021 /// ExprMap - As shared expressions are codegen'd, we keep track of which
1022 /// vreg the value is produced in, so we only emit one copy of each compiled
1023 /// tree.
1024 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001025
Chris Lattner381e8872005-05-15 05:46:45 +00001026 /// TheDAG - The DAG being selected during Select* operations.
1027 SelectionDAG *TheDAG;
Nate Begemanfb5792f2005-07-12 01:41:54 +00001028
1029 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
1030 /// make the right decision when generating code for different targets.
1031 const X86Subtarget *Subtarget;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001032 public:
1033 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
Nate Begemanfb5792f2005-07-12 01:41:54 +00001034 Subtarget = TM.getSubtarget<const X86Subtarget>();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001035 }
1036
Chris Lattner67b1c3c2005-01-21 21:35:14 +00001037 virtual const char *getPassName() const {
1038 return "X86 Pattern Instruction Selection";
1039 }
1040
Chris Lattner11333092005-01-11 03:11:44 +00001041 unsigned getRegPressure(SDOperand O) {
1042 return RegPressureMap[O.Val];
1043 }
1044 unsigned ComputeRegPressure(SDOperand O);
1045
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001046 /// InstructionSelectBasicBlock - This callback is invoked by
1047 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001048 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001049
Chris Lattner63602fb2005-05-13 07:38:09 +00001050 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
1051
Chris Lattner44129b52005-01-25 20:03:11 +00001052 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
1053 bool FloatPromoteOk = false);
Chris Lattnera5ade062005-01-11 21:19:59 +00001054 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +00001055 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattner30ea1e92005-01-19 07:37:26 +00001056 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001057 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +00001058 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001059 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
1060 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001061 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001062
1063 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
1064 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
1065 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner381e8872005-05-15 05:46:45 +00001066 bool EmitPotentialTailCall(SDNode *Node);
1067 void EmitFastCCToFastCCTailCall(SDNode *TailCallNode);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001068 void Select(SDOperand N);
1069 };
1070}
1071
Chris Lattner6415bb42005-05-10 03:53:18 +00001072/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
1073/// the main function.
1074static void EmitSpecialCodeForMain(MachineBasicBlock *BB,
1075 MachineFrameInfo *MFI) {
1076 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
1077 int CWFrameIdx = MFI->CreateStackObject(2, 2);
1078 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1079
1080 // Set the high part to be 64-bit precision.
1081 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1082 CWFrameIdx, 1).addImm(2);
1083
1084 // Reload the modified control word now.
1085 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1086}
1087
Chris Lattner63602fb2005-05-13 07:38:09 +00001088void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
1089 // If this function has live-in values, emit the copies from pregs to vregs at
1090 // the top of the function, before anything else.
1091 MachineBasicBlock *BB = MF.begin();
1092 if (MF.livein_begin() != MF.livein_end()) {
1093 SSARegMap *RegMap = MF.getSSARegMap();
1094 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
1095 E = MF.livein_end(); LI != E; ++LI) {
1096 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
1097 if (RC == X86::R8RegisterClass) {
1098 BuildMI(BB, X86::MOV8rr, 1, LI->second).addReg(LI->first);
1099 } else if (RC == X86::R16RegisterClass) {
1100 BuildMI(BB, X86::MOV16rr, 1, LI->second).addReg(LI->first);
1101 } else if (RC == X86::R32RegisterClass) {
1102 BuildMI(BB, X86::MOV32rr, 1, LI->second).addReg(LI->first);
1103 } else if (RC == X86::RFPRegisterClass) {
1104 BuildMI(BB, X86::FpMOV, 1, LI->second).addReg(LI->first);
Nate Begemanf63be7d2005-07-06 18:59:04 +00001105 } else if (RC == X86::RXMMRegisterClass) {
1106 BuildMI(BB, X86::MOVAPDrr, 1, LI->second).addReg(LI->first);
Chris Lattner63602fb2005-05-13 07:38:09 +00001107 } else {
1108 assert(0 && "Unknown regclass!");
1109 }
1110 }
1111 }
1112
1113
1114 // If this is main, emit special code for main.
1115 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
1116 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
1117}
1118
1119
Chris Lattner7dbcb752005-01-12 04:21:28 +00001120/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
1121/// when it has created a SelectionDAG for us to codegen.
1122void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
1123 // While we're doing this, keep track of whether we see any FP code for
1124 // FP_REG_KILL insertion.
1125 ContainsFPCode = false;
Chris Lattner6415bb42005-05-10 03:53:18 +00001126 MachineFunction *MF = BB->getParent();
Chris Lattner7dbcb752005-01-12 04:21:28 +00001127
1128 // Scan the PHI nodes that already are inserted into this basic block. If any
1129 // of them is a PHI of a floating point value, we need to insert an
1130 // FP_REG_KILL.
Chris Lattner6415bb42005-05-10 03:53:18 +00001131 SSARegMap *RegMap = MF->getSSARegMap();
Chris Lattner63602fb2005-05-13 07:38:09 +00001132 if (BB != MF->begin())
1133 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
1134 I != E; ++I) {
1135 assert(I->getOpcode() == X86::PHI &&
1136 "Isn't just PHI nodes?");
1137 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
1138 X86::RFPRegisterClass) {
1139 ContainsFPCode = true;
1140 break;
1141 }
Chris Lattner7dbcb752005-01-12 04:21:28 +00001142 }
Chris Lattner6415bb42005-05-10 03:53:18 +00001143
Chris Lattner7dbcb752005-01-12 04:21:28 +00001144 // Compute the RegPressureMap, which is an approximation for the number of
1145 // registers required to compute each node.
1146 ComputeRegPressure(DAG.getRoot());
1147
Chris Lattner381e8872005-05-15 05:46:45 +00001148 TheDAG = &DAG;
1149
Chris Lattner7dbcb752005-01-12 04:21:28 +00001150 // Codegen the basic block.
1151 Select(DAG.getRoot());
1152
Chris Lattner381e8872005-05-15 05:46:45 +00001153 TheDAG = 0;
1154
Chris Lattner7dbcb752005-01-12 04:21:28 +00001155 // Finally, look at all of the successors of this block. If any contain a PHI
1156 // node of FP type, we need to insert an FP_REG_KILL in this block.
1157 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
1158 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
1159 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
1160 I != E && I->getOpcode() == X86::PHI; ++I) {
1161 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
1162 X86::RFPRegisterClass) {
1163 ContainsFPCode = true;
1164 break;
1165 }
1166 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001167
Chris Lattnere3e0f272005-05-09 03:36:39 +00001168 // Final check, check LLVM BB's that are successors to the LLVM BB
1169 // corresponding to BB for FP PHI nodes.
1170 const BasicBlock *LLVMBB = BB->getBasicBlock();
1171 const PHINode *PN;
1172 if (!ContainsFPCode)
1173 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
1174 SI != E && !ContainsFPCode; ++SI)
1175 for (BasicBlock::const_iterator II = SI->begin();
1176 (PN = dyn_cast<PHINode>(II)); ++II)
1177 if (PN->getType()->isFloatingPoint()) {
1178 ContainsFPCode = true;
1179 break;
1180 }
1181
1182
Chris Lattner7dbcb752005-01-12 04:21:28 +00001183 // Insert FP_REG_KILL instructions into basic blocks that need them. This
1184 // only occurs due to the floating point stackifier not being aggressive
1185 // enough to handle arbitrary global stackification.
1186 //
1187 // Currently we insert an FP_REG_KILL instruction into each block that uses or
1188 // defines a floating point virtual register.
1189 //
1190 // When the global register allocators (like linear scan) finally update live
1191 // variable analysis, we can keep floating point values in registers across
1192 // basic blocks. This will be a huge win, but we are waiting on the global
1193 // allocators before we can do this.
1194 //
Chris Lattner71df3f82005-03-30 01:10:00 +00001195 if (ContainsFPCode) {
Chris Lattner7dbcb752005-01-12 04:21:28 +00001196 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
1197 ++NumFPKill;
1198 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001199
Chris Lattner7dbcb752005-01-12 04:21:28 +00001200 // Clear state used for selection.
1201 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +00001202 RegPressureMap.clear();
1203}
1204
1205
Chris Lattner11333092005-01-11 03:11:44 +00001206// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
1207// for the number of registers required to compute each node. This is basically
1208// computing a generalized form of the Sethi-Ullman number for each node.
1209unsigned ISel::ComputeRegPressure(SDOperand O) {
1210 SDNode *N = O.Val;
1211 unsigned &Result = RegPressureMap[N];
1212 if (Result) return Result;
1213
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001214 // FIXME: Should operations like CALL (which clobber lots o regs) have a
1215 // higher fixed cost??
1216
Chris Lattnerc4b6a782005-01-11 22:29:12 +00001217 if (N->getNumOperands() == 0) {
1218 Result = 1;
1219 } else {
1220 unsigned MaxRegUse = 0;
1221 unsigned NumExtraMaxRegUsers = 0;
1222 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1223 unsigned Regs;
1224 if (N->getOperand(i).getOpcode() == ISD::Constant)
1225 Regs = 0;
1226 else
1227 Regs = ComputeRegPressure(N->getOperand(i));
1228 if (Regs > MaxRegUse) {
1229 MaxRegUse = Regs;
1230 NumExtraMaxRegUsers = 0;
1231 } else if (Regs == MaxRegUse &&
1232 N->getOperand(i).getValueType() != MVT::Other) {
1233 ++NumExtraMaxRegUsers;
1234 }
Chris Lattner11333092005-01-11 03:11:44 +00001235 }
Chris Lattner90d1be72005-01-17 22:56:09 +00001236
1237 if (O.getOpcode() != ISD::TokenFactor)
1238 Result = MaxRegUse+NumExtraMaxRegUsers;
1239 else
Chris Lattner869e0432005-01-17 23:02:13 +00001240 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +00001241 }
Chris Lattnerafce4302005-01-12 02:19:06 +00001242
Chris Lattner837caa72005-01-11 23:21:30 +00001243 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +00001244 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001245}
1246
Chris Lattnerbf52d492005-01-20 16:50:16 +00001247/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
1248/// The DAG cannot have cycles in it, by definition, so the visited set is not
1249/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
1250/// reuse, so it prevents exponential cases.
1251///
1252static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
1253 std::set<SDNode*> &Visited) {
1254 if (N == Op) return true; // Found it.
1255 SDNode *Node = N.Val;
Chris Lattnerfb0f53f2005-01-21 21:43:02 +00001256 if (Node->getNumOperands() == 0 || // Leaf?
1257 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattnerbf52d492005-01-20 16:50:16 +00001258 if (!Visited.insert(Node).second) return false; // Already visited?
1259
1260 // Recurse for the first N-1 operands.
1261 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1262 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
1263 return true;
1264
1265 // Tail recurse for the last operand.
1266 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
1267}
1268
Chris Lattner98a8ba02005-01-18 01:06:26 +00001269X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
1270 X86AddressMode Result;
1271
1272 // If we need to emit two register operands, emit the one with the highest
1273 // register pressure first.
1274 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
1275 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +00001276 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +00001277 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +00001278 std::set<SDNode*> Visited;
1279 EmitBaseThenIndex = true;
1280 // If Base ends up pointing to Index, we must emit index first. This is
1281 // because of the way we fold loads, we may end up doing bad things with
1282 // the folded add.
1283 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
1284 EmitBaseThenIndex = false;
1285 } else {
1286 std::set<SDNode*> Visited;
1287 EmitBaseThenIndex = false;
1288 // If Base ends up pointing to Index, we must emit index first. This is
1289 // because of the way we fold loads, we may end up doing bad things with
1290 // the folded add.
1291 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
1292 EmitBaseThenIndex = true;
1293 }
1294
1295 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001296 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1297 Result.IndexReg = SelectExpr(IAM.IndexReg);
1298 } else {
1299 Result.IndexReg = SelectExpr(IAM.IndexReg);
1300 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1301 }
Chris Lattnerbf52d492005-01-20 16:50:16 +00001302
Chris Lattner98a8ba02005-01-18 01:06:26 +00001303 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
1304 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1305 } else if (IAM.IndexReg.Val) {
1306 Result.IndexReg = SelectExpr(IAM.IndexReg);
1307 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001308
Chris Lattner98a8ba02005-01-18 01:06:26 +00001309 switch (IAM.BaseType) {
1310 case X86ISelAddressMode::RegBase:
1311 Result.BaseType = X86AddressMode::RegBase;
1312 break;
1313 case X86ISelAddressMode::FrameIndexBase:
1314 Result.BaseType = X86AddressMode::FrameIndexBase;
1315 Result.Base.FrameIndex = IAM.Base.FrameIndex;
1316 break;
1317 default:
1318 assert(0 && "Unknown base type!");
1319 break;
1320 }
1321 Result.Scale = IAM.Scale;
1322 Result.Disp = IAM.Disp;
1323 Result.GV = IAM.GV;
1324 return Result;
1325}
1326
1327/// SelectAddress - Pattern match the maximal addressing mode for this node and
1328/// emit all of the leaf registers.
1329void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
1330 X86ISelAddressMode IAM;
1331 MatchAddress(N, IAM);
1332 AM = SelectAddrExprs(IAM);
1333}
1334
1335/// MatchAddress - Add the specified node to the specified addressing mode,
1336/// returning true if it cannot be done. This just pattern matches for the
1337/// addressing mode, it does not cause any code to be emitted. For that, use
1338/// SelectAddress.
1339bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001340 switch (N.getOpcode()) {
1341 default: break;
1342 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +00001343 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
1344 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001345 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
1346 return false;
1347 }
1348 break;
1349 case ISD::GlobalAddress:
1350 if (AM.GV == 0) {
Nate Begemanfb5792f2005-07-12 01:41:54 +00001351 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1352 // For Darwin, external and weak symbols are indirect, so we want to load
1353 // the value at address GV, not the value of GV itself. This means that
1354 // the GlobalAddress must be in the base or index register of the address,
1355 // not the GV offset field.
1356 if (Subtarget->getIndirectExternAndWeakGlobals() &&
1357 (GV->hasWeakLinkage() || GV->isExternal())) {
1358 break;
1359 } else {
1360 AM.GV = GV;
1361 return false;
1362 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001363 }
1364 break;
1365 case ISD::Constant:
1366 AM.Disp += cast<ConstantSDNode>(N)->getValue();
1367 return false;
1368 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +00001369 // We might have folded the load into this shift, so don't regen the value
1370 // if so.
1371 if (ExprMap.count(N)) break;
1372
Chris Lattner98a8ba02005-01-18 01:06:26 +00001373 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001374 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
1375 unsigned Val = CN->getValue();
1376 if (Val == 1 || Val == 2 || Val == 3) {
1377 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +00001378 SDOperand ShVal = N.Val->getOperand(0);
1379
1380 // Okay, we know that we have a scale by now. However, if the scaled
1381 // value is an add of something and a constant, we can fold the
1382 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +00001383 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +00001384 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001385 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +00001386 ConstantSDNode *AddVal =
1387 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
1388 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +00001389 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001390 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +00001391 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001392 return false;
1393 }
1394 }
1395 break;
Chris Lattner947d5442005-01-11 19:37:02 +00001396 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +00001397 // We might have folded the load into this mul, so don't regen the value if
1398 // so.
1399 if (ExprMap.count(N)) break;
1400
Chris Lattner947d5442005-01-11 19:37:02 +00001401 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +00001402 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
1403 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +00001404 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
1405 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
1406 AM.Scale = unsigned(CN->getValue())-1;
1407
1408 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001409 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +00001410
1411 // Okay, we know that we have a scale by now. However, if the scaled
1412 // value is an add of something and a constant, we can fold the
1413 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +00001414 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +00001415 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001416 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +00001417 ConstantSDNode *AddVal =
1418 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
1419 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001420 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001421 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +00001422 }
1423
1424 AM.IndexReg = AM.Base.Reg = Reg;
1425 return false;
1426 }
1427 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001428
1429 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +00001430 // We might have folded the load into this mul, so don't regen the value if
1431 // so.
1432 if (ExprMap.count(N)) break;
1433
Chris Lattner98a8ba02005-01-18 01:06:26 +00001434 X86ISelAddressMode Backup = AM;
1435 if (!MatchAddress(N.Val->getOperand(0), AM) &&
1436 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001437 return false;
1438 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +00001439 if (!MatchAddress(N.Val->getOperand(1), AM) &&
1440 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +00001441 return false;
1442 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001443 break;
1444 }
1445 }
1446
Chris Lattnera95589b2005-01-11 04:40:19 +00001447 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +00001448 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +00001449 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +00001450 if (AM.IndexReg.Val == 0) {
1451 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +00001452 AM.Scale = 1;
1453 return false;
1454 }
1455
1456 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001457 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +00001458 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001459
1460 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +00001461 AM.BaseType = X86ISelAddressMode::RegBase;
1462 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001463 return false;
1464}
1465
1466/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
1467/// assuming that the temporary registers are in the 8-bit register class.
1468///
1469/// Tmp1 = setcc1
1470/// Tmp2 = setcc2
1471/// DestReg = logicalop Tmp1, Tmp2
1472///
1473static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
1474 unsigned SetCC2, unsigned LogicalOp,
1475 unsigned DestReg) {
1476 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
1477 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1478 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1479 BuildMI(BB, SetCC1, 0, Tmp1);
1480 BuildMI(BB, SetCC2, 0, Tmp2);
1481 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
1482}
1483
1484/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
1485/// condition codes match the specified SetCCOpcode. Note that some conditions
1486/// require multiple instructions to generate the correct value.
1487static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
1488 ISD::CondCode SetCCOpcode, bool isFP) {
1489 unsigned Opc;
1490 if (!isFP) {
1491 switch (SetCCOpcode) {
1492 default: assert(0 && "Illegal integer SetCC!");
1493 case ISD::SETEQ: Opc = X86::SETEr; break;
1494 case ISD::SETGT: Opc = X86::SETGr; break;
1495 case ISD::SETGE: Opc = X86::SETGEr; break;
1496 case ISD::SETLT: Opc = X86::SETLr; break;
1497 case ISD::SETLE: Opc = X86::SETLEr; break;
1498 case ISD::SETNE: Opc = X86::SETNEr; break;
1499 case ISD::SETULT: Opc = X86::SETBr; break;
1500 case ISD::SETUGT: Opc = X86::SETAr; break;
1501 case ISD::SETULE: Opc = X86::SETBEr; break;
1502 case ISD::SETUGE: Opc = X86::SETAEr; break;
1503 }
1504 } else {
1505 // On a floating point condition, the flags are set as follows:
1506 // ZF PF CF op
1507 // 0 | 0 | 0 | X > Y
1508 // 0 | 0 | 1 | X < Y
1509 // 1 | 0 | 0 | X == Y
1510 // 1 | 1 | 1 | unordered
1511 //
1512 switch (SetCCOpcode) {
1513 default: assert(0 && "Invalid FP setcc!");
1514 case ISD::SETUEQ:
1515 case ISD::SETEQ:
1516 Opc = X86::SETEr; // True if ZF = 1
1517 break;
1518 case ISD::SETOGT:
1519 case ISD::SETGT:
1520 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
1521 break;
1522 case ISD::SETOGE:
1523 case ISD::SETGE:
1524 Opc = X86::SETAEr; // True if CF = 0
1525 break;
1526 case ISD::SETULT:
1527 case ISD::SETLT:
1528 Opc = X86::SETBr; // True if CF = 1
1529 break;
1530 case ISD::SETULE:
1531 case ISD::SETLE:
1532 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
1533 break;
1534 case ISD::SETONE:
1535 case ISD::SETNE:
1536 Opc = X86::SETNEr; // True if ZF = 0
1537 break;
1538 case ISD::SETUO:
1539 Opc = X86::SETPr; // True if PF = 1
1540 break;
1541 case ISD::SETO:
1542 Opc = X86::SETNPr; // True if PF = 0
1543 break;
1544 case ISD::SETOEQ: // !PF & ZF
1545 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
1546 return;
1547 case ISD::SETOLT: // !PF & CF
1548 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
1549 return;
1550 case ISD::SETOLE: // !PF & (CF || ZF)
1551 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
1552 return;
1553 case ISD::SETUGT: // PF | (!ZF & !CF)
1554 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
1555 return;
1556 case ISD::SETUGE: // PF | !CF
1557 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
1558 return;
1559 case ISD::SETUNE: // PF | !ZF
1560 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
1561 return;
1562 }
1563 }
1564 BuildMI(BB, Opc, 0, DestReg);
1565}
1566
1567
1568/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
1569/// the Dest block if the Cond condition is true. If we cannot fold this
1570/// condition into the branch, return true.
1571///
Chris Lattner6c07aee2005-01-11 04:06:27 +00001572bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
1573 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001574 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
1575 // B) using two conditional branches instead of one condbr, two setcc's, and
1576 // an or.
1577 if ((Cond.getOpcode() == ISD::OR ||
1578 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
1579 // And and or set the flags for us, so there is no need to emit a TST of the
1580 // result. It is only safe to do this if there is only a single use of the
1581 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +00001582 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001583 SelectExpr(Cond);
1584 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
1585 return false;
1586 }
1587
1588 // Codegen br not C -> JE.
1589 if (Cond.getOpcode() == ISD::XOR)
1590 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
1591 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +00001592 unsigned CondR;
1593 if (getRegPressure(Chain) > getRegPressure(Cond)) {
1594 Select(Chain);
1595 CondR = SelectExpr(Cond.Val->getOperand(0));
1596 } else {
1597 CondR = SelectExpr(Cond.Val->getOperand(0));
1598 Select(Chain);
1599 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001600 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
1601 BuildMI(BB, X86::JE, 1).addMBB(Dest);
1602 return false;
1603 }
1604
1605 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
1606 if (SetCC == 0)
1607 return true; // Can only handle simple setcc's so far.
1608
1609 unsigned Opc;
1610
1611 // Handle integer conditions first.
1612 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1613 switch (SetCC->getCondition()) {
1614 default: assert(0 && "Illegal integer SetCC!");
1615 case ISD::SETEQ: Opc = X86::JE; break;
1616 case ISD::SETGT: Opc = X86::JG; break;
1617 case ISD::SETGE: Opc = X86::JGE; break;
1618 case ISD::SETLT: Opc = X86::JL; break;
1619 case ISD::SETLE: Opc = X86::JLE; break;
1620 case ISD::SETNE: Opc = X86::JNE; break;
1621 case ISD::SETULT: Opc = X86::JB; break;
1622 case ISD::SETUGT: Opc = X86::JA; break;
1623 case ISD::SETULE: Opc = X86::JBE; break;
1624 case ISD::SETUGE: Opc = X86::JAE; break;
1625 }
Chris Lattner6c07aee2005-01-11 04:06:27 +00001626 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001627 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001628 BuildMI(BB, Opc, 1).addMBB(Dest);
1629 return false;
1630 }
1631
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001632 unsigned Opc2 = 0; // Second branch if needed.
1633
1634 // On a floating point condition, the flags are set as follows:
1635 // ZF PF CF op
1636 // 0 | 0 | 0 | X > Y
1637 // 0 | 0 | 1 | X < Y
1638 // 1 | 0 | 0 | X == Y
1639 // 1 | 1 | 1 | unordered
1640 //
1641 switch (SetCC->getCondition()) {
1642 default: assert(0 && "Invalid FP setcc!");
1643 case ISD::SETUEQ:
1644 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
1645 case ISD::SETOGT:
1646 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
1647 case ISD::SETOGE:
1648 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
1649 case ISD::SETULT:
1650 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
1651 case ISD::SETULE:
1652 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
1653 case ISD::SETONE:
1654 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
1655 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
1656 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
1657 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1658 Opc = X86::JA; // ZF = 0 & CF = 0
1659 Opc2 = X86::JP; // PF = 1
1660 break;
1661 case ISD::SETUGE: // PF = 1 | CF = 0
1662 Opc = X86::JAE; // CF = 0
1663 Opc2 = X86::JP; // PF = 1
1664 break;
1665 case ISD::SETUNE: // PF = 1 | ZF = 0
1666 Opc = X86::JNE; // ZF = 0
1667 Opc2 = X86::JP; // PF = 1
1668 break;
1669 case ISD::SETOEQ: // PF = 0 & ZF = 1
1670 //X86::JNP, X86::JE
1671 //X86::AND8rr
1672 return true; // FIXME: Emit more efficient code for this branch.
1673 case ISD::SETOLT: // PF = 0 & CF = 1
1674 //X86::JNP, X86::JB
1675 //X86::AND8rr
1676 return true; // FIXME: Emit more efficient code for this branch.
1677 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1678 //X86::JNP, X86::JBE
1679 //X86::AND8rr
1680 return true; // FIXME: Emit more efficient code for this branch.
1681 }
1682
Chris Lattner6c07aee2005-01-11 04:06:27 +00001683 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001684 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001685 BuildMI(BB, Opc, 1).addMBB(Dest);
1686 if (Opc2)
1687 BuildMI(BB, Opc2, 1).addMBB(Dest);
1688 return false;
1689}
1690
Chris Lattner24aad1b2005-01-10 22:10:13 +00001691/// EmitSelectCC - Emit code into BB that performs a select operation between
1692/// the two registers RTrue and RFalse, generating a result into RDest. Return
1693/// true if the fold cannot be performed.
1694///
1695void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
1696 unsigned RTrue, unsigned RFalse, unsigned RDest) {
1697 enum Condition {
1698 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
1699 NOT_SET
1700 } CondCode = NOT_SET;
1701
1702 static const unsigned CMOVTAB16[] = {
1703 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
1704 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001705 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001706 };
1707 static const unsigned CMOVTAB32[] = {
1708 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
1709 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001710 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001711 };
1712 static const unsigned CMOVTABFP[] = {
1713 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
1714 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
1715 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
1716 };
Nate Begeman16b04f32005-07-15 00:38:55 +00001717 static const int SSE_CMOVTAB[] = {
Nate Begemanf63be7d2005-07-06 18:59:04 +00001718 0 /* CMPEQSS */, 4 /* CMPNEQSS */, 1 /* CMPLTSS */, 2 /* CMPLESS */,
Nate Begeman16b04f32005-07-15 00:38:55 +00001719 1 /* CMPLTSS */, 2 /* CMPLESS */, /*missing*/0, /*missing*/0,
Nate Begemanf63be7d2005-07-06 18:59:04 +00001720 /*missing*/0, /*missing*/0, /*missing*/0, /*missing*/0
1721 };
Chris Lattner24aad1b2005-01-10 22:10:13 +00001722
1723 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
1724 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1725 switch (SetCC->getCondition()) {
1726 default: assert(0 && "Unknown integer comparison!");
1727 case ISD::SETEQ: CondCode = EQ; break;
1728 case ISD::SETGT: CondCode = GT; break;
1729 case ISD::SETGE: CondCode = GE; break;
1730 case ISD::SETLT: CondCode = LT; break;
1731 case ISD::SETLE: CondCode = LE; break;
1732 case ISD::SETNE: CondCode = NE; break;
1733 case ISD::SETULT: CondCode = B; break;
1734 case ISD::SETUGT: CondCode = A; break;
1735 case ISD::SETULE: CondCode = BE; break;
1736 case ISD::SETUGE: CondCode = AE; break;
1737 }
Nate Begemanf63be7d2005-07-06 18:59:04 +00001738 } else if (X86ScalarSSE) {
1739 switch (SetCC->getCondition()) {
1740 default: assert(0 && "Unknown scalar fp comparison!");
1741 case ISD::SETEQ: CondCode = EQ; break;
1742 case ISD::SETNE: CondCode = NE; break;
1743 case ISD::SETULT:
1744 case ISD::SETLT: CondCode = LT; break;
1745 case ISD::SETULE:
1746 case ISD::SETLE: CondCode = LE; break;
1747 case ISD::SETUGT:
1748 case ISD::SETGT: CondCode = GT; break;
1749 case ISD::SETUGE:
1750 case ISD::SETGE: CondCode = GE; break;
1751 }
Chris Lattner24aad1b2005-01-10 22:10:13 +00001752 } else {
1753 // On a floating point condition, the flags are set as follows:
1754 // ZF PF CF op
1755 // 0 | 0 | 0 | X > Y
1756 // 0 | 0 | 1 | X < Y
1757 // 1 | 0 | 0 | X == Y
1758 // 1 | 1 | 1 | unordered
1759 //
1760 switch (SetCC->getCondition()) {
1761 default: assert(0 && "Unknown FP comparison!");
1762 case ISD::SETUEQ:
1763 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
1764 case ISD::SETOGT:
1765 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
1766 case ISD::SETOGE:
1767 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
1768 case ISD::SETULT:
1769 case ISD::SETLT: CondCode = B; break; // True if CF = 1
1770 case ISD::SETULE:
1771 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
1772 case ISD::SETONE:
1773 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
1774 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1775 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1776 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1777 case ISD::SETUGE: // PF = 1 | CF = 0
1778 case ISD::SETUNE: // PF = 1 | ZF = 0
1779 case ISD::SETOEQ: // PF = 0 & ZF = 1
1780 case ISD::SETOLT: // PF = 0 & CF = 1
1781 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1782 // We cannot emit this comparison as a single cmov.
1783 break;
1784 }
1785 }
1786 }
1787
Nate Begemanf63be7d2005-07-06 18:59:04 +00001788 // There's no SSE equivalent of FCMOVE. In some cases we can fake it up, in
1789 // Others we will have to do the PowerPC thing and generate an MBB for the
1790 // true and false values and select between them with a PHI.
Nate Begeman16b04f32005-07-15 00:38:55 +00001791 if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) {
1792 if (0 && CondCode != NOT_SET) {
1793 // FIXME: check for min and max
Nate Begemanf63be7d2005-07-06 18:59:04 +00001794 } else {
Nate Begeman16b04f32005-07-15 00:38:55 +00001795 // FIXME: emit a direct compare and branch rather than setting a cond reg
1796 // and testing it.
Nate Begemanf63be7d2005-07-06 18:59:04 +00001797 unsigned CondReg = SelectExpr(Cond);
1798 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1799
1800 // Create an iterator with which to insert the MBB for copying the false
1801 // value and the MBB to hold the PHI instruction for this SetCC.
1802 MachineBasicBlock *thisMBB = BB;
1803 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1804 ilist<MachineBasicBlock>::iterator It = BB;
1805 ++It;
1806
1807 // thisMBB:
1808 // ...
1809 // TrueVal = ...
1810 // cmpTY ccX, r1, r2
1811 // bCC sinkMBB
1812 // fallthrough --> copy0MBB
1813 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1814 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1815 BuildMI(BB, X86::JNE, 1).addMBB(sinkMBB);
1816 MachineFunction *F = BB->getParent();
1817 F->getBasicBlockList().insert(It, copy0MBB);
1818 F->getBasicBlockList().insert(It, sinkMBB);
1819 // Update machine-CFG edges
1820 BB->addSuccessor(copy0MBB);
1821 BB->addSuccessor(sinkMBB);
1822
1823 // copy0MBB:
1824 // %FalseValue = ...
1825 // # fallthrough to sinkMBB
1826 BB = copy0MBB;
1827 // Update machine-CFG edges
1828 BB->addSuccessor(sinkMBB);
1829
1830 // sinkMBB:
1831 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1832 // ...
1833 BB = sinkMBB;
1834 BuildMI(BB, X86::PHI, 4, RDest).addReg(RFalse)
1835 .addMBB(copy0MBB).addReg(RTrue).addMBB(thisMBB);
1836 }
1837 return;
1838 }
1839
Chris Lattner24aad1b2005-01-10 22:10:13 +00001840 unsigned Opc = 0;
1841 if (CondCode != NOT_SET) {
1842 switch (SVT) {
1843 default: assert(0 && "Cannot select this type!");
1844 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1845 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001846 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001847 }
1848 }
Nate Begemanf63be7d2005-07-06 18:59:04 +00001849
Chris Lattner24aad1b2005-01-10 22:10:13 +00001850 // Finally, if we weren't able to fold this, just emit the condition and test
1851 // it.
1852 if (CondCode == NOT_SET || Opc == 0) {
1853 // Get the condition into the zero flag.
1854 unsigned CondReg = SelectExpr(Cond);
1855 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1856
1857 switch (SVT) {
1858 default: assert(0 && "Cannot select this type!");
1859 case MVT::i16: Opc = X86::CMOVE16rr; break;
1860 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001861 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001862 }
1863 } else {
1864 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001865 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001866 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001867 }
1868 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1869}
1870
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001871void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001872 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001873 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1874 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001875 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001876 switch (RHS.getValueType()) {
1877 default: break;
1878 case MVT::i1:
1879 case MVT::i8: Opc = X86::CMP8mi; break;
1880 case MVT::i16: Opc = X86::CMP16mi; break;
1881 case MVT::i32: Opc = X86::CMP32mi; break;
1882 }
1883 if (Opc) {
1884 X86AddressMode AM;
1885 EmitFoldedLoad(LHS, AM);
1886 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1887 return;
1888 }
1889 }
1890
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001891 switch (RHS.getValueType()) {
1892 default: break;
1893 case MVT::i1:
1894 case MVT::i8: Opc = X86::CMP8ri; break;
1895 case MVT::i16: Opc = X86::CMP16ri; break;
1896 case MVT::i32: Opc = X86::CMP32ri; break;
1897 }
1898 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001899 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001900 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1901 return;
1902 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001903 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
Nate Begemanf63be7d2005-07-06 18:59:04 +00001904 if (!X86ScalarSSE && (CN->isExactlyValue(+0.0) ||
1905 CN->isExactlyValue(-0.0))) {
Chris Lattner7f2afac2005-01-14 22:37:41 +00001906 unsigned Reg = SelectExpr(LHS);
1907 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1908 BuildMI(BB, X86::FNSTSW8r, 0);
1909 BuildMI(BB, X86::SAHF, 1);
Chris Lattner7805fa42005-03-17 16:29:26 +00001910 return;
Chris Lattner7f2afac2005-01-14 22:37:41 +00001911 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001912 }
1913
Chris Lattneref6806c2005-01-12 02:02:48 +00001914 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001915 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001916 switch (RHS.getValueType()) {
1917 default: break;
1918 case MVT::i1:
1919 case MVT::i8: Opc = X86::CMP8mr; break;
1920 case MVT::i16: Opc = X86::CMP16mr; break;
1921 case MVT::i32: Opc = X86::CMP32mr; break;
1922 }
1923 if (Opc) {
1924 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001925 EmitFoldedLoad(LHS, AM);
1926 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001927 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1928 return;
1929 }
1930 }
1931
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001932 switch (LHS.getValueType()) {
1933 default: assert(0 && "Cannot compare this value!");
1934 case MVT::i1:
1935 case MVT::i8: Opc = X86::CMP8rr; break;
1936 case MVT::i16: Opc = X86::CMP16rr; break;
1937 case MVT::i32: Opc = X86::CMP32rr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00001938 case MVT::f32: Opc = X86::UCOMISSrr; break;
1939 case MVT::f64: Opc = X86ScalarSSE ? X86::UCOMISDrr : X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001940 }
Chris Lattner11333092005-01-11 03:11:44 +00001941 unsigned Tmp1, Tmp2;
1942 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1943 Tmp1 = SelectExpr(LHS);
1944 Tmp2 = SelectExpr(RHS);
1945 } else {
1946 Tmp2 = SelectExpr(RHS);
1947 Tmp1 = SelectExpr(LHS);
1948 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001949 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1950}
1951
Chris Lattnera5ade062005-01-11 21:19:59 +00001952/// isFoldableLoad - Return true if this is a load instruction that can safely
1953/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00001954bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1955 if (Op.getOpcode() == ISD::LOAD) {
1956 // FIXME: currently can't fold constant pool indexes.
1957 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1958 return false;
1959 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001960 cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::f32) {
Chris Lattner44129b52005-01-25 20:03:11 +00001961 // FIXME: currently can't fold constant pool indexes.
1962 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1963 return false;
1964 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001965 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00001966 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001967
1968 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001969 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1970 if (ExprMap.count(Op.getValue(1))) return false;
1971 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001972 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001973
Chris Lattner4ff348b2005-01-17 06:26:58 +00001974 // If there is not just one use of its value, we cannot fold.
1975 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1976
1977 // Finally, we cannot fold the load into the operation if this would induce a
1978 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1979 // operand of the operation we are folding the load into) can possible use the
1980 // chain node defined by the load.
1981 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1982 std::set<SDNode*> Visited;
1983 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1984 return false;
1985 }
1986 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001987}
1988
Chris Lattner4ff348b2005-01-17 06:26:58 +00001989
Chris Lattnera5ade062005-01-11 21:19:59 +00001990/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1991/// and compute the address being loaded into AM.
1992void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1993 SDOperand Chain = Op.getOperand(0);
1994 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001995
Chris Lattnera5ade062005-01-11 21:19:59 +00001996 if (getRegPressure(Chain) > getRegPressure(Address)) {
1997 Select(Chain);
1998 SelectAddress(Address, AM);
1999 } else {
2000 SelectAddress(Address, AM);
2001 Select(Chain);
2002 }
2003
2004 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00002005 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
2006 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00002007 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00002008 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002009}
2010
Chris Lattner30ea1e92005-01-19 07:37:26 +00002011// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
2012// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
2013// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
2014// return true.
2015bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00002016 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
2017 // good!
2018 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
2019 std::swap(Op1, Op2); // Op1 is the SHL now.
2020 } else {
2021 return false; // No match
2022 }
2023
2024 SDOperand ShlVal = Op1.getOperand(0);
2025 SDOperand ShlAmt = Op1.getOperand(1);
2026 SDOperand ShrVal = Op2.getOperand(0);
2027 SDOperand ShrAmt = Op2.getOperand(1);
2028
Chris Lattner30ea1e92005-01-19 07:37:26 +00002029 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
2030
Chris Lattner85716372005-01-19 06:18:43 +00002031 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
2032 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
2033 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00002034 if (SubCST->getValue() == RegSize) {
2035 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00002036 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00002037 if (ShrVal == ShlVal) {
2038 unsigned Reg, ShAmt;
2039 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
2040 Reg = SelectExpr(ShrVal);
2041 ShAmt = SelectExpr(ShrAmt);
2042 } else {
2043 ShAmt = SelectExpr(ShrAmt);
2044 Reg = SelectExpr(ShrVal);
2045 }
2046 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2047 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
2048 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
2049 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
2050 return true;
2051 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00002052 unsigned AReg, BReg;
2053 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00002054 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002055 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00002056 } else {
Chris Lattner85716372005-01-19 06:18:43 +00002057 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002058 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00002059 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00002060 unsigned ShAmt = SelectExpr(ShrAmt);
2061 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2062 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
2063 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00002064 return true;
2065 }
2066 }
2067
Chris Lattner4053b1e2005-01-19 08:07:05 +00002068 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
2069 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
2070 if (SubCST->getValue() == RegSize) {
2071 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
2072 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
2073 if (ShrVal == ShlVal) {
2074 unsigned Reg, ShAmt;
2075 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
2076 Reg = SelectExpr(ShrVal);
2077 ShAmt = SelectExpr(ShlAmt);
2078 } else {
2079 ShAmt = SelectExpr(ShlAmt);
2080 Reg = SelectExpr(ShrVal);
2081 }
2082 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2083 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
2084 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
2085 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
2086 return true;
2087 } else if (RegSize != 8) {
2088 unsigned AReg, BReg;
2089 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002090 AReg = SelectExpr(ShlVal);
2091 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002092 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002093 BReg = SelectExpr(ShrVal);
2094 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002095 }
2096 unsigned ShAmt = SelectExpr(ShlAmt);
2097 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2098 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
2099 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
2100 return true;
2101 }
2102 }
Chris Lattner85716372005-01-19 06:18:43 +00002103
Chris Lattner4053b1e2005-01-19 08:07:05 +00002104 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
2105 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
2106 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
2107 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
2108 // (A >> 5) | (A << 27) --> ROR A, 5
2109 // (A >> 5) | (B << 27) --> SHRD A, B, 5
2110 if (ShrVal == ShlVal) {
2111 unsigned Reg = SelectExpr(ShrVal);
2112 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
2113 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
2114 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
2115 return true;
2116 } else if (RegSize != 8) {
2117 unsigned AReg, BReg;
2118 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00002119 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002120 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002121 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00002122 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002123 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002124 }
2125 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
2126 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
2127 .addImm(ShrCst->getValue());
2128 return true;
2129 }
2130 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002131
Chris Lattner85716372005-01-19 06:18:43 +00002132 return false;
2133}
2134
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002135unsigned ISel::SelectExpr(SDOperand N) {
2136 unsigned Result;
2137 unsigned Tmp1, Tmp2, Tmp3;
2138 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00002139 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00002140 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00002141
Chris Lattner7f2afac2005-01-14 22:37:41 +00002142 if (Node->getOpcode() == ISD::CopyFromReg) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00002143 if (MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()) ||
2144 cast<RegSDNode>(Node)->getReg() == X86::ESP) {
2145 // Just use the specified register as our input.
2146 return cast<RegSDNode>(Node)->getReg();
2147 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00002148 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002149
Chris Lattnera5ade062005-01-11 21:19:59 +00002150 unsigned &Reg = ExprMap[N];
2151 if (Reg) return Reg;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002152
Chris Lattnerb38a7492005-04-02 04:01:14 +00002153 switch (N.getOpcode()) {
2154 default:
Chris Lattnera5ade062005-01-11 21:19:59 +00002155 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnerb38a7492005-04-02 04:01:14 +00002156 MakeReg(N.getValueType()) : 1;
2157 break;
Chris Lattner239738a2005-05-14 08:48:15 +00002158 case X86ISD::TAILCALL:
2159 case X86ISD::CALL:
Chris Lattnera5ade062005-01-11 21:19:59 +00002160 // If this is a call instruction, make sure to prepare ALL of the result
2161 // values as well as the chain.
Chris Lattner239738a2005-05-14 08:48:15 +00002162 ExprMap[N.getValue(0)] = 1;
2163 if (Node->getNumValues() > 1) {
2164 Result = MakeReg(Node->getValueType(1));
2165 ExprMap[N.getValue(1)] = Result;
2166 for (unsigned i = 2, e = Node->getNumValues(); i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00002167 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattner239738a2005-05-14 08:48:15 +00002168 } else {
2169 Result = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002170 }
Chris Lattnerb38a7492005-04-02 04:01:14 +00002171 break;
2172 case ISD::ADD_PARTS:
2173 case ISD::SUB_PARTS:
2174 case ISD::SHL_PARTS:
2175 case ISD::SRL_PARTS:
2176 case ISD::SRA_PARTS:
2177 Result = MakeReg(Node->getValueType(0));
2178 ExprMap[N.getValue(0)] = Result;
2179 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
2180 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
2181 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002182 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002183
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002184 switch (N.getOpcode()) {
2185 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00002186 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002187 assert(0 && "Node not handled!\n");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002188 case ISD::FP_EXTEND:
2189 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
2190 Tmp1 = SelectExpr(N.getOperand(0));
2191 BuildMI(BB, X86::CVTSS2SDrr, 1, Result).addReg(Tmp1);
2192 return Result;
Nate Begeman16b04f32005-07-15 00:38:55 +00002193 case ISD::FP_ROUND:
2194 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
2195 Tmp1 = SelectExpr(N.getOperand(0));
2196 BuildMI(BB, X86::CVTSD2SSrr, 1, Result).addReg(Tmp1);
2197 return Result;
Chris Lattnerc6f41812005-05-12 23:06:28 +00002198 case ISD::CopyFromReg:
2199 Select(N.getOperand(0));
2200 if (Result == 1) {
2201 Reg = Result = ExprMap[N.getValue(0)] =
2202 MakeReg(N.getValue(0).getValueType());
2203 }
2204 switch (Node->getValueType(0)) {
2205 default: assert(0 && "Cannot CopyFromReg this!");
2206 case MVT::i1:
2207 case MVT::i8:
2208 BuildMI(BB, X86::MOV8rr, 1,
2209 Result).addReg(cast<RegSDNode>(Node)->getReg());
2210 return Result;
2211 case MVT::i16:
2212 BuildMI(BB, X86::MOV16rr, 1,
2213 Result).addReg(cast<RegSDNode>(Node)->getReg());
2214 return Result;
2215 case MVT::i32:
2216 BuildMI(BB, X86::MOV32rr, 1,
2217 Result).addReg(cast<RegSDNode>(Node)->getReg());
2218 return Result;
2219 }
2220
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002221 case ISD::FrameIndex:
2222 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
2223 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
2224 return Result;
2225 case ISD::ConstantPool:
2226 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
2227 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
2228 return Result;
2229 case ISD::ConstantFP:
2230 ContainsFPCode = true;
2231 Tmp1 = Result; // Intermediate Register
2232 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
2233 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
2234 Tmp1 = MakeReg(MVT::f64);
2235
2236 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
2237 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
2238 BuildMI(BB, X86::FLD0, 0, Tmp1);
2239 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
2240 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
2241 BuildMI(BB, X86::FLD1, 0, Tmp1);
2242 else
2243 assert(0 && "Unexpected constant!");
2244 if (Tmp1 != Result)
2245 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
2246 return Result;
2247 case ISD::Constant:
2248 switch (N.getValueType()) {
2249 default: assert(0 && "Cannot use constants of this type!");
2250 case MVT::i1:
2251 case MVT::i8: Opc = X86::MOV8ri; break;
2252 case MVT::i16: Opc = X86::MOV16ri; break;
2253 case MVT::i32: Opc = X86::MOV32ri; break;
2254 }
2255 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
2256 return Result;
Chris Lattner7ce7eff2005-04-01 22:46:45 +00002257 case ISD::UNDEF:
2258 if (Node->getValueType(0) == MVT::f64) {
2259 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
2260 BuildMI(BB, X86::FLD0, 0, Result);
2261 } else {
2262 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
2263 }
2264 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002265 case ISD::GlobalAddress: {
2266 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanfb5792f2005-07-12 01:41:54 +00002267 // For Darwin, external and weak symbols are indirect, so we want to load
2268 // the value at address GV, not the value of GV itself.
2269 if (Subtarget->getIndirectExternAndWeakGlobals() &&
2270 (GV->hasWeakLinkage() || GV->isExternal())) {
2271 BuildMI(BB, X86::MOV32rm, 4, Result).addReg(0).addZImm(1).addReg(0)
2272 .addGlobalAddress(GV, false, 0);
2273 } else {
2274 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
2275 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002276 return Result;
2277 }
2278 case ISD::ExternalSymbol: {
2279 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
2280 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
2281 return Result;
2282 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002283 case ISD::ZERO_EXTEND: {
2284 int DestIs16 = N.getValueType() == MVT::i16;
2285 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00002286
2287 // FIXME: This hack is here for zero extension casts from bool to i8. This
2288 // would not be needed if bools were promoted by Legalize.
2289 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002290 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00002291 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
2292 return Result;
2293 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002294
Chris Lattner4ff348b2005-01-17 06:26:58 +00002295 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002296 static const unsigned Opc[3] = {
2297 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
2298 };
2299
2300 X86AddressMode AM;
2301 EmitFoldedLoad(N.getOperand(0), AM);
2302 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002303
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002304 return Result;
2305 }
2306
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002307 static const unsigned Opc[3] = {
2308 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
2309 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002310 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002311 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
2312 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002313 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002314 case ISD::SIGN_EXTEND: {
2315 int DestIs16 = N.getValueType() == MVT::i16;
2316 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
2317
Chris Lattner590d8002005-01-09 18:52:44 +00002318 // FIXME: Legalize should promote bools to i8!
2319 assert(N.getOperand(0).getValueType() != MVT::i1 &&
2320 "Sign extend from bool not implemented!");
2321
Chris Lattner4ff348b2005-01-17 06:26:58 +00002322 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002323 static const unsigned Opc[3] = {
2324 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
2325 };
2326
2327 X86AddressMode AM;
2328 EmitFoldedLoad(N.getOperand(0), AM);
2329 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
2330 return Result;
2331 }
2332
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002333 static const unsigned Opc[3] = {
2334 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
2335 };
2336 Tmp1 = SelectExpr(N.getOperand(0));
2337 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
2338 return Result;
2339 }
2340 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00002341 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00002342 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00002343 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00002344 switch (N.getValueType()) {
2345 default: assert(0 && "Unknown truncate!");
2346 case MVT::i1:
2347 case MVT::i8: Opc = X86::MOV8rm; break;
2348 case MVT::i16: Opc = X86::MOV16rm; break;
2349 }
2350 X86AddressMode AM;
2351 EmitFoldedLoad(N.getOperand(0), AM);
2352 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2353 return Result;
2354 }
2355
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002356 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
2357 // a move out of AX or AL.
2358 switch (N.getOperand(0).getValueType()) {
2359 default: assert(0 && "Unknown truncate!");
2360 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
2361 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
2362 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
2363 }
2364 Tmp1 = SelectExpr(N.getOperand(0));
2365 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2366
2367 switch (N.getValueType()) {
2368 default: assert(0 && "Unknown truncate!");
2369 case MVT::i1:
2370 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
2371 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
2372 }
2373 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
2374 return Result;
2375
Chris Lattnera28381c2005-07-16 00:28:20 +00002376 case ISD::SINT_TO_FP: {
Nate Begemanf63be7d2005-07-06 18:59:04 +00002377 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2378 unsigned PromoteOpcode = 0;
2379
Nate Begeman5a8441e2005-07-16 02:02:34 +00002380 // We can handle any sint to fp with the direct sse conversion instructions.
Nate Begemanf63be7d2005-07-06 18:59:04 +00002381 if (X86ScalarSSE) {
Nate Begeman5a8441e2005-07-16 02:02:34 +00002382 Opc = (N.getValueType() == MVT::f64) ? X86::CVTSI2SDrr : X86::CVTSI2SSrr;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002383 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2384 return Result;
2385 }
2386
Chris Lattneref7ba072005-01-11 03:50:45 +00002387 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00002388
Chris Lattner590d8002005-01-09 18:52:44 +00002389 // Spill the integer to memory and reload it from there.
Nate Begeman5a8441e2005-07-16 02:02:34 +00002390 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
Chris Lattner590d8002005-01-09 18:52:44 +00002391 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
2392 MachineFunction *F = BB->getParent();
2393 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2394
2395 switch (SrcTy) {
Chris Lattner590d8002005-01-09 18:52:44 +00002396 case MVT::i32:
Chris Lattnera28381c2005-07-16 00:28:20 +00002397 addFrameReference(BuildMI(BB, X86::MOV32mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00002398 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
2399 break;
2400 case MVT::i16:
Chris Lattnera28381c2005-07-16 00:28:20 +00002401 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00002402 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
2403 break;
2404 default: break; // No promotion required.
2405 }
Chris Lattnera28381c2005-07-16 00:28:20 +00002406 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00002407 }
2408 case ISD::FP_TO_SINT:
2409 case ISD::FP_TO_UINT: {
2410 // FIXME: Most of this grunt work should be done by legalize!
2411 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2412
Nate Begemanf63be7d2005-07-06 18:59:04 +00002413 // If the target supports SSE2 and is performing FP operations in SSE regs
2414 // instead of the FP stack, then we can use the efficient CVTSS2SI and
2415 // CVTSD2SI instructions.
2416 if (ISD::FP_TO_SINT == N.getOpcode() && X86ScalarSSE) {
2417 if (MVT::f32 == N.getOperand(0).getValueType()) {
Nate Begeman16b04f32005-07-15 00:38:55 +00002418 BuildMI(BB, X86::CVTTSS2SIrr, 1, Result).addReg(Tmp1);
Nate Begemanf63be7d2005-07-06 18:59:04 +00002419 } else if (MVT::f64 == N.getOperand(0).getValueType()) {
Nate Begeman16b04f32005-07-15 00:38:55 +00002420 BuildMI(BB, X86::CVTTSD2SIrr, 1, Result).addReg(Tmp1);
Nate Begemanf63be7d2005-07-06 18:59:04 +00002421 } else {
2422 assert(0 && "Not an f32 or f64?");
2423 abort();
2424 }
2425 return Result;
2426 }
2427
Chris Lattner590d8002005-01-09 18:52:44 +00002428 // Change the floating point control register to use "round towards zero"
2429 // mode when truncating to an integer value.
2430 //
2431 MachineFunction *F = BB->getParent();
2432 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
2433 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
2434
2435 // Load the old value of the high byte of the control word...
2436 unsigned HighPartOfCW = MakeReg(MVT::i8);
2437 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
2438 CWFrameIdx, 1);
2439
2440 // Set the high part to be round to zero...
2441 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
2442 CWFrameIdx, 1).addImm(12);
2443
2444 // Reload the modified control word now...
2445 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002446
Chris Lattner590d8002005-01-09 18:52:44 +00002447 // Restore the memory image of control word to original value
2448 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
2449 CWFrameIdx, 1).addReg(HighPartOfCW);
2450
2451 // We don't have the facilities for directly storing byte sized data to
2452 // memory. Promote it to 16 bits. We also must promote unsigned values to
2453 // larger classes because we only have signed FP stores.
2454 MVT::ValueType StoreClass = Node->getValueType(0);
2455 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
2456 switch (StoreClass) {
Chris Lattner2afa1912005-05-09 05:33:18 +00002457 case MVT::i1:
Chris Lattner590d8002005-01-09 18:52:44 +00002458 case MVT::i8: StoreClass = MVT::i16; break;
2459 case MVT::i16: StoreClass = MVT::i32; break;
2460 case MVT::i32: StoreClass = MVT::i64; break;
Chris Lattner590d8002005-01-09 18:52:44 +00002461 default: assert(0 && "Unknown store class!");
2462 }
2463
2464 // Spill the integer to memory and reload it from there.
2465 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
2466 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2467
2468 switch (StoreClass) {
2469 default: assert(0 && "Unknown store class!");
2470 case MVT::i16:
2471 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
2472 break;
2473 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00002474 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00002475 break;
Chris Lattnera0dbf182005-05-09 18:37:02 +00002476 case MVT::i64:
2477 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
2478 break; }
Chris Lattner590d8002005-01-09 18:52:44 +00002479
2480 switch (Node->getValueType(0)) {
2481 default:
2482 assert(0 && "Unknown integer type!");
Chris Lattner590d8002005-01-09 18:52:44 +00002483 case MVT::i32:
2484 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
2485 break;
2486 case MVT::i16:
2487 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
2488 break;
2489 case MVT::i8:
Chris Lattner2afa1912005-05-09 05:33:18 +00002490 case MVT::i1:
Chris Lattner590d8002005-01-09 18:52:44 +00002491 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
2492 break;
2493 }
2494
2495 // Reload the original control word now.
2496 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
2497 return Result;
2498 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002499 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00002500 Op0 = N.getOperand(0);
2501 Op1 = N.getOperand(1);
2502
Chris Lattner44129b52005-01-25 20:03:11 +00002503 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002504 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002505 goto FoldAdd;
2506 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002507
Chris Lattner44129b52005-01-25 20:03:11 +00002508 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002509 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00002510 switch (N.getValueType()) {
2511 default: assert(0 && "Cannot add this type!");
2512 case MVT::i1:
2513 case MVT::i8: Opc = X86::ADD8rm; break;
2514 case MVT::i16: Opc = X86::ADD16rm; break;
2515 case MVT::i32: Opc = X86::ADD32rm; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002516 case MVT::f32: Opc = X86::ADDSSrm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002517 case MVT::f64:
2518 // For F64, handle promoted load operations (from F32) as well!
Nate Begemanf63be7d2005-07-06 18:59:04 +00002519 if (X86ScalarSSE) {
2520 assert(Op1.getOpcode() == ISD::LOAD && "SSE load not promoted");
2521 Opc = X86::ADDSDrm;
2522 } else {
2523 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
2524 }
Chris Lattner44129b52005-01-25 20:03:11 +00002525 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002526 }
2527 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002528 EmitFoldedLoad(Op1, AM);
2529 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002530 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2531 return Result;
2532 }
2533
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002534 // See if we can codegen this as an LEA to fold operations together.
2535 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00002536 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002537 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00002538 MatchAddress(N, AM);
2539 ExprMap[N] = Result;
2540
2541 // If this is not just an add, emit the LEA. For a simple add (like
2542 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
2543 // leave this as LEA, then peephole it to 'ADD' after two address elim
2544 // happens.
2545 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
2546 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
2547 X86AddressMode XAM = SelectAddrExprs(AM);
2548 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
2549 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002550 }
2551 }
Chris Lattner11333092005-01-11 03:11:44 +00002552
Chris Lattnera5ade062005-01-11 21:19:59 +00002553 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002554 Opc = 0;
2555 if (CN->getValue() == 1) { // add X, 1 -> inc X
2556 switch (N.getValueType()) {
2557 default: assert(0 && "Cannot integer add this type!");
2558 case MVT::i8: Opc = X86::INC8r; break;
2559 case MVT::i16: Opc = X86::INC16r; break;
2560 case MVT::i32: Opc = X86::INC32r; break;
2561 }
2562 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
2563 switch (N.getValueType()) {
2564 default: assert(0 && "Cannot integer add this type!");
2565 case MVT::i8: Opc = X86::DEC8r; break;
2566 case MVT::i16: Opc = X86::DEC16r; break;
2567 case MVT::i32: Opc = X86::DEC32r; break;
2568 }
2569 }
2570
2571 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002572 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002573 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2574 return Result;
2575 }
2576
2577 switch (N.getValueType()) {
2578 default: assert(0 && "Cannot add this type!");
2579 case MVT::i8: Opc = X86::ADD8ri; break;
2580 case MVT::i16: Opc = X86::ADD16ri; break;
2581 case MVT::i32: Opc = X86::ADD32ri; break;
2582 }
2583 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002584 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002585 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2586 return Result;
2587 }
2588 }
2589
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002590 switch (N.getValueType()) {
2591 default: assert(0 && "Cannot add this type!");
2592 case MVT::i8: Opc = X86::ADD8rr; break;
2593 case MVT::i16: Opc = X86::ADD16rr; break;
2594 case MVT::i32: Opc = X86::ADD32rr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002595 case MVT::f32: Opc = X86::ADDSSrr; break;
2596 case MVT::f64: Opc = X86ScalarSSE ? X86::ADDSDrr : X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002597 }
Chris Lattner11333092005-01-11 03:11:44 +00002598
Chris Lattnera5ade062005-01-11 21:19:59 +00002599 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2600 Tmp1 = SelectExpr(Op0);
2601 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00002602 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00002603 Tmp2 = SelectExpr(Op1);
2604 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00002605 }
2606
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002607 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2608 return Result;
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002609
Nate Begemanf63be7d2005-07-06 18:59:04 +00002610 case ISD::FSQRT:
2611 Tmp1 = SelectExpr(Node->getOperand(0));
2612 if (X86ScalarSSE) {
2613 Opc = (N.getValueType() == MVT::f32) ? X86::SQRTSSrr : X86::SQRTSDrr;
2614 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2615 } else {
2616 BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1);
2617 }
2618 return Result;
2619
2620 // FIXME:
2621 // Once we can spill 16 byte constants into the constant pool, we can
2622 // implement SSE equivalents of FABS and FCHS.
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002623 case ISD::FABS:
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002624 case ISD::FNEG:
Chris Lattnerc5dcb532005-04-30 04:25:35 +00002625 case ISD::FSIN:
2626 case ISD::FCOS:
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002627 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002628 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002629 switch (N.getOpcode()) {
2630 default: assert(0 && "Unreachable!");
2631 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
2632 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
Chris Lattnerc5dcb532005-04-30 04:25:35 +00002633 case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break;
2634 case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002635 }
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002636 return Result;
2637
Chris Lattner8db0af12005-04-06 04:21:07 +00002638 case ISD::MULHU:
2639 switch (N.getValueType()) {
2640 default: assert(0 && "Unsupported VT!");
2641 case MVT::i8: Tmp2 = X86::MUL8r; break;
2642 case MVT::i16: Tmp2 = X86::MUL16r; break;
2643 case MVT::i32: Tmp2 = X86::MUL32r; break;
2644 }
2645 // FALL THROUGH
2646 case ISD::MULHS: {
2647 unsigned MovOpc, LowReg, HiReg;
2648 switch (N.getValueType()) {
2649 default: assert(0 && "Unsupported VT!");
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002650 case MVT::i8:
Chris Lattner8db0af12005-04-06 04:21:07 +00002651 MovOpc = X86::MOV8rr;
2652 LowReg = X86::AL;
2653 HiReg = X86::AH;
2654 Opc = X86::IMUL8r;
2655 break;
2656 case MVT::i16:
2657 MovOpc = X86::MOV16rr;
2658 LowReg = X86::AX;
2659 HiReg = X86::DX;
2660 Opc = X86::IMUL16r;
2661 break;
2662 case MVT::i32:
2663 MovOpc = X86::MOV32rr;
2664 LowReg = X86::EAX;
2665 HiReg = X86::EDX;
2666 Opc = X86::IMUL32r;
2667 break;
2668 }
2669 if (Node->getOpcode() != ISD::MULHS)
2670 Opc = Tmp2; // Get the MULHU opcode.
2671
2672 Op0 = Node->getOperand(0);
2673 Op1 = Node->getOperand(1);
2674 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2675 Tmp1 = SelectExpr(Op0);
2676 Tmp2 = SelectExpr(Op1);
2677 } else {
2678 Tmp2 = SelectExpr(Op1);
2679 Tmp1 = SelectExpr(Op0);
2680 }
2681
2682 // FIXME: Implement folding of loads into the memory operands here!
2683 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
2684 BuildMI(BB, Opc, 1).addReg(Tmp2);
2685 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
2686 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002687 }
Chris Lattner8db0af12005-04-06 04:21:07 +00002688
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002689 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00002690 case ISD::MUL:
2691 case ISD::AND:
2692 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00002693 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00002694 static const unsigned SUBTab[] = {
2695 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
2696 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
2697 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
2698 };
Nate Begemanf63be7d2005-07-06 18:59:04 +00002699 static const unsigned SSE_SUBTab[] = {
2700 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
2701 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::SUBSSrm, X86::SUBSDrm,
2702 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::SUBSSrr, X86::SUBSDrr,
2703 };
Chris Lattnera5ade062005-01-11 21:19:59 +00002704 static const unsigned MULTab[] = {
2705 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
2706 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
2707 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
2708 };
Nate Begemanf63be7d2005-07-06 18:59:04 +00002709 static const unsigned SSE_MULTab[] = {
2710 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
2711 0, X86::IMUL16rm , X86::IMUL32rm, X86::MULSSrm, X86::MULSDrm,
2712 0, X86::IMUL16rr , X86::IMUL32rr, X86::MULSSrr, X86::MULSDrr,
2713 };
Chris Lattnera5ade062005-01-11 21:19:59 +00002714 static const unsigned ANDTab[] = {
2715 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
2716 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002717 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattnera5ade062005-01-11 21:19:59 +00002718 };
2719 static const unsigned ORTab[] = {
2720 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
2721 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
2722 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
2723 };
2724 static const unsigned XORTab[] = {
2725 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
2726 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
2727 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
2728 };
2729
2730 Op0 = Node->getOperand(0);
2731 Op1 = Node->getOperand(1);
2732
Chris Lattner30ea1e92005-01-19 07:37:26 +00002733 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
2734 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00002735 return Result;
2736
2737 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002738 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
2739 if (CN->isNullValue()) { // 0 - N -> neg N
2740 switch (N.getValueType()) {
2741 default: assert(0 && "Cannot sub this type!");
2742 case MVT::i1:
2743 case MVT::i8: Opc = X86::NEG8r; break;
2744 case MVT::i16: Opc = X86::NEG16r; break;
2745 case MVT::i32: Opc = X86::NEG32r; break;
2746 }
2747 Tmp1 = SelectExpr(N.getOperand(1));
2748 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2749 return Result;
2750 }
2751
Chris Lattnera5ade062005-01-11 21:19:59 +00002752 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2753 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00002754 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00002755 switch (N.getValueType()) {
2756 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00002757 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00002758 case MVT::i8: Opc = X86::NOT8r; break;
2759 case MVT::i16: Opc = X86::NOT16r; break;
2760 case MVT::i32: Opc = X86::NOT32r; break;
2761 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00002762 if (Opc) {
2763 Tmp1 = SelectExpr(Op0);
2764 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2765 return Result;
2766 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00002767 }
2768
Chris Lattner2a4e5082005-01-17 06:48:02 +00002769 // Fold common multiplies into LEA instructions.
2770 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
2771 switch ((int)CN->getValue()) {
2772 default: break;
2773 case 3:
2774 case 5:
2775 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00002776 // Remove N from exprmap so SelectAddress doesn't get confused.
2777 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002778 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00002779 SelectAddress(N, AM);
2780 // Restore it to the map.
2781 ExprMap[N] = Result;
2782 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
2783 return Result;
2784 }
2785 }
2786
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002787 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00002788 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002789 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00002790 case MVT::i8: Opc = 0; break;
2791 case MVT::i16: Opc = 1; break;
2792 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002793 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002794 switch (Node->getOpcode()) {
2795 default: assert(0 && "Unreachable!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002796 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2797 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002798 case ISD::AND: Opc = ANDTab[Opc]; break;
2799 case ISD::OR: Opc = ORTab[Opc]; break;
2800 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002801 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002802 if (Opc) { // Can't fold MUL:i8 R, imm
2803 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002804 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2805 return Result;
2806 }
2807 }
Chris Lattner11333092005-01-11 03:11:44 +00002808
Chris Lattner44129b52005-01-25 20:03:11 +00002809 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00002810 if (Node->getOpcode() != ISD::SUB) {
2811 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002812 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00002813 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00002814 // For FP, emit 'reverse' subract, with a memory operand.
Nate Begemanf63be7d2005-07-06 18:59:04 +00002815 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner44129b52005-01-25 20:03:11 +00002816 if (Op0.getOpcode() == ISD::EXTLOAD)
2817 Opc = X86::FSUBR32m;
2818 else
2819 Opc = X86::FSUBR64m;
2820
Chris Lattnera5ade062005-01-11 21:19:59 +00002821 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002822 EmitFoldedLoad(Op0, AM);
2823 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00002824 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2825 return Result;
2826 }
2827 }
2828
Chris Lattner44129b52005-01-25 20:03:11 +00002829 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002830 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00002831 switch (N.getValueType()) {
2832 default: assert(0 && "Cannot operate on this type!");
2833 case MVT::i1:
2834 case MVT::i8: Opc = 5; break;
2835 case MVT::i16: Opc = 6; break;
2836 case MVT::i32: Opc = 7; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002837 case MVT::f32: Opc = 8; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002838 // For F64, handle promoted load operations (from F32) as well!
Nate Begemanf63be7d2005-07-06 18:59:04 +00002839 case MVT::f64:
2840 assert((!X86ScalarSSE || Op1.getOpcode() == ISD::LOAD) &&
2841 "SSE load should have been promoted");
2842 Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002843 }
2844 switch (Node->getOpcode()) {
2845 default: assert(0 && "Unreachable!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002846 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2847 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002848 case ISD::AND: Opc = ANDTab[Opc]; break;
2849 case ISD::OR: Opc = ORTab[Opc]; break;
2850 case ISD::XOR: Opc = XORTab[Opc]; break;
2851 }
2852
2853 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002854 EmitFoldedLoad(Op1, AM);
2855 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002856 if (Opc) {
2857 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2858 } else {
2859 assert(Node->getOpcode() == ISD::MUL &&
2860 N.getValueType() == MVT::i8 && "Unexpected situation!");
2861 // Must use the MUL instruction, which forces use of AL.
2862 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2863 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2864 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2865 }
2866 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00002867 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002868
2869 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2870 Tmp1 = SelectExpr(Op0);
2871 Tmp2 = SelectExpr(Op1);
2872 } else {
2873 Tmp2 = SelectExpr(Op1);
2874 Tmp1 = SelectExpr(Op0);
2875 }
2876
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002877 switch (N.getValueType()) {
2878 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002879 case MVT::i1:
2880 case MVT::i8: Opc = 10; break;
2881 case MVT::i16: Opc = 11; break;
2882 case MVT::i32: Opc = 12; break;
2883 case MVT::f32: Opc = 13; break;
2884 case MVT::f64: Opc = 14; break;
2885 }
2886 switch (Node->getOpcode()) {
2887 default: assert(0 && "Unreachable!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002888 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2889 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002890 case ISD::AND: Opc = ANDTab[Opc]; break;
2891 case ISD::OR: Opc = ORTab[Opc]; break;
2892 case ISD::XOR: Opc = XORTab[Opc]; break;
2893 }
2894 if (Opc) {
2895 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2896 } else {
2897 assert(Node->getOpcode() == ISD::MUL &&
2898 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00002899 // Must use the MUL instruction, which forces use of AL.
2900 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2901 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2902 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002903 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002904 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00002905 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002906 case ISD::ADD_PARTS:
2907 case ISD::SUB_PARTS: {
2908 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2909 "Not an i64 add/sub!");
2910 // Emit all of the operands.
2911 std::vector<unsigned> InVals;
2912 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2913 InVals.push_back(SelectExpr(N.getOperand(i)));
2914 if (N.getOpcode() == ISD::ADD_PARTS) {
2915 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2916 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2917 } else {
2918 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2919 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2920 }
2921 return Result+N.ResNo;
2922 }
2923
Chris Lattnerb38a7492005-04-02 04:01:14 +00002924 case ISD::SHL_PARTS:
2925 case ISD::SRA_PARTS:
2926 case ISD::SRL_PARTS: {
2927 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2928 "Not an i64 shift!");
2929 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2930 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2931 unsigned TmpReg = MakeReg(MVT::i32);
2932 if (N.getOpcode() == ISD::SRA_PARTS) {
2933 // If this is a SHR of a Long, then we need to do funny sign extension
2934 // stuff. TmpReg gets the value to use as the high-part if we are
2935 // shifting more than 32 bits.
2936 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2937 } else {
2938 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2939 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2940 }
2941
2942 // Initialize CL with the shift amount.
2943 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2944 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2945
2946 unsigned TmpReg2 = MakeReg(MVT::i32);
2947 unsigned TmpReg3 = MakeReg(MVT::i32);
2948 if (N.getOpcode() == ISD::SHL_PARTS) {
2949 // TmpReg2 = shld inHi, inLo
2950 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2951 .addReg(ShiftOpLo);
2952 // TmpReg3 = shl inLo, CL
2953 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002954
Chris Lattnerb38a7492005-04-02 04:01:14 +00002955 // Set the flags to indicate whether the shift was by more than 32 bits.
2956 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002957
Chris Lattnerb38a7492005-04-02 04:01:14 +00002958 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002959 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002960 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2961 // DestLo = (>32) ? TmpReg : TmpReg3;
2962 BuildMI(BB, X86::CMOVNE32rr, 2,
2963 Result).addReg(TmpReg3).addReg(TmpReg);
2964 } else {
2965 // TmpReg2 = shrd inLo, inHi
2966 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
2967 .addReg(ShiftOpHi);
2968 // TmpReg3 = s[ah]r inHi, CL
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002969 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnerb38a7492005-04-02 04:01:14 +00002970 : X86::SHR32rCL, 1, TmpReg3)
2971 .addReg(ShiftOpHi);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002972
Chris Lattnerb38a7492005-04-02 04:01:14 +00002973 // Set the flags to indicate whether the shift was by more than 32 bits.
2974 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002975
Chris Lattnerb38a7492005-04-02 04:01:14 +00002976 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002977 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002978 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002979
Chris Lattnerb38a7492005-04-02 04:01:14 +00002980 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002981 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002982 Result+1).addReg(TmpReg3).addReg(TmpReg);
2983 }
2984 return Result+N.ResNo;
2985 }
2986
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002987 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002988 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2989 Tmp2 = SelectExpr(N.getOperand(1));
2990 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002991 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002992 Tmp3 = SelectExpr(N.getOperand(2));
2993 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002994 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002995 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2996 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002997
2998 case ISD::SDIV:
2999 case ISD::UDIV:
3000 case ISD::SREM:
3001 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00003002 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
3003 "We don't support this operator!");
3004
Chris Lattner5bf26862005-04-13 03:29:53 +00003005 if (N.getOpcode() == ISD::SDIV) {
Chris Lattner3576c842005-01-25 20:35:10 +00003006 // We can fold loads into FpDIVs, but not really into any others.
Nate Begemanb8aa3ac2005-07-07 06:32:01 +00003007 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner3576c842005-01-25 20:35:10 +00003008 // Check for reversed and unreversed DIV.
3009 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
3010 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
3011 Opc = X86::FDIVR32m;
3012 else
3013 Opc = X86::FDIVR64m;
3014 X86AddressMode AM;
3015 EmitFoldedLoad(N.getOperand(0), AM);
3016 Tmp1 = SelectExpr(N.getOperand(1));
3017 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
3018 return Result;
3019 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
3020 N.getOperand(1).getOpcode() == ISD::LOAD) {
3021 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
3022 Opc = X86::FDIV32m;
3023 else
3024 Opc = X86::FDIV64m;
3025 X86AddressMode AM;
3026 EmitFoldedLoad(N.getOperand(1), AM);
3027 Tmp1 = SelectExpr(N.getOperand(0));
3028 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
3029 return Result;
3030 }
3031 }
3032
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003033 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3034 // FIXME: These special cases should be handled by the lowering impl!
3035 unsigned RHS = CN->getValue();
3036 bool isNeg = false;
3037 if ((int)RHS < 0) {
3038 isNeg = true;
3039 RHS = -RHS;
3040 }
3041 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
3042 unsigned Log = log2(RHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003043 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
3044 switch (N.getValueType()) {
3045 default: assert("Unknown type to signed divide!");
3046 case MVT::i8:
3047 SAROpc = X86::SAR8ri;
3048 SHROpc = X86::SHR8ri;
3049 ADDOpc = X86::ADD8rr;
3050 NEGOpc = X86::NEG8r;
3051 break;
3052 case MVT::i16:
3053 SAROpc = X86::SAR16ri;
3054 SHROpc = X86::SHR16ri;
3055 ADDOpc = X86::ADD16rr;
3056 NEGOpc = X86::NEG16r;
3057 break;
3058 case MVT::i32:
3059 SAROpc = X86::SAR32ri;
3060 SHROpc = X86::SHR32ri;
3061 ADDOpc = X86::ADD32rr;
3062 NEGOpc = X86::NEG32r;
3063 break;
3064 }
Chris Lattnera96e5772005-05-13 21:48:20 +00003065 unsigned RegSize = MVT::getSizeInBits(N.getValueType());
Chris Lattner11333092005-01-11 03:11:44 +00003066 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerca96c822005-05-13 21:50:27 +00003067 unsigned TmpReg;
3068 if (Log != 1) {
3069 TmpReg = MakeReg(N.getValueType());
3070 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
3071 } else {
3072 TmpReg = Tmp1;
3073 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003074 unsigned TmpReg2 = MakeReg(N.getValueType());
Chris Lattnera96e5772005-05-13 21:48:20 +00003075 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(RegSize-Log);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003076 unsigned TmpReg3 = MakeReg(N.getValueType());
3077 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003078
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003079 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
3080 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
3081 if (isNeg)
3082 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
3083 return Result;
3084 }
3085 }
Chris Lattner5bf26862005-04-13 03:29:53 +00003086 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003087
Chris Lattner11333092005-01-11 03:11:44 +00003088 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3089 Tmp1 = SelectExpr(N.getOperand(0));
3090 Tmp2 = SelectExpr(N.getOperand(1));
3091 } else {
3092 Tmp2 = SelectExpr(N.getOperand(1));
3093 Tmp1 = SelectExpr(N.getOperand(0));
3094 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003095
3096 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
3097 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
3098 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
3099 switch (N.getValueType()) {
3100 default: assert(0 && "Cannot sdiv this type!");
3101 case MVT::i8:
3102 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
3103 LoReg = X86::AL;
3104 HiReg = X86::AH;
3105 MovOpcode = X86::MOV8rr;
3106 ClrOpcode = X86::MOV8ri;
3107 SExtOpcode = X86::CBW;
3108 break;
3109 case MVT::i16:
3110 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
3111 LoReg = X86::AX;
3112 HiReg = X86::DX;
3113 MovOpcode = X86::MOV16rr;
3114 ClrOpcode = X86::MOV16ri;
3115 SExtOpcode = X86::CWD;
3116 break;
3117 case MVT::i32:
3118 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00003119 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003120 HiReg = X86::EDX;
3121 MovOpcode = X86::MOV32rr;
3122 ClrOpcode = X86::MOV32ri;
3123 SExtOpcode = X86::CDQ;
3124 break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00003125 case MVT::f32:
3126 BuildMI(BB, X86::DIVSSrr, 2, Result).addReg(Tmp1).addReg(Tmp2);
3127 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003128 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00003129 Opc = X86ScalarSSE ? X86::DIVSDrr : X86::FpDIV;
3130 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003131 return Result;
3132 }
3133
3134 // Set up the low part.
3135 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
3136
3137 if (isSigned) {
3138 // Sign extend the low part into the high part.
3139 BuildMI(BB, SExtOpcode, 0);
3140 } else {
3141 // Zero out the high part, effectively zero extending the input.
3142 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
3143 }
3144
3145 // Emit the DIV/IDIV instruction.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003146 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003147
3148 // Get the result of the divide or rem.
3149 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
3150 return Result;
3151 }
3152
3153 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003154 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00003155 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
3156 switch (N.getValueType()) {
3157 default: assert(0 && "Cannot shift this type!");
3158 case MVT::i8: Opc = X86::ADD8rr; break;
3159 case MVT::i16: Opc = X86::ADD16rr; break;
3160 case MVT::i32: Opc = X86::ADD32rr; break;
3161 }
3162 Tmp1 = SelectExpr(N.getOperand(0));
3163 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
3164 return Result;
3165 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003166
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003167 switch (N.getValueType()) {
3168 default: assert(0 && "Cannot shift this type!");
3169 case MVT::i8: Opc = X86::SHL8ri; break;
3170 case MVT::i16: Opc = X86::SHL16ri; break;
3171 case MVT::i32: Opc = X86::SHL32ri; break;
3172 }
Chris Lattner11333092005-01-11 03:11:44 +00003173 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003174 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3175 return Result;
3176 }
Chris Lattner11333092005-01-11 03:11:44 +00003177
3178 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3179 Tmp1 = SelectExpr(N.getOperand(0));
3180 Tmp2 = SelectExpr(N.getOperand(1));
3181 } else {
3182 Tmp2 = SelectExpr(N.getOperand(1));
3183 Tmp1 = SelectExpr(N.getOperand(0));
3184 }
3185
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003186 switch (N.getValueType()) {
3187 default: assert(0 && "Cannot shift this type!");
3188 case MVT::i8 : Opc = X86::SHL8rCL; break;
3189 case MVT::i16: Opc = X86::SHL16rCL; break;
3190 case MVT::i32: Opc = X86::SHL32rCL; break;
3191 }
3192 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3193 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3194 return Result;
3195 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003196 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3197 switch (N.getValueType()) {
3198 default: assert(0 && "Cannot shift this type!");
3199 case MVT::i8: Opc = X86::SHR8ri; break;
3200 case MVT::i16: Opc = X86::SHR16ri; break;
3201 case MVT::i32: Opc = X86::SHR32ri; break;
3202 }
Chris Lattner11333092005-01-11 03:11:44 +00003203 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003204 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3205 return Result;
3206 }
Chris Lattner11333092005-01-11 03:11:44 +00003207
3208 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3209 Tmp1 = SelectExpr(N.getOperand(0));
3210 Tmp2 = SelectExpr(N.getOperand(1));
3211 } else {
3212 Tmp2 = SelectExpr(N.getOperand(1));
3213 Tmp1 = SelectExpr(N.getOperand(0));
3214 }
3215
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003216 switch (N.getValueType()) {
3217 default: assert(0 && "Cannot shift this type!");
3218 case MVT::i8 : Opc = X86::SHR8rCL; break;
3219 case MVT::i16: Opc = X86::SHR16rCL; break;
3220 case MVT::i32: Opc = X86::SHR32rCL; break;
3221 }
3222 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3223 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3224 return Result;
3225 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003226 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3227 switch (N.getValueType()) {
3228 default: assert(0 && "Cannot shift this type!");
3229 case MVT::i8: Opc = X86::SAR8ri; break;
3230 case MVT::i16: Opc = X86::SAR16ri; break;
3231 case MVT::i32: Opc = X86::SAR32ri; break;
3232 }
Chris Lattner11333092005-01-11 03:11:44 +00003233 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003234 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3235 return Result;
3236 }
Chris Lattner11333092005-01-11 03:11:44 +00003237
3238 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3239 Tmp1 = SelectExpr(N.getOperand(0));
3240 Tmp2 = SelectExpr(N.getOperand(1));
3241 } else {
3242 Tmp2 = SelectExpr(N.getOperand(1));
3243 Tmp1 = SelectExpr(N.getOperand(0));
3244 }
3245
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003246 switch (N.getValueType()) {
3247 default: assert(0 && "Cannot shift this type!");
3248 case MVT::i8 : Opc = X86::SAR8rCL; break;
3249 case MVT::i16: Opc = X86::SAR16rCL; break;
3250 case MVT::i32: Opc = X86::SAR32rCL; break;
3251 }
3252 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3253 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3254 return Result;
3255
3256 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00003257 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003258 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
3259 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
3260 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003261 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003262 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00003263 if (Result != 1) { // Generate the token
3264 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3265 assert(0 && "Load already emitted!?");
3266 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003267 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3268
Chris Lattner5188ad72005-01-08 19:28:19 +00003269 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003270 default: assert(0 && "Cannot load this type!");
3271 case MVT::i1:
3272 case MVT::i8: Opc = X86::MOV8rm; break;
3273 case MVT::i16: Opc = X86::MOV16rm; break;
3274 case MVT::i32: Opc = X86::MOV32rm; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00003275 case MVT::f32: Opc = X86::MOVSSrm; break;
3276 case MVT::f64:
3277 if (X86ScalarSSE) {
3278 Opc = X86::MOVSDrm;
3279 } else {
3280 Opc = X86::FLD64m;
3281 ContainsFPCode = true;
3282 }
3283 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003284 }
Chris Lattner11333092005-01-11 03:11:44 +00003285
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003286 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00003287 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003288 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
3289 } else {
3290 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00003291
3292 SDOperand Chain = N.getOperand(0);
3293 SDOperand Address = N.getOperand(1);
3294 if (getRegPressure(Chain) > getRegPressure(Address)) {
3295 Select(Chain);
3296 SelectAddress(Address, AM);
3297 } else {
3298 SelectAddress(Address, AM);
3299 Select(Chain);
3300 }
3301
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003302 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
3303 }
3304 return Result;
Chris Lattner67649df2005-05-14 06:52:07 +00003305 case X86ISD::FILD64m:
3306 // Make sure we generate both values.
3307 assert(Result != 1 && N.getValueType() == MVT::f64);
3308 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3309 assert(0 && "Load already emitted!?");
3310
3311 {
3312 X86AddressMode AM;
3313
3314 SDOperand Chain = N.getOperand(0);
3315 SDOperand Address = N.getOperand(1);
3316 if (getRegPressure(Chain) > getRegPressure(Address)) {
3317 Select(Chain);
3318 SelectAddress(Address, AM);
3319 } else {
3320 SelectAddress(Address, AM);
3321 Select(Chain);
3322 }
Nate Begeman5a8441e2005-07-16 02:02:34 +00003323 if (X86ScalarSSE) {
3324 addFullAddress(BuildMI(BB, X86::FILD64m, 4, X86::FP0), AM);
3325 addFullAddress(BuildMI(BB, X86::FST64m, 5), AM).addReg(X86::FP0);
3326 addFullAddress(BuildMI(BB, X86::MOVSDrm, 4, Result), AM);
3327 } else {
3328 addFullAddress(BuildMI(BB, X86::FILD64m, 4, Result), AM);
3329 }
Chris Lattner67649df2005-05-14 06:52:07 +00003330 }
3331 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003332
3333 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
3334 case ISD::ZEXTLOAD: {
3335 // Make sure we generate both values.
3336 if (Result != 1)
3337 ExprMap[N.getValue(1)] = 1; // Generate the token
3338 else
3339 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3340
Chris Lattnerda2ce112005-01-16 07:34:08 +00003341 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
3342 if (Node->getValueType(0) == MVT::f64) {
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003343 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnerda2ce112005-01-16 07:34:08 +00003344 "Bad EXTLOAD!");
3345 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
3346 CP->getIndex());
3347 return Result;
3348 }
3349
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003350 X86AddressMode AM;
3351 if (getRegPressure(Node->getOperand(0)) >
3352 getRegPressure(Node->getOperand(1))) {
3353 Select(Node->getOperand(0)); // chain
3354 SelectAddress(Node->getOperand(1), AM);
3355 } else {
3356 SelectAddress(Node->getOperand(1), AM);
3357 Select(Node->getOperand(0)); // chain
3358 }
3359
3360 switch (Node->getValueType(0)) {
3361 default: assert(0 && "Unknown type to sign extend to.");
3362 case MVT::f64:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003363 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003364 "Bad EXTLOAD!");
3365 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
3366 break;
3367 case MVT::i32:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003368 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003369 default:
3370 assert(0 && "Bad zero extend!");
3371 case MVT::i1:
3372 case MVT::i8:
3373 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
3374 break;
3375 case MVT::i16:
3376 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
3377 break;
3378 }
3379 break;
3380 case MVT::i16:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003381 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() <= MVT::i8 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003382 "Bad zero extend!");
3383 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3384 break;
3385 case MVT::i8:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003386 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i1 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003387 "Bad zero extend!");
3388 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
3389 break;
3390 }
3391 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003392 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003393 case ISD::SEXTLOAD: {
3394 // Make sure we generate both values.
3395 if (Result != 1)
3396 ExprMap[N.getValue(1)] = 1; // Generate the token
3397 else
3398 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3399
3400 X86AddressMode AM;
3401 if (getRegPressure(Node->getOperand(0)) >
3402 getRegPressure(Node->getOperand(1))) {
3403 Select(Node->getOperand(0)); // chain
3404 SelectAddress(Node->getOperand(1), AM);
3405 } else {
3406 SelectAddress(Node->getOperand(1), AM);
3407 Select(Node->getOperand(0)); // chain
3408 }
3409
3410 switch (Node->getValueType(0)) {
3411 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
3412 default: assert(0 && "Unknown type to sign extend to.");
3413 case MVT::i32:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003414 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003415 default:
3416 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
3417 case MVT::i8:
3418 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
3419 break;
3420 case MVT::i16:
3421 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
3422 break;
3423 }
3424 break;
3425 case MVT::i16:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003426 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i8 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003427 "Cannot sign extend from bool!");
3428 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3429 break;
3430 }
3431 return Result;
3432 }
3433
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003434 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003435 // Generate both result values.
3436 if (Result != 1)
3437 ExprMap[N.getValue(1)] = 1; // Generate the token
3438 else
3439 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3440
3441 // FIXME: We are currently ignoring the requested alignment for handling
3442 // greater than the stack alignment. This will need to be revisited at some
3443 // point. Align = N.getOperand(2);
3444
3445 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
3446 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
3447 std::cerr << "Cannot allocate stack object with greater alignment than"
3448 << " the stack alignment yet!";
3449 abort();
3450 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003451
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003452 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00003453 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003454 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
3455 .addImm(CN->getValue());
3456 } else {
Chris Lattner11333092005-01-11 03:11:44 +00003457 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3458 Select(N.getOperand(0));
3459 Tmp1 = SelectExpr(N.getOperand(1));
3460 } else {
3461 Tmp1 = SelectExpr(N.getOperand(1));
3462 Select(N.getOperand(0));
3463 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003464
3465 // Subtract size from stack pointer, thereby allocating some space.
3466 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
3467 }
3468
3469 // Put a pointer to the space into the result register, by copying the stack
3470 // pointer.
3471 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
3472 return Result;
3473
Chris Lattner239738a2005-05-14 08:48:15 +00003474 case X86ISD::TAILCALL:
3475 case X86ISD::CALL: {
Chris Lattner5188ad72005-01-08 19:28:19 +00003476 // The chain for this call is now lowered.
Chris Lattner239738a2005-05-14 08:48:15 +00003477 ExprMap.insert(std::make_pair(N.getValue(0), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00003478
Chris Lattnerc6f41812005-05-12 23:06:28 +00003479 bool isDirect = isa<GlobalAddressSDNode>(N.getOperand(1)) ||
3480 isa<ExternalSymbolSDNode>(N.getOperand(1));
3481 unsigned Callee = 0;
3482 if (isDirect) {
3483 Select(N.getOperand(0));
3484 } else {
3485 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3486 Select(N.getOperand(0));
3487 Callee = SelectExpr(N.getOperand(1));
3488 } else {
3489 Callee = SelectExpr(N.getOperand(1));
3490 Select(N.getOperand(0));
3491 }
3492 }
3493
3494 // If this call has values to pass in registers, do so now.
Chris Lattner239738a2005-05-14 08:48:15 +00003495 if (Node->getNumOperands() > 4) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003496 // The first value is passed in (a part of) EAX, the second in EDX.
Chris Lattner239738a2005-05-14 08:48:15 +00003497 unsigned RegOp1 = SelectExpr(N.getOperand(4));
Chris Lattnerc6f41812005-05-12 23:06:28 +00003498 unsigned RegOp2 =
Chris Lattner239738a2005-05-14 08:48:15 +00003499 Node->getNumOperands() > 5 ? SelectExpr(N.getOperand(5)) : 0;
Chris Lattnerc6f41812005-05-12 23:06:28 +00003500
Chris Lattner239738a2005-05-14 08:48:15 +00003501 switch (N.getOperand(4).getValueType()) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003502 default: assert(0 && "Bad thing to pass in regs");
3503 case MVT::i1:
3504 case MVT::i8: BuildMI(BB, X86::MOV8rr , 1,X86::AL).addReg(RegOp1); break;
3505 case MVT::i16: BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1); break;
3506 case MVT::i32: BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);break;
3507 }
3508 if (RegOp2)
Chris Lattner239738a2005-05-14 08:48:15 +00003509 switch (N.getOperand(5).getValueType()) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003510 default: assert(0 && "Bad thing to pass in regs");
3511 case MVT::i1:
3512 case MVT::i8:
3513 BuildMI(BB, X86::MOV8rr , 1, X86::DL).addReg(RegOp2);
3514 break;
3515 case MVT::i16:
3516 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
3517 break;
3518 case MVT::i32:
3519 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
3520 break;
3521 }
3522 }
3523
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003524 if (GlobalAddressSDNode *GASD =
3525 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3526 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
3527 } else if (ExternalSymbolSDNode *ESSDN =
3528 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
3529 BuildMI(BB, X86::CALLpcrel32,
3530 1).addExternalSymbol(ESSDN->getSymbol(), true);
3531 } else {
Chris Lattner11333092005-01-11 03:11:44 +00003532 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3533 Select(N.getOperand(0));
3534 Tmp1 = SelectExpr(N.getOperand(1));
3535 } else {
3536 Tmp1 = SelectExpr(N.getOperand(1));
3537 Select(N.getOperand(0));
3538 }
3539
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003540 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
3541 }
Chris Lattner239738a2005-05-14 08:48:15 +00003542
3543 // Get caller stack amount and amount the callee added to the stack pointer.
3544 Tmp1 = cast<ConstantSDNode>(N.getOperand(2))->getValue();
3545 Tmp2 = cast<ConstantSDNode>(N.getOperand(3))->getValue();
3546 BuildMI(BB, X86::ADJCALLSTACKUP, 2).addImm(Tmp1).addImm(Tmp2);
3547
3548 if (Node->getNumValues() != 1)
3549 switch (Node->getValueType(1)) {
3550 default: assert(0 && "Unknown value type for call result!");
3551 case MVT::Other: return 1;
3552 case MVT::i1:
3553 case MVT::i8:
3554 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3555 break;
3556 case MVT::i16:
3557 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3558 break;
3559 case MVT::i32:
3560 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3561 if (Node->getNumValues() == 3 && Node->getValueType(2) == MVT::i32)
3562 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
3563 break;
3564 case MVT::f64: // Floating-point return values live in %ST(0)
Nate Begemanf63be7d2005-07-06 18:59:04 +00003565 if (X86ScalarSSE) {
3566 ContainsFPCode = true;
3567 BuildMI(BB, X86::FpGETRESULT, 1, X86::FP0);
3568
3569 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
3570 MachineFunction *F = BB->getParent();
3571 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
3572 addFrameReference(BuildMI(BB, X86::FST64m, 5), FrameIdx).addReg(X86::FP0);
3573 addFrameReference(BuildMI(BB, X86::MOVSDrm, 4, Result), FrameIdx);
3574 break;
3575 } else {
3576 ContainsFPCode = true;
3577 BuildMI(BB, X86::FpGETRESULT, 1, Result);
3578 break;
3579 }
Chris Lattner239738a2005-05-14 08:48:15 +00003580 }
3581 return Result+N.ResNo-1;
Chris Lattnerc6f41812005-05-12 23:06:28 +00003582 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00003583 case ISD::READPORT:
3584 // First, determine that the size of the operand falls within the acceptable
3585 // range for this architecture.
3586 //
3587 if (Node->getOperand(1).getValueType() != MVT::i16) {
3588 std::cerr << "llvm.readport: Address size is not 16 bits\n";
3589 exit(1);
3590 }
3591
3592 // Make sure we generate both values.
3593 if (Result != 1) { // Generate the token
3594 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3595 assert(0 && "readport already emitted!?");
3596 } else
3597 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3598
3599 Select(Node->getOperand(0)); // Select the chain.
3600
3601 // If the port is a single-byte constant, use the immediate form.
3602 if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
3603 if ((Port->getValue() & 255) == Port->getValue()) {
3604 switch (Node->getValueType(0)) {
3605 case MVT::i8:
3606 BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue());
3607 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3608 return Result;
3609 case MVT::i16:
3610 BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue());
3611 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3612 return Result;
3613 case MVT::i32:
3614 BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue());
3615 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3616 return Result;
3617 default: break;
3618 }
3619 }
3620
3621 // Now, move the I/O port address into the DX register and use the IN
3622 // instruction to get the input data.
3623 //
3624 Tmp1 = SelectExpr(Node->getOperand(1));
3625 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1);
3626 switch (Node->getValueType(0)) {
3627 case MVT::i8:
3628 BuildMI(BB, X86::IN8rr, 0);
3629 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3630 return Result;
3631 case MVT::i16:
3632 BuildMI(BB, X86::IN16rr, 0);
3633 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3634 return Result;
3635 case MVT::i32:
3636 BuildMI(BB, X86::IN32rr, 0);
3637 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3638 return Result;
3639 default:
3640 std::cerr << "Cannot do input on this data type";
3641 exit(1);
3642 }
3643
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003644 }
3645
3646 return 0;
3647}
3648
Chris Lattnere10269b2005-01-17 19:25:26 +00003649/// TryToFoldLoadOpStore - Given a store node, try to fold together a
3650/// load/op/store instruction. If successful return true.
3651bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
3652 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
3653 SDOperand Chain = Node->getOperand(0);
3654 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00003655 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00003656
3657 // The chain has to be a load, the stored value must be an integer binary
3658 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00003659 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00003660 MVT::isFloatingPoint(StVal.getValueType()))
3661 return false;
3662
Chris Lattner5c659812005-01-17 22:10:42 +00003663 // Token chain must either be a factor node or the load to fold.
3664 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
3665 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00003666
Chris Lattner5c659812005-01-17 22:10:42 +00003667 SDOperand TheLoad;
3668
3669 // Check to see if there is a load from the same pointer that we're storing
3670 // to in either operand of the binop.
3671 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
3672 StVal.getOperand(0).getOperand(1) == StPtr)
3673 TheLoad = StVal.getOperand(0);
3674 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
3675 StVal.getOperand(1).getOperand(1) == StPtr)
3676 TheLoad = StVal.getOperand(1);
3677 else
3678 return false; // No matching load operand.
3679
3680 // We can only fold the load if there are no intervening side-effecting
3681 // operations. This means that the store uses the load as its token chain, or
3682 // there are only token factor nodes in between the store and load.
3683 if (Chain != TheLoad.getValue(1)) {
3684 // Okay, the other option is that we have a store referring to (possibly
3685 // nested) token factor nodes. For now, just try peeking through one level
3686 // of token factors to see if this is the case.
3687 bool ChainOk = false;
3688 if (Chain.getOpcode() == ISD::TokenFactor) {
3689 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3690 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
3691 ChainOk = true;
3692 break;
3693 }
3694 }
3695
3696 if (!ChainOk) return false;
3697 }
3698
3699 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00003700 return false;
3701
3702 // Make sure that one of the operands of the binop is the load, and that the
3703 // load folds into the binop.
3704 if (((StVal.getOperand(0) != TheLoad ||
3705 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
3706 (StVal.getOperand(1) != TheLoad ||
3707 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
3708 return false;
3709
3710 // Finally, check to see if this is one of the ops we can handle!
3711 static const unsigned ADDTAB[] = {
3712 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
3713 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
3714 };
3715 static const unsigned SUBTAB[] = {
3716 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
3717 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
3718 };
3719 static const unsigned ANDTAB[] = {
3720 X86::AND8mi, X86::AND16mi, X86::AND32mi,
3721 X86::AND8mr, X86::AND16mr, X86::AND32mr,
3722 };
3723 static const unsigned ORTAB[] = {
3724 X86::OR8mi, X86::OR16mi, X86::OR32mi,
3725 X86::OR8mr, X86::OR16mr, X86::OR32mr,
3726 };
3727 static const unsigned XORTAB[] = {
3728 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
3729 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
3730 };
3731 static const unsigned SHLTAB[] = {
3732 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
3733 /*Have to put the reg in CL*/0, 0, 0,
3734 };
3735 static const unsigned SARTAB[] = {
3736 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
3737 /*Have to put the reg in CL*/0, 0, 0,
3738 };
3739 static const unsigned SHRTAB[] = {
3740 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
3741 /*Have to put the reg in CL*/0, 0, 0,
3742 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003743
Chris Lattnere10269b2005-01-17 19:25:26 +00003744 const unsigned *TabPtr = 0;
3745 switch (StVal.getOpcode()) {
3746 default:
3747 std::cerr << "CANNOT [mem] op= val: ";
3748 StVal.Val->dump(); std::cerr << "\n";
3749 case ISD::MUL:
3750 case ISD::SDIV:
3751 case ISD::UDIV:
3752 case ISD::SREM:
3753 case ISD::UREM: return false;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003754
Chris Lattnere10269b2005-01-17 19:25:26 +00003755 case ISD::ADD: TabPtr = ADDTAB; break;
3756 case ISD::SUB: TabPtr = SUBTAB; break;
3757 case ISD::AND: TabPtr = ANDTAB; break;
3758 case ISD:: OR: TabPtr = ORTAB; break;
3759 case ISD::XOR: TabPtr = XORTAB; break;
3760 case ISD::SHL: TabPtr = SHLTAB; break;
3761 case ISD::SRA: TabPtr = SARTAB; break;
3762 case ISD::SRL: TabPtr = SHRTAB; break;
3763 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003764
Chris Lattnere10269b2005-01-17 19:25:26 +00003765 // Handle: [mem] op= CST
3766 SDOperand Op0 = StVal.getOperand(0);
3767 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00003768 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00003769 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
3770 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
3771 default: break;
3772 case MVT::i1:
3773 case MVT::i8: Opc = TabPtr[0]; break;
3774 case MVT::i16: Opc = TabPtr[1]; break;
3775 case MVT::i32: Opc = TabPtr[2]; break;
3776 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003777
Chris Lattnere10269b2005-01-17 19:25:26 +00003778 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00003779 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3780 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00003781 Select(Chain);
3782
Chris Lattnere10269b2005-01-17 19:25:26 +00003783 X86AddressMode AM;
3784 if (getRegPressure(TheLoad.getOperand(0)) >
3785 getRegPressure(TheLoad.getOperand(1))) {
3786 Select(TheLoad.getOperand(0));
3787 SelectAddress(TheLoad.getOperand(1), AM);
3788 } else {
3789 SelectAddress(TheLoad.getOperand(1), AM);
3790 Select(TheLoad.getOperand(0));
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003791 }
Chris Lattner5c659812005-01-17 22:10:42 +00003792
3793 if (StVal.getOpcode() == ISD::ADD) {
3794 if (CN->getValue() == 1) {
3795 switch (Op0.getValueType()) {
3796 default: break;
3797 case MVT::i8:
3798 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
3799 return true;
3800 case MVT::i16: Opc = TabPtr[1];
3801 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
3802 return true;
3803 case MVT::i32: Opc = TabPtr[2];
3804 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
3805 return true;
3806 }
3807 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
3808 switch (Op0.getValueType()) {
3809 default: break;
3810 case MVT::i8:
3811 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
3812 return true;
3813 case MVT::i16: Opc = TabPtr[1];
3814 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
3815 return true;
3816 case MVT::i32: Opc = TabPtr[2];
3817 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
3818 return true;
3819 }
3820 }
3821 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003822
Chris Lattnere10269b2005-01-17 19:25:26 +00003823 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
3824 return true;
3825 }
3826 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003827
Chris Lattnere10269b2005-01-17 19:25:26 +00003828 // If we have [mem] = V op [mem], try to turn it into:
3829 // [mem] = [mem] op V.
3830 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
3831 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
3832 StVal.getOpcode() != ISD::SRL)
3833 std::swap(Op0, Op1);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003834
Chris Lattnere10269b2005-01-17 19:25:26 +00003835 if (Op0 != TheLoad) return false;
3836
3837 switch (Op0.getValueType()) {
3838 default: return false;
3839 case MVT::i1:
3840 case MVT::i8: Opc = TabPtr[3]; break;
3841 case MVT::i16: Opc = TabPtr[4]; break;
3842 case MVT::i32: Opc = TabPtr[5]; break;
3843 }
Chris Lattner5c659812005-01-17 22:10:42 +00003844
Chris Lattnerb422aea2005-01-18 17:35:28 +00003845 // Table entry doesn't exist?
3846 if (Opc == 0) return false;
3847
Chris Lattner4a108662005-01-18 03:51:59 +00003848 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3849 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00003850 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00003851 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00003852
Chris Lattnere10269b2005-01-17 19:25:26 +00003853 X86AddressMode AM;
3854 SelectAddress(TheLoad.getOperand(1), AM);
3855 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00003856 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00003857 return true;
3858}
3859
Chris Lattner381e8872005-05-15 05:46:45 +00003860/// If node is a ret(tailcall) node, emit the specified tail call and return
3861/// true, otherwise return false.
3862///
3863/// FIXME: This whole thing should be a post-legalize optimization pass which
3864/// recognizes and transforms the dag. We don't want the selection phase doing
3865/// this stuff!!
3866///
3867bool ISel::EmitPotentialTailCall(SDNode *RetNode) {
3868 assert(RetNode->getOpcode() == ISD::RET && "Not a return");
3869
3870 SDOperand Chain = RetNode->getOperand(0);
3871
3872 // If this is a token factor node where one operand is a call, dig into it.
3873 SDOperand TokFactor;
3874 unsigned TokFactorOperand = 0;
3875 if (Chain.getOpcode() == ISD::TokenFactor) {
3876 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3877 if (Chain.getOperand(i).getOpcode() == ISD::CALLSEQ_END ||
3878 Chain.getOperand(i).getOpcode() == X86ISD::TAILCALL) {
3879 TokFactorOperand = i;
3880 TokFactor = Chain;
3881 Chain = Chain.getOperand(i);
3882 break;
3883 }
3884 if (TokFactor.Val == 0) return false; // No call operand.
3885 }
3886
3887 // Skip the CALLSEQ_END node if present.
3888 if (Chain.getOpcode() == ISD::CALLSEQ_END)
3889 Chain = Chain.getOperand(0);
3890
3891 // Is a tailcall the last control operation that occurs before the return?
3892 if (Chain.getOpcode() != X86ISD::TAILCALL)
3893 return false;
3894
3895 // If we return a value, is it the value produced by the call?
3896 if (RetNode->getNumOperands() > 1) {
3897 // Not returning the ret val of the call?
3898 if (Chain.Val->getNumValues() == 1 ||
3899 RetNode->getOperand(1) != Chain.getValue(1))
3900 return false;
3901
3902 if (RetNode->getNumOperands() > 2) {
3903 if (Chain.Val->getNumValues() == 2 ||
3904 RetNode->getOperand(2) != Chain.getValue(2))
3905 return false;
3906 }
3907 assert(RetNode->getNumOperands() <= 3);
3908 }
3909
3910 // CalleeCallArgAmt - The total number of bytes used for the callee arg area.
3911 // For FastCC, this will always be > 0.
3912 unsigned CalleeCallArgAmt =
3913 cast<ConstantSDNode>(Chain.getOperand(2))->getValue();
3914
3915 // CalleeCallArgPopAmt - The number of bytes in the call area popped by the
3916 // callee. For FastCC this will always be > 0, for CCC this is always 0.
3917 unsigned CalleeCallArgPopAmt =
3918 cast<ConstantSDNode>(Chain.getOperand(3))->getValue();
3919
3920 // There are several cases we can handle here. First, if the caller and
3921 // callee are both CCC functions, we can tailcall if the callee takes <= the
3922 // number of argument bytes that the caller does.
3923 if (CalleeCallArgPopAmt == 0 && // Callee is C CallingConv?
3924 X86Lowering.getBytesToPopOnReturn() == 0) { // Caller is C CallingConv?
3925 // Check to see if caller arg area size >= callee arg area size.
3926 if (X86Lowering.getBytesCallerReserves() >= CalleeCallArgAmt) {
3927 //std::cerr << "CCC TAILCALL UNIMP!\n";
3928 // If TokFactor is non-null, emit all operands.
3929
3930 //EmitCCCToCCCTailCall(Chain.Val);
3931 //return true;
3932 }
3933 return false;
3934 }
3935
3936 // Second, if both are FastCC functions, we can always perform the tail call.
3937 if (CalleeCallArgPopAmt && X86Lowering.getBytesToPopOnReturn()) {
3938 // If TokFactor is non-null, emit all operands before the call.
3939 if (TokFactor.Val) {
3940 for (unsigned i = 0, e = TokFactor.getNumOperands(); i != e; ++i)
3941 if (i != TokFactorOperand)
3942 Select(TokFactor.getOperand(i));
3943 }
3944
3945 EmitFastCCToFastCCTailCall(Chain.Val);
3946 return true;
3947 }
3948
3949 // We don't support mixed calls, due to issues with alignment. We could in
3950 // theory handle some mixed calls from CCC -> FastCC if the stack is properly
3951 // aligned (which depends on the number of arguments to the callee). TODO.
3952 return false;
3953}
3954
3955static SDOperand GetAdjustedArgumentStores(SDOperand Chain, int Offset,
3956 SelectionDAG &DAG) {
3957 MVT::ValueType StoreVT;
3958 switch (Chain.getOpcode()) {
3959 case ISD::CALLSEQ_START:
Chris Lattnerea035432005-05-15 06:07:10 +00003960 // If we found the start of the call sequence, we're done. We actually
3961 // strip off the CALLSEQ_START node, to avoid generating the
3962 // ADJCALLSTACKDOWN marker for the tail call.
3963 return Chain.getOperand(0);
Chris Lattner381e8872005-05-15 05:46:45 +00003964 case ISD::TokenFactor: {
3965 std::vector<SDOperand> Ops;
3966 Ops.reserve(Chain.getNumOperands());
3967 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3968 Ops.push_back(GetAdjustedArgumentStores(Chain.getOperand(i), Offset,DAG));
3969 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
3970 }
3971 case ISD::STORE: // Normal store
3972 StoreVT = Chain.getOperand(1).getValueType();
3973 break;
3974 case ISD::TRUNCSTORE: // FLOAT store
Chris Lattner9fadb4c2005-07-10 00:29:18 +00003975 StoreVT = cast<VTSDNode>(Chain.getOperand(4))->getVT();
Chris Lattner381e8872005-05-15 05:46:45 +00003976 break;
3977 }
3978
3979 SDOperand OrigDest = Chain.getOperand(2);
3980 unsigned OrigOffset;
3981
3982 if (OrigDest.getOpcode() == ISD::CopyFromReg) {
3983 OrigOffset = 0;
3984 assert(cast<RegSDNode>(OrigDest)->getReg() == X86::ESP);
3985 } else {
3986 // We expect only (ESP+C)
3987 assert(OrigDest.getOpcode() == ISD::ADD &&
3988 isa<ConstantSDNode>(OrigDest.getOperand(1)) &&
3989 OrigDest.getOperand(0).getOpcode() == ISD::CopyFromReg &&
3990 cast<RegSDNode>(OrigDest.getOperand(0))->getReg() == X86::ESP);
3991 OrigOffset = cast<ConstantSDNode>(OrigDest.getOperand(1))->getValue();
3992 }
3993
3994 // Compute the new offset from the incoming ESP value we wish to use.
3995 unsigned NewOffset = OrigOffset + Offset;
3996
3997 unsigned OpSize = (MVT::getSizeInBits(StoreVT)+7)/8; // Bits -> Bytes
3998 MachineFunction &MF = DAG.getMachineFunction();
3999 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, NewOffset);
4000 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
4001
4002 SDOperand InChain = GetAdjustedArgumentStores(Chain.getOperand(0), Offset,
4003 DAG);
4004 if (Chain.getOpcode() == ISD::STORE)
4005 return DAG.getNode(ISD::STORE, MVT::Other, InChain, Chain.getOperand(1),
4006 FIN);
4007 assert(Chain.getOpcode() == ISD::TRUNCSTORE);
4008 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, InChain, Chain.getOperand(1),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00004009 FIN, DAG.getSrcValue(NULL), DAG.getValueType(StoreVT));
Chris Lattner381e8872005-05-15 05:46:45 +00004010}
4011
4012
4013/// EmitFastCCToFastCCTailCall - Given a tailcall in the tail position to a
4014/// fastcc function from a fastcc function, emit the code to emit a 'proper'
4015/// tail call.
4016void ISel::EmitFastCCToFastCCTailCall(SDNode *TailCallNode) {
4017 unsigned CalleeCallArgSize =
4018 cast<ConstantSDNode>(TailCallNode->getOperand(2))->getValue();
4019 unsigned CallerArgSize = X86Lowering.getBytesToPopOnReturn();
4020
4021 //std::cerr << "****\n*** EMITTING TAIL CALL!\n****\n";
4022
4023 // Adjust argument stores. Instead of storing to [ESP], f.e., store to frame
4024 // indexes that are relative to the incoming ESP. If the incoming and
4025 // outgoing arg sizes are the same we will store to [InESP] instead of
4026 // [CurESP] and the ESP referenced will be relative to the incoming function
4027 // ESP.
4028 int ESPOffset = CallerArgSize-CalleeCallArgSize;
4029 SDOperand AdjustedArgStores =
4030 GetAdjustedArgumentStores(TailCallNode->getOperand(0), ESPOffset, *TheDAG);
4031
4032 // Copy the return address of the caller into a virtual register so we don't
4033 // clobber it.
4034 SDOperand RetVal;
4035 if (ESPOffset) {
4036 SDOperand RetValAddr = X86Lowering.getReturnAddressFrameIndex(*TheDAG);
4037 RetVal = TheDAG->getLoad(MVT::i32, TheDAG->getEntryNode(),
4038 RetValAddr, TheDAG->getSrcValue(NULL));
4039 SelectExpr(RetVal);
4040 }
4041
4042 // Codegen all of the argument stores.
4043 Select(AdjustedArgStores);
4044
4045 if (RetVal.Val) {
4046 // Emit a store of the saved ret value to the new location.
4047 MachineFunction &MF = TheDAG->getMachineFunction();
4048 int ReturnAddrFI = MF.getFrameInfo()->CreateFixedObject(4, ESPOffset-4);
4049 SDOperand RetValAddr = TheDAG->getFrameIndex(ReturnAddrFI, MVT::i32);
4050 Select(TheDAG->getNode(ISD::STORE, MVT::Other, TheDAG->getEntryNode(),
4051 RetVal, RetValAddr));
4052 }
4053
4054 // Get the destination value.
4055 SDOperand Callee = TailCallNode->getOperand(1);
4056 bool isDirect = isa<GlobalAddressSDNode>(Callee) ||
4057 isa<ExternalSymbolSDNode>(Callee);
Chris Lattner9cb2d612005-06-17 13:23:32 +00004058 unsigned CalleeReg = 0;
Chris Lattner381e8872005-05-15 05:46:45 +00004059 if (!isDirect) CalleeReg = SelectExpr(Callee);
4060
4061 unsigned RegOp1 = 0;
4062 unsigned RegOp2 = 0;
4063
4064 if (TailCallNode->getNumOperands() > 4) {
4065 // The first value is passed in (a part of) EAX, the second in EDX.
4066 RegOp1 = SelectExpr(TailCallNode->getOperand(4));
4067 if (TailCallNode->getNumOperands() > 5)
4068 RegOp2 = SelectExpr(TailCallNode->getOperand(5));
4069
4070 switch (TailCallNode->getOperand(4).getValueType()) {
4071 default: assert(0 && "Bad thing to pass in regs");
4072 case MVT::i1:
4073 case MVT::i8:
4074 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(RegOp1);
4075 RegOp1 = X86::AL;
4076 break;
4077 case MVT::i16:
4078 BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1);
4079 RegOp1 = X86::AX;
4080 break;
4081 case MVT::i32:
4082 BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);
4083 RegOp1 = X86::EAX;
4084 break;
4085 }
4086 if (RegOp2)
4087 switch (TailCallNode->getOperand(5).getValueType()) {
4088 default: assert(0 && "Bad thing to pass in regs");
4089 case MVT::i1:
4090 case MVT::i8:
4091 BuildMI(BB, X86::MOV8rr, 1, X86::DL).addReg(RegOp2);
4092 RegOp2 = X86::DL;
4093 break;
4094 case MVT::i16:
4095 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
4096 RegOp2 = X86::DX;
4097 break;
4098 case MVT::i32:
4099 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
4100 RegOp2 = X86::EDX;
4101 break;
4102 }
4103 }
4104
4105 // Adjust ESP.
4106 if (ESPOffset)
4107 BuildMI(BB, X86::ADJSTACKPTRri, 2,
4108 X86::ESP).addReg(X86::ESP).addImm(ESPOffset);
4109
4110 // TODO: handle jmp [mem]
4111 if (!isDirect) {
4112 BuildMI(BB, X86::TAILJMPr, 1).addReg(CalleeReg);
4113 } else if (GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Callee)){
Chris Lattner16cb6f82005-05-19 05:54:33 +00004114 BuildMI(BB, X86::TAILJMPd, 1).addGlobalAddress(GASD->getGlobal(), true);
Chris Lattner381e8872005-05-15 05:46:45 +00004115 } else {
4116 ExternalSymbolSDNode *ESSDN = cast<ExternalSymbolSDNode>(Callee);
4117 BuildMI(BB, X86::TAILJMPd, 1).addExternalSymbol(ESSDN->getSymbol(), true);
4118 }
4119 // ADD IMPLICIT USE RegOp1/RegOp2's
4120}
4121
Chris Lattnere10269b2005-01-17 19:25:26 +00004122
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004123void ISel::Select(SDOperand N) {
4124 unsigned Tmp1, Tmp2, Opc;
4125
Nate Begeman85fdeb22005-03-24 04:39:54 +00004126 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004127 return; // Already selected.
4128
Chris Lattner989de032005-01-11 06:14:36 +00004129 SDNode *Node = N.Val;
4130
4131 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004132 default:
Chris Lattner989de032005-01-11 06:14:36 +00004133 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004134 assert(0 && "Node not handled yet!");
4135 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00004136 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00004137 if (Node->getNumOperands() == 2) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004138 bool OneFirst =
Chris Lattner1d50b7f2005-01-13 19:56:00 +00004139 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
4140 Select(Node->getOperand(OneFirst));
4141 Select(Node->getOperand(!OneFirst));
4142 } else {
4143 std::vector<std::pair<unsigned, unsigned> > OpsP;
4144 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
4145 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
4146 std::sort(OpsP.begin(), OpsP.end());
4147 std::reverse(OpsP.begin(), OpsP.end());
4148 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
4149 Select(Node->getOperand(OpsP[i].second));
4150 }
Chris Lattnerc3580712005-01-13 18:01:36 +00004151 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004152 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00004153 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4154 Select(N.getOperand(0));
4155 Tmp1 = SelectExpr(N.getOperand(1));
4156 } else {
4157 Tmp1 = SelectExpr(N.getOperand(1));
4158 Select(N.getOperand(0));
4159 }
Chris Lattner18c2f132005-01-13 20:50:02 +00004160 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004161
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004162 if (Tmp1 != Tmp2) {
4163 switch (N.getOperand(1).getValueType()) {
4164 default: assert(0 && "Invalid type for operation!");
4165 case MVT::i1:
4166 case MVT::i8: Opc = X86::MOV8rr; break;
4167 case MVT::i16: Opc = X86::MOV16rr; break;
4168 case MVT::i32: Opc = X86::MOV32rr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004169 case MVT::f32: Opc = X86::MOVAPSrr; break;
4170 case MVT::f64:
4171 if (X86ScalarSSE) {
4172 Opc = X86::MOVAPDrr;
4173 } else {
4174 Opc = X86::FpMOV;
4175 ContainsFPCode = true;
4176 }
4177 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004178 }
4179 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
4180 }
4181 return;
4182 case ISD::RET:
Chris Lattner381e8872005-05-15 05:46:45 +00004183 if (N.getOperand(0).getOpcode() == ISD::CALLSEQ_END ||
4184 N.getOperand(0).getOpcode() == X86ISD::TAILCALL ||
4185 N.getOperand(0).getOpcode() == ISD::TokenFactor)
4186 if (EmitPotentialTailCall(Node))
4187 return;
4188
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004189 switch (N.getNumOperands()) {
4190 default:
4191 assert(0 && "Unknown return instruction!");
4192 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004193 assert(N.getOperand(1).getValueType() == MVT::i32 &&
4194 N.getOperand(2).getValueType() == MVT::i32 &&
4195 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00004196 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
4197 Tmp1 = SelectExpr(N.getOperand(1));
4198 Tmp2 = SelectExpr(N.getOperand(2));
4199 } else {
4200 Tmp2 = SelectExpr(N.getOperand(2));
4201 Tmp1 = SelectExpr(N.getOperand(1));
4202 }
4203 Select(N.getOperand(0));
4204
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004205 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4206 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004207 break;
4208 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00004209 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4210 Select(N.getOperand(0));
4211 Tmp1 = SelectExpr(N.getOperand(1));
4212 } else {
4213 Tmp1 = SelectExpr(N.getOperand(1));
4214 Select(N.getOperand(0));
4215 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004216 switch (N.getOperand(1).getValueType()) {
4217 default: assert(0 && "All other types should have been promoted!!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00004218 case MVT::f32:
4219 if (X86ScalarSSE) {
4220 // Spill the value to memory and reload it into top of stack.
4221 unsigned Size = MVT::getSizeInBits(MVT::f32)/8;
4222 MachineFunction *F = BB->getParent();
4223 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
4224 addFrameReference(BuildMI(BB, X86::MOVSSmr, 5), FrameIdx).addReg(Tmp1);
4225 addFrameReference(BuildMI(BB, X86::FLD32m, 4, X86::FP0), FrameIdx);
4226 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
4227 ContainsFPCode = true;
4228 } else {
4229 assert(0 && "MVT::f32 only legal with scalar sse fp");
4230 abort();
4231 }
4232 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004233 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00004234 if (X86ScalarSSE) {
4235 // Spill the value to memory and reload it into top of stack.
4236 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
4237 MachineFunction *F = BB->getParent();
4238 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
4239 addFrameReference(BuildMI(BB, X86::MOVSDmr, 5), FrameIdx).addReg(Tmp1);
4240 addFrameReference(BuildMI(BB, X86::FLD64m, 4, X86::FP0), FrameIdx);
4241 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
4242 ContainsFPCode = true;
4243 } else {
4244 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
4245 }
4246 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004247 case MVT::i32:
Nate Begemanf63be7d2005-07-06 18:59:04 +00004248 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4249 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004250 }
4251 break;
4252 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00004253 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004254 break;
4255 }
Chris Lattner3648c672005-05-13 21:44:04 +00004256 if (X86Lowering.getBytesToPopOnReturn() == 0)
4257 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
4258 else
4259 BuildMI(BB, X86::RETI, 1).addImm(X86Lowering.getBytesToPopOnReturn());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004260 return;
4261 case ISD::BR: {
4262 Select(N.getOperand(0));
4263 MachineBasicBlock *Dest =
4264 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
4265 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
4266 return;
4267 }
4268
4269 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004270 MachineBasicBlock *Dest =
4271 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00004272
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004273 // Try to fold a setcc into the branch. If this fails, emit a test/jne
4274 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00004275 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
4276 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4277 Select(N.getOperand(0));
4278 Tmp1 = SelectExpr(N.getOperand(1));
4279 } else {
4280 Tmp1 = SelectExpr(N.getOperand(1));
4281 Select(N.getOperand(0));
4282 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004283 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
4284 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
4285 }
Chris Lattner11333092005-01-11 03:11:44 +00004286
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004287 return;
4288 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004289
Chris Lattner4df0de92005-01-17 00:00:33 +00004290 case ISD::LOAD:
4291 // If this load could be folded into the only using instruction, and if it
4292 // is safe to emit the instruction here, try to do so now.
4293 if (Node->hasNUsesOfValue(1, 0)) {
4294 SDOperand TheVal = N.getValue(0);
4295 SDNode *User = 0;
4296 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
4297 assert(UI != Node->use_end() && "Didn't find use!");
4298 SDNode *UN = *UI;
4299 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
4300 if (UN->getOperand(i) == TheVal) {
4301 User = UN;
4302 goto FoundIt;
4303 }
4304 }
4305 FoundIt:
4306 // Only handle unary operators right now.
4307 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00004308 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00004309 SelectExpr(SDOperand(User, 0));
4310 return;
4311 }
4312 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00004313 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00004314 SelectExpr(N);
4315 return;
Chris Lattner966cdfb2005-05-09 21:17:38 +00004316 case ISD::READPORT:
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004317 case ISD::EXTLOAD:
4318 case ISD::SEXTLOAD:
4319 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004320 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner239738a2005-05-14 08:48:15 +00004321 case X86ISD::TAILCALL:
4322 case X86ISD::CALL:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00004323 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004324 SelectExpr(N);
4325 return;
Chris Lattnerc6f41812005-05-12 23:06:28 +00004326 case ISD::CopyFromReg:
Chris Lattner67649df2005-05-14 06:52:07 +00004327 case X86ISD::FILD64m:
Chris Lattnerc6f41812005-05-12 23:06:28 +00004328 ExprMap.erase(N);
4329 SelectExpr(N.getValue(0));
4330 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004331
Chris Lattner9fadb4c2005-07-10 00:29:18 +00004332 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr, SRCVALUE, storety
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004333 X86AddressMode AM;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00004334 MVT::ValueType StoredTy = cast<VTSDNode>(N.getOperand(4))->getVT();
Chris Lattnerda2ce112005-01-16 07:34:08 +00004335 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
4336 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
4337 && "Unsupported TRUNCSTORE for this target!");
4338
4339 if (StoredTy == MVT::i16) {
4340 // FIXME: This is here just to allow testing. X86 doesn't really have a
4341 // TRUNCSTORE i16 operation, but this is required for targets that do not
4342 // have 16-bit integer registers. We occasionally disable 16-bit integer
4343 // registers to test the promotion code.
4344 Select(N.getOperand(0));
4345 Tmp1 = SelectExpr(N.getOperand(1));
4346 SelectAddress(N.getOperand(2), AM);
4347
4348 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4349 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
4350 return;
4351 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004352
4353 // Store of constant bool?
4354 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
4355 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4356 Select(N.getOperand(0));
4357 SelectAddress(N.getOperand(2), AM);
4358 } else {
4359 SelectAddress(N.getOperand(2), AM);
4360 Select(N.getOperand(0));
4361 }
4362 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
4363 return;
4364 }
4365
4366 switch (StoredTy) {
4367 default: assert(0 && "Cannot truncstore this type!");
4368 case MVT::i1: Opc = X86::MOV8mr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004369 case MVT::f32:
4370 assert(!X86ScalarSSE && "Cannot truncstore scalar SSE regs");
4371 Opc = X86::FST32m; break;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004372 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004373
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004374 std::vector<std::pair<unsigned, unsigned> > RP;
4375 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
4376 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
4377 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
4378 std::sort(RP.begin(), RP.end());
4379
Chris Lattner572dd082005-02-23 05:57:21 +00004380 Tmp1 = 0; // Silence a warning.
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004381 for (unsigned i = 0; i != 3; ++i)
4382 switch (RP[2-i].second) {
4383 default: assert(0 && "Unknown operand number!");
4384 case 0: Select(N.getOperand(0)); break;
4385 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
4386 case 2: SelectAddress(N.getOperand(2), AM); break;
4387 }
4388
4389 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
4390 return;
4391 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004392 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004393 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004394
4395 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
4396 Opc = 0;
4397 switch (CN->getValueType(0)) {
4398 default: assert(0 && "Invalid type for operation!");
4399 case MVT::i1:
4400 case MVT::i8: Opc = X86::MOV8mi; break;
4401 case MVT::i16: Opc = X86::MOV16mi; break;
4402 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004403 }
4404 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00004405 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4406 Select(N.getOperand(0));
4407 SelectAddress(N.getOperand(2), AM);
4408 } else {
4409 SelectAddress(N.getOperand(2), AM);
4410 Select(N.getOperand(0));
4411 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004412 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
4413 return;
4414 }
Chris Lattner75f354b2005-04-21 19:03:24 +00004415 } else if (GlobalAddressSDNode *GA =
4416 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
4417 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
4418
4419 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4420 Select(N.getOperand(0));
4421 SelectAddress(N.getOperand(2), AM);
4422 } else {
4423 SelectAddress(N.getOperand(2), AM);
4424 Select(N.getOperand(0));
4425 }
Nate Begeman16b04f32005-07-15 00:38:55 +00004426 GlobalValue *GV = GA->getGlobal();
4427 // For Darwin, external and weak symbols are indirect, so we want to load
4428 // the value at address GV, not the value of GV itself.
4429 if (Subtarget->getIndirectExternAndWeakGlobals() &&
4430 (GV->hasWeakLinkage() || GV->isExternal())) {
4431 Tmp1 = MakeReg(MVT::i32);
4432 BuildMI(BB, X86::MOV32rm, 4, Tmp1).addReg(0).addZImm(1).addReg(0)
4433 .addGlobalAddress(GV, false, 0);
4434 addFullAddress(BuildMI(BB, X86::MOV32mr, 4+1),AM).addReg(Tmp1);
4435 } else {
4436 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),AM).addGlobalAddress(GV);
4437 }
Chris Lattner75f354b2005-04-21 19:03:24 +00004438 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004439 }
Chris Lattner837caa72005-01-11 23:21:30 +00004440
4441 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00004442 if (TryToFoldLoadOpStore(Node))
4443 return;
Chris Lattner837caa72005-01-11 23:21:30 +00004444
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004445 switch (N.getOperand(1).getValueType()) {
4446 default: assert(0 && "Cannot store this type!");
4447 case MVT::i1:
4448 case MVT::i8: Opc = X86::MOV8mr; break;
4449 case MVT::i16: Opc = X86::MOV16mr; break;
4450 case MVT::i32: Opc = X86::MOV32mr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004451 case MVT::f32: Opc = X86::MOVSSmr; break;
4452 case MVT::f64: Opc = X86ScalarSSE ? X86::MOVSDmr : X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004453 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004454
Chris Lattner11333092005-01-11 03:11:44 +00004455 std::vector<std::pair<unsigned, unsigned> > RP;
4456 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
4457 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
4458 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
4459 std::sort(RP.begin(), RP.end());
4460
Chris Lattner572dd082005-02-23 05:57:21 +00004461 Tmp1 = 0; // Silence a warning.
Chris Lattner11333092005-01-11 03:11:44 +00004462 for (unsigned i = 0; i != 3; ++i)
4463 switch (RP[2-i].second) {
4464 default: assert(0 && "Unknown operand number!");
4465 case 0: Select(N.getOperand(0)); break;
4466 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00004467 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00004468 }
4469
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004470 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
4471 return;
4472 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00004473 case ISD::CALLSEQ_START:
Chris Lattner3648c672005-05-13 21:44:04 +00004474 Select(N.getOperand(0));
4475 // Stack amount
4476 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
4477 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(Tmp1);
4478 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00004479 case ISD::CALLSEQ_END:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004480 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004481 return;
Chris Lattner989de032005-01-11 06:14:36 +00004482 case ISD::MEMSET: {
4483 Select(N.getOperand(0)); // Select the chain.
4484 unsigned Align =
4485 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
4486 if (Align == 0) Align = 1;
4487
4488 // Turn the byte code into # iterations
4489 unsigned CountReg;
4490 unsigned Opcode;
4491 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
4492 unsigned Val = ValC->getValue() & 255;
4493
4494 // If the value is a constant, then we can potentially use larger sets.
4495 switch (Align & 3) {
4496 case 2: // WORD aligned
4497 CountReg = MakeReg(MVT::i32);
4498 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4499 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
4500 } else {
4501 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4502 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
4503 }
4504 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
4505 Opcode = X86::REP_STOSW;
4506 break;
4507 case 0: // DWORD aligned
4508 CountReg = MakeReg(MVT::i32);
4509 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4510 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
4511 } else {
4512 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4513 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
4514 }
4515 Val = (Val << 8) | Val;
4516 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
4517 Opcode = X86::REP_STOSD;
4518 break;
4519 default: // BYTE aligned
4520 CountReg = SelectExpr(Node->getOperand(3));
4521 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
4522 Opcode = X86::REP_STOSB;
4523 break;
4524 }
4525 } else {
4526 // If it's not a constant value we are storing, just fall back. We could
4527 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
4528 unsigned ValReg = SelectExpr(Node->getOperand(2));
4529 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
4530 CountReg = SelectExpr(Node->getOperand(3));
4531 Opcode = X86::REP_STOSB;
4532 }
4533
4534 // No matter what the alignment is, we put the source in ESI, the
4535 // destination in EDI, and the count in ECX.
4536 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
4537 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
4538 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
4539 BuildMI(BB, Opcode, 0);
4540 return;
4541 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00004542 case ISD::MEMCPY: {
Chris Lattner31805bf2005-01-11 06:19:26 +00004543 Select(N.getOperand(0)); // Select the chain.
4544 unsigned Align =
4545 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
4546 if (Align == 0) Align = 1;
4547
4548 // Turn the byte code into # iterations
4549 unsigned CountReg;
4550 unsigned Opcode;
4551 switch (Align & 3) {
4552 case 2: // WORD aligned
4553 CountReg = MakeReg(MVT::i32);
4554 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4555 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
4556 } else {
4557 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4558 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
4559 }
4560 Opcode = X86::REP_MOVSW;
4561 break;
4562 case 0: // DWORD aligned
4563 CountReg = MakeReg(MVT::i32);
4564 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4565 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
4566 } else {
4567 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4568 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
4569 }
4570 Opcode = X86::REP_MOVSD;
4571 break;
4572 default: // BYTE aligned
4573 CountReg = SelectExpr(Node->getOperand(3));
4574 Opcode = X86::REP_MOVSB;
4575 break;
4576 }
4577
4578 // No matter what the alignment is, we put the source in ESI, the
4579 // destination in EDI, and the count in ECX.
4580 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
4581 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
4582 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
4583 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
4584 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
4585 BuildMI(BB, Opcode, 0);
4586 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004587 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00004588 case ISD::WRITEPORT:
4589 if (Node->getOperand(2).getValueType() != MVT::i16) {
4590 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
4591 exit(1);
4592 }
4593 Select(Node->getOperand(0)); // Emit the chain.
4594
4595 Tmp1 = SelectExpr(Node->getOperand(1));
4596 switch (Node->getOperand(1).getValueType()) {
4597 case MVT::i8:
4598 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
4599 Tmp2 = X86::OUT8ir; Opc = X86::OUT8rr;
4600 break;
4601 case MVT::i16:
4602 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(Tmp1);
4603 Tmp2 = X86::OUT16ir; Opc = X86::OUT16rr;
4604 break;
4605 case MVT::i32:
4606 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4607 Tmp2 = X86::OUT32ir; Opc = X86::OUT32rr;
4608 break;
4609 default:
4610 std::cerr << "llvm.writeport: invalid data type for X86 target";
4611 exit(1);
4612 }
4613
4614 // If the port is a single-byte constant, use the immediate form.
4615 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(2)))
4616 if ((CN->getValue() & 255) == CN->getValue()) {
4617 BuildMI(BB, Tmp2, 1).addImm(CN->getValue());
4618 return;
4619 }
4620
4621 // Otherwise, move the I/O port address into the DX register.
4622 unsigned Reg = SelectExpr(Node->getOperand(2));
4623 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
4624 BuildMI(BB, Opc, 0);
4625 return;
4626 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004627 assert(0 && "Should not be reached!");
4628}
4629
4630
4631/// createX86PatternInstructionSelector - This pass converts an LLVM function
4632/// into a machine code representation using pattern matching and a machine
4633/// description file.
4634///
4635FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004636 return new ISel(TM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004637}