blob: 4c77ff647cb4b35909c9411b024f5f612fd4a84e [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"
Chris Lattnerc6f41812005-05-12 23:06:28 +000017#include "llvm/CallingConv.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000018#include "llvm/Constants.h"
19#include "llvm/Instructions.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000020#include "llvm/Function.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000021#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000022#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/SelectionDAG.h"
25#include "llvm/CodeGen/SelectionDAGISel.h"
26#include "llvm/CodeGen/SSARegMap.h"
27#include "llvm/Target/TargetData.h"
28#include "llvm/Target/TargetLowering.h"
Chris Lattnerc5dcb532005-04-30 04:25:35 +000029#include "llvm/Target/TargetOptions.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000030#include "llvm/Support/CFG.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000031#include "llvm/Support/MathExtras.h"
32#include "llvm/ADT/Statistic.h"
33#include <set>
Jeff Cohen603fea92005-01-12 04:29:05 +000034#include <algorithm>
Chris Lattner8acb1ba2005-01-07 07:49:41 +000035using namespace llvm;
36
Chris Lattnerc6f41812005-05-12 23:06:28 +000037// FIXME: temporary.
38#include "llvm/Support/CommandLine.h"
39static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
40 cl::desc("Enable fastcc on X86"));
41
Chris Lattner67649df2005-05-14 06:52:07 +000042namespace {
43 // X86 Specific DAG Nodes
44 namespace X86ISD {
45 enum NodeType {
46 // Start the numbering where the builtin ops leave off.
47 FIRST_NUMBER = ISD::BUILTIN_OP_END,
48
49 /// FILD64m - This instruction implements SINT_TO_FP with a
50 /// 64-bit source in memory and a FP reg result. This corresponds to
51 /// the X86::FILD64m instruction. It has two inputs (token chain and
52 /// address) and two outputs (FP value and token chain).
53 FILD64m,
Chris Lattner239738a2005-05-14 08:48:15 +000054
55 /// CALL/TAILCALL - These operations represent an abstract X86 call
56 /// instruction, which includes a bunch of information. In particular the
57 /// operands of these node are:
58 ///
59 /// #0 - The incoming token chain
60 /// #1 - The callee
61 /// #2 - The number of arg bytes the caller pushes on the stack.
62 /// #3 - The number of arg bytes the callee pops off the stack.
63 /// #4 - The value to pass in AL/AX/EAX (optional)
64 /// #5 - The value to pass in DL/DX/EDX (optional)
65 ///
66 /// The result values of these nodes are:
67 ///
68 /// #0 - The outgoing token chain
69 /// #1 - The first register result value (optional)
70 /// #2 - The second register result value (optional)
71 ///
72 /// The CALL vs TAILCALL distinction boils down to whether the callee is
73 /// known not to modify the caller's stack frame, as is standard with
74 /// LLVM.
75 CALL,
76 TAILCALL,
Chris Lattner67649df2005-05-14 06:52:07 +000077 };
78 }
79}
80
Chris Lattner8acb1ba2005-01-07 07:49:41 +000081//===----------------------------------------------------------------------===//
82// X86TargetLowering - X86 Implementation of the TargetLowering interface
83namespace {
84 class X86TargetLowering : public TargetLowering {
85 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000086 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattneradf6a962005-05-13 18:50:42 +000087 int BytesToPopOnReturn; // Number of bytes ret should pop.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000088 public:
89 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
90 // Set up the TargetLowering object.
Chris Lattner4df0de92005-01-17 00:00:33 +000091
Chris Lattner653f7232005-05-13 22:46:57 +000092 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Chris Lattner4df0de92005-01-17 00:00:33 +000093 setShiftAmountType(MVT::i8);
94 setSetCCResultType(MVT::i8);
Chris Lattner6659bd72005-04-07 19:41:46 +000095 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner009b55b2005-01-19 03:36:30 +000096 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner4df0de92005-01-17 00:00:33 +000097
98 // Set up the register classes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000099 addRegisterClass(MVT::i8, X86::R8RegisterClass);
100 addRegisterClass(MVT::i16, X86::R16RegisterClass);
101 addRegisterClass(MVT::i32, X86::R32RegisterClass);
102 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000103
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000104 // FIXME: Eliminate these two classes when legalize can handle promotions
105 // well.
Chris Lattnerda2ce112005-01-16 07:34:08 +0000106/**/ addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000107
Chris Lattner67649df2005-05-14 06:52:07 +0000108 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Chris Lattnerda4d4692005-04-09 03:22:37 +0000109 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000110 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
111 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000112 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +0000113 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
114 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
115 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Chris Lattnerc610d422005-05-11 05:00:34 +0000116 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
117 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
118 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
119 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
120 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
121 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000122 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
123 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +0000124 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +0000125
Chris Lattner4e6ce5f2005-05-09 20:37:29 +0000126 setOperationAction(ISD::READIO , MVT::i1 , Expand);
127 setOperationAction(ISD::READIO , MVT::i8 , Expand);
128 setOperationAction(ISD::READIO , MVT::i16 , Expand);
129 setOperationAction(ISD::READIO , MVT::i32 , Expand);
130 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
131 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
132 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
133 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
134
Chris Lattnerc5dcb532005-04-30 04:25:35 +0000135 if (!UnsafeFPMath) {
136 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
137 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
138 }
139
Chris Lattnerda2ce112005-01-16 07:34:08 +0000140 // These should be promoted to a larger select which is supported.
141/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
142 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000143
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000144 computeRegisterProperties();
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000145
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000146 addLegalFPImmediate(+0.0); // FLD0
147 addLegalFPImmediate(+1.0); // FLD1
148 addLegalFPImmediate(-0.0); // FLD0/FCHS
149 addLegalFPImmediate(-1.0); // FLD1/FCHS
150 }
151
Chris Lattner3648c672005-05-13 21:44:04 +0000152 // Return the number of bytes that a function should pop when it returns (in
153 // addition to the space used by the return address).
154 //
155 unsigned getBytesToPopOnReturn() const { return BytesToPopOnReturn; }
156
Chris Lattner67649df2005-05-14 06:52:07 +0000157 /// LowerOperation - Provide custom lowering hooks for some operations.
158 ///
159 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
160
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000161 /// LowerArguments - This hook must be implemented to indicate how we should
162 /// lower the arguments for the specified function, into the specified DAG.
163 virtual std::vector<SDOperand>
164 LowerArguments(Function &F, SelectionDAG &DAG);
165
166 /// LowerCallTo - This hook lowers an abstract call to a function into an
167 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +0000168 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000169 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000170 bool isTailCall, SDOperand Callee, ArgListTy &Args,
171 SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000172
173 virtual std::pair<SDOperand, SDOperand>
174 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
175
176 virtual std::pair<SDOperand,SDOperand>
177 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
178 const Type *ArgTy, SelectionDAG &DAG);
179
180 virtual std::pair<SDOperand, SDOperand>
181 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
182 SelectionDAG &DAG);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000183 private:
184 // C Calling Convention implementation.
185 std::vector<SDOperand> LowerCCCArguments(Function &F, SelectionDAG &DAG);
186 std::pair<SDOperand, SDOperand>
187 LowerCCCCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
Chris Lattner2e7714a2005-05-13 20:29:13 +0000188 bool isTailCall,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000189 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
190
191 // Fast Calling Convention implementation.
192 std::vector<SDOperand> LowerFastCCArguments(Function &F, SelectionDAG &DAG);
193 std::pair<SDOperand, SDOperand>
Chris Lattner2e7714a2005-05-13 20:29:13 +0000194 LowerFastCCCallTo(SDOperand Chain, const Type *RetTy, bool isTailCall,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000195 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000196 };
197}
198
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000199std::vector<SDOperand>
200X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattnerc6f41812005-05-12 23:06:28 +0000201 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
202 return LowerFastCCArguments(F, DAG);
203 return LowerCCCArguments(F, DAG);
204}
205
206std::pair<SDOperand, SDOperand>
207X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
208 bool isVarArg, unsigned CallingConv,
Chris Lattneradf6a962005-05-13 18:50:42 +0000209 bool isTailCall,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000210 SDOperand Callee, ArgListTy &Args,
211 SelectionDAG &DAG) {
212 assert((!isVarArg || CallingConv == CallingConv::C) &&
213 "Only C takes varargs!");
214 if (CallingConv == CallingConv::Fast && EnableFastCC)
Chris Lattner2e7714a2005-05-13 20:29:13 +0000215 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
216 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000217}
218
219//===----------------------------------------------------------------------===//
Chris Lattner653f7232005-05-13 22:46:57 +0000220// C Calling Convention implementation
Chris Lattnerc6f41812005-05-12 23:06:28 +0000221//===----------------------------------------------------------------------===//
222
223std::vector<SDOperand>
224X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000225 std::vector<SDOperand> ArgValues;
226
Chris Lattner6415bb42005-05-10 03:53:18 +0000227 MachineFunction &MF = DAG.getMachineFunction();
228 MachineFrameInfo *MFI = MF.getFrameInfo();
229
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000230 // Add DAG nodes to load the arguments... On entry to a function on the X86,
231 // the stack frame looks like this:
232 //
233 // [ESP] -- return address
234 // [ESP + 4] -- first argument (leftmost lexically)
235 // [ESP + 8] -- second argument, if first argument is four bytes in size
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000236 // ...
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000237 //
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000238 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattnere4d5c442005-03-15 04:54:21 +0000239 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000240 MVT::ValueType ObjectVT = getValueType(I->getType());
241 unsigned ArgIncrement = 4;
242 unsigned ObjSize;
243 switch (ObjectVT) {
244 default: assert(0 && "Unhandled argument type!");
245 case MVT::i1:
246 case MVT::i8: ObjSize = 1; break;
247 case MVT::i16: ObjSize = 2; break;
248 case MVT::i32: ObjSize = 4; break;
249 case MVT::i64: ObjSize = ArgIncrement = 8; break;
250 case MVT::f32: ObjSize = 4; break;
251 case MVT::f64: ObjSize = ArgIncrement = 8; break;
252 }
253 // Create the frame index object for this incoming parameter...
254 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000255
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000256 // Create the SelectionDAG nodes corresponding to a load from this parameter
257 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
258
259 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
260 // dead loads.
261 SDOperand ArgValue;
262 if (!I->use_empty())
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000263 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
264 DAG.getSrcValue(NULL));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000265 else {
266 if (MVT::isInteger(ObjectVT))
267 ArgValue = DAG.getConstant(0, ObjectVT);
268 else
269 ArgValue = DAG.getConstantFP(0, ObjectVT);
270 }
271 ArgValues.push_back(ArgValue);
272
273 ArgOffset += ArgIncrement; // Move on to the next argument...
274 }
275
276 // If the function takes variable number of arguments, make a frame index for
277 // the start of the first vararg value... for expansion of llvm.va_start.
278 if (F.isVarArg())
279 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner3648c672005-05-13 21:44:04 +0000280 ReturnAddrIndex = 0; // No return address slot generated yet.
281 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattner4c52f0e2005-04-09 15:23:56 +0000282
283 // Finally, inform the code generator which regs we return values in.
284 switch (getValueType(F.getReturnType())) {
285 default: assert(0 && "Unknown type!");
286 case MVT::isVoid: break;
287 case MVT::i1:
288 case MVT::i8:
289 case MVT::i16:
290 case MVT::i32:
291 MF.addLiveOut(X86::EAX);
292 break;
293 case MVT::i64:
294 MF.addLiveOut(X86::EAX);
295 MF.addLiveOut(X86::EDX);
296 break;
297 case MVT::f32:
298 case MVT::f64:
299 MF.addLiveOut(X86::ST0);
300 break;
301 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000302 return ArgValues;
303}
304
Chris Lattner5188ad72005-01-08 19:28:19 +0000305std::pair<SDOperand, SDOperand>
Chris Lattnerc6f41812005-05-12 23:06:28 +0000306X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
Chris Lattner2e7714a2005-05-13 20:29:13 +0000307 bool isVarArg, bool isTailCall,
308 SDOperand Callee, ArgListTy &Args,
309 SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000310 // Count how many bytes are to be pushed on the stack.
311 unsigned NumBytes = 0;
312
313 if (Args.empty()) {
314 // Save zero bytes.
Chris Lattner16cd04d2005-05-12 23:24:06 +0000315 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattner5188ad72005-01-08 19:28:19 +0000316 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000317 } else {
318 for (unsigned i = 0, e = Args.size(); i != e; ++i)
319 switch (getValueType(Args[i].second)) {
320 default: assert(0 && "Unknown value type!");
321 case MVT::i1:
322 case MVT::i8:
323 case MVT::i16:
324 case MVT::i32:
325 case MVT::f32:
326 NumBytes += 4;
327 break;
328 case MVT::i64:
329 case MVT::f64:
330 NumBytes += 8;
331 break;
332 }
333
Chris Lattner16cd04d2005-05-12 23:24:06 +0000334 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattner5188ad72005-01-08 19:28:19 +0000335 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000336
337 // Arguments go on the stack in reverse order, as specified by the ABI.
338 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000339 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
340 DAG.getEntryNode());
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000341 std::vector<SDOperand> Stores;
342
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000343 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000344 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
345 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
346
347 switch (getValueType(Args[i].second)) {
348 default: assert(0 && "Unexpected ValueType for argument!");
349 case MVT::i1:
350 case MVT::i8:
351 case MVT::i16:
352 // Promote the integer to 32 bits. If the input type is signed use a
353 // sign extend, otherwise use a zero extend.
354 if (Args[i].second->isSigned())
355 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
356 else
357 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
358
359 // FALL THROUGH
360 case MVT::i32:
361 case MVT::f32:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000362 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000363 Args[i].first, PtrOff,
364 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000365 ArgOffset += 4;
366 break;
367 case MVT::i64:
368 case MVT::f64:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000369 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000370 Args[i].first, PtrOff,
371 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000372 ArgOffset += 8;
373 break;
374 }
375 }
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000376 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000377 }
378
379 std::vector<MVT::ValueType> RetVals;
380 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000381 RetVals.push_back(MVT::Other);
382
Chris Lattner239738a2005-05-14 08:48:15 +0000383 // The result values produced have to be legal. Promote the result.
384 switch (RetTyVT) {
385 case MVT::isVoid: break;
386 default:
387 RetVals.push_back(RetTyVT);
388 break;
389 case MVT::i1:
390 case MVT::i8:
391 case MVT::i16:
392 RetVals.push_back(MVT::i32);
393 break;
394 case MVT::f32:
395 RetVals.push_back(MVT::f64);
396 break;
397 case MVT::i64:
398 RetVals.push_back(MVT::i32);
399 RetVals.push_back(MVT::i32);
400 break;
401 }
402 std::vector<SDOperand> Ops;
403 Ops.push_back(Chain);
404 Ops.push_back(Callee);
405 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
406 Ops.push_back(DAG.getConstant(0, getPointerTy()));
407 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
408 RetVals, Ops);
409 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
410
411 SDOperand ResultVal;
412 switch (RetTyVT) {
413 case MVT::isVoid: break;
414 default:
415 ResultVal = TheCall.getValue(1);
416 break;
417 case MVT::i1:
418 case MVT::i8:
419 case MVT::i16:
420 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
421 break;
422 case MVT::f32:
423 // FIXME: we would really like to remember that this FP_ROUND operation is
424 // okay to eliminate if we allow excess FP precision.
425 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
426 break;
427 case MVT::i64:
428 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
429 TheCall.getValue(2));
430 break;
431 }
432
433 return std::make_pair(ResultVal, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000434}
435
Chris Lattner14824582005-01-09 00:01:27 +0000436std::pair<SDOperand, SDOperand>
437X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
438 // vastart just returns the address of the VarArgsFrameIndex slot.
439 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
440}
441
442std::pair<SDOperand,SDOperand> X86TargetLowering::
443LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
444 const Type *ArgTy, SelectionDAG &DAG) {
445 MVT::ValueType ArgVT = getValueType(ArgTy);
446 SDOperand Result;
447 if (!isVANext) {
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000448 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList,
449 DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000450 } else {
451 unsigned Amt;
452 if (ArgVT == MVT::i32)
453 Amt = 4;
454 else {
455 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
456 "Other types should have been promoted for varargs!");
457 Amt = 8;
458 }
459 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
460 DAG.getConstant(Amt, VAList.getValueType()));
461 }
462 return std::make_pair(Result, Chain);
463}
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000464
Chris Lattnerc6f41812005-05-12 23:06:28 +0000465//===----------------------------------------------------------------------===//
Chris Lattner653f7232005-05-13 22:46:57 +0000466// Fast Calling Convention implementation
Chris Lattnerc6f41812005-05-12 23:06:28 +0000467//===----------------------------------------------------------------------===//
468//
469// The X86 'fast' calling convention passes up to two integer arguments in
470// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
471// and requires that the callee pop its arguments off the stack (allowing proper
472// tail calls), and has the same return value conventions as C calling convs.
473//
Chris Lattner10d26452005-05-13 23:49:10 +0000474// This calling convention always arranges for the callee pop value to be 8n+4
475// bytes, which is needed for tail recursion elimination and stack alignment
476// reasons.
477//
Chris Lattnerc6f41812005-05-12 23:06:28 +0000478// Note that this can be enhanced in the future to pass fp vals in registers
479// (when we have a global fp allocator) and do other tricks.
480//
Chris Lattner63602fb2005-05-13 07:38:09 +0000481
482/// AddLiveIn - This helper function adds the specified physical register to the
483/// MachineFunction as a live in value. It also creates a corresponding virtual
484/// register for it.
485static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
486 TargetRegisterClass *RC) {
487 assert(RC->contains(PReg) && "Not the correct regclass!");
488 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
489 MF.addLiveIn(PReg, VReg);
490 return VReg;
491}
492
493
Chris Lattnerc6f41812005-05-12 23:06:28 +0000494std::vector<SDOperand>
495X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
496 std::vector<SDOperand> ArgValues;
497
498 MachineFunction &MF = DAG.getMachineFunction();
499 MachineFrameInfo *MFI = MF.getFrameInfo();
500
501 // Add DAG nodes to load the arguments... On entry to a function the stack
502 // frame looks like this:
503 //
504 // [ESP] -- return address
505 // [ESP + 4] -- first nonreg argument (leftmost lexically)
506 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
507 // ...
508 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
509
510 // Keep track of the number of integer regs passed so far. This can be either
511 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
512 // used).
513 unsigned NumIntRegs = 0;
514
515 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
516 MVT::ValueType ObjectVT = getValueType(I->getType());
517 unsigned ArgIncrement = 4;
518 unsigned ObjSize = 0;
519 SDOperand ArgValue;
520
521 switch (ObjectVT) {
522 default: assert(0 && "Unhandled argument type!");
523 case MVT::i1:
524 case MVT::i8:
525 if (NumIntRegs < 2) {
526 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000527 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
528 X86::R8RegisterClass);
529 ArgValue = DAG.getCopyFromReg(VReg, MVT::i8, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000530 DAG.setRoot(ArgValue.getValue(1));
531 }
532 ++NumIntRegs;
533 break;
534 }
535
536 ObjSize = 1;
537 break;
538 case MVT::i16:
539 if (NumIntRegs < 2) {
540 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000541 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
542 X86::R16RegisterClass);
543 ArgValue = DAG.getCopyFromReg(VReg, MVT::i16, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000544 DAG.setRoot(ArgValue.getValue(1));
545 }
546 ++NumIntRegs;
547 break;
548 }
549 ObjSize = 2;
550 break;
551 case MVT::i32:
552 if (NumIntRegs < 2) {
553 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000554 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
555 X86::R32RegisterClass);
556 ArgValue = DAG.getCopyFromReg(VReg, MVT::i32, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000557 DAG.setRoot(ArgValue.getValue(1));
558 }
559 ++NumIntRegs;
560 break;
561 }
562 ObjSize = 4;
563 break;
564 case MVT::i64:
565 if (NumIntRegs == 0) {
566 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000567 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
568 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000569
Chris Lattner63602fb2005-05-13 07:38:09 +0000570 SDOperand Low=DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot());
571 SDOperand Hi =DAG.getCopyFromReg(TopReg, MVT::i32, Low.getValue(1));
Chris Lattnerc6f41812005-05-12 23:06:28 +0000572 DAG.setRoot(Hi.getValue(1));
573
574 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
575 }
576 NumIntRegs = 2;
577 break;
578 } else if (NumIntRegs == 1) {
579 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000580 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
581 SDOperand Low = DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000582 DAG.setRoot(Low.getValue(1));
583
584 // Load the high part from memory.
585 // Create the frame index object for this incoming parameter...
586 int FI = MFI->CreateFixedObject(4, ArgOffset);
587 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
588 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
589 DAG.getSrcValue(NULL));
590 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
591 }
592 ArgOffset += 4;
593 NumIntRegs = 2;
594 break;
595 }
596 ObjSize = ArgIncrement = 8;
597 break;
598 case MVT::f32: ObjSize = 4; break;
599 case MVT::f64: ObjSize = ArgIncrement = 8; break;
600 }
601
602 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
603 // dead loads.
604 if (ObjSize && !I->use_empty()) {
605 // Create the frame index object for this incoming parameter...
606 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
607
608 // Create the SelectionDAG nodes corresponding to a load from this
609 // parameter.
610 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
611
612 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
613 DAG.getSrcValue(NULL));
614 } else if (ArgValue.Val == 0) {
615 if (MVT::isInteger(ObjectVT))
616 ArgValue = DAG.getConstant(0, ObjectVT);
617 else
618 ArgValue = DAG.getConstantFP(0, ObjectVT);
619 }
620 ArgValues.push_back(ArgValue);
621
622 if (ObjSize)
623 ArgOffset += ArgIncrement; // Move on to the next argument.
624 }
625
Chris Lattner10d26452005-05-13 23:49:10 +0000626 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
627 // arguments and the arguments after the retaddr has been pushed are aligned.
628 if ((ArgOffset & 7) == 0)
629 ArgOffset += 4;
630
Chris Lattner3648c672005-05-13 21:44:04 +0000631 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
632 ReturnAddrIndex = 0; // No return address slot generated yet.
633 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
Chris Lattnerc6f41812005-05-12 23:06:28 +0000634
635 // Finally, inform the code generator which regs we return values in.
636 switch (getValueType(F.getReturnType())) {
637 default: assert(0 && "Unknown type!");
638 case MVT::isVoid: break;
639 case MVT::i1:
640 case MVT::i8:
641 case MVT::i16:
642 case MVT::i32:
643 MF.addLiveOut(X86::EAX);
644 break;
645 case MVT::i64:
646 MF.addLiveOut(X86::EAX);
647 MF.addLiveOut(X86::EDX);
648 break;
649 case MVT::f32:
650 case MVT::f64:
651 MF.addLiveOut(X86::ST0);
652 break;
653 }
654 return ArgValues;
655}
656
657std::pair<SDOperand, SDOperand>
658X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
Chris Lattner2e7714a2005-05-13 20:29:13 +0000659 bool isTailCall, SDOperand Callee,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000660 ArgListTy &Args, SelectionDAG &DAG) {
661 // Count how many bytes are to be pushed on the stack.
662 unsigned NumBytes = 0;
663
664 // Keep track of the number of integer regs passed so far. This can be either
665 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
666 // used).
667 unsigned NumIntRegs = 0;
668
669 for (unsigned i = 0, e = Args.size(); i != e; ++i)
670 switch (getValueType(Args[i].second)) {
671 default: assert(0 && "Unknown value type!");
672 case MVT::i1:
673 case MVT::i8:
674 case MVT::i16:
675 case MVT::i32:
676 if (NumIntRegs < 2) {
677 ++NumIntRegs;
678 break;
679 }
680 // fall through
681 case MVT::f32:
682 NumBytes += 4;
683 break;
684 case MVT::i64:
685 if (NumIntRegs == 0) {
686 NumIntRegs = 2;
687 break;
688 } else if (NumIntRegs == 1) {
689 NumIntRegs = 2;
690 NumBytes += 4;
691 break;
692 }
693
694 // fall through
695 case MVT::f64:
696 NumBytes += 8;
697 break;
698 }
699
Chris Lattner10d26452005-05-13 23:49:10 +0000700 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
701 // arguments and the arguments after the retaddr has been pushed are aligned.
702 if ((NumBytes & 7) == 0)
703 NumBytes += 4;
704
Chris Lattner16cd04d2005-05-12 23:24:06 +0000705 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000706 DAG.getConstant(NumBytes, getPointerTy()));
707
708 // Arguments go on the stack in reverse order, as specified by the ABI.
709 unsigned ArgOffset = 0;
710 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
711 DAG.getEntryNode());
712 NumIntRegs = 0;
713 std::vector<SDOperand> Stores;
714 std::vector<SDOperand> RegValuesToPass;
715 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
716 switch (getValueType(Args[i].second)) {
717 default: assert(0 && "Unexpected ValueType for argument!");
718 case MVT::i1:
719 case MVT::i8:
720 case MVT::i16:
721 case MVT::i32:
722 if (NumIntRegs < 2) {
723 RegValuesToPass.push_back(Args[i].first);
724 ++NumIntRegs;
725 break;
726 }
727 // Fall through
728 case MVT::f32: {
729 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
730 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
731 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
732 Args[i].first, PtrOff,
733 DAG.getSrcValue(NULL)));
734 ArgOffset += 4;
735 break;
736 }
737 case MVT::i64:
738 if (NumIntRegs < 2) { // Can pass part of it in regs?
739 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
740 Args[i].first, DAG.getConstant(1, MVT::i32));
741 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
742 Args[i].first, DAG.getConstant(0, MVT::i32));
743 RegValuesToPass.push_back(Lo);
744 ++NumIntRegs;
745 if (NumIntRegs < 2) { // Pass both parts in regs?
746 RegValuesToPass.push_back(Hi);
747 ++NumIntRegs;
748 } else {
749 // Pass the high part in memory.
750 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
751 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
752 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner920c0aa2005-05-14 12:03:10 +0000753 Hi, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattnerc6f41812005-05-12 23:06:28 +0000754 ArgOffset += 4;
755 }
756 break;
757 }
758 // Fall through
759 case MVT::f64:
760 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
761 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
762 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
763 Args[i].first, PtrOff,
764 DAG.getSrcValue(NULL)));
765 ArgOffset += 8;
766 break;
767 }
768 }
769 if (!Stores.empty())
770 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
771
Chris Lattner10d26452005-05-13 23:49:10 +0000772 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
773 // arguments and the arguments after the retaddr has been pushed are aligned.
774 if ((ArgOffset & 7) == 0)
775 ArgOffset += 4;
776
Chris Lattner239738a2005-05-14 08:48:15 +0000777 std::vector<MVT::ValueType> RetVals;
778 MVT::ValueType RetTyVT = getValueType(RetTy);
779
780 RetVals.push_back(MVT::Other);
781
782 // The result values produced have to be legal. Promote the result.
783 switch (RetTyVT) {
784 case MVT::isVoid: break;
785 default:
786 RetVals.push_back(RetTyVT);
787 break;
788 case MVT::i1:
789 case MVT::i8:
790 case MVT::i16:
791 RetVals.push_back(MVT::i32);
792 break;
793 case MVT::f32:
794 RetVals.push_back(MVT::f64);
795 break;
796 case MVT::i64:
797 RetVals.push_back(MVT::i32);
798 RetVals.push_back(MVT::i32);
799 break;
800 }
801
802 std::vector<SDOperand> Ops;
803 Ops.push_back(Chain);
804 Ops.push_back(Callee);
805 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
806 // Callee pops all arg values on the stack.
807 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
808
809 // Pass register arguments as needed.
810 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
811
812 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
813 RetVals, Ops);
814 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
815
816 SDOperand ResultVal;
817 switch (RetTyVT) {
818 case MVT::isVoid: break;
819 default:
820 ResultVal = TheCall.getValue(1);
821 break;
822 case MVT::i1:
823 case MVT::i8:
824 case MVT::i16:
825 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
826 break;
827 case MVT::f32:
828 // FIXME: we would really like to remember that this FP_ROUND operation is
829 // okay to eliminate if we allow excess FP precision.
830 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
831 break;
832 case MVT::i64:
833 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
834 TheCall.getValue(2));
835 break;
836 }
837
838 return std::make_pair(ResultVal, Chain);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000839}
840
841
842
Chris Lattner14824582005-01-09 00:01:27 +0000843
844std::pair<SDOperand, SDOperand> X86TargetLowering::
845LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
846 SelectionDAG &DAG) {
847 SDOperand Result;
848 if (Depth) // Depths > 0 not supported yet!
849 Result = DAG.getConstant(0, getPointerTy());
850 else {
851 if (ReturnAddrIndex == 0) {
852 // Set up a frame object for the return address.
853 MachineFunction &MF = DAG.getMachineFunction();
854 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
855 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000856
Chris Lattner14824582005-01-09 00:01:27 +0000857 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
858
859 if (!isFrameAddress)
860 // Just load the return address
Chris Lattnerc6f41812005-05-12 23:06:28 +0000861 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
862 DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000863 else
864 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
865 DAG.getConstant(4, MVT::i32));
866 }
867 return std::make_pair(Result, Chain);
868}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000869
Chris Lattner67649df2005-05-14 06:52:07 +0000870/// LowerOperation - Provide custom lowering hooks for some operations.
871///
872SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
873 switch (Op.getOpcode()) {
874 default: assert(0 && "Should not custom lower this!");
875 case ISD::SINT_TO_FP:
876 assert(Op.getValueType() == MVT::f64 &&
877 Op.getOperand(0).getValueType() == MVT::i64 &&
878 "Unknown SINT_TO_FP to lower!");
879 // We lower sint64->FP into a store to a temporary stack slot, followed by a
880 // FILD64m node.
881 MachineFunction &MF = DAG.getMachineFunction();
882 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
883 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
884 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
885 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
886 std::vector<MVT::ValueType> RTs;
887 RTs.push_back(MVT::f64);
888 RTs.push_back(MVT::Other);
889 std::vector<SDOperand> Ops;
890 Ops.push_back(Store);
891 Ops.push_back(StackSlot);
892 return DAG.getNode(X86ISD::FILD64m, RTs, Ops);
893 }
894}
895
896
897//===----------------------------------------------------------------------===//
898// Pattern Matcher Implementation
899//===----------------------------------------------------------------------===//
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000900
Chris Lattner98a8ba02005-01-18 01:06:26 +0000901namespace {
902 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
903 /// SDOperand's instead of register numbers for the leaves of the matched
904 /// tree.
905 struct X86ISelAddressMode {
906 enum {
907 RegBase,
908 FrameIndexBase,
909 } BaseType;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000910
Chris Lattner98a8ba02005-01-18 01:06:26 +0000911 struct { // This is really a union, discriminated by BaseType!
912 SDOperand Reg;
913 int FrameIndex;
914 } Base;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000915
Chris Lattner98a8ba02005-01-18 01:06:26 +0000916 unsigned Scale;
917 SDOperand IndexReg;
918 unsigned Disp;
919 GlobalValue *GV;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000920
Chris Lattner98a8ba02005-01-18 01:06:26 +0000921 X86ISelAddressMode()
922 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
923 }
924 };
925}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000926
927
928namespace {
929 Statistic<>
930 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
931
932 //===--------------------------------------------------------------------===//
933 /// ISel - X86 specific code to select X86 machine instructions for
934 /// SelectionDAG operations.
935 ///
936 class ISel : public SelectionDAGISel {
937 /// ContainsFPCode - Every instruction we select that uses or defines a FP
938 /// register should set this to true.
939 bool ContainsFPCode;
940
941 /// X86Lowering - This object fully describes how to lower LLVM code to an
942 /// X86-specific SelectionDAG.
943 X86TargetLowering X86Lowering;
944
Chris Lattner11333092005-01-11 03:11:44 +0000945 /// RegPressureMap - This keeps an approximate count of the number of
946 /// registers required to evaluate each node in the graph.
947 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000948
949 /// ExprMap - As shared expressions are codegen'd, we keep track of which
950 /// vreg the value is produced in, so we only emit one copy of each compiled
951 /// tree.
952 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000953
954 public:
955 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
956 }
957
Chris Lattner67b1c3c2005-01-21 21:35:14 +0000958 virtual const char *getPassName() const {
959 return "X86 Pattern Instruction Selection";
960 }
961
Chris Lattner11333092005-01-11 03:11:44 +0000962 unsigned getRegPressure(SDOperand O) {
963 return RegPressureMap[O.Val];
964 }
965 unsigned ComputeRegPressure(SDOperand O);
966
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000967 /// InstructionSelectBasicBlock - This callback is invoked by
968 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000969 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000970
Chris Lattner63602fb2005-05-13 07:38:09 +0000971 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
972
Chris Lattner44129b52005-01-25 20:03:11 +0000973 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
974 bool FloatPromoteOk = false);
Chris Lattnera5ade062005-01-11 21:19:59 +0000975 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +0000976 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattnera5ade062005-01-11 21:19:59 +0000977
Chris Lattner30ea1e92005-01-19 07:37:26 +0000978 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000979 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000980 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000981 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
982 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000983 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000984
985 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
986 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
987 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000988 void Select(SDOperand N);
989 };
990}
991
Chris Lattner6415bb42005-05-10 03:53:18 +0000992/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
993/// the main function.
994static void EmitSpecialCodeForMain(MachineBasicBlock *BB,
995 MachineFrameInfo *MFI) {
996 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
997 int CWFrameIdx = MFI->CreateStackObject(2, 2);
998 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
999
1000 // Set the high part to be 64-bit precision.
1001 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1002 CWFrameIdx, 1).addImm(2);
1003
1004 // Reload the modified control word now.
1005 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1006}
1007
Chris Lattner63602fb2005-05-13 07:38:09 +00001008void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
1009 // If this function has live-in values, emit the copies from pregs to vregs at
1010 // the top of the function, before anything else.
1011 MachineBasicBlock *BB = MF.begin();
1012 if (MF.livein_begin() != MF.livein_end()) {
1013 SSARegMap *RegMap = MF.getSSARegMap();
1014 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
1015 E = MF.livein_end(); LI != E; ++LI) {
1016 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
1017 if (RC == X86::R8RegisterClass) {
1018 BuildMI(BB, X86::MOV8rr, 1, LI->second).addReg(LI->first);
1019 } else if (RC == X86::R16RegisterClass) {
1020 BuildMI(BB, X86::MOV16rr, 1, LI->second).addReg(LI->first);
1021 } else if (RC == X86::R32RegisterClass) {
1022 BuildMI(BB, X86::MOV32rr, 1, LI->second).addReg(LI->first);
1023 } else if (RC == X86::RFPRegisterClass) {
1024 BuildMI(BB, X86::FpMOV, 1, LI->second).addReg(LI->first);
1025 } else {
1026 assert(0 && "Unknown regclass!");
1027 }
1028 }
1029 }
1030
1031
1032 // If this is main, emit special code for main.
1033 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
1034 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
1035}
1036
1037
Chris Lattner7dbcb752005-01-12 04:21:28 +00001038/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
1039/// when it has created a SelectionDAG for us to codegen.
1040void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
1041 // While we're doing this, keep track of whether we see any FP code for
1042 // FP_REG_KILL insertion.
1043 ContainsFPCode = false;
Chris Lattner6415bb42005-05-10 03:53:18 +00001044 MachineFunction *MF = BB->getParent();
Chris Lattner7dbcb752005-01-12 04:21:28 +00001045
1046 // Scan the PHI nodes that already are inserted into this basic block. If any
1047 // of them is a PHI of a floating point value, we need to insert an
1048 // FP_REG_KILL.
Chris Lattner6415bb42005-05-10 03:53:18 +00001049 SSARegMap *RegMap = MF->getSSARegMap();
Chris Lattner63602fb2005-05-13 07:38:09 +00001050 if (BB != MF->begin())
1051 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
1052 I != E; ++I) {
1053 assert(I->getOpcode() == X86::PHI &&
1054 "Isn't just PHI nodes?");
1055 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
1056 X86::RFPRegisterClass) {
1057 ContainsFPCode = true;
1058 break;
1059 }
Chris Lattner7dbcb752005-01-12 04:21:28 +00001060 }
Chris Lattner6415bb42005-05-10 03:53:18 +00001061
Chris Lattner7dbcb752005-01-12 04:21:28 +00001062 // Compute the RegPressureMap, which is an approximation for the number of
1063 // registers required to compute each node.
1064 ComputeRegPressure(DAG.getRoot());
1065
1066 // Codegen the basic block.
1067 Select(DAG.getRoot());
1068
1069 // Finally, look at all of the successors of this block. If any contain a PHI
1070 // node of FP type, we need to insert an FP_REG_KILL in this block.
1071 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
1072 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
1073 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
1074 I != E && I->getOpcode() == X86::PHI; ++I) {
1075 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
1076 X86::RFPRegisterClass) {
1077 ContainsFPCode = true;
1078 break;
1079 }
1080 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001081
Chris Lattnere3e0f272005-05-09 03:36:39 +00001082 // Final check, check LLVM BB's that are successors to the LLVM BB
1083 // corresponding to BB for FP PHI nodes.
1084 const BasicBlock *LLVMBB = BB->getBasicBlock();
1085 const PHINode *PN;
1086 if (!ContainsFPCode)
1087 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
1088 SI != E && !ContainsFPCode; ++SI)
1089 for (BasicBlock::const_iterator II = SI->begin();
1090 (PN = dyn_cast<PHINode>(II)); ++II)
1091 if (PN->getType()->isFloatingPoint()) {
1092 ContainsFPCode = true;
1093 break;
1094 }
1095
1096
Chris Lattner7dbcb752005-01-12 04:21:28 +00001097 // Insert FP_REG_KILL instructions into basic blocks that need them. This
1098 // only occurs due to the floating point stackifier not being aggressive
1099 // enough to handle arbitrary global stackification.
1100 //
1101 // Currently we insert an FP_REG_KILL instruction into each block that uses or
1102 // defines a floating point virtual register.
1103 //
1104 // When the global register allocators (like linear scan) finally update live
1105 // variable analysis, we can keep floating point values in registers across
1106 // basic blocks. This will be a huge win, but we are waiting on the global
1107 // allocators before we can do this.
1108 //
Chris Lattner71df3f82005-03-30 01:10:00 +00001109 if (ContainsFPCode) {
Chris Lattner7dbcb752005-01-12 04:21:28 +00001110 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
1111 ++NumFPKill;
1112 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001113
Chris Lattner7dbcb752005-01-12 04:21:28 +00001114 // Clear state used for selection.
1115 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +00001116 RegPressureMap.clear();
1117}
1118
1119
Chris Lattner11333092005-01-11 03:11:44 +00001120// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
1121// for the number of registers required to compute each node. This is basically
1122// computing a generalized form of the Sethi-Ullman number for each node.
1123unsigned ISel::ComputeRegPressure(SDOperand O) {
1124 SDNode *N = O.Val;
1125 unsigned &Result = RegPressureMap[N];
1126 if (Result) return Result;
1127
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001128 // FIXME: Should operations like CALL (which clobber lots o regs) have a
1129 // higher fixed cost??
1130
Chris Lattnerc4b6a782005-01-11 22:29:12 +00001131 if (N->getNumOperands() == 0) {
1132 Result = 1;
1133 } else {
1134 unsigned MaxRegUse = 0;
1135 unsigned NumExtraMaxRegUsers = 0;
1136 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1137 unsigned Regs;
1138 if (N->getOperand(i).getOpcode() == ISD::Constant)
1139 Regs = 0;
1140 else
1141 Regs = ComputeRegPressure(N->getOperand(i));
1142 if (Regs > MaxRegUse) {
1143 MaxRegUse = Regs;
1144 NumExtraMaxRegUsers = 0;
1145 } else if (Regs == MaxRegUse &&
1146 N->getOperand(i).getValueType() != MVT::Other) {
1147 ++NumExtraMaxRegUsers;
1148 }
Chris Lattner11333092005-01-11 03:11:44 +00001149 }
Chris Lattner90d1be72005-01-17 22:56:09 +00001150
1151 if (O.getOpcode() != ISD::TokenFactor)
1152 Result = MaxRegUse+NumExtraMaxRegUsers;
1153 else
Chris Lattner869e0432005-01-17 23:02:13 +00001154 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +00001155 }
Chris Lattnerafce4302005-01-12 02:19:06 +00001156
Chris Lattner837caa72005-01-11 23:21:30 +00001157 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +00001158 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00001159}
1160
Chris Lattnerbf52d492005-01-20 16:50:16 +00001161/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
1162/// The DAG cannot have cycles in it, by definition, so the visited set is not
1163/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
1164/// reuse, so it prevents exponential cases.
1165///
1166static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
1167 std::set<SDNode*> &Visited) {
1168 if (N == Op) return true; // Found it.
1169 SDNode *Node = N.Val;
Chris Lattnerfb0f53f2005-01-21 21:43:02 +00001170 if (Node->getNumOperands() == 0 || // Leaf?
1171 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattnerbf52d492005-01-20 16:50:16 +00001172 if (!Visited.insert(Node).second) return false; // Already visited?
1173
1174 // Recurse for the first N-1 operands.
1175 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1176 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
1177 return true;
1178
1179 // Tail recurse for the last operand.
1180 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
1181}
1182
Chris Lattner98a8ba02005-01-18 01:06:26 +00001183X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
1184 X86AddressMode Result;
1185
1186 // If we need to emit two register operands, emit the one with the highest
1187 // register pressure first.
1188 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
1189 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +00001190 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +00001191 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +00001192 std::set<SDNode*> Visited;
1193 EmitBaseThenIndex = true;
1194 // If Base ends up pointing to Index, we must emit index first. This is
1195 // because of the way we fold loads, we may end up doing bad things with
1196 // the folded add.
1197 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
1198 EmitBaseThenIndex = false;
1199 } else {
1200 std::set<SDNode*> Visited;
1201 EmitBaseThenIndex = false;
1202 // If Base ends up pointing to Index, we must emit index first. This is
1203 // because of the way we fold loads, we may end up doing bad things with
1204 // the folded add.
1205 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
1206 EmitBaseThenIndex = true;
1207 }
1208
1209 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001210 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1211 Result.IndexReg = SelectExpr(IAM.IndexReg);
1212 } else {
1213 Result.IndexReg = SelectExpr(IAM.IndexReg);
1214 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1215 }
Chris Lattnerbf52d492005-01-20 16:50:16 +00001216
Chris Lattner98a8ba02005-01-18 01:06:26 +00001217 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
1218 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1219 } else if (IAM.IndexReg.Val) {
1220 Result.IndexReg = SelectExpr(IAM.IndexReg);
1221 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001222
Chris Lattner98a8ba02005-01-18 01:06:26 +00001223 switch (IAM.BaseType) {
1224 case X86ISelAddressMode::RegBase:
1225 Result.BaseType = X86AddressMode::RegBase;
1226 break;
1227 case X86ISelAddressMode::FrameIndexBase:
1228 Result.BaseType = X86AddressMode::FrameIndexBase;
1229 Result.Base.FrameIndex = IAM.Base.FrameIndex;
1230 break;
1231 default:
1232 assert(0 && "Unknown base type!");
1233 break;
1234 }
1235 Result.Scale = IAM.Scale;
1236 Result.Disp = IAM.Disp;
1237 Result.GV = IAM.GV;
1238 return Result;
1239}
1240
1241/// SelectAddress - Pattern match the maximal addressing mode for this node and
1242/// emit all of the leaf registers.
1243void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
1244 X86ISelAddressMode IAM;
1245 MatchAddress(N, IAM);
1246 AM = SelectAddrExprs(IAM);
1247}
1248
1249/// MatchAddress - Add the specified node to the specified addressing mode,
1250/// returning true if it cannot be done. This just pattern matches for the
1251/// addressing mode, it does not cause any code to be emitted. For that, use
1252/// SelectAddress.
1253bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001254 switch (N.getOpcode()) {
1255 default: break;
1256 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +00001257 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
1258 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001259 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
1260 return false;
1261 }
1262 break;
1263 case ISD::GlobalAddress:
1264 if (AM.GV == 0) {
1265 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1266 return false;
1267 }
1268 break;
1269 case ISD::Constant:
1270 AM.Disp += cast<ConstantSDNode>(N)->getValue();
1271 return false;
1272 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +00001273 // We might have folded the load into this shift, so don't regen the value
1274 // if so.
1275 if (ExprMap.count(N)) break;
1276
Chris Lattner98a8ba02005-01-18 01:06:26 +00001277 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001278 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
1279 unsigned Val = CN->getValue();
1280 if (Val == 1 || Val == 2 || Val == 3) {
1281 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +00001282 SDOperand ShVal = N.Val->getOperand(0);
1283
1284 // Okay, we know that we have a scale by now. However, if the scaled
1285 // value is an add of something and a constant, we can fold the
1286 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +00001287 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +00001288 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001289 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +00001290 ConstantSDNode *AddVal =
1291 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
1292 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +00001293 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001294 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +00001295 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001296 return false;
1297 }
1298 }
1299 break;
Chris Lattner947d5442005-01-11 19:37:02 +00001300 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +00001301 // We might have folded the load into this mul, so don't regen the value if
1302 // so.
1303 if (ExprMap.count(N)) break;
1304
Chris Lattner947d5442005-01-11 19:37:02 +00001305 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +00001306 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
1307 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +00001308 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
1309 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
1310 AM.Scale = unsigned(CN->getValue())-1;
1311
1312 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001313 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +00001314
1315 // Okay, we know that we have a scale by now. However, if the scaled
1316 // value is an add of something and a constant, we can fold the
1317 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +00001318 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +00001319 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001320 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +00001321 ConstantSDNode *AddVal =
1322 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
1323 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001324 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001325 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +00001326 }
1327
1328 AM.IndexReg = AM.Base.Reg = Reg;
1329 return false;
1330 }
1331 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001332
1333 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +00001334 // We might have folded the load into this mul, so don't regen the value if
1335 // so.
1336 if (ExprMap.count(N)) break;
1337
Chris Lattner98a8ba02005-01-18 01:06:26 +00001338 X86ISelAddressMode Backup = AM;
1339 if (!MatchAddress(N.Val->getOperand(0), AM) &&
1340 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001341 return false;
1342 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +00001343 if (!MatchAddress(N.Val->getOperand(1), AM) &&
1344 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +00001345 return false;
1346 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001347 break;
1348 }
1349 }
1350
Chris Lattnera95589b2005-01-11 04:40:19 +00001351 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +00001352 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +00001353 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +00001354 if (AM.IndexReg.Val == 0) {
1355 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +00001356 AM.Scale = 1;
1357 return false;
1358 }
1359
1360 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001361 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +00001362 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001363
1364 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +00001365 AM.BaseType = X86ISelAddressMode::RegBase;
1366 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001367 return false;
1368}
1369
1370/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
1371/// assuming that the temporary registers are in the 8-bit register class.
1372///
1373/// Tmp1 = setcc1
1374/// Tmp2 = setcc2
1375/// DestReg = logicalop Tmp1, Tmp2
1376///
1377static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
1378 unsigned SetCC2, unsigned LogicalOp,
1379 unsigned DestReg) {
1380 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
1381 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1382 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1383 BuildMI(BB, SetCC1, 0, Tmp1);
1384 BuildMI(BB, SetCC2, 0, Tmp2);
1385 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
1386}
1387
1388/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
1389/// condition codes match the specified SetCCOpcode. Note that some conditions
1390/// require multiple instructions to generate the correct value.
1391static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
1392 ISD::CondCode SetCCOpcode, bool isFP) {
1393 unsigned Opc;
1394 if (!isFP) {
1395 switch (SetCCOpcode) {
1396 default: assert(0 && "Illegal integer SetCC!");
1397 case ISD::SETEQ: Opc = X86::SETEr; break;
1398 case ISD::SETGT: Opc = X86::SETGr; break;
1399 case ISD::SETGE: Opc = X86::SETGEr; break;
1400 case ISD::SETLT: Opc = X86::SETLr; break;
1401 case ISD::SETLE: Opc = X86::SETLEr; break;
1402 case ISD::SETNE: Opc = X86::SETNEr; break;
1403 case ISD::SETULT: Opc = X86::SETBr; break;
1404 case ISD::SETUGT: Opc = X86::SETAr; break;
1405 case ISD::SETULE: Opc = X86::SETBEr; break;
1406 case ISD::SETUGE: Opc = X86::SETAEr; break;
1407 }
1408 } else {
1409 // On a floating point condition, the flags are set as follows:
1410 // ZF PF CF op
1411 // 0 | 0 | 0 | X > Y
1412 // 0 | 0 | 1 | X < Y
1413 // 1 | 0 | 0 | X == Y
1414 // 1 | 1 | 1 | unordered
1415 //
1416 switch (SetCCOpcode) {
1417 default: assert(0 && "Invalid FP setcc!");
1418 case ISD::SETUEQ:
1419 case ISD::SETEQ:
1420 Opc = X86::SETEr; // True if ZF = 1
1421 break;
1422 case ISD::SETOGT:
1423 case ISD::SETGT:
1424 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
1425 break;
1426 case ISD::SETOGE:
1427 case ISD::SETGE:
1428 Opc = X86::SETAEr; // True if CF = 0
1429 break;
1430 case ISD::SETULT:
1431 case ISD::SETLT:
1432 Opc = X86::SETBr; // True if CF = 1
1433 break;
1434 case ISD::SETULE:
1435 case ISD::SETLE:
1436 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
1437 break;
1438 case ISD::SETONE:
1439 case ISD::SETNE:
1440 Opc = X86::SETNEr; // True if ZF = 0
1441 break;
1442 case ISD::SETUO:
1443 Opc = X86::SETPr; // True if PF = 1
1444 break;
1445 case ISD::SETO:
1446 Opc = X86::SETNPr; // True if PF = 0
1447 break;
1448 case ISD::SETOEQ: // !PF & ZF
1449 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
1450 return;
1451 case ISD::SETOLT: // !PF & CF
1452 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
1453 return;
1454 case ISD::SETOLE: // !PF & (CF || ZF)
1455 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
1456 return;
1457 case ISD::SETUGT: // PF | (!ZF & !CF)
1458 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
1459 return;
1460 case ISD::SETUGE: // PF | !CF
1461 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
1462 return;
1463 case ISD::SETUNE: // PF | !ZF
1464 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
1465 return;
1466 }
1467 }
1468 BuildMI(BB, Opc, 0, DestReg);
1469}
1470
1471
1472/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
1473/// the Dest block if the Cond condition is true. If we cannot fold this
1474/// condition into the branch, return true.
1475///
Chris Lattner6c07aee2005-01-11 04:06:27 +00001476bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
1477 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001478 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
1479 // B) using two conditional branches instead of one condbr, two setcc's, and
1480 // an or.
1481 if ((Cond.getOpcode() == ISD::OR ||
1482 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
1483 // And and or set the flags for us, so there is no need to emit a TST of the
1484 // result. It is only safe to do this if there is only a single use of the
1485 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +00001486 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001487 SelectExpr(Cond);
1488 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
1489 return false;
1490 }
1491
1492 // Codegen br not C -> JE.
1493 if (Cond.getOpcode() == ISD::XOR)
1494 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
1495 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +00001496 unsigned CondR;
1497 if (getRegPressure(Chain) > getRegPressure(Cond)) {
1498 Select(Chain);
1499 CondR = SelectExpr(Cond.Val->getOperand(0));
1500 } else {
1501 CondR = SelectExpr(Cond.Val->getOperand(0));
1502 Select(Chain);
1503 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001504 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
1505 BuildMI(BB, X86::JE, 1).addMBB(Dest);
1506 return false;
1507 }
1508
1509 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
1510 if (SetCC == 0)
1511 return true; // Can only handle simple setcc's so far.
1512
1513 unsigned Opc;
1514
1515 // Handle integer conditions first.
1516 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1517 switch (SetCC->getCondition()) {
1518 default: assert(0 && "Illegal integer SetCC!");
1519 case ISD::SETEQ: Opc = X86::JE; break;
1520 case ISD::SETGT: Opc = X86::JG; break;
1521 case ISD::SETGE: Opc = X86::JGE; break;
1522 case ISD::SETLT: Opc = X86::JL; break;
1523 case ISD::SETLE: Opc = X86::JLE; break;
1524 case ISD::SETNE: Opc = X86::JNE; break;
1525 case ISD::SETULT: Opc = X86::JB; break;
1526 case ISD::SETUGT: Opc = X86::JA; break;
1527 case ISD::SETULE: Opc = X86::JBE; break;
1528 case ISD::SETUGE: Opc = X86::JAE; break;
1529 }
Chris Lattner6c07aee2005-01-11 04:06:27 +00001530 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001531 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001532 BuildMI(BB, Opc, 1).addMBB(Dest);
1533 return false;
1534 }
1535
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001536 unsigned Opc2 = 0; // Second branch if needed.
1537
1538 // On a floating point condition, the flags are set as follows:
1539 // ZF PF CF op
1540 // 0 | 0 | 0 | X > Y
1541 // 0 | 0 | 1 | X < Y
1542 // 1 | 0 | 0 | X == Y
1543 // 1 | 1 | 1 | unordered
1544 //
1545 switch (SetCC->getCondition()) {
1546 default: assert(0 && "Invalid FP setcc!");
1547 case ISD::SETUEQ:
1548 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
1549 case ISD::SETOGT:
1550 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
1551 case ISD::SETOGE:
1552 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
1553 case ISD::SETULT:
1554 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
1555 case ISD::SETULE:
1556 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
1557 case ISD::SETONE:
1558 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
1559 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
1560 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
1561 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1562 Opc = X86::JA; // ZF = 0 & CF = 0
1563 Opc2 = X86::JP; // PF = 1
1564 break;
1565 case ISD::SETUGE: // PF = 1 | CF = 0
1566 Opc = X86::JAE; // CF = 0
1567 Opc2 = X86::JP; // PF = 1
1568 break;
1569 case ISD::SETUNE: // PF = 1 | ZF = 0
1570 Opc = X86::JNE; // ZF = 0
1571 Opc2 = X86::JP; // PF = 1
1572 break;
1573 case ISD::SETOEQ: // PF = 0 & ZF = 1
1574 //X86::JNP, X86::JE
1575 //X86::AND8rr
1576 return true; // FIXME: Emit more efficient code for this branch.
1577 case ISD::SETOLT: // PF = 0 & CF = 1
1578 //X86::JNP, X86::JB
1579 //X86::AND8rr
1580 return true; // FIXME: Emit more efficient code for this branch.
1581 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1582 //X86::JNP, X86::JBE
1583 //X86::AND8rr
1584 return true; // FIXME: Emit more efficient code for this branch.
1585 }
1586
Chris Lattner6c07aee2005-01-11 04:06:27 +00001587 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001588 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001589 BuildMI(BB, Opc, 1).addMBB(Dest);
1590 if (Opc2)
1591 BuildMI(BB, Opc2, 1).addMBB(Dest);
1592 return false;
1593}
1594
Chris Lattner24aad1b2005-01-10 22:10:13 +00001595/// EmitSelectCC - Emit code into BB that performs a select operation between
1596/// the two registers RTrue and RFalse, generating a result into RDest. Return
1597/// true if the fold cannot be performed.
1598///
1599void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
1600 unsigned RTrue, unsigned RFalse, unsigned RDest) {
1601 enum Condition {
1602 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
1603 NOT_SET
1604 } CondCode = NOT_SET;
1605
1606 static const unsigned CMOVTAB16[] = {
1607 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
1608 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001609 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001610 };
1611 static const unsigned CMOVTAB32[] = {
1612 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
1613 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001614 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001615 };
1616 static const unsigned CMOVTABFP[] = {
1617 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
1618 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
1619 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
1620 };
1621
1622 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
1623 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1624 switch (SetCC->getCondition()) {
1625 default: assert(0 && "Unknown integer comparison!");
1626 case ISD::SETEQ: CondCode = EQ; break;
1627 case ISD::SETGT: CondCode = GT; break;
1628 case ISD::SETGE: CondCode = GE; break;
1629 case ISD::SETLT: CondCode = LT; break;
1630 case ISD::SETLE: CondCode = LE; break;
1631 case ISD::SETNE: CondCode = NE; break;
1632 case ISD::SETULT: CondCode = B; break;
1633 case ISD::SETUGT: CondCode = A; break;
1634 case ISD::SETULE: CondCode = BE; break;
1635 case ISD::SETUGE: CondCode = AE; break;
1636 }
1637 } else {
1638 // On a floating point condition, the flags are set as follows:
1639 // ZF PF CF op
1640 // 0 | 0 | 0 | X > Y
1641 // 0 | 0 | 1 | X < Y
1642 // 1 | 0 | 0 | X == Y
1643 // 1 | 1 | 1 | unordered
1644 //
1645 switch (SetCC->getCondition()) {
1646 default: assert(0 && "Unknown FP comparison!");
1647 case ISD::SETUEQ:
1648 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
1649 case ISD::SETOGT:
1650 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
1651 case ISD::SETOGE:
1652 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
1653 case ISD::SETULT:
1654 case ISD::SETLT: CondCode = B; break; // True if CF = 1
1655 case ISD::SETULE:
1656 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
1657 case ISD::SETONE:
1658 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
1659 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1660 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1661 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1662 case ISD::SETUGE: // PF = 1 | CF = 0
1663 case ISD::SETUNE: // PF = 1 | ZF = 0
1664 case ISD::SETOEQ: // PF = 0 & ZF = 1
1665 case ISD::SETOLT: // PF = 0 & CF = 1
1666 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1667 // We cannot emit this comparison as a single cmov.
1668 break;
1669 }
1670 }
1671 }
1672
1673 unsigned Opc = 0;
1674 if (CondCode != NOT_SET) {
1675 switch (SVT) {
1676 default: assert(0 && "Cannot select this type!");
1677 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1678 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001679 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001680 }
1681 }
1682
1683 // Finally, if we weren't able to fold this, just emit the condition and test
1684 // it.
1685 if (CondCode == NOT_SET || Opc == 0) {
1686 // Get the condition into the zero flag.
1687 unsigned CondReg = SelectExpr(Cond);
1688 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1689
1690 switch (SVT) {
1691 default: assert(0 && "Cannot select this type!");
1692 case MVT::i16: Opc = X86::CMOVE16rr; break;
1693 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001694 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001695 }
1696 } else {
1697 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001698 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001699 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001700 }
1701 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1702}
1703
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001704void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001705 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001706 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1707 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001708 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001709 switch (RHS.getValueType()) {
1710 default: break;
1711 case MVT::i1:
1712 case MVT::i8: Opc = X86::CMP8mi; break;
1713 case MVT::i16: Opc = X86::CMP16mi; break;
1714 case MVT::i32: Opc = X86::CMP32mi; break;
1715 }
1716 if (Opc) {
1717 X86AddressMode AM;
1718 EmitFoldedLoad(LHS, AM);
1719 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1720 return;
1721 }
1722 }
1723
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001724 switch (RHS.getValueType()) {
1725 default: break;
1726 case MVT::i1:
1727 case MVT::i8: Opc = X86::CMP8ri; break;
1728 case MVT::i16: Opc = X86::CMP16ri; break;
1729 case MVT::i32: Opc = X86::CMP32ri; break;
1730 }
1731 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001732 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001733 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1734 return;
1735 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001736 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1737 if (CN->isExactlyValue(+0.0) ||
1738 CN->isExactlyValue(-0.0)) {
1739 unsigned Reg = SelectExpr(LHS);
1740 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1741 BuildMI(BB, X86::FNSTSW8r, 0);
1742 BuildMI(BB, X86::SAHF, 1);
Chris Lattner7805fa42005-03-17 16:29:26 +00001743 return;
Chris Lattner7f2afac2005-01-14 22:37:41 +00001744 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001745 }
1746
Chris Lattneref6806c2005-01-12 02:02:48 +00001747 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001748 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001749 switch (RHS.getValueType()) {
1750 default: break;
1751 case MVT::i1:
1752 case MVT::i8: Opc = X86::CMP8mr; break;
1753 case MVT::i16: Opc = X86::CMP16mr; break;
1754 case MVT::i32: Opc = X86::CMP32mr; break;
1755 }
1756 if (Opc) {
1757 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001758 EmitFoldedLoad(LHS, AM);
1759 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001760 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1761 return;
1762 }
1763 }
1764
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001765 switch (LHS.getValueType()) {
1766 default: assert(0 && "Cannot compare this value!");
1767 case MVT::i1:
1768 case MVT::i8: Opc = X86::CMP8rr; break;
1769 case MVT::i16: Opc = X86::CMP16rr; break;
1770 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001771 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001772 }
Chris Lattner11333092005-01-11 03:11:44 +00001773 unsigned Tmp1, Tmp2;
1774 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1775 Tmp1 = SelectExpr(LHS);
1776 Tmp2 = SelectExpr(RHS);
1777 } else {
1778 Tmp2 = SelectExpr(RHS);
1779 Tmp1 = SelectExpr(LHS);
1780 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001781 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1782}
1783
Chris Lattnera5ade062005-01-11 21:19:59 +00001784/// isFoldableLoad - Return true if this is a load instruction that can safely
1785/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00001786bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1787 if (Op.getOpcode() == ISD::LOAD) {
1788 // FIXME: currently can't fold constant pool indexes.
1789 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1790 return false;
1791 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
1792 cast<MVTSDNode>(Op)->getExtraValueType() == MVT::f32) {
1793 // FIXME: currently can't fold constant pool indexes.
1794 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1795 return false;
1796 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001797 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00001798 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001799
1800 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001801 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1802 if (ExprMap.count(Op.getValue(1))) return false;
1803 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001804 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001805
Chris Lattner4ff348b2005-01-17 06:26:58 +00001806 // If there is not just one use of its value, we cannot fold.
1807 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1808
1809 // Finally, we cannot fold the load into the operation if this would induce a
1810 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1811 // operand of the operation we are folding the load into) can possible use the
1812 // chain node defined by the load.
1813 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1814 std::set<SDNode*> Visited;
1815 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1816 return false;
1817 }
1818 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001819}
1820
Chris Lattner4ff348b2005-01-17 06:26:58 +00001821
Chris Lattnera5ade062005-01-11 21:19:59 +00001822/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1823/// and compute the address being loaded into AM.
1824void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1825 SDOperand Chain = Op.getOperand(0);
1826 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001827
Chris Lattnera5ade062005-01-11 21:19:59 +00001828 if (getRegPressure(Chain) > getRegPressure(Address)) {
1829 Select(Chain);
1830 SelectAddress(Address, AM);
1831 } else {
1832 SelectAddress(Address, AM);
1833 Select(Chain);
1834 }
1835
1836 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001837 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1838 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00001839 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00001840 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001841}
1842
Chris Lattner30ea1e92005-01-19 07:37:26 +00001843// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1844// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1845// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1846// return true.
1847bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00001848 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1849 // good!
1850 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1851 std::swap(Op1, Op2); // Op1 is the SHL now.
1852 } else {
1853 return false; // No match
1854 }
1855
1856 SDOperand ShlVal = Op1.getOperand(0);
1857 SDOperand ShlAmt = Op1.getOperand(1);
1858 SDOperand ShrVal = Op2.getOperand(0);
1859 SDOperand ShrAmt = Op2.getOperand(1);
1860
Chris Lattner30ea1e92005-01-19 07:37:26 +00001861 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1862
Chris Lattner85716372005-01-19 06:18:43 +00001863 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1864 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1865 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00001866 if (SubCST->getValue() == RegSize) {
1867 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00001868 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00001869 if (ShrVal == ShlVal) {
1870 unsigned Reg, ShAmt;
1871 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1872 Reg = SelectExpr(ShrVal);
1873 ShAmt = SelectExpr(ShrAmt);
1874 } else {
1875 ShAmt = SelectExpr(ShrAmt);
1876 Reg = SelectExpr(ShrVal);
1877 }
1878 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1879 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1880 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1881 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1882 return true;
1883 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00001884 unsigned AReg, BReg;
1885 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00001886 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001887 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00001888 } else {
Chris Lattner85716372005-01-19 06:18:43 +00001889 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001890 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00001891 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00001892 unsigned ShAmt = SelectExpr(ShrAmt);
1893 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1894 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1895 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00001896 return true;
1897 }
1898 }
1899
Chris Lattner4053b1e2005-01-19 08:07:05 +00001900 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1901 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1902 if (SubCST->getValue() == RegSize) {
1903 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1904 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1905 if (ShrVal == ShlVal) {
1906 unsigned Reg, ShAmt;
1907 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1908 Reg = SelectExpr(ShrVal);
1909 ShAmt = SelectExpr(ShlAmt);
1910 } else {
1911 ShAmt = SelectExpr(ShlAmt);
1912 Reg = SelectExpr(ShrVal);
1913 }
1914 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1915 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1916 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1917 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1918 return true;
1919 } else if (RegSize != 8) {
1920 unsigned AReg, BReg;
1921 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001922 AReg = SelectExpr(ShlVal);
1923 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001924 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001925 BReg = SelectExpr(ShrVal);
1926 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001927 }
1928 unsigned ShAmt = SelectExpr(ShlAmt);
1929 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1930 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1931 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1932 return true;
1933 }
1934 }
Chris Lattner85716372005-01-19 06:18:43 +00001935
Chris Lattner4053b1e2005-01-19 08:07:05 +00001936 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1937 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1938 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1939 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1940 // (A >> 5) | (A << 27) --> ROR A, 5
1941 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1942 if (ShrVal == ShlVal) {
1943 unsigned Reg = SelectExpr(ShrVal);
1944 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1945 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1946 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1947 return true;
1948 } else if (RegSize != 8) {
1949 unsigned AReg, BReg;
1950 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001951 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001952 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001953 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001954 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001955 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001956 }
1957 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1958 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1959 .addImm(ShrCst->getValue());
1960 return true;
1961 }
1962 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001963
Chris Lattner85716372005-01-19 06:18:43 +00001964 return false;
1965}
1966
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001967unsigned ISel::SelectExpr(SDOperand N) {
1968 unsigned Result;
1969 unsigned Tmp1, Tmp2, Tmp3;
1970 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001971 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001972 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001973
Chris Lattner7f2afac2005-01-14 22:37:41 +00001974 if (Node->getOpcode() == ISD::CopyFromReg) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00001975 if (MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()) ||
1976 cast<RegSDNode>(Node)->getReg() == X86::ESP) {
1977 // Just use the specified register as our input.
1978 return cast<RegSDNode>(Node)->getReg();
1979 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001980 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001981
Chris Lattnera5ade062005-01-11 21:19:59 +00001982 unsigned &Reg = ExprMap[N];
1983 if (Reg) return Reg;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001984
Chris Lattnerb38a7492005-04-02 04:01:14 +00001985 switch (N.getOpcode()) {
1986 default:
Chris Lattnera5ade062005-01-11 21:19:59 +00001987 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnerb38a7492005-04-02 04:01:14 +00001988 MakeReg(N.getValueType()) : 1;
1989 break;
Chris Lattner239738a2005-05-14 08:48:15 +00001990 case X86ISD::TAILCALL:
1991 case X86ISD::CALL:
Chris Lattnera5ade062005-01-11 21:19:59 +00001992 // If this is a call instruction, make sure to prepare ALL of the result
1993 // values as well as the chain.
Chris Lattner239738a2005-05-14 08:48:15 +00001994 ExprMap[N.getValue(0)] = 1;
1995 if (Node->getNumValues() > 1) {
1996 Result = MakeReg(Node->getValueType(1));
1997 ExprMap[N.getValue(1)] = Result;
1998 for (unsigned i = 2, e = Node->getNumValues(); i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00001999 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattner239738a2005-05-14 08:48:15 +00002000 } else {
2001 Result = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002002 }
Chris Lattnerb38a7492005-04-02 04:01:14 +00002003 break;
2004 case ISD::ADD_PARTS:
2005 case ISD::SUB_PARTS:
2006 case ISD::SHL_PARTS:
2007 case ISD::SRL_PARTS:
2008 case ISD::SRA_PARTS:
2009 Result = MakeReg(Node->getValueType(0));
2010 ExprMap[N.getValue(0)] = Result;
2011 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
2012 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
2013 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002014 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002015
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002016 switch (N.getOpcode()) {
2017 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00002018 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002019 assert(0 && "Node not handled!\n");
Chris Lattnerc6f41812005-05-12 23:06:28 +00002020 case ISD::CopyFromReg:
2021 Select(N.getOperand(0));
2022 if (Result == 1) {
2023 Reg = Result = ExprMap[N.getValue(0)] =
2024 MakeReg(N.getValue(0).getValueType());
2025 }
2026 switch (Node->getValueType(0)) {
2027 default: assert(0 && "Cannot CopyFromReg this!");
2028 case MVT::i1:
2029 case MVT::i8:
2030 BuildMI(BB, X86::MOV8rr, 1,
2031 Result).addReg(cast<RegSDNode>(Node)->getReg());
2032 return Result;
2033 case MVT::i16:
2034 BuildMI(BB, X86::MOV16rr, 1,
2035 Result).addReg(cast<RegSDNode>(Node)->getReg());
2036 return Result;
2037 case MVT::i32:
2038 BuildMI(BB, X86::MOV32rr, 1,
2039 Result).addReg(cast<RegSDNode>(Node)->getReg());
2040 return Result;
2041 }
2042
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002043 case ISD::FrameIndex:
2044 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
2045 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
2046 return Result;
2047 case ISD::ConstantPool:
2048 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
2049 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
2050 return Result;
2051 case ISD::ConstantFP:
2052 ContainsFPCode = true;
2053 Tmp1 = Result; // Intermediate Register
2054 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
2055 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
2056 Tmp1 = MakeReg(MVT::f64);
2057
2058 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
2059 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
2060 BuildMI(BB, X86::FLD0, 0, Tmp1);
2061 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
2062 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
2063 BuildMI(BB, X86::FLD1, 0, Tmp1);
2064 else
2065 assert(0 && "Unexpected constant!");
2066 if (Tmp1 != Result)
2067 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
2068 return Result;
2069 case ISD::Constant:
2070 switch (N.getValueType()) {
2071 default: assert(0 && "Cannot use constants of this type!");
2072 case MVT::i1:
2073 case MVT::i8: Opc = X86::MOV8ri; break;
2074 case MVT::i16: Opc = X86::MOV16ri; break;
2075 case MVT::i32: Opc = X86::MOV32ri; break;
2076 }
2077 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
2078 return Result;
Chris Lattner7ce7eff2005-04-01 22:46:45 +00002079 case ISD::UNDEF:
2080 if (Node->getValueType(0) == MVT::f64) {
2081 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
2082 BuildMI(BB, X86::FLD0, 0, Result);
2083 } else {
2084 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
2085 }
2086 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002087 case ISD::GlobalAddress: {
2088 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
2089 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
2090 return Result;
2091 }
2092 case ISD::ExternalSymbol: {
2093 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
2094 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
2095 return Result;
2096 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002097 case ISD::ZERO_EXTEND: {
2098 int DestIs16 = N.getValueType() == MVT::i16;
2099 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00002100
2101 // FIXME: This hack is here for zero extension casts from bool to i8. This
2102 // would not be needed if bools were promoted by Legalize.
2103 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002104 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00002105 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
2106 return Result;
2107 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002108
Chris Lattner4ff348b2005-01-17 06:26:58 +00002109 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002110 static const unsigned Opc[3] = {
2111 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
2112 };
2113
2114 X86AddressMode AM;
2115 EmitFoldedLoad(N.getOperand(0), AM);
2116 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002117
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002118 return Result;
2119 }
2120
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002121 static const unsigned Opc[3] = {
2122 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
2123 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002124 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002125 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
2126 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002127 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002128 case ISD::SIGN_EXTEND: {
2129 int DestIs16 = N.getValueType() == MVT::i16;
2130 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
2131
Chris Lattner590d8002005-01-09 18:52:44 +00002132 // FIXME: Legalize should promote bools to i8!
2133 assert(N.getOperand(0).getValueType() != MVT::i1 &&
2134 "Sign extend from bool not implemented!");
2135
Chris Lattner4ff348b2005-01-17 06:26:58 +00002136 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00002137 static const unsigned Opc[3] = {
2138 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
2139 };
2140
2141 X86AddressMode AM;
2142 EmitFoldedLoad(N.getOperand(0), AM);
2143 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
2144 return Result;
2145 }
2146
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002147 static const unsigned Opc[3] = {
2148 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
2149 };
2150 Tmp1 = SelectExpr(N.getOperand(0));
2151 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
2152 return Result;
2153 }
2154 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00002155 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00002156 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00002157 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00002158 switch (N.getValueType()) {
2159 default: assert(0 && "Unknown truncate!");
2160 case MVT::i1:
2161 case MVT::i8: Opc = X86::MOV8rm; break;
2162 case MVT::i16: Opc = X86::MOV16rm; break;
2163 }
2164 X86AddressMode AM;
2165 EmitFoldedLoad(N.getOperand(0), AM);
2166 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2167 return Result;
2168 }
2169
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002170 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
2171 // a move out of AX or AL.
2172 switch (N.getOperand(0).getValueType()) {
2173 default: assert(0 && "Unknown truncate!");
2174 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
2175 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
2176 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
2177 }
2178 Tmp1 = SelectExpr(N.getOperand(0));
2179 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2180
2181 switch (N.getValueType()) {
2182 default: assert(0 && "Unknown truncate!");
2183 case MVT::i1:
2184 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
2185 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
2186 }
2187 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
2188 return Result;
2189
Chris Lattner590d8002005-01-09 18:52:44 +00002190 case ISD::SINT_TO_FP:
2191 case ISD::UINT_TO_FP: {
2192 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00002193 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00002194
2195 // Promote the integer to a type supported by FLD. We do this because there
2196 // are no unsigned FLD instructions, so we must promote an unsigned value to
2197 // a larger signed value, then use FLD on the larger value.
2198 //
2199 MVT::ValueType PromoteType = MVT::Other;
2200 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
2201 unsigned PromoteOpcode = 0;
2202 unsigned RealDestReg = Result;
2203 switch (SrcTy) {
2204 case MVT::i1:
2205 case MVT::i8:
2206 // We don't have the facilities for directly loading byte sized data from
2207 // memory (even signed). Promote it to 16 bits.
2208 PromoteType = MVT::i16;
2209 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
2210 X86::MOVSX16rr8 : X86::MOVZX16rr8;
2211 break;
2212 case MVT::i16:
2213 if (Node->getOpcode() == ISD::UINT_TO_FP) {
2214 PromoteType = MVT::i32;
2215 PromoteOpcode = X86::MOVZX32rr16;
2216 }
2217 break;
2218 default:
2219 // Don't fild into the real destination.
2220 if (Node->getOpcode() == ISD::UINT_TO_FP)
2221 Result = MakeReg(Node->getValueType(0));
2222 break;
2223 }
2224
2225 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002226
Chris Lattner590d8002005-01-09 18:52:44 +00002227 if (PromoteType != MVT::Other) {
2228 Tmp2 = MakeReg(PromoteType);
2229 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
2230 SrcTy = PromoteType;
2231 Tmp1 = Tmp2;
2232 }
2233
2234 // Spill the integer to memory and reload it from there.
2235 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
2236 MachineFunction *F = BB->getParent();
2237 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2238
2239 switch (SrcTy) {
Chris Lattner590d8002005-01-09 18:52:44 +00002240 case MVT::i32:
2241 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
2242 FrameIdx).addReg(Tmp1);
2243 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
2244 break;
2245 case MVT::i16:
2246 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
2247 FrameIdx).addReg(Tmp1);
2248 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
2249 break;
2250 default: break; // No promotion required.
2251 }
2252
Chris Lattner085c9952005-01-12 04:00:00 +00002253 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00002254 // If this is a cast from uint -> double, we need to be careful when if
2255 // the "sign" bit is set. If so, we don't want to make a negative number,
2256 // we want to make a positive number. Emit code to add an offset if the
2257 // sign bit is set.
2258
2259 // Compute whether the sign bit is set by shifting the reg right 31 bits.
2260 unsigned IsNeg = MakeReg(MVT::i32);
2261 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
2262
2263 // Create a CP value that has the offset in one word and 0 in the other.
2264 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
2265 0x4f80000000000000ULL);
2266 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
2267 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
2268 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
Chris Lattner590d8002005-01-09 18:52:44 +00002269 }
2270 return RealDestReg;
2271 }
2272 case ISD::FP_TO_SINT:
2273 case ISD::FP_TO_UINT: {
2274 // FIXME: Most of this grunt work should be done by legalize!
2275 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2276
2277 // Change the floating point control register to use "round towards zero"
2278 // mode when truncating to an integer value.
2279 //
2280 MachineFunction *F = BB->getParent();
2281 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
2282 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
2283
2284 // Load the old value of the high byte of the control word...
2285 unsigned HighPartOfCW = MakeReg(MVT::i8);
2286 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
2287 CWFrameIdx, 1);
2288
2289 // Set the high part to be round to zero...
2290 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
2291 CWFrameIdx, 1).addImm(12);
2292
2293 // Reload the modified control word now...
2294 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002295
Chris Lattner590d8002005-01-09 18:52:44 +00002296 // Restore the memory image of control word to original value
2297 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
2298 CWFrameIdx, 1).addReg(HighPartOfCW);
2299
2300 // We don't have the facilities for directly storing byte sized data to
2301 // memory. Promote it to 16 bits. We also must promote unsigned values to
2302 // larger classes because we only have signed FP stores.
2303 MVT::ValueType StoreClass = Node->getValueType(0);
2304 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
2305 switch (StoreClass) {
Chris Lattner2afa1912005-05-09 05:33:18 +00002306 case MVT::i1:
Chris Lattner590d8002005-01-09 18:52:44 +00002307 case MVT::i8: StoreClass = MVT::i16; break;
2308 case MVT::i16: StoreClass = MVT::i32; break;
2309 case MVT::i32: StoreClass = MVT::i64; break;
Chris Lattner590d8002005-01-09 18:52:44 +00002310 default: assert(0 && "Unknown store class!");
2311 }
2312
2313 // Spill the integer to memory and reload it from there.
2314 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
2315 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2316
2317 switch (StoreClass) {
2318 default: assert(0 && "Unknown store class!");
2319 case MVT::i16:
2320 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
2321 break;
2322 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00002323 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00002324 break;
Chris Lattnera0dbf182005-05-09 18:37:02 +00002325 case MVT::i64:
2326 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
2327 break; }
Chris Lattner590d8002005-01-09 18:52:44 +00002328
2329 switch (Node->getValueType(0)) {
2330 default:
2331 assert(0 && "Unknown integer type!");
Chris Lattner590d8002005-01-09 18:52:44 +00002332 case MVT::i32:
2333 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
2334 break;
2335 case MVT::i16:
2336 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
2337 break;
2338 case MVT::i8:
Chris Lattner2afa1912005-05-09 05:33:18 +00002339 case MVT::i1:
Chris Lattner590d8002005-01-09 18:52:44 +00002340 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
2341 break;
2342 }
2343
2344 // Reload the original control word now.
2345 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
2346 return Result;
2347 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002348 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00002349 Op0 = N.getOperand(0);
2350 Op1 = N.getOperand(1);
2351
Chris Lattner44129b52005-01-25 20:03:11 +00002352 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002353 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002354 goto FoldAdd;
2355 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002356
Chris Lattner44129b52005-01-25 20:03:11 +00002357 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002358 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00002359 switch (N.getValueType()) {
2360 default: assert(0 && "Cannot add this type!");
2361 case MVT::i1:
2362 case MVT::i8: Opc = X86::ADD8rm; break;
2363 case MVT::i16: Opc = X86::ADD16rm; break;
2364 case MVT::i32: Opc = X86::ADD32rm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002365 case MVT::f64:
2366 // For F64, handle promoted load operations (from F32) as well!
2367 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
2368 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002369 }
2370 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002371 EmitFoldedLoad(Op1, AM);
2372 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002373 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2374 return Result;
2375 }
2376
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002377 // See if we can codegen this as an LEA to fold operations together.
2378 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00002379 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002380 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00002381 MatchAddress(N, AM);
2382 ExprMap[N] = Result;
2383
2384 // If this is not just an add, emit the LEA. For a simple add (like
2385 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
2386 // leave this as LEA, then peephole it to 'ADD' after two address elim
2387 // happens.
2388 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
2389 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
2390 X86AddressMode XAM = SelectAddrExprs(AM);
2391 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
2392 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002393 }
2394 }
Chris Lattner11333092005-01-11 03:11:44 +00002395
Chris Lattnera5ade062005-01-11 21:19:59 +00002396 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002397 Opc = 0;
2398 if (CN->getValue() == 1) { // add X, 1 -> inc X
2399 switch (N.getValueType()) {
2400 default: assert(0 && "Cannot integer add this type!");
2401 case MVT::i8: Opc = X86::INC8r; break;
2402 case MVT::i16: Opc = X86::INC16r; break;
2403 case MVT::i32: Opc = X86::INC32r; break;
2404 }
2405 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
2406 switch (N.getValueType()) {
2407 default: assert(0 && "Cannot integer add this type!");
2408 case MVT::i8: Opc = X86::DEC8r; break;
2409 case MVT::i16: Opc = X86::DEC16r; break;
2410 case MVT::i32: Opc = X86::DEC32r; break;
2411 }
2412 }
2413
2414 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002415 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002416 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2417 return Result;
2418 }
2419
2420 switch (N.getValueType()) {
2421 default: assert(0 && "Cannot add this type!");
2422 case MVT::i8: Opc = X86::ADD8ri; break;
2423 case MVT::i16: Opc = X86::ADD16ri; break;
2424 case MVT::i32: Opc = X86::ADD32ri; break;
2425 }
2426 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002427 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002428 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2429 return Result;
2430 }
2431 }
2432
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002433 switch (N.getValueType()) {
2434 default: assert(0 && "Cannot add this type!");
2435 case MVT::i8: Opc = X86::ADD8rr; break;
2436 case MVT::i16: Opc = X86::ADD16rr; break;
2437 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002438 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002439 }
Chris Lattner11333092005-01-11 03:11:44 +00002440
Chris Lattnera5ade062005-01-11 21:19:59 +00002441 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2442 Tmp1 = SelectExpr(Op0);
2443 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00002444 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00002445 Tmp2 = SelectExpr(Op1);
2446 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00002447 }
2448
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002449 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2450 return Result;
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002451
2452 case ISD::FABS:
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002453 case ISD::FNEG:
Chris Lattnerc5dcb532005-04-30 04:25:35 +00002454 case ISD::FSIN:
2455 case ISD::FCOS:
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002456 case ISD::FSQRT:
2457 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002458 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002459 switch (N.getOpcode()) {
2460 default: assert(0 && "Unreachable!");
2461 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
2462 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
2463 case ISD::FSQRT: BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1); break;
Chris Lattnerc5dcb532005-04-30 04:25:35 +00002464 case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break;
2465 case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002466 }
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002467 return Result;
2468
Chris Lattner8db0af12005-04-06 04:21:07 +00002469 case ISD::MULHU:
2470 switch (N.getValueType()) {
2471 default: assert(0 && "Unsupported VT!");
2472 case MVT::i8: Tmp2 = X86::MUL8r; break;
2473 case MVT::i16: Tmp2 = X86::MUL16r; break;
2474 case MVT::i32: Tmp2 = X86::MUL32r; break;
2475 }
2476 // FALL THROUGH
2477 case ISD::MULHS: {
2478 unsigned MovOpc, LowReg, HiReg;
2479 switch (N.getValueType()) {
2480 default: assert(0 && "Unsupported VT!");
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002481 case MVT::i8:
Chris Lattner8db0af12005-04-06 04:21:07 +00002482 MovOpc = X86::MOV8rr;
2483 LowReg = X86::AL;
2484 HiReg = X86::AH;
2485 Opc = X86::IMUL8r;
2486 break;
2487 case MVT::i16:
2488 MovOpc = X86::MOV16rr;
2489 LowReg = X86::AX;
2490 HiReg = X86::DX;
2491 Opc = X86::IMUL16r;
2492 break;
2493 case MVT::i32:
2494 MovOpc = X86::MOV32rr;
2495 LowReg = X86::EAX;
2496 HiReg = X86::EDX;
2497 Opc = X86::IMUL32r;
2498 break;
2499 }
2500 if (Node->getOpcode() != ISD::MULHS)
2501 Opc = Tmp2; // Get the MULHU opcode.
2502
2503 Op0 = Node->getOperand(0);
2504 Op1 = Node->getOperand(1);
2505 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2506 Tmp1 = SelectExpr(Op0);
2507 Tmp2 = SelectExpr(Op1);
2508 } else {
2509 Tmp2 = SelectExpr(Op1);
2510 Tmp1 = SelectExpr(Op0);
2511 }
2512
2513 // FIXME: Implement folding of loads into the memory operands here!
2514 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
2515 BuildMI(BB, Opc, 1).addReg(Tmp2);
2516 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
2517 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002518 }
Chris Lattner8db0af12005-04-06 04:21:07 +00002519
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002520 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00002521 case ISD::MUL:
2522 case ISD::AND:
2523 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00002524 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00002525 static const unsigned SUBTab[] = {
2526 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
2527 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
2528 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
2529 };
2530 static const unsigned MULTab[] = {
2531 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
2532 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
2533 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
2534 };
2535 static const unsigned ANDTab[] = {
2536 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
2537 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002538 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattnera5ade062005-01-11 21:19:59 +00002539 };
2540 static const unsigned ORTab[] = {
2541 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
2542 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
2543 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
2544 };
2545 static const unsigned XORTab[] = {
2546 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
2547 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
2548 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
2549 };
2550
2551 Op0 = Node->getOperand(0);
2552 Op1 = Node->getOperand(1);
2553
Chris Lattner30ea1e92005-01-19 07:37:26 +00002554 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
2555 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00002556 return Result;
2557
2558 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002559 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
2560 if (CN->isNullValue()) { // 0 - N -> neg N
2561 switch (N.getValueType()) {
2562 default: assert(0 && "Cannot sub this type!");
2563 case MVT::i1:
2564 case MVT::i8: Opc = X86::NEG8r; break;
2565 case MVT::i16: Opc = X86::NEG16r; break;
2566 case MVT::i32: Opc = X86::NEG32r; break;
2567 }
2568 Tmp1 = SelectExpr(N.getOperand(1));
2569 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2570 return Result;
2571 }
2572
Chris Lattnera5ade062005-01-11 21:19:59 +00002573 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2574 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00002575 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00002576 switch (N.getValueType()) {
2577 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00002578 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00002579 case MVT::i8: Opc = X86::NOT8r; break;
2580 case MVT::i16: Opc = X86::NOT16r; break;
2581 case MVT::i32: Opc = X86::NOT32r; break;
2582 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00002583 if (Opc) {
2584 Tmp1 = SelectExpr(Op0);
2585 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2586 return Result;
2587 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00002588 }
2589
Chris Lattner2a4e5082005-01-17 06:48:02 +00002590 // Fold common multiplies into LEA instructions.
2591 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
2592 switch ((int)CN->getValue()) {
2593 default: break;
2594 case 3:
2595 case 5:
2596 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00002597 // Remove N from exprmap so SelectAddress doesn't get confused.
2598 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002599 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00002600 SelectAddress(N, AM);
2601 // Restore it to the map.
2602 ExprMap[N] = Result;
2603 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
2604 return Result;
2605 }
2606 }
2607
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002608 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00002609 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002610 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00002611 case MVT::i8: Opc = 0; break;
2612 case MVT::i16: Opc = 1; break;
2613 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002614 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002615 switch (Node->getOpcode()) {
2616 default: assert(0 && "Unreachable!");
2617 case ISD::SUB: Opc = SUBTab[Opc]; break;
2618 case ISD::MUL: Opc = MULTab[Opc]; break;
2619 case ISD::AND: Opc = ANDTab[Opc]; break;
2620 case ISD::OR: Opc = ORTab[Opc]; break;
2621 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002622 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002623 if (Opc) { // Can't fold MUL:i8 R, imm
2624 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 }
Chris Lattner11333092005-01-11 03:11:44 +00002629
Chris Lattner44129b52005-01-25 20:03:11 +00002630 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00002631 if (Node->getOpcode() != ISD::SUB) {
2632 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002633 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00002634 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00002635 // For FP, emit 'reverse' subract, with a memory operand.
2636 if (N.getValueType() == MVT::f64) {
2637 if (Op0.getOpcode() == ISD::EXTLOAD)
2638 Opc = X86::FSUBR32m;
2639 else
2640 Opc = X86::FSUBR64m;
2641
Chris Lattnera5ade062005-01-11 21:19:59 +00002642 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002643 EmitFoldedLoad(Op0, AM);
2644 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00002645 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2646 return Result;
2647 }
2648 }
2649
Chris Lattner44129b52005-01-25 20:03:11 +00002650 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002651 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00002652 switch (N.getValueType()) {
2653 default: assert(0 && "Cannot operate on this type!");
2654 case MVT::i1:
2655 case MVT::i8: Opc = 5; break;
2656 case MVT::i16: Opc = 6; break;
2657 case MVT::i32: Opc = 7; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002658 // For F64, handle promoted load operations (from F32) as well!
2659 case MVT::f64: Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002660 }
2661 switch (Node->getOpcode()) {
2662 default: assert(0 && "Unreachable!");
2663 case ISD::SUB: Opc = SUBTab[Opc]; break;
2664 case ISD::MUL: Opc = MULTab[Opc]; break;
2665 case ISD::AND: Opc = ANDTab[Opc]; break;
2666 case ISD::OR: Opc = ORTab[Opc]; break;
2667 case ISD::XOR: Opc = XORTab[Opc]; break;
2668 }
2669
2670 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002671 EmitFoldedLoad(Op1, AM);
2672 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002673 if (Opc) {
2674 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2675 } else {
2676 assert(Node->getOpcode() == ISD::MUL &&
2677 N.getValueType() == MVT::i8 && "Unexpected situation!");
2678 // Must use the MUL instruction, which forces use of AL.
2679 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2680 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2681 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2682 }
2683 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00002684 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002685
2686 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2687 Tmp1 = SelectExpr(Op0);
2688 Tmp2 = SelectExpr(Op1);
2689 } else {
2690 Tmp2 = SelectExpr(Op1);
2691 Tmp1 = SelectExpr(Op0);
2692 }
2693
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002694 switch (N.getValueType()) {
2695 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002696 case MVT::i1:
2697 case MVT::i8: Opc = 10; break;
2698 case MVT::i16: Opc = 11; break;
2699 case MVT::i32: Opc = 12; break;
2700 case MVT::f32: Opc = 13; break;
2701 case MVT::f64: Opc = 14; break;
2702 }
2703 switch (Node->getOpcode()) {
2704 default: assert(0 && "Unreachable!");
2705 case ISD::SUB: Opc = SUBTab[Opc]; break;
2706 case ISD::MUL: Opc = MULTab[Opc]; break;
2707 case ISD::AND: Opc = ANDTab[Opc]; break;
2708 case ISD::OR: Opc = ORTab[Opc]; break;
2709 case ISD::XOR: Opc = XORTab[Opc]; break;
2710 }
2711 if (Opc) {
2712 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2713 } else {
2714 assert(Node->getOpcode() == ISD::MUL &&
2715 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00002716 // Must use the MUL instruction, which forces use of AL.
2717 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2718 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2719 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002720 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002721 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00002722 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002723 case ISD::ADD_PARTS:
2724 case ISD::SUB_PARTS: {
2725 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2726 "Not an i64 add/sub!");
2727 // Emit all of the operands.
2728 std::vector<unsigned> InVals;
2729 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2730 InVals.push_back(SelectExpr(N.getOperand(i)));
2731 if (N.getOpcode() == ISD::ADD_PARTS) {
2732 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2733 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2734 } else {
2735 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2736 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2737 }
2738 return Result+N.ResNo;
2739 }
2740
Chris Lattnerb38a7492005-04-02 04:01:14 +00002741 case ISD::SHL_PARTS:
2742 case ISD::SRA_PARTS:
2743 case ISD::SRL_PARTS: {
2744 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2745 "Not an i64 shift!");
2746 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2747 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2748 unsigned TmpReg = MakeReg(MVT::i32);
2749 if (N.getOpcode() == ISD::SRA_PARTS) {
2750 // If this is a SHR of a Long, then we need to do funny sign extension
2751 // stuff. TmpReg gets the value to use as the high-part if we are
2752 // shifting more than 32 bits.
2753 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2754 } else {
2755 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2756 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2757 }
2758
2759 // Initialize CL with the shift amount.
2760 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2761 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2762
2763 unsigned TmpReg2 = MakeReg(MVT::i32);
2764 unsigned TmpReg3 = MakeReg(MVT::i32);
2765 if (N.getOpcode() == ISD::SHL_PARTS) {
2766 // TmpReg2 = shld inHi, inLo
2767 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2768 .addReg(ShiftOpLo);
2769 // TmpReg3 = shl inLo, CL
2770 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002771
Chris Lattnerb38a7492005-04-02 04:01:14 +00002772 // Set the flags to indicate whether the shift was by more than 32 bits.
2773 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002774
Chris Lattnerb38a7492005-04-02 04:01:14 +00002775 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002776 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002777 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2778 // DestLo = (>32) ? TmpReg : TmpReg3;
2779 BuildMI(BB, X86::CMOVNE32rr, 2,
2780 Result).addReg(TmpReg3).addReg(TmpReg);
2781 } else {
2782 // TmpReg2 = shrd inLo, inHi
2783 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
2784 .addReg(ShiftOpHi);
2785 // TmpReg3 = s[ah]r inHi, CL
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002786 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnerb38a7492005-04-02 04:01:14 +00002787 : X86::SHR32rCL, 1, TmpReg3)
2788 .addReg(ShiftOpHi);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002789
Chris Lattnerb38a7492005-04-02 04:01:14 +00002790 // Set the flags to indicate whether the shift was by more than 32 bits.
2791 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002792
Chris Lattnerb38a7492005-04-02 04:01:14 +00002793 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002794 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002795 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002796
Chris Lattnerb38a7492005-04-02 04:01:14 +00002797 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002798 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002799 Result+1).addReg(TmpReg3).addReg(TmpReg);
2800 }
2801 return Result+N.ResNo;
2802 }
2803
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002804 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002805 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2806 Tmp2 = SelectExpr(N.getOperand(1));
2807 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002808 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002809 Tmp3 = SelectExpr(N.getOperand(2));
2810 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002811 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002812 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2813 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002814
2815 case ISD::SDIV:
2816 case ISD::UDIV:
2817 case ISD::SREM:
2818 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002819 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2820 "We don't support this operator!");
2821
Chris Lattner5bf26862005-04-13 03:29:53 +00002822 if (N.getOpcode() == ISD::SDIV) {
Chris Lattner3576c842005-01-25 20:35:10 +00002823 // We can fold loads into FpDIVs, but not really into any others.
2824 if (N.getValueType() == MVT::f64) {
2825 // Check for reversed and unreversed DIV.
2826 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2827 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
2828 Opc = X86::FDIVR32m;
2829 else
2830 Opc = X86::FDIVR64m;
2831 X86AddressMode AM;
2832 EmitFoldedLoad(N.getOperand(0), AM);
2833 Tmp1 = SelectExpr(N.getOperand(1));
2834 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2835 return Result;
2836 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2837 N.getOperand(1).getOpcode() == ISD::LOAD) {
2838 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
2839 Opc = X86::FDIV32m;
2840 else
2841 Opc = X86::FDIV64m;
2842 X86AddressMode AM;
2843 EmitFoldedLoad(N.getOperand(1), AM);
2844 Tmp1 = SelectExpr(N.getOperand(0));
2845 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2846 return Result;
2847 }
2848 }
2849
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002850 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2851 // FIXME: These special cases should be handled by the lowering impl!
2852 unsigned RHS = CN->getValue();
2853 bool isNeg = false;
2854 if ((int)RHS < 0) {
2855 isNeg = true;
2856 RHS = -RHS;
2857 }
2858 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
2859 unsigned Log = log2(RHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002860 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
2861 switch (N.getValueType()) {
2862 default: assert("Unknown type to signed divide!");
2863 case MVT::i8:
2864 SAROpc = X86::SAR8ri;
2865 SHROpc = X86::SHR8ri;
2866 ADDOpc = X86::ADD8rr;
2867 NEGOpc = X86::NEG8r;
2868 break;
2869 case MVT::i16:
2870 SAROpc = X86::SAR16ri;
2871 SHROpc = X86::SHR16ri;
2872 ADDOpc = X86::ADD16rr;
2873 NEGOpc = X86::NEG16r;
2874 break;
2875 case MVT::i32:
2876 SAROpc = X86::SAR32ri;
2877 SHROpc = X86::SHR32ri;
2878 ADDOpc = X86::ADD32rr;
2879 NEGOpc = X86::NEG32r;
2880 break;
2881 }
Chris Lattnera96e5772005-05-13 21:48:20 +00002882 unsigned RegSize = MVT::getSizeInBits(N.getValueType());
Chris Lattner11333092005-01-11 03:11:44 +00002883 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerca96c822005-05-13 21:50:27 +00002884 unsigned TmpReg;
2885 if (Log != 1) {
2886 TmpReg = MakeReg(N.getValueType());
2887 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2888 } else {
2889 TmpReg = Tmp1;
2890 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002891 unsigned TmpReg2 = MakeReg(N.getValueType());
Chris Lattnera96e5772005-05-13 21:48:20 +00002892 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(RegSize-Log);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002893 unsigned TmpReg3 = MakeReg(N.getValueType());
2894 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002895
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002896 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2897 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2898 if (isNeg)
2899 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2900 return Result;
2901 }
2902 }
Chris Lattner5bf26862005-04-13 03:29:53 +00002903 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002904
Chris Lattner11333092005-01-11 03:11:44 +00002905 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2906 Tmp1 = SelectExpr(N.getOperand(0));
2907 Tmp2 = SelectExpr(N.getOperand(1));
2908 } else {
2909 Tmp2 = SelectExpr(N.getOperand(1));
2910 Tmp1 = SelectExpr(N.getOperand(0));
2911 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002912
2913 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2914 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2915 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2916 switch (N.getValueType()) {
2917 default: assert(0 && "Cannot sdiv this type!");
2918 case MVT::i8:
2919 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2920 LoReg = X86::AL;
2921 HiReg = X86::AH;
2922 MovOpcode = X86::MOV8rr;
2923 ClrOpcode = X86::MOV8ri;
2924 SExtOpcode = X86::CBW;
2925 break;
2926 case MVT::i16:
2927 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2928 LoReg = X86::AX;
2929 HiReg = X86::DX;
2930 MovOpcode = X86::MOV16rr;
2931 ClrOpcode = X86::MOV16ri;
2932 SExtOpcode = X86::CWD;
2933 break;
2934 case MVT::i32:
2935 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00002936 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002937 HiReg = X86::EDX;
2938 MovOpcode = X86::MOV32rr;
2939 ClrOpcode = X86::MOV32ri;
2940 SExtOpcode = X86::CDQ;
2941 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002942 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002943 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002944 return Result;
2945 }
2946
2947 // Set up the low part.
2948 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2949
2950 if (isSigned) {
2951 // Sign extend the low part into the high part.
2952 BuildMI(BB, SExtOpcode, 0);
2953 } else {
2954 // Zero out the high part, effectively zero extending the input.
2955 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2956 }
2957
2958 // Emit the DIV/IDIV instruction.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002959 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002960
2961 // Get the result of the divide or rem.
2962 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2963 return Result;
2964 }
2965
2966 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002967 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002968 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2969 switch (N.getValueType()) {
2970 default: assert(0 && "Cannot shift this type!");
2971 case MVT::i8: Opc = X86::ADD8rr; break;
2972 case MVT::i16: Opc = X86::ADD16rr; break;
2973 case MVT::i32: Opc = X86::ADD32rr; break;
2974 }
2975 Tmp1 = SelectExpr(N.getOperand(0));
2976 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2977 return Result;
2978 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002979
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002980 switch (N.getValueType()) {
2981 default: assert(0 && "Cannot shift this type!");
2982 case MVT::i8: Opc = X86::SHL8ri; break;
2983 case MVT::i16: Opc = X86::SHL16ri; break;
2984 case MVT::i32: Opc = X86::SHL32ri; break;
2985 }
Chris Lattner11333092005-01-11 03:11:44 +00002986 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002987 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2988 return Result;
2989 }
Chris Lattner11333092005-01-11 03:11:44 +00002990
2991 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2992 Tmp1 = SelectExpr(N.getOperand(0));
2993 Tmp2 = SelectExpr(N.getOperand(1));
2994 } else {
2995 Tmp2 = SelectExpr(N.getOperand(1));
2996 Tmp1 = SelectExpr(N.getOperand(0));
2997 }
2998
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002999 switch (N.getValueType()) {
3000 default: assert(0 && "Cannot shift this type!");
3001 case MVT::i8 : Opc = X86::SHL8rCL; break;
3002 case MVT::i16: Opc = X86::SHL16rCL; break;
3003 case MVT::i32: Opc = X86::SHL32rCL; break;
3004 }
3005 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3006 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3007 return Result;
3008 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003009 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3010 switch (N.getValueType()) {
3011 default: assert(0 && "Cannot shift this type!");
3012 case MVT::i8: Opc = X86::SHR8ri; break;
3013 case MVT::i16: Opc = X86::SHR16ri; break;
3014 case MVT::i32: Opc = X86::SHR32ri; break;
3015 }
Chris Lattner11333092005-01-11 03:11:44 +00003016 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003017 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3018 return Result;
3019 }
Chris Lattner11333092005-01-11 03:11:44 +00003020
3021 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3022 Tmp1 = SelectExpr(N.getOperand(0));
3023 Tmp2 = SelectExpr(N.getOperand(1));
3024 } else {
3025 Tmp2 = SelectExpr(N.getOperand(1));
3026 Tmp1 = SelectExpr(N.getOperand(0));
3027 }
3028
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003029 switch (N.getValueType()) {
3030 default: assert(0 && "Cannot shift this type!");
3031 case MVT::i8 : Opc = X86::SHR8rCL; break;
3032 case MVT::i16: Opc = X86::SHR16rCL; break;
3033 case MVT::i32: Opc = X86::SHR32rCL; break;
3034 }
3035 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3036 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3037 return Result;
3038 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003039 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3040 switch (N.getValueType()) {
3041 default: assert(0 && "Cannot shift this type!");
3042 case MVT::i8: Opc = X86::SAR8ri; break;
3043 case MVT::i16: Opc = X86::SAR16ri; break;
3044 case MVT::i32: Opc = X86::SAR32ri; break;
3045 }
Chris Lattner11333092005-01-11 03:11:44 +00003046 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003047 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
3048 return Result;
3049 }
Chris Lattner11333092005-01-11 03:11:44 +00003050
3051 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3052 Tmp1 = SelectExpr(N.getOperand(0));
3053 Tmp2 = SelectExpr(N.getOperand(1));
3054 } else {
3055 Tmp2 = SelectExpr(N.getOperand(1));
3056 Tmp1 = SelectExpr(N.getOperand(0));
3057 }
3058
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003059 switch (N.getValueType()) {
3060 default: assert(0 && "Cannot shift this type!");
3061 case MVT::i8 : Opc = X86::SAR8rCL; break;
3062 case MVT::i16: Opc = X86::SAR16rCL; break;
3063 case MVT::i32: Opc = X86::SAR32rCL; break;
3064 }
3065 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
3066 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
3067 return Result;
3068
3069 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00003070 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003071 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
3072 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
3073 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003074 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003075 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00003076 if (Result != 1) { // Generate the token
3077 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3078 assert(0 && "Load already emitted!?");
3079 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003080 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3081
Chris Lattner5188ad72005-01-08 19:28:19 +00003082 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003083 default: assert(0 && "Cannot load this type!");
3084 case MVT::i1:
3085 case MVT::i8: Opc = X86::MOV8rm; break;
3086 case MVT::i16: Opc = X86::MOV16rm; break;
3087 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003088 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
3089 }
Chris Lattner11333092005-01-11 03:11:44 +00003090
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003091 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00003092 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003093 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
3094 } else {
3095 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00003096
3097 SDOperand Chain = N.getOperand(0);
3098 SDOperand Address = N.getOperand(1);
3099 if (getRegPressure(Chain) > getRegPressure(Address)) {
3100 Select(Chain);
3101 SelectAddress(Address, AM);
3102 } else {
3103 SelectAddress(Address, AM);
3104 Select(Chain);
3105 }
3106
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003107 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
3108 }
3109 return Result;
Chris Lattner67649df2005-05-14 06:52:07 +00003110 case X86ISD::FILD64m:
3111 // Make sure we generate both values.
3112 assert(Result != 1 && N.getValueType() == MVT::f64);
3113 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3114 assert(0 && "Load already emitted!?");
3115
3116 {
3117 X86AddressMode AM;
3118
3119 SDOperand Chain = N.getOperand(0);
3120 SDOperand Address = N.getOperand(1);
3121 if (getRegPressure(Chain) > getRegPressure(Address)) {
3122 Select(Chain);
3123 SelectAddress(Address, AM);
3124 } else {
3125 SelectAddress(Address, AM);
3126 Select(Chain);
3127 }
3128
3129 addFullAddress(BuildMI(BB, X86::FILD64m, 4, Result), AM);
3130 }
3131 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003132
3133 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
3134 case ISD::ZEXTLOAD: {
3135 // Make sure we generate both values.
3136 if (Result != 1)
3137 ExprMap[N.getValue(1)] = 1; // Generate the token
3138 else
3139 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3140
Chris Lattnerda2ce112005-01-16 07:34:08 +00003141 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
3142 if (Node->getValueType(0) == MVT::f64) {
3143 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
3144 "Bad EXTLOAD!");
3145 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
3146 CP->getIndex());
3147 return Result;
3148 }
3149
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003150 X86AddressMode AM;
3151 if (getRegPressure(Node->getOperand(0)) >
3152 getRegPressure(Node->getOperand(1))) {
3153 Select(Node->getOperand(0)); // chain
3154 SelectAddress(Node->getOperand(1), AM);
3155 } else {
3156 SelectAddress(Node->getOperand(1), AM);
3157 Select(Node->getOperand(0)); // chain
3158 }
3159
3160 switch (Node->getValueType(0)) {
3161 default: assert(0 && "Unknown type to sign extend to.");
3162 case MVT::f64:
3163 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
3164 "Bad EXTLOAD!");
3165 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
3166 break;
3167 case MVT::i32:
3168 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
3169 default:
3170 assert(0 && "Bad zero extend!");
3171 case MVT::i1:
3172 case MVT::i8:
3173 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
3174 break;
3175 case MVT::i16:
3176 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
3177 break;
3178 }
3179 break;
3180 case MVT::i16:
3181 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
3182 "Bad zero extend!");
3183 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3184 break;
3185 case MVT::i8:
3186 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
3187 "Bad zero extend!");
3188 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
3189 break;
3190 }
3191 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003192 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003193 case ISD::SEXTLOAD: {
3194 // Make sure we generate both values.
3195 if (Result != 1)
3196 ExprMap[N.getValue(1)] = 1; // Generate the token
3197 else
3198 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3199
3200 X86AddressMode AM;
3201 if (getRegPressure(Node->getOperand(0)) >
3202 getRegPressure(Node->getOperand(1))) {
3203 Select(Node->getOperand(0)); // chain
3204 SelectAddress(Node->getOperand(1), AM);
3205 } else {
3206 SelectAddress(Node->getOperand(1), AM);
3207 Select(Node->getOperand(0)); // chain
3208 }
3209
3210 switch (Node->getValueType(0)) {
3211 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
3212 default: assert(0 && "Unknown type to sign extend to.");
3213 case MVT::i32:
3214 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
3215 default:
3216 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
3217 case MVT::i8:
3218 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
3219 break;
3220 case MVT::i16:
3221 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
3222 break;
3223 }
3224 break;
3225 case MVT::i16:
3226 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
3227 "Cannot sign extend from bool!");
3228 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3229 break;
3230 }
3231 return Result;
3232 }
3233
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003234 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003235 // Generate both result values.
3236 if (Result != 1)
3237 ExprMap[N.getValue(1)] = 1; // Generate the token
3238 else
3239 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3240
3241 // FIXME: We are currently ignoring the requested alignment for handling
3242 // greater than the stack alignment. This will need to be revisited at some
3243 // point. Align = N.getOperand(2);
3244
3245 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
3246 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
3247 std::cerr << "Cannot allocate stack object with greater alignment than"
3248 << " the stack alignment yet!";
3249 abort();
3250 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003251
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003252 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00003253 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003254 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
3255 .addImm(CN->getValue());
3256 } else {
Chris Lattner11333092005-01-11 03:11:44 +00003257 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3258 Select(N.getOperand(0));
3259 Tmp1 = SelectExpr(N.getOperand(1));
3260 } else {
3261 Tmp1 = SelectExpr(N.getOperand(1));
3262 Select(N.getOperand(0));
3263 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003264
3265 // Subtract size from stack pointer, thereby allocating some space.
3266 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
3267 }
3268
3269 // Put a pointer to the space into the result register, by copying the stack
3270 // pointer.
3271 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
3272 return Result;
3273
Chris Lattner239738a2005-05-14 08:48:15 +00003274 case X86ISD::TAILCALL:
3275 case X86ISD::CALL: {
Chris Lattner5188ad72005-01-08 19:28:19 +00003276 // The chain for this call is now lowered.
Chris Lattner239738a2005-05-14 08:48:15 +00003277 ExprMap.insert(std::make_pair(N.getValue(0), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00003278
Chris Lattnerc6f41812005-05-12 23:06:28 +00003279 bool isDirect = isa<GlobalAddressSDNode>(N.getOperand(1)) ||
3280 isa<ExternalSymbolSDNode>(N.getOperand(1));
3281 unsigned Callee = 0;
3282 if (isDirect) {
3283 Select(N.getOperand(0));
3284 } else {
3285 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3286 Select(N.getOperand(0));
3287 Callee = SelectExpr(N.getOperand(1));
3288 } else {
3289 Callee = SelectExpr(N.getOperand(1));
3290 Select(N.getOperand(0));
3291 }
3292 }
3293
3294 // If this call has values to pass in registers, do so now.
Chris Lattner239738a2005-05-14 08:48:15 +00003295 if (Node->getNumOperands() > 4) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003296 // The first value is passed in (a part of) EAX, the second in EDX.
Chris Lattner239738a2005-05-14 08:48:15 +00003297 unsigned RegOp1 = SelectExpr(N.getOperand(4));
Chris Lattnerc6f41812005-05-12 23:06:28 +00003298 unsigned RegOp2 =
Chris Lattner239738a2005-05-14 08:48:15 +00003299 Node->getNumOperands() > 5 ? SelectExpr(N.getOperand(5)) : 0;
Chris Lattnerc6f41812005-05-12 23:06:28 +00003300
Chris Lattner239738a2005-05-14 08:48:15 +00003301 switch (N.getOperand(4).getValueType()) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003302 default: assert(0 && "Bad thing to pass in regs");
3303 case MVT::i1:
3304 case MVT::i8: BuildMI(BB, X86::MOV8rr , 1,X86::AL).addReg(RegOp1); break;
3305 case MVT::i16: BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1); break;
3306 case MVT::i32: BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);break;
3307 }
3308 if (RegOp2)
Chris Lattner239738a2005-05-14 08:48:15 +00003309 switch (N.getOperand(5).getValueType()) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00003310 default: assert(0 && "Bad thing to pass in regs");
3311 case MVT::i1:
3312 case MVT::i8:
3313 BuildMI(BB, X86::MOV8rr , 1, X86::DL).addReg(RegOp2);
3314 break;
3315 case MVT::i16:
3316 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
3317 break;
3318 case MVT::i32:
3319 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
3320 break;
3321 }
3322 }
3323
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003324 if (GlobalAddressSDNode *GASD =
3325 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3326 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
3327 } else if (ExternalSymbolSDNode *ESSDN =
3328 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
3329 BuildMI(BB, X86::CALLpcrel32,
3330 1).addExternalSymbol(ESSDN->getSymbol(), true);
3331 } else {
Chris Lattner11333092005-01-11 03:11:44 +00003332 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3333 Select(N.getOperand(0));
3334 Tmp1 = SelectExpr(N.getOperand(1));
3335 } else {
3336 Tmp1 = SelectExpr(N.getOperand(1));
3337 Select(N.getOperand(0));
3338 }
3339
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003340 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
3341 }
Chris Lattner239738a2005-05-14 08:48:15 +00003342
3343 // Get caller stack amount and amount the callee added to the stack pointer.
3344 Tmp1 = cast<ConstantSDNode>(N.getOperand(2))->getValue();
3345 Tmp2 = cast<ConstantSDNode>(N.getOperand(3))->getValue();
3346 BuildMI(BB, X86::ADJCALLSTACKUP, 2).addImm(Tmp1).addImm(Tmp2);
3347
3348 if (Node->getNumValues() != 1)
3349 switch (Node->getValueType(1)) {
3350 default: assert(0 && "Unknown value type for call result!");
3351 case MVT::Other: return 1;
3352 case MVT::i1:
3353 case MVT::i8:
3354 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3355 break;
3356 case MVT::i16:
3357 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3358 break;
3359 case MVT::i32:
3360 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3361 if (Node->getNumValues() == 3 && Node->getValueType(2) == MVT::i32)
3362 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
3363 break;
3364 case MVT::f64: // Floating-point return values live in %ST(0)
3365 ContainsFPCode = true;
3366 BuildMI(BB, X86::FpGETRESULT, 1, Result);
3367 break;
3368 }
3369 return Result+N.ResNo-1;
Chris Lattnerc6f41812005-05-12 23:06:28 +00003370 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00003371 case ISD::READPORT:
3372 // First, determine that the size of the operand falls within the acceptable
3373 // range for this architecture.
3374 //
3375 if (Node->getOperand(1).getValueType() != MVT::i16) {
3376 std::cerr << "llvm.readport: Address size is not 16 bits\n";
3377 exit(1);
3378 }
3379
3380 // Make sure we generate both values.
3381 if (Result != 1) { // Generate the token
3382 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3383 assert(0 && "readport already emitted!?");
3384 } else
3385 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3386
3387 Select(Node->getOperand(0)); // Select the chain.
3388
3389 // If the port is a single-byte constant, use the immediate form.
3390 if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
3391 if ((Port->getValue() & 255) == Port->getValue()) {
3392 switch (Node->getValueType(0)) {
3393 case MVT::i8:
3394 BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue());
3395 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3396 return Result;
3397 case MVT::i16:
3398 BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue());
3399 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3400 return Result;
3401 case MVT::i32:
3402 BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue());
3403 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3404 return Result;
3405 default: break;
3406 }
3407 }
3408
3409 // Now, move the I/O port address into the DX register and use the IN
3410 // instruction to get the input data.
3411 //
3412 Tmp1 = SelectExpr(Node->getOperand(1));
3413 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1);
3414 switch (Node->getValueType(0)) {
3415 case MVT::i8:
3416 BuildMI(BB, X86::IN8rr, 0);
3417 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3418 return Result;
3419 case MVT::i16:
3420 BuildMI(BB, X86::IN16rr, 0);
3421 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3422 return Result;
3423 case MVT::i32:
3424 BuildMI(BB, X86::IN32rr, 0);
3425 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3426 return Result;
3427 default:
3428 std::cerr << "Cannot do input on this data type";
3429 exit(1);
3430 }
3431
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003432 }
3433
3434 return 0;
3435}
3436
Chris Lattnere10269b2005-01-17 19:25:26 +00003437/// TryToFoldLoadOpStore - Given a store node, try to fold together a
3438/// load/op/store instruction. If successful return true.
3439bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
3440 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
3441 SDOperand Chain = Node->getOperand(0);
3442 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00003443 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00003444
3445 // The chain has to be a load, the stored value must be an integer binary
3446 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00003447 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00003448 MVT::isFloatingPoint(StVal.getValueType()))
3449 return false;
3450
Chris Lattner5c659812005-01-17 22:10:42 +00003451 // Token chain must either be a factor node or the load to fold.
3452 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
3453 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00003454
Chris Lattner5c659812005-01-17 22:10:42 +00003455 SDOperand TheLoad;
3456
3457 // Check to see if there is a load from the same pointer that we're storing
3458 // to in either operand of the binop.
3459 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
3460 StVal.getOperand(0).getOperand(1) == StPtr)
3461 TheLoad = StVal.getOperand(0);
3462 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
3463 StVal.getOperand(1).getOperand(1) == StPtr)
3464 TheLoad = StVal.getOperand(1);
3465 else
3466 return false; // No matching load operand.
3467
3468 // We can only fold the load if there are no intervening side-effecting
3469 // operations. This means that the store uses the load as its token chain, or
3470 // there are only token factor nodes in between the store and load.
3471 if (Chain != TheLoad.getValue(1)) {
3472 // Okay, the other option is that we have a store referring to (possibly
3473 // nested) token factor nodes. For now, just try peeking through one level
3474 // of token factors to see if this is the case.
3475 bool ChainOk = false;
3476 if (Chain.getOpcode() == ISD::TokenFactor) {
3477 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3478 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
3479 ChainOk = true;
3480 break;
3481 }
3482 }
3483
3484 if (!ChainOk) return false;
3485 }
3486
3487 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00003488 return false;
3489
3490 // Make sure that one of the operands of the binop is the load, and that the
3491 // load folds into the binop.
3492 if (((StVal.getOperand(0) != TheLoad ||
3493 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
3494 (StVal.getOperand(1) != TheLoad ||
3495 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
3496 return false;
3497
3498 // Finally, check to see if this is one of the ops we can handle!
3499 static const unsigned ADDTAB[] = {
3500 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
3501 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
3502 };
3503 static const unsigned SUBTAB[] = {
3504 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
3505 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
3506 };
3507 static const unsigned ANDTAB[] = {
3508 X86::AND8mi, X86::AND16mi, X86::AND32mi,
3509 X86::AND8mr, X86::AND16mr, X86::AND32mr,
3510 };
3511 static const unsigned ORTAB[] = {
3512 X86::OR8mi, X86::OR16mi, X86::OR32mi,
3513 X86::OR8mr, X86::OR16mr, X86::OR32mr,
3514 };
3515 static const unsigned XORTAB[] = {
3516 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
3517 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
3518 };
3519 static const unsigned SHLTAB[] = {
3520 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
3521 /*Have to put the reg in CL*/0, 0, 0,
3522 };
3523 static const unsigned SARTAB[] = {
3524 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
3525 /*Have to put the reg in CL*/0, 0, 0,
3526 };
3527 static const unsigned SHRTAB[] = {
3528 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
3529 /*Have to put the reg in CL*/0, 0, 0,
3530 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003531
Chris Lattnere10269b2005-01-17 19:25:26 +00003532 const unsigned *TabPtr = 0;
3533 switch (StVal.getOpcode()) {
3534 default:
3535 std::cerr << "CANNOT [mem] op= val: ";
3536 StVal.Val->dump(); std::cerr << "\n";
3537 case ISD::MUL:
3538 case ISD::SDIV:
3539 case ISD::UDIV:
3540 case ISD::SREM:
3541 case ISD::UREM: return false;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003542
Chris Lattnere10269b2005-01-17 19:25:26 +00003543 case ISD::ADD: TabPtr = ADDTAB; break;
3544 case ISD::SUB: TabPtr = SUBTAB; break;
3545 case ISD::AND: TabPtr = ANDTAB; break;
3546 case ISD:: OR: TabPtr = ORTAB; break;
3547 case ISD::XOR: TabPtr = XORTAB; break;
3548 case ISD::SHL: TabPtr = SHLTAB; break;
3549 case ISD::SRA: TabPtr = SARTAB; break;
3550 case ISD::SRL: TabPtr = SHRTAB; break;
3551 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003552
Chris Lattnere10269b2005-01-17 19:25:26 +00003553 // Handle: [mem] op= CST
3554 SDOperand Op0 = StVal.getOperand(0);
3555 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00003556 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00003557 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
3558 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
3559 default: break;
3560 case MVT::i1:
3561 case MVT::i8: Opc = TabPtr[0]; break;
3562 case MVT::i16: Opc = TabPtr[1]; break;
3563 case MVT::i32: Opc = TabPtr[2]; break;
3564 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003565
Chris Lattnere10269b2005-01-17 19:25:26 +00003566 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00003567 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3568 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00003569 Select(Chain);
3570
Chris Lattnere10269b2005-01-17 19:25:26 +00003571 X86AddressMode AM;
3572 if (getRegPressure(TheLoad.getOperand(0)) >
3573 getRegPressure(TheLoad.getOperand(1))) {
3574 Select(TheLoad.getOperand(0));
3575 SelectAddress(TheLoad.getOperand(1), AM);
3576 } else {
3577 SelectAddress(TheLoad.getOperand(1), AM);
3578 Select(TheLoad.getOperand(0));
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003579 }
Chris Lattner5c659812005-01-17 22:10:42 +00003580
3581 if (StVal.getOpcode() == ISD::ADD) {
3582 if (CN->getValue() == 1) {
3583 switch (Op0.getValueType()) {
3584 default: break;
3585 case MVT::i8:
3586 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
3587 return true;
3588 case MVT::i16: Opc = TabPtr[1];
3589 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
3590 return true;
3591 case MVT::i32: Opc = TabPtr[2];
3592 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
3593 return true;
3594 }
3595 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
3596 switch (Op0.getValueType()) {
3597 default: break;
3598 case MVT::i8:
3599 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
3600 return true;
3601 case MVT::i16: Opc = TabPtr[1];
3602 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
3603 return true;
3604 case MVT::i32: Opc = TabPtr[2];
3605 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
3606 return true;
3607 }
3608 }
3609 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003610
Chris Lattnere10269b2005-01-17 19:25:26 +00003611 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
3612 return true;
3613 }
3614 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003615
Chris Lattnere10269b2005-01-17 19:25:26 +00003616 // If we have [mem] = V op [mem], try to turn it into:
3617 // [mem] = [mem] op V.
3618 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
3619 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
3620 StVal.getOpcode() != ISD::SRL)
3621 std::swap(Op0, Op1);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003622
Chris Lattnere10269b2005-01-17 19:25:26 +00003623 if (Op0 != TheLoad) return false;
3624
3625 switch (Op0.getValueType()) {
3626 default: return false;
3627 case MVT::i1:
3628 case MVT::i8: Opc = TabPtr[3]; break;
3629 case MVT::i16: Opc = TabPtr[4]; break;
3630 case MVT::i32: Opc = TabPtr[5]; break;
3631 }
Chris Lattner5c659812005-01-17 22:10:42 +00003632
Chris Lattnerb422aea2005-01-18 17:35:28 +00003633 // Table entry doesn't exist?
3634 if (Opc == 0) return false;
3635
Chris Lattner4a108662005-01-18 03:51:59 +00003636 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3637 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00003638 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00003639 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00003640
Chris Lattnere10269b2005-01-17 19:25:26 +00003641 X86AddressMode AM;
3642 SelectAddress(TheLoad.getOperand(1), AM);
3643 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00003644 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00003645 return true;
3646}
3647
3648
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003649void ISel::Select(SDOperand N) {
3650 unsigned Tmp1, Tmp2, Opc;
3651
Nate Begeman85fdeb22005-03-24 04:39:54 +00003652 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003653 return; // Already selected.
3654
Chris Lattner989de032005-01-11 06:14:36 +00003655 SDNode *Node = N.Val;
3656
3657 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003658 default:
Chris Lattner989de032005-01-11 06:14:36 +00003659 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003660 assert(0 && "Node not handled yet!");
3661 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00003662 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00003663 if (Node->getNumOperands() == 2) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003664 bool OneFirst =
Chris Lattner1d50b7f2005-01-13 19:56:00 +00003665 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
3666 Select(Node->getOperand(OneFirst));
3667 Select(Node->getOperand(!OneFirst));
3668 } else {
3669 std::vector<std::pair<unsigned, unsigned> > OpsP;
3670 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
3671 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
3672 std::sort(OpsP.begin(), OpsP.end());
3673 std::reverse(OpsP.begin(), OpsP.end());
3674 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
3675 Select(Node->getOperand(OpsP[i].second));
3676 }
Chris Lattnerc3580712005-01-13 18:01:36 +00003677 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003678 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00003679 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3680 Select(N.getOperand(0));
3681 Tmp1 = SelectExpr(N.getOperand(1));
3682 } else {
3683 Tmp1 = SelectExpr(N.getOperand(1));
3684 Select(N.getOperand(0));
3685 }
Chris Lattner18c2f132005-01-13 20:50:02 +00003686 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003687
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003688 if (Tmp1 != Tmp2) {
3689 switch (N.getOperand(1).getValueType()) {
3690 default: assert(0 && "Invalid type for operation!");
3691 case MVT::i1:
3692 case MVT::i8: Opc = X86::MOV8rr; break;
3693 case MVT::i16: Opc = X86::MOV16rr; break;
3694 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00003695 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003696 }
3697 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
3698 }
3699 return;
3700 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003701 switch (N.getNumOperands()) {
3702 default:
3703 assert(0 && "Unknown return instruction!");
3704 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003705 assert(N.getOperand(1).getValueType() == MVT::i32 &&
3706 N.getOperand(2).getValueType() == MVT::i32 &&
3707 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00003708 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
3709 Tmp1 = SelectExpr(N.getOperand(1));
3710 Tmp2 = SelectExpr(N.getOperand(2));
3711 } else {
3712 Tmp2 = SelectExpr(N.getOperand(2));
3713 Tmp1 = SelectExpr(N.getOperand(1));
3714 }
3715 Select(N.getOperand(0));
3716
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003717 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3718 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003719 break;
3720 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00003721 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3722 Select(N.getOperand(0));
3723 Tmp1 = SelectExpr(N.getOperand(1));
3724 } else {
3725 Tmp1 = SelectExpr(N.getOperand(1));
3726 Select(N.getOperand(0));
3727 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003728 switch (N.getOperand(1).getValueType()) {
3729 default: assert(0 && "All other types should have been promoted!!");
3730 case MVT::f64:
3731 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003732 break;
3733 case MVT::i32:
3734 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003735 break;
3736 }
3737 break;
3738 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00003739 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003740 break;
3741 }
Chris Lattner3648c672005-05-13 21:44:04 +00003742 if (X86Lowering.getBytesToPopOnReturn() == 0)
3743 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
3744 else
3745 BuildMI(BB, X86::RETI, 1).addImm(X86Lowering.getBytesToPopOnReturn());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003746 return;
3747 case ISD::BR: {
3748 Select(N.getOperand(0));
3749 MachineBasicBlock *Dest =
3750 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
3751 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
3752 return;
3753 }
3754
3755 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003756 MachineBasicBlock *Dest =
3757 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00003758
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003759 // Try to fold a setcc into the branch. If this fails, emit a test/jne
3760 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00003761 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
3762 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3763 Select(N.getOperand(0));
3764 Tmp1 = SelectExpr(N.getOperand(1));
3765 } else {
3766 Tmp1 = SelectExpr(N.getOperand(1));
3767 Select(N.getOperand(0));
3768 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003769 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
3770 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
3771 }
Chris Lattner11333092005-01-11 03:11:44 +00003772
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003773 return;
3774 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003775
Chris Lattner4df0de92005-01-17 00:00:33 +00003776 case ISD::LOAD:
3777 // If this load could be folded into the only using instruction, and if it
3778 // is safe to emit the instruction here, try to do so now.
3779 if (Node->hasNUsesOfValue(1, 0)) {
3780 SDOperand TheVal = N.getValue(0);
3781 SDNode *User = 0;
3782 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
3783 assert(UI != Node->use_end() && "Didn't find use!");
3784 SDNode *UN = *UI;
3785 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
3786 if (UN->getOperand(i) == TheVal) {
3787 User = UN;
3788 goto FoundIt;
3789 }
3790 }
3791 FoundIt:
3792 // Only handle unary operators right now.
3793 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00003794 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003795 SelectExpr(SDOperand(User, 0));
3796 return;
3797 }
3798 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003799 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003800 SelectExpr(N);
3801 return;
Chris Lattner966cdfb2005-05-09 21:17:38 +00003802 case ISD::READPORT:
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003803 case ISD::EXTLOAD:
3804 case ISD::SEXTLOAD:
3805 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003806 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner239738a2005-05-14 08:48:15 +00003807 case X86ISD::TAILCALL:
3808 case X86ISD::CALL:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003809 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003810 SelectExpr(N);
3811 return;
Chris Lattnerc6f41812005-05-12 23:06:28 +00003812 case ISD::CopyFromReg:
Chris Lattner67649df2005-05-14 06:52:07 +00003813 case X86ISD::FILD64m:
Chris Lattnerc6f41812005-05-12 23:06:28 +00003814 ExprMap.erase(N);
3815 SelectExpr(N.getValue(0));
3816 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003817
3818 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
3819 // On X86, we can represent all types except for Bool and Float natively.
3820 X86AddressMode AM;
3821 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00003822 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
3823 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
3824 && "Unsupported TRUNCSTORE for this target!");
3825
3826 if (StoredTy == MVT::i16) {
3827 // FIXME: This is here just to allow testing. X86 doesn't really have a
3828 // TRUNCSTORE i16 operation, but this is required for targets that do not
3829 // have 16-bit integer registers. We occasionally disable 16-bit integer
3830 // registers to test the promotion code.
3831 Select(N.getOperand(0));
3832 Tmp1 = SelectExpr(N.getOperand(1));
3833 SelectAddress(N.getOperand(2), AM);
3834
3835 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3836 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
3837 return;
3838 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003839
3840 // Store of constant bool?
3841 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3842 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3843 Select(N.getOperand(0));
3844 SelectAddress(N.getOperand(2), AM);
3845 } else {
3846 SelectAddress(N.getOperand(2), AM);
3847 Select(N.getOperand(0));
3848 }
3849 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
3850 return;
3851 }
3852
3853 switch (StoredTy) {
3854 default: assert(0 && "Cannot truncstore this type!");
3855 case MVT::i1: Opc = X86::MOV8mr; break;
3856 case MVT::f32: Opc = X86::FST32m; break;
3857 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003858
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003859 std::vector<std::pair<unsigned, unsigned> > RP;
3860 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3861 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3862 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3863 std::sort(RP.begin(), RP.end());
3864
Chris Lattner572dd082005-02-23 05:57:21 +00003865 Tmp1 = 0; // Silence a warning.
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003866 for (unsigned i = 0; i != 3; ++i)
3867 switch (RP[2-i].second) {
3868 default: assert(0 && "Unknown operand number!");
3869 case 0: Select(N.getOperand(0)); break;
3870 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3871 case 2: SelectAddress(N.getOperand(2), AM); break;
3872 }
3873
3874 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3875 return;
3876 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003877 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003878 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003879
3880 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3881 Opc = 0;
3882 switch (CN->getValueType(0)) {
3883 default: assert(0 && "Invalid type for operation!");
3884 case MVT::i1:
3885 case MVT::i8: Opc = X86::MOV8mi; break;
3886 case MVT::i16: Opc = X86::MOV16mi; break;
3887 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003888 case MVT::f64: break;
3889 }
3890 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00003891 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3892 Select(N.getOperand(0));
3893 SelectAddress(N.getOperand(2), AM);
3894 } else {
3895 SelectAddress(N.getOperand(2), AM);
3896 Select(N.getOperand(0));
3897 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003898 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
3899 return;
3900 }
Chris Lattner75f354b2005-04-21 19:03:24 +00003901 } else if (GlobalAddressSDNode *GA =
3902 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3903 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
3904
3905 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3906 Select(N.getOperand(0));
3907 SelectAddress(N.getOperand(2), AM);
3908 } else {
3909 SelectAddress(N.getOperand(2), AM);
3910 Select(N.getOperand(0));
3911 }
3912 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),
3913 AM).addGlobalAddress(GA->getGlobal());
3914 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003915 }
Chris Lattner837caa72005-01-11 23:21:30 +00003916
3917 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00003918 if (TryToFoldLoadOpStore(Node))
3919 return;
Chris Lattner837caa72005-01-11 23:21:30 +00003920
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003921 switch (N.getOperand(1).getValueType()) {
3922 default: assert(0 && "Cannot store this type!");
3923 case MVT::i1:
3924 case MVT::i8: Opc = X86::MOV8mr; break;
3925 case MVT::i16: Opc = X86::MOV16mr; break;
3926 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00003927 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003928 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003929
Chris Lattner11333092005-01-11 03:11:44 +00003930 std::vector<std::pair<unsigned, unsigned> > RP;
3931 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3932 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3933 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3934 std::sort(RP.begin(), RP.end());
3935
Chris Lattner572dd082005-02-23 05:57:21 +00003936 Tmp1 = 0; // Silence a warning.
Chris Lattner11333092005-01-11 03:11:44 +00003937 for (unsigned i = 0; i != 3; ++i)
3938 switch (RP[2-i].second) {
3939 default: assert(0 && "Unknown operand number!");
3940 case 0: Select(N.getOperand(0)); break;
3941 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00003942 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00003943 }
3944
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003945 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3946 return;
3947 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00003948 case ISD::CALLSEQ_START:
Chris Lattner3648c672005-05-13 21:44:04 +00003949 Select(N.getOperand(0));
3950 // Stack amount
3951 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
3952 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(Tmp1);
3953 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00003954 case ISD::CALLSEQ_END:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003955 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003956 return;
Chris Lattner989de032005-01-11 06:14:36 +00003957 case ISD::MEMSET: {
3958 Select(N.getOperand(0)); // Select the chain.
3959 unsigned Align =
3960 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3961 if (Align == 0) Align = 1;
3962
3963 // Turn the byte code into # iterations
3964 unsigned CountReg;
3965 unsigned Opcode;
3966 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3967 unsigned Val = ValC->getValue() & 255;
3968
3969 // If the value is a constant, then we can potentially use larger sets.
3970 switch (Align & 3) {
3971 case 2: // WORD aligned
3972 CountReg = MakeReg(MVT::i32);
3973 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3974 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3975 } else {
3976 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3977 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3978 }
3979 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3980 Opcode = X86::REP_STOSW;
3981 break;
3982 case 0: // DWORD aligned
3983 CountReg = MakeReg(MVT::i32);
3984 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3985 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3986 } else {
3987 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3988 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3989 }
3990 Val = (Val << 8) | Val;
3991 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3992 Opcode = X86::REP_STOSD;
3993 break;
3994 default: // BYTE aligned
3995 CountReg = SelectExpr(Node->getOperand(3));
3996 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3997 Opcode = X86::REP_STOSB;
3998 break;
3999 }
4000 } else {
4001 // If it's not a constant value we are storing, just fall back. We could
4002 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
4003 unsigned ValReg = SelectExpr(Node->getOperand(2));
4004 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
4005 CountReg = SelectExpr(Node->getOperand(3));
4006 Opcode = X86::REP_STOSB;
4007 }
4008
4009 // No matter what the alignment is, we put the source in ESI, the
4010 // destination in EDI, and the count in ECX.
4011 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
4012 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
4013 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
4014 BuildMI(BB, Opcode, 0);
4015 return;
4016 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00004017 case ISD::MEMCPY: {
Chris Lattner31805bf2005-01-11 06:19:26 +00004018 Select(N.getOperand(0)); // Select the chain.
4019 unsigned Align =
4020 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
4021 if (Align == 0) Align = 1;
4022
4023 // Turn the byte code into # iterations
4024 unsigned CountReg;
4025 unsigned Opcode;
4026 switch (Align & 3) {
4027 case 2: // WORD aligned
4028 CountReg = MakeReg(MVT::i32);
4029 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4030 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
4031 } else {
4032 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4033 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
4034 }
4035 Opcode = X86::REP_MOVSW;
4036 break;
4037 case 0: // DWORD aligned
4038 CountReg = MakeReg(MVT::i32);
4039 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
4040 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
4041 } else {
4042 unsigned ByteReg = SelectExpr(Node->getOperand(3));
4043 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
4044 }
4045 Opcode = X86::REP_MOVSD;
4046 break;
4047 default: // BYTE aligned
4048 CountReg = SelectExpr(Node->getOperand(3));
4049 Opcode = X86::REP_MOVSB;
4050 break;
4051 }
4052
4053 // No matter what the alignment is, we put the source in ESI, the
4054 // destination in EDI, and the count in ECX.
4055 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
4056 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
4057 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
4058 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
4059 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
4060 BuildMI(BB, Opcode, 0);
4061 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004062 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00004063 case ISD::WRITEPORT:
4064 if (Node->getOperand(2).getValueType() != MVT::i16) {
4065 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
4066 exit(1);
4067 }
4068 Select(Node->getOperand(0)); // Emit the chain.
4069
4070 Tmp1 = SelectExpr(Node->getOperand(1));
4071 switch (Node->getOperand(1).getValueType()) {
4072 case MVT::i8:
4073 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
4074 Tmp2 = X86::OUT8ir; Opc = X86::OUT8rr;
4075 break;
4076 case MVT::i16:
4077 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(Tmp1);
4078 Tmp2 = X86::OUT16ir; Opc = X86::OUT16rr;
4079 break;
4080 case MVT::i32:
4081 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
4082 Tmp2 = X86::OUT32ir; Opc = X86::OUT32rr;
4083 break;
4084 default:
4085 std::cerr << "llvm.writeport: invalid data type for X86 target";
4086 exit(1);
4087 }
4088
4089 // If the port is a single-byte constant, use the immediate form.
4090 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(2)))
4091 if ((CN->getValue() & 255) == CN->getValue()) {
4092 BuildMI(BB, Tmp2, 1).addImm(CN->getValue());
4093 return;
4094 }
4095
4096 // Otherwise, move the I/O port address into the DX register.
4097 unsigned Reg = SelectExpr(Node->getOperand(2));
4098 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
4099 BuildMI(BB, Opc, 0);
4100 return;
4101 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004102 assert(0 && "Should not be reached!");
4103}
4104
4105
4106/// createX86PatternInstructionSelector - This pass converts an LLVM function
4107/// into a machine code representation using pattern matching and a machine
4108/// description file.
4109///
4110FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00004111 return new ISel(TM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00004112}