blob: 9f602418aba3916c913b99d074fd9be6fe7eafa5 [file] [log] [blame]
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
Chris Lattner24aad1b2005-01-10 22:10:13 +00002//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00007//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for X86.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
15#include "X86InstrBuilder.h"
16#include "X86RegisterInfo.h"
Nate Begemanfb5792f2005-07-12 01:41:54 +000017#include "X86Subtarget.h"
Chris Lattnerc6f41812005-05-12 23:06:28 +000018#include "llvm/CallingConv.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000019#include "llvm/Constants.h"
20#include "llvm/Instructions.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000021#include "llvm/Function.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000023#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
27#include "llvm/CodeGen/SSARegMap.h"
28#include "llvm/Target/TargetData.h"
29#include "llvm/Target/TargetLowering.h"
Nate Begemanfb5792f2005-07-12 01:41:54 +000030#include "llvm/Target/TargetMachine.h"
Chris Lattnerc5dcb532005-04-30 04:25:35 +000031#include "llvm/Target/TargetOptions.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000032#include "llvm/Support/CFG.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000033#include "llvm/Support/MathExtras.h"
34#include "llvm/ADT/Statistic.h"
35#include <set>
Jeff Cohen603fea92005-01-12 04:29:05 +000036#include <algorithm>
Chris Lattner8acb1ba2005-01-07 07:49:41 +000037using namespace llvm;
38
Chris Lattnerc6f41812005-05-12 23:06:28 +000039// FIXME: temporary.
40#include "llvm/Support/CommandLine.h"
41static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
42 cl::desc("Enable fastcc on X86"));
43
Chris Lattner67649df2005-05-14 06:52:07 +000044namespace {
45 // X86 Specific DAG Nodes
46 namespace X86ISD {
47 enum NodeType {
48 // Start the numbering where the builtin ops leave off.
49 FIRST_NUMBER = ISD::BUILTIN_OP_END,
50
51 /// FILD64m - This instruction implements SINT_TO_FP with a
52 /// 64-bit source in memory and a FP reg result. This corresponds to
53 /// the X86::FILD64m instruction. It has two inputs (token chain and
54 /// address) and two outputs (FP value and token chain).
55 FILD64m,
Chris Lattner239738a2005-05-14 08:48:15 +000056
Chris Lattnerf7443da2005-07-29 00:54:34 +000057 /// FP_TO_INT*_IN_MEM - This instruction implements FP_TO_SINT with the
58 /// integer destination in memory and a FP reg source. This corresponds
59 /// to the X86::FIST*m instructions and the rounding mode change stuff. It
60 /// has two inputs (token chain and address) and two outputs (FP value and
61 /// token chain).
Chris Lattner01546c52005-07-30 00:05:54 +000062 FP_TO_INT16_IN_MEM,
63 FP_TO_INT32_IN_MEM,
Chris Lattnerf7443da2005-07-29 00:54:34 +000064 FP_TO_INT64_IN_MEM,
Jeff Cohend29b6aa2005-07-30 18:33:25 +000065
Chris Lattner239738a2005-05-14 08:48:15 +000066 /// CALL/TAILCALL - These operations represent an abstract X86 call
67 /// instruction, which includes a bunch of information. In particular the
68 /// operands of these node are:
69 ///
70 /// #0 - The incoming token chain
71 /// #1 - The callee
72 /// #2 - The number of arg bytes the caller pushes on the stack.
73 /// #3 - The number of arg bytes the callee pops off the stack.
74 /// #4 - The value to pass in AL/AX/EAX (optional)
75 /// #5 - The value to pass in DL/DX/EDX (optional)
76 ///
77 /// The result values of these nodes are:
78 ///
79 /// #0 - The outgoing token chain
80 /// #1 - The first register result value (optional)
81 /// #2 - The second register result value (optional)
82 ///
83 /// The CALL vs TAILCALL distinction boils down to whether the callee is
84 /// known not to modify the caller's stack frame, as is standard with
85 /// LLVM.
86 CALL,
87 TAILCALL,
Chris Lattner67649df2005-05-14 06:52:07 +000088 };
89 }
90}
91
Chris Lattner8acb1ba2005-01-07 07:49:41 +000092//===----------------------------------------------------------------------===//
93// X86TargetLowering - X86 Implementation of the TargetLowering interface
94namespace {
95 class X86TargetLowering : public TargetLowering {
96 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000097 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner381e8872005-05-15 05:46:45 +000098 int BytesToPopOnReturn; // Number of arg bytes ret should pop.
99 int BytesCallerReserves; // Number of arg bytes caller makes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000100 public:
101 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
102 // Set up the TargetLowering object.
Chris Lattner4df0de92005-01-17 00:00:33 +0000103
Chris Lattner653f7232005-05-13 22:46:57 +0000104 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Chris Lattner4df0de92005-01-17 00:00:33 +0000105 setShiftAmountType(MVT::i8);
106 setSetCCResultType(MVT::i8);
Chris Lattner6659bd72005-04-07 19:41:46 +0000107 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner009b55b2005-01-19 03:36:30 +0000108 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner4df0de92005-01-17 00:00:33 +0000109
110 // Set up the register classes.
Nate Begemanf63be7d2005-07-06 18:59:04 +0000111 // FIXME: Eliminate these two classes when legalize can handle promotions
112 // well.
113 addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000114 addRegisterClass(MVT::i8, X86::R8RegisterClass);
115 addRegisterClass(MVT::i16, X86::R16RegisterClass);
116 addRegisterClass(MVT::i32, X86::R32RegisterClass);
Jeff Cohen00b168892005-07-27 06:12:32 +0000117
Chris Lattnera28381c2005-07-16 00:28:20 +0000118 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
119 // operation.
120 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
121 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
122 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
123 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000124
125 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
126 // this operation.
127 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
128 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Jeff Cohen00b168892005-07-27 06:12:32 +0000129
Chris Lattner745d5382005-07-29 00:40:01 +0000130 if (!X86ScalarSSE) {
131 // We can handle SINT_TO_FP and FP_TO_SINT from/TO i64 even though i64
132 // isn't legal.
Chris Lattner01546c52005-07-30 00:05:54 +0000133 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
134 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
135 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
136 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Chris Lattner745d5382005-07-29 00:40:01 +0000137 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000138
Chris Lattner5d06b8c2005-07-29 01:00:29 +0000139 // Handle FP_TO_UINT by promoting the destination to a larger signed
140 // conversion.
141 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
142 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
143 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Nate Begeman875f3602005-08-14 04:36:51 +0000144
145 if (!X86ScalarSSE)
146 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Chris Lattner5d06b8c2005-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 Begeman875f3602005-08-14 04:36:51 +0000152 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
153
Chris Lattnerda4d4692005-04-09 03:22:37 +0000154 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Nate Begeman7cbd5252005-08-16 19:49:35 +0000155 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000156 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
157 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000158 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000159 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
160 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
161 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Chris Lattnerc610d422005-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 Lenharth691ef2b2005-05-03 17:19:30 +0000168 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
169 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +0000170 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Jeff Cohen00b168892005-07-27 06:12:32 +0000171
Chris Lattner4e6ce5f2005-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 Cohen00b168892005-07-27 06:12:32 +0000180
Chris Lattnerda2ce112005-01-16 07:34:08 +0000181 // These should be promoted to a larger select which is supported.
Nate Begemanf63be7d2005-07-06 18:59:04 +0000182 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000183 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Jeff Cohen00b168892005-07-27 06:12:32 +0000184
Nate Begemanf63be7d2005-07-06 18:59:04 +0000185 if (X86ScalarSSE) {
186 // Set up the FP register classes.
187 addRegisterClass(MVT::f32, X86::RXMMRegisterClass);
188 addRegisterClass(MVT::f64, X86::RXMMRegisterClass);
Jeff Cohen00b168892005-07-27 06:12:32 +0000189
Nate Begeman5a8441e2005-07-16 02:02:34 +0000190 // SSE has no load+extend ops
Nate Begemanf63be7d2005-07-06 18:59:04 +0000191 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
192 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
Nate Begeman5a8441e2005-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 Begeman1c73c7b2005-08-03 23:26:28 +0000196 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000197
Nate Begeman889f2c12005-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 Begemanf63be7d2005-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);
208 setOperationAction(ISD::SREM , MVT::f64, Expand);
209 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);
213 setOperationAction(ISD::SREM , MVT::f32, Expand);
Nate Begeman1c73c7b2005-08-03 23:26:28 +0000214
215 addLegalFPImmediate(+0.0); // xorps / xorpd
Nate Begemanf63be7d2005-07-06 18:59:04 +0000216 } else {
217 // Set up the FP register classes.
218 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Jeff Cohen00b168892005-07-27 06:12:32 +0000219
Nate Begemanf63be7d2005-07-06 18:59:04 +0000220 if (!UnsafeFPMath) {
221 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
222 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
223 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000224
Nate Begemanf63be7d2005-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 Lattner8acb1ba2005-01-07 07:49:41 +0000230 computeRegisterProperties();
Reid Spencera0f5bf32005-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
235 allowUnalignedStores = true; // x86 supports it!
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000236 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000237
Chris Lattner3648c672005-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 Lattner381e8872005-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 Lattner67649df2005-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 Lattner8acb1ba2005-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 Lattner5188ad72005-01-08 19:28:19 +0000258 virtual std::pair<SDOperand, SDOperand>
Jeff Cohen00b168892005-07-27 06:12:32 +0000259 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000260 bool isTailCall, SDOperand Callee, ArgListTy &Args,
261 SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000262
Chris Lattnere0fe2252005-07-05 19:58:54 +0000263 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
264 Value *VAListV, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000265 virtual std::pair<SDOperand,SDOperand>
Chris Lattnere0fe2252005-07-05 19:58:54 +0000266 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
267 const Type *ArgTy, SelectionDAG &DAG);
Jeff Cohen00b168892005-07-27 06:12:32 +0000268
Chris Lattner14824582005-01-09 00:01:27 +0000269 virtual std::pair<SDOperand, SDOperand>
270 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
271 SelectionDAG &DAG);
Chris Lattner381e8872005-05-15 05:46:45 +0000272
273 SDOperand getReturnAddressFrameIndex(SelectionDAG &DAG);
274
Chris Lattnerc6f41812005-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 Lattner2e7714a2005-05-13 20:29:13 +0000280 bool isTailCall,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000281 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Jeff Cohen00b168892005-07-27 06:12:32 +0000282
Chris Lattnerc6f41812005-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 Lattner2e7714a2005-05-13 20:29:13 +0000286 LowerFastCCCallTo(SDOperand Chain, const Type *RetTy, bool isTailCall,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000287 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000288 };
289}
290
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000291std::vector<SDOperand>
292X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattnerc6f41812005-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 Cohen00b168892005-07-27 06:12:32 +0000301 bool isTailCall,
Chris Lattnerc6f41812005-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 Lattner2e7714a2005-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 Lattnerc6f41812005-05-12 23:06:28 +0000309}
310
311//===----------------------------------------------------------------------===//
Chris Lattner653f7232005-05-13 22:46:57 +0000312// C Calling Convention implementation
Chris Lattnerc6f41812005-05-12 23:06:28 +0000313//===----------------------------------------------------------------------===//
314
315std::vector<SDOperand>
316X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000317 std::vector<SDOperand> ArgValues;
318
Chris Lattner6415bb42005-05-10 03:53:18 +0000319 MachineFunction &MF = DAG.getMachineFunction();
320 MachineFrameInfo *MFI = MF.getFrameInfo();
321
Chris Lattner8acb1ba2005-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 Brukman0e0a7a452005-04-21 23:38:14 +0000328 // ...
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000329 //
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000330 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattnere4d5c442005-03-15 04:54:21 +0000331 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner8acb1ba2005-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 Brukman0e0a7a452005-04-21 23:38:14 +0000347
Chris Lattner8acb1ba2005-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 Lattnera80d2bd2005-05-09 05:40:26 +0000355 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
356 DAG.getSrcValue(NULL));
Chris Lattner8acb1ba2005-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 Lattner3648c672005-05-13 21:44:04 +0000372 ReturnAddrIndex = 0; // No return address slot generated yet.
373 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattner381e8872005-05-15 05:46:45 +0000374 BytesCallerReserves = ArgOffset;
Chris Lattner4c52f0e2005-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 Lattner8acb1ba2005-01-07 07:49:41 +0000395 return ArgValues;
396}
397
Chris Lattner5188ad72005-01-08 19:28:19 +0000398std::pair<SDOperand, SDOperand>
Chris Lattnerc6f41812005-05-12 23:06:28 +0000399X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
Chris Lattner2e7714a2005-05-13 20:29:13 +0000400 bool isVarArg, bool isTailCall,
401 SDOperand Callee, ArgListTy &Args,
402 SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-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 Lattner16cd04d2005-05-12 23:24:06 +0000408 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattner5188ad72005-01-08 19:28:19 +0000409 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-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 Lattner16cd04d2005-05-12 23:24:06 +0000427 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattner5188ad72005-01-08 19:28:19 +0000428 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-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 Lattner707ebc52005-08-16 21:56:37 +0000432 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
433 X86::ESP, MVT::i32);
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000434 std::vector<SDOperand> Stores;
435
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000436 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner8acb1ba2005-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 Lattnerb62e1e22005-01-21 19:46:38 +0000455 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000456 Args[i].first, PtrOff,
457 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000458 ArgOffset += 4;
459 break;
460 case MVT::i64:
461 case MVT::f64:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000462 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000463 Args[i].first, PtrOff,
464 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000465 ArgOffset += 8;
466 break;
467 }
468 }
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000469 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000470 }
471
472 std::vector<MVT::ValueType> RetVals;
473 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000474 RetVals.push_back(MVT::Other);
475
Chris Lattner239738a2005-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 Begemanf63be7d2005-07-06 18:59:04 +0000488 if (X86ScalarSSE)
489 RetVals.push_back(MVT::f32);
490 else
491 RetVals.push_back(MVT::f64);
Chris Lattner239738a2005-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 Lattner8acb1ba2005-01-07 07:49:41 +0000530}
531
Chris Lattnere0fe2252005-07-05 19:58:54 +0000532SDOperand
533X86TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
534 Value *VAListV, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000535 // vastart just stores the address of the VarArgsFrameIndex slot.
536 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000537 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
538 DAG.getSrcValue(VAListV));
Chris Lattner14824582005-01-09 00:01:27 +0000539}
540
Chris Lattnere0fe2252005-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 Lattner14824582005-01-09 00:01:27 +0000546 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnere0fe2252005-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 Lattner08568cf2005-07-05 17:50:16 +0000550 DAG.getSrcValue(NULL));
Andrew Lenharth558bc882005-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 Lattner14824582005-01-09 00:01:27 +0000558 }
Andrew Lenharth558bc882005-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 Lattnere0fe2252005-07-05 19:58:54 +0000562 Val, VAListP, DAG.getSrcValue(VAListV));
Chris Lattner14824582005-01-09 00:01:27 +0000563 return std::make_pair(Result, Chain);
564}
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000565
Chris Lattnerc6f41812005-05-12 23:06:28 +0000566//===----------------------------------------------------------------------===//
Chris Lattner653f7232005-05-13 22:46:57 +0000567// Fast Calling Convention implementation
Chris Lattnerc6f41812005-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 Lattner10d26452005-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 Lattnerc6f41812005-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 Lattner63602fb2005-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 Lattnerc6f41812005-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 Cohen00b168892005-07-27 06:12:32 +0000621
Chris Lattnerc6f41812005-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 Lattner63602fb2005-05-13 07:38:09 +0000628 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
629 X86::R8RegisterClass);
Chris Lattner707ebc52005-08-16 21:56:37 +0000630 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
Chris Lattnerc6f41812005-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 Lattner63602fb2005-05-13 07:38:09 +0000642 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
643 X86::R16RegisterClass);
Chris Lattner707ebc52005-08-16 21:56:37 +0000644 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
Chris Lattnerc6f41812005-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 Lattner63602fb2005-05-13 07:38:09 +0000655 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
656 X86::R32RegisterClass);
Chris Lattner707ebc52005-08-16 21:56:37 +0000657 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattnerc6f41812005-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 Lattner63602fb2005-05-13 07:38:09 +0000668 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
669 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000670
Chris Lattner707ebc52005-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 Lattnerc6f41812005-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 Lattner63602fb2005-05-13 07:38:09 +0000681 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
Chris Lattner707ebc52005-08-16 21:56:37 +0000682 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
Chris Lattnerc6f41812005-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 Lattner10d26452005-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 Lattner3648c672005-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 Lattner381e8872005-05-15 05:46:45 +0000735 BytesCallerReserves = 0;
Chris Lattnerc6f41812005-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 Lattner2e7714a2005-05-13 20:29:13 +0000761 bool isTailCall, SDOperand Callee,
Chris Lattnerc6f41812005-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 Lattner10d26452005-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 Lattner16cd04d2005-05-12 23:24:06 +0000807 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattnerc6f41812005-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 Lattner707ebc52005-08-16 21:56:37 +0000812 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
813 X86::ESP, MVT::i32);
Chris Lattnerc6f41812005-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 Lattner920c0aa2005-05-14 12:03:10 +0000855 Hi, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattnerc6f41812005-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 Lattner10d26452005-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 Lattner239738a2005-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 Begemanf63be7d2005-07-06 18:59:04 +0000896 if (X86ScalarSSE)
897 RetVals.push_back(MVT::f32);
898 else
899 RetVals.push_back(MVT::f64);
Chris Lattner239738a2005-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 Lattnerc6f41812005-05-12 23:06:28 +0000944}
945
Chris Lattner381e8872005-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 Lattnerc6f41812005-05-12 23:06:28 +0000955
956
Chris Lattner14824582005-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 Lattner381e8872005-05-15 05:46:45 +0000965 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000966 if (!isFrameAddress)
967 // Just load the return address
Chris Lattnerc6f41812005-05-12 23:06:28 +0000968 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
969 DAG.getSrcValue(NULL));
Chris Lattner14824582005-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 Lattner8acb1ba2005-01-07 07:49:41 +0000976
Chris Lattnera28381c2005-07-16 00:28:20 +0000977//===----------------------------------------------------------------------===//
978// X86 Custom Lowering Hooks
979//===----------------------------------------------------------------------===//
980
Chris Lattner67649df2005-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 Lattner745d5382005-07-29 00:40:01 +0000986 case ISD::SINT_TO_FP: {
Chris Lattner67649df2005-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 Lattner745d5382005-07-29 00:40:01 +00001005 case ISD::FP_TO_SINT: {
Chris Lattner01546c52005-07-30 00:05:54 +00001006 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattner745d5382005-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 Lattner01546c52005-07-30 00:05:54 +00001012 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1013 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Chris Lattner745d5382005-07-29 00:40:01 +00001014 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1015
Chris Lattner01546c52005-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 Cohend29b6aa2005-07-30 18:33:25 +00001023
Chris Lattner01546c52005-07-30 00:05:54 +00001024 // Build the FP_TO_INT*_IN_MEM
Chris Lattner745d5382005-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 Lattner01546c52005-07-30 00:05:54 +00001029 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001030
Chris Lattner745d5382005-07-29 00:40:01 +00001031 // Load the result.
Chris Lattner01546c52005-07-30 00:05:54 +00001032 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1033 DAG.getSrcValue(NULL));
Chris Lattner745d5382005-07-29 00:40:01 +00001034 }
1035 }
Chris Lattner67649df2005-05-14 06:52:07 +00001036}
1037
1038
1039//===----------------------------------------------------------------------===//
1040// Pattern Matcher Implementation
1041//===----------------------------------------------------------------------===//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001042
Chris Lattner98a8ba02005-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 Brukman0e0a7a452005-04-21 23:38:14 +00001052
Chris Lattner98a8ba02005-01-18 01:06:26 +00001053 struct { // This is really a union, discriminated by BaseType!
1054 SDOperand Reg;
1055 int FrameIndex;
1056 } Base;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001057
Chris Lattner98a8ba02005-01-18 01:06:26 +00001058 unsigned Scale;
1059 SDOperand IndexReg;
1060 unsigned Disp;
1061 GlobalValue *GV;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001062
Chris Lattner98a8ba02005-01-18 01:06:26 +00001063 X86ISelAddressMode()
1064 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
1065 }
1066 };
1067}
Chris Lattner8acb1ba2005-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 Lattner11333092005-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 Lattner8acb1ba2005-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 Lattner8acb1ba2005-01-07 07:49:41 +00001095
Chris Lattner381e8872005-05-15 05:46:45 +00001096 /// TheDAG - The DAG being selected during Select* operations.
1097 SelectionDAG *TheDAG;
Jeff Cohen00b168892005-07-27 06:12:32 +00001098
1099 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
Nate Begemanfb5792f2005-07-12 01:41:54 +00001100 /// make the right decision when generating code for different targets.
1101 const X86Subtarget *Subtarget;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001102 public:
1103 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
Chris Lattner8c4a8732005-08-05 21:54:27 +00001104 Subtarget = &TM.getSubtarget<X86Subtarget>();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001105 }
1106
Chris Lattner67b1c3c2005-01-21 21:35:14 +00001107 virtual const char *getPassName() const {
1108 return "X86 Pattern Instruction Selection";
1109 }
1110
Chris Lattner11333092005-01-11 03:11:44 +00001111 unsigned getRegPressure(SDOperand O) {
1112 return RegPressureMap[O.Val];
1113 }
1114 unsigned ComputeRegPressure(SDOperand O);
1115
Chris Lattner8acb1ba2005-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 Lattner7dbcb752005-01-12 04:21:28 +00001118 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001119
Chris Lattner63602fb2005-05-13 07:38:09 +00001120 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
1121
Chris Lattner44129b52005-01-25 20:03:11 +00001122 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
1123 bool FloatPromoteOk = false);
Chris Lattnera5ade062005-01-11 21:19:59 +00001124 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +00001125 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattner30ea1e92005-01-19 07:37:26 +00001126 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001127 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +00001128 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Nate Begeman1c73c7b2005-08-03 23:26:28 +00001129 void EmitSelectCC(SDOperand Cond, SDOperand True, SDOperand False,
1130 MVT::ValueType SVT, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001131 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-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 Lattner381e8872005-05-15 05:46:45 +00001136 bool EmitPotentialTailCall(SDNode *Node);
1137 void EmitFastCCToFastCCTailCall(SDNode *TailCallNode);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001138 void Select(SDOperand N);
1139 };
1140}
1141
Chris Lattner6415bb42005-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 Lattner63602fb2005-05-13 07:38:09 +00001158void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
1159 // If this function has live-in values, emit the copies from pregs to vregs at
1160 // the top of the function, before anything else.
1161 MachineBasicBlock *BB = MF.begin();
1162 if (MF.livein_begin() != MF.livein_end()) {
1163 SSARegMap *RegMap = MF.getSSARegMap();
1164 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
1165 E = MF.livein_end(); LI != E; ++LI) {
1166 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
1167 if (RC == X86::R8RegisterClass) {
1168 BuildMI(BB, X86::MOV8rr, 1, LI->second).addReg(LI->first);
1169 } else if (RC == X86::R16RegisterClass) {
1170 BuildMI(BB, X86::MOV16rr, 1, LI->second).addReg(LI->first);
1171 } else if (RC == X86::R32RegisterClass) {
1172 BuildMI(BB, X86::MOV32rr, 1, LI->second).addReg(LI->first);
1173 } else if (RC == X86::RFPRegisterClass) {
1174 BuildMI(BB, X86::FpMOV, 1, LI->second).addReg(LI->first);
Nate Begemanf63be7d2005-07-06 18:59:04 +00001175 } else if (RC == X86::RXMMRegisterClass) {
1176 BuildMI(BB, X86::MOVAPDrr, 1, LI->second).addReg(LI->first);
Chris Lattner63602fb2005-05-13 07:38:09 +00001177 } else {
1178 assert(0 && "Unknown regclass!");
1179 }
1180 }
1181 }
1182
1183
1184 // If this is main, emit special code for main.
1185 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
1186 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
1187}
1188
1189
Chris Lattner7dbcb752005-01-12 04:21:28 +00001190/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
1191/// when it has created a SelectionDAG for us to codegen.
1192void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
1193 // While we're doing this, keep track of whether we see any FP code for
1194 // FP_REG_KILL insertion.
1195 ContainsFPCode = false;
Chris Lattner6415bb42005-05-10 03:53:18 +00001196 MachineFunction *MF = BB->getParent();
Chris Lattner7dbcb752005-01-12 04:21:28 +00001197
1198 // Scan the PHI nodes that already are inserted into this basic block. If any
1199 // of them is a PHI of a floating point value, we need to insert an
1200 // FP_REG_KILL.
Chris Lattner6415bb42005-05-10 03:53:18 +00001201 SSARegMap *RegMap = MF->getSSARegMap();
Chris Lattner63602fb2005-05-13 07:38:09 +00001202 if (BB != MF->begin())
1203 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
1204 I != E; ++I) {
1205 assert(I->getOpcode() == X86::PHI &&
1206 "Isn't just PHI nodes?");
1207 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
1208 X86::RFPRegisterClass) {
1209 ContainsFPCode = true;
1210 break;
1211 }
Chris Lattner7dbcb752005-01-12 04:21:28 +00001212 }
Chris Lattner6415bb42005-05-10 03:53:18 +00001213
Chris Lattner7dbcb752005-01-12 04:21:28 +00001214 // Compute the RegPressureMap, which is an approximation for the number of
1215 // registers required to compute each node.
1216 ComputeRegPressure(DAG.getRoot());
1217
Chris Lattner381e8872005-05-15 05:46:45 +00001218 TheDAG = &DAG;
1219
Chris Lattner7dbcb752005-01-12 04:21:28 +00001220 // Codegen the basic block.
1221 Select(DAG.getRoot());
1222
Chris Lattner381e8872005-05-15 05:46:45 +00001223 TheDAG = 0;
1224
Chris Lattner7dbcb752005-01-12 04:21:28 +00001225 // Finally, look at all of the successors of this block. If any contain a PHI
1226 // node of FP type, we need to insert an FP_REG_KILL in this block.
1227 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
1228 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
1229 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
1230 I != E && I->getOpcode() == X86::PHI; ++I) {
1231 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
1232 X86::RFPRegisterClass) {
1233 ContainsFPCode = true;
1234 break;
1235 }
1236 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001237
Chris Lattnere3e0f272005-05-09 03:36:39 +00001238 // Final check, check LLVM BB's that are successors to the LLVM BB
1239 // corresponding to BB for FP PHI nodes.
1240 const BasicBlock *LLVMBB = BB->getBasicBlock();
1241 const PHINode *PN;
1242 if (!ContainsFPCode)
1243 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
1244 SI != E && !ContainsFPCode; ++SI)
1245 for (BasicBlock::const_iterator II = SI->begin();
1246 (PN = dyn_cast<PHINode>(II)); ++II)
1247 if (PN->getType()->isFloatingPoint()) {
1248 ContainsFPCode = true;
1249 break;
1250 }
1251
1252
Chris Lattner7dbcb752005-01-12 04:21:28 +00001253 // Insert FP_REG_KILL instructions into basic blocks that need them. This
1254 // only occurs due to the floating point stackifier not being aggressive
1255 // enough to handle arbitrary global stackification.
1256 //
1257 // Currently we insert an FP_REG_KILL instruction into each block that uses or
1258 // defines a floating point virtual register.
1259 //
1260 // When the global register allocators (like linear scan) finally update live
1261 // variable analysis, we can keep floating point values in registers across
1262 // basic blocks. This will be a huge win, but we are waiting on the global
1263 // allocators before we can do this.
1264 //
Chris Lattner71df3f82005-03-30 01:10:00 +00001265 if (ContainsFPCode) {
Chris Lattner7dbcb752005-01-12 04:21:28 +00001266 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
1267 ++NumFPKill;
1268 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001269
Chris Lattner7dbcb752005-01-12 04:21:28 +00001270 // Clear state used for selection.
1271 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +00001272 RegPressureMap.clear();
1273}
1274
1275
Chris Lattner11333092005-01-11 03:11:44 +00001276// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
1277// for the number of registers required to compute each node. This is basically
1278// computing a generalized form of the Sethi-Ullman number for each node.
1279unsigned ISel::ComputeRegPressure(SDOperand O) {
1280 SDNode *N = O.Val;
1281 unsigned &Result = RegPressureMap[N];
1282 if (Result) return Result;
1283
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001284 // FIXME: Should operations like CALL (which clobber lots o regs) have a
1285 // higher fixed cost??
1286
Chris Lattnerc4b6a782005-01-11 22:29:12 +00001287 if (N->getNumOperands() == 0) {
1288 Result = 1;
1289 } else {
1290 unsigned MaxRegUse = 0;
1291 unsigned NumExtraMaxRegUsers = 0;
1292 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1293 unsigned Regs;
1294 if (N->getOperand(i).getOpcode() == ISD::Constant)
1295 Regs = 0;
1296 else
1297 Regs = ComputeRegPressure(N->getOperand(i));
1298 if (Regs > MaxRegUse) {
1299 MaxRegUse = Regs;
1300 NumExtraMaxRegUsers = 0;
1301 } else if (Regs == MaxRegUse &&
1302 N->getOperand(i).getValueType() != MVT::Other) {
1303 ++NumExtraMaxRegUsers;
1304 }
Chris Lattner11333092005-01-11 03:11:44 +00001305 }
Chris Lattner90d1be72005-01-17 22:56:09 +00001306
1307 if (O.getOpcode() != ISD::TokenFactor)
1308 Result = MaxRegUse+NumExtraMaxRegUsers;
1309 else
Chris Lattner869e0432005-01-17 23:02:13 +00001310 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +00001311 }
Chris Lattnerafce4302005-01-12 02:19:06 +00001312
Chris Lattner837caa72005-01-11 23:21:30 +00001313 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +00001314 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001315}
1316
Chris Lattnerbf52d492005-01-20 16:50:16 +00001317/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
1318/// The DAG cannot have cycles in it, by definition, so the visited set is not
1319/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
1320/// reuse, so it prevents exponential cases.
1321///
1322static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
1323 std::set<SDNode*> &Visited) {
1324 if (N == Op) return true; // Found it.
1325 SDNode *Node = N.Val;
Chris Lattnerfb0f53f2005-01-21 21:43:02 +00001326 if (Node->getNumOperands() == 0 || // Leaf?
1327 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattnerbf52d492005-01-20 16:50:16 +00001328 if (!Visited.insert(Node).second) return false; // Already visited?
1329
1330 // Recurse for the first N-1 operands.
1331 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1332 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
1333 return true;
1334
1335 // Tail recurse for the last operand.
1336 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
1337}
1338
Chris Lattner98a8ba02005-01-18 01:06:26 +00001339X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
1340 X86AddressMode Result;
1341
1342 // If we need to emit two register operands, emit the one with the highest
1343 // register pressure first.
1344 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
1345 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +00001346 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +00001347 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +00001348 std::set<SDNode*> Visited;
1349 EmitBaseThenIndex = true;
1350 // If Base ends up pointing to Index, we must emit index first. This is
1351 // because of the way we fold loads, we may end up doing bad things with
1352 // the folded add.
1353 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
1354 EmitBaseThenIndex = false;
1355 } else {
1356 std::set<SDNode*> Visited;
1357 EmitBaseThenIndex = false;
1358 // If Base ends up pointing to Index, we must emit index first. This is
1359 // because of the way we fold loads, we may end up doing bad things with
1360 // the folded add.
1361 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
1362 EmitBaseThenIndex = true;
1363 }
1364
1365 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001366 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1367 Result.IndexReg = SelectExpr(IAM.IndexReg);
1368 } else {
1369 Result.IndexReg = SelectExpr(IAM.IndexReg);
1370 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1371 }
Chris Lattnerbf52d492005-01-20 16:50:16 +00001372
Chris Lattner98a8ba02005-01-18 01:06:26 +00001373 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
1374 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1375 } else if (IAM.IndexReg.Val) {
1376 Result.IndexReg = SelectExpr(IAM.IndexReg);
1377 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001378
Chris Lattner98a8ba02005-01-18 01:06:26 +00001379 switch (IAM.BaseType) {
1380 case X86ISelAddressMode::RegBase:
1381 Result.BaseType = X86AddressMode::RegBase;
1382 break;
1383 case X86ISelAddressMode::FrameIndexBase:
1384 Result.BaseType = X86AddressMode::FrameIndexBase;
1385 Result.Base.FrameIndex = IAM.Base.FrameIndex;
1386 break;
1387 default:
1388 assert(0 && "Unknown base type!");
1389 break;
1390 }
1391 Result.Scale = IAM.Scale;
1392 Result.Disp = IAM.Disp;
1393 Result.GV = IAM.GV;
1394 return Result;
1395}
1396
1397/// SelectAddress - Pattern match the maximal addressing mode for this node and
1398/// emit all of the leaf registers.
1399void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
1400 X86ISelAddressMode IAM;
1401 MatchAddress(N, IAM);
1402 AM = SelectAddrExprs(IAM);
1403}
1404
1405/// MatchAddress - Add the specified node to the specified addressing mode,
1406/// returning true if it cannot be done. This just pattern matches for the
1407/// addressing mode, it does not cause any code to be emitted. For that, use
1408/// SelectAddress.
1409bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001410 switch (N.getOpcode()) {
1411 default: break;
1412 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +00001413 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
1414 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001415 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
1416 return false;
1417 }
1418 break;
1419 case ISD::GlobalAddress:
1420 if (AM.GV == 0) {
Nate Begemanfb5792f2005-07-12 01:41:54 +00001421 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1422 // For Darwin, external and weak symbols are indirect, so we want to load
1423 // the value at address GV, not the value of GV itself. This means that
1424 // the GlobalAddress must be in the base or index register of the address,
1425 // not the GV offset field.
Jeff Cohen00b168892005-07-27 06:12:32 +00001426 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemanfb5792f2005-07-12 01:41:54 +00001427 (GV->hasWeakLinkage() || GV->isExternal())) {
1428 break;
1429 } else {
1430 AM.GV = GV;
1431 return false;
1432 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001433 }
1434 break;
1435 case ISD::Constant:
1436 AM.Disp += cast<ConstantSDNode>(N)->getValue();
1437 return false;
1438 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +00001439 // We might have folded the load into this shift, so don't regen the value
1440 // if so.
1441 if (ExprMap.count(N)) break;
1442
Chris Lattner98a8ba02005-01-18 01:06:26 +00001443 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001444 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
1445 unsigned Val = CN->getValue();
1446 if (Val == 1 || Val == 2 || Val == 3) {
1447 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +00001448 SDOperand ShVal = N.Val->getOperand(0);
1449
1450 // Okay, we know that we have a scale by now. However, if the scaled
1451 // value is an add of something and a constant, we can fold the
1452 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +00001453 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +00001454 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001455 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +00001456 ConstantSDNode *AddVal =
1457 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
1458 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +00001459 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001460 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +00001461 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001462 return false;
1463 }
1464 }
1465 break;
Chris Lattner947d5442005-01-11 19:37:02 +00001466 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +00001467 // We might have folded the load into this mul, so don't regen the value if
1468 // so.
1469 if (ExprMap.count(N)) break;
1470
Chris Lattner947d5442005-01-11 19:37:02 +00001471 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +00001472 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
1473 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +00001474 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
1475 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
1476 AM.Scale = unsigned(CN->getValue())-1;
1477
1478 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001479 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +00001480
1481 // Okay, we know that we have a scale by now. However, if the scaled
1482 // value is an add of something and a constant, we can fold the
1483 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +00001484 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +00001485 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001486 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +00001487 ConstantSDNode *AddVal =
1488 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
1489 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001490 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001491 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +00001492 }
1493
1494 AM.IndexReg = AM.Base.Reg = Reg;
1495 return false;
1496 }
1497 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001498
1499 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +00001500 // We might have folded the load into this mul, so don't regen the value if
1501 // so.
1502 if (ExprMap.count(N)) break;
1503
Chris Lattner98a8ba02005-01-18 01:06:26 +00001504 X86ISelAddressMode Backup = AM;
1505 if (!MatchAddress(N.Val->getOperand(0), AM) &&
1506 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001507 return false;
1508 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +00001509 if (!MatchAddress(N.Val->getOperand(1), AM) &&
1510 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +00001511 return false;
1512 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001513 break;
1514 }
1515 }
1516
Chris Lattnera95589b2005-01-11 04:40:19 +00001517 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +00001518 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +00001519 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +00001520 if (AM.IndexReg.Val == 0) {
1521 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +00001522 AM.Scale = 1;
1523 return false;
1524 }
1525
1526 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001527 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +00001528 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001529
1530 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +00001531 AM.BaseType = X86ISelAddressMode::RegBase;
1532 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001533 return false;
1534}
1535
1536/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
1537/// assuming that the temporary registers are in the 8-bit register class.
1538///
1539/// Tmp1 = setcc1
1540/// Tmp2 = setcc2
1541/// DestReg = logicalop Tmp1, Tmp2
1542///
1543static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
1544 unsigned SetCC2, unsigned LogicalOp,
1545 unsigned DestReg) {
1546 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
1547 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1548 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1549 BuildMI(BB, SetCC1, 0, Tmp1);
1550 BuildMI(BB, SetCC2, 0, Tmp2);
1551 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
1552}
1553
1554/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
1555/// condition codes match the specified SetCCOpcode. Note that some conditions
1556/// require multiple instructions to generate the correct value.
1557static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
1558 ISD::CondCode SetCCOpcode, bool isFP) {
1559 unsigned Opc;
1560 if (!isFP) {
1561 switch (SetCCOpcode) {
1562 default: assert(0 && "Illegal integer SetCC!");
1563 case ISD::SETEQ: Opc = X86::SETEr; break;
1564 case ISD::SETGT: Opc = X86::SETGr; break;
1565 case ISD::SETGE: Opc = X86::SETGEr; break;
1566 case ISD::SETLT: Opc = X86::SETLr; break;
1567 case ISD::SETLE: Opc = X86::SETLEr; break;
1568 case ISD::SETNE: Opc = X86::SETNEr; break;
1569 case ISD::SETULT: Opc = X86::SETBr; break;
1570 case ISD::SETUGT: Opc = X86::SETAr; break;
1571 case ISD::SETULE: Opc = X86::SETBEr; break;
1572 case ISD::SETUGE: Opc = X86::SETAEr; break;
1573 }
1574 } else {
1575 // On a floating point condition, the flags are set as follows:
1576 // ZF PF CF op
1577 // 0 | 0 | 0 | X > Y
1578 // 0 | 0 | 1 | X < Y
1579 // 1 | 0 | 0 | X == Y
1580 // 1 | 1 | 1 | unordered
1581 //
1582 switch (SetCCOpcode) {
1583 default: assert(0 && "Invalid FP setcc!");
1584 case ISD::SETUEQ:
1585 case ISD::SETEQ:
1586 Opc = X86::SETEr; // True if ZF = 1
1587 break;
1588 case ISD::SETOGT:
1589 case ISD::SETGT:
1590 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
1591 break;
1592 case ISD::SETOGE:
1593 case ISD::SETGE:
1594 Opc = X86::SETAEr; // True if CF = 0
1595 break;
1596 case ISD::SETULT:
1597 case ISD::SETLT:
1598 Opc = X86::SETBr; // True if CF = 1
1599 break;
1600 case ISD::SETULE:
1601 case ISD::SETLE:
1602 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
1603 break;
1604 case ISD::SETONE:
1605 case ISD::SETNE:
1606 Opc = X86::SETNEr; // True if ZF = 0
1607 break;
1608 case ISD::SETUO:
1609 Opc = X86::SETPr; // True if PF = 1
1610 break;
1611 case ISD::SETO:
1612 Opc = X86::SETNPr; // True if PF = 0
1613 break;
1614 case ISD::SETOEQ: // !PF & ZF
1615 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
1616 return;
1617 case ISD::SETOLT: // !PF & CF
1618 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
1619 return;
1620 case ISD::SETOLE: // !PF & (CF || ZF)
1621 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
1622 return;
1623 case ISD::SETUGT: // PF | (!ZF & !CF)
1624 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
1625 return;
1626 case ISD::SETUGE: // PF | !CF
1627 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
1628 return;
1629 case ISD::SETUNE: // PF | !ZF
1630 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
1631 return;
1632 }
1633 }
1634 BuildMI(BB, Opc, 0, DestReg);
1635}
1636
1637
1638/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
1639/// the Dest block if the Cond condition is true. If we cannot fold this
1640/// condition into the branch, return true.
1641///
Chris Lattner6c07aee2005-01-11 04:06:27 +00001642bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
1643 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001644 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
1645 // B) using two conditional branches instead of one condbr, two setcc's, and
1646 // an or.
1647 if ((Cond.getOpcode() == ISD::OR ||
1648 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
1649 // And and or set the flags for us, so there is no need to emit a TST of the
1650 // result. It is only safe to do this if there is only a single use of the
1651 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +00001652 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001653 SelectExpr(Cond);
1654 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
1655 return false;
1656 }
1657
1658 // Codegen br not C -> JE.
1659 if (Cond.getOpcode() == ISD::XOR)
1660 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
1661 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +00001662 unsigned CondR;
1663 if (getRegPressure(Chain) > getRegPressure(Cond)) {
1664 Select(Chain);
1665 CondR = SelectExpr(Cond.Val->getOperand(0));
1666 } else {
1667 CondR = SelectExpr(Cond.Val->getOperand(0));
1668 Select(Chain);
1669 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001670 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
1671 BuildMI(BB, X86::JE, 1).addMBB(Dest);
1672 return false;
1673 }
1674
Chris Lattner88ac32c2005-08-09 20:21:10 +00001675 if (Cond.getOpcode() != ISD::SETCC)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001676 return true; // Can only handle simple setcc's so far.
Chris Lattner88ac32c2005-08-09 20:21:10 +00001677 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001678
1679 unsigned Opc;
1680
1681 // Handle integer conditions first.
Chris Lattner88ac32c2005-08-09 20:21:10 +00001682 if (MVT::isInteger(Cond.getOperand(0).getValueType())) {
1683 switch (CC) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001684 default: assert(0 && "Illegal integer SetCC!");
1685 case ISD::SETEQ: Opc = X86::JE; break;
1686 case ISD::SETGT: Opc = X86::JG; break;
1687 case ISD::SETGE: Opc = X86::JGE; break;
1688 case ISD::SETLT: Opc = X86::JL; break;
1689 case ISD::SETLE: Opc = X86::JLE; break;
1690 case ISD::SETNE: Opc = X86::JNE; break;
1691 case ISD::SETULT: Opc = X86::JB; break;
1692 case ISD::SETUGT: Opc = X86::JA; break;
1693 case ISD::SETULE: Opc = X86::JBE; break;
1694 case ISD::SETUGE: Opc = X86::JAE; break;
1695 }
Chris Lattner6c07aee2005-01-11 04:06:27 +00001696 Select(Chain);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001697 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001698 BuildMI(BB, Opc, 1).addMBB(Dest);
1699 return false;
1700 }
1701
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001702 unsigned Opc2 = 0; // Second branch if needed.
1703
1704 // On a floating point condition, the flags are set as follows:
1705 // ZF PF CF op
1706 // 0 | 0 | 0 | X > Y
1707 // 0 | 0 | 1 | X < Y
1708 // 1 | 0 | 0 | X == Y
1709 // 1 | 1 | 1 | unordered
1710 //
Chris Lattner88ac32c2005-08-09 20:21:10 +00001711 switch (CC) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001712 default: assert(0 && "Invalid FP setcc!");
1713 case ISD::SETUEQ:
1714 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
1715 case ISD::SETOGT:
1716 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
1717 case ISD::SETOGE:
1718 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
1719 case ISD::SETULT:
1720 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
1721 case ISD::SETULE:
1722 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
1723 case ISD::SETONE:
1724 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
1725 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
1726 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
1727 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1728 Opc = X86::JA; // ZF = 0 & CF = 0
1729 Opc2 = X86::JP; // PF = 1
1730 break;
1731 case ISD::SETUGE: // PF = 1 | CF = 0
1732 Opc = X86::JAE; // CF = 0
1733 Opc2 = X86::JP; // PF = 1
1734 break;
1735 case ISD::SETUNE: // PF = 1 | ZF = 0
1736 Opc = X86::JNE; // ZF = 0
1737 Opc2 = X86::JP; // PF = 1
1738 break;
1739 case ISD::SETOEQ: // PF = 0 & ZF = 1
1740 //X86::JNP, X86::JE
1741 //X86::AND8rr
1742 return true; // FIXME: Emit more efficient code for this branch.
1743 case ISD::SETOLT: // PF = 0 & CF = 1
1744 //X86::JNP, X86::JB
1745 //X86::AND8rr
1746 return true; // FIXME: Emit more efficient code for this branch.
1747 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1748 //X86::JNP, X86::JBE
1749 //X86::AND8rr
1750 return true; // FIXME: Emit more efficient code for this branch.
1751 }
1752
Chris Lattner6c07aee2005-01-11 04:06:27 +00001753 Select(Chain);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001754 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001755 BuildMI(BB, Opc, 1).addMBB(Dest);
1756 if (Opc2)
1757 BuildMI(BB, Opc2, 1).addMBB(Dest);
1758 return false;
1759}
1760
Chris Lattner24aad1b2005-01-10 22:10:13 +00001761/// EmitSelectCC - Emit code into BB that performs a select operation between
Nate Begeman1c73c7b2005-08-03 23:26:28 +00001762/// the two registers RTrue and RFalse, generating a result into RDest.
Chris Lattner24aad1b2005-01-10 22:10:13 +00001763///
Nate Begeman1c73c7b2005-08-03 23:26:28 +00001764void ISel::EmitSelectCC(SDOperand Cond, SDOperand True, SDOperand False,
1765 MVT::ValueType SVT, unsigned RDest) {
1766 unsigned RTrue, RFalse;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001767 enum Condition {
1768 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
1769 NOT_SET
1770 } CondCode = NOT_SET;
1771
1772 static const unsigned CMOVTAB16[] = {
1773 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
1774 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001775 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001776 };
1777 static const unsigned CMOVTAB32[] = {
1778 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
1779 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001780 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001781 };
1782 static const unsigned CMOVTABFP[] = {
1783 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
1784 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
1785 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
1786 };
Nate Begeman16b04f32005-07-15 00:38:55 +00001787 static const int SSE_CMOVTAB[] = {
Nate Begeman1c73c7b2005-08-03 23:26:28 +00001788 /*CMPEQ*/ 0, /*CMPNEQ*/ 4, /*missing*/ 0, /*missing*/ 0,
1789 /*missing*/ 0, /*missing*/ 0, /*CMPLT*/ 1, /*CMPLE*/ 2,
1790 /*CMPNLE*/ 6, /*CMPNLT*/ 5, /*CMPUNORD*/ 3, /*CMPORD*/ 7
Nate Begemanf63be7d2005-07-06 18:59:04 +00001791 };
Nate Begeman1c73c7b2005-08-03 23:26:28 +00001792
Chris Lattner88ac32c2005-08-09 20:21:10 +00001793 if (Cond.getOpcode() == ISD::SETCC) {
1794 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
1795 if (MVT::isInteger(Cond.getOperand(0).getValueType())) {
1796 switch (CC) {
Chris Lattner24aad1b2005-01-10 22:10:13 +00001797 default: assert(0 && "Unknown integer comparison!");
1798 case ISD::SETEQ: CondCode = EQ; break;
1799 case ISD::SETGT: CondCode = GT; break;
1800 case ISD::SETGE: CondCode = GE; break;
1801 case ISD::SETLT: CondCode = LT; break;
1802 case ISD::SETLE: CondCode = LE; break;
1803 case ISD::SETNE: CondCode = NE; break;
1804 case ISD::SETULT: CondCode = B; break;
1805 case ISD::SETUGT: CondCode = A; break;
1806 case ISD::SETULE: CondCode = BE; break;
1807 case ISD::SETUGE: CondCode = AE; break;
1808 }
1809 } else {
1810 // On a floating point condition, the flags are set as follows:
1811 // ZF PF CF op
1812 // 0 | 0 | 0 | X > Y
1813 // 0 | 0 | 1 | X < Y
1814 // 1 | 0 | 0 | X == Y
1815 // 1 | 1 | 1 | unordered
1816 //
Chris Lattner88ac32c2005-08-09 20:21:10 +00001817 switch (CC) {
Chris Lattner24aad1b2005-01-10 22:10:13 +00001818 default: assert(0 && "Unknown FP comparison!");
1819 case ISD::SETUEQ:
1820 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
1821 case ISD::SETOGT:
1822 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
1823 case ISD::SETOGE:
1824 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
1825 case ISD::SETULT:
1826 case ISD::SETLT: CondCode = B; break; // True if CF = 1
1827 case ISD::SETULE:
1828 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
1829 case ISD::SETONE:
1830 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
1831 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1832 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1833 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1834 case ISD::SETUGE: // PF = 1 | CF = 0
1835 case ISD::SETUNE: // PF = 1 | ZF = 0
1836 case ISD::SETOEQ: // PF = 0 & ZF = 1
1837 case ISD::SETOLT: // PF = 0 & CF = 1
1838 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1839 // We cannot emit this comparison as a single cmov.
1840 break;
1841 }
1842 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001843
Chris Lattner24aad1b2005-01-10 22:10:13 +00001844
Chris Lattner88ac32c2005-08-09 20:21:10 +00001845 // There's no SSE equivalent of FCMOVE. For cases where we set a condition
1846 // code above and one of the results of the select is +0.0, then we can fake
1847 // it up through a clever AND with mask. Otherwise, we will fall through to
1848 // the code below that will use a PHI node to select the right value.
1849 if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) {
1850 if (Cond.getOperand(0).getValueType() == SVT &&
1851 NOT_SET != CondCode) {
1852 ConstantFPSDNode *CT = dyn_cast<ConstantFPSDNode>(True);
1853 ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(False);
1854 bool TrueZero = CT && CT->isExactlyValue(0.0);
1855 bool FalseZero = CF && CF->isExactlyValue(0.0);
1856 if (TrueZero || FalseZero) {
1857 SDOperand LHS = Cond.getOperand(0);
1858 SDOperand RHS = Cond.getOperand(1);
1859
1860 // Select the two halves of the condition
1861 unsigned RLHS, RRHS;
1862 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1863 RLHS = SelectExpr(LHS);
1864 RRHS = SelectExpr(RHS);
1865 } else {
1866 RRHS = SelectExpr(RHS);
1867 RLHS = SelectExpr(LHS);
1868 }
1869
1870 // Emit the comparison and generate a mask from it
1871 unsigned MaskReg = MakeReg(SVT);
1872 unsigned Opc = (SVT == MVT::f32) ? X86::CMPSSrr : X86::CMPSDrr;
1873 BuildMI(BB, Opc, 3, MaskReg).addReg(RLHS).addReg(RRHS)
1874 .addImm(SSE_CMOVTAB[CondCode]);
1875
1876 if (TrueZero) {
1877 RFalse = SelectExpr(False);
1878 Opc = (SVT == MVT::f32) ? X86::ANDNPSrr : X86::ANDNPDrr;
1879 BuildMI(BB, Opc, 2, RDest).addReg(MaskReg).addReg(RFalse);
1880 } else {
1881 RTrue = SelectExpr(True);
1882 Opc = (SVT == MVT::f32) ? X86::ANDPSrr : X86::ANDPDrr;
1883 BuildMI(BB, Opc, 2, RDest).addReg(MaskReg).addReg(RTrue);
1884 }
1885 return;
Nate Begeman1c73c7b2005-08-03 23:26:28 +00001886 }
Nate Begeman1c73c7b2005-08-03 23:26:28 +00001887 }
Nate Begemanf63be7d2005-07-06 18:59:04 +00001888 }
Nate Begeman1c73c7b2005-08-03 23:26:28 +00001889 }
1890
1891 // Select the true and false values for use in both the SSE PHI case, and the
1892 // integer or x87 cmov cases below.
1893 if (getRegPressure(True) > getRegPressure(False)) {
1894 RTrue = SelectExpr(True);
1895 RFalse = SelectExpr(False);
1896 } else {
1897 RFalse = SelectExpr(False);
1898 RTrue = SelectExpr(True);
1899 }
1900
1901 // Since there's no SSE equivalent of FCMOVE, and we couldn't generate an
1902 // AND with mask, we'll have to do the normal RISC thing and generate a PHI
1903 // node to select between the true and false values.
1904 if (X86ScalarSSE && (SVT == MVT::f32 || SVT == MVT::f64)) {
1905 // FIXME: emit a direct compare and branch rather than setting a cond reg
1906 // and testing it.
1907 unsigned CondReg = SelectExpr(Cond);
1908 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1909
1910 // Create an iterator with which to insert the MBB for copying the false
1911 // value and the MBB to hold the PHI instruction for this SetCC.
1912 MachineBasicBlock *thisMBB = BB;
1913 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1914 ilist<MachineBasicBlock>::iterator It = BB;
1915 ++It;
1916
1917 // thisMBB:
1918 // ...
1919 // TrueVal = ...
1920 // cmpTY ccX, r1, r2
1921 // bCC sinkMBB
1922 // fallthrough --> copy0MBB
1923 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1924 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1925 BuildMI(BB, X86::JNE, 1).addMBB(sinkMBB);
1926 MachineFunction *F = BB->getParent();
1927 F->getBasicBlockList().insert(It, copy0MBB);
1928 F->getBasicBlockList().insert(It, sinkMBB);
1929 // Update machine-CFG edges
1930 BB->addSuccessor(copy0MBB);
1931 BB->addSuccessor(sinkMBB);
1932
1933 // copy0MBB:
1934 // %FalseValue = ...
1935 // # fallthrough to sinkMBB
1936 BB = copy0MBB;
1937 // Update machine-CFG edges
1938 BB->addSuccessor(sinkMBB);
1939
1940 // sinkMBB:
1941 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1942 // ...
1943 BB = sinkMBB;
1944 BuildMI(BB, X86::PHI, 4, RDest).addReg(RFalse)
1945 .addMBB(copy0MBB).addReg(RTrue).addMBB(thisMBB);
Nate Begemanf63be7d2005-07-06 18:59:04 +00001946 return;
1947 }
1948
Chris Lattner24aad1b2005-01-10 22:10:13 +00001949 unsigned Opc = 0;
1950 if (CondCode != NOT_SET) {
1951 switch (SVT) {
1952 default: assert(0 && "Cannot select this type!");
1953 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1954 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001955 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001956 }
1957 }
Jeff Cohen00b168892005-07-27 06:12:32 +00001958
Chris Lattner24aad1b2005-01-10 22:10:13 +00001959 // Finally, if we weren't able to fold this, just emit the condition and test
1960 // it.
1961 if (CondCode == NOT_SET || Opc == 0) {
1962 // Get the condition into the zero flag.
1963 unsigned CondReg = SelectExpr(Cond);
1964 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1965
1966 switch (SVT) {
1967 default: assert(0 && "Cannot select this type!");
1968 case MVT::i16: Opc = X86::CMOVE16rr; break;
1969 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001970 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001971 }
1972 } else {
1973 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001974 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001975 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001976 }
1977 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1978}
1979
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001980void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001981 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001982 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1983 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001984 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001985 switch (RHS.getValueType()) {
1986 default: break;
1987 case MVT::i1:
1988 case MVT::i8: Opc = X86::CMP8mi; break;
1989 case MVT::i16: Opc = X86::CMP16mi; break;
1990 case MVT::i32: Opc = X86::CMP32mi; break;
1991 }
1992 if (Opc) {
1993 X86AddressMode AM;
1994 EmitFoldedLoad(LHS, AM);
1995 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1996 return;
1997 }
1998 }
1999
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002000 switch (RHS.getValueType()) {
2001 default: break;
2002 case MVT::i1:
2003 case MVT::i8: Opc = X86::CMP8ri; break;
2004 case MVT::i16: Opc = X86::CMP16ri; break;
2005 case MVT::i32: Opc = X86::CMP32ri; break;
2006 }
2007 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00002008 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002009 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
2010 return;
2011 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00002012 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
Nate Begemanf63be7d2005-07-06 18:59:04 +00002013 if (!X86ScalarSSE && (CN->isExactlyValue(+0.0) ||
2014 CN->isExactlyValue(-0.0))) {
Chris Lattner7f2afac2005-01-14 22:37:41 +00002015 unsigned Reg = SelectExpr(LHS);
2016 BuildMI(BB, X86::FTST, 1).addReg(Reg);
2017 BuildMI(BB, X86::FNSTSW8r, 0);
2018 BuildMI(BB, X86::SAHF, 1);
Chris Lattner7805fa42005-03-17 16:29:26 +00002019 return;
Chris Lattner7f2afac2005-01-14 22:37:41 +00002020 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002021 }
2022
Chris Lattneref6806c2005-01-12 02:02:48 +00002023 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00002024 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00002025 switch (RHS.getValueType()) {
2026 default: break;
2027 case MVT::i1:
2028 case MVT::i8: Opc = X86::CMP8mr; break;
2029 case MVT::i16: Opc = X86::CMP16mr; break;
2030 case MVT::i32: Opc = X86::CMP32mr; break;
2031 }
2032 if (Opc) {
2033 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002034 EmitFoldedLoad(LHS, AM);
2035 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00002036 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
2037 return;
2038 }
2039 }
2040
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002041 switch (LHS.getValueType()) {
2042 default: assert(0 && "Cannot compare this value!");
2043 case MVT::i1:
2044 case MVT::i8: Opc = X86::CMP8rr; break;
2045 case MVT::i16: Opc = X86::CMP16rr; break;
2046 case MVT::i32: Opc = X86::CMP32rr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002047 case MVT::f32: Opc = X86::UCOMISSrr; break;
2048 case MVT::f64: Opc = X86ScalarSSE ? X86::UCOMISDrr : X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002049 }
Chris Lattner11333092005-01-11 03:11:44 +00002050 unsigned Tmp1, Tmp2;
2051 if (getRegPressure(LHS) > getRegPressure(RHS)) {
2052 Tmp1 = SelectExpr(LHS);
2053 Tmp2 = SelectExpr(RHS);
2054 } else {
2055 Tmp2 = SelectExpr(RHS);
2056 Tmp1 = SelectExpr(LHS);
2057 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002058 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
2059}
2060
Chris Lattnera5ade062005-01-11 21:19:59 +00002061/// isFoldableLoad - Return true if this is a load instruction that can safely
2062/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00002063bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
2064 if (Op.getOpcode() == ISD::LOAD) {
2065 // FIXME: currently can't fold constant pool indexes.
2066 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
2067 return false;
2068 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
Chris Lattnerbce81ae2005-07-10 01:56:13 +00002069 cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::f32) {
Chris Lattner44129b52005-01-25 20:03:11 +00002070 // FIXME: currently can't fold constant pool indexes.
2071 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
2072 return false;
2073 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00002074 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00002075 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002076
2077 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00002078 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
2079 if (ExprMap.count(Op.getValue(1))) return false;
2080 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00002081 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00002082
Chris Lattner4ff348b2005-01-17 06:26:58 +00002083 // If there is not just one use of its value, we cannot fold.
2084 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
2085
2086 // Finally, we cannot fold the load into the operation if this would induce a
2087 // cycle into the resultant dag. To check for this, see if OtherOp (the other
2088 // operand of the operation we are folding the load into) can possible use the
2089 // chain node defined by the load.
2090 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
2091 std::set<SDNode*> Visited;
2092 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
2093 return false;
2094 }
2095 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00002096}
2097
Chris Lattner4ff348b2005-01-17 06:26:58 +00002098
Chris Lattnera5ade062005-01-11 21:19:59 +00002099/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
2100/// and compute the address being loaded into AM.
2101void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
2102 SDOperand Chain = Op.getOperand(0);
2103 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002104
Chris Lattnera5ade062005-01-11 21:19:59 +00002105 if (getRegPressure(Chain) > getRegPressure(Address)) {
2106 Select(Chain);
2107 SelectAddress(Address, AM);
2108 } else {
2109 SelectAddress(Address, AM);
2110 Select(Chain);
2111 }
2112
2113 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00002114 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
2115 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00002116 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00002117 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002118}
2119
Chris Lattner30ea1e92005-01-19 07:37:26 +00002120// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
2121// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
2122// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
2123// return true.
2124bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00002125 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
2126 // good!
2127 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
2128 std::swap(Op1, Op2); // Op1 is the SHL now.
2129 } else {
2130 return false; // No match
2131 }
2132
2133 SDOperand ShlVal = Op1.getOperand(0);
2134 SDOperand ShlAmt = Op1.getOperand(1);
2135 SDOperand ShrVal = Op2.getOperand(0);
2136 SDOperand ShrAmt = Op2.getOperand(1);
2137
Chris Lattner30ea1e92005-01-19 07:37:26 +00002138 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
2139
Chris Lattner85716372005-01-19 06:18:43 +00002140 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
2141 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
2142 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00002143 if (SubCST->getValue() == RegSize) {
2144 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00002145 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00002146 if (ShrVal == ShlVal) {
2147 unsigned Reg, ShAmt;
2148 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
2149 Reg = SelectExpr(ShrVal);
2150 ShAmt = SelectExpr(ShrAmt);
2151 } else {
2152 ShAmt = SelectExpr(ShrAmt);
2153 Reg = SelectExpr(ShrVal);
2154 }
2155 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2156 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
2157 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
2158 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
2159 return true;
2160 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00002161 unsigned AReg, BReg;
2162 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00002163 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002164 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00002165 } else {
Chris Lattner85716372005-01-19 06:18:43 +00002166 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002167 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00002168 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00002169 unsigned ShAmt = SelectExpr(ShrAmt);
2170 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2171 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
2172 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00002173 return true;
2174 }
2175 }
2176
Chris Lattner4053b1e2005-01-19 08:07:05 +00002177 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
2178 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
2179 if (SubCST->getValue() == RegSize) {
2180 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
2181 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
2182 if (ShrVal == ShlVal) {
2183 unsigned Reg, ShAmt;
2184 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
2185 Reg = SelectExpr(ShrVal);
2186 ShAmt = SelectExpr(ShlAmt);
2187 } else {
2188 ShAmt = SelectExpr(ShlAmt);
2189 Reg = SelectExpr(ShrVal);
2190 }
2191 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2192 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
2193 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
2194 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
2195 return true;
2196 } else if (RegSize != 8) {
2197 unsigned AReg, BReg;
2198 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002199 AReg = SelectExpr(ShlVal);
2200 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002201 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002202 BReg = SelectExpr(ShrVal);
2203 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002204 }
2205 unsigned ShAmt = SelectExpr(ShlAmt);
2206 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
2207 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
2208 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
2209 return true;
2210 }
2211 }
Chris Lattner85716372005-01-19 06:18:43 +00002212
Chris Lattner4053b1e2005-01-19 08:07:05 +00002213 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
2214 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
2215 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
2216 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
2217 // (A >> 5) | (A << 27) --> ROR A, 5
2218 // (A >> 5) | (B << 27) --> SHRD A, B, 5
2219 if (ShrVal == ShlVal) {
2220 unsigned Reg = SelectExpr(ShrVal);
2221 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
2222 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
2223 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
2224 return true;
2225 } else if (RegSize != 8) {
2226 unsigned AReg, BReg;
2227 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00002228 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002229 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002230 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00002231 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00002232 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00002233 }
2234 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
2235 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
2236 .addImm(ShrCst->getValue());
2237 return true;
2238 }
2239 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002240
Chris Lattner85716372005-01-19 06:18:43 +00002241 return false;
2242}
2243
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002244unsigned ISel::SelectExpr(SDOperand N) {
2245 unsigned Result;
2246 unsigned Tmp1, Tmp2, Tmp3;
2247 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00002248 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00002249 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00002250
Chris Lattner7f2afac2005-01-14 22:37:41 +00002251 if (Node->getOpcode() == ISD::CopyFromReg) {
Chris Lattner707ebc52005-08-16 21:56:37 +00002252 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
2253 // Just use the specified register as our input if we can.
2254 if (MRegisterInfo::isVirtualRegister(Reg) || Reg == X86::ESP)
2255 return Reg;
Chris Lattner7f2afac2005-01-14 22:37:41 +00002256 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002257
Chris Lattnera5ade062005-01-11 21:19:59 +00002258 unsigned &Reg = ExprMap[N];
2259 if (Reg) return Reg;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002260
Chris Lattnerb38a7492005-04-02 04:01:14 +00002261 switch (N.getOpcode()) {
2262 default:
Chris Lattnera5ade062005-01-11 21:19:59 +00002263 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnerb38a7492005-04-02 04:01:14 +00002264 MakeReg(N.getValueType()) : 1;
2265 break;
Chris Lattner239738a2005-05-14 08:48:15 +00002266 case X86ISD::TAILCALL:
2267 case X86ISD::CALL:
Chris Lattnera5ade062005-01-11 21:19:59 +00002268 // If this is a call instruction, make sure to prepare ALL of the result
2269 // values as well as the chain.
Chris Lattner239738a2005-05-14 08:48:15 +00002270 ExprMap[N.getValue(0)] = 1;
2271 if (Node->getNumValues() > 1) {
2272 Result = MakeReg(Node->getValueType(1));
2273 ExprMap[N.getValue(1)] = Result;
2274 for (unsigned i = 2, e = Node->getNumValues(); i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00002275 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattner239738a2005-05-14 08:48:15 +00002276 } else {
2277 Result = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002278 }
Chris Lattnerb38a7492005-04-02 04:01:14 +00002279 break;
2280 case ISD::ADD_PARTS:
2281 case ISD::SUB_PARTS:
2282 case ISD::SHL_PARTS:
2283 case ISD::SRL_PARTS:
2284 case ISD::SRA_PARTS:
2285 Result = MakeReg(Node->getValueType(0));
2286 ExprMap[N.getValue(0)] = Result;
2287 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
2288 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
2289 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002290 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002291
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002292 switch (N.getOpcode()) {
2293 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00002294 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002295 assert(0 && "Node not handled!\n");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002296 case ISD::FP_EXTEND:
Jeff Cohen00b168892005-07-27 06:12:32 +00002297 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002298 Tmp1 = SelectExpr(N.getOperand(0));
2299 BuildMI(BB, X86::CVTSS2SDrr, 1, Result).addReg(Tmp1);
2300 return Result;
Nate Begeman16b04f32005-07-15 00:38:55 +00002301 case ISD::FP_ROUND:
Jeff Cohen00b168892005-07-27 06:12:32 +00002302 assert(X86ScalarSSE && "Scalar SSE FP must be enabled to use f32");
Nate Begeman16b04f32005-07-15 00:38:55 +00002303 Tmp1 = SelectExpr(N.getOperand(0));
2304 BuildMI(BB, X86::CVTSD2SSrr, 1, Result).addReg(Tmp1);
2305 return Result;
Chris Lattnerc6f41812005-05-12 23:06:28 +00002306 case ISD::CopyFromReg:
2307 Select(N.getOperand(0));
2308 if (Result == 1) {
2309 Reg = Result = ExprMap[N.getValue(0)] =
2310 MakeReg(N.getValue(0).getValueType());
2311 }
Chris Lattner707ebc52005-08-16 21:56:37 +00002312 Tmp1 = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattnerc6f41812005-05-12 23:06:28 +00002313 switch (Node->getValueType(0)) {
2314 default: assert(0 && "Cannot CopyFromReg this!");
2315 case MVT::i1:
2316 case MVT::i8:
Chris Lattner707ebc52005-08-16 21:56:37 +00002317 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
Chris Lattnerc6f41812005-05-12 23:06:28 +00002318 return Result;
2319 case MVT::i16:
Chris Lattner707ebc52005-08-16 21:56:37 +00002320 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(Tmp1);
Chris Lattnerc6f41812005-05-12 23:06:28 +00002321 return Result;
2322 case MVT::i32:
Chris Lattner707ebc52005-08-16 21:56:37 +00002323 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(Tmp1);
Chris Lattnerc6f41812005-05-12 23:06:28 +00002324 return Result;
Jeff Cohen00b168892005-07-27 06:12:32 +00002325 }
Chris Lattnerc6f41812005-05-12 23:06:28 +00002326
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002327 case ISD::FrameIndex:
2328 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
2329 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
2330 return Result;
2331 case ISD::ConstantPool:
Chris Lattner5839bf22005-08-26 17:15:30 +00002332 Tmp1 = BB->getParent()->getConstantPool()->
2333 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002334 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
2335 return Result;
2336 case ISD::ConstantFP:
Nate Begeman1c73c7b2005-08-03 23:26:28 +00002337 if (X86ScalarSSE) {
2338 assert(cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) &&
2339 "SSE only supports +0.0");
2340 Opc = (N.getValueType() == MVT::f32) ? X86::FLD0SS : X86::FLD0SD;
2341 BuildMI(BB, Opc, 0, Result);
2342 return Result;
2343 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002344 ContainsFPCode = true;
2345 Tmp1 = Result; // Intermediate Register
2346 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
2347 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
2348 Tmp1 = MakeReg(MVT::f64);
2349
2350 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
2351 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
2352 BuildMI(BB, X86::FLD0, 0, Tmp1);
2353 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
2354 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
2355 BuildMI(BB, X86::FLD1, 0, Tmp1);
2356 else
2357 assert(0 && "Unexpected constant!");
2358 if (Tmp1 != Result)
2359 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
2360 return Result;
2361 case ISD::Constant:
2362 switch (N.getValueType()) {
2363 default: assert(0 && "Cannot use constants of this type!");
2364 case MVT::i1:
2365 case MVT::i8: Opc = X86::MOV8ri; break;
2366 case MVT::i16: Opc = X86::MOV16ri; break;
2367 case MVT::i32: Opc = X86::MOV32ri; break;
2368 }
2369 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
2370 return Result;
Chris Lattner7ce7eff2005-04-01 22:46:45 +00002371 case ISD::UNDEF:
2372 if (Node->getValueType(0) == MVT::f64) {
2373 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
2374 BuildMI(BB, X86::FLD0, 0, Result);
2375 } else {
2376 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
2377 }
2378 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002379 case ISD::GlobalAddress: {
2380 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanfb5792f2005-07-12 01:41:54 +00002381 // For Darwin, external and weak symbols are indirect, so we want to load
2382 // the value at address GV, not the value of GV itself.
Jeff Cohen00b168892005-07-27 06:12:32 +00002383 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begemanfb5792f2005-07-12 01:41:54 +00002384 (GV->hasWeakLinkage() || GV->isExternal())) {
2385 BuildMI(BB, X86::MOV32rm, 4, Result).addReg(0).addZImm(1).addReg(0)
2386 .addGlobalAddress(GV, false, 0);
2387 } else {
2388 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
2389 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002390 return Result;
2391 }
2392 case ISD::ExternalSymbol: {
2393 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
2394 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
2395 return Result;
2396 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002397 case ISD::ZERO_EXTEND: {
2398 int DestIs16 = N.getValueType() == MVT::i16;
2399 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00002400
2401 // FIXME: This hack is here for zero extension casts from bool to i8. This
2402 // would not be needed if bools were promoted by Legalize.
2403 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002404 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00002405 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
2406 return Result;
2407 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002408
Chris Lattner4ff348b2005-01-17 06:26:58 +00002409 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002410 static const unsigned Opc[3] = {
2411 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
2412 };
2413
2414 X86AddressMode AM;
2415 EmitFoldedLoad(N.getOperand(0), AM);
2416 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002417
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002418 return Result;
2419 }
2420
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002421 static const unsigned Opc[3] = {
2422 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
2423 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002424 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002425 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
2426 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002427 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002428 case ISD::SIGN_EXTEND: {
2429 int DestIs16 = N.getValueType() == MVT::i16;
2430 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
2431
Chris Lattner590d8002005-01-09 18:52:44 +00002432 // FIXME: Legalize should promote bools to i8!
2433 assert(N.getOperand(0).getValueType() != MVT::i1 &&
2434 "Sign extend from bool not implemented!");
2435
Chris Lattner4ff348b2005-01-17 06:26:58 +00002436 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002437 static const unsigned Opc[3] = {
2438 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
2439 };
2440
2441 X86AddressMode AM;
2442 EmitFoldedLoad(N.getOperand(0), AM);
2443 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
2444 return Result;
2445 }
2446
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002447 static const unsigned Opc[3] = {
2448 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
2449 };
2450 Tmp1 = SelectExpr(N.getOperand(0));
2451 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
2452 return Result;
2453 }
2454 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00002455 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00002456 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00002457 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00002458 switch (N.getValueType()) {
2459 default: assert(0 && "Unknown truncate!");
2460 case MVT::i1:
2461 case MVT::i8: Opc = X86::MOV8rm; break;
2462 case MVT::i16: Opc = X86::MOV16rm; break;
2463 }
2464 X86AddressMode AM;
2465 EmitFoldedLoad(N.getOperand(0), AM);
2466 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2467 return Result;
2468 }
2469
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002470 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
2471 // a move out of AX or AL.
2472 switch (N.getOperand(0).getValueType()) {
2473 default: assert(0 && "Unknown truncate!");
2474 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
2475 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
2476 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
2477 }
2478 Tmp1 = SelectExpr(N.getOperand(0));
2479 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2480
2481 switch (N.getValueType()) {
2482 default: assert(0 && "Unknown truncate!");
2483 case MVT::i1:
2484 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
2485 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
2486 }
2487 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
2488 return Result;
2489
Chris Lattnera28381c2005-07-16 00:28:20 +00002490 case ISD::SINT_TO_FP: {
Nate Begemanf63be7d2005-07-06 18:59:04 +00002491 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2492 unsigned PromoteOpcode = 0;
2493
Nate Begeman5a8441e2005-07-16 02:02:34 +00002494 // We can handle any sint to fp with the direct sse conversion instructions.
Nate Begemanf63be7d2005-07-06 18:59:04 +00002495 if (X86ScalarSSE) {
Nate Begeman5a8441e2005-07-16 02:02:34 +00002496 Opc = (N.getValueType() == MVT::f64) ? X86::CVTSI2SDrr : X86::CVTSI2SSrr;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002497 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2498 return Result;
2499 }
Jeff Cohen00b168892005-07-27 06:12:32 +00002500
Chris Lattneref7ba072005-01-11 03:50:45 +00002501 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00002502
Chris Lattner590d8002005-01-09 18:52:44 +00002503 // Spill the integer to memory and reload it from there.
Nate Begeman5a8441e2005-07-16 02:02:34 +00002504 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
Chris Lattner590d8002005-01-09 18:52:44 +00002505 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
2506 MachineFunction *F = BB->getParent();
2507 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2508
2509 switch (SrcTy) {
Chris Lattner590d8002005-01-09 18:52:44 +00002510 case MVT::i32:
Chris Lattnera28381c2005-07-16 00:28:20 +00002511 addFrameReference(BuildMI(BB, X86::MOV32mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00002512 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
2513 break;
2514 case MVT::i16:
Chris Lattnera28381c2005-07-16 00:28:20 +00002515 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00002516 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
2517 break;
2518 default: break; // No promotion required.
2519 }
Chris Lattnera28381c2005-07-16 00:28:20 +00002520 return Result;
Chris Lattner590d8002005-01-09 18:52:44 +00002521 }
Chris Lattner01546c52005-07-30 00:05:54 +00002522 case ISD::FP_TO_SINT:
Chris Lattner590d8002005-01-09 18:52:44 +00002523 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2524
Nate Begemanf63be7d2005-07-06 18:59:04 +00002525 // If the target supports SSE2 and is performing FP operations in SSE regs
2526 // instead of the FP stack, then we can use the efficient CVTSS2SI and
2527 // CVTSD2SI instructions.
Chris Lattner01546c52005-07-30 00:05:54 +00002528 assert(X86ScalarSSE);
2529 if (MVT::f32 == N.getOperand(0).getValueType()) {
2530 BuildMI(BB, X86::CVTTSS2SIrr, 1, Result).addReg(Tmp1);
2531 } else if (MVT::f64 == N.getOperand(0).getValueType()) {
2532 BuildMI(BB, X86::CVTTSD2SIrr, 1, Result).addReg(Tmp1);
2533 } else {
2534 assert(0 && "Not an f32 or f64?");
2535 abort();
Jeff Cohen00b168892005-07-27 06:12:32 +00002536 }
Chris Lattner590d8002005-01-09 18:52:44 +00002537 return Result;
Chris Lattner01546c52005-07-30 00:05:54 +00002538
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002539 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00002540 Op0 = N.getOperand(0);
2541 Op1 = N.getOperand(1);
2542
Chris Lattner44129b52005-01-25 20:03:11 +00002543 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002544 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002545 goto FoldAdd;
2546 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002547
Chris Lattner44129b52005-01-25 20:03:11 +00002548 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002549 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00002550 switch (N.getValueType()) {
2551 default: assert(0 && "Cannot add this type!");
2552 case MVT::i1:
2553 case MVT::i8: Opc = X86::ADD8rm; break;
2554 case MVT::i16: Opc = X86::ADD16rm; break;
2555 case MVT::i32: Opc = X86::ADD32rm; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002556 case MVT::f32: Opc = X86::ADDSSrm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002557 case MVT::f64:
2558 // For F64, handle promoted load operations (from F32) as well!
Nate Begemanf63be7d2005-07-06 18:59:04 +00002559 if (X86ScalarSSE) {
2560 assert(Op1.getOpcode() == ISD::LOAD && "SSE load not promoted");
2561 Opc = X86::ADDSDrm;
2562 } else {
2563 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
2564 }
Chris Lattner44129b52005-01-25 20:03:11 +00002565 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002566 }
2567 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002568 EmitFoldedLoad(Op1, AM);
2569 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002570 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2571 return Result;
2572 }
2573
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002574 // See if we can codegen this as an LEA to fold operations together.
2575 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00002576 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002577 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00002578 MatchAddress(N, AM);
2579 ExprMap[N] = Result;
2580
2581 // If this is not just an add, emit the LEA. For a simple add (like
2582 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
2583 // leave this as LEA, then peephole it to 'ADD' after two address elim
2584 // happens.
2585 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
2586 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
2587 X86AddressMode XAM = SelectAddrExprs(AM);
2588 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
2589 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002590 }
2591 }
Chris Lattner11333092005-01-11 03:11:44 +00002592
Chris Lattnera5ade062005-01-11 21:19:59 +00002593 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002594 Opc = 0;
2595 if (CN->getValue() == 1) { // add X, 1 -> inc X
2596 switch (N.getValueType()) {
2597 default: assert(0 && "Cannot integer add this type!");
2598 case MVT::i8: Opc = X86::INC8r; break;
2599 case MVT::i16: Opc = X86::INC16r; break;
2600 case MVT::i32: Opc = X86::INC32r; break;
2601 }
2602 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
2603 switch (N.getValueType()) {
2604 default: assert(0 && "Cannot integer add this type!");
2605 case MVT::i8: Opc = X86::DEC8r; break;
2606 case MVT::i16: Opc = X86::DEC16r; break;
2607 case MVT::i32: Opc = X86::DEC32r; break;
2608 }
2609 }
2610
2611 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002612 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002613 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2614 return Result;
2615 }
2616
2617 switch (N.getValueType()) {
2618 default: assert(0 && "Cannot add this type!");
2619 case MVT::i8: Opc = X86::ADD8ri; break;
2620 case MVT::i16: Opc = X86::ADD16ri; break;
2621 case MVT::i32: Opc = X86::ADD32ri; break;
2622 }
2623 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002624 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002625 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2626 return Result;
2627 }
2628 }
2629
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002630 switch (N.getValueType()) {
2631 default: assert(0 && "Cannot add this type!");
2632 case MVT::i8: Opc = X86::ADD8rr; break;
2633 case MVT::i16: Opc = X86::ADD16rr; break;
2634 case MVT::i32: Opc = X86::ADD32rr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002635 case MVT::f32: Opc = X86::ADDSSrr; break;
2636 case MVT::f64: Opc = X86ScalarSSE ? X86::ADDSDrr : X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002637 }
Chris Lattner11333092005-01-11 03:11:44 +00002638
Chris Lattnera5ade062005-01-11 21:19:59 +00002639 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2640 Tmp1 = SelectExpr(Op0);
2641 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00002642 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00002643 Tmp2 = SelectExpr(Op1);
2644 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00002645 }
2646
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002647 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2648 return Result;
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002649
Nate Begemanf63be7d2005-07-06 18:59:04 +00002650 case ISD::FSQRT:
2651 Tmp1 = SelectExpr(Node->getOperand(0));
2652 if (X86ScalarSSE) {
2653 Opc = (N.getValueType() == MVT::f32) ? X86::SQRTSSrr : X86::SQRTSDrr;
2654 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2655 } else {
2656 BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1);
2657 }
2658 return Result;
2659
2660 // FIXME:
2661 // Once we can spill 16 byte constants into the constant pool, we can
2662 // implement SSE equivalents of FABS and FCHS.
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002663 case ISD::FABS:
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002664 case ISD::FNEG:
Chris Lattnerc5dcb532005-04-30 04:25:35 +00002665 case ISD::FSIN:
2666 case ISD::FCOS:
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002667 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002668 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002669 switch (N.getOpcode()) {
2670 default: assert(0 && "Unreachable!");
2671 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
2672 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
Chris Lattnerc5dcb532005-04-30 04:25:35 +00002673 case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break;
2674 case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002675 }
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002676 return Result;
2677
Chris Lattner8db0af12005-04-06 04:21:07 +00002678 case ISD::MULHU:
2679 switch (N.getValueType()) {
2680 default: assert(0 && "Unsupported VT!");
2681 case MVT::i8: Tmp2 = X86::MUL8r; break;
2682 case MVT::i16: Tmp2 = X86::MUL16r; break;
2683 case MVT::i32: Tmp2 = X86::MUL32r; break;
2684 }
2685 // FALL THROUGH
2686 case ISD::MULHS: {
2687 unsigned MovOpc, LowReg, HiReg;
2688 switch (N.getValueType()) {
2689 default: assert(0 && "Unsupported VT!");
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002690 case MVT::i8:
Chris Lattner8db0af12005-04-06 04:21:07 +00002691 MovOpc = X86::MOV8rr;
2692 LowReg = X86::AL;
2693 HiReg = X86::AH;
2694 Opc = X86::IMUL8r;
2695 break;
2696 case MVT::i16:
2697 MovOpc = X86::MOV16rr;
2698 LowReg = X86::AX;
2699 HiReg = X86::DX;
2700 Opc = X86::IMUL16r;
2701 break;
2702 case MVT::i32:
2703 MovOpc = X86::MOV32rr;
2704 LowReg = X86::EAX;
2705 HiReg = X86::EDX;
2706 Opc = X86::IMUL32r;
2707 break;
2708 }
2709 if (Node->getOpcode() != ISD::MULHS)
2710 Opc = Tmp2; // Get the MULHU opcode.
2711
2712 Op0 = Node->getOperand(0);
2713 Op1 = Node->getOperand(1);
2714 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2715 Tmp1 = SelectExpr(Op0);
2716 Tmp2 = SelectExpr(Op1);
2717 } else {
2718 Tmp2 = SelectExpr(Op1);
2719 Tmp1 = SelectExpr(Op0);
2720 }
2721
2722 // FIXME: Implement folding of loads into the memory operands here!
2723 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
2724 BuildMI(BB, Opc, 1).addReg(Tmp2);
2725 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
2726 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002727 }
Chris Lattner8db0af12005-04-06 04:21:07 +00002728
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002729 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00002730 case ISD::MUL:
2731 case ISD::AND:
2732 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00002733 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00002734 static const unsigned SUBTab[] = {
2735 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
2736 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
2737 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
2738 };
Nate Begemanf63be7d2005-07-06 18:59:04 +00002739 static const unsigned SSE_SUBTab[] = {
2740 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
2741 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::SUBSSrm, X86::SUBSDrm,
2742 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::SUBSSrr, X86::SUBSDrr,
2743 };
Chris Lattnera5ade062005-01-11 21:19:59 +00002744 static const unsigned MULTab[] = {
2745 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
2746 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
2747 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
2748 };
Nate Begemanf63be7d2005-07-06 18:59:04 +00002749 static const unsigned SSE_MULTab[] = {
2750 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
2751 0, X86::IMUL16rm , X86::IMUL32rm, X86::MULSSrm, X86::MULSDrm,
2752 0, X86::IMUL16rr , X86::IMUL32rr, X86::MULSSrr, X86::MULSDrr,
2753 };
Chris Lattnera5ade062005-01-11 21:19:59 +00002754 static const unsigned ANDTab[] = {
2755 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
2756 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002757 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattnera5ade062005-01-11 21:19:59 +00002758 };
2759 static const unsigned ORTab[] = {
2760 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
2761 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
2762 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
2763 };
2764 static const unsigned XORTab[] = {
2765 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
2766 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
2767 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
2768 };
2769
2770 Op0 = Node->getOperand(0);
2771 Op1 = Node->getOperand(1);
2772
Chris Lattner30ea1e92005-01-19 07:37:26 +00002773 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
2774 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00002775 return Result;
2776
2777 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002778 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
2779 if (CN->isNullValue()) { // 0 - N -> neg N
2780 switch (N.getValueType()) {
2781 default: assert(0 && "Cannot sub this type!");
2782 case MVT::i1:
2783 case MVT::i8: Opc = X86::NEG8r; break;
2784 case MVT::i16: Opc = X86::NEG16r; break;
2785 case MVT::i32: Opc = X86::NEG32r; break;
2786 }
2787 Tmp1 = SelectExpr(N.getOperand(1));
2788 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2789 return Result;
2790 }
2791
Chris Lattnera5ade062005-01-11 21:19:59 +00002792 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2793 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00002794 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00002795 switch (N.getValueType()) {
2796 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00002797 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00002798 case MVT::i8: Opc = X86::NOT8r; break;
2799 case MVT::i16: Opc = X86::NOT16r; break;
2800 case MVT::i32: Opc = X86::NOT32r; break;
2801 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00002802 if (Opc) {
2803 Tmp1 = SelectExpr(Op0);
2804 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2805 return Result;
2806 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00002807 }
2808
Chris Lattner2a4e5082005-01-17 06:48:02 +00002809 // Fold common multiplies into LEA instructions.
2810 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
2811 switch ((int)CN->getValue()) {
2812 default: break;
2813 case 3:
2814 case 5:
2815 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00002816 // Remove N from exprmap so SelectAddress doesn't get confused.
2817 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002818 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00002819 SelectAddress(N, AM);
2820 // Restore it to the map.
2821 ExprMap[N] = Result;
2822 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
2823 return Result;
2824 }
2825 }
2826
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002827 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00002828 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002829 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00002830 case MVT::i8: Opc = 0; break;
2831 case MVT::i16: Opc = 1; break;
2832 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002833 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002834 switch (Node->getOpcode()) {
2835 default: assert(0 && "Unreachable!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002836 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2837 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002838 case ISD::AND: Opc = ANDTab[Opc]; break;
2839 case ISD::OR: Opc = ORTab[Opc]; break;
2840 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002841 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002842 if (Opc) { // Can't fold MUL:i8 R, imm
2843 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002844 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2845 return Result;
2846 }
2847 }
Chris Lattner11333092005-01-11 03:11:44 +00002848
Chris Lattner44129b52005-01-25 20:03:11 +00002849 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00002850 if (Node->getOpcode() != ISD::SUB) {
2851 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002852 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00002853 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00002854 // For FP, emit 'reverse' subract, with a memory operand.
Nate Begemanf63be7d2005-07-06 18:59:04 +00002855 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner44129b52005-01-25 20:03:11 +00002856 if (Op0.getOpcode() == ISD::EXTLOAD)
2857 Opc = X86::FSUBR32m;
2858 else
2859 Opc = X86::FSUBR64m;
2860
Chris Lattnera5ade062005-01-11 21:19:59 +00002861 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002862 EmitFoldedLoad(Op0, AM);
2863 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00002864 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2865 return Result;
2866 }
2867 }
2868
Chris Lattner44129b52005-01-25 20:03:11 +00002869 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002870 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00002871 switch (N.getValueType()) {
2872 default: assert(0 && "Cannot operate on this type!");
2873 case MVT::i1:
2874 case MVT::i8: Opc = 5; break;
2875 case MVT::i16: Opc = 6; break;
2876 case MVT::i32: Opc = 7; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00002877 case MVT::f32: Opc = 8; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002878 // For F64, handle promoted load operations (from F32) as well!
Jeff Cohen00b168892005-07-27 06:12:32 +00002879 case MVT::f64:
2880 assert((!X86ScalarSSE || Op1.getOpcode() == ISD::LOAD) &&
Nate Begemanf63be7d2005-07-06 18:59:04 +00002881 "SSE load should have been promoted");
2882 Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002883 }
2884 switch (Node->getOpcode()) {
2885 default: assert(0 && "Unreachable!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002886 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2887 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002888 case ISD::AND: Opc = ANDTab[Opc]; break;
2889 case ISD::OR: Opc = ORTab[Opc]; break;
2890 case ISD::XOR: Opc = XORTab[Opc]; break;
2891 }
2892
2893 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002894 EmitFoldedLoad(Op1, AM);
2895 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002896 if (Opc) {
2897 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2898 } else {
2899 assert(Node->getOpcode() == ISD::MUL &&
2900 N.getValueType() == MVT::i8 && "Unexpected situation!");
2901 // Must use the MUL instruction, which forces use of AL.
2902 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2903 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2904 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2905 }
2906 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00002907 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002908
2909 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2910 Tmp1 = SelectExpr(Op0);
2911 Tmp2 = SelectExpr(Op1);
2912 } else {
2913 Tmp2 = SelectExpr(Op1);
2914 Tmp1 = SelectExpr(Op0);
2915 }
2916
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002917 switch (N.getValueType()) {
2918 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002919 case MVT::i1:
2920 case MVT::i8: Opc = 10; break;
2921 case MVT::i16: Opc = 11; break;
2922 case MVT::i32: Opc = 12; break;
2923 case MVT::f32: Opc = 13; break;
2924 case MVT::f64: Opc = 14; break;
2925 }
2926 switch (Node->getOpcode()) {
2927 default: assert(0 && "Unreachable!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00002928 case ISD::SUB: Opc = X86ScalarSSE ? SSE_SUBTab[Opc] : SUBTab[Opc]; break;
2929 case ISD::MUL: Opc = X86ScalarSSE ? SSE_MULTab[Opc] : MULTab[Opc]; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002930 case ISD::AND: Opc = ANDTab[Opc]; break;
2931 case ISD::OR: Opc = ORTab[Opc]; break;
2932 case ISD::XOR: Opc = XORTab[Opc]; break;
2933 }
2934 if (Opc) {
2935 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2936 } else {
2937 assert(Node->getOpcode() == ISD::MUL &&
2938 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00002939 // Must use the MUL instruction, which forces use of AL.
2940 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2941 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2942 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002943 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002944 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00002945 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002946 case ISD::ADD_PARTS:
2947 case ISD::SUB_PARTS: {
2948 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2949 "Not an i64 add/sub!");
2950 // Emit all of the operands.
2951 std::vector<unsigned> InVals;
2952 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2953 InVals.push_back(SelectExpr(N.getOperand(i)));
2954 if (N.getOpcode() == ISD::ADD_PARTS) {
2955 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2956 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2957 } else {
2958 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2959 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2960 }
2961 return Result+N.ResNo;
2962 }
2963
Chris Lattnerb38a7492005-04-02 04:01:14 +00002964 case ISD::SHL_PARTS:
2965 case ISD::SRA_PARTS:
2966 case ISD::SRL_PARTS: {
2967 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2968 "Not an i64 shift!");
2969 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2970 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2971 unsigned TmpReg = MakeReg(MVT::i32);
2972 if (N.getOpcode() == ISD::SRA_PARTS) {
2973 // If this is a SHR of a Long, then we need to do funny sign extension
2974 // stuff. TmpReg gets the value to use as the high-part if we are
2975 // shifting more than 32 bits.
2976 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2977 } else {
2978 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2979 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2980 }
2981
2982 // Initialize CL with the shift amount.
2983 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2984 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2985
2986 unsigned TmpReg2 = MakeReg(MVT::i32);
2987 unsigned TmpReg3 = MakeReg(MVT::i32);
2988 if (N.getOpcode() == ISD::SHL_PARTS) {
2989 // TmpReg2 = shld inHi, inLo
2990 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2991 .addReg(ShiftOpLo);
2992 // TmpReg3 = shl inLo, CL
2993 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002994
Chris Lattnerb38a7492005-04-02 04:01:14 +00002995 // Set the flags to indicate whether the shift was by more than 32 bits.
2996 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002997
Chris Lattnerb38a7492005-04-02 04:01:14 +00002998 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002999 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00003000 Result+1).addReg(TmpReg2).addReg(TmpReg3);
3001 // DestLo = (>32) ? TmpReg : TmpReg3;
3002 BuildMI(BB, X86::CMOVNE32rr, 2,
3003 Result).addReg(TmpReg3).addReg(TmpReg);
3004 } else {
3005 // TmpReg2 = shrd inLo, inHi
3006 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
3007 .addReg(ShiftOpHi);
3008 // TmpReg3 = s[ah]r inHi, CL
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003009 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnerb38a7492005-04-02 04:01:14 +00003010 : X86::SHR32rCL, 1, TmpReg3)
3011 .addReg(ShiftOpHi);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003012
Chris Lattnerb38a7492005-04-02 04:01:14 +00003013 // Set the flags to indicate whether the shift was by more than 32 bits.
3014 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003015
Chris Lattnerb38a7492005-04-02 04:01:14 +00003016 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003017 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00003018 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003019
Chris Lattnerb38a7492005-04-02 04:01:14 +00003020 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003021 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00003022 Result+1).addReg(TmpReg3).addReg(TmpReg);
3023 }
3024 return Result+N.ResNo;
3025 }
3026
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003027 case ISD::SELECT:
Nate Begeman1c73c7b2005-08-03 23:26:28 +00003028 EmitSelectCC(N.getOperand(0), N.getOperand(1), N.getOperand(2),
3029 N.getValueType(), Result);
Chris Lattnerda2ce112005-01-16 07:34:08 +00003030 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003031
3032 case ISD::SDIV:
3033 case ISD::UDIV:
3034 case ISD::SREM:
3035 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00003036 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
3037 "We don't support this operator!");
3038
Chris Lattner5bf26862005-04-13 03:29:53 +00003039 if (N.getOpcode() == ISD::SDIV) {
Chris Lattner3576c842005-01-25 20:35:10 +00003040 // We can fold loads into FpDIVs, but not really into any others.
Nate Begemanb8aa3ac2005-07-07 06:32:01 +00003041 if (N.getValueType() == MVT::f64 && !X86ScalarSSE) {
Chris Lattner3576c842005-01-25 20:35:10 +00003042 // Check for reversed and unreversed DIV.
3043 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
3044 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
3045 Opc = X86::FDIVR32m;
3046 else
3047 Opc = X86::FDIVR64m;
3048 X86AddressMode AM;
3049 EmitFoldedLoad(N.getOperand(0), AM);
3050 Tmp1 = SelectExpr(N.getOperand(1));
3051 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
3052 return Result;
3053 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
3054 N.getOperand(1).getOpcode() == ISD::LOAD) {
3055 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
3056 Opc = X86::FDIV32m;
3057 else
3058 Opc = X86::FDIV64m;
3059 X86AddressMode AM;
3060 EmitFoldedLoad(N.getOperand(1), AM);
3061 Tmp1 = SelectExpr(N.getOperand(0));
3062 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
3063 return Result;
3064 }
3065 }
3066
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003067 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3068 // FIXME: These special cases should be handled by the lowering impl!
3069 unsigned RHS = CN->getValue();
3070 bool isNeg = false;
3071 if ((int)RHS < 0) {
3072 isNeg = true;
3073 RHS = -RHS;
3074 }
3075 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
Chris Lattner0561b3f2005-08-02 19:26:06 +00003076 unsigned Log = Log2_32(RHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003077 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
3078 switch (N.getValueType()) {
3079 default: assert("Unknown type to signed divide!");
3080 case MVT::i8:
3081 SAROpc = X86::SAR8ri;
3082 SHROpc = X86::SHR8ri;
3083 ADDOpc = X86::ADD8rr;
3084 NEGOpc = X86::NEG8r;
3085 break;
3086 case MVT::i16:
3087 SAROpc = X86::SAR16ri;
3088 SHROpc = X86::SHR16ri;
3089 ADDOpc = X86::ADD16rr;
3090 NEGOpc = X86::NEG16r;
3091 break;
3092 case MVT::i32:
3093 SAROpc = X86::SAR32ri;
3094 SHROpc = X86::SHR32ri;
3095 ADDOpc = X86::ADD32rr;
3096 NEGOpc = X86::NEG32r;
3097 break;
3098 }
Chris Lattnera96e5772005-05-13 21:48:20 +00003099 unsigned RegSize = MVT::getSizeInBits(N.getValueType());
Chris Lattner11333092005-01-11 03:11:44 +00003100 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerca96c822005-05-13 21:50:27 +00003101 unsigned TmpReg;
3102 if (Log != 1) {
3103 TmpReg = MakeReg(N.getValueType());
3104 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
3105 } else {
3106 TmpReg = Tmp1;
3107 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003108 unsigned TmpReg2 = MakeReg(N.getValueType());
Chris Lattnera96e5772005-05-13 21:48:20 +00003109 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(RegSize-Log);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003110 unsigned TmpReg3 = MakeReg(N.getValueType());
3111 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003112
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003113 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
3114 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
3115 if (isNeg)
3116 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
3117 return Result;
3118 }
3119 }
Chris Lattner5bf26862005-04-13 03:29:53 +00003120 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003121
Chris Lattner11333092005-01-11 03:11:44 +00003122 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3123 Tmp1 = SelectExpr(N.getOperand(0));
3124 Tmp2 = SelectExpr(N.getOperand(1));
3125 } else {
3126 Tmp2 = SelectExpr(N.getOperand(1));
3127 Tmp1 = SelectExpr(N.getOperand(0));
3128 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003129
3130 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
3131 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
3132 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
3133 switch (N.getValueType()) {
3134 default: assert(0 && "Cannot sdiv this type!");
3135 case MVT::i8:
3136 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
3137 LoReg = X86::AL;
3138 HiReg = X86::AH;
3139 MovOpcode = X86::MOV8rr;
3140 ClrOpcode = X86::MOV8ri;
3141 SExtOpcode = X86::CBW;
3142 break;
3143 case MVT::i16:
3144 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
3145 LoReg = X86::AX;
3146 HiReg = X86::DX;
3147 MovOpcode = X86::MOV16rr;
3148 ClrOpcode = X86::MOV16ri;
3149 SExtOpcode = X86::CWD;
3150 break;
3151 case MVT::i32:
3152 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00003153 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003154 HiReg = X86::EDX;
3155 MovOpcode = X86::MOV32rr;
3156 ClrOpcode = X86::MOV32ri;
3157 SExtOpcode = X86::CDQ;
3158 break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00003159 case MVT::f32:
3160 BuildMI(BB, X86::DIVSSrr, 2, Result).addReg(Tmp1).addReg(Tmp2);
3161 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003162 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00003163 Opc = X86ScalarSSE ? X86::DIVSDrr : X86::FpDIV;
3164 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003165 return Result;
3166 }
3167
3168 // Set up the low part.
3169 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
3170
3171 if (isSigned) {
3172 // Sign extend the low part into the high part.
3173 BuildMI(BB, SExtOpcode, 0);
3174 } else {
3175 // Zero out the high part, effectively zero extending the input.
3176 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
3177 }
3178
3179 // Emit the DIV/IDIV instruction.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003180 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003181
3182 // Get the result of the divide or rem.
3183 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
3184 return Result;
3185 }
3186
3187 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003188 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00003189 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
3190 switch (N.getValueType()) {
3191 default: assert(0 && "Cannot shift this type!");
3192 case MVT::i8: Opc = X86::ADD8rr; break;
3193 case MVT::i16: Opc = X86::ADD16rr; break;
3194 case MVT::i32: Opc = X86::ADD32rr; break;
3195 }
3196 Tmp1 = SelectExpr(N.getOperand(0));
3197 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
3198 return Result;
3199 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003200
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003201 switch (N.getValueType()) {
3202 default: assert(0 && "Cannot shift this type!");
3203 case MVT::i8: Opc = X86::SHL8ri; break;
3204 case MVT::i16: Opc = X86::SHL16ri; break;
3205 case MVT::i32: Opc = X86::SHL32ri; break;
3206 }
Chris Lattner11333092005-01-11 03:11:44 +00003207 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003208 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3209 return Result;
3210 }
Chris Lattner11333092005-01-11 03:11:44 +00003211
3212 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3213 Tmp1 = SelectExpr(N.getOperand(0));
3214 Tmp2 = SelectExpr(N.getOperand(1));
3215 } else {
3216 Tmp2 = SelectExpr(N.getOperand(1));
3217 Tmp1 = SelectExpr(N.getOperand(0));
3218 }
3219
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003220 switch (N.getValueType()) {
3221 default: assert(0 && "Cannot shift this type!");
3222 case MVT::i8 : Opc = X86::SHL8rCL; break;
3223 case MVT::i16: Opc = X86::SHL16rCL; break;
3224 case MVT::i32: Opc = X86::SHL32rCL; break;
3225 }
3226 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
Chris Lattnerfd444b22005-08-19 00:16:17 +00003227 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003228 return Result;
3229 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003230 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3231 switch (N.getValueType()) {
3232 default: assert(0 && "Cannot shift this type!");
3233 case MVT::i8: Opc = X86::SHR8ri; break;
3234 case MVT::i16: Opc = X86::SHR16ri; break;
3235 case MVT::i32: Opc = X86::SHR32ri; break;
3236 }
Chris Lattner11333092005-01-11 03:11:44 +00003237 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003238 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3239 return Result;
3240 }
Chris Lattner11333092005-01-11 03:11:44 +00003241
3242 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3243 Tmp1 = SelectExpr(N.getOperand(0));
3244 Tmp2 = SelectExpr(N.getOperand(1));
3245 } else {
3246 Tmp2 = SelectExpr(N.getOperand(1));
3247 Tmp1 = SelectExpr(N.getOperand(0));
3248 }
3249
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003250 switch (N.getValueType()) {
3251 default: assert(0 && "Cannot shift this type!");
3252 case MVT::i8 : Opc = X86::SHR8rCL; break;
3253 case MVT::i16: Opc = X86::SHR16rCL; break;
3254 case MVT::i32: Opc = X86::SHR32rCL; break;
3255 }
3256 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
Chris Lattnerfd444b22005-08-19 00:16:17 +00003257 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003258 return Result;
3259 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003260 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3261 switch (N.getValueType()) {
3262 default: assert(0 && "Cannot shift this type!");
3263 case MVT::i8: Opc = X86::SAR8ri; break;
3264 case MVT::i16: Opc = X86::SAR16ri; break;
3265 case MVT::i32: Opc = X86::SAR32ri; break;
3266 }
Chris Lattner11333092005-01-11 03:11:44 +00003267 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003268 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3269 return Result;
3270 }
Chris Lattner11333092005-01-11 03:11:44 +00003271
3272 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3273 Tmp1 = SelectExpr(N.getOperand(0));
3274 Tmp2 = SelectExpr(N.getOperand(1));
3275 } else {
3276 Tmp2 = SelectExpr(N.getOperand(1));
3277 Tmp1 = SelectExpr(N.getOperand(0));
3278 }
3279
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003280 switch (N.getValueType()) {
3281 default: assert(0 && "Cannot shift this type!");
3282 case MVT::i8 : Opc = X86::SAR8rCL; break;
3283 case MVT::i16: Opc = X86::SAR16rCL; break;
3284 case MVT::i32: Opc = X86::SAR32rCL; break;
3285 }
3286 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
Chris Lattner679c4082005-08-19 00:31:37 +00003287 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003288 return Result;
3289
3290 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00003291 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner88ac32c2005-08-09 20:21:10 +00003292 EmitSetCC(BB, Result, cast<CondCodeSDNode>(N.getOperand(2))->get(),
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003293 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
3294 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003295 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003296 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00003297 if (Result != 1) { // Generate the token
3298 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3299 assert(0 && "Load already emitted!?");
3300 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003301 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3302
Chris Lattner5188ad72005-01-08 19:28:19 +00003303 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003304 default: assert(0 && "Cannot load this type!");
3305 case MVT::i1:
3306 case MVT::i8: Opc = X86::MOV8rm; break;
3307 case MVT::i16: Opc = X86::MOV16rm; break;
3308 case MVT::i32: Opc = X86::MOV32rm; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00003309 case MVT::f32: Opc = X86::MOVSSrm; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003310 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00003311 if (X86ScalarSSE) {
3312 Opc = X86::MOVSDrm;
3313 } else {
3314 Opc = X86::FLD64m;
Jeff Cohen00b168892005-07-27 06:12:32 +00003315 ContainsFPCode = true;
Nate Begemanf63be7d2005-07-06 18:59:04 +00003316 }
3317 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003318 }
Chris Lattner11333092005-01-11 03:11:44 +00003319
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003320 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner5839bf22005-08-26 17:15:30 +00003321 unsigned CPIdx = BB->getParent()->getConstantPool()->
3322 getConstantPoolIndex(CP->get());
Chris Lattner11333092005-01-11 03:11:44 +00003323 Select(N.getOperand(0));
Chris Lattner5839bf22005-08-26 17:15:30 +00003324 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CPIdx);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003325 } else {
3326 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00003327
3328 SDOperand Chain = N.getOperand(0);
3329 SDOperand Address = N.getOperand(1);
3330 if (getRegPressure(Chain) > getRegPressure(Address)) {
3331 Select(Chain);
3332 SelectAddress(Address, AM);
3333 } else {
3334 SelectAddress(Address, AM);
3335 Select(Chain);
3336 }
3337
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003338 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
3339 }
3340 return Result;
Chris Lattner67649df2005-05-14 06:52:07 +00003341 case X86ISD::FILD64m:
3342 // Make sure we generate both values.
3343 assert(Result != 1 && N.getValueType() == MVT::f64);
3344 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3345 assert(0 && "Load already emitted!?");
3346
3347 {
3348 X86AddressMode AM;
3349
3350 SDOperand Chain = N.getOperand(0);
3351 SDOperand Address = N.getOperand(1);
3352 if (getRegPressure(Chain) > getRegPressure(Address)) {
3353 Select(Chain);
3354 SelectAddress(Address, AM);
3355 } else {
3356 SelectAddress(Address, AM);
3357 Select(Chain);
3358 }
Chris Lattner745d5382005-07-29 00:40:01 +00003359
3360 addFullAddress(BuildMI(BB, X86::FILD64m, 4, Result), AM);
Chris Lattner67649df2005-05-14 06:52:07 +00003361 }
3362 return Result;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003363
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003364 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
3365 case ISD::ZEXTLOAD: {
3366 // Make sure we generate both values.
3367 if (Result != 1)
3368 ExprMap[N.getValue(1)] = 1; // Generate the token
3369 else
3370 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3371
Chris Lattnerda2ce112005-01-16 07:34:08 +00003372 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
3373 if (Node->getValueType(0) == MVT::f64) {
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003374 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnerda2ce112005-01-16 07:34:08 +00003375 "Bad EXTLOAD!");
Chris Lattner5839bf22005-08-26 17:15:30 +00003376 unsigned CPIdx = BB->getParent()->getConstantPool()->
3377 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
3378
3379 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result), CPIdx);
Chris Lattnerda2ce112005-01-16 07:34:08 +00003380 return Result;
3381 }
3382
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003383 X86AddressMode AM;
3384 if (getRegPressure(Node->getOperand(0)) >
3385 getRegPressure(Node->getOperand(1))) {
3386 Select(Node->getOperand(0)); // chain
3387 SelectAddress(Node->getOperand(1), AM);
3388 } else {
3389 SelectAddress(Node->getOperand(1), AM);
3390 Select(Node->getOperand(0)); // chain
3391 }
3392
3393 switch (Node->getValueType(0)) {
3394 default: assert(0 && "Unknown type to sign extend to.");
3395 case MVT::f64:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003396 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::f32 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003397 "Bad EXTLOAD!");
3398 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
3399 break;
3400 case MVT::i32:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003401 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003402 default:
3403 assert(0 && "Bad zero extend!");
3404 case MVT::i1:
3405 case MVT::i8:
3406 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
3407 break;
3408 case MVT::i16:
3409 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
3410 break;
3411 }
3412 break;
3413 case MVT::i16:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003414 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() <= MVT::i8 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003415 "Bad zero extend!");
3416 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3417 break;
3418 case MVT::i8:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003419 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i1 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003420 "Bad zero extend!");
3421 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
3422 break;
3423 }
3424 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003425 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003426 case ISD::SEXTLOAD: {
3427 // Make sure we generate both values.
3428 if (Result != 1)
3429 ExprMap[N.getValue(1)] = 1; // Generate the token
3430 else
3431 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3432
3433 X86AddressMode AM;
3434 if (getRegPressure(Node->getOperand(0)) >
3435 getRegPressure(Node->getOperand(1))) {
3436 Select(Node->getOperand(0)); // chain
3437 SelectAddress(Node->getOperand(1), AM);
3438 } else {
3439 SelectAddress(Node->getOperand(1), AM);
3440 Select(Node->getOperand(0)); // chain
3441 }
3442
3443 switch (Node->getValueType(0)) {
3444 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
3445 default: assert(0 && "Unknown type to sign extend to.");
3446 case MVT::i32:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003447 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003448 default:
3449 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
3450 case MVT::i8:
3451 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
3452 break;
3453 case MVT::i16:
3454 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
3455 break;
3456 }
3457 break;
3458 case MVT::i16:
Chris Lattnerbce81ae2005-07-10 01:56:13 +00003459 assert(cast<VTSDNode>(Node->getOperand(3))->getVT() == MVT::i8 &&
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003460 "Cannot sign extend from bool!");
3461 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3462 break;
3463 }
3464 return Result;
3465 }
3466
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003467 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003468 // Generate both result values.
3469 if (Result != 1)
3470 ExprMap[N.getValue(1)] = 1; // Generate the token
3471 else
3472 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3473
3474 // FIXME: We are currently ignoring the requested alignment for handling
3475 // greater than the stack alignment. This will need to be revisited at some
3476 // point. Align = N.getOperand(2);
3477
3478 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
3479 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
3480 std::cerr << "Cannot allocate stack object with greater alignment than"
3481 << " the stack alignment yet!";
3482 abort();
3483 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003484
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003485 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00003486 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003487 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
3488 .addImm(CN->getValue());
3489 } else {
Chris Lattner11333092005-01-11 03:11:44 +00003490 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3491 Select(N.getOperand(0));
3492 Tmp1 = SelectExpr(N.getOperand(1));
3493 } else {
3494 Tmp1 = SelectExpr(N.getOperand(1));
3495 Select(N.getOperand(0));
3496 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003497
3498 // Subtract size from stack pointer, thereby allocating some space.
3499 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
3500 }
3501
3502 // Put a pointer to the space into the result register, by copying the stack
3503 // pointer.
3504 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
3505 return Result;
3506
Chris Lattner239738a2005-05-14 08:48:15 +00003507 case X86ISD::TAILCALL:
3508 case X86ISD::CALL: {
Chris Lattner5188ad72005-01-08 19:28:19 +00003509 // The chain for this call is now lowered.
Chris Lattner239738a2005-05-14 08:48:15 +00003510 ExprMap.insert(std::make_pair(N.getValue(0), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00003511
Chris Lattnerc6f41812005-05-12 23:06:28 +00003512 bool isDirect = isa<GlobalAddressSDNode>(N.getOperand(1)) ||
3513 isa<ExternalSymbolSDNode>(N.getOperand(1));
3514 unsigned Callee = 0;
3515 if (isDirect) {
3516 Select(N.getOperand(0));
3517 } else {
3518 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3519 Select(N.getOperand(0));
3520 Callee = SelectExpr(N.getOperand(1));
3521 } else {
3522 Callee = SelectExpr(N.getOperand(1));
3523 Select(N.getOperand(0));
3524 }
3525 }
3526
3527 // If this call has values to pass in registers, do so now.
Chris Lattner239738a2005-05-14 08:48:15 +00003528 if (Node->getNumOperands() > 4) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003529 // The first value is passed in (a part of) EAX, the second in EDX.
Chris Lattner239738a2005-05-14 08:48:15 +00003530 unsigned RegOp1 = SelectExpr(N.getOperand(4));
Chris Lattnerc6f41812005-05-12 23:06:28 +00003531 unsigned RegOp2 =
Chris Lattner239738a2005-05-14 08:48:15 +00003532 Node->getNumOperands() > 5 ? SelectExpr(N.getOperand(5)) : 0;
Jeff Cohen00b168892005-07-27 06:12:32 +00003533
Chris Lattner239738a2005-05-14 08:48:15 +00003534 switch (N.getOperand(4).getValueType()) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003535 default: assert(0 && "Bad thing to pass in regs");
3536 case MVT::i1:
3537 case MVT::i8: BuildMI(BB, X86::MOV8rr , 1,X86::AL).addReg(RegOp1); break;
3538 case MVT::i16: BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1); break;
3539 case MVT::i32: BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);break;
3540 }
3541 if (RegOp2)
Chris Lattner239738a2005-05-14 08:48:15 +00003542 switch (N.getOperand(5).getValueType()) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003543 default: assert(0 && "Bad thing to pass in regs");
3544 case MVT::i1:
3545 case MVT::i8:
3546 BuildMI(BB, X86::MOV8rr , 1, X86::DL).addReg(RegOp2);
3547 break;
3548 case MVT::i16:
3549 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
3550 break;
3551 case MVT::i32:
3552 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
3553 break;
3554 }
3555 }
3556
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003557 if (GlobalAddressSDNode *GASD =
3558 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3559 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
3560 } else if (ExternalSymbolSDNode *ESSDN =
3561 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
3562 BuildMI(BB, X86::CALLpcrel32,
3563 1).addExternalSymbol(ESSDN->getSymbol(), true);
3564 } else {
Chris Lattner11333092005-01-11 03:11:44 +00003565 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3566 Select(N.getOperand(0));
3567 Tmp1 = SelectExpr(N.getOperand(1));
3568 } else {
3569 Tmp1 = SelectExpr(N.getOperand(1));
3570 Select(N.getOperand(0));
3571 }
3572
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003573 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
3574 }
Chris Lattner239738a2005-05-14 08:48:15 +00003575
3576 // Get caller stack amount and amount the callee added to the stack pointer.
3577 Tmp1 = cast<ConstantSDNode>(N.getOperand(2))->getValue();
3578 Tmp2 = cast<ConstantSDNode>(N.getOperand(3))->getValue();
3579 BuildMI(BB, X86::ADJCALLSTACKUP, 2).addImm(Tmp1).addImm(Tmp2);
3580
3581 if (Node->getNumValues() != 1)
3582 switch (Node->getValueType(1)) {
3583 default: assert(0 && "Unknown value type for call result!");
3584 case MVT::Other: return 1;
3585 case MVT::i1:
3586 case MVT::i8:
3587 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3588 break;
3589 case MVT::i16:
3590 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3591 break;
3592 case MVT::i32:
3593 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3594 if (Node->getNumValues() == 3 && Node->getValueType(2) == MVT::i32)
3595 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
3596 break;
3597 case MVT::f64: // Floating-point return values live in %ST(0)
Nate Begemanf63be7d2005-07-06 18:59:04 +00003598 if (X86ScalarSSE) {
3599 ContainsFPCode = true;
3600 BuildMI(BB, X86::FpGETRESULT, 1, X86::FP0);
3601
3602 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
3603 MachineFunction *F = BB->getParent();
3604 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
3605 addFrameReference(BuildMI(BB, X86::FST64m, 5), FrameIdx).addReg(X86::FP0);
3606 addFrameReference(BuildMI(BB, X86::MOVSDrm, 4, Result), FrameIdx);
3607 break;
3608 } else {
3609 ContainsFPCode = true;
3610 BuildMI(BB, X86::FpGETRESULT, 1, Result);
3611 break;
3612 }
Chris Lattner239738a2005-05-14 08:48:15 +00003613 }
3614 return Result+N.ResNo-1;
Chris Lattnerc6f41812005-05-12 23:06:28 +00003615 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00003616 case ISD::READPORT:
3617 // First, determine that the size of the operand falls within the acceptable
3618 // range for this architecture.
3619 //
3620 if (Node->getOperand(1).getValueType() != MVT::i16) {
3621 std::cerr << "llvm.readport: Address size is not 16 bits\n";
3622 exit(1);
3623 }
3624
3625 // Make sure we generate both values.
3626 if (Result != 1) { // Generate the token
3627 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3628 assert(0 && "readport already emitted!?");
3629 } else
3630 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Jeff Cohen00b168892005-07-27 06:12:32 +00003631
Chris Lattner966cdfb2005-05-09 21:17:38 +00003632 Select(Node->getOperand(0)); // Select the chain.
3633
3634 // If the port is a single-byte constant, use the immediate form.
3635 if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
3636 if ((Port->getValue() & 255) == Port->getValue()) {
3637 switch (Node->getValueType(0)) {
3638 case MVT::i8:
3639 BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue());
3640 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3641 return Result;
3642 case MVT::i16:
3643 BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue());
3644 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3645 return Result;
3646 case MVT::i32:
3647 BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue());
3648 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3649 return Result;
3650 default: break;
3651 }
3652 }
3653
3654 // Now, move the I/O port address into the DX register and use the IN
3655 // instruction to get the input data.
3656 //
3657 Tmp1 = SelectExpr(Node->getOperand(1));
3658 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1);
3659 switch (Node->getValueType(0)) {
3660 case MVT::i8:
3661 BuildMI(BB, X86::IN8rr, 0);
3662 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3663 return Result;
3664 case MVT::i16:
3665 BuildMI(BB, X86::IN16rr, 0);
3666 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3667 return Result;
3668 case MVT::i32:
3669 BuildMI(BB, X86::IN32rr, 0);
3670 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3671 return Result;
3672 default:
3673 std::cerr << "Cannot do input on this data type";
3674 exit(1);
3675 }
Jeff Cohen00b168892005-07-27 06:12:32 +00003676
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003677 }
3678
3679 return 0;
3680}
3681
Chris Lattnere10269b2005-01-17 19:25:26 +00003682/// TryToFoldLoadOpStore - Given a store node, try to fold together a
3683/// load/op/store instruction. If successful return true.
3684bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
3685 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
3686 SDOperand Chain = Node->getOperand(0);
3687 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00003688 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00003689
3690 // The chain has to be a load, the stored value must be an integer binary
3691 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00003692 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00003693 MVT::isFloatingPoint(StVal.getValueType()))
3694 return false;
3695
Chris Lattner5c659812005-01-17 22:10:42 +00003696 // Token chain must either be a factor node or the load to fold.
3697 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
3698 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00003699
Chris Lattner5c659812005-01-17 22:10:42 +00003700 SDOperand TheLoad;
3701
3702 // Check to see if there is a load from the same pointer that we're storing
3703 // to in either operand of the binop.
3704 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
3705 StVal.getOperand(0).getOperand(1) == StPtr)
3706 TheLoad = StVal.getOperand(0);
3707 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
3708 StVal.getOperand(1).getOperand(1) == StPtr)
3709 TheLoad = StVal.getOperand(1);
3710 else
3711 return false; // No matching load operand.
3712
3713 // We can only fold the load if there are no intervening side-effecting
3714 // operations. This means that the store uses the load as its token chain, or
3715 // there are only token factor nodes in between the store and load.
3716 if (Chain != TheLoad.getValue(1)) {
3717 // Okay, the other option is that we have a store referring to (possibly
3718 // nested) token factor nodes. For now, just try peeking through one level
3719 // of token factors to see if this is the case.
3720 bool ChainOk = false;
3721 if (Chain.getOpcode() == ISD::TokenFactor) {
3722 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3723 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
3724 ChainOk = true;
3725 break;
3726 }
3727 }
3728
3729 if (!ChainOk) return false;
3730 }
3731
3732 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00003733 return false;
3734
3735 // Make sure that one of the operands of the binop is the load, and that the
3736 // load folds into the binop.
3737 if (((StVal.getOperand(0) != TheLoad ||
3738 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
3739 (StVal.getOperand(1) != TheLoad ||
3740 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
3741 return false;
3742
3743 // Finally, check to see if this is one of the ops we can handle!
3744 static const unsigned ADDTAB[] = {
3745 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
3746 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
3747 };
3748 static const unsigned SUBTAB[] = {
3749 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
3750 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
3751 };
3752 static const unsigned ANDTAB[] = {
3753 X86::AND8mi, X86::AND16mi, X86::AND32mi,
3754 X86::AND8mr, X86::AND16mr, X86::AND32mr,
3755 };
3756 static const unsigned ORTAB[] = {
3757 X86::OR8mi, X86::OR16mi, X86::OR32mi,
3758 X86::OR8mr, X86::OR16mr, X86::OR32mr,
3759 };
3760 static const unsigned XORTAB[] = {
3761 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
3762 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
3763 };
3764 static const unsigned SHLTAB[] = {
3765 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
3766 /*Have to put the reg in CL*/0, 0, 0,
3767 };
3768 static const unsigned SARTAB[] = {
3769 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
3770 /*Have to put the reg in CL*/0, 0, 0,
3771 };
3772 static const unsigned SHRTAB[] = {
3773 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
3774 /*Have to put the reg in CL*/0, 0, 0,
3775 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003776
Chris Lattnere10269b2005-01-17 19:25:26 +00003777 const unsigned *TabPtr = 0;
3778 switch (StVal.getOpcode()) {
3779 default:
3780 std::cerr << "CANNOT [mem] op= val: ";
3781 StVal.Val->dump(); std::cerr << "\n";
3782 case ISD::MUL:
3783 case ISD::SDIV:
3784 case ISD::UDIV:
3785 case ISD::SREM:
3786 case ISD::UREM: return false;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003787
Chris Lattnere10269b2005-01-17 19:25:26 +00003788 case ISD::ADD: TabPtr = ADDTAB; break;
3789 case ISD::SUB: TabPtr = SUBTAB; break;
3790 case ISD::AND: TabPtr = ANDTAB; break;
3791 case ISD:: OR: TabPtr = ORTAB; break;
3792 case ISD::XOR: TabPtr = XORTAB; break;
3793 case ISD::SHL: TabPtr = SHLTAB; break;
3794 case ISD::SRA: TabPtr = SARTAB; break;
3795 case ISD::SRL: TabPtr = SHRTAB; break;
3796 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003797
Chris Lattnere10269b2005-01-17 19:25:26 +00003798 // Handle: [mem] op= CST
3799 SDOperand Op0 = StVal.getOperand(0);
3800 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00003801 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00003802 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
3803 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
3804 default: break;
3805 case MVT::i1:
3806 case MVT::i8: Opc = TabPtr[0]; break;
3807 case MVT::i16: Opc = TabPtr[1]; break;
3808 case MVT::i32: Opc = TabPtr[2]; break;
3809 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003810
Chris Lattnere10269b2005-01-17 19:25:26 +00003811 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00003812 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3813 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00003814 Select(Chain);
3815
Chris Lattnere10269b2005-01-17 19:25:26 +00003816 X86AddressMode AM;
3817 if (getRegPressure(TheLoad.getOperand(0)) >
3818 getRegPressure(TheLoad.getOperand(1))) {
3819 Select(TheLoad.getOperand(0));
3820 SelectAddress(TheLoad.getOperand(1), AM);
3821 } else {
3822 SelectAddress(TheLoad.getOperand(1), AM);
3823 Select(TheLoad.getOperand(0));
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003824 }
Chris Lattner5c659812005-01-17 22:10:42 +00003825
3826 if (StVal.getOpcode() == ISD::ADD) {
3827 if (CN->getValue() == 1) {
3828 switch (Op0.getValueType()) {
3829 default: break;
3830 case MVT::i8:
3831 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
3832 return true;
3833 case MVT::i16: Opc = TabPtr[1];
3834 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
3835 return true;
3836 case MVT::i32: Opc = TabPtr[2];
3837 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
3838 return true;
3839 }
3840 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
3841 switch (Op0.getValueType()) {
3842 default: break;
3843 case MVT::i8:
3844 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
3845 return true;
3846 case MVT::i16: Opc = TabPtr[1];
3847 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
3848 return true;
3849 case MVT::i32: Opc = TabPtr[2];
3850 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
3851 return true;
3852 }
3853 }
3854 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003855
Chris Lattnere10269b2005-01-17 19:25:26 +00003856 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
3857 return true;
3858 }
3859 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003860
Chris Lattnere10269b2005-01-17 19:25:26 +00003861 // If we have [mem] = V op [mem], try to turn it into:
3862 // [mem] = [mem] op V.
3863 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
3864 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
3865 StVal.getOpcode() != ISD::SRL)
3866 std::swap(Op0, Op1);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003867
Chris Lattnere10269b2005-01-17 19:25:26 +00003868 if (Op0 != TheLoad) return false;
3869
3870 switch (Op0.getValueType()) {
3871 default: return false;
3872 case MVT::i1:
3873 case MVT::i8: Opc = TabPtr[3]; break;
3874 case MVT::i16: Opc = TabPtr[4]; break;
3875 case MVT::i32: Opc = TabPtr[5]; break;
3876 }
Chris Lattner5c659812005-01-17 22:10:42 +00003877
Chris Lattnerb422aea2005-01-18 17:35:28 +00003878 // Table entry doesn't exist?
3879 if (Opc == 0) return false;
3880
Chris Lattner4a108662005-01-18 03:51:59 +00003881 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3882 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00003883 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00003884 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00003885
Chris Lattnere10269b2005-01-17 19:25:26 +00003886 X86AddressMode AM;
3887 SelectAddress(TheLoad.getOperand(1), AM);
3888 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00003889 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00003890 return true;
3891}
3892
Chris Lattner381e8872005-05-15 05:46:45 +00003893/// If node is a ret(tailcall) node, emit the specified tail call and return
3894/// true, otherwise return false.
3895///
3896/// FIXME: This whole thing should be a post-legalize optimization pass which
3897/// recognizes and transforms the dag. We don't want the selection phase doing
3898/// this stuff!!
3899///
3900bool ISel::EmitPotentialTailCall(SDNode *RetNode) {
3901 assert(RetNode->getOpcode() == ISD::RET && "Not a return");
3902
3903 SDOperand Chain = RetNode->getOperand(0);
3904
3905 // If this is a token factor node where one operand is a call, dig into it.
3906 SDOperand TokFactor;
3907 unsigned TokFactorOperand = 0;
3908 if (Chain.getOpcode() == ISD::TokenFactor) {
3909 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3910 if (Chain.getOperand(i).getOpcode() == ISD::CALLSEQ_END ||
3911 Chain.getOperand(i).getOpcode() == X86ISD::TAILCALL) {
3912 TokFactorOperand = i;
3913 TokFactor = Chain;
3914 Chain = Chain.getOperand(i);
3915 break;
3916 }
3917 if (TokFactor.Val == 0) return false; // No call operand.
3918 }
3919
3920 // Skip the CALLSEQ_END node if present.
3921 if (Chain.getOpcode() == ISD::CALLSEQ_END)
3922 Chain = Chain.getOperand(0);
3923
3924 // Is a tailcall the last control operation that occurs before the return?
3925 if (Chain.getOpcode() != X86ISD::TAILCALL)
3926 return false;
3927
3928 // If we return a value, is it the value produced by the call?
3929 if (RetNode->getNumOperands() > 1) {
3930 // Not returning the ret val of the call?
3931 if (Chain.Val->getNumValues() == 1 ||
3932 RetNode->getOperand(1) != Chain.getValue(1))
3933 return false;
3934
3935 if (RetNode->getNumOperands() > 2) {
3936 if (Chain.Val->getNumValues() == 2 ||
3937 RetNode->getOperand(2) != Chain.getValue(2))
3938 return false;
3939 }
3940 assert(RetNode->getNumOperands() <= 3);
3941 }
3942
3943 // CalleeCallArgAmt - The total number of bytes used for the callee arg area.
3944 // For FastCC, this will always be > 0.
3945 unsigned CalleeCallArgAmt =
3946 cast<ConstantSDNode>(Chain.getOperand(2))->getValue();
3947
3948 // CalleeCallArgPopAmt - The number of bytes in the call area popped by the
3949 // callee. For FastCC this will always be > 0, for CCC this is always 0.
3950 unsigned CalleeCallArgPopAmt =
3951 cast<ConstantSDNode>(Chain.getOperand(3))->getValue();
3952
3953 // There are several cases we can handle here. First, if the caller and
3954 // callee are both CCC functions, we can tailcall if the callee takes <= the
3955 // number of argument bytes that the caller does.
3956 if (CalleeCallArgPopAmt == 0 && // Callee is C CallingConv?
3957 X86Lowering.getBytesToPopOnReturn() == 0) { // Caller is C CallingConv?
3958 // Check to see if caller arg area size >= callee arg area size.
3959 if (X86Lowering.getBytesCallerReserves() >= CalleeCallArgAmt) {
3960 //std::cerr << "CCC TAILCALL UNIMP!\n";
3961 // If TokFactor is non-null, emit all operands.
3962
3963 //EmitCCCToCCCTailCall(Chain.Val);
3964 //return true;
3965 }
3966 return false;
3967 }
3968
3969 // Second, if both are FastCC functions, we can always perform the tail call.
3970 if (CalleeCallArgPopAmt && X86Lowering.getBytesToPopOnReturn()) {
3971 // If TokFactor is non-null, emit all operands before the call.
3972 if (TokFactor.Val) {
3973 for (unsigned i = 0, e = TokFactor.getNumOperands(); i != e; ++i)
3974 if (i != TokFactorOperand)
3975 Select(TokFactor.getOperand(i));
3976 }
3977
3978 EmitFastCCToFastCCTailCall(Chain.Val);
3979 return true;
3980 }
3981
3982 // We don't support mixed calls, due to issues with alignment. We could in
3983 // theory handle some mixed calls from CCC -> FastCC if the stack is properly
3984 // aligned (which depends on the number of arguments to the callee). TODO.
3985 return false;
3986}
3987
3988static SDOperand GetAdjustedArgumentStores(SDOperand Chain, int Offset,
3989 SelectionDAG &DAG) {
3990 MVT::ValueType StoreVT;
3991 switch (Chain.getOpcode()) {
Chris Lattner7cad4f22005-08-25 00:05:15 +00003992 default: assert(0 && "Unexpected node!");
Chris Lattner381e8872005-05-15 05:46:45 +00003993 case ISD::CALLSEQ_START:
Chris Lattnerea035432005-05-15 06:07:10 +00003994 // If we found the start of the call sequence, we're done. We actually
3995 // strip off the CALLSEQ_START node, to avoid generating the
3996 // ADJCALLSTACKDOWN marker for the tail call.
3997 return Chain.getOperand(0);
Chris Lattner381e8872005-05-15 05:46:45 +00003998 case ISD::TokenFactor: {
3999 std::vector<SDOperand> Ops;
4000 Ops.reserve(Chain.getNumOperands());
4001 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
4002 Ops.push_back(GetAdjustedArgumentStores(Chain.getOperand(i), Offset,DAG));
4003 return DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
4004 }
4005 case ISD::STORE: // Normal store
4006 StoreVT = Chain.getOperand(1).getValueType();
4007 break;
4008 case ISD::TRUNCSTORE: // FLOAT store
Chris Lattner9fadb4c2005-07-10 00:29:18 +00004009 StoreVT = cast<VTSDNode>(Chain.getOperand(4))->getVT();
Chris Lattner381e8872005-05-15 05:46:45 +00004010 break;
4011 }
4012
4013 SDOperand OrigDest = Chain.getOperand(2);
4014 unsigned OrigOffset;
4015
4016 if (OrigDest.getOpcode() == ISD::CopyFromReg) {
4017 OrigOffset = 0;
Chris Lattner707ebc52005-08-16 21:56:37 +00004018 assert(cast<RegisterSDNode>(OrigDest.getOperand(1))->getReg() == X86::ESP);
Chris Lattner381e8872005-05-15 05:46:45 +00004019 } else {
4020 // We expect only (ESP+C)
4021 assert(OrigDest.getOpcode() == ISD::ADD &&
4022 isa<ConstantSDNode>(OrigDest.getOperand(1)) &&
4023 OrigDest.getOperand(0).getOpcode() == ISD::CopyFromReg &&
Chris Lattner707ebc52005-08-16 21:56:37 +00004024 cast<RegisterSDNode>(OrigDest.getOperand(0).getOperand(1))->getReg()
4025 == X86::ESP);
Chris Lattner381e8872005-05-15 05:46:45 +00004026 OrigOffset = cast<ConstantSDNode>(OrigDest.getOperand(1))->getValue();
4027 }
4028
4029 // Compute the new offset from the incoming ESP value we wish to use.
4030 unsigned NewOffset = OrigOffset + Offset;
4031
4032 unsigned OpSize = (MVT::getSizeInBits(StoreVT)+7)/8; // Bits -> Bytes
4033 MachineFunction &MF = DAG.getMachineFunction();
4034 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, NewOffset);
4035 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
4036
4037 SDOperand InChain = GetAdjustedArgumentStores(Chain.getOperand(0), Offset,
4038 DAG);
4039 if (Chain.getOpcode() == ISD::STORE)
4040 return DAG.getNode(ISD::STORE, MVT::Other, InChain, Chain.getOperand(1),
4041 FIN);
4042 assert(Chain.getOpcode() == ISD::TRUNCSTORE);
4043 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, InChain, Chain.getOperand(1),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00004044 FIN, DAG.getSrcValue(NULL), DAG.getValueType(StoreVT));
Chris Lattner381e8872005-05-15 05:46:45 +00004045}
4046
4047
4048/// EmitFastCCToFastCCTailCall - Given a tailcall in the tail position to a
4049/// fastcc function from a fastcc function, emit the code to emit a 'proper'
4050/// tail call.
4051void ISel::EmitFastCCToFastCCTailCall(SDNode *TailCallNode) {
4052 unsigned CalleeCallArgSize =
4053 cast<ConstantSDNode>(TailCallNode->getOperand(2))->getValue();
4054 unsigned CallerArgSize = X86Lowering.getBytesToPopOnReturn();
4055
4056 //std::cerr << "****\n*** EMITTING TAIL CALL!\n****\n";
4057
4058 // Adjust argument stores. Instead of storing to [ESP], f.e., store to frame
4059 // indexes that are relative to the incoming ESP. If the incoming and
4060 // outgoing arg sizes are the same we will store to [InESP] instead of
4061 // [CurESP] and the ESP referenced will be relative to the incoming function
4062 // ESP.
4063 int ESPOffset = CallerArgSize-CalleeCallArgSize;
4064 SDOperand AdjustedArgStores =
4065 GetAdjustedArgumentStores(TailCallNode->getOperand(0), ESPOffset, *TheDAG);
4066
4067 // Copy the return address of the caller into a virtual register so we don't
4068 // clobber it.
4069 SDOperand RetVal;
4070 if (ESPOffset) {
4071 SDOperand RetValAddr = X86Lowering.getReturnAddressFrameIndex(*TheDAG);
4072 RetVal = TheDAG->getLoad(MVT::i32, TheDAG->getEntryNode(),
4073 RetValAddr, TheDAG->getSrcValue(NULL));
4074 SelectExpr(RetVal);
4075 }
4076
4077 // Codegen all of the argument stores.
4078 Select(AdjustedArgStores);
4079
4080 if (RetVal.Val) {
4081 // Emit a store of the saved ret value to the new location.
4082 MachineFunction &MF = TheDAG->getMachineFunction();
4083 int ReturnAddrFI = MF.getFrameInfo()->CreateFixedObject(4, ESPOffset-4);
4084 SDOperand RetValAddr = TheDAG->getFrameIndex(ReturnAddrFI, MVT::i32);
4085 Select(TheDAG->getNode(ISD::STORE, MVT::Other, TheDAG->getEntryNode(),
4086 RetVal, RetValAddr));
4087 }
4088
4089 // Get the destination value.
4090 SDOperand Callee = TailCallNode->getOperand(1);
4091 bool isDirect = isa<GlobalAddressSDNode>(Callee) ||
4092 isa<ExternalSymbolSDNode>(Callee);
Chris Lattner9cb2d612005-06-17 13:23:32 +00004093 unsigned CalleeReg = 0;
Chris Lattner381e8872005-05-15 05:46:45 +00004094 if (!isDirect) CalleeReg = SelectExpr(Callee);
4095
4096 unsigned RegOp1 = 0;
4097 unsigned RegOp2 = 0;
4098
4099 if (TailCallNode->getNumOperands() > 4) {
4100 // The first value is passed in (a part of) EAX, the second in EDX.
4101 RegOp1 = SelectExpr(TailCallNode->getOperand(4));
4102 if (TailCallNode->getNumOperands() > 5)
4103 RegOp2 = SelectExpr(TailCallNode->getOperand(5));
Jeff Cohen00b168892005-07-27 06:12:32 +00004104
Chris Lattner381e8872005-05-15 05:46:45 +00004105 switch (TailCallNode->getOperand(4).getValueType()) {
4106 default: assert(0 && "Bad thing to pass in regs");
4107 case MVT::i1:
4108 case MVT::i8:
4109 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(RegOp1);
4110 RegOp1 = X86::AL;
4111 break;
4112 case MVT::i16:
4113 BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1);
4114 RegOp1 = X86::AX;
4115 break;
4116 case MVT::i32:
4117 BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);
4118 RegOp1 = X86::EAX;
4119 break;
4120 }
4121 if (RegOp2)
4122 switch (TailCallNode->getOperand(5).getValueType()) {
4123 default: assert(0 && "Bad thing to pass in regs");
4124 case MVT::i1:
4125 case MVT::i8:
4126 BuildMI(BB, X86::MOV8rr, 1, X86::DL).addReg(RegOp2);
4127 RegOp2 = X86::DL;
4128 break;
4129 case MVT::i16:
4130 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
4131 RegOp2 = X86::DX;
4132 break;
4133 case MVT::i32:
4134 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
4135 RegOp2 = X86::EDX;
4136 break;
4137 }
4138 }
4139
4140 // Adjust ESP.
4141 if (ESPOffset)
4142 BuildMI(BB, X86::ADJSTACKPTRri, 2,
4143 X86::ESP).addReg(X86::ESP).addImm(ESPOffset);
4144
4145 // TODO: handle jmp [mem]
4146 if (!isDirect) {
4147 BuildMI(BB, X86::TAILJMPr, 1).addReg(CalleeReg);
4148 } else if (GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Callee)){
Chris Lattner16cb6f82005-05-19 05:54:33 +00004149 BuildMI(BB, X86::TAILJMPd, 1).addGlobalAddress(GASD->getGlobal(), true);
Chris Lattner381e8872005-05-15 05:46:45 +00004150 } else {
4151 ExternalSymbolSDNode *ESSDN = cast<ExternalSymbolSDNode>(Callee);
4152 BuildMI(BB, X86::TAILJMPd, 1).addExternalSymbol(ESSDN->getSymbol(), true);
4153 }
4154 // ADD IMPLICIT USE RegOp1/RegOp2's
4155}
4156
Chris Lattnere10269b2005-01-17 19:25:26 +00004157
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004158void ISel::Select(SDOperand N) {
4159 unsigned Tmp1, Tmp2, Opc;
4160
Nate Begeman85fdeb22005-03-24 04:39:54 +00004161 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004162 return; // Already selected.
4163
Chris Lattner989de032005-01-11 06:14:36 +00004164 SDNode *Node = N.Val;
4165
4166 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004167 default:
Chris Lattner989de032005-01-11 06:14:36 +00004168 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004169 assert(0 && "Node not handled yet!");
4170 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00004171 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00004172 if (Node->getNumOperands() == 2) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004173 bool OneFirst =
Chris Lattner1d50b7f2005-01-13 19:56:00 +00004174 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
4175 Select(Node->getOperand(OneFirst));
4176 Select(Node->getOperand(!OneFirst));
4177 } else {
4178 std::vector<std::pair<unsigned, unsigned> > OpsP;
4179 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
4180 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
4181 std::sort(OpsP.begin(), OpsP.end());
4182 std::reverse(OpsP.begin(), OpsP.end());
4183 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
4184 Select(Node->getOperand(OpsP[i].second));
4185 }
Chris Lattnerc3580712005-01-13 18:01:36 +00004186 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004187 case ISD::CopyToReg:
Chris Lattner707ebc52005-08-16 21:56:37 +00004188 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
Chris Lattneref6806c2005-01-12 02:02:48 +00004189 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00004190 Tmp1 = SelectExpr(N.getOperand(2));
Chris Lattneref6806c2005-01-12 02:02:48 +00004191 } else {
Chris Lattner707ebc52005-08-16 21:56:37 +00004192 Tmp1 = SelectExpr(N.getOperand(2));
Chris Lattneref6806c2005-01-12 02:02:48 +00004193 Select(N.getOperand(0));
4194 }
Chris Lattner707ebc52005-08-16 21:56:37 +00004195 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004196
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004197 if (Tmp1 != Tmp2) {
Chris Lattner707ebc52005-08-16 21:56:37 +00004198 switch (N.getOperand(2).getValueType()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004199 default: assert(0 && "Invalid type for operation!");
4200 case MVT::i1:
4201 case MVT::i8: Opc = X86::MOV8rr; break;
4202 case MVT::i16: Opc = X86::MOV16rr; break;
4203 case MVT::i32: Opc = X86::MOV32rr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004204 case MVT::f32: Opc = X86::MOVAPSrr; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00004205 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00004206 if (X86ScalarSSE) {
4207 Opc = X86::MOVAPDrr;
4208 } else {
Jeff Cohen00b168892005-07-27 06:12:32 +00004209 Opc = X86::FpMOV;
4210 ContainsFPCode = true;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004211 }
4212 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004213 }
4214 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
4215 }
4216 return;
4217 case ISD::RET:
Chris Lattner381e8872005-05-15 05:46:45 +00004218 if (N.getOperand(0).getOpcode() == ISD::CALLSEQ_END ||
4219 N.getOperand(0).getOpcode() == X86ISD::TAILCALL ||
4220 N.getOperand(0).getOpcode() == ISD::TokenFactor)
4221 if (EmitPotentialTailCall(Node))
4222 return;
4223
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004224 switch (N.getNumOperands()) {
4225 default:
4226 assert(0 && "Unknown return instruction!");
4227 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004228 assert(N.getOperand(1).getValueType() == MVT::i32 &&
Jeff Cohen00b168892005-07-27 06:12:32 +00004229 N.getOperand(2).getValueType() == MVT::i32 &&
4230 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00004231 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
4232 Tmp1 = SelectExpr(N.getOperand(1));
4233 Tmp2 = SelectExpr(N.getOperand(2));
4234 } else {
4235 Tmp2 = SelectExpr(N.getOperand(2));
4236 Tmp1 = SelectExpr(N.getOperand(1));
4237 }
4238 Select(N.getOperand(0));
4239
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004240 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4241 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004242 break;
4243 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00004244 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4245 Select(N.getOperand(0));
4246 Tmp1 = SelectExpr(N.getOperand(1));
4247 } else {
4248 Tmp1 = SelectExpr(N.getOperand(1));
4249 Select(N.getOperand(0));
4250 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004251 switch (N.getOperand(1).getValueType()) {
4252 default: assert(0 && "All other types should have been promoted!!");
Nate Begemanf63be7d2005-07-06 18:59:04 +00004253 case MVT::f32:
4254 if (X86ScalarSSE) {
4255 // Spill the value to memory and reload it into top of stack.
4256 unsigned Size = MVT::getSizeInBits(MVT::f32)/8;
4257 MachineFunction *F = BB->getParent();
4258 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
4259 addFrameReference(BuildMI(BB, X86::MOVSSmr, 5), FrameIdx).addReg(Tmp1);
4260 addFrameReference(BuildMI(BB, X86::FLD32m, 4, X86::FP0), FrameIdx);
4261 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen00b168892005-07-27 06:12:32 +00004262 ContainsFPCode = true;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004263 } else {
4264 assert(0 && "MVT::f32 only legal with scalar sse fp");
4265 abort();
4266 }
4267 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004268 case MVT::f64:
Nate Begemanf63be7d2005-07-06 18:59:04 +00004269 if (X86ScalarSSE) {
4270 // Spill the value to memory and reload it into top of stack.
4271 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
4272 MachineFunction *F = BB->getParent();
4273 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
4274 addFrameReference(BuildMI(BB, X86::MOVSDmr, 5), FrameIdx).addReg(Tmp1);
4275 addFrameReference(BuildMI(BB, X86::FLD64m, 4, X86::FP0), FrameIdx);
4276 BuildMI(BB, X86::FpSETRESULT, 1).addReg(X86::FP0);
Jeff Cohen00b168892005-07-27 06:12:32 +00004277 ContainsFPCode = true;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004278 } else {
4279 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
4280 }
4281 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004282 case MVT::i32:
Nate Begemanf63be7d2005-07-06 18:59:04 +00004283 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4284 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004285 }
4286 break;
4287 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00004288 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004289 break;
4290 }
Chris Lattner3648c672005-05-13 21:44:04 +00004291 if (X86Lowering.getBytesToPopOnReturn() == 0)
4292 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
4293 else
4294 BuildMI(BB, X86::RETI, 1).addImm(X86Lowering.getBytesToPopOnReturn());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004295 return;
4296 case ISD::BR: {
4297 Select(N.getOperand(0));
4298 MachineBasicBlock *Dest =
4299 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
4300 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
4301 return;
4302 }
4303
4304 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004305 MachineBasicBlock *Dest =
4306 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00004307
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004308 // Try to fold a setcc into the branch. If this fails, emit a test/jne
4309 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00004310 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
4311 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
4312 Select(N.getOperand(0));
4313 Tmp1 = SelectExpr(N.getOperand(1));
4314 } else {
4315 Tmp1 = SelectExpr(N.getOperand(1));
4316 Select(N.getOperand(0));
4317 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004318 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
4319 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
4320 }
Chris Lattner11333092005-01-11 03:11:44 +00004321
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004322 return;
4323 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004324
Chris Lattner4df0de92005-01-17 00:00:33 +00004325 case ISD::LOAD:
4326 // If this load could be folded into the only using instruction, and if it
4327 // is safe to emit the instruction here, try to do so now.
4328 if (Node->hasNUsesOfValue(1, 0)) {
4329 SDOperand TheVal = N.getValue(0);
4330 SDNode *User = 0;
4331 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
4332 assert(UI != Node->use_end() && "Didn't find use!");
4333 SDNode *UN = *UI;
4334 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
4335 if (UN->getOperand(i) == TheVal) {
4336 User = UN;
4337 goto FoundIt;
4338 }
4339 }
4340 FoundIt:
4341 // Only handle unary operators right now.
4342 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00004343 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00004344 SelectExpr(SDOperand(User, 0));
4345 return;
4346 }
4347 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00004348 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00004349 SelectExpr(N);
4350 return;
Chris Lattner966cdfb2005-05-09 21:17:38 +00004351 case ISD::READPORT:
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004352 case ISD::EXTLOAD:
4353 case ISD::SEXTLOAD:
4354 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004355 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner239738a2005-05-14 08:48:15 +00004356 case X86ISD::TAILCALL:
4357 case X86ISD::CALL:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00004358 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004359 SelectExpr(N);
4360 return;
Chris Lattnerc6f41812005-05-12 23:06:28 +00004361 case ISD::CopyFromReg:
Chris Lattner67649df2005-05-14 06:52:07 +00004362 case X86ISD::FILD64m:
Chris Lattnerc6f41812005-05-12 23:06:28 +00004363 ExprMap.erase(N);
4364 SelectExpr(N.getValue(0));
4365 return;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004366
Chris Lattner01546c52005-07-30 00:05:54 +00004367 case X86ISD::FP_TO_INT16_IN_MEM:
4368 case X86ISD::FP_TO_INT32_IN_MEM:
Chris Lattnerf7443da2005-07-29 00:54:34 +00004369 case X86ISD::FP_TO_INT64_IN_MEM: {
Chris Lattner745d5382005-07-29 00:40:01 +00004370 assert(N.getOperand(1).getValueType() == MVT::f64);
4371 X86AddressMode AM;
4372 Select(N.getOperand(0)); // Select the token chain
4373
4374 unsigned ValReg;
4375 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
4376 ValReg = SelectExpr(N.getOperand(1));
4377 SelectAddress(N.getOperand(2), AM);
4378 } else {
4379 SelectAddress(N.getOperand(2), AM);
4380 ValReg = SelectExpr(N.getOperand(1));
4381 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004382
Chris Lattnerf7443da2005-07-29 00:54:34 +00004383 // Change the floating point control register to use "round towards zero"
4384 // mode when truncating to an integer value.
4385 //
4386 MachineFunction *F = BB->getParent();
4387 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
4388 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004389
Chris Lattnerf7443da2005-07-29 00:54:34 +00004390 // Load the old value of the high byte of the control word...
Chris Lattnera35e1df2005-07-30 00:17:52 +00004391 unsigned OldCW = MakeReg(MVT::i16);
4392 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004393
Chris Lattnerf7443da2005-07-29 00:54:34 +00004394 // Set the high part to be round to zero...
Chris Lattnera88da082005-07-30 00:43:00 +00004395 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004396
Chris Lattnerf7443da2005-07-29 00:54:34 +00004397 // Reload the modified control word now...
4398 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004399
Chris Lattnerf7443da2005-07-29 00:54:34 +00004400 // Restore the memory image of control word to original value
Chris Lattnera35e1df2005-07-30 00:17:52 +00004401 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
Chris Lattner01546c52005-07-30 00:05:54 +00004402
4403 // Get the X86 opcode to use.
4404 switch (N.getOpcode()) {
4405 case X86ISD::FP_TO_INT16_IN_MEM: Tmp1 = X86::FIST16m; break;
4406 case X86ISD::FP_TO_INT32_IN_MEM: Tmp1 = X86::FIST32m; break;
4407 case X86ISD::FP_TO_INT64_IN_MEM: Tmp1 = X86::FISTP64m; break;
4408 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004409
Chris Lattner01546c52005-07-30 00:05:54 +00004410 addFullAddress(BuildMI(BB, Tmp1, 5), AM).addReg(ValReg);
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004411
Chris Lattnerf7443da2005-07-29 00:54:34 +00004412 // Reload the original control word now.
4413 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Chris Lattner745d5382005-07-29 00:40:01 +00004414 return;
4415 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004416
Chris Lattner9fadb4c2005-07-10 00:29:18 +00004417 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr, SRCVALUE, storety
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004418 X86AddressMode AM;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00004419 MVT::ValueType StoredTy = cast<VTSDNode>(N.getOperand(4))->getVT();
Chris Lattnerda2ce112005-01-16 07:34:08 +00004420 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
4421 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
4422 && "Unsupported TRUNCSTORE for this target!");
4423
4424 if (StoredTy == MVT::i16) {
4425 // FIXME: This is here just to allow testing. X86 doesn't really have a
4426 // TRUNCSTORE i16 operation, but this is required for targets that do not
4427 // have 16-bit integer registers. We occasionally disable 16-bit integer
4428 // registers to test the promotion code.
4429 Select(N.getOperand(0));
4430 Tmp1 = SelectExpr(N.getOperand(1));
4431 SelectAddress(N.getOperand(2), AM);
4432
4433 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4434 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
4435 return;
4436 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004437
4438 // Store of constant bool?
4439 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
4440 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4441 Select(N.getOperand(0));
4442 SelectAddress(N.getOperand(2), AM);
4443 } else {
4444 SelectAddress(N.getOperand(2), AM);
4445 Select(N.getOperand(0));
4446 }
4447 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
4448 return;
4449 }
4450
4451 switch (StoredTy) {
4452 default: assert(0 && "Cannot truncstore this type!");
4453 case MVT::i1: Opc = X86::MOV8mr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004454 case MVT::f32:
Jeff Cohen00b168892005-07-27 06:12:32 +00004455 assert(!X86ScalarSSE && "Cannot truncstore scalar SSE regs");
Nate Begemanf63be7d2005-07-06 18:59:04 +00004456 Opc = X86::FST32m; break;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004457 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004458
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004459 std::vector<std::pair<unsigned, unsigned> > RP;
4460 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
4461 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
4462 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
4463 std::sort(RP.begin(), RP.end());
4464
Chris Lattner572dd082005-02-23 05:57:21 +00004465 Tmp1 = 0; // Silence a warning.
Chris Lattnere9ef81d2005-01-15 05:22:24 +00004466 for (unsigned i = 0; i != 3; ++i)
4467 switch (RP[2-i].second) {
4468 default: assert(0 && "Unknown operand number!");
4469 case 0: Select(N.getOperand(0)); break;
4470 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
4471 case 2: SelectAddress(N.getOperand(2), AM); break;
4472 }
4473
4474 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
4475 return;
4476 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004477 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004478 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004479
4480 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
4481 Opc = 0;
4482 switch (CN->getValueType(0)) {
4483 default: assert(0 && "Invalid type for operation!");
4484 case MVT::i1:
4485 case MVT::i8: Opc = X86::MOV8mi; break;
4486 case MVT::i16: Opc = X86::MOV16mi; break;
4487 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004488 }
4489 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00004490 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4491 Select(N.getOperand(0));
4492 SelectAddress(N.getOperand(2), AM);
4493 } else {
4494 SelectAddress(N.getOperand(2), AM);
4495 Select(N.getOperand(0));
4496 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004497 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
4498 return;
4499 }
Chris Lattner75f354b2005-04-21 19:03:24 +00004500 } else if (GlobalAddressSDNode *GA =
4501 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
4502 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
4503
4504 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
4505 Select(N.getOperand(0));
4506 SelectAddress(N.getOperand(2), AM);
4507 } else {
4508 SelectAddress(N.getOperand(2), AM);
4509 Select(N.getOperand(0));
4510 }
Nate Begeman16b04f32005-07-15 00:38:55 +00004511 GlobalValue *GV = GA->getGlobal();
4512 // For Darwin, external and weak symbols are indirect, so we want to load
4513 // the value at address GV, not the value of GV itself.
Jeff Cohen00b168892005-07-27 06:12:32 +00004514 if (Subtarget->getIndirectExternAndWeakGlobals() &&
Nate Begeman16b04f32005-07-15 00:38:55 +00004515 (GV->hasWeakLinkage() || GV->isExternal())) {
4516 Tmp1 = MakeReg(MVT::i32);
4517 BuildMI(BB, X86::MOV32rm, 4, Tmp1).addReg(0).addZImm(1).addReg(0)
4518 .addGlobalAddress(GV, false, 0);
4519 addFullAddress(BuildMI(BB, X86::MOV32mr, 4+1),AM).addReg(Tmp1);
4520 } else {
4521 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),AM).addGlobalAddress(GV);
4522 }
Chris Lattner75f354b2005-04-21 19:03:24 +00004523 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004524 }
Chris Lattner837caa72005-01-11 23:21:30 +00004525
4526 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00004527 if (TryToFoldLoadOpStore(Node))
4528 return;
Chris Lattner837caa72005-01-11 23:21:30 +00004529
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004530 switch (N.getOperand(1).getValueType()) {
4531 default: assert(0 && "Cannot store this type!");
4532 case MVT::i1:
4533 case MVT::i8: Opc = X86::MOV8mr; break;
4534 case MVT::i16: Opc = X86::MOV16mr; break;
4535 case MVT::i32: Opc = X86::MOV32mr; break;
Nate Begemanf63be7d2005-07-06 18:59:04 +00004536 case MVT::f32: Opc = X86::MOVSSmr; break;
4537 case MVT::f64: Opc = X86ScalarSSE ? X86::MOVSDmr : X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004538 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004539
Chris Lattner11333092005-01-11 03:11:44 +00004540 std::vector<std::pair<unsigned, unsigned> > RP;
4541 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
4542 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
4543 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
4544 std::sort(RP.begin(), RP.end());
4545
Chris Lattner572dd082005-02-23 05:57:21 +00004546 Tmp1 = 0; // Silence a warning.
Chris Lattner11333092005-01-11 03:11:44 +00004547 for (unsigned i = 0; i != 3; ++i)
4548 switch (RP[2-i].second) {
4549 default: assert(0 && "Unknown operand number!");
4550 case 0: Select(N.getOperand(0)); break;
4551 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00004552 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00004553 }
4554
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004555 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
4556 return;
4557 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00004558 case ISD::CALLSEQ_START:
Chris Lattner3648c672005-05-13 21:44:04 +00004559 Select(N.getOperand(0));
4560 // Stack amount
4561 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
4562 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(Tmp1);
4563 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00004564 case ISD::CALLSEQ_END:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004565 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004566 return;
Chris Lattner989de032005-01-11 06:14:36 +00004567 case ISD::MEMSET: {
4568 Select(N.getOperand(0)); // Select the chain.
4569 unsigned Align =
4570 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
4571 if (Align == 0) Align = 1;
4572
4573 // Turn the byte code into # iterations
4574 unsigned CountReg;
4575 unsigned Opcode;
4576 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
4577 unsigned Val = ValC->getValue() & 255;
4578
4579 // If the value is a constant, then we can potentially use larger sets.
4580 switch (Align & 3) {
4581 case 2: // WORD aligned
4582 CountReg = MakeReg(MVT::i32);
4583 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4584 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
4585 } else {
4586 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4587 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
4588 }
4589 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
4590 Opcode = X86::REP_STOSW;
4591 break;
4592 case 0: // DWORD aligned
4593 CountReg = MakeReg(MVT::i32);
4594 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4595 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
4596 } else {
4597 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4598 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
4599 }
4600 Val = (Val << 8) | Val;
4601 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
4602 Opcode = X86::REP_STOSD;
4603 break;
4604 default: // BYTE aligned
4605 CountReg = SelectExpr(Node->getOperand(3));
4606 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
4607 Opcode = X86::REP_STOSB;
4608 break;
4609 }
4610 } else {
4611 // If it's not a constant value we are storing, just fall back. We could
4612 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
4613 unsigned ValReg = SelectExpr(Node->getOperand(2));
4614 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
4615 CountReg = SelectExpr(Node->getOperand(3));
4616 Opcode = X86::REP_STOSB;
4617 }
4618
4619 // No matter what the alignment is, we put the source in ESI, the
4620 // destination in EDI, and the count in ECX.
4621 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
4622 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
4623 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
4624 BuildMI(BB, Opcode, 0);
4625 return;
4626 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00004627 case ISD::MEMCPY: {
Chris Lattner31805bf2005-01-11 06:19:26 +00004628 Select(N.getOperand(0)); // Select the chain.
4629 unsigned Align =
4630 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
4631 if (Align == 0) Align = 1;
4632
4633 // Turn the byte code into # iterations
4634 unsigned CountReg;
4635 unsigned Opcode;
4636 switch (Align & 3) {
4637 case 2: // WORD aligned
4638 CountReg = MakeReg(MVT::i32);
4639 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4640 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
4641 } else {
4642 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4643 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
4644 }
4645 Opcode = X86::REP_MOVSW;
4646 break;
4647 case 0: // DWORD aligned
4648 CountReg = MakeReg(MVT::i32);
4649 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4650 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
4651 } else {
4652 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4653 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
4654 }
4655 Opcode = X86::REP_MOVSD;
4656 break;
4657 default: // BYTE aligned
4658 CountReg = SelectExpr(Node->getOperand(3));
4659 Opcode = X86::REP_MOVSB;
4660 break;
4661 }
4662
4663 // No matter what the alignment is, we put the source in ESI, the
4664 // destination in EDI, and the count in ECX.
4665 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
4666 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
4667 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
4668 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
4669 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
4670 BuildMI(BB, Opcode, 0);
4671 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004672 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00004673 case ISD::WRITEPORT:
4674 if (Node->getOperand(2).getValueType() != MVT::i16) {
4675 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
4676 exit(1);
4677 }
4678 Select(Node->getOperand(0)); // Emit the chain.
4679
4680 Tmp1 = SelectExpr(Node->getOperand(1));
4681 switch (Node->getOperand(1).getValueType()) {
4682 case MVT::i8:
4683 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
4684 Tmp2 = X86::OUT8ir; Opc = X86::OUT8rr;
4685 break;
4686 case MVT::i16:
4687 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(Tmp1);
4688 Tmp2 = X86::OUT16ir; Opc = X86::OUT16rr;
4689 break;
4690 case MVT::i32:
4691 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4692 Tmp2 = X86::OUT32ir; Opc = X86::OUT32rr;
4693 break;
4694 default:
4695 std::cerr << "llvm.writeport: invalid data type for X86 target";
4696 exit(1);
4697 }
4698
4699 // If the port is a single-byte constant, use the immediate form.
4700 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(2)))
4701 if ((CN->getValue() & 255) == CN->getValue()) {
4702 BuildMI(BB, Tmp2, 1).addImm(CN->getValue());
4703 return;
4704 }
4705
4706 // Otherwise, move the I/O port address into the DX register.
4707 unsigned Reg = SelectExpr(Node->getOperand(2));
4708 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
4709 BuildMI(BB, Opc, 0);
4710 return;
4711 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004712 assert(0 && "Should not be reached!");
4713}
4714
4715
4716/// createX86PatternInstructionSelector - This pass converts an LLVM function
4717/// into a machine code representation using pattern matching and a machine
4718/// description file.
4719///
4720FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004721 return new ISel(TM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004722}