blob: a5bbf7202efc72c5447d9bf1e893500f2454d5ff [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"
Juergen Ributzka9969d3e2013-11-08 23:28:16 +000017#include "X86CallingConv.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "X86InstrBuilder.h"
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +000019#include "X86InstrInfo.h"
Craig Topperc6d4efa2014-03-19 06:53:25 +000020#include "X86MachineFunctionInfo.h"
Evan Cheng8f23ec92008-09-03 01:04:47 +000021#include "X86RegisterInfo.h"
22#include "X86Subtarget.h"
Dan Gohman49e19e92008-08-22 00:20:26 +000023#include "X86TargetMachine.h"
Juergen Ributzka454d3742014-06-13 00:45:11 +000024#include "llvm/Analysis/BranchProbabilityInfo.h"
Dan Gohmand7b5ce32010-07-10 09:00:22 +000025#include "llvm/CodeGen/Analysis.h"
Evan Cheng24422d42008-09-03 00:03:49 +000026#include "llvm/CodeGen/FastISel.h"
Dan Gohman87fb4e82010-07-07 16:29:44 +000027#include "llvm/CodeGen/FunctionLoweringInfo.h"
Owen Anderson50288e32008-09-05 00:06:23 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Evan Cheng6c8f55c2008-09-07 09:09:33 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson0673a8a2008-08-29 17:45:56 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000031#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/CallingConv.h"
33#include "llvm/IR/DerivedTypes.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000034#include "llvm/IR/GetElementPtrTypeIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/GlobalAlias.h"
36#include "llvm/IR/GlobalVariable.h"
37#include "llvm/IR/Instructions.h"
38#include "llvm/IR/IntrinsicInst.h"
39#include "llvm/IR/Operator.h"
Torok Edwin56d06592009-07-11 20:10:48 +000040#include "llvm/Support/ErrorHandling.h"
Evan Chengd10089a2010-01-27 00:00:57 +000041#include "llvm/Target/TargetOptions.h"
Evan Cheng24422d42008-09-03 00:03:49 +000042using namespace llvm;
43
Chris Lattnerd5ac9d82009-03-08 18:44:31 +000044namespace {
Wesley Peck527da1b2010-11-23 03:31:01 +000045
Craig Topper26696312014-03-18 07:27:13 +000046class X86FastISel final : public FastISel {
Evan Cheng24422d42008-09-03 00:03:49 +000047 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
48 /// make the right decision when generating code for different targets.
49 const X86Subtarget *Subtarget;
Evan Cheng6c8f55c2008-09-07 09:09:33 +000050
Wesley Peck527da1b2010-11-23 03:31:01 +000051 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
Evan Cheng6c8f55c2008-09-07 09:09:33 +000052 /// floating point ops.
53 /// When SSE is available, use it for f32 operations.
54 /// When SSE2 is available, use it for f64 operations.
55 bool X86ScalarSSEf64;
56 bool X86ScalarSSEf32;
57
Evan Chenga41ee292008-09-03 06:44:39 +000058public:
Bob Wilson3e6fa462012-08-03 04:06:28 +000059 explicit X86FastISel(FunctionLoweringInfo &funcInfo,
60 const TargetLibraryInfo *libInfo)
61 : FastISel(funcInfo, libInfo) {
Evan Cheng8f23ec92008-09-03 01:04:47 +000062 Subtarget = &TM.getSubtarget<X86Subtarget>();
Craig Topperb0c0f722012-01-10 06:54:16 +000063 X86ScalarSSEf64 = Subtarget->hasSSE2();
64 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng8f23ec92008-09-03 01:04:47 +000065 }
Evan Cheng24422d42008-09-03 00:03:49 +000066
Craig Topper2d9361e2014-03-09 07:44:38 +000067 bool TargetSelectInstruction(const Instruction *I) override;
Evan Cheng24422d42008-09-03 00:03:49 +000068
Eli Bendersky90dd3e72013-04-19 22:29:18 +000069 /// \brief The specified machine instr operand is a vreg, and that
Chris Lattnereeba0c72010-09-05 02:18:34 +000070 /// vreg is being provided by the specified load instruction. If possible,
71 /// try to fold the load as an operand to the instruction, returning true if
72 /// possible.
Craig Topper2d9361e2014-03-09 07:44:38 +000073 bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
74 const LoadInst *LI) override;
Wesley Peck527da1b2010-11-23 03:31:01 +000075
Craig Topper2d9361e2014-03-09 07:44:38 +000076 bool FastLowerArguments() override;
Juergen Ributzka23d43312014-07-15 06:35:47 +000077 bool FastLowerCall(CallLoweringInfo &CLI) override;
Juergen Ributzka3566c082014-07-15 06:35:50 +000078 bool FastLowerIntrinsicCall(const IntrinsicInst *II) override;
Chad Rosiera92ef4b2013-02-25 21:59:35 +000079
Dan Gohmandaef7f42008-08-19 21:45:35 +000080#include "X86GenFastISel.inc"
Evan Chenga41ee292008-09-03 06:44:39 +000081
82private:
Dan Gohmanbcaf6812010-04-15 01:51:59 +000083 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Wesley Peck527da1b2010-11-23 03:31:01 +000084
Juergen Ributzka349777d2014-06-12 23:27:57 +000085 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, MachineMemOperand *MMO,
86 unsigned &ResultReg);
Evan Chengf5bc7e52008-09-05 21:00:03 +000087
Craig Topper4f55b0e2013-07-17 05:57:45 +000088 bool X86FastEmitStore(EVT VT, const Value *Val, const X86AddressMode &AM,
Juergen Ributzka349777d2014-06-12 23:27:57 +000089 MachineMemOperand *MMO = nullptr, bool Aligned = false);
90 bool X86FastEmitStore(EVT VT, unsigned ValReg, bool ValIsKill,
91 const X86AddressMode &AM,
92 MachineMemOperand *MMO = nullptr, bool Aligned = false);
Evan Cheng6500d172008-09-08 06:35:17 +000093
Owen Anderson53aa7a92009-08-10 22:56:29 +000094 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng6500d172008-09-08 06:35:17 +000095 unsigned &ResultReg);
Wesley Peck527da1b2010-11-23 03:31:01 +000096
Dan Gohmanbcaf6812010-04-15 01:51:59 +000097 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
98 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman39d82f92008-09-10 20:11:02 +000099
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000100 bool X86SelectLoad(const Instruction *I);
Wesley Peck527da1b2010-11-23 03:31:01 +0000101
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000102 bool X86SelectStore(const Instruction *I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000103
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000104 bool X86SelectRet(const Instruction *I);
105
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000106 bool X86SelectCmp(const Instruction *I);
Dan Gohmana5753b32008-09-05 01:06:14 +0000107
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000108 bool X86SelectZExt(const Instruction *I);
Dan Gohmana5753b32008-09-05 01:06:14 +0000109
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000110 bool X86SelectBranch(const Instruction *I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +0000111
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000112 bool X86SelectShift(const Instruction *I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +0000113
Eli Bendersky24a36eb2013-04-17 20:10:13 +0000114 bool X86SelectDivRem(const Instruction *I);
115
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000116 bool X86FastEmitCMoveSelect(MVT RetVT, const Instruction *I);
Juergen Ributzka6ef06f92014-06-23 21:55:36 +0000117
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000118 bool X86FastEmitSSESelect(MVT RetVT, const Instruction *I);
Juergen Ributzka21d56082014-06-23 21:55:40 +0000119
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000120 bool X86FastEmitPseudoSelect(MVT RetVT, const Instruction *I);
Juergen Ributzkaaed5c962014-06-23 21:55:44 +0000121
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000122 bool X86SelectSelect(const Instruction *I);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000123
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000124 bool X86SelectTrunc(const Instruction *I);
Wesley Peck527da1b2010-11-23 03:31:01 +0000125
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000126 bool X86SelectFPExt(const Instruction *I);
127 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohmanbf646f22008-09-10 21:02:08 +0000128
Dan Gohman3691d502008-09-25 15:24:26 +0000129 const X86InstrInfo *getInstrInfo() const {
Eric Christopherd9134482014-08-04 21:25:23 +0000130 return getTargetMachine()->getSubtargetImpl()->getInstrInfo();
Dan Gohman007a6bb2008-09-26 19:15:30 +0000131 }
132 const X86TargetMachine *getTargetMachine() const {
133 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman3691d502008-09-25 15:24:26 +0000134 }
135
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000136 bool handleConstantAddresses(const Value *V, X86AddressMode &AM);
137
Craig Topper2d9361e2014-03-09 07:44:38 +0000138 unsigned TargetMaterializeConstant(const Constant *C) override;
Dan Gohman39d82f92008-09-10 20:11:02 +0000139
Craig Topper2d9361e2014-03-09 07:44:38 +0000140 unsigned TargetMaterializeAlloca(const AllocaInst *C) override;
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000141
Craig Topper2d9361e2014-03-09 07:44:38 +0000142 unsigned TargetMaterializeFloatZero(const ConstantFP *CF) override;
Eli Friedman406c4712011-04-27 22:41:55 +0000143
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000144 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
145 /// computed in an SSE register, not on the X87 floating point stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000146 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson9f944592009-08-11 20:47:22 +0000147 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
148 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000149 }
150
Chris Lattner229907c2011-07-18 04:54:35 +0000151 bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
Eli Friedmanbcc69142011-04-27 01:45:07 +0000152
Eli Friedman60afcc22011-05-20 22:21:04 +0000153 bool IsMemcpySmall(uint64_t Len);
154
Eli Friedmanbcc69142011-04-27 01:45:07 +0000155 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
156 X86AddressMode SrcAM, uint64_t Len);
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000157
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000158 bool foldX86XALUIntrinsic(X86::CondCode &CC, const Instruction *I,
159 const Value *Cond);
Evan Cheng24422d42008-09-03 00:03:49 +0000160};
Wesley Peck527da1b2010-11-23 03:31:01 +0000161
Chris Lattnerd5ac9d82009-03-08 18:44:31 +0000162} // end anonymous namespace.
Dan Gohmand58f3e32008-08-28 23:21:34 +0000163
Juergen Ributzkaaa602092014-06-17 21:55:43 +0000164static CmpInst::Predicate optimizeCmpPredicate(const CmpInst *CI) {
165 // If both operands are the same, then try to optimize or fold the cmp.
166 CmpInst::Predicate Predicate = CI->getPredicate();
167 if (CI->getOperand(0) != CI->getOperand(1))
168 return Predicate;
169
170 switch (Predicate) {
171 default: llvm_unreachable("Invalid predicate!");
172 case CmpInst::FCMP_FALSE: Predicate = CmpInst::FCMP_FALSE; break;
173 case CmpInst::FCMP_OEQ: Predicate = CmpInst::FCMP_ORD; break;
174 case CmpInst::FCMP_OGT: Predicate = CmpInst::FCMP_FALSE; break;
175 case CmpInst::FCMP_OGE: Predicate = CmpInst::FCMP_ORD; break;
176 case CmpInst::FCMP_OLT: Predicate = CmpInst::FCMP_FALSE; break;
177 case CmpInst::FCMP_OLE: Predicate = CmpInst::FCMP_ORD; break;
178 case CmpInst::FCMP_ONE: Predicate = CmpInst::FCMP_FALSE; break;
179 case CmpInst::FCMP_ORD: Predicate = CmpInst::FCMP_ORD; break;
180 case CmpInst::FCMP_UNO: Predicate = CmpInst::FCMP_UNO; break;
181 case CmpInst::FCMP_UEQ: Predicate = CmpInst::FCMP_TRUE; break;
182 case CmpInst::FCMP_UGT: Predicate = CmpInst::FCMP_UNO; break;
183 case CmpInst::FCMP_UGE: Predicate = CmpInst::FCMP_TRUE; break;
184 case CmpInst::FCMP_ULT: Predicate = CmpInst::FCMP_UNO; break;
185 case CmpInst::FCMP_ULE: Predicate = CmpInst::FCMP_TRUE; break;
186 case CmpInst::FCMP_UNE: Predicate = CmpInst::FCMP_UNO; break;
187 case CmpInst::FCMP_TRUE: Predicate = CmpInst::FCMP_TRUE; break;
188
189 case CmpInst::ICMP_EQ: Predicate = CmpInst::FCMP_TRUE; break;
190 case CmpInst::ICMP_NE: Predicate = CmpInst::FCMP_FALSE; break;
191 case CmpInst::ICMP_UGT: Predicate = CmpInst::FCMP_FALSE; break;
192 case CmpInst::ICMP_UGE: Predicate = CmpInst::FCMP_TRUE; break;
193 case CmpInst::ICMP_ULT: Predicate = CmpInst::FCMP_FALSE; break;
194 case CmpInst::ICMP_ULE: Predicate = CmpInst::FCMP_TRUE; break;
195 case CmpInst::ICMP_SGT: Predicate = CmpInst::FCMP_FALSE; break;
196 case CmpInst::ICMP_SGE: Predicate = CmpInst::FCMP_TRUE; break;
197 case CmpInst::ICMP_SLT: Predicate = CmpInst::FCMP_FALSE; break;
198 case CmpInst::ICMP_SLE: Predicate = CmpInst::FCMP_TRUE; break;
199 }
200
201 return Predicate;
202}
203
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +0000204static std::pair<X86::CondCode, bool>
Craig Topper9f62d802014-06-27 05:18:21 +0000205getX86ConditionCode(CmpInst::Predicate Predicate) {
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +0000206 X86::CondCode CC = X86::COND_INVALID;
207 bool NeedSwap = false;
208 switch (Predicate) {
209 default: break;
210 // Floating-point Predicates
211 case CmpInst::FCMP_UEQ: CC = X86::COND_E; break;
212 case CmpInst::FCMP_OLT: NeedSwap = true; // fall-through
213 case CmpInst::FCMP_OGT: CC = X86::COND_A; break;
214 case CmpInst::FCMP_OLE: NeedSwap = true; // fall-through
215 case CmpInst::FCMP_OGE: CC = X86::COND_AE; break;
216 case CmpInst::FCMP_UGT: NeedSwap = true; // fall-through
217 case CmpInst::FCMP_ULT: CC = X86::COND_B; break;
218 case CmpInst::FCMP_UGE: NeedSwap = true; // fall-through
219 case CmpInst::FCMP_ULE: CC = X86::COND_BE; break;
220 case CmpInst::FCMP_ONE: CC = X86::COND_NE; break;
221 case CmpInst::FCMP_UNO: CC = X86::COND_P; break;
222 case CmpInst::FCMP_ORD: CC = X86::COND_NP; break;
223 case CmpInst::FCMP_OEQ: // fall-through
224 case CmpInst::FCMP_UNE: CC = X86::COND_INVALID; break;
225
226 // Integer Predicates
227 case CmpInst::ICMP_EQ: CC = X86::COND_E; break;
228 case CmpInst::ICMP_NE: CC = X86::COND_NE; break;
229 case CmpInst::ICMP_UGT: CC = X86::COND_A; break;
230 case CmpInst::ICMP_UGE: CC = X86::COND_AE; break;
231 case CmpInst::ICMP_ULT: CC = X86::COND_B; break;
232 case CmpInst::ICMP_ULE: CC = X86::COND_BE; break;
233 case CmpInst::ICMP_SGT: CC = X86::COND_G; break;
234 case CmpInst::ICMP_SGE: CC = X86::COND_GE; break;
235 case CmpInst::ICMP_SLT: CC = X86::COND_L; break;
236 case CmpInst::ICMP_SLE: CC = X86::COND_LE; break;
237 }
238
239 return std::make_pair(CC, NeedSwap);
240}
241
Juergen Ributzka21d56082014-06-23 21:55:40 +0000242static std::pair<unsigned, bool>
Juergen Ributzka345589e2014-06-27 17:16:34 +0000243getX86SSEConditionCode(CmpInst::Predicate Predicate) {
Juergen Ributzka21d56082014-06-23 21:55:40 +0000244 unsigned CC;
245 bool NeedSwap = false;
246
247 // SSE Condition code mapping:
248 // 0 - EQ
249 // 1 - LT
250 // 2 - LE
251 // 3 - UNORD
252 // 4 - NEQ
253 // 5 - NLT
254 // 6 - NLE
255 // 7 - ORD
256 switch (Predicate) {
257 default: llvm_unreachable("Unexpected predicate");
258 case CmpInst::FCMP_OEQ: CC = 0; break;
259 case CmpInst::FCMP_OGT: NeedSwap = true; // fall-through
260 case CmpInst::FCMP_OLT: CC = 1; break;
261 case CmpInst::FCMP_OGE: NeedSwap = true; // fall-through
262 case CmpInst::FCMP_OLE: CC = 2; break;
263 case CmpInst::FCMP_UNO: CC = 3; break;
264 case CmpInst::FCMP_UNE: CC = 4; break;
265 case CmpInst::FCMP_ULE: NeedSwap = true; // fall-through
266 case CmpInst::FCMP_UGE: CC = 5; break;
267 case CmpInst::FCMP_ULT: NeedSwap = true; // fall-through
268 case CmpInst::FCMP_UGT: CC = 6; break;
269 case CmpInst::FCMP_ORD: CC = 7; break;
270 case CmpInst::FCMP_UEQ:
271 case CmpInst::FCMP_ONE: CC = 8; break;
272 }
273
274 return std::make_pair(CC, NeedSwap);
275}
276
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000277/// \brief Check if it is possible to fold the condition from the XALU intrinsic
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000278/// into the user. The condition code will only be updated on success.
279bool X86FastISel::foldX86XALUIntrinsic(X86::CondCode &CC, const Instruction *I,
280 const Value *Cond) {
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000281 if (!isa<ExtractValueInst>(Cond))
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000282 return false;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000283
284 const auto *EV = cast<ExtractValueInst>(Cond);
285 if (!isa<IntrinsicInst>(EV->getAggregateOperand()))
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000286 return false;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000287
288 const auto *II = cast<IntrinsicInst>(EV->getAggregateOperand());
289 MVT RetVT;
290 const Function *Callee = II->getCalledFunction();
291 Type *RetTy =
292 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(0U);
293 if (!isTypeLegal(RetTy, RetVT))
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000294 return false;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000295
296 if (RetVT != MVT::i32 && RetVT != MVT::i64)
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000297 return false;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000298
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000299 X86::CondCode TmpCC;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000300 switch (II->getIntrinsicID()) {
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000301 default: return false;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000302 case Intrinsic::sadd_with_overflow:
303 case Intrinsic::ssub_with_overflow:
304 case Intrinsic::smul_with_overflow:
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000305 case Intrinsic::umul_with_overflow: TmpCC = X86::COND_O; break;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000306 case Intrinsic::uadd_with_overflow:
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000307 case Intrinsic::usub_with_overflow: TmpCC = X86::COND_B; break;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000308 }
309
310 // Check if both instructions are in the same basic block.
311 if (II->getParent() != I->getParent())
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000312 return false;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000313
314 // Make sure nothing is in the way
315 BasicBlock::const_iterator Start = I;
316 BasicBlock::const_iterator End = II;
317 for (auto Itr = std::prev(Start); Itr != End; --Itr) {
318 // We only expect extractvalue instructions between the intrinsic and the
319 // instruction to be selected.
320 if (!isa<ExtractValueInst>(Itr))
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000321 return false;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000322
323 // Check that the extractvalue operand comes from the intrinsic.
324 const auto *EVI = cast<ExtractValueInst>(Itr);
325 if (EVI->getAggregateOperand() != II)
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000326 return false;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000327 }
328
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +0000329 CC = TmpCC;
330 return true;
Juergen Ributzkac010ddb2014-06-25 22:17:23 +0000331}
332
Chris Lattner229907c2011-07-18 04:54:35 +0000333bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000334 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
335 if (evt == MVT::Other || !evt.isSimple())
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000336 // Unhandled type. Halt "fast" selection and bail.
337 return false;
Duncan Sandsf5dda012010-11-03 11:35:31 +0000338
339 VT = evt.getSimpleVT();
Dan Gohman50331362008-09-30 00:48:39 +0000340 // For now, require SSE/SSE2 for performing floating-point operations,
341 // since x87 requires additional work.
Owen Anderson9f944592009-08-11 20:47:22 +0000342 if (VT == MVT::f64 && !X86ScalarSSEf64)
Craig Topper490c45c2012-08-11 17:53:00 +0000343 return false;
Owen Anderson9f944592009-08-11 20:47:22 +0000344 if (VT == MVT::f32 && !X86ScalarSSEf32)
Craig Topper490c45c2012-08-11 17:53:00 +0000345 return false;
Dan Gohman50331362008-09-30 00:48:39 +0000346 // Similarly, no f80 support yet.
Owen Anderson9f944592009-08-11 20:47:22 +0000347 if (VT == MVT::f80)
Dan Gohman50331362008-09-30 00:48:39 +0000348 return false;
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000349 // We only handle legal types. For example, on x86-32 the instruction
350 // selector contains all of the 64-bit instructions from x86-64,
351 // under the assumption that i64 won't be used if the target doesn't
352 // support it.
Owen Anderson9f944592009-08-11 20:47:22 +0000353 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000354}
355
356#include "X86GenCallingConv.inc"
357
Evan Chengf5bc7e52008-09-05 21:00:03 +0000358/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000359/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000360/// Return true and the result register by reference if it is possible.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000361bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000362 MachineMemOperand *MMO, unsigned &ResultReg) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000363 // Get opcode and regclass of the output for the given load instruction.
364 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000365 const TargetRegisterClass *RC = nullptr;
Owen Anderson9f944592009-08-11 20:47:22 +0000366 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000367 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000368 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +0000369 case MVT::i8:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000370 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +0000371 RC = &X86::GR8RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000372 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000373 case MVT::i16:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000374 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +0000375 RC = &X86::GR16RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000376 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000377 case MVT::i32:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000378 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +0000379 RC = &X86::GR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000380 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000381 case MVT::i64:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000382 // Must be in x86-64 mode.
383 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +0000384 RC = &X86::GR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000385 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000386 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000387 if (X86ScalarSSEf32) {
388 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +0000389 RC = &X86::FR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000390 } else {
391 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +0000392 RC = &X86::RFP32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000393 }
394 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000395 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000396 if (X86ScalarSSEf64) {
397 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +0000398 RC = &X86::FR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000399 } else {
400 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +0000401 RC = &X86::RFP64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000402 }
403 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000404 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +0000405 // No f80 support yet.
406 return false;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000407 }
408
409 ResultReg = createResultReg(RC);
Juergen Ributzka349777d2014-06-12 23:27:57 +0000410 MachineInstrBuilder MIB =
411 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
412 addFullAddress(MIB, AM);
413 if (MMO)
414 MIB->addMemOperand(*FuncInfo.MF, MMO);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000415 return true;
416}
417
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000418/// X86FastEmitStore - Emit a machine instruction to store a value Val of
419/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
420/// and a displacement offset, or a GlobalAddress,
Evan Chengf5bc7e52008-09-05 21:00:03 +0000421/// i.e. V. Return true if it is possible.
Juergen Ributzka349777d2014-06-12 23:27:57 +0000422bool X86FastISel::X86FastEmitStore(EVT VT, unsigned ValReg, bool ValIsKill,
423 const X86AddressMode &AM,
424 MachineMemOperand *MMO, bool Aligned) {
Dan Gohman8f658ba2008-09-08 16:31:35 +0000425 // Get opcode and regclass of the output for the given store instruction.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000426 unsigned Opc = 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000427 switch (VT.getSimpleVT().SimpleTy) {
428 case MVT::f80: // No f80 support yet.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000429 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000430 case MVT::i1: {
431 // Mask out all but lowest bit.
Craig Topperabadc662012-04-20 06:31:50 +0000432 unsigned AndResult = createResultReg(&X86::GR8RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000433 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000434 TII.get(X86::AND8ri), AndResult)
435 .addReg(ValReg, getKillRegState(ValIsKill)).addImm(1);
Craig Topper4f55b0e2013-07-17 05:57:45 +0000436 ValReg = AndResult;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000437 }
438 // FALLTHROUGH, handling i1 as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000439 case MVT::i8: Opc = X86::MOV8mr; break;
440 case MVT::i16: Opc = X86::MOV16mr; break;
441 case MVT::i32: Opc = X86::MOV32mr; break;
442 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
443 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000444 Opc = X86ScalarSSEf32 ?
445 (Subtarget->hasAVX() ? X86::VMOVSSmr : X86::MOVSSmr) : X86::ST_Fp32m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000446 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000447 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000448 Opc = X86ScalarSSEf64 ?
449 (Subtarget->hasAVX() ? X86::VMOVSDmr : X86::MOVSDmr) : X86::ST_Fp64m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000450 break;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000451 case MVT::v4f32:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000452 if (Aligned)
Craig Topper55475d42013-07-17 06:58:23 +0000453 Opc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000454 else
Craig Topper55475d42013-07-17 06:58:23 +0000455 Opc = Subtarget->hasAVX() ? X86::VMOVUPSmr : X86::MOVUPSmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000456 break;
457 case MVT::v2f64:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000458 if (Aligned)
Craig Topperad1fff92013-07-18 07:16:44 +0000459 Opc = Subtarget->hasAVX() ? X86::VMOVAPDmr : X86::MOVAPDmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000460 else
Craig Topperad1fff92013-07-18 07:16:44 +0000461 Opc = Subtarget->hasAVX() ? X86::VMOVUPDmr : X86::MOVUPDmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000462 break;
463 case MVT::v4i32:
464 case MVT::v2i64:
465 case MVT::v8i16:
466 case MVT::v16i8:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000467 if (Aligned)
Craig Topper55475d42013-07-17 06:58:23 +0000468 Opc = Subtarget->hasAVX() ? X86::VMOVDQAmr : X86::MOVDQAmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000469 else
Craig Topper55475d42013-07-17 06:58:23 +0000470 Opc = Subtarget->hasAVX() ? X86::VMOVDQUmr : X86::MOVDQUmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000471 break;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000472 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000473
Juergen Ributzka349777d2014-06-12 23:27:57 +0000474 MachineInstrBuilder MIB =
475 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
476 addFullAddress(MIB, AM).addReg(ValReg, getKillRegState(ValIsKill));
477 if (MMO)
478 MIB->addMemOperand(*FuncInfo.MF, MMO);
479
Evan Chengf5bc7e52008-09-05 21:00:03 +0000480 return true;
481}
482
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000483bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000484 const X86AddressMode &AM,
485 MachineMemOperand *MMO, bool Aligned) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000486 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +0000487 if (isa<ConstantPointerNull>(Val))
Rafael Espindolaea09c592014-02-18 22:05:46 +0000488 Val = Constant::getNullValue(DL.getIntPtrType(Val->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +0000489
Chris Lattner3ba29352008-10-15 05:30:52 +0000490 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000491 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000492 unsigned Opc = 0;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000493 bool Signed = true;
Owen Anderson9f944592009-08-11 20:47:22 +0000494 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000495 default: break;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000496 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000497 case MVT::i8: Opc = X86::MOV8mi; break;
498 case MVT::i16: Opc = X86::MOV16mi; break;
499 case MVT::i32: Opc = X86::MOV32mi; break;
500 case MVT::i64:
Chris Lattner3ba29352008-10-15 05:30:52 +0000501 // Must be a 32-bit sign extended value.
Jakub Staszak11d1aee2012-11-15 19:05:23 +0000502 if (isInt<32>(CI->getSExtValue()))
Chris Lattner3ba29352008-10-15 05:30:52 +0000503 Opc = X86::MOV64mi32;
504 break;
505 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000506
Chris Lattner3ba29352008-10-15 05:30:52 +0000507 if (Opc) {
Juergen Ributzka349777d2014-06-12 23:27:57 +0000508 MachineInstrBuilder MIB =
509 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
510 addFullAddress(MIB, AM).addImm(Signed ? (uint64_t) CI->getSExtValue()
511 : CI->getZExtValue());
512 if (MMO)
513 MIB->addMemOperand(*FuncInfo.MF, MMO);
Chris Lattner3ba29352008-10-15 05:30:52 +0000514 return true;
515 }
516 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000517
Chris Lattner3ba29352008-10-15 05:30:52 +0000518 unsigned ValReg = getRegForValue(Val);
519 if (ValReg == 0)
Wesley Peck527da1b2010-11-23 03:31:01 +0000520 return false;
521
Juergen Ributzka349777d2014-06-12 23:27:57 +0000522 bool ValKill = hasTrivialKill(Val);
523 return X86FastEmitStore(VT, ValReg, ValKill, AM, MMO, Aligned);
Chris Lattner3ba29352008-10-15 05:30:52 +0000524}
525
Evan Cheng6500d172008-09-08 06:35:17 +0000526/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
527/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
528/// ISD::SIGN_EXTEND).
Owen Anderson53aa7a92009-08-10 22:56:29 +0000529bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
530 unsigned Src, EVT SrcVT,
Evan Cheng6500d172008-09-08 06:35:17 +0000531 unsigned &ResultReg) {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000532 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
533 Src, /*TODO: Kill=*/false);
Jakub Staszak701cc972013-02-14 21:50:09 +0000534 if (RR == 0)
Owen Anderson453564b2008-09-11 19:44:55 +0000535 return false;
Jakub Staszak701cc972013-02-14 21:50:09 +0000536
537 ResultReg = RR;
538 return true;
Evan Cheng6500d172008-09-08 06:35:17 +0000539}
540
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000541bool X86FastISel::handleConstantAddresses(const Value *V, X86AddressMode &AM) {
542 // Handle constant address.
543 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
544 // Can't handle alternate code models yet.
545 if (TM.getCodeModel() != CodeModel::Small)
546 return false;
547
548 // Can't handle TLS yet.
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000549 if (GV->isThreadLocal())
550 return false;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000551
552 // RIP-relative addresses can't have additional register operands, so if
553 // we've already folded stuff into the addressing mode, just force the
554 // global value into its own register, which we can use as the basereg.
555 if (!Subtarget->isPICStyleRIPRel() ||
556 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
557 // Okay, we've committed to selecting this global. Set up the address.
558 AM.GV = GV;
559
560 // Allow the subtarget to classify the global.
561 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
562
563 // If this reference is relative to the pic base, set it now.
564 if (isGlobalRelativeToPICBase(GVFlags)) {
565 // FIXME: How do we know Base.Reg is free??
566 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
567 }
568
569 // Unless the ABI requires an extra load, return a direct reference to
570 // the global.
571 if (!isGlobalStubReference(GVFlags)) {
572 if (Subtarget->isPICStyleRIPRel()) {
573 // Use rip-relative addressing if we can. Above we verified that the
574 // base and index registers are unused.
575 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
576 AM.Base.Reg = X86::RIP;
577 }
578 AM.GVOpFlags = GVFlags;
579 return true;
580 }
581
582 // Ok, we need to do a load from a stub. If we've already loaded from
583 // this stub, reuse the loaded pointer, otherwise emit the load now.
584 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
585 unsigned LoadReg;
586 if (I != LocalValueMap.end() && I->second != 0) {
587 LoadReg = I->second;
588 } else {
589 // Issue load from stub.
590 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000591 const TargetRegisterClass *RC = nullptr;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000592 X86AddressMode StubAM;
593 StubAM.Base.Reg = AM.Base.Reg;
594 StubAM.GV = GV;
595 StubAM.GVOpFlags = GVFlags;
596
597 // Prepare for inserting code in the local-value area.
598 SavePoint SaveInsertPt = enterLocalValueArea();
599
600 if (TLI.getPointerTy() == MVT::i64) {
601 Opc = X86::MOV64rm;
602 RC = &X86::GR64RegClass;
603
604 if (Subtarget->isPICStyleRIPRel())
605 StubAM.Base.Reg = X86::RIP;
606 } else {
607 Opc = X86::MOV32rm;
608 RC = &X86::GR32RegClass;
609 }
610
611 LoadReg = createResultReg(RC);
612 MachineInstrBuilder LoadMI =
Rafael Espindolaea09c592014-02-18 22:05:46 +0000613 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), LoadReg);
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000614 addFullAddress(LoadMI, StubAM);
615
616 // Ok, back to normal mode.
617 leaveLocalValueArea(SaveInsertPt);
618
619 // Prevent loading GV stub multiple times in same MBB.
620 LocalValueMap[V] = LoadReg;
621 }
622
623 // Now construct the final address. Note that the Disp, Scale,
624 // and Index values may already be set here.
625 AM.Base.Reg = LoadReg;
Craig Topper062a2ba2014-04-25 05:30:21 +0000626 AM.GV = nullptr;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000627 return true;
628 }
629 }
630
631 // If all else fails, try to materialize the value in a register.
632 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
633 if (AM.Base.Reg == 0) {
634 AM.Base.Reg = getRegForValue(V);
635 return AM.Base.Reg != 0;
636 }
637 if (AM.IndexReg == 0) {
638 assert(AM.Scale == 1 && "Scale with no index!");
639 AM.IndexReg = getRegForValue(V);
640 return AM.IndexReg != 0;
641 }
642 }
643
644 return false;
645}
646
Dan Gohman39d82f92008-09-10 20:11:02 +0000647/// X86SelectAddress - Attempt to fill in an address from the given value.
648///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000649bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000650 SmallVector<const Value *, 32> GEPs;
Bill Wendling585a9012013-09-24 00:13:08 +0000651redo_gep:
Craig Topper062a2ba2014-04-25 05:30:21 +0000652 const User *U = nullptr;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000653 unsigned Opcode = Instruction::UserOp1;
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000654 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanaf4903d2010-06-18 20:44:47 +0000655 // Don't walk into other basic blocks; it's possible we haven't
656 // visited them yet, so the instructions may not yet be assigned
657 // virtual registers.
Dan Gohmanaeb5e662010-11-16 22:43:23 +0000658 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
659 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
660 Opcode = I->getOpcode();
661 U = I;
662 }
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000663 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman6e005fd2008-09-18 23:23:44 +0000664 Opcode = C->getOpcode();
665 U = C;
666 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000667
Chris Lattner229907c2011-07-18 04:54:35 +0000668 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
Chris Lattner874c92b2010-06-15 19:08:40 +0000669 if (Ty->getAddressSpace() > 255)
Dan Gohmana46d6072010-06-18 20:45:41 +0000670 // Fast instruction selection doesn't support the special
671 // address spaces.
Chris Lattner874c92b2010-06-15 19:08:40 +0000672 return false;
673
Dan Gohman6e005fd2008-09-18 23:23:44 +0000674 switch (Opcode) {
675 default: break;
676 case Instruction::BitCast:
677 // Look past bitcasts.
Chris Lattner8212d372009-07-10 05:33:42 +0000678 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman6e005fd2008-09-18 23:23:44 +0000679
680 case Instruction::IntToPtr:
681 // Look past no-op inttoptrs.
682 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000683 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000684 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000685
686 case Instruction::PtrToInt:
687 // Look past no-op ptrtoints.
688 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000689 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000690 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000691
692 case Instruction::Alloca: {
693 // Do static allocas.
694 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman87fb4e82010-07-07 16:29:44 +0000695 DenseMap<const AllocaInst*, int>::iterator SI =
696 FuncInfo.StaticAllocaMap.find(A);
697 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman007a6bb2008-09-26 19:15:30 +0000698 AM.BaseType = X86AddressMode::FrameIndexBase;
699 AM.Base.FrameIndex = SI->second;
700 return true;
701 }
702 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000703 }
704
705 case Instruction::Add: {
706 // Adds of constants are common and easy enough.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000707 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman2564b902008-09-26 20:04:15 +0000708 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
709 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000710 if (isInt<32>(Disp)) {
Dan Gohman2564b902008-09-26 20:04:15 +0000711 AM.Disp = (uint32_t)Disp;
Chris Lattner8212d372009-07-10 05:33:42 +0000712 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman2564b902008-09-26 20:04:15 +0000713 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000714 }
Dan Gohman6e005fd2008-09-18 23:23:44 +0000715 break;
716 }
717
718 case Instruction::GetElementPtr: {
Chris Lattner795667b2010-03-04 19:54:45 +0000719 X86AddressMode SavedAM = AM;
720
Dan Gohman6e005fd2008-09-18 23:23:44 +0000721 // Pattern-match simple GEPs.
Dan Gohman2564b902008-09-26 20:04:15 +0000722 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000723 unsigned IndexReg = AM.IndexReg;
724 unsigned Scale = AM.Scale;
725 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohman4c315242008-12-08 07:57:47 +0000726 // Iterate through the indices, folding what we can. Constants can be
727 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000728 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman6e005fd2008-09-18 23:23:44 +0000729 i != e; ++i, ++GTI) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000730 const Value *Op = *i;
Chris Lattner229907c2011-07-18 04:54:35 +0000731 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000732 const StructLayout *SL = DL.getStructLayout(STy);
Chris Lattner4b026b92011-04-17 17:05:12 +0000733 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
734 continue;
735 }
Eric Christopher0713a9d2011-06-08 23:55:35 +0000736
Chris Lattner4b026b92011-04-17 17:05:12 +0000737 // A array/variable index is always of the form i*S where S is the
738 // constant scale size. See if we can push the scale into immediates.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000739 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Chris Lattner4b026b92011-04-17 17:05:12 +0000740 for (;;) {
741 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
742 // Constant-offset addressing.
743 Disp += CI->getSExtValue() * S;
744 break;
Dan Gohmanc1783b32011-03-22 00:04:35 +0000745 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000746 if (canFoldAddIntoGEP(U, Op)) {
747 // A compatible add with a constant operand. Fold the constant.
Chris Lattner4b026b92011-04-17 17:05:12 +0000748 ConstantInt *CI =
749 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
750 Disp += CI->getSExtValue() * S;
751 // Iterate on the other operand.
752 Op = cast<AddOperator>(Op)->getOperand(0);
753 continue;
754 }
755 if (IndexReg == 0 &&
756 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
757 (S == 1 || S == 2 || S == 4 || S == 8)) {
758 // Scaled-index addressing.
759 Scale = S;
760 IndexReg = getRegForGEPIndex(Op).first;
761 if (IndexReg == 0)
762 return false;
763 break;
764 }
765 // Unsupported.
766 goto unsupported_gep;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000767 }
768 }
Bill Wendling585a9012013-09-24 00:13:08 +0000769
Dan Gohman2564b902008-09-26 20:04:15 +0000770 // Check for displacement overflow.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000771 if (!isInt<32>(Disp))
Dan Gohman2564b902008-09-26 20:04:15 +0000772 break;
Bill Wendling585a9012013-09-24 00:13:08 +0000773
Dan Gohman6e005fd2008-09-18 23:23:44 +0000774 AM.IndexReg = IndexReg;
775 AM.Scale = Scale;
Dan Gohman2564b902008-09-26 20:04:15 +0000776 AM.Disp = (uint32_t)Disp;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000777 GEPs.push_back(V);
Bill Wendling585a9012013-09-24 00:13:08 +0000778
779 if (const GetElementPtrInst *GEP =
780 dyn_cast<GetElementPtrInst>(U->getOperand(0))) {
781 // Ok, the GEP indices were covered by constant-offset and scaled-index
782 // addressing. Update the address state and move on to examining the base.
783 V = GEP;
784 goto redo_gep;
785 } else if (X86SelectAddress(U->getOperand(0), AM)) {
Chris Lattner6ce8e242010-03-04 19:48:19 +0000786 return true;
Bill Wendling585a9012013-09-24 00:13:08 +0000787 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000788
Chris Lattner4b026b92011-04-17 17:05:12 +0000789 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner6ce8e242010-03-04 19:48:19 +0000790 // our address and just match the value instead of completely failing.
791 AM = SavedAM;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000792
793 for (SmallVectorImpl<const Value *>::reverse_iterator
794 I = GEPs.rbegin(), E = GEPs.rend(); I != E; ++I)
795 if (handleConstantAddresses(*I, AM))
796 return true;
797
798 return false;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000799 unsupported_gep:
800 // Ok, the GEP indices weren't all covered.
801 break;
802 }
803 }
804
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000805 return handleConstantAddresses(V, AM);
Dan Gohman39d82f92008-09-10 20:11:02 +0000806}
807
Chris Lattner8212d372009-07-10 05:33:42 +0000808/// X86SelectCallAddress - Attempt to fill in an address from the given value.
809///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000810bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000811 const User *U = nullptr;
Chris Lattner8212d372009-07-10 05:33:42 +0000812 unsigned Opcode = Instruction::UserOp1;
Quentin Colombet778dba12013-10-14 22:32:09 +0000813 const Instruction *I = dyn_cast<Instruction>(V);
Quentin Colombetf34568b2013-10-22 21:29:08 +0000814 // Record if the value is defined in the same basic block.
815 //
816 // This information is crucial to know whether or not folding an
817 // operand is valid.
818 // Indeed, FastISel generates or reuses a virtual register for all
819 // operands of all instructions it selects. Obviously, the definition and
820 // its uses must use the same virtual register otherwise the produced
821 // code is incorrect.
822 // Before instruction selection, FunctionLoweringInfo::set sets the virtual
823 // registers for values that are alive across basic blocks. This ensures
824 // that the values are consistently set between across basic block, even
825 // if different instruction selection mechanisms are used (e.g., a mix of
826 // SDISel and FastISel).
827 // For values local to a basic block, the instruction selection process
828 // generates these virtual registers with whatever method is appropriate
829 // for its needs. In particular, FastISel and SDISel do not share the way
830 // local virtual registers are set.
831 // Therefore, this is impossible (or at least unsafe) to share values
832 // between basic blocks unless they use the same instruction selection
833 // method, which is not guarantee for X86.
834 // Moreover, things like hasOneUse could not be used accurately, if we
835 // allow to reference values across basic blocks whereas they are not
836 // alive across basic blocks initially.
Quentin Colombet778dba12013-10-14 22:32:09 +0000837 bool InMBB = true;
838 if (I) {
Chris Lattner8212d372009-07-10 05:33:42 +0000839 Opcode = I->getOpcode();
840 U = I;
Quentin Colombet778dba12013-10-14 22:32:09 +0000841 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000842 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000843 Opcode = C->getOpcode();
844 U = C;
845 }
846
847 switch (Opcode) {
848 default: break;
849 case Instruction::BitCast:
Quentin Colombet778dba12013-10-14 22:32:09 +0000850 // Look past bitcasts if its operand is in the same BB.
851 if (InMBB)
852 return X86SelectCallAddress(U->getOperand(0), AM);
853 break;
Chris Lattner8212d372009-07-10 05:33:42 +0000854
855 case Instruction::IntToPtr:
Quentin Colombet778dba12013-10-14 22:32:09 +0000856 // Look past no-op inttoptrs if its operand is in the same BB.
857 if (InMBB &&
858 TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000859 return X86SelectCallAddress(U->getOperand(0), AM);
860 break;
861
862 case Instruction::PtrToInt:
Quentin Colombet778dba12013-10-14 22:32:09 +0000863 // Look past no-op ptrtoints if its operand is in the same BB.
864 if (InMBB &&
865 TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000866 return X86SelectCallAddress(U->getOperand(0), AM);
867 break;
868 }
869
870 // Handle constant address.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000871 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000872 // Can't handle alternate code models yet.
Chris Lattner25e7f912009-07-10 21:03:06 +0000873 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner8212d372009-07-10 05:33:42 +0000874 return false;
875
876 // RIP-relative addresses can't have additional register operands.
877 if (Subtarget->isPICStyleRIPRel() &&
878 (AM.Base.Reg != 0 || AM.IndexReg != 0))
879 return false;
880
Saleem Abdulrasoole3c3fe52014-06-30 03:11:18 +0000881 // Can't handle DLL Import.
Nico Rieck7157bb72014-01-14 15:22:47 +0000882 if (GV->hasDLLImportStorageClass())
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000883 return false;
884
885 // Can't handle TLS.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000886 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000887 if (GVar->isThreadLocal())
Chris Lattner8212d372009-07-10 05:33:42 +0000888 return false;
889
890 // Okay, we've committed to selecting this global. Set up the basic address.
891 AM.GV = GV;
Wesley Peck527da1b2010-11-23 03:31:01 +0000892
Chris Lattner7277a802009-07-10 05:45:15 +0000893 // No ABI requires an extra load for anything other than DLLImport, which
894 // we rejected above. Return a direct reference to the global.
Chris Lattner7277a802009-07-10 05:45:15 +0000895 if (Subtarget->isPICStyleRIPRel()) {
896 // Use rip-relative addressing if we can. Above we verified that the
897 // base and index registers are unused.
898 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
899 AM.Base.Reg = X86::RIP;
Chris Lattner21c29402009-07-10 21:00:45 +0000900 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattner7277a802009-07-10 05:45:15 +0000901 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
902 } else if (Subtarget->isPICStyleGOT()) {
903 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner8212d372009-07-10 05:33:42 +0000904 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000905
Chris Lattner8212d372009-07-10 05:33:42 +0000906 return true;
907 }
908
909 // If all else fails, try to materialize the value in a register.
910 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
911 if (AM.Base.Reg == 0) {
912 AM.Base.Reg = getRegForValue(V);
913 return AM.Base.Reg != 0;
914 }
915 if (AM.IndexReg == 0) {
916 assert(AM.Scale == 1 && "Scale with no index!");
917 AM.IndexReg = getRegForValue(V);
918 return AM.IndexReg != 0;
919 }
920 }
921
922 return false;
923}
924
925
Owen Anderson4f948bd2008-09-04 07:08:58 +0000926/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000927bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000928 // Atomic stores need special handling.
Lang Hames7d2f7b52011-10-18 22:11:33 +0000929 const StoreInst *S = cast<StoreInst>(I);
930
931 if (S->isAtomic())
932 return false;
933
Juergen Ributzka349777d2014-06-12 23:27:57 +0000934 const Value *Val = S->getValueOperand();
935 const Value *Ptr = S->getPointerOperand();
Craig Topper4f55b0e2013-07-17 05:57:45 +0000936
Duncan Sandsf5dda012010-11-03 11:35:31 +0000937 MVT VT;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000938 if (!isTypeLegal(Val->getType(), VT, /*AllowI1=*/true))
Owen Anderson4f948bd2008-09-04 07:08:58 +0000939 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000940
Juergen Ributzka349777d2014-06-12 23:27:57 +0000941 unsigned Alignment = S->getAlignment();
942 unsigned ABIAlignment = DL.getABITypeAlignment(Val->getType());
943 if (Alignment == 0) // Ensure that codegen never sees alignment 0
944 Alignment = ABIAlignment;
945 bool Aligned = Alignment >= ABIAlignment;
946
Dan Gohman39d82f92008-09-10 20:11:02 +0000947 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000948 if (!X86SelectAddress(Ptr, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +0000949 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000950
Juergen Ributzka349777d2014-06-12 23:27:57 +0000951 return X86FastEmitStore(VT, Val, AM, createMachineMemOperandFor(I), Aligned);
Owen Anderson4f948bd2008-09-04 07:08:58 +0000952}
953
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000954/// X86SelectRet - Select and emit code to implement ret instructions.
955bool X86FastISel::X86SelectRet(const Instruction *I) {
956 const ReturnInst *Ret = cast<ReturnInst>(I);
957 const Function &F = *I->getParent()->getParent();
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000958 const X86MachineFunctionInfo *X86MFInfo =
959 FuncInfo.MF->getInfo<X86MachineFunctionInfo>();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000960
961 if (!FuncInfo.CanLowerReturn)
962 return false;
963
964 CallingConv::ID CC = F.getCallingConv();
965 if (CC != CallingConv::C &&
966 CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +0000967 CC != CallingConv::X86_FastCall &&
968 CC != CallingConv::X86_64_SysV)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000969 return false;
970
Charles Davise8f297c2013-07-12 06:02:35 +0000971 if (Subtarget->isCallingConvWin64(CC))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000972 return false;
973
974 // Don't handle popping bytes on return for now.
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000975 if (X86MFInfo->getBytesToPopOnReturn() != 0)
Jakub Staszak74010cd2013-02-17 18:35:25 +0000976 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000977
978 // fastcc with -tailcallopt is intended to provide a guaranteed
979 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000980 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000981 return false;
982
983 // Let SDISel handle vararg functions.
984 if (F.isVarArg())
985 return false;
986
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000987 // Build a list of return value registers.
988 SmallVector<unsigned, 4> RetRegs;
989
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000990 if (Ret->getNumOperands() > 0) {
991 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +0000992 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000993
994 // Analyze operands of the call, assigning locations to each operand.
995 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000996 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext());
Duncan Sandsfa7e6f22010-10-31 13:02:38 +0000997 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000998
999 const Value *RV = Ret->getOperand(0);
1000 unsigned Reg = getRegForValue(RV);
1001 if (Reg == 0)
1002 return false;
1003
1004 // Only handle a single return value for now.
1005 if (ValLocs.size() != 1)
1006 return false;
1007
1008 CCValAssign &VA = ValLocs[0];
Wesley Peck527da1b2010-11-23 03:31:01 +00001009
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001010 // Don't bother handling odd stuff for now.
1011 if (VA.getLocInfo() != CCValAssign::Full)
1012 return false;
1013 // Only handle register returns for now.
1014 if (!VA.isRegLoc())
1015 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001016
1017 // The calling-convention tables for x87 returns don't tell
1018 // the whole story.
Akira Hatanaka35166692014-08-01 22:19:41 +00001019 if (VA.getLocReg() == X86::FP0 || VA.getLocReg() == X86::FP1)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001020 return false;
1021
Eli Friedman6fc94dd2011-05-18 23:13:10 +00001022 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedman22da7992011-05-19 22:16:13 +00001023 EVT SrcVT = TLI.getValueType(RV->getType());
1024 EVT DstVT = VA.getValVT();
1025 // Special handling for extended integers.
1026 if (SrcVT != DstVT) {
1027 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
1028 return false;
1029
1030 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
1031 return false;
1032
1033 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
1034
1035 if (SrcVT == MVT::i1) {
1036 if (Outs[0].Flags.isSExt())
1037 return false;
1038 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
1039 SrcVT = MVT::i8;
1040 }
1041 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
1042 ISD::SIGN_EXTEND;
1043 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
1044 SrcReg, /*TODO: Kill=*/false);
1045 }
1046
1047 // Make the copy.
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001048 unsigned DstReg = VA.getLocReg();
1049 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +00001050 // Avoid a cross-class copy. This is very unlikely.
1051 if (!SrcRC->contains(DstReg))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001052 return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001053 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +00001054 DstReg).addReg(SrcReg);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001055
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +00001056 // Add register to return instruction.
1057 RetRegs.push_back(VA.getLocReg());
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001058 }
1059
Nick Lewyckyf8fc8922012-10-02 22:45:06 +00001060 // The x86-64 ABI for returning structs by value requires that we copy
1061 // the sret argument into %rax for the return. We saved the argument into
1062 // a virtual register in the entry block, so now we copy the value out
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +00001063 // and into %rax. We also do the same with %eax for Win32.
1064 if (F.hasStructRetAttr() &&
Yaron Keren136fe7d2014-04-01 18:15:34 +00001065 (Subtarget->is64Bit() || Subtarget->isTargetKnownWindowsMSVC())) {
Nick Lewyckyf8fc8922012-10-02 22:45:06 +00001066 unsigned Reg = X86MFInfo->getSRetReturnReg();
1067 assert(Reg &&
1068 "SRetReturnReg should have been set in LowerFormalArguments()!");
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +00001069 unsigned RetReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001070 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +00001071 RetReg).addReg(Reg);
1072 RetRegs.push_back(RetReg);
Nick Lewyckyf8fc8922012-10-02 22:45:06 +00001073 }
1074
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001075 // Now emit the RET.
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +00001076 MachineInstrBuilder MIB =
Rafael Espindolaea09c592014-02-18 22:05:46 +00001077 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Subtarget->is64Bit() ? X86::RETQ : X86::RETL));
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +00001078 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1079 MIB.addReg(RetRegs[i], RegState::Implicit);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001080 return true;
1081}
1082
Evan Chenga41ee292008-09-03 06:44:39 +00001083/// X86SelectLoad - Select and emit code to implement load instructions.
1084///
Juergen Ributzka349777d2014-06-12 23:27:57 +00001085bool X86FastISel::X86SelectLoad(const Instruction *I) {
1086 const LoadInst *LI = cast<LoadInst>(I);
1087
Eli Friedmanf3dd6da2011-09-02 22:33:24 +00001088 // Atomic loads need special handling.
Juergen Ributzka349777d2014-06-12 23:27:57 +00001089 if (LI->isAtomic())
Eli Friedmanf3dd6da2011-09-02 22:33:24 +00001090 return false;
1091
Duncan Sandsf5dda012010-11-03 11:35:31 +00001092 MVT VT;
Juergen Ributzka349777d2014-06-12 23:27:57 +00001093 if (!isTypeLegal(LI->getType(), VT, /*AllowI1=*/true))
Evan Chenga41ee292008-09-03 06:44:39 +00001094 return false;
1095
Juergen Ributzka349777d2014-06-12 23:27:57 +00001096 const Value *Ptr = LI->getPointerOperand();
1097
Dan Gohman39d82f92008-09-10 20:11:02 +00001098 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +00001099 if (!X86SelectAddress(Ptr, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +00001100 return false;
Evan Chenga41ee292008-09-03 06:44:39 +00001101
Evan Chengf5bc7e52008-09-05 21:00:03 +00001102 unsigned ResultReg = 0;
Juergen Ributzka349777d2014-06-12 23:27:57 +00001103 if (!X86FastEmitLoad(VT, AM, createMachineMemOperandFor(LI), ResultReg))
1104 return false;
1105
1106 UpdateValueMap(I, ResultReg);
1107 return true;
Evan Chenga41ee292008-09-03 06:44:39 +00001108}
1109
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +00001110static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001111 bool HasAVX = Subtarget->hasAVX();
Craig Topperb0c0f722012-01-10 06:54:16 +00001112 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
1113 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001114
Owen Anderson9f944592009-08-11 20:47:22 +00001115 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner74e01282008-10-15 04:32:45 +00001116 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00001117 case MVT::i8: return X86::CMP8rr;
1118 case MVT::i16: return X86::CMP16rr;
1119 case MVT::i32: return X86::CMP32rr;
1120 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001121 case MVT::f32:
1122 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
1123 case MVT::f64:
1124 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001125 }
Dan Gohman1ab1d312008-10-02 22:15:21 +00001126}
1127
Chris Lattner88f47542008-10-15 04:13:29 +00001128/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
1129/// of the comparison, return an opcode that works for the compare (e.g.
1130/// CMP32ri) otherwise return 0.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001131static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson9f944592009-08-11 20:47:22 +00001132 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner88f47542008-10-15 04:13:29 +00001133 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner74e01282008-10-15 04:32:45 +00001134 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00001135 case MVT::i8: return X86::CMP8ri;
1136 case MVT::i16: return X86::CMP16ri;
1137 case MVT::i32: return X86::CMP32ri;
1138 case MVT::i64:
Chris Lattner74e01282008-10-15 04:32:45 +00001139 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
1140 // field.
Chris Lattner3ba29352008-10-15 05:30:52 +00001141 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner74e01282008-10-15 04:32:45 +00001142 return X86::CMP64ri32;
1143 return 0;
1144 }
Chris Lattner88f47542008-10-15 04:13:29 +00001145}
1146
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001147bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
1148 EVT VT) {
Chris Lattnerd46b9512008-10-15 04:26:38 +00001149 unsigned Op0Reg = getRegForValue(Op0);
1150 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001151
Chris Lattnere388725a2008-10-15 05:18:04 +00001152 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +00001153 if (isa<ConstantPointerNull>(Op1))
Rafael Espindolaea09c592014-02-18 22:05:46 +00001154 Op1 = Constant::getNullValue(DL.getIntPtrType(Op0->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +00001155
Chris Lattnerd46b9512008-10-15 04:26:38 +00001156 // We have two options: compare with register or immediate. If the RHS of
1157 // the compare is an immediate that we can fold into this compare, use
1158 // CMPri, otherwise use CMPrr.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001159 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner74e01282008-10-15 04:32:45 +00001160 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001161 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CompareImmOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001162 .addReg(Op0Reg)
1163 .addImm(Op1C->getSExtValue());
Chris Lattnerd46b9512008-10-15 04:26:38 +00001164 return true;
1165 }
1166 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001167
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +00001168 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattnerd46b9512008-10-15 04:26:38 +00001169 if (CompareOpc == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001170
Chris Lattnerd46b9512008-10-15 04:26:38 +00001171 unsigned Op1Reg = getRegForValue(Op1);
1172 if (Op1Reg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001173 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CompareOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001174 .addReg(Op0Reg)
1175 .addReg(Op1Reg);
Wesley Peck527da1b2010-11-23 03:31:01 +00001176
Chris Lattnerd46b9512008-10-15 04:26:38 +00001177 return true;
1178}
1179
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001180bool X86FastISel::X86SelectCmp(const Instruction *I) {
1181 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001182
Duncan Sandsf5dda012010-11-03 11:35:31 +00001183 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00001184 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman09faf812008-09-05 01:33:56 +00001185 return false;
1186
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001187 // Try to optimize or fold the cmp.
1188 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1189 unsigned ResultReg = 0;
1190 switch (Predicate) {
1191 default: break;
1192 case CmpInst::FCMP_FALSE: {
1193 ResultReg = createResultReg(&X86::GR32RegClass);
1194 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV32r0),
1195 ResultReg);
1196 ResultReg = FastEmitInst_extractsubreg(MVT::i8, ResultReg, /*Kill=*/true,
1197 X86::sub_8bit);
1198 if (!ResultReg)
1199 return false;
1200 break;
1201 }
1202 case CmpInst::FCMP_TRUE: {
1203 ResultReg = createResultReg(&X86::GR8RegClass);
1204 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri),
1205 ResultReg).addImm(1);
1206 break;
1207 }
1208 }
1209
1210 if (ResultReg) {
1211 UpdateValueMap(I, ResultReg);
1212 return true;
1213 }
1214
1215 const Value *LHS = CI->getOperand(0);
1216 const Value *RHS = CI->getOperand(1);
1217
1218 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x, 0.0.
1219 // We don't have to materialize a zero constant for this case and can just use
1220 // %x again on the RHS.
1221 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
1222 const auto *RHSC = dyn_cast<ConstantFP>(RHS);
1223 if (RHSC && RHSC->isNullValue())
1224 RHS = LHS;
1225 }
1226
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001227 // FCMP_OEQ and FCMP_UNE cannot be checked with a single instruction.
Juergen Ributzkae3570562014-06-17 14:47:45 +00001228 static unsigned SETFOpcTable[2][3] = {
1229 { X86::SETEr, X86::SETNPr, X86::AND8rr },
1230 { X86::SETNEr, X86::SETPr, X86::OR8rr }
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001231 };
1232 unsigned *SETFOpc = nullptr;
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001233 switch (Predicate) {
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001234 default: break;
1235 case CmpInst::FCMP_OEQ: SETFOpc = &SETFOpcTable[0][0]; break;
1236 case CmpInst::FCMP_UNE: SETFOpc = &SETFOpcTable[1][0]; break;
1237 }
1238
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001239 ResultReg = createResultReg(&X86::GR8RegClass);
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001240 if (SETFOpc) {
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001241 if (!X86FastEmitCompare(LHS, RHS, VT))
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001242 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001243
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001244 unsigned FlagReg1 = createResultReg(&X86::GR8RegClass);
1245 unsigned FlagReg2 = createResultReg(&X86::GR8RegClass);
1246 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[0]),
1247 FlagReg1);
1248 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[1]),
1249 FlagReg2);
Juergen Ributzkae3570562014-06-17 14:47:45 +00001250 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[2]),
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001251 ResultReg).addReg(FlagReg1).addReg(FlagReg2);
Chris Lattnera3596db2008-10-15 03:47:17 +00001252 UpdateValueMap(I, ResultReg);
1253 return true;
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001254 }
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001255
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001256 X86::CondCode CC;
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001257 bool SwapArgs;
Craig Topper9f62d802014-06-27 05:18:21 +00001258 std::tie(CC, SwapArgs) = getX86ConditionCode(Predicate);
Juergen Ributzka345589e2014-06-27 17:16:34 +00001259 assert(CC <= X86::LAST_VALID_COND && "Unexpected condition code.");
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001260 unsigned Opc = X86::getSETFromCond(CC);
Wesley Peck527da1b2010-11-23 03:31:01 +00001261
Chris Lattnerf32ce222008-10-15 03:52:54 +00001262 if (SwapArgs)
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001263 std::swap(LHS, RHS);
Chris Lattnerf32ce222008-10-15 03:52:54 +00001264
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001265 // Emit a compare of LHS/RHS.
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001266 if (!X86FastEmitCompare(LHS, RHS, VT))
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001267 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001268
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001269 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001270 UpdateValueMap(I, ResultReg);
1271 return true;
1272}
Evan Chenga41ee292008-09-03 06:44:39 +00001273
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001274bool X86FastISel::X86SelectZExt(const Instruction *I) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001275 EVT DstVT = TLI.getValueType(I->getType());
1276 if (!TLI.isTypeLegal(DstVT))
1277 return false;
1278
1279 unsigned ResultReg = getRegForValue(I->getOperand(0));
1280 if (ResultReg == 0)
1281 return false;
1282
Tim Northover04eb4232013-05-30 10:43:18 +00001283 // Handle zero-extension from i1 to i8, which is common.
Craig Topper56710102013-08-15 02:33:50 +00001284 MVT SrcVT = TLI.getSimpleValueType(I->getOperand(0)->getType());
Tim Northover04eb4232013-05-30 10:43:18 +00001285 if (SrcVT.SimpleTy == MVT::i1) {
1286 // Set the high bits to zero.
1287 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1288 SrcVT = MVT::i8;
Eli Friedmanc7035512011-05-25 23:49:02 +00001289
Tim Northover04eb4232013-05-30 10:43:18 +00001290 if (ResultReg == 0)
1291 return false;
1292 }
1293
1294 if (DstVT == MVT::i64) {
1295 // Handle extension to 64-bits via sub-register shenanigans.
1296 unsigned MovInst;
1297
1298 switch (SrcVT.SimpleTy) {
1299 case MVT::i8: MovInst = X86::MOVZX32rr8; break;
1300 case MVT::i16: MovInst = X86::MOVZX32rr16; break;
1301 case MVT::i32: MovInst = X86::MOV32rr; break;
1302 default: llvm_unreachable("Unexpected zext to i64 source type");
1303 }
1304
1305 unsigned Result32 = createResultReg(&X86::GR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001306 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovInst), Result32)
Tim Northover04eb4232013-05-30 10:43:18 +00001307 .addReg(ResultReg);
1308
1309 ResultReg = createResultReg(&X86::GR64RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001310 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::SUBREG_TO_REG),
Tim Northover04eb4232013-05-30 10:43:18 +00001311 ResultReg)
1312 .addImm(0).addReg(Result32).addImm(X86::sub_32bit);
1313 } else if (DstVT != MVT::i8) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001314 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1315 ResultReg, /*Kill=*/true);
1316 if (ResultReg == 0)
1317 return false;
Dan Gohmana5753b32008-09-05 01:06:14 +00001318 }
1319
Eli Friedmanc7035512011-05-25 23:49:02 +00001320 UpdateValueMap(I, ResultReg);
1321 return true;
Dan Gohmana5753b32008-09-05 01:06:14 +00001322}
1323
Chris Lattnerd46b9512008-10-15 04:26:38 +00001324
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001325bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmana5753b32008-09-05 01:06:14 +00001326 // Unconditional branches are selected by tablegen-generated code.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001327 // Handle a conditional branch.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001328 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohman87fb4e82010-07-07 16:29:44 +00001329 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1330 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmana5753b32008-09-05 01:06:14 +00001331
Dan Gohman42ef6692010-08-21 02:32:36 +00001332 // Fold the common case of a conditional branch with a comparison
1333 // in the same block (values defined on other blocks may not have
1334 // initialized registers).
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001335 X86::CondCode CC;
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001336 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman42ef6692010-08-21 02:32:36 +00001337 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001338 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmana5753b32008-09-05 01:06:14 +00001339
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001340 // Try to optimize or fold the cmp.
1341 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1342 switch (Predicate) {
1343 default: break;
1344 case CmpInst::FCMP_FALSE: FastEmitBranch(FalseMBB, DbgLoc); return true;
1345 case CmpInst::FCMP_TRUE: FastEmitBranch(TrueMBB, DbgLoc); return true;
1346 }
1347
1348 const Value *CmpLHS = CI->getOperand(0);
1349 const Value *CmpRHS = CI->getOperand(1);
1350
1351 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x,
1352 // 0.0.
1353 // We don't have to materialize a zero constant for this case and can just
1354 // use %x again on the RHS.
1355 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
1356 const auto *CmpRHSC = dyn_cast<ConstantFP>(CmpRHS);
1357 if (CmpRHSC && CmpRHSC->isNullValue())
1358 CmpRHS = CmpLHS;
1359 }
1360
Dan Gohman1ab1d312008-10-02 22:15:21 +00001361 // Try to take advantage of fallthrough opportunities.
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001362 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohman1ab1d312008-10-02 22:15:21 +00001363 std::swap(TrueMBB, FalseMBB);
1364 Predicate = CmpInst::getInversePredicate(Predicate);
1365 }
1366
Juergen Ributzka345589e2014-06-27 17:16:34 +00001367 // FCMP_OEQ and FCMP_UNE cannot be expressed with a single flag/condition
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001368 // code check. Instead two branch instructions are required to check all
Juergen Ributzka345589e2014-06-27 17:16:34 +00001369 // the flags. First we change the predicate to a supported condition code,
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001370 // which will be the first branch. Later one we will emit the second
1371 // branch.
1372 bool NeedExtraBranch = false;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001373 switch (Predicate) {
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001374 default: break;
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001375 case CmpInst::FCMP_OEQ:
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001376 std::swap(TrueMBB, FalseMBB); // fall-through
1377 case CmpInst::FCMP_UNE:
1378 NeedExtraBranch = true;
1379 Predicate = CmpInst::FCMP_ONE;
1380 break;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001381 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001382
Juergen Ributzkaaa602092014-06-17 21:55:43 +00001383 bool SwapArgs;
1384 unsigned BranchOpc;
Craig Topper9f62d802014-06-27 05:18:21 +00001385 std::tie(CC, SwapArgs) = getX86ConditionCode(Predicate);
Juergen Ributzka345589e2014-06-27 17:16:34 +00001386 assert(CC <= X86::LAST_VALID_COND && "Unexpected condition code.");
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001387
1388 BranchOpc = X86::GetCondBranchFromCond(CC);
Chris Lattner47bef252008-10-15 04:02:26 +00001389 if (SwapArgs)
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001390 std::swap(CmpLHS, CmpRHS);
Chris Lattner47bef252008-10-15 04:02:26 +00001391
Chris Lattnerd46b9512008-10-15 04:26:38 +00001392 // Emit a compare of the LHS and RHS, setting the flags.
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001393 if (!X86FastEmitCompare(CmpLHS, CmpRHS, VT))
Chris Lattnerd46b9512008-10-15 04:26:38 +00001394 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001395
Rafael Espindolaea09c592014-02-18 22:05:46 +00001396 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BranchOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001397 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001398
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001399 // X86 requires a second branch to handle UNE (and OEQ, which is mapped
1400 // to UNE above).
1401 if (NeedExtraBranch) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001402 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JP_4))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001403 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001404 }
1405
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001406 // Obtain the branch weight and add the TrueBB to the successor list.
Juergen Ributzka454d3742014-06-13 00:45:11 +00001407 uint32_t BranchWeight = 0;
1408 if (FuncInfo.BPI)
1409 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1410 TrueMBB->getBasicBlock());
1411 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001412
1413 // Emits an unconditional branch to the FalseBB, obtains the branch
Alp Toker1d099d92014-06-19 19:41:26 +00001414 // weight, and adds it to the successor list.
Juergen Ributzka2da1bbc2014-06-16 23:58:24 +00001415 FastEmitBranch(FalseMBB, DbgLoc);
1416
Dan Gohman1ab1d312008-10-02 22:15:21 +00001417 return true;
1418 }
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001419 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1420 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1421 // typically happen for _Bool and C++ bools.
1422 MVT SourceVT;
1423 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1424 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1425 unsigned TestOpc = 0;
1426 switch (SourceVT.SimpleTy) {
1427 default: break;
1428 case MVT::i8: TestOpc = X86::TEST8ri; break;
1429 case MVT::i16: TestOpc = X86::TEST16ri; break;
1430 case MVT::i32: TestOpc = X86::TEST32ri; break;
1431 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1432 }
1433 if (TestOpc) {
1434 unsigned OpReg = getRegForValue(TI->getOperand(0));
1435 if (OpReg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001436 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TestOpc))
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001437 .addReg(OpReg).addImm(1);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001438
Chris Lattnerc59290a2011-04-19 04:26:32 +00001439 unsigned JmpOpc = X86::JNE_4;
1440 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1441 std::swap(TrueMBB, FalseMBB);
1442 JmpOpc = X86::JE_4;
1443 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001444
Rafael Espindolaea09c592014-02-18 22:05:46 +00001445 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(JmpOpc))
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001446 .addMBB(TrueMBB);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001447 FastEmitBranch(FalseMBB, DbgLoc);
Juergen Ributzka454d3742014-06-13 00:45:11 +00001448 uint32_t BranchWeight = 0;
1449 if (FuncInfo.BPI)
1450 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1451 TrueMBB->getBasicBlock());
1452 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001453 return true;
1454 }
1455 }
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001456 } else if (foldX86XALUIntrinsic(CC, BI, BI->getCondition())) {
1457 // Fake request the condition, otherwise the intrinsic might be completely
1458 // optimized away.
1459 unsigned TmpReg = getRegForValue(BI->getCondition());
1460 if (TmpReg == 0)
1461 return false;
Juergen Ributzka2bce27e2014-06-24 23:51:21 +00001462
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001463 unsigned BranchOpc = X86::GetCondBranchFromCond(CC);
Juergen Ributzka2bce27e2014-06-24 23:51:21 +00001464
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001465 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BranchOpc))
1466 .addMBB(TrueMBB);
1467 FastEmitBranch(FalseMBB, DbgLoc);
1468 uint32_t BranchWeight = 0;
1469 if (FuncInfo.BPI)
1470 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1471 TrueMBB->getBasicBlock());
1472 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
1473 return true;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001474 }
1475
1476 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman0eea0292011-04-27 01:34:27 +00001477 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1478 // in an explicit cast, so make sure to handle that correctly.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001479 unsigned OpReg = getRegForValue(BI->getCondition());
1480 if (OpReg == 0) return false;
1481
Rafael Espindolaea09c592014-02-18 22:05:46 +00001482 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
Eli Friedman0eea0292011-04-27 01:34:27 +00001483 .addReg(OpReg).addImm(1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001484 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JNE_4))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001485 .addMBB(TrueMBB);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001486 FastEmitBranch(FalseMBB, DbgLoc);
Juergen Ributzka454d3742014-06-13 00:45:11 +00001487 uint32_t BranchWeight = 0;
1488 if (FuncInfo.BPI)
1489 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1490 TrueMBB->getBasicBlock());
1491 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Dan Gohmana5753b32008-09-05 01:06:14 +00001492 return true;
1493}
1494
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001495bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001496 unsigned CReg = 0, OpReg = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00001497 const TargetRegisterClass *RC = nullptr;
Duncan Sands9dff9be2010-02-15 16:12:20 +00001498 if (I->getType()->isIntegerTy(8)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001499 CReg = X86::CL;
1500 RC = &X86::GR8RegClass;
1501 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001502 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1503 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1504 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001505 default: return false;
1506 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001507 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001508 CReg = X86::CX;
1509 RC = &X86::GR16RegClass;
1510 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001511 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1512 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1513 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001514 default: return false;
1515 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001516 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001517 CReg = X86::ECX;
1518 RC = &X86::GR32RegClass;
1519 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001520 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1521 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1522 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001523 default: return false;
1524 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001525 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001526 CReg = X86::RCX;
1527 RC = &X86::GR64RegClass;
1528 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001529 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1530 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1531 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001532 default: return false;
1533 }
1534 } else {
1535 return false;
1536 }
1537
Duncan Sandsf5dda012010-11-03 11:35:31 +00001538 MVT VT;
1539 if (!isTypeLegal(I->getType(), VT))
Dan Gohmandb06a992008-09-05 21:27:34 +00001540 return false;
1541
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001542 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1543 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001544
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001545 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1546 if (Op1Reg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001547 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00001548 CReg).addReg(Op1Reg);
Dan Gohmand3917152008-10-07 21:50:36 +00001549
1550 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001551 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohmand3917152008-10-07 21:50:36 +00001552 if (CReg != X86::CL)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001553 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001554 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001555 .addReg(CReg, RegState::Kill);
Dan Gohmand3917152008-10-07 21:50:36 +00001556
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001557 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001558 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(OpReg), ResultReg)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001559 .addReg(Op0Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001560 UpdateValueMap(I, ResultReg);
1561 return true;
1562}
1563
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001564bool X86FastISel::X86SelectDivRem(const Instruction *I) {
1565 const static unsigned NumTypes = 4; // i8, i16, i32, i64
1566 const static unsigned NumOps = 4; // SDiv, SRem, UDiv, URem
1567 const static bool S = true; // IsSigned
1568 const static bool U = false; // !IsSigned
1569 const static unsigned Copy = TargetOpcode::COPY;
1570 // For the X86 DIV/IDIV instruction, in most cases the dividend
1571 // (numerator) must be in a specific register pair highreg:lowreg,
1572 // producing the quotient in lowreg and the remainder in highreg.
1573 // For most data types, to set up the instruction, the dividend is
1574 // copied into lowreg, and lowreg is sign-extended or zero-extended
1575 // into highreg. The exception is i8, where the dividend is defined
1576 // as a single register rather than a register pair, and we
1577 // therefore directly sign-extend or zero-extend the dividend into
1578 // lowreg, instead of copying, and ignore the highreg.
1579 const static struct DivRemEntry {
1580 // The following portion depends only on the data type.
1581 const TargetRegisterClass *RC;
1582 unsigned LowInReg; // low part of the register pair
1583 unsigned HighInReg; // high part of the register pair
1584 // The following portion depends on both the data type and the operation.
1585 struct DivRemResult {
1586 unsigned OpDivRem; // The specific DIV/IDIV opcode to use.
1587 unsigned OpSignExtend; // Opcode for sign-extending lowreg into
1588 // highreg, or copying a zero into highreg.
1589 unsigned OpCopy; // Opcode for copying dividend into lowreg, or
1590 // zero/sign-extending into lowreg for i8.
1591 unsigned DivRemResultReg; // Register containing the desired result.
1592 bool IsOpSigned; // Whether to use signed or unsigned form.
1593 } ResultTable[NumOps];
1594 } OpTable[NumTypes] = {
1595 { &X86::GR8RegClass, X86::AX, 0, {
1596 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AL, S }, // SDiv
1597 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AH, S }, // SRem
1598 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AL, U }, // UDiv
1599 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AH, U }, // URem
1600 }
1601 }, // i8
1602 { &X86::GR16RegClass, X86::AX, X86::DX, {
1603 { X86::IDIV16r, X86::CWD, Copy, X86::AX, S }, // SDiv
1604 { X86::IDIV16r, X86::CWD, Copy, X86::DX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001605 { X86::DIV16r, X86::MOV32r0, Copy, X86::AX, U }, // UDiv
1606 { X86::DIV16r, X86::MOV32r0, Copy, X86::DX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001607 }
1608 }, // i16
1609 { &X86::GR32RegClass, X86::EAX, X86::EDX, {
1610 { X86::IDIV32r, X86::CDQ, Copy, X86::EAX, S }, // SDiv
1611 { X86::IDIV32r, X86::CDQ, Copy, X86::EDX, S }, // SRem
1612 { X86::DIV32r, X86::MOV32r0, Copy, X86::EAX, U }, // UDiv
1613 { X86::DIV32r, X86::MOV32r0, Copy, X86::EDX, U }, // URem
1614 }
1615 }, // i32
1616 { &X86::GR64RegClass, X86::RAX, X86::RDX, {
1617 { X86::IDIV64r, X86::CQO, Copy, X86::RAX, S }, // SDiv
1618 { X86::IDIV64r, X86::CQO, Copy, X86::RDX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001619 { X86::DIV64r, X86::MOV32r0, Copy, X86::RAX, U }, // UDiv
1620 { X86::DIV64r, X86::MOV32r0, Copy, X86::RDX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001621 }
1622 }, // i64
1623 };
1624
1625 MVT VT;
1626 if (!isTypeLegal(I->getType(), VT))
1627 return false;
1628
1629 unsigned TypeIndex, OpIndex;
1630 switch (VT.SimpleTy) {
1631 default: return false;
1632 case MVT::i8: TypeIndex = 0; break;
1633 case MVT::i16: TypeIndex = 1; break;
1634 case MVT::i32: TypeIndex = 2; break;
1635 case MVT::i64: TypeIndex = 3;
1636 if (!Subtarget->is64Bit())
1637 return false;
1638 break;
1639 }
1640
1641 switch (I->getOpcode()) {
1642 default: llvm_unreachable("Unexpected div/rem opcode");
1643 case Instruction::SDiv: OpIndex = 0; break;
1644 case Instruction::SRem: OpIndex = 1; break;
1645 case Instruction::UDiv: OpIndex = 2; break;
1646 case Instruction::URem: OpIndex = 3; break;
1647 }
1648
1649 const DivRemEntry &TypeEntry = OpTable[TypeIndex];
1650 const DivRemEntry::DivRemResult &OpEntry = TypeEntry.ResultTable[OpIndex];
1651 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1652 if (Op0Reg == 0)
1653 return false;
1654 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1655 if (Op1Reg == 0)
1656 return false;
1657
1658 // Move op0 into low-order input register.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001659 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001660 TII.get(OpEntry.OpCopy), TypeEntry.LowInReg).addReg(Op0Reg);
1661 // Zero-extend or sign-extend into high-order input register.
1662 if (OpEntry.OpSignExtend) {
1663 if (OpEntry.IsOpSigned)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001664 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001665 TII.get(OpEntry.OpSignExtend));
Tim Northover64ec0ff2013-05-30 13:19:42 +00001666 else {
1667 unsigned Zero32 = createResultReg(&X86::GR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001668 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Tim Northover64ec0ff2013-05-30 13:19:42 +00001669 TII.get(X86::MOV32r0), Zero32);
1670
1671 // Copy the zero into the appropriate sub/super/identical physical
1672 // register. Unfortunately the operations needed are not uniform enough to
1673 // fit neatly into the table above.
1674 if (VT.SimpleTy == MVT::i16) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001675 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001676 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001677 .addReg(Zero32, 0, X86::sub_16bit);
1678 } else if (VT.SimpleTy == MVT::i32) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001679 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001680 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001681 .addReg(Zero32);
1682 } else if (VT.SimpleTy == MVT::i64) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001683 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Tim Northover64ec0ff2013-05-30 13:19:42 +00001684 TII.get(TargetOpcode::SUBREG_TO_REG), TypeEntry.HighInReg)
1685 .addImm(0).addReg(Zero32).addImm(X86::sub_32bit);
1686 }
1687 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001688 }
1689 // Generate the DIV/IDIV instruction.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001690 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001691 TII.get(OpEntry.OpDivRem)).addReg(Op1Reg);
Jim Grosbachc35388f2013-07-09 02:07:25 +00001692 // For i8 remainder, we can't reference AH directly, as we'll end
1693 // up with bogus copies like %R9B = COPY %AH. Reference AX
1694 // instead to prevent AH references in a REX instruction.
1695 //
1696 // The current assumption of the fast register allocator is that isel
1697 // won't generate explicit references to the GPR8_NOREX registers. If
1698 // the allocator and/or the backend get enhanced to be more robust in
1699 // that regard, this can be, and should be, removed.
1700 unsigned ResultReg = 0;
1701 if ((I->getOpcode() == Instruction::SRem ||
1702 I->getOpcode() == Instruction::URem) &&
1703 OpEntry.DivRemResultReg == X86::AH && Subtarget->is64Bit()) {
1704 unsigned SourceSuperReg = createResultReg(&X86::GR16RegClass);
1705 unsigned ResultSuperReg = createResultReg(&X86::GR16RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001706 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbachc35388f2013-07-09 02:07:25 +00001707 TII.get(Copy), SourceSuperReg).addReg(X86::AX);
1708
1709 // Shift AX right by 8 bits instead of using AH.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001710 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SHR16ri),
Jim Grosbachc35388f2013-07-09 02:07:25 +00001711 ResultSuperReg).addReg(SourceSuperReg).addImm(8);
1712
1713 // Now reference the 8-bit subreg of the result.
1714 ResultReg = FastEmitInst_extractsubreg(MVT::i8, ResultSuperReg,
1715 /*Kill=*/true, X86::sub_8bit);
1716 }
1717 // Copy the result out of the physreg if we haven't already.
1718 if (!ResultReg) {
1719 ResultReg = createResultReg(TypeEntry.RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001720 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Copy), ResultReg)
Jim Grosbachc35388f2013-07-09 02:07:25 +00001721 .addReg(OpEntry.DivRemResultReg);
1722 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001723 UpdateValueMap(I, ResultReg);
1724
1725 return true;
1726}
1727
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001728/// \brief Emit a conditional move instruction (if the are supported) to lower
1729/// the select.
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001730bool X86FastISel::X86FastEmitCMoveSelect(MVT RetVT, const Instruction *I) {
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001731 // Check if the subtarget supports these instructions.
1732 if (!Subtarget->hasCMov())
Wesley Peck527da1b2010-11-23 03:31:01 +00001733 return false;
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001734
1735 // FIXME: Add support for i8.
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001736 if (RetVT < MVT::i16 || RetVT > MVT::i64)
1737 return false;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001738
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001739 const Value *Cond = I->getOperand(0);
1740 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
1741 bool NeedTest = true;
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001742 X86::CondCode CC = X86::COND_NE;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001743
Juergen Ributzka345589e2014-06-27 17:16:34 +00001744 // Optimize conditions coming from a compare if both instructions are in the
Juergen Ributzka296833c2014-06-25 20:06:12 +00001745 // same basic block (values defined in other basic blocks may not have
1746 // initialized registers).
1747 const auto *CI = dyn_cast<CmpInst>(Cond);
1748 if (CI && (CI->getParent() == I->getParent())) {
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001749 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1750
1751 // FCMP_OEQ and FCMP_UNE cannot be checked with a single instruction.
1752 static unsigned SETFOpcTable[2][3] = {
1753 { X86::SETNPr, X86::SETEr , X86::TEST8rr },
1754 { X86::SETPr, X86::SETNEr, X86::OR8rr }
1755 };
1756 unsigned *SETFOpc = nullptr;
1757 switch (Predicate) {
1758 default: break;
1759 case CmpInst::FCMP_OEQ:
1760 SETFOpc = &SETFOpcTable[0][0];
1761 Predicate = CmpInst::ICMP_NE;
1762 break;
1763 case CmpInst::FCMP_UNE:
1764 SETFOpc = &SETFOpcTable[1][0];
1765 Predicate = CmpInst::ICMP_NE;
1766 break;
1767 }
1768
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001769 bool NeedSwap;
Craig Topper9f62d802014-06-27 05:18:21 +00001770 std::tie(CC, NeedSwap) = getX86ConditionCode(Predicate);
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001771 assert(CC <= X86::LAST_VALID_COND && "Unexpected condition code.");
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001772
1773 const Value *CmpLHS = CI->getOperand(0);
1774 const Value *CmpRHS = CI->getOperand(1);
1775 if (NeedSwap)
1776 std::swap(CmpLHS, CmpRHS);
1777
1778 EVT CmpVT = TLI.getValueType(CmpLHS->getType());
1779 // Emit a compare of the LHS and RHS, setting the flags.
1780 if (!X86FastEmitCompare(CmpLHS, CmpRHS, CmpVT))
1781 return false;
1782
1783 if (SETFOpc) {
1784 unsigned FlagReg1 = createResultReg(&X86::GR8RegClass);
1785 unsigned FlagReg2 = createResultReg(&X86::GR8RegClass);
1786 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[0]),
1787 FlagReg1);
1788 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SETFOpc[1]),
1789 FlagReg2);
1790 auto const &II = TII.get(SETFOpc[2]);
1791 if (II.getNumDefs()) {
1792 unsigned TmpReg = createResultReg(&X86::GR8RegClass);
1793 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, TmpReg)
1794 .addReg(FlagReg2).addReg(FlagReg1);
1795 } else {
1796 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
1797 .addReg(FlagReg2).addReg(FlagReg1);
1798 }
1799 }
1800 NeedTest = false;
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001801 } else if (foldX86XALUIntrinsic(CC, I, Cond)) {
1802 // Fake request the condition, otherwise the intrinsic might be completely
1803 // optimized away.
1804 unsigned TmpReg = getRegForValue(Cond);
1805 if (TmpReg == 0)
1806 return false;
Juergen Ributzka2bce27e2014-06-24 23:51:21 +00001807
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001808 NeedTest = false;
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001809 }
1810
1811 if (NeedTest) {
1812 // Selects operate on i1, however, CondReg is 8 bits width and may contain
1813 // garbage. Indeed, only the less significant bit is supposed to be
1814 // accurate. If we read more than the lsb, we may see non-zero values
1815 // whereas lsb is zero. Therefore, we have to truncate Op0Reg to i1 for
1816 // the select. This is achieved by performing TEST against 1.
1817 unsigned CondReg = getRegForValue(Cond);
1818 if (CondReg == 0)
1819 return false;
1820 bool CondIsKill = hasTrivialKill(Cond);
1821
1822 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
1823 .addReg(CondReg, getKillRegState(CondIsKill)).addImm(1);
1824 }
1825
1826 const Value *LHS = I->getOperand(1);
1827 const Value *RHS = I->getOperand(2);
1828
1829 unsigned RHSReg = getRegForValue(RHS);
1830 bool RHSIsKill = hasTrivialKill(RHS);
1831
1832 unsigned LHSReg = getRegForValue(LHS);
1833 bool LHSIsKill = hasTrivialKill(LHS);
1834
1835 if (!LHSReg || !RHSReg)
1836 return false;
1837
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001838 unsigned Opc = X86::getCMovFromCond(CC, RC->getSize());
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001839 unsigned ResultReg = FastEmitInst_rr(Opc, RC, RHSReg, RHSIsKill,
1840 LHSReg, LHSIsKill);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001841 UpdateValueMap(I, ResultReg);
1842 return true;
1843}
1844
Juergen Ributzka21d56082014-06-23 21:55:40 +00001845/// \brief Emit SSE instructions to lower the select.
1846///
1847/// Try to use SSE1/SSE2 instructions to simulate a select without branches.
1848/// This lowers fp selects into a CMP/AND/ANDN/OR sequence when the necessary
1849/// SSE instructions are available.
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001850bool X86FastISel::X86FastEmitSSESelect(MVT RetVT, const Instruction *I) {
Juergen Ributzka345589e2014-06-27 17:16:34 +00001851 // Optimize conditions coming from a compare if both instructions are in the
Juergen Ributzka296833c2014-06-25 20:06:12 +00001852 // same basic block (values defined in other basic blocks may not have
1853 // initialized registers).
Juergen Ributzka21d56082014-06-23 21:55:40 +00001854 const auto *CI = dyn_cast<FCmpInst>(I->getOperand(0));
Juergen Ributzka296833c2014-06-25 20:06:12 +00001855 if (!CI || (CI->getParent() != I->getParent()))
Juergen Ributzka21d56082014-06-23 21:55:40 +00001856 return false;
1857
1858 if (I->getType() != CI->getOperand(0)->getType() ||
1859 !((Subtarget->hasSSE1() && RetVT == MVT::f32) ||
1860 (Subtarget->hasSSE2() && RetVT == MVT::f64) ))
1861 return false;
1862
1863 const Value *CmpLHS = CI->getOperand(0);
1864 const Value *CmpRHS = CI->getOperand(1);
1865 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1866
1867 // The optimizer might have replaced fcmp oeq %x, %x with fcmp ord %x, 0.0.
1868 // We don't have to materialize a zero constant for this case and can just use
1869 // %x again on the RHS.
1870 if (Predicate == CmpInst::FCMP_ORD || Predicate == CmpInst::FCMP_UNO) {
1871 const auto *CmpRHSC = dyn_cast<ConstantFP>(CmpRHS);
1872 if (CmpRHSC && CmpRHSC->isNullValue())
1873 CmpRHS = CmpLHS;
1874 }
1875
1876 unsigned CC;
1877 bool NeedSwap;
Juergen Ributzka345589e2014-06-27 17:16:34 +00001878 std::tie(CC, NeedSwap) = getX86SSEConditionCode(Predicate);
Juergen Ributzka21d56082014-06-23 21:55:40 +00001879 if (CC > 7)
1880 return false;
1881
1882 if (NeedSwap)
1883 std::swap(CmpLHS, CmpRHS);
1884
1885 static unsigned OpcTable[2][2][4] = {
1886 { { X86::CMPSSrr, X86::FsANDPSrr, X86::FsANDNPSrr, X86::FsORPSrr },
1887 { X86::VCMPSSrr, X86::VFsANDPSrr, X86::VFsANDNPSrr, X86::VFsORPSrr } },
1888 { { X86::CMPSDrr, X86::FsANDPDrr, X86::FsANDNPDrr, X86::FsORPDrr },
1889 { X86::VCMPSDrr, X86::VFsANDPDrr, X86::VFsANDNPDrr, X86::VFsORPDrr } }
1890 };
1891
1892 bool HasAVX = Subtarget->hasAVX();
1893 unsigned *Opc = nullptr;
1894 switch (RetVT.SimpleTy) {
1895 default: return false;
1896 case MVT::f32: Opc = &OpcTable[0][HasAVX][0]; break;
1897 case MVT::f64: Opc = &OpcTable[1][HasAVX][0]; break;
1898 }
1899
1900 const Value *LHS = I->getOperand(1);
1901 const Value *RHS = I->getOperand(2);
1902
1903 unsigned LHSReg = getRegForValue(LHS);
1904 bool LHSIsKill = hasTrivialKill(LHS);
1905
1906 unsigned RHSReg = getRegForValue(RHS);
1907 bool RHSIsKill = hasTrivialKill(RHS);
1908
1909 unsigned CmpLHSReg = getRegForValue(CmpLHS);
1910 bool CmpLHSIsKill = hasTrivialKill(CmpLHS);
1911
1912 unsigned CmpRHSReg = getRegForValue(CmpRHS);
1913 bool CmpRHSIsKill = hasTrivialKill(CmpRHS);
1914
1915 if (!LHSReg || !RHSReg || !CmpLHS || !CmpRHS)
1916 return false;
1917
1918 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
1919 unsigned CmpReg = FastEmitInst_rri(Opc[0], RC, CmpLHSReg, CmpLHSIsKill,
1920 CmpRHSReg, CmpRHSIsKill, CC);
1921 unsigned AndReg = FastEmitInst_rr(Opc[1], RC, CmpReg, /*IsKill=*/false,
1922 LHSReg, LHSIsKill);
1923 unsigned AndNReg = FastEmitInst_rr(Opc[2], RC, CmpReg, /*IsKill=*/true,
1924 RHSReg, RHSIsKill);
1925 unsigned ResultReg = FastEmitInst_rr(Opc[3], RC, AndNReg, /*IsKill=*/true,
1926 AndReg, /*IsKill=*/true);
1927 UpdateValueMap(I, ResultReg);
1928 return true;
1929}
1930
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00001931bool X86FastISel::X86FastEmitPseudoSelect(MVT RetVT, const Instruction *I) {
Juergen Ributzkaaed5c962014-06-23 21:55:44 +00001932 // These are pseudo CMOV instructions and will be later expanded into control-
1933 // flow.
1934 unsigned Opc;
1935 switch (RetVT.SimpleTy) {
1936 default: return false;
1937 case MVT::i8: Opc = X86::CMOV_GR8; break;
1938 case MVT::i16: Opc = X86::CMOV_GR16; break;
1939 case MVT::i32: Opc = X86::CMOV_GR32; break;
1940 case MVT::f32: Opc = X86::CMOV_FR32; break;
1941 case MVT::f64: Opc = X86::CMOV_FR64; break;
1942 }
1943
1944 const Value *Cond = I->getOperand(0);
1945 X86::CondCode CC = X86::COND_NE;
Juergen Ributzka296833c2014-06-25 20:06:12 +00001946
Juergen Ributzka345589e2014-06-27 17:16:34 +00001947 // Optimize conditions coming from a compare if both instructions are in the
Juergen Ributzka296833c2014-06-25 20:06:12 +00001948 // same basic block (values defined in other basic blocks may not have
1949 // initialized registers).
1950 const auto *CI = dyn_cast<CmpInst>(Cond);
1951 if (CI && (CI->getParent() == I->getParent())) {
Juergen Ributzkaaed5c962014-06-23 21:55:44 +00001952 bool NeedSwap;
Craig Topper9f62d802014-06-27 05:18:21 +00001953 std::tie(CC, NeedSwap) = getX86ConditionCode(CI->getPredicate());
Juergen Ributzkaaed5c962014-06-23 21:55:44 +00001954 if (CC > X86::LAST_VALID_COND)
1955 return false;
1956
1957 const Value *CmpLHS = CI->getOperand(0);
1958 const Value *CmpRHS = CI->getOperand(1);
1959
1960 if (NeedSwap)
1961 std::swap(CmpLHS, CmpRHS);
1962
1963 EVT CmpVT = TLI.getValueType(CmpLHS->getType());
1964 if (!X86FastEmitCompare(CmpLHS, CmpRHS, CmpVT))
1965 return false;
1966 } else {
1967 unsigned CondReg = getRegForValue(Cond);
1968 if (CondReg == 0)
1969 return false;
1970 bool CondIsKill = hasTrivialKill(Cond);
1971 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
1972 .addReg(CondReg, getKillRegState(CondIsKill)).addImm(1);
1973 }
1974
1975 const Value *LHS = I->getOperand(1);
1976 const Value *RHS = I->getOperand(2);
1977
1978 unsigned LHSReg = getRegForValue(LHS);
1979 bool LHSIsKill = hasTrivialKill(LHS);
1980
1981 unsigned RHSReg = getRegForValue(RHS);
1982 bool RHSIsKill = hasTrivialKill(RHS);
1983
1984 if (!LHSReg || !RHSReg)
1985 return false;
1986
1987 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
1988
1989 unsigned ResultReg =
1990 FastEmitInst_rri(Opc, RC, RHSReg, RHSIsKill, LHSReg, LHSIsKill, CC);
1991 UpdateValueMap(I, ResultReg);
1992 return true;
1993}
1994
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00001995bool X86FastISel::X86SelectSelect(const Instruction *I) {
1996 MVT RetVT;
1997 if (!isTypeLegal(I->getType(), RetVT))
1998 return false;
1999
2000 // Check if we can fold the select.
2001 if (const auto *CI = dyn_cast<CmpInst>(I->getOperand(0))) {
2002 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2003 const Value *Opnd = nullptr;
2004 switch (Predicate) {
2005 default: break;
2006 case CmpInst::FCMP_FALSE: Opnd = I->getOperand(2); break;
2007 case CmpInst::FCMP_TRUE: Opnd = I->getOperand(1); break;
2008 }
2009 // No need for a select anymore - this is an unconditional move.
2010 if (Opnd) {
2011 unsigned OpReg = getRegForValue(Opnd);
2012 if (OpReg == 0)
2013 return false;
2014 bool OpIsKill = hasTrivialKill(Opnd);
2015 const TargetRegisterClass *RC = TLI.getRegClassFor(RetVT);
2016 unsigned ResultReg = createResultReg(RC);
2017 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2018 TII.get(TargetOpcode::COPY), ResultReg)
2019 .addReg(OpReg, getKillRegState(OpIsKill));
2020 UpdateValueMap(I, ResultReg);
2021 return true;
2022 }
2023 }
2024
2025 // First try to use real conditional move instructions.
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00002026 if (X86FastEmitCMoveSelect(RetVT, I))
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00002027 return true;
2028
Juergen Ributzka345589e2014-06-27 17:16:34 +00002029 // Try to use a sequence of SSE instructions to simulate a conditional move.
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00002030 if (X86FastEmitSSESelect(RetVT, I))
Juergen Ributzka21d56082014-06-23 21:55:40 +00002031 return true;
2032
Juergen Ributzkaaed5c962014-06-23 21:55:44 +00002033 // Fall-back to pseudo conditional move instructions, which will be later
2034 // converted to control-flow.
Juergen Ributzkaa13d7d62014-06-25 22:50:59 +00002035 if (X86FastEmitPseudoSelect(RetVT, I))
Juergen Ributzkaaed5c962014-06-23 21:55:44 +00002036 return true;
2037
Juergen Ributzka6ef06f92014-06-23 21:55:36 +00002038 return false;
2039}
2040
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002041bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00002042 // fpext from float to double.
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002043 if (X86ScalarSSEf64 &&
Chris Lattnerfdd87902009-10-05 05:54:46 +00002044 I->getType()->isDoubleTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002045 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00002046 if (V->getType()->isFloatTy()) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00002047 unsigned OpReg = getRegForValue(V);
2048 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00002049 unsigned ResultReg = createResultReg(&X86::FR64RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002050 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002051 TII.get(X86::CVTSS2SDrr), ResultReg)
2052 .addReg(OpReg);
Chris Lattnera0f9d492008-10-15 05:07:36 +00002053 UpdateValueMap(I, ResultReg);
2054 return true;
Dan Gohmanbf646f22008-09-10 21:02:08 +00002055 }
2056 }
2057
2058 return false;
2059}
2060
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002061bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002062 if (X86ScalarSSEf64) {
Chris Lattnerfdd87902009-10-05 05:54:46 +00002063 if (I->getType()->isFloatTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002064 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00002065 if (V->getType()->isDoubleTy()) {
Dan Gohmanbf646f22008-09-10 21:02:08 +00002066 unsigned OpReg = getRegForValue(V);
2067 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00002068 unsigned ResultReg = createResultReg(&X86::FR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002069 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002070 TII.get(X86::CVTSD2SSrr), ResultReg)
2071 .addReg(OpReg);
Dan Gohmanbf646f22008-09-10 21:02:08 +00002072 UpdateValueMap(I, ResultReg);
2073 return true;
2074 }
2075 }
2076 }
2077
2078 return false;
2079}
2080
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002081bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002082 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
2083 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peck527da1b2010-11-23 03:31:01 +00002084
Eli Friedmanc7035512011-05-25 23:49:02 +00002085 // This code only handles truncation to byte.
Owen Anderson9f944592009-08-11 20:47:22 +00002086 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Chengb9286692008-09-07 08:47:42 +00002087 return false;
Eli Friedmanc7035512011-05-25 23:49:02 +00002088 if (!TLI.isTypeLegal(SrcVT))
Evan Chengb9286692008-09-07 08:47:42 +00002089 return false;
2090
2091 unsigned InputReg = getRegForValue(I->getOperand(0));
2092 if (!InputReg)
2093 // Unhandled operand. Halt "fast" selection and bail.
2094 return false;
2095
Eli Friedmanc7035512011-05-25 23:49:02 +00002096 if (SrcVT == MVT::i8) {
2097 // Truncate from i8 to i1; no code needed.
2098 UpdateValueMap(I, InputReg);
2099 return true;
2100 }
Evan Chengb9286692008-09-07 08:47:42 +00002101
Eli Friedmanc7035512011-05-25 23:49:02 +00002102 if (!Subtarget->is64Bit()) {
2103 // If we're on x86-32; we can't extract an i8 from a general register.
2104 // First issue a copy to GR16_ABCD or GR32_ABCD.
Craig Topperabadc662012-04-20 06:31:50 +00002105 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) ?
2106 (const TargetRegisterClass*)&X86::GR16_ABCDRegClass :
2107 (const TargetRegisterClass*)&X86::GR32_ABCDRegClass;
Eli Friedmanc7035512011-05-25 23:49:02 +00002108 unsigned CopyReg = createResultReg(CopyRC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002109 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Eli Friedmanc7035512011-05-25 23:49:02 +00002110 CopyReg).addReg(InputReg);
2111 InputReg = CopyReg;
2112 }
2113
2114 // Issue an extract_subreg.
Owen Anderson9f944592009-08-11 20:47:22 +00002115 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedmanc7035512011-05-25 23:49:02 +00002116 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen9340ea52010-05-24 14:48:17 +00002117 X86::sub_8bit);
Evan Chengb9286692008-09-07 08:47:42 +00002118 if (!ResultReg)
2119 return false;
2120
2121 UpdateValueMap(I, ResultReg);
2122 return true;
2123}
2124
Eli Friedman60afcc22011-05-20 22:21:04 +00002125bool X86FastISel::IsMemcpySmall(uint64_t Len) {
2126 return Len <= (Subtarget->is64Bit() ? 32 : 16);
2127}
2128
Eli Friedmanbcc69142011-04-27 01:45:07 +00002129bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
2130 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedman60afcc22011-05-20 22:21:04 +00002131
Eli Friedmanbcc69142011-04-27 01:45:07 +00002132 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedman60afcc22011-05-20 22:21:04 +00002133 if (!IsMemcpySmall(Len))
2134 return false;
2135
2136 bool i64Legal = Subtarget->is64Bit();
Eli Friedmanbcc69142011-04-27 01:45:07 +00002137
2138 // We don't care about alignment here since we just emit integer accesses.
2139 while (Len) {
2140 MVT VT;
2141 if (Len >= 8 && i64Legal)
2142 VT = MVT::i64;
2143 else if (Len >= 4)
2144 VT = MVT::i32;
2145 else if (Len >= 2)
2146 VT = MVT::i16;
2147 else {
Eli Friedmanbcc69142011-04-27 01:45:07 +00002148 VT = MVT::i8;
2149 }
2150
2151 unsigned Reg;
Juergen Ributzka349777d2014-06-12 23:27:57 +00002152 bool RV = X86FastEmitLoad(VT, SrcAM, nullptr, Reg);
2153 RV &= X86FastEmitStore(VT, Reg, /*Kill=*/true, DestAM);
Eli Friedmanbcc69142011-04-27 01:45:07 +00002154 assert(RV && "Failed to emit load or store??");
2155
2156 unsigned Size = VT.getSizeInBits()/8;
2157 Len -= Size;
2158 DestAM.Disp += Size;
2159 SrcAM.Disp += Size;
2160 }
2161
2162 return true;
2163}
2164
Juergen Ributzka3566c082014-07-15 06:35:50 +00002165bool X86FastISel::FastLowerIntrinsicCall(const IntrinsicInst *II) {
Bill Wendling80b34b32008-12-09 02:42:50 +00002166 // FIXME: Handle more intrinsics.
Juergen Ributzka3566c082014-07-15 06:35:50 +00002167 switch (II->getIntrinsicID()) {
Bill Wendling80b34b32008-12-09 02:42:50 +00002168 default: return false;
Juergen Ributzka4dc95872014-06-11 21:44:44 +00002169 case Intrinsic::frameaddress: {
Juergen Ributzka3566c082014-07-15 06:35:50 +00002170 Type *RetTy = II->getCalledFunction()->getReturnType();
Juergen Ributzka4dc95872014-06-11 21:44:44 +00002171
2172 MVT VT;
2173 if (!isTypeLegal(RetTy, VT))
2174 return false;
2175
2176 unsigned Opc;
2177 const TargetRegisterClass *RC = nullptr;
2178
2179 switch (VT.SimpleTy) {
2180 default: llvm_unreachable("Invalid result type for frameaddress.");
2181 case MVT::i32: Opc = X86::MOV32rm; RC = &X86::GR32RegClass; break;
2182 case MVT::i64: Opc = X86::MOV64rm; RC = &X86::GR64RegClass; break;
2183 }
2184
2185 // This needs to be set before we call getFrameRegister, otherwise we get
2186 // the wrong frame register.
2187 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
2188 MFI->setFrameAddressIsTaken(true);
2189
Eric Christopherd9134482014-08-04 21:25:23 +00002190 const X86RegisterInfo *RegInfo = static_cast<const X86RegisterInfo *>(
2191 TM.getSubtargetImpl()->getRegisterInfo());
Juergen Ributzka4dc95872014-06-11 21:44:44 +00002192 unsigned FrameReg = RegInfo->getFrameRegister(*(FuncInfo.MF));
2193 assert(((FrameReg == X86::RBP && VT == MVT::i64) ||
2194 (FrameReg == X86::EBP && VT == MVT::i32)) &&
2195 "Invalid Frame Register!");
2196
2197 // Always make a copy of the frame register to to a vreg first, so that we
2198 // never directly reference the frame register (the TwoAddressInstruction-
2199 // Pass doesn't like that).
2200 unsigned SrcReg = createResultReg(RC);
2201 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2202 TII.get(TargetOpcode::COPY), SrcReg).addReg(FrameReg);
2203
2204 // Now recursively load from the frame address.
2205 // movq (%rbp), %rax
2206 // movq (%rax), %rax
2207 // movq (%rax), %rax
2208 // ...
2209 unsigned DestReg;
Juergen Ributzka3566c082014-07-15 06:35:50 +00002210 unsigned Depth = cast<ConstantInt>(II->getOperand(0))->getZExtValue();
Juergen Ributzka4dc95872014-06-11 21:44:44 +00002211 while (Depth--) {
2212 DestReg = createResultReg(RC);
2213 addDirectMem(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2214 TII.get(Opc), DestReg), SrcReg);
2215 SrcReg = DestReg;
2216 }
2217
Juergen Ributzka3566c082014-07-15 06:35:50 +00002218 UpdateValueMap(II, SrcReg);
Juergen Ributzka4dc95872014-06-11 21:44:44 +00002219 return true;
2220 }
Chris Lattner91328b32011-04-19 05:52:03 +00002221 case Intrinsic::memcpy: {
Juergen Ributzka3566c082014-07-15 06:35:50 +00002222 const MemCpyInst *MCI = cast<MemCpyInst>(II);
Chris Lattner91328b32011-04-19 05:52:03 +00002223 // Don't handle volatile or variable length memcpys.
Juergen Ributzka3566c082014-07-15 06:35:50 +00002224 if (MCI->isVolatile())
Chris Lattner91328b32011-04-19 05:52:03 +00002225 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00002226
Juergen Ributzka3566c082014-07-15 06:35:50 +00002227 if (isa<ConstantInt>(MCI->getLength())) {
Eli Friedmancd2124a2011-06-10 23:39:36 +00002228 // Small memcpy's are common enough that we want to do them
2229 // without a call if possible.
Juergen Ributzka3566c082014-07-15 06:35:50 +00002230 uint64_t Len = cast<ConstantInt>(MCI->getLength())->getZExtValue();
Eli Friedmancd2124a2011-06-10 23:39:36 +00002231 if (IsMemcpySmall(Len)) {
2232 X86AddressMode DestAM, SrcAM;
Juergen Ributzka3566c082014-07-15 06:35:50 +00002233 if (!X86SelectAddress(MCI->getRawDest(), DestAM) ||
2234 !X86SelectAddress(MCI->getRawSource(), SrcAM))
Eli Friedmancd2124a2011-06-10 23:39:36 +00002235 return false;
2236 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
2237 return true;
2238 }
2239 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00002240
Eli Friedmancd2124a2011-06-10 23:39:36 +00002241 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
Juergen Ributzka3566c082014-07-15 06:35:50 +00002242 if (!MCI->getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner91328b32011-04-19 05:52:03 +00002243 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00002244
Juergen Ributzka3566c082014-07-15 06:35:50 +00002245 if (MCI->getSourceAddressSpace() > 255 || MCI->getDestAddressSpace() > 255)
Eli Friedmancd2124a2011-06-10 23:39:36 +00002246 return false;
2247
Juergen Ributzka3566c082014-07-15 06:35:50 +00002248 return LowerCallTo(II, "memcpy", II->getNumArgOperands() - 2);
Chris Lattner91328b32011-04-19 05:52:03 +00002249 }
Eli Friedmancd2124a2011-06-10 23:39:36 +00002250 case Intrinsic::memset: {
Juergen Ributzka3566c082014-07-15 06:35:50 +00002251 const MemSetInst *MSI = cast<MemSetInst>(II);
Eric Christopher0713a9d2011-06-08 23:55:35 +00002252
Juergen Ributzka3566c082014-07-15 06:35:50 +00002253 if (MSI->isVolatile())
Nick Lewyckya530a4d2011-08-02 00:40:16 +00002254 return false;
2255
Eli Friedmancd2124a2011-06-10 23:39:36 +00002256 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
Juergen Ributzka3566c082014-07-15 06:35:50 +00002257 if (!MSI->getLength()->getType()->isIntegerTy(SizeWidth))
Eli Friedmancd2124a2011-06-10 23:39:36 +00002258 return false;
2259
Juergen Ributzka3566c082014-07-15 06:35:50 +00002260 if (MSI->getDestAddressSpace() > 255)
Eli Friedmancd2124a2011-06-10 23:39:36 +00002261 return false;
2262
Juergen Ributzka3566c082014-07-15 06:35:50 +00002263 return LowerCallTo(II, "memset", II->getNumArgOperands() - 2);
Eli Friedmancd2124a2011-06-10 23:39:36 +00002264 }
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002265 case Intrinsic::stackprotector: {
Chad Rosier06e34d92012-05-11 19:43:29 +00002266 // Emit code to store the stack guard onto the stack.
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002267 EVT PtrTy = TLI.getPointerTy();
2268
Juergen Ributzka3566c082014-07-15 06:35:50 +00002269 const Value *Op1 = II->getArgOperand(0); // The guard's value.
2270 const AllocaInst *Slot = cast<AllocaInst>(II->getArgOperand(1));
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002271
Josh Magee22b8ba22013-12-19 03:17:11 +00002272 MFI.setStackProtectorIndex(FuncInfo.StaticAllocaMap[Slot]);
2273
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002274 // Grab the frame index.
2275 X86AddressMode AM;
2276 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher5e95aee2010-03-18 21:58:33 +00002277 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher52ecfdf2010-03-18 20:27:26 +00002278 return true;
2279 }
Dale Johannesend5575f22010-01-26 00:09:58 +00002280 case Intrinsic::dbg_declare: {
Juergen Ributzka3566c082014-07-15 06:35:50 +00002281 const DbgDeclareInst *DI = cast<DbgDeclareInst>(II);
Dale Johannesend5575f22010-01-26 00:09:58 +00002282 X86AddressMode AM;
Dale Johannesenad00f032010-01-29 21:21:28 +00002283 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesend5575f22010-01-26 00:09:58 +00002284 if (!X86SelectAddress(DI->getAddress(), AM))
2285 return false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00002286 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen654528e2010-02-18 18:51:15 +00002287 // FIXME may need to add RegState::Debug to any registers produced,
2288 // although ESP/EBP should be the only ones at the moment.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002289 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II), AM).
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002290 addImm(0).addMetadata(DI->getVariable());
Dale Johannesend5575f22010-01-26 00:09:58 +00002291 return true;
2292 }
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00002293 case Intrinsic::trap: {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002294 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TRAP));
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00002295 return true;
2296 }
Juergen Ributzka272b5702014-06-11 23:11:02 +00002297 case Intrinsic::sqrt: {
2298 if (!Subtarget->hasSSE1())
2299 return false;
2300
Juergen Ributzka3566c082014-07-15 06:35:50 +00002301 Type *RetTy = II->getCalledFunction()->getReturnType();
Juergen Ributzka272b5702014-06-11 23:11:02 +00002302
2303 MVT VT;
2304 if (!isTypeLegal(RetTy, VT))
2305 return false;
2306
Juergen Ributzka345589e2014-06-27 17:16:34 +00002307 // Unfortunately we can't use FastEmit_r, because the AVX version of FSQRT
Juergen Ributzka272b5702014-06-11 23:11:02 +00002308 // is not generated by FastISel yet.
2309 // FIXME: Update this code once tablegen can handle it.
2310 static const unsigned SqrtOpc[2][2] = {
2311 {X86::SQRTSSr, X86::VSQRTSSr},
2312 {X86::SQRTSDr, X86::VSQRTSDr}
2313 };
2314 bool HasAVX = Subtarget->hasAVX();
2315 unsigned Opc;
2316 const TargetRegisterClass *RC;
2317 switch (VT.SimpleTy) {
2318 default: return false;
2319 case MVT::f32: Opc = SqrtOpc[0][HasAVX]; RC = &X86::FR32RegClass; break;
2320 case MVT::f64: Opc = SqrtOpc[1][HasAVX]; RC = &X86::FR64RegClass; break;
2321 }
2322
Juergen Ributzka3566c082014-07-15 06:35:50 +00002323 const Value *SrcVal = II->getArgOperand(0);
Juergen Ributzka272b5702014-06-11 23:11:02 +00002324 unsigned SrcReg = getRegForValue(SrcVal);
2325
2326 if (SrcReg == 0)
2327 return false;
2328
2329 unsigned ImplicitDefReg = 0;
2330 if (HasAVX) {
2331 ImplicitDefReg = createResultReg(RC);
2332 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2333 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
2334 }
2335
2336 unsigned ResultReg = createResultReg(RC);
2337 MachineInstrBuilder MIB;
2338 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
2339 ResultReg);
2340
2341 if (ImplicitDefReg)
2342 MIB.addReg(ImplicitDefReg);
2343
2344 MIB.addReg(SrcReg);
2345
Juergen Ributzka3566c082014-07-15 06:35:50 +00002346 UpdateValueMap(II, ResultReg);
Juergen Ributzka272b5702014-06-11 23:11:02 +00002347 return true;
2348 }
Bill Wendling80b34b32008-12-09 02:42:50 +00002349 case Intrinsic::sadd_with_overflow:
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002350 case Intrinsic::uadd_with_overflow:
2351 case Intrinsic::ssub_with_overflow:
2352 case Intrinsic::usub_with_overflow:
2353 case Intrinsic::smul_with_overflow:
2354 case Intrinsic::umul_with_overflow: {
2355 // This implements the basic lowering of the xalu with overflow intrinsics
Juergen Ributzka345589e2014-06-27 17:16:34 +00002356 // into add/sub/mul followed by either seto or setb.
Juergen Ributzka3566c082014-07-15 06:35:50 +00002357 const Function *Callee = II->getCalledFunction();
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002358 auto *Ty = cast<StructType>(Callee->getReturnType());
2359 Type *RetTy = Ty->getTypeAtIndex(0U);
2360 Type *CondTy = Ty->getTypeAtIndex(1);
Bill Wendling80b34b32008-12-09 02:42:50 +00002361
Duncan Sandsf5dda012010-11-03 11:35:31 +00002362 MVT VT;
Bill Wendling80b34b32008-12-09 02:42:50 +00002363 if (!isTypeLegal(RetTy, VT))
2364 return false;
2365
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002366 if (VT < MVT::i8 || VT > MVT::i64)
Bill Wendling80b34b32008-12-09 02:42:50 +00002367 return false;
2368
Juergen Ributzka3566c082014-07-15 06:35:50 +00002369 const Value *LHS = II->getArgOperand(0);
2370 const Value *RHS = II->getArgOperand(1);
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002371
Juergen Ributzka345589e2014-06-27 17:16:34 +00002372 // Canonicalize immediate to the RHS.
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002373 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
Juergen Ributzka3566c082014-07-15 06:35:50 +00002374 isCommutativeIntrinsic(II))
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002375 std::swap(LHS, RHS);
2376
Juergen Ributzka40226142014-08-08 17:21:37 +00002377 bool UseIncDec = false;
2378 if (isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isOne())
2379 UseIncDec = true;
2380
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002381 unsigned BaseOpc, CondOpc;
Juergen Ributzka3566c082014-07-15 06:35:50 +00002382 switch (II->getIntrinsicID()) {
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002383 default: llvm_unreachable("Unexpected intrinsic!");
2384 case Intrinsic::sadd_with_overflow:
Rui Ueyama4c956fe2014-08-08 22:47:49 +00002385 BaseOpc = UseIncDec ? unsigned(X86ISD::INC) : unsigned(ISD::ADD);
2386 CondOpc = X86::SETOr;
2387 break;
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002388 case Intrinsic::uadd_with_overflow:
2389 BaseOpc = ISD::ADD; CondOpc = X86::SETBr; break;
2390 case Intrinsic::ssub_with_overflow:
Rui Ueyama4c956fe2014-08-08 22:47:49 +00002391 BaseOpc = UseIncDec ? unsigned(X86ISD::DEC) : unsigned(ISD::SUB);
2392 CondOpc = X86::SETOr;
2393 break;
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002394 case Intrinsic::usub_with_overflow:
2395 BaseOpc = ISD::SUB; CondOpc = X86::SETBr; break;
2396 case Intrinsic::smul_with_overflow:
Juergen Ributzka665ea712014-07-07 21:52:21 +00002397 BaseOpc = X86ISD::SMUL; CondOpc = X86::SETOr; break;
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002398 case Intrinsic::umul_with_overflow:
2399 BaseOpc = X86ISD::UMUL; CondOpc = X86::SETOr; break;
2400 }
2401
2402 unsigned LHSReg = getRegForValue(LHS);
2403 if (LHSReg == 0)
2404 return false;
2405 bool LHSIsKill = hasTrivialKill(LHS);
2406
2407 unsigned ResultReg = 0;
2408 // Check if we have an immediate version.
Juergen Ributzka40226142014-08-08 17:21:37 +00002409 if (const auto *CI = dyn_cast<ConstantInt>(RHS)) {
2410 static const unsigned Opc[2][2][4] = {
2411 { { X86::INC8r, X86::INC16r, X86::INC32r, X86::INC64r },
2412 { X86::DEC8r, X86::DEC16r, X86::DEC32r, X86::DEC64r } },
2413 { { X86::INC8r, X86::INC64_16r, X86::INC64_32r, X86::INC64r },
2414 { X86::DEC8r, X86::DEC64_16r, X86::DEC64_32r, X86::DEC64r } }
2415 };
2416
Juergen Ributzka793f28d2014-08-08 18:47:04 +00002417 if (BaseOpc == X86ISD::INC || BaseOpc == X86ISD::DEC) {
Juergen Ributzka40226142014-08-08 17:21:37 +00002418 ResultReg = createResultReg(TLI.getRegClassFor(VT));
2419 bool Is64Bit = Subtarget->is64Bit();
2420 bool IsDec = BaseOpc == X86ISD::DEC;
2421 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2422 TII.get(Opc[Is64Bit][IsDec][VT.SimpleTy-MVT::i8]), ResultReg)
2423 .addReg(LHSReg, getKillRegState(LHSIsKill));
2424 } else
2425 ResultReg = FastEmit_ri(VT, VT, BaseOpc, LHSReg, LHSIsKill,
2426 CI->getZExtValue());
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002427 }
2428
2429 unsigned RHSReg;
2430 bool RHSIsKill;
2431 if (!ResultReg) {
2432 RHSReg = getRegForValue(RHS);
2433 if (RHSReg == 0)
2434 return false;
2435 RHSIsKill = hasTrivialKill(RHS);
2436 ResultReg = FastEmit_rr(VT, VT, BaseOpc, LHSReg, LHSIsKill, RHSReg,
2437 RHSIsKill);
2438 }
2439
Juergen Ributzka665ea712014-07-07 21:52:21 +00002440 // FastISel doesn't have a pattern for all X86::MUL*r and X86::IMUL*r. Emit
2441 // it manually.
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002442 if (BaseOpc == X86ISD::UMUL && !ResultReg) {
2443 static const unsigned MULOpc[] =
Juergen Ributzka665ea712014-07-07 21:52:21 +00002444 { X86::MUL8r, X86::MUL16r, X86::MUL32r, X86::MUL64r };
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002445 static const unsigned Reg[] = { X86::AL, X86::AX, X86::EAX, X86::RAX };
2446 // First copy the first operand into RAX, which is an implicit input to
2447 // the X86::MUL*r instruction.
2448 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2449 TII.get(TargetOpcode::COPY), Reg[VT.SimpleTy-MVT::i8])
2450 .addReg(LHSReg, getKillRegState(LHSIsKill));
2451 ResultReg = FastEmitInst_r(MULOpc[VT.SimpleTy-MVT::i8],
2452 TLI.getRegClassFor(VT), RHSReg, RHSIsKill);
Juergen Ributzka665ea712014-07-07 21:52:21 +00002453 } else if (BaseOpc == X86ISD::SMUL && !ResultReg) {
2454 static const unsigned MULOpc[] =
2455 { X86::IMUL8r, X86::IMUL16rr, X86::IMUL32rr, X86::IMUL64rr };
2456 if (VT == MVT::i8) {
2457 // Copy the first operand into AL, which is an implicit input to the
2458 // X86::IMUL8r instruction.
2459 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2460 TII.get(TargetOpcode::COPY), X86::AL)
2461 .addReg(LHSReg, getKillRegState(LHSIsKill));
2462 ResultReg = FastEmitInst_r(MULOpc[0], TLI.getRegClassFor(VT), RHSReg,
2463 RHSIsKill);
2464 } else
2465 ResultReg = FastEmitInst_rr(MULOpc[VT.SimpleTy-MVT::i8],
2466 TLI.getRegClassFor(VT), LHSReg, LHSIsKill,
2467 RHSReg, RHSIsKill);
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002468 }
2469
2470 if (!ResultReg)
Bill Wendling80b34b32008-12-09 02:42:50 +00002471 return false;
2472
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00002473 unsigned ResultReg2 = FuncInfo.CreateRegs(CondTy);
2474 assert((ResultReg+1) == ResultReg2 && "Nonconsecutive result registers.");
2475 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CondOpc),
2476 ResultReg2);
Eli Friedmana4d4a012011-05-16 21:06:17 +00002477
Juergen Ributzka3566c082014-07-15 06:35:50 +00002478 UpdateValueMap(II, ResultReg, 2);
Bill Wendling80b34b32008-12-09 02:42:50 +00002479 return true;
2480 }
Juergen Ributzka3453bcf2014-06-13 02:21:58 +00002481 case Intrinsic::x86_sse_cvttss2si:
2482 case Intrinsic::x86_sse_cvttss2si64:
2483 case Intrinsic::x86_sse2_cvttsd2si:
2484 case Intrinsic::x86_sse2_cvttsd2si64: {
2485 bool IsInputDouble;
Juergen Ributzka3566c082014-07-15 06:35:50 +00002486 switch (II->getIntrinsicID()) {
Juergen Ributzka3453bcf2014-06-13 02:21:58 +00002487 default: llvm_unreachable("Unexpected intrinsic.");
2488 case Intrinsic::x86_sse_cvttss2si:
2489 case Intrinsic::x86_sse_cvttss2si64:
2490 if (!Subtarget->hasSSE1())
2491 return false;
2492 IsInputDouble = false;
2493 break;
2494 case Intrinsic::x86_sse2_cvttsd2si:
2495 case Intrinsic::x86_sse2_cvttsd2si64:
2496 if (!Subtarget->hasSSE2())
2497 return false;
2498 IsInputDouble = true;
2499 break;
2500 }
2501
Juergen Ributzka3566c082014-07-15 06:35:50 +00002502 Type *RetTy = II->getCalledFunction()->getReturnType();
Juergen Ributzka3453bcf2014-06-13 02:21:58 +00002503 MVT VT;
2504 if (!isTypeLegal(RetTy, VT))
2505 return false;
2506
2507 static const unsigned CvtOpc[2][2][2] = {
2508 { { X86::CVTTSS2SIrr, X86::VCVTTSS2SIrr },
2509 { X86::CVTTSS2SI64rr, X86::VCVTTSS2SI64rr } },
2510 { { X86::CVTTSD2SIrr, X86::VCVTTSD2SIrr },
2511 { X86::CVTTSD2SI64rr, X86::VCVTTSD2SI64rr } }
2512 };
2513 bool HasAVX = Subtarget->hasAVX();
2514 unsigned Opc;
2515 switch (VT.SimpleTy) {
2516 default: llvm_unreachable("Unexpected result type.");
2517 case MVT::i32: Opc = CvtOpc[IsInputDouble][0][HasAVX]; break;
2518 case MVT::i64: Opc = CvtOpc[IsInputDouble][1][HasAVX]; break;
2519 }
2520
2521 // Check if we can fold insertelement instructions into the convert.
Juergen Ributzka3566c082014-07-15 06:35:50 +00002522 const Value *Op = II->getArgOperand(0);
Juergen Ributzka3453bcf2014-06-13 02:21:58 +00002523 while (auto *IE = dyn_cast<InsertElementInst>(Op)) {
2524 const Value *Index = IE->getOperand(2);
2525 if (!isa<ConstantInt>(Index))
2526 break;
2527 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
2528
2529 if (Idx == 0) {
2530 Op = IE->getOperand(1);
2531 break;
2532 }
2533 Op = IE->getOperand(0);
2534 }
2535
2536 unsigned Reg = getRegForValue(Op);
2537 if (Reg == 0)
2538 return false;
2539
2540 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
2541 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
2542 .addReg(Reg);
2543
Juergen Ributzka3566c082014-07-15 06:35:50 +00002544 UpdateValueMap(II, ResultReg);
Juergen Ributzka3453bcf2014-06-13 02:21:58 +00002545 return true;
2546 }
Bill Wendling80b34b32008-12-09 02:42:50 +00002547 }
2548}
2549
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002550bool X86FastISel::FastLowerArguments() {
2551 if (!FuncInfo.CanLowerReturn)
2552 return false;
2553
2554 const Function *F = FuncInfo.Fn;
2555 if (F->isVarArg())
2556 return false;
2557
2558 CallingConv::ID CC = F->getCallingConv();
2559 if (CC != CallingConv::C)
2560 return false;
Charles Davise8f297c2013-07-12 06:02:35 +00002561
2562 if (Subtarget->isCallingConvWin64(CC))
2563 return false;
2564
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002565 if (!Subtarget->is64Bit())
2566 return false;
2567
2568 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002569 unsigned GPRCnt = 0;
2570 unsigned FPRCnt = 0;
2571 unsigned Idx = 0;
2572 for (auto const &Arg : F->args()) {
2573 // The first argument is at index 1.
2574 ++Idx;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002575 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
2576 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
2577 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
2578 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
2579 return false;
2580
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002581 Type *ArgTy = Arg.getType();
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002582 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
2583 return false;
2584
2585 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosier1b33e8d2013-02-26 01:05:31 +00002586 if (!ArgVT.isSimple()) return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002587 switch (ArgVT.getSimpleVT().SimpleTy) {
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002588 default: return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002589 case MVT::i32:
2590 case MVT::i64:
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002591 ++GPRCnt;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002592 break;
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002593 case MVT::f32:
2594 case MVT::f64:
2595 if (!Subtarget->hasSSE1())
2596 return false;
2597 ++FPRCnt;
2598 break;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002599 }
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002600
2601 if (GPRCnt > 6)
2602 return false;
2603
2604 if (FPRCnt > 8)
2605 return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002606 }
2607
Craig Topper840beec2014-04-04 05:16:06 +00002608 static const MCPhysReg GPR32ArgRegs[] = {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002609 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
2610 };
Craig Topper840beec2014-04-04 05:16:06 +00002611 static const MCPhysReg GPR64ArgRegs[] = {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002612 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
2613 };
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002614 static const MCPhysReg XMMArgRegs[] = {
2615 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2616 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2617 };
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002618
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002619 unsigned GPRIdx = 0;
2620 unsigned FPRIdx = 0;
2621 for (auto const &Arg : F->args()) {
2622 MVT VT = TLI.getSimpleValueType(Arg.getType());
2623 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
2624 unsigned SrcReg;
2625 switch (VT.SimpleTy) {
2626 default: llvm_unreachable("Unexpected value type.");
2627 case MVT::i32: SrcReg = GPR32ArgRegs[GPRIdx++]; break;
2628 case MVT::i64: SrcReg = GPR64ArgRegs[GPRIdx++]; break;
2629 case MVT::f32: // fall-through
2630 case MVT::f64: SrcReg = XMMArgRegs[FPRIdx++]; break;
2631 }
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002632 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
2633 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
2634 // Without this, EmitLiveInCopies may eliminate the livein if its only
2635 // use is a bitcast (which isn't turned into an instruction).
2636 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002637 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002638 TII.get(TargetOpcode::COPY), ResultReg)
2639 .addReg(DstReg, getKillRegState(true));
2640 UpdateValueMap(&Arg, ResultReg);
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002641 }
2642 return true;
2643}
2644
Juergen Ributzka23d43312014-07-15 06:35:47 +00002645static unsigned computeBytesPoppedByCallee(const X86Subtarget *Subtarget,
2646 CallingConv::ID CC,
2647 ImmutableCallSite *CS) {
2648 if (Subtarget->is64Bit())
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002649 return 0;
Juergen Ributzka23d43312014-07-15 06:35:47 +00002650 if (Subtarget->getTargetTriple().isOSMSVCRT())
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002651 return 0;
Juergen Ributzka23d43312014-07-15 06:35:47 +00002652 if (CC == CallingConv::Fast || CC == CallingConv::GHC ||
2653 CC == CallingConv::HiPE)
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002654 return 0;
Juergen Ributzka23d43312014-07-15 06:35:47 +00002655 if (CS && !CS->paramHasAttr(1, Attribute::StructRet))
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002656 return 0;
Juergen Ributzka23d43312014-07-15 06:35:47 +00002657 if (CS && CS->paramHasAttr(1, Attribute::InReg))
Rafael Espindola11c38b92012-07-25 13:41:10 +00002658 return 0;
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002659 return 4;
2660}
2661
Juergen Ributzka23d43312014-07-15 06:35:47 +00002662bool X86FastISel::FastLowerCall(CallLoweringInfo &CLI) {
2663 auto &OutVals = CLI.OutVals;
2664 auto &OutFlags = CLI.OutFlags;
2665 auto &OutRegs = CLI.OutRegs;
2666 auto &Ins = CLI.Ins;
2667 auto &InRegs = CLI.InRegs;
2668 CallingConv::ID CC = CLI.CallConv;
2669 bool &IsTailCall = CLI.IsTailCall;
2670 bool IsVarArg = CLI.IsVarArg;
2671 const Value *Callee = CLI.Callee;
2672 const char *SymName = CLI.SymName;
2673
2674 bool Is64Bit = Subtarget->is64Bit();
2675 bool IsWin64 = Subtarget->isCallingConvWin64(CC);
2676
2677 // Handle only C, fastcc, and webkit_js calling conventions for now.
2678 switch (CC) {
2679 default: return false;
2680 case CallingConv::C:
2681 case CallingConv::Fast:
2682 case CallingConv::WebKit_JS:
2683 case CallingConv::X86_FastCall:
2684 case CallingConv::X86_64_Win64:
2685 case CallingConv::X86_64_SysV:
2686 break;
2687 }
2688
2689 // Allow SelectionDAG isel to handle tail calls.
2690 if (IsTailCall)
2691 return false;
2692
2693 // fastcc with -tailcallopt is intended to provide a guaranteed
2694 // tail call optimization. Fastisel doesn't know how to do that.
2695 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
2696 return false;
2697
2698 // Don't know how to handle Win64 varargs yet. Nothing special needed for
2699 // x86-32. Special handling for x86-64 is implemented.
2700 if (IsVarArg && IsWin64)
2701 return false;
2702
2703 // Don't know about inalloca yet.
2704 if (CLI.CS && CLI.CS->hasInAllocaArgument())
2705 return false;
2706
2707 // Fast-isel doesn't know about callee-pop yet.
2708 if (X86::isCalleePop(CC, Subtarget->is64Bit(), IsVarArg,
2709 TM.Options.GuaranteedTailCallOpt))
2710 return false;
2711
2712 // If this is a constant i1/i8/i16 argument, promote to i32 to avoid an extra
2713 // instruction. This is safe because it is common to all FastISel supported
2714 // calling conventions on x86.
2715 for (int i = 0, e = OutVals.size(); i != e; ++i) {
2716 Value *&Val = OutVals[i];
2717 ISD::ArgFlagsTy Flags = OutFlags[i];
2718 if (auto *CI = dyn_cast<ConstantInt>(Val)) {
2719 if (CI->getBitWidth() < 32) {
2720 if (Flags.isSExt())
2721 Val = ConstantExpr::getSExt(CI, Type::getInt32Ty(CI->getContext()));
2722 else
2723 Val = ConstantExpr::getZExt(CI, Type::getInt32Ty(CI->getContext()));
2724 }
2725 }
2726
2727 // Passing bools around ends up doing a trunc to i1 and passing it.
2728 // Codegen this as an argument + "and 1".
2729 if (auto *TI = dyn_cast<TruncInst>(Val)) {
2730 if (TI->getType()->isIntegerTy(1) && CLI.CS &&
2731 (TI->getParent() == CLI.CS->getInstruction()->getParent()) &&
2732 TI->hasOneUse()) {
2733 Val = cast<TruncInst>(Val)->getOperand(0);
2734 unsigned ResultReg = getRegForValue(Val);
2735
2736 if (!ResultReg)
2737 return false;
2738
2739 MVT ArgVT;
2740 if (!isTypeLegal(Val->getType(), ArgVT))
2741 return false;
2742
2743 ResultReg =
2744 FastEmit_ri(ArgVT, ArgVT, ISD::AND, ResultReg, Val->hasOneUse(), 1);
2745
2746 if (!ResultReg)
2747 return false;
2748 UpdateValueMap(Val, ResultReg);
2749 }
2750 }
2751 }
2752
2753 // Analyze operands of the call, assigning locations to each operand.
2754 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002755 CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, CLI.RetTy->getContext());
Juergen Ributzka23d43312014-07-15 06:35:47 +00002756
2757 // Allocate shadow area for Win64
2758 if (IsWin64)
2759 CCInfo.AllocateStack(32, 8);
2760
2761 SmallVector<MVT, 16> OutVTs;
2762 for (auto *Val : OutVals) {
2763 MVT VT;
2764 if (!isTypeLegal(Val->getType(), VT))
2765 return false;
2766 OutVTs.push_back(VT);
2767 }
2768 CCInfo.AnalyzeCallOperands(OutVTs, OutFlags, CC_X86);
2769
2770 // Get a count of how many bytes are to be pushed on the stack.
2771 unsigned NumBytes = CCInfo.getNextStackOffset();
2772
2773 // Issue CALLSEQ_START
2774 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
2775 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
2776 .addImm(NumBytes);
2777
2778 // Walk the register/memloc assignments, inserting copies/loads.
Eric Christopherd9134482014-08-04 21:25:23 +00002779 const X86RegisterInfo *RegInfo = static_cast<const X86RegisterInfo *>(
2780 TM.getSubtargetImpl()->getRegisterInfo());
Juergen Ributzka23d43312014-07-15 06:35:47 +00002781 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2782 CCValAssign const &VA = ArgLocs[i];
2783 const Value *ArgVal = OutVals[VA.getValNo()];
2784 MVT ArgVT = OutVTs[VA.getValNo()];
2785
2786 if (ArgVT == MVT::x86mmx)
2787 return false;
2788
2789 unsigned ArgReg = getRegForValue(ArgVal);
2790 if (!ArgReg)
2791 return false;
2792
2793 // Promote the value if needed.
2794 switch (VA.getLocInfo()) {
2795 case CCValAssign::Full: break;
2796 case CCValAssign::SExt: {
2797 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2798 "Unexpected extend");
2799 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(), ArgReg,
2800 ArgVT, ArgReg);
2801 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
2802 ArgVT = VA.getLocVT();
2803 break;
2804 }
2805 case CCValAssign::ZExt: {
2806 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2807 "Unexpected extend");
2808 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(), ArgReg,
2809 ArgVT, ArgReg);
2810 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
2811 ArgVT = VA.getLocVT();
2812 break;
2813 }
2814 case CCValAssign::AExt: {
2815 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2816 "Unexpected extend");
2817 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(), ArgReg,
2818 ArgVT, ArgReg);
2819 if (!Emitted)
2820 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(), ArgReg,
2821 ArgVT, ArgReg);
2822 if (!Emitted)
2823 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(), ArgReg,
2824 ArgVT, ArgReg);
2825
2826 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
2827 ArgVT = VA.getLocVT();
2828 break;
2829 }
2830 case CCValAssign::BCvt: {
2831 ArgReg = FastEmit_r(ArgVT, VA.getLocVT(), ISD::BITCAST, ArgReg,
2832 /*TODO: Kill=*/false);
2833 assert(ArgReg && "Failed to emit a bitcast!");
2834 ArgVT = VA.getLocVT();
2835 break;
2836 }
2837 case CCValAssign::VExt:
2838 // VExt has not been implemented, so this should be impossible to reach
2839 // for now. However, fallback to Selection DAG isel once implemented.
2840 return false;
2841 case CCValAssign::FPExt:
2842 llvm_unreachable("Unexpected loc info!");
2843 case CCValAssign::Indirect:
2844 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
2845 // support this.
2846 return false;
2847 }
2848
2849 if (VA.isRegLoc()) {
2850 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2851 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
2852 OutRegs.push_back(VA.getLocReg());
2853 } else {
2854 assert(VA.isMemLoc());
Juergen Ributzka39032672014-07-31 00:11:11 +00002855
2856 // Don't emit stores for undef values.
2857 if (isa<UndefValue>(ArgVal))
2858 continue;
2859
Juergen Ributzka23d43312014-07-15 06:35:47 +00002860 unsigned LocMemOffset = VA.getLocMemOffset();
2861 X86AddressMode AM;
2862 AM.Base.Reg = RegInfo->getStackRegister();
2863 AM.Disp = LocMemOffset;
2864 ISD::ArgFlagsTy Flags = OutFlags[VA.getValNo()];
2865 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
2866 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
2867 MachinePointerInfo::getStack(LocMemOffset), MachineMemOperand::MOStore,
2868 ArgVT.getStoreSize(), Alignment);
2869 if (Flags.isByVal()) {
2870 X86AddressMode SrcAM;
2871 SrcAM.Base.Reg = ArgReg;
2872 if (!TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize()))
2873 return false;
2874 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
2875 // If this is a really simple value, emit this with the Value* version
2876 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
2877 // as it can cause us to reevaluate the argument.
2878 if (!X86FastEmitStore(ArgVT, ArgVal, AM, MMO))
2879 return false;
2880 } else {
2881 bool ValIsKill = hasTrivialKill(ArgVal);
2882 if (!X86FastEmitStore(ArgVT, ArgReg, ValIsKill, AM, MMO))
2883 return false;
2884 }
2885 }
2886 }
2887
2888 // ELF / PIC requires GOT in the EBX register before function calls via PLT
2889 // GOT pointer.
2890 if (Subtarget->isPICStyleGOT()) {
2891 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
2892 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2893 TII.get(TargetOpcode::COPY), X86::EBX).addReg(Base);
2894 }
2895
2896 if (Is64Bit && IsVarArg && !IsWin64) {
2897 // From AMD64 ABI document:
2898 // For calls that may call functions that use varargs or stdargs
2899 // (prototype-less calls or calls to functions containing ellipsis (...) in
2900 // the declaration) %al is used as hidden argument to specify the number
2901 // of SSE registers used. The contents of %al do not need to match exactly
2902 // the number of registers, but must be an ubound on the number of SSE
2903 // registers used and is in the range 0 - 8 inclusive.
2904
2905 // Count the number of XMM registers allocated.
2906 static const MCPhysReg XMMArgRegs[] = {
2907 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2908 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2909 };
2910 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
2911 assert((Subtarget->hasSSE1() || !NumXMMRegs)
2912 && "SSE registers cannot be used when SSE is disabled");
2913 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri),
2914 X86::AL).addImm(NumXMMRegs);
2915 }
2916
2917 // Materialize callee address in a register. FIXME: GV address can be
2918 // handled with a CALLpcrel32 instead.
2919 X86AddressMode CalleeAM;
2920 if (!X86SelectCallAddress(Callee, CalleeAM))
2921 return false;
2922
2923 unsigned CalleeOp = 0;
2924 const GlobalValue *GV = nullptr;
2925 if (CalleeAM.GV != nullptr) {
2926 GV = CalleeAM.GV;
2927 } else if (CalleeAM.Base.Reg != 0) {
2928 CalleeOp = CalleeAM.Base.Reg;
2929 } else
2930 return false;
2931
2932 // Issue the call.
2933 MachineInstrBuilder MIB;
2934 if (CalleeOp) {
2935 // Register-indirect call.
Andrea Di Biagio04d5a7b2014-07-15 10:53:44 +00002936 unsigned CallOpc = Is64Bit ? X86::CALL64r : X86::CALL32r;
Juergen Ributzka23d43312014-07-15 06:35:47 +00002937 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc))
2938 .addReg(CalleeOp);
2939 } else {
2940 // Direct call.
2941 assert(GV && "Not a direct call");
2942 unsigned CallOpc = Is64Bit ? X86::CALL64pcrel32 : X86::CALLpcrel32;
2943
2944 // See if we need any target-specific flags on the GV operand.
2945 unsigned char OpFlags = 0;
2946
2947 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2948 // external symbols most go through the PLT in PIC mode. If the symbol
2949 // has hidden or protected visibility, or if it is static or local, then
2950 // we don't need to use the PLT - we can directly call it.
2951 if (Subtarget->isTargetELF() &&
2952 TM.getRelocationModel() == Reloc::PIC_ &&
2953 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
2954 OpFlags = X86II::MO_PLT;
2955 } else if (Subtarget->isPICStyleStubAny() &&
2956 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2957 (!Subtarget->getTargetTriple().isMacOSX() ||
2958 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
2959 // PC-relative references to external symbols should go through $stub,
2960 // unless we're building with the leopard linker or later, which
2961 // automatically synthesizes these stubs.
2962 OpFlags = X86II::MO_DARWIN_STUB;
2963 }
2964
2965 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc));
2966 if (SymName)
2967 MIB.addExternalSymbol(SymName, OpFlags);
2968 else
2969 MIB.addGlobalAddress(GV, 0, OpFlags);
2970 }
2971
2972 // Add a register mask operand representing the call-preserved registers.
2973 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2974 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2975
2976 // Add an implicit use GOT pointer in EBX.
2977 if (Subtarget->isPICStyleGOT())
2978 MIB.addReg(X86::EBX, RegState::Implicit);
2979
2980 if (Is64Bit && IsVarArg && !IsWin64)
2981 MIB.addReg(X86::AL, RegState::Implicit);
2982
2983 // Add implicit physical register uses to the call.
2984 for (auto Reg : OutRegs)
2985 MIB.addReg(Reg, RegState::Implicit);
2986
2987 // Issue CALLSEQ_END
2988 unsigned NumBytesForCalleeToPop =
2989 computeBytesPoppedByCallee(Subtarget, CC, CLI.CS);
2990 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
2991 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
2992 .addImm(NumBytes).addImm(NumBytesForCalleeToPop);
2993
2994 // Now handle call return values.
2995 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002996 CCState CCRetInfo(CC, IsVarArg, *FuncInfo.MF, RVLocs,
Juergen Ributzka23d43312014-07-15 06:35:47 +00002997 CLI.RetTy->getContext());
2998 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
2999
3000 // Copy all of the result registers out of their specified physreg.
3001 unsigned ResultReg = FuncInfo.CreateRegs(CLI.RetTy);
3002 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3003 CCValAssign &VA = RVLocs[i];
3004 EVT CopyVT = VA.getValVT();
3005 unsigned CopyReg = ResultReg + i;
3006
3007 // If this is x86-64, and we disabled SSE, we can't return FP values
3008 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
3009 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
3010 report_fatal_error("SSE register return with SSE disabled");
3011 }
3012
Akira Hatanaka35166692014-08-01 22:19:41 +00003013 // If we prefer to use the value in xmm registers, copy it out as f80 and
3014 // use a truncate to move it from fp stack reg to xmm reg.
3015 if ((VA.getLocReg() == X86::FP0 || VA.getLocReg() == X86::FP1) &&
3016 isScalarFPTypeInSSEReg(VA.getValVT())) {
3017 CopyVT = MVT::f80;
3018 CopyReg = createResultReg(&X86::RFP80RegClass);
3019 }
Juergen Ributzka23d43312014-07-15 06:35:47 +00003020
Akira Hatanaka35166692014-08-01 22:19:41 +00003021 // Copy out the result.
3022 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3023 TII.get(TargetOpcode::COPY), CopyReg).addReg(VA.getLocReg());
3024 InRegs.push_back(VA.getLocReg());
3025
3026 // Round the f80 to the right size, which also moves it to the appropriate
3027 // xmm register. This is accomplished by storing the f80 value in memory
3028 // and then loading it back.
3029 if (CopyVT != VA.getValVT()) {
3030 EVT ResVT = VA.getValVT();
3031 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
3032 unsigned MemSize = ResVT.getSizeInBits()/8;
3033 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
3034 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3035 TII.get(Opc)), FI)
3036 .addReg(CopyReg);
3037 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
3038 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3039 TII.get(Opc), ResultReg + i), FI);
Juergen Ributzka23d43312014-07-15 06:35:47 +00003040 }
3041 }
3042
3043 CLI.ResultReg = ResultReg;
3044 CLI.NumResultRegs = RVLocs.size();
3045 CLI.Call = MIB;
3046
3047 return true;
3048}
Juergen Ributzka5ee9d902014-07-15 05:23:40 +00003049
Dan Gohmand58f3e32008-08-28 23:21:34 +00003050bool
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003051X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohmand58f3e32008-08-28 23:21:34 +00003052 switch (I->getOpcode()) {
3053 default: break;
Evan Chenga41ee292008-09-03 06:44:39 +00003054 case Instruction::Load:
Dan Gohman7bda51f2008-09-03 23:12:08 +00003055 return X86SelectLoad(I);
Owen Andersonb8c7ba22008-09-04 16:48:33 +00003056 case Instruction::Store:
3057 return X86SelectStore(I);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003058 case Instruction::Ret:
3059 return X86SelectRet(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00003060 case Instruction::ICmp:
3061 case Instruction::FCmp:
3062 return X86SelectCmp(I);
Dan Gohmana5753b32008-09-05 01:06:14 +00003063 case Instruction::ZExt:
3064 return X86SelectZExt(I);
3065 case Instruction::Br:
3066 return X86SelectBranch(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00003067 case Instruction::LShr:
3068 case Instruction::AShr:
3069 case Instruction::Shl:
3070 return X86SelectShift(I);
Eli Bendersky24a36eb2013-04-17 20:10:13 +00003071 case Instruction::SDiv:
3072 case Instruction::UDiv:
3073 case Instruction::SRem:
3074 case Instruction::URem:
3075 return X86SelectDivRem(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00003076 case Instruction::Select:
3077 return X86SelectSelect(I);
Evan Chengb9286692008-09-07 08:47:42 +00003078 case Instruction::Trunc:
3079 return X86SelectTrunc(I);
Dan Gohmanbf646f22008-09-10 21:02:08 +00003080 case Instruction::FPExt:
3081 return X86SelectFPExt(I);
3082 case Instruction::FPTrunc:
3083 return X86SelectFPTrunc(I);
Dan Gohmana62e4ab2009-03-13 23:53:06 +00003084 case Instruction::IntToPtr: // Deliberate fall-through.
3085 case Instruction::PtrToInt: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003086 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
3087 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohmana62e4ab2009-03-13 23:53:06 +00003088 if (DstVT.bitsGT(SrcVT))
3089 return X86SelectZExt(I);
3090 if (DstVT.bitsLT(SrcVT))
3091 return X86SelectTrunc(I);
3092 unsigned Reg = getRegForValue(I->getOperand(0));
3093 if (Reg == 0) return false;
3094 UpdateValueMap(I, Reg);
3095 return true;
3096 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00003097 }
3098
3099 return false;
3100}
3101
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003102unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00003103 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00003104 if (!isTypeLegal(C->getType(), VT))
Michael Liao3c898062012-08-30 00:30:16 +00003105 return 0;
3106
3107 // Can't handle alternate code models yet.
3108 if (TM.getCodeModel() != CodeModel::Small)
3109 return 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00003110
Owen Anderson50288e32008-09-05 00:06:23 +00003111 // Get opcode and regclass of the output for the given load instruction.
3112 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003113 const TargetRegisterClass *RC = nullptr;
Duncan Sandsf5dda012010-11-03 11:35:31 +00003114 switch (VT.SimpleTy) {
Michael Liao3c898062012-08-30 00:30:16 +00003115 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00003116 case MVT::i8:
Owen Anderson50288e32008-09-05 00:06:23 +00003117 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +00003118 RC = &X86::GR8RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003119 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003120 case MVT::i16:
Owen Anderson50288e32008-09-05 00:06:23 +00003121 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +00003122 RC = &X86::GR16RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003123 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003124 case MVT::i32:
Owen Anderson50288e32008-09-05 00:06:23 +00003125 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +00003126 RC = &X86::GR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003127 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003128 case MVT::i64:
Owen Anderson50288e32008-09-05 00:06:23 +00003129 // Must be in x86-64 mode.
3130 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +00003131 RC = &X86::GR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003132 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003133 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00003134 if (X86ScalarSSEf32) {
3135 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +00003136 RC = &X86::FR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003137 } else {
3138 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +00003139 RC = &X86::RFP32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003140 }
3141 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003142 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00003143 if (X86ScalarSSEf64) {
3144 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +00003145 RC = &X86::FR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003146 } else {
3147 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +00003148 RC = &X86::RFP64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00003149 }
3150 break;
Owen Anderson9f944592009-08-11 20:47:22 +00003151 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +00003152 // No f80 support yet.
Michael Liao3c898062012-08-30 00:30:16 +00003153 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00003154 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003155
Louis Gerbarg343f5cd2014-06-17 23:22:41 +00003156 // Materialize addresses with LEA/MOV instructions.
Owen Anderson50288e32008-09-05 00:06:23 +00003157 if (isa<GlobalValue>(C)) {
Dan Gohman9801ba42008-09-19 22:16:54 +00003158 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00003159 if (X86SelectAddress(C, AM)) {
Chris Lattner48326602011-04-17 17:12:08 +00003160 // If the expression is just a basereg, then we're done, otherwise we need
3161 // to emit an LEA.
3162 if (AM.BaseType == X86AddressMode::RegBase &&
Craig Topper062a2ba2014-04-25 05:30:21 +00003163 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == nullptr)
Chris Lattner48326602011-04-17 17:12:08 +00003164 return AM.Base.Reg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003165
Dan Gohman9801ba42008-09-19 22:16:54 +00003166 unsigned ResultReg = createResultReg(RC);
Louis Gerbarg343f5cd2014-06-17 23:22:41 +00003167 if (TM.getRelocationModel() == Reloc::Static &&
3168 TLI.getPointerTy() == MVT::i64) {
3169 // The displacement code be more than 32 bits away so we need to use
3170 // an instruction with a 64 bit immediate
3171 Opc = X86::MOV64ri;
3172 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3173 TII.get(Opc), ResultReg).addGlobalAddress(cast<GlobalValue>(C));
3174 } else {
3175 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
3176 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003177 TII.get(Opc), ResultReg), AM);
Louis Gerbarg343f5cd2014-06-17 23:22:41 +00003178 }
Owen Anderson50288e32008-09-05 00:06:23 +00003179 return ResultReg;
Dan Gohman9801ba42008-09-19 22:16:54 +00003180 }
Evan Chengf5bc7e52008-09-05 21:00:03 +00003181 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00003182 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003183
Owen Andersond41c7162008-09-06 01:11:01 +00003184 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +00003185 unsigned Align = DL.getPrefTypeAlignment(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00003186 if (Align == 0) {
3187 // Alignment of vector types. FIXME!
Rafael Espindolaea09c592014-02-18 22:05:46 +00003188 Align = DL.getTypeAllocSize(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00003189 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003190
Dan Gohman8392f0c2008-09-30 01:21:32 +00003191 // x86-32 PIC requires a PIC base register for constant pools.
3192 unsigned PICBase = 0;
Chris Lattnera3260c02009-06-27 01:31:51 +00003193 unsigned char OpFlag = 0;
Chris Lattner21c29402009-07-10 21:00:45 +00003194 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattnerfef11d62009-07-09 04:39:06 +00003195 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohman87fb4e82010-07-07 16:29:44 +00003196 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00003197 } else if (Subtarget->isPICStyleGOT()) {
3198 OpFlag = X86II::MO_GOTOFF;
Dan Gohman87fb4e82010-07-07 16:29:44 +00003199 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00003200 } else if (Subtarget->isPICStyleRIPRel() &&
3201 TM.getCodeModel() == CodeModel::Small) {
3202 PICBase = X86::RIP;
Chris Lattnera3260c02009-06-27 01:31:51 +00003203 }
Dan Gohman8392f0c2008-09-30 01:21:32 +00003204
3205 // Create the load from the constant pool.
Dan Gohman39d82f92008-09-10 20:11:02 +00003206 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman9801ba42008-09-19 22:16:54 +00003207 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003208 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003209 TII.get(Opc), ResultReg),
Chris Lattnera3260c02009-06-27 01:31:51 +00003210 MCPOffset, PICBase, OpFlag);
Dan Gohman8392f0c2008-09-30 01:21:32 +00003211
Owen Anderson50288e32008-09-05 00:06:23 +00003212 return ResultReg;
3213}
3214
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003215unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohmanb01a9c92008-10-03 01:27:49 +00003216 // Fail on dynamic allocas. At this point, getRegForValue has already
3217 // checked its CSE maps, so if we're here trying to handle a dynamic
3218 // alloca, we're not going to succeed. X86SelectAddress has a
3219 // check for dynamic allocas, because it's called directly from
3220 // various places, but TargetMaterializeAlloca also needs a check
3221 // in order to avoid recursion between getRegForValue,
3222 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohman87fb4e82010-07-07 16:29:44 +00003223 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohmanb01a9c92008-10-03 01:27:49 +00003224 return 0;
Reid Klecknerdfbed592014-01-31 23:45:12 +00003225 assert(C->isStaticAlloca() && "dynamic alloca in the static alloca map?");
Dan Gohmanb01a9c92008-10-03 01:27:49 +00003226
Dan Gohman39d82f92008-09-10 20:11:02 +00003227 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00003228 if (!X86SelectAddress(C, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +00003229 return 0;
3230 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
Craig Topper760b1342012-02-22 05:59:10 +00003231 const TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Dan Gohman39d82f92008-09-10 20:11:02 +00003232 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003233 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003234 TII.get(Opc), ResultReg), AM);
Dan Gohman39d82f92008-09-10 20:11:02 +00003235 return ResultReg;
3236}
3237
Eli Friedman406c4712011-04-27 22:41:55 +00003238unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
3239 MVT VT;
3240 if (!isTypeLegal(CF->getType(), VT))
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00003241 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00003242
3243 // Get opcode and regclass for the given zero.
3244 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003245 const TargetRegisterClass *RC = nullptr;
Eli Friedman406c4712011-04-27 22:41:55 +00003246 switch (VT.SimpleTy) {
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00003247 default: return 0;
Craig Topper490c45c2012-08-11 17:53:00 +00003248 case MVT::f32:
3249 if (X86ScalarSSEf32) {
3250 Opc = X86::FsFLD0SS;
3251 RC = &X86::FR32RegClass;
3252 } else {
3253 Opc = X86::LD_Fp032;
3254 RC = &X86::RFP32RegClass;
3255 }
3256 break;
3257 case MVT::f64:
3258 if (X86ScalarSSEf64) {
3259 Opc = X86::FsFLD0SD;
3260 RC = &X86::FR64RegClass;
3261 } else {
3262 Opc = X86::LD_Fp064;
3263 RC = &X86::RFP64RegClass;
3264 }
3265 break;
3266 case MVT::f80:
3267 // No f80 support yet.
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00003268 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00003269 }
3270
3271 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00003272 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
Eli Friedman406c4712011-04-27 22:41:55 +00003273 return ResultReg;
3274}
3275
3276
Eli Bendersky90dd3e72013-04-19 22:29:18 +00003277bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
3278 const LoadInst *LI) {
Juergen Ributzka349777d2014-06-12 23:27:57 +00003279 const Value *Ptr = LI->getPointerOperand();
Chris Lattnereeba0c72010-09-05 02:18:34 +00003280 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +00003281 if (!X86SelectAddress(Ptr, AM))
Chris Lattnereeba0c72010-09-05 02:18:34 +00003282 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00003283
Craig Topper55406d92012-08-11 17:46:16 +00003284 const X86InstrInfo &XII = (const X86InstrInfo&)TII;
Wesley Peck527da1b2010-11-23 03:31:01 +00003285
Rafael Espindolaea09c592014-02-18 22:05:46 +00003286 unsigned Size = DL.getTypeAllocSize(LI->getType());
Chris Lattnereeba0c72010-09-05 02:18:34 +00003287 unsigned Alignment = LI->getAlignment();
3288
Juergen Ributzka349777d2014-06-12 23:27:57 +00003289 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3290 Alignment = DL.getABITypeAlignment(LI->getType());
3291
Chris Lattnereeba0c72010-09-05 02:18:34 +00003292 SmallVector<MachineOperand, 8> AddrOps;
3293 AM.getFullAddress(AddrOps);
Wesley Peck527da1b2010-11-23 03:31:01 +00003294
Chris Lattnereeba0c72010-09-05 02:18:34 +00003295 MachineInstr *Result =
3296 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
Juergen Ributzka349777d2014-06-12 23:27:57 +00003297 if (!Result)
3298 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00003299
Juergen Ributzka349777d2014-06-12 23:27:57 +00003300 Result->addMemOperand(*FuncInfo.MF, createMachineMemOperandFor(LI));
Chris Lattner2d186572011-01-16 02:27:38 +00003301 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnereeba0c72010-09-05 02:18:34 +00003302 MI->eraseFromParent();
3303 return true;
3304}
3305
3306
Evan Cheng24422d42008-09-03 00:03:49 +00003307namespace llvm {
Bob Wilson3e6fa462012-08-03 04:06:28 +00003308 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
3309 const TargetLibraryInfo *libInfo) {
3310 return new X86FastISel(funcInfo, libInfo);
Evan Cheng24422d42008-09-03 00:03:49 +00003311 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00003312}