blob: 1b75cbcae43db119676e88737ad97a30cc33f5e1 [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
Chris Lattner438949a2008-10-15 05:30:52 +000080 bool X86FastEmitStore(MVT VT, Value *Val,
81 const X86AddressMode &AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +000082 bool X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +000083 const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000084
85 bool X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT, unsigned Src, MVT SrcVT,
86 unsigned &ResultReg);
Evan Cheng0de588f2008-09-05 21:00:03 +000087
Dan Gohman2ff7fd12008-09-19 22:16:54 +000088 bool X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall);
Dan Gohman0586d912008-09-10 20:11:02 +000089
Dan Gohman3df24e62008-09-03 23:12:08 +000090 bool X86SelectLoad(Instruction *I);
Owen Andersona3971df2008-09-04 07:08:58 +000091
92 bool X86SelectStore(Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000093
94 bool X86SelectCmp(Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000095
96 bool X86SelectZExt(Instruction *I);
97
98 bool X86SelectBranch(Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +000099
100 bool X86SelectShift(Instruction *I);
101
102 bool X86SelectSelect(Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000103
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000104 bool X86SelectTrunc(Instruction *I);
Dan Gohmand98d6202008-10-02 22:15:21 +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
Chris Lattner160f6cc2008-10-15 05:07:36 +0000131 bool isTypeLegal(const Type *Ty, MVT &VT, bool AllowI1 = false);
Evan Chengc3f44b02008-09-03 00:03:49 +0000132};
Dan Gohman99b21822008-08-28 23:21:34 +0000133
Chris Lattner160f6cc2008-10-15 05:07:36 +0000134bool X86FastISel::isTypeLegal(const Type *Ty, MVT &VT, bool AllowI1) {
135 VT = TLI.getValueType(Ty, /*HandleUnknown=*/true);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000136 if (VT == MVT::Other || !VT.isSimple())
137 // Unhandled type. Halt "fast" selection and bail.
138 return false;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000139
Dan Gohman9b66d732008-09-30 00:48:39 +0000140 // For now, require SSE/SSE2 for performing floating-point operations,
141 // since x87 requires additional work.
142 if (VT == MVT::f64 && !X86ScalarSSEf64)
143 return false;
144 if (VT == MVT::f32 && !X86ScalarSSEf32)
145 return false;
146 // Similarly, no f80 support yet.
147 if (VT == MVT::f80)
148 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000149 // We only handle legal types. For example, on x86-32 the instruction
150 // selector contains all of the 64-bit instructions from x86-64,
151 // under the assumption that i64 won't be used if the target doesn't
152 // support it.
Evan Chengdebdea02008-09-08 17:15:42 +0000153 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000154}
155
156#include "X86GenCallingConv.inc"
157
158/// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
159/// convention.
160CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) {
161 if (Subtarget->is64Bit()) {
162 if (Subtarget->isTargetWin64())
163 return CC_X86_Win64_C;
164 else if (CC == CallingConv::Fast && isTaillCall)
165 return CC_X86_64_TailCall;
166 else
167 return CC_X86_64_C;
168 }
169
170 if (CC == CallingConv::X86_FastCall)
171 return CC_X86_32_FastCall;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000172 else if (CC == CallingConv::Fast)
173 return CC_X86_32_FastCC;
174 else
175 return CC_X86_32_C;
176}
177
Evan Cheng0de588f2008-09-05 21:00:03 +0000178/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000179/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000180/// Return true and the result register by reference if it is possible.
Dan Gohman0586d912008-09-10 20:11:02 +0000181bool X86FastISel::X86FastEmitLoad(MVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000182 unsigned &ResultReg) {
183 // Get opcode and regclass of the output for the given load instruction.
184 unsigned Opc = 0;
185 const TargetRegisterClass *RC = NULL;
186 switch (VT.getSimpleVT()) {
187 default: return false;
188 case MVT::i8:
189 Opc = X86::MOV8rm;
190 RC = X86::GR8RegisterClass;
191 break;
192 case MVT::i16:
193 Opc = X86::MOV16rm;
194 RC = X86::GR16RegisterClass;
195 break;
196 case MVT::i32:
197 Opc = X86::MOV32rm;
198 RC = X86::GR32RegisterClass;
199 break;
200 case MVT::i64:
201 // Must be in x86-64 mode.
202 Opc = X86::MOV64rm;
203 RC = X86::GR64RegisterClass;
204 break;
205 case MVT::f32:
206 if (Subtarget->hasSSE1()) {
207 Opc = X86::MOVSSrm;
208 RC = X86::FR32RegisterClass;
209 } else {
210 Opc = X86::LD_Fp32m;
211 RC = X86::RFP32RegisterClass;
212 }
213 break;
214 case MVT::f64:
215 if (Subtarget->hasSSE2()) {
216 Opc = X86::MOVSDrm;
217 RC = X86::FR64RegisterClass;
218 } else {
219 Opc = X86::LD_Fp64m;
220 RC = X86::RFP64RegisterClass;
221 }
222 break;
223 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000224 // No f80 support yet.
225 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000226 }
227
228 ResultReg = createResultReg(RC);
Evan Cheng0de588f2008-09-05 21:00:03 +0000229 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
230 return true;
231}
232
Evan Chengf3d4efe2008-09-07 09:09:33 +0000233/// X86FastEmitStore - Emit a machine instruction to store a value Val of
234/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
235/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000236/// i.e. V. Return true if it is possible.
237bool
Evan Chengf3d4efe2008-09-07 09:09:33 +0000238X86FastISel::X86FastEmitStore(MVT VT, unsigned Val,
Dan Gohman0586d912008-09-10 20:11:02 +0000239 const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000240 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000241 unsigned Opc = 0;
Evan Cheng0de588f2008-09-05 21:00:03 +0000242 switch (VT.getSimpleVT()) {
Chris Lattner241ab472008-10-15 05:38:32 +0000243 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000244 default: return false;
Chris Lattner241ab472008-10-15 05:38:32 +0000245 case MVT::i8: Opc = X86::MOV8mr; break;
246 case MVT::i16: Opc = X86::MOV16mr; break;
247 case MVT::i32: Opc = X86::MOV32mr; break;
248 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
Evan Cheng0de588f2008-09-05 21:00:03 +0000249 case MVT::f32:
Chris Lattner438949a2008-10-15 05:30:52 +0000250 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000251 break;
252 case MVT::f64:
Chris Lattner438949a2008-10-15 05:30:52 +0000253 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000254 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000255 }
Chris Lattner438949a2008-10-15 05:30:52 +0000256
Evan Chengf3d4efe2008-09-07 09:09:33 +0000257 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000258 return true;
259}
260
Chris Lattner438949a2008-10-15 05:30:52 +0000261bool X86FastISel::X86FastEmitStore(MVT VT, Value *Val,
262 const X86AddressMode &AM) {
263 // Handle 'null' like i32/i64 0.
264 if (isa<ConstantPointerNull>(Val))
265 Val = Constant::getNullValue(TD.getIntPtrType());
266
267 // If this is a store of a simple constant, fold the constant into the store.
268 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
269 unsigned Opc = 0;
270 switch (VT.getSimpleVT()) {
271 default: break;
272 case MVT::i8: Opc = X86::MOV8mi; break;
273 case MVT::i16: Opc = X86::MOV16mi; break;
274 case MVT::i32: Opc = X86::MOV32mi; break;
275 case MVT::i64:
276 // Must be a 32-bit sign extended value.
277 if ((int)CI->getSExtValue() == CI->getSExtValue())
278 Opc = X86::MOV64mi32;
279 break;
280 }
281
282 if (Opc) {
283 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addImm(CI->getSExtValue());
284 return true;
285 }
286 }
287
288 unsigned ValReg = getRegForValue(Val);
289 if (ValReg == 0)
Chris Lattner438949a2008-10-15 05:30:52 +0000290 return false;
291
292 return X86FastEmitStore(VT, ValReg, AM);
293}
294
295
296
Evan Cheng24e3a902008-09-08 06:35:17 +0000297/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
298/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
299/// ISD::SIGN_EXTEND).
300bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT,
301 unsigned Src, MVT SrcVT,
302 unsigned &ResultReg) {
Owen Andersonac34a002008-09-11 19:44:55 +0000303 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
304
305 if (RR != 0) {
306 ResultReg = RR;
307 return true;
308 } else
309 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000310}
311
Dan Gohman0586d912008-09-10 20:11:02 +0000312/// X86SelectAddress - Attempt to fill in an address from the given value.
313///
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000314bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall) {
Dan Gohman35893082008-09-18 23:23:44 +0000315 User *U;
316 unsigned Opcode = Instruction::UserOp1;
317 if (Instruction *I = dyn_cast<Instruction>(V)) {
318 Opcode = I->getOpcode();
319 U = I;
320 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
321 Opcode = C->getOpcode();
322 U = C;
323 }
Dan Gohman0586d912008-09-10 20:11:02 +0000324
Dan Gohman35893082008-09-18 23:23:44 +0000325 switch (Opcode) {
326 default: break;
327 case Instruction::BitCast:
328 // Look past bitcasts.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000329 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000330
331 case Instruction::IntToPtr:
332 // Look past no-op inttoptrs.
333 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000334 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000335 break;
Dan Gohman35893082008-09-18 23:23:44 +0000336
337 case Instruction::PtrToInt:
338 // Look past no-op ptrtoints.
339 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000340 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000341 break;
Dan Gohman35893082008-09-18 23:23:44 +0000342
343 case Instruction::Alloca: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000344 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000345 // Do static allocas.
346 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman0586d912008-09-10 20:11:02 +0000347 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
Dan Gohman97135e12008-09-26 19:15:30 +0000348 if (SI != StaticAllocaMap.end()) {
349 AM.BaseType = X86AddressMode::FrameIndexBase;
350 AM.Base.FrameIndex = SI->second;
351 return true;
352 }
353 break;
Dan Gohman35893082008-09-18 23:23:44 +0000354 }
355
356 case Instruction::Add: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000357 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000358 // Adds of constants are common and easy enough.
359 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000360 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
361 // They have to fit in the 32-bit signed displacement field though.
362 if (isInt32(Disp)) {
363 AM.Disp = (uint32_t)Disp;
364 return X86SelectAddress(U->getOperand(0), AM, isCall);
365 }
Dan Gohman0586d912008-09-10 20:11:02 +0000366 }
Dan Gohman35893082008-09-18 23:23:44 +0000367 break;
368 }
369
370 case Instruction::GetElementPtr: {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000371 if (isCall) break;
Dan Gohman35893082008-09-18 23:23:44 +0000372 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000373 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000374 unsigned IndexReg = AM.IndexReg;
375 unsigned Scale = AM.Scale;
376 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000377 // Iterate through the indices, folding what we can. Constants can be
378 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman35893082008-09-18 23:23:44 +0000379 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
380 i != e; ++i, ++GTI) {
381 Value *Op = *i;
382 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
383 const StructLayout *SL = TD.getStructLayout(STy);
384 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
385 Disp += SL->getElementOffset(Idx);
386 } else {
387 uint64_t S = TD.getABITypeSize(GTI.getIndexedType());
388 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
389 // Constant-offset addressing.
Dan Gohman09aae462008-09-26 20:04:15 +0000390 Disp += CI->getSExtValue() * S;
Dan Gohman35893082008-09-18 23:23:44 +0000391 } else if (IndexReg == 0 &&
Dan Gohman97135e12008-09-26 19:15:30 +0000392 (!AM.GV ||
393 !getTargetMachine()->symbolicAddressesAreRIPRel()) &&
Dan Gohman35893082008-09-18 23:23:44 +0000394 (S == 1 || S == 2 || S == 4 || S == 8)) {
395 // Scaled-index addressing.
396 Scale = S;
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000397 IndexReg = getRegForGEPIndex(Op);
Dan Gohman35893082008-09-18 23:23:44 +0000398 if (IndexReg == 0)
399 return false;
400 } else
401 // Unsupported.
402 goto unsupported_gep;
403 }
404 }
Dan Gohman09aae462008-09-26 20:04:15 +0000405 // Check for displacement overflow.
406 if (!isInt32(Disp))
407 break;
Dan Gohman35893082008-09-18 23:23:44 +0000408 // Ok, the GEP indices were covered by constant-offset and scaled-index
409 // addressing. Update the address state and move on to examining the base.
410 AM.IndexReg = IndexReg;
411 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000412 AM.Disp = (uint32_t)Disp;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000413 return X86SelectAddress(U->getOperand(0), AM, isCall);
Dan Gohman35893082008-09-18 23:23:44 +0000414 unsupported_gep:
415 // Ok, the GEP indices weren't all covered.
416 break;
417 }
418 }
419
420 // Handle constant address.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000421 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000422 // Can't handle alternate code models yet.
423 if (TM.getCodeModel() != CodeModel::Default &&
424 TM.getCodeModel() != CodeModel::Small)
425 return false;
426
Dan Gohman97135e12008-09-26 19:15:30 +0000427 // RIP-relative addresses can't have additional register operands.
428 if (getTargetMachine()->symbolicAddressesAreRIPRel() &&
429 (AM.Base.Reg != 0 || AM.IndexReg != 0))
430 return false;
431
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000432 // Set up the basic address.
433 AM.GV = GV;
434 if (!isCall &&
435 TM.getRelocationModel() == Reloc::PIC_ &&
436 !Subtarget->is64Bit())
Dan Gohman57c3dac2008-09-30 00:58:23 +0000437 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000438
439 // Emit an extra load if the ABI requires it.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000440 if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) {
441 // Check to see if we've already materialized this
442 // value in a register in this block.
Dan Gohman7e8ef602008-09-19 23:42:04 +0000443 if (unsigned Reg = LocalValueMap[V]) {
444 AM.Base.Reg = Reg;
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000445 AM.GV = 0;
Dan Gohman7e8ef602008-09-19 23:42:04 +0000446 return true;
447 }
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000448 // Issue load from stub if necessary.
449 unsigned Opc = 0;
450 const TargetRegisterClass *RC = NULL;
451 if (TLI.getPointerTy() == MVT::i32) {
452 Opc = X86::MOV32rm;
453 RC = X86::GR32RegisterClass;
454 } else {
455 Opc = X86::MOV64rm;
456 RC = X86::GR64RegisterClass;
457 }
Dan Gohman789ce772008-09-25 23:34:02 +0000458
459 X86AddressMode StubAM;
460 StubAM.Base.Reg = AM.Base.Reg;
461 StubAM.GV = AM.GV;
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000462 unsigned ResultReg = createResultReg(RC);
Dan Gohman789ce772008-09-25 23:34:02 +0000463 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), StubAM);
464
465 // Now construct the final address. Note that the Disp, Scale,
466 // and Index values may already be set here.
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000467 AM.Base.Reg = ResultReg;
468 AM.GV = 0;
Dan Gohman789ce772008-09-25 23:34:02 +0000469
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000470 // Prevent loading GV stub multiple times in same MBB.
471 LocalValueMap[V] = AM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000472 }
473 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000474 }
475
Dan Gohman97135e12008-09-26 19:15:30 +0000476 // If all else fails, try to materialize the value in a register.
Dan Gohman7962e852008-09-29 21:13:15 +0000477 if (!AM.GV || !getTargetMachine()->symbolicAddressesAreRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000478 if (AM.Base.Reg == 0) {
479 AM.Base.Reg = getRegForValue(V);
480 return AM.Base.Reg != 0;
481 }
482 if (AM.IndexReg == 0) {
483 assert(AM.Scale == 1 && "Scale with no index!");
484 AM.IndexReg = getRegForValue(V);
485 return AM.IndexReg != 0;
486 }
487 }
488
489 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000490}
491
Owen Andersona3971df2008-09-04 07:08:58 +0000492/// X86SelectStore - Select and emit code to implement store instructions.
493bool X86FastISel::X86SelectStore(Instruction* I) {
Evan Cheng24e3a902008-09-08 06:35:17 +0000494 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000495 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Owen Andersona3971df2008-09-04 07:08:58 +0000496 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000497
Dan Gohman0586d912008-09-10 20:11:02 +0000498 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000499 if (!X86SelectAddress(I->getOperand(1), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000500 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000501
Chris Lattner438949a2008-10-15 05:30:52 +0000502 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000503}
504
Evan Cheng8b19e562008-09-03 06:44:39 +0000505/// X86SelectLoad - Select and emit code to implement load instructions.
506///
Dan Gohman3df24e62008-09-03 23:12:08 +0000507bool X86FastISel::X86SelectLoad(Instruction *I) {
Evan Chengf3d4efe2008-09-07 09:09:33 +0000508 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000509 if (!isTypeLegal(I->getType(), VT))
Evan Cheng8b19e562008-09-03 06:44:39 +0000510 return false;
511
Dan Gohman0586d912008-09-10 20:11:02 +0000512 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000513 if (!X86SelectAddress(I->getOperand(0), AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +0000514 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000515
Evan Cheng0de588f2008-09-05 21:00:03 +0000516 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000517 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000518 UpdateValueMap(I, ResultReg);
519 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000520 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000521 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000522}
523
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000524static unsigned X86ChooseCmpOpcode(MVT VT) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000525 switch (VT.getSimpleVT()) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000526 default: return 0;
527 case MVT::i8: return X86::CMP8rr;
Dan Gohmand98d6202008-10-02 22:15:21 +0000528 case MVT::i16: return X86::CMP16rr;
529 case MVT::i32: return X86::CMP32rr;
530 case MVT::i64: return X86::CMP64rr;
531 case MVT::f32: return X86::UCOMISSrr;
532 case MVT::f64: return X86::UCOMISDrr;
Dan Gohmand98d6202008-10-02 22:15:21 +0000533 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000534}
535
Chris Lattner0e13c782008-10-15 04:13:29 +0000536/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
537/// of the comparison, return an opcode that works for the compare (e.g.
538/// CMP32ri) otherwise return 0.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000539static unsigned X86ChooseCmpImmediateOpcode(MVT VT, ConstantInt *RHSC) {
540 switch (VT.getSimpleVT()) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000541 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000542 default: return 0;
543 case MVT::i8: return X86::CMP8ri;
544 case MVT::i16: return X86::CMP16ri;
545 case MVT::i32: return X86::CMP32ri;
546 case MVT::i64:
547 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
548 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000549 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000550 return X86::CMP64ri32;
551 return 0;
552 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000553}
554
Chris Lattner9a08a612008-10-15 04:26:38 +0000555bool X86FastISel::X86FastEmitCompare(Value *Op0, Value *Op1, MVT VT) {
556 unsigned Op0Reg = getRegForValue(Op0);
557 if (Op0Reg == 0) return false;
558
Chris Lattnerd53886b2008-10-15 05:18:04 +0000559 // Handle 'null' like i32/i64 0.
560 if (isa<ConstantPointerNull>(Op1))
561 Op1 = Constant::getNullValue(TD.getIntPtrType());
562
Chris Lattner9a08a612008-10-15 04:26:38 +0000563 // We have two options: compare with register or immediate. If the RHS of
564 // the compare is an immediate that we can fold into this compare, use
565 // CMPri, otherwise use CMPrr.
566 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000567 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000568 BuildMI(MBB, TII.get(CompareImmOpc)).addReg(Op0Reg)
569 .addImm(Op1C->getSExtValue());
570 return true;
571 }
572 }
573
574 unsigned CompareOpc = X86ChooseCmpOpcode(VT);
575 if (CompareOpc == 0) return false;
576
577 unsigned Op1Reg = getRegForValue(Op1);
578 if (Op1Reg == 0) return false;
579 BuildMI(MBB, TII.get(CompareOpc)).addReg(Op0Reg).addReg(Op1Reg);
580
581 return true;
582}
583
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000584bool X86FastISel::X86SelectCmp(Instruction *I) {
585 CmpInst *CI = cast<CmpInst>(I);
586
Dan Gohman9b66d732008-09-30 00:48:39 +0000587 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000588 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000589 return false;
590
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000591 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000592 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000593 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000594 switch (CI->getPredicate()) {
595 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000596 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
597 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000598
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000599 unsigned EReg = createResultReg(&X86::GR8RegClass);
600 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000601 BuildMI(MBB, TII.get(X86::SETEr), EReg);
602 BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
603 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000604 UpdateValueMap(I, ResultReg);
605 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000606 }
607 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000608 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
609 return false;
610
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000611 unsigned NEReg = createResultReg(&X86::GR8RegClass);
612 unsigned PReg = createResultReg(&X86::GR8RegClass);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000613 BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
614 BuildMI(MBB, TII.get(X86::SETPr), PReg);
615 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000616 UpdateValueMap(I, ResultReg);
617 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000618 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000619 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
620 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
621 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
622 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
623 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
624 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
625 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
626 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
627 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
628 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
629 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
630 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
631
632 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
633 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
634 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
635 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
636 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
637 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
638 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
639 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
640 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
641 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000642 default:
643 return false;
644 }
645
Chris Lattner9a08a612008-10-15 04:26:38 +0000646 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000647 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000648 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000649
Chris Lattner9a08a612008-10-15 04:26:38 +0000650 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000651 if (!X86FastEmitCompare(Op0, Op1, VT))
652 return false;
Chris Lattner9a08a612008-10-15 04:26:38 +0000653
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000654 BuildMI(MBB, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000655 UpdateValueMap(I, ResultReg);
656 return true;
657}
Evan Cheng8b19e562008-09-03 06:44:39 +0000658
Dan Gohmand89ae992008-09-05 01:06:14 +0000659bool X86FastISel::X86SelectZExt(Instruction *I) {
660 // Special-case hack: The only i1 values we know how to produce currently
661 // set the upper bits of an i8 value to zero.
662 if (I->getType() == Type::Int8Ty &&
663 I->getOperand(0)->getType() == Type::Int1Ty) {
664 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000665 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000666 UpdateValueMap(I, ResultReg);
667 return true;
668 }
669
670 return false;
671}
672
Chris Lattner9a08a612008-10-15 04:26:38 +0000673
Dan Gohmand89ae992008-09-05 01:06:14 +0000674bool X86FastISel::X86SelectBranch(Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000675 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000676 // Handle a conditional branch.
677 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000678 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
679 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
680
Dan Gohmand98d6202008-10-02 22:15:21 +0000681 // Fold the common case of a conditional branch with a comparison.
682 if (CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
683 if (CI->hasOneUse()) {
684 MVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000685
Dan Gohmand98d6202008-10-02 22:15:21 +0000686 // Try to take advantage of fallthrough opportunities.
687 CmpInst::Predicate Predicate = CI->getPredicate();
688 if (MBB->isLayoutSuccessor(TrueMBB)) {
689 std::swap(TrueMBB, FalseMBB);
690 Predicate = CmpInst::getInversePredicate(Predicate);
691 }
692
Chris Lattner871d2462008-10-15 03:58:05 +0000693 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
694 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
695
Dan Gohmand98d6202008-10-02 22:15:21 +0000696 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000697 case CmpInst::FCMP_OEQ:
698 std::swap(TrueMBB, FalseMBB);
699 Predicate = CmpInst::FCMP_UNE;
700 // FALL THROUGH
701 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE; break;
Chris Lattner871d2462008-10-15 03:58:05 +0000702 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA; break;
703 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE; break;
704 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA; break;
705 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE; break;
706 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE; break;
707 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP; break;
708 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP; break;
709 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE; break;
710 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB; break;
711 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE; break;
712 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break;
713 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break;
Chris Lattner9a08a612008-10-15 04:26:38 +0000714
Chris Lattner871d2462008-10-15 03:58:05 +0000715 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE; break;
716 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE; break;
717 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA; break;
718 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE; break;
719 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break;
720 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break;
721 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG; break;
722 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE; break;
723 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL; break;
724 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE; break;
Dan Gohmand98d6202008-10-02 22:15:21 +0000725 default:
726 return false;
727 }
Chris Lattner54aebde2008-10-15 03:47:17 +0000728
Chris Lattner709d8292008-10-15 04:02:26 +0000729 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
730 if (SwapArgs)
731 std::swap(Op0, Op1);
732
Chris Lattner9a08a612008-10-15 04:26:38 +0000733 // Emit a compare of the LHS and RHS, setting the flags.
734 if (!X86FastEmitCompare(Op0, Op1, VT))
735 return false;
Chris Lattner0e13c782008-10-15 04:13:29 +0000736
Chris Lattner54aebde2008-10-15 03:47:17 +0000737 BuildMI(MBB, TII.get(BranchOpc)).addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +0000738
739 if (Predicate == CmpInst::FCMP_UNE) {
740 // X86 requires a second branch to handle UNE (and OEQ,
741 // which is mapped to UNE above).
742 BuildMI(MBB, TII.get(X86::JP)).addMBB(TrueMBB);
743 }
744
Dan Gohmand98d6202008-10-02 22:15:21 +0000745 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000746 MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000747 return true;
748 }
749 }
750
751 // Otherwise do a clumsy setcc and re-test it.
752 unsigned OpReg = getRegForValue(BI->getCondition());
753 if (OpReg == 0) return false;
754
755 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
Dan Gohmand98d6202008-10-02 22:15:21 +0000756 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +0000757 FastEmitBranch(FalseMBB);
Dan Gohman8c3f8b62008-10-07 22:10:33 +0000758 MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +0000759 return true;
760}
761
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000762bool X86FastISel::X86SelectShift(Instruction *I) {
Chris Lattner743922e2008-09-21 21:44:29 +0000763 unsigned CReg = 0, OpReg = 0, OpImm = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000764 const TargetRegisterClass *RC = NULL;
765 if (I->getType() == Type::Int8Ty) {
766 CReg = X86::CL;
767 RC = &X86::GR8RegClass;
768 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000769 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
770 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
771 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000772 default: return false;
773 }
774 } else if (I->getType() == Type::Int16Ty) {
775 CReg = X86::CX;
776 RC = &X86::GR16RegClass;
777 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000778 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
779 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
780 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000781 default: return false;
782 }
783 } else if (I->getType() == Type::Int32Ty) {
784 CReg = X86::ECX;
785 RC = &X86::GR32RegClass;
786 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000787 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
788 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
789 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000790 default: return false;
791 }
792 } else if (I->getType() == Type::Int64Ty) {
793 CReg = X86::RCX;
794 RC = &X86::GR64RegClass;
795 switch (I->getOpcode()) {
Chris Lattner743922e2008-09-21 21:44:29 +0000796 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
797 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
798 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000799 default: return false;
800 }
801 } else {
802 return false;
803 }
804
Chris Lattner160f6cc2008-10-15 05:07:36 +0000805 MVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
806 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +0000807 return false;
808
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000809 unsigned Op0Reg = getRegForValue(I->getOperand(0));
810 if (Op0Reg == 0) return false;
Chris Lattner743922e2008-09-21 21:44:29 +0000811
812 // Fold immediate in shl(x,3).
813 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
814 unsigned ResultReg = createResultReg(RC);
815 BuildMI(MBB, TII.get(OpImm),
816 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue());
817 UpdateValueMap(I, ResultReg);
818 return true;
819 }
820
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000821 unsigned Op1Reg = getRegForValue(I->getOperand(1));
822 if (Op1Reg == 0) return false;
823 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
Dan Gohman145b8282008-10-07 21:50:36 +0000824
825 // The shift instruction uses X86::CL. If we defined a super-register
826 // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what
827 // we're doing here.
828 if (CReg != X86::CL)
829 BuildMI(MBB, TII.get(TargetInstrInfo::EXTRACT_SUBREG), X86::CL)
830 .addReg(CReg).addImm(X86::SUBREG_8BIT);
831
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000832 unsigned ResultReg = createResultReg(RC);
Dan Gohman145b8282008-10-07 21:50:36 +0000833 BuildMI(MBB, TII.get(OpReg), ResultReg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000834 UpdateValueMap(I, ResultReg);
835 return true;
836}
837
838bool X86FastISel::X86SelectSelect(Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +0000839 MVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
840 if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
841 return false;
842
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000843 unsigned Opc = 0;
844 const TargetRegisterClass *RC = NULL;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000845 if (VT.getSimpleVT() == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +0000846 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000847 RC = &X86::GR16RegClass;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000848 } else if (VT.getSimpleVT() == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +0000849 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000850 RC = &X86::GR32RegClass;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000851 } else if (VT.getSimpleVT() == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +0000852 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000853 RC = &X86::GR64RegClass;
854 } else {
855 return false;
856 }
857
858 unsigned Op0Reg = getRegForValue(I->getOperand(0));
859 if (Op0Reg == 0) return false;
860 unsigned Op1Reg = getRegForValue(I->getOperand(1));
861 if (Op1Reg == 0) return false;
862 unsigned Op2Reg = getRegForValue(I->getOperand(2));
863 if (Op2Reg == 0) return false;
864
865 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
866 unsigned ResultReg = createResultReg(RC);
867 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
868 UpdateValueMap(I, ResultReg);
869 return true;
870}
871
Dan Gohman78efce62008-09-10 21:02:08 +0000872bool X86FastISel::X86SelectFPExt(Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +0000873 // fpext from float to double.
874 if (Subtarget->hasSSE2() && I->getType() == Type::DoubleTy) {
875 Value *V = I->getOperand(0);
876 if (V->getType() == Type::FloatTy) {
877 unsigned OpReg = getRegForValue(V);
878 if (OpReg == 0) return false;
879 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
880 BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
881 UpdateValueMap(I, ResultReg);
882 return true;
Dan Gohman78efce62008-09-10 21:02:08 +0000883 }
884 }
885
886 return false;
887}
888
889bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
890 if (Subtarget->hasSSE2()) {
891 if (I->getType() == Type::FloatTy) {
892 Value *V = I->getOperand(0);
893 if (V->getType() == Type::DoubleTy) {
894 unsigned OpReg = getRegForValue(V);
895 if (OpReg == 0) return false;
896 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
897 BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
898 UpdateValueMap(I, ResultReg);
899 return true;
900 }
901 }
902 }
903
904 return false;
905}
906
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000907bool X86FastISel::X86SelectTrunc(Instruction *I) {
908 if (Subtarget->is64Bit())
909 // All other cases should be handled by the tblgen generated code.
910 return false;
911 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
912 MVT DstVT = TLI.getValueType(I->getType());
913 if (DstVT != MVT::i8)
914 // All other cases should be handled by the tblgen generated code.
915 return false;
916 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
917 // All other cases should be handled by the tblgen generated code.
918 return false;
919
920 unsigned InputReg = getRegForValue(I->getOperand(0));
921 if (!InputReg)
922 // Unhandled operand. Halt "fast" selection and bail.
923 return false;
924
925 // First issue a copy to GR16_ or GR32_.
926 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
927 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
928 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
929 unsigned CopyReg = createResultReg(CopyRC);
930 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
931
932 // Then issue an extract_subreg.
Dan Gohman145b8282008-10-07 21:50:36 +0000933 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg, X86::SUBREG_8BIT);
Evan Cheng10a8d9c2008-09-07 08:47:42 +0000934 if (!ResultReg)
935 return false;
936
937 UpdateValueMap(I, ResultReg);
938 return true;
939}
940
Evan Chengf3d4efe2008-09-07 09:09:33 +0000941bool X86FastISel::X86SelectCall(Instruction *I) {
942 CallInst *CI = cast<CallInst>(I);
943 Value *Callee = I->getOperand(0);
944
945 // Can't handle inline asm yet.
946 if (isa<InlineAsm>(Callee))
947 return false;
948
949 // FIXME: Handle some intrinsics.
950 if (Function *F = CI->getCalledFunction()) {
951 if (F->isDeclaration() &&F->getIntrinsicID())
952 return false;
953 }
954
Evan Chengf3d4efe2008-09-07 09:09:33 +0000955 // Handle only C and fastcc calling conventions for now.
956 CallSite CS(CI);
957 unsigned CC = CS.getCallingConv();
958 if (CC != CallingConv::C &&
959 CC != CallingConv::Fast &&
960 CC != CallingConv::X86_FastCall)
961 return false;
962
963 // Let SDISel handle vararg functions.
964 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
965 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
966 if (FTy->isVarArg())
967 return false;
968
969 // Handle *simple* calls for now.
970 const Type *RetTy = CS.getType();
971 MVT RetVT;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000972 if (RetTy == Type::VoidTy)
973 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000974 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +0000975 return false;
976
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000977 // Materialize callee address in a register. FIXME: GV address can be
978 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000979 X86AddressMode CalleeAM;
980 if (!X86SelectAddress(Callee, CalleeAM, true))
981 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000982 unsigned CalleeOp = 0;
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000983 GlobalValue *GV = 0;
984 if (CalleeAM.Base.Reg != 0) {
985 assert(CalleeAM.GV == 0);
986 CalleeOp = CalleeAM.Base.Reg;
987 } else if (CalleeAM.GV != 0) {
988 assert(CalleeAM.GV != 0);
989 GV = CalleeAM.GV;
990 } else
991 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +0000992
Evan Chengdebdea02008-09-08 17:15:42 +0000993 // Allow calls which produce i1 results.
994 bool AndToI1 = false;
995 if (RetVT == MVT::i1) {
996 RetVT = MVT::i8;
997 AndToI1 = true;
998 }
999
Evan Chengf3d4efe2008-09-07 09:09:33 +00001000 // Deal with call operands first.
Chris Lattner241ab472008-10-15 05:38:32 +00001001 SmallVector<Value*, 8> ArgVals;
1002 SmallVector<unsigned, 8> Args;
1003 SmallVector<MVT, 8> ArgVTs;
1004 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001005 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001006 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001007 ArgVTs.reserve(CS.arg_size());
1008 ArgFlags.reserve(CS.arg_size());
1009 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1010 i != e; ++i) {
1011 unsigned Arg = getRegForValue(*i);
1012 if (Arg == 0)
1013 return false;
1014 ISD::ArgFlagsTy Flags;
1015 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001016 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001017 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001018 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001019 Flags.setZExt();
1020
1021 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001022 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1023 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1024 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1025 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001026 return false;
1027
1028 const Type *ArgTy = (*i)->getType();
1029 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001030 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001031 return false;
1032 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1033 Flags.setOrigAlign(OriginalAlignment);
1034
1035 Args.push_back(Arg);
Chris Lattner241ab472008-10-15 05:38:32 +00001036 ArgVals.push_back(*i);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001037 ArgVTs.push_back(ArgVT);
1038 ArgFlags.push_back(Flags);
1039 }
1040
1041 // Analyze operands of the call, assigning locations to each operand.
1042 SmallVector<CCValAssign, 16> ArgLocs;
1043 CCState CCInfo(CC, false, TM, ArgLocs);
1044 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1045
1046 // Get a count of how many bytes are to be pushed on the stack.
1047 unsigned NumBytes = CCInfo.getNextStackOffset();
1048
1049 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001050 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
1051 BuildMI(MBB, TII.get(AdjStackDown)).addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001052
Chris Lattner438949a2008-10-15 05:30:52 +00001053 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001054 // copies / loads.
1055 SmallVector<unsigned, 4> RegArgs;
1056 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1057 CCValAssign &VA = ArgLocs[i];
1058 unsigned Arg = Args[VA.getValNo()];
1059 MVT ArgVT = ArgVTs[VA.getValNo()];
1060
1061 // Promote the value if needed.
1062 switch (VA.getLocInfo()) {
1063 default: assert(0 && "Unknown loc info!");
1064 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001065 case CCValAssign::SExt: {
1066 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1067 Arg, ArgVT, Arg);
1068 assert(Emitted && "Failed to emit a sext!");
1069 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001070 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001071 }
1072 case CCValAssign::ZExt: {
1073 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1074 Arg, ArgVT, Arg);
1075 assert(Emitted && "Failed to emit a zext!");
1076 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001077 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001078 }
1079 case CCValAssign::AExt: {
1080 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1081 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001082 if (!Emitted)
1083 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001084 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001085 if (!Emitted)
1086 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1087 Arg, ArgVT, Arg);
1088
Evan Cheng24e3a902008-09-08 06:35:17 +00001089 assert(Emitted && "Failed to emit a aext!");
1090 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001091 break;
1092 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001093 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001094
1095 if (VA.isRegLoc()) {
1096 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
1097 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
1098 Arg, RC, RC);
1099 assert(Emitted && "Failed to emit a copy instruction!");
1100 RegArgs.push_back(VA.getLocReg());
1101 } else {
1102 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001103 X86AddressMode AM;
1104 AM.Base.Reg = StackPtr;
1105 AM.Disp = LocMemOffset;
Chris Lattner241ab472008-10-15 05:38:32 +00001106 Value *ArgVal = ArgVals[VA.getValNo()];
1107
1108 // If this is a really simple value, emit this with the Value* version of
1109 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1110 // can cause us to reevaluate the argument.
1111 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1112 X86FastEmitStore(ArgVT, ArgVal, AM);
1113 else
1114 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001115 }
1116 }
1117
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001118 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1119 // GOT pointer.
1120 if (!Subtarget->is64Bit() &&
1121 TM.getRelocationModel() == Reloc::PIC_ &&
1122 Subtarget->isPICStyleGOT()) {
1123 TargetRegisterClass *RC = X86::GR32RegisterClass;
Dan Gohman57c3dac2008-09-30 00:58:23 +00001124 unsigned Base = getInstrInfo()->getGlobalBaseReg(&MF);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001125 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
1126 assert(Emitted && "Failed to emit a copy instruction!");
1127 }
1128
Evan Chengf3d4efe2008-09-07 09:09:33 +00001129 // Issue the call.
1130 unsigned CallOpc = CalleeOp
1131 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
1132 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
1133 MachineInstrBuilder MIB = CalleeOp
1134 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001135 : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001136
1137 // Add an implicit use GOT pointer in EBX.
1138 if (!Subtarget->is64Bit() &&
1139 TM.getRelocationModel() == Reloc::PIC_ &&
1140 Subtarget->isPICStyleGOT())
1141 MIB.addReg(X86::EBX);
1142
Evan Chengf3d4efe2008-09-07 09:09:33 +00001143 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001144 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1145 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001146
1147 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001148 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
1149 BuildMI(MBB, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001150
1151 // Now handle call return value (if any).
Evan Chengf3d4efe2008-09-07 09:09:33 +00001152 if (RetVT.getSimpleVT() != MVT::isVoid) {
1153 SmallVector<CCValAssign, 16> RVLocs;
1154 CCState CCInfo(CC, false, TM, RVLocs);
1155 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1156
1157 // Copy all of the result registers out of their specified physreg.
1158 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
1159 MVT CopyVT = RVLocs[0].getValVT();
1160 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1161 TargetRegisterClass *SrcRC = DstRC;
1162
1163 // If this is a call to a function that returns an fp value on the x87 fp
1164 // stack, but where we prefer to use the value in xmm registers, copy it
1165 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1166 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1167 RVLocs[0].getLocReg() == X86::ST1) &&
1168 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
1169 CopyVT = MVT::f80;
1170 SrcRC = X86::RSTRegisterClass;
1171 DstRC = X86::RFP80RegisterClass;
1172 }
1173
1174 unsigned ResultReg = createResultReg(DstRC);
1175 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1176 RVLocs[0].getLocReg(), DstRC, SrcRC);
1177 assert(Emitted && "Failed to emit a copy instruction!");
1178 if (CopyVT != RVLocs[0].getValVT()) {
1179 // Round the F80 the right size, which also moves to the appropriate xmm
1180 // register. This is accomplished by storing the F80 value in memory and
1181 // then loading it back. Ewww...
1182 MVT ResVT = RVLocs[0].getValVT();
1183 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1184 unsigned MemSize = ResVT.getSizeInBits()/8;
Dan Gohman0586d912008-09-10 20:11:02 +00001185 int FI = MFI.CreateStackObject(MemSize, MemSize);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001186 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
1187 DstRC = ResVT == MVT::f32
1188 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1189 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1190 ResultReg = createResultReg(DstRC);
1191 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
1192 }
1193
Evan Chengdebdea02008-09-08 17:15:42 +00001194 if (AndToI1) {
1195 // Mask out all but lowest bit for some call which produces an i1.
1196 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
1197 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
1198 ResultReg = AndResult;
1199 }
1200
Evan Chengf3d4efe2008-09-07 09:09:33 +00001201 UpdateValueMap(I, ResultReg);
1202 }
1203
1204 return true;
1205}
1206
1207
Dan Gohman99b21822008-08-28 23:21:34 +00001208bool
Dan Gohman3df24e62008-09-03 23:12:08 +00001209X86FastISel::TargetSelectInstruction(Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001210 switch (I->getOpcode()) {
1211 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001212 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001213 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001214 case Instruction::Store:
1215 return X86SelectStore(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001216 case Instruction::ICmp:
1217 case Instruction::FCmp:
1218 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001219 case Instruction::ZExt:
1220 return X86SelectZExt(I);
1221 case Instruction::Br:
1222 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001223 case Instruction::Call:
1224 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001225 case Instruction::LShr:
1226 case Instruction::AShr:
1227 case Instruction::Shl:
1228 return X86SelectShift(I);
1229 case Instruction::Select:
1230 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001231 case Instruction::Trunc:
1232 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001233 case Instruction::FPExt:
1234 return X86SelectFPExt(I);
1235 case Instruction::FPTrunc:
1236 return X86SelectFPTrunc(I);
Dan Gohman99b21822008-08-28 23:21:34 +00001237 }
1238
1239 return false;
1240}
1241
Dan Gohman0586d912008-09-10 20:11:02 +00001242unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
Evan Cheng59fbc802008-09-09 01:26:59 +00001243 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001244 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001245 return false;
1246
1247 // Get opcode and regclass of the output for the given load instruction.
1248 unsigned Opc = 0;
1249 const TargetRegisterClass *RC = NULL;
1250 switch (VT.getSimpleVT()) {
1251 default: return false;
1252 case MVT::i8:
1253 Opc = X86::MOV8rm;
1254 RC = X86::GR8RegisterClass;
1255 break;
1256 case MVT::i16:
1257 Opc = X86::MOV16rm;
1258 RC = X86::GR16RegisterClass;
1259 break;
1260 case MVT::i32:
1261 Opc = X86::MOV32rm;
1262 RC = X86::GR32RegisterClass;
1263 break;
1264 case MVT::i64:
1265 // Must be in x86-64 mode.
1266 Opc = X86::MOV64rm;
1267 RC = X86::GR64RegisterClass;
1268 break;
1269 case MVT::f32:
1270 if (Subtarget->hasSSE1()) {
1271 Opc = X86::MOVSSrm;
1272 RC = X86::FR32RegisterClass;
1273 } else {
1274 Opc = X86::LD_Fp32m;
1275 RC = X86::RFP32RegisterClass;
1276 }
1277 break;
1278 case MVT::f64:
1279 if (Subtarget->hasSSE2()) {
1280 Opc = X86::MOVSDrm;
1281 RC = X86::FR64RegisterClass;
1282 } else {
1283 Opc = X86::LD_Fp64m;
1284 RC = X86::RFP64RegisterClass;
1285 }
1286 break;
1287 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001288 // No f80 support yet.
1289 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001290 }
1291
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001292 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001293 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001294 X86AddressMode AM;
1295 if (X86SelectAddress(C, AM, false)) {
1296 if (TLI.getPointerTy() == MVT::i32)
1297 Opc = X86::LEA32r;
1298 else
1299 Opc = X86::LEA64r;
1300 unsigned ResultReg = createResultReg(RC);
1301 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001302 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001303 }
Evan Cheng0de588f2008-09-05 21:00:03 +00001304 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00001305 }
1306
Owen Anderson3b217c62008-09-06 01:11:01 +00001307 // MachineConstantPool wants an explicit alignment.
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001308 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001309 if (Align == 0) {
1310 // Alignment of vector types. FIXME!
Dan Gohman1fbc3cd2008-09-18 18:26:43 +00001311 Align = TD.getABITypeSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00001312 Align = Log2_64(Align);
1313 }
Owen Anderson95267a12008-09-05 00:06:23 +00001314
Dan Gohman5396c992008-09-30 01:21:32 +00001315 // x86-32 PIC requires a PIC base register for constant pools.
1316 unsigned PICBase = 0;
1317 if (TM.getRelocationModel() == Reloc::PIC_ &&
1318 !Subtarget->is64Bit())
1319 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1320
1321 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00001322 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001323 unsigned ResultReg = createResultReg(RC);
Dan Gohman5396c992008-09-30 01:21:32 +00001324 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset,
1325 PICBase);
1326
Owen Anderson95267a12008-09-05 00:06:23 +00001327 return ResultReg;
1328}
1329
Dan Gohman0586d912008-09-10 20:11:02 +00001330unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00001331 // Fail on dynamic allocas. At this point, getRegForValue has already
1332 // checked its CSE maps, so if we're here trying to handle a dynamic
1333 // alloca, we're not going to succeed. X86SelectAddress has a
1334 // check for dynamic allocas, because it's called directly from
1335 // various places, but TargetMaterializeAlloca also needs a check
1336 // in order to avoid recursion between getRegForValue,
1337 // X86SelectAddrss, and TargetMaterializeAlloca.
1338 if (!StaticAllocaMap.count(C))
1339 return 0;
1340
Dan Gohman0586d912008-09-10 20:11:02 +00001341 X86AddressMode AM;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001342 if (!X86SelectAddress(C, AM, false))
Dan Gohman0586d912008-09-10 20:11:02 +00001343 return 0;
1344 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1345 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1346 unsigned ResultReg = createResultReg(RC);
1347 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1348 return ResultReg;
1349}
1350
Evan Chengc3f44b02008-09-03 00:03:49 +00001351namespace llvm {
Dan Gohman3df24e62008-09-03 23:12:08 +00001352 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001353 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +00001354 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +00001355 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001356 DenseMap<const AllocaInst *, int> &am
1357#ifndef NDEBUG
1358 , SmallSet<Instruction*, 8> &cil
1359#endif
1360 ) {
1361 return new X86FastISel(mf, mmi, vm, bm, am
1362#ifndef NDEBUG
1363 , cil
1364#endif
1365 );
Evan Chengc3f44b02008-09-03 00:03:49 +00001366 }
Dan Gohman99b21822008-08-28 23:21:34 +00001367}