blob: 2ce78eccef08b812e720fd1ebb6674926cfbbf81 [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);
Dan Gohmand98d6202008-10-02 22:15:21 +000093
94 unsigned X86ChooseCmpOpcode(MVT VT);
Evan Cheng10a8d9c2008-09-07 08:47:42 +000095
Dan Gohman78efce62008-09-10 21:02:08 +000096 bool X86SelectFPExt(Instruction *I);
97 bool X86SelectFPTrunc(Instruction *I);
98
Evan Chengf3d4efe2008-09-07 09:09:33 +000099 bool X86SelectCall(Instruction *I);
100
101 CCAssignFn *CCAssignFnForCall(unsigned CC, bool isTailCall = false);
102
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000103 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000104 return getTargetMachine()->getInstrInfo();
105 }
106 const X86TargetMachine *getTargetMachine() const {
107 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000108 }
109
Dan Gohman0586d912008-09-10 20:11:02 +0000110 unsigned TargetMaterializeConstant(Constant *C);
111
112 unsigned TargetMaterializeAlloca(AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000113
114 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
115 /// computed in an SSE register, not on the X87 floating point stack.
116 bool isScalarFPTypeInSSEReg(MVT VT) const {
117 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
118 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
119 }
120
Dan Gohman9b66d732008-09-30 00:48:39 +0000121 bool isTypeLegal(const Type *Ty, const TargetLowering &TLI, MVT &VT,
122 bool AllowI1 = false);
Evan Chengc3f44b02008-09-03 00:03:49 +0000123};
Dan Gohman99b21822008-08-28 23:21:34 +0000124
Dan Gohman9b66d732008-09-30 00:48:39 +0000125bool X86FastISel::isTypeLegal(const Type *Ty, const TargetLowering &TLI,
126 MVT &VT, bool AllowI1) {
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();
Dan Gohman9b66d732008-09-30 00:48:39 +0000134 // For now, require SSE/SSE2 for performing floating-point operations,
135 // since x87 requires additional work.
136 if (VT == MVT::f64 && !X86ScalarSSEf64)
137 return false;
138 if (VT == MVT::f32 && !X86ScalarSSEf32)
139 return false;
140 // Similarly, no f80 support yet.
141 if (VT == MVT::f80)
142 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000143 // We only handle legal types. For example, on x86-32 the instruction
144 // selector contains all of the 64-bit instructions from x86-64,
145 // under the assumption that i64 won't be used if the target doesn't
146 // support it.
Evan Chengdebdea02008-09-08 17:15:42 +0000147 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000148}
149
150#include "X86GenCallingConv.inc"
151
152/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
153/// convention.
154CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) {
155 if (Subtarget->is64Bit()) {
156 if (Subtarget->isTargetWin64())
157 return CC_X86_Win64_C;
158 else if (CC == CallingConv::Fast && isTaillCall)
159 return CC_X86_64_TailCall;
160 else
161 return CC_X86_64_C;
162 }
163
164 if (CC == CallingConv::X86_FastCall)
165 return CC_X86_32_FastCall;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000166 else if (CC == CallingConv::Fast)
167 return CC_X86_32_FastCC;
168 else
169 return CC_X86_32_C;
170}
171
Evan Cheng0de588f2008-09-05 21:00:03 +0000172/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000173/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000174/// Return true and the result register by reference if it is possible.
Dan Gohman0586d912008-09-10 20:11:02 +0000175bool X86FastISel::X86FastEmitLoad(MVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000176 unsigned &ResultReg) {
177 // Get opcode and regclass of the output for the given load instruction.
178 unsigned Opc = 0;
179 const TargetRegisterClass *RC = NULL;
180 switch (VT.getSimpleVT()) {
181 default: return false;
182 case MVT::i8:
183 Opc = X86::MOV8rm;
184 RC = X86::GR8RegisterClass;
185 break;
186 case MVT::i16:
187 Opc = X86::MOV16rm;
188 RC = X86::GR16RegisterClass;
189 break;
190 case MVT::i32:
191 Opc = X86::MOV32rm;
192 RC = X86::GR32RegisterClass;
193 break;
194 case MVT::i64:
195 // Must be in x86-64 mode.
196 Opc = X86::MOV64rm;
197 RC = X86::GR64RegisterClass;
198 break;
199 case MVT::f32:
200 if (Subtarget->hasSSE1()) {
201 Opc = X86::MOVSSrm;
202 RC = X86::FR32RegisterClass;
203 } else {
204 Opc = X86::LD_Fp32m;
205 RC = X86::RFP32RegisterClass;
206 }
207 break;
208 case MVT::f64:
209 if (Subtarget->hasSSE2()) {
210 Opc = X86::MOVSDrm;
211 RC = X86::FR64RegisterClass;
212 } else {
213 Opc = X86::LD_Fp64m;
214 RC = X86::RFP64RegisterClass;
215 }
216 break;
217 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000218 // No f80 support yet.
219 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000220 }
221
222 ResultReg = createResultReg(RC);
Evan Cheng0de588f2008-09-05 21:00:03 +0000223 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
224 return true;
225}
226
Evan Chengf3d4efe2008-09-07 09:09:33 +0000227/// X86FastEmitStore - Emit a machine instruction to store a value Val of
228/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
229/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000230/// i.e. V. Return true if it is possible.
231bool
Evan Chengf3d4efe2008-09-07 09:09:33 +0000232X86FastISel::X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000233 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000234 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000235 unsigned Opc = 0;
236 const TargetRegisterClass *RC = NULL;
237 switch (VT.getSimpleVT()) {
238 default: return false;
239 case MVT::i8:
240 Opc = X86::MOV8mr;
241 RC = X86::GR8RegisterClass;
242 break;
243 case MVT::i16:
244 Opc = X86::MOV16mr;
245 RC = X86::GR16RegisterClass;
246 break;
247 case MVT::i32:
248 Opc = X86::MOV32mr;
249 RC = X86::GR32RegisterClass;
250 break;
251 case MVT::i64:
252 // Must be in x86-64 mode.
253 Opc = X86::MOV64mr;
254 RC = X86::GR64RegisterClass;
255 break;
256 case MVT::f32:
257 if (Subtarget->hasSSE1()) {
258 Opc = X86::MOVSSmr;
259 RC = X86::FR32RegisterClass;
260 } else {
261 Opc = X86::ST_Fp32m;
262 RC = X86::RFP32RegisterClass;
263 }
264 break;
265 case MVT::f64:
266 if (Subtarget->hasSSE2()) {
267 Opc = X86::MOVSDmr;
268 RC = X86::FR64RegisterClass;
269 } else {
270 Opc = X86::ST_Fp64m;
271 RC = X86::RFP64RegisterClass;
272 }
273 break;
274 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000275 // No f80 support yet.
276 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000277 }
278
Evan Chengf3d4efe2008-09-07 09:09:33 +0000279 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000280 return true;
281}
282
Evan Cheng24e3a902008-09-08 06:35:17 +0000283/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
284/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
285/// ISD::SIGN_EXTEND).
286bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT,
287 unsigned Src, MVT SrcVT,
288 unsigned &ResultReg) {
Owen Andersonac34a002008-09-11 19:44:55 +0000289 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
290
291 if (RR != 0) {
292 ResultReg = RR;
293 return true;
294 } else
295 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000296}
297
Dan Gohman0586d912008-09-10 20:11:02 +0000298/// X86SelectAddress - Attempt to fill in an address from the given value.
299///
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000300bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall) {
Dan Gohman35893082008-09-18 23:23:44 +0000301 User *U;
302 unsigned Opcode = Instruction::UserOp1;
303 if (Instruction *I = dyn_cast<Instruction>(V)) {
304 Opcode = I->getOpcode();
305 U = I;
306 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
307 Opcode = C->getOpcode();
308 U = C;
309 }
Dan Gohman0586d912008-09-10 20:11:02 +0000310
Dan Gohman35893082008-09-18 23:23:44 +0000311 switch (Opcode) {
312 default: break;
313 case Instruction::BitCast:
314 // Look past bitcasts.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000315 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000316
317 case Instruction::IntToPtr:
318 // Look past no-op inttoptrs.
319 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000320 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000321
322 case Instruction::PtrToInt:
323 // Look past no-op ptrtoints.
324 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000325 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000326
327 case Instruction::Alloca: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000328 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000329 // Do static allocas.
330 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman0586d912008-09-10 20:11:02 +0000331 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
Dan Gohman97135e12008-09-26 19:15:30 +0000332 if (SI != StaticAllocaMap.end()) {
333 AM.BaseType = X86AddressMode::FrameIndexBase;
334 AM.Base.FrameIndex = SI->second;
335 return true;
336 }
337 break;
Dan Gohman35893082008-09-18 23:23:44 +0000338 }
339
340 case Instruction::Add: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000341 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000342 // Adds of constants are common and easy enough.
343 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000344 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
345 // They have to fit in the 32-bit signed displacement field though.
346 if (isInt32(Disp)) {
347 AM.Disp = (uint32_t)Disp;
348 return X86SelectAddress(U->getOperand(0), AM, isCall);
349 }
Dan Gohman0586d912008-09-10 20:11:02 +0000350 }
Dan Gohman35893082008-09-18 23:23:44 +0000351 break;
352 }
353
354 case Instruction::GetElementPtr: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000355 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000356 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000357 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000358 unsigned IndexReg = AM.IndexReg;
359 unsigned Scale = AM.Scale;
360 gep_type_iterator GTI = gep_type_begin(U);
361 // Look at all but the last index. Constants can be folded,
362 // and one dynamic index can be handled, if the scale is supported.
363 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
364 i != e; ++i, ++GTI) {
365 Value *Op = *i;
366 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
367 const StructLayout *SL = TD.getStructLayout(STy);
368 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
369 Disp += SL->getElementOffset(Idx);
370 } else {
371 uint64_t S = TD.getABITypeSize(GTI.getIndexedType());
372 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
373 // Constant-offset addressing.
Dan Gohman09aae462008-09-26 20:04:15 +0000374 Disp += CI->getSExtValue() * S;
Dan Gohman35893082008-09-18 23:23:44 +0000375 } else if (IndexReg == 0 &&
Dan Gohman97135e12008-09-26 19:15:30 +0000376 (!AM.GV ||
377 !getTargetMachine()->symbolicAddressesAreRIPRel()) &&
Dan Gohman35893082008-09-18 23:23:44 +0000378 (S == 1 || S == 2 || S == 4 || S == 8)) {
379 // Scaled-index addressing.
380 Scale = S;
381 IndexReg = getRegForValue(Op);
382 if (IndexReg == 0)
383 return false;
384 } else
385 // Unsupported.
386 goto unsupported_gep;
387 }
388 }
Dan Gohman09aae462008-09-26 20:04:15 +0000389 // Check for displacement overflow.
390 if (!isInt32(Disp))
391 break;
Dan Gohman35893082008-09-18 23:23:44 +0000392 // Ok, the GEP indices were covered by constant-offset and scaled-index
393 // addressing. Update the address state and move on to examining the base.
394 AM.IndexReg = IndexReg;
395 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000396 AM.Disp = (uint32_t)Disp;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000397 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000398 unsupported_gep:
399 // Ok, the GEP indices weren't all covered.
400 break;
401 }
402 }
403
404 // Handle constant address.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000405 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000406 // Can't handle alternate code models yet.
407 if (TM.getCodeModel() != CodeModel::Default &&
408 TM.getCodeModel() != CodeModel::Small)
409 return false;
410
Dan Gohman97135e12008-09-26 19:15:30 +0000411 // RIP-relative addresses can't have additional register operands.
412 if (getTargetMachine()->symbolicAddressesAreRIPRel() &&
413 (AM.Base.Reg != 0 || AM.IndexReg != 0))
414 return false;
415
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000416 // Set up the basic address.
417 AM.GV = GV;
418 if (!isCall &&
419 TM.getRelocationModel() == Reloc::PIC_ &&
420 !Subtarget->is64Bit())
Dan Gohman57c3dac2008-09-30 00:58:23 +0000421 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000422
423 // Emit an extra load if the ABI requires it.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000424 if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) {
425 // Check to see if we've already materialized this
426 // value in a register in this block.
Dan Gohman7e8ef602008-09-19 23:42:04 +0000427 if (unsigned Reg = LocalValueMap[V]) {
428 AM.Base.Reg = Reg;
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000429 AM.GV = 0;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000430 return true;
431 }
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000432 // Issue load from stub if necessary.
433 unsigned Opc = 0;
434 const TargetRegisterClass *RC = NULL;
435 if (TLI.getPointerTy() == MVT::i32) {
436 Opc = X86::MOV32rm;
437 RC = X86::GR32RegisterClass;
438 } else {
439 Opc = X86::MOV64rm;
440 RC = X86::GR64RegisterClass;
441 }
Dan Gohman789ce772008-09-25 23:34:02 +0000442
443 X86AddressMode StubAM;
444 StubAM.Base.Reg = AM.Base.Reg;
445 StubAM.GV = AM.GV;
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000446 unsigned ResultReg = createResultReg(RC);
Dan Gohman789ce772008-09-25 23:34:02 +0000447 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), StubAM);
448
449 // Now construct the final address. Note that the Disp, Scale,
450 // and Index values may already be set here.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000451 AM.Base.Reg = ResultReg;
452 AM.GV = 0;
Dan Gohman789ce772008-09-25 23:34:02 +0000453
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000454 // Prevent loading GV stub multiple times in same MBB.
455 LocalValueMap[V] = AM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000456 }
457 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000458 }
459
Dan Gohman97135e12008-09-26 19:15:30 +0000460 // If all else fails, try to materialize the value in a register.
Dan Gohman7962e852008-09-29 21:13:15 +0000461 if (!AM.GV || !getTargetMachine()->symbolicAddressesAreRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000462 if (AM.Base.Reg == 0) {
463 AM.Base.Reg = getRegForValue(V);
464 return AM.Base.Reg != 0;
465 }
466 if (AM.IndexReg == 0) {
467 assert(AM.Scale == 1 && "Scale with no index!");
468 AM.IndexReg = getRegForValue(V);
469 return AM.IndexReg != 0;
470 }
471 }
472
473 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000474}
475
Owen Andersona3971df2008-09-04 07:08:58 +0000476/// X86SelectStore - Select and emit code to implement store instructions.
477bool X86FastISel::X86SelectStore(Instruction* I) {
Evan Cheng24e3a902008-09-08 06:35:17 +0000478 MVT VT;
479 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
Owen Andersona3971df2008-09-04 07:08:58 +0000480 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000481 unsigned Val = getRegForValue(I->getOperand(0));
482 if (Val == 0)
Owen Andersona3971df2008-09-04 07:08:58 +0000483 // Unhandled operand. Halt "fast" selection and bail.
484 return false;
485
Dan Gohman0586d912008-09-10 20:11:02 +0000486 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000487 if (!X86SelectAddress(I->getOperand(1), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000488 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000489
Dan Gohman0586d912008-09-10 20:11:02 +0000490 return X86FastEmitStore(VT, Val, AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000491}
492
Evan Cheng8b19e562008-09-03 06:44:39 +0000493/// X86SelectLoad - Select and emit code to implement load instructions.
494///
Dan Gohman3df24e62008-09-03 23:12:08 +0000495bool X86FastISel::X86SelectLoad(Instruction *I) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000496 MVT VT;
497 if (!isTypeLegal(I->getType(), TLI, VT))
Evan Cheng8b19e562008-09-03 06:44:39 +0000498 return false;
499
Dan Gohman0586d912008-09-10 20:11:02 +0000500 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000501 if (!X86SelectAddress(I->getOperand(0), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000502 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000503
Evan Cheng0de588f2008-09-05 21:00:03 +0000504 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000505 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000506 UpdateValueMap(I, ResultReg);
507 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000508 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000509 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000510}
511
Dan Gohmand98d6202008-10-02 22:15:21 +0000512unsigned X86FastISel::X86ChooseCmpOpcode(MVT VT) {
513 switch (VT.getSimpleVT()) {
514 case MVT::i8: return X86::CMP8rr;
515 case MVT::i16: return X86::CMP16rr;
516 case MVT::i32: return X86::CMP32rr;
517 case MVT::i64: return X86::CMP64rr;
518 case MVT::f32: return X86::UCOMISSrr;
519 case MVT::f64: return X86::UCOMISDrr;
520 default: break;
521 }
522 return 0;
523}
524
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000525bool X86FastISel::X86SelectCmp(Instruction *I) {
526 CmpInst *CI = cast<CmpInst>(I);
527
Dan Gohman9b66d732008-09-30 00:48:39 +0000528 MVT VT;
529 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000530 return false;
531
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000532 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000533 if (Op0Reg == 0) return false;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000534 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000535 if (Op1Reg == 0) return false;
536
Dan Gohmand98d6202008-10-02 22:15:21 +0000537 unsigned Opc = X86ChooseCmpOpcode(VT);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000538
539 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
540 switch (CI->getPredicate()) {
541 case CmpInst::FCMP_OEQ: {
542 unsigned EReg = createResultReg(&X86::GR8RegClass);
543 unsigned NPReg = createResultReg(&X86::GR8RegClass);
544 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
545 BuildMI(MBB, TII.get(X86::SETEr), EReg);
546 BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
547 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
548 break;
549 }
550 case CmpInst::FCMP_UNE: {
551 unsigned NEReg = createResultReg(&X86::GR8RegClass);
552 unsigned PReg = createResultReg(&X86::GR8RegClass);
553 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
554 BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
555 BuildMI(MBB, TII.get(X86::SETPr), PReg);
556 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
557 break;
558 }
559 case CmpInst::FCMP_OGT:
560 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
561 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
562 break;
563 case CmpInst::FCMP_OGE:
564 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
565 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
566 break;
567 case CmpInst::FCMP_OLT:
568 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
569 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
570 break;
571 case CmpInst::FCMP_OLE:
572 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
573 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
574 break;
575 case CmpInst::FCMP_ONE:
576 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
577 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
578 break;
579 case CmpInst::FCMP_ORD:
580 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
581 BuildMI(MBB, TII.get(X86::SETNPr), ResultReg);
582 break;
583 case CmpInst::FCMP_UNO:
584 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
585 BuildMI(MBB, TII.get(X86::SETPr), ResultReg);
586 break;
587 case CmpInst::FCMP_UEQ:
588 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
589 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
590 break;
591 case CmpInst::FCMP_UGT:
592 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
593 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
594 break;
595 case CmpInst::FCMP_UGE:
596 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
597 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
598 break;
599 case CmpInst::FCMP_ULT:
600 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
601 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
602 break;
603 case CmpInst::FCMP_ULE:
604 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
605 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
606 break;
607 case CmpInst::ICMP_EQ:
608 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
609 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
610 break;
611 case CmpInst::ICMP_NE:
612 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
613 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
614 break;
615 case CmpInst::ICMP_UGT:
616 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
617 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
618 break;
619 case CmpInst::ICMP_UGE:
620 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
621 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
622 break;
623 case CmpInst::ICMP_ULT:
624 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
625 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
626 break;
627 case CmpInst::ICMP_ULE:
628 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
629 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
630 break;
631 case CmpInst::ICMP_SGT:
632 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
633 BuildMI(MBB, TII.get(X86::SETGr), ResultReg);
634 break;
635 case CmpInst::ICMP_SGE:
636 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
637 BuildMI(MBB, TII.get(X86::SETGEr), ResultReg);
638 break;
639 case CmpInst::ICMP_SLT:
640 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
641 BuildMI(MBB, TII.get(X86::SETLr), ResultReg);
642 break;
643 case CmpInst::ICMP_SLE:
644 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
645 BuildMI(MBB, TII.get(X86::SETLEr), ResultReg);
646 break;
647 default:
648 return false;
649 }
650
651 UpdateValueMap(I, ResultReg);
652 return true;
653}
Evan Cheng8b19e562008-09-03 06:44:39 +0000654
Dan Gohmand89ae992008-09-05 01:06:14 +0000655bool X86FastISel::X86SelectZExt(Instruction *I) {
656 // Special-case hack: The only i1 values we know how to produce currently
657 // set the upper bits of an i8 value to zero.
658 if (I->getType() == Type::Int8Ty &&
659 I->getOperand(0)->getType() == Type::Int1Ty) {
660 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000661 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000662 UpdateValueMap(I, ResultReg);
663 return true;
664 }
665
666 return false;
667}
668
669bool X86FastISel::X86SelectBranch(Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000670 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000671 // Handle a conditional branch.
672 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000673 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
674 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
675
Dan Gohmand98d6202008-10-02 22:15:21 +0000676 // Fold the common case of a conditional branch with a comparison.
677 if (CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
678 if (CI->hasOneUse()) {
679 MVT VT = TLI.getValueType(CI->getOperand(0)->getType());
680 unsigned Opc = X86ChooseCmpOpcode(VT);
681 if (Opc == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000682
Dan Gohmand98d6202008-10-02 22:15:21 +0000683 // Try to take advantage of fallthrough opportunities.
684 CmpInst::Predicate Predicate = CI->getPredicate();
685 if (MBB->isLayoutSuccessor(TrueMBB)) {
686 std::swap(TrueMBB, FalseMBB);
687 Predicate = CmpInst::getInversePredicate(Predicate);
688 }
689
690 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
691 if (Op0Reg == 0) return false;
692 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
693 if (Op1Reg == 0) return false;
694
695 switch (Predicate) {
696 case CmpInst::FCMP_OGT:
697 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
698 BuildMI(MBB, TII.get(X86::JA)).addMBB(TrueMBB);
699 break;
700 case CmpInst::FCMP_OGE:
701 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
702 BuildMI(MBB, TII.get(X86::JAE)).addMBB(TrueMBB);
703 break;
704 case CmpInst::FCMP_OLT:
705 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
706 BuildMI(MBB, TII.get(X86::JA)).addMBB(TrueMBB);
707 break;
708 case CmpInst::FCMP_OLE:
709 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
710 BuildMI(MBB, TII.get(X86::JAE)).addMBB(TrueMBB);
711 break;
712 case CmpInst::FCMP_ONE:
713 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
714 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
715 break;
716 case CmpInst::FCMP_ORD:
717 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
718 BuildMI(MBB, TII.get(X86::JNP)).addMBB(TrueMBB);
719 break;
720 case CmpInst::FCMP_UNO:
721 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
722 BuildMI(MBB, TII.get(X86::JP)).addMBB(TrueMBB);
723 break;
724 case CmpInst::FCMP_UEQ:
725 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
726 BuildMI(MBB, TII.get(X86::JE)).addMBB(TrueMBB);
727 break;
728 case CmpInst::FCMP_UGT:
729 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
730 BuildMI(MBB, TII.get(X86::JB)).addMBB(TrueMBB);
731 break;
732 case CmpInst::FCMP_UGE:
733 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
734 BuildMI(MBB, TII.get(X86::JBE)).addMBB(TrueMBB);
735 break;
736 case CmpInst::FCMP_ULT:
737 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
738 BuildMI(MBB, TII.get(X86::JB)).addMBB(TrueMBB);
739 break;
740 case CmpInst::FCMP_ULE:
741 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
742 BuildMI(MBB, TII.get(X86::JBE)).addMBB(TrueMBB);
743 break;
744 case CmpInst::ICMP_EQ:
745 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
746 BuildMI(MBB, TII.get(X86::JE)).addMBB(TrueMBB);
747 break;
748 case CmpInst::ICMP_NE:
749 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
750 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
751 break;
752 case CmpInst::ICMP_UGT:
753 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
754 BuildMI(MBB, TII.get(X86::JA)).addMBB(TrueMBB);
755 break;
756 case CmpInst::ICMP_UGE:
757 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
758 BuildMI(MBB, TII.get(X86::JAE)).addMBB(TrueMBB);
759 break;
760 case CmpInst::ICMP_ULT:
761 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
762 BuildMI(MBB, TII.get(X86::JB)).addMBB(TrueMBB);
763 break;
764 case CmpInst::ICMP_ULE:
765 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
766 BuildMI(MBB, TII.get(X86::JBE)).addMBB(TrueMBB);
767 break;
768 case CmpInst::ICMP_SGT:
769 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
770 BuildMI(MBB, TII.get(X86::JG)).addMBB(TrueMBB);
771 break;
772 case CmpInst::ICMP_SGE:
773 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
774 BuildMI(MBB, TII.get(X86::JGE)).addMBB(TrueMBB);
775 break;
776 case CmpInst::ICMP_SLT:
777 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
778 BuildMI(MBB, TII.get(X86::JL)).addMBB(TrueMBB);
779 break;
780 case CmpInst::ICMP_SLE:
781 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
782 BuildMI(MBB, TII.get(X86::JLE)).addMBB(TrueMBB);
783 break;
784 default:
785 return false;
786 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000787 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000788 MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000789 return true;
790 }
791 }
792
793 // Otherwise do a clumsy setcc and re-test it.
794 unsigned OpReg = getRegForValue(BI->getCondition());
795 if (OpReg == 0) return false;
796
797 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
798
799 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000800 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000801 MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +0000802
803 return true;
804}
805
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000806bool X86FastISel::X86SelectShift(Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000807 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000808 const TargetRegisterClass *RC = NULL;
809 if (I->getType() == Type::Int8Ty) {
810 CReg = X86::CL;
811 RC = &X86::GR8RegClass;
812 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000813 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
814 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
815 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000816 default: return false;
817 }
818 } else if (I->getType() == Type::Int16Ty) {
819 CReg = X86::CX;
820 RC = &X86::GR16RegClass;
821 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000822 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
823 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
824 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000825 default: return false;
826 }
827 } else if (I->getType() == Type::Int32Ty) {
828 CReg = X86::ECX;
829 RC = &X86::GR32RegClass;
830 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000831 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
832 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
833 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000834 default: return false;
835 }
836 } else if (I->getType() == Type::Int64Ty) {
837 CReg = X86::RCX;
838 RC = &X86::GR64RegClass;
839 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000840 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
841 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
842 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000843 default: return false;
844 }
845 } else {
846 return false;
847 }
848
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000849 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
Dan Gohman9b66d732008-09-30 00:48:39 +0000850 if (VT == MVT::Other || !isTypeLegal(I->getType(), TLI, VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000851 return false;
852
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000853 unsigned Op0Reg = getRegForValue(I->getOperand(0));
854 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +0000855
856 // Fold immediate in shl(x,3).
857 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
858 unsigned ResultReg = createResultReg(RC);
859 BuildMI(MBB, TII.get(OpImm),
860 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue());
861 UpdateValueMap(I, ResultReg);
862 return true;
863 }
864
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000865 unsigned Op1Reg = getRegForValue(I->getOperand(1));
866 if (Op1Reg == 0) return false;
867 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
Dan Gohman145b8282008-10-07 21:50:36 +0000868
869 // The shift instruction uses X86::CL. If we defined a super-register
870 // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what
871 // we're doing here.
872 if (CReg != X86::CL)
873 BuildMI(MBB, TII.get(TargetInstrInfo::EXTRACT_SUBREG), X86::CL)
874 .addReg(CReg).addImm(X86::SUBREG_8BIT);
875
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000876 unsigned ResultReg = createResultReg(RC);
Dan Gohman145b8282008-10-07 21:50:36 +0000877 BuildMI(MBB, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000878 UpdateValueMap(I, ResultReg);
879 return true;
880}
881
882bool X86FastISel::X86SelectSelect(Instruction *I) {
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000883 const Type *Ty = I->getType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000884 if (isa<PointerType>(Ty))
Dan Gohman1fbc3cd2008-09-18 18:26:43 +0000885 Ty = TD.getIntPtrType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000886
887 unsigned Opc = 0;
888 const TargetRegisterClass *RC = NULL;
889 if (Ty == Type::Int16Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000890 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000891 RC = &X86::GR16RegClass;
892 } else if (Ty == Type::Int32Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000893 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000894 RC = &X86::GR32RegClass;
895 } else if (Ty == Type::Int64Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000896 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000897 RC = &X86::GR64RegClass;
898 } else {
899 return false;
900 }
901
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000902 MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
Dan Gohman9b66d732008-09-30 00:48:39 +0000903 if (VT == MVT::Other || !isTypeLegal(Ty, TLI, VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000904 return false;
905
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000906 unsigned Op0Reg = getRegForValue(I->getOperand(0));
907 if (Op0Reg == 0) return false;
908 unsigned Op1Reg = getRegForValue(I->getOperand(1));
909 if (Op1Reg == 0) return false;
910 unsigned Op2Reg = getRegForValue(I->getOperand(2));
911 if (Op2Reg == 0) return false;
912
913 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
914 unsigned ResultReg = createResultReg(RC);
915 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
916 UpdateValueMap(I, ResultReg);
917 return true;
918}
919
Dan Gohman78efce62008-09-10 21:02:08 +0000920bool X86FastISel::X86SelectFPExt(Instruction *I) {
921 if (Subtarget->hasSSE2()) {
922 if (I->getType() == Type::DoubleTy) {
923 Value *V = I->getOperand(0);
924 if (V->getType() == Type::FloatTy) {
925 unsigned OpReg = getRegForValue(V);
926 if (OpReg == 0) return false;
927 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
928 BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
929 UpdateValueMap(I, ResultReg);
930 return true;
931 }
932 }
933 }
934
935 return false;
936}
937
938bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
939 if (Subtarget->hasSSE2()) {
940 if (I->getType() == Type::FloatTy) {
941 Value *V = I->getOperand(0);
942 if (V->getType() == Type::DoubleTy) {
943 unsigned OpReg = getRegForValue(V);
944 if (OpReg == 0) return false;
945 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
946 BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
947 UpdateValueMap(I, ResultReg);
948 return true;
949 }
950 }
951 }
952
953 return false;
954}
955
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000956bool X86FastISel::X86SelectTrunc(Instruction *I) {
957 if (Subtarget->is64Bit())
958 // All other cases should be handled by the tblgen generated code.
959 return false;
960 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
961 MVT DstVT = TLI.getValueType(I->getType());
962 if (DstVT != MVT::i8)
963 // All other cases should be handled by the tblgen generated code.
964 return false;
965 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
966 // All other cases should be handled by the tblgen generated code.
967 return false;
968
969 unsigned InputReg = getRegForValue(I->getOperand(0));
970 if (!InputReg)
971 // Unhandled operand. Halt "fast" selection and bail.
972 return false;
973
974 // First issue a copy to GR16_ or GR32_.
975 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
976 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
977 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
978 unsigned CopyReg = createResultReg(CopyRC);
979 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
980
981 // Then issue an extract_subreg.
Dan Gohman145b8282008-10-07 21:50:36 +0000982 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg, X86::SUBREG_8BIT);
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000983 if (!ResultReg)
984 return false;
985
986 UpdateValueMap(I, ResultReg);
987 return true;
988}
989
Evan Chengf3d4efe2008-09-07 09:09:33 +0000990bool X86FastISel::X86SelectCall(Instruction *I) {
991 CallInst *CI = cast<CallInst>(I);
992 Value *Callee = I->getOperand(0);
993
994 // Can't handle inline asm yet.
995 if (isa<InlineAsm>(Callee))
996 return false;
997
998 // FIXME: Handle some intrinsics.
999 if (Function *F = CI->getCalledFunction()) {
1000 if (F->isDeclaration() &&F->getIntrinsicID())
1001 return false;
1002 }
1003
Evan Chengf3d4efe2008-09-07 09:09:33 +00001004 // Handle only C and fastcc calling conventions for now.
1005 CallSite CS(CI);
1006 unsigned CC = CS.getCallingConv();
1007 if (CC != CallingConv::C &&
1008 CC != CallingConv::Fast &&
1009 CC != CallingConv::X86_FastCall)
1010 return false;
1011
1012 // Let SDISel handle vararg functions.
1013 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1014 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1015 if (FTy->isVarArg())
1016 return false;
1017
1018 // Handle *simple* calls for now.
1019 const Type *RetTy = CS.getType();
1020 MVT RetVT;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001021 if (RetTy == Type::VoidTy)
1022 RetVT = MVT::isVoid;
1023 else if (!isTypeLegal(RetTy, TLI, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001024 return false;
1025
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001026 // Materialize callee address in a register. FIXME: GV address can be
1027 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001028 X86AddressMode CalleeAM;
1029 if (!X86SelectAddress(Callee, CalleeAM, true))
1030 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001031 unsigned CalleeOp = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001032 GlobalValue *GV = 0;
1033 if (CalleeAM.Base.Reg != 0) {
1034 assert(CalleeAM.GV == 0);
1035 CalleeOp = CalleeAM.Base.Reg;
1036 } else if (CalleeAM.GV != 0) {
1037 assert(CalleeAM.GV != 0);
1038 GV = CalleeAM.GV;
1039 } else
1040 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001041
Evan Chengdebdea02008-09-08 17:15:42 +00001042 // Allow calls which produce i1 results.
1043 bool AndToI1 = false;
1044 if (RetVT == MVT::i1) {
1045 RetVT = MVT::i8;
1046 AndToI1 = true;
1047 }
1048
Evan Chengf3d4efe2008-09-07 09:09:33 +00001049 // Deal with call operands first.
1050 SmallVector<unsigned, 4> Args;
1051 SmallVector<MVT, 4> ArgVTs;
1052 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
1053 Args.reserve(CS.arg_size());
1054 ArgVTs.reserve(CS.arg_size());
1055 ArgFlags.reserve(CS.arg_size());
1056 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1057 i != e; ++i) {
1058 unsigned Arg = getRegForValue(*i);
1059 if (Arg == 0)
1060 return false;
1061 ISD::ArgFlagsTy Flags;
1062 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001063 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001064 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001065 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001066 Flags.setZExt();
1067
1068 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001069 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1070 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1071 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1072 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001073 return false;
1074
1075 const Type *ArgTy = (*i)->getType();
1076 MVT ArgVT;
1077 if (!isTypeLegal(ArgTy, TLI, ArgVT))
1078 return false;
1079 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1080 Flags.setOrigAlign(OriginalAlignment);
1081
1082 Args.push_back(Arg);
1083 ArgVTs.push_back(ArgVT);
1084 ArgFlags.push_back(Flags);
1085 }
1086
1087 // Analyze operands of the call, assigning locations to each operand.
1088 SmallVector<CCValAssign, 16> ArgLocs;
1089 CCState CCInfo(CC, false, TM, ArgLocs);
1090 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1091
1092 // Get a count of how many bytes are to be pushed on the stack.
1093 unsigned NumBytes = CCInfo.getNextStackOffset();
1094
1095 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001096 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
1097 BuildMI(MBB, TII.get(AdjStackDown)).addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001098
1099 // Process argumenet: walk the register/memloc assignments, inserting
1100 // copies / loads.
1101 SmallVector<unsigned, 4> RegArgs;
1102 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1103 CCValAssign &VA = ArgLocs[i];
1104 unsigned Arg = Args[VA.getValNo()];
1105 MVT ArgVT = ArgVTs[VA.getValNo()];
1106
1107 // Promote the value if needed.
1108 switch (VA.getLocInfo()) {
1109 default: assert(0 && "Unknown loc info!");
1110 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001111 case CCValAssign::SExt: {
1112 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1113 Arg, ArgVT, Arg);
1114 assert(Emitted && "Failed to emit a sext!");
1115 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001116 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001117 }
1118 case CCValAssign::ZExt: {
1119 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1120 Arg, ArgVT, Arg);
1121 assert(Emitted && "Failed to emit a zext!");
1122 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001123 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001124 }
1125 case CCValAssign::AExt: {
1126 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1127 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001128 if (!Emitted)
1129 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1130 Arg, ArgVT, Arg);
1131 if (!Emitted)
1132 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1133 Arg, ArgVT, Arg);
1134
Evan Cheng24e3a902008-09-08 06:35:17 +00001135 assert(Emitted && "Failed to emit a aext!");
1136 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001137 break;
1138 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001139 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001140
1141 if (VA.isRegLoc()) {
1142 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
1143 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
1144 Arg, RC, RC);
1145 assert(Emitted && "Failed to emit a copy instruction!");
1146 RegArgs.push_back(VA.getLocReg());
1147 } else {
1148 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001149 X86AddressMode AM;
1150 AM.Base.Reg = StackPtr;
1151 AM.Disp = LocMemOffset;
1152 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001153 }
1154 }
1155
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001156 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1157 // GOT pointer.
1158 if (!Subtarget->is64Bit() &&
1159 TM.getRelocationModel() == Reloc::PIC_ &&
1160 Subtarget->isPICStyleGOT()) {
1161 TargetRegisterClass *RC = X86::GR32RegisterClass;
Dan Gohman57c3dac2008-09-30 00:58:23 +00001162 unsigned Base = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001163 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
1164 assert(Emitted && "Failed to emit a copy instruction!");
1165 }
1166
Evan Chengf3d4efe2008-09-07 09:09:33 +00001167 // Issue the call.
1168 unsigned CallOpc = CalleeOp
1169 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
1170 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
1171 MachineInstrBuilder MIB = CalleeOp
1172 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001173 : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001174
1175 // Add an implicit use GOT pointer in EBX.
1176 if (!Subtarget->is64Bit() &&
1177 TM.getRelocationModel() == Reloc::PIC_ &&
1178 Subtarget->isPICStyleGOT())
1179 MIB.addReg(X86::EBX);
1180
Evan Chengf3d4efe2008-09-07 09:09:33 +00001181 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001182 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1183 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001184
1185 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001186 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
1187 BuildMI(MBB, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001188
1189 // Now handle call return value (if any).
Evan Chengf3d4efe2008-09-07 09:09:33 +00001190 if (RetVT.getSimpleVT() != MVT::isVoid) {
1191 SmallVector<CCValAssign, 16> RVLocs;
1192 CCState CCInfo(CC, false, TM, RVLocs);
1193 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1194
1195 // Copy all of the result registers out of their specified physreg.
1196 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
1197 MVT CopyVT = RVLocs[0].getValVT();
1198 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1199 TargetRegisterClass *SrcRC = DstRC;
1200
1201 // If this is a call to a function that returns an fp value on the x87 fp
1202 // stack, but where we prefer to use the value in xmm registers, copy it
1203 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1204 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1205 RVLocs[0].getLocReg() == X86::ST1) &&
1206 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
1207 CopyVT = MVT::f80;
1208 SrcRC = X86::RSTRegisterClass;
1209 DstRC = X86::RFP80RegisterClass;
1210 }
1211
1212 unsigned ResultReg = createResultReg(DstRC);
1213 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1214 RVLocs[0].getLocReg(), DstRC, SrcRC);
1215 assert(Emitted && "Failed to emit a copy instruction!");
1216 if (CopyVT != RVLocs[0].getValVT()) {
1217 // Round the F80 the right size, which also moves to the appropriate xmm
1218 // register. This is accomplished by storing the F80 value in memory and
1219 // then loading it back. Ewww...
1220 MVT ResVT = RVLocs[0].getValVT();
1221 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1222 unsigned MemSize = ResVT.getSizeInBits()/8;
Dan Gohman0586d912008-09-10 20:11:02 +00001223 int FI = MFI.CreateStackObject(MemSize, MemSize);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001224 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
1225 DstRC = ResVT == MVT::f32
1226 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1227 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1228 ResultReg = createResultReg(DstRC);
1229 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
1230 }
1231
Evan Chengdebdea02008-09-08 17:15:42 +00001232 if (AndToI1) {
1233 // Mask out all but lowest bit for some call which produces an i1.
1234 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
1235 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
1236 ResultReg = AndResult;
1237 }
1238
Evan Chengf3d4efe2008-09-07 09:09:33 +00001239 UpdateValueMap(I, ResultReg);
1240 }
1241
1242 return true;
1243}
1244
1245
Dan Gohman99b21822008-08-28 23:21:34 +00001246bool
Dan Gohman3df24e62008-09-03 23:12:08 +00001247X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001248 switch (I->getOpcode()) {
1249 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001250 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001251 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001252 case Instruction::Store:
1253 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001254 case Instruction::ICmp:
1255 case Instruction::FCmp:
1256 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001257 case Instruction::ZExt:
1258 return X86SelectZExt(I);
1259 case Instruction::Br:
1260 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001261 case Instruction::Call:
1262 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001263 case Instruction::LShr:
1264 case Instruction::AShr:
1265 case Instruction::Shl:
1266 return X86SelectShift(I);
1267 case Instruction::Select:
1268 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001269 case Instruction::Trunc:
1270 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001271 case Instruction::FPExt:
1272 return X86SelectFPExt(I);
1273 case Instruction::FPTrunc:
1274 return X86SelectFPTrunc(I);
Dan Gohman99b21822008-08-28 23:21:34 +00001275 }
1276
1277 return false;
1278}
1279
Dan Gohman0586d912008-09-10 20:11:02 +00001280unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Evan Cheng59fbc802008-09-09 01:26:59 +00001281 MVT VT;
1282 if (!isTypeLegal(C->getType(), TLI, VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001283 return false;
1284
1285 // Get opcode and regclass of the output for the given load instruction.
1286 unsigned Opc = 0;
1287 const TargetRegisterClass *RC = NULL;
1288 switch (VT.getSimpleVT()) {
1289 default: return false;
1290 case MVT::i8:
1291 Opc = X86::MOV8rm;
1292 RC = X86::GR8RegisterClass;
1293 break;
1294 case MVT::i16:
1295 Opc = X86::MOV16rm;
1296 RC = X86::GR16RegisterClass;
1297 break;
1298 case MVT::i32:
1299 Opc = X86::MOV32rm;
1300 RC = X86::GR32RegisterClass;
1301 break;
1302 case MVT::i64:
1303 // Must be in x86-64 mode.
1304 Opc = X86::MOV64rm;
1305 RC = X86::GR64RegisterClass;
1306 break;
1307 case MVT::f32:
1308 if (Subtarget->hasSSE1()) {
1309 Opc = X86::MOVSSrm;
1310 RC = X86::FR32RegisterClass;
1311 } else {
1312 Opc = X86::LD_Fp32m;
1313 RC = X86::RFP32RegisterClass;
1314 }
1315 break;
1316 case MVT::f64:
1317 if (Subtarget->hasSSE2()) {
1318 Opc = X86::MOVSDrm;
1319 RC = X86::FR64RegisterClass;
1320 } else {
1321 Opc = X86::LD_Fp64m;
1322 RC = X86::RFP64RegisterClass;
1323 }
1324 break;
1325 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001326 // No f80 support yet.
1327 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001328 }
1329
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001330 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001331 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001332 X86AddressMode AM;
1333 if (X86SelectAddress(C, AM, false)) {
1334 if (TLI.getPointerTy() == MVT::i32)
1335 Opc = X86::LEA32r;
1336 else
1337 Opc = X86::LEA64r;
1338 unsigned ResultReg = createResultReg(RC);
1339 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001340 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001341 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001342 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001343 }
1344
Owen Anderson3b217c62008-09-06 01:11:01 +00001345 // MachineConstantPool wants an explicit alignment.
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001346 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001347 if (Align == 0) {
1348 // Alignment of vector types. FIXME!
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001349 Align = TD.getABITypeSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001350 Align = Log2_64(Align);
1351 }
Owen Anderson95267a12008-09-05 00:06:23 +00001352
Dan Gohman5396c992008-09-30 01:21:32 +00001353 // x86-32 PIC requires a PIC base register for constant pools.
1354 unsigned PICBase = 0;
1355 if (TM.getRelocationModel() == Reloc::PIC_ &&
1356 !Subtarget->is64Bit())
1357 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1358
1359 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001360 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001361 unsigned ResultReg = createResultReg(RC);
Dan Gohman5396c992008-09-30 01:21:32 +00001362 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset,
1363 PICBase);
1364
Owen Anderson95267a12008-09-05 00:06:23 +00001365 return ResultReg;
1366}
1367
Dan Gohman0586d912008-09-10 20:11:02 +00001368unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001369 // Fail on dynamic allocas. At this point, getRegForValue has already
1370 // checked its CSE maps, so if we're here trying to handle a dynamic
1371 // alloca, we're not going to succeed. X86SelectAddress has a
1372 // check for dynamic allocas, because it's called directly from
1373 // various places, but TargetMaterializeAlloca also needs a check
1374 // in order to avoid recursion between getRegForValue,
1375 // X86SelectAddrss, and TargetMaterializeAlloca.
1376 if (!StaticAllocaMap.count(C))
1377 return 0;
1378
Dan Gohman0586d912008-09-10 20:11:02 +00001379 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001380 if (!X86SelectAddress(C, AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +00001381 return 0;
1382 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1383 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1384 unsigned ResultReg = createResultReg(RC);
1385 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1386 return ResultReg;
1387}
1388
Evan Chengc3f44b02008-09-03 00:03:49 +00001389namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001390 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001391 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +00001392 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001393 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
1394 DenseMap<const AllocaInst *, int> &am) {
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001395 return new X86FastISel(mf, mmi, vm, bm, am);
Evan Chengc3f44b02008-09-03 00:03:49 +00001396 }
Dan Gohman99b21822008-08-28 23:21:34 +00001397}