blob: aafa96389acb6ec1cd40100972e3a13abbd91856 [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 Chengef41ff62011-06-23 17:54:54 +000018#include "X86ISelLowering.h"
Evan Cheng88e30412008-09-03 01:04:47 +000019#include "X86RegisterInfo.h"
20#include "X86Subtarget.h"
Dan Gohman22bb3112008-08-22 00:20:26 +000021#include "X86TargetMachine.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000022#include "llvm/CallingConv.h"
Dan Gohman6e3f05f2008-09-04 23:26:51 +000023#include "llvm/DerivedTypes.h"
Dan Gohmane9865942009-02-23 22:03:08 +000024#include "llvm/GlobalVariable.h"
Eli Friedmana6176ad2011-09-22 23:41:28 +000025#include "llvm/GlobalAlias.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000026#include "llvm/Instructions.h"
Chris Lattnera9a42252009-04-12 07:36:01 +000027#include "llvm/IntrinsicInst.h"
Jay Foad562b84b2011-04-11 09:35:34 +000028#include "llvm/Operator.h"
Dan Gohman84023e02010-07-10 09:00:22 +000029#include "llvm/CodeGen/Analysis.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000030#include "llvm/CodeGen/FastISel.h"
Dan Gohmana4160c32010-07-07 16:29:44 +000031#include "llvm/CodeGen/FunctionLoweringInfo.h"
Owen Anderson95267a12008-09-05 00:06:23 +000032#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000033#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000035#include "llvm/Support/CallSite.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000036#include "llvm/Support/ErrorHandling.h"
Dan Gohman35893082008-09-18 23:23:44 +000037#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Cheng381993f2010-01-27 00:00:57 +000038#include "llvm/Target/TargetOptions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000039using namespace llvm;
40
Chris Lattner087fcf32009-03-08 18:44:31 +000041namespace {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000042
Evan Chengc3f44b02008-09-03 00:03:49 +000043class X86FastISel : public FastISel {
44 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
45 /// make the right decision when generating code for different targets.
46 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000047
48 /// StackPtr - Register used as the stack pointer.
49 ///
50 unsigned StackPtr;
51
Wesley Peckbf17cfa2010-11-23 03:31:01 +000052 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
Evan Chengf3d4efe2008-09-07 09:09:33 +000053 /// floating point ops.
54 /// When SSE is available, use it for f32 operations.
55 /// When SSE2 is available, use it for f64 operations.
56 bool X86ScalarSSEf64;
57 bool X86ScalarSSEf32;
58
Evan Cheng8b19e562008-09-03 06:44:39 +000059public:
Dan Gohmana4160c32010-07-07 16:29:44 +000060 explicit X86FastISel(FunctionLoweringInfo &funcInfo) : FastISel(funcInfo) {
Evan Cheng88e30412008-09-03 01:04:47 +000061 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000062 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Bruno Cardoso Lopesaed890b2011-08-01 21:54:05 +000063 X86ScalarSSEf64 = Subtarget->hasSSE2() || Subtarget->hasAVX();
64 X86ScalarSSEf32 = Subtarget->hasSSE1() || Subtarget->hasAVX();
Evan Cheng88e30412008-09-03 01:04:47 +000065 }
Evan Chengc3f44b02008-09-03 00:03:49 +000066
Dan Gohman46510a72010-04-15 01:51:59 +000067 virtual bool TargetSelectInstruction(const Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000068
Chris Lattnerbeac75d2010-09-05 02:18:34 +000069 /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
70 /// vreg is being provided by the specified load instruction. If possible,
71 /// try to fold the load as an operand to the instruction, returning true if
72 /// possible.
73 virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
74 const LoadInst *LI);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000075
Dan Gohman1adf1b02008-08-19 21:45:35 +000076#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000077
78private:
Dan Gohman46510a72010-04-15 01:51:59 +000079 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000080
Owen Andersone50ed302009-08-10 22:56:29 +000081 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000082
Chris Lattnerb44101c2011-04-19 05:09:50 +000083 bool X86FastEmitStore(EVT VT, const Value *Val, const X86AddressMode &AM);
84 bool X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000085
Owen Andersone50ed302009-08-10 22:56:29 +000086 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +000087 unsigned &ResultReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000088
Dan Gohman46510a72010-04-15 01:51:59 +000089 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
90 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman0586d912008-09-10 20:11:02 +000091
Dan Gohman46510a72010-04-15 01:51:59 +000092 bool X86SelectLoad(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000093
Dan Gohman46510a72010-04-15 01:51:59 +000094 bool X86SelectStore(const Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000095
Dan Gohman84023e02010-07-10 09:00:22 +000096 bool X86SelectRet(const Instruction *I);
97
Dan Gohman46510a72010-04-15 01:51:59 +000098 bool X86SelectCmp(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000099
Dan Gohman46510a72010-04-15 01:51:59 +0000100 bool X86SelectZExt(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000101
Dan Gohman46510a72010-04-15 01:51:59 +0000102 bool X86SelectBranch(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000103
Dan Gohman46510a72010-04-15 01:51:59 +0000104 bool X86SelectShift(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000105
Dan Gohman46510a72010-04-15 01:51:59 +0000106 bool X86SelectSelect(const Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000107
Dan Gohman46510a72010-04-15 01:51:59 +0000108 bool X86SelectTrunc(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000109
Dan Gohman46510a72010-04-15 01:51:59 +0000110 bool X86SelectFPExt(const Instruction *I);
111 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohman78efce62008-09-10 21:02:08 +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
Eli Friedman25255cb2011-06-10 23:39:36 +0000116 bool DoSelectCall(const Instruction *I, const char *MemIntName);
117
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000118 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000119 return getTargetMachine()->getInstrInfo();
120 }
121 const X86TargetMachine *getTargetMachine() const {
122 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000123 }
124
Dan Gohman46510a72010-04-15 01:51:59 +0000125 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000126
Dan Gohman46510a72010-04-15 01:51:59 +0000127 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000128
Eli Friedman2790ba82011-04-27 22:41:55 +0000129 unsigned TargetMaterializeFloatZero(const ConstantFP *CF);
130
Evan Chengf3d4efe2008-09-07 09:09:33 +0000131 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
132 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000133 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
135 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000136 }
137
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000138 bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
Eli Friedmand5089a92011-04-27 01:45:07 +0000139
Eli Friedmanc0883452011-05-20 22:21:04 +0000140 bool IsMemcpySmall(uint64_t Len);
141
Eli Friedmand5089a92011-04-27 01:45:07 +0000142 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
143 X86AddressMode SrcAM, uint64_t Len);
Evan Chengc3f44b02008-09-03 00:03:49 +0000144};
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000145
Chris Lattner087fcf32009-03-08 18:44:31 +0000146} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000147
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000148bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000149 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
150 if (evt == MVT::Other || !evt.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000151 // Unhandled type. Halt "fast" selection and bail.
152 return false;
Duncan Sands1440e8b2010-11-03 11:35:31 +0000153
154 VT = evt.getSimpleVT();
Dan Gohman9b66d732008-09-30 00:48:39 +0000155 // For now, require SSE/SSE2 for performing floating-point operations,
156 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000157 if (VT == MVT::f64 && !X86ScalarSSEf64)
Dan Gohman9b66d732008-09-30 00:48:39 +0000158 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 if (VT == MVT::f32 && !X86ScalarSSEf32)
Dan Gohman9b66d732008-09-30 00:48:39 +0000160 return false;
161 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000162 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000163 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000164 // We only handle legal types. For example, on x86-32 the instruction
165 // selector contains all of the 64-bit instructions from x86-64,
166 // under the assumption that i64 won't be used if the target doesn't
167 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000168 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000169}
170
171#include "X86GenCallingConv.inc"
172
Evan Cheng0de588f2008-09-05 21:00:03 +0000173/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000174/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000175/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000176bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000177 unsigned &ResultReg) {
178 // Get opcode and regclass of the output for the given load instruction.
179 unsigned Opc = 0;
180 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000181 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000182 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000183 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000184 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000185 Opc = X86::MOV8rm;
186 RC = X86::GR8RegisterClass;
187 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000188 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000189 Opc = X86::MOV16rm;
190 RC = X86::GR16RegisterClass;
191 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000193 Opc = X86::MOV32rm;
194 RC = X86::GR32RegisterClass;
195 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000197 // Must be in x86-64 mode.
198 Opc = X86::MOV64rm;
199 RC = X86::GR64RegisterClass;
200 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000202 if (X86ScalarSSEf32) {
203 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Evan Cheng0de588f2008-09-05 21:00:03 +0000204 RC = X86::FR32RegisterClass;
205 } else {
206 Opc = X86::LD_Fp32m;
207 RC = X86::RFP32RegisterClass;
208 }
209 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000211 if (X86ScalarSSEf64) {
212 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Evan Cheng0de588f2008-09-05 21:00:03 +0000213 RC = X86::FR64RegisterClass;
214 } else {
215 Opc = X86::LD_Fp64m;
216 RC = X86::RFP64RegisterClass;
217 }
218 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000219 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000220 // No f80 support yet.
221 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000222 }
223
224 ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000225 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
226 DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000227 return true;
228}
229
Evan Chengf3d4efe2008-09-07 09:09:33 +0000230/// X86FastEmitStore - Emit a machine instruction to store a value Val of
231/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
232/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000233/// i.e. V. Return true if it is possible.
234bool
Chris Lattnerb44101c2011-04-19 05:09:50 +0000235X86FastISel::X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000236 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000237 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 switch (VT.getSimpleVT().SimpleTy) {
239 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000240 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000241 case MVT::i1: {
242 // Mask out all but lowest bit.
243 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000244 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000245 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
246 Val = AndResult;
247 }
248 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 case MVT::i8: Opc = X86::MOV8mr; break;
250 case MVT::i16: Opc = X86::MOV16mr; break;
251 case MVT::i32: Opc = X86::MOV32mr; break;
252 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
253 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000254 Opc = X86ScalarSSEf32 ?
255 (Subtarget->hasAVX() ? X86::VMOVSSmr : X86::MOVSSmr) : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000256 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000258 Opc = X86ScalarSSEf64 ?
259 (Subtarget->hasAVX() ? X86::VMOVSDmr : X86::MOVSDmr) : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000260 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000261 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000262
Dan Gohman84023e02010-07-10 09:00:22 +0000263 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
264 DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000265 return true;
266}
267
Dan Gohman46510a72010-04-15 01:51:59 +0000268bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000269 const X86AddressMode &AM) {
270 // Handle 'null' like i32/i64 0.
271 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000272 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000273
Chris Lattner438949a2008-10-15 05:30:52 +0000274 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000275 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000276 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000277 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000279 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000280 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 case MVT::i8: Opc = X86::MOV8mi; break;
282 case MVT::i16: Opc = X86::MOV16mi; break;
283 case MVT::i32: Opc = X86::MOV32mi; break;
284 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000285 // Must be a 32-bit sign extended value.
286 if ((int)CI->getSExtValue() == CI->getSExtValue())
287 Opc = X86::MOV64mi32;
288 break;
289 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000290
Chris Lattner438949a2008-10-15 05:30:52 +0000291 if (Opc) {
Dan Gohman84023e02010-07-10 09:00:22 +0000292 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
293 DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000294 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000295 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000296 return true;
297 }
298 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000299
Chris Lattner438949a2008-10-15 05:30:52 +0000300 unsigned ValReg = getRegForValue(Val);
301 if (ValReg == 0)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000302 return false;
303
Chris Lattner438949a2008-10-15 05:30:52 +0000304 return X86FastEmitStore(VT, ValReg, AM);
305}
306
Evan Cheng24e3a902008-09-08 06:35:17 +0000307/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
308/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
309/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000310bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
311 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000312 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000313 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
314 Src, /*TODO: Kill=*/false);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000315
Owen Andersonac34a002008-09-11 19:44:55 +0000316 if (RR != 0) {
317 ResultReg = RR;
318 return true;
319 } else
320 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000321}
322
Dan Gohman0586d912008-09-10 20:11:02 +0000323/// X86SelectAddress - Attempt to fill in an address from the given value.
324///
Dan Gohman46510a72010-04-15 01:51:59 +0000325bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
326 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000327 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000328 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000329 // Don't walk into other basic blocks; it's possible we haven't
330 // visited them yet, so the instructions may not yet be assigned
331 // virtual registers.
Dan Gohman742bf872010-11-16 22:43:23 +0000332 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
333 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
334 Opcode = I->getOpcode();
335 U = I;
336 }
Dan Gohman46510a72010-04-15 01:51:59 +0000337 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000338 Opcode = C->getOpcode();
339 U = C;
340 }
Dan Gohman0586d912008-09-10 20:11:02 +0000341
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000342 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
Chris Lattner868ee942010-06-15 19:08:40 +0000343 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000344 // Fast instruction selection doesn't support the special
345 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000346 return false;
347
Dan Gohman35893082008-09-18 23:23:44 +0000348 switch (Opcode) {
349 default: break;
350 case Instruction::BitCast:
351 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000352 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000353
354 case Instruction::IntToPtr:
355 // Look past no-op inttoptrs.
356 if (TLI.getValueType(U->getOperand(0)->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::PtrToInt:
361 // Look past no-op ptrtoints.
362 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000363 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000364 break;
Dan Gohman35893082008-09-18 23:23:44 +0000365
366 case Instruction::Alloca: {
367 // Do static allocas.
368 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000369 DenseMap<const AllocaInst*, int>::iterator SI =
370 FuncInfo.StaticAllocaMap.find(A);
371 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000372 AM.BaseType = X86AddressMode::FrameIndexBase;
373 AM.Base.FrameIndex = SI->second;
374 return true;
375 }
376 break;
Dan Gohman35893082008-09-18 23:23:44 +0000377 }
378
379 case Instruction::Add: {
380 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000381 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000382 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
383 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000384 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000385 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000386 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000387 }
Dan Gohman0586d912008-09-10 20:11:02 +0000388 }
Dan Gohman35893082008-09-18 23:23:44 +0000389 break;
390 }
391
392 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000393 X86AddressMode SavedAM = AM;
394
Dan Gohman35893082008-09-18 23:23:44 +0000395 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000396 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000397 unsigned IndexReg = AM.IndexReg;
398 unsigned Scale = AM.Scale;
399 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000400 // Iterate through the indices, folding what we can. Constants can be
401 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000402 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000403 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000404 const Value *Op = *i;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000405 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Dan Gohman35893082008-09-18 23:23:44 +0000406 const StructLayout *SL = TD.getStructLayout(STy);
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000407 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
408 continue;
409 }
Eric Christopher471e4222011-06-08 23:55:35 +0000410
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000411 // A array/variable index is always of the form i*S where S is the
412 // constant scale size. See if we can push the scale into immediates.
413 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
414 for (;;) {
415 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
416 // Constant-offset addressing.
417 Disp += CI->getSExtValue() * S;
418 break;
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000419 }
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000420 if (isa<AddOperator>(Op) &&
421 (!isa<Instruction>(Op) ||
422 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
423 == FuncInfo.MBB) &&
424 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
425 // An add (in the same block) with a constant operand. Fold the
426 // constant.
427 ConstantInt *CI =
428 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
429 Disp += CI->getSExtValue() * S;
430 // Iterate on the other operand.
431 Op = cast<AddOperator>(Op)->getOperand(0);
432 continue;
433 }
434 if (IndexReg == 0 &&
435 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
436 (S == 1 || S == 2 || S == 4 || S == 8)) {
437 // Scaled-index addressing.
438 Scale = S;
439 IndexReg = getRegForGEPIndex(Op).first;
440 if (IndexReg == 0)
441 return false;
442 break;
443 }
444 // Unsupported.
445 goto unsupported_gep;
Dan Gohman35893082008-09-18 23:23:44 +0000446 }
447 }
Dan Gohman09aae462008-09-26 20:04:15 +0000448 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000449 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000450 break;
Dan Gohman35893082008-09-18 23:23:44 +0000451 // Ok, the GEP indices were covered by constant-offset and scaled-index
452 // addressing. Update the address state and move on to examining the base.
453 AM.IndexReg = IndexReg;
454 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000455 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000456 if (X86SelectAddress(U->getOperand(0), AM))
457 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000458
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000459 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner225d4ca2010-03-04 19:48:19 +0000460 // our address and just match the value instead of completely failing.
461 AM = SavedAM;
462 break;
Dan Gohman35893082008-09-18 23:23:44 +0000463 unsupported_gep:
464 // Ok, the GEP indices weren't all covered.
465 break;
466 }
467 }
468
469 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000470 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Eli Friedmana6176ad2011-09-22 23:41:28 +0000471 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000472 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000473 return false;
474
Eli Friedmana6176ad2011-09-22 23:41:28 +0000475 // Can't handle TLS yet.
Dan Gohman46510a72010-04-15 01:51:59 +0000476 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000477 if (GVar->isThreadLocal())
478 return false;
Eric Christopher471e4222011-06-08 23:55:35 +0000479
Eli Friedmana6176ad2011-09-22 23:41:28 +0000480 // Can't handle TLS yet, part 2 (this is slightly crazy, but this is how
481 // it works...).
482 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
483 if (const GlobalVariable *GVar =
484 dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false)))
485 if (GVar->isThreadLocal())
486 return false;
487
Chris Lattner0a1c9972011-04-17 17:47:38 +0000488 // RIP-relative addresses can't have additional register operands, so if
489 // we've already folded stuff into the addressing mode, just force the
490 // global value into its own register, which we can use as the basereg.
491 if (!Subtarget->isPICStyleRIPRel() ||
492 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
493 // Okay, we've committed to selecting this global. Set up the address.
494 AM.GV = GV;
Dan Gohmane9865942009-02-23 22:03:08 +0000495
Chris Lattner0a1c9972011-04-17 17:47:38 +0000496 // Allow the subtarget to classify the global.
497 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000498
Chris Lattner0a1c9972011-04-17 17:47:38 +0000499 // If this reference is relative to the pic base, set it now.
500 if (isGlobalRelativeToPICBase(GVFlags)) {
501 // FIXME: How do we know Base.Reg is free??
502 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Dan Gohman7e8ef602008-09-19 23:42:04 +0000503 }
Chris Lattner0a1c9972011-04-17 17:47:38 +0000504
505 // Unless the ABI requires an extra load, return a direct reference to
506 // the global.
507 if (!isGlobalStubReference(GVFlags)) {
508 if (Subtarget->isPICStyleRIPRel()) {
509 // Use rip-relative addressing if we can. Above we verified that the
510 // base and index registers are unused.
511 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
512 AM.Base.Reg = X86::RIP;
513 }
514 AM.GVOpFlags = GVFlags;
515 return true;
516 }
517
518 // Ok, we need to do a load from a stub. If we've already loaded from
519 // this stub, reuse the loaded pointer, otherwise emit the load now.
520 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
521 unsigned LoadReg;
522 if (I != LocalValueMap.end() && I->second != 0) {
523 LoadReg = I->second;
524 } else {
525 // Issue load from stub.
526 unsigned Opc = 0;
527 const TargetRegisterClass *RC = NULL;
528 X86AddressMode StubAM;
529 StubAM.Base.Reg = AM.Base.Reg;
530 StubAM.GV = GV;
531 StubAM.GVOpFlags = GVFlags;
532
533 // Prepare for inserting code in the local-value area.
534 SavePoint SaveInsertPt = enterLocalValueArea();
535
536 if (TLI.getPointerTy() == MVT::i64) {
537 Opc = X86::MOV64rm;
538 RC = X86::GR64RegisterClass;
539
540 if (Subtarget->isPICStyleRIPRel())
541 StubAM.Base.Reg = X86::RIP;
542 } else {
543 Opc = X86::MOV32rm;
544 RC = X86::GR32RegisterClass;
545 }
546
547 LoadReg = createResultReg(RC);
548 MachineInstrBuilder LoadMI =
549 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
550 addFullAddress(LoadMI, StubAM);
551
552 // Ok, back to normal mode.
553 leaveLocalValueArea(SaveInsertPt);
554
555 // Prevent loading GV stub multiple times in same MBB.
556 LocalValueMap[V] = LoadReg;
557 }
558
559 // Now construct the final address. Note that the Disp, Scale,
560 // and Index values may already be set here.
561 AM.Base.Reg = LoadReg;
562 AM.GV = 0;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000563 return true;
564 }
Dan Gohman0586d912008-09-10 20:11:02 +0000565 }
566
Dan Gohman97135e12008-09-26 19:15:30 +0000567 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000568 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000569 if (AM.Base.Reg == 0) {
570 AM.Base.Reg = getRegForValue(V);
571 return AM.Base.Reg != 0;
572 }
573 if (AM.IndexReg == 0) {
574 assert(AM.Scale == 1 && "Scale with no index!");
575 AM.IndexReg = getRegForValue(V);
576 return AM.IndexReg != 0;
577 }
578 }
579
580 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000581}
582
Chris Lattner0aa43de2009-07-10 05:33:42 +0000583/// X86SelectCallAddress - Attempt to fill in an address from the given value.
584///
Dan Gohman46510a72010-04-15 01:51:59 +0000585bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
586 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000587 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000588 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000589 Opcode = I->getOpcode();
590 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000591 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000592 Opcode = C->getOpcode();
593 U = C;
594 }
595
596 switch (Opcode) {
597 default: break;
598 case Instruction::BitCast:
599 // Look past bitcasts.
600 return X86SelectCallAddress(U->getOperand(0), AM);
601
602 case Instruction::IntToPtr:
603 // Look past no-op inttoptrs.
604 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
605 return X86SelectCallAddress(U->getOperand(0), AM);
606 break;
607
608 case Instruction::PtrToInt:
609 // Look past no-op ptrtoints.
610 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
611 return X86SelectCallAddress(U->getOperand(0), AM);
612 break;
613 }
614
615 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000616 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000617 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000618 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000619 return false;
620
621 // RIP-relative addresses can't have additional register operands.
622 if (Subtarget->isPICStyleRIPRel() &&
623 (AM.Base.Reg != 0 || AM.IndexReg != 0))
624 return false;
625
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000626 // Can't handle DLLImport.
627 if (GV->hasDLLImportLinkage())
628 return false;
629
630 // Can't handle TLS.
Dan Gohman46510a72010-04-15 01:51:59 +0000631 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000632 if (GVar->isThreadLocal())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000633 return false;
634
635 // Okay, we've committed to selecting this global. Set up the basic address.
636 AM.GV = GV;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000637
Chris Lattnere6c07b52009-07-10 05:45:15 +0000638 // No ABI requires an extra load for anything other than DLLImport, which
639 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000640 if (Subtarget->isPICStyleRIPRel()) {
641 // Use rip-relative addressing if we can. Above we verified that the
642 // base and index registers are unused.
643 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
644 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000645 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000646 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
647 } else if (Subtarget->isPICStyleGOT()) {
648 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000649 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000650
Chris Lattner0aa43de2009-07-10 05:33:42 +0000651 return true;
652 }
653
654 // If all else fails, try to materialize the value in a register.
655 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
656 if (AM.Base.Reg == 0) {
657 AM.Base.Reg = getRegForValue(V);
658 return AM.Base.Reg != 0;
659 }
660 if (AM.IndexReg == 0) {
661 assert(AM.Scale == 1 && "Scale with no index!");
662 AM.IndexReg = getRegForValue(V);
663 return AM.IndexReg != 0;
664 }
665 }
666
667 return false;
668}
669
670
Owen Andersona3971df2008-09-04 07:08:58 +0000671/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000672bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +0000673 // Atomic stores need special handling.
674 if (cast<StoreInst>(I)->isAtomic())
675 return false;
676
Duncan Sands1440e8b2010-11-03 11:35:31 +0000677 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000678 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000679 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000680
Dan Gohman0586d912008-09-10 20:11:02 +0000681 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000682 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000683 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000684
Chris Lattner438949a2008-10-15 05:30:52 +0000685 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000686}
687
Dan Gohman84023e02010-07-10 09:00:22 +0000688/// X86SelectRet - Select and emit code to implement ret instructions.
689bool X86FastISel::X86SelectRet(const Instruction *I) {
690 const ReturnInst *Ret = cast<ReturnInst>(I);
691 const Function &F = *I->getParent()->getParent();
692
693 if (!FuncInfo.CanLowerReturn)
694 return false;
695
696 CallingConv::ID CC = F.getCallingConv();
697 if (CC != CallingConv::C &&
698 CC != CallingConv::Fast &&
699 CC != CallingConv::X86_FastCall)
700 return false;
701
702 if (Subtarget->isTargetWin64())
703 return false;
704
705 // Don't handle popping bytes on return for now.
706 if (FuncInfo.MF->getInfo<X86MachineFunctionInfo>()
707 ->getBytesToPopOnReturn() != 0)
708 return 0;
709
710 // fastcc with -tailcallopt is intended to provide a guaranteed
711 // tail call optimization. Fastisel doesn't know how to do that.
712 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
713 return false;
714
715 // Let SDISel handle vararg functions.
716 if (F.isVarArg())
717 return false;
718
719 if (Ret->getNumOperands() > 0) {
720 SmallVector<ISD::OutputArg, 4> Outs;
721 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
722 Outs, TLI);
723
724 // Analyze operands of the call, assigning locations to each operand.
725 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000726 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
727 I->getContext());
Duncan Sandse26032d2010-10-31 13:02:38 +0000728 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohman84023e02010-07-10 09:00:22 +0000729
730 const Value *RV = Ret->getOperand(0);
731 unsigned Reg = getRegForValue(RV);
732 if (Reg == 0)
733 return false;
734
735 // Only handle a single return value for now.
736 if (ValLocs.size() != 1)
737 return false;
738
739 CCValAssign &VA = ValLocs[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000740
Dan Gohman84023e02010-07-10 09:00:22 +0000741 // Don't bother handling odd stuff for now.
742 if (VA.getLocInfo() != CCValAssign::Full)
743 return false;
744 // Only handle register returns for now.
745 if (!VA.isRegLoc())
746 return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000747
748 // The calling-convention tables for x87 returns don't tell
749 // the whole story.
750 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
751 return false;
752
Eli Friedman22486c92011-05-18 23:13:10 +0000753 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedmandc515752011-05-19 22:16:13 +0000754 EVT SrcVT = TLI.getValueType(RV->getType());
755 EVT DstVT = VA.getValVT();
756 // Special handling for extended integers.
757 if (SrcVT != DstVT) {
758 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
759 return false;
760
761 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
762 return false;
763
764 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
765
766 if (SrcVT == MVT::i1) {
767 if (Outs[0].Flags.isSExt())
768 return false;
769 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
770 SrcVT = MVT::i8;
771 }
772 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
773 ISD::SIGN_EXTEND;
774 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
775 SrcReg, /*TODO: Kill=*/false);
776 }
777
778 // Make the copy.
Dan Gohman84023e02010-07-10 09:00:22 +0000779 unsigned DstReg = VA.getLocReg();
780 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000781 // Avoid a cross-class copy. This is very unlikely.
782 if (!SrcRC->contains(DstReg))
Dan Gohman84023e02010-07-10 09:00:22 +0000783 return false;
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000784 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
785 DstReg).addReg(SrcReg);
Dan Gohman84023e02010-07-10 09:00:22 +0000786
787 // Mark the register as live out of the function.
788 MRI.addLiveOut(VA.getLocReg());
789 }
790
791 // Now emit the RET.
792 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
793 return true;
794}
795
Evan Cheng8b19e562008-09-03 06:44:39 +0000796/// X86SelectLoad - Select and emit code to implement load instructions.
797///
Dan Gohman46510a72010-04-15 01:51:59 +0000798bool X86FastISel::X86SelectLoad(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +0000799 // Atomic loads need special handling.
800 if (cast<LoadInst>(I)->isAtomic())
801 return false;
802
Duncan Sands1440e8b2010-11-03 11:35:31 +0000803 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000804 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000805 return false;
806
Dan Gohman0586d912008-09-10 20:11:02 +0000807 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000808 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000809 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000810
Evan Cheng0de588f2008-09-05 21:00:03 +0000811 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000812 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000813 UpdateValueMap(I, ResultReg);
814 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000815 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000816 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000817}
818
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000819static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000820 bool HasAVX = Subtarget->hasAVX();
821 bool X86ScalarSSEf32 = HasAVX || Subtarget->hasSSE1();
822 bool X86ScalarSSEf64 = HasAVX || Subtarget->hasSSE2();
823
Owen Anderson825b72b2009-08-11 20:47:22 +0000824 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000825 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000826 case MVT::i8: return X86::CMP8rr;
827 case MVT::i16: return X86::CMP16rr;
828 case MVT::i32: return X86::CMP32rr;
829 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000830 case MVT::f32:
831 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
832 case MVT::f64:
833 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohmand98d6202008-10-02 22:15:21 +0000834 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000835}
836
Chris Lattner0e13c782008-10-15 04:13:29 +0000837/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
838/// of the comparison, return an opcode that works for the compare (e.g.
839/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000840static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000841 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000842 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000843 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000844 case MVT::i8: return X86::CMP8ri;
845 case MVT::i16: return X86::CMP16ri;
846 case MVT::i32: return X86::CMP32ri;
847 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000848 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
849 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000850 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000851 return X86::CMP64ri32;
852 return 0;
853 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000854}
855
Dan Gohman46510a72010-04-15 01:51:59 +0000856bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
857 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000858 unsigned Op0Reg = getRegForValue(Op0);
859 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000860
Chris Lattnerd53886b2008-10-15 05:18:04 +0000861 // Handle 'null' like i32/i64 0.
862 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000863 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000864
Chris Lattner9a08a612008-10-15 04:26:38 +0000865 // We have two options: compare with register or immediate. If the RHS of
866 // the compare is an immediate that we can fold into this compare, use
867 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000868 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000869 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000870 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
871 .addReg(Op0Reg)
872 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000873 return true;
874 }
875 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000876
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000877 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattner9a08a612008-10-15 04:26:38 +0000878 if (CompareOpc == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000879
Chris Lattner9a08a612008-10-15 04:26:38 +0000880 unsigned Op1Reg = getRegForValue(Op1);
881 if (Op1Reg == 0) return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000882 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
883 .addReg(Op0Reg)
884 .addReg(Op1Reg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000885
Chris Lattner9a08a612008-10-15 04:26:38 +0000886 return true;
887}
888
Dan Gohman46510a72010-04-15 01:51:59 +0000889bool X86FastISel::X86SelectCmp(const Instruction *I) {
890 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000891
Duncan Sands1440e8b2010-11-03 11:35:31 +0000892 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000893 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000894 return false;
895
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000896 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000897 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000898 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000899 switch (CI->getPredicate()) {
900 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000901 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
902 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000903
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000904 unsigned EReg = createResultReg(&X86::GR8RegClass);
905 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000906 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
907 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
908 TII.get(X86::SETNPr), NPReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000909 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000910 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000911 UpdateValueMap(I, ResultReg);
912 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000913 }
914 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000915 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
916 return false;
917
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000918 unsigned NEReg = createResultReg(&X86::GR8RegClass);
919 unsigned PReg = createResultReg(&X86::GR8RegClass);
Chris Lattner90cb88a2011-04-19 04:22:17 +0000920 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETNEr), NEReg);
921 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETPr), PReg);
922 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::OR8rr),ResultReg)
Dan Gohman84023e02010-07-10 09:00:22 +0000923 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000924 UpdateValueMap(I, ResultReg);
925 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000926 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000927 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
928 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
929 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
930 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
931 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
932 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
933 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
934 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
935 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
936 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
937 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
938 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000939
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000940 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
941 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
942 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
943 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
944 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
945 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
946 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
947 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
948 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
949 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000950 default:
951 return false;
952 }
953
Dan Gohman46510a72010-04-15 01:51:59 +0000954 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000955 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000956 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000957
Chris Lattner9a08a612008-10-15 04:26:38 +0000958 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000959 if (!X86FastEmitCompare(Op0, Op1, VT))
960 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000961
Dan Gohman84023e02010-07-10 09:00:22 +0000962 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000963 UpdateValueMap(I, ResultReg);
964 return true;
965}
Evan Cheng8b19e562008-09-03 06:44:39 +0000966
Dan Gohman46510a72010-04-15 01:51:59 +0000967bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000968 // Handle zero-extension from i1 to i8, which is common.
Eric Christopher471e4222011-06-08 23:55:35 +0000969 if (!I->getOperand(0)->getType()->isIntegerTy(1))
Eli Friedman76927d732011-05-25 23:49:02 +0000970 return false;
971
972 EVT DstVT = TLI.getValueType(I->getType());
973 if (!TLI.isTypeLegal(DstVT))
974 return false;
975
976 unsigned ResultReg = getRegForValue(I->getOperand(0));
977 if (ResultReg == 0)
978 return false;
979
980 // Set the high bits to zero.
981 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
982 if (ResultReg == 0)
983 return false;
984
985 if (DstVT != MVT::i8) {
986 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
987 ResultReg, /*Kill=*/true);
988 if (ResultReg == 0)
989 return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000990 }
991
Eli Friedman76927d732011-05-25 23:49:02 +0000992 UpdateValueMap(I, ResultReg);
993 return true;
Dan Gohmand89ae992008-09-05 01:06:14 +0000994}
995
Chris Lattner9a08a612008-10-15 04:26:38 +0000996
Dan Gohman46510a72010-04-15 01:51:59 +0000997bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000998 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000999 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001000 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +00001001 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1002 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +00001003
Dan Gohman8bef7442010-08-21 02:32:36 +00001004 // Fold the common case of a conditional branch with a comparison
1005 // in the same block (values defined on other blocks may not have
1006 // initialized registers).
Dan Gohman46510a72010-04-15 01:51:59 +00001007 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman8bef7442010-08-21 02:32:36 +00001008 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001009 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +00001010
Dan Gohmand98d6202008-10-02 22:15:21 +00001011 // Try to take advantage of fallthrough opportunities.
1012 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohman84023e02010-07-10 09:00:22 +00001013 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +00001014 std::swap(TrueMBB, FalseMBB);
1015 Predicate = CmpInst::getInversePredicate(Predicate);
1016 }
1017
Chris Lattner871d2462008-10-15 03:58:05 +00001018 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
1019 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
1020
Dan Gohmand98d6202008-10-02 22:15:21 +00001021 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +00001022 case CmpInst::FCMP_OEQ:
1023 std::swap(TrueMBB, FalseMBB);
1024 Predicate = CmpInst::FCMP_UNE;
1025 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001026 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1027 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1028 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1029 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
1030 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
1031 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1032 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
1033 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
1034 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1035 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
1036 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
1037 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1038 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001039
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001040 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1041 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1042 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1043 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1044 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1045 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1046 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
1047 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1048 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1049 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +00001050 default:
1051 return false;
1052 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001053
Dan Gohman46510a72010-04-15 01:51:59 +00001054 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +00001055 if (SwapArgs)
1056 std::swap(Op0, Op1);
1057
Chris Lattner9a08a612008-10-15 04:26:38 +00001058 // Emit a compare of the LHS and RHS, setting the flags.
1059 if (!X86FastEmitCompare(Op0, Op1, VT))
1060 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001061
Dan Gohman84023e02010-07-10 09:00:22 +00001062 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1063 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001064
1065 if (Predicate == CmpInst::FCMP_UNE) {
1066 // X86 requires a second branch to handle UNE (and OEQ,
1067 // which is mapped to UNE above).
Dan Gohman84023e02010-07-10 09:00:22 +00001068 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1069 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001070 }
1071
Stuart Hastings3bf91252010-06-17 22:43:56 +00001072 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001073 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +00001074 return true;
1075 }
Chris Lattner90cb88a2011-04-19 04:22:17 +00001076 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1077 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1078 // typically happen for _Bool and C++ bools.
1079 MVT SourceVT;
1080 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1081 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1082 unsigned TestOpc = 0;
1083 switch (SourceVT.SimpleTy) {
1084 default: break;
1085 case MVT::i8: TestOpc = X86::TEST8ri; break;
1086 case MVT::i16: TestOpc = X86::TEST16ri; break;
1087 case MVT::i32: TestOpc = X86::TEST32ri; break;
1088 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1089 }
1090 if (TestOpc) {
1091 unsigned OpReg = getRegForValue(TI->getOperand(0));
1092 if (OpReg == 0) return false;
1093 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TestOpc))
1094 .addReg(OpReg).addImm(1);
Eric Christopher471e4222011-06-08 23:55:35 +00001095
Chris Lattnerc76d1212011-04-19 04:26:32 +00001096 unsigned JmpOpc = X86::JNE_4;
1097 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1098 std::swap(TrueMBB, FalseMBB);
1099 JmpOpc = X86::JE_4;
1100 }
Eric Christopher471e4222011-06-08 23:55:35 +00001101
Chris Lattnerc76d1212011-04-19 04:26:32 +00001102 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(JmpOpc))
Chris Lattner90cb88a2011-04-19 04:22:17 +00001103 .addMBB(TrueMBB);
1104 FastEmitBranch(FalseMBB, DL);
1105 FuncInfo.MBB->addSuccessor(TrueMBB);
1106 return true;
1107 }
1108 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001109 }
1110
1111 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman547eb4f2011-04-27 01:34:27 +00001112 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1113 // in an explicit cast, so make sure to handle that correctly.
Dan Gohmand98d6202008-10-02 22:15:21 +00001114 unsigned OpReg = getRegForValue(BI->getCondition());
1115 if (OpReg == 0) return false;
1116
Eli Friedman547eb4f2011-04-27 01:34:27 +00001117 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8ri))
1118 .addReg(OpReg).addImm(1);
Dan Gohman84023e02010-07-10 09:00:22 +00001119 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1120 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001121 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001122 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001123 return true;
1124}
1125
Dan Gohman46510a72010-04-15 01:51:59 +00001126bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner602fc062011-04-17 20:23:29 +00001127 unsigned CReg = 0, OpReg = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001128 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001129 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001130 CReg = X86::CL;
1131 RC = &X86::GR8RegClass;
1132 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001133 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1134 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1135 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001136 default: return false;
1137 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001138 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001139 CReg = X86::CX;
1140 RC = &X86::GR16RegClass;
1141 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001142 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1143 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1144 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001145 default: return false;
1146 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001147 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001148 CReg = X86::ECX;
1149 RC = &X86::GR32RegClass;
1150 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001151 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1152 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1153 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001154 default: return false;
1155 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001156 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001157 CReg = X86::RCX;
1158 RC = &X86::GR64RegClass;
1159 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001160 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1161 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1162 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001163 default: return false;
1164 }
1165 } else {
1166 return false;
1167 }
1168
Duncan Sands1440e8b2010-11-03 11:35:31 +00001169 MVT VT;
1170 if (!isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001171 return false;
1172
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001173 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1174 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001175
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001176 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1177 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001178 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1179 CReg).addReg(Op1Reg);
Dan Gohman145b8282008-10-07 21:50:36 +00001180
1181 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001182 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001183 if (CReg != X86::CL)
Dan Gohman84023e02010-07-10 09:00:22 +00001184 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1185 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001186 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001187
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001188 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001189 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1190 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001191 UpdateValueMap(I, ResultReg);
1192 return true;
1193}
1194
Dan Gohman46510a72010-04-15 01:51:59 +00001195bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001196 MVT VT;
1197 if (!isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001198 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001199
Eric Christophere487b012010-09-29 23:00:29 +00001200 // We only use cmov here, if we don't have a cmov instruction bail.
1201 if (!Subtarget->hasCMov()) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001202
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001203 unsigned Opc = 0;
1204 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001205 if (VT == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001206 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001207 RC = &X86::GR16RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001208 } else if (VT == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001209 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001210 RC = &X86::GR32RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001211 } else if (VT == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001212 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001213 RC = &X86::GR64RegClass;
1214 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001215 return false;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001216 }
1217
1218 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1219 if (Op0Reg == 0) return false;
1220 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1221 if (Op1Reg == 0) return false;
1222 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1223 if (Op2Reg == 0) return false;
1224
Dan Gohman84023e02010-07-10 09:00:22 +00001225 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1226 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001227 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001228 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1229 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001230 UpdateValueMap(I, ResultReg);
1231 return true;
1232}
1233
Dan Gohman46510a72010-04-15 01:51:59 +00001234bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001235 // fpext from float to double.
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001236 if (X86ScalarSSEf64 &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001237 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001238 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001239 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001240 unsigned OpReg = getRegForValue(V);
1241 if (OpReg == 0) return false;
1242 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001243 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1244 TII.get(X86::CVTSS2SDrr), ResultReg)
1245 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001246 UpdateValueMap(I, ResultReg);
1247 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001248 }
1249 }
1250
1251 return false;
1252}
1253
Dan Gohman46510a72010-04-15 01:51:59 +00001254bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001255 if (X86ScalarSSEf64) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001256 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001257 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001258 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001259 unsigned OpReg = getRegForValue(V);
1260 if (OpReg == 0) return false;
1261 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001262 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1263 TII.get(X86::CVTSD2SSrr), ResultReg)
1264 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001265 UpdateValueMap(I, ResultReg);
1266 return true;
1267 }
1268 }
1269 }
1270
1271 return false;
1272}
1273
Dan Gohman46510a72010-04-15 01:51:59 +00001274bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001275 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1276 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001277
Eli Friedman76927d732011-05-25 23:49:02 +00001278 // This code only handles truncation to byte.
Owen Anderson825b72b2009-08-11 20:47:22 +00001279 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001280 return false;
Eli Friedman76927d732011-05-25 23:49:02 +00001281 if (!TLI.isTypeLegal(SrcVT))
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001282 return false;
1283
1284 unsigned InputReg = getRegForValue(I->getOperand(0));
1285 if (!InputReg)
1286 // Unhandled operand. Halt "fast" selection and bail.
1287 return false;
1288
Eli Friedman76927d732011-05-25 23:49:02 +00001289 if (SrcVT == MVT::i8) {
1290 // Truncate from i8 to i1; no code needed.
1291 UpdateValueMap(I, InputReg);
1292 return true;
1293 }
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001294
Eli Friedman76927d732011-05-25 23:49:02 +00001295 if (!Subtarget->is64Bit()) {
1296 // If we're on x86-32; we can't extract an i8 from a general register.
1297 // First issue a copy to GR16_ABCD or GR32_ABCD.
1298 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
1299 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
1300 unsigned CopyReg = createResultReg(CopyRC);
1301 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1302 CopyReg).addReg(InputReg);
1303 InputReg = CopyReg;
1304 }
1305
1306 // Issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001307 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedman76927d732011-05-25 23:49:02 +00001308 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001309 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001310 if (!ResultReg)
1311 return false;
1312
1313 UpdateValueMap(I, ResultReg);
1314 return true;
1315}
1316
Eli Friedmanc0883452011-05-20 22:21:04 +00001317bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1318 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1319}
1320
Eli Friedmand5089a92011-04-27 01:45:07 +00001321bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1322 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedmanc0883452011-05-20 22:21:04 +00001323
Eli Friedmand5089a92011-04-27 01:45:07 +00001324 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedmanc0883452011-05-20 22:21:04 +00001325 if (!IsMemcpySmall(Len))
1326 return false;
1327
1328 bool i64Legal = Subtarget->is64Bit();
Eli Friedmand5089a92011-04-27 01:45:07 +00001329
1330 // We don't care about alignment here since we just emit integer accesses.
1331 while (Len) {
1332 MVT VT;
1333 if (Len >= 8 && i64Legal)
1334 VT = MVT::i64;
1335 else if (Len >= 4)
1336 VT = MVT::i32;
1337 else if (Len >= 2)
1338 VT = MVT::i16;
1339 else {
1340 assert(Len == 1);
1341 VT = MVT::i8;
1342 }
1343
1344 unsigned Reg;
1345 bool RV = X86FastEmitLoad(VT, SrcAM, Reg);
1346 RV &= X86FastEmitStore(VT, Reg, DestAM);
1347 assert(RV && "Failed to emit load or store??");
1348
1349 unsigned Size = VT.getSizeInBits()/8;
1350 Len -= Size;
1351 DestAM.Disp += Size;
1352 SrcAM.Disp += Size;
1353 }
1354
1355 return true;
1356}
1357
Dan Gohman46510a72010-04-15 01:51:59 +00001358bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001359 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001360 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001361 default: return false;
Chris Lattner832e4942011-04-19 05:52:03 +00001362 case Intrinsic::memcpy: {
1363 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1364 // Don't handle volatile or variable length memcpys.
Eli Friedman25255cb2011-06-10 23:39:36 +00001365 if (MCI.isVolatile())
Chris Lattner832e4942011-04-19 05:52:03 +00001366 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001367
Eli Friedman25255cb2011-06-10 23:39:36 +00001368 if (isa<ConstantInt>(MCI.getLength())) {
1369 // Small memcpy's are common enough that we want to do them
1370 // without a call if possible.
1371 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1372 if (IsMemcpySmall(Len)) {
1373 X86AddressMode DestAM, SrcAM;
1374 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1375 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1376 return false;
1377 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1378 return true;
1379 }
1380 }
Eric Christopher471e4222011-06-08 23:55:35 +00001381
Eli Friedman25255cb2011-06-10 23:39:36 +00001382 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1383 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner832e4942011-04-19 05:52:03 +00001384 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001385
Eli Friedman25255cb2011-06-10 23:39:36 +00001386 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1387 return false;
1388
1389 return DoSelectCall(&I, "memcpy");
Chris Lattner832e4942011-04-19 05:52:03 +00001390 }
Eli Friedman25255cb2011-06-10 23:39:36 +00001391 case Intrinsic::memset: {
1392 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher471e4222011-06-08 23:55:35 +00001393
Nick Lewycky3207c9a2011-08-02 00:40:16 +00001394 if (MSI.isVolatile())
1395 return false;
1396
Eli Friedman25255cb2011-06-10 23:39:36 +00001397 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1398 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1399 return false;
1400
1401 if (MSI.getDestAddressSpace() > 255)
1402 return false;
1403
1404 return DoSelectCall(&I, "memset");
1405 }
Eric Christopher07754c22010-03-18 20:27:26 +00001406 case Intrinsic::stackprotector: {
1407 // Emit code inline code to store the stack guard onto the stack.
1408 EVT PtrTy = TLI.getPointerTy();
1409
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001410 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1411 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001412
1413 // Grab the frame index.
1414 X86AddressMode AM;
1415 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher88dee302010-03-18 21:58:33 +00001416 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001417 return true;
1418 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001419 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001420 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001421 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001422 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001423 if (!X86SelectAddress(DI->getAddress(), AM))
1424 return false;
Evan Chenge837dea2011-06-28 19:10:37 +00001425 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001426 // FIXME may need to add RegState::Debug to any registers produced,
1427 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001428 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1429 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001430 return true;
1431 }
Eric Christopher77f79892010-01-18 22:11:29 +00001432 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001433 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001434 return true;
1435 }
Bill Wendling52370a12008-12-09 02:42:50 +00001436 case Intrinsic::sadd_with_overflow:
1437 case Intrinsic::uadd_with_overflow: {
Chris Lattner832e4942011-04-19 05:52:03 +00001438 // FIXME: Should fold immediates.
Eric Christopher471e4222011-06-08 23:55:35 +00001439
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001440 // Replace "add with overflow" intrinsics with an "add" instruction followed
Eli Friedman482feb32011-05-16 21:06:17 +00001441 // by a seto/setc instruction.
Bill Wendling52370a12008-12-09 02:42:50 +00001442 const Function *Callee = I.getCalledFunction();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001443 Type *RetTy =
Bill Wendling52370a12008-12-09 02:42:50 +00001444 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1445
Duncan Sands1440e8b2010-11-03 11:35:31 +00001446 MVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001447 if (!isTypeLegal(RetTy, VT))
1448 return false;
1449
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001450 const Value *Op1 = I.getArgOperand(0);
1451 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001452 unsigned Reg1 = getRegForValue(Op1);
1453 unsigned Reg2 = getRegForValue(Op2);
1454
1455 if (Reg1 == 0 || Reg2 == 0)
1456 // FIXME: Handle values *not* in registers.
1457 return false;
1458
1459 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001460 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001461 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001462 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001463 OpC = X86::ADD64rr;
1464 else
1465 return false;
1466
Eli Friedman482feb32011-05-16 21:06:17 +00001467 // The call to CreateRegs builds two sequential registers, to store the
1468 // both the the returned values.
1469 unsigned ResultReg = FuncInfo.CreateRegs(I.getType());
Dan Gohman84023e02010-07-10 09:00:22 +00001470 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1471 .addReg(Reg1).addReg(Reg2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001472
Chris Lattnera9a42252009-04-12 07:36:01 +00001473 unsigned Opc = X86::SETBr;
1474 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1475 Opc = X86::SETOr;
Eli Friedman482feb32011-05-16 21:06:17 +00001476 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg+1);
1477
1478 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling52370a12008-12-09 02:42:50 +00001479 return true;
1480 }
1481 }
1482}
1483
Dan Gohman46510a72010-04-15 01:51:59 +00001484bool X86FastISel::X86SelectCall(const Instruction *I) {
1485 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001486 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001487
1488 // Can't handle inline asm yet.
1489 if (isa<InlineAsm>(Callee))
1490 return false;
1491
Bill Wendling52370a12008-12-09 02:42:50 +00001492 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001493 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001494 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001495
Eli Friedman25255cb2011-06-10 23:39:36 +00001496 return DoSelectCall(I, 0);
1497}
1498
1499// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
1500bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
1501 const CallInst *CI = cast<CallInst>(I);
1502 const Value *Callee = CI->getCalledValue();
1503
Evan Chengf3d4efe2008-09-07 09:09:33 +00001504 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001505 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001506 CallingConv::ID CC = CS.getCallingConv();
Chris Lattnere03b8d32011-04-19 04:42:38 +00001507 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Evan Chengf3d4efe2008-09-07 09:09:33 +00001508 CC != CallingConv::X86_FastCall)
1509 return false;
1510
Evan Cheng381993f2010-01-27 00:00:57 +00001511 // fastcc with -tailcallopt is intended to provide a guaranteed
1512 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman1797ed52010-02-08 20:27:50 +00001513 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001514 return false;
1515
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001516 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1517 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedman37620462011-04-19 17:22:22 +00001518 bool isVarArg = FTy->isVarArg();
1519
1520 // Don't know how to handle Win64 varargs yet. Nothing special needed for
1521 // x86-32. Special handling for x86-64 is implemented.
1522 if (isVarArg && Subtarget->isTargetWin64())
Evan Chengf3d4efe2008-09-07 09:09:33 +00001523 return false;
1524
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001525 // Fast-isel doesn't know about callee-pop yet.
Evan Chengef41ff62011-06-23 17:54:54 +00001526 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
1527 GuaranteedTailCallOpt))
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001528 return false;
1529
Eli Friedman19515b42011-05-17 18:29:03 +00001530 // Check whether the function can return without sret-demotion.
1531 SmallVector<ISD::OutputArg, 4> Outs;
1532 SmallVector<uint64_t, 4> Offsets;
1533 GetReturnInfo(I->getType(), CS.getAttributes().getRetAttributes(),
1534 Outs, TLI, &Offsets);
1535 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Eric Christopher471e4222011-06-08 23:55:35 +00001536 *FuncInfo.MF, FTy->isVarArg(),
1537 Outs, FTy->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00001538 if (!CanLowerReturn)
Eli Friedmanc93943b2011-05-17 02:36:59 +00001539 return false;
1540
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001541 // Materialize callee address in a register. FIXME: GV address can be
1542 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001543 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001544 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001545 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001546 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001547 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001548 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001549 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001550 } else if (CalleeAM.Base.Reg != 0) {
1551 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001552 } else
1553 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001554
Evan Chengf3d4efe2008-09-07 09:09:33 +00001555 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001556 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001557 SmallVector<unsigned, 8> Args;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001558 SmallVector<MVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001559 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001560 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001561 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001562 ArgVTs.reserve(CS.arg_size());
1563 ArgFlags.reserve(CS.arg_size());
Dan Gohman46510a72010-04-15 01:51:59 +00001564 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001565 i != e; ++i) {
Eli Friedman25255cb2011-06-10 23:39:36 +00001566 // If we're lowering a mem intrinsic instead of a regular call, skip the
1567 // last two arguments, which should not passed to the underlying functions.
1568 if (MemIntName && e-i <= 2)
1569 break;
Chris Lattnere03b8d32011-04-19 04:42:38 +00001570 Value *ArgVal = *i;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001571 ISD::ArgFlagsTy Flags;
1572 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001573 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001574 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001575 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001576 Flags.setZExt();
1577
Eli Friedmanc0883452011-05-20 22:21:04 +00001578 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001579 PointerType *Ty = cast<PointerType>(ArgVal->getType());
1580 Type *ElementTy = Ty->getElementType();
Eli Friedmanc0883452011-05-20 22:21:04 +00001581 unsigned FrameSize = TD.getTypeAllocSize(ElementTy);
1582 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
1583 if (!FrameAlign)
1584 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
1585 Flags.setByVal();
1586 Flags.setByValSize(FrameSize);
1587 Flags.setByValAlign(FrameAlign);
1588 if (!IsMemcpySmall(FrameSize))
1589 return false;
1590 }
1591
1592 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
1593 Flags.setInReg();
1594 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
1595 Flags.setNest();
1596
Chris Lattnere03b8d32011-04-19 04:42:38 +00001597 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1598 // instruction. This is safe because it is common to all fastisel supported
1599 // calling conventions on x86.
1600 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1601 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1602 CI->getBitWidth() == 16) {
1603 if (Flags.isSExt())
1604 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1605 else
1606 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1607 }
1608 }
Eric Christopher471e4222011-06-08 23:55:35 +00001609
Chris Lattnerb44101c2011-04-19 05:09:50 +00001610 unsigned ArgReg;
Eric Christopher471e4222011-06-08 23:55:35 +00001611
Chris Lattnerff009ad2011-04-19 05:15:59 +00001612 // Passing bools around ends up doing a trunc to i1 and passing it.
1613 // Codegen this as an argument + "and 1".
Chris Lattnerb44101c2011-04-19 05:09:50 +00001614 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
1615 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
1616 ArgVal->hasOneUse()) {
Chris Lattnerb44101c2011-04-19 05:09:50 +00001617 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
1618 ArgReg = getRegForValue(ArgVal);
1619 if (ArgReg == 0) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001620
Chris Lattnerb44101c2011-04-19 05:09:50 +00001621 MVT ArgVT;
1622 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001623
Chris Lattnerb44101c2011-04-19 05:09:50 +00001624 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
1625 ArgVal->hasOneUse(), 1);
1626 } else {
1627 ArgReg = getRegForValue(ArgVal);
Chris Lattnerb44101c2011-04-19 05:09:50 +00001628 }
Chris Lattnere03b8d32011-04-19 04:42:38 +00001629
Chris Lattnerff009ad2011-04-19 05:15:59 +00001630 if (ArgReg == 0) return false;
1631
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001632 Type *ArgTy = ArgVal->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001633 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001634 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001635 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00001636 if (ArgVT == MVT::x86mmx)
1637 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001638 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1639 Flags.setOrigAlign(OriginalAlignment);
1640
Chris Lattnerb44101c2011-04-19 05:09:50 +00001641 Args.push_back(ArgReg);
Chris Lattnere03b8d32011-04-19 04:42:38 +00001642 ArgVals.push_back(ArgVal);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001643 ArgVTs.push_back(ArgVT);
1644 ArgFlags.push_back(Flags);
1645 }
1646
1647 // Analyze operands of the call, assigning locations to each operand.
1648 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001649 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
1650 I->getParent()->getContext());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001651
Dan Gohmand8acddd2010-06-01 21:09:47 +00001652 // Allocate shadow area for Win64
Chris Lattnere03b8d32011-04-19 04:42:38 +00001653 if (Subtarget->isTargetWin64())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001654 CCInfo.AllocateStack(32, 8);
Dan Gohmand8acddd2010-06-01 21:09:47 +00001655
Duncan Sands45907662010-10-31 13:21:44 +00001656 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001657
1658 // Get a count of how many bytes are to be pushed on the stack.
1659 unsigned NumBytes = CCInfo.getNextStackOffset();
1660
1661 // Issue CALLSEQ_START
Evan Chengd5b03f22011-06-28 21:14:33 +00001662 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001663 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1664 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001665
Chris Lattner438949a2008-10-15 05:30:52 +00001666 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001667 // copies / loads.
1668 SmallVector<unsigned, 4> RegArgs;
1669 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1670 CCValAssign &VA = ArgLocs[i];
1671 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001672 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001673
Evan Chengf3d4efe2008-09-07 09:09:33 +00001674 // Promote the value if needed.
1675 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001676 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001677 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001678 case CCValAssign::SExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001679 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1680 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001681 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1682 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001683 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001684 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001685 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001686 }
1687 case CCValAssign::ZExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001688 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1689 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001690 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1691 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001692 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001693 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001694 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001695 }
1696 case CCValAssign::AExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001697 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1698 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001699 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1700 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001701 if (!Emitted)
1702 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001703 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001704 if (!Emitted)
1705 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1706 Arg, ArgVT, Arg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001707
Chris Lattnerc46ec642011-01-05 22:26:52 +00001708 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001709 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001710 break;
1711 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001712 case CCValAssign::BCvt: {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001713 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001714 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001715 assert(BC != 0 && "Failed to emit a bitcast!");
1716 Arg = BC;
1717 ArgVT = VA.getLocVT();
1718 break;
1719 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001720 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001721
Evan Chengf3d4efe2008-09-07 09:09:33 +00001722 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001723 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1724 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001725 RegArgs.push_back(VA.getLocReg());
1726 } else {
1727 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001728 X86AddressMode AM;
1729 AM.Base.Reg = StackPtr;
1730 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001731 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedmanc0883452011-05-20 22:21:04 +00001732 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001733
Eli Friedmanc0883452011-05-20 22:21:04 +00001734 if (Flags.isByVal()) {
1735 X86AddressMode SrcAM;
1736 SrcAM.Base.Reg = Arg;
1737 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
1738 assert(Res && "memcpy length already checked!"); (void)Res;
1739 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
1740 // If this is a really simple value, emit this with the Value* version
1741 //of X86FastEmitStore. If it isn't simple, we don't want to do this,
1742 // as it can cause us to reevaluate the argument.
Chris Lattner241ab472008-10-15 05:38:32 +00001743 X86FastEmitStore(ArgVT, ArgVal, AM);
Eli Friedmanc0883452011-05-20 22:21:04 +00001744 } else {
Chris Lattner241ab472008-10-15 05:38:32 +00001745 X86FastEmitStore(ArgVT, Arg, AM);
Eli Friedmanc0883452011-05-20 22:21:04 +00001746 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001747 }
1748 }
1749
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001750 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001751 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001752 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001753 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001754 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1755 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001756 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001757
Eli Friedman37620462011-04-19 17:22:22 +00001758 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64()) {
1759 // Count the number of XMM registers allocated.
1760 static const unsigned XMMArgRegs[] = {
1761 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1762 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1763 };
1764 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1765 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::MOV8ri),
1766 X86::AL).addImm(NumXMMRegs);
1767 }
1768
Evan Chengf3d4efe2008-09-07 09:09:33 +00001769 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001770 MachineInstrBuilder MIB;
1771 if (CalleeOp) {
1772 // Register-indirect call.
Nate Begeman0c07b642010-07-22 00:09:39 +00001773 unsigned CallOpc;
1774 if (Subtarget->isTargetWin64())
1775 CallOpc = X86::WINCALL64r;
1776 else if (Subtarget->is64Bit())
1777 CallOpc = X86::CALL64r;
1778 else
1779 CallOpc = X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00001780 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1781 .addReg(CalleeOp);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001782
Chris Lattner51e8eab2009-07-09 06:34:26 +00001783 } else {
1784 // Direct call.
1785 assert(GV && "Not a direct call");
Nate Begeman0c07b642010-07-22 00:09:39 +00001786 unsigned CallOpc;
1787 if (Subtarget->isTargetWin64())
1788 CallOpc = X86::WINCALL64pcrel32;
1789 else if (Subtarget->is64Bit())
1790 CallOpc = X86::CALL64pcrel32;
1791 else
1792 CallOpc = X86::CALLpcrel32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001793
Chris Lattner51e8eab2009-07-09 06:34:26 +00001794 // See if we need any target-specific flags on the GV operand.
1795 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001796
Chris Lattner51e8eab2009-07-09 06:34:26 +00001797 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1798 // external symbols most go through the PLT in PIC mode. If the symbol
1799 // has hidden or protected visibility, or if it is static or local, then
1800 // we don't need to use the PLT - we can directly call it.
1801 if (Subtarget->isTargetELF() &&
1802 TM.getRelocationModel() == Reloc::PIC_ &&
1803 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1804 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001805 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001806 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00001807 (!Subtarget->getTargetTriple().isMacOSX() ||
1808 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner51e8eab2009-07-09 06:34:26 +00001809 // PC-relative references to external symbols should go through $stub,
1810 // unless we're building with the leopard linker or later, which
1811 // automatically synthesizes these stubs.
1812 OpFlags = X86II::MO_DARWIN_STUB;
1813 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001814
1815
Eli Friedman25255cb2011-06-10 23:39:36 +00001816 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc));
1817 if (MemIntName)
Eli Friedman8a37aba2011-06-11 01:55:07 +00001818 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedman25255cb2011-06-10 23:39:36 +00001819 else
1820 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001821 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001822
1823 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001824 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001825 MIB.addReg(X86::EBX);
1826
Eli Friedman37620462011-04-19 17:22:22 +00001827 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64())
1828 MIB.addReg(X86::AL);
1829
Evan Chengf3d4efe2008-09-07 09:09:33 +00001830 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001831 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1832 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001833
1834 // Issue CALLSEQ_END
Evan Chengd5b03f22011-06-28 21:14:33 +00001835 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Eli Friedmand227eed2011-04-28 20:19:12 +00001836 unsigned NumBytesCallee = 0;
1837 if (!Subtarget->is64Bit() && CS.paramHasAttr(1, Attribute::StructRet))
1838 NumBytesCallee = 4;
Dan Gohman84023e02010-07-10 09:00:22 +00001839 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
Eli Friedmand227eed2011-04-28 20:19:12 +00001840 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001841
Eli Friedman19515b42011-05-17 18:29:03 +00001842 // Build info for return calling conv lowering code.
1843 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
1844 SmallVector<ISD::InputArg, 32> Ins;
1845 SmallVector<EVT, 4> RetTys;
1846 ComputeValueVTs(TLI, I->getType(), RetTys);
1847 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
1848 EVT VT = RetTys[i];
1849 EVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
1850 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
1851 for (unsigned j = 0; j != NumRegs; ++j) {
1852 ISD::InputArg MyFlags;
1853 MyFlags.VT = RegisterVT.getSimpleVT();
1854 MyFlags.Used = !CS.getInstruction()->use_empty();
1855 if (CS.paramHasAttr(0, Attribute::SExt))
1856 MyFlags.Flags.setSExt();
1857 if (CS.paramHasAttr(0, Attribute::ZExt))
1858 MyFlags.Flags.setZExt();
1859 if (CS.paramHasAttr(0, Attribute::InReg))
1860 MyFlags.Flags.setInReg();
1861 Ins.push_back(MyFlags);
1862 }
1863 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00001864
Eli Friedman19515b42011-05-17 18:29:03 +00001865 // Now handle call return values.
1866 SmallVector<unsigned, 4> UsedRegs;
1867 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001868 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
1869 I->getParent()->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00001870 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
1871 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
1872 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1873 EVT CopyVT = RVLocs[i].getValVT();
1874 unsigned CopyReg = ResultReg + i;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001875
Evan Chengf3d4efe2008-09-07 09:09:33 +00001876 // If this is a call to a function that returns an fp value on the x87 fp
1877 // stack, but where we prefer to use the value in xmm registers, copy it
1878 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman19515b42011-05-17 18:29:03 +00001879 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001880 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesen098c7ac2011-06-30 23:42:18 +00001881 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001882 CopyVT = MVT::f80;
Jakob Stoklund Olesen098c7ac2011-06-30 23:42:18 +00001883 CopyReg = createResultReg(X86::RFP80RegisterClass);
1884 }
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00001885 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::FpPOP_RETVAL),
1886 CopyReg);
1887 } else {
1888 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1889 CopyReg).addReg(RVLocs[i].getLocReg());
1890 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001891 }
1892
Eli Friedman19515b42011-05-17 18:29:03 +00001893 if (CopyVT != RVLocs[i].getValVT()) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001894 // Round the F80 the right size, which also moves to the appropriate xmm
1895 // register. This is accomplished by storing the F80 value in memory and
1896 // then loading it back. Ewww...
Eli Friedman19515b42011-05-17 18:29:03 +00001897 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001898 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001899 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001900 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00001901 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1902 TII.get(Opc)), FI)
Eli Friedman19515b42011-05-17 18:29:03 +00001903 .addReg(CopyReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001904 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Dan Gohman84023e02010-07-10 09:00:22 +00001905 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eli Friedman19515b42011-05-17 18:29:03 +00001906 TII.get(Opc), ResultReg + i), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001907 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00001908 }
Eli Friedmancdc9a202011-05-17 00:13:47 +00001909
Eli Friedman19515b42011-05-17 18:29:03 +00001910 if (RVLocs.size())
1911 UpdateValueMap(I, ResultReg, RVLocs.size());
1912
Dan Gohmandb497122010-06-18 23:28:01 +00001913 // Set all unused physreg defs as dead.
1914 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1915
Evan Chengf3d4efe2008-09-07 09:09:33 +00001916 return true;
1917}
1918
1919
Dan Gohman99b21822008-08-28 23:21:34 +00001920bool
Dan Gohman46510a72010-04-15 01:51:59 +00001921X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001922 switch (I->getOpcode()) {
1923 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001924 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001925 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001926 case Instruction::Store:
1927 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00001928 case Instruction::Ret:
1929 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001930 case Instruction::ICmp:
1931 case Instruction::FCmp:
1932 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001933 case Instruction::ZExt:
1934 return X86SelectZExt(I);
1935 case Instruction::Br:
1936 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001937 case Instruction::Call:
1938 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001939 case Instruction::LShr:
1940 case Instruction::AShr:
1941 case Instruction::Shl:
1942 return X86SelectShift(I);
1943 case Instruction::Select:
1944 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001945 case Instruction::Trunc:
1946 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001947 case Instruction::FPExt:
1948 return X86SelectFPExt(I);
1949 case Instruction::FPTrunc:
1950 return X86SelectFPTrunc(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001951 case Instruction::IntToPtr: // Deliberate fall-through.
1952 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001953 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1954 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001955 if (DstVT.bitsGT(SrcVT))
1956 return X86SelectZExt(I);
1957 if (DstVT.bitsLT(SrcVT))
1958 return X86SelectTrunc(I);
1959 unsigned Reg = getRegForValue(I->getOperand(0));
1960 if (Reg == 0) return false;
1961 UpdateValueMap(I, Reg);
1962 return true;
1963 }
Dan Gohman99b21822008-08-28 23:21:34 +00001964 }
1965
1966 return false;
1967}
1968
Dan Gohman46510a72010-04-15 01:51:59 +00001969unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001970 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001971 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001972 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001973
Owen Anderson95267a12008-09-05 00:06:23 +00001974 // Get opcode and regclass of the output for the given load instruction.
1975 unsigned Opc = 0;
1976 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001977 switch (VT.SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001978 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001979 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001980 Opc = X86::MOV8rm;
1981 RC = X86::GR8RegisterClass;
1982 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001983 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001984 Opc = X86::MOV16rm;
1985 RC = X86::GR16RegisterClass;
1986 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001987 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001988 Opc = X86::MOV32rm;
1989 RC = X86::GR32RegisterClass;
1990 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001991 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001992 // Must be in x86-64 mode.
1993 Opc = X86::MOV64rm;
1994 RC = X86::GR64RegisterClass;
1995 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001996 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001997 if (X86ScalarSSEf32) {
1998 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Owen Anderson95267a12008-09-05 00:06:23 +00001999 RC = X86::FR32RegisterClass;
2000 } else {
2001 Opc = X86::LD_Fp32m;
2002 RC = X86::RFP32RegisterClass;
2003 }
2004 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002005 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002006 if (X86ScalarSSEf64) {
2007 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Owen Anderson95267a12008-09-05 00:06:23 +00002008 RC = X86::FR64RegisterClass;
2009 } else {
2010 Opc = X86::LD_Fp64m;
2011 RC = X86::RFP64RegisterClass;
2012 }
2013 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002014 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00002015 // No f80 support yet.
2016 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00002017 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002018
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002019 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00002020 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002021 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002022 if (X86SelectAddress(C, AM)) {
Chris Lattner685090f2011-04-17 17:12:08 +00002023 // If the expression is just a basereg, then we're done, otherwise we need
2024 // to emit an LEA.
2025 if (AM.BaseType == X86AddressMode::RegBase &&
2026 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
2027 return AM.Base.Reg;
Eric Christopher471e4222011-06-08 23:55:35 +00002028
Chris Lattner685090f2011-04-17 17:12:08 +00002029 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002030 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002031 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2032 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00002033 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002034 }
Evan Cheng0de588f2008-09-05 21:00:03 +00002035 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00002036 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002037
Owen Anderson3b217c62008-09-06 01:11:01 +00002038 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00002039 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002040 if (Align == 0) {
2041 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00002042 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002043 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002044
Dan Gohman5396c992008-09-30 01:21:32 +00002045 // x86-32 PIC requires a PIC base register for constant pools.
2046 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00002047 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00002048 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00002049 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00002050 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002051 } else if (Subtarget->isPICStyleGOT()) {
2052 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00002053 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002054 } else if (Subtarget->isPICStyleRIPRel() &&
2055 TM.getCodeModel() == CodeModel::Small) {
2056 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00002057 }
Dan Gohman5396c992008-09-30 01:21:32 +00002058
2059 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00002060 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002061 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002062 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2063 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00002064 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00002065
Owen Anderson95267a12008-09-05 00:06:23 +00002066 return ResultReg;
2067}
2068
Dan Gohman46510a72010-04-15 01:51:59 +00002069unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002070 // Fail on dynamic allocas. At this point, getRegForValue has already
2071 // checked its CSE maps, so if we're here trying to handle a dynamic
2072 // alloca, we're not going to succeed. X86SelectAddress has a
2073 // check for dynamic allocas, because it's called directly from
2074 // various places, but TargetMaterializeAlloca also needs a check
2075 // in order to avoid recursion between getRegForValue,
2076 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00002077 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002078 return 0;
2079
Dan Gohman0586d912008-09-10 20:11:02 +00002080 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002081 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00002082 return 0;
2083 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
2084 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
2085 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002086 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2087 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00002088 return ResultReg;
2089}
2090
Eli Friedman2790ba82011-04-27 22:41:55 +00002091unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2092 MVT VT;
2093 if (!isTypeLegal(CF->getType(), VT))
2094 return false;
2095
2096 // Get opcode and regclass for the given zero.
2097 unsigned Opc = 0;
2098 const TargetRegisterClass *RC = NULL;
2099 switch (VT.SimpleTy) {
2100 default: return false;
2101 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002102 if (X86ScalarSSEf32) {
2103 Opc = Subtarget->hasAVX() ? X86::VFsFLD0SS : X86::FsFLD0SS;
Eli Friedman2790ba82011-04-27 22:41:55 +00002104 RC = X86::FR32RegisterClass;
2105 } else {
2106 Opc = X86::LD_Fp032;
2107 RC = X86::RFP32RegisterClass;
2108 }
2109 break;
2110 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002111 if (X86ScalarSSEf64) {
2112 Opc = Subtarget->hasAVX() ? X86::VFsFLD0SD : X86::FsFLD0SD;
Eli Friedman2790ba82011-04-27 22:41:55 +00002113 RC = X86::FR64RegisterClass;
2114 } else {
2115 Opc = X86::LD_Fp064;
2116 RC = X86::RFP64RegisterClass;
2117 }
2118 break;
2119 case MVT::f80:
2120 // No f80 support yet.
2121 return false;
2122 }
2123
2124 unsigned ResultReg = createResultReg(RC);
2125 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
2126 return ResultReg;
2127}
2128
2129
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002130/// TryToFoldLoad - The specified machine instr operand is a vreg, and that
2131/// vreg is being provided by the specified load instruction. If possible,
2132/// try to fold the load as an operand to the instruction, returning true if
2133/// possible.
2134bool X86FastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
2135 const LoadInst *LI) {
2136 X86AddressMode AM;
2137 if (!X86SelectAddress(LI->getOperand(0), AM))
2138 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002139
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002140 X86InstrInfo &XII = (X86InstrInfo&)TII;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002141
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002142 unsigned Size = TD.getTypeAllocSize(LI->getType());
2143 unsigned Alignment = LI->getAlignment();
2144
2145 SmallVector<MachineOperand, 8> AddrOps;
2146 AM.getFullAddress(AddrOps);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002147
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002148 MachineInstr *Result =
2149 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
2150 if (Result == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002151
Chris Lattnerb99fdee2011-01-16 02:27:38 +00002152 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002153 MI->eraseFromParent();
2154 return true;
2155}
2156
2157
Evan Chengc3f44b02008-09-03 00:03:49 +00002158namespace llvm {
Dan Gohmana4160c32010-07-07 16:29:44 +00002159 llvm::FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo) {
2160 return new X86FastISel(funcInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00002161 }
Dan Gohman99b21822008-08-28 23:21:34 +00002162}