blob: 9c91e935f0b644f5cf00012def71af75d9405f8a [file] [log] [blame]
Dan Gohmandaef7f42008-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 Cheng3a0c5e52011-06-23 17:54:54 +000017#include "X86ISelLowering.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "X86InstrBuilder.h"
Evan Cheng8f23ec92008-09-03 01:04:47 +000019#include "X86RegisterInfo.h"
20#include "X86Subtarget.h"
Dan Gohman49e19e92008-08-22 00:20:26 +000021#include "X86TargetMachine.h"
Dan Gohmand7b5ce32010-07-10 09:00:22 +000022#include "llvm/CodeGen/Analysis.h"
Evan Cheng24422d42008-09-03 00:03:49 +000023#include "llvm/CodeGen/FastISel.h"
Dan Gohman87fb4e82010-07-07 16:29:44 +000024#include "llvm/CodeGen/FunctionLoweringInfo.h"
Owen Anderson50288e32008-09-05 00:06:23 +000025#include "llvm/CodeGen/MachineConstantPool.h"
Evan Cheng6c8f55c2008-09-07 09:09:33 +000026#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson0673a8a2008-08-29 17:45:56 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/CallingConv.h"
29#include "llvm/IR/DerivedTypes.h"
30#include "llvm/IR/GlobalAlias.h"
31#include "llvm/IR/GlobalVariable.h"
32#include "llvm/IR/Instructions.h"
33#include "llvm/IR/IntrinsicInst.h"
34#include "llvm/IR/Operator.h"
Evan Cheng6c8f55c2008-09-07 09:09:33 +000035#include "llvm/Support/CallSite.h"
Torok Edwin56d06592009-07-11 20:10:48 +000036#include "llvm/Support/ErrorHandling.h"
Dan Gohman6e005fd2008-09-18 23:23:44 +000037#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Chengd10089a2010-01-27 00:00:57 +000038#include "llvm/Target/TargetOptions.h"
Evan Cheng24422d42008-09-03 00:03:49 +000039using namespace llvm;
40
Chris Lattnerd5ac9d82009-03-08 18:44:31 +000041namespace {
Wesley Peck527da1b2010-11-23 03:31:01 +000042
Evan Cheng24422d42008-09-03 00:03:49 +000043class X86FastISel : public FastISel {
44 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
45 /// make the right decision when generating code for different targets.
46 const X86Subtarget *Subtarget;
Evan Cheng6c8f55c2008-09-07 09:09:33 +000047
Wesley Peck527da1b2010-11-23 03:31:01 +000048 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
Evan Cheng6c8f55c2008-09-07 09:09:33 +000049 /// floating point ops.
50 /// When SSE is available, use it for f32 operations.
51 /// When SSE2 is available, use it for f64 operations.
52 bool X86ScalarSSEf64;
53 bool X86ScalarSSEf32;
54
Evan Chenga41ee292008-09-03 06:44:39 +000055public:
Bob Wilson3e6fa462012-08-03 04:06:28 +000056 explicit X86FastISel(FunctionLoweringInfo &funcInfo,
57 const TargetLibraryInfo *libInfo)
58 : FastISel(funcInfo, libInfo) {
Evan Cheng8f23ec92008-09-03 01:04:47 +000059 Subtarget = &TM.getSubtarget<X86Subtarget>();
Craig Topperb0c0f722012-01-10 06:54:16 +000060 X86ScalarSSEf64 = Subtarget->hasSSE2();
61 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng8f23ec92008-09-03 01:04:47 +000062 }
Evan Cheng24422d42008-09-03 00:03:49 +000063
Dan Gohmanbcaf6812010-04-15 01:51:59 +000064 virtual bool TargetSelectInstruction(const Instruction *I);
Evan Cheng24422d42008-09-03 00:03:49 +000065
Eli Bendersky90dd3e72013-04-19 22:29:18 +000066 /// \brief The specified machine instr operand is a vreg, and that
Chris Lattnereeba0c72010-09-05 02:18:34 +000067 /// vreg is being provided by the specified load instruction. If possible,
68 /// try to fold the load as an operand to the instruction, returning true if
69 /// possible.
Eli Bendersky90dd3e72013-04-19 22:29:18 +000070 virtual bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
71 const LoadInst *LI);
Wesley Peck527da1b2010-11-23 03:31:01 +000072
Chad Rosiera92ef4b2013-02-25 21:59:35 +000073 virtual bool FastLowerArguments();
74
Dan Gohmandaef7f42008-08-19 21:45:35 +000075#include "X86GenFastISel.inc"
Evan Chenga41ee292008-09-03 06:44:39 +000076
77private:
Dan Gohmanbcaf6812010-04-15 01:51:59 +000078 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Wesley Peck527da1b2010-11-23 03:31:01 +000079
Owen Anderson53aa7a92009-08-10 22:56:29 +000080 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Chengf5bc7e52008-09-05 21:00:03 +000081
Chris Lattner5f4b7832011-04-19 05:09:50 +000082 bool X86FastEmitStore(EVT VT, const Value *Val, const X86AddressMode &AM);
83 bool X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM);
Evan Cheng6500d172008-09-08 06:35:17 +000084
Owen Anderson53aa7a92009-08-10 22:56:29 +000085 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng6500d172008-09-08 06:35:17 +000086 unsigned &ResultReg);
Wesley Peck527da1b2010-11-23 03:31:01 +000087
Dan Gohmanbcaf6812010-04-15 01:51:59 +000088 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
89 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman39d82f92008-09-10 20:11:02 +000090
Dan Gohmanbcaf6812010-04-15 01:51:59 +000091 bool X86SelectLoad(const Instruction *I);
Wesley Peck527da1b2010-11-23 03:31:01 +000092
Dan Gohmanbcaf6812010-04-15 01:51:59 +000093 bool X86SelectStore(const Instruction *I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +000094
Dan Gohmand7b5ce32010-07-10 09:00:22 +000095 bool X86SelectRet(const Instruction *I);
96
Dan Gohmanbcaf6812010-04-15 01:51:59 +000097 bool X86SelectCmp(const Instruction *I);
Dan Gohmana5753b32008-09-05 01:06:14 +000098
Dan Gohmanbcaf6812010-04-15 01:51:59 +000099 bool X86SelectZExt(const Instruction *I);
Dan Gohmana5753b32008-09-05 01:06:14 +0000100
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000101 bool X86SelectBranch(const Instruction *I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +0000102
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000103 bool X86SelectShift(const Instruction *I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +0000104
Eli Bendersky24a36eb2013-04-17 20:10:13 +0000105 bool X86SelectDivRem(const Instruction *I);
106
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000107 bool X86SelectSelect(const Instruction *I);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000108
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000109 bool X86SelectTrunc(const Instruction *I);
Wesley Peck527da1b2010-11-23 03:31:01 +0000110
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000111 bool X86SelectFPExt(const Instruction *I);
112 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohmanbf646f22008-09-10 21:02:08 +0000113
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000114 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
115 bool X86SelectCall(const Instruction *I);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000116
Eli Friedmancd2124a2011-06-10 23:39:36 +0000117 bool DoSelectCall(const Instruction *I, const char *MemIntName);
118
Dan Gohman3691d502008-09-25 15:24:26 +0000119 const X86InstrInfo *getInstrInfo() const {
Dan Gohman007a6bb2008-09-26 19:15:30 +0000120 return getTargetMachine()->getInstrInfo();
121 }
122 const X86TargetMachine *getTargetMachine() const {
123 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman3691d502008-09-25 15:24:26 +0000124 }
125
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000126 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman39d82f92008-09-10 20:11:02 +0000127
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000128 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000129
Eli Friedman406c4712011-04-27 22:41:55 +0000130 unsigned TargetMaterializeFloatZero(const ConstantFP *CF);
131
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000132 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
133 /// computed in an SSE register, not on the X87 floating point stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000134 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson9f944592009-08-11 20:47:22 +0000135 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
136 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000137 }
138
Chris Lattner229907c2011-07-18 04:54:35 +0000139 bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
Eli Friedmanbcc69142011-04-27 01:45:07 +0000140
Eli Friedman60afcc22011-05-20 22:21:04 +0000141 bool IsMemcpySmall(uint64_t Len);
142
Eli Friedmanbcc69142011-04-27 01:45:07 +0000143 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
144 X86AddressMode SrcAM, uint64_t Len);
Evan Cheng24422d42008-09-03 00:03:49 +0000145};
Wesley Peck527da1b2010-11-23 03:31:01 +0000146
Chris Lattnerd5ac9d82009-03-08 18:44:31 +0000147} // end anonymous namespace.
Dan Gohmand58f3e32008-08-28 23:21:34 +0000148
Chris Lattner229907c2011-07-18 04:54:35 +0000149bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000150 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
151 if (evt == MVT::Other || !evt.isSimple())
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000152 // Unhandled type. Halt "fast" selection and bail.
153 return false;
Duncan Sandsf5dda012010-11-03 11:35:31 +0000154
155 VT = evt.getSimpleVT();
Dan Gohman50331362008-09-30 00:48:39 +0000156 // For now, require SSE/SSE2 for performing floating-point operations,
157 // since x87 requires additional work.
Owen Anderson9f944592009-08-11 20:47:22 +0000158 if (VT == MVT::f64 && !X86ScalarSSEf64)
Craig Topper490c45c2012-08-11 17:53:00 +0000159 return false;
Owen Anderson9f944592009-08-11 20:47:22 +0000160 if (VT == MVT::f32 && !X86ScalarSSEf32)
Craig Topper490c45c2012-08-11 17:53:00 +0000161 return false;
Dan Gohman50331362008-09-30 00:48:39 +0000162 // Similarly, no f80 support yet.
Owen Anderson9f944592009-08-11 20:47:22 +0000163 if (VT == MVT::f80)
Dan Gohman50331362008-09-30 00:48:39 +0000164 return false;
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000165 // We only handle legal types. For example, on x86-32 the instruction
166 // selector contains all of the 64-bit instructions from x86-64,
167 // under the assumption that i64 won't be used if the target doesn't
168 // support it.
Owen Anderson9f944592009-08-11 20:47:22 +0000169 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000170}
171
172#include "X86GenCallingConv.inc"
173
Evan Chengf5bc7e52008-09-05 21:00:03 +0000174/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000175/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000176/// Return true and the result register by reference if it is possible.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000177bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Chengf5bc7e52008-09-05 21:00:03 +0000178 unsigned &ResultReg) {
179 // Get opcode and regclass of the output for the given load instruction.
180 unsigned Opc = 0;
181 const TargetRegisterClass *RC = NULL;
Owen Anderson9f944592009-08-11 20:47:22 +0000182 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000183 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000184 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +0000185 case MVT::i8:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000186 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +0000187 RC = &X86::GR8RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000188 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000189 case MVT::i16:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000190 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +0000191 RC = &X86::GR16RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000192 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000193 case MVT::i32:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000194 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +0000195 RC = &X86::GR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000196 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000197 case MVT::i64:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000198 // Must be in x86-64 mode.
199 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +0000200 RC = &X86::GR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000201 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000202 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000203 if (X86ScalarSSEf32) {
204 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +0000205 RC = &X86::FR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000206 } else {
207 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +0000208 RC = &X86::RFP32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000209 }
210 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000211 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000212 if (X86ScalarSSEf64) {
213 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +0000214 RC = &X86::FR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000215 } else {
216 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +0000217 RC = &X86::RFP64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000218 }
219 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000220 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +0000221 // No f80 support yet.
222 return false;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000223 }
224
225 ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000226 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
227 DL, TII.get(Opc), ResultReg), AM);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000228 return true;
229}
230
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000231/// X86FastEmitStore - Emit a machine instruction to store a value Val of
232/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
233/// and a displacement offset, or a GlobalAddress,
Evan Chengf5bc7e52008-09-05 21:00:03 +0000234/// i.e. V. Return true if it is possible.
235bool
Chris Lattner5f4b7832011-04-19 05:09:50 +0000236X86FastISel::X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM) {
Dan Gohman8f658ba2008-09-08 16:31:35 +0000237 // Get opcode and regclass of the output for the given store instruction.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000238 unsigned Opc = 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000239 switch (VT.getSimpleVT().SimpleTy) {
240 case MVT::f80: // No f80 support yet.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000241 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000242 case MVT::i1: {
243 // Mask out all but lowest bit.
Craig Topperabadc662012-04-20 06:31:50 +0000244 unsigned AndResult = createResultReg(&X86::GR8RegClass);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000245 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000246 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
247 Val = AndResult;
248 }
249 // FALLTHROUGH, handling i1 as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000250 case MVT::i8: Opc = X86::MOV8mr; break;
251 case MVT::i16: Opc = X86::MOV16mr; break;
252 case MVT::i32: Opc = X86::MOV32mr; break;
253 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
254 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000255 Opc = X86ScalarSSEf32 ?
256 (Subtarget->hasAVX() ? X86::VMOVSSmr : X86::MOVSSmr) : X86::ST_Fp32m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000257 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000258 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000259 Opc = X86ScalarSSEf64 ?
260 (Subtarget->hasAVX() ? X86::VMOVSDmr : X86::MOVSDmr) : X86::ST_Fp64m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000261 break;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000262 case MVT::v4f32:
263 Opc = X86::MOVAPSmr;
264 break;
265 case MVT::v2f64:
266 Opc = X86::MOVAPDmr;
267 break;
268 case MVT::v4i32:
269 case MVT::v2i64:
270 case MVT::v8i16:
271 case MVT::v16i8:
272 Opc = X86::MOVDQAmr;
273 break;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000274 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000275
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000276 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
277 DL, TII.get(Opc)), AM).addReg(Val);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000278 return true;
279}
280
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000281bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner3ba29352008-10-15 05:30:52 +0000282 const X86AddressMode &AM) {
283 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +0000284 if (isa<ConstantPointerNull>(Val))
285 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +0000286
Chris Lattner3ba29352008-10-15 05:30:52 +0000287 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000288 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000289 unsigned Opc = 0;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000290 bool Signed = true;
Owen Anderson9f944592009-08-11 20:47:22 +0000291 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000292 default: break;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000293 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000294 case MVT::i8: Opc = X86::MOV8mi; break;
295 case MVT::i16: Opc = X86::MOV16mi; break;
296 case MVT::i32: Opc = X86::MOV32mi; break;
297 case MVT::i64:
Chris Lattner3ba29352008-10-15 05:30:52 +0000298 // Must be a 32-bit sign extended value.
Jakub Staszak11d1aee2012-11-15 19:05:23 +0000299 if (isInt<32>(CI->getSExtValue()))
Chris Lattner3ba29352008-10-15 05:30:52 +0000300 Opc = X86::MOV64mi32;
301 break;
302 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000303
Chris Lattner3ba29352008-10-15 05:30:52 +0000304 if (Opc) {
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000305 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
306 DL, TII.get(Opc)), AM)
John McCall796583e2010-04-06 23:35:53 +0000307 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000308 CI->getZExtValue());
Chris Lattner3ba29352008-10-15 05:30:52 +0000309 return true;
310 }
311 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000312
Chris Lattner3ba29352008-10-15 05:30:52 +0000313 unsigned ValReg = getRegForValue(Val);
314 if (ValReg == 0)
Wesley Peck527da1b2010-11-23 03:31:01 +0000315 return false;
316
Chris Lattner3ba29352008-10-15 05:30:52 +0000317 return X86FastEmitStore(VT, ValReg, AM);
318}
319
Evan Cheng6500d172008-09-08 06:35:17 +0000320/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
321/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
322/// ISD::SIGN_EXTEND).
Owen Anderson53aa7a92009-08-10 22:56:29 +0000323bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
324 unsigned Src, EVT SrcVT,
Evan Cheng6500d172008-09-08 06:35:17 +0000325 unsigned &ResultReg) {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000326 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
327 Src, /*TODO: Kill=*/false);
Jakub Staszak701cc972013-02-14 21:50:09 +0000328 if (RR == 0)
Owen Anderson453564b2008-09-11 19:44:55 +0000329 return false;
Jakub Staszak701cc972013-02-14 21:50:09 +0000330
331 ResultReg = RR;
332 return true;
Evan Cheng6500d172008-09-08 06:35:17 +0000333}
334
Dan Gohman39d82f92008-09-10 20:11:02 +0000335/// X86SelectAddress - Attempt to fill in an address from the given value.
336///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000337bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
338 const User *U = NULL;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000339 unsigned Opcode = Instruction::UserOp1;
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000340 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanaf4903d2010-06-18 20:44:47 +0000341 // Don't walk into other basic blocks; it's possible we haven't
342 // visited them yet, so the instructions may not yet be assigned
343 // virtual registers.
Dan Gohmanaeb5e662010-11-16 22:43:23 +0000344 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
345 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
346 Opcode = I->getOpcode();
347 U = I;
348 }
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000349 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman6e005fd2008-09-18 23:23:44 +0000350 Opcode = C->getOpcode();
351 U = C;
352 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000353
Chris Lattner229907c2011-07-18 04:54:35 +0000354 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
Chris Lattner874c92b2010-06-15 19:08:40 +0000355 if (Ty->getAddressSpace() > 255)
Dan Gohmana46d6072010-06-18 20:45:41 +0000356 // Fast instruction selection doesn't support the special
357 // address spaces.
Chris Lattner874c92b2010-06-15 19:08:40 +0000358 return false;
359
Dan Gohman6e005fd2008-09-18 23:23:44 +0000360 switch (Opcode) {
361 default: break;
362 case Instruction::BitCast:
363 // Look past bitcasts.
Chris Lattner8212d372009-07-10 05:33:42 +0000364 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman6e005fd2008-09-18 23:23:44 +0000365
366 case Instruction::IntToPtr:
367 // Look past no-op inttoptrs.
368 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000369 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000370 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000371
372 case Instruction::PtrToInt:
373 // Look past no-op ptrtoints.
374 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000375 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000376 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000377
378 case Instruction::Alloca: {
379 // Do static allocas.
380 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman87fb4e82010-07-07 16:29:44 +0000381 DenseMap<const AllocaInst*, int>::iterator SI =
382 FuncInfo.StaticAllocaMap.find(A);
383 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman007a6bb2008-09-26 19:15:30 +0000384 AM.BaseType = X86AddressMode::FrameIndexBase;
385 AM.Base.FrameIndex = SI->second;
386 return true;
387 }
388 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000389 }
390
391 case Instruction::Add: {
392 // Adds of constants are common and easy enough.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000393 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman2564b902008-09-26 20:04:15 +0000394 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
395 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000396 if (isInt<32>(Disp)) {
Dan Gohman2564b902008-09-26 20:04:15 +0000397 AM.Disp = (uint32_t)Disp;
Chris Lattner8212d372009-07-10 05:33:42 +0000398 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman2564b902008-09-26 20:04:15 +0000399 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000400 }
Dan Gohman6e005fd2008-09-18 23:23:44 +0000401 break;
402 }
403
404 case Instruction::GetElementPtr: {
Chris Lattner795667b2010-03-04 19:54:45 +0000405 X86AddressMode SavedAM = AM;
406
Dan Gohman6e005fd2008-09-18 23:23:44 +0000407 // Pattern-match simple GEPs.
Dan Gohman2564b902008-09-26 20:04:15 +0000408 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000409 unsigned IndexReg = AM.IndexReg;
410 unsigned Scale = AM.Scale;
411 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohman4c315242008-12-08 07:57:47 +0000412 // Iterate through the indices, folding what we can. Constants can be
413 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000414 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman6e005fd2008-09-18 23:23:44 +0000415 i != e; ++i, ++GTI) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000416 const Value *Op = *i;
Chris Lattner229907c2011-07-18 04:54:35 +0000417 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Dan Gohman6e005fd2008-09-18 23:23:44 +0000418 const StructLayout *SL = TD.getStructLayout(STy);
Chris Lattner4b026b92011-04-17 17:05:12 +0000419 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
420 continue;
421 }
Eric Christopher0713a9d2011-06-08 23:55:35 +0000422
Chris Lattner4b026b92011-04-17 17:05:12 +0000423 // A array/variable index is always of the form i*S where S is the
424 // constant scale size. See if we can push the scale into immediates.
425 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
426 for (;;) {
427 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
428 // Constant-offset addressing.
429 Disp += CI->getSExtValue() * S;
430 break;
Dan Gohmanc1783b32011-03-22 00:04:35 +0000431 }
Chris Lattner4b026b92011-04-17 17:05:12 +0000432 if (isa<AddOperator>(Op) &&
433 (!isa<Instruction>(Op) ||
434 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
435 == FuncInfo.MBB) &&
436 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
437 // An add (in the same block) with a constant operand. Fold the
438 // constant.
439 ConstantInt *CI =
440 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
441 Disp += CI->getSExtValue() * S;
442 // Iterate on the other operand.
443 Op = cast<AddOperator>(Op)->getOperand(0);
444 continue;
445 }
446 if (IndexReg == 0 &&
447 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
448 (S == 1 || S == 2 || S == 4 || S == 8)) {
449 // Scaled-index addressing.
450 Scale = S;
451 IndexReg = getRegForGEPIndex(Op).first;
452 if (IndexReg == 0)
453 return false;
454 break;
455 }
456 // Unsupported.
457 goto unsupported_gep;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000458 }
459 }
Dan Gohman2564b902008-09-26 20:04:15 +0000460 // Check for displacement overflow.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000461 if (!isInt<32>(Disp))
Dan Gohman2564b902008-09-26 20:04:15 +0000462 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000463 // Ok, the GEP indices were covered by constant-offset and scaled-index
464 // addressing. Update the address state and move on to examining the base.
465 AM.IndexReg = IndexReg;
466 AM.Scale = Scale;
Dan Gohman2564b902008-09-26 20:04:15 +0000467 AM.Disp = (uint32_t)Disp;
Chris Lattner6ce8e242010-03-04 19:48:19 +0000468 if (X86SelectAddress(U->getOperand(0), AM))
469 return true;
Wesley Peck527da1b2010-11-23 03:31:01 +0000470
Chris Lattner4b026b92011-04-17 17:05:12 +0000471 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner6ce8e242010-03-04 19:48:19 +0000472 // our address and just match the value instead of completely failing.
473 AM = SavedAM;
474 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000475 unsupported_gep:
476 // Ok, the GEP indices weren't all covered.
477 break;
478 }
479 }
480
481 // Handle constant address.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000482 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Eli Friedman87c844c2011-09-22 23:41:28 +0000483 // Can't handle alternate code models yet.
Chris Lattner25e7f912009-07-10 21:03:06 +0000484 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman3691d502008-09-25 15:24:26 +0000485 return false;
486
Eli Friedman87c844c2011-09-22 23:41:28 +0000487 // Can't handle TLS yet.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000488 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohman318d7372009-02-23 22:03:08 +0000489 if (GVar->isThreadLocal())
490 return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000491
Eli Friedman87c844c2011-09-22 23:41:28 +0000492 // Can't handle TLS yet, part 2 (this is slightly crazy, but this is how
493 // it works...).
494 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
495 if (const GlobalVariable *GVar =
496 dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false)))
497 if (GVar->isThreadLocal())
498 return false;
499
Chris Lattnereb729d42011-04-17 17:47:38 +0000500 // RIP-relative addresses can't have additional register operands, so if
501 // we've already folded stuff into the addressing mode, just force the
502 // global value into its own register, which we can use as the basereg.
503 if (!Subtarget->isPICStyleRIPRel() ||
504 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
505 // Okay, we've committed to selecting this global. Set up the address.
506 AM.GV = GV;
Dan Gohman318d7372009-02-23 22:03:08 +0000507
Chris Lattnereb729d42011-04-17 17:47:38 +0000508 // Allow the subtarget to classify the global.
509 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
Wesley Peck527da1b2010-11-23 03:31:01 +0000510
Chris Lattnereb729d42011-04-17 17:47:38 +0000511 // If this reference is relative to the pic base, set it now.
512 if (isGlobalRelativeToPICBase(GVFlags)) {
513 // FIXME: How do we know Base.Reg is free??
514 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Dan Gohman098786e2008-09-19 23:42:04 +0000515 }
Chris Lattnereb729d42011-04-17 17:47:38 +0000516
517 // Unless the ABI requires an extra load, return a direct reference to
518 // the global.
519 if (!isGlobalStubReference(GVFlags)) {
520 if (Subtarget->isPICStyleRIPRel()) {
521 // Use rip-relative addressing if we can. Above we verified that the
522 // base and index registers are unused.
523 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
524 AM.Base.Reg = X86::RIP;
525 }
526 AM.GVOpFlags = GVFlags;
527 return true;
528 }
529
530 // Ok, we need to do a load from a stub. If we've already loaded from
531 // this stub, reuse the loaded pointer, otherwise emit the load now.
532 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
533 unsigned LoadReg;
534 if (I != LocalValueMap.end() && I->second != 0) {
535 LoadReg = I->second;
536 } else {
537 // Issue load from stub.
538 unsigned Opc = 0;
539 const TargetRegisterClass *RC = NULL;
540 X86AddressMode StubAM;
541 StubAM.Base.Reg = AM.Base.Reg;
542 StubAM.GV = GV;
543 StubAM.GVOpFlags = GVFlags;
544
545 // Prepare for inserting code in the local-value area.
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000546 SavePoint SaveInsertPt = enterLocalValueArea();
Chris Lattnereb729d42011-04-17 17:47:38 +0000547
548 if (TLI.getPointerTy() == MVT::i64) {
549 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +0000550 RC = &X86::GR64RegClass;
Chris Lattnereb729d42011-04-17 17:47:38 +0000551
552 if (Subtarget->isPICStyleRIPRel())
553 StubAM.Base.Reg = X86::RIP;
554 } else {
555 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +0000556 RC = &X86::GR32RegClass;
Chris Lattnereb729d42011-04-17 17:47:38 +0000557 }
558
559 LoadReg = createResultReg(RC);
560 MachineInstrBuilder LoadMI =
561 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
562 addFullAddress(LoadMI, StubAM);
563
564 // Ok, back to normal mode.
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000565 leaveLocalValueArea(SaveInsertPt);
Chris Lattnereb729d42011-04-17 17:47:38 +0000566
567 // Prevent loading GV stub multiple times in same MBB.
568 LocalValueMap[V] = LoadReg;
569 }
570
571 // Now construct the final address. Note that the Disp, Scale,
572 // and Index values may already be set here.
573 AM.Base.Reg = LoadReg;
574 AM.GV = 0;
Chris Lattner770e0422009-07-09 06:41:35 +0000575 return true;
576 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000577 }
578
Dan Gohman007a6bb2008-09-26 19:15:30 +0000579 // If all else fails, try to materialize the value in a register.
Chris Lattnercce15892009-06-27 05:24:12 +0000580 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman007a6bb2008-09-26 19:15:30 +0000581 if (AM.Base.Reg == 0) {
582 AM.Base.Reg = getRegForValue(V);
583 return AM.Base.Reg != 0;
584 }
585 if (AM.IndexReg == 0) {
586 assert(AM.Scale == 1 && "Scale with no index!");
587 AM.IndexReg = getRegForValue(V);
588 return AM.IndexReg != 0;
589 }
590 }
591
592 return false;
Dan Gohman39d82f92008-09-10 20:11:02 +0000593}
594
Chris Lattner8212d372009-07-10 05:33:42 +0000595/// X86SelectCallAddress - Attempt to fill in an address from the given value.
596///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000597bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
598 const User *U = NULL;
Chris Lattner8212d372009-07-10 05:33:42 +0000599 unsigned Opcode = Instruction::UserOp1;
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000600 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000601 Opcode = I->getOpcode();
602 U = I;
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000603 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000604 Opcode = C->getOpcode();
605 U = C;
606 }
607
608 switch (Opcode) {
609 default: break;
610 case Instruction::BitCast:
611 // Look past bitcasts.
612 return X86SelectCallAddress(U->getOperand(0), AM);
613
614 case Instruction::IntToPtr:
615 // Look past no-op inttoptrs.
616 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
617 return X86SelectCallAddress(U->getOperand(0), AM);
618 break;
619
620 case Instruction::PtrToInt:
621 // Look past no-op ptrtoints.
622 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
623 return X86SelectCallAddress(U->getOperand(0), AM);
624 break;
625 }
626
627 // Handle constant address.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000628 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000629 // Can't handle alternate code models yet.
Chris Lattner25e7f912009-07-10 21:03:06 +0000630 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner8212d372009-07-10 05:33:42 +0000631 return false;
632
633 // RIP-relative addresses can't have additional register operands.
634 if (Subtarget->isPICStyleRIPRel() &&
635 (AM.Base.Reg != 0 || AM.IndexReg != 0))
636 return false;
637
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000638 // Can't handle DLLImport.
639 if (GV->hasDLLImportLinkage())
640 return false;
641
642 // Can't handle TLS.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000643 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000644 if (GVar->isThreadLocal())
Chris Lattner8212d372009-07-10 05:33:42 +0000645 return false;
646
647 // Okay, we've committed to selecting this global. Set up the basic address.
648 AM.GV = GV;
Wesley Peck527da1b2010-11-23 03:31:01 +0000649
Chris Lattner7277a802009-07-10 05:45:15 +0000650 // No ABI requires an extra load for anything other than DLLImport, which
651 // we rejected above. Return a direct reference to the global.
Chris Lattner7277a802009-07-10 05:45:15 +0000652 if (Subtarget->isPICStyleRIPRel()) {
653 // Use rip-relative addressing if we can. Above we verified that the
654 // base and index registers are unused.
655 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
656 AM.Base.Reg = X86::RIP;
Chris Lattner21c29402009-07-10 21:00:45 +0000657 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattner7277a802009-07-10 05:45:15 +0000658 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
659 } else if (Subtarget->isPICStyleGOT()) {
660 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner8212d372009-07-10 05:33:42 +0000661 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000662
Chris Lattner8212d372009-07-10 05:33:42 +0000663 return true;
664 }
665
666 // If all else fails, try to materialize the value in a register.
667 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
668 if (AM.Base.Reg == 0) {
669 AM.Base.Reg = getRegForValue(V);
670 return AM.Base.Reg != 0;
671 }
672 if (AM.IndexReg == 0) {
673 assert(AM.Scale == 1 && "Scale with no index!");
674 AM.IndexReg = getRegForValue(V);
675 return AM.IndexReg != 0;
676 }
677 }
678
679 return false;
680}
681
682
Owen Anderson4f948bd2008-09-04 07:08:58 +0000683/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000684bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000685 // Atomic stores need special handling.
Lang Hames7d2f7b52011-10-18 22:11:33 +0000686 const StoreInst *S = cast<StoreInst>(I);
687
688 if (S->isAtomic())
689 return false;
690
Duncan Sandsf5dda012010-11-03 11:35:31 +0000691 MVT VT;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000692 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Anderson4f948bd2008-09-04 07:08:58 +0000693 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000694
Dan Gohman39d82f92008-09-10 20:11:02 +0000695 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +0000696 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman39d82f92008-09-10 20:11:02 +0000697 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000698
Chris Lattner3ba29352008-10-15 05:30:52 +0000699 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Anderson4f948bd2008-09-04 07:08:58 +0000700}
701
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000702/// X86SelectRet - Select and emit code to implement ret instructions.
703bool X86FastISel::X86SelectRet(const Instruction *I) {
704 const ReturnInst *Ret = cast<ReturnInst>(I);
705 const Function &F = *I->getParent()->getParent();
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000706 const X86MachineFunctionInfo *X86MFInfo =
707 FuncInfo.MF->getInfo<X86MachineFunctionInfo>();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000708
709 if (!FuncInfo.CanLowerReturn)
710 return false;
711
712 CallingConv::ID CC = F.getCallingConv();
713 if (CC != CallingConv::C &&
714 CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +0000715 CC != CallingConv::X86_FastCall &&
716 CC != CallingConv::X86_64_SysV)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000717 return false;
718
Charles Davise8f297c2013-07-12 06:02:35 +0000719 if (Subtarget->isCallingConvWin64(CC))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000720 return false;
721
722 // Don't handle popping bytes on return for now.
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000723 if (X86MFInfo->getBytesToPopOnReturn() != 0)
Jakub Staszak74010cd2013-02-17 18:35:25 +0000724 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000725
726 // fastcc with -tailcallopt is intended to provide a guaranteed
727 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000728 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000729 return false;
730
731 // Let SDISel handle vararg functions.
732 if (F.isVarArg())
733 return false;
734
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000735 // Build a list of return value registers.
736 SmallVector<unsigned, 4> RetRegs;
737
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000738 if (Ret->getNumOperands() > 0) {
739 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +0000740 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000741
742 // Analyze operands of the call, assigning locations to each operand.
743 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000744 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +0000745 I->getContext());
Duncan Sandsfa7e6f22010-10-31 13:02:38 +0000746 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000747
748 const Value *RV = Ret->getOperand(0);
749 unsigned Reg = getRegForValue(RV);
750 if (Reg == 0)
751 return false;
752
753 // Only handle a single return value for now.
754 if (ValLocs.size() != 1)
755 return false;
756
757 CCValAssign &VA = ValLocs[0];
Wesley Peck527da1b2010-11-23 03:31:01 +0000758
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000759 // Don't bother handling odd stuff for now.
760 if (VA.getLocInfo() != CCValAssign::Full)
761 return false;
762 // Only handle register returns for now.
763 if (!VA.isRegLoc())
764 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000765
766 // The calling-convention tables for x87 returns don't tell
767 // the whole story.
768 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
769 return false;
770
Eli Friedman6fc94dd2011-05-18 23:13:10 +0000771 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedman22da7992011-05-19 22:16:13 +0000772 EVT SrcVT = TLI.getValueType(RV->getType());
773 EVT DstVT = VA.getValVT();
774 // Special handling for extended integers.
775 if (SrcVT != DstVT) {
776 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
777 return false;
778
779 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
780 return false;
781
782 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
783
784 if (SrcVT == MVT::i1) {
785 if (Outs[0].Flags.isSExt())
786 return false;
787 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
788 SrcVT = MVT::i8;
789 }
790 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
791 ISD::SIGN_EXTEND;
792 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
793 SrcReg, /*TODO: Kill=*/false);
794 }
795
796 // Make the copy.
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000797 unsigned DstReg = VA.getLocReg();
798 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000799 // Avoid a cross-class copy. This is very unlikely.
800 if (!SrcRC->contains(DstReg))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000801 return false;
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000802 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
803 DstReg).addReg(SrcReg);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000804
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000805 // Add register to return instruction.
806 RetRegs.push_back(VA.getLocReg());
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000807 }
808
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000809 // The x86-64 ABI for returning structs by value requires that we copy
810 // the sret argument into %rax for the return. We saved the argument into
811 // a virtual register in the entry block, so now we copy the value out
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000812 // and into %rax. We also do the same with %eax for Win32.
813 if (F.hasStructRetAttr() &&
814 (Subtarget->is64Bit() || Subtarget->isTargetWindows())) {
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000815 unsigned Reg = X86MFInfo->getSRetReturnReg();
816 assert(Reg &&
817 "SRetReturnReg should have been set in LowerFormalArguments()!");
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000818 unsigned RetReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000819 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000820 RetReg).addReg(Reg);
821 RetRegs.push_back(RetReg);
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000822 }
823
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000824 // Now emit the RET.
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000825 MachineInstrBuilder MIB =
826 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
827 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
828 MIB.addReg(RetRegs[i], RegState::Implicit);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000829 return true;
830}
831
Evan Chenga41ee292008-09-03 06:44:39 +0000832/// X86SelectLoad - Select and emit code to implement load instructions.
833///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000834bool X86FastISel::X86SelectLoad(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000835 // Atomic loads need special handling.
836 if (cast<LoadInst>(I)->isAtomic())
837 return false;
838
Duncan Sandsf5dda012010-11-03 11:35:31 +0000839 MVT VT;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000840 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Chenga41ee292008-09-03 06:44:39 +0000841 return false;
842
Dan Gohman39d82f92008-09-10 20:11:02 +0000843 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +0000844 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman39d82f92008-09-10 20:11:02 +0000845 return false;
Evan Chenga41ee292008-09-03 06:44:39 +0000846
Evan Chengf5bc7e52008-09-05 21:00:03 +0000847 unsigned ResultReg = 0;
Dan Gohman39d82f92008-09-10 20:11:02 +0000848 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000849 UpdateValueMap(I, ResultReg);
850 return true;
Evan Chenga41ee292008-09-03 06:44:39 +0000851 }
Evan Chengf5bc7e52008-09-05 21:00:03 +0000852 return false;
Evan Chenga41ee292008-09-03 06:44:39 +0000853}
854
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +0000855static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000856 bool HasAVX = Subtarget->hasAVX();
Craig Topperb0c0f722012-01-10 06:54:16 +0000857 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
858 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000859
Owen Anderson9f944592009-08-11 20:47:22 +0000860 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner74e01282008-10-15 04:32:45 +0000861 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000862 case MVT::i8: return X86::CMP8rr;
863 case MVT::i16: return X86::CMP16rr;
864 case MVT::i32: return X86::CMP32rr;
865 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000866 case MVT::f32:
867 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
868 case MVT::f64:
869 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohman1ab1d312008-10-02 22:15:21 +0000870 }
Dan Gohman1ab1d312008-10-02 22:15:21 +0000871}
872
Chris Lattner88f47542008-10-15 04:13:29 +0000873/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
874/// of the comparison, return an opcode that works for the compare (e.g.
875/// CMP32ri) otherwise return 0.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000876static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson9f944592009-08-11 20:47:22 +0000877 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner88f47542008-10-15 04:13:29 +0000878 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner74e01282008-10-15 04:32:45 +0000879 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000880 case MVT::i8: return X86::CMP8ri;
881 case MVT::i16: return X86::CMP16ri;
882 case MVT::i32: return X86::CMP32ri;
883 case MVT::i64:
Chris Lattner74e01282008-10-15 04:32:45 +0000884 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
885 // field.
Chris Lattner3ba29352008-10-15 05:30:52 +0000886 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner74e01282008-10-15 04:32:45 +0000887 return X86::CMP64ri32;
888 return 0;
889 }
Chris Lattner88f47542008-10-15 04:13:29 +0000890}
891
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000892bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
893 EVT VT) {
Chris Lattnerd46b9512008-10-15 04:26:38 +0000894 unsigned Op0Reg = getRegForValue(Op0);
895 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000896
Chris Lattnere388725a2008-10-15 05:18:04 +0000897 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +0000898 if (isa<ConstantPointerNull>(Op1))
899 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +0000900
Chris Lattnerd46b9512008-10-15 04:26:38 +0000901 // We have two options: compare with register or immediate. If the RHS of
902 // the compare is an immediate that we can fold into this compare, use
903 // CMPri, otherwise use CMPrr.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000904 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner74e01282008-10-15 04:32:45 +0000905 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000906 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
907 .addReg(Op0Reg)
908 .addImm(Op1C->getSExtValue());
Chris Lattnerd46b9512008-10-15 04:26:38 +0000909 return true;
910 }
911 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000912
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +0000913 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattnerd46b9512008-10-15 04:26:38 +0000914 if (CompareOpc == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000915
Chris Lattnerd46b9512008-10-15 04:26:38 +0000916 unsigned Op1Reg = getRegForValue(Op1);
917 if (Op1Reg == 0) return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000918 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
919 .addReg(Op0Reg)
920 .addReg(Op1Reg);
Wesley Peck527da1b2010-11-23 03:31:01 +0000921
Chris Lattnerd46b9512008-10-15 04:26:38 +0000922 return true;
923}
924
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000925bool X86FastISel::X86SelectCmp(const Instruction *I) {
926 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000927
Duncan Sandsf5dda012010-11-03 11:35:31 +0000928 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +0000929 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman09faf812008-09-05 01:33:56 +0000930 return false;
931
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000932 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattnera3596db2008-10-15 03:47:17 +0000933 unsigned SetCCOpc;
Chris Lattnerf32ce222008-10-15 03:52:54 +0000934 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000935 switch (CI->getPredicate()) {
936 case CmpInst::FCMP_OEQ: {
Chris Lattnerdc1c3802008-10-15 04:29:23 +0000937 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
938 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000939
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000940 unsigned EReg = createResultReg(&X86::GR8RegClass);
941 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000942 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
943 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
944 TII.get(X86::SETNPr), NPReg);
Wesley Peck527da1b2010-11-23 03:31:01 +0000945 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen9bba9022009-02-13 02:33:27 +0000946 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattnera3596db2008-10-15 03:47:17 +0000947 UpdateValueMap(I, ResultReg);
948 return true;
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000949 }
950 case CmpInst::FCMP_UNE: {
Chris Lattnerdc1c3802008-10-15 04:29:23 +0000951 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
952 return false;
953
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000954 unsigned NEReg = createResultReg(&X86::GR8RegClass);
955 unsigned PReg = createResultReg(&X86::GR8RegClass);
Chris Lattner2c8a4c32011-04-19 04:22:17 +0000956 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETNEr), NEReg);
957 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETPr), PReg);
958 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::OR8rr),ResultReg)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000959 .addReg(PReg).addReg(NEReg);
Chris Lattnera3596db2008-10-15 03:47:17 +0000960 UpdateValueMap(I, ResultReg);
961 return true;
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000962 }
Chris Lattnerf32ce222008-10-15 03:52:54 +0000963 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
964 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
965 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
966 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
967 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
968 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
969 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
970 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
971 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
972 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
973 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
974 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peck527da1b2010-11-23 03:31:01 +0000975
Chris Lattnerf32ce222008-10-15 03:52:54 +0000976 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
977 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
978 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
979 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
980 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
981 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
982 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
983 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
984 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
985 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000986 default:
987 return false;
988 }
989
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000990 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattnerf32ce222008-10-15 03:52:54 +0000991 if (SwapArgs)
Chris Lattnerd46b9512008-10-15 04:26:38 +0000992 std::swap(Op0, Op1);
Chris Lattnerf32ce222008-10-15 03:52:54 +0000993
Chris Lattnerd46b9512008-10-15 04:26:38 +0000994 // Emit a compare of Op0/Op1.
Chris Lattnerdc1c3802008-10-15 04:29:23 +0000995 if (!X86FastEmitCompare(Op0, Op1, VT))
996 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000997
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000998 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000999 UpdateValueMap(I, ResultReg);
1000 return true;
1001}
Evan Chenga41ee292008-09-03 06:44:39 +00001002
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001003bool X86FastISel::X86SelectZExt(const Instruction *I) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001004 EVT DstVT = TLI.getValueType(I->getType());
1005 if (!TLI.isTypeLegal(DstVT))
1006 return false;
1007
1008 unsigned ResultReg = getRegForValue(I->getOperand(0));
1009 if (ResultReg == 0)
1010 return false;
1011
Tim Northover04eb4232013-05-30 10:43:18 +00001012 // Handle zero-extension from i1 to i8, which is common.
1013 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType()).getSimpleVT();
1014 if (SrcVT.SimpleTy == MVT::i1) {
1015 // Set the high bits to zero.
1016 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1017 SrcVT = MVT::i8;
Eli Friedmanc7035512011-05-25 23:49:02 +00001018
Tim Northover04eb4232013-05-30 10:43:18 +00001019 if (ResultReg == 0)
1020 return false;
1021 }
1022
1023 if (DstVT == MVT::i64) {
1024 // Handle extension to 64-bits via sub-register shenanigans.
1025 unsigned MovInst;
1026
1027 switch (SrcVT.SimpleTy) {
1028 case MVT::i8: MovInst = X86::MOVZX32rr8; break;
1029 case MVT::i16: MovInst = X86::MOVZX32rr16; break;
1030 case MVT::i32: MovInst = X86::MOV32rr; break;
1031 default: llvm_unreachable("Unexpected zext to i64 source type");
1032 }
1033
1034 unsigned Result32 = createResultReg(&X86::GR32RegClass);
1035 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovInst), Result32)
1036 .addReg(ResultReg);
1037
1038 ResultReg = createResultReg(&X86::GR64RegClass);
1039 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::SUBREG_TO_REG),
1040 ResultReg)
1041 .addImm(0).addReg(Result32).addImm(X86::sub_32bit);
1042 } else if (DstVT != MVT::i8) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001043 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1044 ResultReg, /*Kill=*/true);
1045 if (ResultReg == 0)
1046 return false;
Dan Gohmana5753b32008-09-05 01:06:14 +00001047 }
1048
Eli Friedmanc7035512011-05-25 23:49:02 +00001049 UpdateValueMap(I, ResultReg);
1050 return true;
Dan Gohmana5753b32008-09-05 01:06:14 +00001051}
1052
Chris Lattnerd46b9512008-10-15 04:26:38 +00001053
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001054bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmana5753b32008-09-05 01:06:14 +00001055 // Unconditional branches are selected by tablegen-generated code.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001056 // Handle a conditional branch.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001057 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohman87fb4e82010-07-07 16:29:44 +00001058 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1059 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmana5753b32008-09-05 01:06:14 +00001060
Dan Gohman42ef6692010-08-21 02:32:36 +00001061 // Fold the common case of a conditional branch with a comparison
1062 // in the same block (values defined on other blocks may not have
1063 // initialized registers).
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001064 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman42ef6692010-08-21 02:32:36 +00001065 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001066 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmana5753b32008-09-05 01:06:14 +00001067
Dan Gohman1ab1d312008-10-02 22:15:21 +00001068 // Try to take advantage of fallthrough opportunities.
1069 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001070 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohman1ab1d312008-10-02 22:15:21 +00001071 std::swap(TrueMBB, FalseMBB);
1072 Predicate = CmpInst::getInversePredicate(Predicate);
1073 }
1074
Chris Lattner0ce717a2008-10-15 03:58:05 +00001075 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
1076 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
1077
Dan Gohman1ab1d312008-10-02 22:15:21 +00001078 switch (Predicate) {
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001079 case CmpInst::FCMP_OEQ:
1080 std::swap(TrueMBB, FalseMBB);
1081 Predicate = CmpInst::FCMP_UNE;
1082 // FALL THROUGH
Chris Lattner2b0a7a22010-02-11 19:25:55 +00001083 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1084 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1085 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1086 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
1087 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
1088 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1089 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
1090 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
1091 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1092 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
1093 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
1094 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1095 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peck527da1b2010-11-23 03:31:01 +00001096
Chris Lattner2b0a7a22010-02-11 19:25:55 +00001097 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1098 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1099 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1100 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1101 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1102 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1103 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
1104 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1105 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1106 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001107 default:
1108 return false;
1109 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001110
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001111 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner47bef252008-10-15 04:02:26 +00001112 if (SwapArgs)
1113 std::swap(Op0, Op1);
1114
Chris Lattnerd46b9512008-10-15 04:26:38 +00001115 // Emit a compare of the LHS and RHS, setting the flags.
1116 if (!X86FastEmitCompare(Op0, Op1, VT))
1117 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001118
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001119 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1120 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001121
1122 if (Predicate == CmpInst::FCMP_UNE) {
1123 // X86 requires a second branch to handle UNE (and OEQ,
1124 // which is mapped to UNE above).
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001125 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1126 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001127 }
1128
Stuart Hastings0125b642010-06-17 22:43:56 +00001129 FastEmitBranch(FalseMBB, DL);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001130 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohman1ab1d312008-10-02 22:15:21 +00001131 return true;
1132 }
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001133 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1134 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1135 // typically happen for _Bool and C++ bools.
1136 MVT SourceVT;
1137 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1138 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1139 unsigned TestOpc = 0;
1140 switch (SourceVT.SimpleTy) {
1141 default: break;
1142 case MVT::i8: TestOpc = X86::TEST8ri; break;
1143 case MVT::i16: TestOpc = X86::TEST16ri; break;
1144 case MVT::i32: TestOpc = X86::TEST32ri; break;
1145 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1146 }
1147 if (TestOpc) {
1148 unsigned OpReg = getRegForValue(TI->getOperand(0));
1149 if (OpReg == 0) return false;
1150 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TestOpc))
1151 .addReg(OpReg).addImm(1);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001152
Chris Lattnerc59290a2011-04-19 04:26:32 +00001153 unsigned JmpOpc = X86::JNE_4;
1154 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1155 std::swap(TrueMBB, FalseMBB);
1156 JmpOpc = X86::JE_4;
1157 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001158
Chris Lattnerc59290a2011-04-19 04:26:32 +00001159 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(JmpOpc))
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001160 .addMBB(TrueMBB);
1161 FastEmitBranch(FalseMBB, DL);
1162 FuncInfo.MBB->addSuccessor(TrueMBB);
1163 return true;
1164 }
1165 }
Dan Gohman1ab1d312008-10-02 22:15:21 +00001166 }
1167
1168 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman0eea0292011-04-27 01:34:27 +00001169 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1170 // in an explicit cast, so make sure to handle that correctly.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001171 unsigned OpReg = getRegForValue(BI->getCondition());
1172 if (OpReg == 0) return false;
1173
Eli Friedman0eea0292011-04-27 01:34:27 +00001174 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8ri))
1175 .addReg(OpReg).addImm(1);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001176 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1177 .addMBB(TrueMBB);
Stuart Hastings0125b642010-06-17 22:43:56 +00001178 FastEmitBranch(FalseMBB, DL);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001179 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmana5753b32008-09-05 01:06:14 +00001180 return true;
1181}
1182
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001183bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001184 unsigned CReg = 0, OpReg = 0;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001185 const TargetRegisterClass *RC = NULL;
Duncan Sands9dff9be2010-02-15 16:12:20 +00001186 if (I->getType()->isIntegerTy(8)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001187 CReg = X86::CL;
1188 RC = &X86::GR8RegClass;
1189 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001190 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1191 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1192 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001193 default: return false;
1194 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001195 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001196 CReg = X86::CX;
1197 RC = &X86::GR16RegClass;
1198 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001199 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1200 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1201 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001202 default: return false;
1203 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001204 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001205 CReg = X86::ECX;
1206 RC = &X86::GR32RegClass;
1207 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001208 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1209 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1210 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001211 default: return false;
1212 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001213 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001214 CReg = X86::RCX;
1215 RC = &X86::GR64RegClass;
1216 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001217 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1218 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1219 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001220 default: return false;
1221 }
1222 } else {
1223 return false;
1224 }
1225
Duncan Sandsf5dda012010-11-03 11:35:31 +00001226 MVT VT;
1227 if (!isTypeLegal(I->getType(), VT))
Dan Gohmandb06a992008-09-05 21:27:34 +00001228 return false;
1229
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001230 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1231 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001232
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001233 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1234 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00001235 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1236 CReg).addReg(Op1Reg);
Dan Gohmand3917152008-10-07 21:50:36 +00001237
1238 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001239 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohmand3917152008-10-07 21:50:36 +00001240 if (CReg != X86::CL)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001241 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1242 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001243 .addReg(CReg, RegState::Kill);
Dan Gohmand3917152008-10-07 21:50:36 +00001244
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001245 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001246 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1247 .addReg(Op0Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001248 UpdateValueMap(I, ResultReg);
1249 return true;
1250}
1251
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001252bool X86FastISel::X86SelectDivRem(const Instruction *I) {
1253 const static unsigned NumTypes = 4; // i8, i16, i32, i64
1254 const static unsigned NumOps = 4; // SDiv, SRem, UDiv, URem
1255 const static bool S = true; // IsSigned
1256 const static bool U = false; // !IsSigned
1257 const static unsigned Copy = TargetOpcode::COPY;
1258 // For the X86 DIV/IDIV instruction, in most cases the dividend
1259 // (numerator) must be in a specific register pair highreg:lowreg,
1260 // producing the quotient in lowreg and the remainder in highreg.
1261 // For most data types, to set up the instruction, the dividend is
1262 // copied into lowreg, and lowreg is sign-extended or zero-extended
1263 // into highreg. The exception is i8, where the dividend is defined
1264 // as a single register rather than a register pair, and we
1265 // therefore directly sign-extend or zero-extend the dividend into
1266 // lowreg, instead of copying, and ignore the highreg.
1267 const static struct DivRemEntry {
1268 // The following portion depends only on the data type.
1269 const TargetRegisterClass *RC;
1270 unsigned LowInReg; // low part of the register pair
1271 unsigned HighInReg; // high part of the register pair
1272 // The following portion depends on both the data type and the operation.
1273 struct DivRemResult {
1274 unsigned OpDivRem; // The specific DIV/IDIV opcode to use.
1275 unsigned OpSignExtend; // Opcode for sign-extending lowreg into
1276 // highreg, or copying a zero into highreg.
1277 unsigned OpCopy; // Opcode for copying dividend into lowreg, or
1278 // zero/sign-extending into lowreg for i8.
1279 unsigned DivRemResultReg; // Register containing the desired result.
1280 bool IsOpSigned; // Whether to use signed or unsigned form.
1281 } ResultTable[NumOps];
1282 } OpTable[NumTypes] = {
1283 { &X86::GR8RegClass, X86::AX, 0, {
1284 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AL, S }, // SDiv
1285 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AH, S }, // SRem
1286 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AL, U }, // UDiv
1287 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AH, U }, // URem
1288 }
1289 }, // i8
1290 { &X86::GR16RegClass, X86::AX, X86::DX, {
1291 { X86::IDIV16r, X86::CWD, Copy, X86::AX, S }, // SDiv
1292 { X86::IDIV16r, X86::CWD, Copy, X86::DX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001293 { X86::DIV16r, X86::MOV32r0, Copy, X86::AX, U }, // UDiv
1294 { X86::DIV16r, X86::MOV32r0, Copy, X86::DX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001295 }
1296 }, // i16
1297 { &X86::GR32RegClass, X86::EAX, X86::EDX, {
1298 { X86::IDIV32r, X86::CDQ, Copy, X86::EAX, S }, // SDiv
1299 { X86::IDIV32r, X86::CDQ, Copy, X86::EDX, S }, // SRem
1300 { X86::DIV32r, X86::MOV32r0, Copy, X86::EAX, U }, // UDiv
1301 { X86::DIV32r, X86::MOV32r0, Copy, X86::EDX, U }, // URem
1302 }
1303 }, // i32
1304 { &X86::GR64RegClass, X86::RAX, X86::RDX, {
1305 { X86::IDIV64r, X86::CQO, Copy, X86::RAX, S }, // SDiv
1306 { X86::IDIV64r, X86::CQO, Copy, X86::RDX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001307 { X86::DIV64r, X86::MOV32r0, Copy, X86::RAX, U }, // UDiv
1308 { X86::DIV64r, X86::MOV32r0, Copy, X86::RDX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001309 }
1310 }, // i64
1311 };
1312
1313 MVT VT;
1314 if (!isTypeLegal(I->getType(), VT))
1315 return false;
1316
1317 unsigned TypeIndex, OpIndex;
1318 switch (VT.SimpleTy) {
1319 default: return false;
1320 case MVT::i8: TypeIndex = 0; break;
1321 case MVT::i16: TypeIndex = 1; break;
1322 case MVT::i32: TypeIndex = 2; break;
1323 case MVT::i64: TypeIndex = 3;
1324 if (!Subtarget->is64Bit())
1325 return false;
1326 break;
1327 }
1328
1329 switch (I->getOpcode()) {
1330 default: llvm_unreachable("Unexpected div/rem opcode");
1331 case Instruction::SDiv: OpIndex = 0; break;
1332 case Instruction::SRem: OpIndex = 1; break;
1333 case Instruction::UDiv: OpIndex = 2; break;
1334 case Instruction::URem: OpIndex = 3; break;
1335 }
1336
1337 const DivRemEntry &TypeEntry = OpTable[TypeIndex];
1338 const DivRemEntry::DivRemResult &OpEntry = TypeEntry.ResultTable[OpIndex];
1339 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1340 if (Op0Reg == 0)
1341 return false;
1342 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1343 if (Op1Reg == 0)
1344 return false;
1345
1346 // Move op0 into low-order input register.
1347 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1348 TII.get(OpEntry.OpCopy), TypeEntry.LowInReg).addReg(Op0Reg);
1349 // Zero-extend or sign-extend into high-order input register.
1350 if (OpEntry.OpSignExtend) {
1351 if (OpEntry.IsOpSigned)
1352 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1353 TII.get(OpEntry.OpSignExtend));
Tim Northover64ec0ff2013-05-30 13:19:42 +00001354 else {
1355 unsigned Zero32 = createResultReg(&X86::GR32RegClass);
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001356 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Tim Northover64ec0ff2013-05-30 13:19:42 +00001357 TII.get(X86::MOV32r0), Zero32);
1358
1359 // Copy the zero into the appropriate sub/super/identical physical
1360 // register. Unfortunately the operations needed are not uniform enough to
1361 // fit neatly into the table above.
1362 if (VT.SimpleTy == MVT::i16) {
1363 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001364 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001365 .addReg(Zero32, 0, X86::sub_16bit);
1366 } else if (VT.SimpleTy == MVT::i32) {
1367 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001368 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001369 .addReg(Zero32);
1370 } else if (VT.SimpleTy == MVT::i64) {
1371 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1372 TII.get(TargetOpcode::SUBREG_TO_REG), TypeEntry.HighInReg)
1373 .addImm(0).addReg(Zero32).addImm(X86::sub_32bit);
1374 }
1375 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001376 }
1377 // Generate the DIV/IDIV instruction.
1378 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1379 TII.get(OpEntry.OpDivRem)).addReg(Op1Reg);
Jim Grosbachc35388f2013-07-09 02:07:25 +00001380 // For i8 remainder, we can't reference AH directly, as we'll end
1381 // up with bogus copies like %R9B = COPY %AH. Reference AX
1382 // instead to prevent AH references in a REX instruction.
1383 //
1384 // The current assumption of the fast register allocator is that isel
1385 // won't generate explicit references to the GPR8_NOREX registers. If
1386 // the allocator and/or the backend get enhanced to be more robust in
1387 // that regard, this can be, and should be, removed.
1388 unsigned ResultReg = 0;
1389 if ((I->getOpcode() == Instruction::SRem ||
1390 I->getOpcode() == Instruction::URem) &&
1391 OpEntry.DivRemResultReg == X86::AH && Subtarget->is64Bit()) {
1392 unsigned SourceSuperReg = createResultReg(&X86::GR16RegClass);
1393 unsigned ResultSuperReg = createResultReg(&X86::GR16RegClass);
1394 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1395 TII.get(Copy), SourceSuperReg).addReg(X86::AX);
1396
1397 // Shift AX right by 8 bits instead of using AH.
1398 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SHR16ri),
1399 ResultSuperReg).addReg(SourceSuperReg).addImm(8);
1400
1401 // Now reference the 8-bit subreg of the result.
1402 ResultReg = FastEmitInst_extractsubreg(MVT::i8, ResultSuperReg,
1403 /*Kill=*/true, X86::sub_8bit);
1404 }
1405 // Copy the result out of the physreg if we haven't already.
1406 if (!ResultReg) {
1407 ResultReg = createResultReg(TypeEntry.RC);
1408 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Copy), ResultReg)
1409 .addReg(OpEntry.DivRemResultReg);
1410 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001411 UpdateValueMap(I, ResultReg);
1412
1413 return true;
1414}
1415
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001416bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001417 MVT VT;
1418 if (!isTypeLegal(I->getType(), VT))
Chris Lattnera0f9d492008-10-15 05:07:36 +00001419 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001420
Eric Christopher0574cc52010-09-29 23:00:29 +00001421 // We only use cmov here, if we don't have a cmov instruction bail.
1422 if (!Subtarget->hasCMov()) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001423
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001424 unsigned Opc = 0;
1425 const TargetRegisterClass *RC = NULL;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001426 if (VT == MVT::i16) {
Dan Gohmane5560182008-09-05 21:13:04 +00001427 Opc = X86::CMOVE16rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001428 RC = &X86::GR16RegClass;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001429 } else if (VT == MVT::i32) {
Dan Gohmane5560182008-09-05 21:13:04 +00001430 Opc = X86::CMOVE32rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001431 RC = &X86::GR32RegClass;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001432 } else if (VT == MVT::i64) {
Dan Gohmane5560182008-09-05 21:13:04 +00001433 Opc = X86::CMOVE64rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001434 RC = &X86::GR64RegClass;
1435 } else {
Wesley Peck527da1b2010-11-23 03:31:01 +00001436 return false;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001437 }
1438
1439 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1440 if (Op0Reg == 0) return false;
1441 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1442 if (Op1Reg == 0) return false;
1443 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1444 if (Op2Reg == 0) return false;
1445
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001446 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1447 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001448 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001449 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1450 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001451 UpdateValueMap(I, ResultReg);
1452 return true;
1453}
1454
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001455bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00001456 // fpext from float to double.
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001457 if (X86ScalarSSEf64 &&
Chris Lattnerfdd87902009-10-05 05:54:46 +00001458 I->getType()->isDoubleTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001459 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001460 if (V->getType()->isFloatTy()) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00001461 unsigned OpReg = getRegForValue(V);
1462 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00001463 unsigned ResultReg = createResultReg(&X86::FR64RegClass);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001464 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1465 TII.get(X86::CVTSS2SDrr), ResultReg)
1466 .addReg(OpReg);
Chris Lattnera0f9d492008-10-15 05:07:36 +00001467 UpdateValueMap(I, ResultReg);
1468 return true;
Dan Gohmanbf646f22008-09-10 21:02:08 +00001469 }
1470 }
1471
1472 return false;
1473}
1474
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001475bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001476 if (X86ScalarSSEf64) {
Chris Lattnerfdd87902009-10-05 05:54:46 +00001477 if (I->getType()->isFloatTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001478 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001479 if (V->getType()->isDoubleTy()) {
Dan Gohmanbf646f22008-09-10 21:02:08 +00001480 unsigned OpReg = getRegForValue(V);
1481 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00001482 unsigned ResultReg = createResultReg(&X86::FR32RegClass);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001483 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1484 TII.get(X86::CVTSD2SSrr), ResultReg)
1485 .addReg(OpReg);
Dan Gohmanbf646f22008-09-10 21:02:08 +00001486 UpdateValueMap(I, ResultReg);
1487 return true;
1488 }
1489 }
1490 }
1491
1492 return false;
1493}
1494
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001495bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001496 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1497 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peck527da1b2010-11-23 03:31:01 +00001498
Eli Friedmanc7035512011-05-25 23:49:02 +00001499 // This code only handles truncation to byte.
Owen Anderson9f944592009-08-11 20:47:22 +00001500 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Chengb9286692008-09-07 08:47:42 +00001501 return false;
Eli Friedmanc7035512011-05-25 23:49:02 +00001502 if (!TLI.isTypeLegal(SrcVT))
Evan Chengb9286692008-09-07 08:47:42 +00001503 return false;
1504
1505 unsigned InputReg = getRegForValue(I->getOperand(0));
1506 if (!InputReg)
1507 // Unhandled operand. Halt "fast" selection and bail.
1508 return false;
1509
Eli Friedmanc7035512011-05-25 23:49:02 +00001510 if (SrcVT == MVT::i8) {
1511 // Truncate from i8 to i1; no code needed.
1512 UpdateValueMap(I, InputReg);
1513 return true;
1514 }
Evan Chengb9286692008-09-07 08:47:42 +00001515
Eli Friedmanc7035512011-05-25 23:49:02 +00001516 if (!Subtarget->is64Bit()) {
1517 // If we're on x86-32; we can't extract an i8 from a general register.
1518 // First issue a copy to GR16_ABCD or GR32_ABCD.
Craig Topperabadc662012-04-20 06:31:50 +00001519 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) ?
1520 (const TargetRegisterClass*)&X86::GR16_ABCDRegClass :
1521 (const TargetRegisterClass*)&X86::GR32_ABCDRegClass;
Eli Friedmanc7035512011-05-25 23:49:02 +00001522 unsigned CopyReg = createResultReg(CopyRC);
1523 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1524 CopyReg).addReg(InputReg);
1525 InputReg = CopyReg;
1526 }
1527
1528 // Issue an extract_subreg.
Owen Anderson9f944592009-08-11 20:47:22 +00001529 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedmanc7035512011-05-25 23:49:02 +00001530 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen9340ea52010-05-24 14:48:17 +00001531 X86::sub_8bit);
Evan Chengb9286692008-09-07 08:47:42 +00001532 if (!ResultReg)
1533 return false;
1534
1535 UpdateValueMap(I, ResultReg);
1536 return true;
1537}
1538
Eli Friedman60afcc22011-05-20 22:21:04 +00001539bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1540 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1541}
1542
Eli Friedmanbcc69142011-04-27 01:45:07 +00001543bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1544 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedman60afcc22011-05-20 22:21:04 +00001545
Eli Friedmanbcc69142011-04-27 01:45:07 +00001546 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedman60afcc22011-05-20 22:21:04 +00001547 if (!IsMemcpySmall(Len))
1548 return false;
1549
1550 bool i64Legal = Subtarget->is64Bit();
Eli Friedmanbcc69142011-04-27 01:45:07 +00001551
1552 // We don't care about alignment here since we just emit integer accesses.
1553 while (Len) {
1554 MVT VT;
1555 if (Len >= 8 && i64Legal)
1556 VT = MVT::i64;
1557 else if (Len >= 4)
1558 VT = MVT::i32;
1559 else if (Len >= 2)
1560 VT = MVT::i16;
1561 else {
Eli Friedmanbcc69142011-04-27 01:45:07 +00001562 VT = MVT::i8;
1563 }
1564
1565 unsigned Reg;
1566 bool RV = X86FastEmitLoad(VT, SrcAM, Reg);
1567 RV &= X86FastEmitStore(VT, Reg, DestAM);
1568 assert(RV && "Failed to emit load or store??");
1569
1570 unsigned Size = VT.getSizeInBits()/8;
1571 Len -= Size;
1572 DestAM.Disp += Size;
1573 SrcAM.Disp += Size;
1574 }
1575
1576 return true;
1577}
1578
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001579bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling80b34b32008-12-09 02:42:50 +00001580 // FIXME: Handle more intrinsics.
Chris Lattner99a8cb62009-04-12 07:36:01 +00001581 switch (I.getIntrinsicID()) {
Bill Wendling80b34b32008-12-09 02:42:50 +00001582 default: return false;
Chris Lattner91328b32011-04-19 05:52:03 +00001583 case Intrinsic::memcpy: {
1584 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1585 // Don't handle volatile or variable length memcpys.
Eli Friedmancd2124a2011-06-10 23:39:36 +00001586 if (MCI.isVolatile())
Chris Lattner91328b32011-04-19 05:52:03 +00001587 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00001588
Eli Friedmancd2124a2011-06-10 23:39:36 +00001589 if (isa<ConstantInt>(MCI.getLength())) {
1590 // Small memcpy's are common enough that we want to do them
1591 // without a call if possible.
1592 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1593 if (IsMemcpySmall(Len)) {
1594 X86AddressMode DestAM, SrcAM;
1595 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1596 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1597 return false;
1598 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1599 return true;
1600 }
1601 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001602
Eli Friedmancd2124a2011-06-10 23:39:36 +00001603 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1604 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner91328b32011-04-19 05:52:03 +00001605 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00001606
Eli Friedmancd2124a2011-06-10 23:39:36 +00001607 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1608 return false;
1609
1610 return DoSelectCall(&I, "memcpy");
Chris Lattner91328b32011-04-19 05:52:03 +00001611 }
Eli Friedmancd2124a2011-06-10 23:39:36 +00001612 case Intrinsic::memset: {
1613 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001614
Nick Lewyckya530a4d2011-08-02 00:40:16 +00001615 if (MSI.isVolatile())
1616 return false;
1617
Eli Friedmancd2124a2011-06-10 23:39:36 +00001618 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1619 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1620 return false;
1621
1622 if (MSI.getDestAddressSpace() > 255)
1623 return false;
1624
1625 return DoSelectCall(&I, "memset");
1626 }
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001627 case Intrinsic::stackprotector: {
Chad Rosier06e34d92012-05-11 19:43:29 +00001628 // Emit code to store the stack guard onto the stack.
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001629 EVT PtrTy = TLI.getPointerTy();
1630
Gabor Greif83205af2010-06-26 11:51:52 +00001631 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1632 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001633
1634 // Grab the frame index.
1635 X86AddressMode AM;
1636 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher5e95aee2010-03-18 21:58:33 +00001637 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001638 return true;
1639 }
Dale Johannesend5575f22010-01-26 00:09:58 +00001640 case Intrinsic::dbg_declare: {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001641 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesend5575f22010-01-26 00:09:58 +00001642 X86AddressMode AM;
Dale Johannesenad00f032010-01-29 21:21:28 +00001643 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesend5575f22010-01-26 00:09:58 +00001644 if (!X86SelectAddress(DI->getAddress(), AM))
1645 return false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00001646 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen654528e2010-02-18 18:51:15 +00001647 // FIXME may need to add RegState::Debug to any registers produced,
1648 // although ESP/EBP should be the only ones at the moment.
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001649 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1650 addImm(0).addMetadata(DI->getVariable());
Dale Johannesend5575f22010-01-26 00:09:58 +00001651 return true;
1652 }
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00001653 case Intrinsic::trap: {
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001654 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00001655 return true;
1656 }
Bill Wendling80b34b32008-12-09 02:42:50 +00001657 case Intrinsic::sadd_with_overflow:
1658 case Intrinsic::uadd_with_overflow: {
Chris Lattner91328b32011-04-19 05:52:03 +00001659 // FIXME: Should fold immediates.
Eric Christopher0713a9d2011-06-08 23:55:35 +00001660
Bill Wendlinge25d3412008-12-09 07:55:31 +00001661 // Replace "add with overflow" intrinsics with an "add" instruction followed
Eli Friedmana4d4a012011-05-16 21:06:17 +00001662 // by a seto/setc instruction.
Bill Wendling80b34b32008-12-09 02:42:50 +00001663 const Function *Callee = I.getCalledFunction();
Chris Lattner229907c2011-07-18 04:54:35 +00001664 Type *RetTy =
Bill Wendling80b34b32008-12-09 02:42:50 +00001665 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1666
Duncan Sandsf5dda012010-11-03 11:35:31 +00001667 MVT VT;
Bill Wendling80b34b32008-12-09 02:42:50 +00001668 if (!isTypeLegal(RetTy, VT))
1669 return false;
1670
Gabor Greif83205af2010-06-26 11:51:52 +00001671 const Value *Op1 = I.getArgOperand(0);
1672 const Value *Op2 = I.getArgOperand(1);
Bill Wendling80b34b32008-12-09 02:42:50 +00001673 unsigned Reg1 = getRegForValue(Op1);
1674 unsigned Reg2 = getRegForValue(Op2);
1675
1676 if (Reg1 == 0 || Reg2 == 0)
1677 // FIXME: Handle values *not* in registers.
1678 return false;
1679
1680 unsigned OpC = 0;
Owen Anderson9f944592009-08-11 20:47:22 +00001681 if (VT == MVT::i32)
Bill Wendling80b34b32008-12-09 02:42:50 +00001682 OpC = X86::ADD32rr;
Owen Anderson9f944592009-08-11 20:47:22 +00001683 else if (VT == MVT::i64)
Bill Wendling80b34b32008-12-09 02:42:50 +00001684 OpC = X86::ADD64rr;
1685 else
1686 return false;
1687
Eli Friedmana4d4a012011-05-16 21:06:17 +00001688 // The call to CreateRegs builds two sequential registers, to store the
Sylvestre Ledru35521e22012-07-23 08:51:15 +00001689 // both the returned values.
Eli Friedmana4d4a012011-05-16 21:06:17 +00001690 unsigned ResultReg = FuncInfo.CreateRegs(I.getType());
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001691 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1692 .addReg(Reg1).addReg(Reg2);
Wesley Peck527da1b2010-11-23 03:31:01 +00001693
Chris Lattner99a8cb62009-04-12 07:36:01 +00001694 unsigned Opc = X86::SETBr;
1695 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1696 Opc = X86::SETOr;
Eli Friedmana4d4a012011-05-16 21:06:17 +00001697 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg+1);
1698
1699 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling80b34b32008-12-09 02:42:50 +00001700 return true;
1701 }
1702 }
1703}
1704
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001705bool X86FastISel::FastLowerArguments() {
1706 if (!FuncInfo.CanLowerReturn)
1707 return false;
1708
1709 const Function *F = FuncInfo.Fn;
1710 if (F->isVarArg())
1711 return false;
1712
1713 CallingConv::ID CC = F->getCallingConv();
1714 if (CC != CallingConv::C)
1715 return false;
Charles Davise8f297c2013-07-12 06:02:35 +00001716
1717 if (Subtarget->isCallingConvWin64(CC))
1718 return false;
1719
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001720 if (!Subtarget->is64Bit())
1721 return false;
1722
1723 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
1724 unsigned Idx = 1;
1725 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1726 I != E; ++I, ++Idx) {
1727 if (Idx > 6)
1728 return false;
1729
1730 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
1731 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
1732 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
1733 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
1734 return false;
1735
1736 Type *ArgTy = I->getType();
1737 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
1738 return false;
1739
1740 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosier1b33e8d2013-02-26 01:05:31 +00001741 if (!ArgVT.isSimple()) return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001742 switch (ArgVT.getSimpleVT().SimpleTy) {
1743 case MVT::i32:
1744 case MVT::i64:
1745 break;
1746 default:
1747 return false;
1748 }
1749 }
1750
1751 static const uint16_t GPR32ArgRegs[] = {
1752 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
1753 };
1754 static const uint16_t GPR64ArgRegs[] = {
1755 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
1756 };
1757
1758 Idx = 0;
1759 const TargetRegisterClass *RC32 = TLI.getRegClassFor(MVT::i32);
1760 const TargetRegisterClass *RC64 = TLI.getRegClassFor(MVT::i64);
1761 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1762 I != E; ++I, ++Idx) {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001763 bool is32Bit = TLI.getValueType(I->getType()) == MVT::i32;
1764 const TargetRegisterClass *RC = is32Bit ? RC32 : RC64;
1765 unsigned SrcReg = is32Bit ? GPR32ArgRegs[Idx] : GPR64ArgRegs[Idx];
1766 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
1767 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1768 // Without this, EmitLiveInCopies may eliminate the livein if its only
1769 // use is a bitcast (which isn't turned into an instruction).
1770 unsigned ResultReg = createResultReg(RC);
1771 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1772 ResultReg).addReg(DstReg, getKillRegState(true));
1773 UpdateValueMap(I, ResultReg);
1774 }
1775 return true;
1776}
1777
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001778bool X86FastISel::X86SelectCall(const Instruction *I) {
1779 const CallInst *CI = cast<CallInst>(I);
Gabor Greif83205af2010-06-26 11:51:52 +00001780 const Value *Callee = CI->getCalledValue();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001781
1782 // Can't handle inline asm yet.
1783 if (isa<InlineAsm>(Callee))
1784 return false;
1785
Bill Wendling80b34b32008-12-09 02:42:50 +00001786 // Handle intrinsic calls.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001787 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattner99a8cb62009-04-12 07:36:01 +00001788 return X86VisitIntrinsicCall(*II);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001789
Chad Rosierdf42cf32012-12-11 00:18:02 +00001790 // Allow SelectionDAG isel to handle tail calls.
1791 if (cast<CallInst>(I)->isTailCall())
1792 return false;
1793
Eli Friedmancd2124a2011-06-10 23:39:36 +00001794 return DoSelectCall(I, 0);
1795}
1796
Rafael Espindola73173c52012-07-25 15:42:45 +00001797static unsigned computeBytesPoppedByCallee(const X86Subtarget &Subtarget,
1798 const ImmutableCallSite &CS) {
Rafael Espindola2caee7f2012-07-25 13:35:45 +00001799 if (Subtarget.is64Bit())
1800 return 0;
1801 if (Subtarget.isTargetWindows())
1802 return 0;
1803 CallingConv::ID CC = CS.getCallingConv();
1804 if (CC == CallingConv::Fast || CC == CallingConv::GHC)
1805 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001806 if (!CS.paramHasAttr(1, Attribute::StructRet))
Rafael Espindola2caee7f2012-07-25 13:35:45 +00001807 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001808 if (CS.paramHasAttr(1, Attribute::InReg))
Rafael Espindola11c38b92012-07-25 13:41:10 +00001809 return 0;
Rafael Espindola2caee7f2012-07-25 13:35:45 +00001810 return 4;
1811}
1812
Eli Friedmancd2124a2011-06-10 23:39:36 +00001813// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
1814bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
1815 const CallInst *CI = cast<CallInst>(I);
1816 const Value *Callee = CI->getCalledValue();
1817
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001818 // Handle only C and fastcc calling conventions for now.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001819 ImmutableCallSite CS(CI);
Sandeep Patel68c5f472009-09-02 08:44:58 +00001820 CallingConv::ID CC = CS.getCallingConv();
Charles Davise8f297c2013-07-12 06:02:35 +00001821 bool isWin64 = Subtarget->isCallingConvWin64(CC);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00001822 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +00001823 CC != CallingConv::X86_FastCall && CC != CallingConv::X86_64_Win64 &&
1824 CC != CallingConv::X86_64_SysV)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001825 return false;
1826
Evan Chengd10089a2010-01-27 00:00:57 +00001827 // fastcc with -tailcallopt is intended to provide a guaranteed
1828 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001829 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Evan Chengd10089a2010-01-27 00:00:57 +00001830 return false;
1831
Chris Lattner229907c2011-07-18 04:54:35 +00001832 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1833 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedmanee92a6b2011-04-19 17:22:22 +00001834 bool isVarArg = FTy->isVarArg();
1835
1836 // Don't know how to handle Win64 varargs yet. Nothing special needed for
1837 // x86-32. Special handling for x86-64 is implemented.
Charles Davise8f297c2013-07-12 06:02:35 +00001838 if (isVarArg && isWin64)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001839 return false;
1840
Dan Gohmandc53f1c2010-05-27 18:43:40 +00001841 // Fast-isel doesn't know about callee-pop yet.
Evan Cheng3a0c5e52011-06-23 17:54:54 +00001842 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001843 TM.Options.GuaranteedTailCallOpt))
Dan Gohmandc53f1c2010-05-27 18:43:40 +00001844 return false;
1845
Eli Friedman7b279422011-05-17 18:29:03 +00001846 // Check whether the function can return without sret-demotion.
1847 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +00001848 GetReturnInfo(I->getType(), CS.getAttributes(), Outs, TLI);
Eli Friedman7b279422011-05-17 18:29:03 +00001849 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Bill Wendlingea6397f2012-07-19 00:11:40 +00001850 *FuncInfo.MF, FTy->isVarArg(),
1851 Outs, FTy->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00001852 if (!CanLowerReturn)
Eli Friedman7335e8a2011-05-17 02:36:59 +00001853 return false;
1854
Dan Gohmanaf13bf12008-09-17 21:18:49 +00001855 // Materialize callee address in a register. FIXME: GV address can be
1856 // handled with a CALLpcrel32 instead.
Dan Gohman9801ba42008-09-19 22:16:54 +00001857 X86AddressMode CalleeAM;
Chris Lattner8212d372009-07-10 05:33:42 +00001858 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman9801ba42008-09-19 22:16:54 +00001859 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00001860 unsigned CalleeOp = 0;
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001861 const GlobalValue *GV = 0;
Chris Lattnerd17366a2009-06-27 04:50:14 +00001862 if (CalleeAM.GV != 0) {
Dan Gohman9801ba42008-09-19 22:16:54 +00001863 GV = CalleeAM.GV;
Chris Lattnerd17366a2009-06-27 04:50:14 +00001864 } else if (CalleeAM.Base.Reg != 0) {
1865 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman9801ba42008-09-19 22:16:54 +00001866 } else
1867 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00001868
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001869 // Deal with call operands first.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001870 SmallVector<const Value *, 8> ArgVals;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00001871 SmallVector<unsigned, 8> Args;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001872 SmallVector<MVT, 8> ArgVTs;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00001873 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosierf0687632012-02-15 00:36:26 +00001874 unsigned arg_size = CS.arg_size();
1875 Args.reserve(arg_size);
1876 ArgVals.reserve(arg_size);
1877 ArgVTs.reserve(arg_size);
1878 ArgFlags.reserve(arg_size);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001879 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001880 i != e; ++i) {
Eli Friedmancd2124a2011-06-10 23:39:36 +00001881 // If we're lowering a mem intrinsic instead of a regular call, skip the
1882 // last two arguments, which should not passed to the underlying functions.
1883 if (MemIntName && e-i <= 2)
1884 break;
Chris Lattnerd7f7c932011-04-19 04:42:38 +00001885 Value *ArgVal = *i;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001886 ISD::ArgFlagsTy Flags;
1887 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001888 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001889 Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001890 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001891 Flags.setZExt();
1892
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001893 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattner229907c2011-07-18 04:54:35 +00001894 PointerType *Ty = cast<PointerType>(ArgVal->getType());
1895 Type *ElementTy = Ty->getElementType();
Eli Friedman60afcc22011-05-20 22:21:04 +00001896 unsigned FrameSize = TD.getTypeAllocSize(ElementTy);
1897 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
1898 if (!FrameAlign)
1899 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
1900 Flags.setByVal();
1901 Flags.setByValSize(FrameSize);
1902 Flags.setByValAlign(FrameAlign);
1903 if (!IsMemcpySmall(FrameSize))
1904 return false;
1905 }
1906
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001907 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
Eli Friedman60afcc22011-05-20 22:21:04 +00001908 Flags.setInReg();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001909 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
Eli Friedman60afcc22011-05-20 22:21:04 +00001910 Flags.setNest();
1911
Chris Lattnerd7f7c932011-04-19 04:42:38 +00001912 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1913 // instruction. This is safe because it is common to all fastisel supported
1914 // calling conventions on x86.
1915 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1916 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1917 CI->getBitWidth() == 16) {
1918 if (Flags.isSExt())
1919 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1920 else
1921 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1922 }
1923 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001924
Chris Lattner5f4b7832011-04-19 05:09:50 +00001925 unsigned ArgReg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00001926
Chris Lattner34a08c22011-04-19 05:15:59 +00001927 // Passing bools around ends up doing a trunc to i1 and passing it.
1928 // Codegen this as an argument + "and 1".
Chris Lattner5f4b7832011-04-19 05:09:50 +00001929 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
1930 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
1931 ArgVal->hasOneUse()) {
Chris Lattner5f4b7832011-04-19 05:09:50 +00001932 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
1933 ArgReg = getRegForValue(ArgVal);
1934 if (ArgReg == 0) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00001935
Chris Lattner5f4b7832011-04-19 05:09:50 +00001936 MVT ArgVT;
1937 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00001938
Chris Lattner5f4b7832011-04-19 05:09:50 +00001939 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
1940 ArgVal->hasOneUse(), 1);
1941 } else {
1942 ArgReg = getRegForValue(ArgVal);
Chris Lattner5f4b7832011-04-19 05:09:50 +00001943 }
Chris Lattnerd7f7c932011-04-19 04:42:38 +00001944
Chris Lattner34a08c22011-04-19 05:15:59 +00001945 if (ArgReg == 0) return false;
1946
Chris Lattner229907c2011-07-18 04:54:35 +00001947 Type *ArgTy = ArgVal->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00001948 MVT ArgVT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00001949 if (!isTypeLegal(ArgTy, ArgVT))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001950 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00001951 if (ArgVT == MVT::x86mmx)
1952 return false;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001953 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1954 Flags.setOrigAlign(OriginalAlignment);
1955
Chris Lattner5f4b7832011-04-19 05:09:50 +00001956 Args.push_back(ArgReg);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00001957 ArgVals.push_back(ArgVal);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001958 ArgVTs.push_back(ArgVT);
1959 ArgFlags.push_back(Flags);
1960 }
1961
1962 // Analyze operands of the call, assigning locations to each operand.
1963 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00001964 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00001965 I->getParent()->getContext());
Wesley Peck527da1b2010-11-23 03:31:01 +00001966
Dan Gohman47a07242010-06-01 21:09:47 +00001967 // Allocate shadow area for Win64
Charles Davise8f297c2013-07-12 06:02:35 +00001968 if (isWin64)
Wesley Peck527da1b2010-11-23 03:31:01 +00001969 CCInfo.AllocateStack(32, 8);
Dan Gohman47a07242010-06-01 21:09:47 +00001970
Duncan Sandsfb0a48e2010-10-31 13:21:44 +00001971 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001972
1973 // Get a count of how many bytes are to be pushed on the stack.
1974 unsigned NumBytes = CCInfo.getNextStackOffset();
1975
1976 // Issue CALLSEQ_START
Evan Cheng194c3dc2011-06-28 21:14:33 +00001977 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001978 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1979 .addImm(NumBytes);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001980
Chris Lattner3ba29352008-10-15 05:30:52 +00001981 // Process argument: walk the register/memloc assignments, inserting
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001982 // copies / loads.
1983 SmallVector<unsigned, 4> RegArgs;
1984 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1985 CCValAssign &VA = ArgLocs[i];
1986 unsigned Arg = Args[VA.getValNo()];
Owen Anderson53aa7a92009-08-10 22:56:29 +00001987 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00001988
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001989 // Promote the value if needed.
1990 switch (VA.getLocInfo()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001991 case CCValAssign::Full: break;
Evan Cheng6500d172008-09-08 06:35:17 +00001992 case CCValAssign::SExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00001993 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1994 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00001995 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1996 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00001997 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00001998 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001999 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002000 }
2001 case CCValAssign::ZExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002002 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2003 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002004 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
2005 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002006 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002007 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002008 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002009 }
2010 case CCValAssign::AExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002011 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2012 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002013 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
2014 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002015 if (!Emitted)
2016 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattnera0f9d492008-10-15 05:07:36 +00002017 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002018 if (!Emitted)
2019 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2020 Arg, ArgVT, Arg);
Wesley Peck527da1b2010-11-23 03:31:01 +00002021
Chris Lattner2d7df022011-01-05 22:26:52 +00002022 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002023 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002024 break;
2025 }
Dan Gohman8c795692009-08-05 05:33:42 +00002026 case CCValAssign::BCvt: {
Duncan Sandsf5dda012010-11-03 11:35:31 +00002027 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peck527da1b2010-11-23 03:31:01 +00002028 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohman8c795692009-08-05 05:33:42 +00002029 assert(BC != 0 && "Failed to emit a bitcast!");
2030 Arg = BC;
2031 ArgVT = VA.getLocVT();
2032 break;
2033 }
Chad Rosier8446ede2012-07-11 19:58:38 +00002034 case CCValAssign::VExt:
2035 // VExt has not been implemented, so this should be impossible to reach
2036 // for now. However, fallback to Selection DAG isel once implemented.
2037 return false;
2038 case CCValAssign::Indirect:
2039 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
2040 // support this.
2041 return false;
Evan Cheng6500d172008-09-08 06:35:17 +00002042 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002043
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002044 if (VA.isRegLoc()) {
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00002045 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2046 VA.getLocReg()).addReg(Arg);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002047 RegArgs.push_back(VA.getLocReg());
2048 } else {
2049 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman39d82f92008-09-10 20:11:02 +00002050 X86AddressMode AM;
Bill Wendling8f268402013-06-07 21:00:34 +00002051 const X86RegisterInfo *RegInfo = static_cast<const X86RegisterInfo*>(
2052 getTargetMachine()->getRegisterInfo());
Michael Liao70a99c82012-11-01 03:47:50 +00002053 AM.Base.Reg = RegInfo->getStackRegister();
Dan Gohman39d82f92008-09-10 20:11:02 +00002054 AM.Disp = LocMemOffset;
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002055 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedman60afcc22011-05-20 22:21:04 +00002056 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002057
Eli Friedman60afcc22011-05-20 22:21:04 +00002058 if (Flags.isByVal()) {
2059 X86AddressMode SrcAM;
2060 SrcAM.Base.Reg = Arg;
2061 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
2062 assert(Res && "memcpy length already checked!"); (void)Res;
2063 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
2064 // If this is a really simple value, emit this with the Value* version
Nick Lewycky064c1c02011-10-12 00:14:12 +00002065 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
Eli Friedman60afcc22011-05-20 22:21:04 +00002066 // as it can cause us to reevaluate the argument.
Lang Hames7d2f7b52011-10-18 22:11:33 +00002067 if (!X86FastEmitStore(ArgVT, ArgVal, AM))
2068 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002069 } else {
Lang Hames7d2f7b52011-10-18 22:11:33 +00002070 if (!X86FastEmitStore(ArgVT, Arg, AM))
2071 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002072 }
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002073 }
2074 }
2075
Dan Gohman3691d502008-09-25 15:24:26 +00002076 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peck527da1b2010-11-23 03:31:01 +00002077 // GOT pointer.
Chris Lattnerfef11d62009-07-09 04:39:06 +00002078 if (Subtarget->isPICStyleGOT()) {
Dan Gohman87fb4e82010-07-07 16:29:44 +00002079 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00002080 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2081 X86::EBX).addReg(Base);
Dan Gohman3691d502008-09-25 15:24:26 +00002082 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002083
Charles Davise8f297c2013-07-12 06:02:35 +00002084 if (Subtarget->is64Bit() && isVarArg && !isWin64) {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002085 // Count the number of XMM registers allocated.
Craig Topperbef78fc2012-03-11 07:57:25 +00002086 static const uint16_t XMMArgRegs[] = {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002087 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2088 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2089 };
2090 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
2091 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::MOV8ri),
2092 X86::AL).addImm(NumXMMRegs);
2093 }
2094
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002095 // Issue the call.
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002096 MachineInstrBuilder MIB;
2097 if (CalleeOp) {
2098 // Register-indirect call.
Nate Begeman68a069a2010-07-22 00:09:39 +00002099 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002100 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002101 CallOpc = X86::CALL64r;
2102 else
2103 CallOpc = X86::CALL32r;
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002104 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
2105 .addReg(CalleeOp);
Wesley Peck527da1b2010-11-23 03:31:01 +00002106
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002107 } else {
2108 // Direct call.
2109 assert(GV && "Not a direct call");
Nate Begeman68a069a2010-07-22 00:09:39 +00002110 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002111 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002112 CallOpc = X86::CALL64pcrel32;
2113 else
2114 CallOpc = X86::CALLpcrel32;
Wesley Peck527da1b2010-11-23 03:31:01 +00002115
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002116 // See if we need any target-specific flags on the GV operand.
2117 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00002118
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002119 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2120 // external symbols most go through the PLT in PIC mode. If the symbol
2121 // has hidden or protected visibility, or if it is static or local, then
2122 // we don't need to use the PLT - we can directly call it.
2123 if (Subtarget->isTargetELF() &&
2124 TM.getRelocationModel() == Reloc::PIC_ &&
2125 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
2126 OpFlags = X86II::MO_PLT;
Chris Lattnere2f524f2009-07-10 20:47:30 +00002127 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002128 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00002129 (!Subtarget->getTargetTriple().isMacOSX() ||
2130 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002131 // PC-relative references to external symbols should go through $stub,
2132 // unless we're building with the leopard linker or later, which
2133 // automatically synthesizes these stubs.
2134 OpFlags = X86II::MO_DARWIN_STUB;
2135 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002136
2137
Eli Friedmancd2124a2011-06-10 23:39:36 +00002138 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc));
2139 if (MemIntName)
Eli Friedman1735b292011-06-11 01:55:07 +00002140 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedmancd2124a2011-06-10 23:39:36 +00002141 else
2142 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002143 }
Dan Gohman3691d502008-09-25 15:24:26 +00002144
Jakob Stoklund Olesen8a450cb2012-02-16 00:02:50 +00002145 // Add a register mask with the call-preserved registers.
2146 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2147 MIB.addRegMask(TRI.getCallPreservedMask(CS.getCallingConv()));
2148
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00002149 // Add an implicit use GOT pointer in EBX.
2150 if (Subtarget->isPICStyleGOT())
2151 MIB.addReg(X86::EBX, RegState::Implicit);
2152
Charles Davise8f297c2013-07-12 06:02:35 +00002153 if (Subtarget->is64Bit() && isVarArg && !isWin64)
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00002154 MIB.addReg(X86::AL, RegState::Implicit);
2155
2156 // Add implicit physical register uses to the call.
2157 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2158 MIB.addReg(RegArgs[i], RegState::Implicit);
2159
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002160 // Issue CALLSEQ_END
Evan Cheng194c3dc2011-06-28 21:14:33 +00002161 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindola73173c52012-07-25 15:42:45 +00002162 const unsigned NumBytesCallee = computeBytesPoppedByCallee(*Subtarget, CS);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002163 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
Eli Friedman7cd51012011-04-28 20:19:12 +00002164 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002165
Eli Friedman7b279422011-05-17 18:29:03 +00002166 // Build info for return calling conv lowering code.
2167 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
2168 SmallVector<ISD::InputArg, 32> Ins;
2169 SmallVector<EVT, 4> RetTys;
2170 ComputeValueVTs(TLI, I->getType(), RetTys);
2171 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
2172 EVT VT = RetTys[i];
Patrik Hagglundbad545c2012-12-19 11:48:16 +00002173 MVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
Eli Friedman7b279422011-05-17 18:29:03 +00002174 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
2175 for (unsigned j = 0; j != NumRegs; ++j) {
2176 ISD::InputArg MyFlags;
Patrik Hagglundbad545c2012-12-19 11:48:16 +00002177 MyFlags.VT = RegisterVT;
Eli Friedman7b279422011-05-17 18:29:03 +00002178 MyFlags.Used = !CS.getInstruction()->use_empty();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002179 if (CS.paramHasAttr(0, Attribute::SExt))
Eli Friedman7b279422011-05-17 18:29:03 +00002180 MyFlags.Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002181 if (CS.paramHasAttr(0, Attribute::ZExt))
Eli Friedman7b279422011-05-17 18:29:03 +00002182 MyFlags.Flags.setZExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002183 if (CS.paramHasAttr(0, Attribute::InReg))
Eli Friedman7b279422011-05-17 18:29:03 +00002184 MyFlags.Flags.setInReg();
2185 Ins.push_back(MyFlags);
2186 }
2187 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00002188
Eli Friedman7b279422011-05-17 18:29:03 +00002189 // Now handle call return values.
2190 SmallVector<unsigned, 4> UsedRegs;
2191 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002192 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00002193 I->getParent()->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00002194 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
2195 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
2196 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2197 EVT CopyVT = RVLocs[i].getValVT();
2198 unsigned CopyReg = ResultReg + i;
Wesley Peck527da1b2010-11-23 03:31:01 +00002199
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002200 // If this is a call to a function that returns an fp value on the x87 fp
2201 // stack, but where we prefer to use the value in xmm registers, copy it
2202 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman7b279422011-05-17 18:29:03 +00002203 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002204 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00002205 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002206 CopyVT = MVT::f80;
Craig Topperabadc662012-04-20 06:31:50 +00002207 CopyReg = createResultReg(&X86::RFP80RegClass);
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00002208 }
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002209 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::FpPOP_RETVAL),
2210 CopyReg);
2211 } else {
2212 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2213 CopyReg).addReg(RVLocs[i].getLocReg());
2214 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002215 }
2216
Eli Friedman7b279422011-05-17 18:29:03 +00002217 if (CopyVT != RVLocs[i].getValVT()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002218 // Round the F80 the right size, which also moves to the appropriate xmm
2219 // register. This is accomplished by storing the F80 value in memory and
2220 // then loading it back. Ewww...
Eli Friedman7b279422011-05-17 18:29:03 +00002221 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson9f944592009-08-11 20:47:22 +00002222 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002223 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene1fbe0542009-11-12 20:49:22 +00002224 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002225 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2226 TII.get(Opc)), FI)
Eli Friedman7b279422011-05-17 18:29:03 +00002227 .addReg(CopyReg);
Owen Anderson9f944592009-08-11 20:47:22 +00002228 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002229 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eli Friedman7b279422011-05-17 18:29:03 +00002230 TII.get(Opc), ResultReg + i), FI);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002231 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00002232 }
Eli Friedman83ba1502011-05-17 00:13:47 +00002233
Eli Friedman7b279422011-05-17 18:29:03 +00002234 if (RVLocs.size())
2235 UpdateValueMap(I, ResultReg, RVLocs.size());
2236
Dan Gohman86936502010-06-18 23:28:01 +00002237 // Set all unused physreg defs as dead.
2238 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2239
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002240 return true;
2241}
2242
2243
Dan Gohmand58f3e32008-08-28 23:21:34 +00002244bool
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002245X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohmand58f3e32008-08-28 23:21:34 +00002246 switch (I->getOpcode()) {
2247 default: break;
Evan Chenga41ee292008-09-03 06:44:39 +00002248 case Instruction::Load:
Dan Gohman7bda51f2008-09-03 23:12:08 +00002249 return X86SelectLoad(I);
Owen Andersonb8c7ba22008-09-04 16:48:33 +00002250 case Instruction::Store:
2251 return X86SelectStore(I);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002252 case Instruction::Ret:
2253 return X86SelectRet(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00002254 case Instruction::ICmp:
2255 case Instruction::FCmp:
2256 return X86SelectCmp(I);
Dan Gohmana5753b32008-09-05 01:06:14 +00002257 case Instruction::ZExt:
2258 return X86SelectZExt(I);
2259 case Instruction::Br:
2260 return X86SelectBranch(I);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002261 case Instruction::Call:
2262 return X86SelectCall(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00002263 case Instruction::LShr:
2264 case Instruction::AShr:
2265 case Instruction::Shl:
2266 return X86SelectShift(I);
Eli Bendersky24a36eb2013-04-17 20:10:13 +00002267 case Instruction::SDiv:
2268 case Instruction::UDiv:
2269 case Instruction::SRem:
2270 case Instruction::URem:
2271 return X86SelectDivRem(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00002272 case Instruction::Select:
2273 return X86SelectSelect(I);
Evan Chengb9286692008-09-07 08:47:42 +00002274 case Instruction::Trunc:
2275 return X86SelectTrunc(I);
Dan Gohmanbf646f22008-09-10 21:02:08 +00002276 case Instruction::FPExt:
2277 return X86SelectFPExt(I);
2278 case Instruction::FPTrunc:
2279 return X86SelectFPTrunc(I);
Dan Gohmana62e4ab2009-03-13 23:53:06 +00002280 case Instruction::IntToPtr: // Deliberate fall-through.
2281 case Instruction::PtrToInt: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002282 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
2283 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohmana62e4ab2009-03-13 23:53:06 +00002284 if (DstVT.bitsGT(SrcVT))
2285 return X86SelectZExt(I);
2286 if (DstVT.bitsLT(SrcVT))
2287 return X86SelectTrunc(I);
2288 unsigned Reg = getRegForValue(I->getOperand(0));
2289 if (Reg == 0) return false;
2290 UpdateValueMap(I, Reg);
2291 return true;
2292 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00002293 }
2294
2295 return false;
2296}
2297
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002298unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00002299 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00002300 if (!isTypeLegal(C->getType(), VT))
Michael Liao3c898062012-08-30 00:30:16 +00002301 return 0;
2302
2303 // Can't handle alternate code models yet.
2304 if (TM.getCodeModel() != CodeModel::Small)
2305 return 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00002306
Owen Anderson50288e32008-09-05 00:06:23 +00002307 // Get opcode and regclass of the output for the given load instruction.
2308 unsigned Opc = 0;
2309 const TargetRegisterClass *RC = NULL;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002310 switch (VT.SimpleTy) {
Michael Liao3c898062012-08-30 00:30:16 +00002311 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00002312 case MVT::i8:
Owen Anderson50288e32008-09-05 00:06:23 +00002313 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +00002314 RC = &X86::GR8RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002315 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002316 case MVT::i16:
Owen Anderson50288e32008-09-05 00:06:23 +00002317 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +00002318 RC = &X86::GR16RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002319 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002320 case MVT::i32:
Owen Anderson50288e32008-09-05 00:06:23 +00002321 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +00002322 RC = &X86::GR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002323 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002324 case MVT::i64:
Owen Anderson50288e32008-09-05 00:06:23 +00002325 // Must be in x86-64 mode.
2326 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +00002327 RC = &X86::GR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002328 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002329 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002330 if (X86ScalarSSEf32) {
2331 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +00002332 RC = &X86::FR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002333 } else {
2334 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +00002335 RC = &X86::RFP32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002336 }
2337 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002338 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002339 if (X86ScalarSSEf64) {
2340 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +00002341 RC = &X86::FR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002342 } else {
2343 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +00002344 RC = &X86::RFP64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002345 }
2346 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002347 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +00002348 // No f80 support yet.
Michael Liao3c898062012-08-30 00:30:16 +00002349 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00002350 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002351
Dan Gohman9801ba42008-09-19 22:16:54 +00002352 // Materialize addresses with LEA instructions.
Owen Anderson50288e32008-09-05 00:06:23 +00002353 if (isa<GlobalValue>(C)) {
Dan Gohman9801ba42008-09-19 22:16:54 +00002354 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00002355 if (X86SelectAddress(C, AM)) {
Chris Lattner48326602011-04-17 17:12:08 +00002356 // If the expression is just a basereg, then we're done, otherwise we need
2357 // to emit an LEA.
2358 if (AM.BaseType == X86AddressMode::RegBase &&
2359 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
2360 return AM.Base.Reg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002361
Chris Lattner48326602011-04-17 17:12:08 +00002362 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman9801ba42008-09-19 22:16:54 +00002363 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002364 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2365 TII.get(Opc), ResultReg), AM);
Owen Anderson50288e32008-09-05 00:06:23 +00002366 return ResultReg;
Dan Gohman9801ba42008-09-19 22:16:54 +00002367 }
Evan Chengf5bc7e52008-09-05 21:00:03 +00002368 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00002369 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002370
Owen Andersond41c7162008-09-06 01:11:01 +00002371 // MachineConstantPool wants an explicit alignment.
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002372 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00002373 if (Align == 0) {
2374 // Alignment of vector types. FIXME!
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00002375 Align = TD.getTypeAllocSize(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00002376 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002377
Dan Gohman8392f0c2008-09-30 01:21:32 +00002378 // x86-32 PIC requires a PIC base register for constant pools.
2379 unsigned PICBase = 0;
Chris Lattnera3260c02009-06-27 01:31:51 +00002380 unsigned char OpFlag = 0;
Chris Lattner21c29402009-07-10 21:00:45 +00002381 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattnerfef11d62009-07-09 04:39:06 +00002382 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohman87fb4e82010-07-07 16:29:44 +00002383 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00002384 } else if (Subtarget->isPICStyleGOT()) {
2385 OpFlag = X86II::MO_GOTOFF;
Dan Gohman87fb4e82010-07-07 16:29:44 +00002386 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00002387 } else if (Subtarget->isPICStyleRIPRel() &&
2388 TM.getCodeModel() == CodeModel::Small) {
2389 PICBase = X86::RIP;
Chris Lattnera3260c02009-06-27 01:31:51 +00002390 }
Dan Gohman8392f0c2008-09-30 01:21:32 +00002391
2392 // Create the load from the constant pool.
Dan Gohman39d82f92008-09-10 20:11:02 +00002393 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman9801ba42008-09-19 22:16:54 +00002394 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002395 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2396 TII.get(Opc), ResultReg),
Chris Lattnera3260c02009-06-27 01:31:51 +00002397 MCPOffset, PICBase, OpFlag);
Dan Gohman8392f0c2008-09-30 01:21:32 +00002398
Owen Anderson50288e32008-09-05 00:06:23 +00002399 return ResultReg;
2400}
2401
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002402unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002403 // Fail on dynamic allocas. At this point, getRegForValue has already
2404 // checked its CSE maps, so if we're here trying to handle a dynamic
2405 // alloca, we're not going to succeed. X86SelectAddress has a
2406 // check for dynamic allocas, because it's called directly from
2407 // various places, but TargetMaterializeAlloca also needs a check
2408 // in order to avoid recursion between getRegForValue,
2409 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohman87fb4e82010-07-07 16:29:44 +00002410 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002411 return 0;
2412
Dan Gohman39d82f92008-09-10 20:11:02 +00002413 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00002414 if (!X86SelectAddress(C, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +00002415 return 0;
2416 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
Craig Topper760b1342012-02-22 05:59:10 +00002417 const TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Dan Gohman39d82f92008-09-10 20:11:02 +00002418 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002419 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2420 TII.get(Opc), ResultReg), AM);
Dan Gohman39d82f92008-09-10 20:11:02 +00002421 return ResultReg;
2422}
2423
Eli Friedman406c4712011-04-27 22:41:55 +00002424unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2425 MVT VT;
2426 if (!isTypeLegal(CF->getType(), VT))
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002427 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00002428
2429 // Get opcode and regclass for the given zero.
2430 unsigned Opc = 0;
2431 const TargetRegisterClass *RC = NULL;
2432 switch (VT.SimpleTy) {
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002433 default: return 0;
Craig Topper490c45c2012-08-11 17:53:00 +00002434 case MVT::f32:
2435 if (X86ScalarSSEf32) {
2436 Opc = X86::FsFLD0SS;
2437 RC = &X86::FR32RegClass;
2438 } else {
2439 Opc = X86::LD_Fp032;
2440 RC = &X86::RFP32RegClass;
2441 }
2442 break;
2443 case MVT::f64:
2444 if (X86ScalarSSEf64) {
2445 Opc = X86::FsFLD0SD;
2446 RC = &X86::FR64RegClass;
2447 } else {
2448 Opc = X86::LD_Fp064;
2449 RC = &X86::RFP64RegClass;
2450 }
2451 break;
2452 case MVT::f80:
2453 // No f80 support yet.
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002454 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00002455 }
2456
2457 unsigned ResultReg = createResultReg(RC);
2458 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
2459 return ResultReg;
2460}
2461
2462
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002463bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2464 const LoadInst *LI) {
Chris Lattnereeba0c72010-09-05 02:18:34 +00002465 X86AddressMode AM;
2466 if (!X86SelectAddress(LI->getOperand(0), AM))
2467 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00002468
Craig Topper55406d92012-08-11 17:46:16 +00002469 const X86InstrInfo &XII = (const X86InstrInfo&)TII;
Wesley Peck527da1b2010-11-23 03:31:01 +00002470
Chris Lattnereeba0c72010-09-05 02:18:34 +00002471 unsigned Size = TD.getTypeAllocSize(LI->getType());
2472 unsigned Alignment = LI->getAlignment();
2473
2474 SmallVector<MachineOperand, 8> AddrOps;
2475 AM.getFullAddress(AddrOps);
Wesley Peck527da1b2010-11-23 03:31:01 +00002476
Chris Lattnereeba0c72010-09-05 02:18:34 +00002477 MachineInstr *Result =
2478 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
2479 if (Result == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00002480
Chris Lattner2d186572011-01-16 02:27:38 +00002481 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnereeba0c72010-09-05 02:18:34 +00002482 MI->eraseFromParent();
2483 return true;
2484}
2485
2486
Evan Cheng24422d42008-09-03 00:03:49 +00002487namespace llvm {
Bob Wilson3e6fa462012-08-03 04:06:28 +00002488 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
2489 const TargetLibraryInfo *libInfo) {
2490 return new X86FastISel(funcInfo, libInfo);
Evan Cheng24422d42008-09-03 00:03:49 +00002491 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00002492}