blob: b7ff7494c058fb2a56eb20ea13251a6772daf434 [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
Dan Gohman2cc3aa42008-09-25 15:24:26 +000043 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
44 /// base register.
45 unsigned GlobalBaseReg;
46
Evan Chengf3d4efe2008-09-07 09:09:33 +000047 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
48 /// floating point ops.
49 /// When SSE is available, use it for f32 operations.
50 /// When SSE2 is available, use it for f64 operations.
51 bool X86ScalarSSEf64;
52 bool X86ScalarSSEf32;
53
Evan Cheng8b19e562008-09-03 06:44:39 +000054public:
Dan Gohman3df24e62008-09-03 23:12:08 +000055 explicit X86FastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +000056 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +000057 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +000058 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
59 DenseMap<const AllocaInst *, int> &am)
Dan Gohmand57dd5f2008-09-23 21:53:34 +000060 : FastISel(mf, mmi, vm, bm, am) {
Evan Cheng88e30412008-09-03 01:04:47 +000061 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000062 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Dan Gohman2cc3aa42008-09-25 15:24:26 +000063 GlobalBaseReg = 0;
Evan Chengf3d4efe2008-09-07 09:09:33 +000064 X86ScalarSSEf64 = Subtarget->hasSSE2();
65 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000066 }
Evan Chengc3f44b02008-09-03 00:03:49 +000067
Dan Gohman3df24e62008-09-03 23:12:08 +000068 virtual bool TargetSelectInstruction(Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000069
Dan Gohman1adf1b02008-08-19 21:45:35 +000070#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000071
72private:
Dan Gohman0586d912008-09-10 20:11:02 +000073 bool X86FastEmitLoad(MVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000074
Evan Chengf3d4efe2008-09-07 09:09:33 +000075 bool X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000076 const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000077
78 bool X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT, unsigned Src, MVT SrcVT,
79 unsigned &ResultReg);
Evan Cheng0de588f2008-09-05 21:00:03 +000080
Dan Gohman2ff7fd12008-09-19 22:16:54 +000081 bool X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall);
Dan Gohman0586d912008-09-10 20:11:02 +000082
Dan Gohman3df24e62008-09-03 23:12:08 +000083 bool X86SelectLoad(Instruction *I);
Owen Andersona3971df2008-09-04 07:08:58 +000084
85 bool X86SelectStore(Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000086
87 bool X86SelectCmp(Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000088
89 bool X86SelectZExt(Instruction *I);
90
91 bool X86SelectBranch(Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +000092
93 bool X86SelectShift(Instruction *I);
94
95 bool X86SelectSelect(Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +000096
Evan Cheng10a8d9c2008-09-07 08:47:42 +000097 bool X86SelectTrunc(Instruction *I);
98
Dan Gohman78efce62008-09-10 21:02:08 +000099 bool X86SelectFPExt(Instruction *I);
100 bool X86SelectFPTrunc(Instruction *I);
101
Evan Chengf3d4efe2008-09-07 09:09:33 +0000102 bool X86SelectCall(Instruction *I);
103
104 CCAssignFn *CCAssignFnForCall(unsigned CC, bool isTailCall = false);
105
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000106 unsigned getGlobalBaseReg();
107
108 const X86InstrInfo *getInstrInfo() const {
109 return static_cast<const X86InstrInfo *>(TM.getInstrInfo());
110 }
111
Dan Gohman0586d912008-09-10 20:11:02 +0000112 unsigned TargetMaterializeConstant(Constant *C);
113
114 unsigned TargetMaterializeAlloca(AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000115
116 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
117 /// computed in an SSE register, not on the X87 floating point stack.
118 bool isScalarFPTypeInSSEReg(MVT VT) const {
119 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
120 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
121 }
122
Evan Chengc3f44b02008-09-03 00:03:49 +0000123};
Dan Gohman99b21822008-08-28 23:21:34 +0000124
Evan Chengdebdea02008-09-08 17:15:42 +0000125static bool isTypeLegal(const Type *Ty, const TargetLowering &TLI, MVT &VT,
126 bool AllowI1 = false) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000127 VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
128 if (VT == MVT::Other || !VT.isSimple())
129 // Unhandled type. Halt "fast" selection and bail.
130 return false;
131 if (VT == MVT::iPTR)
132 // Use pointer type.
133 VT = TLI.getPointerTy();
134 // We only handle legal types. For example, on x86-32 the instruction
135 // selector contains all of the 64-bit instructions from x86-64,
136 // under the assumption that i64 won't be used if the target doesn't
137 // support it.
Evan Chengdebdea02008-09-08 17:15:42 +0000138 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000139}
140
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000141/// getGlobalBaseReg - Return the the global base register. Output
142/// instructions required to initialize the global base register, if necessary.
143///
144unsigned X86FastISel::getGlobalBaseReg() {
145 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
146 if (!GlobalBaseReg)
147 GlobalBaseReg = getInstrInfo()->initializeGlobalBaseReg(MBB->getParent());
148 return GlobalBaseReg;
149}
150
Evan Chengf3d4efe2008-09-07 09:09:33 +0000151#include "X86GenCallingConv.inc"
152
153/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
154/// convention.
155CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) {
156 if (Subtarget->is64Bit()) {
157 if (Subtarget->isTargetWin64())
158 return CC_X86_Win64_C;
159 else if (CC == CallingConv::Fast && isTaillCall)
160 return CC_X86_64_TailCall;
161 else
162 return CC_X86_64_C;
163 }
164
165 if (CC == CallingConv::X86_FastCall)
166 return CC_X86_32_FastCall;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000167 else if (CC == CallingConv::Fast)
168 return CC_X86_32_FastCC;
169 else
170 return CC_X86_32_C;
171}
172
Evan Cheng0de588f2008-09-05 21:00:03 +0000173/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000174/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000175/// Return true and the result register by reference if it is possible.
Dan Gohman0586d912008-09-10 20:11:02 +0000176bool X86FastISel::X86FastEmitLoad(MVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000177 unsigned &ResultReg) {
178 // Get opcode and regclass of the output for the given load instruction.
179 unsigned Opc = 0;
180 const TargetRegisterClass *RC = NULL;
181 switch (VT.getSimpleVT()) {
182 default: return false;
183 case MVT::i8:
184 Opc = X86::MOV8rm;
185 RC = X86::GR8RegisterClass;
186 break;
187 case MVT::i16:
188 Opc = X86::MOV16rm;
189 RC = X86::GR16RegisterClass;
190 break;
191 case MVT::i32:
192 Opc = X86::MOV32rm;
193 RC = X86::GR32RegisterClass;
194 break;
195 case MVT::i64:
196 // Must be in x86-64 mode.
197 Opc = X86::MOV64rm;
198 RC = X86::GR64RegisterClass;
199 break;
200 case MVT::f32:
201 if (Subtarget->hasSSE1()) {
202 Opc = X86::MOVSSrm;
203 RC = X86::FR32RegisterClass;
204 } else {
205 Opc = X86::LD_Fp32m;
206 RC = X86::RFP32RegisterClass;
207 }
208 break;
209 case MVT::f64:
210 if (Subtarget->hasSSE2()) {
211 Opc = X86::MOVSDrm;
212 RC = X86::FR64RegisterClass;
213 } else {
214 Opc = X86::LD_Fp64m;
215 RC = X86::RFP64RegisterClass;
216 }
217 break;
218 case MVT::f80:
219 Opc = X86::LD_Fp80m;
220 RC = X86::RFP80RegisterClass;
221 break;
222 }
223
224 ResultReg = createResultReg(RC);
Evan Cheng0de588f2008-09-05 21:00:03 +0000225 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
226 return true;
227}
228
Evan Chengf3d4efe2008-09-07 09:09:33 +0000229/// X86FastEmitStore - Emit a machine instruction to store a value Val of
230/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
231/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000232/// i.e. V. Return true if it is possible.
233bool
Evan Chengf3d4efe2008-09-07 09:09:33 +0000234X86FastISel::X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000235 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000236 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000237 unsigned Opc = 0;
238 const TargetRegisterClass *RC = NULL;
239 switch (VT.getSimpleVT()) {
240 default: return false;
241 case MVT::i8:
242 Opc = X86::MOV8mr;
243 RC = X86::GR8RegisterClass;
244 break;
245 case MVT::i16:
246 Opc = X86::MOV16mr;
247 RC = X86::GR16RegisterClass;
248 break;
249 case MVT::i32:
250 Opc = X86::MOV32mr;
251 RC = X86::GR32RegisterClass;
252 break;
253 case MVT::i64:
254 // Must be in x86-64 mode.
255 Opc = X86::MOV64mr;
256 RC = X86::GR64RegisterClass;
257 break;
258 case MVT::f32:
259 if (Subtarget->hasSSE1()) {
260 Opc = X86::MOVSSmr;
261 RC = X86::FR32RegisterClass;
262 } else {
263 Opc = X86::ST_Fp32m;
264 RC = X86::RFP32RegisterClass;
265 }
266 break;
267 case MVT::f64:
268 if (Subtarget->hasSSE2()) {
269 Opc = X86::MOVSDmr;
270 RC = X86::FR64RegisterClass;
271 } else {
272 Opc = X86::ST_Fp64m;
273 RC = X86::RFP64RegisterClass;
274 }
275 break;
276 case MVT::f80:
277 Opc = X86::ST_FP80m;
278 RC = X86::RFP80RegisterClass;
279 break;
280 }
281
Evan Chengf3d4efe2008-09-07 09:09:33 +0000282 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000283 return true;
284}
285
Evan Cheng24e3a902008-09-08 06:35:17 +0000286/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
287/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
288/// ISD::SIGN_EXTEND).
289bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT,
290 unsigned Src, MVT SrcVT,
291 unsigned &ResultReg) {
Owen Andersonac34a002008-09-11 19:44:55 +0000292 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
293
294 if (RR != 0) {
295 ResultReg = RR;
296 return true;
297 } else
298 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000299}
300
Dan Gohman0586d912008-09-10 20:11:02 +0000301/// X86SelectAddress - Attempt to fill in an address from the given value.
302///
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000303bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall) {
Dan Gohman35893082008-09-18 23:23:44 +0000304 User *U;
305 unsigned Opcode = Instruction::UserOp1;
306 if (Instruction *I = dyn_cast<Instruction>(V)) {
307 Opcode = I->getOpcode();
308 U = I;
309 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
310 Opcode = C->getOpcode();
311 U = C;
312 }
Dan Gohman0586d912008-09-10 20:11:02 +0000313
Dan Gohman35893082008-09-18 23:23:44 +0000314 switch (Opcode) {
315 default: break;
316 case Instruction::BitCast:
317 // Look past bitcasts.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000318 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000319
320 case Instruction::IntToPtr:
321 // Look past no-op inttoptrs.
322 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000323 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000324
325 case Instruction::PtrToInt:
326 // Look past no-op ptrtoints.
327 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000328 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000329
330 case Instruction::Alloca: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000331 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000332 // Do static allocas.
333 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman0586d912008-09-10 20:11:02 +0000334 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
335 if (SI == StaticAllocaMap.end())
336 return false;
337 AM.BaseType = X86AddressMode::FrameIndexBase;
338 AM.Base.FrameIndex = SI->second;
Dan Gohman35893082008-09-18 23:23:44 +0000339 return true;
340 }
341
342 case Instruction::Add: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000343 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000344 // Adds of constants are common and easy enough.
345 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
346 AM.Disp += CI->getZExtValue();
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000347 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman0586d912008-09-10 20:11:02 +0000348 }
Dan Gohman35893082008-09-18 23:23:44 +0000349 break;
350 }
351
352 case Instruction::GetElementPtr: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000353 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000354 // Pattern-match simple GEPs.
355 uint64_t Disp = AM.Disp;
356 unsigned IndexReg = AM.IndexReg;
357 unsigned Scale = AM.Scale;
358 gep_type_iterator GTI = gep_type_begin(U);
359 // Look at all but the last index. Constants can be folded,
360 // and one dynamic index can be handled, if the scale is supported.
361 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
362 i != e; ++i, ++GTI) {
363 Value *Op = *i;
364 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
365 const StructLayout *SL = TD.getStructLayout(STy);
366 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
367 Disp += SL->getElementOffset(Idx);
368 } else {
369 uint64_t S = TD.getABITypeSize(GTI.getIndexedType());
370 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
371 // Constant-offset addressing.
372 Disp += CI->getZExtValue() * S;
373 } else if (IndexReg == 0 &&
374 (S == 1 || S == 2 || S == 4 || S == 8)) {
375 // Scaled-index addressing.
376 Scale = S;
377 IndexReg = getRegForValue(Op);
378 if (IndexReg == 0)
379 return false;
380 } else
381 // Unsupported.
382 goto unsupported_gep;
383 }
384 }
385 // Ok, the GEP indices were covered by constant-offset and scaled-index
386 // addressing. Update the address state and move on to examining the base.
387 AM.IndexReg = IndexReg;
388 AM.Scale = Scale;
389 AM.Disp = Disp;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000390 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000391 unsupported_gep:
392 // Ok, the GEP indices weren't all covered.
393 break;
394 }
395 }
396
397 // Handle constant address.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000398 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000399 // Can't handle alternate code models yet.
400 if (TM.getCodeModel() != CodeModel::Default &&
401 TM.getCodeModel() != CodeModel::Small)
402 return false;
403
404 // Set up the basic address.
405 AM.GV = GV;
406 if (!isCall &&
407 TM.getRelocationModel() == Reloc::PIC_ &&
408 !Subtarget->is64Bit())
409 AM.Base.Reg = getGlobalBaseReg();
410
411 // Emit an extra load if the ABI requires it.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000412 if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) {
413 // Check to see if we've already materialized this
414 // value in a register in this block.
Dan Gohman7e8ef602008-09-19 23:42:04 +0000415 if (unsigned Reg = LocalValueMap[V]) {
416 AM.Base.Reg = Reg;
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000417 AM.GV = 0;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000418 return true;
419 }
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000420 // Issue load from stub if necessary.
421 unsigned Opc = 0;
422 const TargetRegisterClass *RC = NULL;
423 if (TLI.getPointerTy() == MVT::i32) {
424 Opc = X86::MOV32rm;
425 RC = X86::GR32RegisterClass;
426 } else {
427 Opc = X86::MOV64rm;
428 RC = X86::GR64RegisterClass;
429 }
Dan Gohman789ce772008-09-25 23:34:02 +0000430
431 X86AddressMode StubAM;
432 StubAM.Base.Reg = AM.Base.Reg;
433 StubAM.GV = AM.GV;
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000434 unsigned ResultReg = createResultReg(RC);
Dan Gohman789ce772008-09-25 23:34:02 +0000435 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), StubAM);
436
437 // Now construct the final address. Note that the Disp, Scale,
438 // and Index values may already be set here.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000439 AM.Base.Reg = ResultReg;
440 AM.GV = 0;
Dan Gohman789ce772008-09-25 23:34:02 +0000441
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000442 // Prevent loading GV stub multiple times in same MBB.
443 LocalValueMap[V] = AM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000444 }
445 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000446 }
447
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000448 // If all else fails, just materialize the value in a register.
449 AM.Base.Reg = getRegForValue(V);
450 return AM.Base.Reg != 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000451}
452
Owen Andersona3971df2008-09-04 07:08:58 +0000453/// X86SelectStore - Select and emit code to implement store instructions.
454bool X86FastISel::X86SelectStore(Instruction* I) {
Evan Cheng24e3a902008-09-08 06:35:17 +0000455 MVT VT;
456 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
Owen Andersona3971df2008-09-04 07:08:58 +0000457 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000458 unsigned Val = getRegForValue(I->getOperand(0));
459 if (Val == 0)
Owen Andersona3971df2008-09-04 07:08:58 +0000460 // Unhandled operand. Halt "fast" selection and bail.
461 return false;
462
Dan Gohman0586d912008-09-10 20:11:02 +0000463 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000464 if (!X86SelectAddress(I->getOperand(1), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000465 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000466
Dan Gohman0586d912008-09-10 20:11:02 +0000467 return X86FastEmitStore(VT, Val, AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000468}
469
Evan Cheng8b19e562008-09-03 06:44:39 +0000470/// X86SelectLoad - Select and emit code to implement load instructions.
471///
Dan Gohman3df24e62008-09-03 23:12:08 +0000472bool X86FastISel::X86SelectLoad(Instruction *I) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000473 MVT VT;
474 if (!isTypeLegal(I->getType(), TLI, VT))
Evan Cheng8b19e562008-09-03 06:44:39 +0000475 return false;
476
Dan Gohman0586d912008-09-10 20:11:02 +0000477 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000478 if (!X86SelectAddress(I->getOperand(0), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000479 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000480
Evan Cheng0de588f2008-09-05 21:00:03 +0000481 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000482 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000483 UpdateValueMap(I, ResultReg);
484 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000485 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000486 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000487}
488
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000489bool X86FastISel::X86SelectCmp(Instruction *I) {
490 CmpInst *CI = cast<CmpInst>(I);
491
Dan Gohman4f22bb02008-09-05 01:33:56 +0000492 MVT VT = TLI.getValueType(I->getOperand(0)->getType());
493 if (!TLI.isTypeLegal(VT))
494 return false;
495
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000496 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000497 if (Op0Reg == 0) return false;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000498 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000499 if (Op1Reg == 0) return false;
500
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000501 unsigned Opc;
Dan Gohmanf52550b2008-09-05 01:15:35 +0000502 switch (VT.getSimpleVT()) {
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000503 case MVT::i8: Opc = X86::CMP8rr; break;
504 case MVT::i16: Opc = X86::CMP16rr; break;
505 case MVT::i32: Opc = X86::CMP32rr; break;
506 case MVT::i64: Opc = X86::CMP64rr; break;
507 case MVT::f32: Opc = X86::UCOMISSrr; break;
508 case MVT::f64: Opc = X86::UCOMISDrr; break;
509 default: return false;
510 }
511
512 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
513 switch (CI->getPredicate()) {
514 case CmpInst::FCMP_OEQ: {
515 unsigned EReg = createResultReg(&X86::GR8RegClass);
516 unsigned NPReg = createResultReg(&X86::GR8RegClass);
517 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
518 BuildMI(MBB, TII.get(X86::SETEr), EReg);
519 BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
520 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
521 break;
522 }
523 case CmpInst::FCMP_UNE: {
524 unsigned NEReg = createResultReg(&X86::GR8RegClass);
525 unsigned PReg = createResultReg(&X86::GR8RegClass);
526 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
527 BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
528 BuildMI(MBB, TII.get(X86::SETPr), PReg);
529 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
530 break;
531 }
532 case CmpInst::FCMP_OGT:
533 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
534 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
535 break;
536 case CmpInst::FCMP_OGE:
537 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
538 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
539 break;
540 case CmpInst::FCMP_OLT:
541 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
542 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
543 break;
544 case CmpInst::FCMP_OLE:
545 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
546 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
547 break;
548 case CmpInst::FCMP_ONE:
549 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
550 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
551 break;
552 case CmpInst::FCMP_ORD:
553 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
554 BuildMI(MBB, TII.get(X86::SETNPr), ResultReg);
555 break;
556 case CmpInst::FCMP_UNO:
557 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
558 BuildMI(MBB, TII.get(X86::SETPr), ResultReg);
559 break;
560 case CmpInst::FCMP_UEQ:
561 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
562 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
563 break;
564 case CmpInst::FCMP_UGT:
565 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
566 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
567 break;
568 case CmpInst::FCMP_UGE:
569 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
570 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
571 break;
572 case CmpInst::FCMP_ULT:
573 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
574 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
575 break;
576 case CmpInst::FCMP_ULE:
577 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
578 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
579 break;
580 case CmpInst::ICMP_EQ:
581 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
582 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
583 break;
584 case CmpInst::ICMP_NE:
585 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
586 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
587 break;
588 case CmpInst::ICMP_UGT:
589 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
590 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
591 break;
592 case CmpInst::ICMP_UGE:
593 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
594 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
595 break;
596 case CmpInst::ICMP_ULT:
597 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
598 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
599 break;
600 case CmpInst::ICMP_ULE:
601 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
602 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
603 break;
604 case CmpInst::ICMP_SGT:
605 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
606 BuildMI(MBB, TII.get(X86::SETGr), ResultReg);
607 break;
608 case CmpInst::ICMP_SGE:
609 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
610 BuildMI(MBB, TII.get(X86::SETGEr), ResultReg);
611 break;
612 case CmpInst::ICMP_SLT:
613 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
614 BuildMI(MBB, TII.get(X86::SETLr), ResultReg);
615 break;
616 case CmpInst::ICMP_SLE:
617 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
618 BuildMI(MBB, TII.get(X86::SETLEr), ResultReg);
619 break;
620 default:
621 return false;
622 }
623
624 UpdateValueMap(I, ResultReg);
625 return true;
626}
Evan Cheng8b19e562008-09-03 06:44:39 +0000627
Dan Gohmand89ae992008-09-05 01:06:14 +0000628bool X86FastISel::X86SelectZExt(Instruction *I) {
629 // Special-case hack: The only i1 values we know how to produce currently
630 // set the upper bits of an i8 value to zero.
631 if (I->getType() == Type::Int8Ty &&
632 I->getOperand(0)->getType() == Type::Int1Ty) {
633 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000634 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000635 UpdateValueMap(I, ResultReg);
636 return true;
637 }
638
639 return false;
640}
641
642bool X86FastISel::X86SelectBranch(Instruction *I) {
643 BranchInst *BI = cast<BranchInst>(I);
644 // Unconditional branches are selected by tablegen-generated code.
645 unsigned OpReg = getRegForValue(BI->getCondition());
Dan Gohmanf52550b2008-09-05 01:15:35 +0000646 if (OpReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000647 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
648 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
649
650 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
651 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
652 BuildMI(MBB, TII.get(X86::JMP)).addMBB(FalseMBB);
653
654 MBB->addSuccessor(TrueMBB);
655 MBB->addSuccessor(FalseMBB);
656
657 return true;
658}
659
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000660bool X86FastISel::X86SelectShift(Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000661 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000662 const TargetRegisterClass *RC = NULL;
663 if (I->getType() == Type::Int8Ty) {
664 CReg = X86::CL;
665 RC = &X86::GR8RegClass;
666 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000667 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
668 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
669 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000670 default: return false;
671 }
672 } else if (I->getType() == Type::Int16Ty) {
673 CReg = X86::CX;
674 RC = &X86::GR16RegClass;
675 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000676 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
677 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
678 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000679 default: return false;
680 }
681 } else if (I->getType() == Type::Int32Ty) {
682 CReg = X86::ECX;
683 RC = &X86::GR32RegClass;
684 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000685 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
686 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
687 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000688 default: return false;
689 }
690 } else if (I->getType() == Type::Int64Ty) {
691 CReg = X86::RCX;
692 RC = &X86::GR64RegClass;
693 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000694 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
695 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
696 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000697 default: return false;
698 }
699 } else {
700 return false;
701 }
702
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000703 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
704 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
705 return false;
706
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000707 unsigned Op0Reg = getRegForValue(I->getOperand(0));
708 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +0000709
710 // Fold immediate in shl(x,3).
711 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
712 unsigned ResultReg = createResultReg(RC);
713 BuildMI(MBB, TII.get(OpImm),
714 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue());
715 UpdateValueMap(I, ResultReg);
716 return true;
717 }
718
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000719 unsigned Op1Reg = getRegForValue(I->getOperand(1));
720 if (Op1Reg == 0) return false;
721 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
722 unsigned ResultReg = createResultReg(RC);
Chris Lattner743922e2008-09-21 21:44:29 +0000723 BuildMI(MBB, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000724 UpdateValueMap(I, ResultReg);
725 return true;
726}
727
728bool X86FastISel::X86SelectSelect(Instruction *I) {
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000729 const Type *Ty = I->getType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000730 if (isa<PointerType>(Ty))
Dan Gohman1fbc3cd2008-09-18 18:26:43 +0000731 Ty = TD.getIntPtrType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000732
733 unsigned Opc = 0;
734 const TargetRegisterClass *RC = NULL;
735 if (Ty == Type::Int16Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000736 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000737 RC = &X86::GR16RegClass;
738 } else if (Ty == Type::Int32Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000739 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000740 RC = &X86::GR32RegClass;
741 } else if (Ty == Type::Int64Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000742 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000743 RC = &X86::GR64RegClass;
744 } else {
745 return false;
746 }
747
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000748 MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
749 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
750 return false;
751
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000752 unsigned Op0Reg = getRegForValue(I->getOperand(0));
753 if (Op0Reg == 0) return false;
754 unsigned Op1Reg = getRegForValue(I->getOperand(1));
755 if (Op1Reg == 0) return false;
756 unsigned Op2Reg = getRegForValue(I->getOperand(2));
757 if (Op2Reg == 0) return false;
758
759 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
760 unsigned ResultReg = createResultReg(RC);
761 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
762 UpdateValueMap(I, ResultReg);
763 return true;
764}
765
Dan Gohman78efce62008-09-10 21:02:08 +0000766bool X86FastISel::X86SelectFPExt(Instruction *I) {
767 if (Subtarget->hasSSE2()) {
768 if (I->getType() == Type::DoubleTy) {
769 Value *V = I->getOperand(0);
770 if (V->getType() == Type::FloatTy) {
771 unsigned OpReg = getRegForValue(V);
772 if (OpReg == 0) return false;
773 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
774 BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
775 UpdateValueMap(I, ResultReg);
776 return true;
777 }
778 }
779 }
780
781 return false;
782}
783
784bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
785 if (Subtarget->hasSSE2()) {
786 if (I->getType() == Type::FloatTy) {
787 Value *V = I->getOperand(0);
788 if (V->getType() == Type::DoubleTy) {
789 unsigned OpReg = getRegForValue(V);
790 if (OpReg == 0) return false;
791 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
792 BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
793 UpdateValueMap(I, ResultReg);
794 return true;
795 }
796 }
797 }
798
799 return false;
800}
801
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000802bool X86FastISel::X86SelectTrunc(Instruction *I) {
803 if (Subtarget->is64Bit())
804 // All other cases should be handled by the tblgen generated code.
805 return false;
806 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
807 MVT DstVT = TLI.getValueType(I->getType());
808 if (DstVT != MVT::i8)
809 // All other cases should be handled by the tblgen generated code.
810 return false;
811 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
812 // All other cases should be handled by the tblgen generated code.
813 return false;
814
815 unsigned InputReg = getRegForValue(I->getOperand(0));
816 if (!InputReg)
817 // Unhandled operand. Halt "fast" selection and bail.
818 return false;
819
820 // First issue a copy to GR16_ or GR32_.
821 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
822 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
823 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
824 unsigned CopyReg = createResultReg(CopyRC);
825 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
826
827 // Then issue an extract_subreg.
828 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg,1); // x86_subreg_8bit
829 if (!ResultReg)
830 return false;
831
832 UpdateValueMap(I, ResultReg);
833 return true;
834}
835
Evan Chengf3d4efe2008-09-07 09:09:33 +0000836bool X86FastISel::X86SelectCall(Instruction *I) {
837 CallInst *CI = cast<CallInst>(I);
838 Value *Callee = I->getOperand(0);
839
840 // Can't handle inline asm yet.
841 if (isa<InlineAsm>(Callee))
842 return false;
843
844 // FIXME: Handle some intrinsics.
845 if (Function *F = CI->getCalledFunction()) {
846 if (F->isDeclaration() &&F->getIntrinsicID())
847 return false;
848 }
849
Evan Chengf3d4efe2008-09-07 09:09:33 +0000850 // Handle only C and fastcc calling conventions for now.
851 CallSite CS(CI);
852 unsigned CC = CS.getCallingConv();
853 if (CC != CallingConv::C &&
854 CC != CallingConv::Fast &&
855 CC != CallingConv::X86_FastCall)
856 return false;
857
858 // Let SDISel handle vararg functions.
859 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
860 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
861 if (FTy->isVarArg())
862 return false;
863
864 // Handle *simple* calls for now.
865 const Type *RetTy = CS.getType();
866 MVT RetVT;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000867 if (RetTy == Type::VoidTy)
868 RetVT = MVT::isVoid;
869 else if (!isTypeLegal(RetTy, TLI, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000870 return false;
871
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000872 // Materialize callee address in a register. FIXME: GV address can be
873 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000874 X86AddressMode CalleeAM;
875 if (!X86SelectAddress(Callee, CalleeAM, true))
876 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000877 unsigned CalleeOp = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000878 GlobalValue *GV = 0;
879 if (CalleeAM.Base.Reg != 0) {
880 assert(CalleeAM.GV == 0);
881 CalleeOp = CalleeAM.Base.Reg;
882 } else if (CalleeAM.GV != 0) {
883 assert(CalleeAM.GV != 0);
884 GV = CalleeAM.GV;
885 } else
886 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000887
Evan Chengdebdea02008-09-08 17:15:42 +0000888 // Allow calls which produce i1 results.
889 bool AndToI1 = false;
890 if (RetVT == MVT::i1) {
891 RetVT = MVT::i8;
892 AndToI1 = true;
893 }
894
Evan Chengf3d4efe2008-09-07 09:09:33 +0000895 // Deal with call operands first.
896 SmallVector<unsigned, 4> Args;
897 SmallVector<MVT, 4> ArgVTs;
898 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
899 Args.reserve(CS.arg_size());
900 ArgVTs.reserve(CS.arg_size());
901 ArgFlags.reserve(CS.arg_size());
902 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
903 i != e; ++i) {
904 unsigned Arg = getRegForValue(*i);
905 if (Arg == 0)
906 return false;
907 ISD::ArgFlagsTy Flags;
908 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +0000909 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000910 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +0000911 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000912 Flags.setZExt();
913
914 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +0000915 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
916 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
917 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
918 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000919 return false;
920
921 const Type *ArgTy = (*i)->getType();
922 MVT ArgVT;
923 if (!isTypeLegal(ArgTy, TLI, ArgVT))
924 return false;
925 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
926 Flags.setOrigAlign(OriginalAlignment);
927
928 Args.push_back(Arg);
929 ArgVTs.push_back(ArgVT);
930 ArgFlags.push_back(Flags);
931 }
932
933 // Analyze operands of the call, assigning locations to each operand.
934 SmallVector<CCValAssign, 16> ArgLocs;
935 CCState CCInfo(CC, false, TM, ArgLocs);
936 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
937
938 // Get a count of how many bytes are to be pushed on the stack.
939 unsigned NumBytes = CCInfo.getNextStackOffset();
940
941 // Issue CALLSEQ_START
942 BuildMI(MBB, TII.get(X86::ADJCALLSTACKDOWN)).addImm(NumBytes);
943
944 // Process argumenet: walk the register/memloc assignments, inserting
945 // copies / loads.
946 SmallVector<unsigned, 4> RegArgs;
947 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
948 CCValAssign &VA = ArgLocs[i];
949 unsigned Arg = Args[VA.getValNo()];
950 MVT ArgVT = ArgVTs[VA.getValNo()];
951
952 // Promote the value if needed.
953 switch (VA.getLocInfo()) {
954 default: assert(0 && "Unknown loc info!");
955 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000956 case CCValAssign::SExt: {
957 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
958 Arg, ArgVT, Arg);
959 assert(Emitted && "Failed to emit a sext!");
960 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000961 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000962 }
963 case CCValAssign::ZExt: {
964 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
965 Arg, ArgVT, Arg);
966 assert(Emitted && "Failed to emit a zext!");
967 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000968 break;
Evan Cheng24e3a902008-09-08 06:35:17 +0000969 }
970 case CCValAssign::AExt: {
971 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
972 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +0000973 if (!Emitted)
974 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
975 Arg, ArgVT, Arg);
976 if (!Emitted)
977 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
978 Arg, ArgVT, Arg);
979
Evan Cheng24e3a902008-09-08 06:35:17 +0000980 assert(Emitted && "Failed to emit a aext!");
981 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +0000982 break;
983 }
Evan Cheng24e3a902008-09-08 06:35:17 +0000984 }
Evan Chengf3d4efe2008-09-07 09:09:33 +0000985
986 if (VA.isRegLoc()) {
987 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
988 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
989 Arg, RC, RC);
990 assert(Emitted && "Failed to emit a copy instruction!");
991 RegArgs.push_back(VA.getLocReg());
992 } else {
993 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +0000994 X86AddressMode AM;
995 AM.Base.Reg = StackPtr;
996 AM.Disp = LocMemOffset;
997 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000998 }
999 }
1000
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001001 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1002 // GOT pointer.
1003 if (!Subtarget->is64Bit() &&
1004 TM.getRelocationModel() == Reloc::PIC_ &&
1005 Subtarget->isPICStyleGOT()) {
1006 TargetRegisterClass *RC = X86::GR32RegisterClass;
1007 unsigned Base = getGlobalBaseReg();
1008 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
1009 assert(Emitted && "Failed to emit a copy instruction!");
1010 }
1011
Evan Chengf3d4efe2008-09-07 09:09:33 +00001012 // Issue the call.
1013 unsigned CallOpc = CalleeOp
1014 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
1015 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
1016 MachineInstrBuilder MIB = CalleeOp
1017 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001018 : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001019
1020 // Add an implicit use GOT pointer in EBX.
1021 if (!Subtarget->is64Bit() &&
1022 TM.getRelocationModel() == Reloc::PIC_ &&
1023 Subtarget->isPICStyleGOT())
1024 MIB.addReg(X86::EBX);
1025
Evan Chengf3d4efe2008-09-07 09:09:33 +00001026 // Add implicit physical register uses to the call.
1027 while (!RegArgs.empty()) {
1028 MIB.addReg(RegArgs.back());
1029 RegArgs.pop_back();
1030 }
1031
1032 // Issue CALLSEQ_END
1033 BuildMI(MBB, TII.get(X86::ADJCALLSTACKUP)).addImm(NumBytes).addImm(0);
1034
1035 // Now handle call return value (if any).
Evan Chengf3d4efe2008-09-07 09:09:33 +00001036 if (RetVT.getSimpleVT() != MVT::isVoid) {
1037 SmallVector<CCValAssign, 16> RVLocs;
1038 CCState CCInfo(CC, false, TM, RVLocs);
1039 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1040
1041 // Copy all of the result registers out of their specified physreg.
1042 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
1043 MVT CopyVT = RVLocs[0].getValVT();
1044 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1045 TargetRegisterClass *SrcRC = DstRC;
1046
1047 // If this is a call to a function that returns an fp value on the x87 fp
1048 // stack, but where we prefer to use the value in xmm registers, copy it
1049 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1050 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1051 RVLocs[0].getLocReg() == X86::ST1) &&
1052 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
1053 CopyVT = MVT::f80;
1054 SrcRC = X86::RSTRegisterClass;
1055 DstRC = X86::RFP80RegisterClass;
1056 }
1057
1058 unsigned ResultReg = createResultReg(DstRC);
1059 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1060 RVLocs[0].getLocReg(), DstRC, SrcRC);
1061 assert(Emitted && "Failed to emit a copy instruction!");
1062 if (CopyVT != RVLocs[0].getValVT()) {
1063 // Round the F80 the right size, which also moves to the appropriate xmm
1064 // register. This is accomplished by storing the F80 value in memory and
1065 // then loading it back. Ewww...
1066 MVT ResVT = RVLocs[0].getValVT();
1067 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1068 unsigned MemSize = ResVT.getSizeInBits()/8;
Dan Gohman0586d912008-09-10 20:11:02 +00001069 int FI = MFI.CreateStackObject(MemSize, MemSize);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001070 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
1071 DstRC = ResVT == MVT::f32
1072 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1073 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1074 ResultReg = createResultReg(DstRC);
1075 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
1076 }
1077
Evan Chengdebdea02008-09-08 17:15:42 +00001078 if (AndToI1) {
1079 // Mask out all but lowest bit for some call which produces an i1.
1080 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
1081 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
1082 ResultReg = AndResult;
1083 }
1084
Evan Chengf3d4efe2008-09-07 09:09:33 +00001085 UpdateValueMap(I, ResultReg);
1086 }
1087
1088 return true;
1089}
1090
1091
Dan Gohman99b21822008-08-28 23:21:34 +00001092bool
Dan Gohman3df24e62008-09-03 23:12:08 +00001093X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001094 switch (I->getOpcode()) {
1095 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001096 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001097 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001098 case Instruction::Store:
1099 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001100 case Instruction::ICmp:
1101 case Instruction::FCmp:
1102 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001103 case Instruction::ZExt:
1104 return X86SelectZExt(I);
1105 case Instruction::Br:
1106 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001107 case Instruction::Call:
1108 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001109 case Instruction::LShr:
1110 case Instruction::AShr:
1111 case Instruction::Shl:
1112 return X86SelectShift(I);
1113 case Instruction::Select:
1114 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001115 case Instruction::Trunc:
1116 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001117 case Instruction::FPExt:
1118 return X86SelectFPExt(I);
1119 case Instruction::FPTrunc:
1120 return X86SelectFPTrunc(I);
Dan Gohman99b21822008-08-28 23:21:34 +00001121 }
1122
1123 return false;
1124}
1125
Dan Gohman0586d912008-09-10 20:11:02 +00001126unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Evan Cheng59fbc802008-09-09 01:26:59 +00001127 MVT VT;
1128 if (!isTypeLegal(C->getType(), TLI, VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001129 return false;
1130
1131 // Get opcode and regclass of the output for the given load instruction.
1132 unsigned Opc = 0;
1133 const TargetRegisterClass *RC = NULL;
1134 switch (VT.getSimpleVT()) {
1135 default: return false;
1136 case MVT::i8:
1137 Opc = X86::MOV8rm;
1138 RC = X86::GR8RegisterClass;
1139 break;
1140 case MVT::i16:
1141 Opc = X86::MOV16rm;
1142 RC = X86::GR16RegisterClass;
1143 break;
1144 case MVT::i32:
1145 Opc = X86::MOV32rm;
1146 RC = X86::GR32RegisterClass;
1147 break;
1148 case MVT::i64:
1149 // Must be in x86-64 mode.
1150 Opc = X86::MOV64rm;
1151 RC = X86::GR64RegisterClass;
1152 break;
1153 case MVT::f32:
1154 if (Subtarget->hasSSE1()) {
1155 Opc = X86::MOVSSrm;
1156 RC = X86::FR32RegisterClass;
1157 } else {
1158 Opc = X86::LD_Fp32m;
1159 RC = X86::RFP32RegisterClass;
1160 }
1161 break;
1162 case MVT::f64:
1163 if (Subtarget->hasSSE2()) {
1164 Opc = X86::MOVSDrm;
1165 RC = X86::FR64RegisterClass;
1166 } else {
1167 Opc = X86::LD_Fp64m;
1168 RC = X86::RFP64RegisterClass;
1169 }
1170 break;
1171 case MVT::f80:
1172 Opc = X86::LD_Fp80m;
1173 RC = X86::RFP80RegisterClass;
1174 break;
1175 }
1176
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001177 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001178 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001179 X86AddressMode AM;
1180 if (X86SelectAddress(C, AM, false)) {
1181 if (TLI.getPointerTy() == MVT::i32)
1182 Opc = X86::LEA32r;
1183 else
1184 Opc = X86::LEA64r;
1185 unsigned ResultReg = createResultReg(RC);
1186 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001187 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001188 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001189 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001190 }
1191
Owen Anderson3b217c62008-09-06 01:11:01 +00001192 // MachineConstantPool wants an explicit alignment.
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001193 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001194 if (Align == 0) {
1195 // Alignment of vector types. FIXME!
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001196 Align = TD.getABITypeSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001197 Align = Log2_64(Align);
1198 }
Owen Anderson95267a12008-09-05 00:06:23 +00001199
Dan Gohman0586d912008-09-10 20:11:02 +00001200 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001201 unsigned ResultReg = createResultReg(RC);
Owen Anderson95267a12008-09-05 00:06:23 +00001202 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset);
Owen Anderson95267a12008-09-05 00:06:23 +00001203 return ResultReg;
1204}
1205
Dan Gohman0586d912008-09-10 20:11:02 +00001206unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
1207 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001208 if (!X86SelectAddress(C, AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +00001209 return 0;
1210 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1211 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1212 unsigned ResultReg = createResultReg(RC);
1213 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1214 return ResultReg;
1215}
1216
Evan Chengc3f44b02008-09-03 00:03:49 +00001217namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001218 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001219 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +00001220 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001221 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
1222 DenseMap<const AllocaInst *, int> &am) {
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001223 return new X86FastISel(mf, mmi, vm, bm, am);
Evan Chengc3f44b02008-09-03 00:03:49 +00001224 }
Dan Gohman99b21822008-08-28 23:21:34 +00001225}