blob: eeea18e1d8c1dfc0a09fc97232c00ff8aa2c907d [file] [log] [blame]
Dan Gohman1adf1b02008-08-19 21:45:35 +00001//===-- X86FastISel.cpp - X86 FastISel implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the X86-specific support for the FastISel class. Much
11// of the target-specific code is generated by tablegen in the file
12// X86GenFastISel.inc, which is #included here.
13//
14//===----------------------------------------------------------------------===//
15
16#include "X86.h"
Evan Cheng8b19e562008-09-03 06:44:39 +000017#include "X86InstrBuilder.h"
Evan Cheng88e30412008-09-03 01:04:47 +000018#include "X86RegisterInfo.h"
19#include "X86Subtarget.h"
Dan Gohman22bb3112008-08-22 00:20:26 +000020#include "X86TargetMachine.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000021#include "llvm/CallingConv.h"
Dan Gohman6e3f05f2008-09-04 23:26:51 +000022#include "llvm/DerivedTypes.h"
Dan Gohmane9865942009-02-23 22:03:08 +000023#include "llvm/GlobalVariable.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000024#include "llvm/Instructions.h"
Chris Lattnera9a42252009-04-12 07:36:01 +000025#include "llvm/IntrinsicInst.h"
Jay Foad562b84b2011-04-11 09:35:34 +000026#include "llvm/Operator.h"
Dan Gohman84023e02010-07-10 09:00:22 +000027#include "llvm/CodeGen/Analysis.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000028#include "llvm/CodeGen/FastISel.h"
Dan Gohmana4160c32010-07-07 16:29:44 +000029#include "llvm/CodeGen/FunctionLoweringInfo.h"
Owen Anderson95267a12008-09-05 00:06:23 +000030#include "llvm/CodeGen/MachineConstantPool.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000031#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson667d8f72008-08-29 17:45:56 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf3d4efe2008-09-07 09:09:33 +000033#include "llvm/Support/CallSite.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000034#include "llvm/Support/ErrorHandling.h"
Dan Gohman35893082008-09-18 23:23:44 +000035#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Cheng381993f2010-01-27 00:00:57 +000036#include "llvm/Target/TargetOptions.h"
Evan Chengc3f44b02008-09-03 00:03:49 +000037using namespace llvm;
38
Chris Lattner087fcf32009-03-08 18:44:31 +000039namespace {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000040
Evan Chengc3f44b02008-09-03 00:03:49 +000041class X86FastISel : public FastISel {
42 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
43 /// make the right decision when generating code for different targets.
44 const X86Subtarget *Subtarget;
Evan Chengf3d4efe2008-09-07 09:09:33 +000045
46 /// StackPtr - Register used as the stack pointer.
47 ///
48 unsigned StackPtr;
49
Wesley Peckbf17cfa2010-11-23 03:31:01 +000050 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
Evan Chengf3d4efe2008-09-07 09:09:33 +000051 /// floating point ops.
52 /// When SSE is available, use it for f32 operations.
53 /// When SSE2 is available, use it for f64 operations.
54 bool X86ScalarSSEf64;
55 bool X86ScalarSSEf32;
56
Evan Cheng8b19e562008-09-03 06:44:39 +000057public:
Dan Gohmana4160c32010-07-07 16:29:44 +000058 explicit X86FastISel(FunctionLoweringInfo &funcInfo) : FastISel(funcInfo) {
Evan Cheng88e30412008-09-03 01:04:47 +000059 Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengf3d4efe2008-09-07 09:09:33 +000060 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
61 X86ScalarSSEf64 = Subtarget->hasSSE2();
62 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000063 }
Evan Chengc3f44b02008-09-03 00:03:49 +000064
Dan Gohman46510a72010-04-15 01:51:59 +000065 virtual bool TargetSelectInstruction(const Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000066
Chris Lattnerbeac75d2010-09-05 02:18:34 +000067 /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
68 /// vreg is being provided by the specified load instruction. If possible,
69 /// try to fold the load as an operand to the instruction, returning true if
70 /// possible.
71 virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
72 const LoadInst *LI);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000073
Dan Gohman1adf1b02008-08-19 21:45:35 +000074#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000075
76private:
Dan Gohman46510a72010-04-15 01:51:59 +000077 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000078
Owen Andersone50ed302009-08-10 22:56:29 +000079 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000080
Chris Lattnerb44101c2011-04-19 05:09:50 +000081 bool X86FastEmitStore(EVT VT, const Value *Val, const X86AddressMode &AM);
82 bool X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000083
Owen Andersone50ed302009-08-10 22:56:29 +000084 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +000085 unsigned &ResultReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000086
Dan Gohman46510a72010-04-15 01:51:59 +000087 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
88 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman0586d912008-09-10 20:11:02 +000089
Dan Gohman46510a72010-04-15 01:51:59 +000090 bool X86SelectLoad(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000091
Dan Gohman46510a72010-04-15 01:51:59 +000092 bool X86SelectStore(const Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000093
Dan Gohman84023e02010-07-10 09:00:22 +000094 bool X86SelectRet(const Instruction *I);
95
Dan Gohman46510a72010-04-15 01:51:59 +000096 bool X86SelectCmp(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000097
Dan Gohman46510a72010-04-15 01:51:59 +000098 bool X86SelectZExt(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000099
Dan Gohman46510a72010-04-15 01:51:59 +0000100 bool X86SelectBranch(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000101
Dan Gohman46510a72010-04-15 01:51:59 +0000102 bool X86SelectShift(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000103
Dan Gohman46510a72010-04-15 01:51:59 +0000104 bool X86SelectSelect(const Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000105
Dan Gohman46510a72010-04-15 01:51:59 +0000106 bool X86SelectTrunc(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000107
Dan Gohman46510a72010-04-15 01:51:59 +0000108 bool X86SelectFPExt(const Instruction *I);
109 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohman78efce62008-09-10 21:02:08 +0000110
Dan Gohman46510a72010-04-15 01:51:59 +0000111 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
112 bool X86SelectCall(const Instruction *I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000113
Eli Friedman25255cb2011-06-10 23:39:36 +0000114 bool DoSelectCall(const Instruction *I, const char *MemIntName);
115
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000116 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000117 return getTargetMachine()->getInstrInfo();
118 }
119 const X86TargetMachine *getTargetMachine() const {
120 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000121 }
122
Dan Gohman46510a72010-04-15 01:51:59 +0000123 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000124
Dan Gohman46510a72010-04-15 01:51:59 +0000125 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000126
Eli Friedman2790ba82011-04-27 22:41:55 +0000127 unsigned TargetMaterializeFloatZero(const ConstantFP *CF);
128
Evan Chengf3d4efe2008-09-07 09:09:33 +0000129 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
130 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000131 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
133 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000134 }
135
Duncan Sands1440e8b2010-11-03 11:35:31 +0000136 bool isTypeLegal(const Type *Ty, MVT &VT, bool AllowI1 = false);
Eli Friedmand5089a92011-04-27 01:45:07 +0000137
Eli Friedmanc0883452011-05-20 22:21:04 +0000138 bool IsMemcpySmall(uint64_t Len);
139
Eli Friedmand5089a92011-04-27 01:45:07 +0000140 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
141 X86AddressMode SrcAM, uint64_t Len);
Evan Chengc3f44b02008-09-03 00:03:49 +0000142};
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000143
Chris Lattner087fcf32009-03-08 18:44:31 +0000144} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000145
Duncan Sands1440e8b2010-11-03 11:35:31 +0000146bool X86FastISel::isTypeLegal(const Type *Ty, MVT &VT, bool AllowI1) {
147 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
148 if (evt == MVT::Other || !evt.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000149 // Unhandled type. Halt "fast" selection and bail.
150 return false;
Duncan Sands1440e8b2010-11-03 11:35:31 +0000151
152 VT = evt.getSimpleVT();
Dan Gohman9b66d732008-09-30 00:48:39 +0000153 // For now, require SSE/SSE2 for performing floating-point operations,
154 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000155 if (VT == MVT::f64 && !X86ScalarSSEf64)
Dan Gohman9b66d732008-09-30 00:48:39 +0000156 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000157 if (VT == MVT::f32 && !X86ScalarSSEf32)
Dan Gohman9b66d732008-09-30 00:48:39 +0000158 return false;
159 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000161 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000162 // We only handle legal types. For example, on x86-32 the instruction
163 // selector contains all of the 64-bit instructions from x86-64,
164 // under the assumption that i64 won't be used if the target doesn't
165 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000166 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000167}
168
169#include "X86GenCallingConv.inc"
170
Evan Cheng0de588f2008-09-05 21:00:03 +0000171/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000172/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000173/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000174bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000175 unsigned &ResultReg) {
176 // Get opcode and regclass of the output for the given load instruction.
177 unsigned Opc = 0;
178 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000179 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000180 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000181 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000182 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000183 Opc = X86::MOV8rm;
184 RC = X86::GR8RegisterClass;
185 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000186 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000187 Opc = X86::MOV16rm;
188 RC = X86::GR16RegisterClass;
189 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000190 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000191 Opc = X86::MOV32rm;
192 RC = X86::GR32RegisterClass;
193 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000194 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000195 // Must be in x86-64 mode.
196 Opc = X86::MOV64rm;
197 RC = X86::GR64RegisterClass;
198 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000199 case MVT::f32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000200 if (Subtarget->hasSSE1()) {
201 Opc = X86::MOVSSrm;
202 RC = X86::FR32RegisterClass;
203 } else {
204 Opc = X86::LD_Fp32m;
205 RC = X86::RFP32RegisterClass;
206 }
207 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000208 case MVT::f64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000209 if (Subtarget->hasSSE2()) {
210 Opc = X86::MOVSDrm;
211 RC = X86::FR64RegisterClass;
212 } else {
213 Opc = X86::LD_Fp64m;
214 RC = X86::RFP64RegisterClass;
215 }
216 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000217 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000218 // No f80 support yet.
219 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000220 }
221
222 ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000223 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
224 DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000225 return true;
226}
227
Evan Chengf3d4efe2008-09-07 09:09:33 +0000228/// X86FastEmitStore - Emit a machine instruction to store a value Val of
229/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
230/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000231/// i.e. V. Return true if it is possible.
232bool
Chris Lattnerb44101c2011-04-19 05:09:50 +0000233X86FastISel::X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000234 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000235 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 switch (VT.getSimpleVT().SimpleTy) {
237 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000238 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000239 case MVT::i1: {
240 // Mask out all but lowest bit.
241 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000242 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000243 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
244 Val = AndResult;
245 }
246 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000247 case MVT::i8: Opc = X86::MOV8mr; break;
248 case MVT::i16: Opc = X86::MOV16mr; break;
249 case MVT::i32: Opc = X86::MOV32mr; break;
250 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
251 case MVT::f32:
Chris Lattner438949a2008-10-15 05:30:52 +0000252 Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000253 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000254 case MVT::f64:
Chris Lattner438949a2008-10-15 05:30:52 +0000255 Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000256 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000257 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000258
Dan Gohman84023e02010-07-10 09:00:22 +0000259 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
260 DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000261 return true;
262}
263
Dan Gohman46510a72010-04-15 01:51:59 +0000264bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000265 const X86AddressMode &AM) {
266 // Handle 'null' like i32/i64 0.
267 if (isa<ConstantPointerNull>(Val))
Owen Anderson1d0be152009-08-13 21:58:54 +0000268 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000269
Chris Lattner438949a2008-10-15 05:30:52 +0000270 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000271 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000272 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000273 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000275 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000276 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 case MVT::i8: Opc = X86::MOV8mi; break;
278 case MVT::i16: Opc = X86::MOV16mi; break;
279 case MVT::i32: Opc = X86::MOV32mi; break;
280 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000281 // Must be a 32-bit sign extended value.
282 if ((int)CI->getSExtValue() == CI->getSExtValue())
283 Opc = X86::MOV64mi32;
284 break;
285 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000286
Chris Lattner438949a2008-10-15 05:30:52 +0000287 if (Opc) {
Dan Gohman84023e02010-07-10 09:00:22 +0000288 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
289 DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000290 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000291 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000292 return true;
293 }
294 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000295
Chris Lattner438949a2008-10-15 05:30:52 +0000296 unsigned ValReg = getRegForValue(Val);
297 if (ValReg == 0)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000298 return false;
299
Chris Lattner438949a2008-10-15 05:30:52 +0000300 return X86FastEmitStore(VT, ValReg, AM);
301}
302
Evan Cheng24e3a902008-09-08 06:35:17 +0000303/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
304/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
305/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000306bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
307 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000308 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000309 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
310 Src, /*TODO: Kill=*/false);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000311
Owen Andersonac34a002008-09-11 19:44:55 +0000312 if (RR != 0) {
313 ResultReg = RR;
314 return true;
315 } else
316 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +0000317}
318
Dan Gohman0586d912008-09-10 20:11:02 +0000319/// X86SelectAddress - Attempt to fill in an address from the given value.
320///
Dan Gohman46510a72010-04-15 01:51:59 +0000321bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
322 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000323 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000324 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000325 // Don't walk into other basic blocks; it's possible we haven't
326 // visited them yet, so the instructions may not yet be assigned
327 // virtual registers.
Dan Gohman742bf872010-11-16 22:43:23 +0000328 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
329 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
330 Opcode = I->getOpcode();
331 U = I;
332 }
Dan Gohman46510a72010-04-15 01:51:59 +0000333 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000334 Opcode = C->getOpcode();
335 U = C;
336 }
Dan Gohman0586d912008-09-10 20:11:02 +0000337
Chris Lattner868ee942010-06-15 19:08:40 +0000338 if (const PointerType *Ty = dyn_cast<PointerType>(V->getType()))
339 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000340 // Fast instruction selection doesn't support the special
341 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000342 return false;
343
Dan Gohman35893082008-09-18 23:23:44 +0000344 switch (Opcode) {
345 default: break;
346 case Instruction::BitCast:
347 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000348 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000349
350 case Instruction::IntToPtr:
351 // Look past no-op inttoptrs.
352 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000353 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000354 break;
Dan Gohman35893082008-09-18 23:23:44 +0000355
356 case Instruction::PtrToInt:
357 // Look past no-op ptrtoints.
358 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000359 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000360 break;
Dan Gohman35893082008-09-18 23:23:44 +0000361
362 case Instruction::Alloca: {
363 // Do static allocas.
364 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000365 DenseMap<const AllocaInst*, int>::iterator SI =
366 FuncInfo.StaticAllocaMap.find(A);
367 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000368 AM.BaseType = X86AddressMode::FrameIndexBase;
369 AM.Base.FrameIndex = SI->second;
370 return true;
371 }
372 break;
Dan Gohman35893082008-09-18 23:23:44 +0000373 }
374
375 case Instruction::Add: {
376 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000377 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000378 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
379 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000380 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000381 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000382 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000383 }
Dan Gohman0586d912008-09-10 20:11:02 +0000384 }
Dan Gohman35893082008-09-18 23:23:44 +0000385 break;
386 }
387
388 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000389 X86AddressMode SavedAM = AM;
390
Dan Gohman35893082008-09-18 23:23:44 +0000391 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000392 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000393 unsigned IndexReg = AM.IndexReg;
394 unsigned Scale = AM.Scale;
395 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000396 // Iterate through the indices, folding what we can. Constants can be
397 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000398 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000399 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000400 const Value *Op = *i;
Dan Gohman35893082008-09-18 23:23:44 +0000401 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
402 const StructLayout *SL = TD.getStructLayout(STy);
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000403 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
404 continue;
405 }
Eric Christopher471e4222011-06-08 23:55:35 +0000406
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000407 // A array/variable index is always of the form i*S where S is the
408 // constant scale size. See if we can push the scale into immediates.
409 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
410 for (;;) {
411 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
412 // Constant-offset addressing.
413 Disp += CI->getSExtValue() * S;
414 break;
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000415 }
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000416 if (isa<AddOperator>(Op) &&
417 (!isa<Instruction>(Op) ||
418 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
419 == FuncInfo.MBB) &&
420 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
421 // An add (in the same block) with a constant operand. Fold the
422 // constant.
423 ConstantInt *CI =
424 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
425 Disp += CI->getSExtValue() * S;
426 // Iterate on the other operand.
427 Op = cast<AddOperator>(Op)->getOperand(0);
428 continue;
429 }
430 if (IndexReg == 0 &&
431 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
432 (S == 1 || S == 2 || S == 4 || S == 8)) {
433 // Scaled-index addressing.
434 Scale = S;
435 IndexReg = getRegForGEPIndex(Op).first;
436 if (IndexReg == 0)
437 return false;
438 break;
439 }
440 // Unsupported.
441 goto unsupported_gep;
Dan Gohman35893082008-09-18 23:23:44 +0000442 }
443 }
Dan Gohman09aae462008-09-26 20:04:15 +0000444 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000445 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000446 break;
Dan Gohman35893082008-09-18 23:23:44 +0000447 // Ok, the GEP indices were covered by constant-offset and scaled-index
448 // addressing. Update the address state and move on to examining the base.
449 AM.IndexReg = IndexReg;
450 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000451 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000452 if (X86SelectAddress(U->getOperand(0), AM))
453 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000454
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000455 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner225d4ca2010-03-04 19:48:19 +0000456 // our address and just match the value instead of completely failing.
457 AM = SavedAM;
458 break;
Dan Gohman35893082008-09-18 23:23:44 +0000459 unsupported_gep:
460 // Ok, the GEP indices weren't all covered.
461 break;
462 }
463 }
464
465 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000466 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0a1c9972011-04-17 17:47:38 +0000467 // Can't handle alternate code models or TLS yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000468 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000469 return false;
470
Dan Gohman46510a72010-04-15 01:51:59 +0000471 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000472 if (GVar->isThreadLocal())
473 return false;
Eric Christopher471e4222011-06-08 23:55:35 +0000474
Chris Lattner0a1c9972011-04-17 17:47:38 +0000475 // RIP-relative addresses can't have additional register operands, so if
476 // we've already folded stuff into the addressing mode, just force the
477 // global value into its own register, which we can use as the basereg.
478 if (!Subtarget->isPICStyleRIPRel() ||
479 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
480 // Okay, we've committed to selecting this global. Set up the address.
481 AM.GV = GV;
Dan Gohmane9865942009-02-23 22:03:08 +0000482
Chris Lattner0a1c9972011-04-17 17:47:38 +0000483 // Allow the subtarget to classify the global.
484 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000485
Chris Lattner0a1c9972011-04-17 17:47:38 +0000486 // If this reference is relative to the pic base, set it now.
487 if (isGlobalRelativeToPICBase(GVFlags)) {
488 // FIXME: How do we know Base.Reg is free??
489 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Dan Gohman7e8ef602008-09-19 23:42:04 +0000490 }
Chris Lattner0a1c9972011-04-17 17:47:38 +0000491
492 // Unless the ABI requires an extra load, return a direct reference to
493 // the global.
494 if (!isGlobalStubReference(GVFlags)) {
495 if (Subtarget->isPICStyleRIPRel()) {
496 // Use rip-relative addressing if we can. Above we verified that the
497 // base and index registers are unused.
498 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
499 AM.Base.Reg = X86::RIP;
500 }
501 AM.GVOpFlags = GVFlags;
502 return true;
503 }
504
505 // Ok, we need to do a load from a stub. If we've already loaded from
506 // this stub, reuse the loaded pointer, otherwise emit the load now.
507 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
508 unsigned LoadReg;
509 if (I != LocalValueMap.end() && I->second != 0) {
510 LoadReg = I->second;
511 } else {
512 // Issue load from stub.
513 unsigned Opc = 0;
514 const TargetRegisterClass *RC = NULL;
515 X86AddressMode StubAM;
516 StubAM.Base.Reg = AM.Base.Reg;
517 StubAM.GV = GV;
518 StubAM.GVOpFlags = GVFlags;
519
520 // Prepare for inserting code in the local-value area.
521 SavePoint SaveInsertPt = enterLocalValueArea();
522
523 if (TLI.getPointerTy() == MVT::i64) {
524 Opc = X86::MOV64rm;
525 RC = X86::GR64RegisterClass;
526
527 if (Subtarget->isPICStyleRIPRel())
528 StubAM.Base.Reg = X86::RIP;
529 } else {
530 Opc = X86::MOV32rm;
531 RC = X86::GR32RegisterClass;
532 }
533
534 LoadReg = createResultReg(RC);
535 MachineInstrBuilder LoadMI =
536 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
537 addFullAddress(LoadMI, StubAM);
538
539 // Ok, back to normal mode.
540 leaveLocalValueArea(SaveInsertPt);
541
542 // Prevent loading GV stub multiple times in same MBB.
543 LocalValueMap[V] = LoadReg;
544 }
545
546 // Now construct the final address. Note that the Disp, Scale,
547 // and Index values may already be set here.
548 AM.Base.Reg = LoadReg;
549 AM.GV = 0;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000550 return true;
551 }
Dan Gohman0586d912008-09-10 20:11:02 +0000552 }
553
Dan Gohman97135e12008-09-26 19:15:30 +0000554 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000555 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000556 if (AM.Base.Reg == 0) {
557 AM.Base.Reg = getRegForValue(V);
558 return AM.Base.Reg != 0;
559 }
560 if (AM.IndexReg == 0) {
561 assert(AM.Scale == 1 && "Scale with no index!");
562 AM.IndexReg = getRegForValue(V);
563 return AM.IndexReg != 0;
564 }
565 }
566
567 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000568}
569
Chris Lattner0aa43de2009-07-10 05:33:42 +0000570/// X86SelectCallAddress - Attempt to fill in an address from the given value.
571///
Dan Gohman46510a72010-04-15 01:51:59 +0000572bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
573 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000574 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000575 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000576 Opcode = I->getOpcode();
577 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000578 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000579 Opcode = C->getOpcode();
580 U = C;
581 }
582
583 switch (Opcode) {
584 default: break;
585 case Instruction::BitCast:
586 // Look past bitcasts.
587 return X86SelectCallAddress(U->getOperand(0), AM);
588
589 case Instruction::IntToPtr:
590 // Look past no-op inttoptrs.
591 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
592 return X86SelectCallAddress(U->getOperand(0), AM);
593 break;
594
595 case Instruction::PtrToInt:
596 // Look past no-op ptrtoints.
597 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
598 return X86SelectCallAddress(U->getOperand(0), AM);
599 break;
600 }
601
602 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000603 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000604 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000605 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000606 return false;
607
608 // RIP-relative addresses can't have additional register operands.
609 if (Subtarget->isPICStyleRIPRel() &&
610 (AM.Base.Reg != 0 || AM.IndexReg != 0))
611 return false;
612
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000613 // Can't handle DLLImport.
614 if (GV->hasDLLImportLinkage())
615 return false;
616
617 // Can't handle TLS.
Dan Gohman46510a72010-04-15 01:51:59 +0000618 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000619 if (GVar->isThreadLocal())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000620 return false;
621
622 // Okay, we've committed to selecting this global. Set up the basic address.
623 AM.GV = GV;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000624
Chris Lattnere6c07b52009-07-10 05:45:15 +0000625 // No ABI requires an extra load for anything other than DLLImport, which
626 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000627 if (Subtarget->isPICStyleRIPRel()) {
628 // Use rip-relative addressing if we can. Above we verified that the
629 // base and index registers are unused.
630 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
631 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000632 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000633 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
634 } else if (Subtarget->isPICStyleGOT()) {
635 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000636 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000637
Chris Lattner0aa43de2009-07-10 05:33:42 +0000638 return true;
639 }
640
641 // If all else fails, try to materialize the value in a register.
642 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
643 if (AM.Base.Reg == 0) {
644 AM.Base.Reg = getRegForValue(V);
645 return AM.Base.Reg != 0;
646 }
647 if (AM.IndexReg == 0) {
648 assert(AM.Scale == 1 && "Scale with no index!");
649 AM.IndexReg = getRegForValue(V);
650 return AM.IndexReg != 0;
651 }
652 }
653
654 return false;
655}
656
657
Owen Andersona3971df2008-09-04 07:08:58 +0000658/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000659bool X86FastISel::X86SelectStore(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000660 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000661 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000662 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000663
Dan Gohman0586d912008-09-10 20:11:02 +0000664 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000665 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000666 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000667
Chris Lattner438949a2008-10-15 05:30:52 +0000668 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000669}
670
Dan Gohman84023e02010-07-10 09:00:22 +0000671/// X86SelectRet - Select and emit code to implement ret instructions.
672bool X86FastISel::X86SelectRet(const Instruction *I) {
673 const ReturnInst *Ret = cast<ReturnInst>(I);
674 const Function &F = *I->getParent()->getParent();
675
676 if (!FuncInfo.CanLowerReturn)
677 return false;
678
679 CallingConv::ID CC = F.getCallingConv();
680 if (CC != CallingConv::C &&
681 CC != CallingConv::Fast &&
682 CC != CallingConv::X86_FastCall)
683 return false;
684
685 if (Subtarget->isTargetWin64())
686 return false;
687
688 // Don't handle popping bytes on return for now.
689 if (FuncInfo.MF->getInfo<X86MachineFunctionInfo>()
690 ->getBytesToPopOnReturn() != 0)
691 return 0;
692
693 // fastcc with -tailcallopt is intended to provide a guaranteed
694 // tail call optimization. Fastisel doesn't know how to do that.
695 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
696 return false;
697
698 // Let SDISel handle vararg functions.
699 if (F.isVarArg())
700 return false;
701
702 if (Ret->getNumOperands() > 0) {
703 SmallVector<ISD::OutputArg, 4> Outs;
704 GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
705 Outs, TLI);
706
707 // Analyze operands of the call, assigning locations to each operand.
708 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000709 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
710 I->getContext());
Duncan Sandse26032d2010-10-31 13:02:38 +0000711 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohman84023e02010-07-10 09:00:22 +0000712
713 const Value *RV = Ret->getOperand(0);
714 unsigned Reg = getRegForValue(RV);
715 if (Reg == 0)
716 return false;
717
718 // Only handle a single return value for now.
719 if (ValLocs.size() != 1)
720 return false;
721
722 CCValAssign &VA = ValLocs[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000723
Dan Gohman84023e02010-07-10 09:00:22 +0000724 // Don't bother handling odd stuff for now.
725 if (VA.getLocInfo() != CCValAssign::Full)
726 return false;
727 // Only handle register returns for now.
728 if (!VA.isRegLoc())
729 return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000730
731 // The calling-convention tables for x87 returns don't tell
732 // the whole story.
733 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
734 return false;
735
Eli Friedman22486c92011-05-18 23:13:10 +0000736 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedmandc515752011-05-19 22:16:13 +0000737 EVT SrcVT = TLI.getValueType(RV->getType());
738 EVT DstVT = VA.getValVT();
739 // Special handling for extended integers.
740 if (SrcVT != DstVT) {
741 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
742 return false;
743
744 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
745 return false;
746
747 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
748
749 if (SrcVT == MVT::i1) {
750 if (Outs[0].Flags.isSExt())
751 return false;
752 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
753 SrcVT = MVT::i8;
754 }
755 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
756 ISD::SIGN_EXTEND;
757 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
758 SrcReg, /*TODO: Kill=*/false);
759 }
760
761 // Make the copy.
Dan Gohman84023e02010-07-10 09:00:22 +0000762 unsigned DstReg = VA.getLocReg();
763 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000764 // Avoid a cross-class copy. This is very unlikely.
765 if (!SrcRC->contains(DstReg))
Dan Gohman84023e02010-07-10 09:00:22 +0000766 return false;
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000767 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
768 DstReg).addReg(SrcReg);
Dan Gohman84023e02010-07-10 09:00:22 +0000769
770 // Mark the register as live out of the function.
771 MRI.addLiveOut(VA.getLocReg());
772 }
773
774 // Now emit the RET.
775 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
776 return true;
777}
778
Evan Cheng8b19e562008-09-03 06:44:39 +0000779/// X86SelectLoad - Select and emit code to implement load instructions.
780///
Dan Gohman46510a72010-04-15 01:51:59 +0000781bool X86FastISel::X86SelectLoad(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000782 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000783 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000784 return false;
785
Dan Gohman0586d912008-09-10 20:11:02 +0000786 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000787 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000788 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000789
Evan Cheng0de588f2008-09-05 21:00:03 +0000790 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000791 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000792 UpdateValueMap(I, ResultReg);
793 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000794 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000795 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000796}
797
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000798static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000799 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000800 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000801 case MVT::i8: return X86::CMP8rr;
802 case MVT::i16: return X86::CMP16rr;
803 case MVT::i32: return X86::CMP32rr;
804 case MVT::i64: return X86::CMP64rr;
Dan Gohmanbe4d10d2010-07-12 15:46:30 +0000805 case MVT::f32: return Subtarget->hasSSE1() ? X86::UCOMISSrr : 0;
806 case MVT::f64: return Subtarget->hasSSE2() ? X86::UCOMISDrr : 0;
Dan Gohmand98d6202008-10-02 22:15:21 +0000807 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000808}
809
Chris Lattner0e13c782008-10-15 04:13:29 +0000810/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
811/// of the comparison, return an opcode that works for the compare (e.g.
812/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000813static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000814 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000815 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000816 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000817 case MVT::i8: return X86::CMP8ri;
818 case MVT::i16: return X86::CMP16ri;
819 case MVT::i32: return X86::CMP32ri;
820 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000821 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
822 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000823 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000824 return X86::CMP64ri32;
825 return 0;
826 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000827}
828
Dan Gohman46510a72010-04-15 01:51:59 +0000829bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
830 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000831 unsigned Op0Reg = getRegForValue(Op0);
832 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000833
Chris Lattnerd53886b2008-10-15 05:18:04 +0000834 // Handle 'null' like i32/i64 0.
835 if (isa<ConstantPointerNull>(Op1))
Owen Anderson1d0be152009-08-13 21:58:54 +0000836 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000837
Chris Lattner9a08a612008-10-15 04:26:38 +0000838 // We have two options: compare with register or immediate. If the RHS of
839 // the compare is an immediate that we can fold into this compare, use
840 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000841 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000842 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000843 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
844 .addReg(Op0Reg)
845 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000846 return true;
847 }
848 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000849
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000850 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattner9a08a612008-10-15 04:26:38 +0000851 if (CompareOpc == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000852
Chris Lattner9a08a612008-10-15 04:26:38 +0000853 unsigned Op1Reg = getRegForValue(Op1);
854 if (Op1Reg == 0) return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000855 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
856 .addReg(Op0Reg)
857 .addReg(Op1Reg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000858
Chris Lattner9a08a612008-10-15 04:26:38 +0000859 return true;
860}
861
Dan Gohman46510a72010-04-15 01:51:59 +0000862bool X86FastISel::X86SelectCmp(const Instruction *I) {
863 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000864
Duncan Sands1440e8b2010-11-03 11:35:31 +0000865 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000866 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000867 return false;
868
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000869 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000870 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000871 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000872 switch (CI->getPredicate()) {
873 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000874 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
875 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000876
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000877 unsigned EReg = createResultReg(&X86::GR8RegClass);
878 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000879 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
880 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
881 TII.get(X86::SETNPr), NPReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000882 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000883 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000884 UpdateValueMap(I, ResultReg);
885 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000886 }
887 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000888 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
889 return false;
890
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000891 unsigned NEReg = createResultReg(&X86::GR8RegClass);
892 unsigned PReg = createResultReg(&X86::GR8RegClass);
Chris Lattner90cb88a2011-04-19 04:22:17 +0000893 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETNEr), NEReg);
894 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETPr), PReg);
895 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::OR8rr),ResultReg)
Dan Gohman84023e02010-07-10 09:00:22 +0000896 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000897 UpdateValueMap(I, ResultReg);
898 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000899 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000900 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
901 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
902 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
903 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
904 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
905 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
906 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
907 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
908 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
909 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
910 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
911 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000912
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000913 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
914 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
915 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
916 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
917 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
918 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
919 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
920 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
921 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
922 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000923 default:
924 return false;
925 }
926
Dan Gohman46510a72010-04-15 01:51:59 +0000927 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000928 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000929 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000930
Chris Lattner9a08a612008-10-15 04:26:38 +0000931 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000932 if (!X86FastEmitCompare(Op0, Op1, VT))
933 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000934
Dan Gohman84023e02010-07-10 09:00:22 +0000935 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000936 UpdateValueMap(I, ResultReg);
937 return true;
938}
Evan Cheng8b19e562008-09-03 06:44:39 +0000939
Dan Gohman46510a72010-04-15 01:51:59 +0000940bool X86FastISel::X86SelectZExt(const Instruction *I) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000941 // Handle zero-extension from i1 to i8, which is common.
Eric Christopher471e4222011-06-08 23:55:35 +0000942 if (!I->getOperand(0)->getType()->isIntegerTy(1))
Eli Friedman76927d732011-05-25 23:49:02 +0000943 return false;
944
945 EVT DstVT = TLI.getValueType(I->getType());
946 if (!TLI.isTypeLegal(DstVT))
947 return false;
948
949 unsigned ResultReg = getRegForValue(I->getOperand(0));
950 if (ResultReg == 0)
951 return false;
952
953 // Set the high bits to zero.
954 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
955 if (ResultReg == 0)
956 return false;
957
958 if (DstVT != MVT::i8) {
959 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
960 ResultReg, /*Kill=*/true);
961 if (ResultReg == 0)
962 return false;
Dan Gohmand89ae992008-09-05 01:06:14 +0000963 }
964
Eli Friedman76927d732011-05-25 23:49:02 +0000965 UpdateValueMap(I, ResultReg);
966 return true;
Dan Gohmand89ae992008-09-05 01:06:14 +0000967}
968
Chris Lattner9a08a612008-10-15 04:26:38 +0000969
Dan Gohman46510a72010-04-15 01:51:59 +0000970bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +0000971 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +0000972 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +0000973 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +0000974 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
975 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +0000976
Dan Gohman8bef7442010-08-21 02:32:36 +0000977 // Fold the common case of a conditional branch with a comparison
978 // in the same block (values defined on other blocks may not have
979 // initialized registers).
Dan Gohman46510a72010-04-15 01:51:59 +0000980 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman8bef7442010-08-21 02:32:36 +0000981 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000982 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +0000983
Dan Gohmand98d6202008-10-02 22:15:21 +0000984 // Try to take advantage of fallthrough opportunities.
985 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohman84023e02010-07-10 09:00:22 +0000986 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000987 std::swap(TrueMBB, FalseMBB);
988 Predicate = CmpInst::getInversePredicate(Predicate);
989 }
990
Chris Lattner871d2462008-10-15 03:58:05 +0000991 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
992 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
993
Dan Gohmand98d6202008-10-02 22:15:21 +0000994 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +0000995 case CmpInst::FCMP_OEQ:
996 std::swap(TrueMBB, FalseMBB);
997 Predicate = CmpInst::FCMP_UNE;
998 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +0000999 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1000 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1001 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1002 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
1003 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
1004 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1005 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
1006 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
1007 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1008 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
1009 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
1010 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1011 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001012
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001013 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1014 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1015 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1016 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1017 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1018 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1019 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
1020 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1021 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1022 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +00001023 default:
1024 return false;
1025 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001026
Dan Gohman46510a72010-04-15 01:51:59 +00001027 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +00001028 if (SwapArgs)
1029 std::swap(Op0, Op1);
1030
Chris Lattner9a08a612008-10-15 04:26:38 +00001031 // Emit a compare of the LHS and RHS, setting the flags.
1032 if (!X86FastEmitCompare(Op0, Op1, VT))
1033 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001034
Dan Gohman84023e02010-07-10 09:00:22 +00001035 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1036 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001037
1038 if (Predicate == CmpInst::FCMP_UNE) {
1039 // X86 requires a second branch to handle UNE (and OEQ,
1040 // which is mapped to UNE above).
Dan Gohman84023e02010-07-10 09:00:22 +00001041 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1042 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001043 }
1044
Stuart Hastings3bf91252010-06-17 22:43:56 +00001045 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001046 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +00001047 return true;
1048 }
Chris Lattner90cb88a2011-04-19 04:22:17 +00001049 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1050 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1051 // typically happen for _Bool and C++ bools.
1052 MVT SourceVT;
1053 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1054 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1055 unsigned TestOpc = 0;
1056 switch (SourceVT.SimpleTy) {
1057 default: break;
1058 case MVT::i8: TestOpc = X86::TEST8ri; break;
1059 case MVT::i16: TestOpc = X86::TEST16ri; break;
1060 case MVT::i32: TestOpc = X86::TEST32ri; break;
1061 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1062 }
1063 if (TestOpc) {
1064 unsigned OpReg = getRegForValue(TI->getOperand(0));
1065 if (OpReg == 0) return false;
1066 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TestOpc))
1067 .addReg(OpReg).addImm(1);
Eric Christopher471e4222011-06-08 23:55:35 +00001068
Chris Lattnerc76d1212011-04-19 04:26:32 +00001069 unsigned JmpOpc = X86::JNE_4;
1070 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1071 std::swap(TrueMBB, FalseMBB);
1072 JmpOpc = X86::JE_4;
1073 }
Eric Christopher471e4222011-06-08 23:55:35 +00001074
Chris Lattnerc76d1212011-04-19 04:26:32 +00001075 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(JmpOpc))
Chris Lattner90cb88a2011-04-19 04:22:17 +00001076 .addMBB(TrueMBB);
1077 FastEmitBranch(FalseMBB, DL);
1078 FuncInfo.MBB->addSuccessor(TrueMBB);
1079 return true;
1080 }
1081 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001082 }
1083
1084 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman547eb4f2011-04-27 01:34:27 +00001085 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1086 // in an explicit cast, so make sure to handle that correctly.
Dan Gohmand98d6202008-10-02 22:15:21 +00001087 unsigned OpReg = getRegForValue(BI->getCondition());
1088 if (OpReg == 0) return false;
1089
Eli Friedman547eb4f2011-04-27 01:34:27 +00001090 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8ri))
1091 .addReg(OpReg).addImm(1);
Dan Gohman84023e02010-07-10 09:00:22 +00001092 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1093 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001094 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001095 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001096 return true;
1097}
1098
Dan Gohman46510a72010-04-15 01:51:59 +00001099bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner602fc062011-04-17 20:23:29 +00001100 unsigned CReg = 0, OpReg = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001101 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001102 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001103 CReg = X86::CL;
1104 RC = &X86::GR8RegClass;
1105 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001106 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1107 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1108 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001109 default: return false;
1110 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001111 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001112 CReg = X86::CX;
1113 RC = &X86::GR16RegClass;
1114 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001115 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1116 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1117 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001118 default: return false;
1119 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001120 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001121 CReg = X86::ECX;
1122 RC = &X86::GR32RegClass;
1123 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001124 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1125 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1126 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001127 default: return false;
1128 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001129 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001130 CReg = X86::RCX;
1131 RC = &X86::GR64RegClass;
1132 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001133 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1134 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1135 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001136 default: return false;
1137 }
1138 } else {
1139 return false;
1140 }
1141
Duncan Sands1440e8b2010-11-03 11:35:31 +00001142 MVT VT;
1143 if (!isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001144 return false;
1145
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001146 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1147 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001148
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001149 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1150 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001151 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1152 CReg).addReg(Op1Reg);
Dan Gohman145b8282008-10-07 21:50:36 +00001153
1154 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001155 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001156 if (CReg != X86::CL)
Dan Gohman84023e02010-07-10 09:00:22 +00001157 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1158 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001159 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001160
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001161 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001162 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1163 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001164 UpdateValueMap(I, ResultReg);
1165 return true;
1166}
1167
Dan Gohman46510a72010-04-15 01:51:59 +00001168bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001169 MVT VT;
1170 if (!isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001171 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001172
Eric Christophere487b012010-09-29 23:00:29 +00001173 // We only use cmov here, if we don't have a cmov instruction bail.
1174 if (!Subtarget->hasCMov()) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001175
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001176 unsigned Opc = 0;
1177 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001178 if (VT == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001179 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001180 RC = &X86::GR16RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001181 } else if (VT == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001182 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001183 RC = &X86::GR32RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001184 } else if (VT == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001185 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001186 RC = &X86::GR64RegClass;
1187 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001188 return false;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001189 }
1190
1191 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1192 if (Op0Reg == 0) return false;
1193 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1194 if (Op1Reg == 0) return false;
1195 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1196 if (Op2Reg == 0) return false;
1197
Dan Gohman84023e02010-07-10 09:00:22 +00001198 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1199 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001200 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001201 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1202 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001203 UpdateValueMap(I, ResultReg);
1204 return true;
1205}
1206
Dan Gohman46510a72010-04-15 01:51:59 +00001207bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001208 // fpext from float to double.
Owen Anderson1d0be152009-08-13 21:58:54 +00001209 if (Subtarget->hasSSE2() &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001210 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001211 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001212 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001213 unsigned OpReg = getRegForValue(V);
1214 if (OpReg == 0) return false;
1215 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001216 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1217 TII.get(X86::CVTSS2SDrr), ResultReg)
1218 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001219 UpdateValueMap(I, ResultReg);
1220 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001221 }
1222 }
1223
1224 return false;
1225}
1226
Dan Gohman46510a72010-04-15 01:51:59 +00001227bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Dan Gohman78efce62008-09-10 21:02:08 +00001228 if (Subtarget->hasSSE2()) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001229 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001230 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001231 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001232 unsigned OpReg = getRegForValue(V);
1233 if (OpReg == 0) return false;
1234 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001235 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1236 TII.get(X86::CVTSD2SSrr), ResultReg)
1237 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001238 UpdateValueMap(I, ResultReg);
1239 return true;
1240 }
1241 }
1242 }
1243
1244 return false;
1245}
1246
Dan Gohman46510a72010-04-15 01:51:59 +00001247bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001248 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1249 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001250
Eli Friedman76927d732011-05-25 23:49:02 +00001251 // This code only handles truncation to byte.
Owen Anderson825b72b2009-08-11 20:47:22 +00001252 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001253 return false;
Eli Friedman76927d732011-05-25 23:49:02 +00001254 if (!TLI.isTypeLegal(SrcVT))
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001255 return false;
1256
1257 unsigned InputReg = getRegForValue(I->getOperand(0));
1258 if (!InputReg)
1259 // Unhandled operand. Halt "fast" selection and bail.
1260 return false;
1261
Eli Friedman76927d732011-05-25 23:49:02 +00001262 if (SrcVT == MVT::i8) {
1263 // Truncate from i8 to i1; no code needed.
1264 UpdateValueMap(I, InputReg);
1265 return true;
1266 }
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001267
Eli Friedman76927d732011-05-25 23:49:02 +00001268 if (!Subtarget->is64Bit()) {
1269 // If we're on x86-32; we can't extract an i8 from a general register.
1270 // First issue a copy to GR16_ABCD or GR32_ABCD.
1271 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
1272 ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
1273 unsigned CopyReg = createResultReg(CopyRC);
1274 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1275 CopyReg).addReg(InputReg);
1276 InputReg = CopyReg;
1277 }
1278
1279 // Issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001280 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedman76927d732011-05-25 23:49:02 +00001281 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001282 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001283 if (!ResultReg)
1284 return false;
1285
1286 UpdateValueMap(I, ResultReg);
1287 return true;
1288}
1289
Eli Friedmanc0883452011-05-20 22:21:04 +00001290bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1291 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1292}
1293
Eli Friedmand5089a92011-04-27 01:45:07 +00001294bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1295 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedmanc0883452011-05-20 22:21:04 +00001296
Eli Friedmand5089a92011-04-27 01:45:07 +00001297 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedmanc0883452011-05-20 22:21:04 +00001298 if (!IsMemcpySmall(Len))
1299 return false;
1300
1301 bool i64Legal = Subtarget->is64Bit();
Eli Friedmand5089a92011-04-27 01:45:07 +00001302
1303 // We don't care about alignment here since we just emit integer accesses.
1304 while (Len) {
1305 MVT VT;
1306 if (Len >= 8 && i64Legal)
1307 VT = MVT::i64;
1308 else if (Len >= 4)
1309 VT = MVT::i32;
1310 else if (Len >= 2)
1311 VT = MVT::i16;
1312 else {
1313 assert(Len == 1);
1314 VT = MVT::i8;
1315 }
1316
1317 unsigned Reg;
1318 bool RV = X86FastEmitLoad(VT, SrcAM, Reg);
1319 RV &= X86FastEmitStore(VT, Reg, DestAM);
1320 assert(RV && "Failed to emit load or store??");
1321
1322 unsigned Size = VT.getSizeInBits()/8;
1323 Len -= Size;
1324 DestAM.Disp += Size;
1325 SrcAM.Disp += Size;
1326 }
1327
1328 return true;
1329}
1330
Dan Gohman46510a72010-04-15 01:51:59 +00001331bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001332 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001333 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001334 default: return false;
Chris Lattner832e4942011-04-19 05:52:03 +00001335 case Intrinsic::memcpy: {
1336 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1337 // Don't handle volatile or variable length memcpys.
Eli Friedman25255cb2011-06-10 23:39:36 +00001338 if (MCI.isVolatile())
Chris Lattner832e4942011-04-19 05:52:03 +00001339 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001340
Eli Friedman25255cb2011-06-10 23:39:36 +00001341 if (isa<ConstantInt>(MCI.getLength())) {
1342 // Small memcpy's are common enough that we want to do them
1343 // without a call if possible.
1344 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1345 if (IsMemcpySmall(Len)) {
1346 X86AddressMode DestAM, SrcAM;
1347 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1348 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1349 return false;
1350 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1351 return true;
1352 }
1353 }
Eric Christopher471e4222011-06-08 23:55:35 +00001354
Eli Friedman25255cb2011-06-10 23:39:36 +00001355 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1356 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner832e4942011-04-19 05:52:03 +00001357 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001358
Eli Friedman25255cb2011-06-10 23:39:36 +00001359 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1360 return false;
1361
1362 return DoSelectCall(&I, "memcpy");
Chris Lattner832e4942011-04-19 05:52:03 +00001363 }
Eli Friedman25255cb2011-06-10 23:39:36 +00001364 case Intrinsic::memset: {
1365 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher471e4222011-06-08 23:55:35 +00001366
Eli Friedman25255cb2011-06-10 23:39:36 +00001367 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1368 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1369 return false;
1370
1371 if (MSI.getDestAddressSpace() > 255)
1372 return false;
1373
1374 return DoSelectCall(&I, "memset");
1375 }
Eric Christopher07754c22010-03-18 20:27:26 +00001376 case Intrinsic::stackprotector: {
1377 // Emit code inline code to store the stack guard onto the stack.
1378 EVT PtrTy = TLI.getPointerTy();
1379
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001380 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1381 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001382
1383 // Grab the frame index.
1384 X86AddressMode AM;
1385 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher88dee302010-03-18 21:58:33 +00001386 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001387 return true;
1388 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001389 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001390 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001391 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001392 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001393 if (!X86SelectAddress(DI->getAddress(), AM))
1394 return false;
Chris Lattner518bb532010-02-09 19:54:29 +00001395 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001396 // FIXME may need to add RegState::Debug to any registers produced,
1397 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001398 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1399 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001400 return true;
1401 }
Eric Christopher77f79892010-01-18 22:11:29 +00001402 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001403 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001404 return true;
1405 }
Bill Wendling52370a12008-12-09 02:42:50 +00001406 case Intrinsic::sadd_with_overflow:
1407 case Intrinsic::uadd_with_overflow: {
Chris Lattner832e4942011-04-19 05:52:03 +00001408 // FIXME: Should fold immediates.
Eric Christopher471e4222011-06-08 23:55:35 +00001409
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001410 // Replace "add with overflow" intrinsics with an "add" instruction followed
Eli Friedman482feb32011-05-16 21:06:17 +00001411 // by a seto/setc instruction.
Bill Wendling52370a12008-12-09 02:42:50 +00001412 const Function *Callee = I.getCalledFunction();
1413 const Type *RetTy =
1414 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1415
Duncan Sands1440e8b2010-11-03 11:35:31 +00001416 MVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001417 if (!isTypeLegal(RetTy, VT))
1418 return false;
1419
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001420 const Value *Op1 = I.getArgOperand(0);
1421 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001422 unsigned Reg1 = getRegForValue(Op1);
1423 unsigned Reg2 = getRegForValue(Op2);
1424
1425 if (Reg1 == 0 || Reg2 == 0)
1426 // FIXME: Handle values *not* in registers.
1427 return false;
1428
1429 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001430 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001431 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001432 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001433 OpC = X86::ADD64rr;
1434 else
1435 return false;
1436
Eli Friedman482feb32011-05-16 21:06:17 +00001437 // The call to CreateRegs builds two sequential registers, to store the
1438 // both the the returned values.
1439 unsigned ResultReg = FuncInfo.CreateRegs(I.getType());
Dan Gohman84023e02010-07-10 09:00:22 +00001440 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1441 .addReg(Reg1).addReg(Reg2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001442
Chris Lattnera9a42252009-04-12 07:36:01 +00001443 unsigned Opc = X86::SETBr;
1444 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1445 Opc = X86::SETOr;
Eli Friedman482feb32011-05-16 21:06:17 +00001446 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg+1);
1447
1448 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling52370a12008-12-09 02:42:50 +00001449 return true;
1450 }
1451 }
1452}
1453
Dan Gohman46510a72010-04-15 01:51:59 +00001454bool X86FastISel::X86SelectCall(const Instruction *I) {
1455 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001456 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001457
1458 // Can't handle inline asm yet.
1459 if (isa<InlineAsm>(Callee))
1460 return false;
1461
Bill Wendling52370a12008-12-09 02:42:50 +00001462 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001463 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001464 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001465
Eli Friedman25255cb2011-06-10 23:39:36 +00001466 return DoSelectCall(I, 0);
1467}
1468
1469// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
1470bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
1471 const CallInst *CI = cast<CallInst>(I);
1472 const Value *Callee = CI->getCalledValue();
1473
Evan Chengf3d4efe2008-09-07 09:09:33 +00001474 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001475 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001476 CallingConv::ID CC = CS.getCallingConv();
Chris Lattnere03b8d32011-04-19 04:42:38 +00001477 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Evan Chengf3d4efe2008-09-07 09:09:33 +00001478 CC != CallingConv::X86_FastCall)
1479 return false;
1480
Evan Cheng381993f2010-01-27 00:00:57 +00001481 // fastcc with -tailcallopt is intended to provide a guaranteed
1482 // tail call optimization. Fastisel doesn't know how to do that.
Dan Gohman1797ed52010-02-08 20:27:50 +00001483 if (CC == CallingConv::Fast && GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001484 return false;
1485
Evan Chengf3d4efe2008-09-07 09:09:33 +00001486 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1487 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedman37620462011-04-19 17:22:22 +00001488 bool isVarArg = FTy->isVarArg();
1489
1490 // Don't know how to handle Win64 varargs yet. Nothing special needed for
1491 // x86-32. Special handling for x86-64 is implemented.
1492 if (isVarArg && Subtarget->isTargetWin64())
Evan Chengf3d4efe2008-09-07 09:09:33 +00001493 return false;
1494
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001495 // Fast-isel doesn't know about callee-pop yet.
Eli Friedman37620462011-04-19 17:22:22 +00001496 if (Subtarget->IsCalleePop(isVarArg, CC))
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001497 return false;
1498
Eli Friedman19515b42011-05-17 18:29:03 +00001499 // Check whether the function can return without sret-demotion.
1500 SmallVector<ISD::OutputArg, 4> Outs;
1501 SmallVector<uint64_t, 4> Offsets;
1502 GetReturnInfo(I->getType(), CS.getAttributes().getRetAttributes(),
1503 Outs, TLI, &Offsets);
1504 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Eric Christopher471e4222011-06-08 23:55:35 +00001505 *FuncInfo.MF, FTy->isVarArg(),
1506 Outs, FTy->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00001507 if (!CanLowerReturn)
Eli Friedmanc93943b2011-05-17 02:36:59 +00001508 return false;
1509
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001510 // Materialize callee address in a register. FIXME: GV address can be
1511 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001512 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001513 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001514 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001515 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001516 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001517 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001518 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001519 } else if (CalleeAM.Base.Reg != 0) {
1520 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001521 } else
1522 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001523
Evan Chengf3d4efe2008-09-07 09:09:33 +00001524 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001525 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001526 SmallVector<unsigned, 8> Args;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001527 SmallVector<MVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001528 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001529 Args.reserve(CS.arg_size());
Chris Lattner241ab472008-10-15 05:38:32 +00001530 ArgVals.reserve(CS.arg_size());
Evan Chengf3d4efe2008-09-07 09:09:33 +00001531 ArgVTs.reserve(CS.arg_size());
1532 ArgFlags.reserve(CS.arg_size());
Dan Gohman46510a72010-04-15 01:51:59 +00001533 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001534 i != e; ++i) {
Eli Friedman25255cb2011-06-10 23:39:36 +00001535 // If we're lowering a mem intrinsic instead of a regular call, skip the
1536 // last two arguments, which should not passed to the underlying functions.
1537 if (MemIntName && e-i <= 2)
1538 break;
Chris Lattnere03b8d32011-04-19 04:42:38 +00001539 Value *ArgVal = *i;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001540 ISD::ArgFlagsTy Flags;
1541 unsigned AttrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00001542 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001543 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00001544 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001545 Flags.setZExt();
1546
Eli Friedmanc0883452011-05-20 22:21:04 +00001547 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
1548 const PointerType *Ty = cast<PointerType>(ArgVal->getType());
1549 const Type *ElementTy = Ty->getElementType();
1550 unsigned FrameSize = TD.getTypeAllocSize(ElementTy);
1551 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
1552 if (!FrameAlign)
1553 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
1554 Flags.setByVal();
1555 Flags.setByValSize(FrameSize);
1556 Flags.setByValAlign(FrameAlign);
1557 if (!IsMemcpySmall(FrameSize))
1558 return false;
1559 }
1560
1561 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
1562 Flags.setInReg();
1563 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
1564 Flags.setNest();
1565
Chris Lattnere03b8d32011-04-19 04:42:38 +00001566 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1567 // instruction. This is safe because it is common to all fastisel supported
1568 // calling conventions on x86.
1569 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1570 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1571 CI->getBitWidth() == 16) {
1572 if (Flags.isSExt())
1573 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1574 else
1575 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1576 }
1577 }
Eric Christopher471e4222011-06-08 23:55:35 +00001578
Chris Lattnerb44101c2011-04-19 05:09:50 +00001579 unsigned ArgReg;
Eric Christopher471e4222011-06-08 23:55:35 +00001580
Chris Lattnerff009ad2011-04-19 05:15:59 +00001581 // Passing bools around ends up doing a trunc to i1 and passing it.
1582 // Codegen this as an argument + "and 1".
Chris Lattnerb44101c2011-04-19 05:09:50 +00001583 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
1584 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
1585 ArgVal->hasOneUse()) {
Chris Lattnerb44101c2011-04-19 05:09:50 +00001586 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
1587 ArgReg = getRegForValue(ArgVal);
1588 if (ArgReg == 0) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001589
Chris Lattnerb44101c2011-04-19 05:09:50 +00001590 MVT ArgVT;
1591 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001592
Chris Lattnerb44101c2011-04-19 05:09:50 +00001593 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
1594 ArgVal->hasOneUse(), 1);
1595 } else {
1596 ArgReg = getRegForValue(ArgVal);
Chris Lattnerb44101c2011-04-19 05:09:50 +00001597 }
Chris Lattnere03b8d32011-04-19 04:42:38 +00001598
Chris Lattnerff009ad2011-04-19 05:15:59 +00001599 if (ArgReg == 0) return false;
1600
Chris Lattnere03b8d32011-04-19 04:42:38 +00001601 const Type *ArgTy = ArgVal->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001602 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001603 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001604 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00001605 if (ArgVT == MVT::x86mmx)
1606 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001607 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1608 Flags.setOrigAlign(OriginalAlignment);
1609
Chris Lattnerb44101c2011-04-19 05:09:50 +00001610 Args.push_back(ArgReg);
Chris Lattnere03b8d32011-04-19 04:42:38 +00001611 ArgVals.push_back(ArgVal);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001612 ArgVTs.push_back(ArgVT);
1613 ArgFlags.push_back(Flags);
1614 }
1615
1616 // Analyze operands of the call, assigning locations to each operand.
1617 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001618 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
1619 I->getParent()->getContext());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001620
Dan Gohmand8acddd2010-06-01 21:09:47 +00001621 // Allocate shadow area for Win64
Chris Lattnere03b8d32011-04-19 04:42:38 +00001622 if (Subtarget->isTargetWin64())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001623 CCInfo.AllocateStack(32, 8);
Dan Gohmand8acddd2010-06-01 21:09:47 +00001624
Duncan Sands45907662010-10-31 13:21:44 +00001625 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001626
1627 // Get a count of how many bytes are to be pushed on the stack.
1628 unsigned NumBytes = CCInfo.getNextStackOffset();
1629
1630 // Issue CALLSEQ_START
Dan Gohman6d4b0522008-10-01 18:28:06 +00001631 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001632 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1633 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001634
Chris Lattner438949a2008-10-15 05:30:52 +00001635 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001636 // copies / loads.
1637 SmallVector<unsigned, 4> RegArgs;
1638 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1639 CCValAssign &VA = ArgLocs[i];
1640 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001641 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001642
Evan Chengf3d4efe2008-09-07 09:09:33 +00001643 // Promote the value if needed.
1644 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001645 default: llvm_unreachable("Unknown loc info!");
Evan Chengf3d4efe2008-09-07 09:09:33 +00001646 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001647 case CCValAssign::SExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001648 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1649 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001650 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1651 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001652 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001653 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001654 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001655 }
1656 case CCValAssign::ZExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001657 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1658 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001659 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1660 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001661 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001662 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001663 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001664 }
1665 case CCValAssign::AExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001666 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1667 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001668 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1669 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001670 if (!Emitted)
1671 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001672 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001673 if (!Emitted)
1674 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1675 Arg, ArgVT, Arg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001676
Chris Lattnerc46ec642011-01-05 22:26:52 +00001677 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001678 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001679 break;
1680 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001681 case CCValAssign::BCvt: {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001682 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001683 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001684 assert(BC != 0 && "Failed to emit a bitcast!");
1685 Arg = BC;
1686 ArgVT = VA.getLocVT();
1687 break;
1688 }
Evan Cheng24e3a902008-09-08 06:35:17 +00001689 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001690
Evan Chengf3d4efe2008-09-07 09:09:33 +00001691 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001692 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1693 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001694 RegArgs.push_back(VA.getLocReg());
1695 } else {
1696 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00001697 X86AddressMode AM;
1698 AM.Base.Reg = StackPtr;
1699 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00001700 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedmanc0883452011-05-20 22:21:04 +00001701 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001702
Eli Friedmanc0883452011-05-20 22:21:04 +00001703 if (Flags.isByVal()) {
1704 X86AddressMode SrcAM;
1705 SrcAM.Base.Reg = Arg;
1706 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
1707 assert(Res && "memcpy length already checked!"); (void)Res;
1708 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
1709 // If this is a really simple value, emit this with the Value* version
1710 //of X86FastEmitStore. If it isn't simple, we don't want to do this,
1711 // as it can cause us to reevaluate the argument.
Chris Lattner241ab472008-10-15 05:38:32 +00001712 X86FastEmitStore(ArgVT, ArgVal, AM);
Eli Friedmanc0883452011-05-20 22:21:04 +00001713 } else {
Chris Lattner241ab472008-10-15 05:38:32 +00001714 X86FastEmitStore(ArgVT, Arg, AM);
Eli Friedmanc0883452011-05-20 22:21:04 +00001715 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00001716 }
1717 }
1718
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001719 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001720 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00001721 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001722 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001723 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1724 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001725 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001726
Eli Friedman37620462011-04-19 17:22:22 +00001727 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64()) {
1728 // Count the number of XMM registers allocated.
1729 static const unsigned XMMArgRegs[] = {
1730 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1731 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1732 };
1733 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1734 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::MOV8ri),
1735 X86::AL).addImm(NumXMMRegs);
1736 }
1737
Evan Chengf3d4efe2008-09-07 09:09:33 +00001738 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00001739 MachineInstrBuilder MIB;
1740 if (CalleeOp) {
1741 // Register-indirect call.
Nate Begeman0c07b642010-07-22 00:09:39 +00001742 unsigned CallOpc;
1743 if (Subtarget->isTargetWin64())
1744 CallOpc = X86::WINCALL64r;
1745 else if (Subtarget->is64Bit())
1746 CallOpc = X86::CALL64r;
1747 else
1748 CallOpc = X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00001749 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1750 .addReg(CalleeOp);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001751
Chris Lattner51e8eab2009-07-09 06:34:26 +00001752 } else {
1753 // Direct call.
1754 assert(GV && "Not a direct call");
Nate Begeman0c07b642010-07-22 00:09:39 +00001755 unsigned CallOpc;
1756 if (Subtarget->isTargetWin64())
1757 CallOpc = X86::WINCALL64pcrel32;
1758 else if (Subtarget->is64Bit())
1759 CallOpc = X86::CALL64pcrel32;
1760 else
1761 CallOpc = X86::CALLpcrel32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001762
Chris Lattner51e8eab2009-07-09 06:34:26 +00001763 // See if we need any target-specific flags on the GV operand.
1764 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001765
Chris Lattner51e8eab2009-07-09 06:34:26 +00001766 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1767 // external symbols most go through the PLT in PIC mode. If the symbol
1768 // has hidden or protected visibility, or if it is static or local, then
1769 // we don't need to use the PLT - we can directly call it.
1770 if (Subtarget->isTargetELF() &&
1771 TM.getRelocationModel() == Reloc::PIC_ &&
1772 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1773 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001774 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00001775 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00001776 (!Subtarget->getTargetTriple().isMacOSX() ||
1777 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner51e8eab2009-07-09 06:34:26 +00001778 // PC-relative references to external symbols should go through $stub,
1779 // unless we're building with the leopard linker or later, which
1780 // automatically synthesizes these stubs.
1781 OpFlags = X86II::MO_DARWIN_STUB;
1782 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001783
1784
Eli Friedman25255cb2011-06-10 23:39:36 +00001785 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc));
1786 if (MemIntName)
1787 MIB.addExternalSymbol(MemIntName);
1788 else
1789 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00001790 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001791
1792 // Add an implicit use GOT pointer in EBX.
Chris Lattner15a380a2009-07-09 04:39:06 +00001793 if (Subtarget->isPICStyleGOT())
Dan Gohman2cc3aa42008-09-25 15:24:26 +00001794 MIB.addReg(X86::EBX);
1795
Eli Friedman37620462011-04-19 17:22:22 +00001796 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64())
1797 MIB.addReg(X86::AL);
1798
Evan Chengf3d4efe2008-09-07 09:09:33 +00001799 // Add implicit physical register uses to the call.
Dan Gohman8c3f8b62008-10-07 22:10:33 +00001800 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1801 MIB.addReg(RegArgs[i]);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001802
1803 // Issue CALLSEQ_END
Dan Gohman6d4b0522008-10-01 18:28:06 +00001804 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
Eli Friedmand227eed2011-04-28 20:19:12 +00001805 unsigned NumBytesCallee = 0;
1806 if (!Subtarget->is64Bit() && CS.paramHasAttr(1, Attribute::StructRet))
1807 NumBytesCallee = 4;
Dan Gohman84023e02010-07-10 09:00:22 +00001808 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
Eli Friedmand227eed2011-04-28 20:19:12 +00001809 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001810
Eli Friedman19515b42011-05-17 18:29:03 +00001811 // Build info for return calling conv lowering code.
1812 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
1813 SmallVector<ISD::InputArg, 32> Ins;
1814 SmallVector<EVT, 4> RetTys;
1815 ComputeValueVTs(TLI, I->getType(), RetTys);
1816 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
1817 EVT VT = RetTys[i];
1818 EVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
1819 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
1820 for (unsigned j = 0; j != NumRegs; ++j) {
1821 ISD::InputArg MyFlags;
1822 MyFlags.VT = RegisterVT.getSimpleVT();
1823 MyFlags.Used = !CS.getInstruction()->use_empty();
1824 if (CS.paramHasAttr(0, Attribute::SExt))
1825 MyFlags.Flags.setSExt();
1826 if (CS.paramHasAttr(0, Attribute::ZExt))
1827 MyFlags.Flags.setZExt();
1828 if (CS.paramHasAttr(0, Attribute::InReg))
1829 MyFlags.Flags.setInReg();
1830 Ins.push_back(MyFlags);
1831 }
1832 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00001833
Eli Friedman19515b42011-05-17 18:29:03 +00001834 // Now handle call return values.
1835 SmallVector<unsigned, 4> UsedRegs;
1836 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001837 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
1838 I->getParent()->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00001839 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
1840 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
1841 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1842 EVT CopyVT = RVLocs[i].getValVT();
1843 unsigned CopyReg = ResultReg + i;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001844
Evan Chengf3d4efe2008-09-07 09:09:33 +00001845 // If this is a call to a function that returns an fp value on the x87 fp
1846 // stack, but where we prefer to use the value in xmm registers, copy it
1847 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman19515b42011-05-17 18:29:03 +00001848 if ((RVLocs[i].getLocReg() == X86::ST0 ||
1849 RVLocs[i].getLocReg() == X86::ST1) &&
Evan Chengf3d4efe2008-09-07 09:09:33 +00001850 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001851 CopyVT = MVT::f80;
Eli Friedman19515b42011-05-17 18:29:03 +00001852 CopyReg = createResultReg(X86::RFP80RegisterClass);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001853 }
1854
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001855 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Eli Friedman19515b42011-05-17 18:29:03 +00001856 CopyReg).addReg(RVLocs[i].getLocReg());
1857 UsedRegs.push_back(RVLocs[i].getLocReg());
Dan Gohmandb497122010-06-18 23:28:01 +00001858
Eli Friedman19515b42011-05-17 18:29:03 +00001859 if (CopyVT != RVLocs[i].getValVT()) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001860 // Round the F80 the right size, which also moves to the appropriate xmm
1861 // register. This is accomplished by storing the F80 value in memory and
1862 // then loading it back. Ewww...
Eli Friedman19515b42011-05-17 18:29:03 +00001863 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00001864 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001865 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00001866 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00001867 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1868 TII.get(Opc)), FI)
Eli Friedman19515b42011-05-17 18:29:03 +00001869 .addReg(CopyReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001870 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Dan Gohman84023e02010-07-10 09:00:22 +00001871 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eli Friedman19515b42011-05-17 18:29:03 +00001872 TII.get(Opc), ResultReg + i), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001873 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00001874 }
Eli Friedmancdc9a202011-05-17 00:13:47 +00001875
Eli Friedman19515b42011-05-17 18:29:03 +00001876 if (RVLocs.size())
1877 UpdateValueMap(I, ResultReg, RVLocs.size());
1878
Dan Gohmandb497122010-06-18 23:28:01 +00001879 // Set all unused physreg defs as dead.
1880 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1881
Evan Chengf3d4efe2008-09-07 09:09:33 +00001882 return true;
1883}
1884
1885
Dan Gohman99b21822008-08-28 23:21:34 +00001886bool
Dan Gohman46510a72010-04-15 01:51:59 +00001887X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00001888 switch (I->getOpcode()) {
1889 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00001890 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00001891 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00001892 case Instruction::Store:
1893 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00001894 case Instruction::Ret:
1895 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001896 case Instruction::ICmp:
1897 case Instruction::FCmp:
1898 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00001899 case Instruction::ZExt:
1900 return X86SelectZExt(I);
1901 case Instruction::Br:
1902 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001903 case Instruction::Call:
1904 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001905 case Instruction::LShr:
1906 case Instruction::AShr:
1907 case Instruction::Shl:
1908 return X86SelectShift(I);
1909 case Instruction::Select:
1910 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001911 case Instruction::Trunc:
1912 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00001913 case Instruction::FPExt:
1914 return X86SelectFPExt(I);
1915 case Instruction::FPTrunc:
1916 return X86SelectFPTrunc(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00001917 case Instruction::IntToPtr: // Deliberate fall-through.
1918 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001919 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1920 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00001921 if (DstVT.bitsGT(SrcVT))
1922 return X86SelectZExt(I);
1923 if (DstVT.bitsLT(SrcVT))
1924 return X86SelectTrunc(I);
1925 unsigned Reg = getRegForValue(I->getOperand(0));
1926 if (Reg == 0) return false;
1927 UpdateValueMap(I, Reg);
1928 return true;
1929 }
Dan Gohman99b21822008-08-28 23:21:34 +00001930 }
1931
1932 return false;
1933}
1934
Dan Gohman46510a72010-04-15 01:51:59 +00001935unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001936 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001937 if (!isTypeLegal(C->getType(), VT))
Owen Anderson95267a12008-09-05 00:06:23 +00001938 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001939
Owen Anderson95267a12008-09-05 00:06:23 +00001940 // Get opcode and regclass of the output for the given load instruction.
1941 unsigned Opc = 0;
1942 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001943 switch (VT.SimpleTy) {
Owen Anderson95267a12008-09-05 00:06:23 +00001944 default: return false;
Owen Anderson825b72b2009-08-11 20:47:22 +00001945 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00001946 Opc = X86::MOV8rm;
1947 RC = X86::GR8RegisterClass;
1948 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001949 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00001950 Opc = X86::MOV16rm;
1951 RC = X86::GR16RegisterClass;
1952 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001953 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00001954 Opc = X86::MOV32rm;
1955 RC = X86::GR32RegisterClass;
1956 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001957 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00001958 // Must be in x86-64 mode.
1959 Opc = X86::MOV64rm;
1960 RC = X86::GR64RegisterClass;
1961 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001962 case MVT::f32:
Owen Anderson95267a12008-09-05 00:06:23 +00001963 if (Subtarget->hasSSE1()) {
1964 Opc = X86::MOVSSrm;
1965 RC = X86::FR32RegisterClass;
1966 } else {
1967 Opc = X86::LD_Fp32m;
1968 RC = X86::RFP32RegisterClass;
1969 }
1970 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001971 case MVT::f64:
Owen Anderson95267a12008-09-05 00:06:23 +00001972 if (Subtarget->hasSSE2()) {
1973 Opc = X86::MOVSDrm;
1974 RC = X86::FR64RegisterClass;
1975 } else {
1976 Opc = X86::LD_Fp64m;
1977 RC = X86::RFP64RegisterClass;
1978 }
1979 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001980 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00001981 // No f80 support yet.
1982 return false;
Owen Anderson95267a12008-09-05 00:06:23 +00001983 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001984
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001985 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00001986 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001987 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001988 if (X86SelectAddress(C, AM)) {
Chris Lattner685090f2011-04-17 17:12:08 +00001989 // If the expression is just a basereg, then we're done, otherwise we need
1990 // to emit an LEA.
1991 if (AM.BaseType == X86AddressMode::RegBase &&
1992 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
1993 return AM.Base.Reg;
Eric Christopher471e4222011-06-08 23:55:35 +00001994
Chris Lattner685090f2011-04-17 17:12:08 +00001995 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001996 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001997 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1998 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00001999 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002000 }
Evan Cheng0de588f2008-09-05 21:00:03 +00002001 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00002002 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002003
Owen Anderson3b217c62008-09-06 01:11:01 +00002004 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00002005 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002006 if (Align == 0) {
2007 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00002008 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002009 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002010
Dan Gohman5396c992008-09-30 01:21:32 +00002011 // x86-32 PIC requires a PIC base register for constant pools.
2012 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00002013 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00002014 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00002015 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00002016 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002017 } else if (Subtarget->isPICStyleGOT()) {
2018 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00002019 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002020 } else if (Subtarget->isPICStyleRIPRel() &&
2021 TM.getCodeModel() == CodeModel::Small) {
2022 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00002023 }
Dan Gohman5396c992008-09-30 01:21:32 +00002024
2025 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00002026 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002027 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002028 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2029 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00002030 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00002031
Owen Anderson95267a12008-09-05 00:06:23 +00002032 return ResultReg;
2033}
2034
Dan Gohman46510a72010-04-15 01:51:59 +00002035unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002036 // Fail on dynamic allocas. At this point, getRegForValue has already
2037 // checked its CSE maps, so if we're here trying to handle a dynamic
2038 // alloca, we're not going to succeed. X86SelectAddress has a
2039 // check for dynamic allocas, because it's called directly from
2040 // various places, but TargetMaterializeAlloca also needs a check
2041 // in order to avoid recursion between getRegForValue,
2042 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00002043 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002044 return 0;
2045
Dan Gohman0586d912008-09-10 20:11:02 +00002046 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002047 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00002048 return 0;
2049 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
2050 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
2051 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002052 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2053 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00002054 return ResultReg;
2055}
2056
Eli Friedman2790ba82011-04-27 22:41:55 +00002057unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2058 MVT VT;
2059 if (!isTypeLegal(CF->getType(), VT))
2060 return false;
2061
2062 // Get opcode and regclass for the given zero.
2063 unsigned Opc = 0;
2064 const TargetRegisterClass *RC = NULL;
2065 switch (VT.SimpleTy) {
2066 default: return false;
2067 case MVT::f32:
2068 if (Subtarget->hasSSE1()) {
2069 Opc = X86::FsFLD0SS;
2070 RC = X86::FR32RegisterClass;
2071 } else {
2072 Opc = X86::LD_Fp032;
2073 RC = X86::RFP32RegisterClass;
2074 }
2075 break;
2076 case MVT::f64:
2077 if (Subtarget->hasSSE2()) {
2078 Opc = X86::FsFLD0SD;
2079 RC = X86::FR64RegisterClass;
2080 } else {
2081 Opc = X86::LD_Fp064;
2082 RC = X86::RFP64RegisterClass;
2083 }
2084 break;
2085 case MVT::f80:
2086 // No f80 support yet.
2087 return false;
2088 }
2089
2090 unsigned ResultReg = createResultReg(RC);
2091 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
2092 return ResultReg;
2093}
2094
2095
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002096/// TryToFoldLoad - The specified machine instr operand is a vreg, and that
2097/// vreg is being provided by the specified load instruction. If possible,
2098/// try to fold the load as an operand to the instruction, returning true if
2099/// possible.
2100bool X86FastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
2101 const LoadInst *LI) {
2102 X86AddressMode AM;
2103 if (!X86SelectAddress(LI->getOperand(0), AM))
2104 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002105
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002106 X86InstrInfo &XII = (X86InstrInfo&)TII;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002107
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002108 unsigned Size = TD.getTypeAllocSize(LI->getType());
2109 unsigned Alignment = LI->getAlignment();
2110
2111 SmallVector<MachineOperand, 8> AddrOps;
2112 AM.getFullAddress(AddrOps);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002113
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002114 MachineInstr *Result =
2115 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
2116 if (Result == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002117
Chris Lattnerb99fdee2011-01-16 02:27:38 +00002118 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002119 MI->eraseFromParent();
2120 return true;
2121}
2122
2123
Evan Chengc3f44b02008-09-03 00:03:49 +00002124namespace llvm {
Dan Gohmana4160c32010-07-07 16:29:44 +00002125 llvm::FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo) {
2126 return new X86FastISel(funcInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00002127 }
Dan Gohman99b21822008-08-28 23:21:34 +00002128}