blob: 7419822b673a875193089dc6db764d4545451d57 [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
Craig Topper4f55b0e2013-07-17 05:57:45 +000082 bool X86FastEmitStore(EVT VT, const Value *Val, const X86AddressMode &AM,
83 bool Aligned = false);
84 bool X86FastEmitStore(EVT VT, unsigned ValReg, const X86AddressMode &AM,
85 bool Aligned = false);
Evan Cheng6500d172008-09-08 06:35:17 +000086
Owen Anderson53aa7a92009-08-10 22:56:29 +000087 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng6500d172008-09-08 06:35:17 +000088 unsigned &ResultReg);
Wesley Peck527da1b2010-11-23 03:31:01 +000089
Dan Gohmanbcaf6812010-04-15 01:51:59 +000090 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
91 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman39d82f92008-09-10 20:11:02 +000092
Dan Gohmanbcaf6812010-04-15 01:51:59 +000093 bool X86SelectLoad(const Instruction *I);
Wesley Peck527da1b2010-11-23 03:31:01 +000094
Dan Gohmanbcaf6812010-04-15 01:51:59 +000095 bool X86SelectStore(const Instruction *I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +000096
Dan Gohmand7b5ce32010-07-10 09:00:22 +000097 bool X86SelectRet(const Instruction *I);
98
Dan Gohmanbcaf6812010-04-15 01:51:59 +000099 bool X86SelectCmp(const Instruction *I);
Dan Gohmana5753b32008-09-05 01:06:14 +0000100
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000101 bool X86SelectZExt(const Instruction *I);
Dan Gohmana5753b32008-09-05 01:06:14 +0000102
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000103 bool X86SelectBranch(const Instruction *I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +0000104
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000105 bool X86SelectShift(const Instruction *I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +0000106
Eli Bendersky24a36eb2013-04-17 20:10:13 +0000107 bool X86SelectDivRem(const Instruction *I);
108
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000109 bool X86SelectSelect(const Instruction *I);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000110
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000111 bool X86SelectTrunc(const Instruction *I);
Wesley Peck527da1b2010-11-23 03:31:01 +0000112
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000113 bool X86SelectFPExt(const Instruction *I);
114 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohmanbf646f22008-09-10 21:02:08 +0000115
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000116 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
117 bool X86SelectCall(const Instruction *I);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000118
Eli Friedmancd2124a2011-06-10 23:39:36 +0000119 bool DoSelectCall(const Instruction *I, const char *MemIntName);
120
Dan Gohman3691d502008-09-25 15:24:26 +0000121 const X86InstrInfo *getInstrInfo() const {
Dan Gohman007a6bb2008-09-26 19:15:30 +0000122 return getTargetMachine()->getInstrInfo();
123 }
124 const X86TargetMachine *getTargetMachine() const {
125 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman3691d502008-09-25 15:24:26 +0000126 }
127
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000128 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman39d82f92008-09-10 20:11:02 +0000129
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000130 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000131
Eli Friedman406c4712011-04-27 22:41:55 +0000132 unsigned TargetMaterializeFloatZero(const ConstantFP *CF);
133
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000134 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
135 /// computed in an SSE register, not on the X87 floating point stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000136 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson9f944592009-08-11 20:47:22 +0000137 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
138 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000139 }
140
Chris Lattner229907c2011-07-18 04:54:35 +0000141 bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
Eli Friedmanbcc69142011-04-27 01:45:07 +0000142
Eli Friedman60afcc22011-05-20 22:21:04 +0000143 bool IsMemcpySmall(uint64_t Len);
144
Eli Friedmanbcc69142011-04-27 01:45:07 +0000145 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
146 X86AddressMode SrcAM, uint64_t Len);
Evan Cheng24422d42008-09-03 00:03:49 +0000147};
Wesley Peck527da1b2010-11-23 03:31:01 +0000148
Chris Lattnerd5ac9d82009-03-08 18:44:31 +0000149} // end anonymous namespace.
Dan Gohmand58f3e32008-08-28 23:21:34 +0000150
Chris Lattner229907c2011-07-18 04:54:35 +0000151bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000152 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
153 if (evt == MVT::Other || !evt.isSimple())
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000154 // Unhandled type. Halt "fast" selection and bail.
155 return false;
Duncan Sandsf5dda012010-11-03 11:35:31 +0000156
157 VT = evt.getSimpleVT();
Dan Gohman50331362008-09-30 00:48:39 +0000158 // For now, require SSE/SSE2 for performing floating-point operations,
159 // since x87 requires additional work.
Owen Anderson9f944592009-08-11 20:47:22 +0000160 if (VT == MVT::f64 && !X86ScalarSSEf64)
Craig Topper490c45c2012-08-11 17:53:00 +0000161 return false;
Owen Anderson9f944592009-08-11 20:47:22 +0000162 if (VT == MVT::f32 && !X86ScalarSSEf32)
Craig Topper490c45c2012-08-11 17:53:00 +0000163 return false;
Dan Gohman50331362008-09-30 00:48:39 +0000164 // Similarly, no f80 support yet.
Owen Anderson9f944592009-08-11 20:47:22 +0000165 if (VT == MVT::f80)
Dan Gohman50331362008-09-30 00:48:39 +0000166 return false;
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000167 // We only handle legal types. For example, on x86-32 the instruction
168 // selector contains all of the 64-bit instructions from x86-64,
169 // under the assumption that i64 won't be used if the target doesn't
170 // support it.
Owen Anderson9f944592009-08-11 20:47:22 +0000171 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000172}
173
174#include "X86GenCallingConv.inc"
175
Evan Chengf5bc7e52008-09-05 21:00:03 +0000176/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000177/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000178/// Return true and the result register by reference if it is possible.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000179bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Chengf5bc7e52008-09-05 21:00:03 +0000180 unsigned &ResultReg) {
181 // Get opcode and regclass of the output for the given load instruction.
182 unsigned Opc = 0;
183 const TargetRegisterClass *RC = NULL;
Owen Anderson9f944592009-08-11 20:47:22 +0000184 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000185 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000186 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +0000187 case MVT::i8:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000188 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +0000189 RC = &X86::GR8RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000190 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000191 case MVT::i16:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000192 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +0000193 RC = &X86::GR16RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000194 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000195 case MVT::i32:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000196 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +0000197 RC = &X86::GR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000198 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000199 case MVT::i64:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000200 // Must be in x86-64 mode.
201 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +0000202 RC = &X86::GR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000203 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000204 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000205 if (X86ScalarSSEf32) {
206 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +0000207 RC = &X86::FR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000208 } else {
209 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +0000210 RC = &X86::RFP32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000211 }
212 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000213 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000214 if (X86ScalarSSEf64) {
215 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +0000216 RC = &X86::FR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000217 } else {
218 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +0000219 RC = &X86::RFP64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000220 }
221 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000222 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +0000223 // No f80 support yet.
224 return false;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000225 }
226
227 ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000228 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
229 DL, TII.get(Opc), ResultReg), AM);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000230 return true;
231}
232
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000233/// X86FastEmitStore - Emit a machine instruction to store a value Val of
234/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
235/// and a displacement offset, or a GlobalAddress,
Evan Chengf5bc7e52008-09-05 21:00:03 +0000236/// i.e. V. Return true if it is possible.
237bool
Craig Topper4f55b0e2013-07-17 05:57:45 +0000238X86FastISel::X86FastEmitStore(EVT VT, unsigned ValReg,
239 const X86AddressMode &AM, bool Aligned) {
Dan Gohman8f658ba2008-09-08 16:31:35 +0000240 // Get opcode and regclass of the output for the given store instruction.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000241 unsigned Opc = 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000242 switch (VT.getSimpleVT().SimpleTy) {
243 case MVT::f80: // No f80 support yet.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000244 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000245 case MVT::i1: {
246 // Mask out all but lowest bit.
Craig Topperabadc662012-04-20 06:31:50 +0000247 unsigned AndResult = createResultReg(&X86::GR8RegClass);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000248 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Craig Topper4f55b0e2013-07-17 05:57:45 +0000249 TII.get(X86::AND8ri), AndResult).addReg(ValReg).addImm(1);
250 ValReg = AndResult;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000251 }
252 // FALLTHROUGH, handling i1 as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000253 case MVT::i8: Opc = X86::MOV8mr; break;
254 case MVT::i16: Opc = X86::MOV16mr; break;
255 case MVT::i32: Opc = X86::MOV32mr; break;
256 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
257 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000258 Opc = X86ScalarSSEf32 ?
259 (Subtarget->hasAVX() ? X86::VMOVSSmr : X86::MOVSSmr) : X86::ST_Fp32m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000260 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000261 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000262 Opc = X86ScalarSSEf64 ?
263 (Subtarget->hasAVX() ? X86::VMOVSDmr : X86::MOVSDmr) : X86::ST_Fp64m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000264 break;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000265 case MVT::v4f32:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000266 if (Aligned)
267 Opc = X86::MOVAPSmr;
268 else
269 Opc = X86::MOVUPSmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000270 break;
271 case MVT::v2f64:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000272 if (Aligned)
273 Opc = X86::MOVAPSmr;
274 else
275 Opc = X86::MOVUPSmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000276 break;
277 case MVT::v4i32:
278 case MVT::v2i64:
279 case MVT::v8i16:
280 case MVT::v16i8:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000281 if (Aligned)
282 Opc = X86::MOVDQAmr;
283 else
284 Opc = X86::MOVDQUmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000285 break;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000286 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000287
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000288 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Craig Topper4f55b0e2013-07-17 05:57:45 +0000289 DL, TII.get(Opc)), AM).addReg(ValReg);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000290 return true;
291}
292
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000293bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Craig Topper4f55b0e2013-07-17 05:57:45 +0000294 const X86AddressMode &AM, bool Aligned) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000295 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +0000296 if (isa<ConstantPointerNull>(Val))
297 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +0000298
Chris Lattner3ba29352008-10-15 05:30:52 +0000299 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000300 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000301 unsigned Opc = 0;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000302 bool Signed = true;
Owen Anderson9f944592009-08-11 20:47:22 +0000303 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000304 default: break;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000305 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000306 case MVT::i8: Opc = X86::MOV8mi; break;
307 case MVT::i16: Opc = X86::MOV16mi; break;
308 case MVT::i32: Opc = X86::MOV32mi; break;
309 case MVT::i64:
Chris Lattner3ba29352008-10-15 05:30:52 +0000310 // Must be a 32-bit sign extended value.
Jakub Staszak11d1aee2012-11-15 19:05:23 +0000311 if (isInt<32>(CI->getSExtValue()))
Chris Lattner3ba29352008-10-15 05:30:52 +0000312 Opc = X86::MOV64mi32;
313 break;
314 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000315
Chris Lattner3ba29352008-10-15 05:30:52 +0000316 if (Opc) {
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000317 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
318 DL, TII.get(Opc)), AM)
John McCall796583e2010-04-06 23:35:53 +0000319 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000320 CI->getZExtValue());
Chris Lattner3ba29352008-10-15 05:30:52 +0000321 return true;
322 }
323 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000324
Chris Lattner3ba29352008-10-15 05:30:52 +0000325 unsigned ValReg = getRegForValue(Val);
326 if (ValReg == 0)
Wesley Peck527da1b2010-11-23 03:31:01 +0000327 return false;
328
Craig Topper4f55b0e2013-07-17 05:57:45 +0000329 return X86FastEmitStore(VT, ValReg, AM, Aligned);
Chris Lattner3ba29352008-10-15 05:30:52 +0000330}
331
Evan Cheng6500d172008-09-08 06:35:17 +0000332/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
333/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
334/// ISD::SIGN_EXTEND).
Owen Anderson53aa7a92009-08-10 22:56:29 +0000335bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
336 unsigned Src, EVT SrcVT,
Evan Cheng6500d172008-09-08 06:35:17 +0000337 unsigned &ResultReg) {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000338 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
339 Src, /*TODO: Kill=*/false);
Jakub Staszak701cc972013-02-14 21:50:09 +0000340 if (RR == 0)
Owen Anderson453564b2008-09-11 19:44:55 +0000341 return false;
Jakub Staszak701cc972013-02-14 21:50:09 +0000342
343 ResultReg = RR;
344 return true;
Evan Cheng6500d172008-09-08 06:35:17 +0000345}
346
Dan Gohman39d82f92008-09-10 20:11:02 +0000347/// X86SelectAddress - Attempt to fill in an address from the given value.
348///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000349bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
350 const User *U = NULL;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000351 unsigned Opcode = Instruction::UserOp1;
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000352 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanaf4903d2010-06-18 20:44:47 +0000353 // Don't walk into other basic blocks; it's possible we haven't
354 // visited them yet, so the instructions may not yet be assigned
355 // virtual registers.
Dan Gohmanaeb5e662010-11-16 22:43:23 +0000356 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
357 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
358 Opcode = I->getOpcode();
359 U = I;
360 }
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000361 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman6e005fd2008-09-18 23:23:44 +0000362 Opcode = C->getOpcode();
363 U = C;
364 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000365
Chris Lattner229907c2011-07-18 04:54:35 +0000366 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
Chris Lattner874c92b2010-06-15 19:08:40 +0000367 if (Ty->getAddressSpace() > 255)
Dan Gohmana46d6072010-06-18 20:45:41 +0000368 // Fast instruction selection doesn't support the special
369 // address spaces.
Chris Lattner874c92b2010-06-15 19:08:40 +0000370 return false;
371
Dan Gohman6e005fd2008-09-18 23:23:44 +0000372 switch (Opcode) {
373 default: break;
374 case Instruction::BitCast:
375 // Look past bitcasts.
Chris Lattner8212d372009-07-10 05:33:42 +0000376 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman6e005fd2008-09-18 23:23:44 +0000377
378 case Instruction::IntToPtr:
379 // Look past no-op inttoptrs.
380 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000381 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000382 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000383
384 case Instruction::PtrToInt:
385 // Look past no-op ptrtoints.
386 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000387 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000388 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000389
390 case Instruction::Alloca: {
391 // Do static allocas.
392 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman87fb4e82010-07-07 16:29:44 +0000393 DenseMap<const AllocaInst*, int>::iterator SI =
394 FuncInfo.StaticAllocaMap.find(A);
395 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman007a6bb2008-09-26 19:15:30 +0000396 AM.BaseType = X86AddressMode::FrameIndexBase;
397 AM.Base.FrameIndex = SI->second;
398 return true;
399 }
400 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000401 }
402
403 case Instruction::Add: {
404 // Adds of constants are common and easy enough.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000405 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman2564b902008-09-26 20:04:15 +0000406 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
407 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000408 if (isInt<32>(Disp)) {
Dan Gohman2564b902008-09-26 20:04:15 +0000409 AM.Disp = (uint32_t)Disp;
Chris Lattner8212d372009-07-10 05:33:42 +0000410 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman2564b902008-09-26 20:04:15 +0000411 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000412 }
Dan Gohman6e005fd2008-09-18 23:23:44 +0000413 break;
414 }
415
416 case Instruction::GetElementPtr: {
Chris Lattner795667b2010-03-04 19:54:45 +0000417 X86AddressMode SavedAM = AM;
418
Dan Gohman6e005fd2008-09-18 23:23:44 +0000419 // Pattern-match simple GEPs.
Dan Gohman2564b902008-09-26 20:04:15 +0000420 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000421 unsigned IndexReg = AM.IndexReg;
422 unsigned Scale = AM.Scale;
423 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohman4c315242008-12-08 07:57:47 +0000424 // Iterate through the indices, folding what we can. Constants can be
425 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000426 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman6e005fd2008-09-18 23:23:44 +0000427 i != e; ++i, ++GTI) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000428 const Value *Op = *i;
Chris Lattner229907c2011-07-18 04:54:35 +0000429 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Dan Gohman6e005fd2008-09-18 23:23:44 +0000430 const StructLayout *SL = TD.getStructLayout(STy);
Chris Lattner4b026b92011-04-17 17:05:12 +0000431 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
432 continue;
433 }
Eric Christopher0713a9d2011-06-08 23:55:35 +0000434
Chris Lattner4b026b92011-04-17 17:05:12 +0000435 // A array/variable index is always of the form i*S where S is the
436 // constant scale size. See if we can push the scale into immediates.
437 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
438 for (;;) {
439 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
440 // Constant-offset addressing.
441 Disp += CI->getSExtValue() * S;
442 break;
Dan Gohmanc1783b32011-03-22 00:04:35 +0000443 }
Chris Lattner4b026b92011-04-17 17:05:12 +0000444 if (isa<AddOperator>(Op) &&
445 (!isa<Instruction>(Op) ||
446 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
447 == FuncInfo.MBB) &&
448 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
449 // An add (in the same block) with a constant operand. Fold the
450 // constant.
451 ConstantInt *CI =
452 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
453 Disp += CI->getSExtValue() * S;
454 // Iterate on the other operand.
455 Op = cast<AddOperator>(Op)->getOperand(0);
456 continue;
457 }
458 if (IndexReg == 0 &&
459 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
460 (S == 1 || S == 2 || S == 4 || S == 8)) {
461 // Scaled-index addressing.
462 Scale = S;
463 IndexReg = getRegForGEPIndex(Op).first;
464 if (IndexReg == 0)
465 return false;
466 break;
467 }
468 // Unsupported.
469 goto unsupported_gep;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000470 }
471 }
Dan Gohman2564b902008-09-26 20:04:15 +0000472 // Check for displacement overflow.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000473 if (!isInt<32>(Disp))
Dan Gohman2564b902008-09-26 20:04:15 +0000474 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000475 // Ok, the GEP indices were covered by constant-offset and scaled-index
476 // addressing. Update the address state and move on to examining the base.
477 AM.IndexReg = IndexReg;
478 AM.Scale = Scale;
Dan Gohman2564b902008-09-26 20:04:15 +0000479 AM.Disp = (uint32_t)Disp;
Chris Lattner6ce8e242010-03-04 19:48:19 +0000480 if (X86SelectAddress(U->getOperand(0), AM))
481 return true;
Wesley Peck527da1b2010-11-23 03:31:01 +0000482
Chris Lattner4b026b92011-04-17 17:05:12 +0000483 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner6ce8e242010-03-04 19:48:19 +0000484 // our address and just match the value instead of completely failing.
485 AM = SavedAM;
486 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000487 unsupported_gep:
488 // Ok, the GEP indices weren't all covered.
489 break;
490 }
491 }
492
493 // Handle constant address.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000494 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Eli Friedman87c844c2011-09-22 23:41:28 +0000495 // Can't handle alternate code models yet.
Chris Lattner25e7f912009-07-10 21:03:06 +0000496 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman3691d502008-09-25 15:24:26 +0000497 return false;
498
Eli Friedman87c844c2011-09-22 23:41:28 +0000499 // Can't handle TLS yet.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000500 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohman318d7372009-02-23 22:03:08 +0000501 if (GVar->isThreadLocal())
502 return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000503
Eli Friedman87c844c2011-09-22 23:41:28 +0000504 // Can't handle TLS yet, part 2 (this is slightly crazy, but this is how
505 // it works...).
506 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
507 if (const GlobalVariable *GVar =
508 dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false)))
509 if (GVar->isThreadLocal())
510 return false;
511
Chris Lattnereb729d42011-04-17 17:47:38 +0000512 // RIP-relative addresses can't have additional register operands, so if
513 // we've already folded stuff into the addressing mode, just force the
514 // global value into its own register, which we can use as the basereg.
515 if (!Subtarget->isPICStyleRIPRel() ||
516 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
517 // Okay, we've committed to selecting this global. Set up the address.
518 AM.GV = GV;
Dan Gohman318d7372009-02-23 22:03:08 +0000519
Chris Lattnereb729d42011-04-17 17:47:38 +0000520 // Allow the subtarget to classify the global.
521 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
Wesley Peck527da1b2010-11-23 03:31:01 +0000522
Chris Lattnereb729d42011-04-17 17:47:38 +0000523 // If this reference is relative to the pic base, set it now.
524 if (isGlobalRelativeToPICBase(GVFlags)) {
525 // FIXME: How do we know Base.Reg is free??
526 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Dan Gohman098786e2008-09-19 23:42:04 +0000527 }
Chris Lattnereb729d42011-04-17 17:47:38 +0000528
529 // Unless the ABI requires an extra load, return a direct reference to
530 // the global.
531 if (!isGlobalStubReference(GVFlags)) {
532 if (Subtarget->isPICStyleRIPRel()) {
533 // Use rip-relative addressing if we can. Above we verified that the
534 // base and index registers are unused.
535 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
536 AM.Base.Reg = X86::RIP;
537 }
538 AM.GVOpFlags = GVFlags;
539 return true;
540 }
541
542 // Ok, we need to do a load from a stub. If we've already loaded from
543 // this stub, reuse the loaded pointer, otherwise emit the load now.
544 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
545 unsigned LoadReg;
546 if (I != LocalValueMap.end() && I->second != 0) {
547 LoadReg = I->second;
548 } else {
549 // Issue load from stub.
550 unsigned Opc = 0;
551 const TargetRegisterClass *RC = NULL;
552 X86AddressMode StubAM;
553 StubAM.Base.Reg = AM.Base.Reg;
554 StubAM.GV = GV;
555 StubAM.GVOpFlags = GVFlags;
556
557 // Prepare for inserting code in the local-value area.
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000558 SavePoint SaveInsertPt = enterLocalValueArea();
Chris Lattnereb729d42011-04-17 17:47:38 +0000559
560 if (TLI.getPointerTy() == MVT::i64) {
561 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +0000562 RC = &X86::GR64RegClass;
Chris Lattnereb729d42011-04-17 17:47:38 +0000563
564 if (Subtarget->isPICStyleRIPRel())
565 StubAM.Base.Reg = X86::RIP;
566 } else {
567 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +0000568 RC = &X86::GR32RegClass;
Chris Lattnereb729d42011-04-17 17:47:38 +0000569 }
570
571 LoadReg = createResultReg(RC);
572 MachineInstrBuilder LoadMI =
573 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
574 addFullAddress(LoadMI, StubAM);
575
576 // Ok, back to normal mode.
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000577 leaveLocalValueArea(SaveInsertPt);
Chris Lattnereb729d42011-04-17 17:47:38 +0000578
579 // Prevent loading GV stub multiple times in same MBB.
580 LocalValueMap[V] = LoadReg;
581 }
582
583 // Now construct the final address. Note that the Disp, Scale,
584 // and Index values may already be set here.
585 AM.Base.Reg = LoadReg;
586 AM.GV = 0;
Chris Lattner770e0422009-07-09 06:41:35 +0000587 return true;
588 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000589 }
590
Dan Gohman007a6bb2008-09-26 19:15:30 +0000591 // If all else fails, try to materialize the value in a register.
Chris Lattnercce15892009-06-27 05:24:12 +0000592 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman007a6bb2008-09-26 19:15:30 +0000593 if (AM.Base.Reg == 0) {
594 AM.Base.Reg = getRegForValue(V);
595 return AM.Base.Reg != 0;
596 }
597 if (AM.IndexReg == 0) {
598 assert(AM.Scale == 1 && "Scale with no index!");
599 AM.IndexReg = getRegForValue(V);
600 return AM.IndexReg != 0;
601 }
602 }
603
604 return false;
Dan Gohman39d82f92008-09-10 20:11:02 +0000605}
606
Chris Lattner8212d372009-07-10 05:33:42 +0000607/// X86SelectCallAddress - Attempt to fill in an address from the given value.
608///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000609bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
610 const User *U = NULL;
Chris Lattner8212d372009-07-10 05:33:42 +0000611 unsigned Opcode = Instruction::UserOp1;
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000612 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000613 Opcode = I->getOpcode();
614 U = I;
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000615 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000616 Opcode = C->getOpcode();
617 U = C;
618 }
619
620 switch (Opcode) {
621 default: break;
622 case Instruction::BitCast:
623 // Look past bitcasts.
624 return X86SelectCallAddress(U->getOperand(0), AM);
625
626 case Instruction::IntToPtr:
627 // Look past no-op inttoptrs.
628 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
629 return X86SelectCallAddress(U->getOperand(0), AM);
630 break;
631
632 case Instruction::PtrToInt:
633 // Look past no-op ptrtoints.
634 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
635 return X86SelectCallAddress(U->getOperand(0), AM);
636 break;
637 }
638
639 // Handle constant address.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000640 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000641 // Can't handle alternate code models yet.
Chris Lattner25e7f912009-07-10 21:03:06 +0000642 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner8212d372009-07-10 05:33:42 +0000643 return false;
644
645 // RIP-relative addresses can't have additional register operands.
646 if (Subtarget->isPICStyleRIPRel() &&
647 (AM.Base.Reg != 0 || AM.IndexReg != 0))
648 return false;
649
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000650 // Can't handle DLLImport.
651 if (GV->hasDLLImportLinkage())
652 return false;
653
654 // Can't handle TLS.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000655 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000656 if (GVar->isThreadLocal())
Chris Lattner8212d372009-07-10 05:33:42 +0000657 return false;
658
659 // Okay, we've committed to selecting this global. Set up the basic address.
660 AM.GV = GV;
Wesley Peck527da1b2010-11-23 03:31:01 +0000661
Chris Lattner7277a802009-07-10 05:45:15 +0000662 // No ABI requires an extra load for anything other than DLLImport, which
663 // we rejected above. Return a direct reference to the global.
Chris Lattner7277a802009-07-10 05:45:15 +0000664 if (Subtarget->isPICStyleRIPRel()) {
665 // Use rip-relative addressing if we can. Above we verified that the
666 // base and index registers are unused.
667 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
668 AM.Base.Reg = X86::RIP;
Chris Lattner21c29402009-07-10 21:00:45 +0000669 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattner7277a802009-07-10 05:45:15 +0000670 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
671 } else if (Subtarget->isPICStyleGOT()) {
672 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner8212d372009-07-10 05:33:42 +0000673 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000674
Chris Lattner8212d372009-07-10 05:33:42 +0000675 return true;
676 }
677
678 // If all else fails, try to materialize the value in a register.
679 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
680 if (AM.Base.Reg == 0) {
681 AM.Base.Reg = getRegForValue(V);
682 return AM.Base.Reg != 0;
683 }
684 if (AM.IndexReg == 0) {
685 assert(AM.Scale == 1 && "Scale with no index!");
686 AM.IndexReg = getRegForValue(V);
687 return AM.IndexReg != 0;
688 }
689 }
690
691 return false;
692}
693
694
Owen Anderson4f948bd2008-09-04 07:08:58 +0000695/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000696bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000697 // Atomic stores need special handling.
Lang Hames7d2f7b52011-10-18 22:11:33 +0000698 const StoreInst *S = cast<StoreInst>(I);
699
700 if (S->isAtomic())
701 return false;
702
Craig Topper4f55b0e2013-07-17 05:57:45 +0000703 unsigned SABIAlignment =
704 TD.getABITypeAlignment(S->getValueOperand()->getType());
705 bool Aligned = S->getAlignment() == 0 || S->getAlignment() >= SABIAlignment;
706
Duncan Sandsf5dda012010-11-03 11:35:31 +0000707 MVT VT;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000708 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Anderson4f948bd2008-09-04 07:08:58 +0000709 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000710
Dan Gohman39d82f92008-09-10 20:11:02 +0000711 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +0000712 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman39d82f92008-09-10 20:11:02 +0000713 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000714
Craig Topper4f55b0e2013-07-17 05:57:45 +0000715 return X86FastEmitStore(VT, I->getOperand(0), AM, Aligned);
Owen Anderson4f948bd2008-09-04 07:08:58 +0000716}
717
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000718/// X86SelectRet - Select and emit code to implement ret instructions.
719bool X86FastISel::X86SelectRet(const Instruction *I) {
720 const ReturnInst *Ret = cast<ReturnInst>(I);
721 const Function &F = *I->getParent()->getParent();
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000722 const X86MachineFunctionInfo *X86MFInfo =
723 FuncInfo.MF->getInfo<X86MachineFunctionInfo>();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000724
725 if (!FuncInfo.CanLowerReturn)
726 return false;
727
728 CallingConv::ID CC = F.getCallingConv();
729 if (CC != CallingConv::C &&
730 CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +0000731 CC != CallingConv::X86_FastCall &&
732 CC != CallingConv::X86_64_SysV)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000733 return false;
734
Charles Davise8f297c2013-07-12 06:02:35 +0000735 if (Subtarget->isCallingConvWin64(CC))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000736 return false;
737
738 // Don't handle popping bytes on return for now.
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000739 if (X86MFInfo->getBytesToPopOnReturn() != 0)
Jakub Staszak74010cd2013-02-17 18:35:25 +0000740 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000741
742 // fastcc with -tailcallopt is intended to provide a guaranteed
743 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000744 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000745 return false;
746
747 // Let SDISel handle vararg functions.
748 if (F.isVarArg())
749 return false;
750
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000751 // Build a list of return value registers.
752 SmallVector<unsigned, 4> RetRegs;
753
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000754 if (Ret->getNumOperands() > 0) {
755 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +0000756 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000757
758 // Analyze operands of the call, assigning locations to each operand.
759 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000760 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +0000761 I->getContext());
Duncan Sandsfa7e6f22010-10-31 13:02:38 +0000762 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000763
764 const Value *RV = Ret->getOperand(0);
765 unsigned Reg = getRegForValue(RV);
766 if (Reg == 0)
767 return false;
768
769 // Only handle a single return value for now.
770 if (ValLocs.size() != 1)
771 return false;
772
773 CCValAssign &VA = ValLocs[0];
Wesley Peck527da1b2010-11-23 03:31:01 +0000774
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000775 // Don't bother handling odd stuff for now.
776 if (VA.getLocInfo() != CCValAssign::Full)
777 return false;
778 // Only handle register returns for now.
779 if (!VA.isRegLoc())
780 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000781
782 // The calling-convention tables for x87 returns don't tell
783 // the whole story.
784 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
785 return false;
786
Eli Friedman6fc94dd2011-05-18 23:13:10 +0000787 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedman22da7992011-05-19 22:16:13 +0000788 EVT SrcVT = TLI.getValueType(RV->getType());
789 EVT DstVT = VA.getValVT();
790 // Special handling for extended integers.
791 if (SrcVT != DstVT) {
792 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
793 return false;
794
795 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
796 return false;
797
798 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
799
800 if (SrcVT == MVT::i1) {
801 if (Outs[0].Flags.isSExt())
802 return false;
803 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
804 SrcVT = MVT::i8;
805 }
806 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
807 ISD::SIGN_EXTEND;
808 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
809 SrcReg, /*TODO: Kill=*/false);
810 }
811
812 // Make the copy.
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000813 unsigned DstReg = VA.getLocReg();
814 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000815 // Avoid a cross-class copy. This is very unlikely.
816 if (!SrcRC->contains(DstReg))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000817 return false;
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000818 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
819 DstReg).addReg(SrcReg);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000820
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000821 // Add register to return instruction.
822 RetRegs.push_back(VA.getLocReg());
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000823 }
824
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000825 // The x86-64 ABI for returning structs by value requires that we copy
826 // the sret argument into %rax for the return. We saved the argument into
827 // a virtual register in the entry block, so now we copy the value out
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000828 // and into %rax. We also do the same with %eax for Win32.
829 if (F.hasStructRetAttr() &&
830 (Subtarget->is64Bit() || Subtarget->isTargetWindows())) {
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000831 unsigned Reg = X86MFInfo->getSRetReturnReg();
832 assert(Reg &&
833 "SRetReturnReg should have been set in LowerFormalArguments()!");
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000834 unsigned RetReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000835 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000836 RetReg).addReg(Reg);
837 RetRegs.push_back(RetReg);
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000838 }
839
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000840 // Now emit the RET.
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000841 MachineInstrBuilder MIB =
842 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
843 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
844 MIB.addReg(RetRegs[i], RegState::Implicit);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000845 return true;
846}
847
Evan Chenga41ee292008-09-03 06:44:39 +0000848/// X86SelectLoad - Select and emit code to implement load instructions.
849///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000850bool X86FastISel::X86SelectLoad(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000851 // Atomic loads need special handling.
852 if (cast<LoadInst>(I)->isAtomic())
853 return false;
854
Duncan Sandsf5dda012010-11-03 11:35:31 +0000855 MVT VT;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000856 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Chenga41ee292008-09-03 06:44:39 +0000857 return false;
858
Dan Gohman39d82f92008-09-10 20:11:02 +0000859 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +0000860 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman39d82f92008-09-10 20:11:02 +0000861 return false;
Evan Chenga41ee292008-09-03 06:44:39 +0000862
Evan Chengf5bc7e52008-09-05 21:00:03 +0000863 unsigned ResultReg = 0;
Dan Gohman39d82f92008-09-10 20:11:02 +0000864 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000865 UpdateValueMap(I, ResultReg);
866 return true;
Evan Chenga41ee292008-09-03 06:44:39 +0000867 }
Evan Chengf5bc7e52008-09-05 21:00:03 +0000868 return false;
Evan Chenga41ee292008-09-03 06:44:39 +0000869}
870
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +0000871static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000872 bool HasAVX = Subtarget->hasAVX();
Craig Topperb0c0f722012-01-10 06:54:16 +0000873 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
874 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000875
Owen Anderson9f944592009-08-11 20:47:22 +0000876 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner74e01282008-10-15 04:32:45 +0000877 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000878 case MVT::i8: return X86::CMP8rr;
879 case MVT::i16: return X86::CMP16rr;
880 case MVT::i32: return X86::CMP32rr;
881 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000882 case MVT::f32:
883 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
884 case MVT::f64:
885 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohman1ab1d312008-10-02 22:15:21 +0000886 }
Dan Gohman1ab1d312008-10-02 22:15:21 +0000887}
888
Chris Lattner88f47542008-10-15 04:13:29 +0000889/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
890/// of the comparison, return an opcode that works for the compare (e.g.
891/// CMP32ri) otherwise return 0.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000892static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson9f944592009-08-11 20:47:22 +0000893 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner88f47542008-10-15 04:13:29 +0000894 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner74e01282008-10-15 04:32:45 +0000895 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000896 case MVT::i8: return X86::CMP8ri;
897 case MVT::i16: return X86::CMP16ri;
898 case MVT::i32: return X86::CMP32ri;
899 case MVT::i64:
Chris Lattner74e01282008-10-15 04:32:45 +0000900 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
901 // field.
Chris Lattner3ba29352008-10-15 05:30:52 +0000902 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner74e01282008-10-15 04:32:45 +0000903 return X86::CMP64ri32;
904 return 0;
905 }
Chris Lattner88f47542008-10-15 04:13:29 +0000906}
907
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000908bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
909 EVT VT) {
Chris Lattnerd46b9512008-10-15 04:26:38 +0000910 unsigned Op0Reg = getRegForValue(Op0);
911 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000912
Chris Lattnere388725a2008-10-15 05:18:04 +0000913 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +0000914 if (isa<ConstantPointerNull>(Op1))
915 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +0000916
Chris Lattnerd46b9512008-10-15 04:26:38 +0000917 // We have two options: compare with register or immediate. If the RHS of
918 // the compare is an immediate that we can fold into this compare, use
919 // CMPri, otherwise use CMPrr.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000920 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner74e01282008-10-15 04:32:45 +0000921 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000922 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
923 .addReg(Op0Reg)
924 .addImm(Op1C->getSExtValue());
Chris Lattnerd46b9512008-10-15 04:26:38 +0000925 return true;
926 }
927 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000928
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +0000929 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattnerd46b9512008-10-15 04:26:38 +0000930 if (CompareOpc == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000931
Chris Lattnerd46b9512008-10-15 04:26:38 +0000932 unsigned Op1Reg = getRegForValue(Op1);
933 if (Op1Reg == 0) return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000934 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
935 .addReg(Op0Reg)
936 .addReg(Op1Reg);
Wesley Peck527da1b2010-11-23 03:31:01 +0000937
Chris Lattnerd46b9512008-10-15 04:26:38 +0000938 return true;
939}
940
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000941bool X86FastISel::X86SelectCmp(const Instruction *I) {
942 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000943
Duncan Sandsf5dda012010-11-03 11:35:31 +0000944 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +0000945 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman09faf812008-09-05 01:33:56 +0000946 return false;
947
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000948 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattnera3596db2008-10-15 03:47:17 +0000949 unsigned SetCCOpc;
Chris Lattnerf32ce222008-10-15 03:52:54 +0000950 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000951 switch (CI->getPredicate()) {
952 case CmpInst::FCMP_OEQ: {
Chris Lattnerdc1c3802008-10-15 04:29:23 +0000953 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
954 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000955
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000956 unsigned EReg = createResultReg(&X86::GR8RegClass);
957 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000958 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
959 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
960 TII.get(X86::SETNPr), NPReg);
Wesley Peck527da1b2010-11-23 03:31:01 +0000961 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen9bba9022009-02-13 02:33:27 +0000962 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattnera3596db2008-10-15 03:47:17 +0000963 UpdateValueMap(I, ResultReg);
964 return true;
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000965 }
966 case CmpInst::FCMP_UNE: {
Chris Lattnerdc1c3802008-10-15 04:29:23 +0000967 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
968 return false;
969
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000970 unsigned NEReg = createResultReg(&X86::GR8RegClass);
971 unsigned PReg = createResultReg(&X86::GR8RegClass);
Chris Lattner2c8a4c32011-04-19 04:22:17 +0000972 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETNEr), NEReg);
973 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETPr), PReg);
974 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::OR8rr),ResultReg)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000975 .addReg(PReg).addReg(NEReg);
Chris Lattnera3596db2008-10-15 03:47:17 +0000976 UpdateValueMap(I, ResultReg);
977 return true;
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000978 }
Chris Lattnerf32ce222008-10-15 03:52:54 +0000979 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
980 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
981 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
982 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
983 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
984 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
985 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
986 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
987 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
988 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
989 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
990 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peck527da1b2010-11-23 03:31:01 +0000991
Chris Lattnerf32ce222008-10-15 03:52:54 +0000992 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
993 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
994 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
995 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
996 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
997 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
998 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
999 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
1000 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
1001 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001002 default:
1003 return false;
1004 }
1005
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001006 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattnerf32ce222008-10-15 03:52:54 +00001007 if (SwapArgs)
Chris Lattnerd46b9512008-10-15 04:26:38 +00001008 std::swap(Op0, Op1);
Chris Lattnerf32ce222008-10-15 03:52:54 +00001009
Chris Lattnerd46b9512008-10-15 04:26:38 +00001010 // Emit a compare of Op0/Op1.
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001011 if (!X86FastEmitCompare(Op0, Op1, VT))
1012 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001013
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001014 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001015 UpdateValueMap(I, ResultReg);
1016 return true;
1017}
Evan Chenga41ee292008-09-03 06:44:39 +00001018
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001019bool X86FastISel::X86SelectZExt(const Instruction *I) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001020 EVT DstVT = TLI.getValueType(I->getType());
1021 if (!TLI.isTypeLegal(DstVT))
1022 return false;
1023
1024 unsigned ResultReg = getRegForValue(I->getOperand(0));
1025 if (ResultReg == 0)
1026 return false;
1027
Tim Northover04eb4232013-05-30 10:43:18 +00001028 // Handle zero-extension from i1 to i8, which is common.
1029 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType()).getSimpleVT();
1030 if (SrcVT.SimpleTy == MVT::i1) {
1031 // Set the high bits to zero.
1032 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1033 SrcVT = MVT::i8;
Eli Friedmanc7035512011-05-25 23:49:02 +00001034
Tim Northover04eb4232013-05-30 10:43:18 +00001035 if (ResultReg == 0)
1036 return false;
1037 }
1038
1039 if (DstVT == MVT::i64) {
1040 // Handle extension to 64-bits via sub-register shenanigans.
1041 unsigned MovInst;
1042
1043 switch (SrcVT.SimpleTy) {
1044 case MVT::i8: MovInst = X86::MOVZX32rr8; break;
1045 case MVT::i16: MovInst = X86::MOVZX32rr16; break;
1046 case MVT::i32: MovInst = X86::MOV32rr; break;
1047 default: llvm_unreachable("Unexpected zext to i64 source type");
1048 }
1049
1050 unsigned Result32 = createResultReg(&X86::GR32RegClass);
1051 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovInst), Result32)
1052 .addReg(ResultReg);
1053
1054 ResultReg = createResultReg(&X86::GR64RegClass);
1055 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::SUBREG_TO_REG),
1056 ResultReg)
1057 .addImm(0).addReg(Result32).addImm(X86::sub_32bit);
1058 } else if (DstVT != MVT::i8) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001059 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1060 ResultReg, /*Kill=*/true);
1061 if (ResultReg == 0)
1062 return false;
Dan Gohmana5753b32008-09-05 01:06:14 +00001063 }
1064
Eli Friedmanc7035512011-05-25 23:49:02 +00001065 UpdateValueMap(I, ResultReg);
1066 return true;
Dan Gohmana5753b32008-09-05 01:06:14 +00001067}
1068
Chris Lattnerd46b9512008-10-15 04:26:38 +00001069
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001070bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmana5753b32008-09-05 01:06:14 +00001071 // Unconditional branches are selected by tablegen-generated code.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001072 // Handle a conditional branch.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001073 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohman87fb4e82010-07-07 16:29:44 +00001074 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1075 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmana5753b32008-09-05 01:06:14 +00001076
Dan Gohman42ef6692010-08-21 02:32:36 +00001077 // Fold the common case of a conditional branch with a comparison
1078 // in the same block (values defined on other blocks may not have
1079 // initialized registers).
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001080 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman42ef6692010-08-21 02:32:36 +00001081 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001082 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmana5753b32008-09-05 01:06:14 +00001083
Dan Gohman1ab1d312008-10-02 22:15:21 +00001084 // Try to take advantage of fallthrough opportunities.
1085 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001086 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohman1ab1d312008-10-02 22:15:21 +00001087 std::swap(TrueMBB, FalseMBB);
1088 Predicate = CmpInst::getInversePredicate(Predicate);
1089 }
1090
Chris Lattner0ce717a2008-10-15 03:58:05 +00001091 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
1092 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
1093
Dan Gohman1ab1d312008-10-02 22:15:21 +00001094 switch (Predicate) {
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001095 case CmpInst::FCMP_OEQ:
1096 std::swap(TrueMBB, FalseMBB);
1097 Predicate = CmpInst::FCMP_UNE;
1098 // FALL THROUGH
Chris Lattner2b0a7a22010-02-11 19:25:55 +00001099 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1100 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1101 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1102 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
1103 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
1104 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1105 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
1106 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
1107 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1108 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
1109 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
1110 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1111 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peck527da1b2010-11-23 03:31:01 +00001112
Chris Lattner2b0a7a22010-02-11 19:25:55 +00001113 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1114 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1115 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1116 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1117 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1118 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1119 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
1120 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1121 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1122 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001123 default:
1124 return false;
1125 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001126
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001127 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner47bef252008-10-15 04:02:26 +00001128 if (SwapArgs)
1129 std::swap(Op0, Op1);
1130
Chris Lattnerd46b9512008-10-15 04:26:38 +00001131 // Emit a compare of the LHS and RHS, setting the flags.
1132 if (!X86FastEmitCompare(Op0, Op1, VT))
1133 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001134
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001135 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1136 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001137
1138 if (Predicate == CmpInst::FCMP_UNE) {
1139 // X86 requires a second branch to handle UNE (and OEQ,
1140 // which is mapped to UNE above).
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001141 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1142 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001143 }
1144
Stuart Hastings0125b642010-06-17 22:43:56 +00001145 FastEmitBranch(FalseMBB, DL);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001146 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohman1ab1d312008-10-02 22:15:21 +00001147 return true;
1148 }
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001149 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1150 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1151 // typically happen for _Bool and C++ bools.
1152 MVT SourceVT;
1153 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1154 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1155 unsigned TestOpc = 0;
1156 switch (SourceVT.SimpleTy) {
1157 default: break;
1158 case MVT::i8: TestOpc = X86::TEST8ri; break;
1159 case MVT::i16: TestOpc = X86::TEST16ri; break;
1160 case MVT::i32: TestOpc = X86::TEST32ri; break;
1161 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1162 }
1163 if (TestOpc) {
1164 unsigned OpReg = getRegForValue(TI->getOperand(0));
1165 if (OpReg == 0) return false;
1166 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TestOpc))
1167 .addReg(OpReg).addImm(1);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001168
Chris Lattnerc59290a2011-04-19 04:26:32 +00001169 unsigned JmpOpc = X86::JNE_4;
1170 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1171 std::swap(TrueMBB, FalseMBB);
1172 JmpOpc = X86::JE_4;
1173 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001174
Chris Lattnerc59290a2011-04-19 04:26:32 +00001175 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(JmpOpc))
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001176 .addMBB(TrueMBB);
1177 FastEmitBranch(FalseMBB, DL);
1178 FuncInfo.MBB->addSuccessor(TrueMBB);
1179 return true;
1180 }
1181 }
Dan Gohman1ab1d312008-10-02 22:15:21 +00001182 }
1183
1184 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman0eea0292011-04-27 01:34:27 +00001185 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1186 // in an explicit cast, so make sure to handle that correctly.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001187 unsigned OpReg = getRegForValue(BI->getCondition());
1188 if (OpReg == 0) return false;
1189
Eli Friedman0eea0292011-04-27 01:34:27 +00001190 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8ri))
1191 .addReg(OpReg).addImm(1);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001192 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1193 .addMBB(TrueMBB);
Stuart Hastings0125b642010-06-17 22:43:56 +00001194 FastEmitBranch(FalseMBB, DL);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001195 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmana5753b32008-09-05 01:06:14 +00001196 return true;
1197}
1198
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001199bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001200 unsigned CReg = 0, OpReg = 0;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001201 const TargetRegisterClass *RC = NULL;
Duncan Sands9dff9be2010-02-15 16:12:20 +00001202 if (I->getType()->isIntegerTy(8)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001203 CReg = X86::CL;
1204 RC = &X86::GR8RegClass;
1205 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001206 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1207 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1208 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001209 default: return false;
1210 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001211 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001212 CReg = X86::CX;
1213 RC = &X86::GR16RegClass;
1214 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001215 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1216 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1217 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001218 default: return false;
1219 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001220 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001221 CReg = X86::ECX;
1222 RC = &X86::GR32RegClass;
1223 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001224 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1225 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1226 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001227 default: return false;
1228 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001229 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001230 CReg = X86::RCX;
1231 RC = &X86::GR64RegClass;
1232 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001233 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1234 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1235 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001236 default: return false;
1237 }
1238 } else {
1239 return false;
1240 }
1241
Duncan Sandsf5dda012010-11-03 11:35:31 +00001242 MVT VT;
1243 if (!isTypeLegal(I->getType(), VT))
Dan Gohmandb06a992008-09-05 21:27:34 +00001244 return false;
1245
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001246 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1247 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001248
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001249 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1250 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00001251 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1252 CReg).addReg(Op1Reg);
Dan Gohmand3917152008-10-07 21:50:36 +00001253
1254 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001255 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohmand3917152008-10-07 21:50:36 +00001256 if (CReg != X86::CL)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001257 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1258 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001259 .addReg(CReg, RegState::Kill);
Dan Gohmand3917152008-10-07 21:50:36 +00001260
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001261 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001262 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1263 .addReg(Op0Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001264 UpdateValueMap(I, ResultReg);
1265 return true;
1266}
1267
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001268bool X86FastISel::X86SelectDivRem(const Instruction *I) {
1269 const static unsigned NumTypes = 4; // i8, i16, i32, i64
1270 const static unsigned NumOps = 4; // SDiv, SRem, UDiv, URem
1271 const static bool S = true; // IsSigned
1272 const static bool U = false; // !IsSigned
1273 const static unsigned Copy = TargetOpcode::COPY;
1274 // For the X86 DIV/IDIV instruction, in most cases the dividend
1275 // (numerator) must be in a specific register pair highreg:lowreg,
1276 // producing the quotient in lowreg and the remainder in highreg.
1277 // For most data types, to set up the instruction, the dividend is
1278 // copied into lowreg, and lowreg is sign-extended or zero-extended
1279 // into highreg. The exception is i8, where the dividend is defined
1280 // as a single register rather than a register pair, and we
1281 // therefore directly sign-extend or zero-extend the dividend into
1282 // lowreg, instead of copying, and ignore the highreg.
1283 const static struct DivRemEntry {
1284 // The following portion depends only on the data type.
1285 const TargetRegisterClass *RC;
1286 unsigned LowInReg; // low part of the register pair
1287 unsigned HighInReg; // high part of the register pair
1288 // The following portion depends on both the data type and the operation.
1289 struct DivRemResult {
1290 unsigned OpDivRem; // The specific DIV/IDIV opcode to use.
1291 unsigned OpSignExtend; // Opcode for sign-extending lowreg into
1292 // highreg, or copying a zero into highreg.
1293 unsigned OpCopy; // Opcode for copying dividend into lowreg, or
1294 // zero/sign-extending into lowreg for i8.
1295 unsigned DivRemResultReg; // Register containing the desired result.
1296 bool IsOpSigned; // Whether to use signed or unsigned form.
1297 } ResultTable[NumOps];
1298 } OpTable[NumTypes] = {
1299 { &X86::GR8RegClass, X86::AX, 0, {
1300 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AL, S }, // SDiv
1301 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AH, S }, // SRem
1302 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AL, U }, // UDiv
1303 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AH, U }, // URem
1304 }
1305 }, // i8
1306 { &X86::GR16RegClass, X86::AX, X86::DX, {
1307 { X86::IDIV16r, X86::CWD, Copy, X86::AX, S }, // SDiv
1308 { X86::IDIV16r, X86::CWD, Copy, X86::DX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001309 { X86::DIV16r, X86::MOV32r0, Copy, X86::AX, U }, // UDiv
1310 { X86::DIV16r, X86::MOV32r0, Copy, X86::DX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001311 }
1312 }, // i16
1313 { &X86::GR32RegClass, X86::EAX, X86::EDX, {
1314 { X86::IDIV32r, X86::CDQ, Copy, X86::EAX, S }, // SDiv
1315 { X86::IDIV32r, X86::CDQ, Copy, X86::EDX, S }, // SRem
1316 { X86::DIV32r, X86::MOV32r0, Copy, X86::EAX, U }, // UDiv
1317 { X86::DIV32r, X86::MOV32r0, Copy, X86::EDX, U }, // URem
1318 }
1319 }, // i32
1320 { &X86::GR64RegClass, X86::RAX, X86::RDX, {
1321 { X86::IDIV64r, X86::CQO, Copy, X86::RAX, S }, // SDiv
1322 { X86::IDIV64r, X86::CQO, Copy, X86::RDX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001323 { X86::DIV64r, X86::MOV32r0, Copy, X86::RAX, U }, // UDiv
1324 { X86::DIV64r, X86::MOV32r0, Copy, X86::RDX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001325 }
1326 }, // i64
1327 };
1328
1329 MVT VT;
1330 if (!isTypeLegal(I->getType(), VT))
1331 return false;
1332
1333 unsigned TypeIndex, OpIndex;
1334 switch (VT.SimpleTy) {
1335 default: return false;
1336 case MVT::i8: TypeIndex = 0; break;
1337 case MVT::i16: TypeIndex = 1; break;
1338 case MVT::i32: TypeIndex = 2; break;
1339 case MVT::i64: TypeIndex = 3;
1340 if (!Subtarget->is64Bit())
1341 return false;
1342 break;
1343 }
1344
1345 switch (I->getOpcode()) {
1346 default: llvm_unreachable("Unexpected div/rem opcode");
1347 case Instruction::SDiv: OpIndex = 0; break;
1348 case Instruction::SRem: OpIndex = 1; break;
1349 case Instruction::UDiv: OpIndex = 2; break;
1350 case Instruction::URem: OpIndex = 3; break;
1351 }
1352
1353 const DivRemEntry &TypeEntry = OpTable[TypeIndex];
1354 const DivRemEntry::DivRemResult &OpEntry = TypeEntry.ResultTable[OpIndex];
1355 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1356 if (Op0Reg == 0)
1357 return false;
1358 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1359 if (Op1Reg == 0)
1360 return false;
1361
1362 // Move op0 into low-order input register.
1363 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1364 TII.get(OpEntry.OpCopy), TypeEntry.LowInReg).addReg(Op0Reg);
1365 // Zero-extend or sign-extend into high-order input register.
1366 if (OpEntry.OpSignExtend) {
1367 if (OpEntry.IsOpSigned)
1368 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1369 TII.get(OpEntry.OpSignExtend));
Tim Northover64ec0ff2013-05-30 13:19:42 +00001370 else {
1371 unsigned Zero32 = createResultReg(&X86::GR32RegClass);
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001372 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Tim Northover64ec0ff2013-05-30 13:19:42 +00001373 TII.get(X86::MOV32r0), Zero32);
1374
1375 // Copy the zero into the appropriate sub/super/identical physical
1376 // register. Unfortunately the operations needed are not uniform enough to
1377 // fit neatly into the table above.
1378 if (VT.SimpleTy == MVT::i16) {
1379 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001380 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001381 .addReg(Zero32, 0, X86::sub_16bit);
1382 } else if (VT.SimpleTy == MVT::i32) {
1383 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001384 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001385 .addReg(Zero32);
1386 } else if (VT.SimpleTy == MVT::i64) {
1387 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1388 TII.get(TargetOpcode::SUBREG_TO_REG), TypeEntry.HighInReg)
1389 .addImm(0).addReg(Zero32).addImm(X86::sub_32bit);
1390 }
1391 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001392 }
1393 // Generate the DIV/IDIV instruction.
1394 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1395 TII.get(OpEntry.OpDivRem)).addReg(Op1Reg);
Jim Grosbachc35388f2013-07-09 02:07:25 +00001396 // For i8 remainder, we can't reference AH directly, as we'll end
1397 // up with bogus copies like %R9B = COPY %AH. Reference AX
1398 // instead to prevent AH references in a REX instruction.
1399 //
1400 // The current assumption of the fast register allocator is that isel
1401 // won't generate explicit references to the GPR8_NOREX registers. If
1402 // the allocator and/or the backend get enhanced to be more robust in
1403 // that regard, this can be, and should be, removed.
1404 unsigned ResultReg = 0;
1405 if ((I->getOpcode() == Instruction::SRem ||
1406 I->getOpcode() == Instruction::URem) &&
1407 OpEntry.DivRemResultReg == X86::AH && Subtarget->is64Bit()) {
1408 unsigned SourceSuperReg = createResultReg(&X86::GR16RegClass);
1409 unsigned ResultSuperReg = createResultReg(&X86::GR16RegClass);
1410 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1411 TII.get(Copy), SourceSuperReg).addReg(X86::AX);
1412
1413 // Shift AX right by 8 bits instead of using AH.
1414 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SHR16ri),
1415 ResultSuperReg).addReg(SourceSuperReg).addImm(8);
1416
1417 // Now reference the 8-bit subreg of the result.
1418 ResultReg = FastEmitInst_extractsubreg(MVT::i8, ResultSuperReg,
1419 /*Kill=*/true, X86::sub_8bit);
1420 }
1421 // Copy the result out of the physreg if we haven't already.
1422 if (!ResultReg) {
1423 ResultReg = createResultReg(TypeEntry.RC);
1424 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Copy), ResultReg)
1425 .addReg(OpEntry.DivRemResultReg);
1426 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001427 UpdateValueMap(I, ResultReg);
1428
1429 return true;
1430}
1431
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001432bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001433 MVT VT;
1434 if (!isTypeLegal(I->getType(), VT))
Chris Lattnera0f9d492008-10-15 05:07:36 +00001435 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001436
Eric Christopher0574cc52010-09-29 23:00:29 +00001437 // We only use cmov here, if we don't have a cmov instruction bail.
1438 if (!Subtarget->hasCMov()) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001439
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001440 unsigned Opc = 0;
1441 const TargetRegisterClass *RC = NULL;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001442 if (VT == MVT::i16) {
Dan Gohmane5560182008-09-05 21:13:04 +00001443 Opc = X86::CMOVE16rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001444 RC = &X86::GR16RegClass;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001445 } else if (VT == MVT::i32) {
Dan Gohmane5560182008-09-05 21:13:04 +00001446 Opc = X86::CMOVE32rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001447 RC = &X86::GR32RegClass;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001448 } else if (VT == MVT::i64) {
Dan Gohmane5560182008-09-05 21:13:04 +00001449 Opc = X86::CMOVE64rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001450 RC = &X86::GR64RegClass;
1451 } else {
Wesley Peck527da1b2010-11-23 03:31:01 +00001452 return false;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001453 }
1454
1455 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1456 if (Op0Reg == 0) return false;
1457 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1458 if (Op1Reg == 0) return false;
1459 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1460 if (Op2Reg == 0) return false;
1461
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001462 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1463 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001464 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001465 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1466 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001467 UpdateValueMap(I, ResultReg);
1468 return true;
1469}
1470
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001471bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00001472 // fpext from float to double.
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001473 if (X86ScalarSSEf64 &&
Chris Lattnerfdd87902009-10-05 05:54:46 +00001474 I->getType()->isDoubleTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001475 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001476 if (V->getType()->isFloatTy()) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00001477 unsigned OpReg = getRegForValue(V);
1478 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00001479 unsigned ResultReg = createResultReg(&X86::FR64RegClass);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001480 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1481 TII.get(X86::CVTSS2SDrr), ResultReg)
1482 .addReg(OpReg);
Chris Lattnera0f9d492008-10-15 05:07:36 +00001483 UpdateValueMap(I, ResultReg);
1484 return true;
Dan Gohmanbf646f22008-09-10 21:02:08 +00001485 }
1486 }
1487
1488 return false;
1489}
1490
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001491bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001492 if (X86ScalarSSEf64) {
Chris Lattnerfdd87902009-10-05 05:54:46 +00001493 if (I->getType()->isFloatTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001494 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001495 if (V->getType()->isDoubleTy()) {
Dan Gohmanbf646f22008-09-10 21:02:08 +00001496 unsigned OpReg = getRegForValue(V);
1497 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00001498 unsigned ResultReg = createResultReg(&X86::FR32RegClass);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001499 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1500 TII.get(X86::CVTSD2SSrr), ResultReg)
1501 .addReg(OpReg);
Dan Gohmanbf646f22008-09-10 21:02:08 +00001502 UpdateValueMap(I, ResultReg);
1503 return true;
1504 }
1505 }
1506 }
1507
1508 return false;
1509}
1510
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001511bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001512 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1513 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peck527da1b2010-11-23 03:31:01 +00001514
Eli Friedmanc7035512011-05-25 23:49:02 +00001515 // This code only handles truncation to byte.
Owen Anderson9f944592009-08-11 20:47:22 +00001516 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Chengb9286692008-09-07 08:47:42 +00001517 return false;
Eli Friedmanc7035512011-05-25 23:49:02 +00001518 if (!TLI.isTypeLegal(SrcVT))
Evan Chengb9286692008-09-07 08:47:42 +00001519 return false;
1520
1521 unsigned InputReg = getRegForValue(I->getOperand(0));
1522 if (!InputReg)
1523 // Unhandled operand. Halt "fast" selection and bail.
1524 return false;
1525
Eli Friedmanc7035512011-05-25 23:49:02 +00001526 if (SrcVT == MVT::i8) {
1527 // Truncate from i8 to i1; no code needed.
1528 UpdateValueMap(I, InputReg);
1529 return true;
1530 }
Evan Chengb9286692008-09-07 08:47:42 +00001531
Eli Friedmanc7035512011-05-25 23:49:02 +00001532 if (!Subtarget->is64Bit()) {
1533 // If we're on x86-32; we can't extract an i8 from a general register.
1534 // First issue a copy to GR16_ABCD or GR32_ABCD.
Craig Topperabadc662012-04-20 06:31:50 +00001535 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) ?
1536 (const TargetRegisterClass*)&X86::GR16_ABCDRegClass :
1537 (const TargetRegisterClass*)&X86::GR32_ABCDRegClass;
Eli Friedmanc7035512011-05-25 23:49:02 +00001538 unsigned CopyReg = createResultReg(CopyRC);
1539 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1540 CopyReg).addReg(InputReg);
1541 InputReg = CopyReg;
1542 }
1543
1544 // Issue an extract_subreg.
Owen Anderson9f944592009-08-11 20:47:22 +00001545 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedmanc7035512011-05-25 23:49:02 +00001546 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen9340ea52010-05-24 14:48:17 +00001547 X86::sub_8bit);
Evan Chengb9286692008-09-07 08:47:42 +00001548 if (!ResultReg)
1549 return false;
1550
1551 UpdateValueMap(I, ResultReg);
1552 return true;
1553}
1554
Eli Friedman60afcc22011-05-20 22:21:04 +00001555bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1556 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1557}
1558
Eli Friedmanbcc69142011-04-27 01:45:07 +00001559bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1560 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedman60afcc22011-05-20 22:21:04 +00001561
Eli Friedmanbcc69142011-04-27 01:45:07 +00001562 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedman60afcc22011-05-20 22:21:04 +00001563 if (!IsMemcpySmall(Len))
1564 return false;
1565
1566 bool i64Legal = Subtarget->is64Bit();
Eli Friedmanbcc69142011-04-27 01:45:07 +00001567
1568 // We don't care about alignment here since we just emit integer accesses.
1569 while (Len) {
1570 MVT VT;
1571 if (Len >= 8 && i64Legal)
1572 VT = MVT::i64;
1573 else if (Len >= 4)
1574 VT = MVT::i32;
1575 else if (Len >= 2)
1576 VT = MVT::i16;
1577 else {
Eli Friedmanbcc69142011-04-27 01:45:07 +00001578 VT = MVT::i8;
1579 }
1580
1581 unsigned Reg;
1582 bool RV = X86FastEmitLoad(VT, SrcAM, Reg);
1583 RV &= X86FastEmitStore(VT, Reg, DestAM);
1584 assert(RV && "Failed to emit load or store??");
1585
1586 unsigned Size = VT.getSizeInBits()/8;
1587 Len -= Size;
1588 DestAM.Disp += Size;
1589 SrcAM.Disp += Size;
1590 }
1591
1592 return true;
1593}
1594
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001595bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling80b34b32008-12-09 02:42:50 +00001596 // FIXME: Handle more intrinsics.
Chris Lattner99a8cb62009-04-12 07:36:01 +00001597 switch (I.getIntrinsicID()) {
Bill Wendling80b34b32008-12-09 02:42:50 +00001598 default: return false;
Chris Lattner91328b32011-04-19 05:52:03 +00001599 case Intrinsic::memcpy: {
1600 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1601 // Don't handle volatile or variable length memcpys.
Eli Friedmancd2124a2011-06-10 23:39:36 +00001602 if (MCI.isVolatile())
Chris Lattner91328b32011-04-19 05:52:03 +00001603 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00001604
Eli Friedmancd2124a2011-06-10 23:39:36 +00001605 if (isa<ConstantInt>(MCI.getLength())) {
1606 // Small memcpy's are common enough that we want to do them
1607 // without a call if possible.
1608 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1609 if (IsMemcpySmall(Len)) {
1610 X86AddressMode DestAM, SrcAM;
1611 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1612 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1613 return false;
1614 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1615 return true;
1616 }
1617 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001618
Eli Friedmancd2124a2011-06-10 23:39:36 +00001619 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1620 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner91328b32011-04-19 05:52:03 +00001621 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00001622
Eli Friedmancd2124a2011-06-10 23:39:36 +00001623 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1624 return false;
1625
1626 return DoSelectCall(&I, "memcpy");
Chris Lattner91328b32011-04-19 05:52:03 +00001627 }
Eli Friedmancd2124a2011-06-10 23:39:36 +00001628 case Intrinsic::memset: {
1629 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001630
Nick Lewyckya530a4d2011-08-02 00:40:16 +00001631 if (MSI.isVolatile())
1632 return false;
1633
Eli Friedmancd2124a2011-06-10 23:39:36 +00001634 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1635 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1636 return false;
1637
1638 if (MSI.getDestAddressSpace() > 255)
1639 return false;
1640
1641 return DoSelectCall(&I, "memset");
1642 }
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001643 case Intrinsic::stackprotector: {
Chad Rosier06e34d92012-05-11 19:43:29 +00001644 // Emit code to store the stack guard onto the stack.
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001645 EVT PtrTy = TLI.getPointerTy();
1646
Gabor Greif83205af2010-06-26 11:51:52 +00001647 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1648 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001649
1650 // Grab the frame index.
1651 X86AddressMode AM;
1652 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher5e95aee2010-03-18 21:58:33 +00001653 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001654 return true;
1655 }
Dale Johannesend5575f22010-01-26 00:09:58 +00001656 case Intrinsic::dbg_declare: {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001657 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesend5575f22010-01-26 00:09:58 +00001658 X86AddressMode AM;
Dale Johannesenad00f032010-01-29 21:21:28 +00001659 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesend5575f22010-01-26 00:09:58 +00001660 if (!X86SelectAddress(DI->getAddress(), AM))
1661 return false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00001662 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen654528e2010-02-18 18:51:15 +00001663 // FIXME may need to add RegState::Debug to any registers produced,
1664 // although ESP/EBP should be the only ones at the moment.
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001665 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1666 addImm(0).addMetadata(DI->getVariable());
Dale Johannesend5575f22010-01-26 00:09:58 +00001667 return true;
1668 }
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00001669 case Intrinsic::trap: {
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001670 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00001671 return true;
1672 }
Bill Wendling80b34b32008-12-09 02:42:50 +00001673 case Intrinsic::sadd_with_overflow:
1674 case Intrinsic::uadd_with_overflow: {
Chris Lattner91328b32011-04-19 05:52:03 +00001675 // FIXME: Should fold immediates.
Eric Christopher0713a9d2011-06-08 23:55:35 +00001676
Bill Wendlinge25d3412008-12-09 07:55:31 +00001677 // Replace "add with overflow" intrinsics with an "add" instruction followed
Eli Friedmana4d4a012011-05-16 21:06:17 +00001678 // by a seto/setc instruction.
Bill Wendling80b34b32008-12-09 02:42:50 +00001679 const Function *Callee = I.getCalledFunction();
Chris Lattner229907c2011-07-18 04:54:35 +00001680 Type *RetTy =
Bill Wendling80b34b32008-12-09 02:42:50 +00001681 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1682
Duncan Sandsf5dda012010-11-03 11:35:31 +00001683 MVT VT;
Bill Wendling80b34b32008-12-09 02:42:50 +00001684 if (!isTypeLegal(RetTy, VT))
1685 return false;
1686
Gabor Greif83205af2010-06-26 11:51:52 +00001687 const Value *Op1 = I.getArgOperand(0);
1688 const Value *Op2 = I.getArgOperand(1);
Bill Wendling80b34b32008-12-09 02:42:50 +00001689 unsigned Reg1 = getRegForValue(Op1);
1690 unsigned Reg2 = getRegForValue(Op2);
1691
1692 if (Reg1 == 0 || Reg2 == 0)
1693 // FIXME: Handle values *not* in registers.
1694 return false;
1695
1696 unsigned OpC = 0;
Owen Anderson9f944592009-08-11 20:47:22 +00001697 if (VT == MVT::i32)
Bill Wendling80b34b32008-12-09 02:42:50 +00001698 OpC = X86::ADD32rr;
Owen Anderson9f944592009-08-11 20:47:22 +00001699 else if (VT == MVT::i64)
Bill Wendling80b34b32008-12-09 02:42:50 +00001700 OpC = X86::ADD64rr;
1701 else
1702 return false;
1703
Eli Friedmana4d4a012011-05-16 21:06:17 +00001704 // The call to CreateRegs builds two sequential registers, to store the
Sylvestre Ledru35521e22012-07-23 08:51:15 +00001705 // both the returned values.
Eli Friedmana4d4a012011-05-16 21:06:17 +00001706 unsigned ResultReg = FuncInfo.CreateRegs(I.getType());
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001707 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1708 .addReg(Reg1).addReg(Reg2);
Wesley Peck527da1b2010-11-23 03:31:01 +00001709
Chris Lattner99a8cb62009-04-12 07:36:01 +00001710 unsigned Opc = X86::SETBr;
1711 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1712 Opc = X86::SETOr;
Eli Friedmana4d4a012011-05-16 21:06:17 +00001713 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg+1);
1714
1715 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling80b34b32008-12-09 02:42:50 +00001716 return true;
1717 }
1718 }
1719}
1720
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001721bool X86FastISel::FastLowerArguments() {
1722 if (!FuncInfo.CanLowerReturn)
1723 return false;
1724
1725 const Function *F = FuncInfo.Fn;
1726 if (F->isVarArg())
1727 return false;
1728
1729 CallingConv::ID CC = F->getCallingConv();
1730 if (CC != CallingConv::C)
1731 return false;
Charles Davise8f297c2013-07-12 06:02:35 +00001732
1733 if (Subtarget->isCallingConvWin64(CC))
1734 return false;
1735
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001736 if (!Subtarget->is64Bit())
1737 return false;
1738
1739 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
1740 unsigned Idx = 1;
1741 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1742 I != E; ++I, ++Idx) {
1743 if (Idx > 6)
1744 return false;
1745
1746 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
1747 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
1748 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
1749 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
1750 return false;
1751
1752 Type *ArgTy = I->getType();
1753 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
1754 return false;
1755
1756 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosier1b33e8d2013-02-26 01:05:31 +00001757 if (!ArgVT.isSimple()) return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001758 switch (ArgVT.getSimpleVT().SimpleTy) {
1759 case MVT::i32:
1760 case MVT::i64:
1761 break;
1762 default:
1763 return false;
1764 }
1765 }
1766
1767 static const uint16_t GPR32ArgRegs[] = {
1768 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
1769 };
1770 static const uint16_t GPR64ArgRegs[] = {
1771 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
1772 };
1773
1774 Idx = 0;
1775 const TargetRegisterClass *RC32 = TLI.getRegClassFor(MVT::i32);
1776 const TargetRegisterClass *RC64 = TLI.getRegClassFor(MVT::i64);
1777 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1778 I != E; ++I, ++Idx) {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001779 bool is32Bit = TLI.getValueType(I->getType()) == MVT::i32;
1780 const TargetRegisterClass *RC = is32Bit ? RC32 : RC64;
1781 unsigned SrcReg = is32Bit ? GPR32ArgRegs[Idx] : GPR64ArgRegs[Idx];
1782 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
1783 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1784 // Without this, EmitLiveInCopies may eliminate the livein if its only
1785 // use is a bitcast (which isn't turned into an instruction).
1786 unsigned ResultReg = createResultReg(RC);
1787 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1788 ResultReg).addReg(DstReg, getKillRegState(true));
1789 UpdateValueMap(I, ResultReg);
1790 }
1791 return true;
1792}
1793
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001794bool X86FastISel::X86SelectCall(const Instruction *I) {
1795 const CallInst *CI = cast<CallInst>(I);
Gabor Greif83205af2010-06-26 11:51:52 +00001796 const Value *Callee = CI->getCalledValue();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001797
1798 // Can't handle inline asm yet.
1799 if (isa<InlineAsm>(Callee))
1800 return false;
1801
Bill Wendling80b34b32008-12-09 02:42:50 +00001802 // Handle intrinsic calls.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001803 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattner99a8cb62009-04-12 07:36:01 +00001804 return X86VisitIntrinsicCall(*II);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001805
Chad Rosierdf42cf32012-12-11 00:18:02 +00001806 // Allow SelectionDAG isel to handle tail calls.
1807 if (cast<CallInst>(I)->isTailCall())
1808 return false;
1809
Eli Friedmancd2124a2011-06-10 23:39:36 +00001810 return DoSelectCall(I, 0);
1811}
1812
Rafael Espindola73173c52012-07-25 15:42:45 +00001813static unsigned computeBytesPoppedByCallee(const X86Subtarget &Subtarget,
1814 const ImmutableCallSite &CS) {
Rafael Espindola2caee7f2012-07-25 13:35:45 +00001815 if (Subtarget.is64Bit())
1816 return 0;
1817 if (Subtarget.isTargetWindows())
1818 return 0;
1819 CallingConv::ID CC = CS.getCallingConv();
1820 if (CC == CallingConv::Fast || CC == CallingConv::GHC)
1821 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001822 if (!CS.paramHasAttr(1, Attribute::StructRet))
Rafael Espindola2caee7f2012-07-25 13:35:45 +00001823 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001824 if (CS.paramHasAttr(1, Attribute::InReg))
Rafael Espindola11c38b92012-07-25 13:41:10 +00001825 return 0;
Rafael Espindola2caee7f2012-07-25 13:35:45 +00001826 return 4;
1827}
1828
Eli Friedmancd2124a2011-06-10 23:39:36 +00001829// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
1830bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
1831 const CallInst *CI = cast<CallInst>(I);
1832 const Value *Callee = CI->getCalledValue();
1833
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001834 // Handle only C and fastcc calling conventions for now.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001835 ImmutableCallSite CS(CI);
Sandeep Patel68c5f472009-09-02 08:44:58 +00001836 CallingConv::ID CC = CS.getCallingConv();
Charles Davise8f297c2013-07-12 06:02:35 +00001837 bool isWin64 = Subtarget->isCallingConvWin64(CC);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00001838 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +00001839 CC != CallingConv::X86_FastCall && CC != CallingConv::X86_64_Win64 &&
1840 CC != CallingConv::X86_64_SysV)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001841 return false;
1842
Evan Chengd10089a2010-01-27 00:00:57 +00001843 // fastcc with -tailcallopt is intended to provide a guaranteed
1844 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001845 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Evan Chengd10089a2010-01-27 00:00:57 +00001846 return false;
1847
Chris Lattner229907c2011-07-18 04:54:35 +00001848 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1849 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedmanee92a6b2011-04-19 17:22:22 +00001850 bool isVarArg = FTy->isVarArg();
1851
1852 // Don't know how to handle Win64 varargs yet. Nothing special needed for
1853 // x86-32. Special handling for x86-64 is implemented.
Charles Davise8f297c2013-07-12 06:02:35 +00001854 if (isVarArg && isWin64)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001855 return false;
1856
Dan Gohmandc53f1c2010-05-27 18:43:40 +00001857 // Fast-isel doesn't know about callee-pop yet.
Evan Cheng3a0c5e52011-06-23 17:54:54 +00001858 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001859 TM.Options.GuaranteedTailCallOpt))
Dan Gohmandc53f1c2010-05-27 18:43:40 +00001860 return false;
1861
Eli Friedman7b279422011-05-17 18:29:03 +00001862 // Check whether the function can return without sret-demotion.
1863 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +00001864 GetReturnInfo(I->getType(), CS.getAttributes(), Outs, TLI);
Eli Friedman7b279422011-05-17 18:29:03 +00001865 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Bill Wendlingea6397f2012-07-19 00:11:40 +00001866 *FuncInfo.MF, FTy->isVarArg(),
1867 Outs, FTy->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00001868 if (!CanLowerReturn)
Eli Friedman7335e8a2011-05-17 02:36:59 +00001869 return false;
1870
Dan Gohmanaf13bf12008-09-17 21:18:49 +00001871 // Materialize callee address in a register. FIXME: GV address can be
1872 // handled with a CALLpcrel32 instead.
Dan Gohman9801ba42008-09-19 22:16:54 +00001873 X86AddressMode CalleeAM;
Chris Lattner8212d372009-07-10 05:33:42 +00001874 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman9801ba42008-09-19 22:16:54 +00001875 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00001876 unsigned CalleeOp = 0;
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001877 const GlobalValue *GV = 0;
Chris Lattnerd17366a2009-06-27 04:50:14 +00001878 if (CalleeAM.GV != 0) {
Dan Gohman9801ba42008-09-19 22:16:54 +00001879 GV = CalleeAM.GV;
Chris Lattnerd17366a2009-06-27 04:50:14 +00001880 } else if (CalleeAM.Base.Reg != 0) {
1881 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman9801ba42008-09-19 22:16:54 +00001882 } else
1883 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00001884
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001885 // Deal with call operands first.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001886 SmallVector<const Value *, 8> ArgVals;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00001887 SmallVector<unsigned, 8> Args;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001888 SmallVector<MVT, 8> ArgVTs;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00001889 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosierf0687632012-02-15 00:36:26 +00001890 unsigned arg_size = CS.arg_size();
1891 Args.reserve(arg_size);
1892 ArgVals.reserve(arg_size);
1893 ArgVTs.reserve(arg_size);
1894 ArgFlags.reserve(arg_size);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001895 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001896 i != e; ++i) {
Eli Friedmancd2124a2011-06-10 23:39:36 +00001897 // If we're lowering a mem intrinsic instead of a regular call, skip the
1898 // last two arguments, which should not passed to the underlying functions.
1899 if (MemIntName && e-i <= 2)
1900 break;
Chris Lattnerd7f7c932011-04-19 04:42:38 +00001901 Value *ArgVal = *i;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001902 ISD::ArgFlagsTy Flags;
1903 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001904 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001905 Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001906 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001907 Flags.setZExt();
1908
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001909 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattner229907c2011-07-18 04:54:35 +00001910 PointerType *Ty = cast<PointerType>(ArgVal->getType());
1911 Type *ElementTy = Ty->getElementType();
Eli Friedman60afcc22011-05-20 22:21:04 +00001912 unsigned FrameSize = TD.getTypeAllocSize(ElementTy);
1913 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
1914 if (!FrameAlign)
1915 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
1916 Flags.setByVal();
1917 Flags.setByValSize(FrameSize);
1918 Flags.setByValAlign(FrameAlign);
1919 if (!IsMemcpySmall(FrameSize))
1920 return false;
1921 }
1922
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001923 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
Eli Friedman60afcc22011-05-20 22:21:04 +00001924 Flags.setInReg();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001925 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
Eli Friedman60afcc22011-05-20 22:21:04 +00001926 Flags.setNest();
1927
Chris Lattnerd7f7c932011-04-19 04:42:38 +00001928 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1929 // instruction. This is safe because it is common to all fastisel supported
1930 // calling conventions on x86.
1931 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1932 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1933 CI->getBitWidth() == 16) {
1934 if (Flags.isSExt())
1935 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1936 else
1937 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1938 }
1939 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001940
Chris Lattner5f4b7832011-04-19 05:09:50 +00001941 unsigned ArgReg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00001942
Chris Lattner34a08c22011-04-19 05:15:59 +00001943 // Passing bools around ends up doing a trunc to i1 and passing it.
1944 // Codegen this as an argument + "and 1".
Chris Lattner5f4b7832011-04-19 05:09:50 +00001945 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
1946 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
1947 ArgVal->hasOneUse()) {
Chris Lattner5f4b7832011-04-19 05:09:50 +00001948 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
1949 ArgReg = getRegForValue(ArgVal);
1950 if (ArgReg == 0) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00001951
Chris Lattner5f4b7832011-04-19 05:09:50 +00001952 MVT ArgVT;
1953 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00001954
Chris Lattner5f4b7832011-04-19 05:09:50 +00001955 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
1956 ArgVal->hasOneUse(), 1);
1957 } else {
1958 ArgReg = getRegForValue(ArgVal);
Chris Lattner5f4b7832011-04-19 05:09:50 +00001959 }
Chris Lattnerd7f7c932011-04-19 04:42:38 +00001960
Chris Lattner34a08c22011-04-19 05:15:59 +00001961 if (ArgReg == 0) return false;
1962
Chris Lattner229907c2011-07-18 04:54:35 +00001963 Type *ArgTy = ArgVal->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00001964 MVT ArgVT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00001965 if (!isTypeLegal(ArgTy, ArgVT))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001966 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00001967 if (ArgVT == MVT::x86mmx)
1968 return false;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001969 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1970 Flags.setOrigAlign(OriginalAlignment);
1971
Chris Lattner5f4b7832011-04-19 05:09:50 +00001972 Args.push_back(ArgReg);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00001973 ArgVals.push_back(ArgVal);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001974 ArgVTs.push_back(ArgVT);
1975 ArgFlags.push_back(Flags);
1976 }
1977
1978 // Analyze operands of the call, assigning locations to each operand.
1979 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00001980 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00001981 I->getParent()->getContext());
Wesley Peck527da1b2010-11-23 03:31:01 +00001982
Dan Gohman47a07242010-06-01 21:09:47 +00001983 // Allocate shadow area for Win64
Charles Davise8f297c2013-07-12 06:02:35 +00001984 if (isWin64)
Wesley Peck527da1b2010-11-23 03:31:01 +00001985 CCInfo.AllocateStack(32, 8);
Dan Gohman47a07242010-06-01 21:09:47 +00001986
Duncan Sandsfb0a48e2010-10-31 13:21:44 +00001987 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001988
1989 // Get a count of how many bytes are to be pushed on the stack.
1990 unsigned NumBytes = CCInfo.getNextStackOffset();
1991
1992 // Issue CALLSEQ_START
Evan Cheng194c3dc2011-06-28 21:14:33 +00001993 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001994 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1995 .addImm(NumBytes);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001996
Chris Lattner3ba29352008-10-15 05:30:52 +00001997 // Process argument: walk the register/memloc assignments, inserting
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001998 // copies / loads.
1999 SmallVector<unsigned, 4> RegArgs;
2000 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2001 CCValAssign &VA = ArgLocs[i];
2002 unsigned Arg = Args[VA.getValNo()];
Owen Anderson53aa7a92009-08-10 22:56:29 +00002003 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002004
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002005 // Promote the value if needed.
2006 switch (VA.getLocInfo()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002007 case CCValAssign::Full: break;
Evan Cheng6500d172008-09-08 06:35:17 +00002008 case CCValAssign::SExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002009 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2010 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002011 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2012 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002013 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002014 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002015 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002016 }
2017 case CCValAssign::ZExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002018 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2019 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002020 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
2021 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002022 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002023 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002024 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002025 }
2026 case CCValAssign::AExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002027 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2028 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002029 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
2030 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002031 if (!Emitted)
2032 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattnera0f9d492008-10-15 05:07:36 +00002033 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002034 if (!Emitted)
2035 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2036 Arg, ArgVT, Arg);
Wesley Peck527da1b2010-11-23 03:31:01 +00002037
Chris Lattner2d7df022011-01-05 22:26:52 +00002038 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002039 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002040 break;
2041 }
Dan Gohman8c795692009-08-05 05:33:42 +00002042 case CCValAssign::BCvt: {
Duncan Sandsf5dda012010-11-03 11:35:31 +00002043 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peck527da1b2010-11-23 03:31:01 +00002044 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohman8c795692009-08-05 05:33:42 +00002045 assert(BC != 0 && "Failed to emit a bitcast!");
2046 Arg = BC;
2047 ArgVT = VA.getLocVT();
2048 break;
2049 }
Chad Rosier8446ede2012-07-11 19:58:38 +00002050 case CCValAssign::VExt:
2051 // VExt has not been implemented, so this should be impossible to reach
2052 // for now. However, fallback to Selection DAG isel once implemented.
2053 return false;
2054 case CCValAssign::Indirect:
2055 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
2056 // support this.
2057 return false;
Evan Cheng6500d172008-09-08 06:35:17 +00002058 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002059
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002060 if (VA.isRegLoc()) {
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00002061 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2062 VA.getLocReg()).addReg(Arg);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002063 RegArgs.push_back(VA.getLocReg());
2064 } else {
2065 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman39d82f92008-09-10 20:11:02 +00002066 X86AddressMode AM;
Bill Wendling8f268402013-06-07 21:00:34 +00002067 const X86RegisterInfo *RegInfo = static_cast<const X86RegisterInfo*>(
2068 getTargetMachine()->getRegisterInfo());
Michael Liao70a99c82012-11-01 03:47:50 +00002069 AM.Base.Reg = RegInfo->getStackRegister();
Dan Gohman39d82f92008-09-10 20:11:02 +00002070 AM.Disp = LocMemOffset;
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002071 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedman60afcc22011-05-20 22:21:04 +00002072 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002073
Eli Friedman60afcc22011-05-20 22:21:04 +00002074 if (Flags.isByVal()) {
2075 X86AddressMode SrcAM;
2076 SrcAM.Base.Reg = Arg;
2077 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
2078 assert(Res && "memcpy length already checked!"); (void)Res;
2079 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
2080 // If this is a really simple value, emit this with the Value* version
Nick Lewycky064c1c02011-10-12 00:14:12 +00002081 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
Eli Friedman60afcc22011-05-20 22:21:04 +00002082 // as it can cause us to reevaluate the argument.
Lang Hames7d2f7b52011-10-18 22:11:33 +00002083 if (!X86FastEmitStore(ArgVT, ArgVal, AM))
2084 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002085 } else {
Lang Hames7d2f7b52011-10-18 22:11:33 +00002086 if (!X86FastEmitStore(ArgVT, Arg, AM))
2087 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002088 }
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002089 }
2090 }
2091
Dan Gohman3691d502008-09-25 15:24:26 +00002092 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peck527da1b2010-11-23 03:31:01 +00002093 // GOT pointer.
Chris Lattnerfef11d62009-07-09 04:39:06 +00002094 if (Subtarget->isPICStyleGOT()) {
Dan Gohman87fb4e82010-07-07 16:29:44 +00002095 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00002096 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2097 X86::EBX).addReg(Base);
Dan Gohman3691d502008-09-25 15:24:26 +00002098 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002099
Charles Davise8f297c2013-07-12 06:02:35 +00002100 if (Subtarget->is64Bit() && isVarArg && !isWin64) {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002101 // Count the number of XMM registers allocated.
Craig Topperbef78fc2012-03-11 07:57:25 +00002102 static const uint16_t XMMArgRegs[] = {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002103 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2104 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2105 };
2106 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
2107 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::MOV8ri),
2108 X86::AL).addImm(NumXMMRegs);
2109 }
2110
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002111 // Issue the call.
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002112 MachineInstrBuilder MIB;
2113 if (CalleeOp) {
2114 // Register-indirect call.
Nate Begeman68a069a2010-07-22 00:09:39 +00002115 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002116 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002117 CallOpc = X86::CALL64r;
2118 else
2119 CallOpc = X86::CALL32r;
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002120 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
2121 .addReg(CalleeOp);
Wesley Peck527da1b2010-11-23 03:31:01 +00002122
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002123 } else {
2124 // Direct call.
2125 assert(GV && "Not a direct call");
Nate Begeman68a069a2010-07-22 00:09:39 +00002126 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002127 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002128 CallOpc = X86::CALL64pcrel32;
2129 else
2130 CallOpc = X86::CALLpcrel32;
Wesley Peck527da1b2010-11-23 03:31:01 +00002131
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002132 // See if we need any target-specific flags on the GV operand.
2133 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00002134
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002135 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2136 // external symbols most go through the PLT in PIC mode. If the symbol
2137 // has hidden or protected visibility, or if it is static or local, then
2138 // we don't need to use the PLT - we can directly call it.
2139 if (Subtarget->isTargetELF() &&
2140 TM.getRelocationModel() == Reloc::PIC_ &&
2141 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
2142 OpFlags = X86II::MO_PLT;
Chris Lattnere2f524f2009-07-10 20:47:30 +00002143 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002144 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00002145 (!Subtarget->getTargetTriple().isMacOSX() ||
2146 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002147 // PC-relative references to external symbols should go through $stub,
2148 // unless we're building with the leopard linker or later, which
2149 // automatically synthesizes these stubs.
2150 OpFlags = X86II::MO_DARWIN_STUB;
2151 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002152
2153
Eli Friedmancd2124a2011-06-10 23:39:36 +00002154 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc));
2155 if (MemIntName)
Eli Friedman1735b292011-06-11 01:55:07 +00002156 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedmancd2124a2011-06-10 23:39:36 +00002157 else
2158 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002159 }
Dan Gohman3691d502008-09-25 15:24:26 +00002160
Jakob Stoklund Olesen8a450cb2012-02-16 00:02:50 +00002161 // Add a register mask with the call-preserved registers.
2162 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2163 MIB.addRegMask(TRI.getCallPreservedMask(CS.getCallingConv()));
2164
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00002165 // Add an implicit use GOT pointer in EBX.
2166 if (Subtarget->isPICStyleGOT())
2167 MIB.addReg(X86::EBX, RegState::Implicit);
2168
Charles Davise8f297c2013-07-12 06:02:35 +00002169 if (Subtarget->is64Bit() && isVarArg && !isWin64)
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00002170 MIB.addReg(X86::AL, RegState::Implicit);
2171
2172 // Add implicit physical register uses to the call.
2173 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2174 MIB.addReg(RegArgs[i], RegState::Implicit);
2175
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002176 // Issue CALLSEQ_END
Evan Cheng194c3dc2011-06-28 21:14:33 +00002177 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindola73173c52012-07-25 15:42:45 +00002178 const unsigned NumBytesCallee = computeBytesPoppedByCallee(*Subtarget, CS);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002179 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
Eli Friedman7cd51012011-04-28 20:19:12 +00002180 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002181
Eli Friedman7b279422011-05-17 18:29:03 +00002182 // Build info for return calling conv lowering code.
2183 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
2184 SmallVector<ISD::InputArg, 32> Ins;
2185 SmallVector<EVT, 4> RetTys;
2186 ComputeValueVTs(TLI, I->getType(), RetTys);
2187 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
2188 EVT VT = RetTys[i];
Patrik Hagglundbad545c2012-12-19 11:48:16 +00002189 MVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
Eli Friedman7b279422011-05-17 18:29:03 +00002190 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
2191 for (unsigned j = 0; j != NumRegs; ++j) {
2192 ISD::InputArg MyFlags;
Patrik Hagglundbad545c2012-12-19 11:48:16 +00002193 MyFlags.VT = RegisterVT;
Eli Friedman7b279422011-05-17 18:29:03 +00002194 MyFlags.Used = !CS.getInstruction()->use_empty();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002195 if (CS.paramHasAttr(0, Attribute::SExt))
Eli Friedman7b279422011-05-17 18:29:03 +00002196 MyFlags.Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002197 if (CS.paramHasAttr(0, Attribute::ZExt))
Eli Friedman7b279422011-05-17 18:29:03 +00002198 MyFlags.Flags.setZExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002199 if (CS.paramHasAttr(0, Attribute::InReg))
Eli Friedman7b279422011-05-17 18:29:03 +00002200 MyFlags.Flags.setInReg();
2201 Ins.push_back(MyFlags);
2202 }
2203 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00002204
Eli Friedman7b279422011-05-17 18:29:03 +00002205 // Now handle call return values.
2206 SmallVector<unsigned, 4> UsedRegs;
2207 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002208 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00002209 I->getParent()->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00002210 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
2211 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
2212 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2213 EVT CopyVT = RVLocs[i].getValVT();
2214 unsigned CopyReg = ResultReg + i;
Wesley Peck527da1b2010-11-23 03:31:01 +00002215
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002216 // If this is a call to a function that returns an fp value on the x87 fp
2217 // stack, but where we prefer to use the value in xmm registers, copy it
2218 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman7b279422011-05-17 18:29:03 +00002219 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002220 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00002221 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002222 CopyVT = MVT::f80;
Craig Topperabadc662012-04-20 06:31:50 +00002223 CopyReg = createResultReg(&X86::RFP80RegClass);
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00002224 }
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002225 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::FpPOP_RETVAL),
2226 CopyReg);
2227 } else {
2228 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2229 CopyReg).addReg(RVLocs[i].getLocReg());
2230 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002231 }
2232
Eli Friedman7b279422011-05-17 18:29:03 +00002233 if (CopyVT != RVLocs[i].getValVT()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002234 // Round the F80 the right size, which also moves to the appropriate xmm
2235 // register. This is accomplished by storing the F80 value in memory and
2236 // then loading it back. Ewww...
Eli Friedman7b279422011-05-17 18:29:03 +00002237 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson9f944592009-08-11 20:47:22 +00002238 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002239 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene1fbe0542009-11-12 20:49:22 +00002240 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002241 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2242 TII.get(Opc)), FI)
Eli Friedman7b279422011-05-17 18:29:03 +00002243 .addReg(CopyReg);
Owen Anderson9f944592009-08-11 20:47:22 +00002244 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002245 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eli Friedman7b279422011-05-17 18:29:03 +00002246 TII.get(Opc), ResultReg + i), FI);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002247 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00002248 }
Eli Friedman83ba1502011-05-17 00:13:47 +00002249
Eli Friedman7b279422011-05-17 18:29:03 +00002250 if (RVLocs.size())
2251 UpdateValueMap(I, ResultReg, RVLocs.size());
2252
Dan Gohman86936502010-06-18 23:28:01 +00002253 // Set all unused physreg defs as dead.
2254 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2255
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002256 return true;
2257}
2258
2259
Dan Gohmand58f3e32008-08-28 23:21:34 +00002260bool
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002261X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohmand58f3e32008-08-28 23:21:34 +00002262 switch (I->getOpcode()) {
2263 default: break;
Evan Chenga41ee292008-09-03 06:44:39 +00002264 case Instruction::Load:
Dan Gohman7bda51f2008-09-03 23:12:08 +00002265 return X86SelectLoad(I);
Owen Andersonb8c7ba22008-09-04 16:48:33 +00002266 case Instruction::Store:
2267 return X86SelectStore(I);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002268 case Instruction::Ret:
2269 return X86SelectRet(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00002270 case Instruction::ICmp:
2271 case Instruction::FCmp:
2272 return X86SelectCmp(I);
Dan Gohmana5753b32008-09-05 01:06:14 +00002273 case Instruction::ZExt:
2274 return X86SelectZExt(I);
2275 case Instruction::Br:
2276 return X86SelectBranch(I);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002277 case Instruction::Call:
2278 return X86SelectCall(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00002279 case Instruction::LShr:
2280 case Instruction::AShr:
2281 case Instruction::Shl:
2282 return X86SelectShift(I);
Eli Bendersky24a36eb2013-04-17 20:10:13 +00002283 case Instruction::SDiv:
2284 case Instruction::UDiv:
2285 case Instruction::SRem:
2286 case Instruction::URem:
2287 return X86SelectDivRem(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00002288 case Instruction::Select:
2289 return X86SelectSelect(I);
Evan Chengb9286692008-09-07 08:47:42 +00002290 case Instruction::Trunc:
2291 return X86SelectTrunc(I);
Dan Gohmanbf646f22008-09-10 21:02:08 +00002292 case Instruction::FPExt:
2293 return X86SelectFPExt(I);
2294 case Instruction::FPTrunc:
2295 return X86SelectFPTrunc(I);
Dan Gohmana62e4ab2009-03-13 23:53:06 +00002296 case Instruction::IntToPtr: // Deliberate fall-through.
2297 case Instruction::PtrToInt: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002298 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
2299 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohmana62e4ab2009-03-13 23:53:06 +00002300 if (DstVT.bitsGT(SrcVT))
2301 return X86SelectZExt(I);
2302 if (DstVT.bitsLT(SrcVT))
2303 return X86SelectTrunc(I);
2304 unsigned Reg = getRegForValue(I->getOperand(0));
2305 if (Reg == 0) return false;
2306 UpdateValueMap(I, Reg);
2307 return true;
2308 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00002309 }
2310
2311 return false;
2312}
2313
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002314unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00002315 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00002316 if (!isTypeLegal(C->getType(), VT))
Michael Liao3c898062012-08-30 00:30:16 +00002317 return 0;
2318
2319 // Can't handle alternate code models yet.
2320 if (TM.getCodeModel() != CodeModel::Small)
2321 return 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00002322
Owen Anderson50288e32008-09-05 00:06:23 +00002323 // Get opcode and regclass of the output for the given load instruction.
2324 unsigned Opc = 0;
2325 const TargetRegisterClass *RC = NULL;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002326 switch (VT.SimpleTy) {
Michael Liao3c898062012-08-30 00:30:16 +00002327 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00002328 case MVT::i8:
Owen Anderson50288e32008-09-05 00:06:23 +00002329 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +00002330 RC = &X86::GR8RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002331 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002332 case MVT::i16:
Owen Anderson50288e32008-09-05 00:06:23 +00002333 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +00002334 RC = &X86::GR16RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002335 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002336 case MVT::i32:
Owen Anderson50288e32008-09-05 00:06:23 +00002337 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +00002338 RC = &X86::GR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002339 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002340 case MVT::i64:
Owen Anderson50288e32008-09-05 00:06:23 +00002341 // Must be in x86-64 mode.
2342 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +00002343 RC = &X86::GR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002344 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002345 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002346 if (X86ScalarSSEf32) {
2347 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +00002348 RC = &X86::FR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002349 } else {
2350 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +00002351 RC = &X86::RFP32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002352 }
2353 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002354 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002355 if (X86ScalarSSEf64) {
2356 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +00002357 RC = &X86::FR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002358 } else {
2359 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +00002360 RC = &X86::RFP64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002361 }
2362 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002363 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +00002364 // No f80 support yet.
Michael Liao3c898062012-08-30 00:30:16 +00002365 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00002366 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002367
Dan Gohman9801ba42008-09-19 22:16:54 +00002368 // Materialize addresses with LEA instructions.
Owen Anderson50288e32008-09-05 00:06:23 +00002369 if (isa<GlobalValue>(C)) {
Dan Gohman9801ba42008-09-19 22:16:54 +00002370 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00002371 if (X86SelectAddress(C, AM)) {
Chris Lattner48326602011-04-17 17:12:08 +00002372 // If the expression is just a basereg, then we're done, otherwise we need
2373 // to emit an LEA.
2374 if (AM.BaseType == X86AddressMode::RegBase &&
2375 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
2376 return AM.Base.Reg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002377
Chris Lattner48326602011-04-17 17:12:08 +00002378 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman9801ba42008-09-19 22:16:54 +00002379 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002380 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2381 TII.get(Opc), ResultReg), AM);
Owen Anderson50288e32008-09-05 00:06:23 +00002382 return ResultReg;
Dan Gohman9801ba42008-09-19 22:16:54 +00002383 }
Evan Chengf5bc7e52008-09-05 21:00:03 +00002384 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00002385 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002386
Owen Andersond41c7162008-09-06 01:11:01 +00002387 // MachineConstantPool wants an explicit alignment.
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002388 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00002389 if (Align == 0) {
2390 // Alignment of vector types. FIXME!
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00002391 Align = TD.getTypeAllocSize(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00002392 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002393
Dan Gohman8392f0c2008-09-30 01:21:32 +00002394 // x86-32 PIC requires a PIC base register for constant pools.
2395 unsigned PICBase = 0;
Chris Lattnera3260c02009-06-27 01:31:51 +00002396 unsigned char OpFlag = 0;
Chris Lattner21c29402009-07-10 21:00:45 +00002397 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattnerfef11d62009-07-09 04:39:06 +00002398 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohman87fb4e82010-07-07 16:29:44 +00002399 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00002400 } else if (Subtarget->isPICStyleGOT()) {
2401 OpFlag = X86II::MO_GOTOFF;
Dan Gohman87fb4e82010-07-07 16:29:44 +00002402 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00002403 } else if (Subtarget->isPICStyleRIPRel() &&
2404 TM.getCodeModel() == CodeModel::Small) {
2405 PICBase = X86::RIP;
Chris Lattnera3260c02009-06-27 01:31:51 +00002406 }
Dan Gohman8392f0c2008-09-30 01:21:32 +00002407
2408 // Create the load from the constant pool.
Dan Gohman39d82f92008-09-10 20:11:02 +00002409 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman9801ba42008-09-19 22:16:54 +00002410 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002411 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2412 TII.get(Opc), ResultReg),
Chris Lattnera3260c02009-06-27 01:31:51 +00002413 MCPOffset, PICBase, OpFlag);
Dan Gohman8392f0c2008-09-30 01:21:32 +00002414
Owen Anderson50288e32008-09-05 00:06:23 +00002415 return ResultReg;
2416}
2417
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002418unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002419 // Fail on dynamic allocas. At this point, getRegForValue has already
2420 // checked its CSE maps, so if we're here trying to handle a dynamic
2421 // alloca, we're not going to succeed. X86SelectAddress has a
2422 // check for dynamic allocas, because it's called directly from
2423 // various places, but TargetMaterializeAlloca also needs a check
2424 // in order to avoid recursion between getRegForValue,
2425 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohman87fb4e82010-07-07 16:29:44 +00002426 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002427 return 0;
2428
Dan Gohman39d82f92008-09-10 20:11:02 +00002429 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00002430 if (!X86SelectAddress(C, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +00002431 return 0;
2432 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
Craig Topper760b1342012-02-22 05:59:10 +00002433 const TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Dan Gohman39d82f92008-09-10 20:11:02 +00002434 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002435 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2436 TII.get(Opc), ResultReg), AM);
Dan Gohman39d82f92008-09-10 20:11:02 +00002437 return ResultReg;
2438}
2439
Eli Friedman406c4712011-04-27 22:41:55 +00002440unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2441 MVT VT;
2442 if (!isTypeLegal(CF->getType(), VT))
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002443 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00002444
2445 // Get opcode and regclass for the given zero.
2446 unsigned Opc = 0;
2447 const TargetRegisterClass *RC = NULL;
2448 switch (VT.SimpleTy) {
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002449 default: return 0;
Craig Topper490c45c2012-08-11 17:53:00 +00002450 case MVT::f32:
2451 if (X86ScalarSSEf32) {
2452 Opc = X86::FsFLD0SS;
2453 RC = &X86::FR32RegClass;
2454 } else {
2455 Opc = X86::LD_Fp032;
2456 RC = &X86::RFP32RegClass;
2457 }
2458 break;
2459 case MVT::f64:
2460 if (X86ScalarSSEf64) {
2461 Opc = X86::FsFLD0SD;
2462 RC = &X86::FR64RegClass;
2463 } else {
2464 Opc = X86::LD_Fp064;
2465 RC = &X86::RFP64RegClass;
2466 }
2467 break;
2468 case MVT::f80:
2469 // No f80 support yet.
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002470 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00002471 }
2472
2473 unsigned ResultReg = createResultReg(RC);
2474 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
2475 return ResultReg;
2476}
2477
2478
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002479bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2480 const LoadInst *LI) {
Chris Lattnereeba0c72010-09-05 02:18:34 +00002481 X86AddressMode AM;
2482 if (!X86SelectAddress(LI->getOperand(0), AM))
2483 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00002484
Craig Topper55406d92012-08-11 17:46:16 +00002485 const X86InstrInfo &XII = (const X86InstrInfo&)TII;
Wesley Peck527da1b2010-11-23 03:31:01 +00002486
Chris Lattnereeba0c72010-09-05 02:18:34 +00002487 unsigned Size = TD.getTypeAllocSize(LI->getType());
2488 unsigned Alignment = LI->getAlignment();
2489
2490 SmallVector<MachineOperand, 8> AddrOps;
2491 AM.getFullAddress(AddrOps);
Wesley Peck527da1b2010-11-23 03:31:01 +00002492
Chris Lattnereeba0c72010-09-05 02:18:34 +00002493 MachineInstr *Result =
2494 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
2495 if (Result == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00002496
Chris Lattner2d186572011-01-16 02:27:38 +00002497 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnereeba0c72010-09-05 02:18:34 +00002498 MI->eraseFromParent();
2499 return true;
2500}
2501
2502
Evan Cheng24422d42008-09-03 00:03:49 +00002503namespace llvm {
Bob Wilson3e6fa462012-08-03 04:06:28 +00002504 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
2505 const TargetLibraryInfo *libInfo) {
2506 return new X86FastISel(funcInfo, libInfo);
Evan Cheng24422d42008-09-03 00:03:49 +00002507 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00002508}