blob: 999f089395e966e485818d27750f581e46da7df0 [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();
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000192 }
Nate Begemanf63be7d2005-07-06 18:59:04 +0000193
Chris Lattner3648c672005-05-13 21:44:04 +0000194 // Return the number of bytes that a function should pop when it returns (in
195 // addition to the space used by the return address).
196 //
197 unsigned getBytesToPopOnReturn() const { return BytesToPopOnReturn; }
198
Chris Lattner381e8872005-05-15 05:46:45 +0000199 // Return the number of bytes that the caller reserves for arguments passed
200 // to this function.
201 unsigned getBytesCallerReserves() const { return BytesCallerReserves; }
202
Chris Lattner67649df2005-05-14 06:52:07 +0000203 /// LowerOperation - Provide custom lowering hooks for some operations.
204 ///
205 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
206
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000207 /// LowerArguments - This hook must be implemented to indicate how we should
208 /// lower the arguments for the specified function, into the specified DAG.
209 virtual std::vector<SDOperand>
210 LowerArguments(Function &F, SelectionDAG &DAG);
211
212 /// LowerCallTo - This hook lowers an abstract call to a function into an
213 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +0000214 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000215 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000216 bool isTailCall, SDOperand Callee, ArgListTy &Args,
217 SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000218
Chris Lattnere0fe2252005-07-05 19:58:54 +0000219 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
220 Value *VAListV, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000221 virtual std::pair<SDOperand,SDOperand>
Chris Lattnere0fe2252005-07-05 19:58:54 +0000222 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
223 const Type *ArgTy, SelectionDAG &DAG);
224
Chris Lattner14824582005-01-09 00:01:27 +0000225 virtual std::pair<SDOperand, SDOperand>
226 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
227 SelectionDAG &DAG);
Chris Lattner381e8872005-05-15 05:46:45 +0000228
229 SDOperand getReturnAddressFrameIndex(SelectionDAG &DAG);
230
Chris Lattnerc6f41812005-05-12 23:06:28 +0000231 private:
232 // C Calling Convention implementation.
233 std::vector<SDOperand> LowerCCCArguments(Function &F, SelectionDAG &DAG);
234 std::pair<SDOperand, SDOperand>
235 LowerCCCCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
Chris Lattner2e7714a2005-05-13 20:29:13 +0000236 bool isTailCall,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000237 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
238
239 // Fast Calling Convention implementation.
240 std::vector<SDOperand> LowerFastCCArguments(Function &F, SelectionDAG &DAG);
241 std::pair<SDOperand, SDOperand>
Chris Lattner2e7714a2005-05-13 20:29:13 +0000242 LowerFastCCCallTo(SDOperand Chain, const Type *RetTy, bool isTailCall,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000243 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000244 };
245}
246
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000247std::vector<SDOperand>
248X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattnerc6f41812005-05-12 23:06:28 +0000249 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
250 return LowerFastCCArguments(F, DAG);
251 return LowerCCCArguments(F, DAG);
252}
253
254std::pair<SDOperand, SDOperand>
255X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
256 bool isVarArg, unsigned CallingConv,
Chris Lattneradf6a962005-05-13 18:50:42 +0000257 bool isTailCall,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000258 SDOperand Callee, ArgListTy &Args,
259 SelectionDAG &DAG) {
260 assert((!isVarArg || CallingConv == CallingConv::C) &&
261 "Only C takes varargs!");
262 if (CallingConv == CallingConv::Fast && EnableFastCC)
Chris Lattner2e7714a2005-05-13 20:29:13 +0000263 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
264 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000265}
266
267//===----------------------------------------------------------------------===//
Chris Lattner653f7232005-05-13 22:46:57 +0000268// C Calling Convention implementation
Chris Lattnerc6f41812005-05-12 23:06:28 +0000269//===----------------------------------------------------------------------===//
270
271std::vector<SDOperand>
272X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000273 std::vector<SDOperand> ArgValues;
274
Chris Lattner6415bb42005-05-10 03:53:18 +0000275 MachineFunction &MF = DAG.getMachineFunction();
276 MachineFrameInfo *MFI = MF.getFrameInfo();
277
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000278 // Add DAG nodes to load the arguments... On entry to a function on the X86,
279 // the stack frame looks like this:
280 //
281 // [ESP] -- return address
282 // [ESP + 4] -- first argument (leftmost lexically)
283 // [ESP + 8] -- second argument, if first argument is four bytes in size
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000284 // ...
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000285 //
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000286 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattnere4d5c442005-03-15 04:54:21 +0000287 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000288 MVT::ValueType ObjectVT = getValueType(I->getType());
289 unsigned ArgIncrement = 4;
290 unsigned ObjSize;
291 switch (ObjectVT) {
292 default: assert(0 && "Unhandled argument type!");
293 case MVT::i1:
294 case MVT::i8: ObjSize = 1; break;
295 case MVT::i16: ObjSize = 2; break;
296 case MVT::i32: ObjSize = 4; break;
297 case MVT::i64: ObjSize = ArgIncrement = 8; break;
298 case MVT::f32: ObjSize = 4; break;
299 case MVT::f64: ObjSize = ArgIncrement = 8; break;
300 }
301 // Create the frame index object for this incoming parameter...
302 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000303
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000304 // Create the SelectionDAG nodes corresponding to a load from this parameter
305 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
306
307 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
308 // dead loads.
309 SDOperand ArgValue;
310 if (!I->use_empty())
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000311 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
312 DAG.getSrcValue(NULL));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000313 else {
314 if (MVT::isInteger(ObjectVT))
315 ArgValue = DAG.getConstant(0, ObjectVT);
316 else
317 ArgValue = DAG.getConstantFP(0, ObjectVT);
318 }
319 ArgValues.push_back(ArgValue);
320
321 ArgOffset += ArgIncrement; // Move on to the next argument...
322 }
323
324 // If the function takes variable number of arguments, make a frame index for
325 // the start of the first vararg value... for expansion of llvm.va_start.
326 if (F.isVarArg())
327 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner3648c672005-05-13 21:44:04 +0000328 ReturnAddrIndex = 0; // No return address slot generated yet.
329 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattner381e8872005-05-15 05:46:45 +0000330 BytesCallerReserves = ArgOffset;
Chris Lattner4c52f0e2005-04-09 15:23:56 +0000331
332 // Finally, inform the code generator which regs we return values in.
333 switch (getValueType(F.getReturnType())) {
334 default: assert(0 && "Unknown type!");
335 case MVT::isVoid: break;
336 case MVT::i1:
337 case MVT::i8:
338 case MVT::i16:
339 case MVT::i32:
340 MF.addLiveOut(X86::EAX);
341 break;
342 case MVT::i64:
343 MF.addLiveOut(X86::EAX);
344 MF.addLiveOut(X86::EDX);
345 break;
346 case MVT::f32:
347 case MVT::f64:
348 MF.addLiveOut(X86::ST0);
349 break;
350 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000351 return ArgValues;
352}
353
Chris Lattner5188ad72005-01-08 19:28:19 +0000354std::pair<SDOperand, SDOperand>
Chris Lattnerc6f41812005-05-12 23:06:28 +0000355X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
Chris Lattner2e7714a2005-05-13 20:29:13 +0000356 bool isVarArg, bool isTailCall,
357 SDOperand Callee, ArgListTy &Args,
358 SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000359 // Count how many bytes are to be pushed on the stack.
360 unsigned NumBytes = 0;
361
362 if (Args.empty()) {
363 // Save zero bytes.
Chris Lattner16cd04d2005-05-12 23:24:06 +0000364 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattner5188ad72005-01-08 19:28:19 +0000365 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000366 } else {
367 for (unsigned i = 0, e = Args.size(); i != e; ++i)
368 switch (getValueType(Args[i].second)) {
369 default: assert(0 && "Unknown value type!");
370 case MVT::i1:
371 case MVT::i8:
372 case MVT::i16:
373 case MVT::i32:
374 case MVT::f32:
375 NumBytes += 4;
376 break;
377 case MVT::i64:
378 case MVT::f64:
379 NumBytes += 8;
380 break;
381 }
382
Chris Lattner16cd04d2005-05-12 23:24:06 +0000383 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattner5188ad72005-01-08 19:28:19 +0000384 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000385
386 // Arguments go on the stack in reverse order, as specified by the ABI.
387 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000388 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
389 DAG.getEntryNode());
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000390 std::vector<SDOperand> Stores;
391
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000392 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000393 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
394 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
395
396 switch (getValueType(Args[i].second)) {
397 default: assert(0 && "Unexpected ValueType for argument!");
398 case MVT::i1:
399 case MVT::i8:
400 case MVT::i16:
401 // Promote the integer to 32 bits. If the input type is signed use a
402 // sign extend, otherwise use a zero extend.
403 if (Args[i].second->isSigned())
404 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
405 else
406 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
407
408 // FALL THROUGH
409 case MVT::i32:
410 case MVT::f32:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000411 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000412 Args[i].first, PtrOff,
413 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000414 ArgOffset += 4;
415 break;
416 case MVT::i64:
417 case MVT::f64:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000418 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000419 Args[i].first, PtrOff,
420 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000421 ArgOffset += 8;
422 break;
423 }
424 }
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000425 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000426 }
427
428 std::vector<MVT::ValueType> RetVals;
429 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000430 RetVals.push_back(MVT::Other);
431
Chris Lattner239738a2005-05-14 08:48:15 +0000432 // The result values produced have to be legal. Promote the result.
433 switch (RetTyVT) {
434 case MVT::isVoid: break;
435 default:
436 RetVals.push_back(RetTyVT);
437 break;
438 case MVT::i1:
439 case MVT::i8:
440 case MVT::i16:
441 RetVals.push_back(MVT::i32);
442 break;
443 case MVT::f32:
Nate Begemanf63be7d2005-07-06 18:59:04 +0000444 if (X86ScalarSSE)
445 RetVals.push_back(MVT::f32);
446 else
447 RetVals.push_back(MVT::f64);
Chris Lattner239738a2005-05-14 08:48:15 +0000448 break;
449 case MVT::i64:
450 RetVals.push_back(MVT::i32);
451 RetVals.push_back(MVT::i32);
452 break;
453 }
454 std::vector<SDOperand> Ops;
455 Ops.push_back(Chain);
456 Ops.push_back(Callee);
457 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
458 Ops.push_back(DAG.getConstant(0, getPointerTy()));
459 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
460 RetVals, Ops);
461 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
462
463 SDOperand ResultVal;
464 switch (RetTyVT) {
465 case MVT::isVoid: break;
466 default:
467 ResultVal = TheCall.getValue(1);
468 break;
469 case MVT::i1:
470 case MVT::i8:
471 case MVT::i16:
472 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
473 break;
474 case MVT::f32:
475 // FIXME: we would really like to remember that this FP_ROUND operation is
476 // okay to eliminate if we allow excess FP precision.
477 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
478 break;
479 case MVT::i64:
480 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
481 TheCall.getValue(2));
482 break;
483 }
484
485 return std::make_pair(ResultVal, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000486}
487
Chris Lattnere0fe2252005-07-05 19:58:54 +0000488SDOperand
489X86TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
490 Value *VAListV, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000491 // vastart just stores the address of the VarArgsFrameIndex slot.
492 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000493 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
494 DAG.getSrcValue(VAListV));
Chris Lattner14824582005-01-09 00:01:27 +0000495}
496
Chris Lattnere0fe2252005-07-05 19:58:54 +0000497
498std::pair<SDOperand,SDOperand>
499X86TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP,
500 Value *VAListV, const Type *ArgTy,
501 SelectionDAG &DAG) {
Chris Lattner14824582005-01-09 00:01:27 +0000502 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000503 SDOperand Val = DAG.getLoad(MVT::i32, Chain,
504 VAListP, DAG.getSrcValue(VAListV));
505 SDOperand Result = DAG.getLoad(ArgVT, Chain, Val,
Chris Lattner08568cf2005-07-05 17:50:16 +0000506 DAG.getSrcValue(NULL));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000507 unsigned Amt;
508 if (ArgVT == MVT::i32)
509 Amt = 4;
510 else {
511 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
512 "Other types should have been promoted for varargs!");
513 Amt = 8;
Chris Lattner14824582005-01-09 00:01:27 +0000514 }
Andrew Lenharth558bc882005-06-18 18:34:52 +0000515 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
516 DAG.getConstant(Amt, Val.getValueType()));
517 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000518 Val, VAListP, DAG.getSrcValue(VAListV));
Chris Lattner14824582005-01-09 00:01:27 +0000519 return std::make_pair(Result, Chain);
520}
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000521
Chris Lattnerc6f41812005-05-12 23:06:28 +0000522//===----------------------------------------------------------------------===//
Chris Lattner653f7232005-05-13 22:46:57 +0000523// Fast Calling Convention implementation
Chris Lattnerc6f41812005-05-12 23:06:28 +0000524//===----------------------------------------------------------------------===//
525//
526// The X86 'fast' calling convention passes up to two integer arguments in
527// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
528// and requires that the callee pop its arguments off the stack (allowing proper
529// tail calls), and has the same return value conventions as C calling convs.
530//
Chris Lattner10d26452005-05-13 23:49:10 +0000531// This calling convention always arranges for the callee pop value to be 8n+4
532// bytes, which is needed for tail recursion elimination and stack alignment
533// reasons.
534//
Chris Lattnerc6f41812005-05-12 23:06:28 +0000535// Note that this can be enhanced in the future to pass fp vals in registers
536// (when we have a global fp allocator) and do other tricks.
537//
Chris Lattner63602fb2005-05-13 07:38:09 +0000538
539/// AddLiveIn - This helper function adds the specified physical register to the
540/// MachineFunction as a live in value. It also creates a corresponding virtual
541/// register for it.
542static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
543 TargetRegisterClass *RC) {
544 assert(RC->contains(PReg) && "Not the correct regclass!");
545 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
546 MF.addLiveIn(PReg, VReg);
547 return VReg;
548}
549
550
Chris Lattnerc6f41812005-05-12 23:06:28 +0000551std::vector<SDOperand>
552X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
553 std::vector<SDOperand> ArgValues;
554
555 MachineFunction &MF = DAG.getMachineFunction();
556 MachineFrameInfo *MFI = MF.getFrameInfo();
557
558 // Add DAG nodes to load the arguments... On entry to a function the stack
559 // frame looks like this:
560 //
561 // [ESP] -- return address
562 // [ESP + 4] -- first nonreg argument (leftmost lexically)
563 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
564 // ...
565 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
566
567 // Keep track of the number of integer regs passed so far. This can be either
568 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
569 // used).
570 unsigned NumIntRegs = 0;
571
572 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
573 MVT::ValueType ObjectVT = getValueType(I->getType());
574 unsigned ArgIncrement = 4;
575 unsigned ObjSize = 0;
576 SDOperand ArgValue;
577
578 switch (ObjectVT) {
579 default: assert(0 && "Unhandled argument type!");
580 case MVT::i1:
581 case MVT::i8:
582 if (NumIntRegs < 2) {
583 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000584 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
585 X86::R8RegisterClass);
586 ArgValue = DAG.getCopyFromReg(VReg, MVT::i8, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000587 DAG.setRoot(ArgValue.getValue(1));
588 }
589 ++NumIntRegs;
590 break;
591 }
592
593 ObjSize = 1;
594 break;
595 case MVT::i16:
596 if (NumIntRegs < 2) {
597 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000598 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
599 X86::R16RegisterClass);
600 ArgValue = DAG.getCopyFromReg(VReg, MVT::i16, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000601 DAG.setRoot(ArgValue.getValue(1));
602 }
603 ++NumIntRegs;
604 break;
605 }
606 ObjSize = 2;
607 break;
608 case MVT::i32:
609 if (NumIntRegs < 2) {
610 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000611 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
612 X86::R32RegisterClass);
613 ArgValue = DAG.getCopyFromReg(VReg, MVT::i32, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000614 DAG.setRoot(ArgValue.getValue(1));
615 }
616 ++NumIntRegs;
617 break;
618 }
619 ObjSize = 4;
620 break;
621 case MVT::i64:
622 if (NumIntRegs == 0) {
623 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000624 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
625 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000626
Chris Lattner63602fb2005-05-13 07:38:09 +0000627 SDOperand Low=DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot());
628 SDOperand Hi =DAG.getCopyFromReg(TopReg, MVT::i32, Low.getValue(1));
Chris Lattnerc6f41812005-05-12 23:06:28 +0000629 DAG.setRoot(Hi.getValue(1));
630
631 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
632 }
633 NumIntRegs = 2;
634 break;
635 } else if (NumIntRegs == 1) {
636 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000637 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
638 SDOperand Low = DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000639 DAG.setRoot(Low.getValue(1));
640
641 // Load the high part from memory.
642 // Create the frame index object for this incoming parameter...
643 int FI = MFI->CreateFixedObject(4, ArgOffset);
644 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
645 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
646 DAG.getSrcValue(NULL));
647 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
648 }
649 ArgOffset += 4;
650 NumIntRegs = 2;
651 break;
652 }
653 ObjSize = ArgIncrement = 8;
654 break;
655 case MVT::f32: ObjSize = 4; break;
656 case MVT::f64: ObjSize = ArgIncrement = 8; break;
657 }
658
659 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
660 // dead loads.
661 if (ObjSize && !I->use_empty()) {
662 // Create the frame index object for this incoming parameter...
663 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
664
665 // Create the SelectionDAG nodes corresponding to a load from this
666 // parameter.
667 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
668
669 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
670 DAG.getSrcValue(NULL));
671 } else if (ArgValue.Val == 0) {
672 if (MVT::isInteger(ObjectVT))
673 ArgValue = DAG.getConstant(0, ObjectVT);
674 else
675 ArgValue = DAG.getConstantFP(0, ObjectVT);
676 }
677 ArgValues.push_back(ArgValue);
678
679 if (ObjSize)
680 ArgOffset += ArgIncrement; // Move on to the next argument.
681 }
682
Chris Lattner10d26452005-05-13 23:49:10 +0000683 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
684 // arguments and the arguments after the retaddr has been pushed are aligned.
685 if ((ArgOffset & 7) == 0)
686 ArgOffset += 4;
687
Chris Lattner3648c672005-05-13 21:44:04 +0000688 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
689 ReturnAddrIndex = 0; // No return address slot generated yet.
690 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
Chris Lattner381e8872005-05-15 05:46:45 +0000691 BytesCallerReserves = 0;
Chris Lattnerc6f41812005-05-12 23:06:28 +0000692
693 // Finally, inform the code generator which regs we return values in.
694 switch (getValueType(F.getReturnType())) {
695 default: assert(0 && "Unknown type!");
696 case MVT::isVoid: break;
697 case MVT::i1:
698 case MVT::i8:
699 case MVT::i16:
700 case MVT::i32:
701 MF.addLiveOut(X86::EAX);
702 break;
703 case MVT::i64:
704 MF.addLiveOut(X86::EAX);
705 MF.addLiveOut(X86::EDX);
706 break;
707 case MVT::f32:
708 case MVT::f64:
709 MF.addLiveOut(X86::ST0);
710 break;
711 }
712 return ArgValues;
713}
714
715std::pair<SDOperand, SDOperand>
716X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
Chris Lattner2e7714a2005-05-13 20:29:13 +0000717 bool isTailCall, SDOperand Callee,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000718 ArgListTy &Args, SelectionDAG &DAG) {
719 // Count how many bytes are to be pushed on the stack.
720 unsigned NumBytes = 0;
721
722 // Keep track of the number of integer regs passed so far. This can be either
723 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
724 // used).
725 unsigned NumIntRegs = 0;
726
727 for (unsigned i = 0, e = Args.size(); i != e; ++i)
728 switch (getValueType(Args[i].second)) {
729 default: assert(0 && "Unknown value type!");
730 case MVT::i1:
731 case MVT::i8:
732 case MVT::i16:
733 case MVT::i32:
734 if (NumIntRegs < 2) {
735 ++NumIntRegs;
736 break;
737 }
738 // fall through
739 case MVT::f32:
740 NumBytes += 4;
741 break;
742 case MVT::i64:
743 if (NumIntRegs == 0) {
744 NumIntRegs = 2;
745 break;
746 } else if (NumIntRegs == 1) {
747 NumIntRegs = 2;
748 NumBytes += 4;
749 break;
750 }
751
752 // fall through
753 case MVT::f64:
754 NumBytes += 8;
755 break;
756 }
757
Chris Lattner10d26452005-05-13 23:49:10 +0000758 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
759 // arguments and the arguments after the retaddr has been pushed are aligned.
760 if ((NumBytes & 7) == 0)
761 NumBytes += 4;
762
Chris Lattner16cd04d2005-05-12 23:24:06 +0000763 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000764 DAG.getConstant(NumBytes, getPointerTy()));
765
766 // Arguments go on the stack in reverse order, as specified by the ABI.
767 unsigned ArgOffset = 0;
768 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
769 DAG.getEntryNode());
770 NumIntRegs = 0;
771 std::vector<SDOperand> Stores;
772 std::vector<SDOperand> RegValuesToPass;
773 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
774 switch (getValueType(Args[i].second)) {
775 default: assert(0 && "Unexpected ValueType for argument!");
776 case MVT::i1:
777 case MVT::i8:
778 case MVT::i16:
779 case MVT::i32:
780 if (NumIntRegs < 2) {
781 RegValuesToPass.push_back(Args[i].first);
782 ++NumIntRegs;
783 break;
784 }
785 // Fall through
786 case MVT::f32: {
787 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
788 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
789 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
790 Args[i].first, PtrOff,
791 DAG.getSrcValue(NULL)));
792 ArgOffset += 4;
793 break;
794 }
795 case MVT::i64:
796 if (NumIntRegs < 2) { // Can pass part of it in regs?
797 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
798 Args[i].first, DAG.getConstant(1, MVT::i32));
799 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
800 Args[i].first, DAG.getConstant(0, MVT::i32));
801 RegValuesToPass.push_back(Lo);
802 ++NumIntRegs;
803 if (NumIntRegs < 2) { // Pass both parts in regs?
804 RegValuesToPass.push_back(Hi);
805 ++NumIntRegs;
806 } else {
807 // Pass the high part in memory.
808 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
809 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
810 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner920c0aa2005-05-14 12:03:10 +0000811 Hi, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattnerc6f41812005-05-12 23:06:28 +0000812 ArgOffset += 4;
813 }
814 break;
815 }
816 // Fall through
817 case MVT::f64:
818 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
819 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
820 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
821 Args[i].first, PtrOff,
822 DAG.getSrcValue(NULL)));
823 ArgOffset += 8;
824 break;
825 }
826 }
827 if (!Stores.empty())
828 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
829
Chris Lattner10d26452005-05-13 23:49:10 +0000830 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
831 // arguments and the arguments after the retaddr has been pushed are aligned.
832 if ((ArgOffset & 7) == 0)
833 ArgOffset += 4;
834
Chris Lattner239738a2005-05-14 08:48:15 +0000835 std::vector<MVT::ValueType> RetVals;
836 MVT::ValueType RetTyVT = getValueType(RetTy);
837
838 RetVals.push_back(MVT::Other);
839
840 // The result values produced have to be legal. Promote the result.
841 switch (RetTyVT) {
842 case MVT::isVoid: break;
843 default:
844 RetVals.push_back(RetTyVT);
845 break;
846 case MVT::i1:
847 case MVT::i8:
848 case MVT::i16:
849 RetVals.push_back(MVT::i32);
850 break;
851 case MVT::f32:
Nate Begemanf63be7d2005-07-06 18:59:04 +0000852 if (X86ScalarSSE)
853 RetVals.push_back(MVT::f32);
854 else
855 RetVals.push_back(MVT::f64);
Chris Lattner239738a2005-05-14 08:48:15 +0000856 break;
857 case MVT::i64:
858 RetVals.push_back(MVT::i32);
859 RetVals.push_back(MVT::i32);
860 break;
861 }
862
863 std::vector<SDOperand> Ops;
864 Ops.push_back(Chain);
865 Ops.push_back(Callee);
866 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
867 // Callee pops all arg values on the stack.
868 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
869
870 // Pass register arguments as needed.
871 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
872
873 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
874 RetVals, Ops);
875 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
876
877 SDOperand ResultVal;
878 switch (RetTyVT) {
879 case MVT::isVoid: break;
880 default:
881 ResultVal = TheCall.getValue(1);
882 break;
883 case MVT::i1:
884 case MVT::i8:
885 case MVT::i16:
886 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
887 break;
888 case MVT::f32:
889 // FIXME: we would really like to remember that this FP_ROUND operation is
890 // okay to eliminate if we allow excess FP precision.
891 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
892 break;
893 case MVT::i64:
894 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
895 TheCall.getValue(2));
896 break;
897 }
898
899 return std::make_pair(ResultVal, Chain);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000900}
901
Chris Lattner381e8872005-05-15 05:46:45 +0000902SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
903 if (ReturnAddrIndex == 0) {
904 // Set up a frame object for the return address.
905 MachineFunction &MF = DAG.getMachineFunction();
906 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
907 }
908
909 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
910}
Chris Lattnerc6f41812005-05-12 23:06:28 +0000911
912
Chris Lattner14824582005-01-09 00:01:27 +0000913
914std::pair<SDOperand, SDOperand> X86TargetLowering::
915LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
916 SelectionDAG &DAG) {
917 SDOperand Result;
918 if (Depth) // Depths > 0 not supported yet!
919 Result = DAG.getConstant(0, getPointerTy());
920 else {
Chris Lattner381e8872005-05-15 05:46:45 +0000921 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000922 if (!isFrameAddress)
923 // Just load the return address
Chris Lattnerc6f41812005-05-12 23:06:28 +0000924 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
925 DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000926 else
927 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
928 DAG.getConstant(4, MVT::i32));
929 }
930 return std::make_pair(Result, Chain);
931}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000932
Chris Lattnera28381c2005-07-16 00:28:20 +0000933//===----------------------------------------------------------------------===//
934// X86 Custom Lowering Hooks
935//===----------------------------------------------------------------------===//
936
Chris Lattner67649df2005-05-14 06:52:07 +0000937/// LowerOperation - Provide custom lowering hooks for some operations.
938///
939SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
940 switch (Op.getOpcode()) {
941 default: assert(0 && "Should not custom lower this!");
942 case ISD::SINT_TO_FP:
943 assert(Op.getValueType() == MVT::f64 &&
944 Op.getOperand(0).getValueType() == MVT::i64 &&
945 "Unknown SINT_TO_FP to lower!");
946 // We lower sint64->FP into a store to a temporary stack slot, followed by a
947 // FILD64m node.
948 MachineFunction &MF = DAG.getMachineFunction();
949 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
950 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
951 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
952 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
953 std::vector<MVT::ValueType> RTs;
954 RTs.push_back(MVT::f64);
955 RTs.push_back(MVT::Other);
956 std::vector<SDOperand> Ops;
957 Ops.push_back(Store);
958 Ops.push_back(StackSlot);
959 return DAG.getNode(X86ISD::FILD64m, RTs, Ops);
960 }
961}
962
963
964//===----------------------------------------------------------------------===//
965// Pattern Matcher Implementation
966//===----------------------------------------------------------------------===//
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000967
Chris Lattner98a8ba02005-01-18 01:06:26 +0000968namespace {
969 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
970 /// SDOperand's instead of register numbers for the leaves of the matched
971 /// tree.
972 struct X86ISelAddressMode {
973 enum {
974 RegBase,
975 FrameIndexBase,
976 } BaseType;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000977
Chris Lattner98a8ba02005-01-18 01:06:26 +0000978 struct { // This is really a union, discriminated by BaseType!
979 SDOperand Reg;
980 int FrameIndex;
981 } Base;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000982
Chris Lattner98a8ba02005-01-18 01:06:26 +0000983 unsigned Scale;
984 SDOperand IndexReg;
985 unsigned Disp;
986 GlobalValue *GV;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000987
Chris Lattner98a8ba02005-01-18 01:06:26 +0000988 X86ISelAddressMode()
989 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
990 }
991 };
992}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000993
994
995namespace {
996 Statistic<>
997 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
998
999 //===--------------------------------------------------------------------===//
1000 /// ISel - X86 specific code to select X86 machine instructions for
1001 /// SelectionDAG operations.
1002 ///
1003 class ISel : public SelectionDAGISel {
1004 /// ContainsFPCode - Every instruction we select that uses or defines a FP
1005 /// register should set this to true.
1006 bool ContainsFPCode;
1007
1008 /// X86Lowering - This object fully describes how to lower LLVM code to an
1009 /// X86-specific SelectionDAG.
1010 X86TargetLowering X86Lowering;
1011
Chris Lattner11333092005-01-11 03:11:44 +00001012 /// RegPressureMap - This keeps an approximate count of the number of
1013 /// registers required to evaluate each node in the graph.
1014 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001015
1016 /// ExprMap - As shared expressions are codegen'd, we keep track of which
1017 /// vreg the value is produced in, so we only emit one copy of each compiled
1018 /// tree.
1019 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001020
Chris Lattner381e8872005-05-15 05:46:45 +00001021 /// TheDAG - The DAG being selected during Select* operations.
1022 SelectionDAG *TheDAG;
Nate Begemanfb5792f2005-07-12 01:41:54 +00001023
1024 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
1025 /// make the right decision when generating code for different targets.
1026 const X86Subtarget *Subtarget;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001027 public:
1028 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
Nate Begemanfb5792f2005-07-12 01:41:54 +00001029 Subtarget = TM.getSubtarget<const X86Subtarget>();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001030 }
1031
Chris Lattner67b1c3c2005-01-21 21:35:14 +00001032 virtual const char *getPassName() const {
1033 return "X86 Pattern Instruction Selection";
1034 }
1035
Chris Lattner11333092005-01-11 03:11:44 +00001036 unsigned getRegPressure(SDOperand O) {
1037 return RegPressureMap[O.Val];
1038 }
1039 unsigned ComputeRegPressure(SDOperand O);
1040
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001041 /// InstructionSelectBasicBlock - This callback is invoked by
1042 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001043 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001044
Chris Lattner63602fb2005-05-13 07:38:09 +00001045 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
1046
Chris Lattner44129b52005-01-25 20:03:11 +00001047 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
1048 bool FloatPromoteOk = false);
Chris Lattnera5ade062005-01-11 21:19:59 +00001049 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +00001050 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattner30ea1e92005-01-19 07:37:26 +00001051 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001052 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +00001053 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001054 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
1055 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001056 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001057
1058 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
1059 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
1060 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner381e8872005-05-15 05:46:45 +00001061 bool EmitPotentialTailCall(SDNode *Node);
1062 void EmitFastCCToFastCCTailCall(SDNode *TailCallNode);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001063 void Select(SDOperand N);
1064 };
1065}
1066
Chris Lattner6415bb42005-05-10 03:53:18 +00001067/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
1068/// the main function.
1069static void EmitSpecialCodeForMain(MachineBasicBlock *BB,
1070 MachineFrameInfo *MFI) {
1071 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
1072 int CWFrameIdx = MFI->CreateStackObject(2, 2);
1073 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1074
1075 // Set the high part to be 64-bit precision.
1076 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1077 CWFrameIdx, 1).addImm(2);
1078
1079 // Reload the modified control word now.
1080 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1081}
1082
Chris Lattner63602fb2005-05-13 07:38:09 +00001083void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
1084 // If this function has live-in values, emit the copies from pregs to vregs at
1085 // the top of the function, before anything else.
1086 MachineBasicBlock *BB = MF.begin();
1087 if (MF.livein_begin() != MF.livein_end()) {
1088 SSARegMap *RegMap = MF.getSSARegMap();
1089 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
1090 E = MF.livein_end(); LI != E; ++LI) {
1091 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
1092 if (RC == X86::R8RegisterClass) {
1093 BuildMI(BB, X86::MOV8rr, 1, LI->second).addReg(LI->first);
1094 } else if (RC == X86::R16RegisterClass) {
1095 BuildMI(BB, X86::MOV16rr, 1, LI->second).addReg(LI->first);
1096 } else if (RC == X86::R32RegisterClass) {
1097 BuildMI(BB, X86::MOV32rr, 1, LI->second).addReg(LI->first);
1098 } else if (RC == X86::RFPRegisterClass) {
1099 BuildMI(BB, X86::FpMOV, 1, LI->second).addReg(LI->first);
Nate Begemanf63be7d2005-07-06 18:59:04 +00001100 } else if (RC == X86::RXMMRegisterClass) {
1101 BuildMI(BB, X86::MOVAPDrr, 1, LI->second).addReg(LI->first);
Chris Lattner63602fb2005-05-13 07:38:09 +00001102 } else {
1103 assert(0 && "Unknown regclass!");
1104 }
1105 }
1106 }
1107
1108
1109 // If this is main, emit special code for main.
1110 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
1111 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
1112}
1113
1114
Chris Lattner7dbcb752005-01-12 04:21:28 +00001115/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
1116/// when it has created a SelectionDAG for us to codegen.
1117void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
1118 // While we're doing this, keep track of whether we see any FP code for
1119 // FP_REG_KILL insertion.
1120 ContainsFPCode = false;
Chris Lattner6415bb42005-05-10 03:53:18 +00001121 MachineFunction *MF = BB->getParent();
Chris Lattner7dbcb752005-01-12 04:21:28 +00001122
1123 // Scan the PHI nodes that already are inserted into this basic block. If any
1124 // of them is a PHI of a floating point value, we need to insert an
1125 // FP_REG_KILL.
Chris Lattner6415bb42005-05-10 03:53:18 +00001126 SSARegMap *RegMap = MF->getSSARegMap();
Chris Lattner63602fb2005-05-13 07:38:09 +00001127 if (BB != MF->begin())
1128 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
1129 I != E; ++I) {
1130 assert(I->getOpcode() == X86::PHI &&
1131 "Isn't just PHI nodes?");
1132 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
1133 X86::RFPRegisterClass) {
1134 ContainsFPCode = true;
1135 break;
1136 }
Chris Lattner7dbcb752005-01-12 04:21:28 +00001137 }
Chris Lattner6415bb42005-05-10 03:53:18 +00001138
Chris Lattner7dbcb752005-01-12 04:21:28 +00001139 // Compute the RegPressureMap, which is an approximation for the number of
1140 // registers required to compute each node.
1141 ComputeRegPressure(DAG.getRoot());
1142
Chris Lattner381e8872005-05-15 05:46:45 +00001143 TheDAG = &DAG;
1144
Chris Lattner7dbcb752005-01-12 04:21:28 +00001145 // Codegen the basic block.
1146 Select(DAG.getRoot());
1147
Chris Lattner381e8872005-05-15 05:46:45 +00001148 TheDAG = 0;
1149
Chris Lattner7dbcb752005-01-12 04:21:28 +00001150 // Finally, look at all of the successors of this block. If any contain a PHI
1151 // node of FP type, we need to insert an FP_REG_KILL in this block.
1152 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
1153 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
1154 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
1155 I != E && I->getOpcode() == X86::PHI; ++I) {
1156 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
1157 X86::RFPRegisterClass) {
1158 ContainsFPCode = true;
1159 break;
1160 }
1161 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001162
Chris Lattnere3e0f272005-05-09 03:36:39 +00001163 // Final check, check LLVM BB's that are successors to the LLVM BB
1164 // corresponding to BB for FP PHI nodes.
1165 const BasicBlock *LLVMBB = BB->getBasicBlock();
1166 const PHINode *PN;
1167 if (!ContainsFPCode)
1168 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
1169 SI != E && !ContainsFPCode; ++SI)
1170 for (BasicBlock::const_iterator II = SI->begin();
1171 (PN = dyn_cast<PHINode>(II)); ++II)
1172 if (PN->getType()->isFloatingPoint()) {
1173 ContainsFPCode = true;
1174 break;
1175 }
1176
1177
Chris Lattner7dbcb752005-01-12 04:21:28 +00001178 // Insert FP_REG_KILL instructions into basic blocks that need them. This
1179 // only occurs due to the floating point stackifier not being aggressive
1180 // enough to handle arbitrary global stackification.
1181 //
1182 // Currently we insert an FP_REG_KILL instruction into each block that uses or
1183 // defines a floating point virtual register.
1184 //
1185 // When the global register allocators (like linear scan) finally update live
1186 // variable analysis, we can keep floating point values in registers across
1187 // basic blocks. This will be a huge win, but we are waiting on the global
1188 // allocators before we can do this.
1189 //
Chris Lattner71df3f82005-03-30 01:10:00 +00001190 if (ContainsFPCode) {
Chris Lattner7dbcb752005-01-12 04:21:28 +00001191 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
1192 ++NumFPKill;
1193 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001194
Chris Lattner7dbcb752005-01-12 04:21:28 +00001195 // Clear state used for selection.
1196 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +00001197 RegPressureMap.clear();
1198}
1199
1200
Chris Lattner11333092005-01-11 03:11:44 +00001201// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
1202// for the number of registers required to compute each node. This is basically
1203// computing a generalized form of the Sethi-Ullman number for each node.
1204unsigned ISel::ComputeRegPressure(SDOperand O) {
1205 SDNode *N = O.Val;
1206 unsigned &Result = RegPressureMap[N];
1207 if (Result) return Result;
1208
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001209 // FIXME: Should operations like CALL (which clobber lots o regs) have a
1210 // higher fixed cost??
1211
Chris Lattnerc4b6a782005-01-11 22:29:12 +00001212 if (N->getNumOperands() == 0) {
1213 Result = 1;
1214 } else {
1215 unsigned MaxRegUse = 0;
1216 unsigned NumExtraMaxRegUsers = 0;
1217 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1218 unsigned Regs;
1219 if (N->getOperand(i).getOpcode() == ISD::Constant)
1220 Regs = 0;
1221 else
1222 Regs = ComputeRegPressure(N->getOperand(i));
1223 if (Regs > MaxRegUse) {
1224 MaxRegUse = Regs;
1225 NumExtraMaxRegUsers = 0;
1226 } else if (Regs == MaxRegUse &&
1227 N->getOperand(i).getValueType() != MVT::Other) {
1228 ++NumExtraMaxRegUsers;
1229 }
Chris Lattner11333092005-01-11 03:11:44 +00001230 }
Chris Lattner90d1be72005-01-17 22:56:09 +00001231
1232 if (O.getOpcode() != ISD::TokenFactor)
1233 Result = MaxRegUse+NumExtraMaxRegUsers;
1234 else
Chris Lattner869e0432005-01-17 23:02:13 +00001235 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +00001236 }
Chris Lattnerafce4302005-01-12 02:19:06 +00001237
Chris Lattner837caa72005-01-11 23:21:30 +00001238 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +00001239 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001240}
1241
Chris Lattnerbf52d492005-01-20 16:50:16 +00001242/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
1243/// The DAG cannot have cycles in it, by definition, so the visited set is not
1244/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
1245/// reuse, so it prevents exponential cases.
1246///
1247static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
1248 std::set<SDNode*> &Visited) {
1249 if (N == Op) return true; // Found it.
1250 SDNode *Node = N.Val;
Chris Lattnerfb0f53f2005-01-21 21:43:02 +00001251 if (Node->getNumOperands() == 0 || // Leaf?
1252 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattnerbf52d492005-01-20 16:50:16 +00001253 if (!Visited.insert(Node).second) return false; // Already visited?
1254
1255 // Recurse for the first N-1 operands.
1256 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1257 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
1258 return true;
1259
1260 // Tail recurse for the last operand.
1261 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
1262}
1263
Chris Lattner98a8ba02005-01-18 01:06:26 +00001264X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
1265 X86AddressMode Result;
1266
1267 // If we need to emit two register operands, emit the one with the highest
1268 // register pressure first.
1269 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
1270 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +00001271 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +00001272 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +00001273 std::set<SDNode*> Visited;
1274 EmitBaseThenIndex = true;
1275 // If Base ends up pointing to Index, we must emit index first. This is
1276 // because of the way we fold loads, we may end up doing bad things with
1277 // the folded add.
1278 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
1279 EmitBaseThenIndex = false;
1280 } else {
1281 std::set<SDNode*> Visited;
1282 EmitBaseThenIndex = false;
1283 // If Base ends up pointing to Index, we must emit index first. This is
1284 // because of the way we fold loads, we may end up doing bad things with
1285 // the folded add.
1286 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
1287 EmitBaseThenIndex = true;
1288 }
1289
1290 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001291 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1292 Result.IndexReg = SelectExpr(IAM.IndexReg);
1293 } else {
1294 Result.IndexReg = SelectExpr(IAM.IndexReg);
1295 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1296 }
Chris Lattnerbf52d492005-01-20 16:50:16 +00001297
Chris Lattner98a8ba02005-01-18 01:06:26 +00001298 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
1299 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1300 } else if (IAM.IndexReg.Val) {
1301 Result.IndexReg = SelectExpr(IAM.IndexReg);
1302 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001303
Chris Lattner98a8ba02005-01-18 01:06:26 +00001304 switch (IAM.BaseType) {
1305 case X86ISelAddressMode::RegBase:
1306 Result.BaseType = X86AddressMode::RegBase;
1307 break;
1308 case X86ISelAddressMode::FrameIndexBase:
1309 Result.BaseType = X86AddressMode::FrameIndexBase;
1310 Result.Base.FrameIndex = IAM.Base.FrameIndex;
1311 break;
1312 default:
1313 assert(0 && "Unknown base type!");
1314 break;
1315 }
1316 Result.Scale = IAM.Scale;
1317 Result.Disp = IAM.Disp;
1318 Result.GV = IAM.GV;
1319 return Result;
1320}
1321
1322/// SelectAddress - Pattern match the maximal addressing mode for this node and
1323/// emit all of the leaf registers.
1324void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
1325 X86ISelAddressMode IAM;
1326 MatchAddress(N, IAM);
1327 AM = SelectAddrExprs(IAM);
1328}
1329
1330/// MatchAddress - Add the specified node to the specified addressing mode,
1331/// returning true if it cannot be done. This just pattern matches for the
1332/// addressing mode, it does not cause any code to be emitted. For that, use
1333/// SelectAddress.
1334bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001335 switch (N.getOpcode()) {
1336 default: break;
1337 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +00001338 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
1339 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001340 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
1341 return false;
1342 }
1343 break;
1344 case ISD::GlobalAddress:
1345 if (AM.GV == 0) {
Nate Begemanfb5792f2005-07-12 01:41:54 +00001346 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1347 // For Darwin, external and weak symbols are indirect, so we want to load
1348 // the value at address GV, not the value of GV itself. This means that
1349 // the GlobalAddress must be in the base or index register of the address,
1350 // not the GV offset field.
1351 if (Subtarget->getIndirectExternAndWeakGlobals() &&
1352 (GV->hasWeakLinkage() || GV->isExternal())) {
1353 break;
1354 } else {
1355 AM.GV = GV;
1356 return false;
1357 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001358 }
1359 break;
1360 case ISD::Constant:
1361 AM.Disp += cast<ConstantSDNode>(N)->getValue();
1362 return false;
1363 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +00001364 // We might have folded the load into this shift, so don't regen the value
1365 // if so.
1366 if (ExprMap.count(N)) break;
1367
Chris Lattner98a8ba02005-01-18 01:06:26 +00001368 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001369 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
1370 unsigned Val = CN->getValue();
1371 if (Val == 1 || Val == 2 || Val == 3) {
1372 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +00001373 SDOperand ShVal = N.Val->getOperand(0);
1374
1375 // Okay, we know that we have a scale by now. However, if the scaled
1376 // value is an add of something and a constant, we can fold the
1377 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +00001378 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +00001379 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001380 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +00001381 ConstantSDNode *AddVal =
1382 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
1383 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +00001384 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001385 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +00001386 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001387 return false;
1388 }
1389 }
1390 break;
Chris Lattner947d5442005-01-11 19:37:02 +00001391 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +00001392 // We might have folded the load into this mul, so don't regen the value if
1393 // so.
1394 if (ExprMap.count(N)) break;
1395
Chris Lattner947d5442005-01-11 19:37:02 +00001396 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +00001397 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
1398 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +00001399 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
1400 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
1401 AM.Scale = unsigned(CN->getValue())-1;
1402
1403 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001404 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +00001405
1406 // Okay, we know that we have a scale by now. However, if the scaled
1407 // value is an add of something and a constant, we can fold the
1408 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +00001409 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +00001410 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001411 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +00001412 ConstantSDNode *AddVal =
1413 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
1414 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001415 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001416 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +00001417 }
1418
1419 AM.IndexReg = AM.Base.Reg = Reg;
1420 return false;
1421 }
1422 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001423
1424 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +00001425 // We might have folded the load into this mul, so don't regen the value if
1426 // so.
1427 if (ExprMap.count(N)) break;
1428
Chris Lattner98a8ba02005-01-18 01:06:26 +00001429 X86ISelAddressMode Backup = AM;
1430 if (!MatchAddress(N.Val->getOperand(0), AM) &&
1431 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001432 return false;
1433 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +00001434 if (!MatchAddress(N.Val->getOperand(1), AM) &&
1435 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +00001436 return false;
1437 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001438 break;
1439 }
1440 }
1441
Chris Lattnera95589b2005-01-11 04:40:19 +00001442 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +00001443 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +00001444 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +00001445 if (AM.IndexReg.Val == 0) {
1446 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +00001447 AM.Scale = 1;
1448 return false;
1449 }
1450
1451 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001452 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +00001453 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001454
1455 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +00001456 AM.BaseType = X86ISelAddressMode::RegBase;
1457 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001458 return false;
1459}
1460
1461/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
1462/// assuming that the temporary registers are in the 8-bit register class.
1463///
1464/// Tmp1 = setcc1
1465/// Tmp2 = setcc2
1466/// DestReg = logicalop Tmp1, Tmp2
1467///
1468static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
1469 unsigned SetCC2, unsigned LogicalOp,
1470 unsigned DestReg) {
1471 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
1472 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1473 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1474 BuildMI(BB, SetCC1, 0, Tmp1);
1475 BuildMI(BB, SetCC2, 0, Tmp2);
1476 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
1477}
1478
1479/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
1480/// condition codes match the specified SetCCOpcode. Note that some conditions
1481/// require multiple instructions to generate the correct value.
1482static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
1483 ISD::CondCode SetCCOpcode, bool isFP) {
1484 unsigned Opc;
1485 if (!isFP) {
1486 switch (SetCCOpcode) {
1487 default: assert(0 && "Illegal integer SetCC!");
1488 case ISD::SETEQ: Opc = X86::SETEr; break;
1489 case ISD::SETGT: Opc = X86::SETGr; break;
1490 case ISD::SETGE: Opc = X86::SETGEr; break;
1491 case ISD::SETLT: Opc = X86::SETLr; break;
1492 case ISD::SETLE: Opc = X86::SETLEr; break;
1493 case ISD::SETNE: Opc = X86::SETNEr; break;
1494 case ISD::SETULT: Opc = X86::SETBr; break;
1495 case ISD::SETUGT: Opc = X86::SETAr; break;
1496 case ISD::SETULE: Opc = X86::SETBEr; break;
1497 case ISD::SETUGE: Opc = X86::SETAEr; break;
1498 }
1499 } else {
1500 // On a floating point condition, the flags are set as follows:
1501 // ZF PF CF op
1502 // 0 | 0 | 0 | X > Y
1503 // 0 | 0 | 1 | X < Y
1504 // 1 | 0 | 0 | X == Y
1505 // 1 | 1 | 1 | unordered
1506 //
1507 switch (SetCCOpcode) {
1508 default: assert(0 && "Invalid FP setcc!");
1509 case ISD::SETUEQ:
1510 case ISD::SETEQ:
1511 Opc = X86::SETEr; // True if ZF = 1
1512 break;
1513 case ISD::SETOGT:
1514 case ISD::SETGT:
1515 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
1516 break;
1517 case ISD::SETOGE:
1518 case ISD::SETGE:
1519 Opc = X86::SETAEr; // True if CF = 0
1520 break;
1521 case ISD::SETULT:
1522 case ISD::SETLT:
1523 Opc = X86::SETBr; // True if CF = 1
1524 break;
1525 case ISD::SETULE:
1526 case ISD::SETLE:
1527 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
1528 break;
1529 case ISD::SETONE:
1530 case ISD::SETNE:
1531 Opc = X86::SETNEr; // True if ZF = 0
1532 break;
1533 case ISD::SETUO:
1534 Opc = X86::SETPr; // True if PF = 1
1535 break;
1536 case ISD::SETO:
1537 Opc = X86::SETNPr; // True if PF = 0
1538 break;
1539 case ISD::SETOEQ: // !PF & ZF
1540 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
1541 return;
1542 case ISD::SETOLT: // !PF & CF
1543 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
1544 return;
1545 case ISD::SETOLE: // !PF & (CF || ZF)
1546 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
1547 return;
1548 case ISD::SETUGT: // PF | (!ZF & !CF)
1549 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
1550 return;
1551 case ISD::SETUGE: // PF | !CF
1552 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
1553 return;
1554 case ISD::SETUNE: // PF | !ZF
1555 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
1556 return;
1557 }
1558 }
1559 BuildMI(BB, Opc, 0, DestReg);
1560}
1561
1562
1563/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
1564/// the Dest block if the Cond condition is true. If we cannot fold this
1565/// condition into the branch, return true.
1566///
Chris Lattner6c07aee2005-01-11 04:06:27 +00001567bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
1568 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001569 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
1570 // B) using two conditional branches instead of one condbr, two setcc's, and
1571 // an or.
1572 if ((Cond.getOpcode() == ISD::OR ||
1573 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
1574 // And and or set the flags for us, so there is no need to emit a TST of the
1575 // result. It is only safe to do this if there is only a single use of the
1576 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +00001577 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001578 SelectExpr(Cond);
1579 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
1580 return false;
1581 }
1582
1583 // Codegen br not C -> JE.
1584 if (Cond.getOpcode() == ISD::XOR)
1585 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
1586 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +00001587 unsigned CondR;
1588 if (getRegPressure(Chain) > getRegPressure(Cond)) {
1589 Select(Chain);
1590 CondR = SelectExpr(Cond.Val->getOperand(0));
1591 } else {
1592 CondR = SelectExpr(Cond.Val->getOperand(0));
1593 Select(Chain);
1594 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001595 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
1596 BuildMI(BB, X86::JE, 1).addMBB(Dest);
1597 return false;
1598 }
1599
1600 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
1601 if (SetCC == 0)
1602 return true; // Can only handle simple setcc's so far.
1603
1604 unsigned Opc;
1605
1606 // Handle integer conditions first.
1607 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1608 switch (SetCC->getCondition()) {
1609 default: assert(0 && "Illegal integer SetCC!");
1610 case ISD::SETEQ: Opc = X86::JE; break;
1611 case ISD::SETGT: Opc = X86::JG; break;
1612 case ISD::SETGE: Opc = X86::JGE; break;
1613 case ISD::SETLT: Opc = X86::JL; break;
1614 case ISD::SETLE: Opc = X86::JLE; break;
1615 case ISD::SETNE: Opc = X86::JNE; break;
1616 case ISD::SETULT: Opc = X86::JB; break;
1617 case ISD::SETUGT: Opc = X86::JA; break;
1618 case ISD::SETULE: Opc = X86::JBE; break;
1619 case ISD::SETUGE: Opc = X86::JAE; break;
1620 }
Chris Lattner6c07aee2005-01-11 04:06:27 +00001621 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001622 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001623 BuildMI(BB, Opc, 1).addMBB(Dest);
1624 return false;
1625 }
1626
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001627 unsigned Opc2 = 0; // Second branch if needed.
1628
1629 // On a floating point condition, the flags are set as follows:
1630 // ZF PF CF op
1631 // 0 | 0 | 0 | X > Y
1632 // 0 | 0 | 1 | X < Y
1633 // 1 | 0 | 0 | X == Y
1634 // 1 | 1 | 1 | unordered
1635 //
1636 switch (SetCC->getCondition()) {
1637 default: assert(0 && "Invalid FP setcc!");
1638 case ISD::SETUEQ:
1639 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
1640 case ISD::SETOGT:
1641 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
1642 case ISD::SETOGE:
1643 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
1644 case ISD::SETULT:
1645 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
1646 case ISD::SETULE:
1647 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
1648 case ISD::SETONE:
1649 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
1650 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
1651 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
1652 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1653 Opc = X86::JA; // ZF = 0 & CF = 0
1654 Opc2 = X86::JP; // PF = 1
1655 break;
1656 case ISD::SETUGE: // PF = 1 | CF = 0
1657 Opc = X86::JAE; // CF = 0
1658 Opc2 = X86::JP; // PF = 1
1659 break;
1660 case ISD::SETUNE: // PF = 1 | ZF = 0
1661 Opc = X86::JNE; // ZF = 0
1662 Opc2 = X86::JP; // PF = 1
1663 break;
1664 case ISD::SETOEQ: // PF = 0 & ZF = 1
1665 //X86::JNP, X86::JE
1666 //X86::AND8rr
1667 return true; // FIXME: Emit more efficient code for this branch.
1668 case ISD::SETOLT: // PF = 0 & CF = 1
1669 //X86::JNP, X86::JB
1670 //X86::AND8rr
1671 return true; // FIXME: Emit more efficient code for this branch.
1672 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1673 //X86::JNP, X86::JBE
1674 //X86::AND8rr
1675 return true; // FIXME: Emit more efficient code for this branch.
1676 }
1677
Chris Lattner6c07aee2005-01-11 04:06:27 +00001678 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001679 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001680 BuildMI(BB, Opc, 1).addMBB(Dest);
1681 if (Opc2)
1682 BuildMI(BB, Opc2, 1).addMBB(Dest);
1683 return false;
1684}
1685
Chris Lattner24aad1b2005-01-10 22:10:13 +00001686/// EmitSelectCC - Emit code into BB that performs a select operation between
1687/// the two registers RTrue and RFalse, generating a result into RDest. Return
1688/// true if the fold cannot be performed.
1689///
1690void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
1691 unsigned RTrue, unsigned RFalse, unsigned RDest) {
1692 enum Condition {
1693 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
1694 NOT_SET
1695 } CondCode = NOT_SET;
1696
1697 static const unsigned CMOVTAB16[] = {
1698 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
1699 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001700 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001701 };
1702 static const unsigned CMOVTAB32[] = {
1703 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
1704 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001705 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001706 };
1707 static const unsigned CMOVTABFP[] = {
1708 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
1709 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
1710 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
1711 };
Nate Begeman16b04f32005-07-15 00:38:55 +00001712 static const int SSE_CMOVTAB[] = {
Nate Begemanf63be7d2005-07-06 18:59:04 +00001713 0 /* CMPEQSS */, 4 /* CMPNEQSS */, 1 /* CMPLTSS */, 2 /* CMPLESS */,
Nate Begeman16b04f32005-07-15 00:38:55 +00001714 1 /* CMPLTSS */, 2 /* CMPLESS */, /*missing*/0, /*missing*/0,
Nate Begemanf63be7d2005-07-06 18:59:04 +00001715 /*missing*/0, /*missing*/0, /*missing*/0, /*missing*/0
1716 };
Chris Lattner24aad1b2005-01-10 22:10:13 +00001717
1718 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
1719 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1720 switch (SetCC->getCondition()) {
1721 default: assert(0 && "Unknown integer comparison!");
1722 case ISD::SETEQ: CondCode = EQ; break;
1723 case ISD::SETGT: CondCode = GT; break;
1724 case ISD::SETGE: CondCode = GE; break;
1725 case ISD::SETLT: CondCode = LT; break;
1726 case ISD::SETLE: CondCode = LE; break;
1727 case ISD::SETNE: CondCode = NE; break;
1728 case ISD::SETULT: CondCode = B; break;
1729 case ISD::SETUGT: CondCode = A; break;
1730 case ISD::SETULE: CondCode = BE; break;
1731 case ISD::SETUGE: CondCode = AE; break;
1732 }
Nate Begemanf63be7d2005-07-06 18:59:04 +00001733 } else if (X86ScalarSSE) {
1734 switch (SetCC->getCondition()) {
1735 default: assert(0 && "Unknown scalar fp comparison!");
1736 case ISD::SETEQ: CondCode = EQ; break;
1737 case ISD::SETNE: CondCode = NE; break;
1738 case ISD::SETULT:
1739 case ISD::SETLT: CondCode = LT; break;
1740 case ISD::SETULE:
1741 case ISD::SETLE: CondCode = LE; break;
1742 case ISD::SETUGT:
1743 case ISD::SETGT: CondCode = GT; break;
1744 case ISD::SETUGE:
1745 case ISD::SETGE: CondCode = GE; break;
1746 }
Chris Lattner24aad1b2005-01-10 22:10:13 +00001747 } else {
1748 // On a floating point condition, the flags are set as follows:
1749 // ZF PF CF op
1750 // 0 | 0 | 0 | X > Y
1751 // 0 | 0 | 1 | X < Y
1752 // 1 | 0 | 0 | X == Y
1753 // 1 | 1 | 1 | unordered
1754 //
1755 switch (SetCC->getCondition()) {
1756 default: assert(0 && "Unknown FP comparison!");
1757 case ISD::SETUEQ:
1758 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
1759 case ISD::SETOGT:
1760 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
1761 case ISD::SETOGE:
1762 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
1763 case ISD::SETULT:
1764 case ISD::SETLT: CondCode = B; break; // True if CF = 1
1765 case ISD::SETULE:
1766 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
1767 case ISD::SETONE:
1768 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
1769 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1770 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1771 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1772 case ISD::SETUGE: // PF = 1 | CF = 0
1773 case ISD::SETUNE: // PF = 1 | ZF = 0
1774 case ISD::SETOEQ: // PF = 0 & ZF = 1
1775 case ISD::SETOLT: // PF = 0 & CF = 1
1776 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1777 // We cannot emit this comparison as a single cmov.
1778 break;
1779 }
1780 }
1781 }
1782
Nate Begemanf63be7d2005-07-06 18:59:04 +00001783 // There's no SSE equivalent of FCMOVE. In some cases we can fake it up, in
1784 // Others we will have to do the PowerPC thing and generate an MBB for the
1785 // true and false values and select between them with a PHI.
Nate Begeman16b04f32005-07-15 00:38:55 +00001786 if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) {
1787 if (0 && CondCode != NOT_SET) {
1788 // FIXME: check for min and max
Nate Begemanf63be7d2005-07-06 18:59:04 +00001789 } else {
Nate Begeman16b04f32005-07-15 00:38:55 +00001790 // FIXME: emit a direct compare and branch rather than setting a cond reg
1791 // and testing it.
Nate Begemanf63be7d2005-07-06 18:59:04 +00001792 unsigned CondReg = SelectExpr(Cond);
1793 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1794
1795 // Create an iterator with which to insert the MBB for copying the false
1796 // value and the MBB to hold the PHI instruction for this SetCC.
1797 MachineBasicBlock *thisMBB = BB;
1798 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1799 ilist<MachineBasicBlock>::iterator It = BB;
1800 ++It;
1801
1802 // thisMBB:
1803 // ...
1804 // TrueVal = ...
1805 // cmpTY ccX, r1, r2
1806 // bCC sinkMBB
1807 // fallthrough --> copy0MBB
1808 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1809 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1810 BuildMI(BB, X86::JNE, 1).addMBB(sinkMBB);
1811 MachineFunction *F = BB->getParent();
1812 F->getBasicBlockList().insert(It, copy0MBB);
1813 F->getBasicBlockList().insert(It, sinkMBB);
1814 // Update machine-CFG edges
1815 BB->addSuccessor(copy0MBB);
1816 BB->addSuccessor(sinkMBB);
1817
1818 // copy0MBB:
1819 // %FalseValue = ...
1820 // # fallthrough to sinkMBB
1821 BB = copy0MBB;
1822 // Update machine-CFG edges
1823 BB->addSuccessor(sinkMBB);
1824
1825 // sinkMBB:
1826 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1827 // ...
1828 BB = sinkMBB;
1829 BuildMI(BB, X86::PHI, 4, RDest).addReg(RFalse)
1830 .addMBB(copy0MBB).addReg(RTrue).addMBB(thisMBB);
1831 }
1832 return;
1833 }
1834
Chris Lattner24aad1b2005-01-10 22:10:13 +00001835 unsigned Opc = 0;
1836 if (CondCode != NOT_SET) {
1837 switch (SVT) {
1838 default: assert(0 && "Cannot select this type!");
1839 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1840 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001841 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001842 }
1843 }
Nate Begemanf63be7d2005-07-06 18:59:04 +00001844
Chris Lattner24aad1b2005-01-10 22:10:13 +00001845 // Finally, if we weren't able to fold this, just emit the condition and test
1846 // it.
1847 if (CondCode == NOT_SET || Opc == 0) {
1848 // Get the condition into the zero flag.
1849 unsigned CondReg = SelectExpr(Cond);
1850 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1851
1852 switch (SVT) {
1853 default: assert(0 && "Cannot select this type!");
1854 case MVT::i16: Opc = X86::CMOVE16rr; break;
1855 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001856 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001857 }
1858 } else {
1859 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001860 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001861 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001862 }
1863 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1864}
1865
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001866void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001867 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001868 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1869 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001870 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001871 switch (RHS.getValueType()) {
1872 default: break;
1873 case MVT::i1:
1874 case MVT::i8: Opc = X86::CMP8mi; break;
1875 case MVT::i16: Opc = X86::CMP16mi; break;
1876 case MVT::i32: Opc = X86::CMP32mi; break;
1877 }
1878 if (Opc) {
1879 X86AddressMode AM;
1880 EmitFoldedLoad(LHS, AM);
1881 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1882 return;
1883 }
1884 }
1885
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001886 switch (RHS.getValueType()) {
1887 default: break;
1888 case MVT::i1:
1889 case MVT::i8: Opc = X86::CMP8ri; break;
1890 case MVT::i16: Opc = X86::CMP16ri; break;
1891 case MVT::i32: Opc = X86::CMP32ri; break;
1892 }
1893 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001894 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001895 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1896 return;
1897 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001898 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
Nate Begemanf63be7d2005-07-06 18:59:04 +00001899 if (!X86ScalarSSE && (CN->isExactlyValue(+0.0) ||
1900 CN->isExactlyValue(-0.0))) {
Chris Lattner7f2afac2005-01-14 22:37:41 +00001901 unsigned Reg = SelectExpr(LHS);
1902 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1903 BuildMI(BB, X86::FNSTSW8r, 0);
1904 BuildMI(BB, X86::SAHF, 1);
Chris Lattner7805fa42005-03-17 16:29:26 +00001905 return;
Chris Lattner7f2afac2005-01-14 22:37:41 +00001906 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001907 }
1908
Chris Lattneref6806c2005-01-12 02:02:48 +00001909 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001910 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001911 switch (RHS.getValueType()) {
1912 default: break;
1913 case MVT::i1:
1914 case MVT::i8: Opc = X86::CMP8mr; break;
1915 case MVT::i16: Opc = X86::CMP16mr; break;
1916 case MVT::i32: Opc = X86::CMP32mr; break;
1917 }
1918 if (Opc) {
1919 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001920 EmitFoldedLoad(LHS, AM);
1921 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001922 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1923 return;
1924 }
1925 }
1926
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001927 switch (LHS.getValueType()) {
1928 default: assert(0 && "Cannot compare this value!");
1929 case MVT::i1:
1930 case MVT::i8: Opc = X86::CMP8rr; break;
1931 case MVT::i16: Opc = X86::CMP16rr; break;
1932 case MVT::i32: Opc = X86::CMP32rr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00001933 case MVT::f32: Opc = X86::UCOMISSrr; break;
1934 case MVT::f64: Opc = X86ScalarSSE ? X86::UCOMISDrr : X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001935 }
Chris Lattner11333092005-01-11 03:11:44 +00001936 unsigned Tmp1, Tmp2;
1937 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1938 Tmp1 = SelectExpr(LHS);
1939 Tmp2 = SelectExpr(RHS);
1940 } else {
1941 Tmp2 = SelectExpr(RHS);
1942 Tmp1 = SelectExpr(LHS);
1943 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001944 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1945}
1946
Chris Lattnera5ade062005-01-11 21:19:59 +00001947/// isFoldableLoad - Return true if this is a load instruction that can safely
1948/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00001949bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1950 if (Op.getOpcode() == ISD::LOAD) {
1951 // FIXME: currently can't fold constant pool indexes.
1952 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1953 return false;
1954 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001955 cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::f32) {
Chris Lattner44129b52005-01-25 20:03:11 +00001956 // FIXME: currently can't fold constant pool indexes.
1957 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1958 return false;
1959 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001960 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00001961 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001962
1963 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001964 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1965 if (ExprMap.count(Op.getValue(1))) return false;
1966 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001967 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001968
Chris Lattner4ff348b2005-01-17 06:26:58 +00001969 // If there is not just one use of its value, we cannot fold.
1970 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1971
1972 // Finally, we cannot fold the load into the operation if this would induce a
1973 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1974 // operand of the operation we are folding the load into) can possible use the
1975 // chain node defined by the load.
1976 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1977 std::set<SDNode*> Visited;
1978 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1979 return false;
1980 }
1981 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001982}
1983
Chris Lattner4ff348b2005-01-17 06:26:58 +00001984
Chris Lattnera5ade062005-01-11 21:19:59 +00001985/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1986/// and compute the address being loaded into AM.
1987void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1988 SDOperand Chain = Op.getOperand(0);
1989 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001990
Chris Lattnera5ade062005-01-11 21:19:59 +00001991 if (getRegPressure(Chain) > getRegPressure(Address)) {
1992 Select(Chain);
1993 SelectAddress(Address, AM);
1994 } else {
1995 SelectAddress(Address, AM);
1996 Select(Chain);
1997 }
1998
1999 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00002000 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
2001 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00002002 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00002003 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002004}
2005
Chris Lattner30ea1e92005-01-19 07:37:26 +00002006// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
2007// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
2008// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
2009// return true.
2010bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00002011 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
2012 // good!
2013 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
2014 std::swap(Op1, Op2); // Op1 is the SHL now.
2015 } else {
2016 return false; // No match
2017 }
2018
2019 SDOperand ShlVal = Op1.getOperand(0);
2020 SDOperand ShlAmt = Op1.getOperand(1);
2021 SDOperand ShrVal = Op2.getOperand(0);
2022 SDOperand ShrAmt = Op2.getOperand(1);
2023
Chris Lattner30ea1e92005-01-19 07:37:26 +00002024 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
2025
Chris Lattner85716372005-01-19 06:18:43 +00002026 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
2027 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
2028 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00002029 if (SubCST->getValue() == RegSize) {
2030 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00002031 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00002032 if (ShrVal == ShlVal) {
2033 unsigned Reg, ShAmt;
2034 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
2035 Reg = SelectExpr(ShrVal);
2036 ShAmt = SelectExpr(ShrAmt);
2037 } else {
2038 ShAmt = SelectExpr(ShrAmt);
2039 Reg = SelectExpr(ShrVal);
2040 }
2041 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2042 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
2043 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
2044 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
2045 return true;
2046 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00002047 unsigned AReg, BReg;
2048 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00002049 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002050 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00002051 } else {
Chris Lattner85716372005-01-19 06:18:43 +00002052 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002053 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00002054 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00002055 unsigned ShAmt = SelectExpr(ShrAmt);
2056 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2057 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
2058 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00002059 return true;
2060 }
2061 }
2062
Chris Lattner4053b1e2005-01-19 08:07:05 +00002063 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
2064 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
2065 if (SubCST->getValue() == RegSize) {
2066 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
2067 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
2068 if (ShrVal == ShlVal) {
2069 unsigned Reg, ShAmt;
2070 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
2071 Reg = SelectExpr(ShrVal);
2072 ShAmt = SelectExpr(ShlAmt);
2073 } else {
2074 ShAmt = SelectExpr(ShlAmt);
2075 Reg = SelectExpr(ShrVal);
2076 }
2077 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2078 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
2079 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
2080 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
2081 return true;
2082 } else if (RegSize != 8) {
2083 unsigned AReg, BReg;
2084 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002085 AReg = SelectExpr(ShlVal);
2086 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002087 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002088 BReg = SelectExpr(ShrVal);
2089 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002090 }
2091 unsigned ShAmt = SelectExpr(ShlAmt);
2092 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2093 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
2094 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
2095 return true;
2096 }
2097 }
Chris Lattner85716372005-01-19 06:18:43 +00002098
Chris Lattner4053b1e2005-01-19 08:07:05 +00002099 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
2100 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
2101 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
2102 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
2103 // (A >> 5) | (A << 27) --> ROR A, 5
2104 // (A >> 5) | (B << 27) --> SHRD A, B, 5
2105 if (ShrVal == ShlVal) {
2106 unsigned Reg = SelectExpr(ShrVal);
2107 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
2108 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
2109 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
2110 return true;
2111 } else if (RegSize != 8) {
2112 unsigned AReg, BReg;
2113 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00002114 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002115 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002116 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00002117 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002118 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002119 }
2120 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
2121 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
2122 .addImm(ShrCst->getValue());
2123 return true;
2124 }
2125 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002126
Chris Lattner85716372005-01-19 06:18:43 +00002127 return false;
2128}
2129
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002130unsigned ISel::SelectExpr(SDOperand N) {
2131 unsigned Result;
2132 unsigned Tmp1, Tmp2, Tmp3;
2133 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00002134 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00002135 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00002136
Chris Lattner7f2afac2005-01-14 22:37:41 +00002137 if (Node->getOpcode() == ISD::CopyFromReg) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00002138 if (MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()) ||
2139 cast<RegSDNode>(Node)->getReg() == X86::ESP) {
2140 // Just use the specified register as our input.
2141 return cast<RegSDNode>(Node)->getReg();
2142 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00002143 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002144
Chris Lattnera5ade062005-01-11 21:19:59 +00002145 unsigned &Reg = ExprMap[N];
2146 if (Reg) return Reg;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002147
Chris Lattnerb38a7492005-04-02 04:01:14 +00002148 switch (N.getOpcode()) {
2149 default:
Chris Lattnera5ade062005-01-11 21:19:59 +00002150 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnerb38a7492005-04-02 04:01:14 +00002151 MakeReg(N.getValueType()) : 1;
2152 break;
Chris Lattner239738a2005-05-14 08:48:15 +00002153 case X86ISD::TAILCALL:
2154 case X86ISD::CALL:
Chris Lattnera5ade062005-01-11 21:19:59 +00002155 // If this is a call instruction, make sure to prepare ALL of the result
2156 // values as well as the chain.
Chris Lattner239738a2005-05-14 08:48:15 +00002157 ExprMap[N.getValue(0)] = 1;
2158 if (Node->getNumValues() > 1) {
2159 Result = MakeReg(Node->getValueType(1));
2160 ExprMap[N.getValue(1)] = Result;
2161 for (unsigned i = 2, e = Node->getNumValues(); i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00002162 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattner239738a2005-05-14 08:48:15 +00002163 } else {
2164 Result = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002165 }
Chris Lattnerb38a7492005-04-02 04:01:14 +00002166 break;
2167 case ISD::ADD_PARTS:
2168 case ISD::SUB_PARTS:
2169 case ISD::SHL_PARTS:
2170 case ISD::SRL_PARTS:
2171 case ISD::SRA_PARTS:
2172 Result = MakeReg(Node->getValueType(0));
2173 ExprMap[N.getValue(0)] = Result;
2174 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
2175 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
2176 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002177 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002178
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002179 switch (N.getOpcode()) {
2180 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00002181 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002182 assert(0 && "Node not handled!\n");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002183 case ISD::FP_EXTEND:
2184 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
2185 Tmp1 = SelectExpr(N.getOperand(0));
2186 BuildMI(BB, X86::CVTSS2SDrr, 1, Result).addReg(Tmp1);
2187 return Result;
Nate Begeman16b04f32005-07-15 00:38:55 +00002188 case ISD::FP_ROUND:
2189 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
2190 Tmp1 = SelectExpr(N.getOperand(0));
2191 BuildMI(BB, X86::CVTSD2SSrr, 1, Result).addReg(Tmp1);
2192 return Result;
Chris Lattnerc6f41812005-05-12 23:06:28 +00002193 case ISD::CopyFromReg:
2194 Select(N.getOperand(0));
2195 if (Result == 1) {
2196 Reg = Result = ExprMap[N.getValue(0)] =
2197 MakeReg(N.getValue(0).getValueType());
2198 }
2199 switch (Node->getValueType(0)) {
2200 default: assert(0 && "Cannot CopyFromReg this!");
2201 case MVT::i1:
2202 case MVT::i8:
2203 BuildMI(BB, X86::MOV8rr, 1,
2204 Result).addReg(cast<RegSDNode>(Node)->getReg());
2205 return Result;
2206 case MVT::i16:
2207 BuildMI(BB, X86::MOV16rr, 1,
2208 Result).addReg(cast<RegSDNode>(Node)->getReg());
2209 return Result;
2210 case MVT::i32:
2211 BuildMI(BB, X86::MOV32rr, 1,
2212 Result).addReg(cast<RegSDNode>(Node)->getReg());
2213 return Result;
2214 }
2215
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002216 case ISD::FrameIndex:
2217 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
2218 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
2219 return Result;
2220 case ISD::ConstantPool:
2221 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
2222 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
2223 return Result;
2224 case ISD::ConstantFP:
2225 ContainsFPCode = true;
2226 Tmp1 = Result; // Intermediate Register
2227 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
2228 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
2229 Tmp1 = MakeReg(MVT::f64);
2230
2231 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
2232 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
2233 BuildMI(BB, X86::FLD0, 0, Tmp1);
2234 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
2235 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
2236 BuildMI(BB, X86::FLD1, 0, Tmp1);
2237 else
2238 assert(0 && "Unexpected constant!");
2239 if (Tmp1 != Result)
2240 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
2241 return Result;
2242 case ISD::Constant:
2243 switch (N.getValueType()) {
2244 default: assert(0 && "Cannot use constants of this type!");
2245 case MVT::i1:
2246 case MVT::i8: Opc = X86::MOV8ri; break;
2247 case MVT::i16: Opc = X86::MOV16ri; break;
2248 case MVT::i32: Opc = X86::MOV32ri; break;
2249 }
2250 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
2251 return Result;
Chris Lattner7ce7eff2005-04-01 22:46:45 +00002252 case ISD::UNDEF:
2253 if (Node->getValueType(0) == MVT::f64) {
2254 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
2255 BuildMI(BB, X86::FLD0, 0, Result);
2256 } else {
2257 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
2258 }
2259 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002260 case ISD::GlobalAddress: {
2261 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanfb5792f2005-07-12 01:41:54 +00002262 // For Darwin, external and weak symbols are indirect, so we want to load
2263 // the value at address GV, not the value of GV itself.
2264 if (Subtarget->getIndirectExternAndWeakGlobals() &&
2265 (GV->hasWeakLinkage() || GV->isExternal())) {
2266 BuildMI(BB, X86::MOV32rm, 4, Result).addReg(0).addZImm(1).addReg(0)
2267 .addGlobalAddress(GV, false, 0);
2268 } else {
2269 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
2270 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002271 return Result;
2272 }
2273 case ISD::ExternalSymbol: {
2274 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
2275 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
2276 return Result;
2277 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002278 case ISD::ZERO_EXTEND: {
2279 int DestIs16 = N.getValueType() == MVT::i16;
2280 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00002281
2282 // FIXME: This hack is here for zero extension casts from bool to i8. This
2283 // would not be needed if bools were promoted by Legalize.
2284 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002285 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00002286 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
2287 return Result;
2288 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002289
Chris Lattner4ff348b2005-01-17 06:26:58 +00002290 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002291 static const unsigned Opc[3] = {
2292 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
2293 };
2294
2295 X86AddressMode AM;
2296 EmitFoldedLoad(N.getOperand(0), AM);
2297 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002298
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002299 return Result;
2300 }
2301
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002302 static const unsigned Opc[3] = {
2303 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
2304 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002305 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002306 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
2307 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002308 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002309 case ISD::SIGN_EXTEND: {
2310 int DestIs16 = N.getValueType() == MVT::i16;
2311 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
2312
Chris Lattner590d8002005-01-09 18:52:44 +00002313 // FIXME: Legalize should promote bools to i8!
2314 assert(N.getOperand(0).getValueType() != MVT::i1 &&
2315 "Sign extend from bool not implemented!");
2316
Chris Lattner4ff348b2005-01-17 06:26:58 +00002317 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002318 static const unsigned Opc[3] = {
2319 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
2320 };
2321
2322 X86AddressMode AM;
2323 EmitFoldedLoad(N.getOperand(0), AM);
2324 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
2325 return Result;
2326 }
2327
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002328 static const unsigned Opc[3] = {
2329 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
2330 };
2331 Tmp1 = SelectExpr(N.getOperand(0));
2332 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
2333 return Result;
2334 }
2335 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00002336 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00002337 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00002338 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00002339 switch (N.getValueType()) {
2340 default: assert(0 && "Unknown truncate!");
2341 case MVT::i1:
2342 case MVT::i8: Opc = X86::MOV8rm; break;
2343 case MVT::i16: Opc = X86::MOV16rm; break;
2344 }
2345 X86AddressMode AM;
2346 EmitFoldedLoad(N.getOperand(0), AM);
2347 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2348 return Result;
2349 }
2350
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002351 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
2352 // a move out of AX or AL.
2353 switch (N.getOperand(0).getValueType()) {
2354 default: assert(0 && "Unknown truncate!");
2355 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
2356 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
2357 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
2358 }
2359 Tmp1 = SelectExpr(N.getOperand(0));
2360 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2361
2362 switch (N.getValueType()) {
2363 default: assert(0 && "Unknown truncate!");
2364 case MVT::i1:
2365 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
2366 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
2367 }
2368 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
2369 return Result;
2370
Chris Lattnera28381c2005-07-16 00:28:20 +00002371 case ISD::SINT_TO_FP: {
Nate Begemanf63be7d2005-07-06 18:59:04 +00002372 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2373 unsigned PromoteOpcode = 0;
2374
Nate Begeman5a8441e2005-07-16 02:02:34 +00002375 // We can handle any sint to fp with the direct sse conversion instructions.
Nate Begemanf63be7d2005-07-06 18:59:04 +00002376 if (X86ScalarSSE) {
Nate Begeman5a8441e2005-07-16 02:02:34 +00002377 Opc = (N.getValueType() == MVT::f64) ? X86::CVTSI2SDrr : X86::CVTSI2SSrr;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002378 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2379 return Result;
2380 }
2381
Chris Lattneref7ba072005-01-11 03:50:45 +00002382 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00002383
Chris Lattner590d8002005-01-09 18:52:44 +00002384 // Spill the integer to memory and reload it from there.
Nate Begeman5a8441e2005-07-16 02:02:34 +00002385 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
Chris Lattner590d8002005-01-09 18:52:44 +00002386 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
2387 MachineFunction *F = BB->getParent();
2388 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2389
2390 switch (SrcTy) {
Chris Lattner590d8002005-01-09 18:52:44 +00002391 case MVT::i32:
Chris Lattnera28381c2005-07-16 00:28:20 +00002392 addFrameReference(BuildMI(BB, X86::MOV32mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00002393 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
2394 break;
2395 case MVT::i16:
Chris Lattnera28381c2005-07-16 00:28:20 +00002396 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00002397 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
2398 break;
2399 default: break; // No promotion required.
2400 }
Chris Lattnera28381c2005-07-16 00:28:20 +00002401 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00002402 }
2403 case ISD::FP_TO_SINT:
2404 case ISD::FP_TO_UINT: {
2405 // FIXME: Most of this grunt work should be done by legalize!
2406 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2407
Nate Begemanf63be7d2005-07-06 18:59:04 +00002408 // If the target supports SSE2 and is performing FP operations in SSE regs
2409 // instead of the FP stack, then we can use the efficient CVTSS2SI and
2410 // CVTSD2SI instructions.
2411 if (ISD::FP_TO_SINT == N.getOpcode() && X86ScalarSSE) {
2412 if (MVT::f32 == N.getOperand(0).getValueType()) {
Nate Begeman16b04f32005-07-15 00:38:55 +00002413 BuildMI(BB, X86::CVTTSS2SIrr, 1, Result).addReg(Tmp1);
Nate Begemanf63be7d2005-07-06 18:59:04 +00002414 } else if (MVT::f64 == N.getOperand(0).getValueType()) {
Nate Begeman16b04f32005-07-15 00:38:55 +00002415 BuildMI(BB, X86::CVTTSD2SIrr, 1, Result).addReg(Tmp1);
Nate Begemanf63be7d2005-07-06 18:59:04 +00002416 } else {
2417 assert(0 && "Not an f32 or f64?");
2418 abort();
2419 }
2420 return Result;
2421 }
2422
Chris Lattner590d8002005-01-09 18:52:44 +00002423 // Change the floating point control register to use "round towards zero"
2424 // mode when truncating to an integer value.
2425 //
2426 MachineFunction *F = BB->getParent();
2427 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
2428 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
2429
2430 // Load the old value of the high byte of the control word...
2431 unsigned HighPartOfCW = MakeReg(MVT::i8);
2432 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
2433 CWFrameIdx, 1);
2434
2435 // Set the high part to be round to zero...
2436 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
2437 CWFrameIdx, 1).addImm(12);
2438
2439 // Reload the modified control word now...
2440 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002441
Chris Lattner590d8002005-01-09 18:52:44 +00002442 // Restore the memory image of control word to original value
2443 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
2444 CWFrameIdx, 1).addReg(HighPartOfCW);
2445
2446 // We don't have the facilities for directly storing byte sized data to
2447 // memory. Promote it to 16 bits. We also must promote unsigned values to
2448 // larger classes because we only have signed FP stores.
2449 MVT::ValueType StoreClass = Node->getValueType(0);
2450 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
2451 switch (StoreClass) {
Chris Lattner2afa1912005-05-09 05:33:18 +00002452 case MVT::i1:
Chris Lattner590d8002005-01-09 18:52:44 +00002453 case MVT::i8: StoreClass = MVT::i16; break;
2454 case MVT::i16: StoreClass = MVT::i32; break;
2455 case MVT::i32: StoreClass = MVT::i64; break;
Chris Lattner590d8002005-01-09 18:52:44 +00002456 default: assert(0 && "Unknown store class!");
2457 }
2458
2459 // Spill the integer to memory and reload it from there.
2460 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
2461 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2462
2463 switch (StoreClass) {
2464 default: assert(0 && "Unknown store class!");
2465 case MVT::i16:
2466 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
2467 break;
2468 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00002469 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00002470 break;
Chris Lattnera0dbf182005-05-09 18:37:02 +00002471 case MVT::i64:
2472 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
2473 break; }
Chris Lattner590d8002005-01-09 18:52:44 +00002474
2475 switch (Node->getValueType(0)) {
2476 default:
2477 assert(0 && "Unknown integer type!");
Chris Lattner590d8002005-01-09 18:52:44 +00002478 case MVT::i32:
2479 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
2480 break;
2481 case MVT::i16:
2482 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
2483 break;
2484 case MVT::i8:
Chris Lattner2afa1912005-05-09 05:33:18 +00002485 case MVT::i1:
Chris Lattner590d8002005-01-09 18:52:44 +00002486 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
2487 break;
2488 }
2489
2490 // Reload the original control word now.
2491 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
2492 return Result;
2493 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002494 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00002495 Op0 = N.getOperand(0);
2496 Op1 = N.getOperand(1);
2497
Chris Lattner44129b52005-01-25 20:03:11 +00002498 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002499 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002500 goto FoldAdd;
2501 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002502
Chris Lattner44129b52005-01-25 20:03:11 +00002503 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002504 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00002505 switch (N.getValueType()) {
2506 default: assert(0 && "Cannot add this type!");
2507 case MVT::i1:
2508 case MVT::i8: Opc = X86::ADD8rm; break;
2509 case MVT::i16: Opc = X86::ADD16rm; break;
2510 case MVT::i32: Opc = X86::ADD32rm; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002511 case MVT::f32: Opc = X86::ADDSSrm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002512 case MVT::f64:
2513 // For F64, handle promoted load operations (from F32) as well!
Nate Begemanf63be7d2005-07-06 18:59:04 +00002514 if (X86ScalarSSE) {
2515 assert(Op1.getOpcode() == ISD::LOAD && "SSE load not promoted");
2516 Opc = X86::ADDSDrm;
2517 } else {
2518 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
2519 }
Chris Lattner44129b52005-01-25 20:03:11 +00002520 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002521 }
2522 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002523 EmitFoldedLoad(Op1, AM);
2524 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002525 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2526 return Result;
2527 }
2528
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002529 // See if we can codegen this as an LEA to fold operations together.
2530 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00002531 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002532 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00002533 MatchAddress(N, AM);
2534 ExprMap[N] = Result;
2535
2536 // If this is not just an add, emit the LEA. For a simple add (like
2537 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
2538 // leave this as LEA, then peephole it to 'ADD' after two address elim
2539 // happens.
2540 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
2541 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
2542 X86AddressMode XAM = SelectAddrExprs(AM);
2543 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
2544 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002545 }
2546 }
Chris Lattner11333092005-01-11 03:11:44 +00002547
Chris Lattnera5ade062005-01-11 21:19:59 +00002548 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002549 Opc = 0;
2550 if (CN->getValue() == 1) { // add X, 1 -> inc X
2551 switch (N.getValueType()) {
2552 default: assert(0 && "Cannot integer add this type!");
2553 case MVT::i8: Opc = X86::INC8r; break;
2554 case MVT::i16: Opc = X86::INC16r; break;
2555 case MVT::i32: Opc = X86::INC32r; break;
2556 }
2557 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
2558 switch (N.getValueType()) {
2559 default: assert(0 && "Cannot integer add this type!");
2560 case MVT::i8: Opc = X86::DEC8r; break;
2561 case MVT::i16: Opc = X86::DEC16r; break;
2562 case MVT::i32: Opc = X86::DEC32r; break;
2563 }
2564 }
2565
2566 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002567 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002568 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2569 return Result;
2570 }
2571
2572 switch (N.getValueType()) {
2573 default: assert(0 && "Cannot add this type!");
2574 case MVT::i8: Opc = X86::ADD8ri; break;
2575 case MVT::i16: Opc = X86::ADD16ri; break;
2576 case MVT::i32: Opc = X86::ADD32ri; break;
2577 }
2578 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002579 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002580 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2581 return Result;
2582 }
2583 }
2584
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002585 switch (N.getValueType()) {
2586 default: assert(0 && "Cannot add this type!");
2587 case MVT::i8: Opc = X86::ADD8rr; break;
2588 case MVT::i16: Opc = X86::ADD16rr; break;
2589 case MVT::i32: Opc = X86::ADD32rr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002590 case MVT::f32: Opc = X86::ADDSSrr; break;
2591 case MVT::f64: Opc = X86ScalarSSE ? X86::ADDSDrr : X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002592 }
Chris Lattner11333092005-01-11 03:11:44 +00002593
Chris Lattnera5ade062005-01-11 21:19:59 +00002594 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2595 Tmp1 = SelectExpr(Op0);
2596 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00002597 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00002598 Tmp2 = SelectExpr(Op1);
2599 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00002600 }
2601
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002602 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2603 return Result;
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002604
Nate Begemanf63be7d2005-07-06 18:59:04 +00002605 case ISD::FSQRT:
2606 Tmp1 = SelectExpr(Node->getOperand(0));
2607 if (X86ScalarSSE) {
2608 Opc = (N.getValueType() == MVT::f32) ? X86::SQRTSSrr : X86::SQRTSDrr;
2609 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2610 } else {
2611 BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1);
2612 }
2613 return Result;
2614
2615 // FIXME:
2616 // Once we can spill 16 byte constants into the constant pool, we can
2617 // implement SSE equivalents of FABS and FCHS.
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002618 case ISD::FABS:
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002619 case ISD::FNEG:
Chris Lattnerc5dcb532005-04-30 04:25:35 +00002620 case ISD::FSIN:
2621 case ISD::FCOS:
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002622 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002623 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002624 switch (N.getOpcode()) {
2625 default: assert(0 && "Unreachable!");
2626 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
2627 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
Chris Lattnerc5dcb532005-04-30 04:25:35 +00002628 case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break;
2629 case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002630 }
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002631 return Result;
2632
Chris Lattner8db0af12005-04-06 04:21:07 +00002633 case ISD::MULHU:
2634 switch (N.getValueType()) {
2635 default: assert(0 && "Unsupported VT!");
2636 case MVT::i8: Tmp2 = X86::MUL8r; break;
2637 case MVT::i16: Tmp2 = X86::MUL16r; break;
2638 case MVT::i32: Tmp2 = X86::MUL32r; break;
2639 }
2640 // FALL THROUGH
2641 case ISD::MULHS: {
2642 unsigned MovOpc, LowReg, HiReg;
2643 switch (N.getValueType()) {
2644 default: assert(0 && "Unsupported VT!");
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002645 case MVT::i8:
Chris Lattner8db0af12005-04-06 04:21:07 +00002646 MovOpc = X86::MOV8rr;
2647 LowReg = X86::AL;
2648 HiReg = X86::AH;
2649 Opc = X86::IMUL8r;
2650 break;
2651 case MVT::i16:
2652 MovOpc = X86::MOV16rr;
2653 LowReg = X86::AX;
2654 HiReg = X86::DX;
2655 Opc = X86::IMUL16r;
2656 break;
2657 case MVT::i32:
2658 MovOpc = X86::MOV32rr;
2659 LowReg = X86::EAX;
2660 HiReg = X86::EDX;
2661 Opc = X86::IMUL32r;
2662 break;
2663 }
2664 if (Node->getOpcode() != ISD::MULHS)
2665 Opc = Tmp2; // Get the MULHU opcode.
2666
2667 Op0 = Node->getOperand(0);
2668 Op1 = Node->getOperand(1);
2669 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2670 Tmp1 = SelectExpr(Op0);
2671 Tmp2 = SelectExpr(Op1);
2672 } else {
2673 Tmp2 = SelectExpr(Op1);
2674 Tmp1 = SelectExpr(Op0);
2675 }
2676
2677 // FIXME: Implement folding of loads into the memory operands here!
2678 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
2679 BuildMI(BB, Opc, 1).addReg(Tmp2);
2680 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
2681 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002682 }
Chris Lattner8db0af12005-04-06 04:21:07 +00002683
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002684 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00002685 case ISD::MUL:
2686 case ISD::AND:
2687 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00002688 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00002689 static const unsigned SUBTab[] = {
2690 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
2691 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
2692 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
2693 };
Nate Begemanf63be7d2005-07-06 18:59:04 +00002694 static const unsigned SSE_SUBTab[] = {
2695 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
2696 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::SUBSSrm, X86::SUBSDrm,
2697 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::SUBSSrr, X86::SUBSDrr,
2698 };
Chris Lattnera5ade062005-01-11 21:19:59 +00002699 static const unsigned MULTab[] = {
2700 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
2701 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
2702 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
2703 };
Nate Begemanf63be7d2005-07-06 18:59:04 +00002704 static const unsigned SSE_MULTab[] = {
2705 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
2706 0, X86::IMUL16rm , X86::IMUL32rm, X86::MULSSrm, X86::MULSDrm,
2707 0, X86::IMUL16rr , X86::IMUL32rr, X86::MULSSrr, X86::MULSDrr,
2708 };
Chris Lattnera5ade062005-01-11 21:19:59 +00002709 static const unsigned ANDTab[] = {
2710 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
2711 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002712 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattnera5ade062005-01-11 21:19:59 +00002713 };
2714 static const unsigned ORTab[] = {
2715 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
2716 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
2717 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
2718 };
2719 static const unsigned XORTab[] = {
2720 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
2721 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
2722 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
2723 };
2724
2725 Op0 = Node->getOperand(0);
2726 Op1 = Node->getOperand(1);
2727
Chris Lattner30ea1e92005-01-19 07:37:26 +00002728 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
2729 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00002730 return Result;
2731
2732 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002733 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
2734 if (CN->isNullValue()) { // 0 - N -> neg N
2735 switch (N.getValueType()) {
2736 default: assert(0 && "Cannot sub this type!");
2737 case MVT::i1:
2738 case MVT::i8: Opc = X86::NEG8r; break;
2739 case MVT::i16: Opc = X86::NEG16r; break;
2740 case MVT::i32: Opc = X86::NEG32r; break;
2741 }
2742 Tmp1 = SelectExpr(N.getOperand(1));
2743 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2744 return Result;
2745 }
2746
Chris Lattnera5ade062005-01-11 21:19:59 +00002747 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2748 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00002749 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00002750 switch (N.getValueType()) {
2751 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00002752 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00002753 case MVT::i8: Opc = X86::NOT8r; break;
2754 case MVT::i16: Opc = X86::NOT16r; break;
2755 case MVT::i32: Opc = X86::NOT32r; break;
2756 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00002757 if (Opc) {
2758 Tmp1 = SelectExpr(Op0);
2759 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2760 return Result;
2761 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00002762 }
2763
Chris Lattner2a4e5082005-01-17 06:48:02 +00002764 // Fold common multiplies into LEA instructions.
2765 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
2766 switch ((int)CN->getValue()) {
2767 default: break;
2768 case 3:
2769 case 5:
2770 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00002771 // Remove N from exprmap so SelectAddress doesn't get confused.
2772 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002773 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00002774 SelectAddress(N, AM);
2775 // Restore it to the map.
2776 ExprMap[N] = Result;
2777 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
2778 return Result;
2779 }
2780 }
2781
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002782 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00002783 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002784 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00002785 case MVT::i8: Opc = 0; break;
2786 case MVT::i16: Opc = 1; break;
2787 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002788 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002789 switch (Node->getOpcode()) {
2790 default: assert(0 && "Unreachable!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002791 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2792 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002793 case ISD::AND: Opc = ANDTab[Opc]; break;
2794 case ISD::OR: Opc = ORTab[Opc]; break;
2795 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002796 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002797 if (Opc) { // Can't fold MUL:i8 R, imm
2798 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002799 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2800 return Result;
2801 }
2802 }
Chris Lattner11333092005-01-11 03:11:44 +00002803
Chris Lattner44129b52005-01-25 20:03:11 +00002804 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00002805 if (Node->getOpcode() != ISD::SUB) {
2806 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002807 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00002808 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00002809 // For FP, emit 'reverse' subract, with a memory operand.
Nate Begemanf63be7d2005-07-06 18:59:04 +00002810 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner44129b52005-01-25 20:03:11 +00002811 if (Op0.getOpcode() == ISD::EXTLOAD)
2812 Opc = X86::FSUBR32m;
2813 else
2814 Opc = X86::FSUBR64m;
2815
Chris Lattnera5ade062005-01-11 21:19:59 +00002816 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002817 EmitFoldedLoad(Op0, AM);
2818 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00002819 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2820 return Result;
2821 }
2822 }
2823
Chris Lattner44129b52005-01-25 20:03:11 +00002824 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002825 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00002826 switch (N.getValueType()) {
2827 default: assert(0 && "Cannot operate on this type!");
2828 case MVT::i1:
2829 case MVT::i8: Opc = 5; break;
2830 case MVT::i16: Opc = 6; break;
2831 case MVT::i32: Opc = 7; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002832 case MVT::f32: Opc = 8; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002833 // For F64, handle promoted load operations (from F32) as well!
Nate Begemanf63be7d2005-07-06 18:59:04 +00002834 case MVT::f64:
2835 assert((!X86ScalarSSE || Op1.getOpcode() == ISD::LOAD) &&
2836 "SSE load should have been promoted");
2837 Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002838 }
2839 switch (Node->getOpcode()) {
2840 default: assert(0 && "Unreachable!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002841 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2842 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002843 case ISD::AND: Opc = ANDTab[Opc]; break;
2844 case ISD::OR: Opc = ORTab[Opc]; break;
2845 case ISD::XOR: Opc = XORTab[Opc]; break;
2846 }
2847
2848 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002849 EmitFoldedLoad(Op1, AM);
2850 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002851 if (Opc) {
2852 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2853 } else {
2854 assert(Node->getOpcode() == ISD::MUL &&
2855 N.getValueType() == MVT::i8 && "Unexpected situation!");
2856 // Must use the MUL instruction, which forces use of AL.
2857 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2858 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2859 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2860 }
2861 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00002862 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002863
2864 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2865 Tmp1 = SelectExpr(Op0);
2866 Tmp2 = SelectExpr(Op1);
2867 } else {
2868 Tmp2 = SelectExpr(Op1);
2869 Tmp1 = SelectExpr(Op0);
2870 }
2871
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002872 switch (N.getValueType()) {
2873 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002874 case MVT::i1:
2875 case MVT::i8: Opc = 10; break;
2876 case MVT::i16: Opc = 11; break;
2877 case MVT::i32: Opc = 12; break;
2878 case MVT::f32: Opc = 13; break;
2879 case MVT::f64: Opc = 14; break;
2880 }
2881 switch (Node->getOpcode()) {
2882 default: assert(0 && "Unreachable!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002883 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2884 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002885 case ISD::AND: Opc = ANDTab[Opc]; break;
2886 case ISD::OR: Opc = ORTab[Opc]; break;
2887 case ISD::XOR: Opc = XORTab[Opc]; break;
2888 }
2889 if (Opc) {
2890 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2891 } else {
2892 assert(Node->getOpcode() == ISD::MUL &&
2893 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00002894 // Must use the MUL instruction, which forces use of AL.
2895 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2896 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2897 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002898 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002899 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00002900 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002901 case ISD::ADD_PARTS:
2902 case ISD::SUB_PARTS: {
2903 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2904 "Not an i64 add/sub!");
2905 // Emit all of the operands.
2906 std::vector<unsigned> InVals;
2907 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2908 InVals.push_back(SelectExpr(N.getOperand(i)));
2909 if (N.getOpcode() == ISD::ADD_PARTS) {
2910 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2911 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2912 } else {
2913 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2914 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2915 }
2916 return Result+N.ResNo;
2917 }
2918
Chris Lattnerb38a7492005-04-02 04:01:14 +00002919 case ISD::SHL_PARTS:
2920 case ISD::SRA_PARTS:
2921 case ISD::SRL_PARTS: {
2922 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2923 "Not an i64 shift!");
2924 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2925 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2926 unsigned TmpReg = MakeReg(MVT::i32);
2927 if (N.getOpcode() == ISD::SRA_PARTS) {
2928 // If this is a SHR of a Long, then we need to do funny sign extension
2929 // stuff. TmpReg gets the value to use as the high-part if we are
2930 // shifting more than 32 bits.
2931 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2932 } else {
2933 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2934 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2935 }
2936
2937 // Initialize CL with the shift amount.
2938 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2939 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2940
2941 unsigned TmpReg2 = MakeReg(MVT::i32);
2942 unsigned TmpReg3 = MakeReg(MVT::i32);
2943 if (N.getOpcode() == ISD::SHL_PARTS) {
2944 // TmpReg2 = shld inHi, inLo
2945 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2946 .addReg(ShiftOpLo);
2947 // TmpReg3 = shl inLo, CL
2948 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002949
Chris Lattnerb38a7492005-04-02 04:01:14 +00002950 // Set the flags to indicate whether the shift was by more than 32 bits.
2951 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002952
Chris Lattnerb38a7492005-04-02 04:01:14 +00002953 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002954 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002955 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2956 // DestLo = (>32) ? TmpReg : TmpReg3;
2957 BuildMI(BB, X86::CMOVNE32rr, 2,
2958 Result).addReg(TmpReg3).addReg(TmpReg);
2959 } else {
2960 // TmpReg2 = shrd inLo, inHi
2961 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
2962 .addReg(ShiftOpHi);
2963 // TmpReg3 = s[ah]r inHi, CL
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002964 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnerb38a7492005-04-02 04:01:14 +00002965 : X86::SHR32rCL, 1, TmpReg3)
2966 .addReg(ShiftOpHi);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002967
Chris Lattnerb38a7492005-04-02 04:01:14 +00002968 // Set the flags to indicate whether the shift was by more than 32 bits.
2969 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002970
Chris Lattnerb38a7492005-04-02 04:01:14 +00002971 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002972 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002973 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002974
Chris Lattnerb38a7492005-04-02 04:01:14 +00002975 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002976 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002977 Result+1).addReg(TmpReg3).addReg(TmpReg);
2978 }
2979 return Result+N.ResNo;
2980 }
2981
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002982 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002983 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2984 Tmp2 = SelectExpr(N.getOperand(1));
2985 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002986 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002987 Tmp3 = SelectExpr(N.getOperand(2));
2988 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002989 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002990 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2991 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002992
2993 case ISD::SDIV:
2994 case ISD::UDIV:
2995 case ISD::SREM:
2996 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002997 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2998 "We don't support this operator!");
2999
Chris Lattner5bf26862005-04-13 03:29:53 +00003000 if (N.getOpcode() == ISD::SDIV) {
Chris Lattner3576c842005-01-25 20:35:10 +00003001 // We can fold loads into FpDIVs, but not really into any others.
Nate Begemanb8aa3ac2005-07-07 06:32:01 +00003002 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner3576c842005-01-25 20:35:10 +00003003 // Check for reversed and unreversed DIV.
3004 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
3005 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
3006 Opc = X86::FDIVR32m;
3007 else
3008 Opc = X86::FDIVR64m;
3009 X86AddressMode AM;
3010 EmitFoldedLoad(N.getOperand(0), AM);
3011 Tmp1 = SelectExpr(N.getOperand(1));
3012 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
3013 return Result;
3014 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
3015 N.getOperand(1).getOpcode() == ISD::LOAD) {
3016 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
3017 Opc = X86::FDIV32m;
3018 else
3019 Opc = X86::FDIV64m;
3020 X86AddressMode AM;
3021 EmitFoldedLoad(N.getOperand(1), AM);
3022 Tmp1 = SelectExpr(N.getOperand(0));
3023 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
3024 return Result;
3025 }
3026 }
3027
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003028 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3029 // FIXME: These special cases should be handled by the lowering impl!
3030 unsigned RHS = CN->getValue();
3031 bool isNeg = false;
3032 if ((int)RHS < 0) {
3033 isNeg = true;
3034 RHS = -RHS;
3035 }
3036 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
3037 unsigned Log = log2(RHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003038 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
3039 switch (N.getValueType()) {
3040 default: assert("Unknown type to signed divide!");
3041 case MVT::i8:
3042 SAROpc = X86::SAR8ri;
3043 SHROpc = X86::SHR8ri;
3044 ADDOpc = X86::ADD8rr;
3045 NEGOpc = X86::NEG8r;
3046 break;
3047 case MVT::i16:
3048 SAROpc = X86::SAR16ri;
3049 SHROpc = X86::SHR16ri;
3050 ADDOpc = X86::ADD16rr;
3051 NEGOpc = X86::NEG16r;
3052 break;
3053 case MVT::i32:
3054 SAROpc = X86::SAR32ri;
3055 SHROpc = X86::SHR32ri;
3056 ADDOpc = X86::ADD32rr;
3057 NEGOpc = X86::NEG32r;
3058 break;
3059 }
Chris Lattnera96e5772005-05-13 21:48:20 +00003060 unsigned RegSize = MVT::getSizeInBits(N.getValueType());
Chris Lattner11333092005-01-11 03:11:44 +00003061 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerca96c822005-05-13 21:50:27 +00003062 unsigned TmpReg;
3063 if (Log != 1) {
3064 TmpReg = MakeReg(N.getValueType());
3065 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
3066 } else {
3067 TmpReg = Tmp1;
3068 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003069 unsigned TmpReg2 = MakeReg(N.getValueType());
Chris Lattnera96e5772005-05-13 21:48:20 +00003070 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(RegSize-Log);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003071 unsigned TmpReg3 = MakeReg(N.getValueType());
3072 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003073
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003074 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
3075 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
3076 if (isNeg)
3077 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
3078 return Result;
3079 }
3080 }
Chris Lattner5bf26862005-04-13 03:29:53 +00003081 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003082
Chris Lattner11333092005-01-11 03:11:44 +00003083 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3084 Tmp1 = SelectExpr(N.getOperand(0));
3085 Tmp2 = SelectExpr(N.getOperand(1));
3086 } else {
3087 Tmp2 = SelectExpr(N.getOperand(1));
3088 Tmp1 = SelectExpr(N.getOperand(0));
3089 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003090
3091 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
3092 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
3093 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
3094 switch (N.getValueType()) {
3095 default: assert(0 && "Cannot sdiv this type!");
3096 case MVT::i8:
3097 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
3098 LoReg = X86::AL;
3099 HiReg = X86::AH;
3100 MovOpcode = X86::MOV8rr;
3101 ClrOpcode = X86::MOV8ri;
3102 SExtOpcode = X86::CBW;
3103 break;
3104 case MVT::i16:
3105 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
3106 LoReg = X86::AX;
3107 HiReg = X86::DX;
3108 MovOpcode = X86::MOV16rr;
3109 ClrOpcode = X86::MOV16ri;
3110 SExtOpcode = X86::CWD;
3111 break;
3112 case MVT::i32:
3113 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00003114 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003115 HiReg = X86::EDX;
3116 MovOpcode = X86::MOV32rr;
3117 ClrOpcode = X86::MOV32ri;
3118 SExtOpcode = X86::CDQ;
3119 break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00003120 case MVT::f32:
3121 BuildMI(BB, X86::DIVSSrr, 2, Result).addReg(Tmp1).addReg(Tmp2);
3122 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003123 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00003124 Opc = X86ScalarSSE ? X86::DIVSDrr : X86::FpDIV;
3125 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003126 return Result;
3127 }
3128
3129 // Set up the low part.
3130 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
3131
3132 if (isSigned) {
3133 // Sign extend the low part into the high part.
3134 BuildMI(BB, SExtOpcode, 0);
3135 } else {
3136 // Zero out the high part, effectively zero extending the input.
3137 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
3138 }
3139
3140 // Emit the DIV/IDIV instruction.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003141 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003142
3143 // Get the result of the divide or rem.
3144 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
3145 return Result;
3146 }
3147
3148 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003149 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00003150 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
3151 switch (N.getValueType()) {
3152 default: assert(0 && "Cannot shift this type!");
3153 case MVT::i8: Opc = X86::ADD8rr; break;
3154 case MVT::i16: Opc = X86::ADD16rr; break;
3155 case MVT::i32: Opc = X86::ADD32rr; break;
3156 }
3157 Tmp1 = SelectExpr(N.getOperand(0));
3158 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
3159 return Result;
3160 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003161
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003162 switch (N.getValueType()) {
3163 default: assert(0 && "Cannot shift this type!");
3164 case MVT::i8: Opc = X86::SHL8ri; break;
3165 case MVT::i16: Opc = X86::SHL16ri; break;
3166 case MVT::i32: Opc = X86::SHL32ri; break;
3167 }
Chris Lattner11333092005-01-11 03:11:44 +00003168 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003169 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3170 return Result;
3171 }
Chris Lattner11333092005-01-11 03:11:44 +00003172
3173 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3174 Tmp1 = SelectExpr(N.getOperand(0));
3175 Tmp2 = SelectExpr(N.getOperand(1));
3176 } else {
3177 Tmp2 = SelectExpr(N.getOperand(1));
3178 Tmp1 = SelectExpr(N.getOperand(0));
3179 }
3180
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003181 switch (N.getValueType()) {
3182 default: assert(0 && "Cannot shift this type!");
3183 case MVT::i8 : Opc = X86::SHL8rCL; break;
3184 case MVT::i16: Opc = X86::SHL16rCL; break;
3185 case MVT::i32: Opc = X86::SHL32rCL; break;
3186 }
3187 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3188 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3189 return Result;
3190 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003191 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3192 switch (N.getValueType()) {
3193 default: assert(0 && "Cannot shift this type!");
3194 case MVT::i8: Opc = X86::SHR8ri; break;
3195 case MVT::i16: Opc = X86::SHR16ri; break;
3196 case MVT::i32: Opc = X86::SHR32ri; break;
3197 }
Chris Lattner11333092005-01-11 03:11:44 +00003198 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003199 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3200 return Result;
3201 }
Chris Lattner11333092005-01-11 03:11:44 +00003202
3203 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3204 Tmp1 = SelectExpr(N.getOperand(0));
3205 Tmp2 = SelectExpr(N.getOperand(1));
3206 } else {
3207 Tmp2 = SelectExpr(N.getOperand(1));
3208 Tmp1 = SelectExpr(N.getOperand(0));
3209 }
3210
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003211 switch (N.getValueType()) {
3212 default: assert(0 && "Cannot shift this type!");
3213 case MVT::i8 : Opc = X86::SHR8rCL; break;
3214 case MVT::i16: Opc = X86::SHR16rCL; break;
3215 case MVT::i32: Opc = X86::SHR32rCL; break;
3216 }
3217 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3218 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3219 return Result;
3220 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003221 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3222 switch (N.getValueType()) {
3223 default: assert(0 && "Cannot shift this type!");
3224 case MVT::i8: Opc = X86::SAR8ri; break;
3225 case MVT::i16: Opc = X86::SAR16ri; break;
3226 case MVT::i32: Opc = X86::SAR32ri; break;
3227 }
Chris Lattner11333092005-01-11 03:11:44 +00003228 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003229 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3230 return Result;
3231 }
Chris Lattner11333092005-01-11 03:11:44 +00003232
3233 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3234 Tmp1 = SelectExpr(N.getOperand(0));
3235 Tmp2 = SelectExpr(N.getOperand(1));
3236 } else {
3237 Tmp2 = SelectExpr(N.getOperand(1));
3238 Tmp1 = SelectExpr(N.getOperand(0));
3239 }
3240
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003241 switch (N.getValueType()) {
3242 default: assert(0 && "Cannot shift this type!");
3243 case MVT::i8 : Opc = X86::SAR8rCL; break;
3244 case MVT::i16: Opc = X86::SAR16rCL; break;
3245 case MVT::i32: Opc = X86::SAR32rCL; break;
3246 }
3247 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3248 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3249 return Result;
3250
3251 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00003252 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003253 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
3254 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
3255 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003256 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003257 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00003258 if (Result != 1) { // Generate the token
3259 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3260 assert(0 && "Load already emitted!?");
3261 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003262 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3263
Chris Lattner5188ad72005-01-08 19:28:19 +00003264 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003265 default: assert(0 && "Cannot load this type!");
3266 case MVT::i1:
3267 case MVT::i8: Opc = X86::MOV8rm; break;
3268 case MVT::i16: Opc = X86::MOV16rm; break;
3269 case MVT::i32: Opc = X86::MOV32rm; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00003270 case MVT::f32: Opc = X86::MOVSSrm; break;
3271 case MVT::f64:
3272 if (X86ScalarSSE) {
3273 Opc = X86::MOVSDrm;
3274 } else {
3275 Opc = X86::FLD64m;
3276 ContainsFPCode = true;
3277 }
3278 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003279 }
Chris Lattner11333092005-01-11 03:11:44 +00003280
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003281 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00003282 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003283 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
3284 } else {
3285 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00003286
3287 SDOperand Chain = N.getOperand(0);
3288 SDOperand Address = N.getOperand(1);
3289 if (getRegPressure(Chain) > getRegPressure(Address)) {
3290 Select(Chain);
3291 SelectAddress(Address, AM);
3292 } else {
3293 SelectAddress(Address, AM);
3294 Select(Chain);
3295 }
3296
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003297 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
3298 }
3299 return Result;
Chris Lattner67649df2005-05-14 06:52:07 +00003300 case X86ISD::FILD64m:
3301 // Make sure we generate both values.
3302 assert(Result != 1 && N.getValueType() == MVT::f64);
3303 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3304 assert(0 && "Load already emitted!?");
3305
3306 {
3307 X86AddressMode AM;
3308
3309 SDOperand Chain = N.getOperand(0);
3310 SDOperand Address = N.getOperand(1);
3311 if (getRegPressure(Chain) > getRegPressure(Address)) {
3312 Select(Chain);
3313 SelectAddress(Address, AM);
3314 } else {
3315 SelectAddress(Address, AM);
3316 Select(Chain);
3317 }
Nate Begeman5a8441e2005-07-16 02:02:34 +00003318 if (X86ScalarSSE) {
3319 addFullAddress(BuildMI(BB, X86::FILD64m, 4, X86::FP0), AM);
3320 addFullAddress(BuildMI(BB, X86::FST64m, 5), AM).addReg(X86::FP0);
3321 addFullAddress(BuildMI(BB, X86::MOVSDrm, 4, Result), AM);
3322 } else {
3323 addFullAddress(BuildMI(BB, X86::FILD64m, 4, Result), AM);
3324 }
Chris Lattner67649df2005-05-14 06:52:07 +00003325 }
3326 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003327
3328 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
3329 case ISD::ZEXTLOAD: {
3330 // Make sure we generate both values.
3331 if (Result != 1)
3332 ExprMap[N.getValue(1)] = 1; // Generate the token
3333 else
3334 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3335
Chris Lattnerda2ce112005-01-16 07:34:08 +00003336 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
3337 if (Node->getValueType(0) == MVT::f64) {
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003338 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnerda2ce112005-01-16 07:34:08 +00003339 "Bad EXTLOAD!");
3340 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
3341 CP->getIndex());
3342 return Result;
3343 }
3344
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003345 X86AddressMode AM;
3346 if (getRegPressure(Node->getOperand(0)) >
3347 getRegPressure(Node->getOperand(1))) {
3348 Select(Node->getOperand(0)); // chain
3349 SelectAddress(Node->getOperand(1), AM);
3350 } else {
3351 SelectAddress(Node->getOperand(1), AM);
3352 Select(Node->getOperand(0)); // chain
3353 }
3354
3355 switch (Node->getValueType(0)) {
3356 default: assert(0 && "Unknown type to sign extend to.");
3357 case MVT::f64:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003358 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003359 "Bad EXTLOAD!");
3360 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
3361 break;
3362 case MVT::i32:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003363 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003364 default:
3365 assert(0 && "Bad zero extend!");
3366 case MVT::i1:
3367 case MVT::i8:
3368 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
3369 break;
3370 case MVT::i16:
3371 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
3372 break;
3373 }
3374 break;
3375 case MVT::i16:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003376 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() <= MVT::i8 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003377 "Bad zero extend!");
3378 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3379 break;
3380 case MVT::i8:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003381 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i1 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003382 "Bad zero extend!");
3383 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
3384 break;
3385 }
3386 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003387 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003388 case ISD::SEXTLOAD: {
3389 // Make sure we generate both values.
3390 if (Result != 1)
3391 ExprMap[N.getValue(1)] = 1; // Generate the token
3392 else
3393 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3394
3395 X86AddressMode AM;
3396 if (getRegPressure(Node->getOperand(0)) >
3397 getRegPressure(Node->getOperand(1))) {
3398 Select(Node->getOperand(0)); // chain
3399 SelectAddress(Node->getOperand(1), AM);
3400 } else {
3401 SelectAddress(Node->getOperand(1), AM);
3402 Select(Node->getOperand(0)); // chain
3403 }
3404
3405 switch (Node->getValueType(0)) {
3406 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
3407 default: assert(0 && "Unknown type to sign extend to.");
3408 case MVT::i32:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003409 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003410 default:
3411 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
3412 case MVT::i8:
3413 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
3414 break;
3415 case MVT::i16:
3416 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
3417 break;
3418 }
3419 break;
3420 case MVT::i16:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003421 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i8 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003422 "Cannot sign extend from bool!");
3423 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3424 break;
3425 }
3426 return Result;
3427 }
3428
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003429 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003430 // Generate both result values.
3431 if (Result != 1)
3432 ExprMap[N.getValue(1)] = 1; // Generate the token
3433 else
3434 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3435
3436 // FIXME: We are currently ignoring the requested alignment for handling
3437 // greater than the stack alignment. This will need to be revisited at some
3438 // point. Align = N.getOperand(2);
3439
3440 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
3441 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
3442 std::cerr << "Cannot allocate stack object with greater alignment than"
3443 << " the stack alignment yet!";
3444 abort();
3445 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003446
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003447 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00003448 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003449 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
3450 .addImm(CN->getValue());
3451 } else {
Chris Lattner11333092005-01-11 03:11:44 +00003452 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3453 Select(N.getOperand(0));
3454 Tmp1 = SelectExpr(N.getOperand(1));
3455 } else {
3456 Tmp1 = SelectExpr(N.getOperand(1));
3457 Select(N.getOperand(0));
3458 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003459
3460 // Subtract size from stack pointer, thereby allocating some space.
3461 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
3462 }
3463
3464 // Put a pointer to the space into the result register, by copying the stack
3465 // pointer.
3466 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
3467 return Result;
3468
Chris Lattner239738a2005-05-14 08:48:15 +00003469 case X86ISD::TAILCALL:
3470 case X86ISD::CALL: {
Chris Lattner5188ad72005-01-08 19:28:19 +00003471 // The chain for this call is now lowered.
Chris Lattner239738a2005-05-14 08:48:15 +00003472 ExprMap.insert(std::make_pair(N.getValue(0), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00003473
Chris Lattnerc6f41812005-05-12 23:06:28 +00003474 bool isDirect = isa<GlobalAddressSDNode>(N.getOperand(1)) ||
3475 isa<ExternalSymbolSDNode>(N.getOperand(1));
3476 unsigned Callee = 0;
3477 if (isDirect) {
3478 Select(N.getOperand(0));
3479 } else {
3480 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3481 Select(N.getOperand(0));
3482 Callee = SelectExpr(N.getOperand(1));
3483 } else {
3484 Callee = SelectExpr(N.getOperand(1));
3485 Select(N.getOperand(0));
3486 }
3487 }
3488
3489 // If this call has values to pass in registers, do so now.
Chris Lattner239738a2005-05-14 08:48:15 +00003490 if (Node->getNumOperands() > 4) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003491 // The first value is passed in (a part of) EAX, the second in EDX.
Chris Lattner239738a2005-05-14 08:48:15 +00003492 unsigned RegOp1 = SelectExpr(N.getOperand(4));
Chris Lattnerc6f41812005-05-12 23:06:28 +00003493 unsigned RegOp2 =
Chris Lattner239738a2005-05-14 08:48:15 +00003494 Node->getNumOperands() > 5 ? SelectExpr(N.getOperand(5)) : 0;
Chris Lattnerc6f41812005-05-12 23:06:28 +00003495
Chris Lattner239738a2005-05-14 08:48:15 +00003496 switch (N.getOperand(4).getValueType()) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003497 default: assert(0 && "Bad thing to pass in regs");
3498 case MVT::i1:
3499 case MVT::i8: BuildMI(BB, X86::MOV8rr , 1,X86::AL).addReg(RegOp1); break;
3500 case MVT::i16: BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1); break;
3501 case MVT::i32: BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);break;
3502 }
3503 if (RegOp2)
Chris Lattner239738a2005-05-14 08:48:15 +00003504 switch (N.getOperand(5).getValueType()) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003505 default: assert(0 && "Bad thing to pass in regs");
3506 case MVT::i1:
3507 case MVT::i8:
3508 BuildMI(BB, X86::MOV8rr , 1, X86::DL).addReg(RegOp2);
3509 break;
3510 case MVT::i16:
3511 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
3512 break;
3513 case MVT::i32:
3514 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
3515 break;
3516 }
3517 }
3518
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003519 if (GlobalAddressSDNode *GASD =
3520 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3521 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
3522 } else if (ExternalSymbolSDNode *ESSDN =
3523 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
3524 BuildMI(BB, X86::CALLpcrel32,
3525 1).addExternalSymbol(ESSDN->getSymbol(), true);
3526 } else {
Chris Lattner11333092005-01-11 03:11:44 +00003527 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3528 Select(N.getOperand(0));
3529 Tmp1 = SelectExpr(N.getOperand(1));
3530 } else {
3531 Tmp1 = SelectExpr(N.getOperand(1));
3532 Select(N.getOperand(0));
3533 }
3534
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003535 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
3536 }
Chris Lattner239738a2005-05-14 08:48:15 +00003537
3538 // Get caller stack amount and amount the callee added to the stack pointer.
3539 Tmp1 = cast<ConstantSDNode>(N.getOperand(2))->getValue();
3540 Tmp2 = cast<ConstantSDNode>(N.getOperand(3))->getValue();
3541 BuildMI(BB, X86::ADJCALLSTACKUP, 2).addImm(Tmp1).addImm(Tmp2);
3542
3543 if (Node->getNumValues() != 1)
3544 switch (Node->getValueType(1)) {
3545 default: assert(0 && "Unknown value type for call result!");
3546 case MVT::Other: return 1;
3547 case MVT::i1:
3548 case MVT::i8:
3549 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3550 break;
3551 case MVT::i16:
3552 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3553 break;
3554 case MVT::i32:
3555 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3556 if (Node->getNumValues() == 3 && Node->getValueType(2) == MVT::i32)
3557 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
3558 break;
3559 case MVT::f64: // Floating-point return values live in %ST(0)
Nate Begemanf63be7d2005-07-06 18:59:04 +00003560 if (X86ScalarSSE) {
3561 ContainsFPCode = true;
3562 BuildMI(BB, X86::FpGETRESULT, 1, X86::FP0);
3563
3564 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
3565 MachineFunction *F = BB->getParent();
3566 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
3567 addFrameReference(BuildMI(BB, X86::FST64m, 5), FrameIdx).addReg(X86::FP0);
3568 addFrameReference(BuildMI(BB, X86::MOVSDrm, 4, Result), FrameIdx);
3569 break;
3570 } else {
3571 ContainsFPCode = true;
3572 BuildMI(BB, X86::FpGETRESULT, 1, Result);
3573 break;
3574 }
Chris Lattner239738a2005-05-14 08:48:15 +00003575 }
3576 return Result+N.ResNo-1;
Chris Lattnerc6f41812005-05-12 23:06:28 +00003577 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00003578 case ISD::READPORT:
3579 // First, determine that the size of the operand falls within the acceptable
3580 // range for this architecture.
3581 //
3582 if (Node->getOperand(1).getValueType() != MVT::i16) {
3583 std::cerr << "llvm.readport: Address size is not 16 bits\n";
3584 exit(1);
3585 }
3586
3587 // Make sure we generate both values.
3588 if (Result != 1) { // Generate the token
3589 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3590 assert(0 && "readport already emitted!?");
3591 } else
3592 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3593
3594 Select(Node->getOperand(0)); // Select the chain.
3595
3596 // If the port is a single-byte constant, use the immediate form.
3597 if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
3598 if ((Port->getValue() & 255) == Port->getValue()) {
3599 switch (Node->getValueType(0)) {
3600 case MVT::i8:
3601 BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue());
3602 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3603 return Result;
3604 case MVT::i16:
3605 BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue());
3606 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3607 return Result;
3608 case MVT::i32:
3609 BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue());
3610 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3611 return Result;
3612 default: break;
3613 }
3614 }
3615
3616 // Now, move the I/O port address into the DX register and use the IN
3617 // instruction to get the input data.
3618 //
3619 Tmp1 = SelectExpr(Node->getOperand(1));
3620 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1);
3621 switch (Node->getValueType(0)) {
3622 case MVT::i8:
3623 BuildMI(BB, X86::IN8rr, 0);
3624 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3625 return Result;
3626 case MVT::i16:
3627 BuildMI(BB, X86::IN16rr, 0);
3628 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3629 return Result;
3630 case MVT::i32:
3631 BuildMI(BB, X86::IN32rr, 0);
3632 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3633 return Result;
3634 default:
3635 std::cerr << "Cannot do input on this data type";
3636 exit(1);
3637 }
3638
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003639 }
3640
3641 return 0;
3642}
3643
Chris Lattnere10269b2005-01-17 19:25:26 +00003644/// TryToFoldLoadOpStore - Given a store node, try to fold together a
3645/// load/op/store instruction. If successful return true.
3646bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
3647 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
3648 SDOperand Chain = Node->getOperand(0);
3649 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00003650 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00003651
3652 // The chain has to be a load, the stored value must be an integer binary
3653 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00003654 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00003655 MVT::isFloatingPoint(StVal.getValueType()))
3656 return false;
3657
Chris Lattner5c659812005-01-17 22:10:42 +00003658 // Token chain must either be a factor node or the load to fold.
3659 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
3660 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00003661
Chris Lattner5c659812005-01-17 22:10:42 +00003662 SDOperand TheLoad;
3663
3664 // Check to see if there is a load from the same pointer that we're storing
3665 // to in either operand of the binop.
3666 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
3667 StVal.getOperand(0).getOperand(1) == StPtr)
3668 TheLoad = StVal.getOperand(0);
3669 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
3670 StVal.getOperand(1).getOperand(1) == StPtr)
3671 TheLoad = StVal.getOperand(1);
3672 else
3673 return false; // No matching load operand.
3674
3675 // We can only fold the load if there are no intervening side-effecting
3676 // operations. This means that the store uses the load as its token chain, or
3677 // there are only token factor nodes in between the store and load.
3678 if (Chain != TheLoad.getValue(1)) {
3679 // Okay, the other option is that we have a store referring to (possibly
3680 // nested) token factor nodes. For now, just try peeking through one level
3681 // of token factors to see if this is the case.
3682 bool ChainOk = false;
3683 if (Chain.getOpcode() == ISD::TokenFactor) {
3684 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3685 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
3686 ChainOk = true;
3687 break;
3688 }
3689 }
3690
3691 if (!ChainOk) return false;
3692 }
3693
3694 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00003695 return false;
3696
3697 // Make sure that one of the operands of the binop is the load, and that the
3698 // load folds into the binop.
3699 if (((StVal.getOperand(0) != TheLoad ||
3700 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
3701 (StVal.getOperand(1) != TheLoad ||
3702 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
3703 return false;
3704
3705 // Finally, check to see if this is one of the ops we can handle!
3706 static const unsigned ADDTAB[] = {
3707 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
3708 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
3709 };
3710 static const unsigned SUBTAB[] = {
3711 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
3712 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
3713 };
3714 static const unsigned ANDTAB[] = {
3715 X86::AND8mi, X86::AND16mi, X86::AND32mi,
3716 X86::AND8mr, X86::AND16mr, X86::AND32mr,
3717 };
3718 static const unsigned ORTAB[] = {
3719 X86::OR8mi, X86::OR16mi, X86::OR32mi,
3720 X86::OR8mr, X86::OR16mr, X86::OR32mr,
3721 };
3722 static const unsigned XORTAB[] = {
3723 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
3724 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
3725 };
3726 static const unsigned SHLTAB[] = {
3727 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
3728 /*Have to put the reg in CL*/0, 0, 0,
3729 };
3730 static const unsigned SARTAB[] = {
3731 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
3732 /*Have to put the reg in CL*/0, 0, 0,
3733 };
3734 static const unsigned SHRTAB[] = {
3735 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
3736 /*Have to put the reg in CL*/0, 0, 0,
3737 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003738
Chris Lattnere10269b2005-01-17 19:25:26 +00003739 const unsigned *TabPtr = 0;
3740 switch (StVal.getOpcode()) {
3741 default:
3742 std::cerr << "CANNOT [mem] op= val: ";
3743 StVal.Val->dump(); std::cerr << "\n";
3744 case ISD::MUL:
3745 case ISD::SDIV:
3746 case ISD::UDIV:
3747 case ISD::SREM:
3748 case ISD::UREM: return false;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003749
Chris Lattnere10269b2005-01-17 19:25:26 +00003750 case ISD::ADD: TabPtr = ADDTAB; break;
3751 case ISD::SUB: TabPtr = SUBTAB; break;
3752 case ISD::AND: TabPtr = ANDTAB; break;
3753 case ISD:: OR: TabPtr = ORTAB; break;
3754 case ISD::XOR: TabPtr = XORTAB; break;
3755 case ISD::SHL: TabPtr = SHLTAB; break;
3756 case ISD::SRA: TabPtr = SARTAB; break;
3757 case ISD::SRL: TabPtr = SHRTAB; break;
3758 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003759
Chris Lattnere10269b2005-01-17 19:25:26 +00003760 // Handle: [mem] op= CST
3761 SDOperand Op0 = StVal.getOperand(0);
3762 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00003763 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00003764 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
3765 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
3766 default: break;
3767 case MVT::i1:
3768 case MVT::i8: Opc = TabPtr[0]; break;
3769 case MVT::i16: Opc = TabPtr[1]; break;
3770 case MVT::i32: Opc = TabPtr[2]; break;
3771 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003772
Chris Lattnere10269b2005-01-17 19:25:26 +00003773 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00003774 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3775 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00003776 Select(Chain);
3777
Chris Lattnere10269b2005-01-17 19:25:26 +00003778 X86AddressMode AM;
3779 if (getRegPressure(TheLoad.getOperand(0)) >
3780 getRegPressure(TheLoad.getOperand(1))) {
3781 Select(TheLoad.getOperand(0));
3782 SelectAddress(TheLoad.getOperand(1), AM);
3783 } else {
3784 SelectAddress(TheLoad.getOperand(1), AM);
3785 Select(TheLoad.getOperand(0));
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003786 }
Chris Lattner5c659812005-01-17 22:10:42 +00003787
3788 if (StVal.getOpcode() == ISD::ADD) {
3789 if (CN->getValue() == 1) {
3790 switch (Op0.getValueType()) {
3791 default: break;
3792 case MVT::i8:
3793 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
3794 return true;
3795 case MVT::i16: Opc = TabPtr[1];
3796 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
3797 return true;
3798 case MVT::i32: Opc = TabPtr[2];
3799 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
3800 return true;
3801 }
3802 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
3803 switch (Op0.getValueType()) {
3804 default: break;
3805 case MVT::i8:
3806 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
3807 return true;
3808 case MVT::i16: Opc = TabPtr[1];
3809 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
3810 return true;
3811 case MVT::i32: Opc = TabPtr[2];
3812 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
3813 return true;
3814 }
3815 }
3816 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003817
Chris Lattnere10269b2005-01-17 19:25:26 +00003818 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
3819 return true;
3820 }
3821 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003822
Chris Lattnere10269b2005-01-17 19:25:26 +00003823 // If we have [mem] = V op [mem], try to turn it into:
3824 // [mem] = [mem] op V.
3825 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
3826 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
3827 StVal.getOpcode() != ISD::SRL)
3828 std::swap(Op0, Op1);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003829
Chris Lattnere10269b2005-01-17 19:25:26 +00003830 if (Op0 != TheLoad) return false;
3831
3832 switch (Op0.getValueType()) {
3833 default: return false;
3834 case MVT::i1:
3835 case MVT::i8: Opc = TabPtr[3]; break;
3836 case MVT::i16: Opc = TabPtr[4]; break;
3837 case MVT::i32: Opc = TabPtr[5]; break;
3838 }
Chris Lattner5c659812005-01-17 22:10:42 +00003839
Chris Lattnerb422aea2005-01-18 17:35:28 +00003840 // Table entry doesn't exist?
3841 if (Opc == 0) return false;
3842
Chris Lattner4a108662005-01-18 03:51:59 +00003843 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3844 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00003845 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00003846 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00003847
Chris Lattnere10269b2005-01-17 19:25:26 +00003848 X86AddressMode AM;
3849 SelectAddress(TheLoad.getOperand(1), AM);
3850 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00003851 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00003852 return true;
3853}
3854
Chris Lattner381e8872005-05-15 05:46:45 +00003855/// If node is a ret(tailcall) node, emit the specified tail call and return
3856/// true, otherwise return false.
3857///
3858/// FIXME: This whole thing should be a post-legalize optimization pass which
3859/// recognizes and transforms the dag. We don't want the selection phase doing
3860/// this stuff!!
3861///
3862bool ISel::EmitPotentialTailCall(SDNode *RetNode) {
3863 assert(RetNode->getOpcode() == ISD::RET && "Not a return");
3864
3865 SDOperand Chain = RetNode->getOperand(0);
3866
3867 // If this is a token factor node where one operand is a call, dig into it.
3868 SDOperand TokFactor;
3869 unsigned TokFactorOperand = 0;
3870 if (Chain.getOpcode() == ISD::TokenFactor) {
3871 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3872 if (Chain.getOperand(i).getOpcode() == ISD::CALLSEQ_END ||
3873 Chain.getOperand(i).getOpcode() == X86ISD::TAILCALL) {
3874 TokFactorOperand = i;
3875 TokFactor = Chain;
3876 Chain = Chain.getOperand(i);
3877 break;
3878 }
3879 if (TokFactor.Val == 0) return false; // No call operand.
3880 }
3881
3882 // Skip the CALLSEQ_END node if present.
3883 if (Chain.getOpcode() == ISD::CALLSEQ_END)
3884 Chain = Chain.getOperand(0);
3885
3886 // Is a tailcall the last control operation that occurs before the return?
3887 if (Chain.getOpcode() != X86ISD::TAILCALL)
3888 return false;
3889
3890 // If we return a value, is it the value produced by the call?
3891 if (RetNode->getNumOperands() > 1) {
3892 // Not returning the ret val of the call?
3893 if (Chain.Val->getNumValues() == 1 ||
3894 RetNode->getOperand(1) != Chain.getValue(1))
3895 return false;
3896
3897 if (RetNode->getNumOperands() > 2) {
3898 if (Chain.Val->getNumValues() == 2 ||
3899 RetNode->getOperand(2) != Chain.getValue(2))
3900 return false;
3901 }
3902 assert(RetNode->getNumOperands() <= 3);
3903 }
3904
3905 // CalleeCallArgAmt - The total number of bytes used for the callee arg area.
3906 // For FastCC, this will always be > 0.
3907 unsigned CalleeCallArgAmt =
3908 cast<ConstantSDNode>(Chain.getOperand(2))->getValue();
3909
3910 // CalleeCallArgPopAmt - The number of bytes in the call area popped by the
3911 // callee. For FastCC this will always be > 0, for CCC this is always 0.
3912 unsigned CalleeCallArgPopAmt =
3913 cast<ConstantSDNode>(Chain.getOperand(3))->getValue();
3914
3915 // There are several cases we can handle here. First, if the caller and
3916 // callee are both CCC functions, we can tailcall if the callee takes <= the
3917 // number of argument bytes that the caller does.
3918 if (CalleeCallArgPopAmt == 0 && // Callee is C CallingConv?
3919 X86Lowering.getBytesToPopOnReturn() == 0) { // Caller is C CallingConv?
3920 // Check to see if caller arg area size >= callee arg area size.
3921 if (X86Lowering.getBytesCallerReserves() >= CalleeCallArgAmt) {
3922 //std::cerr << "CCC TAILCALL UNIMP!\n";
3923 // If TokFactor is non-null, emit all operands.
3924
3925 //EmitCCCToCCCTailCall(Chain.Val);
3926 //return true;
3927 }
3928 return false;
3929 }
3930
3931 // Second, if both are FastCC functions, we can always perform the tail call.
3932 if (CalleeCallArgPopAmt && X86Lowering.getBytesToPopOnReturn()) {
3933 // If TokFactor is non-null, emit all operands before the call.
3934 if (TokFactor.Val) {
3935 for (unsigned i = 0, e = TokFactor.getNumOperands(); i != e; ++i)
3936 if (i != TokFactorOperand)
3937 Select(TokFactor.getOperand(i));
3938 }
3939
3940 EmitFastCCToFastCCTailCall(Chain.Val);
3941 return true;
3942 }
3943
3944 // We don't support mixed calls, due to issues with alignment. We could in
3945 // theory handle some mixed calls from CCC -> FastCC if the stack is properly
3946 // aligned (which depends on the number of arguments to the callee). TODO.
3947 return false;
3948}
3949
3950static SDOperand GetAdjustedArgumentStores(SDOperand Chain, int Offset,
3951 SelectionDAG &DAG) {
3952 MVT::ValueType StoreVT;
3953 switch (Chain.getOpcode()) {
3954 case ISD::CALLSEQ_START:
Chris Lattnerea035432005-05-15 06:07:10 +00003955 // If we found the start of the call sequence, we're done. We actually
3956 // strip off the CALLSEQ_START node, to avoid generating the
3957 // ADJCALLSTACKDOWN marker for the tail call.
3958 return Chain.getOperand(0);
Chris Lattner381e8872005-05-15 05:46:45 +00003959 case ISD::TokenFactor: {
3960 std::vector<SDOperand> Ops;
3961 Ops.reserve(Chain.getNumOperands());
3962 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3963 Ops.push_back(GetAdjustedArgumentStores(Chain.getOperand(i), Offset,DAG));
3964 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
3965 }
3966 case ISD::STORE: // Normal store
3967 StoreVT = Chain.getOperand(1).getValueType();
3968 break;
3969 case ISD::TRUNCSTORE: // FLOAT store
Chris Lattner9fadb4c2005-07-10 00:29:18 +00003970 StoreVT = cast<VTSDNode>(Chain.getOperand(4))->getVT();
Chris Lattner381e8872005-05-15 05:46:45 +00003971 break;
3972 }
3973
3974 SDOperand OrigDest = Chain.getOperand(2);
3975 unsigned OrigOffset;
3976
3977 if (OrigDest.getOpcode() == ISD::CopyFromReg) {
3978 OrigOffset = 0;
3979 assert(cast<RegSDNode>(OrigDest)->getReg() == X86::ESP);
3980 } else {
3981 // We expect only (ESP+C)
3982 assert(OrigDest.getOpcode() == ISD::ADD &&
3983 isa<ConstantSDNode>(OrigDest.getOperand(1)) &&
3984 OrigDest.getOperand(0).getOpcode() == ISD::CopyFromReg &&
3985 cast<RegSDNode>(OrigDest.getOperand(0))->getReg() == X86::ESP);
3986 OrigOffset = cast<ConstantSDNode>(OrigDest.getOperand(1))->getValue();
3987 }
3988
3989 // Compute the new offset from the incoming ESP value we wish to use.
3990 unsigned NewOffset = OrigOffset + Offset;
3991
3992 unsigned OpSize = (MVT::getSizeInBits(StoreVT)+7)/8; // Bits -> Bytes
3993 MachineFunction &MF = DAG.getMachineFunction();
3994 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, NewOffset);
3995 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
3996
3997 SDOperand InChain = GetAdjustedArgumentStores(Chain.getOperand(0), Offset,
3998 DAG);
3999 if (Chain.getOpcode() == ISD::STORE)
4000 return DAG.getNode(ISD::STORE, MVT::Other, InChain, Chain.getOperand(1),
4001 FIN);
4002 assert(Chain.getOpcode() == ISD::TRUNCSTORE);
4003 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, InChain, Chain.getOperand(1),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00004004 FIN, DAG.getSrcValue(NULL), DAG.getValueType(StoreVT));
Chris Lattner381e8872005-05-15 05:46:45 +00004005}
4006
4007
4008/// EmitFastCCToFastCCTailCall - Given a tailcall in the tail position to a
4009/// fastcc function from a fastcc function, emit the code to emit a 'proper'
4010/// tail call.
4011void ISel::EmitFastCCToFastCCTailCall(SDNode *TailCallNode) {
4012 unsigned CalleeCallArgSize =
4013 cast<ConstantSDNode>(TailCallNode->getOperand(2))->getValue();
4014 unsigned CallerArgSize = X86Lowering.getBytesToPopOnReturn();
4015
4016 //std::cerr << "****\n*** EMITTING TAIL CALL!\n****\n";
4017
4018 // Adjust argument stores. Instead of storing to [ESP], f.e., store to frame
4019 // indexes that are relative to the incoming ESP. If the incoming and
4020 // outgoing arg sizes are the same we will store to [InESP] instead of
4021 // [CurESP] and the ESP referenced will be relative to the incoming function
4022 // ESP.
4023 int ESPOffset = CallerArgSize-CalleeCallArgSize;
4024 SDOperand AdjustedArgStores =
4025 GetAdjustedArgumentStores(TailCallNode->getOperand(0), ESPOffset, *TheDAG);
4026
4027 // Copy the return address of the caller into a virtual register so we don't
4028 // clobber it.
4029 SDOperand RetVal;
4030 if (ESPOffset) {
4031 SDOperand RetValAddr = X86Lowering.getReturnAddressFrameIndex(*TheDAG);
4032 RetVal = TheDAG->getLoad(MVT::i32, TheDAG->getEntryNode(),
4033 RetValAddr, TheDAG->getSrcValue(NULL));
4034 SelectExpr(RetVal);
4035 }
4036
4037 // Codegen all of the argument stores.
4038 Select(AdjustedArgStores);
4039
4040 if (RetVal.Val) {
4041 // Emit a store of the saved ret value to the new location.
4042 MachineFunction &MF = TheDAG->getMachineFunction();
4043 int ReturnAddrFI = MF.getFrameInfo()->CreateFixedObject(4, ESPOffset-4);
4044 SDOperand RetValAddr = TheDAG->getFrameIndex(ReturnAddrFI, MVT::i32);
4045 Select(TheDAG->getNode(ISD::STORE, MVT::Other, TheDAG->getEntryNode(),
4046 RetVal, RetValAddr));
4047 }
4048
4049 // Get the destination value.
4050 SDOperand Callee = TailCallNode->getOperand(1);
4051 bool isDirect = isa<GlobalAddressSDNode>(Callee) ||
4052 isa<ExternalSymbolSDNode>(Callee);
Chris Lattner9cb2d612005-06-17 13:23:32 +00004053 unsigned CalleeReg = 0;
Chris Lattner381e8872005-05-15 05:46:45 +00004054 if (!isDirect) CalleeReg = SelectExpr(Callee);
4055
4056 unsigned RegOp1 = 0;
4057 unsigned RegOp2 = 0;
4058
4059 if (TailCallNode->getNumOperands() > 4) {
4060 // The first value is passed in (a part of) EAX, the second in EDX.
4061 RegOp1 = SelectExpr(TailCallNode->getOperand(4));
4062 if (TailCallNode->getNumOperands() > 5)
4063 RegOp2 = SelectExpr(TailCallNode->getOperand(5));
4064
4065 switch (TailCallNode->getOperand(4).getValueType()) {
4066 default: assert(0 && "Bad thing to pass in regs");
4067 case MVT::i1:
4068 case MVT::i8:
4069 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(RegOp1);
4070 RegOp1 = X86::AL;
4071 break;
4072 case MVT::i16:
4073 BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1);
4074 RegOp1 = X86::AX;
4075 break;
4076 case MVT::i32:
4077 BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);
4078 RegOp1 = X86::EAX;
4079 break;
4080 }
4081 if (RegOp2)
4082 switch (TailCallNode->getOperand(5).getValueType()) {
4083 default: assert(0 && "Bad thing to pass in regs");
4084 case MVT::i1:
4085 case MVT::i8:
4086 BuildMI(BB, X86::MOV8rr, 1, X86::DL).addReg(RegOp2);
4087 RegOp2 = X86::DL;
4088 break;
4089 case MVT::i16:
4090 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
4091 RegOp2 = X86::DX;
4092 break;
4093 case MVT::i32:
4094 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
4095 RegOp2 = X86::EDX;
4096 break;
4097 }
4098 }
4099
4100 // Adjust ESP.
4101 if (ESPOffset)
4102 BuildMI(BB, X86::ADJSTACKPTRri, 2,
4103 X86::ESP).addReg(X86::ESP).addImm(ESPOffset);
4104
4105 // TODO: handle jmp [mem]
4106 if (!isDirect) {
4107 BuildMI(BB, X86::TAILJMPr, 1).addReg(CalleeReg);
4108 } else if (GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Callee)){
Chris Lattner16cb6f82005-05-19 05:54:33 +00004109 BuildMI(BB, X86::TAILJMPd, 1).addGlobalAddress(GASD->getGlobal(), true);
Chris Lattner381e8872005-05-15 05:46:45 +00004110 } else {
4111 ExternalSymbolSDNode *ESSDN = cast<ExternalSymbolSDNode>(Callee);
4112 BuildMI(BB, X86::TAILJMPd, 1).addExternalSymbol(ESSDN->getSymbol(), true);
4113 }
4114 // ADD IMPLICIT USE RegOp1/RegOp2's
4115}
4116
Chris Lattnere10269b2005-01-17 19:25:26 +00004117
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004118void ISel::Select(SDOperand N) {
4119 unsigned Tmp1, Tmp2, Opc;
4120
Nate Begeman85fdeb22005-03-24 04:39:54 +00004121 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004122 return; // Already selected.
4123
Chris Lattner989de032005-01-11 06:14:36 +00004124 SDNode *Node = N.Val;
4125
4126 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004127 default:
Chris Lattner989de032005-01-11 06:14:36 +00004128 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004129 assert(0 && "Node not handled yet!");
4130 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00004131 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00004132 if (Node->getNumOperands() == 2) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004133 bool OneFirst =
Chris Lattner1d50b7f2005-01-13 19:56:00 +00004134 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
4135 Select(Node->getOperand(OneFirst));
4136 Select(Node->getOperand(!OneFirst));
4137 } else {
4138 std::vector<std::pair<unsigned, unsigned> > OpsP;
4139 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
4140 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
4141 std::sort(OpsP.begin(), OpsP.end());
4142 std::reverse(OpsP.begin(), OpsP.end());
4143 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
4144 Select(Node->getOperand(OpsP[i].second));
4145 }
Chris Lattnerc3580712005-01-13 18:01:36 +00004146 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004147 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00004148 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4149 Select(N.getOperand(0));
4150 Tmp1 = SelectExpr(N.getOperand(1));
4151 } else {
4152 Tmp1 = SelectExpr(N.getOperand(1));
4153 Select(N.getOperand(0));
4154 }
Chris Lattner18c2f132005-01-13 20:50:02 +00004155 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004156
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004157 if (Tmp1 != Tmp2) {
4158 switch (N.getOperand(1).getValueType()) {
4159 default: assert(0 && "Invalid type for operation!");
4160 case MVT::i1:
4161 case MVT::i8: Opc = X86::MOV8rr; break;
4162 case MVT::i16: Opc = X86::MOV16rr; break;
4163 case MVT::i32: Opc = X86::MOV32rr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004164 case MVT::f32: Opc = X86::MOVAPSrr; break;
4165 case MVT::f64:
4166 if (X86ScalarSSE) {
4167 Opc = X86::MOVAPDrr;
4168 } else {
4169 Opc = X86::FpMOV;
4170 ContainsFPCode = true;
4171 }
4172 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004173 }
4174 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
4175 }
4176 return;
4177 case ISD::RET:
Chris Lattner381e8872005-05-15 05:46:45 +00004178 if (N.getOperand(0).getOpcode() == ISD::CALLSEQ_END ||
4179 N.getOperand(0).getOpcode() == X86ISD::TAILCALL ||
4180 N.getOperand(0).getOpcode() == ISD::TokenFactor)
4181 if (EmitPotentialTailCall(Node))
4182 return;
4183
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004184 switch (N.getNumOperands()) {
4185 default:
4186 assert(0 && "Unknown return instruction!");
4187 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004188 assert(N.getOperand(1).getValueType() == MVT::i32 &&
4189 N.getOperand(2).getValueType() == MVT::i32 &&
4190 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00004191 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
4192 Tmp1 = SelectExpr(N.getOperand(1));
4193 Tmp2 = SelectExpr(N.getOperand(2));
4194 } else {
4195 Tmp2 = SelectExpr(N.getOperand(2));
4196 Tmp1 = SelectExpr(N.getOperand(1));
4197 }
4198 Select(N.getOperand(0));
4199
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004200 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4201 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004202 break;
4203 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00004204 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4205 Select(N.getOperand(0));
4206 Tmp1 = SelectExpr(N.getOperand(1));
4207 } else {
4208 Tmp1 = SelectExpr(N.getOperand(1));
4209 Select(N.getOperand(0));
4210 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004211 switch (N.getOperand(1).getValueType()) {
4212 default: assert(0 && "All other types should have been promoted!!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00004213 case MVT::f32:
4214 if (X86ScalarSSE) {
4215 // Spill the value to memory and reload it into top of stack.
4216 unsigned Size = MVT::getSizeInBits(MVT::f32)/8;
4217 MachineFunction *F = BB->getParent();
4218 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
4219 addFrameReference(BuildMI(BB, X86::MOVSSmr, 5), FrameIdx).addReg(Tmp1);
4220 addFrameReference(BuildMI(BB, X86::FLD32m, 4, X86::FP0), FrameIdx);
4221 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
4222 ContainsFPCode = true;
4223 } else {
4224 assert(0 && "MVT::f32 only legal with scalar sse fp");
4225 abort();
4226 }
4227 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004228 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00004229 if (X86ScalarSSE) {
4230 // Spill the value to memory and reload it into top of stack.
4231 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
4232 MachineFunction *F = BB->getParent();
4233 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
4234 addFrameReference(BuildMI(BB, X86::MOVSDmr, 5), FrameIdx).addReg(Tmp1);
4235 addFrameReference(BuildMI(BB, X86::FLD64m, 4, X86::FP0), FrameIdx);
4236 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
4237 ContainsFPCode = true;
4238 } else {
4239 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
4240 }
4241 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004242 case MVT::i32:
Nate Begemanf63be7d2005-07-06 18:59:04 +00004243 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4244 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004245 }
4246 break;
4247 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00004248 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004249 break;
4250 }
Chris Lattner3648c672005-05-13 21:44:04 +00004251 if (X86Lowering.getBytesToPopOnReturn() == 0)
4252 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
4253 else
4254 BuildMI(BB, X86::RETI, 1).addImm(X86Lowering.getBytesToPopOnReturn());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004255 return;
4256 case ISD::BR: {
4257 Select(N.getOperand(0));
4258 MachineBasicBlock *Dest =
4259 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
4260 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
4261 return;
4262 }
4263
4264 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004265 MachineBasicBlock *Dest =
4266 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00004267
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004268 // Try to fold a setcc into the branch. If this fails, emit a test/jne
4269 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00004270 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
4271 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4272 Select(N.getOperand(0));
4273 Tmp1 = SelectExpr(N.getOperand(1));
4274 } else {
4275 Tmp1 = SelectExpr(N.getOperand(1));
4276 Select(N.getOperand(0));
4277 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004278 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
4279 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
4280 }
Chris Lattner11333092005-01-11 03:11:44 +00004281
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004282 return;
4283 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004284
Chris Lattner4df0de92005-01-17 00:00:33 +00004285 case ISD::LOAD:
4286 // If this load could be folded into the only using instruction, and if it
4287 // is safe to emit the instruction here, try to do so now.
4288 if (Node->hasNUsesOfValue(1, 0)) {
4289 SDOperand TheVal = N.getValue(0);
4290 SDNode *User = 0;
4291 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
4292 assert(UI != Node->use_end() && "Didn't find use!");
4293 SDNode *UN = *UI;
4294 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
4295 if (UN->getOperand(i) == TheVal) {
4296 User = UN;
4297 goto FoundIt;
4298 }
4299 }
4300 FoundIt:
4301 // Only handle unary operators right now.
4302 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00004303 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00004304 SelectExpr(SDOperand(User, 0));
4305 return;
4306 }
4307 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00004308 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00004309 SelectExpr(N);
4310 return;
Chris Lattner966cdfb2005-05-09 21:17:38 +00004311 case ISD::READPORT:
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004312 case ISD::EXTLOAD:
4313 case ISD::SEXTLOAD:
4314 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004315 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner239738a2005-05-14 08:48:15 +00004316 case X86ISD::TAILCALL:
4317 case X86ISD::CALL:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00004318 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004319 SelectExpr(N);
4320 return;
Chris Lattnerc6f41812005-05-12 23:06:28 +00004321 case ISD::CopyFromReg:
Chris Lattner67649df2005-05-14 06:52:07 +00004322 case X86ISD::FILD64m:
Chris Lattnerc6f41812005-05-12 23:06:28 +00004323 ExprMap.erase(N);
4324 SelectExpr(N.getValue(0));
4325 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004326
Chris Lattner9fadb4c2005-07-10 00:29:18 +00004327 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr, SRCVALUE, storety
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004328 X86AddressMode AM;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00004329 MVT::ValueType StoredTy = cast<VTSDNode>(N.getOperand(4))->getVT();
Chris Lattnerda2ce112005-01-16 07:34:08 +00004330 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
4331 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
4332 && "Unsupported TRUNCSTORE for this target!");
4333
4334 if (StoredTy == MVT::i16) {
4335 // FIXME: This is here just to allow testing. X86 doesn't really have a
4336 // TRUNCSTORE i16 operation, but this is required for targets that do not
4337 // have 16-bit integer registers. We occasionally disable 16-bit integer
4338 // registers to test the promotion code.
4339 Select(N.getOperand(0));
4340 Tmp1 = SelectExpr(N.getOperand(1));
4341 SelectAddress(N.getOperand(2), AM);
4342
4343 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4344 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
4345 return;
4346 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004347
4348 // Store of constant bool?
4349 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
4350 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4351 Select(N.getOperand(0));
4352 SelectAddress(N.getOperand(2), AM);
4353 } else {
4354 SelectAddress(N.getOperand(2), AM);
4355 Select(N.getOperand(0));
4356 }
4357 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
4358 return;
4359 }
4360
4361 switch (StoredTy) {
4362 default: assert(0 && "Cannot truncstore this type!");
4363 case MVT::i1: Opc = X86::MOV8mr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004364 case MVT::f32:
4365 assert(!X86ScalarSSE && "Cannot truncstore scalar SSE regs");
4366 Opc = X86::FST32m; break;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004367 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004368
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004369 std::vector<std::pair<unsigned, unsigned> > RP;
4370 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
4371 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
4372 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
4373 std::sort(RP.begin(), RP.end());
4374
Chris Lattner572dd082005-02-23 05:57:21 +00004375 Tmp1 = 0; // Silence a warning.
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004376 for (unsigned i = 0; i != 3; ++i)
4377 switch (RP[2-i].second) {
4378 default: assert(0 && "Unknown operand number!");
4379 case 0: Select(N.getOperand(0)); break;
4380 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
4381 case 2: SelectAddress(N.getOperand(2), AM); break;
4382 }
4383
4384 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
4385 return;
4386 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004387 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004388 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004389
4390 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
4391 Opc = 0;
4392 switch (CN->getValueType(0)) {
4393 default: assert(0 && "Invalid type for operation!");
4394 case MVT::i1:
4395 case MVT::i8: Opc = X86::MOV8mi; break;
4396 case MVT::i16: Opc = X86::MOV16mi; break;
4397 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004398 }
4399 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00004400 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4401 Select(N.getOperand(0));
4402 SelectAddress(N.getOperand(2), AM);
4403 } else {
4404 SelectAddress(N.getOperand(2), AM);
4405 Select(N.getOperand(0));
4406 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004407 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
4408 return;
4409 }
Chris Lattner75f354b2005-04-21 19:03:24 +00004410 } else if (GlobalAddressSDNode *GA =
4411 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
4412 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
4413
4414 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4415 Select(N.getOperand(0));
4416 SelectAddress(N.getOperand(2), AM);
4417 } else {
4418 SelectAddress(N.getOperand(2), AM);
4419 Select(N.getOperand(0));
4420 }
Nate Begeman16b04f32005-07-15 00:38:55 +00004421 GlobalValue *GV = GA->getGlobal();
4422 // For Darwin, external and weak symbols are indirect, so we want to load
4423 // the value at address GV, not the value of GV itself.
4424 if (Subtarget->getIndirectExternAndWeakGlobals() &&
4425 (GV->hasWeakLinkage() || GV->isExternal())) {
4426 Tmp1 = MakeReg(MVT::i32);
4427 BuildMI(BB, X86::MOV32rm, 4, Tmp1).addReg(0).addZImm(1).addReg(0)
4428 .addGlobalAddress(GV, false, 0);
4429 addFullAddress(BuildMI(BB, X86::MOV32mr, 4+1),AM).addReg(Tmp1);
4430 } else {
4431 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),AM).addGlobalAddress(GV);
4432 }
Chris Lattner75f354b2005-04-21 19:03:24 +00004433 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004434 }
Chris Lattner837caa72005-01-11 23:21:30 +00004435
4436 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00004437 if (TryToFoldLoadOpStore(Node))
4438 return;
Chris Lattner837caa72005-01-11 23:21:30 +00004439
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004440 switch (N.getOperand(1).getValueType()) {
4441 default: assert(0 && "Cannot store this type!");
4442 case MVT::i1:
4443 case MVT::i8: Opc = X86::MOV8mr; break;
4444 case MVT::i16: Opc = X86::MOV16mr; break;
4445 case MVT::i32: Opc = X86::MOV32mr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004446 case MVT::f32: Opc = X86::MOVSSmr; break;
4447 case MVT::f64: Opc = X86ScalarSSE ? X86::MOVSDmr : X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004448 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004449
Chris Lattner11333092005-01-11 03:11:44 +00004450 std::vector<std::pair<unsigned, unsigned> > RP;
4451 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
4452 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
4453 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
4454 std::sort(RP.begin(), RP.end());
4455
Chris Lattner572dd082005-02-23 05:57:21 +00004456 Tmp1 = 0; // Silence a warning.
Chris Lattner11333092005-01-11 03:11:44 +00004457 for (unsigned i = 0; i != 3; ++i)
4458 switch (RP[2-i].second) {
4459 default: assert(0 && "Unknown operand number!");
4460 case 0: Select(N.getOperand(0)); break;
4461 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00004462 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00004463 }
4464
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004465 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
4466 return;
4467 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00004468 case ISD::CALLSEQ_START:
Chris Lattner3648c672005-05-13 21:44:04 +00004469 Select(N.getOperand(0));
4470 // Stack amount
4471 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
4472 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(Tmp1);
4473 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00004474 case ISD::CALLSEQ_END:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004475 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004476 return;
Chris Lattner989de032005-01-11 06:14:36 +00004477 case ISD::MEMSET: {
4478 Select(N.getOperand(0)); // Select the chain.
4479 unsigned Align =
4480 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
4481 if (Align == 0) Align = 1;
4482
4483 // Turn the byte code into # iterations
4484 unsigned CountReg;
4485 unsigned Opcode;
4486 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
4487 unsigned Val = ValC->getValue() & 255;
4488
4489 // If the value is a constant, then we can potentially use larger sets.
4490 switch (Align & 3) {
4491 case 2: // WORD aligned
4492 CountReg = MakeReg(MVT::i32);
4493 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4494 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
4495 } else {
4496 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4497 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
4498 }
4499 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
4500 Opcode = X86::REP_STOSW;
4501 break;
4502 case 0: // DWORD aligned
4503 CountReg = MakeReg(MVT::i32);
4504 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4505 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
4506 } else {
4507 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4508 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
4509 }
4510 Val = (Val << 8) | Val;
4511 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
4512 Opcode = X86::REP_STOSD;
4513 break;
4514 default: // BYTE aligned
4515 CountReg = SelectExpr(Node->getOperand(3));
4516 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
4517 Opcode = X86::REP_STOSB;
4518 break;
4519 }
4520 } else {
4521 // If it's not a constant value we are storing, just fall back. We could
4522 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
4523 unsigned ValReg = SelectExpr(Node->getOperand(2));
4524 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
4525 CountReg = SelectExpr(Node->getOperand(3));
4526 Opcode = X86::REP_STOSB;
4527 }
4528
4529 // No matter what the alignment is, we put the source in ESI, the
4530 // destination in EDI, and the count in ECX.
4531 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
4532 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
4533 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
4534 BuildMI(BB, Opcode, 0);
4535 return;
4536 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00004537 case ISD::MEMCPY: {
Chris Lattner31805bf2005-01-11 06:19:26 +00004538 Select(N.getOperand(0)); // Select the chain.
4539 unsigned Align =
4540 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
4541 if (Align == 0) Align = 1;
4542
4543 // Turn the byte code into # iterations
4544 unsigned CountReg;
4545 unsigned Opcode;
4546 switch (Align & 3) {
4547 case 2: // WORD aligned
4548 CountReg = MakeReg(MVT::i32);
4549 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4550 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
4551 } else {
4552 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4553 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
4554 }
4555 Opcode = X86::REP_MOVSW;
4556 break;
4557 case 0: // DWORD aligned
4558 CountReg = MakeReg(MVT::i32);
4559 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4560 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
4561 } else {
4562 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4563 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
4564 }
4565 Opcode = X86::REP_MOVSD;
4566 break;
4567 default: // BYTE aligned
4568 CountReg = SelectExpr(Node->getOperand(3));
4569 Opcode = X86::REP_MOVSB;
4570 break;
4571 }
4572
4573 // No matter what the alignment is, we put the source in ESI, the
4574 // destination in EDI, and the count in ECX.
4575 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
4576 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
4577 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
4578 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
4579 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
4580 BuildMI(BB, Opcode, 0);
4581 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004582 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00004583 case ISD::WRITEPORT:
4584 if (Node->getOperand(2).getValueType() != MVT::i16) {
4585 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
4586 exit(1);
4587 }
4588 Select(Node->getOperand(0)); // Emit the chain.
4589
4590 Tmp1 = SelectExpr(Node->getOperand(1));
4591 switch (Node->getOperand(1).getValueType()) {
4592 case MVT::i8:
4593 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
4594 Tmp2 = X86::OUT8ir; Opc = X86::OUT8rr;
4595 break;
4596 case MVT::i16:
4597 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(Tmp1);
4598 Tmp2 = X86::OUT16ir; Opc = X86::OUT16rr;
4599 break;
4600 case MVT::i32:
4601 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4602 Tmp2 = X86::OUT32ir; Opc = X86::OUT32rr;
4603 break;
4604 default:
4605 std::cerr << "llvm.writeport: invalid data type for X86 target";
4606 exit(1);
4607 }
4608
4609 // If the port is a single-byte constant, use the immediate form.
4610 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(2)))
4611 if ((CN->getValue() & 255) == CN->getValue()) {
4612 BuildMI(BB, Tmp2, 1).addImm(CN->getValue());
4613 return;
4614 }
4615
4616 // Otherwise, move the I/O port address into the DX register.
4617 unsigned Reg = SelectExpr(Node->getOperand(2));
4618 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
4619 BuildMI(BB, Opc, 0);
4620 return;
4621 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004622 assert(0 && "Should not be reached!");
4623}
4624
4625
4626/// createX86PatternInstructionSelector - This pass converts an LLVM function
4627/// into a machine code representation using pattern matching and a machine
4628/// description file.
4629///
4630FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004631 return new ISel(TM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004632}