blob: 1382f184c34319d47a66ab6eb165114734373a64 [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"
Evan Cheng88e30412008-09-03 01:04:47 +000018#include "X86RegisterInfo.h"
19#include "X86Subtarget.h"
Dan Gohman22bb3112008-08-22 00:20:26 +000020#include "X86TargetMachine.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000021#include "llvm/CallingConv.h"
Dan Gohman6e3f05f2008-09-04 23:26:51 +000022#include "llvm/DerivedTypes.h"
Dan Gohmane9865942009-02-23 22:03:08 +000023#include "llvm/GlobalVariable.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000024#include "llvm/Instructions.h"
Chris Lattnera9a42252009-04-12 07:36:01 +000025#include "llvm/IntrinsicInst.h"
Jay Foad562b84b2011-04-11 09:35:34 +000026#include "llvm/Operator.h"
Dan Gohman84023e02010-07-10 09:00:22 +000027#include "llvm/CodeGen/Analysis.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000028#include "llvm/CodeGen/FastISel.h"
Dan Gohmana4160c32010-07-07 16:29:44 +000029#include "llvm/CodeGen/FunctionLoweringInfo.h"
Owen Anderson95267a12008-09-05 00:06:23 +000030#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000031#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000033#include "llvm/Support/CallSite.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000034#include "llvm/Support/ErrorHandling.h"
Dan Gohman35893082008-09-18 23:23:44 +000035#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Cheng381993f2010-01-27 00:00:57 +000036#include "llvm/Target/TargetOptions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000037using namespace llvm;
38
Chris Lattner087fcf32009-03-08 18:44:31 +000039namespace {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000040
Evan Chengc3f44b02008-09-03 00:03:49 +000041class X86FastISel : public FastISel {
42 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
43 /// make the right decision when generating code for different targets.
44 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000045
46 /// StackPtr - Register used as the stack pointer.
47 ///
48 unsigned StackPtr;
49
Wesley Peckbf17cfa2010-11-23 03:31:01 +000050 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
Evan Chengf3d4efe2008-09-07 09:09:33 +000051 /// floating point ops.
52 /// When SSE is available, use it for f32 operations.
53 /// When SSE2 is available, use it for f64 operations.
54 bool X86ScalarSSEf64;
55 bool X86ScalarSSEf32;
56
Evan Cheng8b19e562008-09-03 06:44:39 +000057public:
Dan Gohmana4160c32010-07-07 16:29:44 +000058 explicit X86FastISel(FunctionLoweringInfo &funcInfo) : FastISel(funcInfo) {
Evan Cheng88e30412008-09-03 01:04:47 +000059 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000060 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
61 X86ScalarSSEf64 = Subtarget->hasSSE2();
62 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000063 }
Evan Chengc3f44b02008-09-03 00:03:49 +000064
Dan Gohman46510a72010-04-15 01:51:59 +000065 virtual bool TargetSelectInstruction(const Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000066
Chris Lattnerbeac75d2010-09-05 02:18:34 +000067 /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
68 /// vreg is being provided by the specified load instruction. If possible,
69 /// try to fold the load as an operand to the instruction, returning true if
70 /// possible.
71 virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
72 const LoadInst *LI);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000073
Dan Gohman1adf1b02008-08-19 21:45:35 +000074#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000075
76private:
Dan Gohman46510a72010-04-15 01:51:59 +000077 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000078
Owen Andersone50ed302009-08-10 22:56:29 +000079 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000080
Chris Lattnerb44101c2011-04-19 05:09:50 +000081 bool X86FastEmitStore(EVT VT, const Value *Val, const X86AddressMode &AM);
82 bool X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000083
Owen Andersone50ed302009-08-10 22:56:29 +000084 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +000085 unsigned &ResultReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000086
Dan Gohman46510a72010-04-15 01:51:59 +000087 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
88 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman0586d912008-09-10 20:11:02 +000089
Dan Gohman46510a72010-04-15 01:51:59 +000090 bool X86SelectLoad(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000091
Dan Gohman46510a72010-04-15 01:51:59 +000092 bool X86SelectStore(const Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000093
Dan Gohman84023e02010-07-10 09:00:22 +000094 bool X86SelectRet(const Instruction *I);
95
Dan Gohman46510a72010-04-15 01:51:59 +000096 bool X86SelectCmp(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000097
Dan Gohman46510a72010-04-15 01:51:59 +000098 bool X86SelectZExt(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000099
Dan Gohman46510a72010-04-15 01:51:59 +0000100 bool X86SelectBranch(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000101
Dan Gohman46510a72010-04-15 01:51:59 +0000102 bool X86SelectShift(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000103
Dan Gohman46510a72010-04-15 01:51:59 +0000104 bool X86SelectSelect(const Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000105
Dan Gohman46510a72010-04-15 01:51:59 +0000106 bool X86SelectTrunc(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000107
Dan Gohman46510a72010-04-15 01:51:59 +0000108 bool X86SelectFPExt(const Instruction *I);
109 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohman78efce62008-09-10 21:02:08 +0000110
Dan Gohman46510a72010-04-15 01:51:59 +0000111 bool X86SelectExtractValue(const Instruction *I);
Bill Wendling52370a12008-12-09 02:42:50 +0000112
Dan Gohman46510a72010-04-15 01:51:59 +0000113 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
114 bool X86SelectCall(const Instruction *I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000115
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000116 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000117 return getTargetMachine()->getInstrInfo();
118 }
119 const X86TargetMachine *getTargetMachine() const {
120 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000121 }
122
Dan Gohman46510a72010-04-15 01:51:59 +0000123 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000124
Dan Gohman46510a72010-04-15 01:51:59 +0000125 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000126
Eli Friedman2790ba82011-04-27 22:41:55 +0000127 unsigned TargetMaterializeFloatZero(const ConstantFP *CF);
128
Evan Chengf3d4efe2008-09-07 09:09:33 +0000129 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
130 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000131 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
133 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000134 }
135
Duncan Sands1440e8b2010-11-03 11:35:31 +0000136 bool isTypeLegal(const Type *Ty, MVT &VT, bool AllowI1 = false);
Eli Friedmand5089a92011-04-27 01:45:07 +0000137
138 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
139 X86AddressMode SrcAM, uint64_t Len);
Evan Chengc3f44b02008-09-03 00:03:49 +0000140};
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000141
Chris Lattner087fcf32009-03-08 18:44:31 +0000142} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000143
Duncan Sands1440e8b2010-11-03 11:35:31 +0000144bool X86FastISel::isTypeLegal(const Type *Ty, MVT &VT, bool AllowI1) {
145 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
146 if (evt == MVT::Other || !evt.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000147 // Unhandled type. Halt "fast" selection and bail.
148 return false;
Duncan Sands1440e8b2010-11-03 11:35:31 +0000149
150 VT = evt.getSimpleVT();
Dan Gohman9b66d732008-09-30 00:48:39 +0000151 // For now, require SSE/SSE2 for performing floating-point operations,
152 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000153 if (VT == MVT::f64 && !X86ScalarSSEf64)
Dan Gohman9b66d732008-09-30 00:48:39 +0000154 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000155 if (VT == MVT::f32 && !X86ScalarSSEf32)
Dan Gohman9b66d732008-09-30 00:48:39 +0000156 return false;
157 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000159 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000160 // We only handle legal types. For example, on x86-32 the instruction
161 // selector contains all of the 64-bit instructions from x86-64,
162 // under the assumption that i64 won't be used if the target doesn't
163 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000164 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000165}
166
167#include "X86GenCallingConv.inc"
168
Evan Cheng0de588f2008-09-05 21:00:03 +0000169/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000170/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000171/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000172bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000173 unsigned &ResultReg) {
174 // Get opcode and regclass of the output for the given load instruction.
175 unsigned Opc = 0;
176 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000177 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000178 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000179 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000180 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000181 Opc = X86::MOV8rm;
182 RC = X86::GR8RegisterClass;
183 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000184 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000185 Opc = X86::MOV16rm;
186 RC = X86::GR16RegisterClass;
187 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000188 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000189 Opc = X86::MOV32rm;
190 RC = X86::GR32RegisterClass;
191 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000193 // Must be in x86-64 mode.
194 Opc = X86::MOV64rm;
195 RC = X86::GR64RegisterClass;
196 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000197 case MVT::f32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000198 if (Subtarget->hasSSE1()) {
199 Opc = X86::MOVSSrm;
200 RC = X86::FR32RegisterClass;
201 } else {
202 Opc = X86::LD_Fp32m;
203 RC = X86::RFP32RegisterClass;
204 }
205 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000206 case MVT::f64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000207 if (Subtarget->hasSSE2()) {
208 Opc = X86::MOVSDrm;
209 RC = X86::FR64RegisterClass;
210 } else {
211 Opc = X86::LD_Fp64m;
212 RC = X86::RFP64RegisterClass;
213 }
214 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000215 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000216 // No f80 support yet.
217 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000218 }
219
220 ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000221 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
222 DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000223 return true;
224}
225
Evan Chengf3d4efe2008-09-07 09:09:33 +0000226/// X86FastEmitStore - Emit a machine instruction to store a value Val of
227/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
228/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000229/// i.e. V. Return true if it is possible.
230bool
Chris Lattnerb44101c2011-04-19 05:09:50 +0000231X86FastISel::X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000232 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000233 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000234 switch (VT.getSimpleVT().SimpleTy) {
235 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000236 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000237 case MVT::i1: {
238 // Mask out all but lowest bit.
239 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000240 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000241 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
242 Val = AndResult;
243 }
244 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +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.
249 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;
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 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 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000256
Dan Gohman84023e02010-07-10 09:00:22 +0000257 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
258 DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000259 return true;
260}
261
Dan Gohman46510a72010-04-15 01:51:59 +0000262bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000263 const X86AddressMode &AM) {
264 // Handle 'null' like i32/i64 0.
265 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000266 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000267
Chris Lattner438949a2008-10-15 05:30:52 +0000268 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000269 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000270 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000271 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000273 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000274 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000275 case MVT::i8: Opc = X86::MOV8mi; break;
276 case MVT::i16: Opc = X86::MOV16mi; break;
277 case MVT::i32: Opc = X86::MOV32mi; break;
278 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000279 // Must be a 32-bit sign extended value.
280 if ((int)CI->getSExtValue() == CI->getSExtValue())
281 Opc = X86::MOV64mi32;
282 break;
283 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000284
Chris Lattner438949a2008-10-15 05:30:52 +0000285 if (Opc) {
Dan Gohman84023e02010-07-10 09:00:22 +0000286 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
287 DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000288 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000289 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000290 return true;
291 }
292 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000293
Chris Lattner438949a2008-10-15 05:30:52 +0000294 unsigned ValReg = getRegForValue(Val);
295 if (ValReg == 0)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000296 return false;
297
Chris Lattner438949a2008-10-15 05:30:52 +0000298 return X86FastEmitStore(VT, ValReg, AM);
299}
300
Evan Cheng24e3a902008-09-08 06:35:17 +0000301/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
302/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
303/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000304bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
305 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000306 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000307 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
308 Src, /*TODO: Kill=*/false);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000309
Owen Andersonac34a002008-09-11 19:44:55 +0000310 if (RR != 0) {
311 ResultReg = RR;
312 return true;
313 } else
314 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000315}
316
Dan Gohman0586d912008-09-10 20:11:02 +0000317/// X86SelectAddress - Attempt to fill in an address from the given value.
318///
Dan Gohman46510a72010-04-15 01:51:59 +0000319bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
320 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000321 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000322 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000323 // Don't walk into other basic blocks; it's possible we haven't
324 // visited them yet, so the instructions may not yet be assigned
325 // virtual registers.
Dan Gohman742bf872010-11-16 22:43:23 +0000326 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
327 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
328 Opcode = I->getOpcode();
329 U = I;
330 }
Dan Gohman46510a72010-04-15 01:51:59 +0000331 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000332 Opcode = C->getOpcode();
333 U = C;
334 }
Dan Gohman0586d912008-09-10 20:11:02 +0000335
Chris Lattner868ee942010-06-15 19:08:40 +0000336 if (const PointerType *Ty = dyn_cast<PointerType>(V->getType()))
337 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000338 // Fast instruction selection doesn't support the special
339 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000340 return false;
341
Dan Gohman35893082008-09-18 23:23:44 +0000342 switch (Opcode) {
343 default: break;
344 case Instruction::BitCast:
345 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000346 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000347
348 case Instruction::IntToPtr:
349 // Look past no-op inttoptrs.
350 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000351 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000352 break;
Dan Gohman35893082008-09-18 23:23:44 +0000353
354 case Instruction::PtrToInt:
355 // Look past no-op ptrtoints.
356 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000357 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000358 break;
Dan Gohman35893082008-09-18 23:23:44 +0000359
360 case Instruction::Alloca: {
361 // Do static allocas.
362 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000363 DenseMap<const AllocaInst*, int>::iterator SI =
364 FuncInfo.StaticAllocaMap.find(A);
365 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000366 AM.BaseType = X86AddressMode::FrameIndexBase;
367 AM.Base.FrameIndex = SI->second;
368 return true;
369 }
370 break;
Dan Gohman35893082008-09-18 23:23:44 +0000371 }
372
373 case Instruction::Add: {
374 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000375 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000376 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
377 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000378 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000379 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000380 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000381 }
Dan Gohman0586d912008-09-10 20:11:02 +0000382 }
Dan Gohman35893082008-09-18 23:23:44 +0000383 break;
384 }
385
386 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000387 X86AddressMode SavedAM = AM;
388
Dan Gohman35893082008-09-18 23:23:44 +0000389 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000390 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000391 unsigned IndexReg = AM.IndexReg;
392 unsigned Scale = AM.Scale;
393 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000394 // Iterate through the indices, folding what we can. Constants can be
395 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000396 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000397 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000398 const Value *Op = *i;
Dan Gohman35893082008-09-18 23:23:44 +0000399 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
400 const StructLayout *SL = TD.getStructLayout(STy);
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000401 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
402 continue;
403 }
404
405 // A array/variable index is always of the form i*S where S is the
406 // constant scale size. See if we can push the scale into immediates.
407 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
408 for (;;) {
409 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
410 // Constant-offset addressing.
411 Disp += CI->getSExtValue() * S;
412 break;
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000413 }
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000414 if (isa<AddOperator>(Op) &&
415 (!isa<Instruction>(Op) ||
416 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
417 == FuncInfo.MBB) &&
418 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
419 // An add (in the same block) with a constant operand. Fold the
420 // constant.
421 ConstantInt *CI =
422 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
423 Disp += CI->getSExtValue() * S;
424 // Iterate on the other operand.
425 Op = cast<AddOperator>(Op)->getOperand(0);
426 continue;
427 }
428 if (IndexReg == 0 &&
429 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
430 (S == 1 || S == 2 || S == 4 || S == 8)) {
431 // Scaled-index addressing.
432 Scale = S;
433 IndexReg = getRegForGEPIndex(Op).first;
434 if (IndexReg == 0)
435 return false;
436 break;
437 }
438 // Unsupported.
439 goto unsupported_gep;
Dan Gohman35893082008-09-18 23:23:44 +0000440 }
441 }
Dan Gohman09aae462008-09-26 20:04:15 +0000442 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000443 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000444 break;
Dan Gohman35893082008-09-18 23:23:44 +0000445 // Ok, the GEP indices were covered by constant-offset and scaled-index
446 // addressing. Update the address state and move on to examining the base.
447 AM.IndexReg = IndexReg;
448 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000449 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000450 if (X86SelectAddress(U->getOperand(0), AM))
451 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000452
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000453 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner225d4ca2010-03-04 19:48:19 +0000454 // our address and just match the value instead of completely failing.
455 AM = SavedAM;
456 break;
Dan Gohman35893082008-09-18 23:23:44 +0000457 unsupported_gep:
458 // Ok, the GEP indices weren't all covered.
459 break;
460 }
461 }
462
463 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000464 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0a1c9972011-04-17 17:47:38 +0000465 // Can't handle alternate code models or TLS yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000466 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000467 return false;
468
Dan Gohman46510a72010-04-15 01:51:59 +0000469 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000470 if (GVar->isThreadLocal())
471 return false;
Chris Lattner0a1c9972011-04-17 17:47:38 +0000472
473 // RIP-relative addresses can't have additional register operands, so if
474 // we've already folded stuff into the addressing mode, just force the
475 // global value into its own register, which we can use as the basereg.
476 if (!Subtarget->isPICStyleRIPRel() ||
477 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
478 // Okay, we've committed to selecting this global. Set up the address.
479 AM.GV = GV;
Dan Gohmane9865942009-02-23 22:03:08 +0000480
Chris Lattner0a1c9972011-04-17 17:47:38 +0000481 // Allow the subtarget to classify the global.
482 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000483
Chris Lattner0a1c9972011-04-17 17:47:38 +0000484 // If this reference is relative to the pic base, set it now.
485 if (isGlobalRelativeToPICBase(GVFlags)) {
486 // FIXME: How do we know Base.Reg is free??
487 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Dan Gohman7e8ef602008-09-19 23:42:04 +0000488 }
Chris Lattner0a1c9972011-04-17 17:47:38 +0000489
490 // Unless the ABI requires an extra load, return a direct reference to
491 // the global.
492 if (!isGlobalStubReference(GVFlags)) {
493 if (Subtarget->isPICStyleRIPRel()) {
494 // Use rip-relative addressing if we can. Above we verified that the
495 // base and index registers are unused.
496 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
497 AM.Base.Reg = X86::RIP;
498 }
499 AM.GVOpFlags = GVFlags;
500 return true;
501 }
502
503 // Ok, we need to do a load from a stub. If we've already loaded from
504 // this stub, reuse the loaded pointer, otherwise emit the load now.
505 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
506 unsigned LoadReg;
507 if (I != LocalValueMap.end() && I->second != 0) {
508 LoadReg = I->second;
509 } else {
510 // Issue load from stub.
511 unsigned Opc = 0;
512 const TargetRegisterClass *RC = NULL;
513 X86AddressMode StubAM;
514 StubAM.Base.Reg = AM.Base.Reg;
515 StubAM.GV = GV;
516 StubAM.GVOpFlags = GVFlags;
517
518 // Prepare for inserting code in the local-value area.
519 SavePoint SaveInsertPt = enterLocalValueArea();
520
521 if (TLI.getPointerTy() == MVT::i64) {
522 Opc = X86::MOV64rm;
523 RC = X86::GR64RegisterClass;
524
525 if (Subtarget->isPICStyleRIPRel())
526 StubAM.Base.Reg = X86::RIP;
527 } else {
528 Opc = X86::MOV32rm;
529 RC = X86::GR32RegisterClass;
530 }
531
532 LoadReg = createResultReg(RC);
533 MachineInstrBuilder LoadMI =
534 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
535 addFullAddress(LoadMI, StubAM);
536
537 // Ok, back to normal mode.
538 leaveLocalValueArea(SaveInsertPt);
539
540 // Prevent loading GV stub multiple times in same MBB.
541 LocalValueMap[V] = LoadReg;
542 }
543
544 // Now construct the final address. Note that the Disp, Scale,
545 // and Index values may already be set here.
546 AM.Base.Reg = LoadReg;
547 AM.GV = 0;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000548 return true;
549 }
Dan Gohman0586d912008-09-10 20:11:02 +0000550 }
551
Dan Gohman97135e12008-09-26 19:15:30 +0000552 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000553 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000554 if (AM.Base.Reg == 0) {
555 AM.Base.Reg = getRegForValue(V);
556 return AM.Base.Reg != 0;
557 }
558 if (AM.IndexReg == 0) {
559 assert(AM.Scale == 1 && "Scale with no index!");
560 AM.IndexReg = getRegForValue(V);
561 return AM.IndexReg != 0;
562 }
563 }
564
565 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000566}
567
Chris Lattner0aa43de2009-07-10 05:33:42 +0000568/// X86SelectCallAddress - Attempt to fill in an address from the given value.
569///
Dan Gohman46510a72010-04-15 01:51:59 +0000570bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
571 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000572 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000573 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000574 Opcode = I->getOpcode();
575 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000576 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000577 Opcode = C->getOpcode();
578 U = C;
579 }
580
581 switch (Opcode) {
582 default: break;
583 case Instruction::BitCast:
584 // Look past bitcasts.
585 return X86SelectCallAddress(U->getOperand(0), AM);
586
587 case Instruction::IntToPtr:
588 // Look past no-op inttoptrs.
589 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
590 return X86SelectCallAddress(U->getOperand(0), AM);
591 break;
592
593 case Instruction::PtrToInt:
594 // Look past no-op ptrtoints.
595 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
596 return X86SelectCallAddress(U->getOperand(0), AM);
597 break;
598 }
599
600 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000601 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000602 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000603 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000604 return false;
605
606 // RIP-relative addresses can't have additional register operands.
607 if (Subtarget->isPICStyleRIPRel() &&
608 (AM.Base.Reg != 0 || AM.IndexReg != 0))
609 return false;
610
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000611 // Can't handle DLLImport.
612 if (GV->hasDLLImportLinkage())
613 return false;
614
615 // Can't handle TLS.
Dan Gohman46510a72010-04-15 01:51:59 +0000616 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000617 if (GVar->isThreadLocal())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000618 return false;
619
620 // Okay, we've committed to selecting this global. Set up the basic address.
621 AM.GV = GV;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000622
Chris Lattnere6c07b52009-07-10 05:45:15 +0000623 // No ABI requires an extra load for anything other than DLLImport, which
624 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000625 if (Subtarget->isPICStyleRIPRel()) {
626 // Use rip-relative addressing if we can. Above we verified that the
627 // base and index registers are unused.
628 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
629 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000630 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000631 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
632 } else if (Subtarget->isPICStyleGOT()) {
633 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000634 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000635
Chris Lattner0aa43de2009-07-10 05:33:42 +0000636 return true;
637 }
638
639 // If all else fails, try to materialize the value in a register.
640 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
641 if (AM.Base.Reg == 0) {
642 AM.Base.Reg = getRegForValue(V);
643 return AM.Base.Reg != 0;
644 }
645 if (AM.IndexReg == 0) {
646 assert(AM.Scale == 1 && "Scale with no index!");
647 AM.IndexReg = getRegForValue(V);
648 return AM.IndexReg != 0;
649 }
650 }
651
652 return false;
653}
654
655
Owen Andersona3971df2008-09-04 07:08:58 +0000656/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000657bool X86FastISel::X86SelectStore(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000658 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000659 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000660 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000661
Dan Gohman0586d912008-09-10 20:11:02 +0000662 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000663 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000664 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000665
Chris Lattner438949a2008-10-15 05:30:52 +0000666 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000667}
668
Dan Gohman84023e02010-07-10 09:00:22 +0000669/// X86SelectRet - Select and emit code to implement ret instructions.
670bool X86FastISel::X86SelectRet(const Instruction *I) {
671 const ReturnInst *Ret = cast<ReturnInst>(I);
672 const Function &F = *I->getParent()->getParent();
673
674 if (!FuncInfo.CanLowerReturn)
675 return false;
676
677 CallingConv::ID CC = F.getCallingConv();
678 if (CC != CallingConv::C &&
679 CC != CallingConv::Fast &&
680 CC != CallingConv::X86_FastCall)
681 return false;
682
683 if (Subtarget->isTargetWin64())
684 return false;
685
686 // Don't handle popping bytes on return for now.
687 if (FuncInfo.MF->getInfo<X86MachineFunctionInfo>()
688 ->getBytesToPopOnReturn() != 0)
689 return 0;
690
691 // fastcc with -tailcallopt is intended to provide a guaranteed
692 // tail call optimization. Fastisel doesn't know how to do that.
693 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
694 return false;
695
696 // Let SDISel handle vararg functions.
697 if (F.isVarArg())
698 return false;
699
700 if (Ret->getNumOperands() > 0) {
701 SmallVector<ISD::OutputArg, 4> Outs;
702 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
703 Outs, TLI);
704
705 // Analyze operands of the call, assigning locations to each operand.
706 SmallVector<CCValAssign, 16> ValLocs;
707 CCState CCInfo(CC, F.isVarArg(), TM, ValLocs, I->getContext());
Duncan Sandse26032d2010-10-31 13:02:38 +0000708 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohman84023e02010-07-10 09:00:22 +0000709
710 const Value *RV = Ret->getOperand(0);
711 unsigned Reg = getRegForValue(RV);
712 if (Reg == 0)
713 return false;
714
715 // Only handle a single return value for now.
716 if (ValLocs.size() != 1)
717 return false;
718
719 CCValAssign &VA = ValLocs[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000720
Dan Gohman84023e02010-07-10 09:00:22 +0000721 // Don't bother handling odd stuff for now.
722 if (VA.getLocInfo() != CCValAssign::Full)
723 return false;
724 // Only handle register returns for now.
725 if (!VA.isRegLoc())
726 return false;
727 // TODO: For now, don't try to handle cases where getLocInfo()
728 // says Full but the types don't match.
Duncan Sands1e96bab2010-11-04 10:49:57 +0000729 if (TLI.getValueType(RV->getType()) != VA.getValVT())
Dan Gohman84023e02010-07-10 09:00:22 +0000730 return false;
731
732 // The calling-convention tables for x87 returns don't tell
733 // the whole story.
734 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
735 return false;
736
737 // Make the copy.
738 unsigned SrcReg = Reg + VA.getValNo();
739 unsigned DstReg = VA.getLocReg();
740 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000741 // Avoid a cross-class copy. This is very unlikely.
742 if (!SrcRC->contains(DstReg))
Dan Gohman84023e02010-07-10 09:00:22 +0000743 return false;
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000744 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
745 DstReg).addReg(SrcReg);
Dan Gohman84023e02010-07-10 09:00:22 +0000746
747 // Mark the register as live out of the function.
748 MRI.addLiveOut(VA.getLocReg());
749 }
750
751 // Now emit the RET.
752 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
753 return true;
754}
755
Evan Cheng8b19e562008-09-03 06:44:39 +0000756/// X86SelectLoad - Select and emit code to implement load instructions.
757///
Dan Gohman46510a72010-04-15 01:51:59 +0000758bool X86FastISel::X86SelectLoad(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000759 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000760 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000761 return false;
762
Dan Gohman0586d912008-09-10 20:11:02 +0000763 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000764 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000765 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000766
Evan Cheng0de588f2008-09-05 21:00:03 +0000767 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000768 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000769 UpdateValueMap(I, ResultReg);
770 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000771 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000772 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000773}
774
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000775static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000776 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000777 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000778 case MVT::i8: return X86::CMP8rr;
779 case MVT::i16: return X86::CMP16rr;
780 case MVT::i32: return X86::CMP32rr;
781 case MVT::i64: return X86::CMP64rr;
Dan Gohmanbe4d10d2010-07-12 15:46:30 +0000782 case MVT::f32: return Subtarget->hasSSE1() ? X86::UCOMISSrr : 0;
783 case MVT::f64: return Subtarget->hasSSE2() ? X86::UCOMISDrr : 0;
Dan Gohmand98d6202008-10-02 22:15:21 +0000784 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000785}
786
Chris Lattner0e13c782008-10-15 04:13:29 +0000787/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
788/// of the comparison, return an opcode that works for the compare (e.g.
789/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000790static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000791 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000792 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000793 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000794 case MVT::i8: return X86::CMP8ri;
795 case MVT::i16: return X86::CMP16ri;
796 case MVT::i32: return X86::CMP32ri;
797 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000798 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
799 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000800 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000801 return X86::CMP64ri32;
802 return 0;
803 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000804}
805
Dan Gohman46510a72010-04-15 01:51:59 +0000806bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
807 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000808 unsigned Op0Reg = getRegForValue(Op0);
809 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000810
Chris Lattnerd53886b2008-10-15 05:18:04 +0000811 // Handle 'null' like i32/i64 0.
812 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000813 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000814
Chris Lattner9a08a612008-10-15 04:26:38 +0000815 // We have two options: compare with register or immediate. If the RHS of
816 // the compare is an immediate that we can fold into this compare, use
817 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000818 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000819 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000820 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
821 .addReg(Op0Reg)
822 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000823 return true;
824 }
825 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000826
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000827 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattner9a08a612008-10-15 04:26:38 +0000828 if (CompareOpc == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000829
Chris Lattner9a08a612008-10-15 04:26:38 +0000830 unsigned Op1Reg = getRegForValue(Op1);
831 if (Op1Reg == 0) return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000832 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
833 .addReg(Op0Reg)
834 .addReg(Op1Reg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000835
Chris Lattner9a08a612008-10-15 04:26:38 +0000836 return true;
837}
838
Dan Gohman46510a72010-04-15 01:51:59 +0000839bool X86FastISel::X86SelectCmp(const Instruction *I) {
840 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000841
Duncan Sands1440e8b2010-11-03 11:35:31 +0000842 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000843 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000844 return false;
845
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000846 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000847 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000848 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000849 switch (CI->getPredicate()) {
850 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000851 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
852 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000853
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000854 unsigned EReg = createResultReg(&X86::GR8RegClass);
855 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000856 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
857 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
858 TII.get(X86::SETNPr), NPReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000859 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000860 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000861 UpdateValueMap(I, ResultReg);
862 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000863 }
864 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000865 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
866 return false;
867
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000868 unsigned NEReg = createResultReg(&X86::GR8RegClass);
869 unsigned PReg = createResultReg(&X86::GR8RegClass);
Chris Lattner90cb88a2011-04-19 04:22:17 +0000870 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETNEr), NEReg);
871 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETPr), PReg);
872 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::OR8rr),ResultReg)
Dan Gohman84023e02010-07-10 09:00:22 +0000873 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000874 UpdateValueMap(I, ResultReg);
875 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000876 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000877 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
878 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
879 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
880 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
881 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
882 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
883 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
884 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
885 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
886 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
887 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
888 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000889
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000890 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
891 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
892 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
893 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
894 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
895 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
896 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
897 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
898 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
899 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000900 default:
901 return false;
902 }
903
Dan Gohman46510a72010-04-15 01:51:59 +0000904 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000905 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000906 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000907
Chris Lattner9a08a612008-10-15 04:26:38 +0000908 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000909 if (!X86FastEmitCompare(Op0, Op1, VT))
910 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000911
Dan Gohman84023e02010-07-10 09:00:22 +0000912 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000913 UpdateValueMap(I, ResultReg);
914 return true;
915}
Evan Cheng8b19e562008-09-03 06:44:39 +0000916
Dan Gohman46510a72010-04-15 01:51:59 +0000917bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000918 // Handle zero-extension from i1 to i8, which is common.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000919 if (I->getType()->isIntegerTy(8) &&
920 I->getOperand(0)->getType()->isIntegerTy(1)) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000921 unsigned ResultReg = getRegForValue(I->getOperand(0));
Dan Gohmanf52550b2008-09-05 01:15:35 +0000922 if (ResultReg == 0) return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000923 // Set the high bits to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000924 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000925 if (ResultReg == 0) return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000926 UpdateValueMap(I, ResultReg);
927 return true;
928 }
929
930 return false;
931}
932
Chris Lattner9a08a612008-10-15 04:26:38 +0000933
Dan Gohman46510a72010-04-15 01:51:59 +0000934bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000935 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000936 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +0000937 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +0000938 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
939 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +0000940
Dan Gohman8bef7442010-08-21 02:32:36 +0000941 // Fold the common case of a conditional branch with a comparison
942 // in the same block (values defined on other blocks may not have
943 // initialized registers).
Dan Gohman46510a72010-04-15 01:51:59 +0000944 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman8bef7442010-08-21 02:32:36 +0000945 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000946 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000947
Dan Gohmand98d6202008-10-02 22:15:21 +0000948 // Try to take advantage of fallthrough opportunities.
949 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohman84023e02010-07-10 09:00:22 +0000950 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000951 std::swap(TrueMBB, FalseMBB);
952 Predicate = CmpInst::getInversePredicate(Predicate);
953 }
954
Chris Lattner871d2462008-10-15 03:58:05 +0000955 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
956 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
957
Dan Gohmand98d6202008-10-02 22:15:21 +0000958 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000959 case CmpInst::FCMP_OEQ:
960 std::swap(TrueMBB, FalseMBB);
961 Predicate = CmpInst::FCMP_UNE;
962 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000963 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
964 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
965 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
966 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
967 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
968 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
969 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
970 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
971 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
972 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
973 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
974 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
975 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000976
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000977 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
978 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
979 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
980 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
981 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
982 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
983 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
984 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
985 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
986 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +0000987 default:
988 return false;
989 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000990
Dan Gohman46510a72010-04-15 01:51:59 +0000991 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +0000992 if (SwapArgs)
993 std::swap(Op0, Op1);
994
Chris Lattner9a08a612008-10-15 04:26:38 +0000995 // Emit a compare of the LHS and RHS, setting the flags.
996 if (!X86FastEmitCompare(Op0, Op1, VT))
997 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000998
Dan Gohman84023e02010-07-10 09:00:22 +0000999 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1000 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001001
1002 if (Predicate == CmpInst::FCMP_UNE) {
1003 // X86 requires a second branch to handle UNE (and OEQ,
1004 // which is mapped to UNE above).
Dan Gohman84023e02010-07-10 09:00:22 +00001005 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1006 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001007 }
1008
Stuart Hastings3bf91252010-06-17 22:43:56 +00001009 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001010 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +00001011 return true;
1012 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001013 } else if (ExtractValueInst *EI =
1014 dyn_cast<ExtractValueInst>(BI->getCondition())) {
1015 // Check to see if the branch instruction is from an "arithmetic with
1016 // overflow" intrinsic. The main way these intrinsics are used is:
1017 //
1018 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
1019 // %sum = extractvalue { i32, i1 } %t, 0
1020 // %obit = extractvalue { i32, i1 } %t, 1
1021 // br i1 %obit, label %overflow, label %normal
1022 //
Dan Gohman653456c2009-01-07 00:15:08 +00001023 // The %sum and %obit are converted in an ADD and a SETO/SETB before
Bill Wendling30a64a72008-12-09 23:19:12 +00001024 // reaching the branch. Therefore, we search backwards through the MBB
Dan Gohman653456c2009-01-07 00:15:08 +00001025 // looking for the SETO/SETB instruction. If an instruction modifies the
1026 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
1027 // convert the branch into a JO/JB instruction.
Dan Gohman46510a72010-04-15 01:51:59 +00001028 if (const IntrinsicInst *CI =
1029 dyn_cast<IntrinsicInst>(EI->getAggregateOperand())){
Chris Lattnera9a42252009-04-12 07:36:01 +00001030 if (CI->getIntrinsicID() == Intrinsic::sadd_with_overflow ||
1031 CI->getIntrinsicID() == Intrinsic::uadd_with_overflow) {
1032 const MachineInstr *SetMI = 0;
Dan Gohman20d4be12010-07-01 02:58:57 +00001033 unsigned Reg = getRegForValue(EI);
Bill Wendling30a64a72008-12-09 23:19:12 +00001034
Chris Lattnera9a42252009-04-12 07:36:01 +00001035 for (MachineBasicBlock::const_reverse_iterator
Dan Gohman84023e02010-07-10 09:00:22 +00001036 RI = FuncInfo.MBB->rbegin(), RE = FuncInfo.MBB->rend();
1037 RI != RE; ++RI) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001038 const MachineInstr &MI = *RI;
Bill Wendling30a64a72008-12-09 23:19:12 +00001039
Evan Cheng1015ba72010-05-21 20:53:24 +00001040 if (MI.definesRegister(Reg)) {
Jakob Stoklund Olesen84d499a2010-07-16 22:35:34 +00001041 if (MI.isCopy()) {
1042 Reg = MI.getOperand(1).getReg();
Chris Lattnera9a42252009-04-12 07:36:01 +00001043 continue;
Bill Wendling9a901322008-12-10 19:44:24 +00001044 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001045
Chris Lattnera9a42252009-04-12 07:36:01 +00001046 SetMI = &MI;
1047 break;
Bill Wendling30a64a72008-12-09 23:19:12 +00001048 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001049
Chris Lattnera9a42252009-04-12 07:36:01 +00001050 const TargetInstrDesc &TID = MI.getDesc();
Evan Chengc36b7062011-01-07 23:50:32 +00001051 if (TID.hasImplicitDefOfPhysReg(X86::EFLAGS) ||
1052 MI.hasUnmodeledSideEffects())
Chris Lattnera9a42252009-04-12 07:36:01 +00001053 break;
Bill Wendling9a901322008-12-10 19:44:24 +00001054 }
Chris Lattnera9a42252009-04-12 07:36:01 +00001055
1056 if (SetMI) {
1057 unsigned OpCode = SetMI->getOpcode();
1058
1059 if (OpCode == X86::SETOr || OpCode == X86::SETBr) {
Dan Gohman84023e02010-07-10 09:00:22 +00001060 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1061 TII.get(OpCode == X86::SETOr ? X86::JO_4 : X86::JB_4))
Chris Lattner8d57b772009-04-12 07:51:14 +00001062 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001063 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001064 FuncInfo.MBB->addSuccessor(TrueMBB);
Chris Lattnera9a42252009-04-12 07:36:01 +00001065 return true;
1066 }
Bill Wendling9a901322008-12-10 19:44:24 +00001067 }
Bill Wendling30a64a72008-12-09 23:19:12 +00001068 }
1069 }
Chris Lattner90cb88a2011-04-19 04:22:17 +00001070 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1071 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1072 // typically happen for _Bool and C++ bools.
1073 MVT SourceVT;
1074 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1075 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1076 unsigned TestOpc = 0;
1077 switch (SourceVT.SimpleTy) {
1078 default: break;
1079 case MVT::i8: TestOpc = X86::TEST8ri; break;
1080 case MVT::i16: TestOpc = X86::TEST16ri; break;
1081 case MVT::i32: TestOpc = X86::TEST32ri; break;
1082 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1083 }
1084 if (TestOpc) {
1085 unsigned OpReg = getRegForValue(TI->getOperand(0));
1086 if (OpReg == 0) return false;
1087 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TestOpc))
1088 .addReg(OpReg).addImm(1);
Chris Lattnerc76d1212011-04-19 04:26:32 +00001089
1090 unsigned JmpOpc = X86::JNE_4;
1091 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1092 std::swap(TrueMBB, FalseMBB);
1093 JmpOpc = X86::JE_4;
1094 }
1095
1096 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(JmpOpc))
Chris Lattner90cb88a2011-04-19 04:22:17 +00001097 .addMBB(TrueMBB);
1098 FastEmitBranch(FalseMBB, DL);
1099 FuncInfo.MBB->addSuccessor(TrueMBB);
1100 return true;
1101 }
1102 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001103 }
1104
1105 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman547eb4f2011-04-27 01:34:27 +00001106 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1107 // in an explicit cast, so make sure to handle that correctly.
Dan Gohmand98d6202008-10-02 22:15:21 +00001108 unsigned OpReg = getRegForValue(BI->getCondition());
1109 if (OpReg == 0) return false;
1110
Eli Friedman547eb4f2011-04-27 01:34:27 +00001111 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8ri))
1112 .addReg(OpReg).addImm(1);
Dan Gohman84023e02010-07-10 09:00:22 +00001113 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1114 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001115 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001116 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001117 return true;
1118}
1119
Dan Gohman46510a72010-04-15 01:51:59 +00001120bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner602fc062011-04-17 20:23:29 +00001121 unsigned CReg = 0, OpReg = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001122 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001123 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001124 CReg = X86::CL;
1125 RC = &X86::GR8RegClass;
1126 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001127 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1128 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1129 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001130 default: return false;
1131 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001132 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001133 CReg = X86::CX;
1134 RC = &X86::GR16RegClass;
1135 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001136 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1137 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1138 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001139 default: return false;
1140 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001141 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001142 CReg = X86::ECX;
1143 RC = &X86::GR32RegClass;
1144 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001145 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1146 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1147 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001148 default: return false;
1149 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001150 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001151 CReg = X86::RCX;
1152 RC = &X86::GR64RegClass;
1153 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001154 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1155 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1156 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001157 default: return false;
1158 }
1159 } else {
1160 return false;
1161 }
1162
Duncan Sands1440e8b2010-11-03 11:35:31 +00001163 MVT VT;
1164 if (!isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001165 return false;
1166
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001167 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1168 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001169
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001170 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1171 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001172 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1173 CReg).addReg(Op1Reg);
Dan Gohman145b8282008-10-07 21:50:36 +00001174
1175 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001176 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001177 if (CReg != X86::CL)
Dan Gohman84023e02010-07-10 09:00:22 +00001178 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1179 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001180 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001181
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001182 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001183 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1184 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001185 UpdateValueMap(I, ResultReg);
1186 return true;
1187}
1188
Dan Gohman46510a72010-04-15 01:51:59 +00001189bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001190 MVT VT;
1191 if (!isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001192 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001193
Eric Christophere487b012010-09-29 23:00:29 +00001194 // We only use cmov here, if we don't have a cmov instruction bail.
1195 if (!Subtarget->hasCMov()) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001196
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001197 unsigned Opc = 0;
1198 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001199 if (VT == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001200 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001201 RC = &X86::GR16RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001202 } else if (VT == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001203 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001204 RC = &X86::GR32RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001205 } else if (VT == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001206 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001207 RC = &X86::GR64RegClass;
1208 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001209 return false;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001210 }
1211
1212 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1213 if (Op0Reg == 0) return false;
1214 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1215 if (Op1Reg == 0) return false;
1216 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1217 if (Op2Reg == 0) return false;
1218
Dan Gohman84023e02010-07-10 09:00:22 +00001219 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1220 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001221 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001222 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1223 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001224 UpdateValueMap(I, ResultReg);
1225 return true;
1226}
1227
Dan Gohman46510a72010-04-15 01:51:59 +00001228bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001229 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001230 if (Subtarget->hasSSE2() &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001231 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001232 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001233 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001234 unsigned OpReg = getRegForValue(V);
1235 if (OpReg == 0) return false;
1236 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001237 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1238 TII.get(X86::CVTSS2SDrr), ResultReg)
1239 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001240 UpdateValueMap(I, ResultReg);
1241 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001242 }
1243 }
1244
1245 return false;
1246}
1247
Dan Gohman46510a72010-04-15 01:51:59 +00001248bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Dan Gohman78efce62008-09-10 21:02:08 +00001249 if (Subtarget->hasSSE2()) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001250 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001251 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001252 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001253 unsigned OpReg = getRegForValue(V);
1254 if (OpReg == 0) return false;
1255 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001256 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1257 TII.get(X86::CVTSD2SSrr), ResultReg)
1258 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001259 UpdateValueMap(I, ResultReg);
1260 return true;
1261 }
1262 }
1263 }
1264
1265 return false;
1266}
1267
Dan Gohman46510a72010-04-15 01:51:59 +00001268bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001269 if (Subtarget->is64Bit())
1270 // All other cases should be handled by the tblgen generated code.
1271 return false;
Owen Andersone50ed302009-08-10 22:56:29 +00001272 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1273 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001274
Chris Lattner44ceb8a2009-03-13 16:36:42 +00001275 // This code only handles truncation to byte right now.
Owen Anderson825b72b2009-08-11 20:47:22 +00001276 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001277 // All other cases should be handled by the tblgen generated code.
1278 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001279 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001280 // All other cases should be handled by the tblgen generated code.
1281 return false;
1282
1283 unsigned InputReg = getRegForValue(I->getOperand(0));
1284 if (!InputReg)
1285 // Unhandled operand. Halt "fast" selection and bail.
1286 return false;
1287
Dan Gohman62417622009-04-27 16:33:14 +00001288 // First issue a copy to GR16_ABCD or GR32_ABCD.
Owen Anderson825b72b2009-08-11 20:47:22 +00001289 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
Dan Gohman62417622009-04-27 16:33:14 +00001290 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001291 unsigned CopyReg = createResultReg(CopyRC);
Jakob Stoklund Olesen68818982010-07-14 23:58:21 +00001292 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1293 CopyReg).addReg(InputReg);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001294
1295 // Then issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001296 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001297 CopyReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001298 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001299 if (!ResultReg)
1300 return false;
1301
1302 UpdateValueMap(I, ResultReg);
1303 return true;
1304}
1305
Dan Gohman46510a72010-04-15 01:51:59 +00001306bool X86FastISel::X86SelectExtractValue(const Instruction *I) {
1307 const ExtractValueInst *EI = cast<ExtractValueInst>(I);
1308 const Value *Agg = EI->getAggregateOperand();
Bill Wendling52370a12008-12-09 02:42:50 +00001309
Dan Gohman46510a72010-04-15 01:51:59 +00001310 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Agg)) {
Chris Lattnera9a42252009-04-12 07:36:01 +00001311 switch (CI->getIntrinsicID()) {
1312 default: break;
1313 case Intrinsic::sadd_with_overflow:
Dan Gohman84023e02010-07-10 09:00:22 +00001314 case Intrinsic::uadd_with_overflow: {
Chris Lattnera9a42252009-04-12 07:36:01 +00001315 // Cheat a little. We know that the registers for "add" and "seto" are
1316 // allocated sequentially. However, we only keep track of the register
1317 // for "add" in the value map. Use extractvalue's index to get the
1318 // correct register for "seto".
Dan Gohman84023e02010-07-10 09:00:22 +00001319 unsigned OpReg = getRegForValue(Agg);
1320 if (OpReg == 0)
1321 return false;
1322 UpdateValueMap(I, OpReg + *EI->idx_begin());
Chris Lattnera9a42252009-04-12 07:36:01 +00001323 return true;
Bill Wendling52370a12008-12-09 02:42:50 +00001324 }
Dan Gohman84023e02010-07-10 09:00:22 +00001325 }
Bill Wendling52370a12008-12-09 02:42:50 +00001326 }
1327
1328 return false;
1329}
1330
Eli Friedmand5089a92011-04-27 01:45:07 +00001331bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1332 X86AddressMode SrcAM, uint64_t Len) {
1333 // Make sure we don't bloat code by inlining very large memcpy's.
1334 bool i64Legal = TLI.isTypeLegal(MVT::i64);
1335 if (Len > (i64Legal ? 32 : 16)) return false;
1336
1337 // We don't care about alignment here since we just emit integer accesses.
1338 while (Len) {
1339 MVT VT;
1340 if (Len >= 8 && i64Legal)
1341 VT = MVT::i64;
1342 else if (Len >= 4)
1343 VT = MVT::i32;
1344 else if (Len >= 2)
1345 VT = MVT::i16;
1346 else {
1347 assert(Len == 1);
1348 VT = MVT::i8;
1349 }
1350
1351 unsigned Reg;
1352 bool RV = X86FastEmitLoad(VT, SrcAM, Reg);
1353 RV &= X86FastEmitStore(VT, Reg, DestAM);
1354 assert(RV && "Failed to emit load or store??");
1355
1356 unsigned Size = VT.getSizeInBits()/8;
1357 Len -= Size;
1358 DestAM.Disp += Size;
1359 SrcAM.Disp += Size;
1360 }
1361
1362 return true;
1363}
1364
Dan Gohman46510a72010-04-15 01:51:59 +00001365bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001366 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001367 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001368 default: return false;
Chris Lattner832e4942011-04-19 05:52:03 +00001369 case Intrinsic::memcpy: {
1370 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1371 // Don't handle volatile or variable length memcpys.
1372 if (MCI.isVolatile() || !isa<ConstantInt>(MCI.getLength()))
1373 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001374
Chris Lattner832e4942011-04-19 05:52:03 +00001375 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
Chris Lattner832e4942011-04-19 05:52:03 +00001376
1377 // Get the address of the dest and source addresses.
1378 X86AddressMode DestAM, SrcAM;
1379 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1380 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1381 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001382
1383 return TryEmitSmallMemcpy(DestAM, SrcAM, Len);
Chris Lattner832e4942011-04-19 05:52:03 +00001384 }
1385
Eric Christopher07754c22010-03-18 20:27:26 +00001386 case Intrinsic::stackprotector: {
1387 // Emit code inline code to store the stack guard onto the stack.
1388 EVT PtrTy = TLI.getPointerTy();
1389
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001390 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1391 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001392
1393 // Grab the frame index.
1394 X86AddressMode AM;
1395 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher88dee302010-03-18 21:58:33 +00001396 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001397 return true;
1398 }
Eric Christopherf27805b2010-03-11 06:20:22 +00001399 case Intrinsic::objectsize: {
Chris Lattner832e4942011-04-19 05:52:03 +00001400 // FIXME: This should be moved to generic code!
1401 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
Eric Christopherf27805b2010-03-11 06:20:22 +00001402 const Type *Ty = I.getCalledFunction()->getReturnType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001403
Duncan Sands1440e8b2010-11-03 11:35:31 +00001404 MVT VT;
Eric Christopherf27805b2010-03-11 06:20:22 +00001405 if (!isTypeLegal(Ty, VT))
1406 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001407
Eric Christopherf27805b2010-03-11 06:20:22 +00001408 unsigned OpC = 0;
1409 if (VT == MVT::i32)
1410 OpC = X86::MOV32ri;
1411 else if (VT == MVT::i64)
1412 OpC = X86::MOV64ri;
1413 else
1414 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001415
Eric Christopherf27805b2010-03-11 06:20:22 +00001416 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +00001417 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg).
Dan Gohmane368b462010-06-18 14:22:04 +00001418 addImm(CI->isZero() ? -1ULL : 0);
Eric Christopherf27805b2010-03-11 06:20:22 +00001419 UpdateValueMap(&I, ResultReg);
1420 return true;
1421 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001422 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001423 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001424 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001425 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001426 if (!X86SelectAddress(DI->getAddress(), AM))
1427 return false;
Chris Lattner518bb532010-02-09 19:54:29 +00001428 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001429 // FIXME may need to add RegState::Debug to any registers produced,
1430 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001431 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1432 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001433 return true;
1434 }
Eric Christopher77f79892010-01-18 22:11:29 +00001435 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001436 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001437 return true;
1438 }
Bill Wendling52370a12008-12-09 02:42:50 +00001439 case Intrinsic::sadd_with_overflow:
1440 case Intrinsic::uadd_with_overflow: {
Chris Lattner832e4942011-04-19 05:52:03 +00001441 // FIXME: Should fold immediates.
1442
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001443 // Replace "add with overflow" intrinsics with an "add" instruction followed
1444 // by a seto/setc instruction. Later on, when the "extractvalue"
1445 // instructions are encountered, we use the fact that two registers were
1446 // created sequentially to get the correct registers for the "sum" and the
1447 // "overflow bit".
Bill Wendling52370a12008-12-09 02:42:50 +00001448 const Function *Callee = I.getCalledFunction();
1449 const Type *RetTy =
1450 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1451
Duncan Sands1440e8b2010-11-03 11:35:31 +00001452 MVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001453 if (!isTypeLegal(RetTy, VT))
1454 return false;
1455
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001456 const Value *Op1 = I.getArgOperand(0);
1457 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001458 unsigned Reg1 = getRegForValue(Op1);
1459 unsigned Reg2 = getRegForValue(Op2);
1460
1461 if (Reg1 == 0 || Reg2 == 0)
1462 // FIXME: Handle values *not* in registers.
1463 return false;
1464
1465 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001466 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001467 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001468 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001469 OpC = X86::ADD64rr;
1470 else
1471 return false;
1472
1473 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +00001474 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1475 .addReg(Reg1).addReg(Reg2);
Chris Lattner8d57b772009-04-12 07:51:14 +00001476 unsigned DestReg1 = UpdateValueMap(&I, ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001477
Chris Lattner8d57b772009-04-12 07:51:14 +00001478 // If the add with overflow is an intra-block value then we just want to
1479 // create temporaries for it like normal. If it is a cross-block value then
1480 // UpdateValueMap will return the cross-block register used. Since we
1481 // *really* want the value to be live in the register pair known by
1482 // UpdateValueMap, we have to use DestReg1+1 as the destination register in
1483 // the cross block case. In the non-cross-block case, we should just make
1484 // another register for the value.
1485 if (DestReg1 != ResultReg)
1486 ResultReg = DestReg1+1;
1487 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001488 ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001489
Chris Lattnera9a42252009-04-12 07:36:01 +00001490 unsigned Opc = X86::SETBr;
1491 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1492 Opc = X86::SETOr;
Dan Gohman84023e02010-07-10 09:00:22 +00001493 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
Bill Wendling52370a12008-12-09 02:42:50 +00001494 return true;
1495 }
1496 }
1497}
1498
Dan Gohman46510a72010-04-15 01:51:59 +00001499bool X86FastISel::X86SelectCall(const Instruction *I) {
1500 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001501 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001502
1503 // Can't handle inline asm yet.
1504 if (isa<InlineAsm>(Callee))
1505 return false;
1506
Bill Wendling52370a12008-12-09 02:42:50 +00001507 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001508 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001509 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001510
Evan Chengf3d4efe2008-09-07 09:09:33 +00001511 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001512 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001513 CallingConv::ID CC = CS.getCallingConv();
Chris Lattnere03b8d32011-04-19 04:42:38 +00001514 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Evan Chengf3d4efe2008-09-07 09:09:33 +00001515 CC != CallingConv::X86_FastCall)
1516 return false;
1517
Evan Cheng381993f2010-01-27 00:00:57 +00001518 // fastcc with -tailcallopt is intended to provide a guaranteed
1519 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman1797ed52010-02-08 20:27:50 +00001520 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001521 return false;
1522
Evan Chengf3d4efe2008-09-07 09:09:33 +00001523 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1524 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedman37620462011-04-19 17:22:22 +00001525 bool isVarArg = FTy->isVarArg();
1526
1527 // Don't know how to handle Win64 varargs yet. Nothing special needed for
1528 // x86-32. Special handling for x86-64 is implemented.
1529 if (isVarArg && Subtarget->isTargetWin64())
Evan Chengf3d4efe2008-09-07 09:09:33 +00001530 return false;
1531
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001532 // Fast-isel doesn't know about callee-pop yet.
Eli Friedman37620462011-04-19 17:22:22 +00001533 if (Subtarget->IsCalleePop(isVarArg, CC))
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001534 return false;
1535
Evan Chengf3d4efe2008-09-07 09:09:33 +00001536 // Handle *simple* calls for now.
1537 const Type *RetTy = CS.getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001538 MVT RetVT;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001539 if (RetTy->isVoidTy())
Owen Anderson825b72b2009-08-11 20:47:22 +00001540 RetVT = MVT::isVoid;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001541 else if (!isTypeLegal(RetTy, RetVT, true))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001542 return false;
1543
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001544 // Materialize callee address in a register. FIXME: GV address can be
1545 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001546 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001547 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001548 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001549 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001550 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001551 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001552 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001553 } else if (CalleeAM.Base.Reg != 0) {
1554 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001555 } else
1556 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001557
Evan Chengdebdea02008-09-08 17:15:42 +00001558 // Allow calls which produce i1 results.
1559 bool AndToI1 = false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001560 if (RetVT == MVT::i1) {
1561 RetVT = MVT::i8;
Evan Chengdebdea02008-09-08 17:15:42 +00001562 AndToI1 = true;
1563 }
1564
Evan Chengf3d4efe2008-09-07 09:09:33 +00001565 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001566 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001567 SmallVector<unsigned, 8> Args;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001568 SmallVector<MVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001569 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001570 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001571 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001572 ArgVTs.reserve(CS.arg_size());
1573 ArgFlags.reserve(CS.arg_size());
Dan Gohman46510a72010-04-15 01:51:59 +00001574 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001575 i != e; ++i) {
Chris Lattnere03b8d32011-04-19 04:42:38 +00001576 Value *ArgVal = *i;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001577 ISD::ArgFlagsTy Flags;
1578 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001579 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001580 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001581 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001582 Flags.setZExt();
1583
Chris Lattnere03b8d32011-04-19 04:42:38 +00001584 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1585 // instruction. This is safe because it is common to all fastisel supported
1586 // calling conventions on x86.
1587 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1588 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1589 CI->getBitWidth() == 16) {
1590 if (Flags.isSExt())
1591 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1592 else
1593 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1594 }
1595 }
1596
Chris Lattnerb44101c2011-04-19 05:09:50 +00001597 unsigned ArgReg;
Chris Lattnerff009ad2011-04-19 05:15:59 +00001598
1599 // Passing bools around ends up doing a trunc to i1 and passing it.
1600 // Codegen this as an argument + "and 1".
Chris Lattnerb44101c2011-04-19 05:09:50 +00001601 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
1602 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
1603 ArgVal->hasOneUse()) {
Chris Lattnerb44101c2011-04-19 05:09:50 +00001604 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
1605 ArgReg = getRegForValue(ArgVal);
1606 if (ArgReg == 0) return false;
1607
1608 MVT ArgVT;
1609 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
1610
1611 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
1612 ArgVal->hasOneUse(), 1);
1613 } else {
1614 ArgReg = getRegForValue(ArgVal);
Chris Lattnerb44101c2011-04-19 05:09:50 +00001615 }
Chris Lattnere03b8d32011-04-19 04:42:38 +00001616
Chris Lattnerff009ad2011-04-19 05:15:59 +00001617 if (ArgReg == 0) return false;
1618
Evan Chengf3d4efe2008-09-07 09:09:33 +00001619 // FIXME: Only handle *easy* calls for now.
Devang Patel05988662008-09-25 21:00:45 +00001620 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
Devang Patel05988662008-09-25 21:00:45 +00001621 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1622 CS.paramHasAttr(AttrInd, Attribute::ByVal))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001623 return false;
1624
Chris Lattnere03b8d32011-04-19 04:42:38 +00001625 const Type *ArgTy = ArgVal->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001626 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001627 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001628 return false;
1629 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1630 Flags.setOrigAlign(OriginalAlignment);
1631
Chris Lattnerb44101c2011-04-19 05:09:50 +00001632 Args.push_back(ArgReg);
Chris Lattnere03b8d32011-04-19 04:42:38 +00001633 ArgVals.push_back(ArgVal);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001634 ArgVTs.push_back(ArgVT);
1635 ArgFlags.push_back(Flags);
1636 }
1637
1638 // Analyze operands of the call, assigning locations to each operand.
1639 SmallVector<CCValAssign, 16> ArgLocs;
Eli Friedman37620462011-04-19 17:22:22 +00001640 CCState CCInfo(CC, isVarArg, TM, ArgLocs, I->getParent()->getContext());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001641
Dan Gohmand8acddd2010-06-01 21:09:47 +00001642 // Allocate shadow area for Win64
Chris Lattnere03b8d32011-04-19 04:42:38 +00001643 if (Subtarget->isTargetWin64())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001644 CCInfo.AllocateStack(32, 8);
Dan Gohmand8acddd2010-06-01 21:09:47 +00001645
Duncan Sands45907662010-10-31 13:21:44 +00001646 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001647
1648 // Get a count of how many bytes are to be pushed on the stack.
1649 unsigned NumBytes = CCInfo.getNextStackOffset();
1650
1651 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001652 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001653 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1654 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001655
Chris Lattner438949a2008-10-15 05:30:52 +00001656 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001657 // copies / loads.
1658 SmallVector<unsigned, 4> RegArgs;
1659 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1660 CCValAssign &VA = ArgLocs[i];
1661 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001662 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001663
Evan Chengf3d4efe2008-09-07 09:09:33 +00001664 // Promote the value if needed.
1665 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001666 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001667 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001668 case CCValAssign::SExt: {
1669 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1670 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001671 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001672 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001673 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001674 }
1675 case CCValAssign::ZExt: {
1676 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1677 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001678 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001679 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001680 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001681 }
1682 case CCValAssign::AExt: {
Dale Johannesena8bd1ff2010-09-27 17:29:47 +00001683 // We don't handle MMX parameters yet.
1684 if (VA.getLocVT().isVector() && VA.getLocVT().getSizeInBits() == 128)
1685 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +00001686 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1687 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001688 if (!Emitted)
1689 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001690 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001691 if (!Emitted)
1692 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1693 Arg, ArgVT, Arg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001694
Chris Lattnerc46ec642011-01-05 22:26:52 +00001695 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001696 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001697 break;
1698 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001699 case CCValAssign::BCvt: {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001700 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001701 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001702 assert(BC != 0 && "Failed to emit a bitcast!");
1703 Arg = BC;
1704 ArgVT = VA.getLocVT();
1705 break;
1706 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001707 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001708
Evan Chengf3d4efe2008-09-07 09:09:33 +00001709 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001710 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1711 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001712 RegArgs.push_back(VA.getLocReg());
1713 } else {
1714 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001715 X86AddressMode AM;
1716 AM.Base.Reg = StackPtr;
1717 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001718 const Value *ArgVal = ArgVals[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001719
Chris Lattner241ab472008-10-15 05:38:32 +00001720 // If this is a really simple value, emit this with the Value* version of
1721 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1722 // can cause us to reevaluate the argument.
1723 if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
1724 X86FastEmitStore(ArgVT, ArgVal, AM);
1725 else
1726 X86FastEmitStore(ArgVT, Arg, AM);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001727 }
1728 }
1729
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001730 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001731 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001732 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001733 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001734 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1735 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001736 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001737
Eli Friedman37620462011-04-19 17:22:22 +00001738 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64()) {
1739 // Count the number of XMM registers allocated.
1740 static const unsigned XMMArgRegs[] = {
1741 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1742 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1743 };
1744 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1745 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::MOV8ri),
1746 X86::AL).addImm(NumXMMRegs);
1747 }
1748
Evan Chengf3d4efe2008-09-07 09:09:33 +00001749 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001750 MachineInstrBuilder MIB;
1751 if (CalleeOp) {
1752 // Register-indirect call.
Nate Begeman0c07b642010-07-22 00:09:39 +00001753 unsigned CallOpc;
1754 if (Subtarget->isTargetWin64())
1755 CallOpc = X86::WINCALL64r;
1756 else if (Subtarget->is64Bit())
1757 CallOpc = X86::CALL64r;
1758 else
1759 CallOpc = X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00001760 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1761 .addReg(CalleeOp);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001762
Chris Lattner51e8eab2009-07-09 06:34:26 +00001763 } else {
1764 // Direct call.
1765 assert(GV && "Not a direct call");
Nate Begeman0c07b642010-07-22 00:09:39 +00001766 unsigned CallOpc;
1767 if (Subtarget->isTargetWin64())
1768 CallOpc = X86::WINCALL64pcrel32;
1769 else if (Subtarget->is64Bit())
1770 CallOpc = X86::CALL64pcrel32;
1771 else
1772 CallOpc = X86::CALLpcrel32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001773
Chris Lattner51e8eab2009-07-09 06:34:26 +00001774 // See if we need any target-specific flags on the GV operand.
1775 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001776
Chris Lattner51e8eab2009-07-09 06:34:26 +00001777 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1778 // external symbols most go through the PLT in PIC mode. If the symbol
1779 // has hidden or protected visibility, or if it is static or local, then
1780 // we don't need to use the PLT - we can directly call it.
1781 if (Subtarget->isTargetELF() &&
1782 TM.getRelocationModel() == Reloc::PIC_ &&
1783 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1784 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001785 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001786 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00001787 (!Subtarget->getTargetTriple().isMacOSX() ||
1788 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner51e8eab2009-07-09 06:34:26 +00001789 // PC-relative references to external symbols should go through $stub,
1790 // unless we're building with the leopard linker or later, which
1791 // automatically synthesizes these stubs.
1792 OpFlags = X86II::MO_DARWIN_STUB;
1793 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001794
1795
Dan Gohman84023e02010-07-10 09:00:22 +00001796 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1797 .addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001798 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001799
1800 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001801 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001802 MIB.addReg(X86::EBX);
1803
Eli Friedman37620462011-04-19 17:22:22 +00001804 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64())
1805 MIB.addReg(X86::AL);
1806
Evan Chengf3d4efe2008-09-07 09:09:33 +00001807 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001808 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1809 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001810
1811 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001812 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Eli Friedmand227eed2011-04-28 20:19:12 +00001813 unsigned NumBytesCallee = 0;
1814 if (!Subtarget->is64Bit() && CS.paramHasAttr(1, Attribute::StructRet))
1815 NumBytesCallee = 4;
Dan Gohman84023e02010-07-10 09:00:22 +00001816 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
Eli Friedmand227eed2011-04-28 20:19:12 +00001817 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001818
1819 // Now handle call return value (if any).
Dan Gohmandb497122010-06-18 23:28:01 +00001820 SmallVector<unsigned, 4> UsedRegs;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001821 if (RetVT != MVT::isVoid) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001822 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +00001823 CCState CCInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001824 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1825
1826 // Copy all of the result registers out of their specified physreg.
1827 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
Owen Andersone50ed302009-08-10 22:56:29 +00001828 EVT CopyVT = RVLocs[0].getValVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001829 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001830
Evan Chengf3d4efe2008-09-07 09:09:33 +00001831 // If this is a call to a function that returns an fp value on the x87 fp
1832 // stack, but where we prefer to use the value in xmm registers, copy it
1833 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1834 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1835 RVLocs[0].getLocReg() == X86::ST1) &&
1836 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001837 CopyVT = MVT::f80;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001838 DstRC = X86::RFP80RegisterClass;
1839 }
1840
1841 unsigned ResultReg = createResultReg(DstRC);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001842 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1843 ResultReg).addReg(RVLocs[0].getLocReg());
Dan Gohmandb497122010-06-18 23:28:01 +00001844 UsedRegs.push_back(RVLocs[0].getLocReg());
1845
Evan Chengf3d4efe2008-09-07 09:09:33 +00001846 if (CopyVT != RVLocs[0].getValVT()) {
1847 // Round the F80 the right size, which also moves to the appropriate xmm
1848 // register. This is accomplished by storing the F80 value in memory and
1849 // then loading it back. Ewww...
Owen Andersone50ed302009-08-10 22:56:29 +00001850 EVT ResVT = RVLocs[0].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001851 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001852 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001853 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00001854 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1855 TII.get(Opc)), FI)
1856 .addReg(ResultReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001857 DstRC = ResVT == MVT::f32
Evan Chengf3d4efe2008-09-07 09:09:33 +00001858 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001859 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001860 ResultReg = createResultReg(DstRC);
Dan Gohman84023e02010-07-10 09:00:22 +00001861 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1862 TII.get(Opc), ResultReg), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001863 }
1864
Evan Chengdebdea02008-09-08 17:15:42 +00001865 if (AndToI1) {
1866 // Mask out all but lowest bit for some call which produces an i1.
1867 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001868 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001869 TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
Evan Chengdebdea02008-09-08 17:15:42 +00001870 ResultReg = AndResult;
1871 }
1872
Evan Chengf3d4efe2008-09-07 09:09:33 +00001873 UpdateValueMap(I, ResultReg);
1874 }
1875
Dan Gohmandb497122010-06-18 23:28:01 +00001876 // Set all unused physreg defs as dead.
1877 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1878
Evan Chengf3d4efe2008-09-07 09:09:33 +00001879 return true;
1880}
1881
1882
Dan Gohman99b21822008-08-28 23:21:34 +00001883bool
Dan Gohman46510a72010-04-15 01:51:59 +00001884X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001885 switch (I->getOpcode()) {
1886 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001887 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001888 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001889 case Instruction::Store:
1890 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00001891 case Instruction::Ret:
1892 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001893 case Instruction::ICmp:
1894 case Instruction::FCmp:
1895 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001896 case Instruction::ZExt:
1897 return X86SelectZExt(I);
1898 case Instruction::Br:
1899 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001900 case Instruction::Call:
1901 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001902 case Instruction::LShr:
1903 case Instruction::AShr:
1904 case Instruction::Shl:
1905 return X86SelectShift(I);
1906 case Instruction::Select:
1907 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001908 case Instruction::Trunc:
1909 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001910 case Instruction::FPExt:
1911 return X86SelectFPExt(I);
1912 case Instruction::FPTrunc:
1913 return X86SelectFPTrunc(I);
Bill Wendling52370a12008-12-09 02:42:50 +00001914 case Instruction::ExtractValue:
1915 return X86SelectExtractValue(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001916 case Instruction::IntToPtr: // Deliberate fall-through.
1917 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001918 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1919 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001920 if (DstVT.bitsGT(SrcVT))
1921 return X86SelectZExt(I);
1922 if (DstVT.bitsLT(SrcVT))
1923 return X86SelectTrunc(I);
1924 unsigned Reg = getRegForValue(I->getOperand(0));
1925 if (Reg == 0) return false;
1926 UpdateValueMap(I, Reg);
1927 return true;
1928 }
Dan Gohman99b21822008-08-28 23:21:34 +00001929 }
1930
1931 return false;
1932}
1933
Dan Gohman46510a72010-04-15 01:51:59 +00001934unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001935 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001936 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001937 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001938
Owen Anderson95267a12008-09-05 00:06:23 +00001939 // Get opcode and regclass of the output for the given load instruction.
1940 unsigned Opc = 0;
1941 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001942 switch (VT.SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001943 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001944 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001945 Opc = X86::MOV8rm;
1946 RC = X86::GR8RegisterClass;
1947 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001948 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001949 Opc = X86::MOV16rm;
1950 RC = X86::GR16RegisterClass;
1951 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001952 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001953 Opc = X86::MOV32rm;
1954 RC = X86::GR32RegisterClass;
1955 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001956 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001957 // Must be in x86-64 mode.
1958 Opc = X86::MOV64rm;
1959 RC = X86::GR64RegisterClass;
1960 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001961 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001962 if (Subtarget->hasSSE1()) {
1963 Opc = X86::MOVSSrm;
1964 RC = X86::FR32RegisterClass;
1965 } else {
1966 Opc = X86::LD_Fp32m;
1967 RC = X86::RFP32RegisterClass;
1968 }
1969 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001970 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001971 if (Subtarget->hasSSE2()) {
1972 Opc = X86::MOVSDrm;
1973 RC = X86::FR64RegisterClass;
1974 } else {
1975 Opc = X86::LD_Fp64m;
1976 RC = X86::RFP64RegisterClass;
1977 }
1978 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001979 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001980 // No f80 support yet.
1981 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001982 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001983
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001984 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001985 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001986 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001987 if (X86SelectAddress(C, AM)) {
Chris Lattner685090f2011-04-17 17:12:08 +00001988 // If the expression is just a basereg, then we're done, otherwise we need
1989 // to emit an LEA.
1990 if (AM.BaseType == X86AddressMode::RegBase &&
1991 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
1992 return AM.Base.Reg;
1993
1994 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001995 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001996 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1997 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001998 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001999 }
Evan Cheng0de588f2008-09-05 21:00:03 +00002000 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00002001 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002002
Owen Anderson3b217c62008-09-06 01:11:01 +00002003 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00002004 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002005 if (Align == 0) {
2006 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00002007 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002008 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002009
Dan Gohman5396c992008-09-30 01:21:32 +00002010 // x86-32 PIC requires a PIC base register for constant pools.
2011 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00002012 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00002013 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00002014 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00002015 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002016 } else if (Subtarget->isPICStyleGOT()) {
2017 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00002018 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002019 } else if (Subtarget->isPICStyleRIPRel() &&
2020 TM.getCodeModel() == CodeModel::Small) {
2021 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00002022 }
Dan Gohman5396c992008-09-30 01:21:32 +00002023
2024 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00002025 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002026 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002027 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2028 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00002029 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00002030
Owen Anderson95267a12008-09-05 00:06:23 +00002031 return ResultReg;
2032}
2033
Dan Gohman46510a72010-04-15 01:51:59 +00002034unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002035 // Fail on dynamic allocas. At this point, getRegForValue has already
2036 // checked its CSE maps, so if we're here trying to handle a dynamic
2037 // alloca, we're not going to succeed. X86SelectAddress has a
2038 // check for dynamic allocas, because it's called directly from
2039 // various places, but TargetMaterializeAlloca also needs a check
2040 // in order to avoid recursion between getRegForValue,
2041 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00002042 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002043 return 0;
2044
Dan Gohman0586d912008-09-10 20:11:02 +00002045 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002046 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00002047 return 0;
2048 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
2049 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
2050 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002051 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2052 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00002053 return ResultReg;
2054}
2055
Eli Friedman2790ba82011-04-27 22:41:55 +00002056unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2057 MVT VT;
2058 if (!isTypeLegal(CF->getType(), VT))
2059 return false;
2060
2061 // Get opcode and regclass for the given zero.
2062 unsigned Opc = 0;
2063 const TargetRegisterClass *RC = NULL;
2064 switch (VT.SimpleTy) {
2065 default: return false;
2066 case MVT::f32:
2067 if (Subtarget->hasSSE1()) {
2068 Opc = X86::FsFLD0SS;
2069 RC = X86::FR32RegisterClass;
2070 } else {
2071 Opc = X86::LD_Fp032;
2072 RC = X86::RFP32RegisterClass;
2073 }
2074 break;
2075 case MVT::f64:
2076 if (Subtarget->hasSSE2()) {
2077 Opc = X86::FsFLD0SD;
2078 RC = X86::FR64RegisterClass;
2079 } else {
2080 Opc = X86::LD_Fp064;
2081 RC = X86::RFP64RegisterClass;
2082 }
2083 break;
2084 case MVT::f80:
2085 // No f80 support yet.
2086 return false;
2087 }
2088
2089 unsigned ResultReg = createResultReg(RC);
2090 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
2091 return ResultReg;
2092}
2093
2094
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002095/// TryToFoldLoad - The specified machine instr operand is a vreg, and that
2096/// vreg is being provided by the specified load instruction. If possible,
2097/// try to fold the load as an operand to the instruction, returning true if
2098/// possible.
2099bool X86FastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
2100 const LoadInst *LI) {
2101 X86AddressMode AM;
2102 if (!X86SelectAddress(LI->getOperand(0), AM))
2103 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002104
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002105 X86InstrInfo &XII = (X86InstrInfo&)TII;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002106
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002107 unsigned Size = TD.getTypeAllocSize(LI->getType());
2108 unsigned Alignment = LI->getAlignment();
2109
2110 SmallVector<MachineOperand, 8> AddrOps;
2111 AM.getFullAddress(AddrOps);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002112
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002113 MachineInstr *Result =
2114 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
2115 if (Result == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002116
Chris Lattnerb99fdee2011-01-16 02:27:38 +00002117 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002118 MI->eraseFromParent();
2119 return true;
2120}
2121
2122
Evan Chengc3f44b02008-09-03 00:03:49 +00002123namespace llvm {
Dan Gohmana4160c32010-07-07 16:29:44 +00002124 llvm::FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo) {
2125 return new X86FastISel(funcInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00002126 }
Dan Gohman99b21822008-08-28 23:21:34 +00002127}