blob: a244afd60189eb54d0536ec84baaf3be0f3da9ef [file] [log] [blame]
Chris Lattner88c8a232005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
Chris Lattner1d13a922005-01-10 22:10:13 +00002//
Chris Lattner88c8a232005-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 Brukmanc88330a2005-04-21 23:38:14 +00007//
Chris Lattner88c8a232005-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 Begemanf26625e2005-07-12 01:41:54 +000017#include "X86Subtarget.h"
Chris Lattner7ce7a8f2005-05-12 23:06:28 +000018#include "llvm/CallingConv.h"
Chris Lattner6972c312005-05-09 03:36:39 +000019#include "llvm/Constants.h"
20#include "llvm/Instructions.h"
Chris Lattner88c8a232005-01-07 07:49:41 +000021#include "llvm/Function.h"
Chris Lattner6972c312005-05-09 03:36:39 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner88c8a232005-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 Begemanf26625e2005-07-12 01:41:54 +000030#include "llvm/Target/TargetMachine.h"
Chris Lattnerdb68d392005-04-30 04:25:35 +000031#include "llvm/Target/TargetOptions.h"
Chris Lattner6972c312005-05-09 03:36:39 +000032#include "llvm/Support/CFG.h"
Chris Lattner88c8a232005-01-07 07:49:41 +000033#include "llvm/Support/MathExtras.h"
34#include "llvm/ADT/Statistic.h"
35#include <set>
Jeff Cohen407aa012005-01-12 04:29:05 +000036#include <algorithm>
Chris Lattner88c8a232005-01-07 07:49:41 +000037using namespace llvm;
38
Chris Lattner7ce7a8f2005-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 Lattnera36117b2005-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 Lattner1b3520c2005-05-14 08:48:15 +000056
Chris Lattner6dc60e82005-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 Lattner4738d1b2005-07-30 00:05:54 +000062 FP_TO_INT16_IN_MEM,
63 FP_TO_INT32_IN_MEM,
Chris Lattner6dc60e82005-07-29 00:54:34 +000064 FP_TO_INT64_IN_MEM,
Jeff Cohen546fd592005-07-30 18:33:25 +000065
Chris Lattner1b3520c2005-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 Lattnera36117b2005-05-14 06:52:07 +000088 };
89 }
90}
91
Chris Lattner88c8a232005-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 Lattner9f59d282005-01-09 00:01:27 +000097 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattnerdd66a412005-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 Lattner88c8a232005-01-07 07:49:41 +0000100 public:
101 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
102 // Set up the TargetLowering object.
Chris Lattnerc1f386c2005-01-17 00:00:33 +0000103
Chris Lattner5011ff02005-05-13 22:46:57 +0000104 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Chris Lattnerc1f386c2005-01-17 00:00:33 +0000105 setShiftAmountType(MVT::i8);
106 setSetCCResultType(MVT::i8);
Chris Lattner38fd9702005-04-07 19:41:46 +0000107 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnerd8d30662005-01-19 03:36:30 +0000108 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattnerc1f386c2005-01-17 00:00:33 +0000109
110 // Set up the register classes.
Nate Begeman8a093362005-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 Lattner88c8a232005-01-07 07:49:41 +0000114 addRegisterClass(MVT::i8, X86::R8RegisterClass);
115 addRegisterClass(MVT::i16, X86::R16RegisterClass);
116 addRegisterClass(MVT::i32, X86::R32RegisterClass);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000117
Chris Lattner507a2752005-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 Begeman7e74c832005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000129
Chris Lattner67756e22005-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 Lattner4738d1b2005-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 Lattner67756e22005-07-29 00:40:01 +0000137 }
Jeff Cohen546fd592005-07-30 18:33:25 +0000138
Chris Lattnerbc85c322005-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);
Nate Begeman4d959f62005-08-14 04:36:51 +0000144
145 if (!X86ScalarSSE)
146 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Chris Lattnerbc85c322005-07-29 01:00:29 +0000147
148 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
149 // this operation.
150 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
151 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Nate Begeman4d959f62005-08-14 04:36:51 +0000152 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
153
Chris Lattnera3a135a2005-04-09 03:22:37 +0000154 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Nate Begeman371e4952005-08-16 19:49:35 +0000155 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
Chris Lattnerb14a63a2005-01-16 07:34:08 +0000156 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
157 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnerb14a63a2005-01-16 07:34:08 +0000158 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerb14a63a2005-01-16 07:34:08 +0000159 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
160 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
Chris Lattner0815dcae2005-09-28 22:29:17 +0000161 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattner05ad4b82005-05-11 05:00:34 +0000162 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
163 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
164 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
165 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
166 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
167 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
Andrew Lenharth5e177822005-05-03 17:19:30 +0000168 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
169 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharthb8e94c32005-05-04 19:25:37 +0000170 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000171
Chris Lattner6c6a39a2005-05-09 20:37:29 +0000172 setOperationAction(ISD::READIO , MVT::i1 , Expand);
173 setOperationAction(ISD::READIO , MVT::i8 , Expand);
174 setOperationAction(ISD::READIO , MVT::i16 , Expand);
175 setOperationAction(ISD::READIO , MVT::i32 , Expand);
176 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
177 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
178 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
179 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000180
Chris Lattnerb14a63a2005-01-16 07:34:08 +0000181 // These should be promoted to a larger select which is supported.
Nate Begeman8a093362005-07-06 18:59:04 +0000182 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Chris Lattnerb14a63a2005-01-16 07:34:08 +0000183 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000184
Nate Begeman8a093362005-07-06 18:59:04 +0000185 if (X86ScalarSSE) {
186 // Set up the FP register classes.
Nate Begeman9d7008b2005-10-14 22:06:00 +0000187 addRegisterClass(MVT::f32, X86::V4F4RegisterClass);
188 addRegisterClass(MVT::f64, X86::V2F8RegisterClass);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000189
Nate Begeman7e74c832005-07-16 02:02:34 +0000190 // SSE has no load+extend ops
Nate Begeman8a093362005-07-06 18:59:04 +0000191 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
192 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
Nate Begeman7e74c832005-07-16 02:02:34 +0000193
194 // SSE has no i16 to fp conversion, only i32
195 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
Nate Begeman8d394eb2005-08-03 23:26:28 +0000196 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
Nate Begeman7e74c832005-07-16 02:02:34 +0000197
Nate Begemane5394d42005-08-14 18:37:02 +0000198 // Expand FP_TO_UINT into a select.
199 // FIXME: We would like to use a Custom expander here eventually to do
200 // the optimal thing for SSE vs. the default expansion in the legalizer.
201 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
202
Nate Begeman8a093362005-07-06 18:59:04 +0000203 // We don't support sin/cos/sqrt/fmod
204 setOperationAction(ISD::FSIN , MVT::f64, Expand);
205 setOperationAction(ISD::FCOS , MVT::f64, Expand);
206 setOperationAction(ISD::FABS , MVT::f64, Expand);
207 setOperationAction(ISD::FNEG , MVT::f64, Expand);
Chris Lattner0815dcae2005-09-28 22:29:17 +0000208 setOperationAction(ISD::FREM , MVT::f64, Expand);
Nate Begeman8a093362005-07-06 18:59:04 +0000209 setOperationAction(ISD::FSIN , MVT::f32, Expand);
210 setOperationAction(ISD::FCOS , MVT::f32, Expand);
211 setOperationAction(ISD::FABS , MVT::f32, Expand);
212 setOperationAction(ISD::FNEG , MVT::f32, Expand);
Chris Lattner0815dcae2005-09-28 22:29:17 +0000213 setOperationAction(ISD::FREM , MVT::f32, Expand);
Nate Begeman8d394eb2005-08-03 23:26:28 +0000214
215 addLegalFPImmediate(+0.0); // xorps / xorpd
Nate Begeman8a093362005-07-06 18:59:04 +0000216 } else {
217 // Set up the FP register classes.
218 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000219
Nate Begeman8a093362005-07-06 18:59:04 +0000220 if (!UnsafeFPMath) {
221 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
222 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
223 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000224
Nate Begeman8a093362005-07-06 18:59:04 +0000225 addLegalFPImmediate(+0.0); // FLD0
226 addLegalFPImmediate(+1.0); // FLD1
227 addLegalFPImmediate(-0.0); // FLD0/FCHS
228 addLegalFPImmediate(-1.0); // FLD1/FCHS
229 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000230 computeRegisterProperties();
Reid Spencerd37d8542005-07-19 04:52:44 +0000231
232 maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores
233 maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores
234 maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores
Reid Spenceraa7fbca2005-08-27 19:09:48 +0000235 allowUnalignedMemoryAccesses = true; // x86 supports it!
Chris Lattner88c8a232005-01-07 07:49:41 +0000236 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000237
Chris Lattnerc0e369e2005-05-13 21:44:04 +0000238 // Return the number of bytes that a function should pop when it returns (in
239 // addition to the space used by the return address).
240 //
241 unsigned getBytesToPopOnReturn() const { return BytesToPopOnReturn; }
242
Chris Lattnerdd66a412005-05-15 05:46:45 +0000243 // Return the number of bytes that the caller reserves for arguments passed
244 // to this function.
245 unsigned getBytesCallerReserves() const { return BytesCallerReserves; }
246
Chris Lattnera36117b2005-05-14 06:52:07 +0000247 /// LowerOperation - Provide custom lowering hooks for some operations.
248 ///
249 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
250
Chris Lattner88c8a232005-01-07 07:49:41 +0000251 /// LowerArguments - This hook must be implemented to indicate how we should
252 /// lower the arguments for the specified function, into the specified DAG.
253 virtual std::vector<SDOperand>
254 LowerArguments(Function &F, SelectionDAG &DAG);
255
256 /// LowerCallTo - This hook lowers an abstract call to a function into an
257 /// actual call.
Chris Lattnerb52e0412005-01-08 19:28:19 +0000258 virtual std::pair<SDOperand, SDOperand>
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000259 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattner2e77db62005-05-13 18:50:42 +0000260 bool isTailCall, SDOperand Callee, ArgListTy &Args,
261 SelectionDAG &DAG);
Chris Lattner9f59d282005-01-09 00:01:27 +0000262
Chris Lattnera7220852005-07-05 19:58:54 +0000263 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
264 Value *VAListV, SelectionDAG &DAG);
Chris Lattner9f59d282005-01-09 00:01:27 +0000265 virtual std::pair<SDOperand,SDOperand>
Chris Lattnera7220852005-07-05 19:58:54 +0000266 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
267 const Type *ArgTy, SelectionDAG &DAG);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000268
Chris Lattner9f59d282005-01-09 00:01:27 +0000269 virtual std::pair<SDOperand, SDOperand>
270 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
271 SelectionDAG &DAG);
Chris Lattnerdd66a412005-05-15 05:46:45 +0000272
273 SDOperand getReturnAddressFrameIndex(SelectionDAG &DAG);
274
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000275 private:
276 // C Calling Convention implementation.
277 std::vector<SDOperand> LowerCCCArguments(Function &F, SelectionDAG &DAG);
278 std::pair<SDOperand, SDOperand>
279 LowerCCCCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
Chris Lattnerf27e31d2005-05-13 20:29:13 +0000280 bool isTailCall,
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000281 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000282
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000283 // Fast Calling Convention implementation.
284 std::vector<SDOperand> LowerFastCCArguments(Function &F, SelectionDAG &DAG);
285 std::pair<SDOperand, SDOperand>
Chris Lattnerf27e31d2005-05-13 20:29:13 +0000286 LowerFastCCCallTo(SDOperand Chain, const Type *RetTy, bool isTailCall,
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000287 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner88c8a232005-01-07 07:49:41 +0000288 };
289}
290
Chris Lattner88c8a232005-01-07 07:49:41 +0000291std::vector<SDOperand>
292X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000293 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
294 return LowerFastCCArguments(F, DAG);
295 return LowerCCCArguments(F, DAG);
296}
297
298std::pair<SDOperand, SDOperand>
299X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
300 bool isVarArg, unsigned CallingConv,
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000301 bool isTailCall,
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000302 SDOperand Callee, ArgListTy &Args,
303 SelectionDAG &DAG) {
304 assert((!isVarArg || CallingConv == CallingConv::C) &&
305 "Only C takes varargs!");
306 if (CallingConv == CallingConv::Fast && EnableFastCC)
Chris Lattnerf27e31d2005-05-13 20:29:13 +0000307 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
308 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000309}
310
311//===----------------------------------------------------------------------===//
Chris Lattner5011ff02005-05-13 22:46:57 +0000312// C Calling Convention implementation
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000313//===----------------------------------------------------------------------===//
314
315std::vector<SDOperand>
316X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000317 std::vector<SDOperand> ArgValues;
318
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000319 MachineFunction &MF = DAG.getMachineFunction();
320 MachineFrameInfo *MFI = MF.getFrameInfo();
321
Chris Lattner88c8a232005-01-07 07:49:41 +0000322 // Add DAG nodes to load the arguments... On entry to a function on the X86,
323 // the stack frame looks like this:
324 //
325 // [ESP] -- return address
326 // [ESP + 4] -- first argument (leftmost lexically)
327 // [ESP + 8] -- second argument, if first argument is four bytes in size
Misha Brukmanc88330a2005-04-21 23:38:14 +0000328 // ...
Chris Lattner88c8a232005-01-07 07:49:41 +0000329 //
Chris Lattner88c8a232005-01-07 07:49:41 +0000330 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattner531f9e92005-03-15 04:54:21 +0000331 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000332 MVT::ValueType ObjectVT = getValueType(I->getType());
333 unsigned ArgIncrement = 4;
334 unsigned ObjSize;
335 switch (ObjectVT) {
336 default: assert(0 && "Unhandled argument type!");
337 case MVT::i1:
338 case MVT::i8: ObjSize = 1; break;
339 case MVT::i16: ObjSize = 2; break;
340 case MVT::i32: ObjSize = 4; break;
341 case MVT::i64: ObjSize = ArgIncrement = 8; break;
342 case MVT::f32: ObjSize = 4; break;
343 case MVT::f64: ObjSize = ArgIncrement = 8; break;
344 }
345 // Create the frame index object for this incoming parameter...
346 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Misha Brukmanc88330a2005-04-21 23:38:14 +0000347
Chris Lattner88c8a232005-01-07 07:49:41 +0000348 // Create the SelectionDAG nodes corresponding to a load from this parameter
349 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
350
351 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
352 // dead loads.
353 SDOperand ArgValue;
354 if (!I->use_empty())
Chris Lattnerdaa064d2005-05-09 05:40:26 +0000355 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
356 DAG.getSrcValue(NULL));
Chris Lattner88c8a232005-01-07 07:49:41 +0000357 else {
358 if (MVT::isInteger(ObjectVT))
359 ArgValue = DAG.getConstant(0, ObjectVT);
360 else
361 ArgValue = DAG.getConstantFP(0, ObjectVT);
362 }
363 ArgValues.push_back(ArgValue);
364
365 ArgOffset += ArgIncrement; // Move on to the next argument...
366 }
367
368 // If the function takes variable number of arguments, make a frame index for
369 // the start of the first vararg value... for expansion of llvm.va_start.
370 if (F.isVarArg())
371 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattnerc0e369e2005-05-13 21:44:04 +0000372 ReturnAddrIndex = 0; // No return address slot generated yet.
373 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerdd66a412005-05-15 05:46:45 +0000374 BytesCallerReserves = ArgOffset;
Chris Lattnerb59006c2005-04-09 15:23:56 +0000375
376 // Finally, inform the code generator which regs we return values in.
377 switch (getValueType(F.getReturnType())) {
378 default: assert(0 && "Unknown type!");
379 case MVT::isVoid: break;
380 case MVT::i1:
381 case MVT::i8:
382 case MVT::i16:
383 case MVT::i32:
384 MF.addLiveOut(X86::EAX);
385 break;
386 case MVT::i64:
387 MF.addLiveOut(X86::EAX);
388 MF.addLiveOut(X86::EDX);
389 break;
390 case MVT::f32:
391 case MVT::f64:
392 MF.addLiveOut(X86::ST0);
393 break;
394 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000395 return ArgValues;
396}
397
Chris Lattnerb52e0412005-01-08 19:28:19 +0000398std::pair<SDOperand, SDOperand>
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000399X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
Chris Lattnerf27e31d2005-05-13 20:29:13 +0000400 bool isVarArg, bool isTailCall,
401 SDOperand Callee, ArgListTy &Args,
402 SelectionDAG &DAG) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000403 // Count how many bytes are to be pushed on the stack.
404 unsigned NumBytes = 0;
405
406 if (Args.empty()) {
407 // Save zero bytes.
Chris Lattner2dce7032005-05-12 23:24:06 +0000408 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattnerb52e0412005-01-08 19:28:19 +0000409 DAG.getConstant(0, getPointerTy()));
Chris Lattner88c8a232005-01-07 07:49:41 +0000410 } else {
411 for (unsigned i = 0, e = Args.size(); i != e; ++i)
412 switch (getValueType(Args[i].second)) {
413 default: assert(0 && "Unknown value type!");
414 case MVT::i1:
415 case MVT::i8:
416 case MVT::i16:
417 case MVT::i32:
418 case MVT::f32:
419 NumBytes += 4;
420 break;
421 case MVT::i64:
422 case MVT::f64:
423 NumBytes += 8;
424 break;
425 }
426
Chris Lattner2dce7032005-05-12 23:24:06 +0000427 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattnerb52e0412005-01-08 19:28:19 +0000428 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner88c8a232005-01-07 07:49:41 +0000429
430 // Arguments go on the stack in reverse order, as specified by the ABI.
431 unsigned ArgOffset = 0;
Chris Lattner7c762782005-08-16 21:56:37 +0000432 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
433 X86::ESP, MVT::i32);
Chris Lattnerc78776d2005-01-21 19:46:38 +0000434 std::vector<SDOperand> Stores;
435
Chris Lattner88c8a232005-01-07 07:49:41 +0000436 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000437 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
438 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
439
440 switch (getValueType(Args[i].second)) {
441 default: assert(0 && "Unexpected ValueType for argument!");
442 case MVT::i1:
443 case MVT::i8:
444 case MVT::i16:
445 // Promote the integer to 32 bits. If the input type is signed use a
446 // sign extend, otherwise use a zero extend.
447 if (Args[i].second->isSigned())
448 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
449 else
450 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
451
452 // FALL THROUGH
453 case MVT::i32:
454 case MVT::f32:
Chris Lattnerc78776d2005-01-21 19:46:38 +0000455 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnerdaa064d2005-05-09 05:40:26 +0000456 Args[i].first, PtrOff,
457 DAG.getSrcValue(NULL)));
Chris Lattner88c8a232005-01-07 07:49:41 +0000458 ArgOffset += 4;
459 break;
460 case MVT::i64:
461 case MVT::f64:
Chris Lattnerc78776d2005-01-21 19:46:38 +0000462 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnerdaa064d2005-05-09 05:40:26 +0000463 Args[i].first, PtrOff,
464 DAG.getSrcValue(NULL)));
Chris Lattner88c8a232005-01-07 07:49:41 +0000465 ArgOffset += 8;
466 break;
467 }
468 }
Chris Lattnerc78776d2005-01-21 19:46:38 +0000469 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner88c8a232005-01-07 07:49:41 +0000470 }
471
472 std::vector<MVT::ValueType> RetVals;
473 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattner88c8a232005-01-07 07:49:41 +0000474 RetVals.push_back(MVT::Other);
475
Chris Lattner1b3520c2005-05-14 08:48:15 +0000476 // The result values produced have to be legal. Promote the result.
477 switch (RetTyVT) {
478 case MVT::isVoid: break;
479 default:
480 RetVals.push_back(RetTyVT);
481 break;
482 case MVT::i1:
483 case MVT::i8:
484 case MVT::i16:
485 RetVals.push_back(MVT::i32);
486 break;
487 case MVT::f32:
Nate Begeman8a093362005-07-06 18:59:04 +0000488 if (X86ScalarSSE)
489 RetVals.push_back(MVT::f32);
490 else
491 RetVals.push_back(MVT::f64);
Chris Lattner1b3520c2005-05-14 08:48:15 +0000492 break;
493 case MVT::i64:
494 RetVals.push_back(MVT::i32);
495 RetVals.push_back(MVT::i32);
496 break;
497 }
498 std::vector<SDOperand> Ops;
499 Ops.push_back(Chain);
500 Ops.push_back(Callee);
501 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
502 Ops.push_back(DAG.getConstant(0, getPointerTy()));
503 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
504 RetVals, Ops);
505 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
506
507 SDOperand ResultVal;
508 switch (RetTyVT) {
509 case MVT::isVoid: break;
510 default:
511 ResultVal = TheCall.getValue(1);
512 break;
513 case MVT::i1:
514 case MVT::i8:
515 case MVT::i16:
516 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
517 break;
518 case MVT::f32:
519 // FIXME: we would really like to remember that this FP_ROUND operation is
520 // okay to eliminate if we allow excess FP precision.
521 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
522 break;
523 case MVT::i64:
524 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
525 TheCall.getValue(2));
526 break;
527 }
528
529 return std::make_pair(ResultVal, Chain);
Chris Lattner88c8a232005-01-07 07:49:41 +0000530}
531
Chris Lattnera7220852005-07-05 19:58:54 +0000532SDOperand
533X86TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
534 Value *VAListV, SelectionDAG &DAG) {
Andrew Lenharth9144ec42005-06-18 18:34:52 +0000535 // vastart just stores the address of the VarArgsFrameIndex slot.
536 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Chris Lattnera7220852005-07-05 19:58:54 +0000537 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
538 DAG.getSrcValue(VAListV));
Chris Lattner9f59d282005-01-09 00:01:27 +0000539}
540
Chris Lattnera7220852005-07-05 19:58:54 +0000541
542std::pair<SDOperand,SDOperand>
543X86TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP,
544 Value *VAListV, const Type *ArgTy,
545 SelectionDAG &DAG) {
Chris Lattner9f59d282005-01-09 00:01:27 +0000546 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnera7220852005-07-05 19:58:54 +0000547 SDOperand Val = DAG.getLoad(MVT::i32, Chain,
548 VAListP, DAG.getSrcValue(VAListV));
549 SDOperand Result = DAG.getLoad(ArgVT, Chain, Val,
Chris Lattner91ae1292005-07-05 17:50:16 +0000550 DAG.getSrcValue(NULL));
Andrew Lenharth9144ec42005-06-18 18:34:52 +0000551 unsigned Amt;
552 if (ArgVT == MVT::i32)
553 Amt = 4;
554 else {
555 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
556 "Other types should have been promoted for varargs!");
557 Amt = 8;
Chris Lattner9f59d282005-01-09 00:01:27 +0000558 }
Andrew Lenharth9144ec42005-06-18 18:34:52 +0000559 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
560 DAG.getConstant(Amt, Val.getValueType()));
561 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera7220852005-07-05 19:58:54 +0000562 Val, VAListP, DAG.getSrcValue(VAListV));
Chris Lattner9f59d282005-01-09 00:01:27 +0000563 return std::make_pair(Result, Chain);
564}
Misha Brukmanc88330a2005-04-21 23:38:14 +0000565
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000566//===----------------------------------------------------------------------===//
Chris Lattner5011ff02005-05-13 22:46:57 +0000567// Fast Calling Convention implementation
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000568//===----------------------------------------------------------------------===//
569//
570// The X86 'fast' calling convention passes up to two integer arguments in
571// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
572// and requires that the callee pop its arguments off the stack (allowing proper
573// tail calls), and has the same return value conventions as C calling convs.
574//
Chris Lattner9b29fe22005-05-13 23:49:10 +0000575// This calling convention always arranges for the callee pop value to be 8n+4
576// bytes, which is needed for tail recursion elimination and stack alignment
577// reasons.
578//
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000579// Note that this can be enhanced in the future to pass fp vals in registers
580// (when we have a global fp allocator) and do other tricks.
581//
Chris Lattner0b17b452005-05-13 07:38:09 +0000582
583/// AddLiveIn - This helper function adds the specified physical register to the
584/// MachineFunction as a live in value. It also creates a corresponding virtual
585/// register for it.
586static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
587 TargetRegisterClass *RC) {
588 assert(RC->contains(PReg) && "Not the correct regclass!");
589 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
590 MF.addLiveIn(PReg, VReg);
591 return VReg;
592}
593
594
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000595std::vector<SDOperand>
596X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
597 std::vector<SDOperand> ArgValues;
598
599 MachineFunction &MF = DAG.getMachineFunction();
600 MachineFrameInfo *MFI = MF.getFrameInfo();
601
602 // Add DAG nodes to load the arguments... On entry to a function the stack
603 // frame looks like this:
604 //
605 // [ESP] -- return address
606 // [ESP + 4] -- first nonreg argument (leftmost lexically)
607 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
608 // ...
609 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
610
611 // Keep track of the number of integer regs passed so far. This can be either
612 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
613 // used).
614 unsigned NumIntRegs = 0;
615
616 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
617 MVT::ValueType ObjectVT = getValueType(I->getType());
618 unsigned ArgIncrement = 4;
619 unsigned ObjSize = 0;
620 SDOperand ArgValue;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000621
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000622 switch (ObjectVT) {
623 default: assert(0 && "Unhandled argument type!");
624 case MVT::i1:
625 case MVT::i8:
626 if (NumIntRegs < 2) {
627 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000628 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
629 X86::R8RegisterClass);
Chris Lattner7c762782005-08-16 21:56:37 +0000630 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000631 DAG.setRoot(ArgValue.getValue(1));
632 }
633 ++NumIntRegs;
634 break;
635 }
636
637 ObjSize = 1;
638 break;
639 case MVT::i16:
640 if (NumIntRegs < 2) {
641 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000642 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
643 X86::R16RegisterClass);
Chris Lattner7c762782005-08-16 21:56:37 +0000644 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000645 DAG.setRoot(ArgValue.getValue(1));
646 }
647 ++NumIntRegs;
648 break;
649 }
650 ObjSize = 2;
651 break;
652 case MVT::i32:
653 if (NumIntRegs < 2) {
654 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000655 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
656 X86::R32RegisterClass);
Chris Lattner7c762782005-08-16 21:56:37 +0000657 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000658 DAG.setRoot(ArgValue.getValue(1));
659 }
660 ++NumIntRegs;
661 break;
662 }
663 ObjSize = 4;
664 break;
665 case MVT::i64:
666 if (NumIntRegs == 0) {
667 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000668 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
669 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000670
Chris Lattner7c762782005-08-16 21:56:37 +0000671 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
672 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000673 DAG.setRoot(Hi.getValue(1));
674
675 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
676 }
677 NumIntRegs = 2;
678 break;
679 } else if (NumIntRegs == 1) {
680 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000681 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
Chris Lattner7c762782005-08-16 21:56:37 +0000682 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000683 DAG.setRoot(Low.getValue(1));
684
685 // Load the high part from memory.
686 // Create the frame index object for this incoming parameter...
687 int FI = MFI->CreateFixedObject(4, ArgOffset);
688 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
689 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
690 DAG.getSrcValue(NULL));
691 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
692 }
693 ArgOffset += 4;
694 NumIntRegs = 2;
695 break;
696 }
697 ObjSize = ArgIncrement = 8;
698 break;
699 case MVT::f32: ObjSize = 4; break;
700 case MVT::f64: ObjSize = ArgIncrement = 8; break;
701 }
702
703 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
704 // dead loads.
705 if (ObjSize && !I->use_empty()) {
706 // Create the frame index object for this incoming parameter...
707 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
708
709 // Create the SelectionDAG nodes corresponding to a load from this
710 // parameter.
711 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
712
713 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
714 DAG.getSrcValue(NULL));
715 } else if (ArgValue.Val == 0) {
716 if (MVT::isInteger(ObjectVT))
717 ArgValue = DAG.getConstant(0, ObjectVT);
718 else
719 ArgValue = DAG.getConstantFP(0, ObjectVT);
720 }
721 ArgValues.push_back(ArgValue);
722
723 if (ObjSize)
724 ArgOffset += ArgIncrement; // Move on to the next argument.
725 }
726
Chris Lattner9b29fe22005-05-13 23:49:10 +0000727 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
728 // arguments and the arguments after the retaddr has been pushed are aligned.
729 if ((ArgOffset & 7) == 0)
730 ArgOffset += 4;
731
Chris Lattnerc0e369e2005-05-13 21:44:04 +0000732 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
733 ReturnAddrIndex = 0; // No return address slot generated yet.
734 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
Chris Lattnerdd66a412005-05-15 05:46:45 +0000735 BytesCallerReserves = 0;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000736
737 // Finally, inform the code generator which regs we return values in.
738 switch (getValueType(F.getReturnType())) {
739 default: assert(0 && "Unknown type!");
740 case MVT::isVoid: break;
741 case MVT::i1:
742 case MVT::i8:
743 case MVT::i16:
744 case MVT::i32:
745 MF.addLiveOut(X86::EAX);
746 break;
747 case MVT::i64:
748 MF.addLiveOut(X86::EAX);
749 MF.addLiveOut(X86::EDX);
750 break;
751 case MVT::f32:
752 case MVT::f64:
753 MF.addLiveOut(X86::ST0);
754 break;
755 }
756 return ArgValues;
757}
758
759std::pair<SDOperand, SDOperand>
760X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
Chris Lattnerf27e31d2005-05-13 20:29:13 +0000761 bool isTailCall, SDOperand Callee,
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000762 ArgListTy &Args, SelectionDAG &DAG) {
763 // Count how many bytes are to be pushed on the stack.
764 unsigned NumBytes = 0;
765
766 // Keep track of the number of integer regs passed so far. This can be either
767 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
768 // used).
769 unsigned NumIntRegs = 0;
770
771 for (unsigned i = 0, e = Args.size(); i != e; ++i)
772 switch (getValueType(Args[i].second)) {
773 default: assert(0 && "Unknown value type!");
774 case MVT::i1:
775 case MVT::i8:
776 case MVT::i16:
777 case MVT::i32:
778 if (NumIntRegs < 2) {
779 ++NumIntRegs;
780 break;
781 }
782 // fall through
783 case MVT::f32:
784 NumBytes += 4;
785 break;
786 case MVT::i64:
787 if (NumIntRegs == 0) {
788 NumIntRegs = 2;
789 break;
790 } else if (NumIntRegs == 1) {
791 NumIntRegs = 2;
792 NumBytes += 4;
793 break;
794 }
795
796 // fall through
797 case MVT::f64:
798 NumBytes += 8;
799 break;
800 }
801
Chris Lattner9b29fe22005-05-13 23:49:10 +0000802 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
803 // arguments and the arguments after the retaddr has been pushed are aligned.
804 if ((NumBytes & 7) == 0)
805 NumBytes += 4;
806
Chris Lattner2dce7032005-05-12 23:24:06 +0000807 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000808 DAG.getConstant(NumBytes, getPointerTy()));
809
810 // Arguments go on the stack in reverse order, as specified by the ABI.
811 unsigned ArgOffset = 0;
Chris Lattner7c762782005-08-16 21:56:37 +0000812 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
813 X86::ESP, MVT::i32);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000814 NumIntRegs = 0;
815 std::vector<SDOperand> Stores;
816 std::vector<SDOperand> RegValuesToPass;
817 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
818 switch (getValueType(Args[i].second)) {
819 default: assert(0 && "Unexpected ValueType for argument!");
820 case MVT::i1:
821 case MVT::i8:
822 case MVT::i16:
823 case MVT::i32:
824 if (NumIntRegs < 2) {
825 RegValuesToPass.push_back(Args[i].first);
826 ++NumIntRegs;
827 break;
828 }
829 // Fall through
830 case MVT::f32: {
831 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
832 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
833 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
834 Args[i].first, PtrOff,
835 DAG.getSrcValue(NULL)));
836 ArgOffset += 4;
837 break;
838 }
839 case MVT::i64:
840 if (NumIntRegs < 2) { // Can pass part of it in regs?
841 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
842 Args[i].first, DAG.getConstant(1, MVT::i32));
843 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
844 Args[i].first, DAG.getConstant(0, MVT::i32));
845 RegValuesToPass.push_back(Lo);
846 ++NumIntRegs;
847 if (NumIntRegs < 2) { // Pass both parts in regs?
848 RegValuesToPass.push_back(Hi);
849 ++NumIntRegs;
850 } else {
851 // Pass the high part in memory.
852 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
853 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
854 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner18b2c2f2005-05-14 12:03:10 +0000855 Hi, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000856 ArgOffset += 4;
857 }
858 break;
859 }
860 // Fall through
861 case MVT::f64:
862 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
863 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
864 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
865 Args[i].first, PtrOff,
866 DAG.getSrcValue(NULL)));
867 ArgOffset += 8;
868 break;
869 }
870 }
871 if (!Stores.empty())
872 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
873
Chris Lattner9b29fe22005-05-13 23:49:10 +0000874 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
875 // arguments and the arguments after the retaddr has been pushed are aligned.
876 if ((ArgOffset & 7) == 0)
877 ArgOffset += 4;
878
Chris Lattner1b3520c2005-05-14 08:48:15 +0000879 std::vector<MVT::ValueType> RetVals;
880 MVT::ValueType RetTyVT = getValueType(RetTy);
881
882 RetVals.push_back(MVT::Other);
883
884 // The result values produced have to be legal. Promote the result.
885 switch (RetTyVT) {
886 case MVT::isVoid: break;
887 default:
888 RetVals.push_back(RetTyVT);
889 break;
890 case MVT::i1:
891 case MVT::i8:
892 case MVT::i16:
893 RetVals.push_back(MVT::i32);
894 break;
895 case MVT::f32:
Nate Begeman8a093362005-07-06 18:59:04 +0000896 if (X86ScalarSSE)
897 RetVals.push_back(MVT::f32);
898 else
899 RetVals.push_back(MVT::f64);
Chris Lattner1b3520c2005-05-14 08:48:15 +0000900 break;
901 case MVT::i64:
902 RetVals.push_back(MVT::i32);
903 RetVals.push_back(MVT::i32);
904 break;
905 }
906
907 std::vector<SDOperand> Ops;
908 Ops.push_back(Chain);
909 Ops.push_back(Callee);
910 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
911 // Callee pops all arg values on the stack.
912 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
913
914 // Pass register arguments as needed.
915 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
916
917 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
918 RetVals, Ops);
919 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
920
921 SDOperand ResultVal;
922 switch (RetTyVT) {
923 case MVT::isVoid: break;
924 default:
925 ResultVal = TheCall.getValue(1);
926 break;
927 case MVT::i1:
928 case MVT::i8:
929 case MVT::i16:
930 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
931 break;
932 case MVT::f32:
933 // FIXME: we would really like to remember that this FP_ROUND operation is
934 // okay to eliminate if we allow excess FP precision.
935 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
936 break;
937 case MVT::i64:
938 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
939 TheCall.getValue(2));
940 break;
941 }
942
943 return std::make_pair(ResultVal, Chain);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000944}
945
Chris Lattnerdd66a412005-05-15 05:46:45 +0000946SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
947 if (ReturnAddrIndex == 0) {
948 // Set up a frame object for the return address.
949 MachineFunction &MF = DAG.getMachineFunction();
950 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
951 }
952
953 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
954}
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000955
956
Chris Lattner9f59d282005-01-09 00:01:27 +0000957
958std::pair<SDOperand, SDOperand> X86TargetLowering::
959LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
960 SelectionDAG &DAG) {
961 SDOperand Result;
962 if (Depth) // Depths > 0 not supported yet!
963 Result = DAG.getConstant(0, getPointerTy());
964 else {
Chris Lattnerdd66a412005-05-15 05:46:45 +0000965 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
Chris Lattner9f59d282005-01-09 00:01:27 +0000966 if (!isFrameAddress)
967 // Just load the return address
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000968 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
969 DAG.getSrcValue(NULL));
Chris Lattner9f59d282005-01-09 00:01:27 +0000970 else
971 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
972 DAG.getConstant(4, MVT::i32));
973 }
974 return std::make_pair(Result, Chain);
975}
Chris Lattner88c8a232005-01-07 07:49:41 +0000976
Chris Lattner507a2752005-07-16 00:28:20 +0000977//===----------------------------------------------------------------------===//
978// X86 Custom Lowering Hooks
979//===----------------------------------------------------------------------===//
980
Chris Lattnera36117b2005-05-14 06:52:07 +0000981/// LowerOperation - Provide custom lowering hooks for some operations.
982///
983SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
984 switch (Op.getOpcode()) {
985 default: assert(0 && "Should not custom lower this!");
Chris Lattner67756e22005-07-29 00:40:01 +0000986 case ISD::SINT_TO_FP: {
Chris Lattnera36117b2005-05-14 06:52:07 +0000987 assert(Op.getValueType() == MVT::f64 &&
988 Op.getOperand(0).getValueType() == MVT::i64 &&
989 "Unknown SINT_TO_FP to lower!");
990 // We lower sint64->FP into a store to a temporary stack slot, followed by a
991 // FILD64m node.
992 MachineFunction &MF = DAG.getMachineFunction();
993 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
994 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
995 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
996 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
997 std::vector<MVT::ValueType> RTs;
998 RTs.push_back(MVT::f64);
999 RTs.push_back(MVT::Other);
1000 std::vector<SDOperand> Ops;
1001 Ops.push_back(Store);
1002 Ops.push_back(StackSlot);
1003 return DAG.getNode(X86ISD::FILD64m, RTs, Ops);
1004 }
Chris Lattner67756e22005-07-29 00:40:01 +00001005 case ISD::FP_TO_SINT: {
Chris Lattner4738d1b2005-07-30 00:05:54 +00001006 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattner67756e22005-07-29 00:40:01 +00001007 Op.getOperand(0).getValueType() == MVT::f64 &&
1008 "Unknown FP_TO_SINT to lower!");
1009 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1010 // stack slot.
1011 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattner4738d1b2005-07-30 00:05:54 +00001012 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1013 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Chris Lattner67756e22005-07-29 00:40:01 +00001014 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1015
Chris Lattner4738d1b2005-07-30 00:05:54 +00001016 unsigned Opc;
1017 switch (Op.getValueType()) {
1018 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1019 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1020 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1021 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1022 }
Jeff Cohen546fd592005-07-30 18:33:25 +00001023
Chris Lattner4738d1b2005-07-30 00:05:54 +00001024 // Build the FP_TO_INT*_IN_MEM
Chris Lattner67756e22005-07-29 00:40:01 +00001025 std::vector<SDOperand> Ops;
1026 Ops.push_back(DAG.getEntryNode());
1027 Ops.push_back(Op.getOperand(0));
1028 Ops.push_back(StackSlot);
Chris Lattner4738d1b2005-07-30 00:05:54 +00001029 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
Jeff Cohen546fd592005-07-30 18:33:25 +00001030
Chris Lattner67756e22005-07-29 00:40:01 +00001031 // Load the result.
Chris Lattner4738d1b2005-07-30 00:05:54 +00001032 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1033 DAG.getSrcValue(NULL));
Chris Lattner67756e22005-07-29 00:40:01 +00001034 }
1035 }
Chris Lattnera36117b2005-05-14 06:52:07 +00001036}
1037
1038
1039//===----------------------------------------------------------------------===//
1040// Pattern Matcher Implementation
1041//===----------------------------------------------------------------------===//
Chris Lattner88c8a232005-01-07 07:49:41 +00001042
Chris Lattnera7acdda2005-01-18 01:06:26 +00001043namespace {
1044 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
1045 /// SDOperand's instead of register numbers for the leaves of the matched
1046 /// tree.
1047 struct X86ISelAddressMode {
1048 enum {
1049 RegBase,
1050 FrameIndexBase,
1051 } BaseType;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001052
Chris Lattnera7acdda2005-01-18 01:06:26 +00001053 struct { // This is really a union, discriminated by BaseType!
1054 SDOperand Reg;
1055 int FrameIndex;
1056 } Base;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001057
Chris Lattnera7acdda2005-01-18 01:06:26 +00001058 unsigned Scale;
1059 SDOperand IndexReg;
1060 unsigned Disp;
1061 GlobalValue *GV;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001062
Chris Lattnera7acdda2005-01-18 01:06:26 +00001063 X86ISelAddressMode()
1064 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
1065 }
1066 };
1067}
Chris Lattner88c8a232005-01-07 07:49:41 +00001068
1069
1070namespace {
1071 Statistic<>
1072 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
1073
1074 //===--------------------------------------------------------------------===//
1075 /// ISel - X86 specific code to select X86 machine instructions for
1076 /// SelectionDAG operations.
1077 ///
1078 class ISel : public SelectionDAGISel {
1079 /// ContainsFPCode - Every instruction we select that uses or defines a FP
1080 /// register should set this to true.
1081 bool ContainsFPCode;
1082
1083 /// X86Lowering - This object fully describes how to lower LLVM code to an
1084 /// X86-specific SelectionDAG.
1085 X86TargetLowering X86Lowering;
1086
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001087 /// RegPressureMap - This keeps an approximate count of the number of
1088 /// registers required to evaluate each node in the graph.
1089 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner88c8a232005-01-07 07:49:41 +00001090
1091 /// ExprMap - As shared expressions are codegen'd, we keep track of which
1092 /// vreg the value is produced in, so we only emit one copy of each compiled
1093 /// tree.
1094 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner88c8a232005-01-07 07:49:41 +00001095
Chris Lattnerdd66a412005-05-15 05:46:45 +00001096 /// TheDAG - The DAG being selected during Select* operations.
1097 SelectionDAG *TheDAG;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001098
1099 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
Nate Begemanf26625e2005-07-12 01:41:54 +00001100 /// make the right decision when generating code for different targets.
1101 const X86Subtarget *Subtarget;
Chris Lattner88c8a232005-01-07 07:49:41 +00001102 public:
1103 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
Chris Lattner158acab2005-08-05 21:54:27 +00001104 Subtarget = &TM.getSubtarget<X86Subtarget>();
Chris Lattner88c8a232005-01-07 07:49:41 +00001105 }
1106
Chris Lattnere1e844c2005-01-21 21:35:14 +00001107 virtual const char *getPassName() const {
1108 return "X86 Pattern Instruction Selection";
1109 }
1110
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001111 unsigned getRegPressure(SDOperand O) {
1112 return RegPressureMap[O.Val];
1113 }
1114 unsigned ComputeRegPressure(SDOperand O);
1115
Chris Lattner88c8a232005-01-07 07:49:41 +00001116 /// InstructionSelectBasicBlock - This callback is invoked by
1117 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner6fba62d62005-01-12 04:21:28 +00001118 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner88c8a232005-01-07 07:49:41 +00001119
Chris Lattner0b17b452005-05-13 07:38:09 +00001120 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
1121
Chris Lattner30607ec2005-01-25 20:03:11 +00001122 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
1123 bool FloatPromoteOk = false);
Chris Lattner62b22422005-01-11 21:19:59 +00001124 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattner96113fd2005-01-17 19:25:26 +00001125 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattner29f58192005-01-19 07:37:26 +00001126 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattner3be6cd52005-01-17 01:34:14 +00001127 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner37ed2852005-01-11 04:06:27 +00001128 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Nate Begeman8d394eb2005-08-03 23:26:28 +00001129 void EmitSelectCC(SDOperand Cond, SDOperand True, SDOperand False,
1130 MVT::ValueType SVT, unsigned RDest);
Chris Lattner88c8a232005-01-07 07:49:41 +00001131 unsigned SelectExpr(SDOperand N);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001132
1133 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
1134 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
1135 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattnerdd66a412005-05-15 05:46:45 +00001136 bool EmitPotentialTailCall(SDNode *Node);
1137 void EmitFastCCToFastCCTailCall(SDNode *TailCallNode);
Chris Lattner88c8a232005-01-07 07:49:41 +00001138 void Select(SDOperand N);
1139 };
1140}
1141
Chris Lattnerd8145bc2005-05-10 03:53:18 +00001142/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
1143/// the main function.
1144static void EmitSpecialCodeForMain(MachineBasicBlock *BB,
1145 MachineFrameInfo *MFI) {
1146 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
1147 int CWFrameIdx = MFI->CreateStackObject(2, 2);
1148 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1149
1150 // Set the high part to be 64-bit precision.
1151 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1152 CWFrameIdx, 1).addImm(2);
1153
1154 // Reload the modified control word now.
1155 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1156}
1157
Chris Lattner0b17b452005-05-13 07:38:09 +00001158void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
Chris Lattner0b17b452005-05-13 07:38:09 +00001159 // If this is main, emit special code for main.
Chris Lattnerb42e9622005-09-14 06:06:45 +00001160 MachineBasicBlock *BB = MF.begin();
Chris Lattner0b17b452005-05-13 07:38:09 +00001161 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
1162 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
1163}
1164
1165
Chris Lattner6fba62d62005-01-12 04:21:28 +00001166/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
1167/// when it has created a SelectionDAG for us to codegen.
1168void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
1169 // While we're doing this, keep track of whether we see any FP code for
1170 // FP_REG_KILL insertion.
1171 ContainsFPCode = false;
Chris Lattnerd8145bc2005-05-10 03:53:18 +00001172 MachineFunction *MF = BB->getParent();
Chris Lattner6fba62d62005-01-12 04:21:28 +00001173
1174 // Scan the PHI nodes that already are inserted into this basic block. If any
1175 // of them is a PHI of a floating point value, we need to insert an
1176 // FP_REG_KILL.
Chris Lattnerd8145bc2005-05-10 03:53:18 +00001177 SSARegMap *RegMap = MF->getSSARegMap();
Chris Lattner0b17b452005-05-13 07:38:09 +00001178 if (BB != MF->begin())
1179 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
1180 I != E; ++I) {
1181 assert(I->getOpcode() == X86::PHI &&
1182 "Isn't just PHI nodes?");
1183 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
1184 X86::RFPRegisterClass) {
1185 ContainsFPCode = true;
1186 break;
1187 }
Chris Lattner6fba62d62005-01-12 04:21:28 +00001188 }
Chris Lattnerd8145bc2005-05-10 03:53:18 +00001189
Chris Lattner6fba62d62005-01-12 04:21:28 +00001190 // Compute the RegPressureMap, which is an approximation for the number of
1191 // registers required to compute each node.
1192 ComputeRegPressure(DAG.getRoot());
1193
Chris Lattnerdd66a412005-05-15 05:46:45 +00001194 TheDAG = &DAG;
1195
Chris Lattner6fba62d62005-01-12 04:21:28 +00001196 // Codegen the basic block.
1197 Select(DAG.getRoot());
1198
Chris Lattnerdd66a412005-05-15 05:46:45 +00001199 TheDAG = 0;
1200
Chris Lattner6fba62d62005-01-12 04:21:28 +00001201 // Finally, look at all of the successors of this block. If any contain a PHI
1202 // node of FP type, we need to insert an FP_REG_KILL in this block.
1203 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
1204 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
1205 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
1206 I != E && I->getOpcode() == X86::PHI; ++I) {
1207 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
1208 X86::RFPRegisterClass) {
1209 ContainsFPCode = true;
1210 break;
1211 }
1212 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001213
Chris Lattner6972c312005-05-09 03:36:39 +00001214 // Final check, check LLVM BB's that are successors to the LLVM BB
1215 // corresponding to BB for FP PHI nodes.
1216 const BasicBlock *LLVMBB = BB->getBasicBlock();
1217 const PHINode *PN;
1218 if (!ContainsFPCode)
1219 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
1220 SI != E && !ContainsFPCode; ++SI)
1221 for (BasicBlock::const_iterator II = SI->begin();
1222 (PN = dyn_cast<PHINode>(II)); ++II)
1223 if (PN->getType()->isFloatingPoint()) {
1224 ContainsFPCode = true;
1225 break;
1226 }
1227
1228
Chris Lattner6fba62d62005-01-12 04:21:28 +00001229 // Insert FP_REG_KILL instructions into basic blocks that need them. This
1230 // only occurs due to the floating point stackifier not being aggressive
1231 // enough to handle arbitrary global stackification.
1232 //
1233 // Currently we insert an FP_REG_KILL instruction into each block that uses or
1234 // defines a floating point virtual register.
1235 //
1236 // When the global register allocators (like linear scan) finally update live
1237 // variable analysis, we can keep floating point values in registers across
1238 // basic blocks. This will be a huge win, but we are waiting on the global
1239 // allocators before we can do this.
1240 //
Chris Lattner472a2652005-03-30 01:10:00 +00001241 if (ContainsFPCode) {
Chris Lattner6fba62d62005-01-12 04:21:28 +00001242 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
1243 ++NumFPKill;
1244 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001245
Chris Lattner6fba62d62005-01-12 04:21:28 +00001246 // Clear state used for selection.
1247 ExprMap.clear();
Chris Lattner6fba62d62005-01-12 04:21:28 +00001248 RegPressureMap.clear();
1249}
1250
1251
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001252// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
1253// for the number of registers required to compute each node. This is basically
1254// computing a generalized form of the Sethi-Ullman number for each node.
1255unsigned ISel::ComputeRegPressure(SDOperand O) {
1256 SDNode *N = O.Val;
1257 unsigned &Result = RegPressureMap[N];
1258 if (Result) return Result;
1259
Chris Lattner8fea42b2005-01-11 03:37:59 +00001260 // FIXME: Should operations like CALL (which clobber lots o regs) have a
1261 // higher fixed cost??
1262
Chris Lattner8aa10fc2005-01-11 22:29:12 +00001263 if (N->getNumOperands() == 0) {
1264 Result = 1;
1265 } else {
1266 unsigned MaxRegUse = 0;
1267 unsigned NumExtraMaxRegUsers = 0;
1268 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1269 unsigned Regs;
1270 if (N->getOperand(i).getOpcode() == ISD::Constant)
1271 Regs = 0;
1272 else
1273 Regs = ComputeRegPressure(N->getOperand(i));
1274 if (Regs > MaxRegUse) {
1275 MaxRegUse = Regs;
1276 NumExtraMaxRegUsers = 0;
1277 } else if (Regs == MaxRegUse &&
1278 N->getOperand(i).getValueType() != MVT::Other) {
1279 ++NumExtraMaxRegUsers;
1280 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001281 }
Chris Lattnerca318ed2005-01-17 22:56:09 +00001282
1283 if (O.getOpcode() != ISD::TokenFactor)
1284 Result = MaxRegUse+NumExtraMaxRegUsers;
1285 else
Chris Lattnera5d137f2005-01-17 23:02:13 +00001286 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattner8aa10fc2005-01-11 22:29:12 +00001287 }
Chris Lattnerb7fe57a2005-01-12 02:19:06 +00001288
Chris Lattner75bac9f2005-01-11 23:21:30 +00001289 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattner8aa10fc2005-01-11 22:29:12 +00001290 return Result;
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001291}
1292
Chris Lattner5b04f332005-01-20 16:50:16 +00001293/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
1294/// The DAG cannot have cycles in it, by definition, so the visited set is not
1295/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
1296/// reuse, so it prevents exponential cases.
1297///
1298static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
1299 std::set<SDNode*> &Visited) {
1300 if (N == Op) return true; // Found it.
1301 SDNode *Node = N.Val;
Chris Lattnere70eb9da2005-01-21 21:43:02 +00001302 if (Node->getNumOperands() == 0 || // Leaf?
1303 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattner5b04f332005-01-20 16:50:16 +00001304 if (!Visited.insert(Node).second) return false; // Already visited?
1305
1306 // Recurse for the first N-1 operands.
1307 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1308 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
1309 return true;
1310
1311 // Tail recurse for the last operand.
1312 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
1313}
1314
Chris Lattnera7acdda2005-01-18 01:06:26 +00001315X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
1316 X86AddressMode Result;
1317
1318 // If we need to emit two register operands, emit the one with the highest
1319 // register pressure first.
1320 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
1321 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattner5b04f332005-01-20 16:50:16 +00001322 bool EmitBaseThenIndex;
Chris Lattnera7acdda2005-01-18 01:06:26 +00001323 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattner5b04f332005-01-20 16:50:16 +00001324 std::set<SDNode*> Visited;
1325 EmitBaseThenIndex = true;
1326 // If Base ends up pointing to Index, we must emit index first. This is
1327 // because of the way we fold loads, we may end up doing bad things with
1328 // the folded add.
1329 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
1330 EmitBaseThenIndex = false;
1331 } else {
1332 std::set<SDNode*> Visited;
1333 EmitBaseThenIndex = false;
1334 // If Base ends up pointing to Index, we must emit index first. This is
1335 // because of the way we fold loads, we may end up doing bad things with
1336 // the folded add.
1337 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
1338 EmitBaseThenIndex = true;
1339 }
1340
1341 if (EmitBaseThenIndex) {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001342 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1343 Result.IndexReg = SelectExpr(IAM.IndexReg);
1344 } else {
1345 Result.IndexReg = SelectExpr(IAM.IndexReg);
1346 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1347 }
Chris Lattner5b04f332005-01-20 16:50:16 +00001348
Chris Lattnera7acdda2005-01-18 01:06:26 +00001349 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
1350 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1351 } else if (IAM.IndexReg.Val) {
1352 Result.IndexReg = SelectExpr(IAM.IndexReg);
1353 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001354
Chris Lattnera7acdda2005-01-18 01:06:26 +00001355 switch (IAM.BaseType) {
1356 case X86ISelAddressMode::RegBase:
1357 Result.BaseType = X86AddressMode::RegBase;
1358 break;
1359 case X86ISelAddressMode::FrameIndexBase:
1360 Result.BaseType = X86AddressMode::FrameIndexBase;
1361 Result.Base.FrameIndex = IAM.Base.FrameIndex;
1362 break;
1363 default:
1364 assert(0 && "Unknown base type!");
1365 break;
1366 }
1367 Result.Scale = IAM.Scale;
1368 Result.Disp = IAM.Disp;
1369 Result.GV = IAM.GV;
1370 return Result;
1371}
1372
1373/// SelectAddress - Pattern match the maximal addressing mode for this node and
1374/// emit all of the leaf registers.
1375void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
1376 X86ISelAddressMode IAM;
1377 MatchAddress(N, IAM);
1378 AM = SelectAddrExprs(IAM);
1379}
1380
1381/// MatchAddress - Add the specified node to the specified addressing mode,
1382/// returning true if it cannot be done. This just pattern matches for the
1383/// addressing mode, it does not cause any code to be emitted. For that, use
1384/// SelectAddress.
1385bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner88c8a232005-01-07 07:49:41 +00001386 switch (N.getOpcode()) {
1387 default: break;
1388 case ISD::FrameIndex:
Chris Lattnera7acdda2005-01-18 01:06:26 +00001389 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
1390 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner88c8a232005-01-07 07:49:41 +00001391 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
1392 return false;
1393 }
1394 break;
1395 case ISD::GlobalAddress:
1396 if (AM.GV == 0) {
Nate Begemanf26625e2005-07-12 01:41:54 +00001397 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1398 // For Darwin, external and weak symbols are indirect, so we want to load
1399 // the value at address GV, not the value of GV itself. This means that
1400 // the GlobalAddress must be in the base or index register of the address,
1401 // not the GV offset field.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001402 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemanf26625e2005-07-12 01:41:54 +00001403 (GV->hasWeakLinkage() || GV->isExternal())) {
1404 break;
1405 } else {
1406 AM.GV = GV;
1407 return false;
1408 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001409 }
1410 break;
1411 case ISD::Constant:
1412 AM.Disp += cast<ConstantSDNode>(N)->getValue();
1413 return false;
1414 case ISD::SHL:
Chris Lattner3676cd62005-01-13 05:53:16 +00001415 // We might have folded the load into this shift, so don't regen the value
1416 // if so.
1417 if (ExprMap.count(N)) break;
1418
Chris Lattnera7acdda2005-01-18 01:06:26 +00001419 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner88c8a232005-01-07 07:49:41 +00001420 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
1421 unsigned Val = CN->getValue();
1422 if (Val == 1 || Val == 2 || Val == 3) {
1423 AM.Scale = 1 << Val;
Chris Lattnerb74ec4c2005-01-11 06:36:20 +00001424 SDOperand ShVal = N.Val->getOperand(0);
1425
1426 // Okay, we know that we have a scale by now. However, if the scaled
1427 // value is an add of something and a constant, we can fold the
1428 // constant into the disp field here.
Chris Lattnered246ec2005-01-18 04:18:32 +00001429 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattnerb74ec4c2005-01-11 06:36:20 +00001430 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001431 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattnerb74ec4c2005-01-11 06:36:20 +00001432 ConstantSDNode *AddVal =
1433 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
1434 AM.Disp += AddVal->getValue() << Val;
Chris Lattner3676cd62005-01-13 05:53:16 +00001435 } else {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001436 AM.IndexReg = ShVal;
Chris Lattnerb74ec4c2005-01-11 06:36:20 +00001437 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001438 return false;
1439 }
1440 }
1441 break;
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001442 case ISD::MUL:
Chris Lattner3676cd62005-01-13 05:53:16 +00001443 // We might have folded the load into this mul, so don't regen the value if
1444 // so.
1445 if (ExprMap.count(N)) break;
1446
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001447 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattnera7acdda2005-01-18 01:06:26 +00001448 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
1449 AM.Base.Reg.Val == 0)
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001450 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
1451 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
1452 AM.Scale = unsigned(CN->getValue())-1;
1453
1454 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001455 SDOperand Reg;
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001456
1457 // Okay, we know that we have a scale by now. However, if the scaled
1458 // value is an add of something and a constant, we can fold the
1459 // constant into the disp field here.
Chris Lattnered246ec2005-01-18 04:18:32 +00001460 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001461 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001462 Reg = MulVal.Val->getOperand(0);
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001463 ConstantSDNode *AddVal =
1464 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
1465 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukmanc88330a2005-04-21 23:38:14 +00001466 } else {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001467 Reg = N.Val->getOperand(0);
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001468 }
1469
1470 AM.IndexReg = AM.Base.Reg = Reg;
1471 return false;
1472 }
1473 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001474
1475 case ISD::ADD: {
Chris Lattner3676cd62005-01-13 05:53:16 +00001476 // We might have folded the load into this mul, so don't regen the value if
1477 // so.
1478 if (ExprMap.count(N)) break;
1479
Chris Lattnera7acdda2005-01-18 01:06:26 +00001480 X86ISelAddressMode Backup = AM;
1481 if (!MatchAddress(N.Val->getOperand(0), AM) &&
1482 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner88c8a232005-01-07 07:49:41 +00001483 return false;
1484 AM = Backup;
Chris Lattnera7acdda2005-01-18 01:06:26 +00001485 if (!MatchAddress(N.Val->getOperand(1), AM) &&
1486 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner17553602005-01-12 18:08:53 +00001487 return false;
1488 AM = Backup;
Chris Lattner88c8a232005-01-07 07:49:41 +00001489 break;
1490 }
1491 }
1492
Chris Lattner378262d2005-01-11 04:40:19 +00001493 // Is the base register already occupied?
Chris Lattnera7acdda2005-01-18 01:06:26 +00001494 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattner378262d2005-01-11 04:40:19 +00001495 // If so, check to see if the scale index register is set.
Chris Lattnera7acdda2005-01-18 01:06:26 +00001496 if (AM.IndexReg.Val == 0) {
1497 AM.IndexReg = N;
Chris Lattner378262d2005-01-11 04:40:19 +00001498 AM.Scale = 1;
1499 return false;
1500 }
1501
1502 // Otherwise, we cannot select it.
Chris Lattner88c8a232005-01-07 07:49:41 +00001503 return true;
Chris Lattner378262d2005-01-11 04:40:19 +00001504 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001505
1506 // Default, generate it as a register.
Chris Lattnera7acdda2005-01-18 01:06:26 +00001507 AM.BaseType = X86ISelAddressMode::RegBase;
1508 AM.Base.Reg = N;
Chris Lattner88c8a232005-01-07 07:49:41 +00001509 return false;
1510}
1511
1512/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
1513/// assuming that the temporary registers are in the 8-bit register class.
1514///
1515/// Tmp1 = setcc1
1516/// Tmp2 = setcc2
1517/// DestReg = logicalop Tmp1, Tmp2
1518///
1519static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
1520 unsigned SetCC2, unsigned LogicalOp,
1521 unsigned DestReg) {
1522 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
1523 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1524 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1525 BuildMI(BB, SetCC1, 0, Tmp1);
1526 BuildMI(BB, SetCC2, 0, Tmp2);
1527 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
1528}
1529
1530/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
1531/// condition codes match the specified SetCCOpcode. Note that some conditions
1532/// require multiple instructions to generate the correct value.
1533static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
1534 ISD::CondCode SetCCOpcode, bool isFP) {
1535 unsigned Opc;
1536 if (!isFP) {
1537 switch (SetCCOpcode) {
1538 default: assert(0 && "Illegal integer SetCC!");
1539 case ISD::SETEQ: Opc = X86::SETEr; break;
1540 case ISD::SETGT: Opc = X86::SETGr; break;
1541 case ISD::SETGE: Opc = X86::SETGEr; break;
1542 case ISD::SETLT: Opc = X86::SETLr; break;
1543 case ISD::SETLE: Opc = X86::SETLEr; break;
1544 case ISD::SETNE: Opc = X86::SETNEr; break;
1545 case ISD::SETULT: Opc = X86::SETBr; break;
1546 case ISD::SETUGT: Opc = X86::SETAr; break;
1547 case ISD::SETULE: Opc = X86::SETBEr; break;
1548 case ISD::SETUGE: Opc = X86::SETAEr; break;
1549 }
1550 } else {
1551 // On a floating point condition, the flags are set as follows:
1552 // ZF PF CF op
1553 // 0 | 0 | 0 | X > Y
1554 // 0 | 0 | 1 | X < Y
1555 // 1 | 0 | 0 | X == Y
1556 // 1 | 1 | 1 | unordered
1557 //
1558 switch (SetCCOpcode) {
1559 default: assert(0 && "Invalid FP setcc!");
1560 case ISD::SETUEQ:
1561 case ISD::SETEQ:
1562 Opc = X86::SETEr; // True if ZF = 1
1563 break;
1564 case ISD::SETOGT:
1565 case ISD::SETGT:
1566 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
1567 break;
1568 case ISD::SETOGE:
1569 case ISD::SETGE:
1570 Opc = X86::SETAEr; // True if CF = 0
1571 break;
1572 case ISD::SETULT:
1573 case ISD::SETLT:
1574 Opc = X86::SETBr; // True if CF = 1
1575 break;
1576 case ISD::SETULE:
1577 case ISD::SETLE:
1578 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
1579 break;
1580 case ISD::SETONE:
1581 case ISD::SETNE:
1582 Opc = X86::SETNEr; // True if ZF = 0
1583 break;
1584 case ISD::SETUO:
1585 Opc = X86::SETPr; // True if PF = 1
1586 break;
1587 case ISD::SETO:
1588 Opc = X86::SETNPr; // True if PF = 0
1589 break;
1590 case ISD::SETOEQ: // !PF & ZF
1591 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
1592 return;
1593 case ISD::SETOLT: // !PF & CF
1594 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
1595 return;
1596 case ISD::SETOLE: // !PF & (CF || ZF)
1597 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
1598 return;
1599 case ISD::SETUGT: // PF | (!ZF & !CF)
1600 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
1601 return;
1602 case ISD::SETUGE: // PF | !CF
1603 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
1604 return;
1605 case ISD::SETUNE: // PF | !ZF
1606 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
1607 return;
1608 }
1609 }
1610 BuildMI(BB, Opc, 0, DestReg);
1611}
1612
1613
1614/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
1615/// the Dest block if the Cond condition is true. If we cannot fold this
1616/// condition into the branch, return true.
1617///
Chris Lattner37ed2852005-01-11 04:06:27 +00001618bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
1619 SDOperand Cond) {
Chris Lattner88c8a232005-01-07 07:49:41 +00001620 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
1621 // B) using two conditional branches instead of one condbr, two setcc's, and
1622 // an or.
1623 if ((Cond.getOpcode() == ISD::OR ||
1624 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
1625 // And and or set the flags for us, so there is no need to emit a TST of the
1626 // result. It is only safe to do this if there is only a single use of the
1627 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner37ed2852005-01-11 04:06:27 +00001628 Select(Chain);
Chris Lattner88c8a232005-01-07 07:49:41 +00001629 SelectExpr(Cond);
1630 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
1631 return false;
1632 }
1633
1634 // Codegen br not C -> JE.
1635 if (Cond.getOpcode() == ISD::XOR)
1636 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
1637 if (NC->isAllOnesValue()) {
Chris Lattner37ed2852005-01-11 04:06:27 +00001638 unsigned CondR;
1639 if (getRegPressure(Chain) > getRegPressure(Cond)) {
1640 Select(Chain);
1641 CondR = SelectExpr(Cond.Val->getOperand(0));
1642 } else {
1643 CondR = SelectExpr(Cond.Val->getOperand(0));
1644 Select(Chain);
1645 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001646 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
1647 BuildMI(BB, X86::JE, 1).addMBB(Dest);
1648 return false;
1649 }
1650
Chris Lattner6ec77452005-08-09 20:21:10 +00001651 if (Cond.getOpcode() != ISD::SETCC)
Chris Lattner88c8a232005-01-07 07:49:41 +00001652 return true; // Can only handle simple setcc's so far.
Chris Lattner6ec77452005-08-09 20:21:10 +00001653 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Chris Lattner88c8a232005-01-07 07:49:41 +00001654
1655 unsigned Opc;
1656
1657 // Handle integer conditions first.
Chris Lattner6ec77452005-08-09 20:21:10 +00001658 if (MVT::isInteger(Cond.getOperand(0).getValueType())) {
1659 switch (CC) {
Chris Lattner88c8a232005-01-07 07:49:41 +00001660 default: assert(0 && "Illegal integer SetCC!");
1661 case ISD::SETEQ: Opc = X86::JE; break;
1662 case ISD::SETGT: Opc = X86::JG; break;
1663 case ISD::SETGE: Opc = X86::JGE; break;
1664 case ISD::SETLT: Opc = X86::JL; break;
1665 case ISD::SETLE: Opc = X86::JLE; break;
1666 case ISD::SETNE: Opc = X86::JNE; break;
1667 case ISD::SETULT: Opc = X86::JB; break;
1668 case ISD::SETUGT: Opc = X86::JA; break;
1669 case ISD::SETULE: Opc = X86::JBE; break;
1670 case ISD::SETUGE: Opc = X86::JAE; break;
1671 }
Chris Lattner37ed2852005-01-11 04:06:27 +00001672 Select(Chain);
Chris Lattner6ec77452005-08-09 20:21:10 +00001673 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.hasOneUse());
Chris Lattner88c8a232005-01-07 07:49:41 +00001674 BuildMI(BB, Opc, 1).addMBB(Dest);
1675 return false;
1676 }
1677
Chris Lattner88c8a232005-01-07 07:49:41 +00001678 unsigned Opc2 = 0; // Second branch if needed.
1679
1680 // On a floating point condition, the flags are set as follows:
1681 // ZF PF CF op
1682 // 0 | 0 | 0 | X > Y
1683 // 0 | 0 | 1 | X < Y
1684 // 1 | 0 | 0 | X == Y
1685 // 1 | 1 | 1 | unordered
1686 //
Chris Lattner6ec77452005-08-09 20:21:10 +00001687 switch (CC) {
Chris Lattner88c8a232005-01-07 07:49:41 +00001688 default: assert(0 && "Invalid FP setcc!");
1689 case ISD::SETUEQ:
1690 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
1691 case ISD::SETOGT:
1692 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
1693 case ISD::SETOGE:
1694 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
1695 case ISD::SETULT:
1696 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
1697 case ISD::SETULE:
1698 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
1699 case ISD::SETONE:
1700 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
1701 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
1702 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
1703 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1704 Opc = X86::JA; // ZF = 0 & CF = 0
1705 Opc2 = X86::JP; // PF = 1
1706 break;
1707 case ISD::SETUGE: // PF = 1 | CF = 0
1708 Opc = X86::JAE; // CF = 0
1709 Opc2 = X86::JP; // PF = 1
1710 break;
1711 case ISD::SETUNE: // PF = 1 | ZF = 0
1712 Opc = X86::JNE; // ZF = 0
1713 Opc2 = X86::JP; // PF = 1
1714 break;
1715 case ISD::SETOEQ: // PF = 0 & ZF = 1
1716 //X86::JNP, X86::JE
1717 //X86::AND8rr
1718 return true; // FIXME: Emit more efficient code for this branch.
1719 case ISD::SETOLT: // PF = 0 & CF = 1
1720 //X86::JNP, X86::JB
1721 //X86::AND8rr
1722 return true; // FIXME: Emit more efficient code for this branch.
1723 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1724 //X86::JNP, X86::JBE
1725 //X86::AND8rr
1726 return true; // FIXME: Emit more efficient code for this branch.
1727 }
1728
Chris Lattner37ed2852005-01-11 04:06:27 +00001729 Select(Chain);
Chris Lattner6ec77452005-08-09 20:21:10 +00001730 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.hasOneUse());
Chris Lattner88c8a232005-01-07 07:49:41 +00001731 BuildMI(BB, Opc, 1).addMBB(Dest);
1732 if (Opc2)
1733 BuildMI(BB, Opc2, 1).addMBB(Dest);
1734 return false;
1735}
1736
Chris Lattner1d13a922005-01-10 22:10:13 +00001737/// EmitSelectCC - Emit code into BB that performs a select operation between
Nate Begeman8d394eb2005-08-03 23:26:28 +00001738/// the two registers RTrue and RFalse, generating a result into RDest.
Chris Lattner1d13a922005-01-10 22:10:13 +00001739///
Nate Begeman8d394eb2005-08-03 23:26:28 +00001740void ISel::EmitSelectCC(SDOperand Cond, SDOperand True, SDOperand False,
1741 MVT::ValueType SVT, unsigned RDest) {
1742 unsigned RTrue, RFalse;
Chris Lattner1d13a922005-01-10 22:10:13 +00001743 enum Condition {
1744 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
1745 NOT_SET
1746 } CondCode = NOT_SET;
1747
1748 static const unsigned CMOVTAB16[] = {
1749 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
1750 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukmanc88330a2005-04-21 23:38:14 +00001751 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner1d13a922005-01-10 22:10:13 +00001752 };
1753 static const unsigned CMOVTAB32[] = {
1754 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
1755 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukmanc88330a2005-04-21 23:38:14 +00001756 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner1d13a922005-01-10 22:10:13 +00001757 };
1758 static const unsigned CMOVTABFP[] = {
1759 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
1760 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
1761 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
1762 };
Nate Begemana0b5e032005-07-15 00:38:55 +00001763 static const int SSE_CMOVTAB[] = {
Nate Begeman8d394eb2005-08-03 23:26:28 +00001764 /*CMPEQ*/ 0, /*CMPNEQ*/ 4, /*missing*/ 0, /*missing*/ 0,
1765 /*missing*/ 0, /*missing*/ 0, /*CMPLT*/ 1, /*CMPLE*/ 2,
1766 /*CMPNLE*/ 6, /*CMPNLT*/ 5, /*CMPUNORD*/ 3, /*CMPORD*/ 7
Nate Begeman8a093362005-07-06 18:59:04 +00001767 };
Nate Begeman8d394eb2005-08-03 23:26:28 +00001768
Chris Lattner6ec77452005-08-09 20:21:10 +00001769 if (Cond.getOpcode() == ISD::SETCC) {
1770 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
1771 if (MVT::isInteger(Cond.getOperand(0).getValueType())) {
1772 switch (CC) {
Chris Lattner1d13a922005-01-10 22:10:13 +00001773 default: assert(0 && "Unknown integer comparison!");
1774 case ISD::SETEQ: CondCode = EQ; break;
1775 case ISD::SETGT: CondCode = GT; break;
1776 case ISD::SETGE: CondCode = GE; break;
1777 case ISD::SETLT: CondCode = LT; break;
1778 case ISD::SETLE: CondCode = LE; break;
1779 case ISD::SETNE: CondCode = NE; break;
1780 case ISD::SETULT: CondCode = B; break;
1781 case ISD::SETUGT: CondCode = A; break;
1782 case ISD::SETULE: CondCode = BE; break;
1783 case ISD::SETUGE: CondCode = AE; break;
1784 }
1785 } else {
1786 // On a floating point condition, the flags are set as follows:
1787 // ZF PF CF op
1788 // 0 | 0 | 0 | X > Y
1789 // 0 | 0 | 1 | X < Y
1790 // 1 | 0 | 0 | X == Y
1791 // 1 | 1 | 1 | unordered
1792 //
Chris Lattner6ec77452005-08-09 20:21:10 +00001793 switch (CC) {
Chris Lattner1d13a922005-01-10 22:10:13 +00001794 default: assert(0 && "Unknown FP comparison!");
1795 case ISD::SETUEQ:
1796 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
1797 case ISD::SETOGT:
1798 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
1799 case ISD::SETOGE:
1800 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
1801 case ISD::SETULT:
1802 case ISD::SETLT: CondCode = B; break; // True if CF = 1
1803 case ISD::SETULE:
1804 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
1805 case ISD::SETONE:
1806 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
1807 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1808 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1809 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1810 case ISD::SETUGE: // PF = 1 | CF = 0
1811 case ISD::SETUNE: // PF = 1 | ZF = 0
1812 case ISD::SETOEQ: // PF = 0 & ZF = 1
1813 case ISD::SETOLT: // PF = 0 & CF = 1
1814 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1815 // We cannot emit this comparison as a single cmov.
1816 break;
1817 }
1818 }
Chris Lattner6ec77452005-08-09 20:21:10 +00001819
Chris Lattner1d13a922005-01-10 22:10:13 +00001820
Chris Lattner6ec77452005-08-09 20:21:10 +00001821 // There's no SSE equivalent of FCMOVE. For cases where we set a condition
1822 // code above and one of the results of the select is +0.0, then we can fake
1823 // it up through a clever AND with mask. Otherwise, we will fall through to
1824 // the code below that will use a PHI node to select the right value.
1825 if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) {
1826 if (Cond.getOperand(0).getValueType() == SVT &&
1827 NOT_SET != CondCode) {
1828 ConstantFPSDNode *CT = dyn_cast<ConstantFPSDNode>(True);
1829 ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(False);
1830 bool TrueZero = CT && CT->isExactlyValue(0.0);
1831 bool FalseZero = CF && CF->isExactlyValue(0.0);
1832 if (TrueZero || FalseZero) {
1833 SDOperand LHS = Cond.getOperand(0);
1834 SDOperand RHS = Cond.getOperand(1);
1835
1836 // Select the two halves of the condition
1837 unsigned RLHS, RRHS;
1838 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1839 RLHS = SelectExpr(LHS);
1840 RRHS = SelectExpr(RHS);
1841 } else {
1842 RRHS = SelectExpr(RHS);
1843 RLHS = SelectExpr(LHS);
1844 }
1845
1846 // Emit the comparison and generate a mask from it
1847 unsigned MaskReg = MakeReg(SVT);
1848 unsigned Opc = (SVT == MVT::f32) ? X86::CMPSSrr : X86::CMPSDrr;
1849 BuildMI(BB, Opc, 3, MaskReg).addReg(RLHS).addReg(RRHS)
1850 .addImm(SSE_CMOVTAB[CondCode]);
1851
1852 if (TrueZero) {
1853 RFalse = SelectExpr(False);
1854 Opc = (SVT == MVT::f32) ? X86::ANDNPSrr : X86::ANDNPDrr;
1855 BuildMI(BB, Opc, 2, RDest).addReg(MaskReg).addReg(RFalse);
1856 } else {
1857 RTrue = SelectExpr(True);
1858 Opc = (SVT == MVT::f32) ? X86::ANDPSrr : X86::ANDPDrr;
1859 BuildMI(BB, Opc, 2, RDest).addReg(MaskReg).addReg(RTrue);
1860 }
1861 return;
Nate Begeman8d394eb2005-08-03 23:26:28 +00001862 }
Nate Begeman8d394eb2005-08-03 23:26:28 +00001863 }
Nate Begeman8a093362005-07-06 18:59:04 +00001864 }
Nate Begeman8d394eb2005-08-03 23:26:28 +00001865 }
1866
1867 // Select the true and false values for use in both the SSE PHI case, and the
1868 // integer or x87 cmov cases below.
1869 if (getRegPressure(True) > getRegPressure(False)) {
1870 RTrue = SelectExpr(True);
1871 RFalse = SelectExpr(False);
1872 } else {
1873 RFalse = SelectExpr(False);
1874 RTrue = SelectExpr(True);
1875 }
1876
1877 // Since there's no SSE equivalent of FCMOVE, and we couldn't generate an
1878 // AND with mask, we'll have to do the normal RISC thing and generate a PHI
1879 // node to select between the true and false values.
1880 if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) {
1881 // FIXME: emit a direct compare and branch rather than setting a cond reg
1882 // and testing it.
1883 unsigned CondReg = SelectExpr(Cond);
1884 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1885
1886 // Create an iterator with which to insert the MBB for copying the false
1887 // value and the MBB to hold the PHI instruction for this SetCC.
1888 MachineBasicBlock *thisMBB = BB;
1889 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1890 ilist<MachineBasicBlock>::iterator It = BB;
1891 ++It;
1892
1893 // thisMBB:
1894 // ...
1895 // TrueVal = ...
1896 // cmpTY ccX, r1, r2
1897 // bCC sinkMBB
1898 // fallthrough --> copy0MBB
1899 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1900 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1901 BuildMI(BB, X86::JNE, 1).addMBB(sinkMBB);
1902 MachineFunction *F = BB->getParent();
1903 F->getBasicBlockList().insert(It, copy0MBB);
1904 F->getBasicBlockList().insert(It, sinkMBB);
1905 // Update machine-CFG edges
1906 BB->addSuccessor(copy0MBB);
1907 BB->addSuccessor(sinkMBB);
1908
1909 // copy0MBB:
1910 // %FalseValue = ...
1911 // # fallthrough to sinkMBB
1912 BB = copy0MBB;
1913 // Update machine-CFG edges
1914 BB->addSuccessor(sinkMBB);
1915
1916 // sinkMBB:
1917 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1918 // ...
1919 BB = sinkMBB;
1920 BuildMI(BB, X86::PHI, 4, RDest).addReg(RFalse)
1921 .addMBB(copy0MBB).addReg(RTrue).addMBB(thisMBB);
Nate Begeman8a093362005-07-06 18:59:04 +00001922 return;
1923 }
1924
Chris Lattner1d13a922005-01-10 22:10:13 +00001925 unsigned Opc = 0;
1926 if (CondCode != NOT_SET) {
1927 switch (SVT) {
1928 default: assert(0 && "Cannot select this type!");
1929 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1930 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattnere44e6d12005-01-11 03:50:45 +00001931 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner1d13a922005-01-10 22:10:13 +00001932 }
1933 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001934
Chris Lattner1d13a922005-01-10 22:10:13 +00001935 // Finally, if we weren't able to fold this, just emit the condition and test
1936 // it.
1937 if (CondCode == NOT_SET || Opc == 0) {
1938 // Get the condition into the zero flag.
1939 unsigned CondReg = SelectExpr(Cond);
1940 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1941
1942 switch (SVT) {
1943 default: assert(0 && "Cannot select this type!");
1944 case MVT::i16: Opc = X86::CMOVE16rr; break;
1945 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattnere44e6d12005-01-11 03:50:45 +00001946 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner1d13a922005-01-10 22:10:13 +00001947 }
1948 } else {
1949 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattner3be6cd52005-01-17 01:34:14 +00001950 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattner8fea42b2005-01-11 03:37:59 +00001951 std::swap(RTrue, RFalse);
Chris Lattner1d13a922005-01-10 22:10:13 +00001952 }
1953 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1954}
1955
Chris Lattner3be6cd52005-01-17 01:34:14 +00001956void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001957 unsigned Opc;
Chris Lattner88c8a232005-01-07 07:49:41 +00001958 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1959 Opc = 0;
Chris Lattnera56d29d2005-01-17 06:26:58 +00001960 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattner2cfce682005-01-12 02:02:48 +00001961 switch (RHS.getValueType()) {
1962 default: break;
1963 case MVT::i1:
1964 case MVT::i8: Opc = X86::CMP8mi; break;
1965 case MVT::i16: Opc = X86::CMP16mi; break;
1966 case MVT::i32: Opc = X86::CMP32mi; break;
1967 }
1968 if (Opc) {
1969 X86AddressMode AM;
1970 EmitFoldedLoad(LHS, AM);
1971 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1972 return;
1973 }
1974 }
1975
Chris Lattner88c8a232005-01-07 07:49:41 +00001976 switch (RHS.getValueType()) {
1977 default: break;
1978 case MVT::i1:
1979 case MVT::i8: Opc = X86::CMP8ri; break;
1980 case MVT::i16: Opc = X86::CMP16ri; break;
1981 case MVT::i32: Opc = X86::CMP32ri; break;
1982 }
1983 if (Opc) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001984 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner88c8a232005-01-07 07:49:41 +00001985 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1986 return;
1987 }
Chris Lattner720a62e2005-01-14 22:37:41 +00001988 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
Nate Begeman8a093362005-07-06 18:59:04 +00001989 if (!X86ScalarSSE && (CN->isExactlyValue(+0.0) ||
1990 CN->isExactlyValue(-0.0))) {
Chris Lattner720a62e2005-01-14 22:37:41 +00001991 unsigned Reg = SelectExpr(LHS);
1992 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1993 BuildMI(BB, X86::FNSTSW8r, 0);
1994 BuildMI(BB, X86::SAHF, 1);
Chris Lattner43832b02005-03-17 16:29:26 +00001995 return;
Chris Lattner720a62e2005-01-14 22:37:41 +00001996 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001997 }
1998
Chris Lattner2cfce682005-01-12 02:02:48 +00001999 Opc = 0;
Chris Lattnera56d29d2005-01-17 06:26:58 +00002000 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattner2cfce682005-01-12 02:02:48 +00002001 switch (RHS.getValueType()) {
2002 default: break;
2003 case MVT::i1:
2004 case MVT::i8: Opc = X86::CMP8mr; break;
2005 case MVT::i16: Opc = X86::CMP16mr; break;
2006 case MVT::i32: Opc = X86::CMP32mr; break;
2007 }
2008 if (Opc) {
2009 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00002010 EmitFoldedLoad(LHS, AM);
2011 unsigned Reg = SelectExpr(RHS);
Chris Lattner2cfce682005-01-12 02:02:48 +00002012 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
2013 return;
2014 }
2015 }
2016
Chris Lattner88c8a232005-01-07 07:49:41 +00002017 switch (LHS.getValueType()) {
2018 default: assert(0 && "Cannot compare this value!");
2019 case MVT::i1:
2020 case MVT::i8: Opc = X86::CMP8rr; break;
2021 case MVT::i16: Opc = X86::CMP16rr; break;
2022 case MVT::i32: Opc = X86::CMP32rr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00002023 case MVT::f32: Opc = X86::UCOMISSrr; break;
2024 case MVT::f64: Opc = X86ScalarSSE ? X86::UCOMISDrr : X86::FUCOMIr; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002025 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002026 unsigned Tmp1, Tmp2;
2027 if (getRegPressure(LHS) > getRegPressure(RHS)) {
2028 Tmp1 = SelectExpr(LHS);
2029 Tmp2 = SelectExpr(RHS);
2030 } else {
2031 Tmp2 = SelectExpr(RHS);
2032 Tmp1 = SelectExpr(LHS);
2033 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002034 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
2035}
2036
Chris Lattner62b22422005-01-11 21:19:59 +00002037/// isFoldableLoad - Return true if this is a load instruction that can safely
2038/// be folded into an operation that uses it.
Chris Lattner30607ec2005-01-25 20:03:11 +00002039bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
2040 if (Op.getOpcode() == ISD::LOAD) {
2041 // FIXME: currently can't fold constant pool indexes.
2042 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
2043 return false;
2044 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
Chris Lattner53676df2005-07-10 01:56:13 +00002045 cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::f32) {
Chris Lattner30607ec2005-01-25 20:03:11 +00002046 // FIXME: currently can't fold constant pool indexes.
2047 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
2048 return false;
2049 } else {
Chris Lattner62b22422005-01-11 21:19:59 +00002050 return false;
Chris Lattner30607ec2005-01-25 20:03:11 +00002051 }
Chris Lattner62b22422005-01-11 21:19:59 +00002052
2053 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner3676cd62005-01-13 05:53:16 +00002054 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
2055 if (ExprMap.count(Op.getValue(1))) return false;
2056 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner78d30282005-01-18 03:51:59 +00002057 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattner62b22422005-01-11 21:19:59 +00002058
Chris Lattnera56d29d2005-01-17 06:26:58 +00002059 // If there is not just one use of its value, we cannot fold.
2060 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
2061
2062 // Finally, we cannot fold the load into the operation if this would induce a
2063 // cycle into the resultant dag. To check for this, see if OtherOp (the other
2064 // operand of the operation we are folding the load into) can possible use the
2065 // chain node defined by the load.
2066 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
2067 std::set<SDNode*> Visited;
2068 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
2069 return false;
2070 }
2071 return true;
Chris Lattner62b22422005-01-11 21:19:59 +00002072}
2073
Chris Lattnera56d29d2005-01-17 06:26:58 +00002074
Chris Lattner62b22422005-01-11 21:19:59 +00002075/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
2076/// and compute the address being loaded into AM.
2077void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
2078 SDOperand Chain = Op.getOperand(0);
2079 SDOperand Address = Op.getOperand(1);
Chris Lattnera7acdda2005-01-18 01:06:26 +00002080
Chris Lattner62b22422005-01-11 21:19:59 +00002081 if (getRegPressure(Chain) > getRegPressure(Address)) {
2082 Select(Chain);
2083 SelectAddress(Address, AM);
2084 } else {
2085 SelectAddress(Address, AM);
2086 Select(Chain);
2087 }
2088
2089 // The chain for this load is now lowered.
Chris Lattner3676cd62005-01-13 05:53:16 +00002090 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
2091 "Load emitted more than once?");
Chris Lattner78d30282005-01-18 03:51:59 +00002092 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner3676cd62005-01-13 05:53:16 +00002093 assert(0 && "Load emitted more than once!");
Chris Lattner62b22422005-01-11 21:19:59 +00002094}
2095
Chris Lattner29f58192005-01-19 07:37:26 +00002096// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
2097// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
2098// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
2099// return true.
2100bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner41fe2012005-01-19 06:18:43 +00002101 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
2102 // good!
2103 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
2104 std::swap(Op1, Op2); // Op1 is the SHL now.
2105 } else {
2106 return false; // No match
2107 }
2108
2109 SDOperand ShlVal = Op1.getOperand(0);
2110 SDOperand ShlAmt = Op1.getOperand(1);
2111 SDOperand ShrVal = Op2.getOperand(0);
2112 SDOperand ShrAmt = Op2.getOperand(1);
2113
Chris Lattner29f58192005-01-19 07:37:26 +00002114 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
2115
Chris Lattner41fe2012005-01-19 06:18:43 +00002116 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
2117 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
2118 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattnerde87d1462005-01-19 08:07:05 +00002119 if (SubCST->getValue() == RegSize) {
2120 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner41fe2012005-01-19 06:18:43 +00002121 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattnerde87d1462005-01-19 08:07:05 +00002122 if (ShrVal == ShlVal) {
2123 unsigned Reg, ShAmt;
2124 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
2125 Reg = SelectExpr(ShrVal);
2126 ShAmt = SelectExpr(ShrAmt);
2127 } else {
2128 ShAmt = SelectExpr(ShrAmt);
2129 Reg = SelectExpr(ShrVal);
2130 }
2131 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2132 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
2133 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
2134 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
2135 return true;
2136 } else if (RegSize != 8) {
Chris Lattner41fe2012005-01-19 06:18:43 +00002137 unsigned AReg, BReg;
2138 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner41fe2012005-01-19 06:18:43 +00002139 BReg = SelectExpr(ShlVal);
Chris Lattner474aac42005-01-19 17:24:34 +00002140 AReg = SelectExpr(ShrVal);
Chris Lattner41fe2012005-01-19 06:18:43 +00002141 } else {
Chris Lattner41fe2012005-01-19 06:18:43 +00002142 AReg = SelectExpr(ShrVal);
Chris Lattner474aac42005-01-19 17:24:34 +00002143 BReg = SelectExpr(ShlVal);
Chris Lattner41fe2012005-01-19 06:18:43 +00002144 }
Chris Lattnerde87d1462005-01-19 08:07:05 +00002145 unsigned ShAmt = SelectExpr(ShrAmt);
2146 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2147 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
2148 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner41fe2012005-01-19 06:18:43 +00002149 return true;
2150 }
2151 }
2152
Chris Lattnerde87d1462005-01-19 08:07:05 +00002153 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
2154 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
2155 if (SubCST->getValue() == RegSize) {
2156 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
2157 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
2158 if (ShrVal == ShlVal) {
2159 unsigned Reg, ShAmt;
2160 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
2161 Reg = SelectExpr(ShrVal);
2162 ShAmt = SelectExpr(ShlAmt);
2163 } else {
2164 ShAmt = SelectExpr(ShlAmt);
2165 Reg = SelectExpr(ShrVal);
2166 }
2167 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2168 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
2169 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
2170 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
2171 return true;
2172 } else if (RegSize != 8) {
2173 unsigned AReg, BReg;
2174 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner474aac42005-01-19 17:24:34 +00002175 AReg = SelectExpr(ShlVal);
2176 BReg = SelectExpr(ShrVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00002177 } else {
Chris Lattner474aac42005-01-19 17:24:34 +00002178 BReg = SelectExpr(ShrVal);
2179 AReg = SelectExpr(ShlVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00002180 }
2181 unsigned ShAmt = SelectExpr(ShlAmt);
2182 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2183 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
2184 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
2185 return true;
2186 }
2187 }
Chris Lattner41fe2012005-01-19 06:18:43 +00002188
Chris Lattnerde87d1462005-01-19 08:07:05 +00002189 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
2190 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
2191 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
2192 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
2193 // (A >> 5) | (A << 27) --> ROR A, 5
2194 // (A >> 5) | (B << 27) --> SHRD A, B, 5
2195 if (ShrVal == ShlVal) {
2196 unsigned Reg = SelectExpr(ShrVal);
2197 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
2198 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
2199 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
2200 return true;
2201 } else if (RegSize != 8) {
2202 unsigned AReg, BReg;
2203 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerde87d1462005-01-19 08:07:05 +00002204 BReg = SelectExpr(ShlVal);
Chris Lattner474aac42005-01-19 17:24:34 +00002205 AReg = SelectExpr(ShrVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00002206 } else {
Chris Lattnerde87d1462005-01-19 08:07:05 +00002207 AReg = SelectExpr(ShrVal);
Chris Lattner474aac42005-01-19 17:24:34 +00002208 BReg = SelectExpr(ShlVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00002209 }
2210 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
2211 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
2212 .addImm(ShrCst->getValue());
2213 return true;
2214 }
2215 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002216
Chris Lattner41fe2012005-01-19 06:18:43 +00002217 return false;
2218}
2219
Chris Lattner88c8a232005-01-07 07:49:41 +00002220unsigned ISel::SelectExpr(SDOperand N) {
2221 unsigned Result;
Chris Lattner9982da22005-10-02 16:29:36 +00002222 unsigned Tmp1 = 0, Tmp2 = 0, Tmp3 = 0, Opc = 0;
Chris Lattnerb52e0412005-01-08 19:28:19 +00002223 SDNode *Node = N.Val;
Chris Lattner62b22422005-01-11 21:19:59 +00002224 SDOperand Op0, Op1;
Chris Lattnerb52e0412005-01-08 19:28:19 +00002225
Chris Lattner720a62e2005-01-14 22:37:41 +00002226 if (Node->getOpcode() == ISD::CopyFromReg) {
Chris Lattner7c762782005-08-16 21:56:37 +00002227 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
2228 // Just use the specified register as our input if we can.
2229 if (MRegisterInfo::isVirtualRegister(Reg) || Reg == X86::ESP)
2230 return Reg;
Chris Lattner720a62e2005-01-14 22:37:41 +00002231 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002232
Chris Lattner62b22422005-01-11 21:19:59 +00002233 unsigned &Reg = ExprMap[N];
2234 if (Reg) return Reg;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002235
Chris Lattnera31d4c72005-04-02 04:01:14 +00002236 switch (N.getOpcode()) {
2237 default:
Chris Lattner62b22422005-01-11 21:19:59 +00002238 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnera31d4c72005-04-02 04:01:14 +00002239 MakeReg(N.getValueType()) : 1;
2240 break;
Chris Lattner1b3520c2005-05-14 08:48:15 +00002241 case X86ISD::TAILCALL:
2242 case X86ISD::CALL:
Chris Lattner62b22422005-01-11 21:19:59 +00002243 // If this is a call instruction, make sure to prepare ALL of the result
2244 // values as well as the chain.
Chris Lattner1b3520c2005-05-14 08:48:15 +00002245 ExprMap[N.getValue(0)] = 1;
2246 if (Node->getNumValues() > 1) {
2247 Result = MakeReg(Node->getValueType(1));
2248 ExprMap[N.getValue(1)] = Result;
2249 for (unsigned i = 2, e = Node->getNumValues(); i != e; ++i)
Chris Lattner62b22422005-01-11 21:19:59 +00002250 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattner1b3520c2005-05-14 08:48:15 +00002251 } else {
2252 Result = 1;
Chris Lattner88c8a232005-01-07 07:49:41 +00002253 }
Chris Lattnera31d4c72005-04-02 04:01:14 +00002254 break;
2255 case ISD::ADD_PARTS:
2256 case ISD::SUB_PARTS:
2257 case ISD::SHL_PARTS:
2258 case ISD::SRL_PARTS:
2259 case ISD::SRA_PARTS:
2260 Result = MakeReg(Node->getValueType(0));
2261 ExprMap[N.getValue(0)] = Result;
2262 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
2263 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
2264 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002265 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002266
Chris Lattner88c8a232005-01-07 07:49:41 +00002267 switch (N.getOpcode()) {
2268 default:
Chris Lattnerb52e0412005-01-08 19:28:19 +00002269 Node->dump();
Chris Lattner88c8a232005-01-07 07:49:41 +00002270 assert(0 && "Node not handled!\n");
Nate Begeman8a093362005-07-06 18:59:04 +00002271 case ISD::FP_EXTEND:
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002272 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
Nate Begeman8a093362005-07-06 18:59:04 +00002273 Tmp1 = SelectExpr(N.getOperand(0));
2274 BuildMI(BB, X86::CVTSS2SDrr, 1, Result).addReg(Tmp1);
2275 return Result;
Nate Begemana0b5e032005-07-15 00:38:55 +00002276 case ISD::FP_ROUND:
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002277 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
Nate Begemana0b5e032005-07-15 00:38:55 +00002278 Tmp1 = SelectExpr(N.getOperand(0));
2279 BuildMI(BB, X86::CVTSD2SSrr, 1, Result).addReg(Tmp1);
2280 return Result;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002281 case ISD::CopyFromReg:
2282 Select(N.getOperand(0));
2283 if (Result == 1) {
2284 Reg = Result = ExprMap[N.getValue(0)] =
2285 MakeReg(N.getValue(0).getValueType());
2286 }
Chris Lattner7c762782005-08-16 21:56:37 +00002287 Tmp1 = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002288 switch (Node->getValueType(0)) {
2289 default: assert(0 && "Cannot CopyFromReg this!");
2290 case MVT::i1:
2291 case MVT::i8:
Chris Lattner7c762782005-08-16 21:56:37 +00002292 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002293 return Result;
2294 case MVT::i16:
Chris Lattner7c762782005-08-16 21:56:37 +00002295 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(Tmp1);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002296 return Result;
2297 case MVT::i32:
Chris Lattner7c762782005-08-16 21:56:37 +00002298 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(Tmp1);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002299 return Result;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002300 }
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00002301
Chris Lattner88c8a232005-01-07 07:49:41 +00002302 case ISD::FrameIndex:
2303 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
2304 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
2305 return Result;
2306 case ISD::ConstantPool:
Chris Lattnerc30405e2005-08-26 17:15:30 +00002307 Tmp1 = BB->getParent()->getConstantPool()->
2308 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner88c8a232005-01-07 07:49:41 +00002309 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
2310 return Result;
2311 case ISD::ConstantFP:
Nate Begeman8d394eb2005-08-03 23:26:28 +00002312 if (X86ScalarSSE) {
2313 assert(cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) &&
2314 "SSE only supports +0.0");
2315 Opc = (N.getValueType() == MVT::f32) ? X86::FLD0SS : X86::FLD0SD;
2316 BuildMI(BB, Opc, 0, Result);
2317 return Result;
2318 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002319 ContainsFPCode = true;
2320 Tmp1 = Result; // Intermediate Register
2321 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
2322 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
2323 Tmp1 = MakeReg(MVT::f64);
2324
2325 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
2326 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
2327 BuildMI(BB, X86::FLD0, 0, Tmp1);
2328 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
2329 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
2330 BuildMI(BB, X86::FLD1, 0, Tmp1);
2331 else
2332 assert(0 && "Unexpected constant!");
2333 if (Tmp1 != Result)
2334 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
2335 return Result;
2336 case ISD::Constant:
2337 switch (N.getValueType()) {
2338 default: assert(0 && "Cannot use constants of this type!");
2339 case MVT::i1:
2340 case MVT::i8: Opc = X86::MOV8ri; break;
2341 case MVT::i16: Opc = X86::MOV16ri; break;
2342 case MVT::i32: Opc = X86::MOV32ri; break;
2343 }
2344 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
2345 return Result;
Chris Lattnerf4b985d2005-04-01 22:46:45 +00002346 case ISD::UNDEF:
2347 if (Node->getValueType(0) == MVT::f64) {
2348 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
2349 BuildMI(BB, X86::FLD0, 0, Result);
2350 } else {
2351 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
2352 }
2353 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002354 case ISD::GlobalAddress: {
2355 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanf26625e2005-07-12 01:41:54 +00002356 // For Darwin, external and weak symbols are indirect, so we want to load
2357 // the value at address GV, not the value of GV itself.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002358 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemanf26625e2005-07-12 01:41:54 +00002359 (GV->hasWeakLinkage() || GV->isExternal())) {
2360 BuildMI(BB, X86::MOV32rm, 4, Result).addReg(0).addZImm(1).addReg(0)
2361 .addGlobalAddress(GV, false, 0);
2362 } else {
2363 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
2364 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002365 return Result;
2366 }
2367 case ISD::ExternalSymbol: {
2368 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
2369 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
2370 return Result;
2371 }
Chris Lattner210975c2005-09-02 00:16:09 +00002372 case ISD::ANY_EXTEND: // treat any extend like zext
Chris Lattner88c8a232005-01-07 07:49:41 +00002373 case ISD::ZERO_EXTEND: {
2374 int DestIs16 = N.getValueType() == MVT::i16;
2375 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner282781c2005-01-09 18:52:44 +00002376
2377 // FIXME: This hack is here for zero extension casts from bool to i8. This
2378 // would not be needed if bools were promoted by Legalize.
2379 if (N.getValueType() == MVT::i8) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00002380 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner282781c2005-01-09 18:52:44 +00002381 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
2382 return Result;
2383 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002384
Chris Lattnera56d29d2005-01-17 06:26:58 +00002385 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00002386 static const unsigned Opc[3] = {
2387 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
2388 };
2389
2390 X86AddressMode AM;
2391 EmitFoldedLoad(N.getOperand(0), AM);
2392 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002393
Chris Lattnerb0eef822005-01-11 23:33:00 +00002394 return Result;
2395 }
2396
Chris Lattner88c8a232005-01-07 07:49:41 +00002397 static const unsigned Opc[3] = {
2398 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
2399 };
Chris Lattnerb0eef822005-01-11 23:33:00 +00002400 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002401 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
2402 return Result;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002403 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002404 case ISD::SIGN_EXTEND: {
2405 int DestIs16 = N.getValueType() == MVT::i16;
2406 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
2407
Chris Lattner282781c2005-01-09 18:52:44 +00002408 // FIXME: Legalize should promote bools to i8!
2409 assert(N.getOperand(0).getValueType() != MVT::i1 &&
2410 "Sign extend from bool not implemented!");
2411
Chris Lattnera56d29d2005-01-17 06:26:58 +00002412 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00002413 static const unsigned Opc[3] = {
2414 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
2415 };
2416
2417 X86AddressMode AM;
2418 EmitFoldedLoad(N.getOperand(0), AM);
2419 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
2420 return Result;
2421 }
2422
Chris Lattner88c8a232005-01-07 07:49:41 +00002423 static const unsigned Opc[3] = {
2424 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
2425 };
2426 Tmp1 = SelectExpr(N.getOperand(0));
2427 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
2428 return Result;
2429 }
2430 case ISD::TRUNCATE:
2431 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
2432 // a move out of AX or AL.
2433 switch (N.getOperand(0).getValueType()) {
2434 default: assert(0 && "Unknown truncate!");
2435 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
2436 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
2437 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
2438 }
2439 Tmp1 = SelectExpr(N.getOperand(0));
2440 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2441
2442 switch (N.getValueType()) {
2443 default: assert(0 && "Unknown truncate!");
2444 case MVT::i1:
2445 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
2446 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
2447 }
2448 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
2449 return Result;
2450
Chris Lattner507a2752005-07-16 00:28:20 +00002451 case ISD::SINT_TO_FP: {
Nate Begeman8a093362005-07-06 18:59:04 +00002452 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2453 unsigned PromoteOpcode = 0;
2454
Nate Begeman7e74c832005-07-16 02:02:34 +00002455 // We can handle any sint to fp with the direct sse conversion instructions.
Nate Begeman8a093362005-07-06 18:59:04 +00002456 if (X86ScalarSSE) {
Nate Begeman7e74c832005-07-16 02:02:34 +00002457 Opc = (N.getValueType() == MVT::f64) ? X86::CVTSI2SDrr : X86::CVTSI2SSrr;
Nate Begeman8a093362005-07-06 18:59:04 +00002458 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2459 return Result;
2460 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002461
Chris Lattnere44e6d12005-01-11 03:50:45 +00002462 ContainsFPCode = true;
Chris Lattner282781c2005-01-09 18:52:44 +00002463
Chris Lattner282781c2005-01-09 18:52:44 +00002464 // Spill the integer to memory and reload it from there.
Nate Begeman7e74c832005-07-16 02:02:34 +00002465 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
Chris Lattner282781c2005-01-09 18:52:44 +00002466 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
2467 MachineFunction *F = BB->getParent();
2468 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2469
2470 switch (SrcTy) {
Chris Lattner282781c2005-01-09 18:52:44 +00002471 case MVT::i32:
Chris Lattner507a2752005-07-16 00:28:20 +00002472 addFrameReference(BuildMI(BB, X86::MOV32mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattner282781c2005-01-09 18:52:44 +00002473 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
2474 break;
2475 case MVT::i16:
Chris Lattner507a2752005-07-16 00:28:20 +00002476 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattner282781c2005-01-09 18:52:44 +00002477 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
2478 break;
2479 default: break; // No promotion required.
2480 }
Chris Lattner507a2752005-07-16 00:28:20 +00002481 return Result;
Chris Lattner282781c2005-01-09 18:52:44 +00002482 }
Chris Lattner4738d1b2005-07-30 00:05:54 +00002483 case ISD::FP_TO_SINT:
Chris Lattner282781c2005-01-09 18:52:44 +00002484 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2485
Nate Begeman8a093362005-07-06 18:59:04 +00002486 // If the target supports SSE2 and is performing FP operations in SSE regs
2487 // instead of the FP stack, then we can use the efficient CVTSS2SI and
2488 // CVTSD2SI instructions.
Chris Lattner4738d1b2005-07-30 00:05:54 +00002489 assert(X86ScalarSSE);
2490 if (MVT::f32 == N.getOperand(0).getValueType()) {
2491 BuildMI(BB, X86::CVTTSS2SIrr, 1, Result).addReg(Tmp1);
2492 } else if (MVT::f64 == N.getOperand(0).getValueType()) {
2493 BuildMI(BB, X86::CVTTSD2SIrr, 1, Result).addReg(Tmp1);
2494 } else {
2495 assert(0 && "Not an f32 or f64?");
2496 abort();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002497 }
Chris Lattner282781c2005-01-09 18:52:44 +00002498 return Result;
Chris Lattner4738d1b2005-07-30 00:05:54 +00002499
Chris Lattner0815dcae2005-09-28 22:29:17 +00002500 case ISD::FADD:
Chris Lattner88c8a232005-01-07 07:49:41 +00002501 case ISD::ADD:
Chris Lattner62b22422005-01-11 21:19:59 +00002502 Op0 = N.getOperand(0);
2503 Op1 = N.getOperand(1);
2504
Chris Lattner30607ec2005-01-25 20:03:11 +00002505 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattner62b22422005-01-11 21:19:59 +00002506 std::swap(Op0, Op1);
Chris Lattnera56d29d2005-01-17 06:26:58 +00002507 goto FoldAdd;
2508 }
Chris Lattner62b22422005-01-11 21:19:59 +00002509
Chris Lattner30607ec2005-01-25 20:03:11 +00002510 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattnera56d29d2005-01-17 06:26:58 +00002511 FoldAdd:
Chris Lattner62b22422005-01-11 21:19:59 +00002512 switch (N.getValueType()) {
2513 default: assert(0 && "Cannot add this type!");
2514 case MVT::i1:
2515 case MVT::i8: Opc = X86::ADD8rm; break;
2516 case MVT::i16: Opc = X86::ADD16rm; break;
2517 case MVT::i32: Opc = X86::ADD32rm; break;
Nate Begeman8a093362005-07-06 18:59:04 +00002518 case MVT::f32: Opc = X86::ADDSSrm; break;
Chris Lattner30607ec2005-01-25 20:03:11 +00002519 case MVT::f64:
2520 // For F64, handle promoted load operations (from F32) as well!
Nate Begeman8a093362005-07-06 18:59:04 +00002521 if (X86ScalarSSE) {
2522 assert(Op1.getOpcode() == ISD::LOAD && "SSE load not promoted");
2523 Opc = X86::ADDSDrm;
2524 } else {
2525 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
2526 }
Chris Lattner30607ec2005-01-25 20:03:11 +00002527 break;
Chris Lattner62b22422005-01-11 21:19:59 +00002528 }
2529 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00002530 EmitFoldedLoad(Op1, AM);
2531 Tmp1 = SelectExpr(Op0);
Chris Lattner62b22422005-01-11 21:19:59 +00002532 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2533 return Result;
2534 }
2535
Chris Lattner88c8a232005-01-07 07:49:41 +00002536 // See if we can codegen this as an LEA to fold operations together.
2537 if (N.getValueType() == MVT::i32) {
Chris Lattnerd7f93952005-01-18 02:25:52 +00002538 ExprMap.erase(N);
Chris Lattnera7acdda2005-01-18 01:06:26 +00002539 X86ISelAddressMode AM;
Chris Lattnerd7f93952005-01-18 02:25:52 +00002540 MatchAddress(N, AM);
2541 ExprMap[N] = Result;
2542
2543 // If this is not just an add, emit the LEA. For a simple add (like
2544 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
2545 // leave this as LEA, then peephole it to 'ADD' after two address elim
2546 // happens.
2547 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
2548 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
2549 X86AddressMode XAM = SelectAddrExprs(AM);
2550 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
2551 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002552 }
2553 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002554
Chris Lattner62b22422005-01-11 21:19:59 +00002555 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner88c8a232005-01-07 07:49:41 +00002556 Opc = 0;
2557 if (CN->getValue() == 1) { // add X, 1 -> inc X
2558 switch (N.getValueType()) {
2559 default: assert(0 && "Cannot integer add this type!");
2560 case MVT::i8: Opc = X86::INC8r; break;
2561 case MVT::i16: Opc = X86::INC16r; break;
2562 case MVT::i32: Opc = X86::INC32r; break;
2563 }
2564 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
2565 switch (N.getValueType()) {
2566 default: assert(0 && "Cannot integer add this type!");
2567 case MVT::i8: Opc = X86::DEC8r; break;
2568 case MVT::i16: Opc = X86::DEC16r; break;
2569 case MVT::i32: Opc = X86::DEC32r; break;
2570 }
2571 }
2572
2573 if (Opc) {
Chris Lattner62b22422005-01-11 21:19:59 +00002574 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00002575 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2576 return Result;
2577 }
2578
2579 switch (N.getValueType()) {
2580 default: assert(0 && "Cannot add this type!");
2581 case MVT::i8: Opc = X86::ADD8ri; break;
2582 case MVT::i16: Opc = X86::ADD16ri; break;
2583 case MVT::i32: Opc = X86::ADD32ri; break;
2584 }
2585 if (Opc) {
Chris Lattner62b22422005-01-11 21:19:59 +00002586 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00002587 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2588 return Result;
2589 }
2590 }
2591
Chris Lattner88c8a232005-01-07 07:49:41 +00002592 switch (N.getValueType()) {
2593 default: assert(0 && "Cannot add this type!");
2594 case MVT::i8: Opc = X86::ADD8rr; break;
2595 case MVT::i16: Opc = X86::ADD16rr; break;
2596 case MVT::i32: Opc = X86::ADD32rr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00002597 case MVT::f32: Opc = X86::ADDSSrr; break;
2598 case MVT::f64: Opc = X86ScalarSSE ? X86::ADDSDrr : X86::FpADD; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002599 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002600
Chris Lattner62b22422005-01-11 21:19:59 +00002601 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2602 Tmp1 = SelectExpr(Op0);
2603 Tmp2 = SelectExpr(Op1);
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002604 } else {
Chris Lattner62b22422005-01-11 21:19:59 +00002605 Tmp2 = SelectExpr(Op1);
2606 Tmp1 = SelectExpr(Op0);
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002607 }
2608
Chris Lattner88c8a232005-01-07 07:49:41 +00002609 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2610 return Result;
Chris Lattner0e0b5992005-04-02 05:30:17 +00002611
Nate Begeman8a093362005-07-06 18:59:04 +00002612 case ISD::FSQRT:
2613 Tmp1 = SelectExpr(Node->getOperand(0));
2614 if (X86ScalarSSE) {
2615 Opc = (N.getValueType() == MVT::f32) ? X86::SQRTSSrr : X86::SQRTSDrr;
2616 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2617 } else {
2618 BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1);
2619 }
2620 return Result;
2621
2622 // FIXME:
2623 // Once we can spill 16 byte constants into the constant pool, we can
2624 // implement SSE equivalents of FABS and FCHS.
Chris Lattner0e0b5992005-04-02 05:30:17 +00002625 case ISD::FABS:
Chris Lattner0e0b5992005-04-02 05:30:17 +00002626 case ISD::FNEG:
Chris Lattnerdb68d392005-04-30 04:25:35 +00002627 case ISD::FSIN:
2628 case ISD::FCOS:
Chris Lattner014d2c42005-04-28 22:07:18 +00002629 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattner0e0b5992005-04-02 05:30:17 +00002630 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner014d2c42005-04-28 22:07:18 +00002631 switch (N.getOpcode()) {
2632 default: assert(0 && "Unreachable!");
2633 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
2634 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
Chris Lattnerdb68d392005-04-30 04:25:35 +00002635 case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break;
2636 case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner014d2c42005-04-28 22:07:18 +00002637 }
Chris Lattner0e0b5992005-04-02 05:30:17 +00002638 return Result;
2639
Chris Lattner4fbb4af2005-04-06 04:21:07 +00002640 case ISD::MULHU:
2641 switch (N.getValueType()) {
2642 default: assert(0 && "Unsupported VT!");
2643 case MVT::i8: Tmp2 = X86::MUL8r; break;
2644 case MVT::i16: Tmp2 = X86::MUL16r; break;
2645 case MVT::i32: Tmp2 = X86::MUL32r; break;
2646 }
2647 // FALL THROUGH
2648 case ISD::MULHS: {
2649 unsigned MovOpc, LowReg, HiReg;
2650 switch (N.getValueType()) {
2651 default: assert(0 && "Unsupported VT!");
Misha Brukmanc88330a2005-04-21 23:38:14 +00002652 case MVT::i8:
Chris Lattner4fbb4af2005-04-06 04:21:07 +00002653 MovOpc = X86::MOV8rr;
2654 LowReg = X86::AL;
2655 HiReg = X86::AH;
2656 Opc = X86::IMUL8r;
2657 break;
2658 case MVT::i16:
2659 MovOpc = X86::MOV16rr;
2660 LowReg = X86::AX;
2661 HiReg = X86::DX;
2662 Opc = X86::IMUL16r;
2663 break;
2664 case MVT::i32:
2665 MovOpc = X86::MOV32rr;
2666 LowReg = X86::EAX;
2667 HiReg = X86::EDX;
2668 Opc = X86::IMUL32r;
2669 break;
2670 }
2671 if (Node->getOpcode() != ISD::MULHS)
2672 Opc = Tmp2; // Get the MULHU opcode.
2673
2674 Op0 = Node->getOperand(0);
2675 Op1 = Node->getOperand(1);
2676 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2677 Tmp1 = SelectExpr(Op0);
2678 Tmp2 = SelectExpr(Op1);
2679 } else {
2680 Tmp2 = SelectExpr(Op1);
2681 Tmp1 = SelectExpr(Op0);
2682 }
2683
2684 // FIXME: Implement folding of loads into the memory operands here!
2685 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
2686 BuildMI(BB, Opc, 1).addReg(Tmp2);
2687 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
2688 return Result;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002689 }
Chris Lattner4fbb4af2005-04-06 04:21:07 +00002690
Chris Lattner0815dcae2005-09-28 22:29:17 +00002691 case ISD::FSUB:
2692 case ISD::FMUL:
Chris Lattner88c8a232005-01-07 07:49:41 +00002693 case ISD::SUB:
Chris Lattner62b22422005-01-11 21:19:59 +00002694 case ISD::MUL:
2695 case ISD::AND:
2696 case ISD::OR:
Chris Lattnerefe90202005-01-12 04:23:22 +00002697 case ISD::XOR: {
Chris Lattner62b22422005-01-11 21:19:59 +00002698 static const unsigned SUBTab[] = {
2699 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
2700 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
2701 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
2702 };
Nate Begeman8a093362005-07-06 18:59:04 +00002703 static const unsigned SSE_SUBTab[] = {
2704 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
2705 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::SUBSSrm, X86::SUBSDrm,
2706 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::SUBSSrr, X86::SUBSDrr,
2707 };
Chris Lattner62b22422005-01-11 21:19:59 +00002708 static const unsigned MULTab[] = {
2709 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
2710 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
2711 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
2712 };
Nate Begeman8a093362005-07-06 18:59:04 +00002713 static const unsigned SSE_MULTab[] = {
2714 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
2715 0, X86::IMUL16rm , X86::IMUL32rm, X86::MULSSrm, X86::MULSDrm,
2716 0, X86::IMUL16rr , X86::IMUL32rr, X86::MULSSrr, X86::MULSDrr,
2717 };
Chris Lattner62b22422005-01-11 21:19:59 +00002718 static const unsigned ANDTab[] = {
2719 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
2720 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukmanc88330a2005-04-21 23:38:14 +00002721 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattner62b22422005-01-11 21:19:59 +00002722 };
2723 static const unsigned ORTab[] = {
2724 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
2725 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
2726 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
2727 };
2728 static const unsigned XORTab[] = {
2729 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
2730 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
2731 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
2732 };
2733
2734 Op0 = Node->getOperand(0);
2735 Op1 = Node->getOperand(1);
2736
Chris Lattner29f58192005-01-19 07:37:26 +00002737 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
2738 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner41fe2012005-01-19 06:18:43 +00002739 return Result;
2740
2741 if (Node->getOpcode() == ISD::SUB)
Chris Lattner88c8a232005-01-07 07:49:41 +00002742 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
2743 if (CN->isNullValue()) { // 0 - N -> neg N
2744 switch (N.getValueType()) {
2745 default: assert(0 && "Cannot sub this type!");
2746 case MVT::i1:
2747 case MVT::i8: Opc = X86::NEG8r; break;
2748 case MVT::i16: Opc = X86::NEG16r; break;
2749 case MVT::i32: Opc = X86::NEG32r; break;
2750 }
2751 Tmp1 = SelectExpr(N.getOperand(1));
2752 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2753 return Result;
2754 }
2755
Chris Lattner62b22422005-01-11 21:19:59 +00002756 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2757 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00002758 Opc = 0;
Chris Lattner9d7cf992005-01-11 04:31:30 +00002759 switch (N.getValueType()) {
2760 default: assert(0 && "Cannot add this type!");
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00002761 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattner9d7cf992005-01-11 04:31:30 +00002762 case MVT::i8: Opc = X86::NOT8r; break;
2763 case MVT::i16: Opc = X86::NOT16r; break;
2764 case MVT::i32: Opc = X86::NOT32r; break;
2765 }
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00002766 if (Opc) {
2767 Tmp1 = SelectExpr(Op0);
2768 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2769 return Result;
2770 }
Chris Lattner9d7cf992005-01-11 04:31:30 +00002771 }
2772
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00002773 // Fold common multiplies into LEA instructions.
2774 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
2775 switch ((int)CN->getValue()) {
2776 default: break;
2777 case 3:
2778 case 5:
2779 case 9:
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00002780 // Remove N from exprmap so SelectAddress doesn't get confused.
2781 ExprMap.erase(N);
Chris Lattnera7acdda2005-01-18 01:06:26 +00002782 X86AddressMode AM;
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00002783 SelectAddress(N, AM);
2784 // Restore it to the map.
2785 ExprMap[N] = Result;
2786 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
2787 return Result;
2788 }
2789 }
2790
Chris Lattner88c8a232005-01-07 07:49:41 +00002791 switch (N.getValueType()) {
Chris Lattner9d7cf992005-01-11 04:31:30 +00002792 default: assert(0 && "Cannot xor this type!");
Chris Lattner88c8a232005-01-07 07:49:41 +00002793 case MVT::i1:
Chris Lattner62b22422005-01-11 21:19:59 +00002794 case MVT::i8: Opc = 0; break;
2795 case MVT::i16: Opc = 1; break;
2796 case MVT::i32: Opc = 2; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002797 }
Chris Lattner62b22422005-01-11 21:19:59 +00002798 switch (Node->getOpcode()) {
2799 default: assert(0 && "Unreachable!");
Chris Lattner0815dcae2005-09-28 22:29:17 +00002800 case ISD::FSUB:
Nate Begeman8a093362005-07-06 18:59:04 +00002801 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
Chris Lattner0815dcae2005-09-28 22:29:17 +00002802 case ISD::FMUL:
Nate Begeman8a093362005-07-06 18:59:04 +00002803 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00002804 case ISD::AND: Opc = ANDTab[Opc]; break;
2805 case ISD::OR: Opc = ORTab[Opc]; break;
2806 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002807 }
Chris Lattner62b22422005-01-11 21:19:59 +00002808 if (Opc) { // Can't fold MUL:i8 R, imm
2809 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00002810 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2811 return Result;
2812 }
2813 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002814
Chris Lattner30607ec2005-01-25 20:03:11 +00002815 if (isFoldableLoad(Op0, Op1, true))
Chris Lattner0815dcae2005-09-28 22:29:17 +00002816 if (Node->getOpcode() != ISD::SUB && Node->getOpcode() != ISD::FSUB) {
Chris Lattner62b22422005-01-11 21:19:59 +00002817 std::swap(Op0, Op1);
Chris Lattnera56d29d2005-01-17 06:26:58 +00002818 goto FoldOps;
Chris Lattner62b22422005-01-11 21:19:59 +00002819 } else {
Chris Lattner30607ec2005-01-25 20:03:11 +00002820 // For FP, emit 'reverse' subract, with a memory operand.
Nate Begeman8a093362005-07-06 18:59:04 +00002821 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner30607ec2005-01-25 20:03:11 +00002822 if (Op0.getOpcode() == ISD::EXTLOAD)
2823 Opc = X86::FSUBR32m;
2824 else
2825 Opc = X86::FSUBR64m;
2826
Chris Lattner62b22422005-01-11 21:19:59 +00002827 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00002828 EmitFoldedLoad(Op0, AM);
2829 Tmp1 = SelectExpr(Op1);
Chris Lattner62b22422005-01-11 21:19:59 +00002830 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2831 return Result;
2832 }
2833 }
2834
Chris Lattner30607ec2005-01-25 20:03:11 +00002835 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattnera56d29d2005-01-17 06:26:58 +00002836 FoldOps:
Chris Lattner62b22422005-01-11 21:19:59 +00002837 switch (N.getValueType()) {
2838 default: assert(0 && "Cannot operate on this type!");
2839 case MVT::i1:
2840 case MVT::i8: Opc = 5; break;
2841 case MVT::i16: Opc = 6; break;
2842 case MVT::i32: Opc = 7; break;
Nate Begeman8a093362005-07-06 18:59:04 +00002843 case MVT::f32: Opc = 8; break;
Chris Lattner30607ec2005-01-25 20:03:11 +00002844 // For F64, handle promoted load operations (from F32) as well!
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002845 case MVT::f64:
2846 assert((!X86ScalarSSE || Op1.getOpcode() == ISD::LOAD) &&
Nate Begeman8a093362005-07-06 18:59:04 +00002847 "SSE load should have been promoted");
2848 Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattner62b22422005-01-11 21:19:59 +00002849 }
2850 switch (Node->getOpcode()) {
2851 default: assert(0 && "Unreachable!");
Chris Lattner0815dcae2005-09-28 22:29:17 +00002852 case ISD::FSUB:
Nate Begeman8a093362005-07-06 18:59:04 +00002853 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
Chris Lattner0815dcae2005-09-28 22:29:17 +00002854 case ISD::FMUL:
Nate Begeman8a093362005-07-06 18:59:04 +00002855 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00002856 case ISD::AND: Opc = ANDTab[Opc]; break;
2857 case ISD::OR: Opc = ORTab[Opc]; break;
2858 case ISD::XOR: Opc = XORTab[Opc]; break;
2859 }
2860
2861 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00002862 EmitFoldedLoad(Op1, AM);
2863 Tmp1 = SelectExpr(Op0);
Chris Lattner62b22422005-01-11 21:19:59 +00002864 if (Opc) {
2865 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2866 } else {
2867 assert(Node->getOpcode() == ISD::MUL &&
2868 N.getValueType() == MVT::i8 && "Unexpected situation!");
2869 // Must use the MUL instruction, which forces use of AL.
2870 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2871 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2872 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2873 }
2874 return Result;
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002875 }
Chris Lattner62b22422005-01-11 21:19:59 +00002876
2877 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2878 Tmp1 = SelectExpr(Op0);
2879 Tmp2 = SelectExpr(Op1);
2880 } else {
2881 Tmp2 = SelectExpr(Op1);
2882 Tmp1 = SelectExpr(Op0);
2883 }
2884
Chris Lattner88c8a232005-01-07 07:49:41 +00002885 switch (N.getValueType()) {
2886 default: assert(0 && "Cannot add this type!");
Chris Lattner62b22422005-01-11 21:19:59 +00002887 case MVT::i1:
2888 case MVT::i8: Opc = 10; break;
2889 case MVT::i16: Opc = 11; break;
2890 case MVT::i32: Opc = 12; break;
2891 case MVT::f32: Opc = 13; break;
2892 case MVT::f64: Opc = 14; break;
2893 }
2894 switch (Node->getOpcode()) {
2895 default: assert(0 && "Unreachable!");
Chris Lattner0815dcae2005-09-28 22:29:17 +00002896 case ISD::FSUB:
Nate Begeman8a093362005-07-06 18:59:04 +00002897 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
Chris Lattner0815dcae2005-09-28 22:29:17 +00002898 case ISD::FMUL:
Nate Begeman8a093362005-07-06 18:59:04 +00002899 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattner62b22422005-01-11 21:19:59 +00002900 case ISD::AND: Opc = ANDTab[Opc]; break;
2901 case ISD::OR: Opc = ORTab[Opc]; break;
2902 case ISD::XOR: Opc = XORTab[Opc]; break;
2903 }
2904 if (Opc) {
2905 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2906 } else {
2907 assert(Node->getOpcode() == ISD::MUL &&
2908 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattner750d38b2005-01-10 20:55:48 +00002909 // Must use the MUL instruction, which forces use of AL.
2910 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2911 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2912 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner88c8a232005-01-07 07:49:41 +00002913 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002914 return Result;
Chris Lattnerefe90202005-01-12 04:23:22 +00002915 }
Chris Lattner2a631fa2005-01-20 18:53:00 +00002916 case ISD::ADD_PARTS:
2917 case ISD::SUB_PARTS: {
2918 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2919 "Not an i64 add/sub!");
2920 // Emit all of the operands.
2921 std::vector<unsigned> InVals;
2922 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2923 InVals.push_back(SelectExpr(N.getOperand(i)));
2924 if (N.getOpcode() == ISD::ADD_PARTS) {
2925 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2926 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2927 } else {
2928 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2929 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2930 }
2931 return Result+N.ResNo;
2932 }
2933
Chris Lattnera31d4c72005-04-02 04:01:14 +00002934 case ISD::SHL_PARTS:
2935 case ISD::SRA_PARTS:
2936 case ISD::SRL_PARTS: {
2937 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2938 "Not an i64 shift!");
2939 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2940 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2941 unsigned TmpReg = MakeReg(MVT::i32);
2942 if (N.getOpcode() == ISD::SRA_PARTS) {
2943 // If this is a SHR of a Long, then we need to do funny sign extension
2944 // stuff. TmpReg gets the value to use as the high-part if we are
2945 // shifting more than 32 bits.
2946 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2947 } else {
2948 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2949 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2950 }
2951
2952 // Initialize CL with the shift amount.
2953 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2954 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2955
2956 unsigned TmpReg2 = MakeReg(MVT::i32);
2957 unsigned TmpReg3 = MakeReg(MVT::i32);
2958 if (N.getOpcode() == ISD::SHL_PARTS) {
2959 // TmpReg2 = shld inHi, inLo
2960 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2961 .addReg(ShiftOpLo);
2962 // TmpReg3 = shl inLo, CL
2963 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002964
Chris Lattnera31d4c72005-04-02 04:01:14 +00002965 // Set the flags to indicate whether the shift was by more than 32 bits.
2966 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002967
Chris Lattnera31d4c72005-04-02 04:01:14 +00002968 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002969 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00002970 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2971 // DestLo = (>32) ? TmpReg : TmpReg3;
2972 BuildMI(BB, X86::CMOVNE32rr, 2,
2973 Result).addReg(TmpReg3).addReg(TmpReg);
2974 } else {
2975 // TmpReg2 = shrd inLo, inHi
2976 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
2977 .addReg(ShiftOpHi);
2978 // TmpReg3 = s[ah]r inHi, CL
Misha Brukmanc88330a2005-04-21 23:38:14 +00002979 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnera31d4c72005-04-02 04:01:14 +00002980 : X86::SHR32rCL, 1, TmpReg3)
2981 .addReg(ShiftOpHi);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002982
Chris Lattnera31d4c72005-04-02 04:01:14 +00002983 // Set the flags to indicate whether the shift was by more than 32 bits.
2984 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002985
Chris Lattnera31d4c72005-04-02 04:01:14 +00002986 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002987 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00002988 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002989
Chris Lattnera31d4c72005-04-02 04:01:14 +00002990 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002991 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00002992 Result+1).addReg(TmpReg3).addReg(TmpReg);
2993 }
2994 return Result+N.ResNo;
2995 }
2996
Chris Lattner88c8a232005-01-07 07:49:41 +00002997 case ISD::SELECT:
Nate Begeman8d394eb2005-08-03 23:26:28 +00002998 EmitSelectCC(N.getOperand(0), N.getOperand(1), N.getOperand(2),
2999 N.getValueType(), Result);
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003000 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00003001
Chris Lattner0815dcae2005-09-28 22:29:17 +00003002 case ISD::FDIV:
3003 case ISD::FREM:
Chris Lattner88c8a232005-01-07 07:49:41 +00003004 case ISD::SDIV:
3005 case ISD::UDIV:
3006 case ISD::SREM:
3007 case ISD::UREM: {
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003008 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
3009 "We don't support this operator!");
3010
Chris Lattner0815dcae2005-09-28 22:29:17 +00003011 if (N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::FDIV) {
Chris Lattner1b206152005-01-25 20:35:10 +00003012 // We can fold loads into FpDIVs, but not really into any others.
Nate Begemanfcd2f762005-07-07 06:32:01 +00003013 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner1b206152005-01-25 20:35:10 +00003014 // Check for reversed and unreversed DIV.
3015 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
3016 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
3017 Opc = X86::FDIVR32m;
3018 else
3019 Opc = X86::FDIVR64m;
3020 X86AddressMode AM;
3021 EmitFoldedLoad(N.getOperand(0), AM);
3022 Tmp1 = SelectExpr(N.getOperand(1));
3023 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
3024 return Result;
3025 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
3026 N.getOperand(1).getOpcode() == ISD::LOAD) {
3027 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
3028 Opc = X86::FDIV32m;
3029 else
3030 Opc = X86::FDIV64m;
3031 X86AddressMode AM;
3032 EmitFoldedLoad(N.getOperand(1), AM);
3033 Tmp1 = SelectExpr(N.getOperand(0));
3034 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
3035 return Result;
3036 }
3037 }
Chris Lattner60c23bd2005-04-13 03:29:53 +00003038 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003039
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003040 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3041 Tmp1 = SelectExpr(N.getOperand(0));
3042 Tmp2 = SelectExpr(N.getOperand(1));
3043 } else {
3044 Tmp2 = SelectExpr(N.getOperand(1));
3045 Tmp1 = SelectExpr(N.getOperand(0));
3046 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003047
3048 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
3049 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
3050 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
3051 switch (N.getValueType()) {
3052 default: assert(0 && "Cannot sdiv this type!");
3053 case MVT::i8:
3054 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
3055 LoReg = X86::AL;
3056 HiReg = X86::AH;
3057 MovOpcode = X86::MOV8rr;
3058 ClrOpcode = X86::MOV8ri;
3059 SExtOpcode = X86::CBW;
3060 break;
3061 case MVT::i16:
3062 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
3063 LoReg = X86::AX;
3064 HiReg = X86::DX;
3065 MovOpcode = X86::MOV16rr;
3066 ClrOpcode = X86::MOV16ri;
3067 SExtOpcode = X86::CWD;
3068 break;
3069 case MVT::i32:
3070 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner3278ce82005-01-12 03:16:09 +00003071 LoReg = X86::EAX;
Chris Lattner88c8a232005-01-07 07:49:41 +00003072 HiReg = X86::EDX;
3073 MovOpcode = X86::MOV32rr;
3074 ClrOpcode = X86::MOV32ri;
3075 SExtOpcode = X86::CDQ;
3076 break;
Nate Begeman8a093362005-07-06 18:59:04 +00003077 case MVT::f32:
3078 BuildMI(BB, X86::DIVSSrr, 2, Result).addReg(Tmp1).addReg(Tmp2);
3079 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00003080 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00003081 Opc = X86ScalarSSE ? X86::DIVSDrr : X86::FpDIV;
3082 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00003083 return Result;
3084 }
3085
3086 // Set up the low part.
3087 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
3088
3089 if (isSigned) {
3090 // Sign extend the low part into the high part.
3091 BuildMI(BB, SExtOpcode, 0);
3092 } else {
3093 // Zero out the high part, effectively zero extending the input.
3094 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
3095 }
3096
3097 // Emit the DIV/IDIV instruction.
Misha Brukmanc88330a2005-04-21 23:38:14 +00003098 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00003099
3100 // Get the result of the divide or rem.
3101 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
3102 return Result;
3103 }
3104
3105 case ISD::SHL:
Chris Lattner88c8a232005-01-07 07:49:41 +00003106 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner62b22422005-01-11 21:19:59 +00003107 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
3108 switch (N.getValueType()) {
3109 default: assert(0 && "Cannot shift this type!");
3110 case MVT::i8: Opc = X86::ADD8rr; break;
3111 case MVT::i16: Opc = X86::ADD16rr; break;
3112 case MVT::i32: Opc = X86::ADD32rr; break;
3113 }
3114 Tmp1 = SelectExpr(N.getOperand(0));
3115 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
3116 return Result;
3117 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003118
Chris Lattner88c8a232005-01-07 07:49:41 +00003119 switch (N.getValueType()) {
3120 default: assert(0 && "Cannot shift this type!");
3121 case MVT::i8: Opc = X86::SHL8ri; break;
3122 case MVT::i16: Opc = X86::SHL16ri; break;
3123 case MVT::i32: Opc = X86::SHL32ri; break;
3124 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003125 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003126 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3127 return Result;
3128 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003129
3130 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3131 Tmp1 = SelectExpr(N.getOperand(0));
3132 Tmp2 = SelectExpr(N.getOperand(1));
3133 } else {
3134 Tmp2 = SelectExpr(N.getOperand(1));
3135 Tmp1 = SelectExpr(N.getOperand(0));
3136 }
3137
Chris Lattner88c8a232005-01-07 07:49:41 +00003138 switch (N.getValueType()) {
3139 default: assert(0 && "Cannot shift this type!");
3140 case MVT::i8 : Opc = X86::SHL8rCL; break;
3141 case MVT::i16: Opc = X86::SHL16rCL; break;
3142 case MVT::i32: Opc = X86::SHL32rCL; break;
3143 }
3144 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
Chris Lattner14569592005-08-19 00:16:17 +00003145 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00003146 return Result;
3147 case ISD::SRL:
Chris Lattner88c8a232005-01-07 07:49:41 +00003148 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3149 switch (N.getValueType()) {
3150 default: assert(0 && "Cannot shift this type!");
3151 case MVT::i8: Opc = X86::SHR8ri; break;
3152 case MVT::i16: Opc = X86::SHR16ri; break;
3153 case MVT::i32: Opc = X86::SHR32ri; break;
3154 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003155 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003156 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3157 return Result;
3158 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003159
3160 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3161 Tmp1 = SelectExpr(N.getOperand(0));
3162 Tmp2 = SelectExpr(N.getOperand(1));
3163 } else {
3164 Tmp2 = SelectExpr(N.getOperand(1));
3165 Tmp1 = SelectExpr(N.getOperand(0));
3166 }
3167
Chris Lattner88c8a232005-01-07 07:49:41 +00003168 switch (N.getValueType()) {
3169 default: assert(0 && "Cannot shift this type!");
3170 case MVT::i8 : Opc = X86::SHR8rCL; break;
3171 case MVT::i16: Opc = X86::SHR16rCL; break;
3172 case MVT::i32: Opc = X86::SHR32rCL; break;
3173 }
3174 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
Chris Lattner14569592005-08-19 00:16:17 +00003175 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00003176 return Result;
3177 case ISD::SRA:
Chris Lattner88c8a232005-01-07 07:49:41 +00003178 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3179 switch (N.getValueType()) {
3180 default: assert(0 && "Cannot shift this type!");
3181 case MVT::i8: Opc = X86::SAR8ri; break;
3182 case MVT::i16: Opc = X86::SAR16ri; break;
3183 case MVT::i32: Opc = X86::SAR32ri; break;
3184 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003185 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003186 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3187 return Result;
3188 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003189
3190 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3191 Tmp1 = SelectExpr(N.getOperand(0));
3192 Tmp2 = SelectExpr(N.getOperand(1));
3193 } else {
3194 Tmp2 = SelectExpr(N.getOperand(1));
3195 Tmp1 = SelectExpr(N.getOperand(0));
3196 }
3197
Chris Lattner88c8a232005-01-07 07:49:41 +00003198 switch (N.getValueType()) {
3199 default: assert(0 && "Cannot shift this type!");
3200 case MVT::i8 : Opc = X86::SAR8rCL; break;
3201 case MVT::i16: Opc = X86::SAR16rCL; break;
3202 case MVT::i32: Opc = X86::SAR32rCL; break;
3203 }
3204 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
Chris Lattnera9d68f12005-08-19 00:31:37 +00003205 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00003206 return Result;
3207
3208 case ISD::SETCC:
Chris Lattner3be6cd52005-01-17 01:34:14 +00003209 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner6ec77452005-08-09 20:21:10 +00003210 EmitSetCC(BB, Result, cast<CondCodeSDNode>(N.getOperand(2))->get(),
Chris Lattner88c8a232005-01-07 07:49:41 +00003211 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
3212 return Result;
Chris Lattnere18a4c42005-01-15 05:22:24 +00003213 case ISD::LOAD:
Chris Lattner88c8a232005-01-07 07:49:41 +00003214 // Make sure we generate both values.
Chris Lattner78d30282005-01-18 03:51:59 +00003215 if (Result != 1) { // Generate the token
3216 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3217 assert(0 && "Load already emitted!?");
3218 } else
Chris Lattner88c8a232005-01-07 07:49:41 +00003219 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3220
Chris Lattnerb52e0412005-01-08 19:28:19 +00003221 switch (Node->getValueType(0)) {
Chris Lattner88c8a232005-01-07 07:49:41 +00003222 default: assert(0 && "Cannot load this type!");
3223 case MVT::i1:
3224 case MVT::i8: Opc = X86::MOV8rm; break;
3225 case MVT::i16: Opc = X86::MOV16rm; break;
3226 case MVT::i32: Opc = X86::MOV32rm; break;
Nate Begeman8a093362005-07-06 18:59:04 +00003227 case MVT::f32: Opc = X86::MOVSSrm; break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003228 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00003229 if (X86ScalarSSE) {
3230 Opc = X86::MOVSDrm;
3231 } else {
3232 Opc = X86::FLD64m;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003233 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00003234 }
3235 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003236 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003237
Chris Lattner88c8a232005-01-07 07:49:41 +00003238 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattnerc30405e2005-08-26 17:15:30 +00003239 unsigned CPIdx = BB->getParent()->getConstantPool()->
3240 getConstantPoolIndex(CP->get());
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003241 Select(N.getOperand(0));
Chris Lattnerc30405e2005-08-26 17:15:30 +00003242 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CPIdx);
Chris Lattner88c8a232005-01-07 07:49:41 +00003243 } else {
3244 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00003245
3246 SDOperand Chain = N.getOperand(0);
3247 SDOperand Address = N.getOperand(1);
3248 if (getRegPressure(Chain) > getRegPressure(Address)) {
3249 Select(Chain);
3250 SelectAddress(Address, AM);
3251 } else {
3252 SelectAddress(Address, AM);
3253 Select(Chain);
3254 }
3255
Chris Lattner88c8a232005-01-07 07:49:41 +00003256 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
3257 }
3258 return Result;
Chris Lattnera36117b2005-05-14 06:52:07 +00003259 case X86ISD::FILD64m:
3260 // Make sure we generate both values.
3261 assert(Result != 1 && N.getValueType() == MVT::f64);
3262 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3263 assert(0 && "Load already emitted!?");
3264
3265 {
3266 X86AddressMode AM;
3267
3268 SDOperand Chain = N.getOperand(0);
3269 SDOperand Address = N.getOperand(1);
3270 if (getRegPressure(Chain) > getRegPressure(Address)) {
3271 Select(Chain);
3272 SelectAddress(Address, AM);
3273 } else {
3274 SelectAddress(Address, AM);
3275 Select(Chain);
3276 }
Chris Lattner67756e22005-07-29 00:40:01 +00003277
3278 addFullAddress(BuildMI(BB, X86::FILD64m, 4, Result), AM);
Chris Lattnera36117b2005-05-14 06:52:07 +00003279 }
3280 return Result;
Jeff Cohen546fd592005-07-30 18:33:25 +00003281
Chris Lattnere18a4c42005-01-15 05:22:24 +00003282 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
3283 case ISD::ZEXTLOAD: {
3284 // Make sure we generate both values.
3285 if (Result != 1)
3286 ExprMap[N.getValue(1)] = 1; // Generate the token
3287 else
3288 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3289
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003290 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
3291 if (Node->getValueType(0) == MVT::f64) {
Chris Lattner53676df2005-07-10 01:56:13 +00003292 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003293 "Bad EXTLOAD!");
Chris Lattnerc30405e2005-08-26 17:15:30 +00003294 unsigned CPIdx = BB->getParent()->getConstantPool()->
Chris Lattnerd0dc6f42005-08-26 17:18:44 +00003295 getConstantPoolIndex(CP->get());
Chris Lattnerc30405e2005-08-26 17:15:30 +00003296
3297 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result), CPIdx);
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003298 return Result;
3299 }
3300
Chris Lattnere18a4c42005-01-15 05:22:24 +00003301 X86AddressMode AM;
3302 if (getRegPressure(Node->getOperand(0)) >
3303 getRegPressure(Node->getOperand(1))) {
3304 Select(Node->getOperand(0)); // chain
3305 SelectAddress(Node->getOperand(1), AM);
3306 } else {
3307 SelectAddress(Node->getOperand(1), AM);
3308 Select(Node->getOperand(0)); // chain
3309 }
3310
3311 switch (Node->getValueType(0)) {
3312 default: assert(0 && "Unknown type to sign extend to.");
3313 case MVT::f64:
Chris Lattner53676df2005-07-10 01:56:13 +00003314 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00003315 "Bad EXTLOAD!");
3316 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
3317 break;
3318 case MVT::i32:
Chris Lattner53676df2005-07-10 01:56:13 +00003319 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere18a4c42005-01-15 05:22:24 +00003320 default:
3321 assert(0 && "Bad zero extend!");
3322 case MVT::i1:
3323 case MVT::i8:
3324 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
3325 break;
3326 case MVT::i16:
3327 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
3328 break;
3329 }
3330 break;
3331 case MVT::i16:
Chris Lattner53676df2005-07-10 01:56:13 +00003332 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() <= MVT::i8 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00003333 "Bad zero extend!");
3334 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3335 break;
3336 case MVT::i8:
Chris Lattner53676df2005-07-10 01:56:13 +00003337 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i1 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00003338 "Bad zero extend!");
3339 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
3340 break;
3341 }
3342 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00003343 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00003344 case ISD::SEXTLOAD: {
3345 // Make sure we generate both values.
3346 if (Result != 1)
3347 ExprMap[N.getValue(1)] = 1; // Generate the token
3348 else
3349 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3350
3351 X86AddressMode AM;
3352 if (getRegPressure(Node->getOperand(0)) >
3353 getRegPressure(Node->getOperand(1))) {
3354 Select(Node->getOperand(0)); // chain
3355 SelectAddress(Node->getOperand(1), AM);
3356 } else {
3357 SelectAddress(Node->getOperand(1), AM);
3358 Select(Node->getOperand(0)); // chain
3359 }
3360
3361 switch (Node->getValueType(0)) {
3362 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
3363 default: assert(0 && "Unknown type to sign extend to.");
3364 case MVT::i32:
Chris Lattner53676df2005-07-10 01:56:13 +00003365 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere18a4c42005-01-15 05:22:24 +00003366 default:
3367 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
3368 case MVT::i8:
3369 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
3370 break;
3371 case MVT::i16:
3372 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
3373 break;
3374 }
3375 break;
3376 case MVT::i16:
Chris Lattner53676df2005-07-10 01:56:13 +00003377 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i8 &&
Chris Lattnere18a4c42005-01-15 05:22:24 +00003378 "Cannot sign extend from bool!");
3379 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3380 break;
3381 }
3382 return Result;
3383 }
3384
Chris Lattner88c8a232005-01-07 07:49:41 +00003385 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner88c8a232005-01-07 07:49:41 +00003386 // Generate both result values.
3387 if (Result != 1)
3388 ExprMap[N.getValue(1)] = 1; // Generate the token
3389 else
3390 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3391
3392 // FIXME: We are currently ignoring the requested alignment for handling
3393 // greater than the stack alignment. This will need to be revisited at some
3394 // point. Align = N.getOperand(2);
3395
3396 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
3397 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
3398 std::cerr << "Cannot allocate stack object with greater alignment than"
3399 << " the stack alignment yet!";
3400 abort();
3401 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003402
Chris Lattner88c8a232005-01-07 07:49:41 +00003403 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003404 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003405 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
3406 .addImm(CN->getValue());
3407 } else {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003408 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3409 Select(N.getOperand(0));
3410 Tmp1 = SelectExpr(N.getOperand(1));
3411 } else {
3412 Tmp1 = SelectExpr(N.getOperand(1));
3413 Select(N.getOperand(0));
3414 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003415
3416 // Subtract size from stack pointer, thereby allocating some space.
3417 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
3418 }
3419
3420 // Put a pointer to the space into the result register, by copying the stack
3421 // pointer.
3422 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
3423 return Result;
3424
Chris Lattner1b3520c2005-05-14 08:48:15 +00003425 case X86ISD::TAILCALL:
3426 case X86ISD::CALL: {
Chris Lattnerb52e0412005-01-08 19:28:19 +00003427 // The chain for this call is now lowered.
Chris Lattner1b3520c2005-05-14 08:48:15 +00003428 ExprMap.insert(std::make_pair(N.getValue(0), 1));
Chris Lattnerb52e0412005-01-08 19:28:19 +00003429
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003430 bool isDirect = isa<GlobalAddressSDNode>(N.getOperand(1)) ||
3431 isa<ExternalSymbolSDNode>(N.getOperand(1));
3432 unsigned Callee = 0;
3433 if (isDirect) {
3434 Select(N.getOperand(0));
3435 } else {
3436 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3437 Select(N.getOperand(0));
3438 Callee = SelectExpr(N.getOperand(1));
3439 } else {
3440 Callee = SelectExpr(N.getOperand(1));
3441 Select(N.getOperand(0));
3442 }
3443 }
3444
3445 // If this call has values to pass in registers, do so now.
Chris Lattner1b3520c2005-05-14 08:48:15 +00003446 if (Node->getNumOperands() > 4) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003447 // The first value is passed in (a part of) EAX, the second in EDX.
Chris Lattner1b3520c2005-05-14 08:48:15 +00003448 unsigned RegOp1 = SelectExpr(N.getOperand(4));
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003449 unsigned RegOp2 =
Chris Lattner1b3520c2005-05-14 08:48:15 +00003450 Node->getNumOperands() > 5 ? SelectExpr(N.getOperand(5)) : 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003451
Chris Lattner1b3520c2005-05-14 08:48:15 +00003452 switch (N.getOperand(4).getValueType()) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003453 default: assert(0 && "Bad thing to pass in regs");
3454 case MVT::i1:
3455 case MVT::i8: BuildMI(BB, X86::MOV8rr , 1,X86::AL).addReg(RegOp1); break;
3456 case MVT::i16: BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1); break;
3457 case MVT::i32: BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);break;
3458 }
3459 if (RegOp2)
Chris Lattner1b3520c2005-05-14 08:48:15 +00003460 switch (N.getOperand(5).getValueType()) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003461 default: assert(0 && "Bad thing to pass in regs");
3462 case MVT::i1:
3463 case MVT::i8:
3464 BuildMI(BB, X86::MOV8rr , 1, X86::DL).addReg(RegOp2);
3465 break;
3466 case MVT::i16:
3467 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
3468 break;
3469 case MVT::i32:
3470 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
3471 break;
3472 }
3473 }
3474
Chris Lattner88c8a232005-01-07 07:49:41 +00003475 if (GlobalAddressSDNode *GASD =
3476 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3477 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
3478 } else if (ExternalSymbolSDNode *ESSDN =
3479 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
3480 BuildMI(BB, X86::CALLpcrel32,
3481 1).addExternalSymbol(ESSDN->getSymbol(), true);
3482 } else {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003483 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3484 Select(N.getOperand(0));
3485 Tmp1 = SelectExpr(N.getOperand(1));
3486 } else {
3487 Tmp1 = SelectExpr(N.getOperand(1));
3488 Select(N.getOperand(0));
3489 }
3490
Chris Lattner88c8a232005-01-07 07:49:41 +00003491 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
3492 }
Chris Lattner1b3520c2005-05-14 08:48:15 +00003493
3494 // Get caller stack amount and amount the callee added to the stack pointer.
3495 Tmp1 = cast<ConstantSDNode>(N.getOperand(2))->getValue();
3496 Tmp2 = cast<ConstantSDNode>(N.getOperand(3))->getValue();
3497 BuildMI(BB, X86::ADJCALLSTACKUP, 2).addImm(Tmp1).addImm(Tmp2);
3498
3499 if (Node->getNumValues() != 1)
3500 switch (Node->getValueType(1)) {
3501 default: assert(0 && "Unknown value type for call result!");
3502 case MVT::Other: return 1;
3503 case MVT::i1:
3504 case MVT::i8:
3505 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3506 break;
3507 case MVT::i16:
3508 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3509 break;
3510 case MVT::i32:
3511 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3512 if (Node->getNumValues() == 3 && Node->getValueType(2) == MVT::i32)
3513 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
3514 break;
3515 case MVT::f64: // Floating-point return values live in %ST(0)
Nate Begeman8a093362005-07-06 18:59:04 +00003516 if (X86ScalarSSE) {
3517 ContainsFPCode = true;
3518 BuildMI(BB, X86::FpGETRESULT, 1, X86::FP0);
3519
3520 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
3521 MachineFunction *F = BB->getParent();
3522 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
3523 addFrameReference(BuildMI(BB, X86::FST64m, 5), FrameIdx).addReg(X86::FP0);
3524 addFrameReference(BuildMI(BB, X86::MOVSDrm, 4, Result), FrameIdx);
3525 break;
3526 } else {
3527 ContainsFPCode = true;
3528 BuildMI(BB, X86::FpGETRESULT, 1, Result);
3529 break;
3530 }
Chris Lattner1b3520c2005-05-14 08:48:15 +00003531 }
3532 return Result+N.ResNo-1;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003533 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00003534 case ISD::READPORT:
3535 // First, determine that the size of the operand falls within the acceptable
3536 // range for this architecture.
3537 //
3538 if (Node->getOperand(1).getValueType() != MVT::i16) {
3539 std::cerr << "llvm.readport: Address size is not 16 bits\n";
3540 exit(1);
3541 }
3542
3543 // Make sure we generate both values.
3544 if (Result != 1) { // Generate the token
3545 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3546 assert(0 && "readport already emitted!?");
3547 } else
3548 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003549
Chris Lattner70ea07c2005-05-09 21:17:38 +00003550 Select(Node->getOperand(0)); // Select the chain.
3551
3552 // If the port is a single-byte constant, use the immediate form.
3553 if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
3554 if ((Port->getValue() & 255) == Port->getValue()) {
3555 switch (Node->getValueType(0)) {
3556 case MVT::i8:
3557 BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue());
3558 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3559 return Result;
3560 case MVT::i16:
3561 BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue());
3562 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3563 return Result;
3564 case MVT::i32:
3565 BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue());
3566 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3567 return Result;
3568 default: break;
3569 }
3570 }
3571
3572 // Now, move the I/O port address into the DX register and use the IN
3573 // instruction to get the input data.
3574 //
3575 Tmp1 = SelectExpr(Node->getOperand(1));
3576 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1);
3577 switch (Node->getValueType(0)) {
3578 case MVT::i8:
3579 BuildMI(BB, X86::IN8rr, 0);
3580 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3581 return Result;
3582 case MVT::i16:
3583 BuildMI(BB, X86::IN16rr, 0);
3584 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3585 return Result;
3586 case MVT::i32:
3587 BuildMI(BB, X86::IN32rr, 0);
3588 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3589 return Result;
3590 default:
3591 std::cerr << "Cannot do input on this data type";
3592 exit(1);
3593 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003594
Chris Lattner88c8a232005-01-07 07:49:41 +00003595 }
3596
3597 return 0;
3598}
3599
Chris Lattner96113fd2005-01-17 19:25:26 +00003600/// TryToFoldLoadOpStore - Given a store node, try to fold together a
3601/// load/op/store instruction. If successful return true.
3602bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
3603 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
3604 SDOperand Chain = Node->getOperand(0);
3605 SDOperand StVal = Node->getOperand(1);
Chris Lattnere86c9332005-01-17 22:10:42 +00003606 SDOperand StPtr = Node->getOperand(2);
Chris Lattner96113fd2005-01-17 19:25:26 +00003607
3608 // The chain has to be a load, the stored value must be an integer binary
3609 // operation with one use.
Chris Lattnere86c9332005-01-17 22:10:42 +00003610 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattner96113fd2005-01-17 19:25:26 +00003611 MVT::isFloatingPoint(StVal.getValueType()))
3612 return false;
3613
Chris Lattnere86c9332005-01-17 22:10:42 +00003614 // Token chain must either be a factor node or the load to fold.
3615 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
3616 return false;
Chris Lattner96113fd2005-01-17 19:25:26 +00003617
Chris Lattnere86c9332005-01-17 22:10:42 +00003618 SDOperand TheLoad;
3619
3620 // Check to see if there is a load from the same pointer that we're storing
3621 // to in either operand of the binop.
3622 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
3623 StVal.getOperand(0).getOperand(1) == StPtr)
3624 TheLoad = StVal.getOperand(0);
3625 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
3626 StVal.getOperand(1).getOperand(1) == StPtr)
3627 TheLoad = StVal.getOperand(1);
3628 else
3629 return false; // No matching load operand.
3630
3631 // We can only fold the load if there are no intervening side-effecting
3632 // operations. This means that the store uses the load as its token chain, or
3633 // there are only token factor nodes in between the store and load.
3634 if (Chain != TheLoad.getValue(1)) {
3635 // Okay, the other option is that we have a store referring to (possibly
3636 // nested) token factor nodes. For now, just try peeking through one level
3637 // of token factors to see if this is the case.
3638 bool ChainOk = false;
3639 if (Chain.getOpcode() == ISD::TokenFactor) {
3640 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3641 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
3642 ChainOk = true;
3643 break;
3644 }
3645 }
3646
3647 if (!ChainOk) return false;
3648 }
3649
3650 if (TheLoad.getOperand(1) != StPtr)
Chris Lattner96113fd2005-01-17 19:25:26 +00003651 return false;
3652
3653 // Make sure that one of the operands of the binop is the load, and that the
3654 // load folds into the binop.
3655 if (((StVal.getOperand(0) != TheLoad ||
3656 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
3657 (StVal.getOperand(1) != TheLoad ||
3658 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
3659 return false;
3660
3661 // Finally, check to see if this is one of the ops we can handle!
3662 static const unsigned ADDTAB[] = {
3663 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
3664 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
3665 };
3666 static const unsigned SUBTAB[] = {
3667 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
3668 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
3669 };
3670 static const unsigned ANDTAB[] = {
3671 X86::AND8mi, X86::AND16mi, X86::AND32mi,
3672 X86::AND8mr, X86::AND16mr, X86::AND32mr,
3673 };
3674 static const unsigned ORTAB[] = {
3675 X86::OR8mi, X86::OR16mi, X86::OR32mi,
3676 X86::OR8mr, X86::OR16mr, X86::OR32mr,
3677 };
3678 static const unsigned XORTAB[] = {
3679 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
3680 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
3681 };
3682 static const unsigned SHLTAB[] = {
3683 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
3684 /*Have to put the reg in CL*/0, 0, 0,
3685 };
3686 static const unsigned SARTAB[] = {
3687 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
3688 /*Have to put the reg in CL*/0, 0, 0,
3689 };
3690 static const unsigned SHRTAB[] = {
3691 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
3692 /*Have to put the reg in CL*/0, 0, 0,
3693 };
Misha Brukmanc88330a2005-04-21 23:38:14 +00003694
Chris Lattner96113fd2005-01-17 19:25:26 +00003695 const unsigned *TabPtr = 0;
3696 switch (StVal.getOpcode()) {
3697 default:
3698 std::cerr << "CANNOT [mem] op= val: ";
3699 StVal.Val->dump(); std::cerr << "\n";
Chris Lattner0815dcae2005-09-28 22:29:17 +00003700 case ISD::FMUL:
Chris Lattner96113fd2005-01-17 19:25:26 +00003701 case ISD::MUL:
Chris Lattner0815dcae2005-09-28 22:29:17 +00003702 case ISD::FDIV:
Chris Lattner96113fd2005-01-17 19:25:26 +00003703 case ISD::SDIV:
3704 case ISD::UDIV:
Chris Lattner0815dcae2005-09-28 22:29:17 +00003705 case ISD::FREM:
Chris Lattner96113fd2005-01-17 19:25:26 +00003706 case ISD::SREM:
3707 case ISD::UREM: return false;
Misha Brukmanc88330a2005-04-21 23:38:14 +00003708
Chris Lattner96113fd2005-01-17 19:25:26 +00003709 case ISD::ADD: TabPtr = ADDTAB; break;
3710 case ISD::SUB: TabPtr = SUBTAB; break;
3711 case ISD::AND: TabPtr = ANDTAB; break;
3712 case ISD:: OR: TabPtr = ORTAB; break;
3713 case ISD::XOR: TabPtr = XORTAB; break;
3714 case ISD::SHL: TabPtr = SHLTAB; break;
3715 case ISD::SRA: TabPtr = SARTAB; break;
3716 case ISD::SRL: TabPtr = SHRTAB; break;
3717 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003718
Chris Lattner96113fd2005-01-17 19:25:26 +00003719 // Handle: [mem] op= CST
3720 SDOperand Op0 = StVal.getOperand(0);
3721 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0e1de102005-01-23 23:20:06 +00003722 unsigned Opc = 0;
Chris Lattner96113fd2005-01-17 19:25:26 +00003723 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
3724 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
3725 default: break;
3726 case MVT::i1:
3727 case MVT::i8: Opc = TabPtr[0]; break;
3728 case MVT::i16: Opc = TabPtr[1]; break;
3729 case MVT::i32: Opc = TabPtr[2]; break;
3730 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003731
Chris Lattner96113fd2005-01-17 19:25:26 +00003732 if (Opc) {
Chris Lattner78d30282005-01-18 03:51:59 +00003733 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3734 assert(0 && "Already emitted?");
Chris Lattnere86c9332005-01-17 22:10:42 +00003735 Select(Chain);
3736
Chris Lattner96113fd2005-01-17 19:25:26 +00003737 X86AddressMode AM;
3738 if (getRegPressure(TheLoad.getOperand(0)) >
3739 getRegPressure(TheLoad.getOperand(1))) {
3740 Select(TheLoad.getOperand(0));
3741 SelectAddress(TheLoad.getOperand(1), AM);
3742 } else {
3743 SelectAddress(TheLoad.getOperand(1), AM);
3744 Select(TheLoad.getOperand(0));
Misha Brukmanc88330a2005-04-21 23:38:14 +00003745 }
Chris Lattnere86c9332005-01-17 22:10:42 +00003746
3747 if (StVal.getOpcode() == ISD::ADD) {
3748 if (CN->getValue() == 1) {
3749 switch (Op0.getValueType()) {
3750 default: break;
3751 case MVT::i8:
3752 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
3753 return true;
3754 case MVT::i16: Opc = TabPtr[1];
3755 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
3756 return true;
3757 case MVT::i32: Opc = TabPtr[2];
3758 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
3759 return true;
3760 }
3761 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
3762 switch (Op0.getValueType()) {
3763 default: break;
3764 case MVT::i8:
3765 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
3766 return true;
3767 case MVT::i16: Opc = TabPtr[1];
3768 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
3769 return true;
3770 case MVT::i32: Opc = TabPtr[2];
3771 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
3772 return true;
3773 }
3774 }
3775 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003776
Chris Lattner96113fd2005-01-17 19:25:26 +00003777 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
3778 return true;
3779 }
3780 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003781
Chris Lattner96113fd2005-01-17 19:25:26 +00003782 // If we have [mem] = V op [mem], try to turn it into:
3783 // [mem] = [mem] op V.
Chris Lattner0815dcae2005-09-28 22:29:17 +00003784 if (Op1 == TheLoad &&
3785 StVal.getOpcode() != ISD::SUB && StVal.getOpcode() != ISD::FSUB &&
Chris Lattner96113fd2005-01-17 19:25:26 +00003786 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
3787 StVal.getOpcode() != ISD::SRL)
3788 std::swap(Op0, Op1);
Misha Brukmanc88330a2005-04-21 23:38:14 +00003789
Chris Lattner96113fd2005-01-17 19:25:26 +00003790 if (Op0 != TheLoad) return false;
3791
3792 switch (Op0.getValueType()) {
3793 default: return false;
3794 case MVT::i1:
3795 case MVT::i8: Opc = TabPtr[3]; break;
3796 case MVT::i16: Opc = TabPtr[4]; break;
3797 case MVT::i32: Opc = TabPtr[5]; break;
3798 }
Chris Lattnere86c9332005-01-17 22:10:42 +00003799
Chris Lattner479c7112005-01-18 17:35:28 +00003800 // Table entry doesn't exist?
3801 if (Opc == 0) return false;
3802
Chris Lattner78d30282005-01-18 03:51:59 +00003803 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3804 assert(0 && "Already emitted?");
Chris Lattnere86c9332005-01-17 22:10:42 +00003805 Select(Chain);
Chris Lattner96113fd2005-01-17 19:25:26 +00003806 Select(TheLoad.getOperand(0));
Chris Lattnera7acdda2005-01-18 01:06:26 +00003807
Chris Lattner96113fd2005-01-17 19:25:26 +00003808 X86AddressMode AM;
3809 SelectAddress(TheLoad.getOperand(1), AM);
3810 unsigned Reg = SelectExpr(Op1);
Chris Lattnera7acdda2005-01-18 01:06:26 +00003811 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattner96113fd2005-01-17 19:25:26 +00003812 return true;
3813}
3814
Chris Lattnerdd66a412005-05-15 05:46:45 +00003815/// If node is a ret(tailcall) node, emit the specified tail call and return
3816/// true, otherwise return false.
3817///
3818/// FIXME: This whole thing should be a post-legalize optimization pass which
3819/// recognizes and transforms the dag. We don't want the selection phase doing
3820/// this stuff!!
3821///
3822bool ISel::EmitPotentialTailCall(SDNode *RetNode) {
3823 assert(RetNode->getOpcode() == ISD::RET && "Not a return");
3824
3825 SDOperand Chain = RetNode->getOperand(0);
3826
3827 // If this is a token factor node where one operand is a call, dig into it.
3828 SDOperand TokFactor;
3829 unsigned TokFactorOperand = 0;
3830 if (Chain.getOpcode() == ISD::TokenFactor) {
3831 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3832 if (Chain.getOperand(i).getOpcode() == ISD::CALLSEQ_END ||
3833 Chain.getOperand(i).getOpcode() == X86ISD::TAILCALL) {
3834 TokFactorOperand = i;
3835 TokFactor = Chain;
3836 Chain = Chain.getOperand(i);
3837 break;
3838 }
3839 if (TokFactor.Val == 0) return false; // No call operand.
3840 }
3841
3842 // Skip the CALLSEQ_END node if present.
3843 if (Chain.getOpcode() == ISD::CALLSEQ_END)
3844 Chain = Chain.getOperand(0);
3845
3846 // Is a tailcall the last control operation that occurs before the return?
3847 if (Chain.getOpcode() != X86ISD::TAILCALL)
3848 return false;
3849
3850 // If we return a value, is it the value produced by the call?
3851 if (RetNode->getNumOperands() > 1) {
3852 // Not returning the ret val of the call?
3853 if (Chain.Val->getNumValues() == 1 ||
3854 RetNode->getOperand(1) != Chain.getValue(1))
3855 return false;
3856
3857 if (RetNode->getNumOperands() > 2) {
3858 if (Chain.Val->getNumValues() == 2 ||
3859 RetNode->getOperand(2) != Chain.getValue(2))
3860 return false;
3861 }
3862 assert(RetNode->getNumOperands() <= 3);
3863 }
3864
3865 // CalleeCallArgAmt - The total number of bytes used for the callee arg area.
3866 // For FastCC, this will always be > 0.
3867 unsigned CalleeCallArgAmt =
3868 cast<ConstantSDNode>(Chain.getOperand(2))->getValue();
3869
3870 // CalleeCallArgPopAmt - The number of bytes in the call area popped by the
3871 // callee. For FastCC this will always be > 0, for CCC this is always 0.
3872 unsigned CalleeCallArgPopAmt =
3873 cast<ConstantSDNode>(Chain.getOperand(3))->getValue();
3874
3875 // There are several cases we can handle here. First, if the caller and
3876 // callee are both CCC functions, we can tailcall if the callee takes <= the
3877 // number of argument bytes that the caller does.
3878 if (CalleeCallArgPopAmt == 0 && // Callee is C CallingConv?
3879 X86Lowering.getBytesToPopOnReturn() == 0) { // Caller is C CallingConv?
3880 // Check to see if caller arg area size >= callee arg area size.
3881 if (X86Lowering.getBytesCallerReserves() >= CalleeCallArgAmt) {
3882 //std::cerr << "CCC TAILCALL UNIMP!\n";
3883 // If TokFactor is non-null, emit all operands.
3884
3885 //EmitCCCToCCCTailCall(Chain.Val);
3886 //return true;
3887 }
3888 return false;
3889 }
3890
3891 // Second, if both are FastCC functions, we can always perform the tail call.
3892 if (CalleeCallArgPopAmt && X86Lowering.getBytesToPopOnReturn()) {
3893 // If TokFactor is non-null, emit all operands before the call.
3894 if (TokFactor.Val) {
3895 for (unsigned i = 0, e = TokFactor.getNumOperands(); i != e; ++i)
3896 if (i != TokFactorOperand)
3897 Select(TokFactor.getOperand(i));
3898 }
3899
3900 EmitFastCCToFastCCTailCall(Chain.Val);
3901 return true;
3902 }
3903
3904 // We don't support mixed calls, due to issues with alignment. We could in
3905 // theory handle some mixed calls from CCC -> FastCC if the stack is properly
3906 // aligned (which depends on the number of arguments to the callee). TODO.
3907 return false;
3908}
3909
3910static SDOperand GetAdjustedArgumentStores(SDOperand Chain, int Offset,
3911 SelectionDAG &DAG) {
3912 MVT::ValueType StoreVT;
3913 switch (Chain.getOpcode()) {
Chris Lattnerc1469402005-08-25 00:05:15 +00003914 default: assert(0 && "Unexpected node!");
Chris Lattnerdd66a412005-05-15 05:46:45 +00003915 case ISD::CALLSEQ_START:
Chris Lattner1a61fa42005-05-15 06:07:10 +00003916 // If we found the start of the call sequence, we're done. We actually
3917 // strip off the CALLSEQ_START node, to avoid generating the
3918 // ADJCALLSTACKDOWN marker for the tail call.
3919 return Chain.getOperand(0);
Chris Lattnerdd66a412005-05-15 05:46:45 +00003920 case ISD::TokenFactor: {
3921 std::vector<SDOperand> Ops;
3922 Ops.reserve(Chain.getNumOperands());
3923 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3924 Ops.push_back(GetAdjustedArgumentStores(Chain.getOperand(i), Offset,DAG));
3925 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
3926 }
3927 case ISD::STORE: // Normal store
3928 StoreVT = Chain.getOperand(1).getValueType();
3929 break;
3930 case ISD::TRUNCSTORE: // FLOAT store
Chris Lattner36db1ed2005-07-10 00:29:18 +00003931 StoreVT = cast<VTSDNode>(Chain.getOperand(4))->getVT();
Chris Lattnerdd66a412005-05-15 05:46:45 +00003932 break;
3933 }
3934
3935 SDOperand OrigDest = Chain.getOperand(2);
3936 unsigned OrigOffset;
3937
3938 if (OrigDest.getOpcode() == ISD::CopyFromReg) {
3939 OrigOffset = 0;
Chris Lattner7c762782005-08-16 21:56:37 +00003940 assert(cast<RegisterSDNode>(OrigDest.getOperand(1))->getReg() == X86::ESP);
Chris Lattnerdd66a412005-05-15 05:46:45 +00003941 } else {
3942 // We expect only (ESP+C)
3943 assert(OrigDest.getOpcode() == ISD::ADD &&
3944 isa<ConstantSDNode>(OrigDest.getOperand(1)) &&
3945 OrigDest.getOperand(0).getOpcode() == ISD::CopyFromReg &&
Chris Lattner7c762782005-08-16 21:56:37 +00003946 cast<RegisterSDNode>(OrigDest.getOperand(0).getOperand(1))->getReg()
3947 == X86::ESP);
Chris Lattnerdd66a412005-05-15 05:46:45 +00003948 OrigOffset = cast<ConstantSDNode>(OrigDest.getOperand(1))->getValue();
3949 }
3950
3951 // Compute the new offset from the incoming ESP value we wish to use.
3952 unsigned NewOffset = OrigOffset + Offset;
3953
3954 unsigned OpSize = (MVT::getSizeInBits(StoreVT)+7)/8; // Bits -> Bytes
3955 MachineFunction &MF = DAG.getMachineFunction();
3956 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, NewOffset);
3957 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
3958
3959 SDOperand InChain = GetAdjustedArgumentStores(Chain.getOperand(0), Offset,
3960 DAG);
3961 if (Chain.getOpcode() == ISD::STORE)
3962 return DAG.getNode(ISD::STORE, MVT::Other, InChain, Chain.getOperand(1),
3963 FIN);
3964 assert(Chain.getOpcode() == ISD::TRUNCSTORE);
3965 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, InChain, Chain.getOperand(1),
Chris Lattner36db1ed2005-07-10 00:29:18 +00003966 FIN, DAG.getSrcValue(NULL), DAG.getValueType(StoreVT));
Chris Lattnerdd66a412005-05-15 05:46:45 +00003967}
3968
3969
3970/// EmitFastCCToFastCCTailCall - Given a tailcall in the tail position to a
3971/// fastcc function from a fastcc function, emit the code to emit a 'proper'
3972/// tail call.
3973void ISel::EmitFastCCToFastCCTailCall(SDNode *TailCallNode) {
3974 unsigned CalleeCallArgSize =
3975 cast<ConstantSDNode>(TailCallNode->getOperand(2))->getValue();
3976 unsigned CallerArgSize = X86Lowering.getBytesToPopOnReturn();
3977
3978 //std::cerr << "****\n*** EMITTING TAIL CALL!\n****\n";
3979
3980 // Adjust argument stores. Instead of storing to [ESP], f.e., store to frame
3981 // indexes that are relative to the incoming ESP. If the incoming and
3982 // outgoing arg sizes are the same we will store to [InESP] instead of
3983 // [CurESP] and the ESP referenced will be relative to the incoming function
3984 // ESP.
3985 int ESPOffset = CallerArgSize-CalleeCallArgSize;
3986 SDOperand AdjustedArgStores =
3987 GetAdjustedArgumentStores(TailCallNode->getOperand(0), ESPOffset, *TheDAG);
3988
3989 // Copy the return address of the caller into a virtual register so we don't
3990 // clobber it.
3991 SDOperand RetVal;
3992 if (ESPOffset) {
3993 SDOperand RetValAddr = X86Lowering.getReturnAddressFrameIndex(*TheDAG);
3994 RetVal = TheDAG->getLoad(MVT::i32, TheDAG->getEntryNode(),
3995 RetValAddr, TheDAG->getSrcValue(NULL));
3996 SelectExpr(RetVal);
3997 }
3998
3999 // Codegen all of the argument stores.
4000 Select(AdjustedArgStores);
4001
4002 if (RetVal.Val) {
4003 // Emit a store of the saved ret value to the new location.
4004 MachineFunction &MF = TheDAG->getMachineFunction();
4005 int ReturnAddrFI = MF.getFrameInfo()->CreateFixedObject(4, ESPOffset-4);
4006 SDOperand RetValAddr = TheDAG->getFrameIndex(ReturnAddrFI, MVT::i32);
4007 Select(TheDAG->getNode(ISD::STORE, MVT::Other, TheDAG->getEntryNode(),
4008 RetVal, RetValAddr));
4009 }
4010
4011 // Get the destination value.
4012 SDOperand Callee = TailCallNode->getOperand(1);
4013 bool isDirect = isa<GlobalAddressSDNode>(Callee) ||
4014 isa<ExternalSymbolSDNode>(Callee);
Chris Lattner459a9cb2005-06-17 13:23:32 +00004015 unsigned CalleeReg = 0;
Chris Lattnerdd66a412005-05-15 05:46:45 +00004016 if (!isDirect) CalleeReg = SelectExpr(Callee);
4017
4018 unsigned RegOp1 = 0;
4019 unsigned RegOp2 = 0;
4020
4021 if (TailCallNode->getNumOperands() > 4) {
4022 // The first value is passed in (a part of) EAX, the second in EDX.
4023 RegOp1 = SelectExpr(TailCallNode->getOperand(4));
4024 if (TailCallNode->getNumOperands() > 5)
4025 RegOp2 = SelectExpr(TailCallNode->getOperand(5));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004026
Chris Lattnerdd66a412005-05-15 05:46:45 +00004027 switch (TailCallNode->getOperand(4).getValueType()) {
4028 default: assert(0 && "Bad thing to pass in regs");
4029 case MVT::i1:
4030 case MVT::i8:
4031 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(RegOp1);
4032 RegOp1 = X86::AL;
4033 break;
4034 case MVT::i16:
4035 BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1);
4036 RegOp1 = X86::AX;
4037 break;
4038 case MVT::i32:
4039 BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);
4040 RegOp1 = X86::EAX;
4041 break;
4042 }
4043 if (RegOp2)
4044 switch (TailCallNode->getOperand(5).getValueType()) {
4045 default: assert(0 && "Bad thing to pass in regs");
4046 case MVT::i1:
4047 case MVT::i8:
4048 BuildMI(BB, X86::MOV8rr, 1, X86::DL).addReg(RegOp2);
4049 RegOp2 = X86::DL;
4050 break;
4051 case MVT::i16:
4052 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
4053 RegOp2 = X86::DX;
4054 break;
4055 case MVT::i32:
4056 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
4057 RegOp2 = X86::EDX;
4058 break;
4059 }
4060 }
4061
4062 // Adjust ESP.
4063 if (ESPOffset)
4064 BuildMI(BB, X86::ADJSTACKPTRri, 2,
4065 X86::ESP).addReg(X86::ESP).addImm(ESPOffset);
4066
4067 // TODO: handle jmp [mem]
4068 if (!isDirect) {
4069 BuildMI(BB, X86::TAILJMPr, 1).addReg(CalleeReg);
4070 } else if (GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Callee)){
Chris Lattner57279592005-05-19 05:54:33 +00004071 BuildMI(BB, X86::TAILJMPd, 1).addGlobalAddress(GASD->getGlobal(), true);
Chris Lattnerdd66a412005-05-15 05:46:45 +00004072 } else {
4073 ExternalSymbolSDNode *ESSDN = cast<ExternalSymbolSDNode>(Callee);
4074 BuildMI(BB, X86::TAILJMPd, 1).addExternalSymbol(ESSDN->getSymbol(), true);
4075 }
4076 // ADD IMPLICIT USE RegOp1/RegOp2's
4077}
4078
Chris Lattner96113fd2005-01-17 19:25:26 +00004079
Chris Lattner88c8a232005-01-07 07:49:41 +00004080void ISel::Select(SDOperand N) {
Chris Lattner9982da22005-10-02 16:29:36 +00004081 unsigned Tmp1 = 0, Tmp2 = 0, Opc = 0;
Chris Lattner88c8a232005-01-07 07:49:41 +00004082
Nate Begeman95210522005-03-24 04:39:54 +00004083 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner88c8a232005-01-07 07:49:41 +00004084 return; // Already selected.
4085
Chris Lattner36f78482005-01-11 06:14:36 +00004086 SDNode *Node = N.Val;
4087
4088 switch (Node->getOpcode()) {
Chris Lattner88c8a232005-01-07 07:49:41 +00004089 default:
Chris Lattner36f78482005-01-11 06:14:36 +00004090 Node->dump(); std::cerr << "\n";
Chris Lattner88c8a232005-01-07 07:49:41 +00004091 assert(0 && "Node not handled yet!");
4092 case ISD::EntryToken: return; // Noop
Chris Lattnerc251fb62005-01-13 18:01:36 +00004093 case ISD::TokenFactor:
Chris Lattner15bd19d2005-01-13 19:56:00 +00004094 if (Node->getNumOperands() == 2) {
Misha Brukmanc88330a2005-04-21 23:38:14 +00004095 bool OneFirst =
Chris Lattner15bd19d2005-01-13 19:56:00 +00004096 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
4097 Select(Node->getOperand(OneFirst));
4098 Select(Node->getOperand(!OneFirst));
4099 } else {
4100 std::vector<std::pair<unsigned, unsigned> > OpsP;
4101 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
4102 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
4103 std::sort(OpsP.begin(), OpsP.end());
4104 std::reverse(OpsP.begin(), OpsP.end());
4105 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
4106 Select(Node->getOperand(OpsP[i].second));
4107 }
Chris Lattnerc251fb62005-01-13 18:01:36 +00004108 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00004109 case ISD::CopyToReg:
Chris Lattner7c762782005-08-16 21:56:37 +00004110 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
Chris Lattner2cfce682005-01-12 02:02:48 +00004111 Select(N.getOperand(0));
Chris Lattner7c762782005-08-16 21:56:37 +00004112 Tmp1 = SelectExpr(N.getOperand(2));
Chris Lattner2cfce682005-01-12 02:02:48 +00004113 } else {
Chris Lattner7c762782005-08-16 21:56:37 +00004114 Tmp1 = SelectExpr(N.getOperand(2));
Chris Lattner2cfce682005-01-12 02:02:48 +00004115 Select(N.getOperand(0));
4116 }
Chris Lattner7c762782005-08-16 21:56:37 +00004117 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukmanc88330a2005-04-21 23:38:14 +00004118
Chris Lattner88c8a232005-01-07 07:49:41 +00004119 if (Tmp1 != Tmp2) {
Chris Lattner7c762782005-08-16 21:56:37 +00004120 switch (N.getOperand(2).getValueType()) {
Chris Lattner88c8a232005-01-07 07:49:41 +00004121 default: assert(0 && "Invalid type for operation!");
4122 case MVT::i1:
4123 case MVT::i8: Opc = X86::MOV8rr; break;
4124 case MVT::i16: Opc = X86::MOV16rr; break;
4125 case MVT::i32: Opc = X86::MOV32rr; break;
Nate Begeman9d7008b2005-10-14 22:06:00 +00004126 case MVT::f32: Opc = X86::MOVSSrr; break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004127 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00004128 if (X86ScalarSSE) {
Nate Begeman9d7008b2005-10-14 22:06:00 +00004129 Opc = X86::MOVSDrr;
Nate Begeman8a093362005-07-06 18:59:04 +00004130 } else {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004131 Opc = X86::FpMOV;
4132 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00004133 }
4134 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00004135 }
4136 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
4137 }
4138 return;
4139 case ISD::RET:
Chris Lattnerdd66a412005-05-15 05:46:45 +00004140 if (N.getOperand(0).getOpcode() == ISD::CALLSEQ_END ||
4141 N.getOperand(0).getOpcode() == X86ISD::TAILCALL ||
4142 N.getOperand(0).getOpcode() == ISD::TokenFactor)
4143 if (EmitPotentialTailCall(Node))
4144 return;
4145
Chris Lattner88c8a232005-01-07 07:49:41 +00004146 switch (N.getNumOperands()) {
4147 default:
4148 assert(0 && "Unknown return instruction!");
4149 case 3:
Chris Lattner88c8a232005-01-07 07:49:41 +00004150 assert(N.getOperand(1).getValueType() == MVT::i32 &&
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004151 N.getOperand(2).getValueType() == MVT::i32 &&
4152 "Unknown two-register value!");
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004153 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
4154 Tmp1 = SelectExpr(N.getOperand(1));
4155 Tmp2 = SelectExpr(N.getOperand(2));
4156 } else {
4157 Tmp2 = SelectExpr(N.getOperand(2));
4158 Tmp1 = SelectExpr(N.getOperand(1));
4159 }
4160 Select(N.getOperand(0));
4161
Chris Lattner88c8a232005-01-07 07:49:41 +00004162 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4163 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00004164 break;
4165 case 2:
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004166 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4167 Select(N.getOperand(0));
4168 Tmp1 = SelectExpr(N.getOperand(1));
4169 } else {
4170 Tmp1 = SelectExpr(N.getOperand(1));
4171 Select(N.getOperand(0));
4172 }
Chris Lattner88c8a232005-01-07 07:49:41 +00004173 switch (N.getOperand(1).getValueType()) {
4174 default: assert(0 && "All other types should have been promoted!!");
Nate Begeman8a093362005-07-06 18:59:04 +00004175 case MVT::f32:
4176 if (X86ScalarSSE) {
4177 // Spill the value to memory and reload it into top of stack.
4178 unsigned Size = MVT::getSizeInBits(MVT::f32)/8;
4179 MachineFunction *F = BB->getParent();
4180 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
4181 addFrameReference(BuildMI(BB, X86::MOVSSmr, 5), FrameIdx).addReg(Tmp1);
4182 addFrameReference(BuildMI(BB, X86::FLD32m, 4, X86::FP0), FrameIdx);
4183 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004184 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00004185 } else {
4186 assert(0 && "MVT::f32 only legal with scalar sse fp");
4187 abort();
4188 }
4189 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00004190 case MVT::f64:
Nate Begeman8a093362005-07-06 18:59:04 +00004191 if (X86ScalarSSE) {
4192 // Spill the value to memory and reload it into top of stack.
4193 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
4194 MachineFunction *F = BB->getParent();
4195 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
4196 addFrameReference(BuildMI(BB, X86::MOVSDmr, 5), FrameIdx).addReg(Tmp1);
4197 addFrameReference(BuildMI(BB, X86::FLD64m, 4, X86::FP0), FrameIdx);
4198 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004199 ContainsFPCode = true;
Nate Begeman8a093362005-07-06 18:59:04 +00004200 } else {
4201 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
4202 }
4203 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00004204 case MVT::i32:
Nate Begeman8a093362005-07-06 18:59:04 +00004205 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4206 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00004207 }
4208 break;
4209 case 1:
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004210 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00004211 break;
4212 }
Chris Lattnerc0e369e2005-05-13 21:44:04 +00004213 if (X86Lowering.getBytesToPopOnReturn() == 0)
4214 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
4215 else
4216 BuildMI(BB, X86::RETI, 1).addImm(X86Lowering.getBytesToPopOnReturn());
Chris Lattner88c8a232005-01-07 07:49:41 +00004217 return;
4218 case ISD::BR: {
4219 Select(N.getOperand(0));
4220 MachineBasicBlock *Dest =
4221 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
4222 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
4223 return;
4224 }
4225
4226 case ISD::BRCOND: {
Chris Lattner88c8a232005-01-07 07:49:41 +00004227 MachineBasicBlock *Dest =
4228 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004229
Chris Lattner88c8a232005-01-07 07:49:41 +00004230 // Try to fold a setcc into the branch. If this fails, emit a test/jne
4231 // pair.
Chris Lattner37ed2852005-01-11 04:06:27 +00004232 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
4233 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4234 Select(N.getOperand(0));
4235 Tmp1 = SelectExpr(N.getOperand(1));
4236 } else {
4237 Tmp1 = SelectExpr(N.getOperand(1));
4238 Select(N.getOperand(0));
4239 }
Chris Lattner88c8a232005-01-07 07:49:41 +00004240 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
4241 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
4242 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004243
Chris Lattner88c8a232005-01-07 07:49:41 +00004244 return;
4245 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00004246
Chris Lattnerc1f386c2005-01-17 00:00:33 +00004247 case ISD::LOAD:
4248 // If this load could be folded into the only using instruction, and if it
4249 // is safe to emit the instruction here, try to do so now.
4250 if (Node->hasNUsesOfValue(1, 0)) {
4251 SDOperand TheVal = N.getValue(0);
4252 SDNode *User = 0;
4253 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
4254 assert(UI != Node->use_end() && "Didn't find use!");
4255 SDNode *UN = *UI;
4256 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
4257 if (UN->getOperand(i) == TheVal) {
4258 User = UN;
4259 goto FoundIt;
4260 }
4261 }
4262 FoundIt:
4263 // Only handle unary operators right now.
4264 if (User->getNumOperands() == 1) {
Chris Lattner78d30282005-01-18 03:51:59 +00004265 ExprMap.erase(N);
Chris Lattnerc1f386c2005-01-17 00:00:33 +00004266 SelectExpr(SDOperand(User, 0));
4267 return;
4268 }
4269 }
Chris Lattner28a205e2005-01-18 04:00:54 +00004270 ExprMap.erase(N);
Chris Lattnerc1f386c2005-01-17 00:00:33 +00004271 SelectExpr(N);
4272 return;
Chris Lattner70ea07c2005-05-09 21:17:38 +00004273 case ISD::READPORT:
Chris Lattnere18a4c42005-01-15 05:22:24 +00004274 case ISD::EXTLOAD:
4275 case ISD::SEXTLOAD:
4276 case ISD::ZEXTLOAD:
Chris Lattner88c8a232005-01-07 07:49:41 +00004277 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner1b3520c2005-05-14 08:48:15 +00004278 case X86ISD::TAILCALL:
4279 case X86ISD::CALL:
Chris Lattner28a205e2005-01-18 04:00:54 +00004280 ExprMap.erase(N);
Chris Lattner88c8a232005-01-07 07:49:41 +00004281 SelectExpr(N);
4282 return;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00004283 case ISD::CopyFromReg:
Chris Lattnera36117b2005-05-14 06:52:07 +00004284 case X86ISD::FILD64m:
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00004285 ExprMap.erase(N);
4286 SelectExpr(N.getValue(0));
4287 return;
Jeff Cohen546fd592005-07-30 18:33:25 +00004288
Chris Lattner4738d1b2005-07-30 00:05:54 +00004289 case X86ISD::FP_TO_INT16_IN_MEM:
4290 case X86ISD::FP_TO_INT32_IN_MEM:
Chris Lattner6dc60e82005-07-29 00:54:34 +00004291 case X86ISD::FP_TO_INT64_IN_MEM: {
Chris Lattner67756e22005-07-29 00:40:01 +00004292 assert(N.getOperand(1).getValueType() == MVT::f64);
4293 X86AddressMode AM;
4294 Select(N.getOperand(0)); // Select the token chain
4295
4296 unsigned ValReg;
4297 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
4298 ValReg = SelectExpr(N.getOperand(1));
4299 SelectAddress(N.getOperand(2), AM);
4300 } else {
4301 SelectAddress(N.getOperand(2), AM);
4302 ValReg = SelectExpr(N.getOperand(1));
4303 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004304
Chris Lattner6dc60e82005-07-29 00:54:34 +00004305 // Change the floating point control register to use "round towards zero"
4306 // mode when truncating to an integer value.
4307 //
4308 MachineFunction *F = BB->getParent();
4309 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
4310 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
Jeff Cohen546fd592005-07-30 18:33:25 +00004311
Chris Lattner6dc60e82005-07-29 00:54:34 +00004312 // Load the old value of the high byte of the control word...
Chris Lattneraeef51b2005-07-30 00:17:52 +00004313 unsigned OldCW = MakeReg(MVT::i16);
4314 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
Jeff Cohen546fd592005-07-30 18:33:25 +00004315
Chris Lattner6dc60e82005-07-29 00:54:34 +00004316 // Set the high part to be round to zero...
Chris Lattner49134572005-07-30 00:43:00 +00004317 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
Jeff Cohen546fd592005-07-30 18:33:25 +00004318
Chris Lattner6dc60e82005-07-29 00:54:34 +00004319 // Reload the modified control word now...
4320 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Jeff Cohen546fd592005-07-30 18:33:25 +00004321
Chris Lattner6dc60e82005-07-29 00:54:34 +00004322 // Restore the memory image of control word to original value
Chris Lattneraeef51b2005-07-30 00:17:52 +00004323 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
Chris Lattner4738d1b2005-07-30 00:05:54 +00004324
4325 // Get the X86 opcode to use.
4326 switch (N.getOpcode()) {
4327 case X86ISD::FP_TO_INT16_IN_MEM: Tmp1 = X86::FIST16m; break;
4328 case X86ISD::FP_TO_INT32_IN_MEM: Tmp1 = X86::FIST32m; break;
4329 case X86ISD::FP_TO_INT64_IN_MEM: Tmp1 = X86::FISTP64m; break;
4330 }
Jeff Cohen546fd592005-07-30 18:33:25 +00004331
Chris Lattner4738d1b2005-07-30 00:05:54 +00004332 addFullAddress(BuildMI(BB, Tmp1, 5), AM).addReg(ValReg);
Jeff Cohen546fd592005-07-30 18:33:25 +00004333
Chris Lattner6dc60e82005-07-29 00:54:34 +00004334 // Reload the original control word now.
4335 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner67756e22005-07-29 00:40:01 +00004336 return;
4337 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00004338
Chris Lattner36db1ed2005-07-10 00:29:18 +00004339 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr, SRCVALUE, storety
Chris Lattnere18a4c42005-01-15 05:22:24 +00004340 X86AddressMode AM;
Chris Lattner36db1ed2005-07-10 00:29:18 +00004341 MVT::ValueType StoredTy = cast<VTSDNode>(N.getOperand(4))->getVT();
Chris Lattnerb14a63a2005-01-16 07:34:08 +00004342 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
4343 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
4344 && "Unsupported TRUNCSTORE for this target!");
4345
4346 if (StoredTy == MVT::i16) {
4347 // FIXME: This is here just to allow testing. X86 doesn't really have a
4348 // TRUNCSTORE i16 operation, but this is required for targets that do not
4349 // have 16-bit integer registers. We occasionally disable 16-bit integer
4350 // registers to test the promotion code.
4351 Select(N.getOperand(0));
4352 Tmp1 = SelectExpr(N.getOperand(1));
4353 SelectAddress(N.getOperand(2), AM);
4354
4355 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4356 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
4357 return;
4358 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00004359
4360 // Store of constant bool?
4361 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
4362 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4363 Select(N.getOperand(0));
4364 SelectAddress(N.getOperand(2), AM);
4365 } else {
4366 SelectAddress(N.getOperand(2), AM);
4367 Select(N.getOperand(0));
4368 }
4369 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
4370 return;
4371 }
4372
4373 switch (StoredTy) {
4374 default: assert(0 && "Cannot truncstore this type!");
4375 case MVT::i1: Opc = X86::MOV8mr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00004376 case MVT::f32:
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004377 assert(!X86ScalarSSE && "Cannot truncstore scalar SSE regs");
Nate Begeman8a093362005-07-06 18:59:04 +00004378 Opc = X86::FST32m; break;
Chris Lattnere18a4c42005-01-15 05:22:24 +00004379 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00004380
Chris Lattnere18a4c42005-01-15 05:22:24 +00004381 std::vector<std::pair<unsigned, unsigned> > RP;
4382 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
4383 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
4384 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
4385 std::sort(RP.begin(), RP.end());
4386
Chris Lattner80c5b972005-02-23 05:57:21 +00004387 Tmp1 = 0; // Silence a warning.
Chris Lattnere18a4c42005-01-15 05:22:24 +00004388 for (unsigned i = 0; i != 3; ++i)
4389 switch (RP[2-i].second) {
4390 default: assert(0 && "Unknown operand number!");
4391 case 0: Select(N.getOperand(0)); break;
4392 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
4393 case 2: SelectAddress(N.getOperand(2), AM); break;
4394 }
4395
4396 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
4397 return;
4398 }
Chris Lattner88c8a232005-01-07 07:49:41 +00004399 case ISD::STORE: {
Chris Lattner88c8a232005-01-07 07:49:41 +00004400 X86AddressMode AM;
Chris Lattner88c8a232005-01-07 07:49:41 +00004401
4402 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
4403 Opc = 0;
4404 switch (CN->getValueType(0)) {
4405 default: assert(0 && "Invalid type for operation!");
4406 case MVT::i1:
4407 case MVT::i8: Opc = X86::MOV8mi; break;
4408 case MVT::i16: Opc = X86::MOV16mi; break;
4409 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00004410 }
4411 if (Opc) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004412 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4413 Select(N.getOperand(0));
4414 SelectAddress(N.getOperand(2), AM);
4415 } else {
4416 SelectAddress(N.getOperand(2), AM);
4417 Select(N.getOperand(0));
4418 }
Chris Lattner88c8a232005-01-07 07:49:41 +00004419 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
4420 return;
4421 }
Chris Lattneradcfc172005-04-21 19:03:24 +00004422 } else if (GlobalAddressSDNode *GA =
4423 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
4424 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
4425
4426 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4427 Select(N.getOperand(0));
4428 SelectAddress(N.getOperand(2), AM);
4429 } else {
4430 SelectAddress(N.getOperand(2), AM);
4431 Select(N.getOperand(0));
4432 }
Nate Begemana0b5e032005-07-15 00:38:55 +00004433 GlobalValue *GV = GA->getGlobal();
4434 // For Darwin, external and weak symbols are indirect, so we want to load
4435 // the value at address GV, not the value of GV itself.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00004436 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemana0b5e032005-07-15 00:38:55 +00004437 (GV->hasWeakLinkage() || GV->isExternal())) {
4438 Tmp1 = MakeReg(MVT::i32);
4439 BuildMI(BB, X86::MOV32rm, 4, Tmp1).addReg(0).addZImm(1).addReg(0)
4440 .addGlobalAddress(GV, false, 0);
4441 addFullAddress(BuildMI(BB, X86::MOV32mr, 4+1),AM).addReg(Tmp1);
4442 } else {
4443 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),AM).addGlobalAddress(GV);
4444 }
Chris Lattneradcfc172005-04-21 19:03:24 +00004445 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00004446 }
Chris Lattner75bac9f2005-01-11 23:21:30 +00004447
4448 // Check to see if this is a load/op/store combination.
Chris Lattner96113fd2005-01-17 19:25:26 +00004449 if (TryToFoldLoadOpStore(Node))
4450 return;
Chris Lattner75bac9f2005-01-11 23:21:30 +00004451
Chris Lattner88c8a232005-01-07 07:49:41 +00004452 switch (N.getOperand(1).getValueType()) {
4453 default: assert(0 && "Cannot store this type!");
4454 case MVT::i1:
4455 case MVT::i8: Opc = X86::MOV8mr; break;
4456 case MVT::i16: Opc = X86::MOV16mr; break;
4457 case MVT::i32: Opc = X86::MOV32mr; break;
Nate Begeman8a093362005-07-06 18:59:04 +00004458 case MVT::f32: Opc = X86::MOVSSmr; break;
4459 case MVT::f64: Opc = X86ScalarSSE ? X86::MOVSDmr : X86::FST64m; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00004460 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00004461
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004462 std::vector<std::pair<unsigned, unsigned> > RP;
4463 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
4464 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
4465 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
4466 std::sort(RP.begin(), RP.end());
4467
Chris Lattner80c5b972005-02-23 05:57:21 +00004468 Tmp1 = 0; // Silence a warning.
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004469 for (unsigned i = 0; i != 3; ++i)
4470 switch (RP[2-i].second) {
4471 default: assert(0 && "Unknown operand number!");
4472 case 0: Select(N.getOperand(0)); break;
4473 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattner8fea42b2005-01-11 03:37:59 +00004474 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner0d1f82a2005-01-11 03:11:44 +00004475 }
4476
Chris Lattner88c8a232005-01-07 07:49:41 +00004477 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
4478 return;
4479 }
Chris Lattner2dce7032005-05-12 23:24:06 +00004480 case ISD::CALLSEQ_START:
Chris Lattnerc0e369e2005-05-13 21:44:04 +00004481 Select(N.getOperand(0));
4482 // Stack amount
4483 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
4484 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(Tmp1);
4485 return;
Chris Lattner2dce7032005-05-12 23:24:06 +00004486 case ISD::CALLSEQ_END:
Chris Lattner88c8a232005-01-07 07:49:41 +00004487 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00004488 return;
Chris Lattner36f78482005-01-11 06:14:36 +00004489 case ISD::MEMSET: {
4490 Select(N.getOperand(0)); // Select the chain.
4491 unsigned Align =
4492 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
4493 if (Align == 0) Align = 1;
4494
4495 // Turn the byte code into # iterations
4496 unsigned CountReg;
4497 unsigned Opcode;
4498 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
4499 unsigned Val = ValC->getValue() & 255;
4500
4501 // If the value is a constant, then we can potentially use larger sets.
4502 switch (Align & 3) {
4503 case 2: // WORD aligned
4504 CountReg = MakeReg(MVT::i32);
4505 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4506 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
4507 } else {
4508 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4509 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
4510 }
4511 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
4512 Opcode = X86::REP_STOSW;
4513 break;
4514 case 0: // DWORD aligned
4515 CountReg = MakeReg(MVT::i32);
4516 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4517 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
4518 } else {
4519 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4520 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
4521 }
4522 Val = (Val << 8) | Val;
4523 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
4524 Opcode = X86::REP_STOSD;
4525 break;
4526 default: // BYTE aligned
4527 CountReg = SelectExpr(Node->getOperand(3));
4528 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
4529 Opcode = X86::REP_STOSB;
4530 break;
4531 }
4532 } else {
4533 // If it's not a constant value we are storing, just fall back. We could
4534 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
4535 unsigned ValReg = SelectExpr(Node->getOperand(2));
4536 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
4537 CountReg = SelectExpr(Node->getOperand(3));
4538 Opcode = X86::REP_STOSB;
4539 }
4540
4541 // No matter what the alignment is, we put the source in ESI, the
4542 // destination in EDI, and the count in ECX.
4543 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
4544 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
4545 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
4546 BuildMI(BB, Opcode, 0);
4547 return;
4548 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00004549 case ISD::MEMCPY: {
Chris Lattnerc07164e2005-01-11 06:19:26 +00004550 Select(N.getOperand(0)); // Select the chain.
4551 unsigned Align =
4552 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
4553 if (Align == 0) Align = 1;
4554
4555 // Turn the byte code into # iterations
4556 unsigned CountReg;
4557 unsigned Opcode;
4558 switch (Align & 3) {
4559 case 2: // WORD aligned
4560 CountReg = MakeReg(MVT::i32);
4561 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4562 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
4563 } else {
4564 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4565 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
4566 }
4567 Opcode = X86::REP_MOVSW;
4568 break;
4569 case 0: // DWORD aligned
4570 CountReg = MakeReg(MVT::i32);
4571 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4572 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
4573 } else {
4574 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4575 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
4576 }
4577 Opcode = X86::REP_MOVSD;
4578 break;
4579 default: // BYTE aligned
4580 CountReg = SelectExpr(Node->getOperand(3));
4581 Opcode = X86::REP_MOVSB;
4582 break;
4583 }
4584
4585 // No matter what the alignment is, we put the source in ESI, the
4586 // destination in EDI, and the count in ECX.
4587 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
4588 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
4589 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
4590 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
4591 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
4592 BuildMI(BB, Opcode, 0);
4593 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00004594 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00004595 case ISD::WRITEPORT:
4596 if (Node->getOperand(2).getValueType() != MVT::i16) {
4597 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
4598 exit(1);
4599 }
4600 Select(Node->getOperand(0)); // Emit the chain.
4601
4602 Tmp1 = SelectExpr(Node->getOperand(1));
4603 switch (Node->getOperand(1).getValueType()) {
4604 case MVT::i8:
4605 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
4606 Tmp2 = X86::OUT8ir; Opc = X86::OUT8rr;
4607 break;
4608 case MVT::i16:
4609 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(Tmp1);
4610 Tmp2 = X86::OUT16ir; Opc = X86::OUT16rr;
4611 break;
4612 case MVT::i32:
4613 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4614 Tmp2 = X86::OUT32ir; Opc = X86::OUT32rr;
4615 break;
4616 default:
4617 std::cerr << "llvm.writeport: invalid data type for X86 target";
4618 exit(1);
4619 }
4620
4621 // If the port is a single-byte constant, use the immediate form.
4622 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(2)))
4623 if ((CN->getValue() & 255) == CN->getValue()) {
4624 BuildMI(BB, Tmp2, 1).addImm(CN->getValue());
4625 return;
4626 }
4627
4628 // Otherwise, move the I/O port address into the DX register.
4629 unsigned Reg = SelectExpr(Node->getOperand(2));
4630 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
4631 BuildMI(BB, Opc, 0);
4632 return;
4633 }
Chris Lattner88c8a232005-01-07 07:49:41 +00004634 assert(0 && "Should not be reached!");
4635}
4636
4637
4638/// createX86PatternInstructionSelector - This pass converts an LLVM function
4639/// into a machine code representation using pattern matching and a machine
4640/// description file.
4641///
4642FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
Misha Brukmanc88330a2005-04-21 23:38:14 +00004643 return new ISel(TM);
Chris Lattner88c8a232005-01-07 07:49:41 +00004644}