blob: 1f2896c8b0ba63ca0abfa330209d1b5af8c89134 [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 }
787 MBB->addSuccessor(TrueMBB);
788 FastEmitBranch(FalseMBB);
789 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 Gohmand89ae992008-09-05 01:06:14 +0000800 MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000801
802 FastEmitBranch(FalseMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +0000803
804 return true;
805}
806
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000807bool X86FastISel::X86SelectShift(Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000808 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000809 const TargetRegisterClass *RC = NULL;
810 if (I->getType() == Type::Int8Ty) {
811 CReg = X86::CL;
812 RC = &X86::GR8RegClass;
813 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000814 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
815 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
816 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000817 default: return false;
818 }
819 } else if (I->getType() == Type::Int16Ty) {
820 CReg = X86::CX;
821 RC = &X86::GR16RegClass;
822 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000823 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
824 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
825 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000826 default: return false;
827 }
828 } else if (I->getType() == Type::Int32Ty) {
829 CReg = X86::ECX;
830 RC = &X86::GR32RegClass;
831 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000832 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
833 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
834 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000835 default: return false;
836 }
837 } else if (I->getType() == Type::Int64Ty) {
838 CReg = X86::RCX;
839 RC = &X86::GR64RegClass;
840 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000841 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
842 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
843 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000844 default: return false;
845 }
846 } else {
847 return false;
848 }
849
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000850 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
Dan Gohman9b66d732008-09-30 00:48:39 +0000851 if (VT == MVT::Other || !isTypeLegal(I->getType(), TLI, VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000852 return false;
853
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000854 unsigned Op0Reg = getRegForValue(I->getOperand(0));
855 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +0000856
857 // Fold immediate in shl(x,3).
858 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
859 unsigned ResultReg = createResultReg(RC);
860 BuildMI(MBB, TII.get(OpImm),
861 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue());
862 UpdateValueMap(I, ResultReg);
863 return true;
864 }
865
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000866 unsigned Op1Reg = getRegForValue(I->getOperand(1));
867 if (Op1Reg == 0) return false;
868 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
Dan Gohman145b8282008-10-07 21:50:36 +0000869
870 // The shift instruction uses X86::CL. If we defined a super-register
871 // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what
872 // we're doing here.
873 if (CReg != X86::CL)
874 BuildMI(MBB, TII.get(TargetInstrInfo::EXTRACT_SUBREG), X86::CL)
875 .addReg(CReg).addImm(X86::SUBREG_8BIT);
876
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000877 unsigned ResultReg = createResultReg(RC);
Dan Gohman145b8282008-10-07 21:50:36 +0000878 BuildMI(MBB, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000879 UpdateValueMap(I, ResultReg);
880 return true;
881}
882
883bool X86FastISel::X86SelectSelect(Instruction *I) {
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000884 const Type *Ty = I->getType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000885 if (isa<PointerType>(Ty))
Dan Gohman1fbc3cd2008-09-18 18:26:43 +0000886 Ty = TD.getIntPtrType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000887
888 unsigned Opc = 0;
889 const TargetRegisterClass *RC = NULL;
890 if (Ty == Type::Int16Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000891 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000892 RC = &X86::GR16RegClass;
893 } else if (Ty == Type::Int32Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000894 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000895 RC = &X86::GR32RegClass;
896 } else if (Ty == Type::Int64Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000897 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000898 RC = &X86::GR64RegClass;
899 } else {
900 return false;
901 }
902
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000903 MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
Dan Gohman9b66d732008-09-30 00:48:39 +0000904 if (VT == MVT::Other || !isTypeLegal(Ty, TLI, VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000905 return false;
906
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000907 unsigned Op0Reg = getRegForValue(I->getOperand(0));
908 if (Op0Reg == 0) return false;
909 unsigned Op1Reg = getRegForValue(I->getOperand(1));
910 if (Op1Reg == 0) return false;
911 unsigned Op2Reg = getRegForValue(I->getOperand(2));
912 if (Op2Reg == 0) return false;
913
914 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
915 unsigned ResultReg = createResultReg(RC);
916 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
917 UpdateValueMap(I, ResultReg);
918 return true;
919}
920
Dan Gohman78efce62008-09-10 21:02:08 +0000921bool X86FastISel::X86SelectFPExt(Instruction *I) {
922 if (Subtarget->hasSSE2()) {
923 if (I->getType() == Type::DoubleTy) {
924 Value *V = I->getOperand(0);
925 if (V->getType() == Type::FloatTy) {
926 unsigned OpReg = getRegForValue(V);
927 if (OpReg == 0) return false;
928 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
929 BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
930 UpdateValueMap(I, ResultReg);
931 return true;
932 }
933 }
934 }
935
936 return false;
937}
938
939bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
940 if (Subtarget->hasSSE2()) {
941 if (I->getType() == Type::FloatTy) {
942 Value *V = I->getOperand(0);
943 if (V->getType() == Type::DoubleTy) {
944 unsigned OpReg = getRegForValue(V);
945 if (OpReg == 0) return false;
946 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
947 BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
948 UpdateValueMap(I, ResultReg);
949 return true;
950 }
951 }
952 }
953
954 return false;
955}
956
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000957bool X86FastISel::X86SelectTrunc(Instruction *I) {
958 if (Subtarget->is64Bit())
959 // All other cases should be handled by the tblgen generated code.
960 return false;
961 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
962 MVT DstVT = TLI.getValueType(I->getType());
963 if (DstVT != MVT::i8)
964 // All other cases should be handled by the tblgen generated code.
965 return false;
966 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
967 // All other cases should be handled by the tblgen generated code.
968 return false;
969
970 unsigned InputReg = getRegForValue(I->getOperand(0));
971 if (!InputReg)
972 // Unhandled operand. Halt "fast" selection and bail.
973 return false;
974
975 // First issue a copy to GR16_ or GR32_.
976 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
977 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
978 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
979 unsigned CopyReg = createResultReg(CopyRC);
980 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
981
982 // Then issue an extract_subreg.
Dan Gohman145b8282008-10-07 21:50:36 +0000983 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg, X86::SUBREG_8BIT);
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000984 if (!ResultReg)
985 return false;
986
987 UpdateValueMap(I, ResultReg);
988 return true;
989}
990
Evan Chengf3d4efe2008-09-07 09:09:33 +0000991bool X86FastISel::X86SelectCall(Instruction *I) {
992 CallInst *CI = cast<CallInst>(I);
993 Value *Callee = I->getOperand(0);
994
995 // Can't handle inline asm yet.
996 if (isa<InlineAsm>(Callee))
997 return false;
998
999 // FIXME: Handle some intrinsics.
1000 if (Function *F = CI->getCalledFunction()) {
1001 if (F->isDeclaration() &&F->getIntrinsicID())
1002 return false;
1003 }
1004
Evan Chengf3d4efe2008-09-07 09:09:33 +00001005 // Handle only C and fastcc calling conventions for now.
1006 CallSite CS(CI);
1007 unsigned CC = CS.getCallingConv();
1008 if (CC != CallingConv::C &&
1009 CC != CallingConv::Fast &&
1010 CC != CallingConv::X86_FastCall)
1011 return false;
1012
1013 // Let SDISel handle vararg functions.
1014 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1015 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1016 if (FTy->isVarArg())
1017 return false;
1018
1019 // Handle *simple* calls for now.
1020 const Type *RetTy = CS.getType();
1021 MVT RetVT;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001022 if (RetTy == Type::VoidTy)
1023 RetVT = MVT::isVoid;
1024 else if (!isTypeLegal(RetTy, TLI, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001025 return false;
1026
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001027 // Materialize callee address in a register. FIXME: GV address can be
1028 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001029 X86AddressMode CalleeAM;
1030 if (!X86SelectAddress(Callee, CalleeAM, true))
1031 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001032 unsigned CalleeOp = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001033 GlobalValue *GV = 0;
1034 if (CalleeAM.Base.Reg != 0) {
1035 assert(CalleeAM.GV == 0);
1036 CalleeOp = CalleeAM.Base.Reg;
1037 } else if (CalleeAM.GV != 0) {
1038 assert(CalleeAM.GV != 0);
1039 GV = CalleeAM.GV;
1040 } else
1041 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001042
Evan Chengdebdea02008-09-08 17:15:42 +00001043 // Allow calls which produce i1 results.
1044 bool AndToI1 = false;
1045 if (RetVT == MVT::i1) {
1046 RetVT = MVT::i8;
1047 AndToI1 = true;
1048 }
1049
Evan Chengf3d4efe2008-09-07 09:09:33 +00001050 // Deal with call operands first.
1051 SmallVector<unsigned, 4> Args;
1052 SmallVector<MVT, 4> ArgVTs;
1053 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
1054 Args.reserve(CS.arg_size());
1055 ArgVTs.reserve(CS.arg_size());
1056 ArgFlags.reserve(CS.arg_size());
1057 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1058 i != e; ++i) {
1059 unsigned Arg = getRegForValue(*i);
1060 if (Arg == 0)
1061 return false;
1062 ISD::ArgFlagsTy Flags;
1063 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001064 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001065 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001066 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001067 Flags.setZExt();
1068
1069 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001070 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1071 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1072 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1073 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001074 return false;
1075
1076 const Type *ArgTy = (*i)->getType();
1077 MVT ArgVT;
1078 if (!isTypeLegal(ArgTy, TLI, ArgVT))
1079 return false;
1080 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1081 Flags.setOrigAlign(OriginalAlignment);
1082
1083 Args.push_back(Arg);
1084 ArgVTs.push_back(ArgVT);
1085 ArgFlags.push_back(Flags);
1086 }
1087
1088 // Analyze operands of the call, assigning locations to each operand.
1089 SmallVector<CCValAssign, 16> ArgLocs;
1090 CCState CCInfo(CC, false, TM, ArgLocs);
1091 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1092
1093 // Get a count of how many bytes are to be pushed on the stack.
1094 unsigned NumBytes = CCInfo.getNextStackOffset();
1095
1096 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001097 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
1098 BuildMI(MBB, TII.get(AdjStackDown)).addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001099
1100 // Process argumenet: walk the register/memloc assignments, inserting
1101 // copies / loads.
1102 SmallVector<unsigned, 4> RegArgs;
1103 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1104 CCValAssign &VA = ArgLocs[i];
1105 unsigned Arg = Args[VA.getValNo()];
1106 MVT ArgVT = ArgVTs[VA.getValNo()];
1107
1108 // Promote the value if needed.
1109 switch (VA.getLocInfo()) {
1110 default: assert(0 && "Unknown loc info!");
1111 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001112 case CCValAssign::SExt: {
1113 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1114 Arg, ArgVT, Arg);
1115 assert(Emitted && "Failed to emit a sext!");
1116 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001117 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001118 }
1119 case CCValAssign::ZExt: {
1120 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1121 Arg, ArgVT, Arg);
1122 assert(Emitted && "Failed to emit a zext!");
1123 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001124 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001125 }
1126 case CCValAssign::AExt: {
1127 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1128 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001129 if (!Emitted)
1130 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1131 Arg, ArgVT, Arg);
1132 if (!Emitted)
1133 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1134 Arg, ArgVT, Arg);
1135
Evan Cheng24e3a902008-09-08 06:35:17 +00001136 assert(Emitted && "Failed to emit a aext!");
1137 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001138 break;
1139 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001140 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001141
1142 if (VA.isRegLoc()) {
1143 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
1144 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
1145 Arg, RC, RC);
1146 assert(Emitted && "Failed to emit a copy instruction!");
1147 RegArgs.push_back(VA.getLocReg());
1148 } else {
1149 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001150 X86AddressMode AM;
1151 AM.Base.Reg = StackPtr;
1152 AM.Disp = LocMemOffset;
1153 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001154 }
1155 }
1156
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001157 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1158 // GOT pointer.
1159 if (!Subtarget->is64Bit() &&
1160 TM.getRelocationModel() == Reloc::PIC_ &&
1161 Subtarget->isPICStyleGOT()) {
1162 TargetRegisterClass *RC = X86::GR32RegisterClass;
Dan Gohman57c3dac2008-09-30 00:58:23 +00001163 unsigned Base = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001164 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
1165 assert(Emitted && "Failed to emit a copy instruction!");
1166 }
1167
Evan Chengf3d4efe2008-09-07 09:09:33 +00001168 // Issue the call.
1169 unsigned CallOpc = CalleeOp
1170 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
1171 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
1172 MachineInstrBuilder MIB = CalleeOp
1173 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001174 : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001175
1176 // Add an implicit use GOT pointer in EBX.
1177 if (!Subtarget->is64Bit() &&
1178 TM.getRelocationModel() == Reloc::PIC_ &&
1179 Subtarget->isPICStyleGOT())
1180 MIB.addReg(X86::EBX);
1181
Evan Chengf3d4efe2008-09-07 09:09:33 +00001182 // Add implicit physical register uses to the call.
1183 while (!RegArgs.empty()) {
1184 MIB.addReg(RegArgs.back());
1185 RegArgs.pop_back();
1186 }
1187
1188 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001189 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
1190 BuildMI(MBB, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001191
1192 // Now handle call return value (if any).
Evan Chengf3d4efe2008-09-07 09:09:33 +00001193 if (RetVT.getSimpleVT() != MVT::isVoid) {
1194 SmallVector<CCValAssign, 16> RVLocs;
1195 CCState CCInfo(CC, false, TM, RVLocs);
1196 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1197
1198 // Copy all of the result registers out of their specified physreg.
1199 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
1200 MVT CopyVT = RVLocs[0].getValVT();
1201 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1202 TargetRegisterClass *SrcRC = DstRC;
1203
1204 // If this is a call to a function that returns an fp value on the x87 fp
1205 // stack, but where we prefer to use the value in xmm registers, copy it
1206 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1207 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1208 RVLocs[0].getLocReg() == X86::ST1) &&
1209 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
1210 CopyVT = MVT::f80;
1211 SrcRC = X86::RSTRegisterClass;
1212 DstRC = X86::RFP80RegisterClass;
1213 }
1214
1215 unsigned ResultReg = createResultReg(DstRC);
1216 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1217 RVLocs[0].getLocReg(), DstRC, SrcRC);
1218 assert(Emitted && "Failed to emit a copy instruction!");
1219 if (CopyVT != RVLocs[0].getValVT()) {
1220 // Round the F80 the right size, which also moves to the appropriate xmm
1221 // register. This is accomplished by storing the F80 value in memory and
1222 // then loading it back. Ewww...
1223 MVT ResVT = RVLocs[0].getValVT();
1224 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1225 unsigned MemSize = ResVT.getSizeInBits()/8;
Dan Gohman0586d912008-09-10 20:11:02 +00001226 int FI = MFI.CreateStackObject(MemSize, MemSize);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001227 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
1228 DstRC = ResVT == MVT::f32
1229 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1230 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1231 ResultReg = createResultReg(DstRC);
1232 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
1233 }
1234
Evan Chengdebdea02008-09-08 17:15:42 +00001235 if (AndToI1) {
1236 // Mask out all but lowest bit for some call which produces an i1.
1237 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
1238 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
1239 ResultReg = AndResult;
1240 }
1241
Evan Chengf3d4efe2008-09-07 09:09:33 +00001242 UpdateValueMap(I, ResultReg);
1243 }
1244
1245 return true;
1246}
1247
1248
Dan Gohman99b21822008-08-28 23:21:34 +00001249bool
Dan Gohman3df24e62008-09-03 23:12:08 +00001250X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001251 switch (I->getOpcode()) {
1252 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001253 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001254 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001255 case Instruction::Store:
1256 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001257 case Instruction::ICmp:
1258 case Instruction::FCmp:
1259 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001260 case Instruction::ZExt:
1261 return X86SelectZExt(I);
1262 case Instruction::Br:
1263 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001264 case Instruction::Call:
1265 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001266 case Instruction::LShr:
1267 case Instruction::AShr:
1268 case Instruction::Shl:
1269 return X86SelectShift(I);
1270 case Instruction::Select:
1271 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001272 case Instruction::Trunc:
1273 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001274 case Instruction::FPExt:
1275 return X86SelectFPExt(I);
1276 case Instruction::FPTrunc:
1277 return X86SelectFPTrunc(I);
Dan Gohman99b21822008-08-28 23:21:34 +00001278 }
1279
1280 return false;
1281}
1282
Dan Gohman0586d912008-09-10 20:11:02 +00001283unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Evan Cheng59fbc802008-09-09 01:26:59 +00001284 MVT VT;
1285 if (!isTypeLegal(C->getType(), TLI, VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001286 return false;
1287
1288 // Get opcode and regclass of the output for the given load instruction.
1289 unsigned Opc = 0;
1290 const TargetRegisterClass *RC = NULL;
1291 switch (VT.getSimpleVT()) {
1292 default: return false;
1293 case MVT::i8:
1294 Opc = X86::MOV8rm;
1295 RC = X86::GR8RegisterClass;
1296 break;
1297 case MVT::i16:
1298 Opc = X86::MOV16rm;
1299 RC = X86::GR16RegisterClass;
1300 break;
1301 case MVT::i32:
1302 Opc = X86::MOV32rm;
1303 RC = X86::GR32RegisterClass;
1304 break;
1305 case MVT::i64:
1306 // Must be in x86-64 mode.
1307 Opc = X86::MOV64rm;
1308 RC = X86::GR64RegisterClass;
1309 break;
1310 case MVT::f32:
1311 if (Subtarget->hasSSE1()) {
1312 Opc = X86::MOVSSrm;
1313 RC = X86::FR32RegisterClass;
1314 } else {
1315 Opc = X86::LD_Fp32m;
1316 RC = X86::RFP32RegisterClass;
1317 }
1318 break;
1319 case MVT::f64:
1320 if (Subtarget->hasSSE2()) {
1321 Opc = X86::MOVSDrm;
1322 RC = X86::FR64RegisterClass;
1323 } else {
1324 Opc = X86::LD_Fp64m;
1325 RC = X86::RFP64RegisterClass;
1326 }
1327 break;
1328 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001329 // No f80 support yet.
1330 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001331 }
1332
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001333 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001334 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001335 X86AddressMode AM;
1336 if (X86SelectAddress(C, AM, false)) {
1337 if (TLI.getPointerTy() == MVT::i32)
1338 Opc = X86::LEA32r;
1339 else
1340 Opc = X86::LEA64r;
1341 unsigned ResultReg = createResultReg(RC);
1342 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001343 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001344 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001345 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001346 }
1347
Owen Anderson3b217c62008-09-06 01:11:01 +00001348 // MachineConstantPool wants an explicit alignment.
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001349 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001350 if (Align == 0) {
1351 // Alignment of vector types. FIXME!
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001352 Align = TD.getABITypeSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001353 Align = Log2_64(Align);
1354 }
Owen Anderson95267a12008-09-05 00:06:23 +00001355
Dan Gohman5396c992008-09-30 01:21:32 +00001356 // x86-32 PIC requires a PIC base register for constant pools.
1357 unsigned PICBase = 0;
1358 if (TM.getRelocationModel() == Reloc::PIC_ &&
1359 !Subtarget->is64Bit())
1360 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1361
1362 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001363 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001364 unsigned ResultReg = createResultReg(RC);
Dan Gohman5396c992008-09-30 01:21:32 +00001365 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset,
1366 PICBase);
1367
Owen Anderson95267a12008-09-05 00:06:23 +00001368 return ResultReg;
1369}
1370
Dan Gohman0586d912008-09-10 20:11:02 +00001371unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001372 // Fail on dynamic allocas. At this point, getRegForValue has already
1373 // checked its CSE maps, so if we're here trying to handle a dynamic
1374 // alloca, we're not going to succeed. X86SelectAddress has a
1375 // check for dynamic allocas, because it's called directly from
1376 // various places, but TargetMaterializeAlloca also needs a check
1377 // in order to avoid recursion between getRegForValue,
1378 // X86SelectAddrss, and TargetMaterializeAlloca.
1379 if (!StaticAllocaMap.count(C))
1380 return 0;
1381
Dan Gohman0586d912008-09-10 20:11:02 +00001382 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001383 if (!X86SelectAddress(C, AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +00001384 return 0;
1385 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1386 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1387 unsigned ResultReg = createResultReg(RC);
1388 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1389 return ResultReg;
1390}
1391
Evan Chengc3f44b02008-09-03 00:03:49 +00001392namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001393 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001394 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +00001395 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001396 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
1397 DenseMap<const AllocaInst *, int> &am) {
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001398 return new X86FastISel(mf, mmi, vm, bm, am);
Evan Chengc3f44b02008-09-03 00:03:49 +00001399 }
Dan Gohman99b21822008-08-28 23:21:34 +00001400}