blob: b53e042edcb51c6e305d667e524c424170025bd4 [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,
Dan Gohmand57dd5f2008-09-23 21:53:34 +000052 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +000053 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +000054 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
55 DenseMap<const AllocaInst *, int> &am)
Dan Gohmand57dd5f2008-09-23 21:53:34 +000056 : FastISel(mf, mmi, vm, bm, am) {
Evan Cheng88e30412008-09-03 01:04:47 +000057 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000058 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
59 X86ScalarSSEf64 = Subtarget->hasSSE2();
60 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000061 }
Evan Chengc3f44b02008-09-03 00:03:49 +000062
Dan Gohman3df24e62008-09-03 23:12:08 +000063 virtual bool TargetSelectInstruction(Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000064
Dan Gohman1adf1b02008-08-19 21:45:35 +000065#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000066
67private:
Dan Gohman0586d912008-09-10 20:11:02 +000068 bool X86FastEmitLoad(MVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000069
Evan Chengf3d4efe2008-09-07 09:09:33 +000070 bool X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000071 const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000072
73 bool X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT, unsigned Src, MVT SrcVT,
74 unsigned &ResultReg);
Evan Cheng0de588f2008-09-05 21:00:03 +000075
Dan Gohman2ff7fd12008-09-19 22:16:54 +000076 bool X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall);
Dan Gohman0586d912008-09-10 20:11:02 +000077
Dan Gohman3df24e62008-09-03 23:12:08 +000078 bool X86SelectLoad(Instruction *I);
Owen Andersona3971df2008-09-04 07:08:58 +000079
80 bool X86SelectStore(Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000081
82 bool X86SelectCmp(Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000083
84 bool X86SelectZExt(Instruction *I);
85
86 bool X86SelectBranch(Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +000087
88 bool X86SelectShift(Instruction *I);
89
90 bool X86SelectSelect(Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +000091
Evan Cheng10a8d9c2008-09-07 08:47:42 +000092 bool X86SelectTrunc(Instruction *I);
93
Dan Gohman78efce62008-09-10 21:02:08 +000094 bool X86SelectFPExt(Instruction *I);
95 bool X86SelectFPTrunc(Instruction *I);
96
Evan Chengf3d4efe2008-09-07 09:09:33 +000097 bool X86SelectCall(Instruction *I);
98
99 CCAssignFn *CCAssignFnForCall(unsigned CC, bool isTailCall = false);
100
Dan Gohman0586d912008-09-10 20:11:02 +0000101 unsigned TargetMaterializeConstant(Constant *C);
102
103 unsigned TargetMaterializeAlloca(AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000104
105 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
106 /// computed in an SSE register, not on the X87 floating point stack.
107 bool isScalarFPTypeInSSEReg(MVT VT) const {
108 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
109 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
110 }
111
Evan Chengc3f44b02008-09-03 00:03:49 +0000112};
Dan Gohman99b21822008-08-28 23:21:34 +0000113
Evan Chengdebdea02008-09-08 17:15:42 +0000114static bool isTypeLegal(const Type *Ty, const TargetLowering &TLI, MVT &VT,
115 bool AllowI1 = false) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000116 VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
117 if (VT == MVT::Other || !VT.isSimple())
118 // Unhandled type. Halt "fast" selection and bail.
119 return false;
120 if (VT == MVT::iPTR)
121 // Use pointer type.
122 VT = TLI.getPointerTy();
123 // We only handle legal types. For example, on x86-32 the instruction
124 // selector contains all of the 64-bit instructions from x86-64,
125 // under the assumption that i64 won't be used if the target doesn't
126 // support it.
Evan Chengdebdea02008-09-08 17:15:42 +0000127 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000128}
129
130#include "X86GenCallingConv.inc"
131
132/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
133/// convention.
134CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) {
135 if (Subtarget->is64Bit()) {
136 if (Subtarget->isTargetWin64())
137 return CC_X86_Win64_C;
138 else if (CC == CallingConv::Fast && isTaillCall)
139 return CC_X86_64_TailCall;
140 else
141 return CC_X86_64_C;
142 }
143
144 if (CC == CallingConv::X86_FastCall)
145 return CC_X86_32_FastCall;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000146 else if (CC == CallingConv::Fast)
147 return CC_X86_32_FastCC;
148 else
149 return CC_X86_32_C;
150}
151
Evan Cheng0de588f2008-09-05 21:00:03 +0000152/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000153/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000154/// Return true and the result register by reference if it is possible.
Dan Gohman0586d912008-09-10 20:11:02 +0000155bool X86FastISel::X86FastEmitLoad(MVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000156 unsigned &ResultReg) {
157 // Get opcode and regclass of the output for the given load instruction.
158 unsigned Opc = 0;
159 const TargetRegisterClass *RC = NULL;
160 switch (VT.getSimpleVT()) {
161 default: return false;
162 case MVT::i8:
163 Opc = X86::MOV8rm;
164 RC = X86::GR8RegisterClass;
165 break;
166 case MVT::i16:
167 Opc = X86::MOV16rm;
168 RC = X86::GR16RegisterClass;
169 break;
170 case MVT::i32:
171 Opc = X86::MOV32rm;
172 RC = X86::GR32RegisterClass;
173 break;
174 case MVT::i64:
175 // Must be in x86-64 mode.
176 Opc = X86::MOV64rm;
177 RC = X86::GR64RegisterClass;
178 break;
179 case MVT::f32:
180 if (Subtarget->hasSSE1()) {
181 Opc = X86::MOVSSrm;
182 RC = X86::FR32RegisterClass;
183 } else {
184 Opc = X86::LD_Fp32m;
185 RC = X86::RFP32RegisterClass;
186 }
187 break;
188 case MVT::f64:
189 if (Subtarget->hasSSE2()) {
190 Opc = X86::MOVSDrm;
191 RC = X86::FR64RegisterClass;
192 } else {
193 Opc = X86::LD_Fp64m;
194 RC = X86::RFP64RegisterClass;
195 }
196 break;
197 case MVT::f80:
198 Opc = X86::LD_Fp80m;
199 RC = X86::RFP80RegisterClass;
200 break;
201 }
202
203 ResultReg = createResultReg(RC);
Evan Cheng0de588f2008-09-05 21:00:03 +0000204 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
205 return true;
206}
207
Evan Chengf3d4efe2008-09-07 09:09:33 +0000208/// X86FastEmitStore - Emit a machine instruction to store a value Val of
209/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
210/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000211/// i.e. V. Return true if it is possible.
212bool
Evan Chengf3d4efe2008-09-07 09:09:33 +0000213X86FastISel::X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000214 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000215 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000216 unsigned Opc = 0;
217 const TargetRegisterClass *RC = NULL;
218 switch (VT.getSimpleVT()) {
219 default: return false;
220 case MVT::i8:
221 Opc = X86::MOV8mr;
222 RC = X86::GR8RegisterClass;
223 break;
224 case MVT::i16:
225 Opc = X86::MOV16mr;
226 RC = X86::GR16RegisterClass;
227 break;
228 case MVT::i32:
229 Opc = X86::MOV32mr;
230 RC = X86::GR32RegisterClass;
231 break;
232 case MVT::i64:
233 // Must be in x86-64 mode.
234 Opc = X86::MOV64mr;
235 RC = X86::GR64RegisterClass;
236 break;
237 case MVT::f32:
238 if (Subtarget->hasSSE1()) {
239 Opc = X86::MOVSSmr;
240 RC = X86::FR32RegisterClass;
241 } else {
242 Opc = X86::ST_Fp32m;
243 RC = X86::RFP32RegisterClass;
244 }
245 break;
246 case MVT::f64:
247 if (Subtarget->hasSSE2()) {
248 Opc = X86::MOVSDmr;
249 RC = X86::FR64RegisterClass;
250 } else {
251 Opc = X86::ST_Fp64m;
252 RC = X86::RFP64RegisterClass;
253 }
254 break;
255 case MVT::f80:
256 Opc = X86::ST_FP80m;
257 RC = X86::RFP80RegisterClass;
258 break;
259 }
260
Evan Chengf3d4efe2008-09-07 09:09:33 +0000261 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000262 return true;
263}
264
Evan Cheng24e3a902008-09-08 06:35:17 +0000265/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
266/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
267/// ISD::SIGN_EXTEND).
268bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT,
269 unsigned Src, MVT SrcVT,
270 unsigned &ResultReg) {
Owen Andersonac34a002008-09-11 19:44:55 +0000271 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
272
273 if (RR != 0) {
274 ResultReg = RR;
275 return true;
276 } else
277 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000278}
279
Dan Gohman0586d912008-09-10 20:11:02 +0000280/// X86SelectAddress - Attempt to fill in an address from the given value.
281///
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000282bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall) {
Dan Gohman35893082008-09-18 23:23:44 +0000283 User *U;
284 unsigned Opcode = Instruction::UserOp1;
285 if (Instruction *I = dyn_cast<Instruction>(V)) {
286 Opcode = I->getOpcode();
287 U = I;
288 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
289 Opcode = C->getOpcode();
290 U = C;
291 }
Dan Gohman0586d912008-09-10 20:11:02 +0000292
Dan Gohman35893082008-09-18 23:23:44 +0000293 switch (Opcode) {
294 default: break;
295 case Instruction::BitCast:
296 // Look past bitcasts.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000297 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000298
299 case Instruction::IntToPtr:
300 // Look past no-op inttoptrs.
301 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000302 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000303
304 case Instruction::PtrToInt:
305 // Look past no-op ptrtoints.
306 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000307 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000308
309 case Instruction::Alloca: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000310 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000311 // Do static allocas.
312 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman0586d912008-09-10 20:11:02 +0000313 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
314 if (SI == StaticAllocaMap.end())
315 return false;
316 AM.BaseType = X86AddressMode::FrameIndexBase;
317 AM.Base.FrameIndex = SI->second;
Dan Gohman35893082008-09-18 23:23:44 +0000318 return true;
319 }
320
321 case Instruction::Add: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000322 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000323 // Adds of constants are common and easy enough.
324 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
325 AM.Disp += CI->getZExtValue();
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000326 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman0586d912008-09-10 20:11:02 +0000327 }
Dan Gohman35893082008-09-18 23:23:44 +0000328 break;
329 }
330
331 case Instruction::GetElementPtr: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000332 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000333 // Pattern-match simple GEPs.
334 uint64_t Disp = AM.Disp;
335 unsigned IndexReg = AM.IndexReg;
336 unsigned Scale = AM.Scale;
337 gep_type_iterator GTI = gep_type_begin(U);
338 // Look at all but the last index. Constants can be folded,
339 // and one dynamic index can be handled, if the scale is supported.
340 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
341 i != e; ++i, ++GTI) {
342 Value *Op = *i;
343 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
344 const StructLayout *SL = TD.getStructLayout(STy);
345 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
346 Disp += SL->getElementOffset(Idx);
347 } else {
348 uint64_t S = TD.getABITypeSize(GTI.getIndexedType());
349 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
350 // Constant-offset addressing.
351 Disp += CI->getZExtValue() * S;
352 } else if (IndexReg == 0 &&
353 (S == 1 || S == 2 || S == 4 || S == 8)) {
354 // Scaled-index addressing.
355 Scale = S;
356 IndexReg = getRegForValue(Op);
357 if (IndexReg == 0)
358 return false;
359 } else
360 // Unsupported.
361 goto unsupported_gep;
362 }
363 }
364 // Ok, the GEP indices were covered by constant-offset and scaled-index
365 // addressing. Update the address state and move on to examining the base.
366 AM.IndexReg = IndexReg;
367 AM.Scale = Scale;
368 AM.Disp = Disp;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000369 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000370 unsupported_gep:
371 // Ok, the GEP indices weren't all covered.
372 break;
373 }
374 }
375
376 // Handle constant address.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000377 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
378 if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) {
379 // Check to see if we've already materialized this
380 // value in a register in this block.
Dan Gohman7e8ef602008-09-19 23:42:04 +0000381 if (unsigned Reg = LocalValueMap[V]) {
382 AM.Base.Reg = Reg;
383 return true;
384 }
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000385 // Issue load from stub if necessary.
386 unsigned Opc = 0;
387 const TargetRegisterClass *RC = NULL;
388 if (TLI.getPointerTy() == MVT::i32) {
389 Opc = X86::MOV32rm;
390 RC = X86::GR32RegisterClass;
391 } else {
392 Opc = X86::MOV64rm;
393 RC = X86::GR64RegisterClass;
394 }
395 AM.Base.Reg = createResultReg(RC);
396 X86AddressMode LocalAM;
397 LocalAM.GV = GV;
398 addFullAddress(BuildMI(MBB, TII.get(Opc), AM.Base.Reg), LocalAM);
399 // Prevent loading GV stub multiple times in same MBB.
400 LocalValueMap[V] = AM.Base.Reg;
401 } else {
402 AM.GV = GV;
403 }
404 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000405 }
406
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000407 // If all else fails, just materialize the value in a register.
408 AM.Base.Reg = getRegForValue(V);
409 return AM.Base.Reg != 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000410}
411
Owen Andersona3971df2008-09-04 07:08:58 +0000412/// X86SelectStore - Select and emit code to implement store instructions.
413bool X86FastISel::X86SelectStore(Instruction* I) {
Evan Cheng24e3a902008-09-08 06:35:17 +0000414 MVT VT;
415 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
Owen Andersona3971df2008-09-04 07:08:58 +0000416 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000417 unsigned Val = getRegForValue(I->getOperand(0));
418 if (Val == 0)
Owen Andersona3971df2008-09-04 07:08:58 +0000419 // Unhandled operand. Halt "fast" selection and bail.
420 return false;
421
Dan Gohman0586d912008-09-10 20:11:02 +0000422 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000423 if (!X86SelectAddress(I->getOperand(1), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000424 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000425
Dan Gohman0586d912008-09-10 20:11:02 +0000426 return X86FastEmitStore(VT, Val, AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000427}
428
Evan Cheng8b19e562008-09-03 06:44:39 +0000429/// X86SelectLoad - Select and emit code to implement load instructions.
430///
Dan Gohman3df24e62008-09-03 23:12:08 +0000431bool X86FastISel::X86SelectLoad(Instruction *I) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000432 MVT VT;
433 if (!isTypeLegal(I->getType(), TLI, VT))
Evan Cheng8b19e562008-09-03 06:44:39 +0000434 return false;
435
Dan Gohman0586d912008-09-10 20:11:02 +0000436 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000437 if (!X86SelectAddress(I->getOperand(0), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000438 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000439
Evan Cheng0de588f2008-09-05 21:00:03 +0000440 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000441 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000442 UpdateValueMap(I, ResultReg);
443 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000444 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000445 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000446}
447
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000448bool X86FastISel::X86SelectCmp(Instruction *I) {
449 CmpInst *CI = cast<CmpInst>(I);
450
Dan Gohman4f22bb02008-09-05 01:33:56 +0000451 MVT VT = TLI.getValueType(I->getOperand(0)->getType());
452 if (!TLI.isTypeLegal(VT))
453 return false;
454
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000455 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000456 if (Op0Reg == 0) return false;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000457 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000458 if (Op1Reg == 0) return false;
459
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000460 unsigned Opc;
Dan Gohmanf52550b2008-09-05 01:15:35 +0000461 switch (VT.getSimpleVT()) {
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000462 case MVT::i8: Opc = X86::CMP8rr; break;
463 case MVT::i16: Opc = X86::CMP16rr; break;
464 case MVT::i32: Opc = X86::CMP32rr; break;
465 case MVT::i64: Opc = X86::CMP64rr; break;
466 case MVT::f32: Opc = X86::UCOMISSrr; break;
467 case MVT::f64: Opc = X86::UCOMISDrr; break;
468 default: return false;
469 }
470
471 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
472 switch (CI->getPredicate()) {
473 case CmpInst::FCMP_OEQ: {
474 unsigned EReg = createResultReg(&X86::GR8RegClass);
475 unsigned NPReg = createResultReg(&X86::GR8RegClass);
476 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
477 BuildMI(MBB, TII.get(X86::SETEr), EReg);
478 BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
479 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
480 break;
481 }
482 case CmpInst::FCMP_UNE: {
483 unsigned NEReg = createResultReg(&X86::GR8RegClass);
484 unsigned PReg = createResultReg(&X86::GR8RegClass);
485 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
486 BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
487 BuildMI(MBB, TII.get(X86::SETPr), PReg);
488 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
489 break;
490 }
491 case CmpInst::FCMP_OGT:
492 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
493 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
494 break;
495 case CmpInst::FCMP_OGE:
496 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
497 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
498 break;
499 case CmpInst::FCMP_OLT:
500 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
501 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
502 break;
503 case CmpInst::FCMP_OLE:
504 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
505 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
506 break;
507 case CmpInst::FCMP_ONE:
508 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
509 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
510 break;
511 case CmpInst::FCMP_ORD:
512 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
513 BuildMI(MBB, TII.get(X86::SETNPr), ResultReg);
514 break;
515 case CmpInst::FCMP_UNO:
516 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
517 BuildMI(MBB, TII.get(X86::SETPr), ResultReg);
518 break;
519 case CmpInst::FCMP_UEQ:
520 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
521 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
522 break;
523 case CmpInst::FCMP_UGT:
524 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
525 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
526 break;
527 case CmpInst::FCMP_UGE:
528 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
529 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
530 break;
531 case CmpInst::FCMP_ULT:
532 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
533 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
534 break;
535 case CmpInst::FCMP_ULE:
536 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
537 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
538 break;
539 case CmpInst::ICMP_EQ:
540 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
541 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
542 break;
543 case CmpInst::ICMP_NE:
544 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
545 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
546 break;
547 case CmpInst::ICMP_UGT:
548 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
549 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
550 break;
551 case CmpInst::ICMP_UGE:
552 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
553 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
554 break;
555 case CmpInst::ICMP_ULT:
556 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
557 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
558 break;
559 case CmpInst::ICMP_ULE:
560 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
561 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
562 break;
563 case CmpInst::ICMP_SGT:
564 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
565 BuildMI(MBB, TII.get(X86::SETGr), ResultReg);
566 break;
567 case CmpInst::ICMP_SGE:
568 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
569 BuildMI(MBB, TII.get(X86::SETGEr), ResultReg);
570 break;
571 case CmpInst::ICMP_SLT:
572 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
573 BuildMI(MBB, TII.get(X86::SETLr), ResultReg);
574 break;
575 case CmpInst::ICMP_SLE:
576 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
577 BuildMI(MBB, TII.get(X86::SETLEr), ResultReg);
578 break;
579 default:
580 return false;
581 }
582
583 UpdateValueMap(I, ResultReg);
584 return true;
585}
Evan Cheng8b19e562008-09-03 06:44:39 +0000586
Dan Gohmand89ae992008-09-05 01:06:14 +0000587bool X86FastISel::X86SelectZExt(Instruction *I) {
588 // Special-case hack: The only i1 values we know how to produce currently
589 // set the upper bits of an i8 value to zero.
590 if (I->getType() == Type::Int8Ty &&
591 I->getOperand(0)->getType() == Type::Int1Ty) {
592 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000593 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000594 UpdateValueMap(I, ResultReg);
595 return true;
596 }
597
598 return false;
599}
600
601bool X86FastISel::X86SelectBranch(Instruction *I) {
602 BranchInst *BI = cast<BranchInst>(I);
603 // Unconditional branches are selected by tablegen-generated code.
604 unsigned OpReg = getRegForValue(BI->getCondition());
Dan Gohmanf52550b2008-09-05 01:15:35 +0000605 if (OpReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000606 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
607 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
608
609 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
610 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
611 BuildMI(MBB, TII.get(X86::JMP)).addMBB(FalseMBB);
612
613 MBB->addSuccessor(TrueMBB);
614 MBB->addSuccessor(FalseMBB);
615
616 return true;
617}
618
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000619bool X86FastISel::X86SelectShift(Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000620 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000621 const TargetRegisterClass *RC = NULL;
622 if (I->getType() == Type::Int8Ty) {
623 CReg = X86::CL;
624 RC = &X86::GR8RegClass;
625 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000626 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
627 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
628 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000629 default: return false;
630 }
631 } else if (I->getType() == Type::Int16Ty) {
632 CReg = X86::CX;
633 RC = &X86::GR16RegClass;
634 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000635 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
636 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
637 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000638 default: return false;
639 }
640 } else if (I->getType() == Type::Int32Ty) {
641 CReg = X86::ECX;
642 RC = &X86::GR32RegClass;
643 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000644 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
645 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
646 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000647 default: return false;
648 }
649 } else if (I->getType() == Type::Int64Ty) {
650 CReg = X86::RCX;
651 RC = &X86::GR64RegClass;
652 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000653 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
654 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
655 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000656 default: return false;
657 }
658 } else {
659 return false;
660 }
661
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000662 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
663 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
664 return false;
665
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000666 unsigned Op0Reg = getRegForValue(I->getOperand(0));
667 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +0000668
669 // Fold immediate in shl(x,3).
670 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
671 unsigned ResultReg = createResultReg(RC);
672 BuildMI(MBB, TII.get(OpImm),
673 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue());
674 UpdateValueMap(I, ResultReg);
675 return true;
676 }
677
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000678 unsigned Op1Reg = getRegForValue(I->getOperand(1));
679 if (Op1Reg == 0) return false;
680 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
681 unsigned ResultReg = createResultReg(RC);
Chris Lattner743922e2008-09-21 21:44:29 +0000682 BuildMI(MBB, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000683 UpdateValueMap(I, ResultReg);
684 return true;
685}
686
687bool X86FastISel::X86SelectSelect(Instruction *I) {
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000688 const Type *Ty = I->getType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000689 if (isa<PointerType>(Ty))
Dan Gohman1fbc3cd2008-09-18 18:26:43 +0000690 Ty = TD.getIntPtrType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000691
692 unsigned Opc = 0;
693 const TargetRegisterClass *RC = NULL;
694 if (Ty == Type::Int16Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000695 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000696 RC = &X86::GR16RegClass;
697 } else if (Ty == Type::Int32Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000698 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000699 RC = &X86::GR32RegClass;
700 } else if (Ty == Type::Int64Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000701 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000702 RC = &X86::GR64RegClass;
703 } else {
704 return false;
705 }
706
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000707 MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
708 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
709 return false;
710
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000711 unsigned Op0Reg = getRegForValue(I->getOperand(0));
712 if (Op0Reg == 0) return false;
713 unsigned Op1Reg = getRegForValue(I->getOperand(1));
714 if (Op1Reg == 0) return false;
715 unsigned Op2Reg = getRegForValue(I->getOperand(2));
716 if (Op2Reg == 0) return false;
717
718 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
719 unsigned ResultReg = createResultReg(RC);
720 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
721 UpdateValueMap(I, ResultReg);
722 return true;
723}
724
Dan Gohman78efce62008-09-10 21:02:08 +0000725bool X86FastISel::X86SelectFPExt(Instruction *I) {
726 if (Subtarget->hasSSE2()) {
727 if (I->getType() == Type::DoubleTy) {
728 Value *V = I->getOperand(0);
729 if (V->getType() == Type::FloatTy) {
730 unsigned OpReg = getRegForValue(V);
731 if (OpReg == 0) return false;
732 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
733 BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
734 UpdateValueMap(I, ResultReg);
735 return true;
736 }
737 }
738 }
739
740 return false;
741}
742
743bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
744 if (Subtarget->hasSSE2()) {
745 if (I->getType() == Type::FloatTy) {
746 Value *V = I->getOperand(0);
747 if (V->getType() == Type::DoubleTy) {
748 unsigned OpReg = getRegForValue(V);
749 if (OpReg == 0) return false;
750 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
751 BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
752 UpdateValueMap(I, ResultReg);
753 return true;
754 }
755 }
756 }
757
758 return false;
759}
760
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000761bool X86FastISel::X86SelectTrunc(Instruction *I) {
762 if (Subtarget->is64Bit())
763 // All other cases should be handled by the tblgen generated code.
764 return false;
765 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
766 MVT DstVT = TLI.getValueType(I->getType());
767 if (DstVT != MVT::i8)
768 // All other cases should be handled by the tblgen generated code.
769 return false;
770 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
771 // All other cases should be handled by the tblgen generated code.
772 return false;
773
774 unsigned InputReg = getRegForValue(I->getOperand(0));
775 if (!InputReg)
776 // Unhandled operand. Halt "fast" selection and bail.
777 return false;
778
779 // First issue a copy to GR16_ or GR32_.
780 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
781 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
782 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
783 unsigned CopyReg = createResultReg(CopyRC);
784 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
785
786 // Then issue an extract_subreg.
787 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg,1); // x86_subreg_8bit
788 if (!ResultReg)
789 return false;
790
791 UpdateValueMap(I, ResultReg);
792 return true;
793}
794
Evan Chengf3d4efe2008-09-07 09:09:33 +0000795bool X86FastISel::X86SelectCall(Instruction *I) {
796 CallInst *CI = cast<CallInst>(I);
797 Value *Callee = I->getOperand(0);
798
799 // Can't handle inline asm yet.
800 if (isa<InlineAsm>(Callee))
801 return false;
802
803 // FIXME: Handle some intrinsics.
804 if (Function *F = CI->getCalledFunction()) {
805 if (F->isDeclaration() &&F->getIntrinsicID())
806 return false;
807 }
808
Evan Chengf3d4efe2008-09-07 09:09:33 +0000809 // Handle only C and fastcc calling conventions for now.
810 CallSite CS(CI);
811 unsigned CC = CS.getCallingConv();
812 if (CC != CallingConv::C &&
813 CC != CallingConv::Fast &&
814 CC != CallingConv::X86_FastCall)
815 return false;
816
817 // Let SDISel handle vararg functions.
818 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
819 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
820 if (FTy->isVarArg())
821 return false;
822
823 // Handle *simple* calls for now.
824 const Type *RetTy = CS.getType();
825 MVT RetVT;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000826 if (RetTy == Type::VoidTy)
827 RetVT = MVT::isVoid;
828 else if (!isTypeLegal(RetTy, TLI, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000829 return false;
830
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000831 // Materialize callee address in a register. FIXME: GV address can be
832 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000833 X86AddressMode CalleeAM;
834 if (!X86SelectAddress(Callee, CalleeAM, true))
835 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000836 unsigned CalleeOp = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000837 GlobalValue *GV = 0;
838 if (CalleeAM.Base.Reg != 0) {
839 assert(CalleeAM.GV == 0);
840 CalleeOp = CalleeAM.Base.Reg;
841 } else if (CalleeAM.GV != 0) {
842 assert(CalleeAM.GV != 0);
843 GV = CalleeAM.GV;
844 } else
845 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000846
Evan Chengdebdea02008-09-08 17:15:42 +0000847 // Allow calls which produce i1 results.
848 bool AndToI1 = false;
849 if (RetVT == MVT::i1) {
850 RetVT = MVT::i8;
851 AndToI1 = true;
852 }
853
Evan Chengf3d4efe2008-09-07 09:09:33 +0000854 // Deal with call operands first.
855 SmallVector<unsigned, 4> Args;
856 SmallVector<MVT, 4> ArgVTs;
857 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
858 Args.reserve(CS.arg_size());
859 ArgVTs.reserve(CS.arg_size());
860 ArgFlags.reserve(CS.arg_size());
861 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
862 i != e; ++i) {
863 unsigned Arg = getRegForValue(*i);
864 if (Arg == 0)
865 return false;
866 ISD::ArgFlagsTy Flags;
867 unsigned AttrInd = i - CS.arg_begin() + 1;
868 if (CS.paramHasAttr(AttrInd, ParamAttr::SExt))
869 Flags.setSExt();
870 if (CS.paramHasAttr(AttrInd, ParamAttr::ZExt))
871 Flags.setZExt();
872
873 // FIXME: Only handle *easy* calls for now.
874 if (CS.paramHasAttr(AttrInd, ParamAttr::InReg) ||
875 CS.paramHasAttr(AttrInd, ParamAttr::StructRet) ||
876 CS.paramHasAttr(AttrInd, ParamAttr::Nest) ||
877 CS.paramHasAttr(AttrInd, ParamAttr::ByVal))
878 return false;
879
880 const Type *ArgTy = (*i)->getType();
881 MVT ArgVT;
882 if (!isTypeLegal(ArgTy, TLI, ArgVT))
883 return false;
884 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
885 Flags.setOrigAlign(OriginalAlignment);
886
887 Args.push_back(Arg);
888 ArgVTs.push_back(ArgVT);
889 ArgFlags.push_back(Flags);
890 }
891
892 // Analyze operands of the call, assigning locations to each operand.
893 SmallVector<CCValAssign, 16> ArgLocs;
894 CCState CCInfo(CC, false, TM, ArgLocs);
895 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
896
897 // Get a count of how many bytes are to be pushed on the stack.
898 unsigned NumBytes = CCInfo.getNextStackOffset();
899
900 // Issue CALLSEQ_START
901 BuildMI(MBB, TII.get(X86::ADJCALLSTACKDOWN)).addImm(NumBytes);
902
903 // Process argumenet: walk the register/memloc assignments, inserting
904 // copies / loads.
905 SmallVector<unsigned, 4> RegArgs;
906 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
907 CCValAssign &VA = ArgLocs[i];
908 unsigned Arg = Args[VA.getValNo()];
909 MVT ArgVT = ArgVTs[VA.getValNo()];
910
911 // Promote the value if needed.
912 switch (VA.getLocInfo()) {
913 default: assert(0 && "Unknown loc info!");
914 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000915 case CCValAssign::SExt: {
916 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
917 Arg, ArgVT, Arg);
918 assert(Emitted && "Failed to emit a sext!");
919 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000920 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000921 }
922 case CCValAssign::ZExt: {
923 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
924 Arg, ArgVT, Arg);
925 assert(Emitted && "Failed to emit a zext!");
926 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000927 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000928 }
929 case CCValAssign::AExt: {
930 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
931 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +0000932 if (!Emitted)
933 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
934 Arg, ArgVT, Arg);
935 if (!Emitted)
936 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
937 Arg, ArgVT, Arg);
938
Evan Cheng24e3a902008-09-08 06:35:17 +0000939 assert(Emitted && "Failed to emit a aext!");
940 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000941 break;
942 }
Evan Cheng24e3a902008-09-08 06:35:17 +0000943 }
Evan Chengf3d4efe2008-09-07 09:09:33 +0000944
945 if (VA.isRegLoc()) {
946 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
947 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
948 Arg, RC, RC);
949 assert(Emitted && "Failed to emit a copy instruction!");
950 RegArgs.push_back(VA.getLocReg());
951 } else {
952 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +0000953 X86AddressMode AM;
954 AM.Base.Reg = StackPtr;
955 AM.Disp = LocMemOffset;
956 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000957 }
958 }
959
960 // Issue the call.
961 unsigned CallOpc = CalleeOp
962 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
963 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
964 MachineInstrBuilder MIB = CalleeOp
965 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000966 : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000967 // Add implicit physical register uses to the call.
968 while (!RegArgs.empty()) {
969 MIB.addReg(RegArgs.back());
970 RegArgs.pop_back();
971 }
972
973 // Issue CALLSEQ_END
974 BuildMI(MBB, TII.get(X86::ADJCALLSTACKUP)).addImm(NumBytes).addImm(0);
975
976 // Now handle call return value (if any).
Evan Chengf3d4efe2008-09-07 09:09:33 +0000977 if (RetVT.getSimpleVT() != MVT::isVoid) {
978 SmallVector<CCValAssign, 16> RVLocs;
979 CCState CCInfo(CC, false, TM, RVLocs);
980 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
981
982 // Copy all of the result registers out of their specified physreg.
983 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
984 MVT CopyVT = RVLocs[0].getValVT();
985 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
986 TargetRegisterClass *SrcRC = DstRC;
987
988 // If this is a call to a function that returns an fp value on the x87 fp
989 // stack, but where we prefer to use the value in xmm registers, copy it
990 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
991 if ((RVLocs[0].getLocReg() == X86::ST0 ||
992 RVLocs[0].getLocReg() == X86::ST1) &&
993 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
994 CopyVT = MVT::f80;
995 SrcRC = X86::RSTRegisterClass;
996 DstRC = X86::RFP80RegisterClass;
997 }
998
999 unsigned ResultReg = createResultReg(DstRC);
1000 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1001 RVLocs[0].getLocReg(), DstRC, SrcRC);
1002 assert(Emitted && "Failed to emit a copy instruction!");
1003 if (CopyVT != RVLocs[0].getValVT()) {
1004 // Round the F80 the right size, which also moves to the appropriate xmm
1005 // register. This is accomplished by storing the F80 value in memory and
1006 // then loading it back. Ewww...
1007 MVT ResVT = RVLocs[0].getValVT();
1008 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1009 unsigned MemSize = ResVT.getSizeInBits()/8;
Dan Gohman0586d912008-09-10 20:11:02 +00001010 int FI = MFI.CreateStackObject(MemSize, MemSize);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001011 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
1012 DstRC = ResVT == MVT::f32
1013 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1014 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1015 ResultReg = createResultReg(DstRC);
1016 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
1017 }
1018
Evan Chengdebdea02008-09-08 17:15:42 +00001019 if (AndToI1) {
1020 // Mask out all but lowest bit for some call which produces an i1.
1021 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
1022 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
1023 ResultReg = AndResult;
1024 }
1025
Evan Chengf3d4efe2008-09-07 09:09:33 +00001026 UpdateValueMap(I, ResultReg);
1027 }
1028
1029 return true;
1030}
1031
1032
Dan Gohman99b21822008-08-28 23:21:34 +00001033bool
Dan Gohman3df24e62008-09-03 23:12:08 +00001034X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001035 switch (I->getOpcode()) {
1036 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001037 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001038 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001039 case Instruction::Store:
1040 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001041 case Instruction::ICmp:
1042 case Instruction::FCmp:
1043 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001044 case Instruction::ZExt:
1045 return X86SelectZExt(I);
1046 case Instruction::Br:
1047 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001048 case Instruction::Call:
1049 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001050 case Instruction::LShr:
1051 case Instruction::AShr:
1052 case Instruction::Shl:
1053 return X86SelectShift(I);
1054 case Instruction::Select:
1055 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001056 case Instruction::Trunc:
1057 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001058 case Instruction::FPExt:
1059 return X86SelectFPExt(I);
1060 case Instruction::FPTrunc:
1061 return X86SelectFPTrunc(I);
Dan Gohman99b21822008-08-28 23:21:34 +00001062 }
1063
1064 return false;
1065}
1066
Dan Gohman0586d912008-09-10 20:11:02 +00001067unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Owen Anderson95267a12008-09-05 00:06:23 +00001068 // Can't handle PIC-mode yet.
1069 if (TM.getRelocationModel() == Reloc::PIC_)
1070 return 0;
1071
Evan Cheng59fbc802008-09-09 01:26:59 +00001072 MVT VT;
1073 if (!isTypeLegal(C->getType(), TLI, VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001074 return false;
1075
1076 // Get opcode and regclass of the output for the given load instruction.
1077 unsigned Opc = 0;
1078 const TargetRegisterClass *RC = NULL;
1079 switch (VT.getSimpleVT()) {
1080 default: return false;
1081 case MVT::i8:
1082 Opc = X86::MOV8rm;
1083 RC = X86::GR8RegisterClass;
1084 break;
1085 case MVT::i16:
1086 Opc = X86::MOV16rm;
1087 RC = X86::GR16RegisterClass;
1088 break;
1089 case MVT::i32:
1090 Opc = X86::MOV32rm;
1091 RC = X86::GR32RegisterClass;
1092 break;
1093 case MVT::i64:
1094 // Must be in x86-64 mode.
1095 Opc = X86::MOV64rm;
1096 RC = X86::GR64RegisterClass;
1097 break;
1098 case MVT::f32:
1099 if (Subtarget->hasSSE1()) {
1100 Opc = X86::MOVSSrm;
1101 RC = X86::FR32RegisterClass;
1102 } else {
1103 Opc = X86::LD_Fp32m;
1104 RC = X86::RFP32RegisterClass;
1105 }
1106 break;
1107 case MVT::f64:
1108 if (Subtarget->hasSSE2()) {
1109 Opc = X86::MOVSDrm;
1110 RC = X86::FR64RegisterClass;
1111 } else {
1112 Opc = X86::LD_Fp64m;
1113 RC = X86::RFP64RegisterClass;
1114 }
1115 break;
1116 case MVT::f80:
1117 Opc = X86::LD_Fp80m;
1118 RC = X86::RFP80RegisterClass;
1119 break;
1120 }
1121
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001122 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001123 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001124 X86AddressMode AM;
1125 if (X86SelectAddress(C, AM, false)) {
1126 if (TLI.getPointerTy() == MVT::i32)
1127 Opc = X86::LEA32r;
1128 else
1129 Opc = X86::LEA64r;
1130 unsigned ResultReg = createResultReg(RC);
1131 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001132 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001133 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001134 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001135 }
1136
Owen Anderson3b217c62008-09-06 01:11:01 +00001137 // MachineConstantPool wants an explicit alignment.
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001138 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001139 if (Align == 0) {
1140 // Alignment of vector types. FIXME!
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001141 Align = TD.getABITypeSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001142 Align = Log2_64(Align);
1143 }
Owen Anderson95267a12008-09-05 00:06:23 +00001144
Dan Gohman0586d912008-09-10 20:11:02 +00001145 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001146 unsigned ResultReg = createResultReg(RC);
Owen Anderson95267a12008-09-05 00:06:23 +00001147 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset);
Owen Anderson95267a12008-09-05 00:06:23 +00001148 return ResultReg;
1149}
1150
Dan Gohman0586d912008-09-10 20:11:02 +00001151unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
1152 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001153 if (!X86SelectAddress(C, AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +00001154 return 0;
1155 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1156 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1157 unsigned ResultReg = createResultReg(RC);
1158 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1159 return ResultReg;
1160}
1161
Evan Chengc3f44b02008-09-03 00:03:49 +00001162namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001163 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001164 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +00001165 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001166 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
1167 DenseMap<const AllocaInst *, int> &am) {
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001168 return new X86FastISel(mf, mmi, vm, bm, am);
Evan Chengc3f44b02008-09-03 00:03:49 +00001169 }
Dan Gohman99b21822008-08-28 23:21:34 +00001170}