blob: 41bd222e110996cc3ab595adb3327d6502a01e5e [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
Chris Lattnerf7443da2005-07-29 00:54:34 +000057 /// FP_TO_INT*_IN_MEM - This instruction implements FP_TO_SINT with the
58 /// integer destination in memory and a FP reg source. This corresponds
59 /// to the X86::FIST*m instructions and the rounding mode change stuff. It
60 /// has two inputs (token chain and address) and two outputs (FP value and
61 /// token chain).
Chris Lattner01546c52005-07-30 00:05:54 +000062 FP_TO_INT16_IN_MEM,
63 FP_TO_INT32_IN_MEM,
Chris Lattnerf7443da2005-07-29 00:54:34 +000064 FP_TO_INT64_IN_MEM,
Jeff Cohend29b6aa2005-07-30 18:33:25 +000065
Chris Lattner239738a2005-05-14 08:48:15 +000066 /// CALL/TAILCALL - These operations represent an abstract X86 call
67 /// instruction, which includes a bunch of information. In particular the
68 /// operands of these node are:
69 ///
70 /// #0 - The incoming token chain
71 /// #1 - The callee
72 /// #2 - The number of arg bytes the caller pushes on the stack.
73 /// #3 - The number of arg bytes the callee pops off the stack.
74 /// #4 - The value to pass in AL/AX/EAX (optional)
75 /// #5 - The value to pass in DL/DX/EDX (optional)
76 ///
77 /// The result values of these nodes are:
78 ///
79 /// #0 - The outgoing token chain
80 /// #1 - The first register result value (optional)
81 /// #2 - The second register result value (optional)
82 ///
83 /// The CALL vs TAILCALL distinction boils down to whether the callee is
84 /// known not to modify the caller's stack frame, as is standard with
85 /// LLVM.
86 CALL,
87 TAILCALL,
Chris Lattner67649df2005-05-14 06:52:07 +000088 };
89 }
90}
91
Chris Lattner8acb1ba2005-01-07 07:49:41 +000092//===----------------------------------------------------------------------===//
93// X86TargetLowering - X86 Implementation of the TargetLowering interface
94namespace {
95 class X86TargetLowering : public TargetLowering {
96 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000097 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner381e8872005-05-15 05:46:45 +000098 int BytesToPopOnReturn; // Number of arg bytes ret should pop.
99 int BytesCallerReserves; // Number of arg bytes caller makes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000100 public:
101 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
102 // Set up the TargetLowering object.
Chris Lattner4df0de92005-01-17 00:00:33 +0000103
Chris Lattner653f7232005-05-13 22:46:57 +0000104 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Chris Lattner4df0de92005-01-17 00:00:33 +0000105 setShiftAmountType(MVT::i8);
106 setSetCCResultType(MVT::i8);
Chris Lattner6659bd72005-04-07 19:41:46 +0000107 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner009b55b2005-01-19 03:36:30 +0000108 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner4df0de92005-01-17 00:00:33 +0000109
110 // Set up the register classes.
Nate Begemanf63be7d2005-07-06 18:59:04 +0000111 // FIXME: Eliminate these two classes when legalize can handle promotions
112 // well.
113 addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000114 addRegisterClass(MVT::i8, X86::R8RegisterClass);
115 addRegisterClass(MVT::i16, X86::R16RegisterClass);
116 addRegisterClass(MVT::i32, X86::R32RegisterClass);
Jeff Cohen00b168892005-07-27 06:12:32 +0000117
Chris Lattnera28381c2005-07-16 00:28:20 +0000118 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
119 // operation.
120 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
121 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
122 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
123 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000124
125 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
126 // this operation.
127 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
128 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Jeff Cohen00b168892005-07-27 06:12:32 +0000129
Chris Lattner745d5382005-07-29 00:40:01 +0000130 if (!X86ScalarSSE) {
131 // We can handle SINT_TO_FP and FP_TO_SINT from/TO i64 even though i64
132 // isn't legal.
Chris Lattner01546c52005-07-30 00:05:54 +0000133 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
134 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
135 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
136 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Chris Lattner745d5382005-07-29 00:40:01 +0000137 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000138
Chris Lattner5d06b8c2005-07-29 01:00:29 +0000139 // Handle FP_TO_UINT by promoting the destination to a larger signed
140 // conversion.
141 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
142 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
143 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
144 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
145
146 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
147 // this operation.
148 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
149 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000150
Chris Lattnerda4d4692005-04-09 03:22:37 +0000151 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000152 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
153 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000154 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000155 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
156 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
157 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Chris Lattnerc610d422005-05-11 05:00:34 +0000158 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
159 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
160 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
161 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
162 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
163 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000164 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
165 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +0000166 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Jeff Cohen00b168892005-07-27 06:12:32 +0000167
Chris Lattner4e6ce5f2005-05-09 20:37:29 +0000168 setOperationAction(ISD::READIO , MVT::i1 , Expand);
169 setOperationAction(ISD::READIO , MVT::i8 , Expand);
170 setOperationAction(ISD::READIO , MVT::i16 , Expand);
171 setOperationAction(ISD::READIO , MVT::i32 , Expand);
172 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
173 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
174 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
175 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
Jeff Cohen00b168892005-07-27 06:12:32 +0000176
Chris Lattnerda2ce112005-01-16 07:34:08 +0000177 // These should be promoted to a larger select which is supported.
Nate Begemanf63be7d2005-07-06 18:59:04 +0000178 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000179 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Jeff Cohen00b168892005-07-27 06:12:32 +0000180
Nate Begemanf63be7d2005-07-06 18:59:04 +0000181 if (X86ScalarSSE) {
182 // Set up the FP register classes.
183 addRegisterClass(MVT::f32, X86::RXMMRegisterClass);
184 addRegisterClass(MVT::f64, X86::RXMMRegisterClass);
Jeff Cohen00b168892005-07-27 06:12:32 +0000185
Nate Begeman5a8441e2005-07-16 02:02:34 +0000186 // SSE has no load+extend ops
Nate Begemanf63be7d2005-07-06 18:59:04 +0000187 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
188 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000189
190 // SSE has no i16 to fp conversion, only i32
191 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
192
Nate Begemanf63be7d2005-07-06 18:59:04 +0000193 // We don't support sin/cos/sqrt/fmod
194 setOperationAction(ISD::FSIN , MVT::f64, Expand);
195 setOperationAction(ISD::FCOS , MVT::f64, Expand);
196 setOperationAction(ISD::FABS , MVT::f64, Expand);
197 setOperationAction(ISD::FNEG , MVT::f64, Expand);
198 setOperationAction(ISD::SREM , MVT::f64, Expand);
199 setOperationAction(ISD::FSIN , MVT::f32, Expand);
200 setOperationAction(ISD::FCOS , MVT::f32, Expand);
201 setOperationAction(ISD::FABS , MVT::f32, Expand);
202 setOperationAction(ISD::FNEG , MVT::f32, Expand);
203 setOperationAction(ISD::SREM , MVT::f32, Expand);
204 } else {
205 // Set up the FP register classes.
206 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Jeff Cohen00b168892005-07-27 06:12:32 +0000207
Nate Begemanf63be7d2005-07-06 18:59:04 +0000208 if (!UnsafeFPMath) {
209 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
210 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
211 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000212
Nate Begemanf63be7d2005-07-06 18:59:04 +0000213 addLegalFPImmediate(+0.0); // FLD0
214 addLegalFPImmediate(+1.0); // FLD1
215 addLegalFPImmediate(-0.0); // FLD0/FCHS
216 addLegalFPImmediate(-1.0); // FLD1/FCHS
217 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000218 computeRegisterProperties();
Reid Spencera0f5bf32005-07-19 04:52:44 +0000219
220 maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores
221 maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores
222 maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores
223 allowUnalignedStores = true; // x86 supports it!
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000224 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000225
Chris Lattner3648c672005-05-13 21:44:04 +0000226 // Return the number of bytes that a function should pop when it returns (in
227 // addition to the space used by the return address).
228 //
229 unsigned getBytesToPopOnReturn() const { return BytesToPopOnReturn; }
230
Chris Lattner381e8872005-05-15 05:46:45 +0000231 // Return the number of bytes that the caller reserves for arguments passed
232 // to this function.
233 unsigned getBytesCallerReserves() const { return BytesCallerReserves; }
234
Chris Lattner67649df2005-05-14 06:52:07 +0000235 /// LowerOperation - Provide custom lowering hooks for some operations.
236 ///
237 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
238
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000239 /// LowerArguments - This hook must be implemented to indicate how we should
240 /// lower the arguments for the specified function, into the specified DAG.
241 virtual std::vector<SDOperand>
242 LowerArguments(Function &F, SelectionDAG &DAG);
243
244 /// LowerCallTo - This hook lowers an abstract call to a function into an
245 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +0000246 virtual std::pair<SDOperand, SDOperand>
Jeff Cohen00b168892005-07-27 06:12:32 +0000247 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000248 bool isTailCall, SDOperand Callee, ArgListTy &Args,
249 SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000250
Chris Lattnere0fe2252005-07-05 19:58:54 +0000251 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
252 Value *VAListV, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000253 virtual std::pair<SDOperand,SDOperand>
Chris Lattnere0fe2252005-07-05 19:58:54 +0000254 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
255 const Type *ArgTy, SelectionDAG &DAG);
Jeff Cohen00b168892005-07-27 06:12:32 +0000256
Chris Lattner14824582005-01-09 00:01:27 +0000257 virtual std::pair<SDOperand, SDOperand>
258 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
259 SelectionDAG &DAG);
Chris Lattner381e8872005-05-15 05:46:45 +0000260
261 SDOperand getReturnAddressFrameIndex(SelectionDAG &DAG);
262
Chris Lattnerc6f41812005-05-12 23:06:28 +0000263 private:
264 // C Calling Convention implementation.
265 std::vector<SDOperand> LowerCCCArguments(Function &F, SelectionDAG &DAG);
266 std::pair<SDOperand, SDOperand>
267 LowerCCCCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
Chris Lattner2e7714a2005-05-13 20:29:13 +0000268 bool isTailCall,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000269 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Jeff Cohen00b168892005-07-27 06:12:32 +0000270
Chris Lattnerc6f41812005-05-12 23:06:28 +0000271 // Fast Calling Convention implementation.
272 std::vector<SDOperand> LowerFastCCArguments(Function &F, SelectionDAG &DAG);
273 std::pair<SDOperand, SDOperand>
Chris Lattner2e7714a2005-05-13 20:29:13 +0000274 LowerFastCCCallTo(SDOperand Chain, const Type *RetTy, bool isTailCall,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000275 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000276 };
277}
278
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000279std::vector<SDOperand>
280X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattnerc6f41812005-05-12 23:06:28 +0000281 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
282 return LowerFastCCArguments(F, DAG);
283 return LowerCCCArguments(F, DAG);
284}
285
286std::pair<SDOperand, SDOperand>
287X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
288 bool isVarArg, unsigned CallingConv,
Jeff Cohen00b168892005-07-27 06:12:32 +0000289 bool isTailCall,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000290 SDOperand Callee, ArgListTy &Args,
291 SelectionDAG &DAG) {
292 assert((!isVarArg || CallingConv == CallingConv::C) &&
293 "Only C takes varargs!");
294 if (CallingConv == CallingConv::Fast && EnableFastCC)
Chris Lattner2e7714a2005-05-13 20:29:13 +0000295 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
296 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000297}
298
299//===----------------------------------------------------------------------===//
Chris Lattner653f7232005-05-13 22:46:57 +0000300// C Calling Convention implementation
Chris Lattnerc6f41812005-05-12 23:06:28 +0000301//===----------------------------------------------------------------------===//
302
303std::vector<SDOperand>
304X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000305 std::vector<SDOperand> ArgValues;
306
Chris Lattner6415bb42005-05-10 03:53:18 +0000307 MachineFunction &MF = DAG.getMachineFunction();
308 MachineFrameInfo *MFI = MF.getFrameInfo();
309
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000310 // Add DAG nodes to load the arguments... On entry to a function on the X86,
311 // the stack frame looks like this:
312 //
313 // [ESP] -- return address
314 // [ESP + 4] -- first argument (leftmost lexically)
315 // [ESP + 8] -- second argument, if first argument is four bytes in size
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000316 // ...
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000317 //
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000318 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattnere4d5c442005-03-15 04:54:21 +0000319 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000320 MVT::ValueType ObjectVT = getValueType(I->getType());
321 unsigned ArgIncrement = 4;
322 unsigned ObjSize;
323 switch (ObjectVT) {
324 default: assert(0 && "Unhandled argument type!");
325 case MVT::i1:
326 case MVT::i8: ObjSize = 1; break;
327 case MVT::i16: ObjSize = 2; break;
328 case MVT::i32: ObjSize = 4; break;
329 case MVT::i64: ObjSize = ArgIncrement = 8; break;
330 case MVT::f32: ObjSize = 4; break;
331 case MVT::f64: ObjSize = ArgIncrement = 8; break;
332 }
333 // Create the frame index object for this incoming parameter...
334 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000335
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000336 // Create the SelectionDAG nodes corresponding to a load from this parameter
337 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
338
339 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
340 // dead loads.
341 SDOperand ArgValue;
342 if (!I->use_empty())
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000343 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
344 DAG.getSrcValue(NULL));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000345 else {
346 if (MVT::isInteger(ObjectVT))
347 ArgValue = DAG.getConstant(0, ObjectVT);
348 else
349 ArgValue = DAG.getConstantFP(0, ObjectVT);
350 }
351 ArgValues.push_back(ArgValue);
352
353 ArgOffset += ArgIncrement; // Move on to the next argument...
354 }
355
356 // If the function takes variable number of arguments, make a frame index for
357 // the start of the first vararg value... for expansion of llvm.va_start.
358 if (F.isVarArg())
359 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner3648c672005-05-13 21:44:04 +0000360 ReturnAddrIndex = 0; // No return address slot generated yet.
361 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattner381e8872005-05-15 05:46:45 +0000362 BytesCallerReserves = ArgOffset;
Chris Lattner4c52f0e2005-04-09 15:23:56 +0000363
364 // Finally, inform the code generator which regs we return values in.
365 switch (getValueType(F.getReturnType())) {
366 default: assert(0 && "Unknown type!");
367 case MVT::isVoid: break;
368 case MVT::i1:
369 case MVT::i8:
370 case MVT::i16:
371 case MVT::i32:
372 MF.addLiveOut(X86::EAX);
373 break;
374 case MVT::i64:
375 MF.addLiveOut(X86::EAX);
376 MF.addLiveOut(X86::EDX);
377 break;
378 case MVT::f32:
379 case MVT::f64:
380 MF.addLiveOut(X86::ST0);
381 break;
382 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000383 return ArgValues;
384}
385
Chris Lattner5188ad72005-01-08 19:28:19 +0000386std::pair<SDOperand, SDOperand>
Chris Lattnerc6f41812005-05-12 23:06:28 +0000387X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
Chris Lattner2e7714a2005-05-13 20:29:13 +0000388 bool isVarArg, bool isTailCall,
389 SDOperand Callee, ArgListTy &Args,
390 SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000391 // Count how many bytes are to be pushed on the stack.
392 unsigned NumBytes = 0;
393
394 if (Args.empty()) {
395 // Save zero bytes.
Chris Lattner16cd04d2005-05-12 23:24:06 +0000396 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattner5188ad72005-01-08 19:28:19 +0000397 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000398 } else {
399 for (unsigned i = 0, e = Args.size(); i != e; ++i)
400 switch (getValueType(Args[i].second)) {
401 default: assert(0 && "Unknown value type!");
402 case MVT::i1:
403 case MVT::i8:
404 case MVT::i16:
405 case MVT::i32:
406 case MVT::f32:
407 NumBytes += 4;
408 break;
409 case MVT::i64:
410 case MVT::f64:
411 NumBytes += 8;
412 break;
413 }
414
Chris Lattner16cd04d2005-05-12 23:24:06 +0000415 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattner5188ad72005-01-08 19:28:19 +0000416 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000417
418 // Arguments go on the stack in reverse order, as specified by the ABI.
419 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000420 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
421 DAG.getEntryNode());
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000422 std::vector<SDOperand> Stores;
423
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000424 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000425 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
426 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
427
428 switch (getValueType(Args[i].second)) {
429 default: assert(0 && "Unexpected ValueType for argument!");
430 case MVT::i1:
431 case MVT::i8:
432 case MVT::i16:
433 // Promote the integer to 32 bits. If the input type is signed use a
434 // sign extend, otherwise use a zero extend.
435 if (Args[i].second->isSigned())
436 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
437 else
438 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
439
440 // FALL THROUGH
441 case MVT::i32:
442 case MVT::f32:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000443 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000444 Args[i].first, PtrOff,
445 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000446 ArgOffset += 4;
447 break;
448 case MVT::i64:
449 case MVT::f64:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000450 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000451 Args[i].first, PtrOff,
452 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000453 ArgOffset += 8;
454 break;
455 }
456 }
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000457 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000458 }
459
460 std::vector<MVT::ValueType> RetVals;
461 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000462 RetVals.push_back(MVT::Other);
463
Chris Lattner239738a2005-05-14 08:48:15 +0000464 // The result values produced have to be legal. Promote the result.
465 switch (RetTyVT) {
466 case MVT::isVoid: break;
467 default:
468 RetVals.push_back(RetTyVT);
469 break;
470 case MVT::i1:
471 case MVT::i8:
472 case MVT::i16:
473 RetVals.push_back(MVT::i32);
474 break;
475 case MVT::f32:
Nate Begemanf63be7d2005-07-06 18:59:04 +0000476 if (X86ScalarSSE)
477 RetVals.push_back(MVT::f32);
478 else
479 RetVals.push_back(MVT::f64);
Chris Lattner239738a2005-05-14 08:48:15 +0000480 break;
481 case MVT::i64:
482 RetVals.push_back(MVT::i32);
483 RetVals.push_back(MVT::i32);
484 break;
485 }
486 std::vector<SDOperand> Ops;
487 Ops.push_back(Chain);
488 Ops.push_back(Callee);
489 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
490 Ops.push_back(DAG.getConstant(0, getPointerTy()));
491 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
492 RetVals, Ops);
493 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
494
495 SDOperand ResultVal;
496 switch (RetTyVT) {
497 case MVT::isVoid: break;
498 default:
499 ResultVal = TheCall.getValue(1);
500 break;
501 case MVT::i1:
502 case MVT::i8:
503 case MVT::i16:
504 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
505 break;
506 case MVT::f32:
507 // FIXME: we would really like to remember that this FP_ROUND operation is
508 // okay to eliminate if we allow excess FP precision.
509 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
510 break;
511 case MVT::i64:
512 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
513 TheCall.getValue(2));
514 break;
515 }
516
517 return std::make_pair(ResultVal, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000518}
519
Chris Lattnere0fe2252005-07-05 19:58:54 +0000520SDOperand
521X86TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
522 Value *VAListV, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000523 // vastart just stores the address of the VarArgsFrameIndex slot.
524 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000525 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
526 DAG.getSrcValue(VAListV));
Chris Lattner14824582005-01-09 00:01:27 +0000527}
528
Chris Lattnere0fe2252005-07-05 19:58:54 +0000529
530std::pair<SDOperand,SDOperand>
531X86TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP,
532 Value *VAListV, const Type *ArgTy,
533 SelectionDAG &DAG) {
Chris Lattner14824582005-01-09 00:01:27 +0000534 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000535 SDOperand Val = DAG.getLoad(MVT::i32, Chain,
536 VAListP, DAG.getSrcValue(VAListV));
537 SDOperand Result = DAG.getLoad(ArgVT, Chain, Val,
Chris Lattner08568cf2005-07-05 17:50:16 +0000538 DAG.getSrcValue(NULL));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000539 unsigned Amt;
540 if (ArgVT == MVT::i32)
541 Amt = 4;
542 else {
543 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
544 "Other types should have been promoted for varargs!");
545 Amt = 8;
Chris Lattner14824582005-01-09 00:01:27 +0000546 }
Andrew Lenharth558bc882005-06-18 18:34:52 +0000547 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
548 DAG.getConstant(Amt, Val.getValueType()));
549 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000550 Val, VAListP, DAG.getSrcValue(VAListV));
Chris Lattner14824582005-01-09 00:01:27 +0000551 return std::make_pair(Result, Chain);
552}
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000553
Chris Lattnerc6f41812005-05-12 23:06:28 +0000554//===----------------------------------------------------------------------===//
Chris Lattner653f7232005-05-13 22:46:57 +0000555// Fast Calling Convention implementation
Chris Lattnerc6f41812005-05-12 23:06:28 +0000556//===----------------------------------------------------------------------===//
557//
558// The X86 'fast' calling convention passes up to two integer arguments in
559// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
560// and requires that the callee pop its arguments off the stack (allowing proper
561// tail calls), and has the same return value conventions as C calling convs.
562//
Chris Lattner10d26452005-05-13 23:49:10 +0000563// This calling convention always arranges for the callee pop value to be 8n+4
564// bytes, which is needed for tail recursion elimination and stack alignment
565// reasons.
566//
Chris Lattnerc6f41812005-05-12 23:06:28 +0000567// Note that this can be enhanced in the future to pass fp vals in registers
568// (when we have a global fp allocator) and do other tricks.
569//
Chris Lattner63602fb2005-05-13 07:38:09 +0000570
571/// AddLiveIn - This helper function adds the specified physical register to the
572/// MachineFunction as a live in value. It also creates a corresponding virtual
573/// register for it.
574static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
575 TargetRegisterClass *RC) {
576 assert(RC->contains(PReg) && "Not the correct regclass!");
577 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
578 MF.addLiveIn(PReg, VReg);
579 return VReg;
580}
581
582
Chris Lattnerc6f41812005-05-12 23:06:28 +0000583std::vector<SDOperand>
584X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
585 std::vector<SDOperand> ArgValues;
586
587 MachineFunction &MF = DAG.getMachineFunction();
588 MachineFrameInfo *MFI = MF.getFrameInfo();
589
590 // Add DAG nodes to load the arguments... On entry to a function the stack
591 // frame looks like this:
592 //
593 // [ESP] -- return address
594 // [ESP + 4] -- first nonreg argument (leftmost lexically)
595 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
596 // ...
597 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
598
599 // Keep track of the number of integer regs passed so far. This can be either
600 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
601 // used).
602 unsigned NumIntRegs = 0;
603
604 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
605 MVT::ValueType ObjectVT = getValueType(I->getType());
606 unsigned ArgIncrement = 4;
607 unsigned ObjSize = 0;
608 SDOperand ArgValue;
Jeff Cohen00b168892005-07-27 06:12:32 +0000609
Chris Lattnerc6f41812005-05-12 23:06:28 +0000610 switch (ObjectVT) {
611 default: assert(0 && "Unhandled argument type!");
612 case MVT::i1:
613 case MVT::i8:
614 if (NumIntRegs < 2) {
615 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000616 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
617 X86::R8RegisterClass);
618 ArgValue = DAG.getCopyFromReg(VReg, MVT::i8, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000619 DAG.setRoot(ArgValue.getValue(1));
620 }
621 ++NumIntRegs;
622 break;
623 }
624
625 ObjSize = 1;
626 break;
627 case MVT::i16:
628 if (NumIntRegs < 2) {
629 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000630 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
631 X86::R16RegisterClass);
632 ArgValue = DAG.getCopyFromReg(VReg, MVT::i16, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000633 DAG.setRoot(ArgValue.getValue(1));
634 }
635 ++NumIntRegs;
636 break;
637 }
638 ObjSize = 2;
639 break;
640 case MVT::i32:
641 if (NumIntRegs < 2) {
642 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000643 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
644 X86::R32RegisterClass);
645 ArgValue = DAG.getCopyFromReg(VReg, MVT::i32, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000646 DAG.setRoot(ArgValue.getValue(1));
647 }
648 ++NumIntRegs;
649 break;
650 }
651 ObjSize = 4;
652 break;
653 case MVT::i64:
654 if (NumIntRegs == 0) {
655 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000656 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
657 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000658
Chris Lattner63602fb2005-05-13 07:38:09 +0000659 SDOperand Low=DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot());
660 SDOperand Hi =DAG.getCopyFromReg(TopReg, MVT::i32, Low.getValue(1));
Chris Lattnerc6f41812005-05-12 23:06:28 +0000661 DAG.setRoot(Hi.getValue(1));
662
663 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
664 }
665 NumIntRegs = 2;
666 break;
667 } else if (NumIntRegs == 1) {
668 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000669 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
670 SDOperand Low = DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000671 DAG.setRoot(Low.getValue(1));
672
673 // Load the high part from memory.
674 // Create the frame index object for this incoming parameter...
675 int FI = MFI->CreateFixedObject(4, ArgOffset);
676 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
677 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
678 DAG.getSrcValue(NULL));
679 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
680 }
681 ArgOffset += 4;
682 NumIntRegs = 2;
683 break;
684 }
685 ObjSize = ArgIncrement = 8;
686 break;
687 case MVT::f32: ObjSize = 4; break;
688 case MVT::f64: ObjSize = ArgIncrement = 8; break;
689 }
690
691 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
692 // dead loads.
693 if (ObjSize && !I->use_empty()) {
694 // Create the frame index object for this incoming parameter...
695 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
696
697 // Create the SelectionDAG nodes corresponding to a load from this
698 // parameter.
699 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
700
701 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
702 DAG.getSrcValue(NULL));
703 } else if (ArgValue.Val == 0) {
704 if (MVT::isInteger(ObjectVT))
705 ArgValue = DAG.getConstant(0, ObjectVT);
706 else
707 ArgValue = DAG.getConstantFP(0, ObjectVT);
708 }
709 ArgValues.push_back(ArgValue);
710
711 if (ObjSize)
712 ArgOffset += ArgIncrement; // Move on to the next argument.
713 }
714
Chris Lattner10d26452005-05-13 23:49:10 +0000715 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
716 // arguments and the arguments after the retaddr has been pushed are aligned.
717 if ((ArgOffset & 7) == 0)
718 ArgOffset += 4;
719
Chris Lattner3648c672005-05-13 21:44:04 +0000720 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
721 ReturnAddrIndex = 0; // No return address slot generated yet.
722 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
Chris Lattner381e8872005-05-15 05:46:45 +0000723 BytesCallerReserves = 0;
Chris Lattnerc6f41812005-05-12 23:06:28 +0000724
725 // Finally, inform the code generator which regs we return values in.
726 switch (getValueType(F.getReturnType())) {
727 default: assert(0 && "Unknown type!");
728 case MVT::isVoid: break;
729 case MVT::i1:
730 case MVT::i8:
731 case MVT::i16:
732 case MVT::i32:
733 MF.addLiveOut(X86::EAX);
734 break;
735 case MVT::i64:
736 MF.addLiveOut(X86::EAX);
737 MF.addLiveOut(X86::EDX);
738 break;
739 case MVT::f32:
740 case MVT::f64:
741 MF.addLiveOut(X86::ST0);
742 break;
743 }
744 return ArgValues;
745}
746
747std::pair<SDOperand, SDOperand>
748X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
Chris Lattner2e7714a2005-05-13 20:29:13 +0000749 bool isTailCall, SDOperand Callee,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000750 ArgListTy &Args, SelectionDAG &DAG) {
751 // Count how many bytes are to be pushed on the stack.
752 unsigned NumBytes = 0;
753
754 // Keep track of the number of integer regs passed so far. This can be either
755 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
756 // used).
757 unsigned NumIntRegs = 0;
758
759 for (unsigned i = 0, e = Args.size(); i != e; ++i)
760 switch (getValueType(Args[i].second)) {
761 default: assert(0 && "Unknown value type!");
762 case MVT::i1:
763 case MVT::i8:
764 case MVT::i16:
765 case MVT::i32:
766 if (NumIntRegs < 2) {
767 ++NumIntRegs;
768 break;
769 }
770 // fall through
771 case MVT::f32:
772 NumBytes += 4;
773 break;
774 case MVT::i64:
775 if (NumIntRegs == 0) {
776 NumIntRegs = 2;
777 break;
778 } else if (NumIntRegs == 1) {
779 NumIntRegs = 2;
780 NumBytes += 4;
781 break;
782 }
783
784 // fall through
785 case MVT::f64:
786 NumBytes += 8;
787 break;
788 }
789
Chris Lattner10d26452005-05-13 23:49:10 +0000790 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
791 // arguments and the arguments after the retaddr has been pushed are aligned.
792 if ((NumBytes & 7) == 0)
793 NumBytes += 4;
794
Chris Lattner16cd04d2005-05-12 23:24:06 +0000795 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000796 DAG.getConstant(NumBytes, getPointerTy()));
797
798 // Arguments go on the stack in reverse order, as specified by the ABI.
799 unsigned ArgOffset = 0;
800 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
801 DAG.getEntryNode());
802 NumIntRegs = 0;
803 std::vector<SDOperand> Stores;
804 std::vector<SDOperand> RegValuesToPass;
805 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
806 switch (getValueType(Args[i].second)) {
807 default: assert(0 && "Unexpected ValueType for argument!");
808 case MVT::i1:
809 case MVT::i8:
810 case MVT::i16:
811 case MVT::i32:
812 if (NumIntRegs < 2) {
813 RegValuesToPass.push_back(Args[i].first);
814 ++NumIntRegs;
815 break;
816 }
817 // Fall through
818 case MVT::f32: {
819 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
820 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
821 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
822 Args[i].first, PtrOff,
823 DAG.getSrcValue(NULL)));
824 ArgOffset += 4;
825 break;
826 }
827 case MVT::i64:
828 if (NumIntRegs < 2) { // Can pass part of it in regs?
829 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
830 Args[i].first, DAG.getConstant(1, MVT::i32));
831 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
832 Args[i].first, DAG.getConstant(0, MVT::i32));
833 RegValuesToPass.push_back(Lo);
834 ++NumIntRegs;
835 if (NumIntRegs < 2) { // Pass both parts in regs?
836 RegValuesToPass.push_back(Hi);
837 ++NumIntRegs;
838 } else {
839 // Pass the high part in memory.
840 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
841 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
842 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner920c0aa2005-05-14 12:03:10 +0000843 Hi, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattnerc6f41812005-05-12 23:06:28 +0000844 ArgOffset += 4;
845 }
846 break;
847 }
848 // Fall through
849 case MVT::f64:
850 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
851 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
852 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
853 Args[i].first, PtrOff,
854 DAG.getSrcValue(NULL)));
855 ArgOffset += 8;
856 break;
857 }
858 }
859 if (!Stores.empty())
860 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
861
Chris Lattner10d26452005-05-13 23:49:10 +0000862 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
863 // arguments and the arguments after the retaddr has been pushed are aligned.
864 if ((ArgOffset & 7) == 0)
865 ArgOffset += 4;
866
Chris Lattner239738a2005-05-14 08:48:15 +0000867 std::vector<MVT::ValueType> RetVals;
868 MVT::ValueType RetTyVT = getValueType(RetTy);
869
870 RetVals.push_back(MVT::Other);
871
872 // The result values produced have to be legal. Promote the result.
873 switch (RetTyVT) {
874 case MVT::isVoid: break;
875 default:
876 RetVals.push_back(RetTyVT);
877 break;
878 case MVT::i1:
879 case MVT::i8:
880 case MVT::i16:
881 RetVals.push_back(MVT::i32);
882 break;
883 case MVT::f32:
Nate Begemanf63be7d2005-07-06 18:59:04 +0000884 if (X86ScalarSSE)
885 RetVals.push_back(MVT::f32);
886 else
887 RetVals.push_back(MVT::f64);
Chris Lattner239738a2005-05-14 08:48:15 +0000888 break;
889 case MVT::i64:
890 RetVals.push_back(MVT::i32);
891 RetVals.push_back(MVT::i32);
892 break;
893 }
894
895 std::vector<SDOperand> Ops;
896 Ops.push_back(Chain);
897 Ops.push_back(Callee);
898 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
899 // Callee pops all arg values on the stack.
900 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
901
902 // Pass register arguments as needed.
903 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
904
905 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
906 RetVals, Ops);
907 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
908
909 SDOperand ResultVal;
910 switch (RetTyVT) {
911 case MVT::isVoid: break;
912 default:
913 ResultVal = TheCall.getValue(1);
914 break;
915 case MVT::i1:
916 case MVT::i8:
917 case MVT::i16:
918 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
919 break;
920 case MVT::f32:
921 // FIXME: we would really like to remember that this FP_ROUND operation is
922 // okay to eliminate if we allow excess FP precision.
923 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
924 break;
925 case MVT::i64:
926 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
927 TheCall.getValue(2));
928 break;
929 }
930
931 return std::make_pair(ResultVal, Chain);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000932}
933
Chris Lattner381e8872005-05-15 05:46:45 +0000934SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
935 if (ReturnAddrIndex == 0) {
936 // Set up a frame object for the return address.
937 MachineFunction &MF = DAG.getMachineFunction();
938 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
939 }
940
941 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
942}
Chris Lattnerc6f41812005-05-12 23:06:28 +0000943
944
Chris Lattner14824582005-01-09 00:01:27 +0000945
946std::pair<SDOperand, SDOperand> X86TargetLowering::
947LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
948 SelectionDAG &DAG) {
949 SDOperand Result;
950 if (Depth) // Depths > 0 not supported yet!
951 Result = DAG.getConstant(0, getPointerTy());
952 else {
Chris Lattner381e8872005-05-15 05:46:45 +0000953 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000954 if (!isFrameAddress)
955 // Just load the return address
Chris Lattnerc6f41812005-05-12 23:06:28 +0000956 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
957 DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000958 else
959 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
960 DAG.getConstant(4, MVT::i32));
961 }
962 return std::make_pair(Result, Chain);
963}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000964
Chris Lattnera28381c2005-07-16 00:28:20 +0000965//===----------------------------------------------------------------------===//
966// X86 Custom Lowering Hooks
967//===----------------------------------------------------------------------===//
968
Chris Lattner67649df2005-05-14 06:52:07 +0000969/// LowerOperation - Provide custom lowering hooks for some operations.
970///
971SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
972 switch (Op.getOpcode()) {
973 default: assert(0 && "Should not custom lower this!");
Chris Lattner745d5382005-07-29 00:40:01 +0000974 case ISD::SINT_TO_FP: {
Chris Lattner67649df2005-05-14 06:52:07 +0000975 assert(Op.getValueType() == MVT::f64 &&
976 Op.getOperand(0).getValueType() == MVT::i64 &&
977 "Unknown SINT_TO_FP to lower!");
978 // We lower sint64->FP into a store to a temporary stack slot, followed by a
979 // FILD64m node.
980 MachineFunction &MF = DAG.getMachineFunction();
981 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
982 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
983 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
984 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
985 std::vector<MVT::ValueType> RTs;
986 RTs.push_back(MVT::f64);
987 RTs.push_back(MVT::Other);
988 std::vector<SDOperand> Ops;
989 Ops.push_back(Store);
990 Ops.push_back(StackSlot);
991 return DAG.getNode(X86ISD::FILD64m, RTs, Ops);
992 }
Chris Lattner745d5382005-07-29 00:40:01 +0000993 case ISD::FP_TO_SINT: {
Chris Lattner01546c52005-07-30 00:05:54 +0000994 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattner745d5382005-07-29 00:40:01 +0000995 Op.getOperand(0).getValueType() == MVT::f64 &&
996 "Unknown FP_TO_SINT to lower!");
997 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
998 // stack slot.
999 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner01546c52005-07-30 00:05:54 +00001000 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1001 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Chris Lattner745d5382005-07-29 00:40:01 +00001002 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1003
Chris Lattner01546c52005-07-30 00:05:54 +00001004 unsigned Opc;
1005 switch (Op.getValueType()) {
1006 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1007 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1008 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1009 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1010 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001011
Chris Lattner01546c52005-07-30 00:05:54 +00001012 // Build the FP_TO_INT*_IN_MEM
Chris Lattner745d5382005-07-29 00:40:01 +00001013 std::vector<SDOperand> Ops;
1014 Ops.push_back(DAG.getEntryNode());
1015 Ops.push_back(Op.getOperand(0));
1016 Ops.push_back(StackSlot);
Chris Lattner01546c52005-07-30 00:05:54 +00001017 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001018
Chris Lattner745d5382005-07-29 00:40:01 +00001019 // Load the result.
Chris Lattner01546c52005-07-30 00:05:54 +00001020 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1021 DAG.getSrcValue(NULL));
Chris Lattner745d5382005-07-29 00:40:01 +00001022 }
1023 }
Chris Lattner67649df2005-05-14 06:52:07 +00001024}
1025
1026
1027//===----------------------------------------------------------------------===//
1028// Pattern Matcher Implementation
1029//===----------------------------------------------------------------------===//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001030
Chris Lattner98a8ba02005-01-18 01:06:26 +00001031namespace {
1032 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
1033 /// SDOperand's instead of register numbers for the leaves of the matched
1034 /// tree.
1035 struct X86ISelAddressMode {
1036 enum {
1037 RegBase,
1038 FrameIndexBase,
1039 } BaseType;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001040
Chris Lattner98a8ba02005-01-18 01:06:26 +00001041 struct { // This is really a union, discriminated by BaseType!
1042 SDOperand Reg;
1043 int FrameIndex;
1044 } Base;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001045
Chris Lattner98a8ba02005-01-18 01:06:26 +00001046 unsigned Scale;
1047 SDOperand IndexReg;
1048 unsigned Disp;
1049 GlobalValue *GV;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001050
Chris Lattner98a8ba02005-01-18 01:06:26 +00001051 X86ISelAddressMode()
1052 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
1053 }
1054 };
1055}
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001056
1057
1058namespace {
1059 Statistic<>
1060 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
1061
1062 //===--------------------------------------------------------------------===//
1063 /// ISel - X86 specific code to select X86 machine instructions for
1064 /// SelectionDAG operations.
1065 ///
1066 class ISel : public SelectionDAGISel {
1067 /// ContainsFPCode - Every instruction we select that uses or defines a FP
1068 /// register should set this to true.
1069 bool ContainsFPCode;
1070
1071 /// X86Lowering - This object fully describes how to lower LLVM code to an
1072 /// X86-specific SelectionDAG.
1073 X86TargetLowering X86Lowering;
1074
Chris Lattner11333092005-01-11 03:11:44 +00001075 /// RegPressureMap - This keeps an approximate count of the number of
1076 /// registers required to evaluate each node in the graph.
1077 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001078
1079 /// ExprMap - As shared expressions are codegen'd, we keep track of which
1080 /// vreg the value is produced in, so we only emit one copy of each compiled
1081 /// tree.
1082 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001083
Chris Lattner381e8872005-05-15 05:46:45 +00001084 /// TheDAG - The DAG being selected during Select* operations.
1085 SelectionDAG *TheDAG;
Jeff Cohen00b168892005-07-27 06:12:32 +00001086
1087 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
Nate Begemanfb5792f2005-07-12 01:41:54 +00001088 /// make the right decision when generating code for different targets.
1089 const X86Subtarget *Subtarget;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001090 public:
1091 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
Nate Begemanfb5792f2005-07-12 01:41:54 +00001092 Subtarget = TM.getSubtarget<const X86Subtarget>();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001093 }
1094
Chris Lattner67b1c3c2005-01-21 21:35:14 +00001095 virtual const char *getPassName() const {
1096 return "X86 Pattern Instruction Selection";
1097 }
1098
Chris Lattner11333092005-01-11 03:11:44 +00001099 unsigned getRegPressure(SDOperand O) {
1100 return RegPressureMap[O.Val];
1101 }
1102 unsigned ComputeRegPressure(SDOperand O);
1103
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001104 /// InstructionSelectBasicBlock - This callback is invoked by
1105 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001106 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001107
Chris Lattner63602fb2005-05-13 07:38:09 +00001108 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
1109
Chris Lattner44129b52005-01-25 20:03:11 +00001110 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
1111 bool FloatPromoteOk = false);
Chris Lattnera5ade062005-01-11 21:19:59 +00001112 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +00001113 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattner30ea1e92005-01-19 07:37:26 +00001114 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001115 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +00001116 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001117 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
1118 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001119 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001120
1121 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
1122 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
1123 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner381e8872005-05-15 05:46:45 +00001124 bool EmitPotentialTailCall(SDNode *Node);
1125 void EmitFastCCToFastCCTailCall(SDNode *TailCallNode);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001126 void Select(SDOperand N);
1127 };
1128}
1129
Chris Lattner6415bb42005-05-10 03:53:18 +00001130/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
1131/// the main function.
1132static void EmitSpecialCodeForMain(MachineBasicBlock *BB,
1133 MachineFrameInfo *MFI) {
1134 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
1135 int CWFrameIdx = MFI->CreateStackObject(2, 2);
1136 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1137
1138 // Set the high part to be 64-bit precision.
1139 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1140 CWFrameIdx, 1).addImm(2);
1141
1142 // Reload the modified control word now.
1143 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1144}
1145
Chris Lattner63602fb2005-05-13 07:38:09 +00001146void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
1147 // If this function has live-in values, emit the copies from pregs to vregs at
1148 // the top of the function, before anything else.
1149 MachineBasicBlock *BB = MF.begin();
1150 if (MF.livein_begin() != MF.livein_end()) {
1151 SSARegMap *RegMap = MF.getSSARegMap();
1152 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
1153 E = MF.livein_end(); LI != E; ++LI) {
1154 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
1155 if (RC == X86::R8RegisterClass) {
1156 BuildMI(BB, X86::MOV8rr, 1, LI->second).addReg(LI->first);
1157 } else if (RC == X86::R16RegisterClass) {
1158 BuildMI(BB, X86::MOV16rr, 1, LI->second).addReg(LI->first);
1159 } else if (RC == X86::R32RegisterClass) {
1160 BuildMI(BB, X86::MOV32rr, 1, LI->second).addReg(LI->first);
1161 } else if (RC == X86::RFPRegisterClass) {
1162 BuildMI(BB, X86::FpMOV, 1, LI->second).addReg(LI->first);
Nate Begemanf63be7d2005-07-06 18:59:04 +00001163 } else if (RC == X86::RXMMRegisterClass) {
1164 BuildMI(BB, X86::MOVAPDrr, 1, LI->second).addReg(LI->first);
Chris Lattner63602fb2005-05-13 07:38:09 +00001165 } else {
1166 assert(0 && "Unknown regclass!");
1167 }
1168 }
1169 }
1170
1171
1172 // If this is main, emit special code for main.
1173 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
1174 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
1175}
1176
1177
Chris Lattner7dbcb752005-01-12 04:21:28 +00001178/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
1179/// when it has created a SelectionDAG for us to codegen.
1180void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
1181 // While we're doing this, keep track of whether we see any FP code for
1182 // FP_REG_KILL insertion.
1183 ContainsFPCode = false;
Chris Lattner6415bb42005-05-10 03:53:18 +00001184 MachineFunction *MF = BB->getParent();
Chris Lattner7dbcb752005-01-12 04:21:28 +00001185
1186 // Scan the PHI nodes that already are inserted into this basic block. If any
1187 // of them is a PHI of a floating point value, we need to insert an
1188 // FP_REG_KILL.
Chris Lattner6415bb42005-05-10 03:53:18 +00001189 SSARegMap *RegMap = MF->getSSARegMap();
Chris Lattner63602fb2005-05-13 07:38:09 +00001190 if (BB != MF->begin())
1191 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
1192 I != E; ++I) {
1193 assert(I->getOpcode() == X86::PHI &&
1194 "Isn't just PHI nodes?");
1195 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
1196 X86::RFPRegisterClass) {
1197 ContainsFPCode = true;
1198 break;
1199 }
Chris Lattner7dbcb752005-01-12 04:21:28 +00001200 }
Chris Lattner6415bb42005-05-10 03:53:18 +00001201
Chris Lattner7dbcb752005-01-12 04:21:28 +00001202 // Compute the RegPressureMap, which is an approximation for the number of
1203 // registers required to compute each node.
1204 ComputeRegPressure(DAG.getRoot());
1205
Chris Lattner381e8872005-05-15 05:46:45 +00001206 TheDAG = &DAG;
1207
Chris Lattner7dbcb752005-01-12 04:21:28 +00001208 // Codegen the basic block.
1209 Select(DAG.getRoot());
1210
Chris Lattner381e8872005-05-15 05:46:45 +00001211 TheDAG = 0;
1212
Chris Lattner7dbcb752005-01-12 04:21:28 +00001213 // Finally, look at all of the successors of this block. If any contain a PHI
1214 // node of FP type, we need to insert an FP_REG_KILL in this block.
1215 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
1216 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
1217 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
1218 I != E && I->getOpcode() == X86::PHI; ++I) {
1219 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
1220 X86::RFPRegisterClass) {
1221 ContainsFPCode = true;
1222 break;
1223 }
1224 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001225
Chris Lattnere3e0f272005-05-09 03:36:39 +00001226 // Final check, check LLVM BB's that are successors to the LLVM BB
1227 // corresponding to BB for FP PHI nodes.
1228 const BasicBlock *LLVMBB = BB->getBasicBlock();
1229 const PHINode *PN;
1230 if (!ContainsFPCode)
1231 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
1232 SI != E && !ContainsFPCode; ++SI)
1233 for (BasicBlock::const_iterator II = SI->begin();
1234 (PN = dyn_cast<PHINode>(II)); ++II)
1235 if (PN->getType()->isFloatingPoint()) {
1236 ContainsFPCode = true;
1237 break;
1238 }
1239
1240
Chris Lattner7dbcb752005-01-12 04:21:28 +00001241 // Insert FP_REG_KILL instructions into basic blocks that need them. This
1242 // only occurs due to the floating point stackifier not being aggressive
1243 // enough to handle arbitrary global stackification.
1244 //
1245 // Currently we insert an FP_REG_KILL instruction into each block that uses or
1246 // defines a floating point virtual register.
1247 //
1248 // When the global register allocators (like linear scan) finally update live
1249 // variable analysis, we can keep floating point values in registers across
1250 // basic blocks. This will be a huge win, but we are waiting on the global
1251 // allocators before we can do this.
1252 //
Chris Lattner71df3f82005-03-30 01:10:00 +00001253 if (ContainsFPCode) {
Chris Lattner7dbcb752005-01-12 04:21:28 +00001254 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
1255 ++NumFPKill;
1256 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001257
Chris Lattner7dbcb752005-01-12 04:21:28 +00001258 // Clear state used for selection.
1259 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +00001260 RegPressureMap.clear();
1261}
1262
1263
Chris Lattner11333092005-01-11 03:11:44 +00001264// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
1265// for the number of registers required to compute each node. This is basically
1266// computing a generalized form of the Sethi-Ullman number for each node.
1267unsigned ISel::ComputeRegPressure(SDOperand O) {
1268 SDNode *N = O.Val;
1269 unsigned &Result = RegPressureMap[N];
1270 if (Result) return Result;
1271
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001272 // FIXME: Should operations like CALL (which clobber lots o regs) have a
1273 // higher fixed cost??
1274
Chris Lattnerc4b6a782005-01-11 22:29:12 +00001275 if (N->getNumOperands() == 0) {
1276 Result = 1;
1277 } else {
1278 unsigned MaxRegUse = 0;
1279 unsigned NumExtraMaxRegUsers = 0;
1280 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1281 unsigned Regs;
1282 if (N->getOperand(i).getOpcode() == ISD::Constant)
1283 Regs = 0;
1284 else
1285 Regs = ComputeRegPressure(N->getOperand(i));
1286 if (Regs > MaxRegUse) {
1287 MaxRegUse = Regs;
1288 NumExtraMaxRegUsers = 0;
1289 } else if (Regs == MaxRegUse &&
1290 N->getOperand(i).getValueType() != MVT::Other) {
1291 ++NumExtraMaxRegUsers;
1292 }
Chris Lattner11333092005-01-11 03:11:44 +00001293 }
Chris Lattner90d1be72005-01-17 22:56:09 +00001294
1295 if (O.getOpcode() != ISD::TokenFactor)
1296 Result = MaxRegUse+NumExtraMaxRegUsers;
1297 else
Chris Lattner869e0432005-01-17 23:02:13 +00001298 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +00001299 }
Chris Lattnerafce4302005-01-12 02:19:06 +00001300
Chris Lattner837caa72005-01-11 23:21:30 +00001301 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +00001302 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001303}
1304
Chris Lattnerbf52d492005-01-20 16:50:16 +00001305/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
1306/// The DAG cannot have cycles in it, by definition, so the visited set is not
1307/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
1308/// reuse, so it prevents exponential cases.
1309///
1310static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
1311 std::set<SDNode*> &Visited) {
1312 if (N == Op) return true; // Found it.
1313 SDNode *Node = N.Val;
Chris Lattnerfb0f53f2005-01-21 21:43:02 +00001314 if (Node->getNumOperands() == 0 || // Leaf?
1315 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattnerbf52d492005-01-20 16:50:16 +00001316 if (!Visited.insert(Node).second) return false; // Already visited?
1317
1318 // Recurse for the first N-1 operands.
1319 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1320 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
1321 return true;
1322
1323 // Tail recurse for the last operand.
1324 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
1325}
1326
Chris Lattner98a8ba02005-01-18 01:06:26 +00001327X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
1328 X86AddressMode Result;
1329
1330 // If we need to emit two register operands, emit the one with the highest
1331 // register pressure first.
1332 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
1333 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +00001334 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +00001335 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +00001336 std::set<SDNode*> Visited;
1337 EmitBaseThenIndex = true;
1338 // If Base ends up pointing to Index, we must emit index first. This is
1339 // because of the way we fold loads, we may end up doing bad things with
1340 // the folded add.
1341 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
1342 EmitBaseThenIndex = false;
1343 } else {
1344 std::set<SDNode*> Visited;
1345 EmitBaseThenIndex = false;
1346 // If Base ends up pointing to Index, we must emit index first. This is
1347 // because of the way we fold loads, we may end up doing bad things with
1348 // the folded add.
1349 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
1350 EmitBaseThenIndex = true;
1351 }
1352
1353 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001354 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1355 Result.IndexReg = SelectExpr(IAM.IndexReg);
1356 } else {
1357 Result.IndexReg = SelectExpr(IAM.IndexReg);
1358 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1359 }
Chris Lattnerbf52d492005-01-20 16:50:16 +00001360
Chris Lattner98a8ba02005-01-18 01:06:26 +00001361 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
1362 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1363 } else if (IAM.IndexReg.Val) {
1364 Result.IndexReg = SelectExpr(IAM.IndexReg);
1365 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001366
Chris Lattner98a8ba02005-01-18 01:06:26 +00001367 switch (IAM.BaseType) {
1368 case X86ISelAddressMode::RegBase:
1369 Result.BaseType = X86AddressMode::RegBase;
1370 break;
1371 case X86ISelAddressMode::FrameIndexBase:
1372 Result.BaseType = X86AddressMode::FrameIndexBase;
1373 Result.Base.FrameIndex = IAM.Base.FrameIndex;
1374 break;
1375 default:
1376 assert(0 && "Unknown base type!");
1377 break;
1378 }
1379 Result.Scale = IAM.Scale;
1380 Result.Disp = IAM.Disp;
1381 Result.GV = IAM.GV;
1382 return Result;
1383}
1384
1385/// SelectAddress - Pattern match the maximal addressing mode for this node and
1386/// emit all of the leaf registers.
1387void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
1388 X86ISelAddressMode IAM;
1389 MatchAddress(N, IAM);
1390 AM = SelectAddrExprs(IAM);
1391}
1392
1393/// MatchAddress - Add the specified node to the specified addressing mode,
1394/// returning true if it cannot be done. This just pattern matches for the
1395/// addressing mode, it does not cause any code to be emitted. For that, use
1396/// SelectAddress.
1397bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001398 switch (N.getOpcode()) {
1399 default: break;
1400 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +00001401 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
1402 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001403 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
1404 return false;
1405 }
1406 break;
1407 case ISD::GlobalAddress:
1408 if (AM.GV == 0) {
Nate Begemanfb5792f2005-07-12 01:41:54 +00001409 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1410 // For Darwin, external and weak symbols are indirect, so we want to load
1411 // the value at address GV, not the value of GV itself. This means that
1412 // the GlobalAddress must be in the base or index register of the address,
1413 // not the GV offset field.
Jeff Cohen00b168892005-07-27 06:12:32 +00001414 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemanfb5792f2005-07-12 01:41:54 +00001415 (GV->hasWeakLinkage() || GV->isExternal())) {
1416 break;
1417 } else {
1418 AM.GV = GV;
1419 return false;
1420 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001421 }
1422 break;
1423 case ISD::Constant:
1424 AM.Disp += cast<ConstantSDNode>(N)->getValue();
1425 return false;
1426 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +00001427 // We might have folded the load into this shift, so don't regen the value
1428 // if so.
1429 if (ExprMap.count(N)) break;
1430
Chris Lattner98a8ba02005-01-18 01:06:26 +00001431 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001432 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
1433 unsigned Val = CN->getValue();
1434 if (Val == 1 || Val == 2 || Val == 3) {
1435 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +00001436 SDOperand ShVal = N.Val->getOperand(0);
1437
1438 // Okay, we know that we have a scale by now. However, if the scaled
1439 // value is an add of something and a constant, we can fold the
1440 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +00001441 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +00001442 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001443 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +00001444 ConstantSDNode *AddVal =
1445 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
1446 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +00001447 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001448 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +00001449 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001450 return false;
1451 }
1452 }
1453 break;
Chris Lattner947d5442005-01-11 19:37:02 +00001454 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +00001455 // We might have folded the load into this mul, so don't regen the value if
1456 // so.
1457 if (ExprMap.count(N)) break;
1458
Chris Lattner947d5442005-01-11 19:37:02 +00001459 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +00001460 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
1461 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +00001462 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
1463 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
1464 AM.Scale = unsigned(CN->getValue())-1;
1465
1466 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001467 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +00001468
1469 // Okay, we know that we have a scale by now. However, if the scaled
1470 // value is an add of something and a constant, we can fold the
1471 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +00001472 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +00001473 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001474 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +00001475 ConstantSDNode *AddVal =
1476 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
1477 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001478 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001479 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +00001480 }
1481
1482 AM.IndexReg = AM.Base.Reg = Reg;
1483 return false;
1484 }
1485 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001486
1487 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +00001488 // We might have folded the load into this mul, so don't regen the value if
1489 // so.
1490 if (ExprMap.count(N)) break;
1491
Chris Lattner98a8ba02005-01-18 01:06:26 +00001492 X86ISelAddressMode Backup = AM;
1493 if (!MatchAddress(N.Val->getOperand(0), AM) &&
1494 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001495 return false;
1496 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +00001497 if (!MatchAddress(N.Val->getOperand(1), AM) &&
1498 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +00001499 return false;
1500 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001501 break;
1502 }
1503 }
1504
Chris Lattnera95589b2005-01-11 04:40:19 +00001505 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +00001506 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +00001507 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +00001508 if (AM.IndexReg.Val == 0) {
1509 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +00001510 AM.Scale = 1;
1511 return false;
1512 }
1513
1514 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001515 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +00001516 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001517
1518 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +00001519 AM.BaseType = X86ISelAddressMode::RegBase;
1520 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001521 return false;
1522}
1523
1524/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
1525/// assuming that the temporary registers are in the 8-bit register class.
1526///
1527/// Tmp1 = setcc1
1528/// Tmp2 = setcc2
1529/// DestReg = logicalop Tmp1, Tmp2
1530///
1531static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
1532 unsigned SetCC2, unsigned LogicalOp,
1533 unsigned DestReg) {
1534 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
1535 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1536 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1537 BuildMI(BB, SetCC1, 0, Tmp1);
1538 BuildMI(BB, SetCC2, 0, Tmp2);
1539 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
1540}
1541
1542/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
1543/// condition codes match the specified SetCCOpcode. Note that some conditions
1544/// require multiple instructions to generate the correct value.
1545static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
1546 ISD::CondCode SetCCOpcode, bool isFP) {
1547 unsigned Opc;
1548 if (!isFP) {
1549 switch (SetCCOpcode) {
1550 default: assert(0 && "Illegal integer SetCC!");
1551 case ISD::SETEQ: Opc = X86::SETEr; break;
1552 case ISD::SETGT: Opc = X86::SETGr; break;
1553 case ISD::SETGE: Opc = X86::SETGEr; break;
1554 case ISD::SETLT: Opc = X86::SETLr; break;
1555 case ISD::SETLE: Opc = X86::SETLEr; break;
1556 case ISD::SETNE: Opc = X86::SETNEr; break;
1557 case ISD::SETULT: Opc = X86::SETBr; break;
1558 case ISD::SETUGT: Opc = X86::SETAr; break;
1559 case ISD::SETULE: Opc = X86::SETBEr; break;
1560 case ISD::SETUGE: Opc = X86::SETAEr; break;
1561 }
1562 } else {
1563 // On a floating point condition, the flags are set as follows:
1564 // ZF PF CF op
1565 // 0 | 0 | 0 | X > Y
1566 // 0 | 0 | 1 | X < Y
1567 // 1 | 0 | 0 | X == Y
1568 // 1 | 1 | 1 | unordered
1569 //
1570 switch (SetCCOpcode) {
1571 default: assert(0 && "Invalid FP setcc!");
1572 case ISD::SETUEQ:
1573 case ISD::SETEQ:
1574 Opc = X86::SETEr; // True if ZF = 1
1575 break;
1576 case ISD::SETOGT:
1577 case ISD::SETGT:
1578 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
1579 break;
1580 case ISD::SETOGE:
1581 case ISD::SETGE:
1582 Opc = X86::SETAEr; // True if CF = 0
1583 break;
1584 case ISD::SETULT:
1585 case ISD::SETLT:
1586 Opc = X86::SETBr; // True if CF = 1
1587 break;
1588 case ISD::SETULE:
1589 case ISD::SETLE:
1590 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
1591 break;
1592 case ISD::SETONE:
1593 case ISD::SETNE:
1594 Opc = X86::SETNEr; // True if ZF = 0
1595 break;
1596 case ISD::SETUO:
1597 Opc = X86::SETPr; // True if PF = 1
1598 break;
1599 case ISD::SETO:
1600 Opc = X86::SETNPr; // True if PF = 0
1601 break;
1602 case ISD::SETOEQ: // !PF & ZF
1603 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
1604 return;
1605 case ISD::SETOLT: // !PF & CF
1606 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
1607 return;
1608 case ISD::SETOLE: // !PF & (CF || ZF)
1609 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
1610 return;
1611 case ISD::SETUGT: // PF | (!ZF & !CF)
1612 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
1613 return;
1614 case ISD::SETUGE: // PF | !CF
1615 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
1616 return;
1617 case ISD::SETUNE: // PF | !ZF
1618 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
1619 return;
1620 }
1621 }
1622 BuildMI(BB, Opc, 0, DestReg);
1623}
1624
1625
1626/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
1627/// the Dest block if the Cond condition is true. If we cannot fold this
1628/// condition into the branch, return true.
1629///
Chris Lattner6c07aee2005-01-11 04:06:27 +00001630bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
1631 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001632 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
1633 // B) using two conditional branches instead of one condbr, two setcc's, and
1634 // an or.
1635 if ((Cond.getOpcode() == ISD::OR ||
1636 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
1637 // And and or set the flags for us, so there is no need to emit a TST of the
1638 // result. It is only safe to do this if there is only a single use of the
1639 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +00001640 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001641 SelectExpr(Cond);
1642 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
1643 return false;
1644 }
1645
1646 // Codegen br not C -> JE.
1647 if (Cond.getOpcode() == ISD::XOR)
1648 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
1649 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +00001650 unsigned CondR;
1651 if (getRegPressure(Chain) > getRegPressure(Cond)) {
1652 Select(Chain);
1653 CondR = SelectExpr(Cond.Val->getOperand(0));
1654 } else {
1655 CondR = SelectExpr(Cond.Val->getOperand(0));
1656 Select(Chain);
1657 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001658 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
1659 BuildMI(BB, X86::JE, 1).addMBB(Dest);
1660 return false;
1661 }
1662
1663 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
1664 if (SetCC == 0)
1665 return true; // Can only handle simple setcc's so far.
1666
1667 unsigned Opc;
1668
1669 // Handle integer conditions first.
1670 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1671 switch (SetCC->getCondition()) {
1672 default: assert(0 && "Illegal integer SetCC!");
1673 case ISD::SETEQ: Opc = X86::JE; break;
1674 case ISD::SETGT: Opc = X86::JG; break;
1675 case ISD::SETGE: Opc = X86::JGE; break;
1676 case ISD::SETLT: Opc = X86::JL; break;
1677 case ISD::SETLE: Opc = X86::JLE; break;
1678 case ISD::SETNE: Opc = X86::JNE; break;
1679 case ISD::SETULT: Opc = X86::JB; break;
1680 case ISD::SETUGT: Opc = X86::JA; break;
1681 case ISD::SETULE: Opc = X86::JBE; break;
1682 case ISD::SETUGE: Opc = X86::JAE; break;
1683 }
Chris Lattner6c07aee2005-01-11 04:06:27 +00001684 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001685 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001686 BuildMI(BB, Opc, 1).addMBB(Dest);
1687 return false;
1688 }
1689
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001690 unsigned Opc2 = 0; // Second branch if needed.
1691
1692 // On a floating point condition, the flags are set as follows:
1693 // ZF PF CF op
1694 // 0 | 0 | 0 | X > Y
1695 // 0 | 0 | 1 | X < Y
1696 // 1 | 0 | 0 | X == Y
1697 // 1 | 1 | 1 | unordered
1698 //
1699 switch (SetCC->getCondition()) {
1700 default: assert(0 && "Invalid FP setcc!");
1701 case ISD::SETUEQ:
1702 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
1703 case ISD::SETOGT:
1704 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
1705 case ISD::SETOGE:
1706 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
1707 case ISD::SETULT:
1708 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
1709 case ISD::SETULE:
1710 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
1711 case ISD::SETONE:
1712 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
1713 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
1714 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
1715 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1716 Opc = X86::JA; // ZF = 0 & CF = 0
1717 Opc2 = X86::JP; // PF = 1
1718 break;
1719 case ISD::SETUGE: // PF = 1 | CF = 0
1720 Opc = X86::JAE; // CF = 0
1721 Opc2 = X86::JP; // PF = 1
1722 break;
1723 case ISD::SETUNE: // PF = 1 | ZF = 0
1724 Opc = X86::JNE; // ZF = 0
1725 Opc2 = X86::JP; // PF = 1
1726 break;
1727 case ISD::SETOEQ: // PF = 0 & ZF = 1
1728 //X86::JNP, X86::JE
1729 //X86::AND8rr
1730 return true; // FIXME: Emit more efficient code for this branch.
1731 case ISD::SETOLT: // PF = 0 & CF = 1
1732 //X86::JNP, X86::JB
1733 //X86::AND8rr
1734 return true; // FIXME: Emit more efficient code for this branch.
1735 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1736 //X86::JNP, X86::JBE
1737 //X86::AND8rr
1738 return true; // FIXME: Emit more efficient code for this branch.
1739 }
1740
Chris Lattner6c07aee2005-01-11 04:06:27 +00001741 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001742 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001743 BuildMI(BB, Opc, 1).addMBB(Dest);
1744 if (Opc2)
1745 BuildMI(BB, Opc2, 1).addMBB(Dest);
1746 return false;
1747}
1748
Chris Lattner24aad1b2005-01-10 22:10:13 +00001749/// EmitSelectCC - Emit code into BB that performs a select operation between
1750/// the two registers RTrue and RFalse, generating a result into RDest. Return
1751/// true if the fold cannot be performed.
1752///
1753void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
1754 unsigned RTrue, unsigned RFalse, unsigned RDest) {
1755 enum Condition {
1756 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
1757 NOT_SET
1758 } CondCode = NOT_SET;
1759
1760 static const unsigned CMOVTAB16[] = {
1761 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
1762 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001763 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001764 };
1765 static const unsigned CMOVTAB32[] = {
1766 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
1767 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001768 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001769 };
1770 static const unsigned CMOVTABFP[] = {
1771 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
1772 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
1773 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
1774 };
Nate Begeman16b04f32005-07-15 00:38:55 +00001775 static const int SSE_CMOVTAB[] = {
Nate Begemanf63be7d2005-07-06 18:59:04 +00001776 0 /* CMPEQSS */, 4 /* CMPNEQSS */, 1 /* CMPLTSS */, 2 /* CMPLESS */,
Nate Begeman16b04f32005-07-15 00:38:55 +00001777 1 /* CMPLTSS */, 2 /* CMPLESS */, /*missing*/0, /*missing*/0,
Nate Begemanf63be7d2005-07-06 18:59:04 +00001778 /*missing*/0, /*missing*/0, /*missing*/0, /*missing*/0
1779 };
Chris Lattner24aad1b2005-01-10 22:10:13 +00001780
1781 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
1782 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1783 switch (SetCC->getCondition()) {
1784 default: assert(0 && "Unknown integer comparison!");
1785 case ISD::SETEQ: CondCode = EQ; break;
1786 case ISD::SETGT: CondCode = GT; break;
1787 case ISD::SETGE: CondCode = GE; break;
1788 case ISD::SETLT: CondCode = LT; break;
1789 case ISD::SETLE: CondCode = LE; break;
1790 case ISD::SETNE: CondCode = NE; break;
1791 case ISD::SETULT: CondCode = B; break;
1792 case ISD::SETUGT: CondCode = A; break;
1793 case ISD::SETULE: CondCode = BE; break;
1794 case ISD::SETUGE: CondCode = AE; break;
1795 }
Nate Begemanf63be7d2005-07-06 18:59:04 +00001796 } else if (X86ScalarSSE) {
1797 switch (SetCC->getCondition()) {
1798 default: assert(0 && "Unknown scalar fp comparison!");
1799 case ISD::SETEQ: CondCode = EQ; break;
1800 case ISD::SETNE: CondCode = NE; break;
1801 case ISD::SETULT:
1802 case ISD::SETLT: CondCode = LT; break;
1803 case ISD::SETULE:
1804 case ISD::SETLE: CondCode = LE; break;
1805 case ISD::SETUGT:
1806 case ISD::SETGT: CondCode = GT; break;
1807 case ISD::SETUGE:
1808 case ISD::SETGE: CondCode = GE; break;
1809 }
Chris Lattner24aad1b2005-01-10 22:10:13 +00001810 } else {
1811 // On a floating point condition, the flags are set as follows:
1812 // ZF PF CF op
1813 // 0 | 0 | 0 | X > Y
1814 // 0 | 0 | 1 | X < Y
1815 // 1 | 0 | 0 | X == Y
1816 // 1 | 1 | 1 | unordered
1817 //
1818 switch (SetCC->getCondition()) {
1819 default: assert(0 && "Unknown FP comparison!");
1820 case ISD::SETUEQ:
1821 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
1822 case ISD::SETOGT:
1823 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
1824 case ISD::SETOGE:
1825 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
1826 case ISD::SETULT:
1827 case ISD::SETLT: CondCode = B; break; // True if CF = 1
1828 case ISD::SETULE:
1829 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
1830 case ISD::SETONE:
1831 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
1832 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1833 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1834 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1835 case ISD::SETUGE: // PF = 1 | CF = 0
1836 case ISD::SETUNE: // PF = 1 | ZF = 0
1837 case ISD::SETOEQ: // PF = 0 & ZF = 1
1838 case ISD::SETOLT: // PF = 0 & CF = 1
1839 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1840 // We cannot emit this comparison as a single cmov.
1841 break;
1842 }
1843 }
1844 }
1845
Nate Begemanf63be7d2005-07-06 18:59:04 +00001846 // There's no SSE equivalent of FCMOVE. In some cases we can fake it up, in
1847 // Others we will have to do the PowerPC thing and generate an MBB for the
1848 // true and false values and select between them with a PHI.
Jeff Cohen00b168892005-07-27 06:12:32 +00001849 if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) {
Nate Begeman16b04f32005-07-15 00:38:55 +00001850 if (0 && CondCode != NOT_SET) {
1851 // FIXME: check for min and max
Nate Begemanf63be7d2005-07-06 18:59:04 +00001852 } else {
Nate Begeman16b04f32005-07-15 00:38:55 +00001853 // FIXME: emit a direct compare and branch rather than setting a cond reg
1854 // and testing it.
Nate Begemanf63be7d2005-07-06 18:59:04 +00001855 unsigned CondReg = SelectExpr(Cond);
1856 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1857
1858 // Create an iterator with which to insert the MBB for copying the false
1859 // value and the MBB to hold the PHI instruction for this SetCC.
1860 MachineBasicBlock *thisMBB = BB;
1861 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1862 ilist<MachineBasicBlock>::iterator It = BB;
1863 ++It;
1864
1865 // thisMBB:
1866 // ...
1867 // TrueVal = ...
1868 // cmpTY ccX, r1, r2
1869 // bCC sinkMBB
1870 // fallthrough --> copy0MBB
1871 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1872 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1873 BuildMI(BB, X86::JNE, 1).addMBB(sinkMBB);
1874 MachineFunction *F = BB->getParent();
1875 F->getBasicBlockList().insert(It, copy0MBB);
1876 F->getBasicBlockList().insert(It, sinkMBB);
1877 // Update machine-CFG edges
1878 BB->addSuccessor(copy0MBB);
1879 BB->addSuccessor(sinkMBB);
1880
1881 // copy0MBB:
1882 // %FalseValue = ...
1883 // # fallthrough to sinkMBB
1884 BB = copy0MBB;
1885 // Update machine-CFG edges
1886 BB->addSuccessor(sinkMBB);
1887
1888 // sinkMBB:
1889 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1890 // ...
1891 BB = sinkMBB;
1892 BuildMI(BB, X86::PHI, 4, RDest).addReg(RFalse)
1893 .addMBB(copy0MBB).addReg(RTrue).addMBB(thisMBB);
1894 }
1895 return;
1896 }
1897
Chris Lattner24aad1b2005-01-10 22:10:13 +00001898 unsigned Opc = 0;
1899 if (CondCode != NOT_SET) {
1900 switch (SVT) {
1901 default: assert(0 && "Cannot select this type!");
1902 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1903 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001904 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001905 }
1906 }
Jeff Cohen00b168892005-07-27 06:12:32 +00001907
Chris Lattner24aad1b2005-01-10 22:10:13 +00001908 // Finally, if we weren't able to fold this, just emit the condition and test
1909 // it.
1910 if (CondCode == NOT_SET || Opc == 0) {
1911 // Get the condition into the zero flag.
1912 unsigned CondReg = SelectExpr(Cond);
1913 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1914
1915 switch (SVT) {
1916 default: assert(0 && "Cannot select this type!");
1917 case MVT::i16: Opc = X86::CMOVE16rr; break;
1918 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001919 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001920 }
1921 } else {
1922 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001923 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001924 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001925 }
1926 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1927}
1928
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001929void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001930 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001931 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1932 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001933 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001934 switch (RHS.getValueType()) {
1935 default: break;
1936 case MVT::i1:
1937 case MVT::i8: Opc = X86::CMP8mi; break;
1938 case MVT::i16: Opc = X86::CMP16mi; break;
1939 case MVT::i32: Opc = X86::CMP32mi; break;
1940 }
1941 if (Opc) {
1942 X86AddressMode AM;
1943 EmitFoldedLoad(LHS, AM);
1944 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1945 return;
1946 }
1947 }
1948
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001949 switch (RHS.getValueType()) {
1950 default: break;
1951 case MVT::i1:
1952 case MVT::i8: Opc = X86::CMP8ri; break;
1953 case MVT::i16: Opc = X86::CMP16ri; break;
1954 case MVT::i32: Opc = X86::CMP32ri; break;
1955 }
1956 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001957 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001958 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1959 return;
1960 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001961 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
Nate Begemanf63be7d2005-07-06 18:59:04 +00001962 if (!X86ScalarSSE && (CN->isExactlyValue(+0.0) ||
1963 CN->isExactlyValue(-0.0))) {
Chris Lattner7f2afac2005-01-14 22:37:41 +00001964 unsigned Reg = SelectExpr(LHS);
1965 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1966 BuildMI(BB, X86::FNSTSW8r, 0);
1967 BuildMI(BB, X86::SAHF, 1);
Chris Lattner7805fa42005-03-17 16:29:26 +00001968 return;
Chris Lattner7f2afac2005-01-14 22:37:41 +00001969 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001970 }
1971
Chris Lattneref6806c2005-01-12 02:02:48 +00001972 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001973 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001974 switch (RHS.getValueType()) {
1975 default: break;
1976 case MVT::i1:
1977 case MVT::i8: Opc = X86::CMP8mr; break;
1978 case MVT::i16: Opc = X86::CMP16mr; break;
1979 case MVT::i32: Opc = X86::CMP32mr; break;
1980 }
1981 if (Opc) {
1982 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001983 EmitFoldedLoad(LHS, AM);
1984 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001985 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1986 return;
1987 }
1988 }
1989
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001990 switch (LHS.getValueType()) {
1991 default: assert(0 && "Cannot compare this value!");
1992 case MVT::i1:
1993 case MVT::i8: Opc = X86::CMP8rr; break;
1994 case MVT::i16: Opc = X86::CMP16rr; break;
1995 case MVT::i32: Opc = X86::CMP32rr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00001996 case MVT::f32: Opc = X86::UCOMISSrr; break;
1997 case MVT::f64: Opc = X86ScalarSSE ? X86::UCOMISDrr : X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001998 }
Chris Lattner11333092005-01-11 03:11:44 +00001999 unsigned Tmp1, Tmp2;
2000 if (getRegPressure(LHS) > getRegPressure(RHS)) {
2001 Tmp1 = SelectExpr(LHS);
2002 Tmp2 = SelectExpr(RHS);
2003 } else {
2004 Tmp2 = SelectExpr(RHS);
2005 Tmp1 = SelectExpr(LHS);
2006 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002007 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
2008}
2009
Chris Lattnera5ade062005-01-11 21:19:59 +00002010/// isFoldableLoad - Return true if this is a load instruction that can safely
2011/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00002012bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
2013 if (Op.getOpcode() == ISD::LOAD) {
2014 // FIXME: currently can't fold constant pool indexes.
2015 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
2016 return false;
2017 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
Chris Lattnerbce81ae2005-07-10 01:56:13 +00002018 cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::f32) {
Chris Lattner44129b52005-01-25 20:03:11 +00002019 // FIXME: currently can't fold constant pool indexes.
2020 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
2021 return false;
2022 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00002023 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00002024 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002025
2026 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00002027 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
2028 if (ExprMap.count(Op.getValue(1))) return false;
2029 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00002030 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00002031
Chris Lattner4ff348b2005-01-17 06:26:58 +00002032 // If there is not just one use of its value, we cannot fold.
2033 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
2034
2035 // Finally, we cannot fold the load into the operation if this would induce a
2036 // cycle into the resultant dag. To check for this, see if OtherOp (the other
2037 // operand of the operation we are folding the load into) can possible use the
2038 // chain node defined by the load.
2039 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
2040 std::set<SDNode*> Visited;
2041 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
2042 return false;
2043 }
2044 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00002045}
2046
Chris Lattner4ff348b2005-01-17 06:26:58 +00002047
Chris Lattnera5ade062005-01-11 21:19:59 +00002048/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
2049/// and compute the address being loaded into AM.
2050void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
2051 SDOperand Chain = Op.getOperand(0);
2052 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002053
Chris Lattnera5ade062005-01-11 21:19:59 +00002054 if (getRegPressure(Chain) > getRegPressure(Address)) {
2055 Select(Chain);
2056 SelectAddress(Address, AM);
2057 } else {
2058 SelectAddress(Address, AM);
2059 Select(Chain);
2060 }
2061
2062 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00002063 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
2064 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00002065 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00002066 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002067}
2068
Chris Lattner30ea1e92005-01-19 07:37:26 +00002069// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
2070// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
2071// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
2072// return true.
2073bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00002074 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
2075 // good!
2076 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
2077 std::swap(Op1, Op2); // Op1 is the SHL now.
2078 } else {
2079 return false; // No match
2080 }
2081
2082 SDOperand ShlVal = Op1.getOperand(0);
2083 SDOperand ShlAmt = Op1.getOperand(1);
2084 SDOperand ShrVal = Op2.getOperand(0);
2085 SDOperand ShrAmt = Op2.getOperand(1);
2086
Chris Lattner30ea1e92005-01-19 07:37:26 +00002087 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
2088
Chris Lattner85716372005-01-19 06:18:43 +00002089 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
2090 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
2091 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00002092 if (SubCST->getValue() == RegSize) {
2093 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00002094 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00002095 if (ShrVal == ShlVal) {
2096 unsigned Reg, ShAmt;
2097 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
2098 Reg = SelectExpr(ShrVal);
2099 ShAmt = SelectExpr(ShrAmt);
2100 } else {
2101 ShAmt = SelectExpr(ShrAmt);
2102 Reg = SelectExpr(ShrVal);
2103 }
2104 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2105 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
2106 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
2107 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
2108 return true;
2109 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00002110 unsigned AReg, BReg;
2111 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00002112 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002113 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00002114 } else {
Chris Lattner85716372005-01-19 06:18:43 +00002115 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002116 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00002117 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00002118 unsigned ShAmt = SelectExpr(ShrAmt);
2119 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2120 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
2121 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00002122 return true;
2123 }
2124 }
2125
Chris Lattner4053b1e2005-01-19 08:07:05 +00002126 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
2127 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
2128 if (SubCST->getValue() == RegSize) {
2129 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
2130 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
2131 if (ShrVal == ShlVal) {
2132 unsigned Reg, ShAmt;
2133 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
2134 Reg = SelectExpr(ShrVal);
2135 ShAmt = SelectExpr(ShlAmt);
2136 } else {
2137 ShAmt = SelectExpr(ShlAmt);
2138 Reg = SelectExpr(ShrVal);
2139 }
2140 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2141 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
2142 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
2143 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
2144 return true;
2145 } else if (RegSize != 8) {
2146 unsigned AReg, BReg;
2147 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002148 AReg = SelectExpr(ShlVal);
2149 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002150 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002151 BReg = SelectExpr(ShrVal);
2152 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002153 }
2154 unsigned ShAmt = SelectExpr(ShlAmt);
2155 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2156 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
2157 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
2158 return true;
2159 }
2160 }
Chris Lattner85716372005-01-19 06:18:43 +00002161
Chris Lattner4053b1e2005-01-19 08:07:05 +00002162 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
2163 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
2164 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
2165 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
2166 // (A >> 5) | (A << 27) --> ROR A, 5
2167 // (A >> 5) | (B << 27) --> SHRD A, B, 5
2168 if (ShrVal == ShlVal) {
2169 unsigned Reg = SelectExpr(ShrVal);
2170 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
2171 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
2172 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
2173 return true;
2174 } else if (RegSize != 8) {
2175 unsigned AReg, BReg;
2176 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00002177 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002178 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002179 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00002180 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002181 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002182 }
2183 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
2184 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
2185 .addImm(ShrCst->getValue());
2186 return true;
2187 }
2188 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002189
Chris Lattner85716372005-01-19 06:18:43 +00002190 return false;
2191}
2192
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002193unsigned ISel::SelectExpr(SDOperand N) {
2194 unsigned Result;
2195 unsigned Tmp1, Tmp2, Tmp3;
2196 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00002197 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00002198 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00002199
Chris Lattner7f2afac2005-01-14 22:37:41 +00002200 if (Node->getOpcode() == ISD::CopyFromReg) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00002201 if (MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()) ||
2202 cast<RegSDNode>(Node)->getReg() == X86::ESP) {
2203 // Just use the specified register as our input.
2204 return cast<RegSDNode>(Node)->getReg();
2205 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00002206 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002207
Chris Lattnera5ade062005-01-11 21:19:59 +00002208 unsigned &Reg = ExprMap[N];
2209 if (Reg) return Reg;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002210
Chris Lattnerb38a7492005-04-02 04:01:14 +00002211 switch (N.getOpcode()) {
2212 default:
Chris Lattnera5ade062005-01-11 21:19:59 +00002213 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnerb38a7492005-04-02 04:01:14 +00002214 MakeReg(N.getValueType()) : 1;
2215 break;
Chris Lattner239738a2005-05-14 08:48:15 +00002216 case X86ISD::TAILCALL:
2217 case X86ISD::CALL:
Chris Lattnera5ade062005-01-11 21:19:59 +00002218 // If this is a call instruction, make sure to prepare ALL of the result
2219 // values as well as the chain.
Chris Lattner239738a2005-05-14 08:48:15 +00002220 ExprMap[N.getValue(0)] = 1;
2221 if (Node->getNumValues() > 1) {
2222 Result = MakeReg(Node->getValueType(1));
2223 ExprMap[N.getValue(1)] = Result;
2224 for (unsigned i = 2, e = Node->getNumValues(); i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00002225 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattner239738a2005-05-14 08:48:15 +00002226 } else {
2227 Result = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002228 }
Chris Lattnerb38a7492005-04-02 04:01:14 +00002229 break;
2230 case ISD::ADD_PARTS:
2231 case ISD::SUB_PARTS:
2232 case ISD::SHL_PARTS:
2233 case ISD::SRL_PARTS:
2234 case ISD::SRA_PARTS:
2235 Result = MakeReg(Node->getValueType(0));
2236 ExprMap[N.getValue(0)] = Result;
2237 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
2238 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
2239 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002240 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002241
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002242 switch (N.getOpcode()) {
2243 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00002244 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002245 assert(0 && "Node not handled!\n");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002246 case ISD::FP_EXTEND:
Jeff Cohen00b168892005-07-27 06:12:32 +00002247 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002248 Tmp1 = SelectExpr(N.getOperand(0));
2249 BuildMI(BB, X86::CVTSS2SDrr, 1, Result).addReg(Tmp1);
2250 return Result;
Nate Begeman16b04f32005-07-15 00:38:55 +00002251 case ISD::FP_ROUND:
Jeff Cohen00b168892005-07-27 06:12:32 +00002252 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
Nate Begeman16b04f32005-07-15 00:38:55 +00002253 Tmp1 = SelectExpr(N.getOperand(0));
2254 BuildMI(BB, X86::CVTSD2SSrr, 1, Result).addReg(Tmp1);
2255 return Result;
Chris Lattnerc6f41812005-05-12 23:06:28 +00002256 case ISD::CopyFromReg:
2257 Select(N.getOperand(0));
2258 if (Result == 1) {
2259 Reg = Result = ExprMap[N.getValue(0)] =
2260 MakeReg(N.getValue(0).getValueType());
2261 }
2262 switch (Node->getValueType(0)) {
2263 default: assert(0 && "Cannot CopyFromReg this!");
2264 case MVT::i1:
2265 case MVT::i8:
2266 BuildMI(BB, X86::MOV8rr, 1,
2267 Result).addReg(cast<RegSDNode>(Node)->getReg());
2268 return Result;
2269 case MVT::i16:
2270 BuildMI(BB, X86::MOV16rr, 1,
2271 Result).addReg(cast<RegSDNode>(Node)->getReg());
2272 return Result;
2273 case MVT::i32:
2274 BuildMI(BB, X86::MOV32rr, 1,
2275 Result).addReg(cast<RegSDNode>(Node)->getReg());
2276 return Result;
Jeff Cohen00b168892005-07-27 06:12:32 +00002277 }
Chris Lattnerc6f41812005-05-12 23:06:28 +00002278
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002279 case ISD::FrameIndex:
2280 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
2281 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
2282 return Result;
2283 case ISD::ConstantPool:
2284 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
2285 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
2286 return Result;
2287 case ISD::ConstantFP:
2288 ContainsFPCode = true;
2289 Tmp1 = Result; // Intermediate Register
2290 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
2291 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
2292 Tmp1 = MakeReg(MVT::f64);
2293
2294 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
2295 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
2296 BuildMI(BB, X86::FLD0, 0, Tmp1);
2297 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
2298 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
2299 BuildMI(BB, X86::FLD1, 0, Tmp1);
2300 else
2301 assert(0 && "Unexpected constant!");
2302 if (Tmp1 != Result)
2303 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
2304 return Result;
2305 case ISD::Constant:
2306 switch (N.getValueType()) {
2307 default: assert(0 && "Cannot use constants of this type!");
2308 case MVT::i1:
2309 case MVT::i8: Opc = X86::MOV8ri; break;
2310 case MVT::i16: Opc = X86::MOV16ri; break;
2311 case MVT::i32: Opc = X86::MOV32ri; break;
2312 }
2313 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
2314 return Result;
Chris Lattner7ce7eff2005-04-01 22:46:45 +00002315 case ISD::UNDEF:
2316 if (Node->getValueType(0) == MVT::f64) {
2317 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
2318 BuildMI(BB, X86::FLD0, 0, Result);
2319 } else {
2320 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
2321 }
2322 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002323 case ISD::GlobalAddress: {
2324 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanfb5792f2005-07-12 01:41:54 +00002325 // For Darwin, external and weak symbols are indirect, so we want to load
2326 // the value at address GV, not the value of GV itself.
Jeff Cohen00b168892005-07-27 06:12:32 +00002327 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemanfb5792f2005-07-12 01:41:54 +00002328 (GV->hasWeakLinkage() || GV->isExternal())) {
2329 BuildMI(BB, X86::MOV32rm, 4, Result).addReg(0).addZImm(1).addReg(0)
2330 .addGlobalAddress(GV, false, 0);
2331 } else {
2332 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
2333 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002334 return Result;
2335 }
2336 case ISD::ExternalSymbol: {
2337 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
2338 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
2339 return Result;
2340 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002341 case ISD::ZERO_EXTEND: {
2342 int DestIs16 = N.getValueType() == MVT::i16;
2343 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00002344
2345 // FIXME: This hack is here for zero extension casts from bool to i8. This
2346 // would not be needed if bools were promoted by Legalize.
2347 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002348 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00002349 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
2350 return Result;
2351 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002352
Chris Lattner4ff348b2005-01-17 06:26:58 +00002353 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002354 static const unsigned Opc[3] = {
2355 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
2356 };
2357
2358 X86AddressMode AM;
2359 EmitFoldedLoad(N.getOperand(0), AM);
2360 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002361
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002362 return Result;
2363 }
2364
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002365 static const unsigned Opc[3] = {
2366 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
2367 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002368 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002369 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
2370 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002371 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002372 case ISD::SIGN_EXTEND: {
2373 int DestIs16 = N.getValueType() == MVT::i16;
2374 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
2375
Chris Lattner590d8002005-01-09 18:52:44 +00002376 // FIXME: Legalize should promote bools to i8!
2377 assert(N.getOperand(0).getValueType() != MVT::i1 &&
2378 "Sign extend from bool not implemented!");
2379
Chris Lattner4ff348b2005-01-17 06:26:58 +00002380 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002381 static const unsigned Opc[3] = {
2382 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
2383 };
2384
2385 X86AddressMode AM;
2386 EmitFoldedLoad(N.getOperand(0), AM);
2387 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
2388 return Result;
2389 }
2390
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002391 static const unsigned Opc[3] = {
2392 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
2393 };
2394 Tmp1 = SelectExpr(N.getOperand(0));
2395 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
2396 return Result;
2397 }
2398 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00002399 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00002400 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00002401 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00002402 switch (N.getValueType()) {
2403 default: assert(0 && "Unknown truncate!");
2404 case MVT::i1:
2405 case MVT::i8: Opc = X86::MOV8rm; break;
2406 case MVT::i16: Opc = X86::MOV16rm; break;
2407 }
2408 X86AddressMode AM;
2409 EmitFoldedLoad(N.getOperand(0), AM);
2410 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2411 return Result;
2412 }
2413
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002414 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
2415 // a move out of AX or AL.
2416 switch (N.getOperand(0).getValueType()) {
2417 default: assert(0 && "Unknown truncate!");
2418 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
2419 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
2420 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
2421 }
2422 Tmp1 = SelectExpr(N.getOperand(0));
2423 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2424
2425 switch (N.getValueType()) {
2426 default: assert(0 && "Unknown truncate!");
2427 case MVT::i1:
2428 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
2429 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
2430 }
2431 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
2432 return Result;
2433
Chris Lattnera28381c2005-07-16 00:28:20 +00002434 case ISD::SINT_TO_FP: {
Nate Begemanf63be7d2005-07-06 18:59:04 +00002435 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2436 unsigned PromoteOpcode = 0;
2437
Nate Begeman5a8441e2005-07-16 02:02:34 +00002438 // We can handle any sint to fp with the direct sse conversion instructions.
Nate Begemanf63be7d2005-07-06 18:59:04 +00002439 if (X86ScalarSSE) {
Nate Begeman5a8441e2005-07-16 02:02:34 +00002440 Opc = (N.getValueType() == MVT::f64) ? X86::CVTSI2SDrr : X86::CVTSI2SSrr;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002441 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2442 return Result;
2443 }
Jeff Cohen00b168892005-07-27 06:12:32 +00002444
Chris Lattneref7ba072005-01-11 03:50:45 +00002445 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00002446
Chris Lattner590d8002005-01-09 18:52:44 +00002447 // Spill the integer to memory and reload it from there.
Nate Begeman5a8441e2005-07-16 02:02:34 +00002448 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
Chris Lattner590d8002005-01-09 18:52:44 +00002449 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
2450 MachineFunction *F = BB->getParent();
2451 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2452
2453 switch (SrcTy) {
Chris Lattner590d8002005-01-09 18:52:44 +00002454 case MVT::i32:
Chris Lattnera28381c2005-07-16 00:28:20 +00002455 addFrameReference(BuildMI(BB, X86::MOV32mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00002456 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
2457 break;
2458 case MVT::i16:
Chris Lattnera28381c2005-07-16 00:28:20 +00002459 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00002460 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
2461 break;
2462 default: break; // No promotion required.
2463 }
Chris Lattnera28381c2005-07-16 00:28:20 +00002464 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00002465 }
Chris Lattner01546c52005-07-30 00:05:54 +00002466 case ISD::FP_TO_SINT:
Chris Lattner590d8002005-01-09 18:52:44 +00002467 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2468
Nate Begemanf63be7d2005-07-06 18:59:04 +00002469 // If the target supports SSE2 and is performing FP operations in SSE regs
2470 // instead of the FP stack, then we can use the efficient CVTSS2SI and
2471 // CVTSD2SI instructions.
Chris Lattner01546c52005-07-30 00:05:54 +00002472 assert(X86ScalarSSE);
2473 if (MVT::f32 == N.getOperand(0).getValueType()) {
2474 BuildMI(BB, X86::CVTTSS2SIrr, 1, Result).addReg(Tmp1);
2475 } else if (MVT::f64 == N.getOperand(0).getValueType()) {
2476 BuildMI(BB, X86::CVTTSD2SIrr, 1, Result).addReg(Tmp1);
2477 } else {
2478 assert(0 && "Not an f32 or f64?");
2479 abort();
Jeff Cohen00b168892005-07-27 06:12:32 +00002480 }
Chris Lattner590d8002005-01-09 18:52:44 +00002481 return Result;
Chris Lattner01546c52005-07-30 00:05:54 +00002482
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002483 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00002484 Op0 = N.getOperand(0);
2485 Op1 = N.getOperand(1);
2486
Chris Lattner44129b52005-01-25 20:03:11 +00002487 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002488 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002489 goto FoldAdd;
2490 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002491
Chris Lattner44129b52005-01-25 20:03:11 +00002492 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002493 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00002494 switch (N.getValueType()) {
2495 default: assert(0 && "Cannot add this type!");
2496 case MVT::i1:
2497 case MVT::i8: Opc = X86::ADD8rm; break;
2498 case MVT::i16: Opc = X86::ADD16rm; break;
2499 case MVT::i32: Opc = X86::ADD32rm; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002500 case MVT::f32: Opc = X86::ADDSSrm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002501 case MVT::f64:
2502 // For F64, handle promoted load operations (from F32) as well!
Nate Begemanf63be7d2005-07-06 18:59:04 +00002503 if (X86ScalarSSE) {
2504 assert(Op1.getOpcode() == ISD::LOAD && "SSE load not promoted");
2505 Opc = X86::ADDSDrm;
2506 } else {
2507 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
2508 }
Chris Lattner44129b52005-01-25 20:03:11 +00002509 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002510 }
2511 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002512 EmitFoldedLoad(Op1, AM);
2513 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002514 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2515 return Result;
2516 }
2517
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002518 // See if we can codegen this as an LEA to fold operations together.
2519 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00002520 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002521 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00002522 MatchAddress(N, AM);
2523 ExprMap[N] = Result;
2524
2525 // If this is not just an add, emit the LEA. For a simple add (like
2526 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
2527 // leave this as LEA, then peephole it to 'ADD' after two address elim
2528 // happens.
2529 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
2530 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
2531 X86AddressMode XAM = SelectAddrExprs(AM);
2532 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
2533 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002534 }
2535 }
Chris Lattner11333092005-01-11 03:11:44 +00002536
Chris Lattnera5ade062005-01-11 21:19:59 +00002537 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002538 Opc = 0;
2539 if (CN->getValue() == 1) { // add X, 1 -> inc X
2540 switch (N.getValueType()) {
2541 default: assert(0 && "Cannot integer add this type!");
2542 case MVT::i8: Opc = X86::INC8r; break;
2543 case MVT::i16: Opc = X86::INC16r; break;
2544 case MVT::i32: Opc = X86::INC32r; break;
2545 }
2546 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
2547 switch (N.getValueType()) {
2548 default: assert(0 && "Cannot integer add this type!");
2549 case MVT::i8: Opc = X86::DEC8r; break;
2550 case MVT::i16: Opc = X86::DEC16r; break;
2551 case MVT::i32: Opc = X86::DEC32r; break;
2552 }
2553 }
2554
2555 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002556 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002557 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2558 return Result;
2559 }
2560
2561 switch (N.getValueType()) {
2562 default: assert(0 && "Cannot add this type!");
2563 case MVT::i8: Opc = X86::ADD8ri; break;
2564 case MVT::i16: Opc = X86::ADD16ri; break;
2565 case MVT::i32: Opc = X86::ADD32ri; break;
2566 }
2567 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002568 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002569 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2570 return Result;
2571 }
2572 }
2573
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002574 switch (N.getValueType()) {
2575 default: assert(0 && "Cannot add this type!");
2576 case MVT::i8: Opc = X86::ADD8rr; break;
2577 case MVT::i16: Opc = X86::ADD16rr; break;
2578 case MVT::i32: Opc = X86::ADD32rr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002579 case MVT::f32: Opc = X86::ADDSSrr; break;
2580 case MVT::f64: Opc = X86ScalarSSE ? X86::ADDSDrr : X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002581 }
Chris Lattner11333092005-01-11 03:11:44 +00002582
Chris Lattnera5ade062005-01-11 21:19:59 +00002583 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2584 Tmp1 = SelectExpr(Op0);
2585 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00002586 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00002587 Tmp2 = SelectExpr(Op1);
2588 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00002589 }
2590
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002591 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2592 return Result;
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002593
Nate Begemanf63be7d2005-07-06 18:59:04 +00002594 case ISD::FSQRT:
2595 Tmp1 = SelectExpr(Node->getOperand(0));
2596 if (X86ScalarSSE) {
2597 Opc = (N.getValueType() == MVT::f32) ? X86::SQRTSSrr : X86::SQRTSDrr;
2598 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2599 } else {
2600 BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1);
2601 }
2602 return Result;
2603
2604 // FIXME:
2605 // Once we can spill 16 byte constants into the constant pool, we can
2606 // implement SSE equivalents of FABS and FCHS.
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002607 case ISD::FABS:
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002608 case ISD::FNEG:
Chris Lattnerc5dcb532005-04-30 04:25:35 +00002609 case ISD::FSIN:
2610 case ISD::FCOS:
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002611 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002612 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002613 switch (N.getOpcode()) {
2614 default: assert(0 && "Unreachable!");
2615 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
2616 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
Chris Lattnerc5dcb532005-04-30 04:25:35 +00002617 case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break;
2618 case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002619 }
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002620 return Result;
2621
Chris Lattner8db0af12005-04-06 04:21:07 +00002622 case ISD::MULHU:
2623 switch (N.getValueType()) {
2624 default: assert(0 && "Unsupported VT!");
2625 case MVT::i8: Tmp2 = X86::MUL8r; break;
2626 case MVT::i16: Tmp2 = X86::MUL16r; break;
2627 case MVT::i32: Tmp2 = X86::MUL32r; break;
2628 }
2629 // FALL THROUGH
2630 case ISD::MULHS: {
2631 unsigned MovOpc, LowReg, HiReg;
2632 switch (N.getValueType()) {
2633 default: assert(0 && "Unsupported VT!");
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002634 case MVT::i8:
Chris Lattner8db0af12005-04-06 04:21:07 +00002635 MovOpc = X86::MOV8rr;
2636 LowReg = X86::AL;
2637 HiReg = X86::AH;
2638 Opc = X86::IMUL8r;
2639 break;
2640 case MVT::i16:
2641 MovOpc = X86::MOV16rr;
2642 LowReg = X86::AX;
2643 HiReg = X86::DX;
2644 Opc = X86::IMUL16r;
2645 break;
2646 case MVT::i32:
2647 MovOpc = X86::MOV32rr;
2648 LowReg = X86::EAX;
2649 HiReg = X86::EDX;
2650 Opc = X86::IMUL32r;
2651 break;
2652 }
2653 if (Node->getOpcode() != ISD::MULHS)
2654 Opc = Tmp2; // Get the MULHU opcode.
2655
2656 Op0 = Node->getOperand(0);
2657 Op1 = Node->getOperand(1);
2658 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2659 Tmp1 = SelectExpr(Op0);
2660 Tmp2 = SelectExpr(Op1);
2661 } else {
2662 Tmp2 = SelectExpr(Op1);
2663 Tmp1 = SelectExpr(Op0);
2664 }
2665
2666 // FIXME: Implement folding of loads into the memory operands here!
2667 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
2668 BuildMI(BB, Opc, 1).addReg(Tmp2);
2669 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
2670 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002671 }
Chris Lattner8db0af12005-04-06 04:21:07 +00002672
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002673 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00002674 case ISD::MUL:
2675 case ISD::AND:
2676 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00002677 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00002678 static const unsigned SUBTab[] = {
2679 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
2680 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
2681 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
2682 };
Nate Begemanf63be7d2005-07-06 18:59:04 +00002683 static const unsigned SSE_SUBTab[] = {
2684 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
2685 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::SUBSSrm, X86::SUBSDrm,
2686 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::SUBSSrr, X86::SUBSDrr,
2687 };
Chris Lattnera5ade062005-01-11 21:19:59 +00002688 static const unsigned MULTab[] = {
2689 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
2690 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
2691 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
2692 };
Nate Begemanf63be7d2005-07-06 18:59:04 +00002693 static const unsigned SSE_MULTab[] = {
2694 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
2695 0, X86::IMUL16rm , X86::IMUL32rm, X86::MULSSrm, X86::MULSDrm,
2696 0, X86::IMUL16rr , X86::IMUL32rr, X86::MULSSrr, X86::MULSDrr,
2697 };
Chris Lattnera5ade062005-01-11 21:19:59 +00002698 static const unsigned ANDTab[] = {
2699 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
2700 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002701 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattnera5ade062005-01-11 21:19:59 +00002702 };
2703 static const unsigned ORTab[] = {
2704 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
2705 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
2706 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
2707 };
2708 static const unsigned XORTab[] = {
2709 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
2710 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
2711 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
2712 };
2713
2714 Op0 = Node->getOperand(0);
2715 Op1 = Node->getOperand(1);
2716
Chris Lattner30ea1e92005-01-19 07:37:26 +00002717 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
2718 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00002719 return Result;
2720
2721 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002722 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
2723 if (CN->isNullValue()) { // 0 - N -> neg N
2724 switch (N.getValueType()) {
2725 default: assert(0 && "Cannot sub this type!");
2726 case MVT::i1:
2727 case MVT::i8: Opc = X86::NEG8r; break;
2728 case MVT::i16: Opc = X86::NEG16r; break;
2729 case MVT::i32: Opc = X86::NEG32r; break;
2730 }
2731 Tmp1 = SelectExpr(N.getOperand(1));
2732 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2733 return Result;
2734 }
2735
Chris Lattnera5ade062005-01-11 21:19:59 +00002736 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2737 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00002738 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00002739 switch (N.getValueType()) {
2740 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00002741 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00002742 case MVT::i8: Opc = X86::NOT8r; break;
2743 case MVT::i16: Opc = X86::NOT16r; break;
2744 case MVT::i32: Opc = X86::NOT32r; break;
2745 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00002746 if (Opc) {
2747 Tmp1 = SelectExpr(Op0);
2748 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2749 return Result;
2750 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00002751 }
2752
Chris Lattner2a4e5082005-01-17 06:48:02 +00002753 // Fold common multiplies into LEA instructions.
2754 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
2755 switch ((int)CN->getValue()) {
2756 default: break;
2757 case 3:
2758 case 5:
2759 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00002760 // Remove N from exprmap so SelectAddress doesn't get confused.
2761 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002762 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00002763 SelectAddress(N, AM);
2764 // Restore it to the map.
2765 ExprMap[N] = Result;
2766 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
2767 return Result;
2768 }
2769 }
2770
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002771 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00002772 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002773 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00002774 case MVT::i8: Opc = 0; break;
2775 case MVT::i16: Opc = 1; break;
2776 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002777 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002778 switch (Node->getOpcode()) {
2779 default: assert(0 && "Unreachable!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002780 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2781 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002782 case ISD::AND: Opc = ANDTab[Opc]; break;
2783 case ISD::OR: Opc = ORTab[Opc]; break;
2784 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002785 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002786 if (Opc) { // Can't fold MUL:i8 R, imm
2787 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002788 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2789 return Result;
2790 }
2791 }
Chris Lattner11333092005-01-11 03:11:44 +00002792
Chris Lattner44129b52005-01-25 20:03:11 +00002793 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00002794 if (Node->getOpcode() != ISD::SUB) {
2795 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002796 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00002797 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00002798 // For FP, emit 'reverse' subract, with a memory operand.
Nate Begemanf63be7d2005-07-06 18:59:04 +00002799 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner44129b52005-01-25 20:03:11 +00002800 if (Op0.getOpcode() == ISD::EXTLOAD)
2801 Opc = X86::FSUBR32m;
2802 else
2803 Opc = X86::FSUBR64m;
2804
Chris Lattnera5ade062005-01-11 21:19:59 +00002805 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002806 EmitFoldedLoad(Op0, AM);
2807 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00002808 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2809 return Result;
2810 }
2811 }
2812
Chris Lattner44129b52005-01-25 20:03:11 +00002813 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002814 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00002815 switch (N.getValueType()) {
2816 default: assert(0 && "Cannot operate on this type!");
2817 case MVT::i1:
2818 case MVT::i8: Opc = 5; break;
2819 case MVT::i16: Opc = 6; break;
2820 case MVT::i32: Opc = 7; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002821 case MVT::f32: Opc = 8; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002822 // For F64, handle promoted load operations (from F32) as well!
Jeff Cohen00b168892005-07-27 06:12:32 +00002823 case MVT::f64:
2824 assert((!X86ScalarSSE || Op1.getOpcode() == ISD::LOAD) &&
Nate Begemanf63be7d2005-07-06 18:59:04 +00002825 "SSE load should have been promoted");
2826 Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002827 }
2828 switch (Node->getOpcode()) {
2829 default: assert(0 && "Unreachable!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002830 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2831 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002832 case ISD::AND: Opc = ANDTab[Opc]; break;
2833 case ISD::OR: Opc = ORTab[Opc]; break;
2834 case ISD::XOR: Opc = XORTab[Opc]; break;
2835 }
2836
2837 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002838 EmitFoldedLoad(Op1, AM);
2839 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002840 if (Opc) {
2841 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2842 } else {
2843 assert(Node->getOpcode() == ISD::MUL &&
2844 N.getValueType() == MVT::i8 && "Unexpected situation!");
2845 // Must use the MUL instruction, which forces use of AL.
2846 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2847 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2848 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2849 }
2850 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00002851 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002852
2853 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2854 Tmp1 = SelectExpr(Op0);
2855 Tmp2 = SelectExpr(Op1);
2856 } else {
2857 Tmp2 = SelectExpr(Op1);
2858 Tmp1 = SelectExpr(Op0);
2859 }
2860
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002861 switch (N.getValueType()) {
2862 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002863 case MVT::i1:
2864 case MVT::i8: Opc = 10; break;
2865 case MVT::i16: Opc = 11; break;
2866 case MVT::i32: Opc = 12; break;
2867 case MVT::f32: Opc = 13; break;
2868 case MVT::f64: Opc = 14; break;
2869 }
2870 switch (Node->getOpcode()) {
2871 default: assert(0 && "Unreachable!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002872 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2873 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002874 case ISD::AND: Opc = ANDTab[Opc]; break;
2875 case ISD::OR: Opc = ORTab[Opc]; break;
2876 case ISD::XOR: Opc = XORTab[Opc]; break;
2877 }
2878 if (Opc) {
2879 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2880 } else {
2881 assert(Node->getOpcode() == ISD::MUL &&
2882 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00002883 // Must use the MUL instruction, which forces use of AL.
2884 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2885 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2886 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002887 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002888 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00002889 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002890 case ISD::ADD_PARTS:
2891 case ISD::SUB_PARTS: {
2892 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2893 "Not an i64 add/sub!");
2894 // Emit all of the operands.
2895 std::vector<unsigned> InVals;
2896 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2897 InVals.push_back(SelectExpr(N.getOperand(i)));
2898 if (N.getOpcode() == ISD::ADD_PARTS) {
2899 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2900 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2901 } else {
2902 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2903 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2904 }
2905 return Result+N.ResNo;
2906 }
2907
Chris Lattnerb38a7492005-04-02 04:01:14 +00002908 case ISD::SHL_PARTS:
2909 case ISD::SRA_PARTS:
2910 case ISD::SRL_PARTS: {
2911 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2912 "Not an i64 shift!");
2913 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2914 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2915 unsigned TmpReg = MakeReg(MVT::i32);
2916 if (N.getOpcode() == ISD::SRA_PARTS) {
2917 // If this is a SHR of a Long, then we need to do funny sign extension
2918 // stuff. TmpReg gets the value to use as the high-part if we are
2919 // shifting more than 32 bits.
2920 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2921 } else {
2922 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2923 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2924 }
2925
2926 // Initialize CL with the shift amount.
2927 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2928 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2929
2930 unsigned TmpReg2 = MakeReg(MVT::i32);
2931 unsigned TmpReg3 = MakeReg(MVT::i32);
2932 if (N.getOpcode() == ISD::SHL_PARTS) {
2933 // TmpReg2 = shld inHi, inLo
2934 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2935 .addReg(ShiftOpLo);
2936 // TmpReg3 = shl inLo, CL
2937 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002938
Chris Lattnerb38a7492005-04-02 04:01:14 +00002939 // Set the flags to indicate whether the shift was by more than 32 bits.
2940 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002941
Chris Lattnerb38a7492005-04-02 04:01:14 +00002942 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002943 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002944 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2945 // DestLo = (>32) ? TmpReg : TmpReg3;
2946 BuildMI(BB, X86::CMOVNE32rr, 2,
2947 Result).addReg(TmpReg3).addReg(TmpReg);
2948 } else {
2949 // TmpReg2 = shrd inLo, inHi
2950 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
2951 .addReg(ShiftOpHi);
2952 // TmpReg3 = s[ah]r inHi, CL
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002953 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnerb38a7492005-04-02 04:01:14 +00002954 : X86::SHR32rCL, 1, TmpReg3)
2955 .addReg(ShiftOpHi);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002956
Chris Lattnerb38a7492005-04-02 04:01:14 +00002957 // Set the flags to indicate whether the shift was by more than 32 bits.
2958 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002959
Chris Lattnerb38a7492005-04-02 04:01:14 +00002960 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002961 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002962 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002963
Chris Lattnerb38a7492005-04-02 04:01:14 +00002964 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002965 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002966 Result+1).addReg(TmpReg3).addReg(TmpReg);
2967 }
2968 return Result+N.ResNo;
2969 }
2970
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002971 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002972 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2973 Tmp2 = SelectExpr(N.getOperand(1));
2974 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002975 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002976 Tmp3 = SelectExpr(N.getOperand(2));
2977 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002978 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002979 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2980 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002981
2982 case ISD::SDIV:
2983 case ISD::UDIV:
2984 case ISD::SREM:
2985 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002986 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2987 "We don't support this operator!");
2988
Chris Lattner5bf26862005-04-13 03:29:53 +00002989 if (N.getOpcode() == ISD::SDIV) {
Chris Lattner3576c842005-01-25 20:35:10 +00002990 // We can fold loads into FpDIVs, but not really into any others.
Nate Begemanb8aa3ac2005-07-07 06:32:01 +00002991 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner3576c842005-01-25 20:35:10 +00002992 // Check for reversed and unreversed DIV.
2993 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2994 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
2995 Opc = X86::FDIVR32m;
2996 else
2997 Opc = X86::FDIVR64m;
2998 X86AddressMode AM;
2999 EmitFoldedLoad(N.getOperand(0), AM);
3000 Tmp1 = SelectExpr(N.getOperand(1));
3001 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
3002 return Result;
3003 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
3004 N.getOperand(1).getOpcode() == ISD::LOAD) {
3005 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
3006 Opc = X86::FDIV32m;
3007 else
3008 Opc = X86::FDIV64m;
3009 X86AddressMode AM;
3010 EmitFoldedLoad(N.getOperand(1), AM);
3011 Tmp1 = SelectExpr(N.getOperand(0));
3012 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
3013 return Result;
3014 }
3015 }
3016
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003017 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3018 // FIXME: These special cases should be handled by the lowering impl!
3019 unsigned RHS = CN->getValue();
3020 bool isNeg = false;
3021 if ((int)RHS < 0) {
3022 isNeg = true;
3023 RHS = -RHS;
3024 }
3025 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
Chris Lattner0561b3f2005-08-02 19:26:06 +00003026 unsigned Log = Log2_32(RHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003027 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
3028 switch (N.getValueType()) {
3029 default: assert("Unknown type to signed divide!");
3030 case MVT::i8:
3031 SAROpc = X86::SAR8ri;
3032 SHROpc = X86::SHR8ri;
3033 ADDOpc = X86::ADD8rr;
3034 NEGOpc = X86::NEG8r;
3035 break;
3036 case MVT::i16:
3037 SAROpc = X86::SAR16ri;
3038 SHROpc = X86::SHR16ri;
3039 ADDOpc = X86::ADD16rr;
3040 NEGOpc = X86::NEG16r;
3041 break;
3042 case MVT::i32:
3043 SAROpc = X86::SAR32ri;
3044 SHROpc = X86::SHR32ri;
3045 ADDOpc = X86::ADD32rr;
3046 NEGOpc = X86::NEG32r;
3047 break;
3048 }
Chris Lattnera96e5772005-05-13 21:48:20 +00003049 unsigned RegSize = MVT::getSizeInBits(N.getValueType());
Chris Lattner11333092005-01-11 03:11:44 +00003050 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerca96c822005-05-13 21:50:27 +00003051 unsigned TmpReg;
3052 if (Log != 1) {
3053 TmpReg = MakeReg(N.getValueType());
3054 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
3055 } else {
3056 TmpReg = Tmp1;
3057 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003058 unsigned TmpReg2 = MakeReg(N.getValueType());
Chris Lattnera96e5772005-05-13 21:48:20 +00003059 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(RegSize-Log);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003060 unsigned TmpReg3 = MakeReg(N.getValueType());
3061 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003062
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003063 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
3064 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
3065 if (isNeg)
3066 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
3067 return Result;
3068 }
3069 }
Chris Lattner5bf26862005-04-13 03:29:53 +00003070 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003071
Chris Lattner11333092005-01-11 03:11:44 +00003072 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3073 Tmp1 = SelectExpr(N.getOperand(0));
3074 Tmp2 = SelectExpr(N.getOperand(1));
3075 } else {
3076 Tmp2 = SelectExpr(N.getOperand(1));
3077 Tmp1 = SelectExpr(N.getOperand(0));
3078 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003079
3080 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
3081 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
3082 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
3083 switch (N.getValueType()) {
3084 default: assert(0 && "Cannot sdiv this type!");
3085 case MVT::i8:
3086 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
3087 LoReg = X86::AL;
3088 HiReg = X86::AH;
3089 MovOpcode = X86::MOV8rr;
3090 ClrOpcode = X86::MOV8ri;
3091 SExtOpcode = X86::CBW;
3092 break;
3093 case MVT::i16:
3094 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
3095 LoReg = X86::AX;
3096 HiReg = X86::DX;
3097 MovOpcode = X86::MOV16rr;
3098 ClrOpcode = X86::MOV16ri;
3099 SExtOpcode = X86::CWD;
3100 break;
3101 case MVT::i32:
3102 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00003103 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003104 HiReg = X86::EDX;
3105 MovOpcode = X86::MOV32rr;
3106 ClrOpcode = X86::MOV32ri;
3107 SExtOpcode = X86::CDQ;
3108 break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00003109 case MVT::f32:
3110 BuildMI(BB, X86::DIVSSrr, 2, Result).addReg(Tmp1).addReg(Tmp2);
3111 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003112 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00003113 Opc = X86ScalarSSE ? X86::DIVSDrr : X86::FpDIV;
3114 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003115 return Result;
3116 }
3117
3118 // Set up the low part.
3119 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
3120
3121 if (isSigned) {
3122 // Sign extend the low part into the high part.
3123 BuildMI(BB, SExtOpcode, 0);
3124 } else {
3125 // Zero out the high part, effectively zero extending the input.
3126 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
3127 }
3128
3129 // Emit the DIV/IDIV instruction.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003130 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003131
3132 // Get the result of the divide or rem.
3133 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
3134 return Result;
3135 }
3136
3137 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003138 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00003139 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
3140 switch (N.getValueType()) {
3141 default: assert(0 && "Cannot shift this type!");
3142 case MVT::i8: Opc = X86::ADD8rr; break;
3143 case MVT::i16: Opc = X86::ADD16rr; break;
3144 case MVT::i32: Opc = X86::ADD32rr; break;
3145 }
3146 Tmp1 = SelectExpr(N.getOperand(0));
3147 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
3148 return Result;
3149 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003150
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003151 switch (N.getValueType()) {
3152 default: assert(0 && "Cannot shift this type!");
3153 case MVT::i8: Opc = X86::SHL8ri; break;
3154 case MVT::i16: Opc = X86::SHL16ri; break;
3155 case MVT::i32: Opc = X86::SHL32ri; break;
3156 }
Chris Lattner11333092005-01-11 03:11:44 +00003157 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003158 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3159 return Result;
3160 }
Chris Lattner11333092005-01-11 03:11:44 +00003161
3162 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3163 Tmp1 = SelectExpr(N.getOperand(0));
3164 Tmp2 = SelectExpr(N.getOperand(1));
3165 } else {
3166 Tmp2 = SelectExpr(N.getOperand(1));
3167 Tmp1 = SelectExpr(N.getOperand(0));
3168 }
3169
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003170 switch (N.getValueType()) {
3171 default: assert(0 && "Cannot shift this type!");
3172 case MVT::i8 : Opc = X86::SHL8rCL; break;
3173 case MVT::i16: Opc = X86::SHL16rCL; break;
3174 case MVT::i32: Opc = X86::SHL32rCL; break;
3175 }
3176 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3177 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3178 return Result;
3179 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003180 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3181 switch (N.getValueType()) {
3182 default: assert(0 && "Cannot shift this type!");
3183 case MVT::i8: Opc = X86::SHR8ri; break;
3184 case MVT::i16: Opc = X86::SHR16ri; break;
3185 case MVT::i32: Opc = X86::SHR32ri; break;
3186 }
Chris Lattner11333092005-01-11 03:11:44 +00003187 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003188 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3189 return Result;
3190 }
Chris Lattner11333092005-01-11 03:11:44 +00003191
3192 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3193 Tmp1 = SelectExpr(N.getOperand(0));
3194 Tmp2 = SelectExpr(N.getOperand(1));
3195 } else {
3196 Tmp2 = SelectExpr(N.getOperand(1));
3197 Tmp1 = SelectExpr(N.getOperand(0));
3198 }
3199
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003200 switch (N.getValueType()) {
3201 default: assert(0 && "Cannot shift this type!");
3202 case MVT::i8 : Opc = X86::SHR8rCL; break;
3203 case MVT::i16: Opc = X86::SHR16rCL; break;
3204 case MVT::i32: Opc = X86::SHR32rCL; break;
3205 }
3206 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3207 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3208 return Result;
3209 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003210 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3211 switch (N.getValueType()) {
3212 default: assert(0 && "Cannot shift this type!");
3213 case MVT::i8: Opc = X86::SAR8ri; break;
3214 case MVT::i16: Opc = X86::SAR16ri; break;
3215 case MVT::i32: Opc = X86::SAR32ri; break;
3216 }
Chris Lattner11333092005-01-11 03:11:44 +00003217 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003218 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3219 return Result;
3220 }
Chris Lattner11333092005-01-11 03:11:44 +00003221
3222 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3223 Tmp1 = SelectExpr(N.getOperand(0));
3224 Tmp2 = SelectExpr(N.getOperand(1));
3225 } else {
3226 Tmp2 = SelectExpr(N.getOperand(1));
3227 Tmp1 = SelectExpr(N.getOperand(0));
3228 }
3229
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003230 switch (N.getValueType()) {
3231 default: assert(0 && "Cannot shift this type!");
3232 case MVT::i8 : Opc = X86::SAR8rCL; break;
3233 case MVT::i16: Opc = X86::SAR16rCL; break;
3234 case MVT::i32: Opc = X86::SAR32rCL; break;
3235 }
3236 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3237 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3238 return Result;
3239
3240 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00003241 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003242 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
3243 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
3244 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003245 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003246 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00003247 if (Result != 1) { // Generate the token
3248 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3249 assert(0 && "Load already emitted!?");
3250 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003251 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3252
Chris Lattner5188ad72005-01-08 19:28:19 +00003253 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003254 default: assert(0 && "Cannot load this type!");
3255 case MVT::i1:
3256 case MVT::i8: Opc = X86::MOV8rm; break;
3257 case MVT::i16: Opc = X86::MOV16rm; break;
3258 case MVT::i32: Opc = X86::MOV32rm; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00003259 case MVT::f32: Opc = X86::MOVSSrm; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003260 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00003261 if (X86ScalarSSE) {
3262 Opc = X86::MOVSDrm;
3263 } else {
3264 Opc = X86::FLD64m;
Jeff Cohen00b168892005-07-27 06:12:32 +00003265 ContainsFPCode = true;
Nate Begemanf63be7d2005-07-06 18:59:04 +00003266 }
3267 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003268 }
Chris Lattner11333092005-01-11 03:11:44 +00003269
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003270 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00003271 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003272 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
3273 } else {
3274 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00003275
3276 SDOperand Chain = N.getOperand(0);
3277 SDOperand Address = N.getOperand(1);
3278 if (getRegPressure(Chain) > getRegPressure(Address)) {
3279 Select(Chain);
3280 SelectAddress(Address, AM);
3281 } else {
3282 SelectAddress(Address, AM);
3283 Select(Chain);
3284 }
3285
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003286 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
3287 }
3288 return Result;
Chris Lattner67649df2005-05-14 06:52:07 +00003289 case X86ISD::FILD64m:
3290 // Make sure we generate both values.
3291 assert(Result != 1 && N.getValueType() == MVT::f64);
3292 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3293 assert(0 && "Load already emitted!?");
3294
3295 {
3296 X86AddressMode AM;
3297
3298 SDOperand Chain = N.getOperand(0);
3299 SDOperand Address = N.getOperand(1);
3300 if (getRegPressure(Chain) > getRegPressure(Address)) {
3301 Select(Chain);
3302 SelectAddress(Address, AM);
3303 } else {
3304 SelectAddress(Address, AM);
3305 Select(Chain);
3306 }
Chris Lattner745d5382005-07-29 00:40:01 +00003307
3308 addFullAddress(BuildMI(BB, X86::FILD64m, 4, Result), AM);
Chris Lattner67649df2005-05-14 06:52:07 +00003309 }
3310 return Result;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003311
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003312 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
3313 case ISD::ZEXTLOAD: {
3314 // Make sure we generate both values.
3315 if (Result != 1)
3316 ExprMap[N.getValue(1)] = 1; // Generate the token
3317 else
3318 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3319
Chris Lattnerda2ce112005-01-16 07:34:08 +00003320 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
3321 if (Node->getValueType(0) == MVT::f64) {
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003322 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnerda2ce112005-01-16 07:34:08 +00003323 "Bad EXTLOAD!");
3324 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
3325 CP->getIndex());
3326 return Result;
3327 }
3328
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003329 X86AddressMode AM;
3330 if (getRegPressure(Node->getOperand(0)) >
3331 getRegPressure(Node->getOperand(1))) {
3332 Select(Node->getOperand(0)); // chain
3333 SelectAddress(Node->getOperand(1), AM);
3334 } else {
3335 SelectAddress(Node->getOperand(1), AM);
3336 Select(Node->getOperand(0)); // chain
3337 }
3338
3339 switch (Node->getValueType(0)) {
3340 default: assert(0 && "Unknown type to sign extend to.");
3341 case MVT::f64:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003342 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003343 "Bad EXTLOAD!");
3344 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
3345 break;
3346 case MVT::i32:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003347 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003348 default:
3349 assert(0 && "Bad zero extend!");
3350 case MVT::i1:
3351 case MVT::i8:
3352 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
3353 break;
3354 case MVT::i16:
3355 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
3356 break;
3357 }
3358 break;
3359 case MVT::i16:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003360 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() <= MVT::i8 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003361 "Bad zero extend!");
3362 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3363 break;
3364 case MVT::i8:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003365 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i1 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003366 "Bad zero extend!");
3367 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
3368 break;
3369 }
3370 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003371 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003372 case ISD::SEXTLOAD: {
3373 // Make sure we generate both values.
3374 if (Result != 1)
3375 ExprMap[N.getValue(1)] = 1; // Generate the token
3376 else
3377 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3378
3379 X86AddressMode AM;
3380 if (getRegPressure(Node->getOperand(0)) >
3381 getRegPressure(Node->getOperand(1))) {
3382 Select(Node->getOperand(0)); // chain
3383 SelectAddress(Node->getOperand(1), AM);
3384 } else {
3385 SelectAddress(Node->getOperand(1), AM);
3386 Select(Node->getOperand(0)); // chain
3387 }
3388
3389 switch (Node->getValueType(0)) {
3390 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
3391 default: assert(0 && "Unknown type to sign extend to.");
3392 case MVT::i32:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003393 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003394 default:
3395 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
3396 case MVT::i8:
3397 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
3398 break;
3399 case MVT::i16:
3400 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
3401 break;
3402 }
3403 break;
3404 case MVT::i16:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003405 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i8 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003406 "Cannot sign extend from bool!");
3407 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3408 break;
3409 }
3410 return Result;
3411 }
3412
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003413 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003414 // Generate both result values.
3415 if (Result != 1)
3416 ExprMap[N.getValue(1)] = 1; // Generate the token
3417 else
3418 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3419
3420 // FIXME: We are currently ignoring the requested alignment for handling
3421 // greater than the stack alignment. This will need to be revisited at some
3422 // point. Align = N.getOperand(2);
3423
3424 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
3425 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
3426 std::cerr << "Cannot allocate stack object with greater alignment than"
3427 << " the stack alignment yet!";
3428 abort();
3429 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003430
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003431 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00003432 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003433 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
3434 .addImm(CN->getValue());
3435 } else {
Chris Lattner11333092005-01-11 03:11:44 +00003436 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3437 Select(N.getOperand(0));
3438 Tmp1 = SelectExpr(N.getOperand(1));
3439 } else {
3440 Tmp1 = SelectExpr(N.getOperand(1));
3441 Select(N.getOperand(0));
3442 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003443
3444 // Subtract size from stack pointer, thereby allocating some space.
3445 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
3446 }
3447
3448 // Put a pointer to the space into the result register, by copying the stack
3449 // pointer.
3450 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
3451 return Result;
3452
Chris Lattner239738a2005-05-14 08:48:15 +00003453 case X86ISD::TAILCALL:
3454 case X86ISD::CALL: {
Chris Lattner5188ad72005-01-08 19:28:19 +00003455 // The chain for this call is now lowered.
Chris Lattner239738a2005-05-14 08:48:15 +00003456 ExprMap.insert(std::make_pair(N.getValue(0), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00003457
Chris Lattnerc6f41812005-05-12 23:06:28 +00003458 bool isDirect = isa<GlobalAddressSDNode>(N.getOperand(1)) ||
3459 isa<ExternalSymbolSDNode>(N.getOperand(1));
3460 unsigned Callee = 0;
3461 if (isDirect) {
3462 Select(N.getOperand(0));
3463 } else {
3464 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3465 Select(N.getOperand(0));
3466 Callee = SelectExpr(N.getOperand(1));
3467 } else {
3468 Callee = SelectExpr(N.getOperand(1));
3469 Select(N.getOperand(0));
3470 }
3471 }
3472
3473 // If this call has values to pass in registers, do so now.
Chris Lattner239738a2005-05-14 08:48:15 +00003474 if (Node->getNumOperands() > 4) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003475 // The first value is passed in (a part of) EAX, the second in EDX.
Chris Lattner239738a2005-05-14 08:48:15 +00003476 unsigned RegOp1 = SelectExpr(N.getOperand(4));
Chris Lattnerc6f41812005-05-12 23:06:28 +00003477 unsigned RegOp2 =
Chris Lattner239738a2005-05-14 08:48:15 +00003478 Node->getNumOperands() > 5 ? SelectExpr(N.getOperand(5)) : 0;
Jeff Cohen00b168892005-07-27 06:12:32 +00003479
Chris Lattner239738a2005-05-14 08:48:15 +00003480 switch (N.getOperand(4).getValueType()) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003481 default: assert(0 && "Bad thing to pass in regs");
3482 case MVT::i1:
3483 case MVT::i8: BuildMI(BB, X86::MOV8rr , 1,X86::AL).addReg(RegOp1); break;
3484 case MVT::i16: BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1); break;
3485 case MVT::i32: BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);break;
3486 }
3487 if (RegOp2)
Chris Lattner239738a2005-05-14 08:48:15 +00003488 switch (N.getOperand(5).getValueType()) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003489 default: assert(0 && "Bad thing to pass in regs");
3490 case MVT::i1:
3491 case MVT::i8:
3492 BuildMI(BB, X86::MOV8rr , 1, X86::DL).addReg(RegOp2);
3493 break;
3494 case MVT::i16:
3495 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
3496 break;
3497 case MVT::i32:
3498 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
3499 break;
3500 }
3501 }
3502
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003503 if (GlobalAddressSDNode *GASD =
3504 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3505 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
3506 } else if (ExternalSymbolSDNode *ESSDN =
3507 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
3508 BuildMI(BB, X86::CALLpcrel32,
3509 1).addExternalSymbol(ESSDN->getSymbol(), true);
3510 } else {
Chris Lattner11333092005-01-11 03:11:44 +00003511 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3512 Select(N.getOperand(0));
3513 Tmp1 = SelectExpr(N.getOperand(1));
3514 } else {
3515 Tmp1 = SelectExpr(N.getOperand(1));
3516 Select(N.getOperand(0));
3517 }
3518
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003519 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
3520 }
Chris Lattner239738a2005-05-14 08:48:15 +00003521
3522 // Get caller stack amount and amount the callee added to the stack pointer.
3523 Tmp1 = cast<ConstantSDNode>(N.getOperand(2))->getValue();
3524 Tmp2 = cast<ConstantSDNode>(N.getOperand(3))->getValue();
3525 BuildMI(BB, X86::ADJCALLSTACKUP, 2).addImm(Tmp1).addImm(Tmp2);
3526
3527 if (Node->getNumValues() != 1)
3528 switch (Node->getValueType(1)) {
3529 default: assert(0 && "Unknown value type for call result!");
3530 case MVT::Other: return 1;
3531 case MVT::i1:
3532 case MVT::i8:
3533 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3534 break;
3535 case MVT::i16:
3536 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3537 break;
3538 case MVT::i32:
3539 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3540 if (Node->getNumValues() == 3 && Node->getValueType(2) == MVT::i32)
3541 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
3542 break;
3543 case MVT::f64: // Floating-point return values live in %ST(0)
Nate Begemanf63be7d2005-07-06 18:59:04 +00003544 if (X86ScalarSSE) {
3545 ContainsFPCode = true;
3546 BuildMI(BB, X86::FpGETRESULT, 1, X86::FP0);
3547
3548 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
3549 MachineFunction *F = BB->getParent();
3550 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
3551 addFrameReference(BuildMI(BB, X86::FST64m, 5), FrameIdx).addReg(X86::FP0);
3552 addFrameReference(BuildMI(BB, X86::MOVSDrm, 4, Result), FrameIdx);
3553 break;
3554 } else {
3555 ContainsFPCode = true;
3556 BuildMI(BB, X86::FpGETRESULT, 1, Result);
3557 break;
3558 }
Chris Lattner239738a2005-05-14 08:48:15 +00003559 }
3560 return Result+N.ResNo-1;
Chris Lattnerc6f41812005-05-12 23:06:28 +00003561 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00003562 case ISD::READPORT:
3563 // First, determine that the size of the operand falls within the acceptable
3564 // range for this architecture.
3565 //
3566 if (Node->getOperand(1).getValueType() != MVT::i16) {
3567 std::cerr << "llvm.readport: Address size is not 16 bits\n";
3568 exit(1);
3569 }
3570
3571 // Make sure we generate both values.
3572 if (Result != 1) { // Generate the token
3573 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3574 assert(0 && "readport already emitted!?");
3575 } else
3576 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Jeff Cohen00b168892005-07-27 06:12:32 +00003577
Chris Lattner966cdfb2005-05-09 21:17:38 +00003578 Select(Node->getOperand(0)); // Select the chain.
3579
3580 // If the port is a single-byte constant, use the immediate form.
3581 if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
3582 if ((Port->getValue() & 255) == Port->getValue()) {
3583 switch (Node->getValueType(0)) {
3584 case MVT::i8:
3585 BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue());
3586 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3587 return Result;
3588 case MVT::i16:
3589 BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue());
3590 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3591 return Result;
3592 case MVT::i32:
3593 BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue());
3594 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3595 return Result;
3596 default: break;
3597 }
3598 }
3599
3600 // Now, move the I/O port address into the DX register and use the IN
3601 // instruction to get the input data.
3602 //
3603 Tmp1 = SelectExpr(Node->getOperand(1));
3604 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1);
3605 switch (Node->getValueType(0)) {
3606 case MVT::i8:
3607 BuildMI(BB, X86::IN8rr, 0);
3608 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3609 return Result;
3610 case MVT::i16:
3611 BuildMI(BB, X86::IN16rr, 0);
3612 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3613 return Result;
3614 case MVT::i32:
3615 BuildMI(BB, X86::IN32rr, 0);
3616 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3617 return Result;
3618 default:
3619 std::cerr << "Cannot do input on this data type";
3620 exit(1);
3621 }
Jeff Cohen00b168892005-07-27 06:12:32 +00003622
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003623 }
3624
3625 return 0;
3626}
3627
Chris Lattnere10269b2005-01-17 19:25:26 +00003628/// TryToFoldLoadOpStore - Given a store node, try to fold together a
3629/// load/op/store instruction. If successful return true.
3630bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
3631 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
3632 SDOperand Chain = Node->getOperand(0);
3633 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00003634 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00003635
3636 // The chain has to be a load, the stored value must be an integer binary
3637 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00003638 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00003639 MVT::isFloatingPoint(StVal.getValueType()))
3640 return false;
3641
Chris Lattner5c659812005-01-17 22:10:42 +00003642 // Token chain must either be a factor node or the load to fold.
3643 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
3644 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00003645
Chris Lattner5c659812005-01-17 22:10:42 +00003646 SDOperand TheLoad;
3647
3648 // Check to see if there is a load from the same pointer that we're storing
3649 // to in either operand of the binop.
3650 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
3651 StVal.getOperand(0).getOperand(1) == StPtr)
3652 TheLoad = StVal.getOperand(0);
3653 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
3654 StVal.getOperand(1).getOperand(1) == StPtr)
3655 TheLoad = StVal.getOperand(1);
3656 else
3657 return false; // No matching load operand.
3658
3659 // We can only fold the load if there are no intervening side-effecting
3660 // operations. This means that the store uses the load as its token chain, or
3661 // there are only token factor nodes in between the store and load.
3662 if (Chain != TheLoad.getValue(1)) {
3663 // Okay, the other option is that we have a store referring to (possibly
3664 // nested) token factor nodes. For now, just try peeking through one level
3665 // of token factors to see if this is the case.
3666 bool ChainOk = false;
3667 if (Chain.getOpcode() == ISD::TokenFactor) {
3668 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3669 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
3670 ChainOk = true;
3671 break;
3672 }
3673 }
3674
3675 if (!ChainOk) return false;
3676 }
3677
3678 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00003679 return false;
3680
3681 // Make sure that one of the operands of the binop is the load, and that the
3682 // load folds into the binop.
3683 if (((StVal.getOperand(0) != TheLoad ||
3684 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
3685 (StVal.getOperand(1) != TheLoad ||
3686 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
3687 return false;
3688
3689 // Finally, check to see if this is one of the ops we can handle!
3690 static const unsigned ADDTAB[] = {
3691 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
3692 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
3693 };
3694 static const unsigned SUBTAB[] = {
3695 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
3696 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
3697 };
3698 static const unsigned ANDTAB[] = {
3699 X86::AND8mi, X86::AND16mi, X86::AND32mi,
3700 X86::AND8mr, X86::AND16mr, X86::AND32mr,
3701 };
3702 static const unsigned ORTAB[] = {
3703 X86::OR8mi, X86::OR16mi, X86::OR32mi,
3704 X86::OR8mr, X86::OR16mr, X86::OR32mr,
3705 };
3706 static const unsigned XORTAB[] = {
3707 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
3708 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
3709 };
3710 static const unsigned SHLTAB[] = {
3711 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
3712 /*Have to put the reg in CL*/0, 0, 0,
3713 };
3714 static const unsigned SARTAB[] = {
3715 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
3716 /*Have to put the reg in CL*/0, 0, 0,
3717 };
3718 static const unsigned SHRTAB[] = {
3719 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
3720 /*Have to put the reg in CL*/0, 0, 0,
3721 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003722
Chris Lattnere10269b2005-01-17 19:25:26 +00003723 const unsigned *TabPtr = 0;
3724 switch (StVal.getOpcode()) {
3725 default:
3726 std::cerr << "CANNOT [mem] op= val: ";
3727 StVal.Val->dump(); std::cerr << "\n";
3728 case ISD::MUL:
3729 case ISD::SDIV:
3730 case ISD::UDIV:
3731 case ISD::SREM:
3732 case ISD::UREM: return false;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003733
Chris Lattnere10269b2005-01-17 19:25:26 +00003734 case ISD::ADD: TabPtr = ADDTAB; break;
3735 case ISD::SUB: TabPtr = SUBTAB; break;
3736 case ISD::AND: TabPtr = ANDTAB; break;
3737 case ISD:: OR: TabPtr = ORTAB; break;
3738 case ISD::XOR: TabPtr = XORTAB; break;
3739 case ISD::SHL: TabPtr = SHLTAB; break;
3740 case ISD::SRA: TabPtr = SARTAB; break;
3741 case ISD::SRL: TabPtr = SHRTAB; break;
3742 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003743
Chris Lattnere10269b2005-01-17 19:25:26 +00003744 // Handle: [mem] op= CST
3745 SDOperand Op0 = StVal.getOperand(0);
3746 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00003747 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00003748 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
3749 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
3750 default: break;
3751 case MVT::i1:
3752 case MVT::i8: Opc = TabPtr[0]; break;
3753 case MVT::i16: Opc = TabPtr[1]; break;
3754 case MVT::i32: Opc = TabPtr[2]; break;
3755 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003756
Chris Lattnere10269b2005-01-17 19:25:26 +00003757 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00003758 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3759 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00003760 Select(Chain);
3761
Chris Lattnere10269b2005-01-17 19:25:26 +00003762 X86AddressMode AM;
3763 if (getRegPressure(TheLoad.getOperand(0)) >
3764 getRegPressure(TheLoad.getOperand(1))) {
3765 Select(TheLoad.getOperand(0));
3766 SelectAddress(TheLoad.getOperand(1), AM);
3767 } else {
3768 SelectAddress(TheLoad.getOperand(1), AM);
3769 Select(TheLoad.getOperand(0));
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003770 }
Chris Lattner5c659812005-01-17 22:10:42 +00003771
3772 if (StVal.getOpcode() == ISD::ADD) {
3773 if (CN->getValue() == 1) {
3774 switch (Op0.getValueType()) {
3775 default: break;
3776 case MVT::i8:
3777 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
3778 return true;
3779 case MVT::i16: Opc = TabPtr[1];
3780 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
3781 return true;
3782 case MVT::i32: Opc = TabPtr[2];
3783 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
3784 return true;
3785 }
3786 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
3787 switch (Op0.getValueType()) {
3788 default: break;
3789 case MVT::i8:
3790 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
3791 return true;
3792 case MVT::i16: Opc = TabPtr[1];
3793 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
3794 return true;
3795 case MVT::i32: Opc = TabPtr[2];
3796 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
3797 return true;
3798 }
3799 }
3800 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003801
Chris Lattnere10269b2005-01-17 19:25:26 +00003802 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
3803 return true;
3804 }
3805 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003806
Chris Lattnere10269b2005-01-17 19:25:26 +00003807 // If we have [mem] = V op [mem], try to turn it into:
3808 // [mem] = [mem] op V.
3809 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
3810 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
3811 StVal.getOpcode() != ISD::SRL)
3812 std::swap(Op0, Op1);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003813
Chris Lattnere10269b2005-01-17 19:25:26 +00003814 if (Op0 != TheLoad) return false;
3815
3816 switch (Op0.getValueType()) {
3817 default: return false;
3818 case MVT::i1:
3819 case MVT::i8: Opc = TabPtr[3]; break;
3820 case MVT::i16: Opc = TabPtr[4]; break;
3821 case MVT::i32: Opc = TabPtr[5]; break;
3822 }
Chris Lattner5c659812005-01-17 22:10:42 +00003823
Chris Lattnerb422aea2005-01-18 17:35:28 +00003824 // Table entry doesn't exist?
3825 if (Opc == 0) return false;
3826
Chris Lattner4a108662005-01-18 03:51:59 +00003827 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3828 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00003829 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00003830 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00003831
Chris Lattnere10269b2005-01-17 19:25:26 +00003832 X86AddressMode AM;
3833 SelectAddress(TheLoad.getOperand(1), AM);
3834 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00003835 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00003836 return true;
3837}
3838
Chris Lattner381e8872005-05-15 05:46:45 +00003839/// If node is a ret(tailcall) node, emit the specified tail call and return
3840/// true, otherwise return false.
3841///
3842/// FIXME: This whole thing should be a post-legalize optimization pass which
3843/// recognizes and transforms the dag. We don't want the selection phase doing
3844/// this stuff!!
3845///
3846bool ISel::EmitPotentialTailCall(SDNode *RetNode) {
3847 assert(RetNode->getOpcode() == ISD::RET && "Not a return");
3848
3849 SDOperand Chain = RetNode->getOperand(0);
3850
3851 // If this is a token factor node where one operand is a call, dig into it.
3852 SDOperand TokFactor;
3853 unsigned TokFactorOperand = 0;
3854 if (Chain.getOpcode() == ISD::TokenFactor) {
3855 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3856 if (Chain.getOperand(i).getOpcode() == ISD::CALLSEQ_END ||
3857 Chain.getOperand(i).getOpcode() == X86ISD::TAILCALL) {
3858 TokFactorOperand = i;
3859 TokFactor = Chain;
3860 Chain = Chain.getOperand(i);
3861 break;
3862 }
3863 if (TokFactor.Val == 0) return false; // No call operand.
3864 }
3865
3866 // Skip the CALLSEQ_END node if present.
3867 if (Chain.getOpcode() == ISD::CALLSEQ_END)
3868 Chain = Chain.getOperand(0);
3869
3870 // Is a tailcall the last control operation that occurs before the return?
3871 if (Chain.getOpcode() != X86ISD::TAILCALL)
3872 return false;
3873
3874 // If we return a value, is it the value produced by the call?
3875 if (RetNode->getNumOperands() > 1) {
3876 // Not returning the ret val of the call?
3877 if (Chain.Val->getNumValues() == 1 ||
3878 RetNode->getOperand(1) != Chain.getValue(1))
3879 return false;
3880
3881 if (RetNode->getNumOperands() > 2) {
3882 if (Chain.Val->getNumValues() == 2 ||
3883 RetNode->getOperand(2) != Chain.getValue(2))
3884 return false;
3885 }
3886 assert(RetNode->getNumOperands() <= 3);
3887 }
3888
3889 // CalleeCallArgAmt - The total number of bytes used for the callee arg area.
3890 // For FastCC, this will always be > 0.
3891 unsigned CalleeCallArgAmt =
3892 cast<ConstantSDNode>(Chain.getOperand(2))->getValue();
3893
3894 // CalleeCallArgPopAmt - The number of bytes in the call area popped by the
3895 // callee. For FastCC this will always be > 0, for CCC this is always 0.
3896 unsigned CalleeCallArgPopAmt =
3897 cast<ConstantSDNode>(Chain.getOperand(3))->getValue();
3898
3899 // There are several cases we can handle here. First, if the caller and
3900 // callee are both CCC functions, we can tailcall if the callee takes <= the
3901 // number of argument bytes that the caller does.
3902 if (CalleeCallArgPopAmt == 0 && // Callee is C CallingConv?
3903 X86Lowering.getBytesToPopOnReturn() == 0) { // Caller is C CallingConv?
3904 // Check to see if caller arg area size >= callee arg area size.
3905 if (X86Lowering.getBytesCallerReserves() >= CalleeCallArgAmt) {
3906 //std::cerr << "CCC TAILCALL UNIMP!\n";
3907 // If TokFactor is non-null, emit all operands.
3908
3909 //EmitCCCToCCCTailCall(Chain.Val);
3910 //return true;
3911 }
3912 return false;
3913 }
3914
3915 // Second, if both are FastCC functions, we can always perform the tail call.
3916 if (CalleeCallArgPopAmt && X86Lowering.getBytesToPopOnReturn()) {
3917 // If TokFactor is non-null, emit all operands before the call.
3918 if (TokFactor.Val) {
3919 for (unsigned i = 0, e = TokFactor.getNumOperands(); i != e; ++i)
3920 if (i != TokFactorOperand)
3921 Select(TokFactor.getOperand(i));
3922 }
3923
3924 EmitFastCCToFastCCTailCall(Chain.Val);
3925 return true;
3926 }
3927
3928 // We don't support mixed calls, due to issues with alignment. We could in
3929 // theory handle some mixed calls from CCC -> FastCC if the stack is properly
3930 // aligned (which depends on the number of arguments to the callee). TODO.
3931 return false;
3932}
3933
3934static SDOperand GetAdjustedArgumentStores(SDOperand Chain, int Offset,
3935 SelectionDAG &DAG) {
3936 MVT::ValueType StoreVT;
3937 switch (Chain.getOpcode()) {
3938 case ISD::CALLSEQ_START:
Chris Lattnerea035432005-05-15 06:07:10 +00003939 // If we found the start of the call sequence, we're done. We actually
3940 // strip off the CALLSEQ_START node, to avoid generating the
3941 // ADJCALLSTACKDOWN marker for the tail call.
3942 return Chain.getOperand(0);
Chris Lattner381e8872005-05-15 05:46:45 +00003943 case ISD::TokenFactor: {
3944 std::vector<SDOperand> Ops;
3945 Ops.reserve(Chain.getNumOperands());
3946 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3947 Ops.push_back(GetAdjustedArgumentStores(Chain.getOperand(i), Offset,DAG));
3948 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
3949 }
3950 case ISD::STORE: // Normal store
3951 StoreVT = Chain.getOperand(1).getValueType();
3952 break;
3953 case ISD::TRUNCSTORE: // FLOAT store
Chris Lattner9fadb4c2005-07-10 00:29:18 +00003954 StoreVT = cast<VTSDNode>(Chain.getOperand(4))->getVT();
Chris Lattner381e8872005-05-15 05:46:45 +00003955 break;
3956 }
3957
3958 SDOperand OrigDest = Chain.getOperand(2);
3959 unsigned OrigOffset;
3960
3961 if (OrigDest.getOpcode() == ISD::CopyFromReg) {
3962 OrigOffset = 0;
3963 assert(cast<RegSDNode>(OrigDest)->getReg() == X86::ESP);
3964 } else {
3965 // We expect only (ESP+C)
3966 assert(OrigDest.getOpcode() == ISD::ADD &&
3967 isa<ConstantSDNode>(OrigDest.getOperand(1)) &&
3968 OrigDest.getOperand(0).getOpcode() == ISD::CopyFromReg &&
3969 cast<RegSDNode>(OrigDest.getOperand(0))->getReg() == X86::ESP);
3970 OrigOffset = cast<ConstantSDNode>(OrigDest.getOperand(1))->getValue();
3971 }
3972
3973 // Compute the new offset from the incoming ESP value we wish to use.
3974 unsigned NewOffset = OrigOffset + Offset;
3975
3976 unsigned OpSize = (MVT::getSizeInBits(StoreVT)+7)/8; // Bits -> Bytes
3977 MachineFunction &MF = DAG.getMachineFunction();
3978 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, NewOffset);
3979 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
3980
3981 SDOperand InChain = GetAdjustedArgumentStores(Chain.getOperand(0), Offset,
3982 DAG);
3983 if (Chain.getOpcode() == ISD::STORE)
3984 return DAG.getNode(ISD::STORE, MVT::Other, InChain, Chain.getOperand(1),
3985 FIN);
3986 assert(Chain.getOpcode() == ISD::TRUNCSTORE);
3987 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, InChain, Chain.getOperand(1),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00003988 FIN, DAG.getSrcValue(NULL), DAG.getValueType(StoreVT));
Chris Lattner381e8872005-05-15 05:46:45 +00003989}
3990
3991
3992/// EmitFastCCToFastCCTailCall - Given a tailcall in the tail position to a
3993/// fastcc function from a fastcc function, emit the code to emit a 'proper'
3994/// tail call.
3995void ISel::EmitFastCCToFastCCTailCall(SDNode *TailCallNode) {
3996 unsigned CalleeCallArgSize =
3997 cast<ConstantSDNode>(TailCallNode->getOperand(2))->getValue();
3998 unsigned CallerArgSize = X86Lowering.getBytesToPopOnReturn();
3999
4000 //std::cerr << "****\n*** EMITTING TAIL CALL!\n****\n";
4001
4002 // Adjust argument stores. Instead of storing to [ESP], f.e., store to frame
4003 // indexes that are relative to the incoming ESP. If the incoming and
4004 // outgoing arg sizes are the same we will store to [InESP] instead of
4005 // [CurESP] and the ESP referenced will be relative to the incoming function
4006 // ESP.
4007 int ESPOffset = CallerArgSize-CalleeCallArgSize;
4008 SDOperand AdjustedArgStores =
4009 GetAdjustedArgumentStores(TailCallNode->getOperand(0), ESPOffset, *TheDAG);
4010
4011 // Copy the return address of the caller into a virtual register so we don't
4012 // clobber it.
4013 SDOperand RetVal;
4014 if (ESPOffset) {
4015 SDOperand RetValAddr = X86Lowering.getReturnAddressFrameIndex(*TheDAG);
4016 RetVal = TheDAG->getLoad(MVT::i32, TheDAG->getEntryNode(),
4017 RetValAddr, TheDAG->getSrcValue(NULL));
4018 SelectExpr(RetVal);
4019 }
4020
4021 // Codegen all of the argument stores.
4022 Select(AdjustedArgStores);
4023
4024 if (RetVal.Val) {
4025 // Emit a store of the saved ret value to the new location.
4026 MachineFunction &MF = TheDAG->getMachineFunction();
4027 int ReturnAddrFI = MF.getFrameInfo()->CreateFixedObject(4, ESPOffset-4);
4028 SDOperand RetValAddr = TheDAG->getFrameIndex(ReturnAddrFI, MVT::i32);
4029 Select(TheDAG->getNode(ISD::STORE, MVT::Other, TheDAG->getEntryNode(),
4030 RetVal, RetValAddr));
4031 }
4032
4033 // Get the destination value.
4034 SDOperand Callee = TailCallNode->getOperand(1);
4035 bool isDirect = isa<GlobalAddressSDNode>(Callee) ||
4036 isa<ExternalSymbolSDNode>(Callee);
Chris Lattner9cb2d612005-06-17 13:23:32 +00004037 unsigned CalleeReg = 0;
Chris Lattner381e8872005-05-15 05:46:45 +00004038 if (!isDirect) CalleeReg = SelectExpr(Callee);
4039
4040 unsigned RegOp1 = 0;
4041 unsigned RegOp2 = 0;
4042
4043 if (TailCallNode->getNumOperands() > 4) {
4044 // The first value is passed in (a part of) EAX, the second in EDX.
4045 RegOp1 = SelectExpr(TailCallNode->getOperand(4));
4046 if (TailCallNode->getNumOperands() > 5)
4047 RegOp2 = SelectExpr(TailCallNode->getOperand(5));
Jeff Cohen00b168892005-07-27 06:12:32 +00004048
Chris Lattner381e8872005-05-15 05:46:45 +00004049 switch (TailCallNode->getOperand(4).getValueType()) {
4050 default: assert(0 && "Bad thing to pass in regs");
4051 case MVT::i1:
4052 case MVT::i8:
4053 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(RegOp1);
4054 RegOp1 = X86::AL;
4055 break;
4056 case MVT::i16:
4057 BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1);
4058 RegOp1 = X86::AX;
4059 break;
4060 case MVT::i32:
4061 BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);
4062 RegOp1 = X86::EAX;
4063 break;
4064 }
4065 if (RegOp2)
4066 switch (TailCallNode->getOperand(5).getValueType()) {
4067 default: assert(0 && "Bad thing to pass in regs");
4068 case MVT::i1:
4069 case MVT::i8:
4070 BuildMI(BB, X86::MOV8rr, 1, X86::DL).addReg(RegOp2);
4071 RegOp2 = X86::DL;
4072 break;
4073 case MVT::i16:
4074 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
4075 RegOp2 = X86::DX;
4076 break;
4077 case MVT::i32:
4078 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
4079 RegOp2 = X86::EDX;
4080 break;
4081 }
4082 }
4083
4084 // Adjust ESP.
4085 if (ESPOffset)
4086 BuildMI(BB, X86::ADJSTACKPTRri, 2,
4087 X86::ESP).addReg(X86::ESP).addImm(ESPOffset);
4088
4089 // TODO: handle jmp [mem]
4090 if (!isDirect) {
4091 BuildMI(BB, X86::TAILJMPr, 1).addReg(CalleeReg);
4092 } else if (GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Callee)){
Chris Lattner16cb6f82005-05-19 05:54:33 +00004093 BuildMI(BB, X86::TAILJMPd, 1).addGlobalAddress(GASD->getGlobal(), true);
Chris Lattner381e8872005-05-15 05:46:45 +00004094 } else {
4095 ExternalSymbolSDNode *ESSDN = cast<ExternalSymbolSDNode>(Callee);
4096 BuildMI(BB, X86::TAILJMPd, 1).addExternalSymbol(ESSDN->getSymbol(), true);
4097 }
4098 // ADD IMPLICIT USE RegOp1/RegOp2's
4099}
4100
Chris Lattnere10269b2005-01-17 19:25:26 +00004101
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004102void ISel::Select(SDOperand N) {
4103 unsigned Tmp1, Tmp2, Opc;
4104
Nate Begeman85fdeb22005-03-24 04:39:54 +00004105 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004106 return; // Already selected.
4107
Chris Lattner989de032005-01-11 06:14:36 +00004108 SDNode *Node = N.Val;
4109
4110 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004111 default:
Chris Lattner989de032005-01-11 06:14:36 +00004112 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004113 assert(0 && "Node not handled yet!");
4114 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00004115 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00004116 if (Node->getNumOperands() == 2) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004117 bool OneFirst =
Chris Lattner1d50b7f2005-01-13 19:56:00 +00004118 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
4119 Select(Node->getOperand(OneFirst));
4120 Select(Node->getOperand(!OneFirst));
4121 } else {
4122 std::vector<std::pair<unsigned, unsigned> > OpsP;
4123 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
4124 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
4125 std::sort(OpsP.begin(), OpsP.end());
4126 std::reverse(OpsP.begin(), OpsP.end());
4127 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
4128 Select(Node->getOperand(OpsP[i].second));
4129 }
Chris Lattnerc3580712005-01-13 18:01:36 +00004130 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004131 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00004132 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4133 Select(N.getOperand(0));
4134 Tmp1 = SelectExpr(N.getOperand(1));
4135 } else {
4136 Tmp1 = SelectExpr(N.getOperand(1));
4137 Select(N.getOperand(0));
4138 }
Chris Lattner18c2f132005-01-13 20:50:02 +00004139 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004140
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004141 if (Tmp1 != Tmp2) {
4142 switch (N.getOperand(1).getValueType()) {
4143 default: assert(0 && "Invalid type for operation!");
4144 case MVT::i1:
4145 case MVT::i8: Opc = X86::MOV8rr; break;
4146 case MVT::i16: Opc = X86::MOV16rr; break;
4147 case MVT::i32: Opc = X86::MOV32rr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004148 case MVT::f32: Opc = X86::MOVAPSrr; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00004149 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00004150 if (X86ScalarSSE) {
4151 Opc = X86::MOVAPDrr;
4152 } else {
Jeff Cohen00b168892005-07-27 06:12:32 +00004153 Opc = X86::FpMOV;
4154 ContainsFPCode = true;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004155 }
4156 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004157 }
4158 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
4159 }
4160 return;
4161 case ISD::RET:
Chris Lattner381e8872005-05-15 05:46:45 +00004162 if (N.getOperand(0).getOpcode() == ISD::CALLSEQ_END ||
4163 N.getOperand(0).getOpcode() == X86ISD::TAILCALL ||
4164 N.getOperand(0).getOpcode() == ISD::TokenFactor)
4165 if (EmitPotentialTailCall(Node))
4166 return;
4167
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004168 switch (N.getNumOperands()) {
4169 default:
4170 assert(0 && "Unknown return instruction!");
4171 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004172 assert(N.getOperand(1).getValueType() == MVT::i32 &&
Jeff Cohen00b168892005-07-27 06:12:32 +00004173 N.getOperand(2).getValueType() == MVT::i32 &&
4174 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00004175 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
4176 Tmp1 = SelectExpr(N.getOperand(1));
4177 Tmp2 = SelectExpr(N.getOperand(2));
4178 } else {
4179 Tmp2 = SelectExpr(N.getOperand(2));
4180 Tmp1 = SelectExpr(N.getOperand(1));
4181 }
4182 Select(N.getOperand(0));
4183
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004184 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4185 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004186 break;
4187 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00004188 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4189 Select(N.getOperand(0));
4190 Tmp1 = SelectExpr(N.getOperand(1));
4191 } else {
4192 Tmp1 = SelectExpr(N.getOperand(1));
4193 Select(N.getOperand(0));
4194 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004195 switch (N.getOperand(1).getValueType()) {
4196 default: assert(0 && "All other types should have been promoted!!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00004197 case MVT::f32:
4198 if (X86ScalarSSE) {
4199 // Spill the value to memory and reload it into top of stack.
4200 unsigned Size = MVT::getSizeInBits(MVT::f32)/8;
4201 MachineFunction *F = BB->getParent();
4202 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
4203 addFrameReference(BuildMI(BB, X86::MOVSSmr, 5), FrameIdx).addReg(Tmp1);
4204 addFrameReference(BuildMI(BB, X86::FLD32m, 4, X86::FP0), FrameIdx);
4205 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen00b168892005-07-27 06:12:32 +00004206 ContainsFPCode = true;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004207 } else {
4208 assert(0 && "MVT::f32 only legal with scalar sse fp");
4209 abort();
4210 }
4211 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004212 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00004213 if (X86ScalarSSE) {
4214 // Spill the value to memory and reload it into top of stack.
4215 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
4216 MachineFunction *F = BB->getParent();
4217 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
4218 addFrameReference(BuildMI(BB, X86::MOVSDmr, 5), FrameIdx).addReg(Tmp1);
4219 addFrameReference(BuildMI(BB, X86::FLD64m, 4, X86::FP0), FrameIdx);
4220 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen00b168892005-07-27 06:12:32 +00004221 ContainsFPCode = true;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004222 } else {
4223 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
4224 }
4225 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004226 case MVT::i32:
Nate Begemanf63be7d2005-07-06 18:59:04 +00004227 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4228 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004229 }
4230 break;
4231 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00004232 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004233 break;
4234 }
Chris Lattner3648c672005-05-13 21:44:04 +00004235 if (X86Lowering.getBytesToPopOnReturn() == 0)
4236 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
4237 else
4238 BuildMI(BB, X86::RETI, 1).addImm(X86Lowering.getBytesToPopOnReturn());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004239 return;
4240 case ISD::BR: {
4241 Select(N.getOperand(0));
4242 MachineBasicBlock *Dest =
4243 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
4244 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
4245 return;
4246 }
4247
4248 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004249 MachineBasicBlock *Dest =
4250 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00004251
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004252 // Try to fold a setcc into the branch. If this fails, emit a test/jne
4253 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00004254 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
4255 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4256 Select(N.getOperand(0));
4257 Tmp1 = SelectExpr(N.getOperand(1));
4258 } else {
4259 Tmp1 = SelectExpr(N.getOperand(1));
4260 Select(N.getOperand(0));
4261 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004262 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
4263 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
4264 }
Chris Lattner11333092005-01-11 03:11:44 +00004265
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004266 return;
4267 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004268
Chris Lattner4df0de92005-01-17 00:00:33 +00004269 case ISD::LOAD:
4270 // If this load could be folded into the only using instruction, and if it
4271 // is safe to emit the instruction here, try to do so now.
4272 if (Node->hasNUsesOfValue(1, 0)) {
4273 SDOperand TheVal = N.getValue(0);
4274 SDNode *User = 0;
4275 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
4276 assert(UI != Node->use_end() && "Didn't find use!");
4277 SDNode *UN = *UI;
4278 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
4279 if (UN->getOperand(i) == TheVal) {
4280 User = UN;
4281 goto FoundIt;
4282 }
4283 }
4284 FoundIt:
4285 // Only handle unary operators right now.
4286 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00004287 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00004288 SelectExpr(SDOperand(User, 0));
4289 return;
4290 }
4291 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00004292 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00004293 SelectExpr(N);
4294 return;
Chris Lattner966cdfb2005-05-09 21:17:38 +00004295 case ISD::READPORT:
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004296 case ISD::EXTLOAD:
4297 case ISD::SEXTLOAD:
4298 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004299 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner239738a2005-05-14 08:48:15 +00004300 case X86ISD::TAILCALL:
4301 case X86ISD::CALL:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00004302 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004303 SelectExpr(N);
4304 return;
Chris Lattnerc6f41812005-05-12 23:06:28 +00004305 case ISD::CopyFromReg:
Chris Lattner67649df2005-05-14 06:52:07 +00004306 case X86ISD::FILD64m:
Chris Lattnerc6f41812005-05-12 23:06:28 +00004307 ExprMap.erase(N);
4308 SelectExpr(N.getValue(0));
4309 return;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004310
Chris Lattner01546c52005-07-30 00:05:54 +00004311 case X86ISD::FP_TO_INT16_IN_MEM:
4312 case X86ISD::FP_TO_INT32_IN_MEM:
Chris Lattnerf7443da2005-07-29 00:54:34 +00004313 case X86ISD::FP_TO_INT64_IN_MEM: {
Chris Lattner745d5382005-07-29 00:40:01 +00004314 assert(N.getOperand(1).getValueType() == MVT::f64);
4315 X86AddressMode AM;
4316 Select(N.getOperand(0)); // Select the token chain
4317
4318 unsigned ValReg;
4319 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
4320 ValReg = SelectExpr(N.getOperand(1));
4321 SelectAddress(N.getOperand(2), AM);
4322 } else {
4323 SelectAddress(N.getOperand(2), AM);
4324 ValReg = SelectExpr(N.getOperand(1));
4325 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004326
Chris Lattnerf7443da2005-07-29 00:54:34 +00004327 // Change the floating point control register to use "round towards zero"
4328 // mode when truncating to an integer value.
4329 //
4330 MachineFunction *F = BB->getParent();
4331 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
4332 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004333
Chris Lattnerf7443da2005-07-29 00:54:34 +00004334 // Load the old value of the high byte of the control word...
Chris Lattnera35e1df2005-07-30 00:17:52 +00004335 unsigned OldCW = MakeReg(MVT::i16);
4336 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004337
Chris Lattnerf7443da2005-07-29 00:54:34 +00004338 // Set the high part to be round to zero...
Chris Lattnera88da082005-07-30 00:43:00 +00004339 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004340
Chris Lattnerf7443da2005-07-29 00:54:34 +00004341 // Reload the modified control word now...
4342 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004343
Chris Lattnerf7443da2005-07-29 00:54:34 +00004344 // Restore the memory image of control word to original value
Chris Lattnera35e1df2005-07-30 00:17:52 +00004345 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
Chris Lattner01546c52005-07-30 00:05:54 +00004346
4347 // Get the X86 opcode to use.
4348 switch (N.getOpcode()) {
4349 case X86ISD::FP_TO_INT16_IN_MEM: Tmp1 = X86::FIST16m; break;
4350 case X86ISD::FP_TO_INT32_IN_MEM: Tmp1 = X86::FIST32m; break;
4351 case X86ISD::FP_TO_INT64_IN_MEM: Tmp1 = X86::FISTP64m; break;
4352 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004353
Chris Lattner01546c52005-07-30 00:05:54 +00004354 addFullAddress(BuildMI(BB, Tmp1, 5), AM).addReg(ValReg);
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004355
Chris Lattnerf7443da2005-07-29 00:54:34 +00004356 // Reload the original control word now.
4357 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner745d5382005-07-29 00:40:01 +00004358 return;
4359 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004360
Chris Lattner9fadb4c2005-07-10 00:29:18 +00004361 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr, SRCVALUE, storety
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004362 X86AddressMode AM;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00004363 MVT::ValueType StoredTy = cast<VTSDNode>(N.getOperand(4))->getVT();
Chris Lattnerda2ce112005-01-16 07:34:08 +00004364 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
4365 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
4366 && "Unsupported TRUNCSTORE for this target!");
4367
4368 if (StoredTy == MVT::i16) {
4369 // FIXME: This is here just to allow testing. X86 doesn't really have a
4370 // TRUNCSTORE i16 operation, but this is required for targets that do not
4371 // have 16-bit integer registers. We occasionally disable 16-bit integer
4372 // registers to test the promotion code.
4373 Select(N.getOperand(0));
4374 Tmp1 = SelectExpr(N.getOperand(1));
4375 SelectAddress(N.getOperand(2), AM);
4376
4377 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4378 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
4379 return;
4380 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004381
4382 // Store of constant bool?
4383 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
4384 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4385 Select(N.getOperand(0));
4386 SelectAddress(N.getOperand(2), AM);
4387 } else {
4388 SelectAddress(N.getOperand(2), AM);
4389 Select(N.getOperand(0));
4390 }
4391 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
4392 return;
4393 }
4394
4395 switch (StoredTy) {
4396 default: assert(0 && "Cannot truncstore this type!");
4397 case MVT::i1: Opc = X86::MOV8mr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004398 case MVT::f32:
Jeff Cohen00b168892005-07-27 06:12:32 +00004399 assert(!X86ScalarSSE && "Cannot truncstore scalar SSE regs");
Nate Begemanf63be7d2005-07-06 18:59:04 +00004400 Opc = X86::FST32m; break;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004401 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004402
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004403 std::vector<std::pair<unsigned, unsigned> > RP;
4404 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
4405 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
4406 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
4407 std::sort(RP.begin(), RP.end());
4408
Chris Lattner572dd082005-02-23 05:57:21 +00004409 Tmp1 = 0; // Silence a warning.
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004410 for (unsigned i = 0; i != 3; ++i)
4411 switch (RP[2-i].second) {
4412 default: assert(0 && "Unknown operand number!");
4413 case 0: Select(N.getOperand(0)); break;
4414 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
4415 case 2: SelectAddress(N.getOperand(2), AM); break;
4416 }
4417
4418 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
4419 return;
4420 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004421 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004422 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004423
4424 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
4425 Opc = 0;
4426 switch (CN->getValueType(0)) {
4427 default: assert(0 && "Invalid type for operation!");
4428 case MVT::i1:
4429 case MVT::i8: Opc = X86::MOV8mi; break;
4430 case MVT::i16: Opc = X86::MOV16mi; break;
4431 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004432 }
4433 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00004434 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4435 Select(N.getOperand(0));
4436 SelectAddress(N.getOperand(2), AM);
4437 } else {
4438 SelectAddress(N.getOperand(2), AM);
4439 Select(N.getOperand(0));
4440 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004441 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
4442 return;
4443 }
Chris Lattner75f354b2005-04-21 19:03:24 +00004444 } else if (GlobalAddressSDNode *GA =
4445 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
4446 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
4447
4448 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4449 Select(N.getOperand(0));
4450 SelectAddress(N.getOperand(2), AM);
4451 } else {
4452 SelectAddress(N.getOperand(2), AM);
4453 Select(N.getOperand(0));
4454 }
Nate Begeman16b04f32005-07-15 00:38:55 +00004455 GlobalValue *GV = GA->getGlobal();
4456 // For Darwin, external and weak symbols are indirect, so we want to load
4457 // the value at address GV, not the value of GV itself.
Jeff Cohen00b168892005-07-27 06:12:32 +00004458 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begeman16b04f32005-07-15 00:38:55 +00004459 (GV->hasWeakLinkage() || GV->isExternal())) {
4460 Tmp1 = MakeReg(MVT::i32);
4461 BuildMI(BB, X86::MOV32rm, 4, Tmp1).addReg(0).addZImm(1).addReg(0)
4462 .addGlobalAddress(GV, false, 0);
4463 addFullAddress(BuildMI(BB, X86::MOV32mr, 4+1),AM).addReg(Tmp1);
4464 } else {
4465 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),AM).addGlobalAddress(GV);
4466 }
Chris Lattner75f354b2005-04-21 19:03:24 +00004467 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004468 }
Chris Lattner837caa72005-01-11 23:21:30 +00004469
4470 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00004471 if (TryToFoldLoadOpStore(Node))
4472 return;
Chris Lattner837caa72005-01-11 23:21:30 +00004473
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004474 switch (N.getOperand(1).getValueType()) {
4475 default: assert(0 && "Cannot store this type!");
4476 case MVT::i1:
4477 case MVT::i8: Opc = X86::MOV8mr; break;
4478 case MVT::i16: Opc = X86::MOV16mr; break;
4479 case MVT::i32: Opc = X86::MOV32mr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004480 case MVT::f32: Opc = X86::MOVSSmr; break;
4481 case MVT::f64: Opc = X86ScalarSSE ? X86::MOVSDmr : X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004482 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004483
Chris Lattner11333092005-01-11 03:11:44 +00004484 std::vector<std::pair<unsigned, unsigned> > RP;
4485 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
4486 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
4487 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
4488 std::sort(RP.begin(), RP.end());
4489
Chris Lattner572dd082005-02-23 05:57:21 +00004490 Tmp1 = 0; // Silence a warning.
Chris Lattner11333092005-01-11 03:11:44 +00004491 for (unsigned i = 0; i != 3; ++i)
4492 switch (RP[2-i].second) {
4493 default: assert(0 && "Unknown operand number!");
4494 case 0: Select(N.getOperand(0)); break;
4495 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00004496 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00004497 }
4498
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004499 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
4500 return;
4501 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00004502 case ISD::CALLSEQ_START:
Chris Lattner3648c672005-05-13 21:44:04 +00004503 Select(N.getOperand(0));
4504 // Stack amount
4505 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
4506 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(Tmp1);
4507 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00004508 case ISD::CALLSEQ_END:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004509 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004510 return;
Chris Lattner989de032005-01-11 06:14:36 +00004511 case ISD::MEMSET: {
4512 Select(N.getOperand(0)); // Select the chain.
4513 unsigned Align =
4514 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
4515 if (Align == 0) Align = 1;
4516
4517 // Turn the byte code into # iterations
4518 unsigned CountReg;
4519 unsigned Opcode;
4520 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
4521 unsigned Val = ValC->getValue() & 255;
4522
4523 // If the value is a constant, then we can potentially use larger sets.
4524 switch (Align & 3) {
4525 case 2: // WORD aligned
4526 CountReg = MakeReg(MVT::i32);
4527 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4528 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
4529 } else {
4530 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4531 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
4532 }
4533 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
4534 Opcode = X86::REP_STOSW;
4535 break;
4536 case 0: // DWORD aligned
4537 CountReg = MakeReg(MVT::i32);
4538 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4539 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
4540 } else {
4541 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4542 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
4543 }
4544 Val = (Val << 8) | Val;
4545 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
4546 Opcode = X86::REP_STOSD;
4547 break;
4548 default: // BYTE aligned
4549 CountReg = SelectExpr(Node->getOperand(3));
4550 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
4551 Opcode = X86::REP_STOSB;
4552 break;
4553 }
4554 } else {
4555 // If it's not a constant value we are storing, just fall back. We could
4556 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
4557 unsigned ValReg = SelectExpr(Node->getOperand(2));
4558 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
4559 CountReg = SelectExpr(Node->getOperand(3));
4560 Opcode = X86::REP_STOSB;
4561 }
4562
4563 // No matter what the alignment is, we put the source in ESI, the
4564 // destination in EDI, and the count in ECX.
4565 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
4566 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
4567 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
4568 BuildMI(BB, Opcode, 0);
4569 return;
4570 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00004571 case ISD::MEMCPY: {
Chris Lattner31805bf2005-01-11 06:19:26 +00004572 Select(N.getOperand(0)); // Select the chain.
4573 unsigned Align =
4574 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
4575 if (Align == 0) Align = 1;
4576
4577 // Turn the byte code into # iterations
4578 unsigned CountReg;
4579 unsigned Opcode;
4580 switch (Align & 3) {
4581 case 2: // WORD aligned
4582 CountReg = MakeReg(MVT::i32);
4583 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4584 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
4585 } else {
4586 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4587 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
4588 }
4589 Opcode = X86::REP_MOVSW;
4590 break;
4591 case 0: // DWORD aligned
4592 CountReg = MakeReg(MVT::i32);
4593 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4594 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
4595 } else {
4596 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4597 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
4598 }
4599 Opcode = X86::REP_MOVSD;
4600 break;
4601 default: // BYTE aligned
4602 CountReg = SelectExpr(Node->getOperand(3));
4603 Opcode = X86::REP_MOVSB;
4604 break;
4605 }
4606
4607 // No matter what the alignment is, we put the source in ESI, the
4608 // destination in EDI, and the count in ECX.
4609 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
4610 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
4611 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
4612 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
4613 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
4614 BuildMI(BB, Opcode, 0);
4615 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004616 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00004617 case ISD::WRITEPORT:
4618 if (Node->getOperand(2).getValueType() != MVT::i16) {
4619 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
4620 exit(1);
4621 }
4622 Select(Node->getOperand(0)); // Emit the chain.
4623
4624 Tmp1 = SelectExpr(Node->getOperand(1));
4625 switch (Node->getOperand(1).getValueType()) {
4626 case MVT::i8:
4627 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
4628 Tmp2 = X86::OUT8ir; Opc = X86::OUT8rr;
4629 break;
4630 case MVT::i16:
4631 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(Tmp1);
4632 Tmp2 = X86::OUT16ir; Opc = X86::OUT16rr;
4633 break;
4634 case MVT::i32:
4635 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4636 Tmp2 = X86::OUT32ir; Opc = X86::OUT32rr;
4637 break;
4638 default:
4639 std::cerr << "llvm.writeport: invalid data type for X86 target";
4640 exit(1);
4641 }
4642
4643 // If the port is a single-byte constant, use the immediate form.
4644 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(2)))
4645 if ((CN->getValue() & 255) == CN->getValue()) {
4646 BuildMI(BB, Tmp2, 1).addImm(CN->getValue());
4647 return;
4648 }
4649
4650 // Otherwise, move the I/O port address into the DX register.
4651 unsigned Reg = SelectExpr(Node->getOperand(2));
4652 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
4653 BuildMI(BB, Opc, 0);
4654 return;
4655 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004656 assert(0 && "Should not be reached!");
4657}
4658
4659
4660/// createX86PatternInstructionSelector - This pass converts an LLVM function
4661/// into a machine code representation using pattern matching and a machine
4662/// description file.
4663///
4664FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004665 return new ISel(TM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004666}