blob: 0e3ce1f28c379a5247f34fb3a102ba1a8ae8cb12 [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 {
Evan Chengf3d4efe2008-09-07 09:09:33 +000034 /// MFI - Keep track of objects allocated on the stack.
35 ///
36 MachineFrameInfo *MFI;
37
Evan Chengc3f44b02008-09-03 00:03:49 +000038 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
39 /// make the right decision when generating code for different targets.
40 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000041
42 /// StackPtr - Register used as the stack pointer.
43 ///
44 unsigned StackPtr;
45
46 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
47 /// floating point ops.
48 /// When SSE is available, use it for f32 operations.
49 /// When SSE2 is available, use it for f64 operations.
50 bool X86ScalarSSEf64;
51 bool X86ScalarSSEf32;
52
Evan Cheng8b19e562008-09-03 06:44:39 +000053public:
Dan Gohman3df24e62008-09-03 23:12:08 +000054 explicit X86FastISel(MachineFunction &mf,
55 DenseMap<const Value *, unsigned> &vm,
56 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm)
Evan Chengf3d4efe2008-09-07 09:09:33 +000057 : FastISel(mf, vm, bm), MFI(MF.getFrameInfo()) {
Evan Cheng88e30412008-09-03 01:04:47 +000058 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000059 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
60 X86ScalarSSEf64 = Subtarget->hasSSE2();
61 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000062 }
Evan Chengc3f44b02008-09-03 00:03:49 +000063
Dan Gohman3df24e62008-09-03 23:12:08 +000064 virtual bool TargetSelectInstruction(Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000065
Dan Gohman1adf1b02008-08-19 21:45:35 +000066#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000067
68private:
Evan Cheng0de588f2008-09-05 21:00:03 +000069 bool X86FastEmitLoad(MVT VT, unsigned Op0, Value *V, unsigned &RR);
70
Evan Chengf3d4efe2008-09-07 09:09:33 +000071 bool X86FastEmitStore(MVT VT, unsigned Val,
72 unsigned Ptr, unsigned Offset, Value *V);
Evan Cheng24e3a902008-09-08 06:35:17 +000073
74 bool X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT, unsigned Src, MVT SrcVT,
75 unsigned &ResultReg);
Evan Cheng0de588f2008-09-05 21:00:03 +000076
Evan Chengf3d4efe2008-09-07 09:09:33 +000077 bool X86SelectConstAddr(Value *V, unsigned &Op0, bool isCall = false);
Evan Cheng8b19e562008-09-03 06:44:39 +000078
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
Owen Anderson9c7216f2008-09-05 20:49:33 +000099 unsigned TargetMaterializeConstant(Constant *C, MachineConstantPool* MCP);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000100
101 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
102 /// computed in an SSE register, not on the X87 floating point stack.
103 bool isScalarFPTypeInSSEReg(MVT VT) const {
104 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
105 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
106 }
107
Evan Chengc3f44b02008-09-03 00:03:49 +0000108};
Dan Gohman99b21822008-08-28 23:21:34 +0000109
Evan Chengf3d4efe2008-09-07 09:09:33 +0000110static bool isTypeLegal(const Type *Ty, const TargetLowering &TLI, MVT &VT) {
111 VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
112 if (VT == MVT::Other || !VT.isSimple())
113 // Unhandled type. Halt "fast" selection and bail.
114 return false;
115 if (VT == MVT::iPTR)
116 // Use pointer type.
117 VT = TLI.getPointerTy();
118 // We only handle legal types. For example, on x86-32 the instruction
119 // selector contains all of the 64-bit instructions from x86-64,
120 // under the assumption that i64 won't be used if the target doesn't
121 // support it.
122 return TLI.isTypeLegal(VT);
123}
124
125#include "X86GenCallingConv.inc"
126
127/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
128/// convention.
129CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) {
130 if (Subtarget->is64Bit()) {
131 if (Subtarget->isTargetWin64())
132 return CC_X86_Win64_C;
133 else if (CC == CallingConv::Fast && isTaillCall)
134 return CC_X86_64_TailCall;
135 else
136 return CC_X86_64_C;
137 }
138
139 if (CC == CallingConv::X86_FastCall)
140 return CC_X86_32_FastCall;
141 else if (CC == CallingConv::Fast && isTaillCall)
142 return CC_X86_32_TailCall;
143 else if (CC == CallingConv::Fast)
144 return CC_X86_32_FastCC;
145 else
146 return CC_X86_32_C;
147}
148
Evan Cheng0de588f2008-09-05 21:00:03 +0000149/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000150/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000151/// Return true and the result register by reference if it is possible.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000152bool X86FastISel::X86FastEmitLoad(MVT VT, unsigned Ptr, Value *GV,
Evan Cheng0de588f2008-09-05 21:00:03 +0000153 unsigned &ResultReg) {
154 // Get opcode and regclass of the output for the given load instruction.
155 unsigned Opc = 0;
156 const TargetRegisterClass *RC = NULL;
157 switch (VT.getSimpleVT()) {
158 default: return false;
159 case MVT::i8:
160 Opc = X86::MOV8rm;
161 RC = X86::GR8RegisterClass;
162 break;
163 case MVT::i16:
164 Opc = X86::MOV16rm;
165 RC = X86::GR16RegisterClass;
166 break;
167 case MVT::i32:
168 Opc = X86::MOV32rm;
169 RC = X86::GR32RegisterClass;
170 break;
171 case MVT::i64:
172 // Must be in x86-64 mode.
173 Opc = X86::MOV64rm;
174 RC = X86::GR64RegisterClass;
175 break;
176 case MVT::f32:
177 if (Subtarget->hasSSE1()) {
178 Opc = X86::MOVSSrm;
179 RC = X86::FR32RegisterClass;
180 } else {
181 Opc = X86::LD_Fp32m;
182 RC = X86::RFP32RegisterClass;
183 }
184 break;
185 case MVT::f64:
186 if (Subtarget->hasSSE2()) {
187 Opc = X86::MOVSDrm;
188 RC = X86::FR64RegisterClass;
189 } else {
190 Opc = X86::LD_Fp64m;
191 RC = X86::RFP64RegisterClass;
192 }
193 break;
194 case MVT::f80:
195 Opc = X86::LD_Fp80m;
196 RC = X86::RFP80RegisterClass;
197 break;
198 }
199
200 ResultReg = createResultReg(RC);
201 X86AddressMode AM;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000202 if (Ptr)
Evan Cheng0de588f2008-09-05 21:00:03 +0000203 // Address is in register.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000204 AM.Base.Reg = Ptr;
Evan Cheng0de588f2008-09-05 21:00:03 +0000205 else
Evan Chengf3d4efe2008-09-07 09:09:33 +0000206 AM.GV = cast<GlobalValue>(GV);
Evan Cheng0de588f2008-09-05 21:00:03 +0000207 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
208 return true;
209}
210
Evan Chengf3d4efe2008-09-07 09:09:33 +0000211/// X86FastEmitStore - Emit a machine instruction to store a value Val of
212/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
213/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000214/// i.e. V. Return true if it is possible.
215bool
Evan Chengf3d4efe2008-09-07 09:09:33 +0000216X86FastISel::X86FastEmitStore(MVT VT, unsigned Val,
217 unsigned Ptr, unsigned Offset, Value *V) {
Dan Gohman863890e2008-09-08 16:31:35 +0000218 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000219 unsigned Opc = 0;
220 const TargetRegisterClass *RC = NULL;
221 switch (VT.getSimpleVT()) {
222 default: return false;
223 case MVT::i8:
224 Opc = X86::MOV8mr;
225 RC = X86::GR8RegisterClass;
226 break;
227 case MVT::i16:
228 Opc = X86::MOV16mr;
229 RC = X86::GR16RegisterClass;
230 break;
231 case MVT::i32:
232 Opc = X86::MOV32mr;
233 RC = X86::GR32RegisterClass;
234 break;
235 case MVT::i64:
236 // Must be in x86-64 mode.
237 Opc = X86::MOV64mr;
238 RC = X86::GR64RegisterClass;
239 break;
240 case MVT::f32:
241 if (Subtarget->hasSSE1()) {
242 Opc = X86::MOVSSmr;
243 RC = X86::FR32RegisterClass;
244 } else {
245 Opc = X86::ST_Fp32m;
246 RC = X86::RFP32RegisterClass;
247 }
248 break;
249 case MVT::f64:
250 if (Subtarget->hasSSE2()) {
251 Opc = X86::MOVSDmr;
252 RC = X86::FR64RegisterClass;
253 } else {
254 Opc = X86::ST_Fp64m;
255 RC = X86::RFP64RegisterClass;
256 }
257 break;
258 case MVT::f80:
259 Opc = X86::ST_FP80m;
260 RC = X86::RFP80RegisterClass;
261 break;
262 }
263
264 X86AddressMode AM;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000265 if (Ptr) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000266 // Address is in register.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000267 AM.Base.Reg = Ptr;
268 AM.Disp = Offset;
269 } else
Evan Cheng0de588f2008-09-05 21:00:03 +0000270 AM.GV = cast<GlobalValue>(V);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000271 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000272 return true;
273}
274
Evan Cheng24e3a902008-09-08 06:35:17 +0000275/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
276/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
277/// ISD::SIGN_EXTEND).
278bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT,
279 unsigned Src, MVT SrcVT,
280 unsigned &ResultReg) {
281 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
282 return ResultReg != 0;
283}
284
Evan Cheng8b19e562008-09-03 06:44:39 +0000285/// X86SelectConstAddr - Select and emit code to materialize constant address.
286///
Evan Chengf3d4efe2008-09-07 09:09:33 +0000287bool X86FastISel::X86SelectConstAddr(Value *V, unsigned &Op0, bool isCall) {
Evan Cheng8b19e562008-09-03 06:44:39 +0000288 // FIXME: Only GlobalAddress for now.
289 GlobalValue *GV = dyn_cast<GlobalValue>(V);
290 if (!GV)
291 return false;
292
Evan Chengf3d4efe2008-09-07 09:09:33 +0000293 if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) {
Evan Cheng8b19e562008-09-03 06:44:39 +0000294 // Issue load from stub if necessary.
295 unsigned Opc = 0;
296 const TargetRegisterClass *RC = NULL;
297 if (TLI.getPointerTy() == MVT::i32) {
298 Opc = X86::MOV32rm;
299 RC = X86::GR32RegisterClass;
300 } else {
301 Opc = X86::MOV64rm;
302 RC = X86::GR64RegisterClass;
303 }
304 Op0 = createResultReg(RC);
305 X86AddressMode AM;
306 AM.GV = GV;
307 addFullAddress(BuildMI(MBB, TII.get(Opc), Op0), AM);
Evan Cheng373d50a2008-09-04 06:18:33 +0000308 // Prevent loading GV stub multiple times in same MBB.
309 LocalValueMap[V] = Op0;
Evan Cheng8b19e562008-09-03 06:44:39 +0000310 }
311 return true;
312}
313
Owen Andersona3971df2008-09-04 07:08:58 +0000314/// X86SelectStore - Select and emit code to implement store instructions.
315bool X86FastISel::X86SelectStore(Instruction* I) {
Evan Cheng24e3a902008-09-08 06:35:17 +0000316 MVT VT;
317 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
Owen Andersona3971df2008-09-04 07:08:58 +0000318 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000319 unsigned Val = getRegForValue(I->getOperand(0));
320 if (Val == 0)
Owen Andersona3971df2008-09-04 07:08:58 +0000321 // Unhandled operand. Halt "fast" selection and bail.
322 return false;
323
324 Value *V = I->getOperand(1);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000325 unsigned Ptr = getRegForValue(V);
326 if (Ptr == 0) {
Dan Gohman863890e2008-09-08 16:31:35 +0000327 // Handle constant store address.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000328 if (!isa<Constant>(V) || !X86SelectConstAddr(V, Ptr))
Owen Andersona3971df2008-09-04 07:08:58 +0000329 // Unhandled operand. Halt "fast" selection and bail.
330 return false;
331 }
Owen Andersona3971df2008-09-04 07:08:58 +0000332
Evan Chengf3d4efe2008-09-07 09:09:33 +0000333 return X86FastEmitStore(VT, Val, Ptr, 0, V);
Owen Andersona3971df2008-09-04 07:08:58 +0000334}
335
Evan Cheng8b19e562008-09-03 06:44:39 +0000336/// X86SelectLoad - Select and emit code to implement load instructions.
337///
Dan Gohman3df24e62008-09-03 23:12:08 +0000338bool X86FastISel::X86SelectLoad(Instruction *I) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000339 MVT VT;
340 if (!isTypeLegal(I->getType(), TLI, VT))
Evan Cheng8b19e562008-09-03 06:44:39 +0000341 return false;
342
343 Value *V = I->getOperand(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000344 unsigned Ptr = getRegForValue(V);
345 if (Ptr == 0) {
Evan Cheng8b19e562008-09-03 06:44:39 +0000346 // Handle constant load address.
Evan Cheng0de588f2008-09-05 21:00:03 +0000347 // FIXME: If load type is something we can't handle, this can result in
348 // a dead stub load instruction.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000349 if (!isa<Constant>(V) || !X86SelectConstAddr(V, Ptr))
Evan Cheng8b19e562008-09-03 06:44:39 +0000350 // Unhandled operand. Halt "fast" selection and bail.
351 return false;
352 }
353
Evan Cheng0de588f2008-09-05 21:00:03 +0000354 unsigned ResultReg = 0;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000355 if (X86FastEmitLoad(VT, Ptr, V, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000356 UpdateValueMap(I, ResultReg);
357 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000358 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000359 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000360}
361
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000362bool X86FastISel::X86SelectCmp(Instruction *I) {
363 CmpInst *CI = cast<CmpInst>(I);
364
Dan Gohman4f22bb02008-09-05 01:33:56 +0000365 MVT VT = TLI.getValueType(I->getOperand(0)->getType());
366 if (!TLI.isTypeLegal(VT))
367 return false;
368
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000369 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000370 if (Op0Reg == 0) return false;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000371 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000372 if (Op1Reg == 0) return false;
373
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000374 unsigned Opc;
Dan Gohmanf52550b2008-09-05 01:15:35 +0000375 switch (VT.getSimpleVT()) {
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000376 case MVT::i8: Opc = X86::CMP8rr; break;
377 case MVT::i16: Opc = X86::CMP16rr; break;
378 case MVT::i32: Opc = X86::CMP32rr; break;
379 case MVT::i64: Opc = X86::CMP64rr; break;
380 case MVT::f32: Opc = X86::UCOMISSrr; break;
381 case MVT::f64: Opc = X86::UCOMISDrr; break;
382 default: return false;
383 }
384
385 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
386 switch (CI->getPredicate()) {
387 case CmpInst::FCMP_OEQ: {
388 unsigned EReg = createResultReg(&X86::GR8RegClass);
389 unsigned NPReg = createResultReg(&X86::GR8RegClass);
390 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
391 BuildMI(MBB, TII.get(X86::SETEr), EReg);
392 BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
393 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
394 break;
395 }
396 case CmpInst::FCMP_UNE: {
397 unsigned NEReg = createResultReg(&X86::GR8RegClass);
398 unsigned PReg = createResultReg(&X86::GR8RegClass);
399 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
400 BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
401 BuildMI(MBB, TII.get(X86::SETPr), PReg);
402 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
403 break;
404 }
405 case CmpInst::FCMP_OGT:
406 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
407 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
408 break;
409 case CmpInst::FCMP_OGE:
410 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
411 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
412 break;
413 case CmpInst::FCMP_OLT:
414 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
415 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
416 break;
417 case CmpInst::FCMP_OLE:
418 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
419 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
420 break;
421 case CmpInst::FCMP_ONE:
422 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
423 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
424 break;
425 case CmpInst::FCMP_ORD:
426 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
427 BuildMI(MBB, TII.get(X86::SETNPr), ResultReg);
428 break;
429 case CmpInst::FCMP_UNO:
430 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
431 BuildMI(MBB, TII.get(X86::SETPr), ResultReg);
432 break;
433 case CmpInst::FCMP_UEQ:
434 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
435 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
436 break;
437 case CmpInst::FCMP_UGT:
438 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
439 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
440 break;
441 case CmpInst::FCMP_UGE:
442 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
443 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
444 break;
445 case CmpInst::FCMP_ULT:
446 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
447 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
448 break;
449 case CmpInst::FCMP_ULE:
450 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
451 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
452 break;
453 case CmpInst::ICMP_EQ:
454 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
455 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
456 break;
457 case CmpInst::ICMP_NE:
458 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
459 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
460 break;
461 case CmpInst::ICMP_UGT:
462 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
463 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
464 break;
465 case CmpInst::ICMP_UGE:
466 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
467 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
468 break;
469 case CmpInst::ICMP_ULT:
470 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
471 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
472 break;
473 case CmpInst::ICMP_ULE:
474 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
475 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
476 break;
477 case CmpInst::ICMP_SGT:
478 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
479 BuildMI(MBB, TII.get(X86::SETGr), ResultReg);
480 break;
481 case CmpInst::ICMP_SGE:
482 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
483 BuildMI(MBB, TII.get(X86::SETGEr), ResultReg);
484 break;
485 case CmpInst::ICMP_SLT:
486 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
487 BuildMI(MBB, TII.get(X86::SETLr), ResultReg);
488 break;
489 case CmpInst::ICMP_SLE:
490 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
491 BuildMI(MBB, TII.get(X86::SETLEr), ResultReg);
492 break;
493 default:
494 return false;
495 }
496
497 UpdateValueMap(I, ResultReg);
498 return true;
499}
Evan Cheng8b19e562008-09-03 06:44:39 +0000500
Dan Gohmand89ae992008-09-05 01:06:14 +0000501bool X86FastISel::X86SelectZExt(Instruction *I) {
502 // Special-case hack: The only i1 values we know how to produce currently
503 // set the upper bits of an i8 value to zero.
504 if (I->getType() == Type::Int8Ty &&
505 I->getOperand(0)->getType() == Type::Int1Ty) {
506 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000507 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000508 UpdateValueMap(I, ResultReg);
509 return true;
510 }
511
512 return false;
513}
514
515bool X86FastISel::X86SelectBranch(Instruction *I) {
516 BranchInst *BI = cast<BranchInst>(I);
517 // Unconditional branches are selected by tablegen-generated code.
518 unsigned OpReg = getRegForValue(BI->getCondition());
Dan Gohmanf52550b2008-09-05 01:15:35 +0000519 if (OpReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000520 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
521 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
522
523 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
524 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
525 BuildMI(MBB, TII.get(X86::JMP)).addMBB(FalseMBB);
526
527 MBB->addSuccessor(TrueMBB);
528 MBB->addSuccessor(FalseMBB);
529
530 return true;
531}
532
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000533bool X86FastISel::X86SelectShift(Instruction *I) {
534 unsigned CReg = 0;
535 unsigned Opc = 0;
536 const TargetRegisterClass *RC = NULL;
537 if (I->getType() == Type::Int8Ty) {
538 CReg = X86::CL;
539 RC = &X86::GR8RegClass;
540 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000541 case Instruction::LShr: Opc = X86::SHR8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000542 case Instruction::AShr: Opc = X86::SAR8rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000543 case Instruction::Shl: Opc = X86::SHL8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000544 default: return false;
545 }
546 } else if (I->getType() == Type::Int16Ty) {
547 CReg = X86::CX;
548 RC = &X86::GR16RegClass;
549 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000550 case Instruction::LShr: Opc = X86::SHR16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000551 case Instruction::AShr: Opc = X86::SAR16rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000552 case Instruction::Shl: Opc = X86::SHL16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000553 default: return false;
554 }
555 } else if (I->getType() == Type::Int32Ty) {
556 CReg = X86::ECX;
557 RC = &X86::GR32RegClass;
558 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000559 case Instruction::LShr: Opc = X86::SHR32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000560 case Instruction::AShr: Opc = X86::SAR32rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000561 case Instruction::Shl: Opc = X86::SHL32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000562 default: return false;
563 }
564 } else if (I->getType() == Type::Int64Ty) {
565 CReg = X86::RCX;
566 RC = &X86::GR64RegClass;
567 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000568 case Instruction::LShr: Opc = X86::SHR64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000569 case Instruction::AShr: Opc = X86::SAR64rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000570 case Instruction::Shl: Opc = X86::SHL64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000571 default: return false;
572 }
573 } else {
574 return false;
575 }
576
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000577 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
578 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
579 return false;
580
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000581 unsigned Op0Reg = getRegForValue(I->getOperand(0));
582 if (Op0Reg == 0) return false;
583 unsigned Op1Reg = getRegForValue(I->getOperand(1));
584 if (Op1Reg == 0) return false;
585 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
586 unsigned ResultReg = createResultReg(RC);
587 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op0Reg);
588 UpdateValueMap(I, ResultReg);
589 return true;
590}
591
592bool X86FastISel::X86SelectSelect(Instruction *I) {
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000593 const Type *Ty = I->getType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000594 if (isa<PointerType>(Ty))
595 Ty = TLI.getTargetData()->getIntPtrType();
596
597 unsigned Opc = 0;
598 const TargetRegisterClass *RC = NULL;
599 if (Ty == Type::Int16Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000600 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000601 RC = &X86::GR16RegClass;
602 } else if (Ty == Type::Int32Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000603 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000604 RC = &X86::GR32RegClass;
605 } else if (Ty == Type::Int64Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000606 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000607 RC = &X86::GR64RegClass;
608 } else {
609 return false;
610 }
611
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000612 MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
613 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
614 return false;
615
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000616 unsigned Op0Reg = getRegForValue(I->getOperand(0));
617 if (Op0Reg == 0) return false;
618 unsigned Op1Reg = getRegForValue(I->getOperand(1));
619 if (Op1Reg == 0) return false;
620 unsigned Op2Reg = getRegForValue(I->getOperand(2));
621 if (Op2Reg == 0) return false;
622
623 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
624 unsigned ResultReg = createResultReg(RC);
625 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
626 UpdateValueMap(I, ResultReg);
627 return true;
628}
629
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000630bool X86FastISel::X86SelectTrunc(Instruction *I) {
631 if (Subtarget->is64Bit())
632 // All other cases should be handled by the tblgen generated code.
633 return false;
634 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
635 MVT DstVT = TLI.getValueType(I->getType());
636 if (DstVT != MVT::i8)
637 // All other cases should be handled by the tblgen generated code.
638 return false;
639 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
640 // All other cases should be handled by the tblgen generated code.
641 return false;
642
643 unsigned InputReg = getRegForValue(I->getOperand(0));
644 if (!InputReg)
645 // Unhandled operand. Halt "fast" selection and bail.
646 return false;
647
648 // First issue a copy to GR16_ or GR32_.
649 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
650 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
651 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
652 unsigned CopyReg = createResultReg(CopyRC);
653 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
654
655 // Then issue an extract_subreg.
656 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg,1); // x86_subreg_8bit
657 if (!ResultReg)
658 return false;
659
660 UpdateValueMap(I, ResultReg);
661 return true;
662}
663
Evan Chengf3d4efe2008-09-07 09:09:33 +0000664bool X86FastISel::X86SelectCall(Instruction *I) {
665 CallInst *CI = cast<CallInst>(I);
666 Value *Callee = I->getOperand(0);
667
668 // Can't handle inline asm yet.
669 if (isa<InlineAsm>(Callee))
670 return false;
671
672 // FIXME: Handle some intrinsics.
673 if (Function *F = CI->getCalledFunction()) {
674 if (F->isDeclaration() &&F->getIntrinsicID())
675 return false;
676 }
677
678 // Materialize callee address in a register. FIXME: GV address can be
679 // handled with a CALLpcrel32 instead.
680 unsigned CalleeOp = getRegForValue(Callee);
681 if (CalleeOp == 0) {
682 if (!isa<Constant>(Callee) || !X86SelectConstAddr(Callee, CalleeOp, true))
683 // Unhandled operand. Halt "fast" selection and bail.
684 return false;
685 }
686
687 // Handle only C and fastcc calling conventions for now.
688 CallSite CS(CI);
689 unsigned CC = CS.getCallingConv();
690 if (CC != CallingConv::C &&
691 CC != CallingConv::Fast &&
692 CC != CallingConv::X86_FastCall)
693 return false;
694
695 // Let SDISel handle vararg functions.
696 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
697 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
698 if (FTy->isVarArg())
699 return false;
700
701 // Handle *simple* calls for now.
702 const Type *RetTy = CS.getType();
703 MVT RetVT;
704 if (!isTypeLegal(RetTy, TLI, RetVT))
705 return false;
706
707 // Deal with call operands first.
708 SmallVector<unsigned, 4> Args;
709 SmallVector<MVT, 4> ArgVTs;
710 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
711 Args.reserve(CS.arg_size());
712 ArgVTs.reserve(CS.arg_size());
713 ArgFlags.reserve(CS.arg_size());
714 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
715 i != e; ++i) {
716 unsigned Arg = getRegForValue(*i);
717 if (Arg == 0)
718 return false;
719 ISD::ArgFlagsTy Flags;
720 unsigned AttrInd = i - CS.arg_begin() + 1;
721 if (CS.paramHasAttr(AttrInd, ParamAttr::SExt))
722 Flags.setSExt();
723 if (CS.paramHasAttr(AttrInd, ParamAttr::ZExt))
724 Flags.setZExt();
725
726 // FIXME: Only handle *easy* calls for now.
727 if (CS.paramHasAttr(AttrInd, ParamAttr::InReg) ||
728 CS.paramHasAttr(AttrInd, ParamAttr::StructRet) ||
729 CS.paramHasAttr(AttrInd, ParamAttr::Nest) ||
730 CS.paramHasAttr(AttrInd, ParamAttr::ByVal))
731 return false;
732
733 const Type *ArgTy = (*i)->getType();
734 MVT ArgVT;
735 if (!isTypeLegal(ArgTy, TLI, ArgVT))
736 return false;
737 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
738 Flags.setOrigAlign(OriginalAlignment);
739
740 Args.push_back(Arg);
741 ArgVTs.push_back(ArgVT);
742 ArgFlags.push_back(Flags);
743 }
744
745 // Analyze operands of the call, assigning locations to each operand.
746 SmallVector<CCValAssign, 16> ArgLocs;
747 CCState CCInfo(CC, false, TM, ArgLocs);
748 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
749
750 // Get a count of how many bytes are to be pushed on the stack.
751 unsigned NumBytes = CCInfo.getNextStackOffset();
752
753 // Issue CALLSEQ_START
754 BuildMI(MBB, TII.get(X86::ADJCALLSTACKDOWN)).addImm(NumBytes);
755
756 // Process argumenet: walk the register/memloc assignments, inserting
757 // copies / loads.
758 SmallVector<unsigned, 4> RegArgs;
759 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
760 CCValAssign &VA = ArgLocs[i];
761 unsigned Arg = Args[VA.getValNo()];
762 MVT ArgVT = ArgVTs[VA.getValNo()];
763
764 // Promote the value if needed.
765 switch (VA.getLocInfo()) {
766 default: assert(0 && "Unknown loc info!");
767 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000768 case CCValAssign::SExt: {
769 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
770 Arg, ArgVT, Arg);
771 assert(Emitted && "Failed to emit a sext!");
772 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000773 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000774 }
775 case CCValAssign::ZExt: {
776 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
777 Arg, ArgVT, Arg);
778 assert(Emitted && "Failed to emit a zext!");
779 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000780 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000781 }
782 case CCValAssign::AExt: {
783 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
784 Arg, ArgVT, Arg);
785 assert(Emitted && "Failed to emit a aext!");
786 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000787 break;
788 }
Evan Cheng24e3a902008-09-08 06:35:17 +0000789 }
Evan Chengf3d4efe2008-09-07 09:09:33 +0000790
791 if (VA.isRegLoc()) {
792 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
793 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
794 Arg, RC, RC);
795 assert(Emitted && "Failed to emit a copy instruction!");
796 RegArgs.push_back(VA.getLocReg());
797 } else {
798 unsigned LocMemOffset = VA.getLocMemOffset();
799 X86FastEmitStore(ArgVT, Arg, StackPtr, LocMemOffset, NULL);
800 }
801 }
802
803 // Issue the call.
804 unsigned CallOpc = CalleeOp
805 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
806 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
807 MachineInstrBuilder MIB = CalleeOp
808 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
809 :BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(cast<GlobalValue>(Callee));
810 // Add implicit physical register uses to the call.
811 while (!RegArgs.empty()) {
812 MIB.addReg(RegArgs.back());
813 RegArgs.pop_back();
814 }
815
816 // Issue CALLSEQ_END
817 BuildMI(MBB, TII.get(X86::ADJCALLSTACKUP)).addImm(NumBytes).addImm(0);
818
819 // Now handle call return value (if any).
Evan Chengf3d4efe2008-09-07 09:09:33 +0000820 if (RetVT.getSimpleVT() != MVT::isVoid) {
821 SmallVector<CCValAssign, 16> RVLocs;
822 CCState CCInfo(CC, false, TM, RVLocs);
823 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
824
825 // Copy all of the result registers out of their specified physreg.
826 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
827 MVT CopyVT = RVLocs[0].getValVT();
828 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
829 TargetRegisterClass *SrcRC = DstRC;
830
831 // If this is a call to a function that returns an fp value on the x87 fp
832 // stack, but where we prefer to use the value in xmm registers, copy it
833 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
834 if ((RVLocs[0].getLocReg() == X86::ST0 ||
835 RVLocs[0].getLocReg() == X86::ST1) &&
836 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
837 CopyVT = MVT::f80;
838 SrcRC = X86::RSTRegisterClass;
839 DstRC = X86::RFP80RegisterClass;
840 }
841
842 unsigned ResultReg = createResultReg(DstRC);
843 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
844 RVLocs[0].getLocReg(), DstRC, SrcRC);
845 assert(Emitted && "Failed to emit a copy instruction!");
846 if (CopyVT != RVLocs[0].getValVT()) {
847 // Round the F80 the right size, which also moves to the appropriate xmm
848 // register. This is accomplished by storing the F80 value in memory and
849 // then loading it back. Ewww...
850 MVT ResVT = RVLocs[0].getValVT();
851 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
852 unsigned MemSize = ResVT.getSizeInBits()/8;
853 int FI = MFI->CreateStackObject(MemSize, MemSize);
854 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
855 DstRC = ResVT == MVT::f32
856 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
857 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
858 ResultReg = createResultReg(DstRC);
859 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
860 }
861
862 UpdateValueMap(I, ResultReg);
863 }
864
865 return true;
866}
867
868
Dan Gohman99b21822008-08-28 23:21:34 +0000869bool
Dan Gohman3df24e62008-09-03 23:12:08 +0000870X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +0000871 switch (I->getOpcode()) {
872 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +0000873 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +0000874 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +0000875 case Instruction::Store:
876 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000877 case Instruction::ICmp:
878 case Instruction::FCmp:
879 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000880 case Instruction::ZExt:
881 return X86SelectZExt(I);
882 case Instruction::Br:
883 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000884 case Instruction::Call:
885 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000886 case Instruction::LShr:
887 case Instruction::AShr:
888 case Instruction::Shl:
889 return X86SelectShift(I);
890 case Instruction::Select:
891 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000892 case Instruction::Trunc:
893 return X86SelectTrunc(I);
Dan Gohman99b21822008-08-28 23:21:34 +0000894 }
895
896 return false;
897}
898
Owen Anderson9c7216f2008-09-05 20:49:33 +0000899unsigned X86FastISel::TargetMaterializeConstant(Constant *C,
900 MachineConstantPool* MCP) {
Owen Anderson95267a12008-09-05 00:06:23 +0000901 // Can't handle PIC-mode yet.
902 if (TM.getRelocationModel() == Reloc::PIC_)
903 return 0;
904
905 MVT VT = MVT::getMVT(C->getType(), /*HandleUnknown=*/true);
906 if (VT == MVT::Other || !VT.isSimple())
907 // Unhandled type. Halt "fast" selection and bail.
908 return false;
909 if (VT == MVT::iPTR)
910 // Use pointer type.
911 VT = TLI.getPointerTy();
912 // We only handle legal types. For example, on x86-32 the instruction
913 // selector contains all of the 64-bit instructions from x86-64,
914 // under the assumption that i64 won't be used if the target doesn't
915 // support it.
916 if (!TLI.isTypeLegal(VT))
917 return false;
918
919 // Get opcode and regclass of the output for the given load instruction.
920 unsigned Opc = 0;
921 const TargetRegisterClass *RC = NULL;
922 switch (VT.getSimpleVT()) {
923 default: return false;
924 case MVT::i8:
925 Opc = X86::MOV8rm;
926 RC = X86::GR8RegisterClass;
927 break;
928 case MVT::i16:
929 Opc = X86::MOV16rm;
930 RC = X86::GR16RegisterClass;
931 break;
932 case MVT::i32:
933 Opc = X86::MOV32rm;
934 RC = X86::GR32RegisterClass;
935 break;
936 case MVT::i64:
937 // Must be in x86-64 mode.
938 Opc = X86::MOV64rm;
939 RC = X86::GR64RegisterClass;
940 break;
941 case MVT::f32:
942 if (Subtarget->hasSSE1()) {
943 Opc = X86::MOVSSrm;
944 RC = X86::FR32RegisterClass;
945 } else {
946 Opc = X86::LD_Fp32m;
947 RC = X86::RFP32RegisterClass;
948 }
949 break;
950 case MVT::f64:
951 if (Subtarget->hasSSE2()) {
952 Opc = X86::MOVSDrm;
953 RC = X86::FR64RegisterClass;
954 } else {
955 Opc = X86::LD_Fp64m;
956 RC = X86::RFP64RegisterClass;
957 }
958 break;
959 case MVT::f80:
960 Opc = X86::LD_Fp80m;
961 RC = X86::RFP80RegisterClass;
962 break;
963 }
964
965 unsigned ResultReg = createResultReg(RC);
966 if (isa<GlobalValue>(C)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000967 // FIXME: If store value type is something we can't handle, this can result
968 // in a dead stub load instruction.
Owen Anderson95267a12008-09-05 00:06:23 +0000969 if (X86SelectConstAddr(C, ResultReg))
970 return ResultReg;
Evan Cheng0de588f2008-09-05 21:00:03 +0000971 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +0000972 }
973
Owen Anderson3b217c62008-09-06 01:11:01 +0000974 // MachineConstantPool wants an explicit alignment.
975 unsigned Align =
976 TM.getTargetData()->getPreferredTypeAlignmentShift(C->getType());
977 if (Align == 0) {
978 // Alignment of vector types. FIXME!
979 Align = TM.getTargetData()->getABITypeSize(C->getType());
980 Align = Log2_64(Align);
981 }
Owen Anderson95267a12008-09-05 00:06:23 +0000982
Owen Anderson3b217c62008-09-06 01:11:01 +0000983 unsigned MCPOffset = MCP->getConstantPoolIndex(C, Align);
Owen Anderson95267a12008-09-05 00:06:23 +0000984 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset);
Owen Anderson95267a12008-09-05 00:06:23 +0000985 return ResultReg;
986}
987
Evan Chengc3f44b02008-09-03 00:03:49 +0000988namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +0000989 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
990 DenseMap<const Value *, unsigned> &vm,
991 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm) {
992 return new X86FastISel(mf, vm, bm);
Evan Chengc3f44b02008-09-03 00:03:49 +0000993 }
Dan Gohman99b21822008-08-28 23:21:34 +0000994}