blob: 6bbc9840906314bfab99889ec55757acfc0878b1 [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);
636 bool InMBB = true;
637 if (I) {
Chris Lattner8212d372009-07-10 05:33:42 +0000638 Opcode = I->getOpcode();
639 U = I;
Quentin Colombet778dba12013-10-14 22:32:09 +0000640 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000641 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000642 Opcode = C->getOpcode();
643 U = C;
644 }
645
646 switch (Opcode) {
647 default: break;
648 case Instruction::BitCast:
Quentin Colombet778dba12013-10-14 22:32:09 +0000649 // Look past bitcasts if its operand is in the same BB.
650 if (InMBB)
651 return X86SelectCallAddress(U->getOperand(0), AM);
652 break;
Chris Lattner8212d372009-07-10 05:33:42 +0000653
654 case Instruction::IntToPtr:
Quentin Colombet778dba12013-10-14 22:32:09 +0000655 // Look past no-op inttoptrs if its operand is in the same BB.
656 if (InMBB &&
657 TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000658 return X86SelectCallAddress(U->getOperand(0), AM);
659 break;
660
661 case Instruction::PtrToInt:
Quentin Colombet778dba12013-10-14 22:32:09 +0000662 // Look past no-op ptrtoints if its operand is in the same BB.
663 if (InMBB &&
664 TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000665 return X86SelectCallAddress(U->getOperand(0), AM);
666 break;
667 }
668
669 // Handle constant address.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000670 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000671 // Can't handle alternate code models yet.
Chris Lattner25e7f912009-07-10 21:03:06 +0000672 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner8212d372009-07-10 05:33:42 +0000673 return false;
674
675 // RIP-relative addresses can't have additional register operands.
676 if (Subtarget->isPICStyleRIPRel() &&
677 (AM.Base.Reg != 0 || AM.IndexReg != 0))
678 return false;
679
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000680 // Can't handle DLLImport.
681 if (GV->hasDLLImportLinkage())
682 return false;
683
684 // Can't handle TLS.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000685 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000686 if (GVar->isThreadLocal())
Chris Lattner8212d372009-07-10 05:33:42 +0000687 return false;
688
689 // Okay, we've committed to selecting this global. Set up the basic address.
690 AM.GV = GV;
Wesley Peck527da1b2010-11-23 03:31:01 +0000691
Chris Lattner7277a802009-07-10 05:45:15 +0000692 // No ABI requires an extra load for anything other than DLLImport, which
693 // we rejected above. Return a direct reference to the global.
Chris Lattner7277a802009-07-10 05:45:15 +0000694 if (Subtarget->isPICStyleRIPRel()) {
695 // Use rip-relative addressing if we can. Above we verified that the
696 // base and index registers are unused.
697 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
698 AM.Base.Reg = X86::RIP;
Chris Lattner21c29402009-07-10 21:00:45 +0000699 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattner7277a802009-07-10 05:45:15 +0000700 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
701 } else if (Subtarget->isPICStyleGOT()) {
702 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner8212d372009-07-10 05:33:42 +0000703 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000704
Chris Lattner8212d372009-07-10 05:33:42 +0000705 return true;
706 }
707
708 // If all else fails, try to materialize the value in a register.
709 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
710 if (AM.Base.Reg == 0) {
711 AM.Base.Reg = getRegForValue(V);
712 return AM.Base.Reg != 0;
713 }
714 if (AM.IndexReg == 0) {
715 assert(AM.Scale == 1 && "Scale with no index!");
716 AM.IndexReg = getRegForValue(V);
717 return AM.IndexReg != 0;
718 }
719 }
720
721 return false;
722}
723
724
Owen Anderson4f948bd2008-09-04 07:08:58 +0000725/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000726bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000727 // Atomic stores need special handling.
Lang Hames7d2f7b52011-10-18 22:11:33 +0000728 const StoreInst *S = cast<StoreInst>(I);
729
730 if (S->isAtomic())
731 return false;
732
Craig Topper4f55b0e2013-07-17 05:57:45 +0000733 unsigned SABIAlignment =
734 TD.getABITypeAlignment(S->getValueOperand()->getType());
735 bool Aligned = S->getAlignment() == 0 || S->getAlignment() >= SABIAlignment;
736
Duncan Sandsf5dda012010-11-03 11:35:31 +0000737 MVT VT;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000738 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Anderson4f948bd2008-09-04 07:08:58 +0000739 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000740
Dan Gohman39d82f92008-09-10 20:11:02 +0000741 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +0000742 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman39d82f92008-09-10 20:11:02 +0000743 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000744
Craig Topper4f55b0e2013-07-17 05:57:45 +0000745 return X86FastEmitStore(VT, I->getOperand(0), AM, Aligned);
Owen Anderson4f948bd2008-09-04 07:08:58 +0000746}
747
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000748/// X86SelectRet - Select and emit code to implement ret instructions.
749bool X86FastISel::X86SelectRet(const Instruction *I) {
750 const ReturnInst *Ret = cast<ReturnInst>(I);
751 const Function &F = *I->getParent()->getParent();
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000752 const X86MachineFunctionInfo *X86MFInfo =
753 FuncInfo.MF->getInfo<X86MachineFunctionInfo>();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000754
755 if (!FuncInfo.CanLowerReturn)
756 return false;
757
758 CallingConv::ID CC = F.getCallingConv();
759 if (CC != CallingConv::C &&
760 CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +0000761 CC != CallingConv::X86_FastCall &&
762 CC != CallingConv::X86_64_SysV)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000763 return false;
764
Charles Davise8f297c2013-07-12 06:02:35 +0000765 if (Subtarget->isCallingConvWin64(CC))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000766 return false;
767
768 // Don't handle popping bytes on return for now.
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000769 if (X86MFInfo->getBytesToPopOnReturn() != 0)
Jakub Staszak74010cd2013-02-17 18:35:25 +0000770 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000771
772 // fastcc with -tailcallopt is intended to provide a guaranteed
773 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000774 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000775 return false;
776
777 // Let SDISel handle vararg functions.
778 if (F.isVarArg())
779 return false;
780
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000781 // Build a list of return value registers.
782 SmallVector<unsigned, 4> RetRegs;
783
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000784 if (Ret->getNumOperands() > 0) {
785 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +0000786 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000787
788 // Analyze operands of the call, assigning locations to each operand.
789 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000790 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +0000791 I->getContext());
Duncan Sandsfa7e6f22010-10-31 13:02:38 +0000792 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000793
794 const Value *RV = Ret->getOperand(0);
795 unsigned Reg = getRegForValue(RV);
796 if (Reg == 0)
797 return false;
798
799 // Only handle a single return value for now.
800 if (ValLocs.size() != 1)
801 return false;
802
803 CCValAssign &VA = ValLocs[0];
Wesley Peck527da1b2010-11-23 03:31:01 +0000804
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000805 // Don't bother handling odd stuff for now.
806 if (VA.getLocInfo() != CCValAssign::Full)
807 return false;
808 // Only handle register returns for now.
809 if (!VA.isRegLoc())
810 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000811
812 // The calling-convention tables for x87 returns don't tell
813 // the whole story.
814 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
815 return false;
816
Eli Friedman6fc94dd2011-05-18 23:13:10 +0000817 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedman22da7992011-05-19 22:16:13 +0000818 EVT SrcVT = TLI.getValueType(RV->getType());
819 EVT DstVT = VA.getValVT();
820 // Special handling for extended integers.
821 if (SrcVT != DstVT) {
822 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
823 return false;
824
825 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
826 return false;
827
828 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
829
830 if (SrcVT == MVT::i1) {
831 if (Outs[0].Flags.isSExt())
832 return false;
833 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
834 SrcVT = MVT::i8;
835 }
836 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
837 ISD::SIGN_EXTEND;
838 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
839 SrcReg, /*TODO: Kill=*/false);
840 }
841
842 // Make the copy.
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000843 unsigned DstReg = VA.getLocReg();
844 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000845 // Avoid a cross-class copy. This is very unlikely.
846 if (!SrcRC->contains(DstReg))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000847 return false;
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000848 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
849 DstReg).addReg(SrcReg);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000850
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000851 // Add register to return instruction.
852 RetRegs.push_back(VA.getLocReg());
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000853 }
854
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000855 // The x86-64 ABI for returning structs by value requires that we copy
856 // the sret argument into %rax for the return. We saved the argument into
857 // a virtual register in the entry block, so now we copy the value out
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000858 // and into %rax. We also do the same with %eax for Win32.
859 if (F.hasStructRetAttr() &&
860 (Subtarget->is64Bit() || Subtarget->isTargetWindows())) {
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000861 unsigned Reg = X86MFInfo->getSRetReturnReg();
862 assert(Reg &&
863 "SRetReturnReg should have been set in LowerFormalArguments()!");
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000864 unsigned RetReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000865 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000866 RetReg).addReg(Reg);
867 RetRegs.push_back(RetReg);
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000868 }
869
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000870 // Now emit the RET.
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000871 MachineInstrBuilder MIB =
872 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
873 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
874 MIB.addReg(RetRegs[i], RegState::Implicit);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000875 return true;
876}
877
Evan Chenga41ee292008-09-03 06:44:39 +0000878/// X86SelectLoad - Select and emit code to implement load instructions.
879///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000880bool X86FastISel::X86SelectLoad(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000881 // Atomic loads need special handling.
882 if (cast<LoadInst>(I)->isAtomic())
883 return false;
884
Duncan Sandsf5dda012010-11-03 11:35:31 +0000885 MVT VT;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000886 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Chenga41ee292008-09-03 06:44:39 +0000887 return false;
888
Dan Gohman39d82f92008-09-10 20:11:02 +0000889 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +0000890 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman39d82f92008-09-10 20:11:02 +0000891 return false;
Evan Chenga41ee292008-09-03 06:44:39 +0000892
Evan Chengf5bc7e52008-09-05 21:00:03 +0000893 unsigned ResultReg = 0;
Dan Gohman39d82f92008-09-10 20:11:02 +0000894 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000895 UpdateValueMap(I, ResultReg);
896 return true;
Evan Chenga41ee292008-09-03 06:44:39 +0000897 }
Evan Chengf5bc7e52008-09-05 21:00:03 +0000898 return false;
Evan Chenga41ee292008-09-03 06:44:39 +0000899}
900
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +0000901static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000902 bool HasAVX = Subtarget->hasAVX();
Craig Topperb0c0f722012-01-10 06:54:16 +0000903 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
904 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000905
Owen Anderson9f944592009-08-11 20:47:22 +0000906 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner74e01282008-10-15 04:32:45 +0000907 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000908 case MVT::i8: return X86::CMP8rr;
909 case MVT::i16: return X86::CMP16rr;
910 case MVT::i32: return X86::CMP32rr;
911 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000912 case MVT::f32:
913 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
914 case MVT::f64:
915 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohman1ab1d312008-10-02 22:15:21 +0000916 }
Dan Gohman1ab1d312008-10-02 22:15:21 +0000917}
918
Chris Lattner88f47542008-10-15 04:13:29 +0000919/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
920/// of the comparison, return an opcode that works for the compare (e.g.
921/// CMP32ri) otherwise return 0.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000922static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson9f944592009-08-11 20:47:22 +0000923 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner88f47542008-10-15 04:13:29 +0000924 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner74e01282008-10-15 04:32:45 +0000925 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000926 case MVT::i8: return X86::CMP8ri;
927 case MVT::i16: return X86::CMP16ri;
928 case MVT::i32: return X86::CMP32ri;
929 case MVT::i64:
Chris Lattner74e01282008-10-15 04:32:45 +0000930 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
931 // field.
Chris Lattner3ba29352008-10-15 05:30:52 +0000932 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner74e01282008-10-15 04:32:45 +0000933 return X86::CMP64ri32;
934 return 0;
935 }
Chris Lattner88f47542008-10-15 04:13:29 +0000936}
937
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000938bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
939 EVT VT) {
Chris Lattnerd46b9512008-10-15 04:26:38 +0000940 unsigned Op0Reg = getRegForValue(Op0);
941 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000942
Chris Lattnere388725a2008-10-15 05:18:04 +0000943 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +0000944 if (isa<ConstantPointerNull>(Op1))
945 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +0000946
Chris Lattnerd46b9512008-10-15 04:26:38 +0000947 // We have two options: compare with register or immediate. If the RHS of
948 // the compare is an immediate that we can fold into this compare, use
949 // CMPri, otherwise use CMPrr.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000950 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner74e01282008-10-15 04:32:45 +0000951 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000952 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
953 .addReg(Op0Reg)
954 .addImm(Op1C->getSExtValue());
Chris Lattnerd46b9512008-10-15 04:26:38 +0000955 return true;
956 }
957 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000958
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +0000959 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattnerd46b9512008-10-15 04:26:38 +0000960 if (CompareOpc == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000961
Chris Lattnerd46b9512008-10-15 04:26:38 +0000962 unsigned Op1Reg = getRegForValue(Op1);
963 if (Op1Reg == 0) return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000964 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
965 .addReg(Op0Reg)
966 .addReg(Op1Reg);
Wesley Peck527da1b2010-11-23 03:31:01 +0000967
Chris Lattnerd46b9512008-10-15 04:26:38 +0000968 return true;
969}
970
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000971bool X86FastISel::X86SelectCmp(const Instruction *I) {
972 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000973
Duncan Sandsf5dda012010-11-03 11:35:31 +0000974 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +0000975 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman09faf812008-09-05 01:33:56 +0000976 return false;
977
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000978 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattnera3596db2008-10-15 03:47:17 +0000979 unsigned SetCCOpc;
Chris Lattnerf32ce222008-10-15 03:52:54 +0000980 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000981 switch (CI->getPredicate()) {
982 case CmpInst::FCMP_OEQ: {
Chris Lattnerdc1c3802008-10-15 04:29:23 +0000983 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
984 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000985
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000986 unsigned EReg = createResultReg(&X86::GR8RegClass);
987 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000988 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
989 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
990 TII.get(X86::SETNPr), NPReg);
Wesley Peck527da1b2010-11-23 03:31:01 +0000991 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen9bba9022009-02-13 02:33:27 +0000992 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattnera3596db2008-10-15 03:47:17 +0000993 UpdateValueMap(I, ResultReg);
994 return true;
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000995 }
996 case CmpInst::FCMP_UNE: {
Chris Lattnerdc1c3802008-10-15 04:29:23 +0000997 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
998 return false;
999
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001000 unsigned NEReg = createResultReg(&X86::GR8RegClass);
1001 unsigned PReg = createResultReg(&X86::GR8RegClass);
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001002 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETNEr), NEReg);
1003 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETPr), PReg);
1004 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::OR8rr),ResultReg)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001005 .addReg(PReg).addReg(NEReg);
Chris Lattnera3596db2008-10-15 03:47:17 +00001006 UpdateValueMap(I, ResultReg);
1007 return true;
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001008 }
Chris Lattnerf32ce222008-10-15 03:52:54 +00001009 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
1010 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
1011 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
1012 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
1013 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
1014 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
1015 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
1016 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
1017 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
1018 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
1019 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
1020 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peck527da1b2010-11-23 03:31:01 +00001021
Chris Lattnerf32ce222008-10-15 03:52:54 +00001022 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
1023 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
1024 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
1025 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
1026 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
1027 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
1028 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
1029 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
1030 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
1031 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001032 default:
1033 return false;
1034 }
1035
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001036 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattnerf32ce222008-10-15 03:52:54 +00001037 if (SwapArgs)
Chris Lattnerd46b9512008-10-15 04:26:38 +00001038 std::swap(Op0, Op1);
Chris Lattnerf32ce222008-10-15 03:52:54 +00001039
Chris Lattnerd46b9512008-10-15 04:26:38 +00001040 // Emit a compare of Op0/Op1.
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001041 if (!X86FastEmitCompare(Op0, Op1, VT))
1042 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001043
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001044 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001045 UpdateValueMap(I, ResultReg);
1046 return true;
1047}
Evan Chenga41ee292008-09-03 06:44:39 +00001048
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001049bool X86FastISel::X86SelectZExt(const Instruction *I) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001050 EVT DstVT = TLI.getValueType(I->getType());
1051 if (!TLI.isTypeLegal(DstVT))
1052 return false;
1053
1054 unsigned ResultReg = getRegForValue(I->getOperand(0));
1055 if (ResultReg == 0)
1056 return false;
1057
Tim Northover04eb4232013-05-30 10:43:18 +00001058 // Handle zero-extension from i1 to i8, which is common.
Craig Topper56710102013-08-15 02:33:50 +00001059 MVT SrcVT = TLI.getSimpleValueType(I->getOperand(0)->getType());
Tim Northover04eb4232013-05-30 10:43:18 +00001060 if (SrcVT.SimpleTy == MVT::i1) {
1061 // Set the high bits to zero.
1062 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1063 SrcVT = MVT::i8;
Eli Friedmanc7035512011-05-25 23:49:02 +00001064
Tim Northover04eb4232013-05-30 10:43:18 +00001065 if (ResultReg == 0)
1066 return false;
1067 }
1068
1069 if (DstVT == MVT::i64) {
1070 // Handle extension to 64-bits via sub-register shenanigans.
1071 unsigned MovInst;
1072
1073 switch (SrcVT.SimpleTy) {
1074 case MVT::i8: MovInst = X86::MOVZX32rr8; break;
1075 case MVT::i16: MovInst = X86::MOVZX32rr16; break;
1076 case MVT::i32: MovInst = X86::MOV32rr; break;
1077 default: llvm_unreachable("Unexpected zext to i64 source type");
1078 }
1079
1080 unsigned Result32 = createResultReg(&X86::GR32RegClass);
1081 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovInst), Result32)
1082 .addReg(ResultReg);
1083
1084 ResultReg = createResultReg(&X86::GR64RegClass);
1085 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::SUBREG_TO_REG),
1086 ResultReg)
1087 .addImm(0).addReg(Result32).addImm(X86::sub_32bit);
1088 } else if (DstVT != MVT::i8) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001089 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1090 ResultReg, /*Kill=*/true);
1091 if (ResultReg == 0)
1092 return false;
Dan Gohmana5753b32008-09-05 01:06:14 +00001093 }
1094
Eli Friedmanc7035512011-05-25 23:49:02 +00001095 UpdateValueMap(I, ResultReg);
1096 return true;
Dan Gohmana5753b32008-09-05 01:06:14 +00001097}
1098
Chris Lattnerd46b9512008-10-15 04:26:38 +00001099
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001100bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmana5753b32008-09-05 01:06:14 +00001101 // Unconditional branches are selected by tablegen-generated code.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001102 // Handle a conditional branch.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001103 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohman87fb4e82010-07-07 16:29:44 +00001104 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1105 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmana5753b32008-09-05 01:06:14 +00001106
Dan Gohman42ef6692010-08-21 02:32:36 +00001107 // Fold the common case of a conditional branch with a comparison
1108 // in the same block (values defined on other blocks may not have
1109 // initialized registers).
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001110 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman42ef6692010-08-21 02:32:36 +00001111 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001112 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmana5753b32008-09-05 01:06:14 +00001113
Dan Gohman1ab1d312008-10-02 22:15:21 +00001114 // Try to take advantage of fallthrough opportunities.
1115 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001116 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohman1ab1d312008-10-02 22:15:21 +00001117 std::swap(TrueMBB, FalseMBB);
1118 Predicate = CmpInst::getInversePredicate(Predicate);
1119 }
1120
Chris Lattner0ce717a2008-10-15 03:58:05 +00001121 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
1122 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
1123
Dan Gohman1ab1d312008-10-02 22:15:21 +00001124 switch (Predicate) {
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001125 case CmpInst::FCMP_OEQ:
1126 std::swap(TrueMBB, FalseMBB);
1127 Predicate = CmpInst::FCMP_UNE;
1128 // FALL THROUGH
Chris Lattner2b0a7a22010-02-11 19:25:55 +00001129 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1130 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1131 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1132 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
1133 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
1134 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1135 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
1136 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
1137 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1138 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
1139 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
1140 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1141 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peck527da1b2010-11-23 03:31:01 +00001142
Chris Lattner2b0a7a22010-02-11 19:25:55 +00001143 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1144 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1145 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1146 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1147 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1148 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1149 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
1150 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1151 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1152 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001153 default:
1154 return false;
1155 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001156
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001157 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner47bef252008-10-15 04:02:26 +00001158 if (SwapArgs)
1159 std::swap(Op0, Op1);
1160
Chris Lattnerd46b9512008-10-15 04:26:38 +00001161 // Emit a compare of the LHS and RHS, setting the flags.
1162 if (!X86FastEmitCompare(Op0, Op1, VT))
1163 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001164
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001165 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1166 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001167
1168 if (Predicate == CmpInst::FCMP_UNE) {
1169 // X86 requires a second branch to handle UNE (and OEQ,
1170 // which is mapped to UNE above).
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001171 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1172 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001173 }
1174
Stuart Hastings0125b642010-06-17 22:43:56 +00001175 FastEmitBranch(FalseMBB, DL);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001176 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohman1ab1d312008-10-02 22:15:21 +00001177 return true;
1178 }
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001179 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1180 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1181 // typically happen for _Bool and C++ bools.
1182 MVT SourceVT;
1183 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1184 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1185 unsigned TestOpc = 0;
1186 switch (SourceVT.SimpleTy) {
1187 default: break;
1188 case MVT::i8: TestOpc = X86::TEST8ri; break;
1189 case MVT::i16: TestOpc = X86::TEST16ri; break;
1190 case MVT::i32: TestOpc = X86::TEST32ri; break;
1191 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1192 }
1193 if (TestOpc) {
1194 unsigned OpReg = getRegForValue(TI->getOperand(0));
1195 if (OpReg == 0) return false;
1196 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TestOpc))
1197 .addReg(OpReg).addImm(1);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001198
Chris Lattnerc59290a2011-04-19 04:26:32 +00001199 unsigned JmpOpc = X86::JNE_4;
1200 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1201 std::swap(TrueMBB, FalseMBB);
1202 JmpOpc = X86::JE_4;
1203 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001204
Chris Lattnerc59290a2011-04-19 04:26:32 +00001205 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(JmpOpc))
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001206 .addMBB(TrueMBB);
1207 FastEmitBranch(FalseMBB, DL);
1208 FuncInfo.MBB->addSuccessor(TrueMBB);
1209 return true;
1210 }
1211 }
Dan Gohman1ab1d312008-10-02 22:15:21 +00001212 }
1213
1214 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman0eea0292011-04-27 01:34:27 +00001215 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1216 // in an explicit cast, so make sure to handle that correctly.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001217 unsigned OpReg = getRegForValue(BI->getCondition());
1218 if (OpReg == 0) return false;
1219
Eli Friedman0eea0292011-04-27 01:34:27 +00001220 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8ri))
1221 .addReg(OpReg).addImm(1);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001222 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1223 .addMBB(TrueMBB);
Stuart Hastings0125b642010-06-17 22:43:56 +00001224 FastEmitBranch(FalseMBB, DL);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001225 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmana5753b32008-09-05 01:06:14 +00001226 return true;
1227}
1228
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001229bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001230 unsigned CReg = 0, OpReg = 0;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001231 const TargetRegisterClass *RC = NULL;
Duncan Sands9dff9be2010-02-15 16:12:20 +00001232 if (I->getType()->isIntegerTy(8)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001233 CReg = X86::CL;
1234 RC = &X86::GR8RegClass;
1235 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001236 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1237 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1238 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001239 default: return false;
1240 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001241 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001242 CReg = X86::CX;
1243 RC = &X86::GR16RegClass;
1244 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001245 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1246 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1247 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001248 default: return false;
1249 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001250 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001251 CReg = X86::ECX;
1252 RC = &X86::GR32RegClass;
1253 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001254 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1255 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1256 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001257 default: return false;
1258 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001259 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001260 CReg = X86::RCX;
1261 RC = &X86::GR64RegClass;
1262 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001263 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1264 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1265 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001266 default: return false;
1267 }
1268 } else {
1269 return false;
1270 }
1271
Duncan Sandsf5dda012010-11-03 11:35:31 +00001272 MVT VT;
1273 if (!isTypeLegal(I->getType(), VT))
Dan Gohmandb06a992008-09-05 21:27:34 +00001274 return false;
1275
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001276 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1277 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001278
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001279 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1280 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00001281 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1282 CReg).addReg(Op1Reg);
Dan Gohmand3917152008-10-07 21:50:36 +00001283
1284 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001285 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohmand3917152008-10-07 21:50:36 +00001286 if (CReg != X86::CL)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001287 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1288 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001289 .addReg(CReg, RegState::Kill);
Dan Gohmand3917152008-10-07 21:50:36 +00001290
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001291 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001292 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1293 .addReg(Op0Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001294 UpdateValueMap(I, ResultReg);
1295 return true;
1296}
1297
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001298bool X86FastISel::X86SelectDivRem(const Instruction *I) {
1299 const static unsigned NumTypes = 4; // i8, i16, i32, i64
1300 const static unsigned NumOps = 4; // SDiv, SRem, UDiv, URem
1301 const static bool S = true; // IsSigned
1302 const static bool U = false; // !IsSigned
1303 const static unsigned Copy = TargetOpcode::COPY;
1304 // For the X86 DIV/IDIV instruction, in most cases the dividend
1305 // (numerator) must be in a specific register pair highreg:lowreg,
1306 // producing the quotient in lowreg and the remainder in highreg.
1307 // For most data types, to set up the instruction, the dividend is
1308 // copied into lowreg, and lowreg is sign-extended or zero-extended
1309 // into highreg. The exception is i8, where the dividend is defined
1310 // as a single register rather than a register pair, and we
1311 // therefore directly sign-extend or zero-extend the dividend into
1312 // lowreg, instead of copying, and ignore the highreg.
1313 const static struct DivRemEntry {
1314 // The following portion depends only on the data type.
1315 const TargetRegisterClass *RC;
1316 unsigned LowInReg; // low part of the register pair
1317 unsigned HighInReg; // high part of the register pair
1318 // The following portion depends on both the data type and the operation.
1319 struct DivRemResult {
1320 unsigned OpDivRem; // The specific DIV/IDIV opcode to use.
1321 unsigned OpSignExtend; // Opcode for sign-extending lowreg into
1322 // highreg, or copying a zero into highreg.
1323 unsigned OpCopy; // Opcode for copying dividend into lowreg, or
1324 // zero/sign-extending into lowreg for i8.
1325 unsigned DivRemResultReg; // Register containing the desired result.
1326 bool IsOpSigned; // Whether to use signed or unsigned form.
1327 } ResultTable[NumOps];
1328 } OpTable[NumTypes] = {
1329 { &X86::GR8RegClass, X86::AX, 0, {
1330 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AL, S }, // SDiv
1331 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AH, S }, // SRem
1332 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AL, U }, // UDiv
1333 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AH, U }, // URem
1334 }
1335 }, // i8
1336 { &X86::GR16RegClass, X86::AX, X86::DX, {
1337 { X86::IDIV16r, X86::CWD, Copy, X86::AX, S }, // SDiv
1338 { X86::IDIV16r, X86::CWD, Copy, X86::DX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001339 { X86::DIV16r, X86::MOV32r0, Copy, X86::AX, U }, // UDiv
1340 { X86::DIV16r, X86::MOV32r0, Copy, X86::DX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001341 }
1342 }, // i16
1343 { &X86::GR32RegClass, X86::EAX, X86::EDX, {
1344 { X86::IDIV32r, X86::CDQ, Copy, X86::EAX, S }, // SDiv
1345 { X86::IDIV32r, X86::CDQ, Copy, X86::EDX, S }, // SRem
1346 { X86::DIV32r, X86::MOV32r0, Copy, X86::EAX, U }, // UDiv
1347 { X86::DIV32r, X86::MOV32r0, Copy, X86::EDX, U }, // URem
1348 }
1349 }, // i32
1350 { &X86::GR64RegClass, X86::RAX, X86::RDX, {
1351 { X86::IDIV64r, X86::CQO, Copy, X86::RAX, S }, // SDiv
1352 { X86::IDIV64r, X86::CQO, Copy, X86::RDX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001353 { X86::DIV64r, X86::MOV32r0, Copy, X86::RAX, U }, // UDiv
1354 { X86::DIV64r, X86::MOV32r0, Copy, X86::RDX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001355 }
1356 }, // i64
1357 };
1358
1359 MVT VT;
1360 if (!isTypeLegal(I->getType(), VT))
1361 return false;
1362
1363 unsigned TypeIndex, OpIndex;
1364 switch (VT.SimpleTy) {
1365 default: return false;
1366 case MVT::i8: TypeIndex = 0; break;
1367 case MVT::i16: TypeIndex = 1; break;
1368 case MVT::i32: TypeIndex = 2; break;
1369 case MVT::i64: TypeIndex = 3;
1370 if (!Subtarget->is64Bit())
1371 return false;
1372 break;
1373 }
1374
1375 switch (I->getOpcode()) {
1376 default: llvm_unreachable("Unexpected div/rem opcode");
1377 case Instruction::SDiv: OpIndex = 0; break;
1378 case Instruction::SRem: OpIndex = 1; break;
1379 case Instruction::UDiv: OpIndex = 2; break;
1380 case Instruction::URem: OpIndex = 3; break;
1381 }
1382
1383 const DivRemEntry &TypeEntry = OpTable[TypeIndex];
1384 const DivRemEntry::DivRemResult &OpEntry = TypeEntry.ResultTable[OpIndex];
1385 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1386 if (Op0Reg == 0)
1387 return false;
1388 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1389 if (Op1Reg == 0)
1390 return false;
1391
1392 // Move op0 into low-order input register.
1393 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1394 TII.get(OpEntry.OpCopy), TypeEntry.LowInReg).addReg(Op0Reg);
1395 // Zero-extend or sign-extend into high-order input register.
1396 if (OpEntry.OpSignExtend) {
1397 if (OpEntry.IsOpSigned)
1398 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1399 TII.get(OpEntry.OpSignExtend));
Tim Northover64ec0ff2013-05-30 13:19:42 +00001400 else {
1401 unsigned Zero32 = createResultReg(&X86::GR32RegClass);
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001402 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Tim Northover64ec0ff2013-05-30 13:19:42 +00001403 TII.get(X86::MOV32r0), Zero32);
1404
1405 // Copy the zero into the appropriate sub/super/identical physical
1406 // register. Unfortunately the operations needed are not uniform enough to
1407 // fit neatly into the table above.
1408 if (VT.SimpleTy == MVT::i16) {
1409 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001410 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001411 .addReg(Zero32, 0, X86::sub_16bit);
1412 } else if (VT.SimpleTy == MVT::i32) {
1413 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001414 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001415 .addReg(Zero32);
1416 } else if (VT.SimpleTy == MVT::i64) {
1417 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1418 TII.get(TargetOpcode::SUBREG_TO_REG), TypeEntry.HighInReg)
1419 .addImm(0).addReg(Zero32).addImm(X86::sub_32bit);
1420 }
1421 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001422 }
1423 // Generate the DIV/IDIV instruction.
1424 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1425 TII.get(OpEntry.OpDivRem)).addReg(Op1Reg);
Jim Grosbachc35388f2013-07-09 02:07:25 +00001426 // For i8 remainder, we can't reference AH directly, as we'll end
1427 // up with bogus copies like %R9B = COPY %AH. Reference AX
1428 // instead to prevent AH references in a REX instruction.
1429 //
1430 // The current assumption of the fast register allocator is that isel
1431 // won't generate explicit references to the GPR8_NOREX registers. If
1432 // the allocator and/or the backend get enhanced to be more robust in
1433 // that regard, this can be, and should be, removed.
1434 unsigned ResultReg = 0;
1435 if ((I->getOpcode() == Instruction::SRem ||
1436 I->getOpcode() == Instruction::URem) &&
1437 OpEntry.DivRemResultReg == X86::AH && Subtarget->is64Bit()) {
1438 unsigned SourceSuperReg = createResultReg(&X86::GR16RegClass);
1439 unsigned ResultSuperReg = createResultReg(&X86::GR16RegClass);
1440 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1441 TII.get(Copy), SourceSuperReg).addReg(X86::AX);
1442
1443 // Shift AX right by 8 bits instead of using AH.
1444 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SHR16ri),
1445 ResultSuperReg).addReg(SourceSuperReg).addImm(8);
1446
1447 // Now reference the 8-bit subreg of the result.
1448 ResultReg = FastEmitInst_extractsubreg(MVT::i8, ResultSuperReg,
1449 /*Kill=*/true, X86::sub_8bit);
1450 }
1451 // Copy the result out of the physreg if we haven't already.
1452 if (!ResultReg) {
1453 ResultReg = createResultReg(TypeEntry.RC);
1454 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Copy), ResultReg)
1455 .addReg(OpEntry.DivRemResultReg);
1456 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001457 UpdateValueMap(I, ResultReg);
1458
1459 return true;
1460}
1461
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001462bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001463 MVT VT;
1464 if (!isTypeLegal(I->getType(), VT))
Chris Lattnera0f9d492008-10-15 05:07:36 +00001465 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001466
Eric Christopher0574cc52010-09-29 23:00:29 +00001467 // We only use cmov here, if we don't have a cmov instruction bail.
1468 if (!Subtarget->hasCMov()) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001469
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001470 unsigned Opc = 0;
1471 const TargetRegisterClass *RC = NULL;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001472 if (VT == MVT::i16) {
Dan Gohmane5560182008-09-05 21:13:04 +00001473 Opc = X86::CMOVE16rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001474 RC = &X86::GR16RegClass;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001475 } else if (VT == MVT::i32) {
Dan Gohmane5560182008-09-05 21:13:04 +00001476 Opc = X86::CMOVE32rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001477 RC = &X86::GR32RegClass;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001478 } else if (VT == MVT::i64) {
Dan Gohmane5560182008-09-05 21:13:04 +00001479 Opc = X86::CMOVE64rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001480 RC = &X86::GR64RegClass;
1481 } else {
Wesley Peck527da1b2010-11-23 03:31:01 +00001482 return false;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001483 }
1484
1485 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1486 if (Op0Reg == 0) return false;
1487 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1488 if (Op1Reg == 0) return false;
1489 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1490 if (Op2Reg == 0) return false;
1491
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001492 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1493 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001494 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001495 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1496 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001497 UpdateValueMap(I, ResultReg);
1498 return true;
1499}
1500
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001501bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00001502 // fpext from float to double.
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001503 if (X86ScalarSSEf64 &&
Chris Lattnerfdd87902009-10-05 05:54:46 +00001504 I->getType()->isDoubleTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001505 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001506 if (V->getType()->isFloatTy()) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00001507 unsigned OpReg = getRegForValue(V);
1508 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00001509 unsigned ResultReg = createResultReg(&X86::FR64RegClass);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001510 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1511 TII.get(X86::CVTSS2SDrr), ResultReg)
1512 .addReg(OpReg);
Chris Lattnera0f9d492008-10-15 05:07:36 +00001513 UpdateValueMap(I, ResultReg);
1514 return true;
Dan Gohmanbf646f22008-09-10 21:02:08 +00001515 }
1516 }
1517
1518 return false;
1519}
1520
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001521bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001522 if (X86ScalarSSEf64) {
Chris Lattnerfdd87902009-10-05 05:54:46 +00001523 if (I->getType()->isFloatTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001524 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001525 if (V->getType()->isDoubleTy()) {
Dan Gohmanbf646f22008-09-10 21:02:08 +00001526 unsigned OpReg = getRegForValue(V);
1527 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00001528 unsigned ResultReg = createResultReg(&X86::FR32RegClass);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001529 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1530 TII.get(X86::CVTSD2SSrr), ResultReg)
1531 .addReg(OpReg);
Dan Gohmanbf646f22008-09-10 21:02:08 +00001532 UpdateValueMap(I, ResultReg);
1533 return true;
1534 }
1535 }
1536 }
1537
1538 return false;
1539}
1540
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001541bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001542 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1543 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peck527da1b2010-11-23 03:31:01 +00001544
Eli Friedmanc7035512011-05-25 23:49:02 +00001545 // This code only handles truncation to byte.
Owen Anderson9f944592009-08-11 20:47:22 +00001546 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Chengb9286692008-09-07 08:47:42 +00001547 return false;
Eli Friedmanc7035512011-05-25 23:49:02 +00001548 if (!TLI.isTypeLegal(SrcVT))
Evan Chengb9286692008-09-07 08:47:42 +00001549 return false;
1550
1551 unsigned InputReg = getRegForValue(I->getOperand(0));
1552 if (!InputReg)
1553 // Unhandled operand. Halt "fast" selection and bail.
1554 return false;
1555
Eli Friedmanc7035512011-05-25 23:49:02 +00001556 if (SrcVT == MVT::i8) {
1557 // Truncate from i8 to i1; no code needed.
1558 UpdateValueMap(I, InputReg);
1559 return true;
1560 }
Evan Chengb9286692008-09-07 08:47:42 +00001561
Eli Friedmanc7035512011-05-25 23:49:02 +00001562 if (!Subtarget->is64Bit()) {
1563 // If we're on x86-32; we can't extract an i8 from a general register.
1564 // First issue a copy to GR16_ABCD or GR32_ABCD.
Craig Topperabadc662012-04-20 06:31:50 +00001565 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) ?
1566 (const TargetRegisterClass*)&X86::GR16_ABCDRegClass :
1567 (const TargetRegisterClass*)&X86::GR32_ABCDRegClass;
Eli Friedmanc7035512011-05-25 23:49:02 +00001568 unsigned CopyReg = createResultReg(CopyRC);
1569 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1570 CopyReg).addReg(InputReg);
1571 InputReg = CopyReg;
1572 }
1573
1574 // Issue an extract_subreg.
Owen Anderson9f944592009-08-11 20:47:22 +00001575 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedmanc7035512011-05-25 23:49:02 +00001576 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen9340ea52010-05-24 14:48:17 +00001577 X86::sub_8bit);
Evan Chengb9286692008-09-07 08:47:42 +00001578 if (!ResultReg)
1579 return false;
1580
1581 UpdateValueMap(I, ResultReg);
1582 return true;
1583}
1584
Eli Friedman60afcc22011-05-20 22:21:04 +00001585bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1586 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1587}
1588
Eli Friedmanbcc69142011-04-27 01:45:07 +00001589bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1590 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedman60afcc22011-05-20 22:21:04 +00001591
Eli Friedmanbcc69142011-04-27 01:45:07 +00001592 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedman60afcc22011-05-20 22:21:04 +00001593 if (!IsMemcpySmall(Len))
1594 return false;
1595
1596 bool i64Legal = Subtarget->is64Bit();
Eli Friedmanbcc69142011-04-27 01:45:07 +00001597
1598 // We don't care about alignment here since we just emit integer accesses.
1599 while (Len) {
1600 MVT VT;
1601 if (Len >= 8 && i64Legal)
1602 VT = MVT::i64;
1603 else if (Len >= 4)
1604 VT = MVT::i32;
1605 else if (Len >= 2)
1606 VT = MVT::i16;
1607 else {
Eli Friedmanbcc69142011-04-27 01:45:07 +00001608 VT = MVT::i8;
1609 }
1610
1611 unsigned Reg;
1612 bool RV = X86FastEmitLoad(VT, SrcAM, Reg);
1613 RV &= X86FastEmitStore(VT, Reg, DestAM);
1614 assert(RV && "Failed to emit load or store??");
1615
1616 unsigned Size = VT.getSizeInBits()/8;
1617 Len -= Size;
1618 DestAM.Disp += Size;
1619 SrcAM.Disp += Size;
1620 }
1621
1622 return true;
1623}
1624
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001625bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling80b34b32008-12-09 02:42:50 +00001626 // FIXME: Handle more intrinsics.
Chris Lattner99a8cb62009-04-12 07:36:01 +00001627 switch (I.getIntrinsicID()) {
Bill Wendling80b34b32008-12-09 02:42:50 +00001628 default: return false;
Chris Lattner91328b32011-04-19 05:52:03 +00001629 case Intrinsic::memcpy: {
1630 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1631 // Don't handle volatile or variable length memcpys.
Eli Friedmancd2124a2011-06-10 23:39:36 +00001632 if (MCI.isVolatile())
Chris Lattner91328b32011-04-19 05:52:03 +00001633 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00001634
Eli Friedmancd2124a2011-06-10 23:39:36 +00001635 if (isa<ConstantInt>(MCI.getLength())) {
1636 // Small memcpy's are common enough that we want to do them
1637 // without a call if possible.
1638 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1639 if (IsMemcpySmall(Len)) {
1640 X86AddressMode DestAM, SrcAM;
1641 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1642 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1643 return false;
1644 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1645 return true;
1646 }
1647 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001648
Eli Friedmancd2124a2011-06-10 23:39:36 +00001649 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1650 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner91328b32011-04-19 05:52:03 +00001651 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00001652
Eli Friedmancd2124a2011-06-10 23:39:36 +00001653 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1654 return false;
1655
1656 return DoSelectCall(&I, "memcpy");
Chris Lattner91328b32011-04-19 05:52:03 +00001657 }
Eli Friedmancd2124a2011-06-10 23:39:36 +00001658 case Intrinsic::memset: {
1659 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001660
Nick Lewyckya530a4d2011-08-02 00:40:16 +00001661 if (MSI.isVolatile())
1662 return false;
1663
Eli Friedmancd2124a2011-06-10 23:39:36 +00001664 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1665 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1666 return false;
1667
1668 if (MSI.getDestAddressSpace() > 255)
1669 return false;
1670
1671 return DoSelectCall(&I, "memset");
1672 }
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001673 case Intrinsic::stackprotector: {
Chad Rosier06e34d92012-05-11 19:43:29 +00001674 // Emit code to store the stack guard onto the stack.
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001675 EVT PtrTy = TLI.getPointerTy();
1676
Gabor Greif83205af2010-06-26 11:51:52 +00001677 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1678 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001679
1680 // Grab the frame index.
1681 X86AddressMode AM;
1682 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher5e95aee2010-03-18 21:58:33 +00001683 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001684 return true;
1685 }
Dale Johannesend5575f22010-01-26 00:09:58 +00001686 case Intrinsic::dbg_declare: {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001687 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesend5575f22010-01-26 00:09:58 +00001688 X86AddressMode AM;
Dale Johannesenad00f032010-01-29 21:21:28 +00001689 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesend5575f22010-01-26 00:09:58 +00001690 if (!X86SelectAddress(DI->getAddress(), AM))
1691 return false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00001692 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen654528e2010-02-18 18:51:15 +00001693 // FIXME may need to add RegState::Debug to any registers produced,
1694 // although ESP/EBP should be the only ones at the moment.
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001695 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1696 addImm(0).addMetadata(DI->getVariable());
Dale Johannesend5575f22010-01-26 00:09:58 +00001697 return true;
1698 }
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00001699 case Intrinsic::trap: {
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001700 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00001701 return true;
1702 }
Bill Wendling80b34b32008-12-09 02:42:50 +00001703 case Intrinsic::sadd_with_overflow:
1704 case Intrinsic::uadd_with_overflow: {
Chris Lattner91328b32011-04-19 05:52:03 +00001705 // FIXME: Should fold immediates.
Eric Christopher0713a9d2011-06-08 23:55:35 +00001706
Bill Wendlinge25d3412008-12-09 07:55:31 +00001707 // Replace "add with overflow" intrinsics with an "add" instruction followed
Eli Friedmana4d4a012011-05-16 21:06:17 +00001708 // by a seto/setc instruction.
Bill Wendling80b34b32008-12-09 02:42:50 +00001709 const Function *Callee = I.getCalledFunction();
Chris Lattner229907c2011-07-18 04:54:35 +00001710 Type *RetTy =
Bill Wendling80b34b32008-12-09 02:42:50 +00001711 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1712
Duncan Sandsf5dda012010-11-03 11:35:31 +00001713 MVT VT;
Bill Wendling80b34b32008-12-09 02:42:50 +00001714 if (!isTypeLegal(RetTy, VT))
1715 return false;
1716
Gabor Greif83205af2010-06-26 11:51:52 +00001717 const Value *Op1 = I.getArgOperand(0);
1718 const Value *Op2 = I.getArgOperand(1);
Bill Wendling80b34b32008-12-09 02:42:50 +00001719 unsigned Reg1 = getRegForValue(Op1);
1720 unsigned Reg2 = getRegForValue(Op2);
1721
1722 if (Reg1 == 0 || Reg2 == 0)
1723 // FIXME: Handle values *not* in registers.
1724 return false;
1725
1726 unsigned OpC = 0;
Owen Anderson9f944592009-08-11 20:47:22 +00001727 if (VT == MVT::i32)
Bill Wendling80b34b32008-12-09 02:42:50 +00001728 OpC = X86::ADD32rr;
Owen Anderson9f944592009-08-11 20:47:22 +00001729 else if (VT == MVT::i64)
Bill Wendling80b34b32008-12-09 02:42:50 +00001730 OpC = X86::ADD64rr;
1731 else
1732 return false;
1733
Eli Friedmana4d4a012011-05-16 21:06:17 +00001734 // The call to CreateRegs builds two sequential registers, to store the
Sylvestre Ledru35521e22012-07-23 08:51:15 +00001735 // both the returned values.
Eli Friedmana4d4a012011-05-16 21:06:17 +00001736 unsigned ResultReg = FuncInfo.CreateRegs(I.getType());
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001737 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1738 .addReg(Reg1).addReg(Reg2);
Wesley Peck527da1b2010-11-23 03:31:01 +00001739
Chris Lattner99a8cb62009-04-12 07:36:01 +00001740 unsigned Opc = X86::SETBr;
1741 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1742 Opc = X86::SETOr;
Eli Friedmana4d4a012011-05-16 21:06:17 +00001743 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg+1);
1744
1745 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling80b34b32008-12-09 02:42:50 +00001746 return true;
1747 }
1748 }
1749}
1750
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001751bool X86FastISel::FastLowerArguments() {
1752 if (!FuncInfo.CanLowerReturn)
1753 return false;
1754
1755 const Function *F = FuncInfo.Fn;
1756 if (F->isVarArg())
1757 return false;
1758
1759 CallingConv::ID CC = F->getCallingConv();
1760 if (CC != CallingConv::C)
1761 return false;
Charles Davise8f297c2013-07-12 06:02:35 +00001762
1763 if (Subtarget->isCallingConvWin64(CC))
1764 return false;
1765
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001766 if (!Subtarget->is64Bit())
1767 return false;
1768
1769 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
1770 unsigned Idx = 1;
1771 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1772 I != E; ++I, ++Idx) {
1773 if (Idx > 6)
1774 return false;
1775
1776 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
1777 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
1778 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
1779 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
1780 return false;
1781
1782 Type *ArgTy = I->getType();
1783 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
1784 return false;
1785
1786 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosier1b33e8d2013-02-26 01:05:31 +00001787 if (!ArgVT.isSimple()) return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001788 switch (ArgVT.getSimpleVT().SimpleTy) {
1789 case MVT::i32:
1790 case MVT::i64:
1791 break;
1792 default:
1793 return false;
1794 }
1795 }
1796
1797 static const uint16_t GPR32ArgRegs[] = {
1798 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
1799 };
1800 static const uint16_t GPR64ArgRegs[] = {
1801 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
1802 };
1803
1804 Idx = 0;
1805 const TargetRegisterClass *RC32 = TLI.getRegClassFor(MVT::i32);
1806 const TargetRegisterClass *RC64 = TLI.getRegClassFor(MVT::i64);
1807 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1808 I != E; ++I, ++Idx) {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001809 bool is32Bit = TLI.getValueType(I->getType()) == MVT::i32;
1810 const TargetRegisterClass *RC = is32Bit ? RC32 : RC64;
1811 unsigned SrcReg = is32Bit ? GPR32ArgRegs[Idx] : GPR64ArgRegs[Idx];
1812 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
1813 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1814 // Without this, EmitLiveInCopies may eliminate the livein if its only
1815 // use is a bitcast (which isn't turned into an instruction).
1816 unsigned ResultReg = createResultReg(RC);
1817 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1818 ResultReg).addReg(DstReg, getKillRegState(true));
1819 UpdateValueMap(I, ResultReg);
1820 }
1821 return true;
1822}
1823
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001824bool X86FastISel::X86SelectCall(const Instruction *I) {
1825 const CallInst *CI = cast<CallInst>(I);
Gabor Greif83205af2010-06-26 11:51:52 +00001826 const Value *Callee = CI->getCalledValue();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001827
1828 // Can't handle inline asm yet.
1829 if (isa<InlineAsm>(Callee))
1830 return false;
1831
Bill Wendling80b34b32008-12-09 02:42:50 +00001832 // Handle intrinsic calls.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001833 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattner99a8cb62009-04-12 07:36:01 +00001834 return X86VisitIntrinsicCall(*II);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001835
Chad Rosierdf42cf32012-12-11 00:18:02 +00001836 // Allow SelectionDAG isel to handle tail calls.
1837 if (cast<CallInst>(I)->isTailCall())
1838 return false;
1839
Eli Friedmancd2124a2011-06-10 23:39:36 +00001840 return DoSelectCall(I, 0);
1841}
1842
Rafael Espindola73173c52012-07-25 15:42:45 +00001843static unsigned computeBytesPoppedByCallee(const X86Subtarget &Subtarget,
1844 const ImmutableCallSite &CS) {
Rafael Espindola2caee7f2012-07-25 13:35:45 +00001845 if (Subtarget.is64Bit())
1846 return 0;
1847 if (Subtarget.isTargetWindows())
1848 return 0;
1849 CallingConv::ID CC = CS.getCallingConv();
1850 if (CC == CallingConv::Fast || CC == CallingConv::GHC)
1851 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001852 if (!CS.paramHasAttr(1, Attribute::StructRet))
Rafael Espindola2caee7f2012-07-25 13:35:45 +00001853 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001854 if (CS.paramHasAttr(1, Attribute::InReg))
Rafael Espindola11c38b92012-07-25 13:41:10 +00001855 return 0;
Rafael Espindola2caee7f2012-07-25 13:35:45 +00001856 return 4;
1857}
1858
Eli Friedmancd2124a2011-06-10 23:39:36 +00001859// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
1860bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
1861 const CallInst *CI = cast<CallInst>(I);
1862 const Value *Callee = CI->getCalledValue();
1863
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001864 // Handle only C and fastcc calling conventions for now.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001865 ImmutableCallSite CS(CI);
Sandeep Patel68c5f472009-09-02 08:44:58 +00001866 CallingConv::ID CC = CS.getCallingConv();
Charles Davise8f297c2013-07-12 06:02:35 +00001867 bool isWin64 = Subtarget->isCallingConvWin64(CC);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00001868 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +00001869 CC != CallingConv::X86_FastCall && CC != CallingConv::X86_64_Win64 &&
1870 CC != CallingConv::X86_64_SysV)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001871 return false;
1872
Evan Chengd10089a2010-01-27 00:00:57 +00001873 // fastcc with -tailcallopt is intended to provide a guaranteed
1874 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001875 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Evan Chengd10089a2010-01-27 00:00:57 +00001876 return false;
1877
Chris Lattner229907c2011-07-18 04:54:35 +00001878 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1879 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedmanee92a6b2011-04-19 17:22:22 +00001880 bool isVarArg = FTy->isVarArg();
1881
1882 // Don't know how to handle Win64 varargs yet. Nothing special needed for
1883 // x86-32. Special handling for x86-64 is implemented.
Charles Davise8f297c2013-07-12 06:02:35 +00001884 if (isVarArg && isWin64)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001885 return false;
1886
Dan Gohmandc53f1c2010-05-27 18:43:40 +00001887 // Fast-isel doesn't know about callee-pop yet.
Evan Cheng3a0c5e52011-06-23 17:54:54 +00001888 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001889 TM.Options.GuaranteedTailCallOpt))
Dan Gohmandc53f1c2010-05-27 18:43:40 +00001890 return false;
1891
Eli Friedman7b279422011-05-17 18:29:03 +00001892 // Check whether the function can return without sret-demotion.
1893 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +00001894 GetReturnInfo(I->getType(), CS.getAttributes(), Outs, TLI);
Eli Friedman7b279422011-05-17 18:29:03 +00001895 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Bill Wendlingea6397f2012-07-19 00:11:40 +00001896 *FuncInfo.MF, FTy->isVarArg(),
1897 Outs, FTy->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00001898 if (!CanLowerReturn)
Eli Friedman7335e8a2011-05-17 02:36:59 +00001899 return false;
1900
Dan Gohmanaf13bf12008-09-17 21:18:49 +00001901 // Materialize callee address in a register. FIXME: GV address can be
1902 // handled with a CALLpcrel32 instead.
Dan Gohman9801ba42008-09-19 22:16:54 +00001903 X86AddressMode CalleeAM;
Chris Lattner8212d372009-07-10 05:33:42 +00001904 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman9801ba42008-09-19 22:16:54 +00001905 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00001906 unsigned CalleeOp = 0;
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001907 const GlobalValue *GV = 0;
Chris Lattnerd17366a2009-06-27 04:50:14 +00001908 if (CalleeAM.GV != 0) {
Dan Gohman9801ba42008-09-19 22:16:54 +00001909 GV = CalleeAM.GV;
Chris Lattnerd17366a2009-06-27 04:50:14 +00001910 } else if (CalleeAM.Base.Reg != 0) {
1911 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman9801ba42008-09-19 22:16:54 +00001912 } else
1913 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00001914
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001915 // Deal with call operands first.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001916 SmallVector<const Value *, 8> ArgVals;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00001917 SmallVector<unsigned, 8> Args;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001918 SmallVector<MVT, 8> ArgVTs;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00001919 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosierf0687632012-02-15 00:36:26 +00001920 unsigned arg_size = CS.arg_size();
1921 Args.reserve(arg_size);
1922 ArgVals.reserve(arg_size);
1923 ArgVTs.reserve(arg_size);
1924 ArgFlags.reserve(arg_size);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001925 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001926 i != e; ++i) {
Eli Friedmancd2124a2011-06-10 23:39:36 +00001927 // If we're lowering a mem intrinsic instead of a regular call, skip the
1928 // last two arguments, which should not passed to the underlying functions.
1929 if (MemIntName && e-i <= 2)
1930 break;
Chris Lattnerd7f7c932011-04-19 04:42:38 +00001931 Value *ArgVal = *i;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001932 ISD::ArgFlagsTy Flags;
1933 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001934 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001935 Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001936 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001937 Flags.setZExt();
1938
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001939 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattner229907c2011-07-18 04:54:35 +00001940 PointerType *Ty = cast<PointerType>(ArgVal->getType());
1941 Type *ElementTy = Ty->getElementType();
Eli Friedman60afcc22011-05-20 22:21:04 +00001942 unsigned FrameSize = TD.getTypeAllocSize(ElementTy);
1943 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
1944 if (!FrameAlign)
1945 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
1946 Flags.setByVal();
1947 Flags.setByValSize(FrameSize);
1948 Flags.setByValAlign(FrameAlign);
1949 if (!IsMemcpySmall(FrameSize))
1950 return false;
1951 }
1952
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001953 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
Eli Friedman60afcc22011-05-20 22:21:04 +00001954 Flags.setInReg();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001955 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
Eli Friedman60afcc22011-05-20 22:21:04 +00001956 Flags.setNest();
1957
Chris Lattnerd7f7c932011-04-19 04:42:38 +00001958 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1959 // instruction. This is safe because it is common to all fastisel supported
1960 // calling conventions on x86.
1961 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1962 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1963 CI->getBitWidth() == 16) {
1964 if (Flags.isSExt())
1965 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1966 else
1967 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1968 }
1969 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001970
Chris Lattner5f4b7832011-04-19 05:09:50 +00001971 unsigned ArgReg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00001972
Chris Lattner34a08c22011-04-19 05:15:59 +00001973 // Passing bools around ends up doing a trunc to i1 and passing it.
1974 // Codegen this as an argument + "and 1".
Chris Lattner5f4b7832011-04-19 05:09:50 +00001975 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
1976 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
1977 ArgVal->hasOneUse()) {
Chris Lattner5f4b7832011-04-19 05:09:50 +00001978 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
1979 ArgReg = getRegForValue(ArgVal);
1980 if (ArgReg == 0) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00001981
Chris Lattner5f4b7832011-04-19 05:09:50 +00001982 MVT ArgVT;
1983 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00001984
Chris Lattner5f4b7832011-04-19 05:09:50 +00001985 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
1986 ArgVal->hasOneUse(), 1);
1987 } else {
1988 ArgReg = getRegForValue(ArgVal);
Chris Lattner5f4b7832011-04-19 05:09:50 +00001989 }
Chris Lattnerd7f7c932011-04-19 04:42:38 +00001990
Chris Lattner34a08c22011-04-19 05:15:59 +00001991 if (ArgReg == 0) return false;
1992
Chris Lattner229907c2011-07-18 04:54:35 +00001993 Type *ArgTy = ArgVal->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00001994 MVT ArgVT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00001995 if (!isTypeLegal(ArgTy, ArgVT))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001996 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00001997 if (ArgVT == MVT::x86mmx)
1998 return false;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001999 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2000 Flags.setOrigAlign(OriginalAlignment);
2001
Chris Lattner5f4b7832011-04-19 05:09:50 +00002002 Args.push_back(ArgReg);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002003 ArgVals.push_back(ArgVal);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002004 ArgVTs.push_back(ArgVT);
2005 ArgFlags.push_back(Flags);
2006 }
2007
2008 // Analyze operands of the call, assigning locations to each operand.
2009 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002010 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00002011 I->getParent()->getContext());
Wesley Peck527da1b2010-11-23 03:31:01 +00002012
Dan Gohman47a07242010-06-01 21:09:47 +00002013 // Allocate shadow area for Win64
Charles Davise8f297c2013-07-12 06:02:35 +00002014 if (isWin64)
Wesley Peck527da1b2010-11-23 03:31:01 +00002015 CCInfo.AllocateStack(32, 8);
Dan Gohman47a07242010-06-01 21:09:47 +00002016
Duncan Sandsfb0a48e2010-10-31 13:21:44 +00002017 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002018
2019 // Get a count of how many bytes are to be pushed on the stack.
2020 unsigned NumBytes = CCInfo.getNextStackOffset();
2021
2022 // Issue CALLSEQ_START
Evan Cheng194c3dc2011-06-28 21:14:33 +00002023 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002024 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
2025 .addImm(NumBytes);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002026
Chris Lattner3ba29352008-10-15 05:30:52 +00002027 // Process argument: walk the register/memloc assignments, inserting
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002028 // copies / loads.
2029 SmallVector<unsigned, 4> RegArgs;
2030 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2031 CCValAssign &VA = ArgLocs[i];
2032 unsigned Arg = Args[VA.getValNo()];
Owen Anderson53aa7a92009-08-10 22:56:29 +00002033 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002034
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002035 // Promote the value if needed.
2036 switch (VA.getLocInfo()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002037 case CCValAssign::Full: break;
Evan Cheng6500d172008-09-08 06:35:17 +00002038 case CCValAssign::SExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002039 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2040 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002041 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2042 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002043 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002044 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002045 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002046 }
2047 case CCValAssign::ZExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002048 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2049 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002050 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
2051 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002052 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002053 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002054 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002055 }
2056 case CCValAssign::AExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002057 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2058 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002059 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
2060 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002061 if (!Emitted)
2062 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattnera0f9d492008-10-15 05:07:36 +00002063 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002064 if (!Emitted)
2065 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2066 Arg, ArgVT, Arg);
Wesley Peck527da1b2010-11-23 03:31:01 +00002067
Chris Lattner2d7df022011-01-05 22:26:52 +00002068 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002069 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002070 break;
2071 }
Dan Gohman8c795692009-08-05 05:33:42 +00002072 case CCValAssign::BCvt: {
Duncan Sandsf5dda012010-11-03 11:35:31 +00002073 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peck527da1b2010-11-23 03:31:01 +00002074 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohman8c795692009-08-05 05:33:42 +00002075 assert(BC != 0 && "Failed to emit a bitcast!");
2076 Arg = BC;
2077 ArgVT = VA.getLocVT();
2078 break;
2079 }
Chad Rosier8446ede2012-07-11 19:58:38 +00002080 case CCValAssign::VExt:
2081 // VExt has not been implemented, so this should be impossible to reach
2082 // for now. However, fallback to Selection DAG isel once implemented.
2083 return false;
2084 case CCValAssign::Indirect:
2085 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
2086 // support this.
2087 return false;
Evan Cheng6500d172008-09-08 06:35:17 +00002088 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002089
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002090 if (VA.isRegLoc()) {
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00002091 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2092 VA.getLocReg()).addReg(Arg);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002093 RegArgs.push_back(VA.getLocReg());
2094 } else {
2095 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman39d82f92008-09-10 20:11:02 +00002096 X86AddressMode AM;
Bill Wendling8f268402013-06-07 21:00:34 +00002097 const X86RegisterInfo *RegInfo = static_cast<const X86RegisterInfo*>(
2098 getTargetMachine()->getRegisterInfo());
Michael Liao70a99c82012-11-01 03:47:50 +00002099 AM.Base.Reg = RegInfo->getStackRegister();
Dan Gohman39d82f92008-09-10 20:11:02 +00002100 AM.Disp = LocMemOffset;
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002101 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedman60afcc22011-05-20 22:21:04 +00002102 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002103
Eli Friedman60afcc22011-05-20 22:21:04 +00002104 if (Flags.isByVal()) {
2105 X86AddressMode SrcAM;
2106 SrcAM.Base.Reg = Arg;
2107 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
2108 assert(Res && "memcpy length already checked!"); (void)Res;
2109 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
2110 // If this is a really simple value, emit this with the Value* version
Nick Lewycky064c1c02011-10-12 00:14:12 +00002111 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
Eli Friedman60afcc22011-05-20 22:21:04 +00002112 // as it can cause us to reevaluate the argument.
Lang Hames7d2f7b52011-10-18 22:11:33 +00002113 if (!X86FastEmitStore(ArgVT, ArgVal, AM))
2114 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002115 } else {
Lang Hames7d2f7b52011-10-18 22:11:33 +00002116 if (!X86FastEmitStore(ArgVT, Arg, AM))
2117 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002118 }
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002119 }
2120 }
2121
Dan Gohman3691d502008-09-25 15:24:26 +00002122 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peck527da1b2010-11-23 03:31:01 +00002123 // GOT pointer.
Chris Lattnerfef11d62009-07-09 04:39:06 +00002124 if (Subtarget->isPICStyleGOT()) {
Dan Gohman87fb4e82010-07-07 16:29:44 +00002125 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00002126 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2127 X86::EBX).addReg(Base);
Dan Gohman3691d502008-09-25 15:24:26 +00002128 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002129
Charles Davise8f297c2013-07-12 06:02:35 +00002130 if (Subtarget->is64Bit() && isVarArg && !isWin64) {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002131 // Count the number of XMM registers allocated.
Craig Topperbef78fc2012-03-11 07:57:25 +00002132 static const uint16_t XMMArgRegs[] = {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002133 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2134 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2135 };
2136 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
2137 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::MOV8ri),
2138 X86::AL).addImm(NumXMMRegs);
2139 }
2140
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002141 // Issue the call.
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002142 MachineInstrBuilder MIB;
2143 if (CalleeOp) {
2144 // Register-indirect call.
Nate Begeman68a069a2010-07-22 00:09:39 +00002145 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002146 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002147 CallOpc = X86::CALL64r;
2148 else
2149 CallOpc = X86::CALL32r;
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002150 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
2151 .addReg(CalleeOp);
Wesley Peck527da1b2010-11-23 03:31:01 +00002152
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002153 } else {
2154 // Direct call.
2155 assert(GV && "Not a direct call");
Nate Begeman68a069a2010-07-22 00:09:39 +00002156 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002157 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002158 CallOpc = X86::CALL64pcrel32;
2159 else
2160 CallOpc = X86::CALLpcrel32;
Wesley Peck527da1b2010-11-23 03:31:01 +00002161
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002162 // See if we need any target-specific flags on the GV operand.
2163 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00002164
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002165 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2166 // external symbols most go through the PLT in PIC mode. If the symbol
2167 // has hidden or protected visibility, or if it is static or local, then
2168 // we don't need to use the PLT - we can directly call it.
2169 if (Subtarget->isTargetELF() &&
2170 TM.getRelocationModel() == Reloc::PIC_ &&
2171 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
2172 OpFlags = X86II::MO_PLT;
Chris Lattnere2f524f2009-07-10 20:47:30 +00002173 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002174 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00002175 (!Subtarget->getTargetTriple().isMacOSX() ||
2176 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002177 // PC-relative references to external symbols should go through $stub,
2178 // unless we're building with the leopard linker or later, which
2179 // automatically synthesizes these stubs.
2180 OpFlags = X86II::MO_DARWIN_STUB;
2181 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002182
2183
Eli Friedmancd2124a2011-06-10 23:39:36 +00002184 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc));
2185 if (MemIntName)
Eli Friedman1735b292011-06-11 01:55:07 +00002186 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedmancd2124a2011-06-10 23:39:36 +00002187 else
2188 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002189 }
Dan Gohman3691d502008-09-25 15:24:26 +00002190
Jakob Stoklund Olesen8a450cb2012-02-16 00:02:50 +00002191 // Add a register mask with the call-preserved registers.
2192 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2193 MIB.addRegMask(TRI.getCallPreservedMask(CS.getCallingConv()));
2194
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00002195 // Add an implicit use GOT pointer in EBX.
2196 if (Subtarget->isPICStyleGOT())
2197 MIB.addReg(X86::EBX, RegState::Implicit);
2198
Charles Davise8f297c2013-07-12 06:02:35 +00002199 if (Subtarget->is64Bit() && isVarArg && !isWin64)
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00002200 MIB.addReg(X86::AL, RegState::Implicit);
2201
2202 // Add implicit physical register uses to the call.
2203 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2204 MIB.addReg(RegArgs[i], RegState::Implicit);
2205
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002206 // Issue CALLSEQ_END
Evan Cheng194c3dc2011-06-28 21:14:33 +00002207 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindola73173c52012-07-25 15:42:45 +00002208 const unsigned NumBytesCallee = computeBytesPoppedByCallee(*Subtarget, CS);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002209 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
Eli Friedman7cd51012011-04-28 20:19:12 +00002210 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002211
Eli Friedman7b279422011-05-17 18:29:03 +00002212 // Build info for return calling conv lowering code.
2213 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
2214 SmallVector<ISD::InputArg, 32> Ins;
2215 SmallVector<EVT, 4> RetTys;
2216 ComputeValueVTs(TLI, I->getType(), RetTys);
2217 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
2218 EVT VT = RetTys[i];
Patrik Hagglundbad545c2012-12-19 11:48:16 +00002219 MVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
Eli Friedman7b279422011-05-17 18:29:03 +00002220 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
2221 for (unsigned j = 0; j != NumRegs; ++j) {
2222 ISD::InputArg MyFlags;
Patrik Hagglundbad545c2012-12-19 11:48:16 +00002223 MyFlags.VT = RegisterVT;
Eli Friedman7b279422011-05-17 18:29:03 +00002224 MyFlags.Used = !CS.getInstruction()->use_empty();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002225 if (CS.paramHasAttr(0, Attribute::SExt))
Eli Friedman7b279422011-05-17 18:29:03 +00002226 MyFlags.Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002227 if (CS.paramHasAttr(0, Attribute::ZExt))
Eli Friedman7b279422011-05-17 18:29:03 +00002228 MyFlags.Flags.setZExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002229 if (CS.paramHasAttr(0, Attribute::InReg))
Eli Friedman7b279422011-05-17 18:29:03 +00002230 MyFlags.Flags.setInReg();
2231 Ins.push_back(MyFlags);
2232 }
2233 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00002234
Eli Friedman7b279422011-05-17 18:29:03 +00002235 // Now handle call return values.
2236 SmallVector<unsigned, 4> UsedRegs;
2237 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002238 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00002239 I->getParent()->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00002240 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
2241 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
2242 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2243 EVT CopyVT = RVLocs[i].getValVT();
2244 unsigned CopyReg = ResultReg + i;
Wesley Peck527da1b2010-11-23 03:31:01 +00002245
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002246 // If this is a call to a function that returns an fp value on the x87 fp
2247 // stack, but where we prefer to use the value in xmm registers, copy it
2248 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman7b279422011-05-17 18:29:03 +00002249 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002250 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00002251 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002252 CopyVT = MVT::f80;
Craig Topperabadc662012-04-20 06:31:50 +00002253 CopyReg = createResultReg(&X86::RFP80RegClass);
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00002254 }
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002255 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::FpPOP_RETVAL),
2256 CopyReg);
2257 } else {
2258 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2259 CopyReg).addReg(RVLocs[i].getLocReg());
2260 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002261 }
2262
Eli Friedman7b279422011-05-17 18:29:03 +00002263 if (CopyVT != RVLocs[i].getValVT()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002264 // Round the F80 the right size, which also moves to the appropriate xmm
2265 // register. This is accomplished by storing the F80 value in memory and
2266 // then loading it back. Ewww...
Eli Friedman7b279422011-05-17 18:29:03 +00002267 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson9f944592009-08-11 20:47:22 +00002268 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002269 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene1fbe0542009-11-12 20:49:22 +00002270 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002271 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2272 TII.get(Opc)), FI)
Eli Friedman7b279422011-05-17 18:29:03 +00002273 .addReg(CopyReg);
Owen Anderson9f944592009-08-11 20:47:22 +00002274 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002275 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eli Friedman7b279422011-05-17 18:29:03 +00002276 TII.get(Opc), ResultReg + i), FI);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002277 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00002278 }
Eli Friedman83ba1502011-05-17 00:13:47 +00002279
Eli Friedman7b279422011-05-17 18:29:03 +00002280 if (RVLocs.size())
2281 UpdateValueMap(I, ResultReg, RVLocs.size());
2282
Dan Gohman86936502010-06-18 23:28:01 +00002283 // Set all unused physreg defs as dead.
2284 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2285
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002286 return true;
2287}
2288
2289
Dan Gohmand58f3e32008-08-28 23:21:34 +00002290bool
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002291X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohmand58f3e32008-08-28 23:21:34 +00002292 switch (I->getOpcode()) {
2293 default: break;
Evan Chenga41ee292008-09-03 06:44:39 +00002294 case Instruction::Load:
Dan Gohman7bda51f2008-09-03 23:12:08 +00002295 return X86SelectLoad(I);
Owen Andersonb8c7ba22008-09-04 16:48:33 +00002296 case Instruction::Store:
2297 return X86SelectStore(I);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002298 case Instruction::Ret:
2299 return X86SelectRet(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00002300 case Instruction::ICmp:
2301 case Instruction::FCmp:
2302 return X86SelectCmp(I);
Dan Gohmana5753b32008-09-05 01:06:14 +00002303 case Instruction::ZExt:
2304 return X86SelectZExt(I);
2305 case Instruction::Br:
2306 return X86SelectBranch(I);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002307 case Instruction::Call:
2308 return X86SelectCall(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00002309 case Instruction::LShr:
2310 case Instruction::AShr:
2311 case Instruction::Shl:
2312 return X86SelectShift(I);
Eli Bendersky24a36eb2013-04-17 20:10:13 +00002313 case Instruction::SDiv:
2314 case Instruction::UDiv:
2315 case Instruction::SRem:
2316 case Instruction::URem:
2317 return X86SelectDivRem(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00002318 case Instruction::Select:
2319 return X86SelectSelect(I);
Evan Chengb9286692008-09-07 08:47:42 +00002320 case Instruction::Trunc:
2321 return X86SelectTrunc(I);
Dan Gohmanbf646f22008-09-10 21:02:08 +00002322 case Instruction::FPExt:
2323 return X86SelectFPExt(I);
2324 case Instruction::FPTrunc:
2325 return X86SelectFPTrunc(I);
Dan Gohmana62e4ab2009-03-13 23:53:06 +00002326 case Instruction::IntToPtr: // Deliberate fall-through.
2327 case Instruction::PtrToInt: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002328 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
2329 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohmana62e4ab2009-03-13 23:53:06 +00002330 if (DstVT.bitsGT(SrcVT))
2331 return X86SelectZExt(I);
2332 if (DstVT.bitsLT(SrcVT))
2333 return X86SelectTrunc(I);
2334 unsigned Reg = getRegForValue(I->getOperand(0));
2335 if (Reg == 0) return false;
2336 UpdateValueMap(I, Reg);
2337 return true;
2338 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00002339 }
2340
2341 return false;
2342}
2343
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002344unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00002345 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00002346 if (!isTypeLegal(C->getType(), VT))
Michael Liao3c898062012-08-30 00:30:16 +00002347 return 0;
2348
2349 // Can't handle alternate code models yet.
2350 if (TM.getCodeModel() != CodeModel::Small)
2351 return 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00002352
Owen Anderson50288e32008-09-05 00:06:23 +00002353 // Get opcode and regclass of the output for the given load instruction.
2354 unsigned Opc = 0;
2355 const TargetRegisterClass *RC = NULL;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002356 switch (VT.SimpleTy) {
Michael Liao3c898062012-08-30 00:30:16 +00002357 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00002358 case MVT::i8:
Owen Anderson50288e32008-09-05 00:06:23 +00002359 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +00002360 RC = &X86::GR8RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002361 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002362 case MVT::i16:
Owen Anderson50288e32008-09-05 00:06:23 +00002363 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +00002364 RC = &X86::GR16RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002365 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002366 case MVT::i32:
Owen Anderson50288e32008-09-05 00:06:23 +00002367 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +00002368 RC = &X86::GR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002369 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002370 case MVT::i64:
Owen Anderson50288e32008-09-05 00:06:23 +00002371 // Must be in x86-64 mode.
2372 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +00002373 RC = &X86::GR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002374 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002375 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002376 if (X86ScalarSSEf32) {
2377 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +00002378 RC = &X86::FR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002379 } else {
2380 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +00002381 RC = &X86::RFP32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002382 }
2383 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002384 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002385 if (X86ScalarSSEf64) {
2386 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +00002387 RC = &X86::FR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002388 } else {
2389 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +00002390 RC = &X86::RFP64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002391 }
2392 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002393 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +00002394 // No f80 support yet.
Michael Liao3c898062012-08-30 00:30:16 +00002395 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00002396 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002397
Dan Gohman9801ba42008-09-19 22:16:54 +00002398 // Materialize addresses with LEA instructions.
Owen Anderson50288e32008-09-05 00:06:23 +00002399 if (isa<GlobalValue>(C)) {
Dan Gohman9801ba42008-09-19 22:16:54 +00002400 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00002401 if (X86SelectAddress(C, AM)) {
Chris Lattner48326602011-04-17 17:12:08 +00002402 // If the expression is just a basereg, then we're done, otherwise we need
2403 // to emit an LEA.
2404 if (AM.BaseType == X86AddressMode::RegBase &&
2405 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
2406 return AM.Base.Reg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002407
Chris Lattner48326602011-04-17 17:12:08 +00002408 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman9801ba42008-09-19 22:16:54 +00002409 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002410 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2411 TII.get(Opc), ResultReg), AM);
Owen Anderson50288e32008-09-05 00:06:23 +00002412 return ResultReg;
Dan Gohman9801ba42008-09-19 22:16:54 +00002413 }
Evan Chengf5bc7e52008-09-05 21:00:03 +00002414 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00002415 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002416
Owen Andersond41c7162008-09-06 01:11:01 +00002417 // MachineConstantPool wants an explicit alignment.
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002418 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00002419 if (Align == 0) {
2420 // Alignment of vector types. FIXME!
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00002421 Align = TD.getTypeAllocSize(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00002422 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002423
Dan Gohman8392f0c2008-09-30 01:21:32 +00002424 // x86-32 PIC requires a PIC base register for constant pools.
2425 unsigned PICBase = 0;
Chris Lattnera3260c02009-06-27 01:31:51 +00002426 unsigned char OpFlag = 0;
Chris Lattner21c29402009-07-10 21:00:45 +00002427 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattnerfef11d62009-07-09 04:39:06 +00002428 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohman87fb4e82010-07-07 16:29:44 +00002429 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00002430 } else if (Subtarget->isPICStyleGOT()) {
2431 OpFlag = X86II::MO_GOTOFF;
Dan Gohman87fb4e82010-07-07 16:29:44 +00002432 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00002433 } else if (Subtarget->isPICStyleRIPRel() &&
2434 TM.getCodeModel() == CodeModel::Small) {
2435 PICBase = X86::RIP;
Chris Lattnera3260c02009-06-27 01:31:51 +00002436 }
Dan Gohman8392f0c2008-09-30 01:21:32 +00002437
2438 // Create the load from the constant pool.
Dan Gohman39d82f92008-09-10 20:11:02 +00002439 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman9801ba42008-09-19 22:16:54 +00002440 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002441 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2442 TII.get(Opc), ResultReg),
Chris Lattnera3260c02009-06-27 01:31:51 +00002443 MCPOffset, PICBase, OpFlag);
Dan Gohman8392f0c2008-09-30 01:21:32 +00002444
Owen Anderson50288e32008-09-05 00:06:23 +00002445 return ResultReg;
2446}
2447
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002448unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002449 // Fail on dynamic allocas. At this point, getRegForValue has already
2450 // checked its CSE maps, so if we're here trying to handle a dynamic
2451 // alloca, we're not going to succeed. X86SelectAddress has a
2452 // check for dynamic allocas, because it's called directly from
2453 // various places, but TargetMaterializeAlloca also needs a check
2454 // in order to avoid recursion between getRegForValue,
2455 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohman87fb4e82010-07-07 16:29:44 +00002456 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002457 return 0;
2458
Dan Gohman39d82f92008-09-10 20:11:02 +00002459 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00002460 if (!X86SelectAddress(C, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +00002461 return 0;
2462 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
Craig Topper760b1342012-02-22 05:59:10 +00002463 const TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Dan Gohman39d82f92008-09-10 20:11:02 +00002464 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002465 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2466 TII.get(Opc), ResultReg), AM);
Dan Gohman39d82f92008-09-10 20:11:02 +00002467 return ResultReg;
2468}
2469
Eli Friedman406c4712011-04-27 22:41:55 +00002470unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2471 MVT VT;
2472 if (!isTypeLegal(CF->getType(), VT))
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002473 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00002474
2475 // Get opcode and regclass for the given zero.
2476 unsigned Opc = 0;
2477 const TargetRegisterClass *RC = NULL;
2478 switch (VT.SimpleTy) {
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002479 default: return 0;
Craig Topper490c45c2012-08-11 17:53:00 +00002480 case MVT::f32:
2481 if (X86ScalarSSEf32) {
2482 Opc = X86::FsFLD0SS;
2483 RC = &X86::FR32RegClass;
2484 } else {
2485 Opc = X86::LD_Fp032;
2486 RC = &X86::RFP32RegClass;
2487 }
2488 break;
2489 case MVT::f64:
2490 if (X86ScalarSSEf64) {
2491 Opc = X86::FsFLD0SD;
2492 RC = &X86::FR64RegClass;
2493 } else {
2494 Opc = X86::LD_Fp064;
2495 RC = &X86::RFP64RegClass;
2496 }
2497 break;
2498 case MVT::f80:
2499 // No f80 support yet.
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002500 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00002501 }
2502
2503 unsigned ResultReg = createResultReg(RC);
2504 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
2505 return ResultReg;
2506}
2507
2508
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002509bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2510 const LoadInst *LI) {
Chris Lattnereeba0c72010-09-05 02:18:34 +00002511 X86AddressMode AM;
2512 if (!X86SelectAddress(LI->getOperand(0), AM))
2513 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00002514
Craig Topper55406d92012-08-11 17:46:16 +00002515 const X86InstrInfo &XII = (const X86InstrInfo&)TII;
Wesley Peck527da1b2010-11-23 03:31:01 +00002516
Chris Lattnereeba0c72010-09-05 02:18:34 +00002517 unsigned Size = TD.getTypeAllocSize(LI->getType());
2518 unsigned Alignment = LI->getAlignment();
2519
2520 SmallVector<MachineOperand, 8> AddrOps;
2521 AM.getFullAddress(AddrOps);
Wesley Peck527da1b2010-11-23 03:31:01 +00002522
Chris Lattnereeba0c72010-09-05 02:18:34 +00002523 MachineInstr *Result =
2524 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
2525 if (Result == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00002526
Chris Lattner2d186572011-01-16 02:27:38 +00002527 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnereeba0c72010-09-05 02:18:34 +00002528 MI->eraseFromParent();
2529 return true;
2530}
2531
2532
Evan Cheng24422d42008-09-03 00:03:49 +00002533namespace llvm {
Bob Wilson3e6fa462012-08-03 04:06:28 +00002534 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
2535 const TargetLibraryInfo *libInfo) {
2536 return new X86FastISel(funcInfo, libInfo);
Evan Cheng24422d42008-09-03 00:03:49 +00002537 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00002538}