blob: 7747788e8136090af7f5af356807f0706ed7668d [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"
Dan Gohman35893082008-09-18 23:23:44 +000030#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000031
32using namespace llvm;
33
34class X86FastISel : public FastISel {
35 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
36 /// make the right decision when generating code for different targets.
37 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000038
39 /// StackPtr - Register used as the stack pointer.
40 ///
41 unsigned StackPtr;
42
43 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
44 /// floating point ops.
45 /// When SSE is available, use it for f32 operations.
46 /// When SSE2 is available, use it for f64 operations.
47 bool X86ScalarSSEf64;
48 bool X86ScalarSSEf32;
49
Evan Cheng8b19e562008-09-03 06:44:39 +000050public:
Dan Gohman3df24e62008-09-03 23:12:08 +000051 explicit X86FastISel(MachineFunction &mf,
52 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +000053 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
54 DenseMap<const AllocaInst *, int> &am)
55 : FastISel(mf, vm, bm, am) {
Evan Cheng88e30412008-09-03 01:04:47 +000056 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000057 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
58 X86ScalarSSEf64 = Subtarget->hasSSE2();
59 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000060 }
Evan Chengc3f44b02008-09-03 00:03:49 +000061
Dan Gohman3df24e62008-09-03 23:12:08 +000062 virtual bool TargetSelectInstruction(Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000063
Dan Gohman1adf1b02008-08-19 21:45:35 +000064#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000065
66private:
Dan Gohman0586d912008-09-10 20:11:02 +000067 bool X86FastEmitLoad(MVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000068
Evan Chengf3d4efe2008-09-07 09:09:33 +000069 bool X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000070 const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000071
72 bool X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT, unsigned Src, MVT SrcVT,
73 unsigned &ResultReg);
Evan Cheng0de588f2008-09-05 21:00:03 +000074
Dan Gohman2ff7fd12008-09-19 22:16:54 +000075 bool X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall);
Dan Gohman0586d912008-09-10 20:11:02 +000076
Dan Gohman3df24e62008-09-03 23:12:08 +000077 bool X86SelectLoad(Instruction *I);
Owen Andersona3971df2008-09-04 07:08:58 +000078
79 bool X86SelectStore(Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000080
81 bool X86SelectCmp(Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000082
83 bool X86SelectZExt(Instruction *I);
84
85 bool X86SelectBranch(Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +000086
87 bool X86SelectShift(Instruction *I);
88
89 bool X86SelectSelect(Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +000090
Evan Cheng10a8d9c2008-09-07 08:47:42 +000091 bool X86SelectTrunc(Instruction *I);
92
Dan Gohman78efce62008-09-10 21:02:08 +000093 bool X86SelectFPExt(Instruction *I);
94 bool X86SelectFPTrunc(Instruction *I);
95
Evan Chengf3d4efe2008-09-07 09:09:33 +000096 bool X86SelectCall(Instruction *I);
97
98 CCAssignFn *CCAssignFnForCall(unsigned CC, bool isTailCall = false);
99
Dan Gohman0586d912008-09-10 20:11:02 +0000100 unsigned TargetMaterializeConstant(Constant *C);
101
102 unsigned TargetMaterializeAlloca(AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000103
104 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
105 /// computed in an SSE register, not on the X87 floating point stack.
106 bool isScalarFPTypeInSSEReg(MVT VT) const {
107 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
108 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
109 }
110
Evan Chengc3f44b02008-09-03 00:03:49 +0000111};
Dan Gohman99b21822008-08-28 23:21:34 +0000112
Evan Chengdebdea02008-09-08 17:15:42 +0000113static bool isTypeLegal(const Type *Ty, const TargetLowering &TLI, MVT &VT,
114 bool AllowI1 = false) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000115 VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
116 if (VT == MVT::Other || !VT.isSimple())
117 // Unhandled type. Halt "fast" selection and bail.
118 return false;
119 if (VT == MVT::iPTR)
120 // Use pointer type.
121 VT = TLI.getPointerTy();
122 // We only handle legal types. For example, on x86-32 the instruction
123 // selector contains all of the 64-bit instructions from x86-64,
124 // under the assumption that i64 won't be used if the target doesn't
125 // support it.
Evan Chengdebdea02008-09-08 17:15:42 +0000126 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000127}
128
129#include "X86GenCallingConv.inc"
130
131/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
132/// convention.
133CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) {
134 if (Subtarget->is64Bit()) {
135 if (Subtarget->isTargetWin64())
136 return CC_X86_Win64_C;
137 else if (CC == CallingConv::Fast && isTaillCall)
138 return CC_X86_64_TailCall;
139 else
140 return CC_X86_64_C;
141 }
142
143 if (CC == CallingConv::X86_FastCall)
144 return CC_X86_32_FastCall;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000145 else if (CC == CallingConv::Fast)
146 return CC_X86_32_FastCC;
147 else
148 return CC_X86_32_C;
149}
150
Evan Cheng0de588f2008-09-05 21:00:03 +0000151/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000152/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000153/// Return true and the result register by reference if it is possible.
Dan Gohman0586d912008-09-10 20:11:02 +0000154bool X86FastISel::X86FastEmitLoad(MVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000155 unsigned &ResultReg) {
156 // Get opcode and regclass of the output for the given load instruction.
157 unsigned Opc = 0;
158 const TargetRegisterClass *RC = NULL;
159 switch (VT.getSimpleVT()) {
160 default: return false;
161 case MVT::i8:
162 Opc = X86::MOV8rm;
163 RC = X86::GR8RegisterClass;
164 break;
165 case MVT::i16:
166 Opc = X86::MOV16rm;
167 RC = X86::GR16RegisterClass;
168 break;
169 case MVT::i32:
170 Opc = X86::MOV32rm;
171 RC = X86::GR32RegisterClass;
172 break;
173 case MVT::i64:
174 // Must be in x86-64 mode.
175 Opc = X86::MOV64rm;
176 RC = X86::GR64RegisterClass;
177 break;
178 case MVT::f32:
179 if (Subtarget->hasSSE1()) {
180 Opc = X86::MOVSSrm;
181 RC = X86::FR32RegisterClass;
182 } else {
183 Opc = X86::LD_Fp32m;
184 RC = X86::RFP32RegisterClass;
185 }
186 break;
187 case MVT::f64:
188 if (Subtarget->hasSSE2()) {
189 Opc = X86::MOVSDrm;
190 RC = X86::FR64RegisterClass;
191 } else {
192 Opc = X86::LD_Fp64m;
193 RC = X86::RFP64RegisterClass;
194 }
195 break;
196 case MVT::f80:
197 Opc = X86::LD_Fp80m;
198 RC = X86::RFP80RegisterClass;
199 break;
200 }
201
202 ResultReg = createResultReg(RC);
Evan Cheng0de588f2008-09-05 21:00:03 +0000203 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
204 return true;
205}
206
Evan Chengf3d4efe2008-09-07 09:09:33 +0000207/// X86FastEmitStore - Emit a machine instruction to store a value Val of
208/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
209/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000210/// i.e. V. Return true if it is possible.
211bool
Evan Chengf3d4efe2008-09-07 09:09:33 +0000212X86FastISel::X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000213 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000214 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000215 unsigned Opc = 0;
216 const TargetRegisterClass *RC = NULL;
217 switch (VT.getSimpleVT()) {
218 default: return false;
219 case MVT::i8:
220 Opc = X86::MOV8mr;
221 RC = X86::GR8RegisterClass;
222 break;
223 case MVT::i16:
224 Opc = X86::MOV16mr;
225 RC = X86::GR16RegisterClass;
226 break;
227 case MVT::i32:
228 Opc = X86::MOV32mr;
229 RC = X86::GR32RegisterClass;
230 break;
231 case MVT::i64:
232 // Must be in x86-64 mode.
233 Opc = X86::MOV64mr;
234 RC = X86::GR64RegisterClass;
235 break;
236 case MVT::f32:
237 if (Subtarget->hasSSE1()) {
238 Opc = X86::MOVSSmr;
239 RC = X86::FR32RegisterClass;
240 } else {
241 Opc = X86::ST_Fp32m;
242 RC = X86::RFP32RegisterClass;
243 }
244 break;
245 case MVT::f64:
246 if (Subtarget->hasSSE2()) {
247 Opc = X86::MOVSDmr;
248 RC = X86::FR64RegisterClass;
249 } else {
250 Opc = X86::ST_Fp64m;
251 RC = X86::RFP64RegisterClass;
252 }
253 break;
254 case MVT::f80:
255 Opc = X86::ST_FP80m;
256 RC = X86::RFP80RegisterClass;
257 break;
258 }
259
Evan Chengf3d4efe2008-09-07 09:09:33 +0000260 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000261 return true;
262}
263
Evan Cheng24e3a902008-09-08 06:35:17 +0000264/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
265/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
266/// ISD::SIGN_EXTEND).
267bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT,
268 unsigned Src, MVT SrcVT,
269 unsigned &ResultReg) {
Owen Andersonac34a002008-09-11 19:44:55 +0000270 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
271
272 if (RR != 0) {
273 ResultReg = RR;
274 return true;
275 } else
276 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000277}
278
Dan Gohman0586d912008-09-10 20:11:02 +0000279/// X86SelectAddress - Attempt to fill in an address from the given value.
280///
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000281bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall) {
Dan Gohman35893082008-09-18 23:23:44 +0000282 User *U;
283 unsigned Opcode = Instruction::UserOp1;
284 if (Instruction *I = dyn_cast<Instruction>(V)) {
285 Opcode = I->getOpcode();
286 U = I;
287 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
288 Opcode = C->getOpcode();
289 U = C;
290 }
Dan Gohman0586d912008-09-10 20:11:02 +0000291
Dan Gohman35893082008-09-18 23:23:44 +0000292 switch (Opcode) {
293 default: break;
294 case Instruction::BitCast:
295 // Look past bitcasts.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000296 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000297
298 case Instruction::IntToPtr:
299 // Look past no-op inttoptrs.
300 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000301 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000302
303 case Instruction::PtrToInt:
304 // Look past no-op ptrtoints.
305 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000306 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000307
308 case Instruction::Alloca: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000309 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000310 // Do static allocas.
311 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman0586d912008-09-10 20:11:02 +0000312 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
313 if (SI == StaticAllocaMap.end())
314 return false;
315 AM.BaseType = X86AddressMode::FrameIndexBase;
316 AM.Base.FrameIndex = SI->second;
Dan Gohman35893082008-09-18 23:23:44 +0000317 return true;
318 }
319
320 case Instruction::Add: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000321 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000322 // Adds of constants are common and easy enough.
323 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
324 AM.Disp += CI->getZExtValue();
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000325 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman0586d912008-09-10 20:11:02 +0000326 }
Dan Gohman35893082008-09-18 23:23:44 +0000327 break;
328 }
329
330 case Instruction::GetElementPtr: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000331 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000332 // Pattern-match simple GEPs.
333 uint64_t Disp = AM.Disp;
334 unsigned IndexReg = AM.IndexReg;
335 unsigned Scale = AM.Scale;
336 gep_type_iterator GTI = gep_type_begin(U);
337 // Look at all but the last index. Constants can be folded,
338 // and one dynamic index can be handled, if the scale is supported.
339 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
340 i != e; ++i, ++GTI) {
341 Value *Op = *i;
342 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
343 const StructLayout *SL = TD.getStructLayout(STy);
344 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
345 Disp += SL->getElementOffset(Idx);
346 } else {
347 uint64_t S = TD.getABITypeSize(GTI.getIndexedType());
348 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
349 // Constant-offset addressing.
350 Disp += CI->getZExtValue() * S;
351 } else if (IndexReg == 0 &&
352 (S == 1 || S == 2 || S == 4 || S == 8)) {
353 // Scaled-index addressing.
354 Scale = S;
355 IndexReg = getRegForValue(Op);
356 if (IndexReg == 0)
357 return false;
358 } else
359 // Unsupported.
360 goto unsupported_gep;
361 }
362 }
363 // Ok, the GEP indices were covered by constant-offset and scaled-index
364 // addressing. Update the address state and move on to examining the base.
365 AM.IndexReg = IndexReg;
366 AM.Scale = Scale;
367 AM.Disp = Disp;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000368 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000369 unsupported_gep:
370 // Ok, the GEP indices weren't all covered.
371 break;
372 }
373 }
374
375 // Handle constant address.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000376 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
377 if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) {
378 // Check to see if we've already materialized this
379 // value in a register in this block.
Dan Gohman7e8ef602008-09-19 23:42:04 +0000380 if (unsigned Reg = LocalValueMap[V]) {
381 AM.Base.Reg = Reg;
382 return true;
383 }
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000384 // Issue load from stub if necessary.
385 unsigned Opc = 0;
386 const TargetRegisterClass *RC = NULL;
387 if (TLI.getPointerTy() == MVT::i32) {
388 Opc = X86::MOV32rm;
389 RC = X86::GR32RegisterClass;
390 } else {
391 Opc = X86::MOV64rm;
392 RC = X86::GR64RegisterClass;
393 }
394 AM.Base.Reg = createResultReg(RC);
395 X86AddressMode LocalAM;
396 LocalAM.GV = GV;
397 addFullAddress(BuildMI(MBB, TII.get(Opc), AM.Base.Reg), LocalAM);
398 // Prevent loading GV stub multiple times in same MBB.
399 LocalValueMap[V] = AM.Base.Reg;
400 } else {
401 AM.GV = GV;
402 }
403 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000404 }
405
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000406 // If all else fails, just materialize the value in a register.
407 AM.Base.Reg = getRegForValue(V);
408 return AM.Base.Reg != 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000409}
410
Owen Andersona3971df2008-09-04 07:08:58 +0000411/// X86SelectStore - Select and emit code to implement store instructions.
412bool X86FastISel::X86SelectStore(Instruction* I) {
Evan Cheng24e3a902008-09-08 06:35:17 +0000413 MVT VT;
414 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
Owen Andersona3971df2008-09-04 07:08:58 +0000415 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000416 unsigned Val = getRegForValue(I->getOperand(0));
417 if (Val == 0)
Owen Andersona3971df2008-09-04 07:08:58 +0000418 // Unhandled operand. Halt "fast" selection and bail.
419 return false;
420
Dan Gohman0586d912008-09-10 20:11:02 +0000421 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000422 if (!X86SelectAddress(I->getOperand(1), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000423 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000424
Dan Gohman0586d912008-09-10 20:11:02 +0000425 return X86FastEmitStore(VT, Val, AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000426}
427
Evan Cheng8b19e562008-09-03 06:44:39 +0000428/// X86SelectLoad - Select and emit code to implement load instructions.
429///
Dan Gohman3df24e62008-09-03 23:12:08 +0000430bool X86FastISel::X86SelectLoad(Instruction *I) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000431 MVT VT;
432 if (!isTypeLegal(I->getType(), TLI, VT))
Evan Cheng8b19e562008-09-03 06:44:39 +0000433 return false;
434
Dan Gohman0586d912008-09-10 20:11:02 +0000435 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000436 if (!X86SelectAddress(I->getOperand(0), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000437 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000438
Evan Cheng0de588f2008-09-05 21:00:03 +0000439 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000440 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000441 UpdateValueMap(I, ResultReg);
442 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000443 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000444 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000445}
446
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000447bool X86FastISel::X86SelectCmp(Instruction *I) {
448 CmpInst *CI = cast<CmpInst>(I);
449
Dan Gohman4f22bb02008-09-05 01:33:56 +0000450 MVT VT = TLI.getValueType(I->getOperand(0)->getType());
451 if (!TLI.isTypeLegal(VT))
452 return false;
453
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000454 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000455 if (Op0Reg == 0) return false;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000456 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000457 if (Op1Reg == 0) return false;
458
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000459 unsigned Opc;
Dan Gohmanf52550b2008-09-05 01:15:35 +0000460 switch (VT.getSimpleVT()) {
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000461 case MVT::i8: Opc = X86::CMP8rr; break;
462 case MVT::i16: Opc = X86::CMP16rr; break;
463 case MVT::i32: Opc = X86::CMP32rr; break;
464 case MVT::i64: Opc = X86::CMP64rr; break;
465 case MVT::f32: Opc = X86::UCOMISSrr; break;
466 case MVT::f64: Opc = X86::UCOMISDrr; break;
467 default: return false;
468 }
469
470 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
471 switch (CI->getPredicate()) {
472 case CmpInst::FCMP_OEQ: {
473 unsigned EReg = createResultReg(&X86::GR8RegClass);
474 unsigned NPReg = createResultReg(&X86::GR8RegClass);
475 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
476 BuildMI(MBB, TII.get(X86::SETEr), EReg);
477 BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
478 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
479 break;
480 }
481 case CmpInst::FCMP_UNE: {
482 unsigned NEReg = createResultReg(&X86::GR8RegClass);
483 unsigned PReg = createResultReg(&X86::GR8RegClass);
484 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
485 BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
486 BuildMI(MBB, TII.get(X86::SETPr), PReg);
487 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
488 break;
489 }
490 case CmpInst::FCMP_OGT:
491 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
492 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
493 break;
494 case CmpInst::FCMP_OGE:
495 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
496 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
497 break;
498 case CmpInst::FCMP_OLT:
499 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
500 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
501 break;
502 case CmpInst::FCMP_OLE:
503 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
504 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
505 break;
506 case CmpInst::FCMP_ONE:
507 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
508 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
509 break;
510 case CmpInst::FCMP_ORD:
511 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
512 BuildMI(MBB, TII.get(X86::SETNPr), ResultReg);
513 break;
514 case CmpInst::FCMP_UNO:
515 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
516 BuildMI(MBB, TII.get(X86::SETPr), ResultReg);
517 break;
518 case CmpInst::FCMP_UEQ:
519 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
520 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
521 break;
522 case CmpInst::FCMP_UGT:
523 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
524 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
525 break;
526 case CmpInst::FCMP_UGE:
527 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
528 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
529 break;
530 case CmpInst::FCMP_ULT:
531 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
532 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
533 break;
534 case CmpInst::FCMP_ULE:
535 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
536 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
537 break;
538 case CmpInst::ICMP_EQ:
539 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
540 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
541 break;
542 case CmpInst::ICMP_NE:
543 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
544 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
545 break;
546 case CmpInst::ICMP_UGT:
547 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
548 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
549 break;
550 case CmpInst::ICMP_UGE:
551 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
552 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
553 break;
554 case CmpInst::ICMP_ULT:
555 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
556 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
557 break;
558 case CmpInst::ICMP_ULE:
559 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
560 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
561 break;
562 case CmpInst::ICMP_SGT:
563 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
564 BuildMI(MBB, TII.get(X86::SETGr), ResultReg);
565 break;
566 case CmpInst::ICMP_SGE:
567 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
568 BuildMI(MBB, TII.get(X86::SETGEr), ResultReg);
569 break;
570 case CmpInst::ICMP_SLT:
571 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
572 BuildMI(MBB, TII.get(X86::SETLr), ResultReg);
573 break;
574 case CmpInst::ICMP_SLE:
575 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
576 BuildMI(MBB, TII.get(X86::SETLEr), ResultReg);
577 break;
578 default:
579 return false;
580 }
581
582 UpdateValueMap(I, ResultReg);
583 return true;
584}
Evan Cheng8b19e562008-09-03 06:44:39 +0000585
Dan Gohmand89ae992008-09-05 01:06:14 +0000586bool X86FastISel::X86SelectZExt(Instruction *I) {
587 // Special-case hack: The only i1 values we know how to produce currently
588 // set the upper bits of an i8 value to zero.
589 if (I->getType() == Type::Int8Ty &&
590 I->getOperand(0)->getType() == Type::Int1Ty) {
591 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000592 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000593 UpdateValueMap(I, ResultReg);
594 return true;
595 }
596
597 return false;
598}
599
600bool X86FastISel::X86SelectBranch(Instruction *I) {
601 BranchInst *BI = cast<BranchInst>(I);
602 // Unconditional branches are selected by tablegen-generated code.
603 unsigned OpReg = getRegForValue(BI->getCondition());
Dan Gohmanf52550b2008-09-05 01:15:35 +0000604 if (OpReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000605 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
606 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
607
608 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
609 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
610 BuildMI(MBB, TII.get(X86::JMP)).addMBB(FalseMBB);
611
612 MBB->addSuccessor(TrueMBB);
613 MBB->addSuccessor(FalseMBB);
614
615 return true;
616}
617
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000618bool X86FastISel::X86SelectShift(Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000619 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000620 const TargetRegisterClass *RC = NULL;
621 if (I->getType() == Type::Int8Ty) {
622 CReg = X86::CL;
623 RC = &X86::GR8RegClass;
624 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000625 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
626 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
627 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000628 default: return false;
629 }
630 } else if (I->getType() == Type::Int16Ty) {
631 CReg = X86::CX;
632 RC = &X86::GR16RegClass;
633 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000634 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
635 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
636 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000637 default: return false;
638 }
639 } else if (I->getType() == Type::Int32Ty) {
640 CReg = X86::ECX;
641 RC = &X86::GR32RegClass;
642 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000643 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
644 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
645 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000646 default: return false;
647 }
648 } else if (I->getType() == Type::Int64Ty) {
649 CReg = X86::RCX;
650 RC = &X86::GR64RegClass;
651 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000652 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
653 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
654 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000655 default: return false;
656 }
657 } else {
658 return false;
659 }
660
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000661 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
662 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
663 return false;
664
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000665 unsigned Op0Reg = getRegForValue(I->getOperand(0));
666 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +0000667
668 // Fold immediate in shl(x,3).
669 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
670 unsigned ResultReg = createResultReg(RC);
671 BuildMI(MBB, TII.get(OpImm),
672 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue());
673 UpdateValueMap(I, ResultReg);
674 return true;
675 }
676
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000677 unsigned Op1Reg = getRegForValue(I->getOperand(1));
678 if (Op1Reg == 0) return false;
679 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
680 unsigned ResultReg = createResultReg(RC);
Chris Lattner743922e2008-09-21 21:44:29 +0000681 BuildMI(MBB, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000682 UpdateValueMap(I, ResultReg);
683 return true;
684}
685
686bool X86FastISel::X86SelectSelect(Instruction *I) {
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000687 const Type *Ty = I->getType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000688 if (isa<PointerType>(Ty))
Dan Gohman1fbc3cd2008-09-18 18:26:43 +0000689 Ty = TD.getIntPtrType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000690
691 unsigned Opc = 0;
692 const TargetRegisterClass *RC = NULL;
693 if (Ty == Type::Int16Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000694 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000695 RC = &X86::GR16RegClass;
696 } else if (Ty == Type::Int32Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000697 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000698 RC = &X86::GR32RegClass;
699 } else if (Ty == Type::Int64Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000700 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000701 RC = &X86::GR64RegClass;
702 } else {
703 return false;
704 }
705
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000706 MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
707 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
708 return false;
709
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000710 unsigned Op0Reg = getRegForValue(I->getOperand(0));
711 if (Op0Reg == 0) return false;
712 unsigned Op1Reg = getRegForValue(I->getOperand(1));
713 if (Op1Reg == 0) return false;
714 unsigned Op2Reg = getRegForValue(I->getOperand(2));
715 if (Op2Reg == 0) return false;
716
717 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
718 unsigned ResultReg = createResultReg(RC);
719 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
720 UpdateValueMap(I, ResultReg);
721 return true;
722}
723
Dan Gohman78efce62008-09-10 21:02:08 +0000724bool X86FastISel::X86SelectFPExt(Instruction *I) {
725 if (Subtarget->hasSSE2()) {
726 if (I->getType() == Type::DoubleTy) {
727 Value *V = I->getOperand(0);
728 if (V->getType() == Type::FloatTy) {
729 unsigned OpReg = getRegForValue(V);
730 if (OpReg == 0) return false;
731 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
732 BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
733 UpdateValueMap(I, ResultReg);
734 return true;
735 }
736 }
737 }
738
739 return false;
740}
741
742bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
743 if (Subtarget->hasSSE2()) {
744 if (I->getType() == Type::FloatTy) {
745 Value *V = I->getOperand(0);
746 if (V->getType() == Type::DoubleTy) {
747 unsigned OpReg = getRegForValue(V);
748 if (OpReg == 0) return false;
749 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
750 BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
751 UpdateValueMap(I, ResultReg);
752 return true;
753 }
754 }
755 }
756
757 return false;
758}
759
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000760bool X86FastISel::X86SelectTrunc(Instruction *I) {
761 if (Subtarget->is64Bit())
762 // All other cases should be handled by the tblgen generated code.
763 return false;
764 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
765 MVT DstVT = TLI.getValueType(I->getType());
766 if (DstVT != MVT::i8)
767 // All other cases should be handled by the tblgen generated code.
768 return false;
769 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
770 // All other cases should be handled by the tblgen generated code.
771 return false;
772
773 unsigned InputReg = getRegForValue(I->getOperand(0));
774 if (!InputReg)
775 // Unhandled operand. Halt "fast" selection and bail.
776 return false;
777
778 // First issue a copy to GR16_ or GR32_.
779 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
780 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
781 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
782 unsigned CopyReg = createResultReg(CopyRC);
783 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
784
785 // Then issue an extract_subreg.
786 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg,1); // x86_subreg_8bit
787 if (!ResultReg)
788 return false;
789
790 UpdateValueMap(I, ResultReg);
791 return true;
792}
793
Evan Chengf3d4efe2008-09-07 09:09:33 +0000794bool X86FastISel::X86SelectCall(Instruction *I) {
795 CallInst *CI = cast<CallInst>(I);
796 Value *Callee = I->getOperand(0);
797
798 // Can't handle inline asm yet.
799 if (isa<InlineAsm>(Callee))
800 return false;
801
802 // FIXME: Handle some intrinsics.
803 if (Function *F = CI->getCalledFunction()) {
804 if (F->isDeclaration() &&F->getIntrinsicID())
805 return false;
806 }
807
Evan Chengf3d4efe2008-09-07 09:09:33 +0000808 // Handle only C and fastcc calling conventions for now.
809 CallSite CS(CI);
810 unsigned CC = CS.getCallingConv();
811 if (CC != CallingConv::C &&
812 CC != CallingConv::Fast &&
813 CC != CallingConv::X86_FastCall)
814 return false;
815
816 // Let SDISel handle vararg functions.
817 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
818 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
819 if (FTy->isVarArg())
820 return false;
821
822 // Handle *simple* calls for now.
823 const Type *RetTy = CS.getType();
824 MVT RetVT;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000825 if (RetTy == Type::VoidTy)
826 RetVT = MVT::isVoid;
827 else if (!isTypeLegal(RetTy, TLI, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000828 return false;
829
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000830 // Materialize callee address in a register. FIXME: GV address can be
831 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000832 X86AddressMode CalleeAM;
833 if (!X86SelectAddress(Callee, CalleeAM, true))
834 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000835 unsigned CalleeOp = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000836 GlobalValue *GV = 0;
837 if (CalleeAM.Base.Reg != 0) {
838 assert(CalleeAM.GV == 0);
839 CalleeOp = CalleeAM.Base.Reg;
840 } else if (CalleeAM.GV != 0) {
841 assert(CalleeAM.GV != 0);
842 GV = CalleeAM.GV;
843 } else
844 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000845
Evan Chengdebdea02008-09-08 17:15:42 +0000846 // Allow calls which produce i1 results.
847 bool AndToI1 = false;
848 if (RetVT == MVT::i1) {
849 RetVT = MVT::i8;
850 AndToI1 = true;
851 }
852
Evan Chengf3d4efe2008-09-07 09:09:33 +0000853 // Deal with call operands first.
854 SmallVector<unsigned, 4> Args;
855 SmallVector<MVT, 4> ArgVTs;
856 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
857 Args.reserve(CS.arg_size());
858 ArgVTs.reserve(CS.arg_size());
859 ArgFlags.reserve(CS.arg_size());
860 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
861 i != e; ++i) {
862 unsigned Arg = getRegForValue(*i);
863 if (Arg == 0)
864 return false;
865 ISD::ArgFlagsTy Flags;
866 unsigned AttrInd = i - CS.arg_begin() + 1;
867 if (CS.paramHasAttr(AttrInd, ParamAttr::SExt))
868 Flags.setSExt();
869 if (CS.paramHasAttr(AttrInd, ParamAttr::ZExt))
870 Flags.setZExt();
871
872 // FIXME: Only handle *easy* calls for now.
873 if (CS.paramHasAttr(AttrInd, ParamAttr::InReg) ||
874 CS.paramHasAttr(AttrInd, ParamAttr::StructRet) ||
875 CS.paramHasAttr(AttrInd, ParamAttr::Nest) ||
876 CS.paramHasAttr(AttrInd, ParamAttr::ByVal))
877 return false;
878
879 const Type *ArgTy = (*i)->getType();
880 MVT ArgVT;
881 if (!isTypeLegal(ArgTy, TLI, ArgVT))
882 return false;
883 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
884 Flags.setOrigAlign(OriginalAlignment);
885
886 Args.push_back(Arg);
887 ArgVTs.push_back(ArgVT);
888 ArgFlags.push_back(Flags);
889 }
890
891 // Analyze operands of the call, assigning locations to each operand.
892 SmallVector<CCValAssign, 16> ArgLocs;
893 CCState CCInfo(CC, false, TM, ArgLocs);
894 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
895
896 // Get a count of how many bytes are to be pushed on the stack.
897 unsigned NumBytes = CCInfo.getNextStackOffset();
898
899 // Issue CALLSEQ_START
900 BuildMI(MBB, TII.get(X86::ADJCALLSTACKDOWN)).addImm(NumBytes);
901
902 // Process argumenet: walk the register/memloc assignments, inserting
903 // copies / loads.
904 SmallVector<unsigned, 4> RegArgs;
905 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
906 CCValAssign &VA = ArgLocs[i];
907 unsigned Arg = Args[VA.getValNo()];
908 MVT ArgVT = ArgVTs[VA.getValNo()];
909
910 // Promote the value if needed.
911 switch (VA.getLocInfo()) {
912 default: assert(0 && "Unknown loc info!");
913 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000914 case CCValAssign::SExt: {
915 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
916 Arg, ArgVT, Arg);
917 assert(Emitted && "Failed to emit a sext!");
918 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000919 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000920 }
921 case CCValAssign::ZExt: {
922 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
923 Arg, ArgVT, Arg);
924 assert(Emitted && "Failed to emit a zext!");
925 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000926 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000927 }
928 case CCValAssign::AExt: {
929 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
930 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +0000931 if (!Emitted)
932 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
933 Arg, ArgVT, Arg);
934 if (!Emitted)
935 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
936 Arg, ArgVT, Arg);
937
Evan Cheng24e3a902008-09-08 06:35:17 +0000938 assert(Emitted && "Failed to emit a aext!");
939 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000940 break;
941 }
Evan Cheng24e3a902008-09-08 06:35:17 +0000942 }
Evan Chengf3d4efe2008-09-07 09:09:33 +0000943
944 if (VA.isRegLoc()) {
945 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
946 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
947 Arg, RC, RC);
948 assert(Emitted && "Failed to emit a copy instruction!");
949 RegArgs.push_back(VA.getLocReg());
950 } else {
951 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +0000952 X86AddressMode AM;
953 AM.Base.Reg = StackPtr;
954 AM.Disp = LocMemOffset;
955 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000956 }
957 }
958
959 // Issue the call.
960 unsigned CallOpc = CalleeOp
961 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
962 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
963 MachineInstrBuilder MIB = CalleeOp
964 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000965 : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000966 // Add implicit physical register uses to the call.
967 while (!RegArgs.empty()) {
968 MIB.addReg(RegArgs.back());
969 RegArgs.pop_back();
970 }
971
972 // Issue CALLSEQ_END
973 BuildMI(MBB, TII.get(X86::ADJCALLSTACKUP)).addImm(NumBytes).addImm(0);
974
975 // Now handle call return value (if any).
Evan Chengf3d4efe2008-09-07 09:09:33 +0000976 if (RetVT.getSimpleVT() != MVT::isVoid) {
977 SmallVector<CCValAssign, 16> RVLocs;
978 CCState CCInfo(CC, false, TM, RVLocs);
979 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
980
981 // Copy all of the result registers out of their specified physreg.
982 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
983 MVT CopyVT = RVLocs[0].getValVT();
984 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
985 TargetRegisterClass *SrcRC = DstRC;
986
987 // If this is a call to a function that returns an fp value on the x87 fp
988 // stack, but where we prefer to use the value in xmm registers, copy it
989 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
990 if ((RVLocs[0].getLocReg() == X86::ST0 ||
991 RVLocs[0].getLocReg() == X86::ST1) &&
992 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
993 CopyVT = MVT::f80;
994 SrcRC = X86::RSTRegisterClass;
995 DstRC = X86::RFP80RegisterClass;
996 }
997
998 unsigned ResultReg = createResultReg(DstRC);
999 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1000 RVLocs[0].getLocReg(), DstRC, SrcRC);
1001 assert(Emitted && "Failed to emit a copy instruction!");
1002 if (CopyVT != RVLocs[0].getValVT()) {
1003 // Round the F80 the right size, which also moves to the appropriate xmm
1004 // register. This is accomplished by storing the F80 value in memory and
1005 // then loading it back. Ewww...
1006 MVT ResVT = RVLocs[0].getValVT();
1007 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1008 unsigned MemSize = ResVT.getSizeInBits()/8;
Dan Gohman0586d912008-09-10 20:11:02 +00001009 int FI = MFI.CreateStackObject(MemSize, MemSize);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001010 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
1011 DstRC = ResVT == MVT::f32
1012 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1013 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1014 ResultReg = createResultReg(DstRC);
1015 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
1016 }
1017
Evan Chengdebdea02008-09-08 17:15:42 +00001018 if (AndToI1) {
1019 // Mask out all but lowest bit for some call which produces an i1.
1020 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
1021 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
1022 ResultReg = AndResult;
1023 }
1024
Evan Chengf3d4efe2008-09-07 09:09:33 +00001025 UpdateValueMap(I, ResultReg);
1026 }
1027
1028 return true;
1029}
1030
1031
Dan Gohman99b21822008-08-28 23:21:34 +00001032bool
Dan Gohman3df24e62008-09-03 23:12:08 +00001033X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001034 switch (I->getOpcode()) {
1035 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001036 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001037 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001038 case Instruction::Store:
1039 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001040 case Instruction::ICmp:
1041 case Instruction::FCmp:
1042 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001043 case Instruction::ZExt:
1044 return X86SelectZExt(I);
1045 case Instruction::Br:
1046 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001047 case Instruction::Call:
1048 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001049 case Instruction::LShr:
1050 case Instruction::AShr:
1051 case Instruction::Shl:
1052 return X86SelectShift(I);
1053 case Instruction::Select:
1054 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001055 case Instruction::Trunc:
1056 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001057 case Instruction::FPExt:
1058 return X86SelectFPExt(I);
1059 case Instruction::FPTrunc:
1060 return X86SelectFPTrunc(I);
Dan Gohman99b21822008-08-28 23:21:34 +00001061 }
1062
1063 return false;
1064}
1065
Dan Gohman0586d912008-09-10 20:11:02 +00001066unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Owen Anderson95267a12008-09-05 00:06:23 +00001067 // Can't handle PIC-mode yet.
1068 if (TM.getRelocationModel() == Reloc::PIC_)
1069 return 0;
1070
Evan Cheng59fbc802008-09-09 01:26:59 +00001071 MVT VT;
1072 if (!isTypeLegal(C->getType(), TLI, VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001073 return false;
1074
1075 // Get opcode and regclass of the output for the given load instruction.
1076 unsigned Opc = 0;
1077 const TargetRegisterClass *RC = NULL;
1078 switch (VT.getSimpleVT()) {
1079 default: return false;
1080 case MVT::i8:
1081 Opc = X86::MOV8rm;
1082 RC = X86::GR8RegisterClass;
1083 break;
1084 case MVT::i16:
1085 Opc = X86::MOV16rm;
1086 RC = X86::GR16RegisterClass;
1087 break;
1088 case MVT::i32:
1089 Opc = X86::MOV32rm;
1090 RC = X86::GR32RegisterClass;
1091 break;
1092 case MVT::i64:
1093 // Must be in x86-64 mode.
1094 Opc = X86::MOV64rm;
1095 RC = X86::GR64RegisterClass;
1096 break;
1097 case MVT::f32:
1098 if (Subtarget->hasSSE1()) {
1099 Opc = X86::MOVSSrm;
1100 RC = X86::FR32RegisterClass;
1101 } else {
1102 Opc = X86::LD_Fp32m;
1103 RC = X86::RFP32RegisterClass;
1104 }
1105 break;
1106 case MVT::f64:
1107 if (Subtarget->hasSSE2()) {
1108 Opc = X86::MOVSDrm;
1109 RC = X86::FR64RegisterClass;
1110 } else {
1111 Opc = X86::LD_Fp64m;
1112 RC = X86::RFP64RegisterClass;
1113 }
1114 break;
1115 case MVT::f80:
1116 Opc = X86::LD_Fp80m;
1117 RC = X86::RFP80RegisterClass;
1118 break;
1119 }
1120
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001121 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001122 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001123 X86AddressMode AM;
1124 if (X86SelectAddress(C, AM, false)) {
1125 if (TLI.getPointerTy() == MVT::i32)
1126 Opc = X86::LEA32r;
1127 else
1128 Opc = X86::LEA64r;
1129 unsigned ResultReg = createResultReg(RC);
1130 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001131 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001132 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001133 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001134 }
1135
Owen Anderson3b217c62008-09-06 01:11:01 +00001136 // MachineConstantPool wants an explicit alignment.
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001137 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001138 if (Align == 0) {
1139 // Alignment of vector types. FIXME!
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001140 Align = TD.getABITypeSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001141 Align = Log2_64(Align);
1142 }
Owen Anderson95267a12008-09-05 00:06:23 +00001143
Dan Gohman0586d912008-09-10 20:11:02 +00001144 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001145 unsigned ResultReg = createResultReg(RC);
Owen Anderson95267a12008-09-05 00:06:23 +00001146 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset);
Owen Anderson95267a12008-09-05 00:06:23 +00001147 return ResultReg;
1148}
1149
Dan Gohman0586d912008-09-10 20:11:02 +00001150unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
1151 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001152 if (!X86SelectAddress(C, AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +00001153 return 0;
1154 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1155 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1156 unsigned ResultReg = createResultReg(RC);
1157 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1158 return ResultReg;
1159}
1160
Evan Chengc3f44b02008-09-03 00:03:49 +00001161namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001162 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
1163 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001164 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
1165 DenseMap<const AllocaInst *, int> &am) {
1166 return new X86FastISel(mf, vm, bm, am);
Evan Chengc3f44b02008-09-03 00:03:49 +00001167 }
Dan Gohman99b21822008-08-28 23:21:34 +00001168}