blob: 1765e9e1de3d12affde2f1dc4193b31c27f14616 [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,
Dan Gohmandd5b58a2008-10-14 23:54:11 +000055 DenseMap<const AllocaInst *, int> &am
56#ifndef NDEBUG
57 , SmallSet<Instruction*, 8> &cil
58#endif
59 )
60 : FastISel(mf, mmi, vm, bm, am
61#ifndef NDEBUG
62 , cil
63#endif
64 ) {
Evan Cheng88e30412008-09-03 01:04:47 +000065 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000066 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
67 X86ScalarSSEf64 = Subtarget->hasSSE2();
68 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000069 }
Evan Chengc3f44b02008-09-03 00:03:49 +000070
Dan Gohman3df24e62008-09-03 23:12:08 +000071 virtual bool TargetSelectInstruction(Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000072
Dan Gohman1adf1b02008-08-19 21:45:35 +000073#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000074
75private:
Chris Lattner9a08a612008-10-15 04:26:38 +000076 bool X86FastEmitCompare(Value *LHS, Value *RHS, MVT VT);
77
Dan Gohman0586d912008-09-10 20:11:02 +000078 bool X86FastEmitLoad(MVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000079
Evan Chengf3d4efe2008-09-07 09:09:33 +000080 bool X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000081 const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000082
83 bool X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT, unsigned Src, MVT SrcVT,
84 unsigned &ResultReg);
Evan Cheng0de588f2008-09-05 21:00:03 +000085
Dan Gohman2ff7fd12008-09-19 22:16:54 +000086 bool X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall);
Dan Gohman0586d912008-09-10 20:11:02 +000087
Dan Gohman3df24e62008-09-03 23:12:08 +000088 bool X86SelectLoad(Instruction *I);
Owen Andersona3971df2008-09-04 07:08:58 +000089
90 bool X86SelectStore(Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000091
92 bool X86SelectCmp(Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000093
94 bool X86SelectZExt(Instruction *I);
95
96 bool X86SelectBranch(Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +000097
98 bool X86SelectShift(Instruction *I);
99
100 bool X86SelectSelect(Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000101
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000102 bool X86SelectTrunc(Instruction *I);
Dan Gohmand98d6202008-10-02 22:15:21 +0000103
104 unsigned X86ChooseCmpOpcode(MVT VT);
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000105
Dan Gohman78efce62008-09-10 21:02:08 +0000106 bool X86SelectFPExt(Instruction *I);
107 bool X86SelectFPTrunc(Instruction *I);
108
Evan Chengf3d4efe2008-09-07 09:09:33 +0000109 bool X86SelectCall(Instruction *I);
110
111 CCAssignFn *CCAssignFnForCall(unsigned CC, bool isTailCall = false);
112
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000113 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000114 return getTargetMachine()->getInstrInfo();
115 }
116 const X86TargetMachine *getTargetMachine() const {
117 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000118 }
119
Dan Gohman0586d912008-09-10 20:11:02 +0000120 unsigned TargetMaterializeConstant(Constant *C);
121
122 unsigned TargetMaterializeAlloca(AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000123
124 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
125 /// computed in an SSE register, not on the X87 floating point stack.
126 bool isScalarFPTypeInSSEReg(MVT VT) const {
127 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
128 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
129 }
130
Dan Gohman9b66d732008-09-30 00:48:39 +0000131 bool isTypeLegal(const Type *Ty, const TargetLowering &TLI, MVT &VT,
132 bool AllowI1 = false);
Evan Chengc3f44b02008-09-03 00:03:49 +0000133};
Dan Gohman99b21822008-08-28 23:21:34 +0000134
Dan Gohman9b66d732008-09-30 00:48:39 +0000135bool X86FastISel::isTypeLegal(const Type *Ty, const TargetLowering &TLI,
136 MVT &VT, bool AllowI1) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000137 VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
138 if (VT == MVT::Other || !VT.isSimple())
139 // Unhandled type. Halt "fast" selection and bail.
140 return false;
141 if (VT == MVT::iPTR)
142 // Use pointer type.
143 VT = TLI.getPointerTy();
Dan Gohman9b66d732008-09-30 00:48:39 +0000144 // For now, require SSE/SSE2 for performing floating-point operations,
145 // since x87 requires additional work.
146 if (VT == MVT::f64 && !X86ScalarSSEf64)
147 return false;
148 if (VT == MVT::f32 && !X86ScalarSSEf32)
149 return false;
150 // Similarly, no f80 support yet.
151 if (VT == MVT::f80)
152 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000153 // We only handle legal types. For example, on x86-32 the instruction
154 // selector contains all of the 64-bit instructions from x86-64,
155 // under the assumption that i64 won't be used if the target doesn't
156 // support it.
Evan Chengdebdea02008-09-08 17:15:42 +0000157 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000158}
159
160#include "X86GenCallingConv.inc"
161
162/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
163/// convention.
164CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) {
165 if (Subtarget->is64Bit()) {
166 if (Subtarget->isTargetWin64())
167 return CC_X86_Win64_C;
168 else if (CC == CallingConv::Fast && isTaillCall)
169 return CC_X86_64_TailCall;
170 else
171 return CC_X86_64_C;
172 }
173
174 if (CC == CallingConv::X86_FastCall)
175 return CC_X86_32_FastCall;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000176 else if (CC == CallingConv::Fast)
177 return CC_X86_32_FastCC;
178 else
179 return CC_X86_32_C;
180}
181
Evan Cheng0de588f2008-09-05 21:00:03 +0000182/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000183/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000184/// Return true and the result register by reference if it is possible.
Dan Gohman0586d912008-09-10 20:11:02 +0000185bool X86FastISel::X86FastEmitLoad(MVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000186 unsigned &ResultReg) {
187 // Get opcode and regclass of the output for the given load instruction.
188 unsigned Opc = 0;
189 const TargetRegisterClass *RC = NULL;
190 switch (VT.getSimpleVT()) {
191 default: return false;
192 case MVT::i8:
193 Opc = X86::MOV8rm;
194 RC = X86::GR8RegisterClass;
195 break;
196 case MVT::i16:
197 Opc = X86::MOV16rm;
198 RC = X86::GR16RegisterClass;
199 break;
200 case MVT::i32:
201 Opc = X86::MOV32rm;
202 RC = X86::GR32RegisterClass;
203 break;
204 case MVT::i64:
205 // Must be in x86-64 mode.
206 Opc = X86::MOV64rm;
207 RC = X86::GR64RegisterClass;
208 break;
209 case MVT::f32:
210 if (Subtarget->hasSSE1()) {
211 Opc = X86::MOVSSrm;
212 RC = X86::FR32RegisterClass;
213 } else {
214 Opc = X86::LD_Fp32m;
215 RC = X86::RFP32RegisterClass;
216 }
217 break;
218 case MVT::f64:
219 if (Subtarget->hasSSE2()) {
220 Opc = X86::MOVSDrm;
221 RC = X86::FR64RegisterClass;
222 } else {
223 Opc = X86::LD_Fp64m;
224 RC = X86::RFP64RegisterClass;
225 }
226 break;
227 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000228 // No f80 support yet.
229 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000230 }
231
232 ResultReg = createResultReg(RC);
Evan Cheng0de588f2008-09-05 21:00:03 +0000233 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
234 return true;
235}
236
Evan Chengf3d4efe2008-09-07 09:09:33 +0000237/// X86FastEmitStore - Emit a machine instruction to store a value Val of
238/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
239/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000240/// i.e. V. Return true if it is possible.
241bool
Evan Chengf3d4efe2008-09-07 09:09:33 +0000242X86FastISel::X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000243 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000244 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000245 unsigned Opc = 0;
246 const TargetRegisterClass *RC = NULL;
247 switch (VT.getSimpleVT()) {
248 default: return false;
249 case MVT::i8:
250 Opc = X86::MOV8mr;
251 RC = X86::GR8RegisterClass;
252 break;
253 case MVT::i16:
254 Opc = X86::MOV16mr;
255 RC = X86::GR16RegisterClass;
256 break;
257 case MVT::i32:
258 Opc = X86::MOV32mr;
259 RC = X86::GR32RegisterClass;
260 break;
261 case MVT::i64:
262 // Must be in x86-64 mode.
263 Opc = X86::MOV64mr;
264 RC = X86::GR64RegisterClass;
265 break;
266 case MVT::f32:
267 if (Subtarget->hasSSE1()) {
268 Opc = X86::MOVSSmr;
269 RC = X86::FR32RegisterClass;
270 } else {
271 Opc = X86::ST_Fp32m;
272 RC = X86::RFP32RegisterClass;
273 }
274 break;
275 case MVT::f64:
276 if (Subtarget->hasSSE2()) {
277 Opc = X86::MOVSDmr;
278 RC = X86::FR64RegisterClass;
279 } else {
280 Opc = X86::ST_Fp64m;
281 RC = X86::RFP64RegisterClass;
282 }
283 break;
284 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000285 // No f80 support yet.
286 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000287 }
288
Evan Chengf3d4efe2008-09-07 09:09:33 +0000289 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000290 return true;
291}
292
Evan Cheng24e3a902008-09-08 06:35:17 +0000293/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
294/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
295/// ISD::SIGN_EXTEND).
296bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT,
297 unsigned Src, MVT SrcVT,
298 unsigned &ResultReg) {
Owen Andersonac34a002008-09-11 19:44:55 +0000299 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
300
301 if (RR != 0) {
302 ResultReg = RR;
303 return true;
304 } else
305 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000306}
307
Dan Gohman0586d912008-09-10 20:11:02 +0000308/// X86SelectAddress - Attempt to fill in an address from the given value.
309///
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000310bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall) {
Dan Gohman35893082008-09-18 23:23:44 +0000311 User *U;
312 unsigned Opcode = Instruction::UserOp1;
313 if (Instruction *I = dyn_cast<Instruction>(V)) {
314 Opcode = I->getOpcode();
315 U = I;
316 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
317 Opcode = C->getOpcode();
318 U = C;
319 }
Dan Gohman0586d912008-09-10 20:11:02 +0000320
Dan Gohman35893082008-09-18 23:23:44 +0000321 switch (Opcode) {
322 default: break;
323 case Instruction::BitCast:
324 // Look past bitcasts.
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::IntToPtr:
328 // Look past no-op inttoptrs.
329 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000330 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000331
332 case Instruction::PtrToInt:
333 // Look past no-op ptrtoints.
334 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000335 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000336
337 case Instruction::Alloca: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000338 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000339 // Do static allocas.
340 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman0586d912008-09-10 20:11:02 +0000341 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
Dan Gohman97135e12008-09-26 19:15:30 +0000342 if (SI != StaticAllocaMap.end()) {
343 AM.BaseType = X86AddressMode::FrameIndexBase;
344 AM.Base.FrameIndex = SI->second;
345 return true;
346 }
347 break;
Dan Gohman35893082008-09-18 23:23:44 +0000348 }
349
350 case Instruction::Add: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000351 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000352 // Adds of constants are common and easy enough.
353 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000354 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
355 // They have to fit in the 32-bit signed displacement field though.
356 if (isInt32(Disp)) {
357 AM.Disp = (uint32_t)Disp;
358 return X86SelectAddress(U->getOperand(0), AM, isCall);
359 }
Dan Gohman0586d912008-09-10 20:11:02 +0000360 }
Dan Gohman35893082008-09-18 23:23:44 +0000361 break;
362 }
363
364 case Instruction::GetElementPtr: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000365 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000366 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000367 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000368 unsigned IndexReg = AM.IndexReg;
369 unsigned Scale = AM.Scale;
370 gep_type_iterator GTI = gep_type_begin(U);
371 // Look at all but the last index. Constants can be folded,
372 // and one dynamic index can be handled, if the scale is supported.
373 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
374 i != e; ++i, ++GTI) {
375 Value *Op = *i;
376 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
377 const StructLayout *SL = TD.getStructLayout(STy);
378 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
379 Disp += SL->getElementOffset(Idx);
380 } else {
381 uint64_t S = TD.getABITypeSize(GTI.getIndexedType());
382 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
383 // Constant-offset addressing.
Dan Gohman09aae462008-09-26 20:04:15 +0000384 Disp += CI->getSExtValue() * S;
Dan Gohman35893082008-09-18 23:23:44 +0000385 } else if (IndexReg == 0 &&
Dan Gohman97135e12008-09-26 19:15:30 +0000386 (!AM.GV ||
387 !getTargetMachine()->symbolicAddressesAreRIPRel()) &&
Dan Gohman35893082008-09-18 23:23:44 +0000388 (S == 1 || S == 2 || S == 4 || S == 8)) {
389 // Scaled-index addressing.
390 Scale = S;
391 IndexReg = getRegForValue(Op);
392 if (IndexReg == 0)
393 return false;
394 } else
395 // Unsupported.
396 goto unsupported_gep;
397 }
398 }
Dan Gohman09aae462008-09-26 20:04:15 +0000399 // Check for displacement overflow.
400 if (!isInt32(Disp))
401 break;
Dan Gohman35893082008-09-18 23:23:44 +0000402 // Ok, the GEP indices were covered by constant-offset and scaled-index
403 // addressing. Update the address state and move on to examining the base.
404 AM.IndexReg = IndexReg;
405 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000406 AM.Disp = (uint32_t)Disp;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000407 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000408 unsupported_gep:
409 // Ok, the GEP indices weren't all covered.
410 break;
411 }
412 }
413
414 // Handle constant address.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000415 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000416 // Can't handle alternate code models yet.
417 if (TM.getCodeModel() != CodeModel::Default &&
418 TM.getCodeModel() != CodeModel::Small)
419 return false;
420
Dan Gohman97135e12008-09-26 19:15:30 +0000421 // RIP-relative addresses can't have additional register operands.
422 if (getTargetMachine()->symbolicAddressesAreRIPRel() &&
423 (AM.Base.Reg != 0 || AM.IndexReg != 0))
424 return false;
425
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000426 // Set up the basic address.
427 AM.GV = GV;
428 if (!isCall &&
429 TM.getRelocationModel() == Reloc::PIC_ &&
430 !Subtarget->is64Bit())
Dan Gohman57c3dac2008-09-30 00:58:23 +0000431 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000432
433 // Emit an extra load if the ABI requires it.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000434 if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) {
435 // Check to see if we've already materialized this
436 // value in a register in this block.
Dan Gohman7e8ef602008-09-19 23:42:04 +0000437 if (unsigned Reg = LocalValueMap[V]) {
438 AM.Base.Reg = Reg;
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000439 AM.GV = 0;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000440 return true;
441 }
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000442 // Issue load from stub if necessary.
443 unsigned Opc = 0;
444 const TargetRegisterClass *RC = NULL;
445 if (TLI.getPointerTy() == MVT::i32) {
446 Opc = X86::MOV32rm;
447 RC = X86::GR32RegisterClass;
448 } else {
449 Opc = X86::MOV64rm;
450 RC = X86::GR64RegisterClass;
451 }
Dan Gohman789ce772008-09-25 23:34:02 +0000452
453 X86AddressMode StubAM;
454 StubAM.Base.Reg = AM.Base.Reg;
455 StubAM.GV = AM.GV;
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000456 unsigned ResultReg = createResultReg(RC);
Dan Gohman789ce772008-09-25 23:34:02 +0000457 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), StubAM);
458
459 // Now construct the final address. Note that the Disp, Scale,
460 // and Index values may already be set here.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000461 AM.Base.Reg = ResultReg;
462 AM.GV = 0;
Dan Gohman789ce772008-09-25 23:34:02 +0000463
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000464 // Prevent loading GV stub multiple times in same MBB.
465 LocalValueMap[V] = AM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000466 }
467 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000468 }
469
Dan Gohman97135e12008-09-26 19:15:30 +0000470 // If all else fails, try to materialize the value in a register.
Dan Gohman7962e852008-09-29 21:13:15 +0000471 if (!AM.GV || !getTargetMachine()->symbolicAddressesAreRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000472 if (AM.Base.Reg == 0) {
473 AM.Base.Reg = getRegForValue(V);
474 return AM.Base.Reg != 0;
475 }
476 if (AM.IndexReg == 0) {
477 assert(AM.Scale == 1 && "Scale with no index!");
478 AM.IndexReg = getRegForValue(V);
479 return AM.IndexReg != 0;
480 }
481 }
482
483 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000484}
485
Owen Andersona3971df2008-09-04 07:08:58 +0000486/// X86SelectStore - Select and emit code to implement store instructions.
487bool X86FastISel::X86SelectStore(Instruction* I) {
Evan Cheng24e3a902008-09-08 06:35:17 +0000488 MVT VT;
489 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
Owen Andersona3971df2008-09-04 07:08:58 +0000490 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000491 unsigned Val = getRegForValue(I->getOperand(0));
492 if (Val == 0)
Owen Andersona3971df2008-09-04 07:08:58 +0000493 // Unhandled operand. Halt "fast" selection and bail.
494 return false;
495
Dan Gohman0586d912008-09-10 20:11:02 +0000496 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000497 if (!X86SelectAddress(I->getOperand(1), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000498 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000499
Dan Gohman0586d912008-09-10 20:11:02 +0000500 return X86FastEmitStore(VT, Val, AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000501}
502
Evan Cheng8b19e562008-09-03 06:44:39 +0000503/// X86SelectLoad - Select and emit code to implement load instructions.
504///
Dan Gohman3df24e62008-09-03 23:12:08 +0000505bool X86FastISel::X86SelectLoad(Instruction *I) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000506 MVT VT;
507 if (!isTypeLegal(I->getType(), TLI, VT))
Evan Cheng8b19e562008-09-03 06:44:39 +0000508 return false;
509
Dan Gohman0586d912008-09-10 20:11:02 +0000510 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000511 if (!X86SelectAddress(I->getOperand(0), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000512 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000513
Evan Cheng0de588f2008-09-05 21:00:03 +0000514 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000515 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000516 UpdateValueMap(I, ResultReg);
517 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000518 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000519 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000520}
521
Dan Gohmand98d6202008-10-02 22:15:21 +0000522unsigned X86FastISel::X86ChooseCmpOpcode(MVT VT) {
523 switch (VT.getSimpleVT()) {
524 case MVT::i8: return X86::CMP8rr;
525 case MVT::i16: return X86::CMP16rr;
526 case MVT::i32: return X86::CMP32rr;
527 case MVT::i64: return X86::CMP64rr;
528 case MVT::f32: return X86::UCOMISSrr;
529 case MVT::f64: return X86::UCOMISDrr;
530 default: break;
531 }
532 return 0;
533}
534
Chris Lattner0e13c782008-10-15 04:13:29 +0000535/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
536/// of the comparison, return an opcode that works for the compare (e.g.
537/// CMP32ri) otherwise return 0.
538static unsigned X86ChooseCmpImmediateOpcode(ConstantInt *RHSC) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000539 if (RHSC->getType() == Type::Int8Ty)
540 return X86::CMP8ri;
541 if (RHSC->getType() == Type::Int16Ty)
542 return X86::CMP16ri;
543 if (RHSC->getType() == Type::Int32Ty)
544 return X86::CMP32ri;
545
546 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
547 // field.
548 if (RHSC->getType() == Type::Int64Ty &&
549 (int)RHSC->getSExtValue() == RHSC->getSExtValue())
550 return X86::CMP64ri32;
551
552 // Otherwise, we can't fold the immediate into this comparison.
553 return 0;
554}
555
Chris Lattner9a08a612008-10-15 04:26:38 +0000556bool X86FastISel::X86FastEmitCompare(Value *Op0, Value *Op1, MVT VT) {
557 unsigned Op0Reg = getRegForValue(Op0);
558 if (Op0Reg == 0) return false;
559
560 // We have two options: compare with register or immediate. If the RHS of
561 // the compare is an immediate that we can fold into this compare, use
562 // CMPri, otherwise use CMPrr.
563 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
564 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(Op1C)) {
565 BuildMI(MBB, TII.get(CompareImmOpc)).addReg(Op0Reg)
566 .addImm(Op1C->getSExtValue());
567 return true;
568 }
569 }
570
571 unsigned CompareOpc = X86ChooseCmpOpcode(VT);
572 if (CompareOpc == 0) return false;
573
574 unsigned Op1Reg = getRegForValue(Op1);
575 if (Op1Reg == 0) return false;
576 BuildMI(MBB, TII.get(CompareOpc)).addReg(Op0Reg).addReg(Op1Reg);
577
578 return true;
579}
580
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000581bool X86FastISel::X86SelectCmp(Instruction *I) {
582 CmpInst *CI = cast<CmpInst>(I);
583
Dan Gohman9b66d732008-09-30 00:48:39 +0000584 MVT VT;
585 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000586 return false;
587
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000588 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000589 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000590 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000591 switch (CI->getPredicate()) {
592 case CmpInst::FCMP_OEQ: {
Chris Lattner9a08a612008-10-15 04:26:38 +0000593 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
594 if (Op0Reg == 0) return false;
595 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
596 if (Op1Reg == 0) return false;
597 unsigned Opc = X86ChooseCmpOpcode(VT);
598
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000599 unsigned EReg = createResultReg(&X86::GR8RegClass);
600 unsigned NPReg = createResultReg(&X86::GR8RegClass);
601 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
602 BuildMI(MBB, TII.get(X86::SETEr), EReg);
603 BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
604 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000605 UpdateValueMap(I, ResultReg);
606 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000607 }
608 case CmpInst::FCMP_UNE: {
Chris Lattner9a08a612008-10-15 04:26:38 +0000609 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
610 if (Op0Reg == 0) return false;
611 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
612 if (Op1Reg == 0) return false;
613 unsigned Opc = X86ChooseCmpOpcode(VT);
614
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000615 unsigned NEReg = createResultReg(&X86::GR8RegClass);
616 unsigned PReg = createResultReg(&X86::GR8RegClass);
617 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
618 BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
619 BuildMI(MBB, TII.get(X86::SETPr), PReg);
620 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000621 UpdateValueMap(I, ResultReg);
622 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000623 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000624 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
625 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
626 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
627 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
628 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
629 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
630 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
631 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
632 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
633 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
634 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
635 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
636
637 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
638 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
639 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
640 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
641 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
642 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
643 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
644 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
645 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
646 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000647 default:
648 return false;
649 }
650
Chris Lattner9a08a612008-10-15 04:26:38 +0000651 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000652 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000653 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000654
Chris Lattner9a08a612008-10-15 04:26:38 +0000655 // Emit a compare of Op0/Op1.
656 X86FastEmitCompare(Op0, Op1, VT);
657
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000658 BuildMI(MBB, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000659 UpdateValueMap(I, ResultReg);
660 return true;
661}
Evan Cheng8b19e562008-09-03 06:44:39 +0000662
Dan Gohmand89ae992008-09-05 01:06:14 +0000663bool X86FastISel::X86SelectZExt(Instruction *I) {
664 // Special-case hack: The only i1 values we know how to produce currently
665 // set the upper bits of an i8 value to zero.
666 if (I->getType() == Type::Int8Ty &&
667 I->getOperand(0)->getType() == Type::Int1Ty) {
668 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000669 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000670 UpdateValueMap(I, ResultReg);
671 return true;
672 }
673
674 return false;
675}
676
Chris Lattner9a08a612008-10-15 04:26:38 +0000677
Dan Gohmand89ae992008-09-05 01:06:14 +0000678bool X86FastISel::X86SelectBranch(Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000679 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000680 // Handle a conditional branch.
681 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000682 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
683 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
684
Dan Gohmand98d6202008-10-02 22:15:21 +0000685 // Fold the common case of a conditional branch with a comparison.
686 if (CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
687 if (CI->hasOneUse()) {
688 MVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000689
Dan Gohmand98d6202008-10-02 22:15:21 +0000690 // Try to take advantage of fallthrough opportunities.
691 CmpInst::Predicate Predicate = CI->getPredicate();
692 if (MBB->isLayoutSuccessor(TrueMBB)) {
693 std::swap(TrueMBB, FalseMBB);
694 Predicate = CmpInst::getInversePredicate(Predicate);
695 }
696
Chris Lattner871d2462008-10-15 03:58:05 +0000697 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
698 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
699
Dan Gohmand98d6202008-10-02 22:15:21 +0000700 switch (Predicate) {
Chris Lattner871d2462008-10-15 03:58:05 +0000701 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA; break;
702 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE; break;
703 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA; break;
704 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE; break;
705 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE; break;
706 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP; break;
707 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP; break;
708 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE; break;
709 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB; break;
710 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE; break;
711 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break;
712 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break;
Chris Lattner9a08a612008-10-15 04:26:38 +0000713
Chris Lattner871d2462008-10-15 03:58:05 +0000714 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE; break;
715 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE; break;
716 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA; break;
717 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE; break;
718 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break;
719 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break;
720 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG; break;
721 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE; break;
722 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL; break;
723 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE; break;
Dan Gohmand98d6202008-10-02 22:15:21 +0000724 default:
725 return false;
726 }
Chris Lattner54aebde2008-10-15 03:47:17 +0000727
Chris Lattner709d8292008-10-15 04:02:26 +0000728 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
729 if (SwapArgs)
730 std::swap(Op0, Op1);
731
Chris Lattner9a08a612008-10-15 04:26:38 +0000732 // Emit a compare of the LHS and RHS, setting the flags.
733 if (!X86FastEmitCompare(Op0, Op1, VT))
734 return false;
Chris Lattner0e13c782008-10-15 04:13:29 +0000735
Chris Lattner54aebde2008-10-15 03:47:17 +0000736 BuildMI(MBB, TII.get(BranchOpc)).addMBB(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000737 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000738 MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000739 return true;
740 }
741 }
742
743 // Otherwise do a clumsy setcc and re-test it.
744 unsigned OpReg = getRegForValue(BI->getCondition());
745 if (OpReg == 0) return false;
746
747 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
Dan Gohmand98d6202008-10-02 22:15:21 +0000748 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000749 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000750 MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +0000751 return true;
752}
753
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000754bool X86FastISel::X86SelectShift(Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000755 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000756 const TargetRegisterClass *RC = NULL;
757 if (I->getType() == Type::Int8Ty) {
758 CReg = X86::CL;
759 RC = &X86::GR8RegClass;
760 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000761 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
762 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
763 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000764 default: return false;
765 }
766 } else if (I->getType() == Type::Int16Ty) {
767 CReg = X86::CX;
768 RC = &X86::GR16RegClass;
769 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000770 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
771 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
772 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000773 default: return false;
774 }
775 } else if (I->getType() == Type::Int32Ty) {
776 CReg = X86::ECX;
777 RC = &X86::GR32RegClass;
778 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000779 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
780 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
781 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000782 default: return false;
783 }
784 } else if (I->getType() == Type::Int64Ty) {
785 CReg = X86::RCX;
786 RC = &X86::GR64RegClass;
787 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000788 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
789 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
790 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000791 default: return false;
792 }
793 } else {
794 return false;
795 }
796
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000797 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
Dan Gohman9b66d732008-09-30 00:48:39 +0000798 if (VT == MVT::Other || !isTypeLegal(I->getType(), TLI, VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000799 return false;
800
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000801 unsigned Op0Reg = getRegForValue(I->getOperand(0));
802 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +0000803
804 // Fold immediate in shl(x,3).
805 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
806 unsigned ResultReg = createResultReg(RC);
807 BuildMI(MBB, TII.get(OpImm),
808 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue());
809 UpdateValueMap(I, ResultReg);
810 return true;
811 }
812
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000813 unsigned Op1Reg = getRegForValue(I->getOperand(1));
814 if (Op1Reg == 0) return false;
815 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
Dan Gohman145b8282008-10-07 21:50:36 +0000816
817 // The shift instruction uses X86::CL. If we defined a super-register
818 // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what
819 // we're doing here.
820 if (CReg != X86::CL)
821 BuildMI(MBB, TII.get(TargetInstrInfo::EXTRACT_SUBREG), X86::CL)
822 .addReg(CReg).addImm(X86::SUBREG_8BIT);
823
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000824 unsigned ResultReg = createResultReg(RC);
Dan Gohman145b8282008-10-07 21:50:36 +0000825 BuildMI(MBB, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000826 UpdateValueMap(I, ResultReg);
827 return true;
828}
829
830bool X86FastISel::X86SelectSelect(Instruction *I) {
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000831 const Type *Ty = I->getType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000832 if (isa<PointerType>(Ty))
Dan Gohman1fbc3cd2008-09-18 18:26:43 +0000833 Ty = TD.getIntPtrType();
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000834
835 unsigned Opc = 0;
836 const TargetRegisterClass *RC = NULL;
837 if (Ty == Type::Int16Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000838 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000839 RC = &X86::GR16RegClass;
840 } else if (Ty == Type::Int32Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000841 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000842 RC = &X86::GR32RegClass;
843 } else if (Ty == Type::Int64Ty) {
Dan Gohman31d26912008-09-05 21:13:04 +0000844 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000845 RC = &X86::GR64RegClass;
846 } else {
847 return false;
848 }
849
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000850 MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
Dan Gohman9b66d732008-09-30 00:48:39 +0000851 if (VT == MVT::Other || !isTypeLegal(Ty, 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;
856 unsigned Op1Reg = getRegForValue(I->getOperand(1));
857 if (Op1Reg == 0) return false;
858 unsigned Op2Reg = getRegForValue(I->getOperand(2));
859 if (Op2Reg == 0) return false;
860
861 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
862 unsigned ResultReg = createResultReg(RC);
863 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
864 UpdateValueMap(I, ResultReg);
865 return true;
866}
867
Dan Gohman78efce62008-09-10 21:02:08 +0000868bool X86FastISel::X86SelectFPExt(Instruction *I) {
869 if (Subtarget->hasSSE2()) {
870 if (I->getType() == Type::DoubleTy) {
871 Value *V = I->getOperand(0);
872 if (V->getType() == Type::FloatTy) {
873 unsigned OpReg = getRegForValue(V);
874 if (OpReg == 0) return false;
875 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
876 BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
877 UpdateValueMap(I, ResultReg);
878 return true;
879 }
880 }
881 }
882
883 return false;
884}
885
886bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
887 if (Subtarget->hasSSE2()) {
888 if (I->getType() == Type::FloatTy) {
889 Value *V = I->getOperand(0);
890 if (V->getType() == Type::DoubleTy) {
891 unsigned OpReg = getRegForValue(V);
892 if (OpReg == 0) return false;
893 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
894 BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
895 UpdateValueMap(I, ResultReg);
896 return true;
897 }
898 }
899 }
900
901 return false;
902}
903
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000904bool X86FastISel::X86SelectTrunc(Instruction *I) {
905 if (Subtarget->is64Bit())
906 // All other cases should be handled by the tblgen generated code.
907 return false;
908 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
909 MVT DstVT = TLI.getValueType(I->getType());
910 if (DstVT != MVT::i8)
911 // All other cases should be handled by the tblgen generated code.
912 return false;
913 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
914 // All other cases should be handled by the tblgen generated code.
915 return false;
916
917 unsigned InputReg = getRegForValue(I->getOperand(0));
918 if (!InputReg)
919 // Unhandled operand. Halt "fast" selection and bail.
920 return false;
921
922 // First issue a copy to GR16_ or GR32_.
923 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
924 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
925 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
926 unsigned CopyReg = createResultReg(CopyRC);
927 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
928
929 // Then issue an extract_subreg.
Dan Gohman145b8282008-10-07 21:50:36 +0000930 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg, X86::SUBREG_8BIT);
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000931 if (!ResultReg)
932 return false;
933
934 UpdateValueMap(I, ResultReg);
935 return true;
936}
937
Evan Chengf3d4efe2008-09-07 09:09:33 +0000938bool X86FastISel::X86SelectCall(Instruction *I) {
939 CallInst *CI = cast<CallInst>(I);
940 Value *Callee = I->getOperand(0);
941
942 // Can't handle inline asm yet.
943 if (isa<InlineAsm>(Callee))
944 return false;
945
946 // FIXME: Handle some intrinsics.
947 if (Function *F = CI->getCalledFunction()) {
948 if (F->isDeclaration() &&F->getIntrinsicID())
949 return false;
950 }
951
Evan Chengf3d4efe2008-09-07 09:09:33 +0000952 // Handle only C and fastcc calling conventions for now.
953 CallSite CS(CI);
954 unsigned CC = CS.getCallingConv();
955 if (CC != CallingConv::C &&
956 CC != CallingConv::Fast &&
957 CC != CallingConv::X86_FastCall)
958 return false;
959
960 // Let SDISel handle vararg functions.
961 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
962 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
963 if (FTy->isVarArg())
964 return false;
965
966 // Handle *simple* calls for now.
967 const Type *RetTy = CS.getType();
968 MVT RetVT;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000969 if (RetTy == Type::VoidTy)
970 RetVT = MVT::isVoid;
971 else if (!isTypeLegal(RetTy, TLI, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000972 return false;
973
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000974 // Materialize callee address in a register. FIXME: GV address can be
975 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000976 X86AddressMode CalleeAM;
977 if (!X86SelectAddress(Callee, CalleeAM, true))
978 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000979 unsigned CalleeOp = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000980 GlobalValue *GV = 0;
981 if (CalleeAM.Base.Reg != 0) {
982 assert(CalleeAM.GV == 0);
983 CalleeOp = CalleeAM.Base.Reg;
984 } else if (CalleeAM.GV != 0) {
985 assert(CalleeAM.GV != 0);
986 GV = CalleeAM.GV;
987 } else
988 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000989
Evan Chengdebdea02008-09-08 17:15:42 +0000990 // Allow calls which produce i1 results.
991 bool AndToI1 = false;
992 if (RetVT == MVT::i1) {
993 RetVT = MVT::i8;
994 AndToI1 = true;
995 }
996
Evan Chengf3d4efe2008-09-07 09:09:33 +0000997 // Deal with call operands first.
998 SmallVector<unsigned, 4> Args;
999 SmallVector<MVT, 4> ArgVTs;
1000 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
1001 Args.reserve(CS.arg_size());
1002 ArgVTs.reserve(CS.arg_size());
1003 ArgFlags.reserve(CS.arg_size());
1004 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1005 i != e; ++i) {
1006 unsigned Arg = getRegForValue(*i);
1007 if (Arg == 0)
1008 return false;
1009 ISD::ArgFlagsTy Flags;
1010 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001011 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001012 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001013 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001014 Flags.setZExt();
1015
1016 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001017 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1018 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1019 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1020 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001021 return false;
1022
1023 const Type *ArgTy = (*i)->getType();
1024 MVT ArgVT;
1025 if (!isTypeLegal(ArgTy, TLI, ArgVT))
1026 return false;
1027 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1028 Flags.setOrigAlign(OriginalAlignment);
1029
1030 Args.push_back(Arg);
1031 ArgVTs.push_back(ArgVT);
1032 ArgFlags.push_back(Flags);
1033 }
1034
1035 // Analyze operands of the call, assigning locations to each operand.
1036 SmallVector<CCValAssign, 16> ArgLocs;
1037 CCState CCInfo(CC, false, TM, ArgLocs);
1038 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1039
1040 // Get a count of how many bytes are to be pushed on the stack.
1041 unsigned NumBytes = CCInfo.getNextStackOffset();
1042
1043 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001044 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
1045 BuildMI(MBB, TII.get(AdjStackDown)).addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001046
1047 // Process argumenet: walk the register/memloc assignments, inserting
1048 // copies / loads.
1049 SmallVector<unsigned, 4> RegArgs;
1050 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1051 CCValAssign &VA = ArgLocs[i];
1052 unsigned Arg = Args[VA.getValNo()];
1053 MVT ArgVT = ArgVTs[VA.getValNo()];
1054
1055 // Promote the value if needed.
1056 switch (VA.getLocInfo()) {
1057 default: assert(0 && "Unknown loc info!");
1058 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001059 case CCValAssign::SExt: {
1060 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1061 Arg, ArgVT, Arg);
1062 assert(Emitted && "Failed to emit a sext!");
1063 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001064 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001065 }
1066 case CCValAssign::ZExt: {
1067 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1068 Arg, ArgVT, Arg);
1069 assert(Emitted && "Failed to emit a zext!");
1070 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001071 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001072 }
1073 case CCValAssign::AExt: {
1074 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1075 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001076 if (!Emitted)
1077 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1078 Arg, ArgVT, Arg);
1079 if (!Emitted)
1080 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1081 Arg, ArgVT, Arg);
1082
Evan Cheng24e3a902008-09-08 06:35:17 +00001083 assert(Emitted && "Failed to emit a aext!");
1084 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001085 break;
1086 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001087 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001088
1089 if (VA.isRegLoc()) {
1090 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
1091 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
1092 Arg, RC, RC);
1093 assert(Emitted && "Failed to emit a copy instruction!");
1094 RegArgs.push_back(VA.getLocReg());
1095 } else {
1096 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001097 X86AddressMode AM;
1098 AM.Base.Reg = StackPtr;
1099 AM.Disp = LocMemOffset;
1100 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001101 }
1102 }
1103
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001104 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1105 // GOT pointer.
1106 if (!Subtarget->is64Bit() &&
1107 TM.getRelocationModel() == Reloc::PIC_ &&
1108 Subtarget->isPICStyleGOT()) {
1109 TargetRegisterClass *RC = X86::GR32RegisterClass;
Dan Gohman57c3dac2008-09-30 00:58:23 +00001110 unsigned Base = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001111 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
1112 assert(Emitted && "Failed to emit a copy instruction!");
1113 }
1114
Evan Chengf3d4efe2008-09-07 09:09:33 +00001115 // Issue the call.
1116 unsigned CallOpc = CalleeOp
1117 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
1118 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
1119 MachineInstrBuilder MIB = CalleeOp
1120 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001121 : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001122
1123 // Add an implicit use GOT pointer in EBX.
1124 if (!Subtarget->is64Bit() &&
1125 TM.getRelocationModel() == Reloc::PIC_ &&
1126 Subtarget->isPICStyleGOT())
1127 MIB.addReg(X86::EBX);
1128
Evan Chengf3d4efe2008-09-07 09:09:33 +00001129 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001130 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1131 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001132
1133 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001134 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
1135 BuildMI(MBB, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001136
1137 // Now handle call return value (if any).
Evan Chengf3d4efe2008-09-07 09:09:33 +00001138 if (RetVT.getSimpleVT() != MVT::isVoid) {
1139 SmallVector<CCValAssign, 16> RVLocs;
1140 CCState CCInfo(CC, false, TM, RVLocs);
1141 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1142
1143 // Copy all of the result registers out of their specified physreg.
1144 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
1145 MVT CopyVT = RVLocs[0].getValVT();
1146 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1147 TargetRegisterClass *SrcRC = DstRC;
1148
1149 // If this is a call to a function that returns an fp value on the x87 fp
1150 // stack, but where we prefer to use the value in xmm registers, copy it
1151 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1152 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1153 RVLocs[0].getLocReg() == X86::ST1) &&
1154 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
1155 CopyVT = MVT::f80;
1156 SrcRC = X86::RSTRegisterClass;
1157 DstRC = X86::RFP80RegisterClass;
1158 }
1159
1160 unsigned ResultReg = createResultReg(DstRC);
1161 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1162 RVLocs[0].getLocReg(), DstRC, SrcRC);
1163 assert(Emitted && "Failed to emit a copy instruction!");
1164 if (CopyVT != RVLocs[0].getValVT()) {
1165 // Round the F80 the right size, which also moves to the appropriate xmm
1166 // register. This is accomplished by storing the F80 value in memory and
1167 // then loading it back. Ewww...
1168 MVT ResVT = RVLocs[0].getValVT();
1169 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1170 unsigned MemSize = ResVT.getSizeInBits()/8;
Dan Gohman0586d912008-09-10 20:11:02 +00001171 int FI = MFI.CreateStackObject(MemSize, MemSize);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001172 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
1173 DstRC = ResVT == MVT::f32
1174 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1175 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1176 ResultReg = createResultReg(DstRC);
1177 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
1178 }
1179
Evan Chengdebdea02008-09-08 17:15:42 +00001180 if (AndToI1) {
1181 // Mask out all but lowest bit for some call which produces an i1.
1182 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
1183 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
1184 ResultReg = AndResult;
1185 }
1186
Evan Chengf3d4efe2008-09-07 09:09:33 +00001187 UpdateValueMap(I, ResultReg);
1188 }
1189
1190 return true;
1191}
1192
1193
Dan Gohman99b21822008-08-28 23:21:34 +00001194bool
Dan Gohman3df24e62008-09-03 23:12:08 +00001195X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001196 switch (I->getOpcode()) {
1197 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001198 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001199 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001200 case Instruction::Store:
1201 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001202 case Instruction::ICmp:
1203 case Instruction::FCmp:
1204 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001205 case Instruction::ZExt:
1206 return X86SelectZExt(I);
1207 case Instruction::Br:
1208 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001209 case Instruction::Call:
1210 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001211 case Instruction::LShr:
1212 case Instruction::AShr:
1213 case Instruction::Shl:
1214 return X86SelectShift(I);
1215 case Instruction::Select:
1216 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001217 case Instruction::Trunc:
1218 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001219 case Instruction::FPExt:
1220 return X86SelectFPExt(I);
1221 case Instruction::FPTrunc:
1222 return X86SelectFPTrunc(I);
Dan Gohman99b21822008-08-28 23:21:34 +00001223 }
1224
1225 return false;
1226}
1227
Dan Gohman0586d912008-09-10 20:11:02 +00001228unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Evan Cheng59fbc802008-09-09 01:26:59 +00001229 MVT VT;
1230 if (!isTypeLegal(C->getType(), TLI, VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001231 return false;
1232
1233 // Get opcode and regclass of the output for the given load instruction.
1234 unsigned Opc = 0;
1235 const TargetRegisterClass *RC = NULL;
1236 switch (VT.getSimpleVT()) {
1237 default: return false;
1238 case MVT::i8:
1239 Opc = X86::MOV8rm;
1240 RC = X86::GR8RegisterClass;
1241 break;
1242 case MVT::i16:
1243 Opc = X86::MOV16rm;
1244 RC = X86::GR16RegisterClass;
1245 break;
1246 case MVT::i32:
1247 Opc = X86::MOV32rm;
1248 RC = X86::GR32RegisterClass;
1249 break;
1250 case MVT::i64:
1251 // Must be in x86-64 mode.
1252 Opc = X86::MOV64rm;
1253 RC = X86::GR64RegisterClass;
1254 break;
1255 case MVT::f32:
1256 if (Subtarget->hasSSE1()) {
1257 Opc = X86::MOVSSrm;
1258 RC = X86::FR32RegisterClass;
1259 } else {
1260 Opc = X86::LD_Fp32m;
1261 RC = X86::RFP32RegisterClass;
1262 }
1263 break;
1264 case MVT::f64:
1265 if (Subtarget->hasSSE2()) {
1266 Opc = X86::MOVSDrm;
1267 RC = X86::FR64RegisterClass;
1268 } else {
1269 Opc = X86::LD_Fp64m;
1270 RC = X86::RFP64RegisterClass;
1271 }
1272 break;
1273 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001274 // No f80 support yet.
1275 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001276 }
1277
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001278 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001279 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001280 X86AddressMode AM;
1281 if (X86SelectAddress(C, AM, false)) {
1282 if (TLI.getPointerTy() == MVT::i32)
1283 Opc = X86::LEA32r;
1284 else
1285 Opc = X86::LEA64r;
1286 unsigned ResultReg = createResultReg(RC);
1287 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001288 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001289 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001290 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001291 }
1292
Owen Anderson3b217c62008-09-06 01:11:01 +00001293 // MachineConstantPool wants an explicit alignment.
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001294 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001295 if (Align == 0) {
1296 // Alignment of vector types. FIXME!
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001297 Align = TD.getABITypeSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001298 Align = Log2_64(Align);
1299 }
Owen Anderson95267a12008-09-05 00:06:23 +00001300
Dan Gohman5396c992008-09-30 01:21:32 +00001301 // x86-32 PIC requires a PIC base register for constant pools.
1302 unsigned PICBase = 0;
1303 if (TM.getRelocationModel() == Reloc::PIC_ &&
1304 !Subtarget->is64Bit())
1305 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1306
1307 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001308 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001309 unsigned ResultReg = createResultReg(RC);
Dan Gohman5396c992008-09-30 01:21:32 +00001310 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset,
1311 PICBase);
1312
Owen Anderson95267a12008-09-05 00:06:23 +00001313 return ResultReg;
1314}
1315
Dan Gohman0586d912008-09-10 20:11:02 +00001316unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001317 // Fail on dynamic allocas. At this point, getRegForValue has already
1318 // checked its CSE maps, so if we're here trying to handle a dynamic
1319 // alloca, we're not going to succeed. X86SelectAddress has a
1320 // check for dynamic allocas, because it's called directly from
1321 // various places, but TargetMaterializeAlloca also needs a check
1322 // in order to avoid recursion between getRegForValue,
1323 // X86SelectAddrss, and TargetMaterializeAlloca.
1324 if (!StaticAllocaMap.count(C))
1325 return 0;
1326
Dan Gohman0586d912008-09-10 20:11:02 +00001327 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001328 if (!X86SelectAddress(C, AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +00001329 return 0;
1330 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1331 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1332 unsigned ResultReg = createResultReg(RC);
1333 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1334 return ResultReg;
1335}
1336
Evan Chengc3f44b02008-09-03 00:03:49 +00001337namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001338 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001339 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +00001340 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001341 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001342 DenseMap<const AllocaInst *, int> &am
1343#ifndef NDEBUG
1344 , SmallSet<Instruction*, 8> &cil
1345#endif
1346 ) {
1347 return new X86FastISel(mf, mmi, vm, bm, am
1348#ifndef NDEBUG
1349 , cil
1350#endif
1351 );
Evan Chengc3f44b02008-09-03 00:03:49 +00001352 }
Dan Gohman99b21822008-08-28 23:21:34 +00001353}