blob: 7984e76edd643ca2d42a3e1a25fc8ff6e8de8015 [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
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000128 bool handleConstantAddresses(const Value *V, X86AddressMode &AM);
129
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000130 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman39d82f92008-09-10 20:11:02 +0000131
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000132 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000133
Eli Friedman406c4712011-04-27 22:41:55 +0000134 unsigned TargetMaterializeFloatZero(const ConstantFP *CF);
135
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000136 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
137 /// computed in an SSE register, not on the X87 floating point stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000138 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson9f944592009-08-11 20:47:22 +0000139 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
140 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000141 }
142
Chris Lattner229907c2011-07-18 04:54:35 +0000143 bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
Eli Friedmanbcc69142011-04-27 01:45:07 +0000144
Eli Friedman60afcc22011-05-20 22:21:04 +0000145 bool IsMemcpySmall(uint64_t Len);
146
Eli Friedmanbcc69142011-04-27 01:45:07 +0000147 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
148 X86AddressMode SrcAM, uint64_t Len);
Evan Cheng24422d42008-09-03 00:03:49 +0000149};
Wesley Peck527da1b2010-11-23 03:31:01 +0000150
Chris Lattnerd5ac9d82009-03-08 18:44:31 +0000151} // end anonymous namespace.
Dan Gohmand58f3e32008-08-28 23:21:34 +0000152
Chris Lattner229907c2011-07-18 04:54:35 +0000153bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000154 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
155 if (evt == MVT::Other || !evt.isSimple())
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000156 // Unhandled type. Halt "fast" selection and bail.
157 return false;
Duncan Sandsf5dda012010-11-03 11:35:31 +0000158
159 VT = evt.getSimpleVT();
Dan Gohman50331362008-09-30 00:48:39 +0000160 // For now, require SSE/SSE2 for performing floating-point operations,
161 // since x87 requires additional work.
Owen Anderson9f944592009-08-11 20:47:22 +0000162 if (VT == MVT::f64 && !X86ScalarSSEf64)
Craig Topper490c45c2012-08-11 17:53:00 +0000163 return false;
Owen Anderson9f944592009-08-11 20:47:22 +0000164 if (VT == MVT::f32 && !X86ScalarSSEf32)
Craig Topper490c45c2012-08-11 17:53:00 +0000165 return false;
Dan Gohman50331362008-09-30 00:48:39 +0000166 // Similarly, no f80 support yet.
Owen Anderson9f944592009-08-11 20:47:22 +0000167 if (VT == MVT::f80)
Dan Gohman50331362008-09-30 00:48:39 +0000168 return false;
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000169 // We only handle legal types. For example, on x86-32 the instruction
170 // selector contains all of the 64-bit instructions from x86-64,
171 // under the assumption that i64 won't be used if the target doesn't
172 // support it.
Owen Anderson9f944592009-08-11 20:47:22 +0000173 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000174}
175
176#include "X86GenCallingConv.inc"
177
Evan Chengf5bc7e52008-09-05 21:00:03 +0000178/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000179/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000180/// Return true and the result register by reference if it is possible.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000181bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Chengf5bc7e52008-09-05 21:00:03 +0000182 unsigned &ResultReg) {
183 // Get opcode and regclass of the output for the given load instruction.
184 unsigned Opc = 0;
185 const TargetRegisterClass *RC = NULL;
Owen Anderson9f944592009-08-11 20:47:22 +0000186 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000187 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000188 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +0000189 case MVT::i8:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000190 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +0000191 RC = &X86::GR8RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000192 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000193 case MVT::i16:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000194 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +0000195 RC = &X86::GR16RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000196 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000197 case MVT::i32:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000198 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +0000199 RC = &X86::GR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000200 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000201 case MVT::i64:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000202 // Must be in x86-64 mode.
203 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +0000204 RC = &X86::GR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000205 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000206 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000207 if (X86ScalarSSEf32) {
208 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +0000209 RC = &X86::FR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000210 } else {
211 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +0000212 RC = &X86::RFP32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000213 }
214 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000215 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000216 if (X86ScalarSSEf64) {
217 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +0000218 RC = &X86::FR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000219 } else {
220 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +0000221 RC = &X86::RFP64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000222 }
223 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000224 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +0000225 // No f80 support yet.
226 return false;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000227 }
228
229 ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000230 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
231 DL, TII.get(Opc), ResultReg), AM);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000232 return true;
233}
234
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000235/// X86FastEmitStore - Emit a machine instruction to store a value Val of
236/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
237/// and a displacement offset, or a GlobalAddress,
Evan Chengf5bc7e52008-09-05 21:00:03 +0000238/// i.e. V. Return true if it is possible.
239bool
Craig Topper4f55b0e2013-07-17 05:57:45 +0000240X86FastISel::X86FastEmitStore(EVT VT, unsigned ValReg,
241 const X86AddressMode &AM, bool Aligned) {
Dan Gohman8f658ba2008-09-08 16:31:35 +0000242 // Get opcode and regclass of the output for the given store instruction.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000243 unsigned Opc = 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000244 switch (VT.getSimpleVT().SimpleTy) {
245 case MVT::f80: // No f80 support yet.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000246 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000247 case MVT::i1: {
248 // Mask out all but lowest bit.
Craig Topperabadc662012-04-20 06:31:50 +0000249 unsigned AndResult = createResultReg(&X86::GR8RegClass);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000250 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Craig Topper4f55b0e2013-07-17 05:57:45 +0000251 TII.get(X86::AND8ri), AndResult).addReg(ValReg).addImm(1);
252 ValReg = AndResult;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000253 }
254 // FALLTHROUGH, handling i1 as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000255 case MVT::i8: Opc = X86::MOV8mr; break;
256 case MVT::i16: Opc = X86::MOV16mr; break;
257 case MVT::i32: Opc = X86::MOV32mr; break;
258 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
259 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000260 Opc = X86ScalarSSEf32 ?
261 (Subtarget->hasAVX() ? X86::VMOVSSmr : X86::MOVSSmr) : X86::ST_Fp32m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000262 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000263 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000264 Opc = X86ScalarSSEf64 ?
265 (Subtarget->hasAVX() ? X86::VMOVSDmr : X86::MOVSDmr) : X86::ST_Fp64m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000266 break;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000267 case MVT::v4f32:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000268 if (Aligned)
Craig Topper55475d42013-07-17 06:58:23 +0000269 Opc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000270 else
Craig Topper55475d42013-07-17 06:58:23 +0000271 Opc = Subtarget->hasAVX() ? X86::VMOVUPSmr : X86::MOVUPSmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000272 break;
273 case MVT::v2f64:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000274 if (Aligned)
Craig Topperad1fff92013-07-18 07:16:44 +0000275 Opc = Subtarget->hasAVX() ? X86::VMOVAPDmr : X86::MOVAPDmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000276 else
Craig Topperad1fff92013-07-18 07:16:44 +0000277 Opc = Subtarget->hasAVX() ? X86::VMOVUPDmr : X86::MOVUPDmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000278 break;
279 case MVT::v4i32:
280 case MVT::v2i64:
281 case MVT::v8i16:
282 case MVT::v16i8:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000283 if (Aligned)
Craig Topper55475d42013-07-17 06:58:23 +0000284 Opc = Subtarget->hasAVX() ? X86::VMOVDQAmr : X86::MOVDQAmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000285 else
Craig Topper55475d42013-07-17 06:58:23 +0000286 Opc = Subtarget->hasAVX() ? X86::VMOVDQUmr : X86::MOVDQUmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000287 break;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000288 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000289
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000290 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Craig Topper4f55b0e2013-07-17 05:57:45 +0000291 DL, TII.get(Opc)), AM).addReg(ValReg);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000292 return true;
293}
294
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000295bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Craig Topper4f55b0e2013-07-17 05:57:45 +0000296 const X86AddressMode &AM, bool Aligned) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000297 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +0000298 if (isa<ConstantPointerNull>(Val))
299 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +0000300
Chris Lattner3ba29352008-10-15 05:30:52 +0000301 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000302 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000303 unsigned Opc = 0;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000304 bool Signed = true;
Owen Anderson9f944592009-08-11 20:47:22 +0000305 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000306 default: break;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000307 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000308 case MVT::i8: Opc = X86::MOV8mi; break;
309 case MVT::i16: Opc = X86::MOV16mi; break;
310 case MVT::i32: Opc = X86::MOV32mi; break;
311 case MVT::i64:
Chris Lattner3ba29352008-10-15 05:30:52 +0000312 // Must be a 32-bit sign extended value.
Jakub Staszak11d1aee2012-11-15 19:05:23 +0000313 if (isInt<32>(CI->getSExtValue()))
Chris Lattner3ba29352008-10-15 05:30:52 +0000314 Opc = X86::MOV64mi32;
315 break;
316 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000317
Chris Lattner3ba29352008-10-15 05:30:52 +0000318 if (Opc) {
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000319 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
320 DL, TII.get(Opc)), AM)
John McCall796583e2010-04-06 23:35:53 +0000321 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000322 CI->getZExtValue());
Chris Lattner3ba29352008-10-15 05:30:52 +0000323 return true;
324 }
325 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000326
Chris Lattner3ba29352008-10-15 05:30:52 +0000327 unsigned ValReg = getRegForValue(Val);
328 if (ValReg == 0)
Wesley Peck527da1b2010-11-23 03:31:01 +0000329 return false;
330
Craig Topper4f55b0e2013-07-17 05:57:45 +0000331 return X86FastEmitStore(VT, ValReg, AM, Aligned);
Chris Lattner3ba29352008-10-15 05:30:52 +0000332}
333
Evan Cheng6500d172008-09-08 06:35:17 +0000334/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
335/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
336/// ISD::SIGN_EXTEND).
Owen Anderson53aa7a92009-08-10 22:56:29 +0000337bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
338 unsigned Src, EVT SrcVT,
Evan Cheng6500d172008-09-08 06:35:17 +0000339 unsigned &ResultReg) {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000340 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
341 Src, /*TODO: Kill=*/false);
Jakub Staszak701cc972013-02-14 21:50:09 +0000342 if (RR == 0)
Owen Anderson453564b2008-09-11 19:44:55 +0000343 return false;
Jakub Staszak701cc972013-02-14 21:50:09 +0000344
345 ResultReg = RR;
346 return true;
Evan Cheng6500d172008-09-08 06:35:17 +0000347}
348
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000349bool X86FastISel::handleConstantAddresses(const Value *V, X86AddressMode &AM) {
350 // Handle constant address.
351 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
352 // Can't handle alternate code models yet.
353 if (TM.getCodeModel() != CodeModel::Small)
354 return false;
355
356 // Can't handle TLS yet.
357 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
358 if (GVar->isThreadLocal())
359 return false;
360
361 // Can't handle TLS yet, part 2 (this is slightly crazy, but this is how
362 // it works...).
363 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
364 if (const GlobalVariable *GVar =
365 dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false)))
366 if (GVar->isThreadLocal())
367 return false;
368
369 // RIP-relative addresses can't have additional register operands, so if
370 // we've already folded stuff into the addressing mode, just force the
371 // global value into its own register, which we can use as the basereg.
372 if (!Subtarget->isPICStyleRIPRel() ||
373 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
374 // Okay, we've committed to selecting this global. Set up the address.
375 AM.GV = GV;
376
377 // Allow the subtarget to classify the global.
378 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
379
380 // If this reference is relative to the pic base, set it now.
381 if (isGlobalRelativeToPICBase(GVFlags)) {
382 // FIXME: How do we know Base.Reg is free??
383 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
384 }
385
386 // Unless the ABI requires an extra load, return a direct reference to
387 // the global.
388 if (!isGlobalStubReference(GVFlags)) {
389 if (Subtarget->isPICStyleRIPRel()) {
390 // Use rip-relative addressing if we can. Above we verified that the
391 // base and index registers are unused.
392 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
393 AM.Base.Reg = X86::RIP;
394 }
395 AM.GVOpFlags = GVFlags;
396 return true;
397 }
398
399 // Ok, we need to do a load from a stub. If we've already loaded from
400 // this stub, reuse the loaded pointer, otherwise emit the load now.
401 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
402 unsigned LoadReg;
403 if (I != LocalValueMap.end() && I->second != 0) {
404 LoadReg = I->second;
405 } else {
406 // Issue load from stub.
407 unsigned Opc = 0;
408 const TargetRegisterClass *RC = NULL;
409 X86AddressMode StubAM;
410 StubAM.Base.Reg = AM.Base.Reg;
411 StubAM.GV = GV;
412 StubAM.GVOpFlags = GVFlags;
413
414 // Prepare for inserting code in the local-value area.
415 SavePoint SaveInsertPt = enterLocalValueArea();
416
417 if (TLI.getPointerTy() == MVT::i64) {
418 Opc = X86::MOV64rm;
419 RC = &X86::GR64RegClass;
420
421 if (Subtarget->isPICStyleRIPRel())
422 StubAM.Base.Reg = X86::RIP;
423 } else {
424 Opc = X86::MOV32rm;
425 RC = &X86::GR32RegClass;
426 }
427
428 LoadReg = createResultReg(RC);
429 MachineInstrBuilder LoadMI =
430 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
431 addFullAddress(LoadMI, StubAM);
432
433 // Ok, back to normal mode.
434 leaveLocalValueArea(SaveInsertPt);
435
436 // Prevent loading GV stub multiple times in same MBB.
437 LocalValueMap[V] = LoadReg;
438 }
439
440 // Now construct the final address. Note that the Disp, Scale,
441 // and Index values may already be set here.
442 AM.Base.Reg = LoadReg;
443 AM.GV = 0;
444 return true;
445 }
446 }
447
448 // If all else fails, try to materialize the value in a register.
449 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
450 if (AM.Base.Reg == 0) {
451 AM.Base.Reg = getRegForValue(V);
452 return AM.Base.Reg != 0;
453 }
454 if (AM.IndexReg == 0) {
455 assert(AM.Scale == 1 && "Scale with no index!");
456 AM.IndexReg = getRegForValue(V);
457 return AM.IndexReg != 0;
458 }
459 }
460
461 return false;
462}
463
Dan Gohman39d82f92008-09-10 20:11:02 +0000464/// X86SelectAddress - Attempt to fill in an address from the given value.
465///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000466bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000467 SmallVector<const Value *, 32> GEPs;
Bill Wendling585a9012013-09-24 00:13:08 +0000468redo_gep:
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000469 const User *U = NULL;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000470 unsigned Opcode = Instruction::UserOp1;
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000471 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanaf4903d2010-06-18 20:44:47 +0000472 // Don't walk into other basic blocks; it's possible we haven't
473 // visited them yet, so the instructions may not yet be assigned
474 // virtual registers.
Dan Gohmanaeb5e662010-11-16 22:43:23 +0000475 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
476 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
477 Opcode = I->getOpcode();
478 U = I;
479 }
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000480 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman6e005fd2008-09-18 23:23:44 +0000481 Opcode = C->getOpcode();
482 U = C;
483 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000484
Chris Lattner229907c2011-07-18 04:54:35 +0000485 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
Chris Lattner874c92b2010-06-15 19:08:40 +0000486 if (Ty->getAddressSpace() > 255)
Dan Gohmana46d6072010-06-18 20:45:41 +0000487 // Fast instruction selection doesn't support the special
488 // address spaces.
Chris Lattner874c92b2010-06-15 19:08:40 +0000489 return false;
490
Dan Gohman6e005fd2008-09-18 23:23:44 +0000491 switch (Opcode) {
492 default: break;
493 case Instruction::BitCast:
494 // Look past bitcasts.
Chris Lattner8212d372009-07-10 05:33:42 +0000495 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman6e005fd2008-09-18 23:23:44 +0000496
497 case Instruction::IntToPtr:
498 // Look past no-op inttoptrs.
499 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000500 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000501 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000502
503 case Instruction::PtrToInt:
504 // Look past no-op ptrtoints.
505 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000506 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000507 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000508
509 case Instruction::Alloca: {
510 // Do static allocas.
511 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman87fb4e82010-07-07 16:29:44 +0000512 DenseMap<const AllocaInst*, int>::iterator SI =
513 FuncInfo.StaticAllocaMap.find(A);
514 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman007a6bb2008-09-26 19:15:30 +0000515 AM.BaseType = X86AddressMode::FrameIndexBase;
516 AM.Base.FrameIndex = SI->second;
517 return true;
518 }
519 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000520 }
521
522 case Instruction::Add: {
523 // Adds of constants are common and easy enough.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000524 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman2564b902008-09-26 20:04:15 +0000525 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
526 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000527 if (isInt<32>(Disp)) {
Dan Gohman2564b902008-09-26 20:04:15 +0000528 AM.Disp = (uint32_t)Disp;
Chris Lattner8212d372009-07-10 05:33:42 +0000529 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman2564b902008-09-26 20:04:15 +0000530 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000531 }
Dan Gohman6e005fd2008-09-18 23:23:44 +0000532 break;
533 }
534
535 case Instruction::GetElementPtr: {
Chris Lattner795667b2010-03-04 19:54:45 +0000536 X86AddressMode SavedAM = AM;
537
Dan Gohman6e005fd2008-09-18 23:23:44 +0000538 // Pattern-match simple GEPs.
Dan Gohman2564b902008-09-26 20:04:15 +0000539 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000540 unsigned IndexReg = AM.IndexReg;
541 unsigned Scale = AM.Scale;
542 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohman4c315242008-12-08 07:57:47 +0000543 // Iterate through the indices, folding what we can. Constants can be
544 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000545 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman6e005fd2008-09-18 23:23:44 +0000546 i != e; ++i, ++GTI) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000547 const Value *Op = *i;
Chris Lattner229907c2011-07-18 04:54:35 +0000548 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Dan Gohman6e005fd2008-09-18 23:23:44 +0000549 const StructLayout *SL = TD.getStructLayout(STy);
Chris Lattner4b026b92011-04-17 17:05:12 +0000550 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
551 continue;
552 }
Eric Christopher0713a9d2011-06-08 23:55:35 +0000553
Chris Lattner4b026b92011-04-17 17:05:12 +0000554 // A array/variable index is always of the form i*S where S is the
555 // constant scale size. See if we can push the scale into immediates.
556 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
557 for (;;) {
558 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
559 // Constant-offset addressing.
560 Disp += CI->getSExtValue() * S;
561 break;
Dan Gohmanc1783b32011-03-22 00:04:35 +0000562 }
Chris Lattner4b026b92011-04-17 17:05:12 +0000563 if (isa<AddOperator>(Op) &&
564 (!isa<Instruction>(Op) ||
565 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
566 == FuncInfo.MBB) &&
567 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
568 // An add (in the same block) with a constant operand. Fold the
569 // constant.
570 ConstantInt *CI =
571 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
572 Disp += CI->getSExtValue() * S;
573 // Iterate on the other operand.
574 Op = cast<AddOperator>(Op)->getOperand(0);
575 continue;
576 }
577 if (IndexReg == 0 &&
578 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
579 (S == 1 || S == 2 || S == 4 || S == 8)) {
580 // Scaled-index addressing.
581 Scale = S;
582 IndexReg = getRegForGEPIndex(Op).first;
583 if (IndexReg == 0)
584 return false;
585 break;
586 }
587 // Unsupported.
588 goto unsupported_gep;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000589 }
590 }
Bill Wendling585a9012013-09-24 00:13:08 +0000591
Dan Gohman2564b902008-09-26 20:04:15 +0000592 // Check for displacement overflow.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000593 if (!isInt<32>(Disp))
Dan Gohman2564b902008-09-26 20:04:15 +0000594 break;
Bill Wendling585a9012013-09-24 00:13:08 +0000595
Dan Gohman6e005fd2008-09-18 23:23:44 +0000596 AM.IndexReg = IndexReg;
597 AM.Scale = Scale;
Dan Gohman2564b902008-09-26 20:04:15 +0000598 AM.Disp = (uint32_t)Disp;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000599 GEPs.push_back(V);
Bill Wendling585a9012013-09-24 00:13:08 +0000600
601 if (const GetElementPtrInst *GEP =
602 dyn_cast<GetElementPtrInst>(U->getOperand(0))) {
603 // Ok, the GEP indices were covered by constant-offset and scaled-index
604 // addressing. Update the address state and move on to examining the base.
605 V = GEP;
606 goto redo_gep;
607 } else if (X86SelectAddress(U->getOperand(0), AM)) {
Chris Lattner6ce8e242010-03-04 19:48:19 +0000608 return true;
Bill Wendling585a9012013-09-24 00:13:08 +0000609 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000610
Chris Lattner4b026b92011-04-17 17:05:12 +0000611 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner6ce8e242010-03-04 19:48:19 +0000612 // our address and just match the value instead of completely failing.
613 AM = SavedAM;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000614
615 for (SmallVectorImpl<const Value *>::reverse_iterator
616 I = GEPs.rbegin(), E = GEPs.rend(); I != E; ++I)
617 if (handleConstantAddresses(*I, AM))
618 return true;
619
620 return false;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000621 unsupported_gep:
622 // Ok, the GEP indices weren't all covered.
623 break;
624 }
625 }
626
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000627 return handleConstantAddresses(V, AM);
Dan Gohman39d82f92008-09-10 20:11:02 +0000628}
629
Chris Lattner8212d372009-07-10 05:33:42 +0000630/// X86SelectCallAddress - Attempt to fill in an address from the given value.
631///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000632bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
633 const User *U = NULL;
Chris Lattner8212d372009-07-10 05:33:42 +0000634 unsigned Opcode = Instruction::UserOp1;
Quentin Colombet778dba12013-10-14 22:32:09 +0000635 const Instruction *I = dyn_cast<Instruction>(V);
Quentin Colombetf34568b2013-10-22 21:29:08 +0000636 // Record if the value is defined in the same basic block.
637 //
638 // This information is crucial to know whether or not folding an
639 // operand is valid.
640 // Indeed, FastISel generates or reuses a virtual register for all
641 // operands of all instructions it selects. Obviously, the definition and
642 // its uses must use the same virtual register otherwise the produced
643 // code is incorrect.
644 // Before instruction selection, FunctionLoweringInfo::set sets the virtual
645 // registers for values that are alive across basic blocks. This ensures
646 // that the values are consistently set between across basic block, even
647 // if different instruction selection mechanisms are used (e.g., a mix of
648 // SDISel and FastISel).
649 // For values local to a basic block, the instruction selection process
650 // generates these virtual registers with whatever method is appropriate
651 // for its needs. In particular, FastISel and SDISel do not share the way
652 // local virtual registers are set.
653 // Therefore, this is impossible (or at least unsafe) to share values
654 // between basic blocks unless they use the same instruction selection
655 // method, which is not guarantee for X86.
656 // Moreover, things like hasOneUse could not be used accurately, if we
657 // allow to reference values across basic blocks whereas they are not
658 // alive across basic blocks initially.
Quentin Colombet778dba12013-10-14 22:32:09 +0000659 bool InMBB = true;
660 if (I) {
Chris Lattner8212d372009-07-10 05:33:42 +0000661 Opcode = I->getOpcode();
662 U = I;
Quentin Colombet778dba12013-10-14 22:32:09 +0000663 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000664 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000665 Opcode = C->getOpcode();
666 U = C;
667 }
668
669 switch (Opcode) {
670 default: break;
671 case Instruction::BitCast:
Quentin Colombet778dba12013-10-14 22:32:09 +0000672 // Look past bitcasts if its operand is in the same BB.
673 if (InMBB)
674 return X86SelectCallAddress(U->getOperand(0), AM);
675 break;
Chris Lattner8212d372009-07-10 05:33:42 +0000676
677 case Instruction::IntToPtr:
Quentin Colombet778dba12013-10-14 22:32:09 +0000678 // Look past no-op inttoptrs if its operand is in the same BB.
679 if (InMBB &&
680 TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000681 return X86SelectCallAddress(U->getOperand(0), AM);
682 break;
683
684 case Instruction::PtrToInt:
Quentin Colombet778dba12013-10-14 22:32:09 +0000685 // Look past no-op ptrtoints if its operand is in the same BB.
686 if (InMBB &&
687 TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000688 return X86SelectCallAddress(U->getOperand(0), AM);
689 break;
690 }
691
692 // Handle constant address.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000693 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000694 // Can't handle alternate code models yet.
Chris Lattner25e7f912009-07-10 21:03:06 +0000695 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner8212d372009-07-10 05:33:42 +0000696 return false;
697
698 // RIP-relative addresses can't have additional register operands.
699 if (Subtarget->isPICStyleRIPRel() &&
700 (AM.Base.Reg != 0 || AM.IndexReg != 0))
701 return false;
702
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000703 // Can't handle DLLImport.
704 if (GV->hasDLLImportLinkage())
705 return false;
706
707 // Can't handle TLS.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000708 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000709 if (GVar->isThreadLocal())
Chris Lattner8212d372009-07-10 05:33:42 +0000710 return false;
711
712 // Okay, we've committed to selecting this global. Set up the basic address.
713 AM.GV = GV;
Wesley Peck527da1b2010-11-23 03:31:01 +0000714
Chris Lattner7277a802009-07-10 05:45:15 +0000715 // No ABI requires an extra load for anything other than DLLImport, which
716 // we rejected above. Return a direct reference to the global.
Chris Lattner7277a802009-07-10 05:45:15 +0000717 if (Subtarget->isPICStyleRIPRel()) {
718 // Use rip-relative addressing if we can. Above we verified that the
719 // base and index registers are unused.
720 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
721 AM.Base.Reg = X86::RIP;
Chris Lattner21c29402009-07-10 21:00:45 +0000722 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattner7277a802009-07-10 05:45:15 +0000723 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
724 } else if (Subtarget->isPICStyleGOT()) {
725 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner8212d372009-07-10 05:33:42 +0000726 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000727
Chris Lattner8212d372009-07-10 05:33:42 +0000728 return true;
729 }
730
731 // If all else fails, try to materialize the value in a register.
732 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
733 if (AM.Base.Reg == 0) {
734 AM.Base.Reg = getRegForValue(V);
735 return AM.Base.Reg != 0;
736 }
737 if (AM.IndexReg == 0) {
738 assert(AM.Scale == 1 && "Scale with no index!");
739 AM.IndexReg = getRegForValue(V);
740 return AM.IndexReg != 0;
741 }
742 }
743
744 return false;
745}
746
747
Owen Anderson4f948bd2008-09-04 07:08:58 +0000748/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000749bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000750 // Atomic stores need special handling.
Lang Hames7d2f7b52011-10-18 22:11:33 +0000751 const StoreInst *S = cast<StoreInst>(I);
752
753 if (S->isAtomic())
754 return false;
755
Craig Topper4f55b0e2013-07-17 05:57:45 +0000756 unsigned SABIAlignment =
757 TD.getABITypeAlignment(S->getValueOperand()->getType());
758 bool Aligned = S->getAlignment() == 0 || S->getAlignment() >= SABIAlignment;
759
Duncan Sandsf5dda012010-11-03 11:35:31 +0000760 MVT VT;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000761 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Anderson4f948bd2008-09-04 07:08:58 +0000762 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000763
Dan Gohman39d82f92008-09-10 20:11:02 +0000764 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +0000765 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman39d82f92008-09-10 20:11:02 +0000766 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000767
Craig Topper4f55b0e2013-07-17 05:57:45 +0000768 return X86FastEmitStore(VT, I->getOperand(0), AM, Aligned);
Owen Anderson4f948bd2008-09-04 07:08:58 +0000769}
770
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000771/// X86SelectRet - Select and emit code to implement ret instructions.
772bool X86FastISel::X86SelectRet(const Instruction *I) {
773 const ReturnInst *Ret = cast<ReturnInst>(I);
774 const Function &F = *I->getParent()->getParent();
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000775 const X86MachineFunctionInfo *X86MFInfo =
776 FuncInfo.MF->getInfo<X86MachineFunctionInfo>();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000777
778 if (!FuncInfo.CanLowerReturn)
779 return false;
780
781 CallingConv::ID CC = F.getCallingConv();
782 if (CC != CallingConv::C &&
783 CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +0000784 CC != CallingConv::X86_FastCall &&
785 CC != CallingConv::X86_64_SysV)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000786 return false;
787
Charles Davise8f297c2013-07-12 06:02:35 +0000788 if (Subtarget->isCallingConvWin64(CC))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000789 return false;
790
791 // Don't handle popping bytes on return for now.
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000792 if (X86MFInfo->getBytesToPopOnReturn() != 0)
Jakub Staszak74010cd2013-02-17 18:35:25 +0000793 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000794
795 // fastcc with -tailcallopt is intended to provide a guaranteed
796 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000797 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000798 return false;
799
800 // Let SDISel handle vararg functions.
801 if (F.isVarArg())
802 return false;
803
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000804 // Build a list of return value registers.
805 SmallVector<unsigned, 4> RetRegs;
806
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000807 if (Ret->getNumOperands() > 0) {
808 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +0000809 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000810
811 // Analyze operands of the call, assigning locations to each operand.
812 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000813 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +0000814 I->getContext());
Duncan Sandsfa7e6f22010-10-31 13:02:38 +0000815 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000816
817 const Value *RV = Ret->getOperand(0);
818 unsigned Reg = getRegForValue(RV);
819 if (Reg == 0)
820 return false;
821
822 // Only handle a single return value for now.
823 if (ValLocs.size() != 1)
824 return false;
825
826 CCValAssign &VA = ValLocs[0];
Wesley Peck527da1b2010-11-23 03:31:01 +0000827
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000828 // Don't bother handling odd stuff for now.
829 if (VA.getLocInfo() != CCValAssign::Full)
830 return false;
831 // Only handle register returns for now.
832 if (!VA.isRegLoc())
833 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000834
835 // The calling-convention tables for x87 returns don't tell
836 // the whole story.
837 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
838 return false;
839
Eli Friedman6fc94dd2011-05-18 23:13:10 +0000840 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedman22da7992011-05-19 22:16:13 +0000841 EVT SrcVT = TLI.getValueType(RV->getType());
842 EVT DstVT = VA.getValVT();
843 // Special handling for extended integers.
844 if (SrcVT != DstVT) {
845 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
846 return false;
847
848 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
849 return false;
850
851 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
852
853 if (SrcVT == MVT::i1) {
854 if (Outs[0].Flags.isSExt())
855 return false;
856 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
857 SrcVT = MVT::i8;
858 }
859 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
860 ISD::SIGN_EXTEND;
861 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
862 SrcReg, /*TODO: Kill=*/false);
863 }
864
865 // Make the copy.
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000866 unsigned DstReg = VA.getLocReg();
867 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000868 // Avoid a cross-class copy. This is very unlikely.
869 if (!SrcRC->contains(DstReg))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000870 return false;
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000871 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
872 DstReg).addReg(SrcReg);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000873
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000874 // Add register to return instruction.
875 RetRegs.push_back(VA.getLocReg());
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000876 }
877
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000878 // The x86-64 ABI for returning structs by value requires that we copy
879 // the sret argument into %rax for the return. We saved the argument into
880 // a virtual register in the entry block, so now we copy the value out
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000881 // and into %rax. We also do the same with %eax for Win32.
882 if (F.hasStructRetAttr() &&
883 (Subtarget->is64Bit() || Subtarget->isTargetWindows())) {
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000884 unsigned Reg = X86MFInfo->getSRetReturnReg();
885 assert(Reg &&
886 "SRetReturnReg should have been set in LowerFormalArguments()!");
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000887 unsigned RetReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000888 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000889 RetReg).addReg(Reg);
890 RetRegs.push_back(RetReg);
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000891 }
892
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000893 // Now emit the RET.
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000894 MachineInstrBuilder MIB =
895 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
896 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
897 MIB.addReg(RetRegs[i], RegState::Implicit);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000898 return true;
899}
900
Evan Chenga41ee292008-09-03 06:44:39 +0000901/// X86SelectLoad - Select and emit code to implement load instructions.
902///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000903bool X86FastISel::X86SelectLoad(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000904 // Atomic loads need special handling.
905 if (cast<LoadInst>(I)->isAtomic())
906 return false;
907
Duncan Sandsf5dda012010-11-03 11:35:31 +0000908 MVT VT;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000909 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Chenga41ee292008-09-03 06:44:39 +0000910 return false;
911
Dan Gohman39d82f92008-09-10 20:11:02 +0000912 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +0000913 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman39d82f92008-09-10 20:11:02 +0000914 return false;
Evan Chenga41ee292008-09-03 06:44:39 +0000915
Evan Chengf5bc7e52008-09-05 21:00:03 +0000916 unsigned ResultReg = 0;
Dan Gohman39d82f92008-09-10 20:11:02 +0000917 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000918 UpdateValueMap(I, ResultReg);
919 return true;
Evan Chenga41ee292008-09-03 06:44:39 +0000920 }
Evan Chengf5bc7e52008-09-05 21:00:03 +0000921 return false;
Evan Chenga41ee292008-09-03 06:44:39 +0000922}
923
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +0000924static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000925 bool HasAVX = Subtarget->hasAVX();
Craig Topperb0c0f722012-01-10 06:54:16 +0000926 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
927 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000928
Owen Anderson9f944592009-08-11 20:47:22 +0000929 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner74e01282008-10-15 04:32:45 +0000930 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000931 case MVT::i8: return X86::CMP8rr;
932 case MVT::i16: return X86::CMP16rr;
933 case MVT::i32: return X86::CMP32rr;
934 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000935 case MVT::f32:
936 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
937 case MVT::f64:
938 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohman1ab1d312008-10-02 22:15:21 +0000939 }
Dan Gohman1ab1d312008-10-02 22:15:21 +0000940}
941
Chris Lattner88f47542008-10-15 04:13:29 +0000942/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
943/// of the comparison, return an opcode that works for the compare (e.g.
944/// CMP32ri) otherwise return 0.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000945static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson9f944592009-08-11 20:47:22 +0000946 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner88f47542008-10-15 04:13:29 +0000947 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner74e01282008-10-15 04:32:45 +0000948 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000949 case MVT::i8: return X86::CMP8ri;
950 case MVT::i16: return X86::CMP16ri;
951 case MVT::i32: return X86::CMP32ri;
952 case MVT::i64:
Chris Lattner74e01282008-10-15 04:32:45 +0000953 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
954 // field.
Chris Lattner3ba29352008-10-15 05:30:52 +0000955 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner74e01282008-10-15 04:32:45 +0000956 return X86::CMP64ri32;
957 return 0;
958 }
Chris Lattner88f47542008-10-15 04:13:29 +0000959}
960
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000961bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
962 EVT VT) {
Chris Lattnerd46b9512008-10-15 04:26:38 +0000963 unsigned Op0Reg = getRegForValue(Op0);
964 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000965
Chris Lattnere388725a2008-10-15 05:18:04 +0000966 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +0000967 if (isa<ConstantPointerNull>(Op1))
968 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +0000969
Chris Lattnerd46b9512008-10-15 04:26:38 +0000970 // We have two options: compare with register or immediate. If the RHS of
971 // the compare is an immediate that we can fold into this compare, use
972 // CMPri, otherwise use CMPrr.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000973 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner74e01282008-10-15 04:32:45 +0000974 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000975 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
976 .addReg(Op0Reg)
977 .addImm(Op1C->getSExtValue());
Chris Lattnerd46b9512008-10-15 04:26:38 +0000978 return true;
979 }
980 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000981
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +0000982 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattnerd46b9512008-10-15 04:26:38 +0000983 if (CompareOpc == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000984
Chris Lattnerd46b9512008-10-15 04:26:38 +0000985 unsigned Op1Reg = getRegForValue(Op1);
986 if (Op1Reg == 0) return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000987 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
988 .addReg(Op0Reg)
989 .addReg(Op1Reg);
Wesley Peck527da1b2010-11-23 03:31:01 +0000990
Chris Lattnerd46b9512008-10-15 04:26:38 +0000991 return true;
992}
993
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000994bool X86FastISel::X86SelectCmp(const Instruction *I) {
995 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000996
Duncan Sandsf5dda012010-11-03 11:35:31 +0000997 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +0000998 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman09faf812008-09-05 01:33:56 +0000999 return false;
1000
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001001 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattnera3596db2008-10-15 03:47:17 +00001002 unsigned SetCCOpc;
Chris Lattnerf32ce222008-10-15 03:52:54 +00001003 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001004 switch (CI->getPredicate()) {
1005 case CmpInst::FCMP_OEQ: {
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001006 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
1007 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001008
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001009 unsigned EReg = createResultReg(&X86::GR8RegClass);
1010 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001011 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
1012 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1013 TII.get(X86::SETNPr), NPReg);
Wesley Peck527da1b2010-11-23 03:31:01 +00001014 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen9bba9022009-02-13 02:33:27 +00001015 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattnera3596db2008-10-15 03:47:17 +00001016 UpdateValueMap(I, ResultReg);
1017 return true;
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001018 }
1019 case CmpInst::FCMP_UNE: {
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001020 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
1021 return false;
1022
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001023 unsigned NEReg = createResultReg(&X86::GR8RegClass);
1024 unsigned PReg = createResultReg(&X86::GR8RegClass);
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001025 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETNEr), NEReg);
1026 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETPr), PReg);
1027 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::OR8rr),ResultReg)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001028 .addReg(PReg).addReg(NEReg);
Chris Lattnera3596db2008-10-15 03:47:17 +00001029 UpdateValueMap(I, ResultReg);
1030 return true;
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001031 }
Chris Lattnerf32ce222008-10-15 03:52:54 +00001032 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
1033 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
1034 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
1035 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
1036 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
1037 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
1038 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
1039 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
1040 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
1041 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
1042 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
1043 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peck527da1b2010-11-23 03:31:01 +00001044
Chris Lattnerf32ce222008-10-15 03:52:54 +00001045 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
1046 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
1047 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
1048 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
1049 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
1050 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
1051 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
1052 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
1053 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
1054 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001055 default:
1056 return false;
1057 }
1058
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001059 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattnerf32ce222008-10-15 03:52:54 +00001060 if (SwapArgs)
Chris Lattnerd46b9512008-10-15 04:26:38 +00001061 std::swap(Op0, Op1);
Chris Lattnerf32ce222008-10-15 03:52:54 +00001062
Chris Lattnerd46b9512008-10-15 04:26:38 +00001063 // Emit a compare of Op0/Op1.
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001064 if (!X86FastEmitCompare(Op0, Op1, VT))
1065 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001066
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001067 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001068 UpdateValueMap(I, ResultReg);
1069 return true;
1070}
Evan Chenga41ee292008-09-03 06:44:39 +00001071
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001072bool X86FastISel::X86SelectZExt(const Instruction *I) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001073 EVT DstVT = TLI.getValueType(I->getType());
1074 if (!TLI.isTypeLegal(DstVT))
1075 return false;
1076
1077 unsigned ResultReg = getRegForValue(I->getOperand(0));
1078 if (ResultReg == 0)
1079 return false;
1080
Tim Northover04eb4232013-05-30 10:43:18 +00001081 // Handle zero-extension from i1 to i8, which is common.
Craig Topper56710102013-08-15 02:33:50 +00001082 MVT SrcVT = TLI.getSimpleValueType(I->getOperand(0)->getType());
Tim Northover04eb4232013-05-30 10:43:18 +00001083 if (SrcVT.SimpleTy == MVT::i1) {
1084 // Set the high bits to zero.
1085 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1086 SrcVT = MVT::i8;
Eli Friedmanc7035512011-05-25 23:49:02 +00001087
Tim Northover04eb4232013-05-30 10:43:18 +00001088 if (ResultReg == 0)
1089 return false;
1090 }
1091
1092 if (DstVT == MVT::i64) {
1093 // Handle extension to 64-bits via sub-register shenanigans.
1094 unsigned MovInst;
1095
1096 switch (SrcVT.SimpleTy) {
1097 case MVT::i8: MovInst = X86::MOVZX32rr8; break;
1098 case MVT::i16: MovInst = X86::MOVZX32rr16; break;
1099 case MVT::i32: MovInst = X86::MOV32rr; break;
1100 default: llvm_unreachable("Unexpected zext to i64 source type");
1101 }
1102
1103 unsigned Result32 = createResultReg(&X86::GR32RegClass);
1104 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovInst), Result32)
1105 .addReg(ResultReg);
1106
1107 ResultReg = createResultReg(&X86::GR64RegClass);
1108 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::SUBREG_TO_REG),
1109 ResultReg)
1110 .addImm(0).addReg(Result32).addImm(X86::sub_32bit);
1111 } else if (DstVT != MVT::i8) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001112 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1113 ResultReg, /*Kill=*/true);
1114 if (ResultReg == 0)
1115 return false;
Dan Gohmana5753b32008-09-05 01:06:14 +00001116 }
1117
Eli Friedmanc7035512011-05-25 23:49:02 +00001118 UpdateValueMap(I, ResultReg);
1119 return true;
Dan Gohmana5753b32008-09-05 01:06:14 +00001120}
1121
Chris Lattnerd46b9512008-10-15 04:26:38 +00001122
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001123bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmana5753b32008-09-05 01:06:14 +00001124 // Unconditional branches are selected by tablegen-generated code.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001125 // Handle a conditional branch.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001126 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohman87fb4e82010-07-07 16:29:44 +00001127 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1128 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmana5753b32008-09-05 01:06:14 +00001129
Dan Gohman42ef6692010-08-21 02:32:36 +00001130 // Fold the common case of a conditional branch with a comparison
1131 // in the same block (values defined on other blocks may not have
1132 // initialized registers).
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001133 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman42ef6692010-08-21 02:32:36 +00001134 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001135 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmana5753b32008-09-05 01:06:14 +00001136
Dan Gohman1ab1d312008-10-02 22:15:21 +00001137 // Try to take advantage of fallthrough opportunities.
1138 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001139 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohman1ab1d312008-10-02 22:15:21 +00001140 std::swap(TrueMBB, FalseMBB);
1141 Predicate = CmpInst::getInversePredicate(Predicate);
1142 }
1143
Chris Lattner0ce717a2008-10-15 03:58:05 +00001144 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
1145 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
1146
Dan Gohman1ab1d312008-10-02 22:15:21 +00001147 switch (Predicate) {
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001148 case CmpInst::FCMP_OEQ:
1149 std::swap(TrueMBB, FalseMBB);
1150 Predicate = CmpInst::FCMP_UNE;
1151 // FALL THROUGH
Chris Lattner2b0a7a22010-02-11 19:25:55 +00001152 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1153 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1154 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1155 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
1156 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
1157 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1158 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
1159 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
1160 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1161 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
1162 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
1163 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1164 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peck527da1b2010-11-23 03:31:01 +00001165
Chris Lattner2b0a7a22010-02-11 19:25:55 +00001166 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1167 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1168 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1169 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1170 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1171 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1172 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
1173 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1174 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1175 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001176 default:
1177 return false;
1178 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001179
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001180 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner47bef252008-10-15 04:02:26 +00001181 if (SwapArgs)
1182 std::swap(Op0, Op1);
1183
Chris Lattnerd46b9512008-10-15 04:26:38 +00001184 // Emit a compare of the LHS and RHS, setting the flags.
1185 if (!X86FastEmitCompare(Op0, Op1, VT))
1186 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001187
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001188 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1189 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001190
1191 if (Predicate == CmpInst::FCMP_UNE) {
1192 // X86 requires a second branch to handle UNE (and OEQ,
1193 // which is mapped to UNE above).
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001194 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1195 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001196 }
1197
Stuart Hastings0125b642010-06-17 22:43:56 +00001198 FastEmitBranch(FalseMBB, DL);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001199 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohman1ab1d312008-10-02 22:15:21 +00001200 return true;
1201 }
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001202 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1203 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1204 // typically happen for _Bool and C++ bools.
1205 MVT SourceVT;
1206 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1207 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1208 unsigned TestOpc = 0;
1209 switch (SourceVT.SimpleTy) {
1210 default: break;
1211 case MVT::i8: TestOpc = X86::TEST8ri; break;
1212 case MVT::i16: TestOpc = X86::TEST16ri; break;
1213 case MVT::i32: TestOpc = X86::TEST32ri; break;
1214 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1215 }
1216 if (TestOpc) {
1217 unsigned OpReg = getRegForValue(TI->getOperand(0));
1218 if (OpReg == 0) return false;
1219 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TestOpc))
1220 .addReg(OpReg).addImm(1);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001221
Chris Lattnerc59290a2011-04-19 04:26:32 +00001222 unsigned JmpOpc = X86::JNE_4;
1223 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1224 std::swap(TrueMBB, FalseMBB);
1225 JmpOpc = X86::JE_4;
1226 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001227
Chris Lattnerc59290a2011-04-19 04:26:32 +00001228 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(JmpOpc))
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001229 .addMBB(TrueMBB);
1230 FastEmitBranch(FalseMBB, DL);
1231 FuncInfo.MBB->addSuccessor(TrueMBB);
1232 return true;
1233 }
1234 }
Dan Gohman1ab1d312008-10-02 22:15:21 +00001235 }
1236
1237 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman0eea0292011-04-27 01:34:27 +00001238 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1239 // in an explicit cast, so make sure to handle that correctly.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001240 unsigned OpReg = getRegForValue(BI->getCondition());
1241 if (OpReg == 0) return false;
1242
Eli Friedman0eea0292011-04-27 01:34:27 +00001243 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8ri))
1244 .addReg(OpReg).addImm(1);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001245 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1246 .addMBB(TrueMBB);
Stuart Hastings0125b642010-06-17 22:43:56 +00001247 FastEmitBranch(FalseMBB, DL);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001248 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmana5753b32008-09-05 01:06:14 +00001249 return true;
1250}
1251
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001252bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001253 unsigned CReg = 0, OpReg = 0;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001254 const TargetRegisterClass *RC = NULL;
Duncan Sands9dff9be2010-02-15 16:12:20 +00001255 if (I->getType()->isIntegerTy(8)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001256 CReg = X86::CL;
1257 RC = &X86::GR8RegClass;
1258 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001259 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1260 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1261 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001262 default: return false;
1263 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001264 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001265 CReg = X86::CX;
1266 RC = &X86::GR16RegClass;
1267 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001268 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1269 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1270 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001271 default: return false;
1272 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001273 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001274 CReg = X86::ECX;
1275 RC = &X86::GR32RegClass;
1276 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001277 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1278 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1279 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001280 default: return false;
1281 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001282 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001283 CReg = X86::RCX;
1284 RC = &X86::GR64RegClass;
1285 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001286 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1287 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1288 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001289 default: return false;
1290 }
1291 } else {
1292 return false;
1293 }
1294
Duncan Sandsf5dda012010-11-03 11:35:31 +00001295 MVT VT;
1296 if (!isTypeLegal(I->getType(), VT))
Dan Gohmandb06a992008-09-05 21:27:34 +00001297 return false;
1298
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001299 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1300 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001301
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001302 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1303 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00001304 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1305 CReg).addReg(Op1Reg);
Dan Gohmand3917152008-10-07 21:50:36 +00001306
1307 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001308 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohmand3917152008-10-07 21:50:36 +00001309 if (CReg != X86::CL)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001310 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1311 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001312 .addReg(CReg, RegState::Kill);
Dan Gohmand3917152008-10-07 21:50:36 +00001313
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001314 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001315 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1316 .addReg(Op0Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001317 UpdateValueMap(I, ResultReg);
1318 return true;
1319}
1320
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001321bool X86FastISel::X86SelectDivRem(const Instruction *I) {
1322 const static unsigned NumTypes = 4; // i8, i16, i32, i64
1323 const static unsigned NumOps = 4; // SDiv, SRem, UDiv, URem
1324 const static bool S = true; // IsSigned
1325 const static bool U = false; // !IsSigned
1326 const static unsigned Copy = TargetOpcode::COPY;
1327 // For the X86 DIV/IDIV instruction, in most cases the dividend
1328 // (numerator) must be in a specific register pair highreg:lowreg,
1329 // producing the quotient in lowreg and the remainder in highreg.
1330 // For most data types, to set up the instruction, the dividend is
1331 // copied into lowreg, and lowreg is sign-extended or zero-extended
1332 // into highreg. The exception is i8, where the dividend is defined
1333 // as a single register rather than a register pair, and we
1334 // therefore directly sign-extend or zero-extend the dividend into
1335 // lowreg, instead of copying, and ignore the highreg.
1336 const static struct DivRemEntry {
1337 // The following portion depends only on the data type.
1338 const TargetRegisterClass *RC;
1339 unsigned LowInReg; // low part of the register pair
1340 unsigned HighInReg; // high part of the register pair
1341 // The following portion depends on both the data type and the operation.
1342 struct DivRemResult {
1343 unsigned OpDivRem; // The specific DIV/IDIV opcode to use.
1344 unsigned OpSignExtend; // Opcode for sign-extending lowreg into
1345 // highreg, or copying a zero into highreg.
1346 unsigned OpCopy; // Opcode for copying dividend into lowreg, or
1347 // zero/sign-extending into lowreg for i8.
1348 unsigned DivRemResultReg; // Register containing the desired result.
1349 bool IsOpSigned; // Whether to use signed or unsigned form.
1350 } ResultTable[NumOps];
1351 } OpTable[NumTypes] = {
1352 { &X86::GR8RegClass, X86::AX, 0, {
1353 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AL, S }, // SDiv
1354 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AH, S }, // SRem
1355 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AL, U }, // UDiv
1356 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AH, U }, // URem
1357 }
1358 }, // i8
1359 { &X86::GR16RegClass, X86::AX, X86::DX, {
1360 { X86::IDIV16r, X86::CWD, Copy, X86::AX, S }, // SDiv
1361 { X86::IDIV16r, X86::CWD, Copy, X86::DX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001362 { X86::DIV16r, X86::MOV32r0, Copy, X86::AX, U }, // UDiv
1363 { X86::DIV16r, X86::MOV32r0, Copy, X86::DX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001364 }
1365 }, // i16
1366 { &X86::GR32RegClass, X86::EAX, X86::EDX, {
1367 { X86::IDIV32r, X86::CDQ, Copy, X86::EAX, S }, // SDiv
1368 { X86::IDIV32r, X86::CDQ, Copy, X86::EDX, S }, // SRem
1369 { X86::DIV32r, X86::MOV32r0, Copy, X86::EAX, U }, // UDiv
1370 { X86::DIV32r, X86::MOV32r0, Copy, X86::EDX, U }, // URem
1371 }
1372 }, // i32
1373 { &X86::GR64RegClass, X86::RAX, X86::RDX, {
1374 { X86::IDIV64r, X86::CQO, Copy, X86::RAX, S }, // SDiv
1375 { X86::IDIV64r, X86::CQO, Copy, X86::RDX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001376 { X86::DIV64r, X86::MOV32r0, Copy, X86::RAX, U }, // UDiv
1377 { X86::DIV64r, X86::MOV32r0, Copy, X86::RDX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001378 }
1379 }, // i64
1380 };
1381
1382 MVT VT;
1383 if (!isTypeLegal(I->getType(), VT))
1384 return false;
1385
1386 unsigned TypeIndex, OpIndex;
1387 switch (VT.SimpleTy) {
1388 default: return false;
1389 case MVT::i8: TypeIndex = 0; break;
1390 case MVT::i16: TypeIndex = 1; break;
1391 case MVT::i32: TypeIndex = 2; break;
1392 case MVT::i64: TypeIndex = 3;
1393 if (!Subtarget->is64Bit())
1394 return false;
1395 break;
1396 }
1397
1398 switch (I->getOpcode()) {
1399 default: llvm_unreachable("Unexpected div/rem opcode");
1400 case Instruction::SDiv: OpIndex = 0; break;
1401 case Instruction::SRem: OpIndex = 1; break;
1402 case Instruction::UDiv: OpIndex = 2; break;
1403 case Instruction::URem: OpIndex = 3; break;
1404 }
1405
1406 const DivRemEntry &TypeEntry = OpTable[TypeIndex];
1407 const DivRemEntry::DivRemResult &OpEntry = TypeEntry.ResultTable[OpIndex];
1408 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1409 if (Op0Reg == 0)
1410 return false;
1411 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1412 if (Op1Reg == 0)
1413 return false;
1414
1415 // Move op0 into low-order input register.
1416 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1417 TII.get(OpEntry.OpCopy), TypeEntry.LowInReg).addReg(Op0Reg);
1418 // Zero-extend or sign-extend into high-order input register.
1419 if (OpEntry.OpSignExtend) {
1420 if (OpEntry.IsOpSigned)
1421 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1422 TII.get(OpEntry.OpSignExtend));
Tim Northover64ec0ff2013-05-30 13:19:42 +00001423 else {
1424 unsigned Zero32 = createResultReg(&X86::GR32RegClass);
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001425 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Tim Northover64ec0ff2013-05-30 13:19:42 +00001426 TII.get(X86::MOV32r0), Zero32);
1427
1428 // Copy the zero into the appropriate sub/super/identical physical
1429 // register. Unfortunately the operations needed are not uniform enough to
1430 // fit neatly into the table above.
1431 if (VT.SimpleTy == MVT::i16) {
1432 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001433 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001434 .addReg(Zero32, 0, X86::sub_16bit);
1435 } else if (VT.SimpleTy == MVT::i32) {
1436 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001437 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001438 .addReg(Zero32);
1439 } else if (VT.SimpleTy == MVT::i64) {
1440 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1441 TII.get(TargetOpcode::SUBREG_TO_REG), TypeEntry.HighInReg)
1442 .addImm(0).addReg(Zero32).addImm(X86::sub_32bit);
1443 }
1444 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001445 }
1446 // Generate the DIV/IDIV instruction.
1447 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1448 TII.get(OpEntry.OpDivRem)).addReg(Op1Reg);
Jim Grosbachc35388f2013-07-09 02:07:25 +00001449 // For i8 remainder, we can't reference AH directly, as we'll end
1450 // up with bogus copies like %R9B = COPY %AH. Reference AX
1451 // instead to prevent AH references in a REX instruction.
1452 //
1453 // The current assumption of the fast register allocator is that isel
1454 // won't generate explicit references to the GPR8_NOREX registers. If
1455 // the allocator and/or the backend get enhanced to be more robust in
1456 // that regard, this can be, and should be, removed.
1457 unsigned ResultReg = 0;
1458 if ((I->getOpcode() == Instruction::SRem ||
1459 I->getOpcode() == Instruction::URem) &&
1460 OpEntry.DivRemResultReg == X86::AH && Subtarget->is64Bit()) {
1461 unsigned SourceSuperReg = createResultReg(&X86::GR16RegClass);
1462 unsigned ResultSuperReg = createResultReg(&X86::GR16RegClass);
1463 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1464 TII.get(Copy), SourceSuperReg).addReg(X86::AX);
1465
1466 // Shift AX right by 8 bits instead of using AH.
1467 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SHR16ri),
1468 ResultSuperReg).addReg(SourceSuperReg).addImm(8);
1469
1470 // Now reference the 8-bit subreg of the result.
1471 ResultReg = FastEmitInst_extractsubreg(MVT::i8, ResultSuperReg,
1472 /*Kill=*/true, X86::sub_8bit);
1473 }
1474 // Copy the result out of the physreg if we haven't already.
1475 if (!ResultReg) {
1476 ResultReg = createResultReg(TypeEntry.RC);
1477 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Copy), ResultReg)
1478 .addReg(OpEntry.DivRemResultReg);
1479 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001480 UpdateValueMap(I, ResultReg);
1481
1482 return true;
1483}
1484
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001485bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001486 MVT VT;
1487 if (!isTypeLegal(I->getType(), VT))
Chris Lattnera0f9d492008-10-15 05:07:36 +00001488 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001489
Eric Christopher0574cc52010-09-29 23:00:29 +00001490 // We only use cmov here, if we don't have a cmov instruction bail.
1491 if (!Subtarget->hasCMov()) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001492
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001493 unsigned Opc = 0;
1494 const TargetRegisterClass *RC = NULL;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001495 if (VT == MVT::i16) {
Dan Gohmane5560182008-09-05 21:13:04 +00001496 Opc = X86::CMOVE16rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001497 RC = &X86::GR16RegClass;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001498 } else if (VT == MVT::i32) {
Dan Gohmane5560182008-09-05 21:13:04 +00001499 Opc = X86::CMOVE32rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001500 RC = &X86::GR32RegClass;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001501 } else if (VT == MVT::i64) {
Dan Gohmane5560182008-09-05 21:13:04 +00001502 Opc = X86::CMOVE64rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001503 RC = &X86::GR64RegClass;
1504 } else {
Wesley Peck527da1b2010-11-23 03:31:01 +00001505 return false;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001506 }
1507
1508 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1509 if (Op0Reg == 0) return false;
1510 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1511 if (Op1Reg == 0) return false;
1512 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1513 if (Op2Reg == 0) return false;
1514
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001515 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1516 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001517 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001518 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1519 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001520 UpdateValueMap(I, ResultReg);
1521 return true;
1522}
1523
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001524bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00001525 // fpext from float to double.
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001526 if (X86ScalarSSEf64 &&
Chris Lattnerfdd87902009-10-05 05:54:46 +00001527 I->getType()->isDoubleTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001528 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001529 if (V->getType()->isFloatTy()) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00001530 unsigned OpReg = getRegForValue(V);
1531 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00001532 unsigned ResultReg = createResultReg(&X86::FR64RegClass);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001533 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1534 TII.get(X86::CVTSS2SDrr), ResultReg)
1535 .addReg(OpReg);
Chris Lattnera0f9d492008-10-15 05:07:36 +00001536 UpdateValueMap(I, ResultReg);
1537 return true;
Dan Gohmanbf646f22008-09-10 21:02:08 +00001538 }
1539 }
1540
1541 return false;
1542}
1543
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001544bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001545 if (X86ScalarSSEf64) {
Chris Lattnerfdd87902009-10-05 05:54:46 +00001546 if (I->getType()->isFloatTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001547 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001548 if (V->getType()->isDoubleTy()) {
Dan Gohmanbf646f22008-09-10 21:02:08 +00001549 unsigned OpReg = getRegForValue(V);
1550 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00001551 unsigned ResultReg = createResultReg(&X86::FR32RegClass);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001552 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1553 TII.get(X86::CVTSD2SSrr), ResultReg)
1554 .addReg(OpReg);
Dan Gohmanbf646f22008-09-10 21:02:08 +00001555 UpdateValueMap(I, ResultReg);
1556 return true;
1557 }
1558 }
1559 }
1560
1561 return false;
1562}
1563
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001564bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001565 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1566 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peck527da1b2010-11-23 03:31:01 +00001567
Eli Friedmanc7035512011-05-25 23:49:02 +00001568 // This code only handles truncation to byte.
Owen Anderson9f944592009-08-11 20:47:22 +00001569 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Chengb9286692008-09-07 08:47:42 +00001570 return false;
Eli Friedmanc7035512011-05-25 23:49:02 +00001571 if (!TLI.isTypeLegal(SrcVT))
Evan Chengb9286692008-09-07 08:47:42 +00001572 return false;
1573
1574 unsigned InputReg = getRegForValue(I->getOperand(0));
1575 if (!InputReg)
1576 // Unhandled operand. Halt "fast" selection and bail.
1577 return false;
1578
Eli Friedmanc7035512011-05-25 23:49:02 +00001579 if (SrcVT == MVT::i8) {
1580 // Truncate from i8 to i1; no code needed.
1581 UpdateValueMap(I, InputReg);
1582 return true;
1583 }
Evan Chengb9286692008-09-07 08:47:42 +00001584
Eli Friedmanc7035512011-05-25 23:49:02 +00001585 if (!Subtarget->is64Bit()) {
1586 // If we're on x86-32; we can't extract an i8 from a general register.
1587 // First issue a copy to GR16_ABCD or GR32_ABCD.
Craig Topperabadc662012-04-20 06:31:50 +00001588 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) ?
1589 (const TargetRegisterClass*)&X86::GR16_ABCDRegClass :
1590 (const TargetRegisterClass*)&X86::GR32_ABCDRegClass;
Eli Friedmanc7035512011-05-25 23:49:02 +00001591 unsigned CopyReg = createResultReg(CopyRC);
1592 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1593 CopyReg).addReg(InputReg);
1594 InputReg = CopyReg;
1595 }
1596
1597 // Issue an extract_subreg.
Owen Anderson9f944592009-08-11 20:47:22 +00001598 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedmanc7035512011-05-25 23:49:02 +00001599 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen9340ea52010-05-24 14:48:17 +00001600 X86::sub_8bit);
Evan Chengb9286692008-09-07 08:47:42 +00001601 if (!ResultReg)
1602 return false;
1603
1604 UpdateValueMap(I, ResultReg);
1605 return true;
1606}
1607
Eli Friedman60afcc22011-05-20 22:21:04 +00001608bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1609 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1610}
1611
Eli Friedmanbcc69142011-04-27 01:45:07 +00001612bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1613 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedman60afcc22011-05-20 22:21:04 +00001614
Eli Friedmanbcc69142011-04-27 01:45:07 +00001615 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedman60afcc22011-05-20 22:21:04 +00001616 if (!IsMemcpySmall(Len))
1617 return false;
1618
1619 bool i64Legal = Subtarget->is64Bit();
Eli Friedmanbcc69142011-04-27 01:45:07 +00001620
1621 // We don't care about alignment here since we just emit integer accesses.
1622 while (Len) {
1623 MVT VT;
1624 if (Len >= 8 && i64Legal)
1625 VT = MVT::i64;
1626 else if (Len >= 4)
1627 VT = MVT::i32;
1628 else if (Len >= 2)
1629 VT = MVT::i16;
1630 else {
Eli Friedmanbcc69142011-04-27 01:45:07 +00001631 VT = MVT::i8;
1632 }
1633
1634 unsigned Reg;
1635 bool RV = X86FastEmitLoad(VT, SrcAM, Reg);
1636 RV &= X86FastEmitStore(VT, Reg, DestAM);
1637 assert(RV && "Failed to emit load or store??");
1638
1639 unsigned Size = VT.getSizeInBits()/8;
1640 Len -= Size;
1641 DestAM.Disp += Size;
1642 SrcAM.Disp += Size;
1643 }
1644
1645 return true;
1646}
1647
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001648bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling80b34b32008-12-09 02:42:50 +00001649 // FIXME: Handle more intrinsics.
Chris Lattner99a8cb62009-04-12 07:36:01 +00001650 switch (I.getIntrinsicID()) {
Bill Wendling80b34b32008-12-09 02:42:50 +00001651 default: return false;
Chris Lattner91328b32011-04-19 05:52:03 +00001652 case Intrinsic::memcpy: {
1653 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1654 // Don't handle volatile or variable length memcpys.
Eli Friedmancd2124a2011-06-10 23:39:36 +00001655 if (MCI.isVolatile())
Chris Lattner91328b32011-04-19 05:52:03 +00001656 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00001657
Eli Friedmancd2124a2011-06-10 23:39:36 +00001658 if (isa<ConstantInt>(MCI.getLength())) {
1659 // Small memcpy's are common enough that we want to do them
1660 // without a call if possible.
1661 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1662 if (IsMemcpySmall(Len)) {
1663 X86AddressMode DestAM, SrcAM;
1664 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1665 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1666 return false;
1667 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1668 return true;
1669 }
1670 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001671
Eli Friedmancd2124a2011-06-10 23:39:36 +00001672 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1673 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner91328b32011-04-19 05:52:03 +00001674 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00001675
Eli Friedmancd2124a2011-06-10 23:39:36 +00001676 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1677 return false;
1678
1679 return DoSelectCall(&I, "memcpy");
Chris Lattner91328b32011-04-19 05:52:03 +00001680 }
Eli Friedmancd2124a2011-06-10 23:39:36 +00001681 case Intrinsic::memset: {
1682 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001683
Nick Lewyckya530a4d2011-08-02 00:40:16 +00001684 if (MSI.isVolatile())
1685 return false;
1686
Eli Friedmancd2124a2011-06-10 23:39:36 +00001687 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1688 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1689 return false;
1690
1691 if (MSI.getDestAddressSpace() > 255)
1692 return false;
1693
1694 return DoSelectCall(&I, "memset");
1695 }
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001696 case Intrinsic::stackprotector: {
Chad Rosier06e34d92012-05-11 19:43:29 +00001697 // Emit code to store the stack guard onto the stack.
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001698 EVT PtrTy = TLI.getPointerTy();
1699
Gabor Greif83205af2010-06-26 11:51:52 +00001700 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1701 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001702
1703 // Grab the frame index.
1704 X86AddressMode AM;
1705 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher5e95aee2010-03-18 21:58:33 +00001706 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001707 return true;
1708 }
Dale Johannesend5575f22010-01-26 00:09:58 +00001709 case Intrinsic::dbg_declare: {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001710 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesend5575f22010-01-26 00:09:58 +00001711 X86AddressMode AM;
Dale Johannesenad00f032010-01-29 21:21:28 +00001712 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesend5575f22010-01-26 00:09:58 +00001713 if (!X86SelectAddress(DI->getAddress(), AM))
1714 return false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00001715 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen654528e2010-02-18 18:51:15 +00001716 // FIXME may need to add RegState::Debug to any registers produced,
1717 // although ESP/EBP should be the only ones at the moment.
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001718 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1719 addImm(0).addMetadata(DI->getVariable());
Dale Johannesend5575f22010-01-26 00:09:58 +00001720 return true;
1721 }
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00001722 case Intrinsic::trap: {
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001723 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00001724 return true;
1725 }
Bill Wendling80b34b32008-12-09 02:42:50 +00001726 case Intrinsic::sadd_with_overflow:
1727 case Intrinsic::uadd_with_overflow: {
Chris Lattner91328b32011-04-19 05:52:03 +00001728 // FIXME: Should fold immediates.
Eric Christopher0713a9d2011-06-08 23:55:35 +00001729
Bill Wendlinge25d3412008-12-09 07:55:31 +00001730 // Replace "add with overflow" intrinsics with an "add" instruction followed
Eli Friedmana4d4a012011-05-16 21:06:17 +00001731 // by a seto/setc instruction.
Bill Wendling80b34b32008-12-09 02:42:50 +00001732 const Function *Callee = I.getCalledFunction();
Chris Lattner229907c2011-07-18 04:54:35 +00001733 Type *RetTy =
Bill Wendling80b34b32008-12-09 02:42:50 +00001734 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1735
Duncan Sandsf5dda012010-11-03 11:35:31 +00001736 MVT VT;
Bill Wendling80b34b32008-12-09 02:42:50 +00001737 if (!isTypeLegal(RetTy, VT))
1738 return false;
1739
Gabor Greif83205af2010-06-26 11:51:52 +00001740 const Value *Op1 = I.getArgOperand(0);
1741 const Value *Op2 = I.getArgOperand(1);
Bill Wendling80b34b32008-12-09 02:42:50 +00001742 unsigned Reg1 = getRegForValue(Op1);
1743 unsigned Reg2 = getRegForValue(Op2);
1744
1745 if (Reg1 == 0 || Reg2 == 0)
1746 // FIXME: Handle values *not* in registers.
1747 return false;
1748
1749 unsigned OpC = 0;
Owen Anderson9f944592009-08-11 20:47:22 +00001750 if (VT == MVT::i32)
Bill Wendling80b34b32008-12-09 02:42:50 +00001751 OpC = X86::ADD32rr;
Owen Anderson9f944592009-08-11 20:47:22 +00001752 else if (VT == MVT::i64)
Bill Wendling80b34b32008-12-09 02:42:50 +00001753 OpC = X86::ADD64rr;
1754 else
1755 return false;
1756
Eli Friedmana4d4a012011-05-16 21:06:17 +00001757 // The call to CreateRegs builds two sequential registers, to store the
Sylvestre Ledru35521e22012-07-23 08:51:15 +00001758 // both the returned values.
Eli Friedmana4d4a012011-05-16 21:06:17 +00001759 unsigned ResultReg = FuncInfo.CreateRegs(I.getType());
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001760 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1761 .addReg(Reg1).addReg(Reg2);
Wesley Peck527da1b2010-11-23 03:31:01 +00001762
Chris Lattner99a8cb62009-04-12 07:36:01 +00001763 unsigned Opc = X86::SETBr;
1764 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1765 Opc = X86::SETOr;
Eli Friedmana4d4a012011-05-16 21:06:17 +00001766 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg+1);
1767
1768 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling80b34b32008-12-09 02:42:50 +00001769 return true;
1770 }
1771 }
1772}
1773
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001774bool X86FastISel::FastLowerArguments() {
1775 if (!FuncInfo.CanLowerReturn)
1776 return false;
1777
1778 const Function *F = FuncInfo.Fn;
1779 if (F->isVarArg())
1780 return false;
1781
1782 CallingConv::ID CC = F->getCallingConv();
1783 if (CC != CallingConv::C)
1784 return false;
Charles Davise8f297c2013-07-12 06:02:35 +00001785
1786 if (Subtarget->isCallingConvWin64(CC))
1787 return false;
1788
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001789 if (!Subtarget->is64Bit())
1790 return false;
1791
1792 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
1793 unsigned Idx = 1;
1794 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1795 I != E; ++I, ++Idx) {
1796 if (Idx > 6)
1797 return false;
1798
1799 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
1800 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
1801 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
1802 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
1803 return false;
1804
1805 Type *ArgTy = I->getType();
1806 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
1807 return false;
1808
1809 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosier1b33e8d2013-02-26 01:05:31 +00001810 if (!ArgVT.isSimple()) return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001811 switch (ArgVT.getSimpleVT().SimpleTy) {
1812 case MVT::i32:
1813 case MVT::i64:
1814 break;
1815 default:
1816 return false;
1817 }
1818 }
1819
1820 static const uint16_t GPR32ArgRegs[] = {
1821 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
1822 };
1823 static const uint16_t GPR64ArgRegs[] = {
1824 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
1825 };
1826
1827 Idx = 0;
1828 const TargetRegisterClass *RC32 = TLI.getRegClassFor(MVT::i32);
1829 const TargetRegisterClass *RC64 = TLI.getRegClassFor(MVT::i64);
1830 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1831 I != E; ++I, ++Idx) {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001832 bool is32Bit = TLI.getValueType(I->getType()) == MVT::i32;
1833 const TargetRegisterClass *RC = is32Bit ? RC32 : RC64;
1834 unsigned SrcReg = is32Bit ? GPR32ArgRegs[Idx] : GPR64ArgRegs[Idx];
1835 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
1836 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1837 // Without this, EmitLiveInCopies may eliminate the livein if its only
1838 // use is a bitcast (which isn't turned into an instruction).
1839 unsigned ResultReg = createResultReg(RC);
1840 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1841 ResultReg).addReg(DstReg, getKillRegState(true));
1842 UpdateValueMap(I, ResultReg);
1843 }
1844 return true;
1845}
1846
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001847bool X86FastISel::X86SelectCall(const Instruction *I) {
1848 const CallInst *CI = cast<CallInst>(I);
Gabor Greif83205af2010-06-26 11:51:52 +00001849 const Value *Callee = CI->getCalledValue();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001850
1851 // Can't handle inline asm yet.
1852 if (isa<InlineAsm>(Callee))
1853 return false;
1854
Bill Wendling80b34b32008-12-09 02:42:50 +00001855 // Handle intrinsic calls.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001856 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattner99a8cb62009-04-12 07:36:01 +00001857 return X86VisitIntrinsicCall(*II);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001858
Chad Rosierdf42cf32012-12-11 00:18:02 +00001859 // Allow SelectionDAG isel to handle tail calls.
1860 if (cast<CallInst>(I)->isTailCall())
1861 return false;
1862
Eli Friedmancd2124a2011-06-10 23:39:36 +00001863 return DoSelectCall(I, 0);
1864}
1865
Rafael Espindola73173c52012-07-25 15:42:45 +00001866static unsigned computeBytesPoppedByCallee(const X86Subtarget &Subtarget,
1867 const ImmutableCallSite &CS) {
Rafael Espindola2caee7f2012-07-25 13:35:45 +00001868 if (Subtarget.is64Bit())
1869 return 0;
1870 if (Subtarget.isTargetWindows())
1871 return 0;
1872 CallingConv::ID CC = CS.getCallingConv();
1873 if (CC == CallingConv::Fast || CC == CallingConv::GHC)
1874 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001875 if (!CS.paramHasAttr(1, Attribute::StructRet))
Rafael Espindola2caee7f2012-07-25 13:35:45 +00001876 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001877 if (CS.paramHasAttr(1, Attribute::InReg))
Rafael Espindola11c38b92012-07-25 13:41:10 +00001878 return 0;
Rafael Espindola2caee7f2012-07-25 13:35:45 +00001879 return 4;
1880}
1881
Eli Friedmancd2124a2011-06-10 23:39:36 +00001882// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
1883bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
1884 const CallInst *CI = cast<CallInst>(I);
1885 const Value *Callee = CI->getCalledValue();
1886
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001887 // Handle only C and fastcc calling conventions for now.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001888 ImmutableCallSite CS(CI);
Sandeep Patel68c5f472009-09-02 08:44:58 +00001889 CallingConv::ID CC = CS.getCallingConv();
Charles Davise8f297c2013-07-12 06:02:35 +00001890 bool isWin64 = Subtarget->isCallingConvWin64(CC);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00001891 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +00001892 CC != CallingConv::X86_FastCall && CC != CallingConv::X86_64_Win64 &&
1893 CC != CallingConv::X86_64_SysV)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001894 return false;
1895
Evan Chengd10089a2010-01-27 00:00:57 +00001896 // fastcc with -tailcallopt is intended to provide a guaranteed
1897 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001898 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Evan Chengd10089a2010-01-27 00:00:57 +00001899 return false;
1900
Chris Lattner229907c2011-07-18 04:54:35 +00001901 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1902 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedmanee92a6b2011-04-19 17:22:22 +00001903 bool isVarArg = FTy->isVarArg();
1904
1905 // Don't know how to handle Win64 varargs yet. Nothing special needed for
1906 // x86-32. Special handling for x86-64 is implemented.
Charles Davise8f297c2013-07-12 06:02:35 +00001907 if (isVarArg && isWin64)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001908 return false;
1909
Dan Gohmandc53f1c2010-05-27 18:43:40 +00001910 // Fast-isel doesn't know about callee-pop yet.
Evan Cheng3a0c5e52011-06-23 17:54:54 +00001911 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001912 TM.Options.GuaranteedTailCallOpt))
Dan Gohmandc53f1c2010-05-27 18:43:40 +00001913 return false;
1914
Eli Friedman7b279422011-05-17 18:29:03 +00001915 // Check whether the function can return without sret-demotion.
1916 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +00001917 GetReturnInfo(I->getType(), CS.getAttributes(), Outs, TLI);
Eli Friedman7b279422011-05-17 18:29:03 +00001918 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Bill Wendlingea6397f2012-07-19 00:11:40 +00001919 *FuncInfo.MF, FTy->isVarArg(),
1920 Outs, FTy->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00001921 if (!CanLowerReturn)
Eli Friedman7335e8a2011-05-17 02:36:59 +00001922 return false;
1923
Dan Gohmanaf13bf12008-09-17 21:18:49 +00001924 // Materialize callee address in a register. FIXME: GV address can be
1925 // handled with a CALLpcrel32 instead.
Dan Gohman9801ba42008-09-19 22:16:54 +00001926 X86AddressMode CalleeAM;
Chris Lattner8212d372009-07-10 05:33:42 +00001927 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman9801ba42008-09-19 22:16:54 +00001928 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00001929 unsigned CalleeOp = 0;
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001930 const GlobalValue *GV = 0;
Chris Lattnerd17366a2009-06-27 04:50:14 +00001931 if (CalleeAM.GV != 0) {
Dan Gohman9801ba42008-09-19 22:16:54 +00001932 GV = CalleeAM.GV;
Chris Lattnerd17366a2009-06-27 04:50:14 +00001933 } else if (CalleeAM.Base.Reg != 0) {
1934 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman9801ba42008-09-19 22:16:54 +00001935 } else
1936 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00001937
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001938 // Deal with call operands first.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001939 SmallVector<const Value *, 8> ArgVals;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00001940 SmallVector<unsigned, 8> Args;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001941 SmallVector<MVT, 8> ArgVTs;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00001942 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosierf0687632012-02-15 00:36:26 +00001943 unsigned arg_size = CS.arg_size();
1944 Args.reserve(arg_size);
1945 ArgVals.reserve(arg_size);
1946 ArgVTs.reserve(arg_size);
1947 ArgFlags.reserve(arg_size);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001948 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001949 i != e; ++i) {
Eli Friedmancd2124a2011-06-10 23:39:36 +00001950 // If we're lowering a mem intrinsic instead of a regular call, skip the
1951 // last two arguments, which should not passed to the underlying functions.
1952 if (MemIntName && e-i <= 2)
1953 break;
Chris Lattnerd7f7c932011-04-19 04:42:38 +00001954 Value *ArgVal = *i;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001955 ISD::ArgFlagsTy Flags;
1956 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001957 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001958 Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001959 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001960 Flags.setZExt();
1961
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001962 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattner229907c2011-07-18 04:54:35 +00001963 PointerType *Ty = cast<PointerType>(ArgVal->getType());
1964 Type *ElementTy = Ty->getElementType();
Eli Friedman60afcc22011-05-20 22:21:04 +00001965 unsigned FrameSize = TD.getTypeAllocSize(ElementTy);
1966 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
1967 if (!FrameAlign)
1968 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
1969 Flags.setByVal();
1970 Flags.setByValSize(FrameSize);
1971 Flags.setByValAlign(FrameAlign);
1972 if (!IsMemcpySmall(FrameSize))
1973 return false;
1974 }
1975
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001976 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
Eli Friedman60afcc22011-05-20 22:21:04 +00001977 Flags.setInReg();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001978 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
Eli Friedman60afcc22011-05-20 22:21:04 +00001979 Flags.setNest();
1980
Chris Lattnerd7f7c932011-04-19 04:42:38 +00001981 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1982 // instruction. This is safe because it is common to all fastisel supported
1983 // calling conventions on x86.
1984 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1985 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1986 CI->getBitWidth() == 16) {
1987 if (Flags.isSExt())
1988 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1989 else
1990 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1991 }
1992 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001993
Chris Lattner5f4b7832011-04-19 05:09:50 +00001994 unsigned ArgReg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00001995
Chris Lattner34a08c22011-04-19 05:15:59 +00001996 // Passing bools around ends up doing a trunc to i1 and passing it.
1997 // Codegen this as an argument + "and 1".
Chris Lattner5f4b7832011-04-19 05:09:50 +00001998 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
1999 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
2000 ArgVal->hasOneUse()) {
Chris Lattner5f4b7832011-04-19 05:09:50 +00002001 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
2002 ArgReg = getRegForValue(ArgVal);
2003 if (ArgReg == 0) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002004
Chris Lattner5f4b7832011-04-19 05:09:50 +00002005 MVT ArgVT;
2006 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002007
Chris Lattner5f4b7832011-04-19 05:09:50 +00002008 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
2009 ArgVal->hasOneUse(), 1);
2010 } else {
2011 ArgReg = getRegForValue(ArgVal);
Chris Lattner5f4b7832011-04-19 05:09:50 +00002012 }
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002013
Chris Lattner34a08c22011-04-19 05:15:59 +00002014 if (ArgReg == 0) return false;
2015
Chris Lattner229907c2011-07-18 04:54:35 +00002016 Type *ArgTy = ArgVal->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002017 MVT ArgVT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00002018 if (!isTypeLegal(ArgTy, ArgVT))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002019 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002020 if (ArgVT == MVT::x86mmx)
2021 return false;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002022 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2023 Flags.setOrigAlign(OriginalAlignment);
2024
Chris Lattner5f4b7832011-04-19 05:09:50 +00002025 Args.push_back(ArgReg);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002026 ArgVals.push_back(ArgVal);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002027 ArgVTs.push_back(ArgVT);
2028 ArgFlags.push_back(Flags);
2029 }
2030
2031 // Analyze operands of the call, assigning locations to each operand.
2032 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002033 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00002034 I->getParent()->getContext());
Wesley Peck527da1b2010-11-23 03:31:01 +00002035
Dan Gohman47a07242010-06-01 21:09:47 +00002036 // Allocate shadow area for Win64
Charles Davise8f297c2013-07-12 06:02:35 +00002037 if (isWin64)
Wesley Peck527da1b2010-11-23 03:31:01 +00002038 CCInfo.AllocateStack(32, 8);
Dan Gohman47a07242010-06-01 21:09:47 +00002039
Duncan Sandsfb0a48e2010-10-31 13:21:44 +00002040 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002041
2042 // Get a count of how many bytes are to be pushed on the stack.
2043 unsigned NumBytes = CCInfo.getNextStackOffset();
2044
2045 // Issue CALLSEQ_START
Evan Cheng194c3dc2011-06-28 21:14:33 +00002046 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002047 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
2048 .addImm(NumBytes);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002049
Chris Lattner3ba29352008-10-15 05:30:52 +00002050 // Process argument: walk the register/memloc assignments, inserting
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002051 // copies / loads.
2052 SmallVector<unsigned, 4> RegArgs;
2053 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2054 CCValAssign &VA = ArgLocs[i];
2055 unsigned Arg = Args[VA.getValNo()];
Owen Anderson53aa7a92009-08-10 22:56:29 +00002056 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002057
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002058 // Promote the value if needed.
2059 switch (VA.getLocInfo()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002060 case CCValAssign::Full: break;
Evan Cheng6500d172008-09-08 06:35:17 +00002061 case CCValAssign::SExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002062 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2063 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002064 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2065 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002066 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002067 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002068 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002069 }
2070 case CCValAssign::ZExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002071 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2072 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002073 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
2074 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002075 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002076 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002077 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002078 }
2079 case CCValAssign::AExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002080 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2081 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002082 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
2083 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002084 if (!Emitted)
2085 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattnera0f9d492008-10-15 05:07:36 +00002086 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002087 if (!Emitted)
2088 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2089 Arg, ArgVT, Arg);
Wesley Peck527da1b2010-11-23 03:31:01 +00002090
Chris Lattner2d7df022011-01-05 22:26:52 +00002091 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002092 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002093 break;
2094 }
Dan Gohman8c795692009-08-05 05:33:42 +00002095 case CCValAssign::BCvt: {
Duncan Sandsf5dda012010-11-03 11:35:31 +00002096 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peck527da1b2010-11-23 03:31:01 +00002097 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohman8c795692009-08-05 05:33:42 +00002098 assert(BC != 0 && "Failed to emit a bitcast!");
2099 Arg = BC;
2100 ArgVT = VA.getLocVT();
2101 break;
2102 }
Chad Rosier8446ede2012-07-11 19:58:38 +00002103 case CCValAssign::VExt:
2104 // VExt has not been implemented, so this should be impossible to reach
2105 // for now. However, fallback to Selection DAG isel once implemented.
2106 return false;
2107 case CCValAssign::Indirect:
2108 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
2109 // support this.
2110 return false;
Evan Cheng6500d172008-09-08 06:35:17 +00002111 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002112
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002113 if (VA.isRegLoc()) {
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00002114 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2115 VA.getLocReg()).addReg(Arg);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002116 RegArgs.push_back(VA.getLocReg());
2117 } else {
2118 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman39d82f92008-09-10 20:11:02 +00002119 X86AddressMode AM;
Bill Wendling8f268402013-06-07 21:00:34 +00002120 const X86RegisterInfo *RegInfo = static_cast<const X86RegisterInfo*>(
2121 getTargetMachine()->getRegisterInfo());
Michael Liao70a99c82012-11-01 03:47:50 +00002122 AM.Base.Reg = RegInfo->getStackRegister();
Dan Gohman39d82f92008-09-10 20:11:02 +00002123 AM.Disp = LocMemOffset;
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002124 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedman60afcc22011-05-20 22:21:04 +00002125 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002126
Eli Friedman60afcc22011-05-20 22:21:04 +00002127 if (Flags.isByVal()) {
2128 X86AddressMode SrcAM;
2129 SrcAM.Base.Reg = Arg;
2130 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
2131 assert(Res && "memcpy length already checked!"); (void)Res;
2132 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
2133 // If this is a really simple value, emit this with the Value* version
Nick Lewycky064c1c02011-10-12 00:14:12 +00002134 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
Eli Friedman60afcc22011-05-20 22:21:04 +00002135 // as it can cause us to reevaluate the argument.
Lang Hames7d2f7b52011-10-18 22:11:33 +00002136 if (!X86FastEmitStore(ArgVT, ArgVal, AM))
2137 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002138 } else {
Lang Hames7d2f7b52011-10-18 22:11:33 +00002139 if (!X86FastEmitStore(ArgVT, Arg, AM))
2140 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002141 }
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002142 }
2143 }
2144
Dan Gohman3691d502008-09-25 15:24:26 +00002145 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peck527da1b2010-11-23 03:31:01 +00002146 // GOT pointer.
Chris Lattnerfef11d62009-07-09 04:39:06 +00002147 if (Subtarget->isPICStyleGOT()) {
Dan Gohman87fb4e82010-07-07 16:29:44 +00002148 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00002149 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2150 X86::EBX).addReg(Base);
Dan Gohman3691d502008-09-25 15:24:26 +00002151 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002152
Charles Davise8f297c2013-07-12 06:02:35 +00002153 if (Subtarget->is64Bit() && isVarArg && !isWin64) {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002154 // Count the number of XMM registers allocated.
Craig Topperbef78fc2012-03-11 07:57:25 +00002155 static const uint16_t XMMArgRegs[] = {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002156 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2157 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2158 };
2159 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
2160 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::MOV8ri),
2161 X86::AL).addImm(NumXMMRegs);
2162 }
2163
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002164 // Issue the call.
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002165 MachineInstrBuilder MIB;
2166 if (CalleeOp) {
2167 // Register-indirect call.
Nate Begeman68a069a2010-07-22 00:09:39 +00002168 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002169 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002170 CallOpc = X86::CALL64r;
2171 else
2172 CallOpc = X86::CALL32r;
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002173 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
2174 .addReg(CalleeOp);
Wesley Peck527da1b2010-11-23 03:31:01 +00002175
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002176 } else {
2177 // Direct call.
2178 assert(GV && "Not a direct call");
Nate Begeman68a069a2010-07-22 00:09:39 +00002179 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002180 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002181 CallOpc = X86::CALL64pcrel32;
2182 else
2183 CallOpc = X86::CALLpcrel32;
Wesley Peck527da1b2010-11-23 03:31:01 +00002184
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002185 // See if we need any target-specific flags on the GV operand.
2186 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00002187
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002188 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2189 // external symbols most go through the PLT in PIC mode. If the symbol
2190 // has hidden or protected visibility, or if it is static or local, then
2191 // we don't need to use the PLT - we can directly call it.
2192 if (Subtarget->isTargetELF() &&
2193 TM.getRelocationModel() == Reloc::PIC_ &&
2194 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
2195 OpFlags = X86II::MO_PLT;
Chris Lattnere2f524f2009-07-10 20:47:30 +00002196 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002197 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00002198 (!Subtarget->getTargetTriple().isMacOSX() ||
2199 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002200 // PC-relative references to external symbols should go through $stub,
2201 // unless we're building with the leopard linker or later, which
2202 // automatically synthesizes these stubs.
2203 OpFlags = X86II::MO_DARWIN_STUB;
2204 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002205
2206
Eli Friedmancd2124a2011-06-10 23:39:36 +00002207 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc));
2208 if (MemIntName)
Eli Friedman1735b292011-06-11 01:55:07 +00002209 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedmancd2124a2011-06-10 23:39:36 +00002210 else
2211 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002212 }
Dan Gohman3691d502008-09-25 15:24:26 +00002213
Jakob Stoklund Olesen8a450cb2012-02-16 00:02:50 +00002214 // Add a register mask with the call-preserved registers.
2215 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2216 MIB.addRegMask(TRI.getCallPreservedMask(CS.getCallingConv()));
2217
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00002218 // Add an implicit use GOT pointer in EBX.
2219 if (Subtarget->isPICStyleGOT())
2220 MIB.addReg(X86::EBX, RegState::Implicit);
2221
Charles Davise8f297c2013-07-12 06:02:35 +00002222 if (Subtarget->is64Bit() && isVarArg && !isWin64)
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00002223 MIB.addReg(X86::AL, RegState::Implicit);
2224
2225 // Add implicit physical register uses to the call.
2226 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2227 MIB.addReg(RegArgs[i], RegState::Implicit);
2228
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002229 // Issue CALLSEQ_END
Evan Cheng194c3dc2011-06-28 21:14:33 +00002230 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindola73173c52012-07-25 15:42:45 +00002231 const unsigned NumBytesCallee = computeBytesPoppedByCallee(*Subtarget, CS);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002232 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
Eli Friedman7cd51012011-04-28 20:19:12 +00002233 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002234
Eli Friedman7b279422011-05-17 18:29:03 +00002235 // Build info for return calling conv lowering code.
2236 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
2237 SmallVector<ISD::InputArg, 32> Ins;
2238 SmallVector<EVT, 4> RetTys;
2239 ComputeValueVTs(TLI, I->getType(), RetTys);
2240 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
2241 EVT VT = RetTys[i];
Patrik Hagglundbad545c2012-12-19 11:48:16 +00002242 MVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
Eli Friedman7b279422011-05-17 18:29:03 +00002243 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
2244 for (unsigned j = 0; j != NumRegs; ++j) {
2245 ISD::InputArg MyFlags;
Patrik Hagglundbad545c2012-12-19 11:48:16 +00002246 MyFlags.VT = RegisterVT;
Eli Friedman7b279422011-05-17 18:29:03 +00002247 MyFlags.Used = !CS.getInstruction()->use_empty();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002248 if (CS.paramHasAttr(0, Attribute::SExt))
Eli Friedman7b279422011-05-17 18:29:03 +00002249 MyFlags.Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002250 if (CS.paramHasAttr(0, Attribute::ZExt))
Eli Friedman7b279422011-05-17 18:29:03 +00002251 MyFlags.Flags.setZExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002252 if (CS.paramHasAttr(0, Attribute::InReg))
Eli Friedman7b279422011-05-17 18:29:03 +00002253 MyFlags.Flags.setInReg();
2254 Ins.push_back(MyFlags);
2255 }
2256 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00002257
Eli Friedman7b279422011-05-17 18:29:03 +00002258 // Now handle call return values.
2259 SmallVector<unsigned, 4> UsedRegs;
2260 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002261 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00002262 I->getParent()->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00002263 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
2264 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
2265 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2266 EVT CopyVT = RVLocs[i].getValVT();
2267 unsigned CopyReg = ResultReg + i;
Wesley Peck527da1b2010-11-23 03:31:01 +00002268
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002269 // If this is a call to a function that returns an fp value on the x87 fp
2270 // stack, but where we prefer to use the value in xmm registers, copy it
2271 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman7b279422011-05-17 18:29:03 +00002272 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002273 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00002274 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002275 CopyVT = MVT::f80;
Craig Topperabadc662012-04-20 06:31:50 +00002276 CopyReg = createResultReg(&X86::RFP80RegClass);
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00002277 }
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002278 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::FpPOP_RETVAL),
2279 CopyReg);
2280 } else {
2281 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2282 CopyReg).addReg(RVLocs[i].getLocReg());
2283 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002284 }
2285
Eli Friedman7b279422011-05-17 18:29:03 +00002286 if (CopyVT != RVLocs[i].getValVT()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002287 // Round the F80 the right size, which also moves to the appropriate xmm
2288 // register. This is accomplished by storing the F80 value in memory and
2289 // then loading it back. Ewww...
Eli Friedman7b279422011-05-17 18:29:03 +00002290 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson9f944592009-08-11 20:47:22 +00002291 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002292 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene1fbe0542009-11-12 20:49:22 +00002293 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002294 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2295 TII.get(Opc)), FI)
Eli Friedman7b279422011-05-17 18:29:03 +00002296 .addReg(CopyReg);
Owen Anderson9f944592009-08-11 20:47:22 +00002297 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002298 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eli Friedman7b279422011-05-17 18:29:03 +00002299 TII.get(Opc), ResultReg + i), FI);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002300 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00002301 }
Eli Friedman83ba1502011-05-17 00:13:47 +00002302
Eli Friedman7b279422011-05-17 18:29:03 +00002303 if (RVLocs.size())
2304 UpdateValueMap(I, ResultReg, RVLocs.size());
2305
Dan Gohman86936502010-06-18 23:28:01 +00002306 // Set all unused physreg defs as dead.
2307 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2308
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002309 return true;
2310}
2311
2312
Dan Gohmand58f3e32008-08-28 23:21:34 +00002313bool
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002314X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohmand58f3e32008-08-28 23:21:34 +00002315 switch (I->getOpcode()) {
2316 default: break;
Evan Chenga41ee292008-09-03 06:44:39 +00002317 case Instruction::Load:
Dan Gohman7bda51f2008-09-03 23:12:08 +00002318 return X86SelectLoad(I);
Owen Andersonb8c7ba22008-09-04 16:48:33 +00002319 case Instruction::Store:
2320 return X86SelectStore(I);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002321 case Instruction::Ret:
2322 return X86SelectRet(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00002323 case Instruction::ICmp:
2324 case Instruction::FCmp:
2325 return X86SelectCmp(I);
Dan Gohmana5753b32008-09-05 01:06:14 +00002326 case Instruction::ZExt:
2327 return X86SelectZExt(I);
2328 case Instruction::Br:
2329 return X86SelectBranch(I);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002330 case Instruction::Call:
2331 return X86SelectCall(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00002332 case Instruction::LShr:
2333 case Instruction::AShr:
2334 case Instruction::Shl:
2335 return X86SelectShift(I);
Eli Bendersky24a36eb2013-04-17 20:10:13 +00002336 case Instruction::SDiv:
2337 case Instruction::UDiv:
2338 case Instruction::SRem:
2339 case Instruction::URem:
2340 return X86SelectDivRem(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00002341 case Instruction::Select:
2342 return X86SelectSelect(I);
Evan Chengb9286692008-09-07 08:47:42 +00002343 case Instruction::Trunc:
2344 return X86SelectTrunc(I);
Dan Gohmanbf646f22008-09-10 21:02:08 +00002345 case Instruction::FPExt:
2346 return X86SelectFPExt(I);
2347 case Instruction::FPTrunc:
2348 return X86SelectFPTrunc(I);
Dan Gohmana62e4ab2009-03-13 23:53:06 +00002349 case Instruction::IntToPtr: // Deliberate fall-through.
2350 case Instruction::PtrToInt: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002351 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
2352 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohmana62e4ab2009-03-13 23:53:06 +00002353 if (DstVT.bitsGT(SrcVT))
2354 return X86SelectZExt(I);
2355 if (DstVT.bitsLT(SrcVT))
2356 return X86SelectTrunc(I);
2357 unsigned Reg = getRegForValue(I->getOperand(0));
2358 if (Reg == 0) return false;
2359 UpdateValueMap(I, Reg);
2360 return true;
2361 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00002362 }
2363
2364 return false;
2365}
2366
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002367unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00002368 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00002369 if (!isTypeLegal(C->getType(), VT))
Michael Liao3c898062012-08-30 00:30:16 +00002370 return 0;
2371
2372 // Can't handle alternate code models yet.
2373 if (TM.getCodeModel() != CodeModel::Small)
2374 return 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00002375
Owen Anderson50288e32008-09-05 00:06:23 +00002376 // Get opcode and regclass of the output for the given load instruction.
2377 unsigned Opc = 0;
2378 const TargetRegisterClass *RC = NULL;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002379 switch (VT.SimpleTy) {
Michael Liao3c898062012-08-30 00:30:16 +00002380 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00002381 case MVT::i8:
Owen Anderson50288e32008-09-05 00:06:23 +00002382 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +00002383 RC = &X86::GR8RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002384 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002385 case MVT::i16:
Owen Anderson50288e32008-09-05 00:06:23 +00002386 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +00002387 RC = &X86::GR16RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002388 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002389 case MVT::i32:
Owen Anderson50288e32008-09-05 00:06:23 +00002390 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +00002391 RC = &X86::GR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002392 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002393 case MVT::i64:
Owen Anderson50288e32008-09-05 00:06:23 +00002394 // Must be in x86-64 mode.
2395 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +00002396 RC = &X86::GR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002397 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002398 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002399 if (X86ScalarSSEf32) {
2400 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +00002401 RC = &X86::FR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002402 } else {
2403 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +00002404 RC = &X86::RFP32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002405 }
2406 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002407 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002408 if (X86ScalarSSEf64) {
2409 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +00002410 RC = &X86::FR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002411 } else {
2412 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +00002413 RC = &X86::RFP64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002414 }
2415 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002416 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +00002417 // No f80 support yet.
Michael Liao3c898062012-08-30 00:30:16 +00002418 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00002419 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002420
Dan Gohman9801ba42008-09-19 22:16:54 +00002421 // Materialize addresses with LEA instructions.
Owen Anderson50288e32008-09-05 00:06:23 +00002422 if (isa<GlobalValue>(C)) {
Dan Gohman9801ba42008-09-19 22:16:54 +00002423 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00002424 if (X86SelectAddress(C, AM)) {
Chris Lattner48326602011-04-17 17:12:08 +00002425 // If the expression is just a basereg, then we're done, otherwise we need
2426 // to emit an LEA.
2427 if (AM.BaseType == X86AddressMode::RegBase &&
2428 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
2429 return AM.Base.Reg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002430
Chris Lattner48326602011-04-17 17:12:08 +00002431 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman9801ba42008-09-19 22:16:54 +00002432 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002433 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2434 TII.get(Opc), ResultReg), AM);
Owen Anderson50288e32008-09-05 00:06:23 +00002435 return ResultReg;
Dan Gohman9801ba42008-09-19 22:16:54 +00002436 }
Evan Chengf5bc7e52008-09-05 21:00:03 +00002437 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00002438 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002439
Owen Andersond41c7162008-09-06 01:11:01 +00002440 // MachineConstantPool wants an explicit alignment.
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002441 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00002442 if (Align == 0) {
2443 // Alignment of vector types. FIXME!
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00002444 Align = TD.getTypeAllocSize(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00002445 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002446
Dan Gohman8392f0c2008-09-30 01:21:32 +00002447 // x86-32 PIC requires a PIC base register for constant pools.
2448 unsigned PICBase = 0;
Chris Lattnera3260c02009-06-27 01:31:51 +00002449 unsigned char OpFlag = 0;
Chris Lattner21c29402009-07-10 21:00:45 +00002450 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattnerfef11d62009-07-09 04:39:06 +00002451 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohman87fb4e82010-07-07 16:29:44 +00002452 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00002453 } else if (Subtarget->isPICStyleGOT()) {
2454 OpFlag = X86II::MO_GOTOFF;
Dan Gohman87fb4e82010-07-07 16:29:44 +00002455 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00002456 } else if (Subtarget->isPICStyleRIPRel() &&
2457 TM.getCodeModel() == CodeModel::Small) {
2458 PICBase = X86::RIP;
Chris Lattnera3260c02009-06-27 01:31:51 +00002459 }
Dan Gohman8392f0c2008-09-30 01:21:32 +00002460
2461 // Create the load from the constant pool.
Dan Gohman39d82f92008-09-10 20:11:02 +00002462 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman9801ba42008-09-19 22:16:54 +00002463 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002464 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2465 TII.get(Opc), ResultReg),
Chris Lattnera3260c02009-06-27 01:31:51 +00002466 MCPOffset, PICBase, OpFlag);
Dan Gohman8392f0c2008-09-30 01:21:32 +00002467
Owen Anderson50288e32008-09-05 00:06:23 +00002468 return ResultReg;
2469}
2470
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002471unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002472 // Fail on dynamic allocas. At this point, getRegForValue has already
2473 // checked its CSE maps, so if we're here trying to handle a dynamic
2474 // alloca, we're not going to succeed. X86SelectAddress has a
2475 // check for dynamic allocas, because it's called directly from
2476 // various places, but TargetMaterializeAlloca also needs a check
2477 // in order to avoid recursion between getRegForValue,
2478 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohman87fb4e82010-07-07 16:29:44 +00002479 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002480 return 0;
2481
Dan Gohman39d82f92008-09-10 20:11:02 +00002482 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00002483 if (!X86SelectAddress(C, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +00002484 return 0;
2485 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
Craig Topper760b1342012-02-22 05:59:10 +00002486 const TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Dan Gohman39d82f92008-09-10 20:11:02 +00002487 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002488 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2489 TII.get(Opc), ResultReg), AM);
Dan Gohman39d82f92008-09-10 20:11:02 +00002490 return ResultReg;
2491}
2492
Eli Friedman406c4712011-04-27 22:41:55 +00002493unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2494 MVT VT;
2495 if (!isTypeLegal(CF->getType(), VT))
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002496 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00002497
2498 // Get opcode and regclass for the given zero.
2499 unsigned Opc = 0;
2500 const TargetRegisterClass *RC = NULL;
2501 switch (VT.SimpleTy) {
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002502 default: return 0;
Craig Topper490c45c2012-08-11 17:53:00 +00002503 case MVT::f32:
2504 if (X86ScalarSSEf32) {
2505 Opc = X86::FsFLD0SS;
2506 RC = &X86::FR32RegClass;
2507 } else {
2508 Opc = X86::LD_Fp032;
2509 RC = &X86::RFP32RegClass;
2510 }
2511 break;
2512 case MVT::f64:
2513 if (X86ScalarSSEf64) {
2514 Opc = X86::FsFLD0SD;
2515 RC = &X86::FR64RegClass;
2516 } else {
2517 Opc = X86::LD_Fp064;
2518 RC = &X86::RFP64RegClass;
2519 }
2520 break;
2521 case MVT::f80:
2522 // No f80 support yet.
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002523 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00002524 }
2525
2526 unsigned ResultReg = createResultReg(RC);
2527 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
2528 return ResultReg;
2529}
2530
2531
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002532bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2533 const LoadInst *LI) {
Chris Lattnereeba0c72010-09-05 02:18:34 +00002534 X86AddressMode AM;
2535 if (!X86SelectAddress(LI->getOperand(0), AM))
2536 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00002537
Craig Topper55406d92012-08-11 17:46:16 +00002538 const X86InstrInfo &XII = (const X86InstrInfo&)TII;
Wesley Peck527da1b2010-11-23 03:31:01 +00002539
Chris Lattnereeba0c72010-09-05 02:18:34 +00002540 unsigned Size = TD.getTypeAllocSize(LI->getType());
2541 unsigned Alignment = LI->getAlignment();
2542
2543 SmallVector<MachineOperand, 8> AddrOps;
2544 AM.getFullAddress(AddrOps);
Wesley Peck527da1b2010-11-23 03:31:01 +00002545
Chris Lattnereeba0c72010-09-05 02:18:34 +00002546 MachineInstr *Result =
2547 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
2548 if (Result == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00002549
Chris Lattner2d186572011-01-16 02:27:38 +00002550 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnereeba0c72010-09-05 02:18:34 +00002551 MI->eraseFromParent();
2552 return true;
2553}
2554
2555
Evan Cheng24422d42008-09-03 00:03:49 +00002556namespace llvm {
Bob Wilson3e6fa462012-08-03 04:06:28 +00002557 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
2558 const TargetLibraryInfo *libInfo) {
2559 return new X86FastISel(funcInfo, libInfo);
Evan Cheng24422d42008-09-03 00:03:49 +00002560 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00002561}