blob: 4584bde7adc86a763f590532bd45dad92293c371 [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 Chengdebdea02008-09-08 17:15:42 +0000110static bool isTypeLegal(const Type *Ty, const TargetLowering &TLI, MVT &VT,
111 bool AllowI1 = false) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000112 VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
113 if (VT == MVT::Other || !VT.isSimple())
114 // Unhandled type. Halt "fast" selection and bail.
115 return false;
116 if (VT == MVT::iPTR)
117 // Use pointer type.
118 VT = TLI.getPointerTy();
119 // We only handle legal types. For example, on x86-32 the instruction
120 // selector contains all of the 64-bit instructions from x86-64,
121 // under the assumption that i64 won't be used if the target doesn't
122 // support it.
Evan Chengdebdea02008-09-08 17:15:42 +0000123 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000124}
125
126#include "X86GenCallingConv.inc"
127
128/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
129/// convention.
130CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) {
131 if (Subtarget->is64Bit()) {
132 if (Subtarget->isTargetWin64())
133 return CC_X86_Win64_C;
134 else if (CC == CallingConv::Fast && isTaillCall)
135 return CC_X86_64_TailCall;
136 else
137 return CC_X86_64_C;
138 }
139
140 if (CC == CallingConv::X86_FastCall)
141 return CC_X86_32_FastCall;
142 else if (CC == CallingConv::Fast && isTaillCall)
143 return CC_X86_32_TailCall;
144 else if (CC == CallingConv::Fast)
145 return CC_X86_32_FastCC;
146 else
147 return CC_X86_32_C;
148}
149
Evan Cheng0de588f2008-09-05 21:00:03 +0000150/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000151/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000152/// Return true and the result register by reference if it is possible.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000153bool X86FastISel::X86FastEmitLoad(MVT VT, unsigned Ptr, Value *GV,
Evan Cheng0de588f2008-09-05 21:00:03 +0000154 unsigned &ResultReg) {
155 // Get opcode and regclass of the output for the given load instruction.
156 unsigned Opc = 0;
157 const TargetRegisterClass *RC = NULL;
158 switch (VT.getSimpleVT()) {
159 default: return false;
160 case MVT::i8:
161 Opc = X86::MOV8rm;
162 RC = X86::GR8RegisterClass;
163 break;
164 case MVT::i16:
165 Opc = X86::MOV16rm;
166 RC = X86::GR16RegisterClass;
167 break;
168 case MVT::i32:
169 Opc = X86::MOV32rm;
170 RC = X86::GR32RegisterClass;
171 break;
172 case MVT::i64:
173 // Must be in x86-64 mode.
174 Opc = X86::MOV64rm;
175 RC = X86::GR64RegisterClass;
176 break;
177 case MVT::f32:
178 if (Subtarget->hasSSE1()) {
179 Opc = X86::MOVSSrm;
180 RC = X86::FR32RegisterClass;
181 } else {
182 Opc = X86::LD_Fp32m;
183 RC = X86::RFP32RegisterClass;
184 }
185 break;
186 case MVT::f64:
187 if (Subtarget->hasSSE2()) {
188 Opc = X86::MOVSDrm;
189 RC = X86::FR64RegisterClass;
190 } else {
191 Opc = X86::LD_Fp64m;
192 RC = X86::RFP64RegisterClass;
193 }
194 break;
195 case MVT::f80:
196 Opc = X86::LD_Fp80m;
197 RC = X86::RFP80RegisterClass;
198 break;
199 }
200
201 ResultReg = createResultReg(RC);
202 X86AddressMode AM;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000203 if (Ptr)
Evan Cheng0de588f2008-09-05 21:00:03 +0000204 // Address is in register.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000205 AM.Base.Reg = Ptr;
Evan Cheng0de588f2008-09-05 21:00:03 +0000206 else
Evan Chengf3d4efe2008-09-07 09:09:33 +0000207 AM.GV = cast<GlobalValue>(GV);
Evan Cheng0de588f2008-09-05 21:00:03 +0000208 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
209 return true;
210}
211
Evan Chengf3d4efe2008-09-07 09:09:33 +0000212/// X86FastEmitStore - Emit a machine instruction to store a value Val of
213/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
214/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000215/// i.e. V. Return true if it is possible.
216bool
Evan Chengf3d4efe2008-09-07 09:09:33 +0000217X86FastISel::X86FastEmitStore(MVT VT, unsigned Val,
218 unsigned Ptr, unsigned Offset, Value *V) {
Dan Gohman863890e2008-09-08 16:31:35 +0000219 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000220 unsigned Opc = 0;
221 const TargetRegisterClass *RC = NULL;
222 switch (VT.getSimpleVT()) {
223 default: return false;
224 case MVT::i8:
225 Opc = X86::MOV8mr;
226 RC = X86::GR8RegisterClass;
227 break;
228 case MVT::i16:
229 Opc = X86::MOV16mr;
230 RC = X86::GR16RegisterClass;
231 break;
232 case MVT::i32:
233 Opc = X86::MOV32mr;
234 RC = X86::GR32RegisterClass;
235 break;
236 case MVT::i64:
237 // Must be in x86-64 mode.
238 Opc = X86::MOV64mr;
239 RC = X86::GR64RegisterClass;
240 break;
241 case MVT::f32:
242 if (Subtarget->hasSSE1()) {
243 Opc = X86::MOVSSmr;
244 RC = X86::FR32RegisterClass;
245 } else {
246 Opc = X86::ST_Fp32m;
247 RC = X86::RFP32RegisterClass;
248 }
249 break;
250 case MVT::f64:
251 if (Subtarget->hasSSE2()) {
252 Opc = X86::MOVSDmr;
253 RC = X86::FR64RegisterClass;
254 } else {
255 Opc = X86::ST_Fp64m;
256 RC = X86::RFP64RegisterClass;
257 }
258 break;
259 case MVT::f80:
260 Opc = X86::ST_FP80m;
261 RC = X86::RFP80RegisterClass;
262 break;
263 }
264
265 X86AddressMode AM;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000266 if (Ptr) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000267 // Address is in register.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000268 AM.Base.Reg = Ptr;
269 AM.Disp = Offset;
270 } else
Evan Cheng0de588f2008-09-05 21:00:03 +0000271 AM.GV = cast<GlobalValue>(V);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000272 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000273 return true;
274}
275
Evan Cheng24e3a902008-09-08 06:35:17 +0000276/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
277/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
278/// ISD::SIGN_EXTEND).
279bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT,
280 unsigned Src, MVT SrcVT,
281 unsigned &ResultReg) {
282 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
283 return ResultReg != 0;
284}
285
Evan Cheng8b19e562008-09-03 06:44:39 +0000286/// X86SelectConstAddr - Select and emit code to materialize constant address.
287///
Evan Chengf3d4efe2008-09-07 09:09:33 +0000288bool X86FastISel::X86SelectConstAddr(Value *V, unsigned &Op0, bool isCall) {
Evan Cheng8b19e562008-09-03 06:44:39 +0000289 // FIXME: Only GlobalAddress for now.
290 GlobalValue *GV = dyn_cast<GlobalValue>(V);
291 if (!GV)
292 return false;
293
Evan Chengf3d4efe2008-09-07 09:09:33 +0000294 if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) {
Evan Cheng8b19e562008-09-03 06:44:39 +0000295 // Issue load from stub if necessary.
296 unsigned Opc = 0;
297 const TargetRegisterClass *RC = NULL;
298 if (TLI.getPointerTy() == MVT::i32) {
299 Opc = X86::MOV32rm;
300 RC = X86::GR32RegisterClass;
301 } else {
302 Opc = X86::MOV64rm;
303 RC = X86::GR64RegisterClass;
304 }
305 Op0 = createResultReg(RC);
306 X86AddressMode AM;
307 AM.GV = GV;
308 addFullAddress(BuildMI(MBB, TII.get(Opc), Op0), AM);
Evan Cheng373d50a2008-09-04 06:18:33 +0000309 // Prevent loading GV stub multiple times in same MBB.
310 LocalValueMap[V] = Op0;
Evan Cheng8b19e562008-09-03 06:44:39 +0000311 }
312 return true;
313}
314
Owen Andersona3971df2008-09-04 07:08:58 +0000315/// X86SelectStore - Select and emit code to implement store instructions.
316bool X86FastISel::X86SelectStore(Instruction* I) {
Evan Cheng24e3a902008-09-08 06:35:17 +0000317 MVT VT;
318 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
Owen Andersona3971df2008-09-04 07:08:58 +0000319 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000320 unsigned Val = getRegForValue(I->getOperand(0));
321 if (Val == 0)
Owen Andersona3971df2008-09-04 07:08:58 +0000322 // Unhandled operand. Halt "fast" selection and bail.
323 return false;
324
325 Value *V = I->getOperand(1);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000326 unsigned Ptr = getRegForValue(V);
327 if (Ptr == 0) {
Dan Gohman863890e2008-09-08 16:31:35 +0000328 // Handle constant store address.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000329 if (!isa<Constant>(V) || !X86SelectConstAddr(V, Ptr))
Owen Andersona3971df2008-09-04 07:08:58 +0000330 // Unhandled operand. Halt "fast" selection and bail.
331 return false;
332 }
Owen Andersona3971df2008-09-04 07:08:58 +0000333
Evan Chengf3d4efe2008-09-07 09:09:33 +0000334 return X86FastEmitStore(VT, Val, Ptr, 0, V);
Owen Andersona3971df2008-09-04 07:08:58 +0000335}
336
Evan Cheng8b19e562008-09-03 06:44:39 +0000337/// X86SelectLoad - Select and emit code to implement load instructions.
338///
Dan Gohman3df24e62008-09-03 23:12:08 +0000339bool X86FastISel::X86SelectLoad(Instruction *I) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000340 MVT VT;
341 if (!isTypeLegal(I->getType(), TLI, VT))
Evan Cheng8b19e562008-09-03 06:44:39 +0000342 return false;
343
344 Value *V = I->getOperand(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000345 unsigned Ptr = getRegForValue(V);
346 if (Ptr == 0) {
Evan Cheng8b19e562008-09-03 06:44:39 +0000347 // Handle constant load address.
Evan Cheng0de588f2008-09-05 21:00:03 +0000348 // FIXME: If load type is something we can't handle, this can result in
349 // a dead stub load instruction.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000350 if (!isa<Constant>(V) || !X86SelectConstAddr(V, Ptr))
Evan Cheng8b19e562008-09-03 06:44:39 +0000351 // Unhandled operand. Halt "fast" selection and bail.
352 return false;
353 }
354
Evan Cheng0de588f2008-09-05 21:00:03 +0000355 unsigned ResultReg = 0;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000356 if (X86FastEmitLoad(VT, Ptr, V, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000357 UpdateValueMap(I, ResultReg);
358 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000359 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000360 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000361}
362
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000363bool X86FastISel::X86SelectCmp(Instruction *I) {
364 CmpInst *CI = cast<CmpInst>(I);
365
Dan Gohman4f22bb02008-09-05 01:33:56 +0000366 MVT VT = TLI.getValueType(I->getOperand(0)->getType());
367 if (!TLI.isTypeLegal(VT))
368 return false;
369
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000370 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000371 if (Op0Reg == 0) return false;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000372 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000373 if (Op1Reg == 0) return false;
374
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000375 unsigned Opc;
Dan Gohmanf52550b2008-09-05 01:15:35 +0000376 switch (VT.getSimpleVT()) {
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000377 case MVT::i8: Opc = X86::CMP8rr; break;
378 case MVT::i16: Opc = X86::CMP16rr; break;
379 case MVT::i32: Opc = X86::CMP32rr; break;
380 case MVT::i64: Opc = X86::CMP64rr; break;
381 case MVT::f32: Opc = X86::UCOMISSrr; break;
382 case MVT::f64: Opc = X86::UCOMISDrr; break;
383 default: return false;
384 }
385
386 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
387 switch (CI->getPredicate()) {
388 case CmpInst::FCMP_OEQ: {
389 unsigned EReg = createResultReg(&X86::GR8RegClass);
390 unsigned NPReg = createResultReg(&X86::GR8RegClass);
391 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
392 BuildMI(MBB, TII.get(X86::SETEr), EReg);
393 BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
394 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
395 break;
396 }
397 case CmpInst::FCMP_UNE: {
398 unsigned NEReg = createResultReg(&X86::GR8RegClass);
399 unsigned PReg = createResultReg(&X86::GR8RegClass);
400 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
401 BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
402 BuildMI(MBB, TII.get(X86::SETPr), PReg);
403 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
404 break;
405 }
406 case CmpInst::FCMP_OGT:
407 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
408 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
409 break;
410 case CmpInst::FCMP_OGE:
411 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
412 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
413 break;
414 case CmpInst::FCMP_OLT:
415 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
416 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
417 break;
418 case CmpInst::FCMP_OLE:
419 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
420 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
421 break;
422 case CmpInst::FCMP_ONE:
423 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
424 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
425 break;
426 case CmpInst::FCMP_ORD:
427 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
428 BuildMI(MBB, TII.get(X86::SETNPr), ResultReg);
429 break;
430 case CmpInst::FCMP_UNO:
431 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
432 BuildMI(MBB, TII.get(X86::SETPr), ResultReg);
433 break;
434 case CmpInst::FCMP_UEQ:
435 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
436 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
437 break;
438 case CmpInst::FCMP_UGT:
439 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
440 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
441 break;
442 case CmpInst::FCMP_UGE:
443 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
444 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
445 break;
446 case CmpInst::FCMP_ULT:
447 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
448 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
449 break;
450 case CmpInst::FCMP_ULE:
451 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
452 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
453 break;
454 case CmpInst::ICMP_EQ:
455 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
456 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
457 break;
458 case CmpInst::ICMP_NE:
459 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
460 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
461 break;
462 case CmpInst::ICMP_UGT:
463 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
464 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
465 break;
466 case CmpInst::ICMP_UGE:
467 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
468 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
469 break;
470 case CmpInst::ICMP_ULT:
471 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
472 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
473 break;
474 case CmpInst::ICMP_ULE:
475 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
476 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
477 break;
478 case CmpInst::ICMP_SGT:
479 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
480 BuildMI(MBB, TII.get(X86::SETGr), ResultReg);
481 break;
482 case CmpInst::ICMP_SGE:
483 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
484 BuildMI(MBB, TII.get(X86::SETGEr), ResultReg);
485 break;
486 case CmpInst::ICMP_SLT:
487 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
488 BuildMI(MBB, TII.get(X86::SETLr), ResultReg);
489 break;
490 case CmpInst::ICMP_SLE:
491 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
492 BuildMI(MBB, TII.get(X86::SETLEr), ResultReg);
493 break;
494 default:
495 return false;
496 }
497
498 UpdateValueMap(I, ResultReg);
499 return true;
500}
Evan Cheng8b19e562008-09-03 06:44:39 +0000501
Dan Gohmand89ae992008-09-05 01:06:14 +0000502bool X86FastISel::X86SelectZExt(Instruction *I) {
503 // Special-case hack: The only i1 values we know how to produce currently
504 // set the upper bits of an i8 value to zero.
505 if (I->getType() == Type::Int8Ty &&
506 I->getOperand(0)->getType() == Type::Int1Ty) {
507 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000508 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000509 UpdateValueMap(I, ResultReg);
510 return true;
511 }
512
513 return false;
514}
515
516bool X86FastISel::X86SelectBranch(Instruction *I) {
517 BranchInst *BI = cast<BranchInst>(I);
518 // Unconditional branches are selected by tablegen-generated code.
519 unsigned OpReg = getRegForValue(BI->getCondition());
Dan Gohmanf52550b2008-09-05 01:15:35 +0000520 if (OpReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000521 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
522 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
523
524 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
525 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
526 BuildMI(MBB, TII.get(X86::JMP)).addMBB(FalseMBB);
527
528 MBB->addSuccessor(TrueMBB);
529 MBB->addSuccessor(FalseMBB);
530
531 return true;
532}
533
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000534bool X86FastISel::X86SelectShift(Instruction *I) {
535 unsigned CReg = 0;
536 unsigned Opc = 0;
537 const TargetRegisterClass *RC = NULL;
538 if (I->getType() == Type::Int8Ty) {
539 CReg = X86::CL;
540 RC = &X86::GR8RegClass;
541 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000542 case Instruction::LShr: Opc = X86::SHR8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000543 case Instruction::AShr: Opc = X86::SAR8rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000544 case Instruction::Shl: Opc = X86::SHL8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000545 default: return false;
546 }
547 } else if (I->getType() == Type::Int16Ty) {
548 CReg = X86::CX;
549 RC = &X86::GR16RegClass;
550 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000551 case Instruction::LShr: Opc = X86::SHR16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000552 case Instruction::AShr: Opc = X86::SAR16rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000553 case Instruction::Shl: Opc = X86::SHL16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000554 default: return false;
555 }
556 } else if (I->getType() == Type::Int32Ty) {
557 CReg = X86::ECX;
558 RC = &X86::GR32RegClass;
559 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000560 case Instruction::LShr: Opc = X86::SHR32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000561 case Instruction::AShr: Opc = X86::SAR32rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000562 case Instruction::Shl: Opc = X86::SHL32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000563 default: return false;
564 }
565 } else if (I->getType() == Type::Int64Ty) {
566 CReg = X86::RCX;
567 RC = &X86::GR64RegClass;
568 switch (I->getOpcode()) {
Dan Gohman31d26912008-09-05 21:13:04 +0000569 case Instruction::LShr: Opc = X86::SHR64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000570 case Instruction::AShr: Opc = X86::SAR64rCL; break;
Dan Gohman31d26912008-09-05 21:13:04 +0000571 case Instruction::Shl: Opc = X86::SHL64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000572 default: return false;
573 }
574 } else {
575 return false;
576 }
577
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000578 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
579 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
580 return false;
581
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000582 unsigned Op0Reg = getRegForValue(I->getOperand(0));
583 if (Op0Reg == 0) return false;
584 unsigned Op1Reg = getRegForValue(I->getOperand(1));
585 if (Op1Reg == 0) return false;
586 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
587 unsigned ResultReg = createResultReg(RC);
588 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op0Reg);
589 UpdateValueMap(I, ResultReg);
590 return true;
591}
592
593bool X86FastISel::X86SelectSelect(Instruction *I) {
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000594 const Type *Ty = I->getType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000595 if (isa<PointerType>(Ty))
596 Ty = TLI.getTargetData()->getIntPtrType();
597
598 unsigned Opc = 0;
599 const TargetRegisterClass *RC = NULL;
600 if (Ty == Type::Int16Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000601 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000602 RC = &X86::GR16RegClass;
603 } else if (Ty == Type::Int32Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000604 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000605 RC = &X86::GR32RegClass;
606 } else if (Ty == Type::Int64Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000607 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000608 RC = &X86::GR64RegClass;
609 } else {
610 return false;
611 }
612
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000613 MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
614 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
615 return false;
616
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000617 unsigned Op0Reg = getRegForValue(I->getOperand(0));
618 if (Op0Reg == 0) return false;
619 unsigned Op1Reg = getRegForValue(I->getOperand(1));
620 if (Op1Reg == 0) return false;
621 unsigned Op2Reg = getRegForValue(I->getOperand(2));
622 if (Op2Reg == 0) return false;
623
624 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
625 unsigned ResultReg = createResultReg(RC);
626 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
627 UpdateValueMap(I, ResultReg);
628 return true;
629}
630
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000631bool X86FastISel::X86SelectTrunc(Instruction *I) {
632 if (Subtarget->is64Bit())
633 // All other cases should be handled by the tblgen generated code.
634 return false;
635 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
636 MVT DstVT = TLI.getValueType(I->getType());
637 if (DstVT != MVT::i8)
638 // All other cases should be handled by the tblgen generated code.
639 return false;
640 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
641 // All other cases should be handled by the tblgen generated code.
642 return false;
643
644 unsigned InputReg = getRegForValue(I->getOperand(0));
645 if (!InputReg)
646 // Unhandled operand. Halt "fast" selection and bail.
647 return false;
648
649 // First issue a copy to GR16_ or GR32_.
650 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
651 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
652 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
653 unsigned CopyReg = createResultReg(CopyRC);
654 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
655
656 // Then issue an extract_subreg.
657 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg,1); // x86_subreg_8bit
658 if (!ResultReg)
659 return false;
660
661 UpdateValueMap(I, ResultReg);
662 return true;
663}
664
Evan Chengf3d4efe2008-09-07 09:09:33 +0000665bool X86FastISel::X86SelectCall(Instruction *I) {
666 CallInst *CI = cast<CallInst>(I);
667 Value *Callee = I->getOperand(0);
668
669 // Can't handle inline asm yet.
670 if (isa<InlineAsm>(Callee))
671 return false;
672
673 // FIXME: Handle some intrinsics.
674 if (Function *F = CI->getCalledFunction()) {
675 if (F->isDeclaration() &&F->getIntrinsicID())
676 return false;
677 }
678
679 // Materialize callee address in a register. FIXME: GV address can be
680 // handled with a CALLpcrel32 instead.
681 unsigned CalleeOp = getRegForValue(Callee);
682 if (CalleeOp == 0) {
683 if (!isa<Constant>(Callee) || !X86SelectConstAddr(Callee, CalleeOp, true))
684 // Unhandled operand. Halt "fast" selection and bail.
685 return false;
686 }
687
688 // Handle only C and fastcc calling conventions for now.
689 CallSite CS(CI);
690 unsigned CC = CS.getCallingConv();
691 if (CC != CallingConv::C &&
692 CC != CallingConv::Fast &&
693 CC != CallingConv::X86_FastCall)
694 return false;
695
696 // Let SDISel handle vararg functions.
697 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
698 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
699 if (FTy->isVarArg())
700 return false;
701
702 // Handle *simple* calls for now.
703 const Type *RetTy = CS.getType();
704 MVT RetVT;
Evan Chengdebdea02008-09-08 17:15:42 +0000705 if (!isTypeLegal(RetTy, TLI, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000706 return false;
707
Evan Chengdebdea02008-09-08 17:15:42 +0000708 // Allow calls which produce i1 results.
709 bool AndToI1 = false;
710 if (RetVT == MVT::i1) {
711 RetVT = MVT::i8;
712 AndToI1 = true;
713 }
714
Evan Chengf3d4efe2008-09-07 09:09:33 +0000715 // Deal with call operands first.
716 SmallVector<unsigned, 4> Args;
717 SmallVector<MVT, 4> ArgVTs;
718 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
719 Args.reserve(CS.arg_size());
720 ArgVTs.reserve(CS.arg_size());
721 ArgFlags.reserve(CS.arg_size());
722 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
723 i != e; ++i) {
724 unsigned Arg = getRegForValue(*i);
725 if (Arg == 0)
726 return false;
727 ISD::ArgFlagsTy Flags;
728 unsigned AttrInd = i - CS.arg_begin() + 1;
729 if (CS.paramHasAttr(AttrInd, ParamAttr::SExt))
730 Flags.setSExt();
731 if (CS.paramHasAttr(AttrInd, ParamAttr::ZExt))
732 Flags.setZExt();
733
734 // FIXME: Only handle *easy* calls for now.
735 if (CS.paramHasAttr(AttrInd, ParamAttr::InReg) ||
736 CS.paramHasAttr(AttrInd, ParamAttr::StructRet) ||
737 CS.paramHasAttr(AttrInd, ParamAttr::Nest) ||
738 CS.paramHasAttr(AttrInd, ParamAttr::ByVal))
739 return false;
740
741 const Type *ArgTy = (*i)->getType();
742 MVT ArgVT;
743 if (!isTypeLegal(ArgTy, TLI, ArgVT))
744 return false;
745 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
746 Flags.setOrigAlign(OriginalAlignment);
747
748 Args.push_back(Arg);
749 ArgVTs.push_back(ArgVT);
750 ArgFlags.push_back(Flags);
751 }
752
753 // Analyze operands of the call, assigning locations to each operand.
754 SmallVector<CCValAssign, 16> ArgLocs;
755 CCState CCInfo(CC, false, TM, ArgLocs);
756 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
757
758 // Get a count of how many bytes are to be pushed on the stack.
759 unsigned NumBytes = CCInfo.getNextStackOffset();
760
761 // Issue CALLSEQ_START
762 BuildMI(MBB, TII.get(X86::ADJCALLSTACKDOWN)).addImm(NumBytes);
763
764 // Process argumenet: walk the register/memloc assignments, inserting
765 // copies / loads.
766 SmallVector<unsigned, 4> RegArgs;
767 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
768 CCValAssign &VA = ArgLocs[i];
769 unsigned Arg = Args[VA.getValNo()];
770 MVT ArgVT = ArgVTs[VA.getValNo()];
771
772 // Promote the value if needed.
773 switch (VA.getLocInfo()) {
774 default: assert(0 && "Unknown loc info!");
775 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000776 case CCValAssign::SExt: {
777 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
778 Arg, ArgVT, Arg);
779 assert(Emitted && "Failed to emit a sext!");
780 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000781 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000782 }
783 case CCValAssign::ZExt: {
784 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
785 Arg, ArgVT, Arg);
786 assert(Emitted && "Failed to emit a zext!");
787 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000788 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000789 }
790 case CCValAssign::AExt: {
791 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
792 Arg, ArgVT, Arg);
793 assert(Emitted && "Failed to emit a aext!");
794 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000795 break;
796 }
Evan Cheng24e3a902008-09-08 06:35:17 +0000797 }
Evan Chengf3d4efe2008-09-07 09:09:33 +0000798
799 if (VA.isRegLoc()) {
800 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
801 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
802 Arg, RC, RC);
803 assert(Emitted && "Failed to emit a copy instruction!");
804 RegArgs.push_back(VA.getLocReg());
805 } else {
806 unsigned LocMemOffset = VA.getLocMemOffset();
807 X86FastEmitStore(ArgVT, Arg, StackPtr, LocMemOffset, NULL);
808 }
809 }
810
811 // Issue the call.
812 unsigned CallOpc = CalleeOp
813 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
814 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
815 MachineInstrBuilder MIB = CalleeOp
816 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
817 :BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(cast<GlobalValue>(Callee));
818 // Add implicit physical register uses to the call.
819 while (!RegArgs.empty()) {
820 MIB.addReg(RegArgs.back());
821 RegArgs.pop_back();
822 }
823
824 // Issue CALLSEQ_END
825 BuildMI(MBB, TII.get(X86::ADJCALLSTACKUP)).addImm(NumBytes).addImm(0);
826
827 // Now handle call return value (if any).
Evan Chengf3d4efe2008-09-07 09:09:33 +0000828 if (RetVT.getSimpleVT() != MVT::isVoid) {
829 SmallVector<CCValAssign, 16> RVLocs;
830 CCState CCInfo(CC, false, TM, RVLocs);
831 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
832
833 // Copy all of the result registers out of their specified physreg.
834 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
835 MVT CopyVT = RVLocs[0].getValVT();
836 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
837 TargetRegisterClass *SrcRC = DstRC;
838
839 // If this is a call to a function that returns an fp value on the x87 fp
840 // stack, but where we prefer to use the value in xmm registers, copy it
841 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
842 if ((RVLocs[0].getLocReg() == X86::ST0 ||
843 RVLocs[0].getLocReg() == X86::ST1) &&
844 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
845 CopyVT = MVT::f80;
846 SrcRC = X86::RSTRegisterClass;
847 DstRC = X86::RFP80RegisterClass;
848 }
849
850 unsigned ResultReg = createResultReg(DstRC);
851 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
852 RVLocs[0].getLocReg(), DstRC, SrcRC);
853 assert(Emitted && "Failed to emit a copy instruction!");
854 if (CopyVT != RVLocs[0].getValVT()) {
855 // Round the F80 the right size, which also moves to the appropriate xmm
856 // register. This is accomplished by storing the F80 value in memory and
857 // then loading it back. Ewww...
858 MVT ResVT = RVLocs[0].getValVT();
859 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
860 unsigned MemSize = ResVT.getSizeInBits()/8;
861 int FI = MFI->CreateStackObject(MemSize, MemSize);
862 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
863 DstRC = ResVT == MVT::f32
864 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
865 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
866 ResultReg = createResultReg(DstRC);
867 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
868 }
869
Evan Chengdebdea02008-09-08 17:15:42 +0000870 if (AndToI1) {
871 // Mask out all but lowest bit for some call which produces an i1.
872 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
873 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
874 ResultReg = AndResult;
875 }
876
Evan Chengf3d4efe2008-09-07 09:09:33 +0000877 UpdateValueMap(I, ResultReg);
878 }
879
880 return true;
881}
882
883
Dan Gohman99b21822008-08-28 23:21:34 +0000884bool
Dan Gohman3df24e62008-09-03 23:12:08 +0000885X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +0000886 switch (I->getOpcode()) {
887 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +0000888 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +0000889 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +0000890 case Instruction::Store:
891 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000892 case Instruction::ICmp:
893 case Instruction::FCmp:
894 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000895 case Instruction::ZExt:
896 return X86SelectZExt(I);
897 case Instruction::Br:
898 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000899 case Instruction::Call:
900 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000901 case Instruction::LShr:
902 case Instruction::AShr:
903 case Instruction::Shl:
904 return X86SelectShift(I);
905 case Instruction::Select:
906 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000907 case Instruction::Trunc:
908 return X86SelectTrunc(I);
Dan Gohman99b21822008-08-28 23:21:34 +0000909 }
910
911 return false;
912}
913
Owen Anderson9c7216f2008-09-05 20:49:33 +0000914unsigned X86FastISel::TargetMaterializeConstant(Constant *C,
915 MachineConstantPool* MCP) {
Owen Anderson95267a12008-09-05 00:06:23 +0000916 // Can't handle PIC-mode yet.
917 if (TM.getRelocationModel() == Reloc::PIC_)
918 return 0;
919
920 MVT VT = MVT::getMVT(C->getType(), /*HandleUnknown=*/true);
921 if (VT == MVT::Other || !VT.isSimple())
922 // Unhandled type. Halt "fast" selection and bail.
923 return false;
924 if (VT == MVT::iPTR)
925 // Use pointer type.
926 VT = TLI.getPointerTy();
927 // We only handle legal types. For example, on x86-32 the instruction
928 // selector contains all of the 64-bit instructions from x86-64,
929 // under the assumption that i64 won't be used if the target doesn't
930 // support it.
931 if (!TLI.isTypeLegal(VT))
932 return false;
933
934 // Get opcode and regclass of the output for the given load instruction.
935 unsigned Opc = 0;
936 const TargetRegisterClass *RC = NULL;
937 switch (VT.getSimpleVT()) {
938 default: return false;
939 case MVT::i8:
940 Opc = X86::MOV8rm;
941 RC = X86::GR8RegisterClass;
942 break;
943 case MVT::i16:
944 Opc = X86::MOV16rm;
945 RC = X86::GR16RegisterClass;
946 break;
947 case MVT::i32:
948 Opc = X86::MOV32rm;
949 RC = X86::GR32RegisterClass;
950 break;
951 case MVT::i64:
952 // Must be in x86-64 mode.
953 Opc = X86::MOV64rm;
954 RC = X86::GR64RegisterClass;
955 break;
956 case MVT::f32:
957 if (Subtarget->hasSSE1()) {
958 Opc = X86::MOVSSrm;
959 RC = X86::FR32RegisterClass;
960 } else {
961 Opc = X86::LD_Fp32m;
962 RC = X86::RFP32RegisterClass;
963 }
964 break;
965 case MVT::f64:
966 if (Subtarget->hasSSE2()) {
967 Opc = X86::MOVSDrm;
968 RC = X86::FR64RegisterClass;
969 } else {
970 Opc = X86::LD_Fp64m;
971 RC = X86::RFP64RegisterClass;
972 }
973 break;
974 case MVT::f80:
975 Opc = X86::LD_Fp80m;
976 RC = X86::RFP80RegisterClass;
977 break;
978 }
979
980 unsigned ResultReg = createResultReg(RC);
981 if (isa<GlobalValue>(C)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000982 // FIXME: If store value type is something we can't handle, this can result
983 // in a dead stub load instruction.
Owen Anderson95267a12008-09-05 00:06:23 +0000984 if (X86SelectConstAddr(C, ResultReg))
985 return ResultReg;
Evan Cheng0de588f2008-09-05 21:00:03 +0000986 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +0000987 }
988
Owen Anderson3b217c62008-09-06 01:11:01 +0000989 // MachineConstantPool wants an explicit alignment.
990 unsigned Align =
991 TM.getTargetData()->getPreferredTypeAlignmentShift(C->getType());
992 if (Align == 0) {
993 // Alignment of vector types. FIXME!
994 Align = TM.getTargetData()->getABITypeSize(C->getType());
995 Align = Log2_64(Align);
996 }
Owen Anderson95267a12008-09-05 00:06:23 +0000997
Owen Anderson3b217c62008-09-06 01:11:01 +0000998 unsigned MCPOffset = MCP->getConstantPoolIndex(C, Align);
Owen Anderson95267a12008-09-05 00:06:23 +0000999 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset);
Owen Anderson95267a12008-09-05 00:06:23 +00001000 return ResultReg;
1001}
1002
Evan Chengc3f44b02008-09-03 00:03:49 +00001003namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001004 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
1005 DenseMap<const Value *, unsigned> &vm,
1006 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm) {
1007 return new X86FastISel(mf, vm, bm);
Evan Chengc3f44b02008-09-03 00:03:49 +00001008 }
Dan Gohman99b21822008-08-28 23:21:34 +00001009}