blob: 343732e6801520f2d77c8eb777d9de9100f94de3 [file] [log] [blame]
Dan Gohman1adf1b02008-08-19 21:45:35 +00001//===-- X86FastISel.cpp - X86 FastISel implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the X86-specific support for the FastISel class. Much
11// of the target-specific code is generated by tablegen in the file
12// X86GenFastISel.inc, which is #included here.
13//
14//===----------------------------------------------------------------------===//
15
16#include "X86.h"
Evan Cheng8b19e562008-09-03 06:44:39 +000017#include "X86InstrBuilder.h"
Dan Gohman1adf1b02008-08-19 21:45:35 +000018#include "X86ISelLowering.h"
Evan Cheng88e30412008-09-03 01:04:47 +000019#include "X86RegisterInfo.h"
20#include "X86Subtarget.h"
Dan Gohman22bb3112008-08-22 00:20:26 +000021#include "X86TargetMachine.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000022#include "llvm/CallingConv.h"
Dan Gohman6e3f05f2008-09-04 23:26:51 +000023#include "llvm/DerivedTypes.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000024#include "llvm/Instructions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000025#include "llvm/CodeGen/FastISel.h"
Owen Anderson95267a12008-09-05 00:06:23 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000029#include "llvm/Support/CallSite.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000030
31using namespace llvm;
32
33class X86FastISel : public FastISel {
34 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
35 /// make the right decision when generating code for different targets.
36 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000037
38 /// StackPtr - Register used as the stack pointer.
39 ///
40 unsigned StackPtr;
41
42 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
43 /// floating point ops.
44 /// When SSE is available, use it for f32 operations.
45 /// When SSE2 is available, use it for f64 operations.
46 bool X86ScalarSSEf64;
47 bool X86ScalarSSEf32;
48
Evan Cheng8b19e562008-09-03 06:44:39 +000049public:
Dan Gohman3df24e62008-09-03 23:12:08 +000050 explicit X86FastISel(MachineFunction &mf,
51 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +000052 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
53 DenseMap<const AllocaInst *, int> &am)
54 : FastISel(mf, vm, bm, am) {
Evan Cheng88e30412008-09-03 01:04:47 +000055 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000056 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
57 X86ScalarSSEf64 = Subtarget->hasSSE2();
58 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000059 }
Evan Chengc3f44b02008-09-03 00:03:49 +000060
Dan Gohman3df24e62008-09-03 23:12:08 +000061 virtual bool TargetSelectInstruction(Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000062
Dan Gohman1adf1b02008-08-19 21:45:35 +000063#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000064
65private:
Dan Gohman0586d912008-09-10 20:11:02 +000066 bool X86FastEmitLoad(MVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000067
Evan Chengf3d4efe2008-09-07 09:09:33 +000068 bool X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000069 const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000070
71 bool X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT, unsigned Src, MVT SrcVT,
72 unsigned &ResultReg);
Evan Cheng0de588f2008-09-05 21:00:03 +000073
Evan Cheng59fbc802008-09-09 01:26:59 +000074 bool X86SelectConstAddr(Value *V, unsigned &Op0,
75 bool isCall = false, bool inReg = false);
Evan Cheng8b19e562008-09-03 06:44:39 +000076
Dan Gohman0586d912008-09-10 20:11:02 +000077 bool X86SelectAddress(Value *V, X86AddressMode &AM);
78
Dan Gohman3df24e62008-09-03 23:12:08 +000079 bool X86SelectLoad(Instruction *I);
Owen Andersona3971df2008-09-04 07:08:58 +000080
81 bool X86SelectStore(Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000082
83 bool X86SelectCmp(Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000084
85 bool X86SelectZExt(Instruction *I);
86
87 bool X86SelectBranch(Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +000088
89 bool X86SelectShift(Instruction *I);
90
91 bool X86SelectSelect(Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +000092
Evan Cheng10a8d9c2008-09-07 08:47:42 +000093 bool X86SelectTrunc(Instruction *I);
94
Evan Chengf3d4efe2008-09-07 09:09:33 +000095 bool X86SelectCall(Instruction *I);
96
97 CCAssignFn *CCAssignFnForCall(unsigned CC, bool isTailCall = false);
98
Dan Gohman0586d912008-09-10 20:11:02 +000099 unsigned TargetMaterializeConstant(Constant *C);
100
101 unsigned TargetMaterializeAlloca(AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000102
103 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
104 /// computed in an SSE register, not on the X87 floating point stack.
105 bool isScalarFPTypeInSSEReg(MVT VT) const {
106 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
107 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
108 }
109
Evan Chengc3f44b02008-09-03 00:03:49 +0000110};
Dan Gohman99b21822008-08-28 23:21:34 +0000111
Evan Chengdebdea02008-09-08 17:15:42 +0000112static bool isTypeLegal(const Type *Ty, const TargetLowering &TLI, MVT &VT,
113 bool AllowI1 = false) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000114 VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
115 if (VT == MVT::Other || !VT.isSimple())
116 // Unhandled type. Halt "fast" selection and bail.
117 return false;
118 if (VT == MVT::iPTR)
119 // Use pointer type.
120 VT = TLI.getPointerTy();
121 // We only handle legal types. For example, on x86-32 the instruction
122 // selector contains all of the 64-bit instructions from x86-64,
123 // under the assumption that i64 won't be used if the target doesn't
124 // support it.
Evan Chengdebdea02008-09-08 17:15:42 +0000125 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000126}
127
128#include "X86GenCallingConv.inc"
129
130/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
131/// convention.
132CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) {
133 if (Subtarget->is64Bit()) {
134 if (Subtarget->isTargetWin64())
135 return CC_X86_Win64_C;
136 else if (CC == CallingConv::Fast && isTaillCall)
137 return CC_X86_64_TailCall;
138 else
139 return CC_X86_64_C;
140 }
141
142 if (CC == CallingConv::X86_FastCall)
143 return CC_X86_32_FastCall;
144 else if (CC == CallingConv::Fast && isTaillCall)
145 return CC_X86_32_TailCall;
146 else if (CC == CallingConv::Fast)
147 return CC_X86_32_FastCC;
148 else
149 return CC_X86_32_C;
150}
151
Evan Cheng0de588f2008-09-05 21:00:03 +0000152/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000153/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000154/// Return true and the result register by reference if it is possible.
Dan Gohman0586d912008-09-10 20:11:02 +0000155bool X86FastISel::X86FastEmitLoad(MVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000156 unsigned &ResultReg) {
157 // Get opcode and regclass of the output for the given load instruction.
158 unsigned Opc = 0;
159 const TargetRegisterClass *RC = NULL;
160 switch (VT.getSimpleVT()) {
161 default: return false;
162 case MVT::i8:
163 Opc = X86::MOV8rm;
164 RC = X86::GR8RegisterClass;
165 break;
166 case MVT::i16:
167 Opc = X86::MOV16rm;
168 RC = X86::GR16RegisterClass;
169 break;
170 case MVT::i32:
171 Opc = X86::MOV32rm;
172 RC = X86::GR32RegisterClass;
173 break;
174 case MVT::i64:
175 // Must be in x86-64 mode.
176 Opc = X86::MOV64rm;
177 RC = X86::GR64RegisterClass;
178 break;
179 case MVT::f32:
180 if (Subtarget->hasSSE1()) {
181 Opc = X86::MOVSSrm;
182 RC = X86::FR32RegisterClass;
183 } else {
184 Opc = X86::LD_Fp32m;
185 RC = X86::RFP32RegisterClass;
186 }
187 break;
188 case MVT::f64:
189 if (Subtarget->hasSSE2()) {
190 Opc = X86::MOVSDrm;
191 RC = X86::FR64RegisterClass;
192 } else {
193 Opc = X86::LD_Fp64m;
194 RC = X86::RFP64RegisterClass;
195 }
196 break;
197 case MVT::f80:
198 Opc = X86::LD_Fp80m;
199 RC = X86::RFP80RegisterClass;
200 break;
201 }
202
203 ResultReg = createResultReg(RC);
Evan Cheng0de588f2008-09-05 21:00:03 +0000204 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
205 return true;
206}
207
Evan Chengf3d4efe2008-09-07 09:09:33 +0000208/// X86FastEmitStore - Emit a machine instruction to store a value Val of
209/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
210/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000211/// i.e. V. Return true if it is possible.
212bool
Evan Chengf3d4efe2008-09-07 09:09:33 +0000213X86FastISel::X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000214 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000215 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000216 unsigned Opc = 0;
217 const TargetRegisterClass *RC = NULL;
218 switch (VT.getSimpleVT()) {
219 default: return false;
220 case MVT::i8:
221 Opc = X86::MOV8mr;
222 RC = X86::GR8RegisterClass;
223 break;
224 case MVT::i16:
225 Opc = X86::MOV16mr;
226 RC = X86::GR16RegisterClass;
227 break;
228 case MVT::i32:
229 Opc = X86::MOV32mr;
230 RC = X86::GR32RegisterClass;
231 break;
232 case MVT::i64:
233 // Must be in x86-64 mode.
234 Opc = X86::MOV64mr;
235 RC = X86::GR64RegisterClass;
236 break;
237 case MVT::f32:
238 if (Subtarget->hasSSE1()) {
239 Opc = X86::MOVSSmr;
240 RC = X86::FR32RegisterClass;
241 } else {
242 Opc = X86::ST_Fp32m;
243 RC = X86::RFP32RegisterClass;
244 }
245 break;
246 case MVT::f64:
247 if (Subtarget->hasSSE2()) {
248 Opc = X86::MOVSDmr;
249 RC = X86::FR64RegisterClass;
250 } else {
251 Opc = X86::ST_Fp64m;
252 RC = X86::RFP64RegisterClass;
253 }
254 break;
255 case MVT::f80:
256 Opc = X86::ST_FP80m;
257 RC = X86::RFP80RegisterClass;
258 break;
259 }
260
Evan Chengf3d4efe2008-09-07 09:09:33 +0000261 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000262 return true;
263}
264
Evan Cheng24e3a902008-09-08 06:35:17 +0000265/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
266/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
267/// ISD::SIGN_EXTEND).
268bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT,
269 unsigned Src, MVT SrcVT,
270 unsigned &ResultReg) {
271 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
272 return ResultReg != 0;
273}
274
Evan Cheng8b19e562008-09-03 06:44:39 +0000275/// X86SelectConstAddr - Select and emit code to materialize constant address.
276///
Evan Cheng59fbc802008-09-09 01:26:59 +0000277bool X86FastISel::X86SelectConstAddr(Value *V, unsigned &Op0,
278 bool isCall, bool inReg) {
Evan Cheng8b19e562008-09-03 06:44:39 +0000279 // FIXME: Only GlobalAddress for now.
280 GlobalValue *GV = dyn_cast<GlobalValue>(V);
281 if (!GV)
282 return false;
283
Evan Chengf3d4efe2008-09-07 09:09:33 +0000284 if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) {
Evan Cheng8b19e562008-09-03 06:44:39 +0000285 // Issue load from stub if necessary.
286 unsigned Opc = 0;
287 const TargetRegisterClass *RC = NULL;
288 if (TLI.getPointerTy() == MVT::i32) {
289 Opc = X86::MOV32rm;
290 RC = X86::GR32RegisterClass;
291 } else {
292 Opc = X86::MOV64rm;
293 RC = X86::GR64RegisterClass;
294 }
295 Op0 = createResultReg(RC);
296 X86AddressMode AM;
297 AM.GV = GV;
298 addFullAddress(BuildMI(MBB, TII.get(Opc), Op0), AM);
Evan Cheng373d50a2008-09-04 06:18:33 +0000299 // Prevent loading GV stub multiple times in same MBB.
300 LocalValueMap[V] = Op0;
Evan Cheng59fbc802008-09-09 01:26:59 +0000301 } else if (inReg) {
302 unsigned Opc = 0;
303 const TargetRegisterClass *RC = NULL;
304 if (TLI.getPointerTy() == MVT::i32) {
305 Opc = X86::LEA32r;
306 RC = X86::GR32RegisterClass;
307 } else {
308 Opc = X86::LEA64r;
309 RC = X86::GR64RegisterClass;
310 }
311 Op0 = createResultReg(RC);
312 X86AddressMode AM;
313 AM.GV = GV;
314 addFullAddress(BuildMI(MBB, TII.get(Opc), Op0), AM);
315 // Prevent materializing GV address multiple times in same MBB.
316 LocalValueMap[V] = Op0;
Evan Cheng8b19e562008-09-03 06:44:39 +0000317 }
Evan Cheng59fbc802008-09-09 01:26:59 +0000318
Evan Cheng8b19e562008-09-03 06:44:39 +0000319 return true;
320}
321
Dan Gohman0586d912008-09-10 20:11:02 +0000322/// X86SelectAddress - Attempt to fill in an address from the given value.
323///
324bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM) {
325 // Look past bitcasts.
326 if (const BitCastInst *BC = dyn_cast<BitCastInst>(V))
327 return X86SelectAddress(BC->getOperand(0), AM);
328
329 if (const AllocaInst *A = dyn_cast<AllocaInst>(V)) {
330 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
331 if (SI == StaticAllocaMap.end())
332 return false;
333 AM.BaseType = X86AddressMode::FrameIndexBase;
334 AM.Base.FrameIndex = SI->second;
335 } else if (unsigned Ptr = lookUpRegForValue(V)) {
336 AM.Base.Reg = Ptr;
337 } else {
338 // Handle constant address.
339 // FIXME: If load type is something we can't handle, this can result in
340 // a dead stub load instruction.
341 if (isa<Constant>(V) && X86SelectConstAddr(V, AM.Base.Reg)) {
342 if (AM.Base.Reg == 0)
343 AM.GV = cast<GlobalValue>(V);
344 } else {
345 AM.Base.Reg = getRegForValue(V);
346 if (AM.Base.Reg == 0)
347 // Unhandled operand. Halt "fast" selection and bail.
348 return false;
349 }
350 }
351
352 return true;
353}
354
Owen Andersona3971df2008-09-04 07:08:58 +0000355/// X86SelectStore - Select and emit code to implement store instructions.
356bool X86FastISel::X86SelectStore(Instruction* I) {
Evan Cheng24e3a902008-09-08 06:35:17 +0000357 MVT VT;
358 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
Owen Andersona3971df2008-09-04 07:08:58 +0000359 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000360 unsigned Val = getRegForValue(I->getOperand(0));
361 if (Val == 0)
Owen Andersona3971df2008-09-04 07:08:58 +0000362 // Unhandled operand. Halt "fast" selection and bail.
363 return false;
364
Dan Gohman0586d912008-09-10 20:11:02 +0000365 X86AddressMode AM;
366 if (!X86SelectAddress(I->getOperand(1), AM))
367 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000368
Dan Gohman0586d912008-09-10 20:11:02 +0000369 return X86FastEmitStore(VT, Val, AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000370}
371
Evan Cheng8b19e562008-09-03 06:44:39 +0000372/// X86SelectLoad - Select and emit code to implement load instructions.
373///
Dan Gohman3df24e62008-09-03 23:12:08 +0000374bool X86FastISel::X86SelectLoad(Instruction *I) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000375 MVT VT;
376 if (!isTypeLegal(I->getType(), TLI, VT))
Evan Cheng8b19e562008-09-03 06:44:39 +0000377 return false;
378
Dan Gohman0586d912008-09-10 20:11:02 +0000379 X86AddressMode AM;
380 if (!X86SelectAddress(I->getOperand(0), AM))
381 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000382
Evan Cheng0de588f2008-09-05 21:00:03 +0000383 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000384 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000385 UpdateValueMap(I, ResultReg);
386 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000387 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000388 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000389}
390
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000391bool X86FastISel::X86SelectCmp(Instruction *I) {
392 CmpInst *CI = cast<CmpInst>(I);
393
Dan Gohman4f22bb02008-09-05 01:33:56 +0000394 MVT VT = TLI.getValueType(I->getOperand(0)->getType());
395 if (!TLI.isTypeLegal(VT))
396 return false;
397
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000398 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000399 if (Op0Reg == 0) return false;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000400 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000401 if (Op1Reg == 0) return false;
402
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000403 unsigned Opc;
Dan Gohmanf52550b2008-09-05 01:15:35 +0000404 switch (VT.getSimpleVT()) {
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000405 case MVT::i8: Opc = X86::CMP8rr; break;
406 case MVT::i16: Opc = X86::CMP16rr; break;
407 case MVT::i32: Opc = X86::CMP32rr; break;
408 case MVT::i64: Opc = X86::CMP64rr; break;
409 case MVT::f32: Opc = X86::UCOMISSrr; break;
410 case MVT::f64: Opc = X86::UCOMISDrr; break;
411 default: return false;
412 }
413
414 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
415 switch (CI->getPredicate()) {
416 case CmpInst::FCMP_OEQ: {
417 unsigned EReg = createResultReg(&X86::GR8RegClass);
418 unsigned NPReg = createResultReg(&X86::GR8RegClass);
419 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
420 BuildMI(MBB, TII.get(X86::SETEr), EReg);
421 BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
422 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
423 break;
424 }
425 case CmpInst::FCMP_UNE: {
426 unsigned NEReg = createResultReg(&X86::GR8RegClass);
427 unsigned PReg = createResultReg(&X86::GR8RegClass);
428 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
429 BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
430 BuildMI(MBB, TII.get(X86::SETPr), PReg);
431 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
432 break;
433 }
434 case CmpInst::FCMP_OGT:
435 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
436 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
437 break;
438 case CmpInst::FCMP_OGE:
439 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
440 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
441 break;
442 case CmpInst::FCMP_OLT:
443 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
444 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
445 break;
446 case CmpInst::FCMP_OLE:
447 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
448 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
449 break;
450 case CmpInst::FCMP_ONE:
451 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
452 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
453 break;
454 case CmpInst::FCMP_ORD:
455 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
456 BuildMI(MBB, TII.get(X86::SETNPr), ResultReg);
457 break;
458 case CmpInst::FCMP_UNO:
459 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
460 BuildMI(MBB, TII.get(X86::SETPr), ResultReg);
461 break;
462 case CmpInst::FCMP_UEQ:
463 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
464 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
465 break;
466 case CmpInst::FCMP_UGT:
467 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
468 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
469 break;
470 case CmpInst::FCMP_UGE:
471 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
472 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
473 break;
474 case CmpInst::FCMP_ULT:
475 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
476 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
477 break;
478 case CmpInst::FCMP_ULE:
479 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
480 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
481 break;
482 case CmpInst::ICMP_EQ:
483 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
484 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
485 break;
486 case CmpInst::ICMP_NE:
487 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
488 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
489 break;
490 case CmpInst::ICMP_UGT:
491 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
492 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
493 break;
494 case CmpInst::ICMP_UGE:
495 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
496 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
497 break;
498 case CmpInst::ICMP_ULT:
499 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
500 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
501 break;
502 case CmpInst::ICMP_ULE:
503 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
504 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
505 break;
506 case CmpInst::ICMP_SGT:
507 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
508 BuildMI(MBB, TII.get(X86::SETGr), ResultReg);
509 break;
510 case CmpInst::ICMP_SGE:
511 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
512 BuildMI(MBB, TII.get(X86::SETGEr), ResultReg);
513 break;
514 case CmpInst::ICMP_SLT:
515 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
516 BuildMI(MBB, TII.get(X86::SETLr), ResultReg);
517 break;
518 case CmpInst::ICMP_SLE:
519 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
520 BuildMI(MBB, TII.get(X86::SETLEr), ResultReg);
521 break;
522 default:
523 return false;
524 }
525
526 UpdateValueMap(I, ResultReg);
527 return true;
528}
Evan Cheng8b19e562008-09-03 06:44:39 +0000529
Dan Gohmand89ae992008-09-05 01:06:14 +0000530bool X86FastISel::X86SelectZExt(Instruction *I) {
531 // Special-case hack: The only i1 values we know how to produce currently
532 // set the upper bits of an i8 value to zero.
533 if (I->getType() == Type::Int8Ty &&
534 I->getOperand(0)->getType() == Type::Int1Ty) {
535 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000536 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000537 UpdateValueMap(I, ResultReg);
538 return true;
539 }
540
541 return false;
542}
543
544bool X86FastISel::X86SelectBranch(Instruction *I) {
545 BranchInst *BI = cast<BranchInst>(I);
546 // Unconditional branches are selected by tablegen-generated code.
547 unsigned OpReg = getRegForValue(BI->getCondition());
Dan Gohmanf52550b2008-09-05 01:15:35 +0000548 if (OpReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000549 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
550 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
551
552 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
553 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
554 BuildMI(MBB, TII.get(X86::JMP)).addMBB(FalseMBB);
555
556 MBB->addSuccessor(TrueMBB);
557 MBB->addSuccessor(FalseMBB);
558
559 return true;
560}
561
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000562bool X86FastISel::X86SelectShift(Instruction *I) {
563 unsigned CReg = 0;
564 unsigned Opc = 0;
565 const TargetRegisterClass *RC = NULL;
566 if (I->getType() == Type::Int8Ty) {
567 CReg = X86::CL;
568 RC = &X86::GR8RegClass;
569 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000570 case Instruction::LShr: Opc = X86::SHR8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000571 case Instruction::AShr: Opc = X86::SAR8rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000572 case Instruction::Shl: Opc = X86::SHL8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000573 default: return false;
574 }
575 } else if (I->getType() == Type::Int16Ty) {
576 CReg = X86::CX;
577 RC = &X86::GR16RegClass;
578 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000579 case Instruction::LShr: Opc = X86::SHR16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000580 case Instruction::AShr: Opc = X86::SAR16rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000581 case Instruction::Shl: Opc = X86::SHL16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000582 default: return false;
583 }
584 } else if (I->getType() == Type::Int32Ty) {
585 CReg = X86::ECX;
586 RC = &X86::GR32RegClass;
587 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000588 case Instruction::LShr: Opc = X86::SHR32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000589 case Instruction::AShr: Opc = X86::SAR32rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000590 case Instruction::Shl: Opc = X86::SHL32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000591 default: return false;
592 }
593 } else if (I->getType() == Type::Int64Ty) {
594 CReg = X86::RCX;
595 RC = &X86::GR64RegClass;
596 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000597 case Instruction::LShr: Opc = X86::SHR64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000598 case Instruction::AShr: Opc = X86::SAR64rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000599 case Instruction::Shl: Opc = X86::SHL64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000600 default: return false;
601 }
602 } else {
603 return false;
604 }
605
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000606 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
607 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
608 return false;
609
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000610 unsigned Op0Reg = getRegForValue(I->getOperand(0));
611 if (Op0Reg == 0) return false;
612 unsigned Op1Reg = getRegForValue(I->getOperand(1));
613 if (Op1Reg == 0) return false;
614 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
615 unsigned ResultReg = createResultReg(RC);
616 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op0Reg);
617 UpdateValueMap(I, ResultReg);
618 return true;
619}
620
621bool X86FastISel::X86SelectSelect(Instruction *I) {
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000622 const Type *Ty = I->getType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000623 if (isa<PointerType>(Ty))
624 Ty = TLI.getTargetData()->getIntPtrType();
625
626 unsigned Opc = 0;
627 const TargetRegisterClass *RC = NULL;
628 if (Ty == Type::Int16Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000629 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000630 RC = &X86::GR16RegClass;
631 } else if (Ty == Type::Int32Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000632 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000633 RC = &X86::GR32RegClass;
634 } else if (Ty == Type::Int64Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000635 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000636 RC = &X86::GR64RegClass;
637 } else {
638 return false;
639 }
640
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000641 MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
642 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
643 return false;
644
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000645 unsigned Op0Reg = getRegForValue(I->getOperand(0));
646 if (Op0Reg == 0) return false;
647 unsigned Op1Reg = getRegForValue(I->getOperand(1));
648 if (Op1Reg == 0) return false;
649 unsigned Op2Reg = getRegForValue(I->getOperand(2));
650 if (Op2Reg == 0) return false;
651
652 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
653 unsigned ResultReg = createResultReg(RC);
654 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
655 UpdateValueMap(I, ResultReg);
656 return true;
657}
658
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000659bool X86FastISel::X86SelectTrunc(Instruction *I) {
660 if (Subtarget->is64Bit())
661 // All other cases should be handled by the tblgen generated code.
662 return false;
663 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
664 MVT DstVT = TLI.getValueType(I->getType());
665 if (DstVT != MVT::i8)
666 // All other cases should be handled by the tblgen generated code.
667 return false;
668 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
669 // All other cases should be handled by the tblgen generated code.
670 return false;
671
672 unsigned InputReg = getRegForValue(I->getOperand(0));
673 if (!InputReg)
674 // Unhandled operand. Halt "fast" selection and bail.
675 return false;
676
677 // First issue a copy to GR16_ or GR32_.
678 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
679 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
680 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
681 unsigned CopyReg = createResultReg(CopyRC);
682 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
683
684 // Then issue an extract_subreg.
685 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg,1); // x86_subreg_8bit
686 if (!ResultReg)
687 return false;
688
689 UpdateValueMap(I, ResultReg);
690 return true;
691}
692
Evan Chengf3d4efe2008-09-07 09:09:33 +0000693bool X86FastISel::X86SelectCall(Instruction *I) {
694 CallInst *CI = cast<CallInst>(I);
695 Value *Callee = I->getOperand(0);
696
697 // Can't handle inline asm yet.
698 if (isa<InlineAsm>(Callee))
699 return false;
700
701 // FIXME: Handle some intrinsics.
702 if (Function *F = CI->getCalledFunction()) {
703 if (F->isDeclaration() &&F->getIntrinsicID())
704 return false;
705 }
706
707 // Materialize callee address in a register. FIXME: GV address can be
708 // handled with a CALLpcrel32 instead.
709 unsigned CalleeOp = getRegForValue(Callee);
710 if (CalleeOp == 0) {
711 if (!isa<Constant>(Callee) || !X86SelectConstAddr(Callee, CalleeOp, true))
712 // Unhandled operand. Halt "fast" selection and bail.
713 return false;
714 }
715
716 // Handle only C and fastcc calling conventions for now.
717 CallSite CS(CI);
718 unsigned CC = CS.getCallingConv();
719 if (CC != CallingConv::C &&
720 CC != CallingConv::Fast &&
721 CC != CallingConv::X86_FastCall)
722 return false;
723
724 // Let SDISel handle vararg functions.
725 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
726 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
727 if (FTy->isVarArg())
728 return false;
729
730 // Handle *simple* calls for now.
731 const Type *RetTy = CS.getType();
732 MVT RetVT;
Evan Chengdebdea02008-09-08 17:15:42 +0000733 if (!isTypeLegal(RetTy, TLI, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000734 return false;
735
Evan Chengdebdea02008-09-08 17:15:42 +0000736 // Allow calls which produce i1 results.
737 bool AndToI1 = false;
738 if (RetVT == MVT::i1) {
739 RetVT = MVT::i8;
740 AndToI1 = true;
741 }
742
Evan Chengf3d4efe2008-09-07 09:09:33 +0000743 // Deal with call operands first.
744 SmallVector<unsigned, 4> Args;
745 SmallVector<MVT, 4> ArgVTs;
746 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
747 Args.reserve(CS.arg_size());
748 ArgVTs.reserve(CS.arg_size());
749 ArgFlags.reserve(CS.arg_size());
750 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
751 i != e; ++i) {
752 unsigned Arg = getRegForValue(*i);
753 if (Arg == 0)
754 return false;
755 ISD::ArgFlagsTy Flags;
756 unsigned AttrInd = i - CS.arg_begin() + 1;
757 if (CS.paramHasAttr(AttrInd, ParamAttr::SExt))
758 Flags.setSExt();
759 if (CS.paramHasAttr(AttrInd, ParamAttr::ZExt))
760 Flags.setZExt();
761
762 // FIXME: Only handle *easy* calls for now.
763 if (CS.paramHasAttr(AttrInd, ParamAttr::InReg) ||
764 CS.paramHasAttr(AttrInd, ParamAttr::StructRet) ||
765 CS.paramHasAttr(AttrInd, ParamAttr::Nest) ||
766 CS.paramHasAttr(AttrInd, ParamAttr::ByVal))
767 return false;
768
769 const Type *ArgTy = (*i)->getType();
770 MVT ArgVT;
771 if (!isTypeLegal(ArgTy, TLI, ArgVT))
772 return false;
773 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
774 Flags.setOrigAlign(OriginalAlignment);
775
776 Args.push_back(Arg);
777 ArgVTs.push_back(ArgVT);
778 ArgFlags.push_back(Flags);
779 }
780
781 // Analyze operands of the call, assigning locations to each operand.
782 SmallVector<CCValAssign, 16> ArgLocs;
783 CCState CCInfo(CC, false, TM, ArgLocs);
784 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
785
786 // Get a count of how many bytes are to be pushed on the stack.
787 unsigned NumBytes = CCInfo.getNextStackOffset();
788
789 // Issue CALLSEQ_START
790 BuildMI(MBB, TII.get(X86::ADJCALLSTACKDOWN)).addImm(NumBytes);
791
792 // Process argumenet: walk the register/memloc assignments, inserting
793 // copies / loads.
794 SmallVector<unsigned, 4> RegArgs;
795 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
796 CCValAssign &VA = ArgLocs[i];
797 unsigned Arg = Args[VA.getValNo()];
798 MVT ArgVT = ArgVTs[VA.getValNo()];
799
800 // Promote the value if needed.
801 switch (VA.getLocInfo()) {
802 default: assert(0 && "Unknown loc info!");
803 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000804 case CCValAssign::SExt: {
805 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
806 Arg, ArgVT, Arg);
807 assert(Emitted && "Failed to emit a sext!");
808 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000809 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000810 }
811 case CCValAssign::ZExt: {
812 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
813 Arg, ArgVT, Arg);
814 assert(Emitted && "Failed to emit a zext!");
815 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000816 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000817 }
818 case CCValAssign::AExt: {
819 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
820 Arg, ArgVT, Arg);
821 assert(Emitted && "Failed to emit a aext!");
822 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000823 break;
824 }
Evan Cheng24e3a902008-09-08 06:35:17 +0000825 }
Evan Chengf3d4efe2008-09-07 09:09:33 +0000826
827 if (VA.isRegLoc()) {
828 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
829 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
830 Arg, RC, RC);
831 assert(Emitted && "Failed to emit a copy instruction!");
832 RegArgs.push_back(VA.getLocReg());
833 } else {
834 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +0000835 X86AddressMode AM;
836 AM.Base.Reg = StackPtr;
837 AM.Disp = LocMemOffset;
838 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000839 }
840 }
841
842 // Issue the call.
843 unsigned CallOpc = CalleeOp
844 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
845 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
846 MachineInstrBuilder MIB = CalleeOp
847 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
848 :BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(cast<GlobalValue>(Callee));
849 // Add implicit physical register uses to the call.
850 while (!RegArgs.empty()) {
851 MIB.addReg(RegArgs.back());
852 RegArgs.pop_back();
853 }
854
855 // Issue CALLSEQ_END
856 BuildMI(MBB, TII.get(X86::ADJCALLSTACKUP)).addImm(NumBytes).addImm(0);
857
858 // Now handle call return value (if any).
Evan Chengf3d4efe2008-09-07 09:09:33 +0000859 if (RetVT.getSimpleVT() != MVT::isVoid) {
860 SmallVector<CCValAssign, 16> RVLocs;
861 CCState CCInfo(CC, false, TM, RVLocs);
862 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
863
864 // Copy all of the result registers out of their specified physreg.
865 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
866 MVT CopyVT = RVLocs[0].getValVT();
867 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
868 TargetRegisterClass *SrcRC = DstRC;
869
870 // If this is a call to a function that returns an fp value on the x87 fp
871 // stack, but where we prefer to use the value in xmm registers, copy it
872 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
873 if ((RVLocs[0].getLocReg() == X86::ST0 ||
874 RVLocs[0].getLocReg() == X86::ST1) &&
875 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
876 CopyVT = MVT::f80;
877 SrcRC = X86::RSTRegisterClass;
878 DstRC = X86::RFP80RegisterClass;
879 }
880
881 unsigned ResultReg = createResultReg(DstRC);
882 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
883 RVLocs[0].getLocReg(), DstRC, SrcRC);
884 assert(Emitted && "Failed to emit a copy instruction!");
885 if (CopyVT != RVLocs[0].getValVT()) {
886 // Round the F80 the right size, which also moves to the appropriate xmm
887 // register. This is accomplished by storing the F80 value in memory and
888 // then loading it back. Ewww...
889 MVT ResVT = RVLocs[0].getValVT();
890 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
891 unsigned MemSize = ResVT.getSizeInBits()/8;
Dan Gohman0586d912008-09-10 20:11:02 +0000892 int FI = MFI.CreateStackObject(MemSize, MemSize);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000893 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
894 DstRC = ResVT == MVT::f32
895 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
896 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
897 ResultReg = createResultReg(DstRC);
898 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
899 }
900
Evan Chengdebdea02008-09-08 17:15:42 +0000901 if (AndToI1) {
902 // Mask out all but lowest bit for some call which produces an i1.
903 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
904 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
905 ResultReg = AndResult;
906 }
907
Evan Chengf3d4efe2008-09-07 09:09:33 +0000908 UpdateValueMap(I, ResultReg);
909 }
910
911 return true;
912}
913
914
Dan Gohman99b21822008-08-28 23:21:34 +0000915bool
Dan Gohman3df24e62008-09-03 23:12:08 +0000916X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +0000917 switch (I->getOpcode()) {
918 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +0000919 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +0000920 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +0000921 case Instruction::Store:
922 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000923 case Instruction::ICmp:
924 case Instruction::FCmp:
925 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000926 case Instruction::ZExt:
927 return X86SelectZExt(I);
928 case Instruction::Br:
929 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000930 case Instruction::Call:
931 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000932 case Instruction::LShr:
933 case Instruction::AShr:
934 case Instruction::Shl:
935 return X86SelectShift(I);
936 case Instruction::Select:
937 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000938 case Instruction::Trunc:
939 return X86SelectTrunc(I);
Dan Gohman99b21822008-08-28 23:21:34 +0000940 }
941
942 return false;
943}
944
Dan Gohman0586d912008-09-10 20:11:02 +0000945unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Owen Anderson95267a12008-09-05 00:06:23 +0000946 // Can't handle PIC-mode yet.
947 if (TM.getRelocationModel() == Reloc::PIC_)
948 return 0;
949
Evan Cheng59fbc802008-09-09 01:26:59 +0000950 MVT VT;
951 if (!isTypeLegal(C->getType(), TLI, VT))
Owen Anderson95267a12008-09-05 00:06:23 +0000952 return false;
953
954 // Get opcode and regclass of the output for the given load instruction.
955 unsigned Opc = 0;
956 const TargetRegisterClass *RC = NULL;
957 switch (VT.getSimpleVT()) {
958 default: return false;
959 case MVT::i8:
960 Opc = X86::MOV8rm;
961 RC = X86::GR8RegisterClass;
962 break;
963 case MVT::i16:
964 Opc = X86::MOV16rm;
965 RC = X86::GR16RegisterClass;
966 break;
967 case MVT::i32:
968 Opc = X86::MOV32rm;
969 RC = X86::GR32RegisterClass;
970 break;
971 case MVT::i64:
972 // Must be in x86-64 mode.
973 Opc = X86::MOV64rm;
974 RC = X86::GR64RegisterClass;
975 break;
976 case MVT::f32:
977 if (Subtarget->hasSSE1()) {
978 Opc = X86::MOVSSrm;
979 RC = X86::FR32RegisterClass;
980 } else {
981 Opc = X86::LD_Fp32m;
982 RC = X86::RFP32RegisterClass;
983 }
984 break;
985 case MVT::f64:
986 if (Subtarget->hasSSE2()) {
987 Opc = X86::MOVSDrm;
988 RC = X86::FR64RegisterClass;
989 } else {
990 Opc = X86::LD_Fp64m;
991 RC = X86::RFP64RegisterClass;
992 }
993 break;
994 case MVT::f80:
995 Opc = X86::LD_Fp80m;
996 RC = X86::RFP80RegisterClass;
997 break;
998 }
999
1000 unsigned ResultReg = createResultReg(RC);
1001 if (isa<GlobalValue>(C)) {
Evan Cheng59fbc802008-09-09 01:26:59 +00001002 if (X86SelectConstAddr(C, ResultReg, false, true))
Owen Anderson95267a12008-09-05 00:06:23 +00001003 return ResultReg;
Evan Cheng0de588f2008-09-05 21:00:03 +00001004 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001005 }
1006
Owen Anderson3b217c62008-09-06 01:11:01 +00001007 // MachineConstantPool wants an explicit alignment.
1008 unsigned Align =
1009 TM.getTargetData()->getPreferredTypeAlignmentShift(C->getType());
1010 if (Align == 0) {
1011 // Alignment of vector types. FIXME!
1012 Align = TM.getTargetData()->getABITypeSize(C->getType());
1013 Align = Log2_64(Align);
1014 }
Owen Anderson95267a12008-09-05 00:06:23 +00001015
Dan Gohman0586d912008-09-10 20:11:02 +00001016 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Owen Anderson95267a12008-09-05 00:06:23 +00001017 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset);
Owen Anderson95267a12008-09-05 00:06:23 +00001018 return ResultReg;
1019}
1020
Dan Gohman0586d912008-09-10 20:11:02 +00001021unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
1022 X86AddressMode AM;
1023 if (!X86SelectAddress(C, AM))
1024 return 0;
1025 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1026 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1027 unsigned ResultReg = createResultReg(RC);
1028 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1029 return ResultReg;
1030}
1031
Evan Chengc3f44b02008-09-03 00:03:49 +00001032namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001033 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
1034 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001035 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
1036 DenseMap<const AllocaInst *, int> &am) {
1037 return new X86FastISel(mf, vm, bm, am);
Evan Chengc3f44b02008-09-03 00:03:49 +00001038 }
Dan Gohman99b21822008-08-28 23:21:34 +00001039}