1 //===-- X86FastISel.cpp - X86 FastISel implementation ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
17 #include "X86InstrBuilder.h"
18 #include "X86RegisterInfo.h"
19 #include "X86Subtarget.h"
20 #include "X86TargetMachine.h"
21 #include "llvm/CallingConv.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/GlobalVariable.h"
24 #include "llvm/Instructions.h"
25 #include "llvm/IntrinsicInst.h"
26 #include "llvm/CodeGen/Analysis.h"
27 #include "llvm/CodeGen/FastISel.h"
28 #include "llvm/CodeGen/FunctionLoweringInfo.h"
29 #include "llvm/CodeGen/MachineConstantPool.h"
30 #include "llvm/CodeGen/MachineFrameInfo.h"
31 #include "llvm/CodeGen/MachineRegisterInfo.h"
32 #include "llvm/Support/CallSite.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/GetElementPtrTypeIterator.h"
35 #include "llvm/Target/TargetOptions.h"
40 class X86FastISel
: public FastISel
{
41 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
42 /// make the right decision when generating code for different targets.
43 const X86Subtarget
*Subtarget
;
45 /// StackPtr - Register used as the stack pointer.
49 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
50 /// floating point ops.
51 /// When SSE is available, use it for f32 operations.
52 /// When SSE2 is available, use it for f64 operations.
57 explicit X86FastISel(FunctionLoweringInfo
&funcInfo
) : FastISel(funcInfo
) {
58 Subtarget
= &TM
.getSubtarget
<X86Subtarget
>();
59 StackPtr
= Subtarget
->is64Bit() ? X86::RSP
: X86::ESP
;
60 X86ScalarSSEf64
= Subtarget
->hasSSE2();
61 X86ScalarSSEf32
= Subtarget
->hasSSE1();
64 virtual bool TargetSelectInstruction(const Instruction
*I
);
66 /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
67 /// vreg is being provided by the specified load instruction. If possible,
68 /// try to fold the load as an operand to the instruction, returning true if
70 virtual bool TryToFoldLoad(MachineInstr
*MI
, unsigned OpNo
,
73 #include "X86GenFastISel.inc"
76 bool X86FastEmitCompare(const Value
*LHS
, const Value
*RHS
, EVT VT
);
78 bool X86FastEmitLoad(EVT VT
, const X86AddressMode
&AM
, unsigned &RR
);
80 bool X86FastEmitStore(EVT VT
, const Value
*Val
,
81 const X86AddressMode
&AM
);
82 bool X86FastEmitStore(EVT VT
, unsigned Val
,
83 const X86AddressMode
&AM
);
85 bool X86FastEmitExtend(ISD::NodeType Opc
, EVT DstVT
, unsigned Src
, EVT SrcVT
,
88 bool X86SelectAddress(const Value
*V
, X86AddressMode
&AM
);
89 bool X86SelectCallAddress(const Value
*V
, X86AddressMode
&AM
);
91 bool X86SelectLoad(const Instruction
*I
);
93 bool X86SelectStore(const Instruction
*I
);
95 bool X86SelectRet(const Instruction
*I
);
97 bool X86SelectCmp(const Instruction
*I
);
99 bool X86SelectZExt(const Instruction
*I
);
101 bool X86SelectBranch(const Instruction
*I
);
103 bool X86SelectShift(const Instruction
*I
);
105 bool X86SelectSelect(const Instruction
*I
);
107 bool X86SelectTrunc(const Instruction
*I
);
109 bool X86SelectFPExt(const Instruction
*I
);
110 bool X86SelectFPTrunc(const Instruction
*I
);
112 bool X86SelectExtractValue(const Instruction
*I
);
114 bool X86VisitIntrinsicCall(const IntrinsicInst
&I
);
115 bool X86SelectCall(const Instruction
*I
);
117 const X86InstrInfo
*getInstrInfo() const {
118 return getTargetMachine()->getInstrInfo();
120 const X86TargetMachine
*getTargetMachine() const {
121 return static_cast<const X86TargetMachine
*>(&TM
);
124 unsigned TargetMaterializeConstant(const Constant
*C
);
126 unsigned TargetMaterializeAlloca(const AllocaInst
*C
);
128 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
129 /// computed in an SSE register, not on the X87 floating point stack.
130 bool isScalarFPTypeInSSEReg(EVT VT
) const {
131 return (VT
== MVT::f64
&& X86ScalarSSEf64
) || // f64 is when SSE2
132 (VT
== MVT::f32
&& X86ScalarSSEf32
); // f32 is when SSE1
135 bool isTypeLegal(const Type
*Ty
, MVT
&VT
, bool AllowI1
= false);
138 } // end anonymous namespace.
140 bool X86FastISel::isTypeLegal(const Type
*Ty
, MVT
&VT
, bool AllowI1
) {
141 EVT evt
= TLI
.getValueType(Ty
, /*HandleUnknown=*/true);
142 if (evt
== MVT::Other
|| !evt
.isSimple())
143 // Unhandled type. Halt "fast" selection and bail.
146 VT
= evt
.getSimpleVT();
147 // For now, require SSE/SSE2 for performing floating-point operations,
148 // since x87 requires additional work.
149 if (VT
== MVT::f64
&& !X86ScalarSSEf64
)
151 if (VT
== MVT::f32
&& !X86ScalarSSEf32
)
153 // Similarly, no f80 support yet.
156 // We only handle legal types. For example, on x86-32 the instruction
157 // selector contains all of the 64-bit instructions from x86-64,
158 // under the assumption that i64 won't be used if the target doesn't
160 return (AllowI1
&& VT
== MVT::i1
) || TLI
.isTypeLegal(VT
);
163 #include "X86GenCallingConv.inc"
165 /// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
166 /// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
167 /// Return true and the result register by reference if it is possible.
168 bool X86FastISel::X86FastEmitLoad(EVT VT
, const X86AddressMode
&AM
,
169 unsigned &ResultReg
) {
170 // Get opcode and regclass of the output for the given load instruction.
172 const TargetRegisterClass
*RC
= NULL
;
173 switch (VT
.getSimpleVT().SimpleTy
) {
174 default: return false;
178 RC
= X86::GR8RegisterClass
;
182 RC
= X86::GR16RegisterClass
;
186 RC
= X86::GR32RegisterClass
;
189 // Must be in x86-64 mode.
191 RC
= X86::GR64RegisterClass
;
194 if (Subtarget
->hasSSE1()) {
196 RC
= X86::FR32RegisterClass
;
199 RC
= X86::RFP32RegisterClass
;
203 if (Subtarget
->hasSSE2()) {
205 RC
= X86::FR64RegisterClass
;
208 RC
= X86::RFP64RegisterClass
;
212 // No f80 support yet.
216 ResultReg
= createResultReg(RC
);
217 addFullAddress(BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
,
218 DL
, TII
.get(Opc
), ResultReg
), AM
);
222 /// X86FastEmitStore - Emit a machine instruction to store a value Val of
223 /// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
224 /// and a displacement offset, or a GlobalAddress,
225 /// i.e. V. Return true if it is possible.
227 X86FastISel::X86FastEmitStore(EVT VT
, unsigned Val
,
228 const X86AddressMode
&AM
) {
229 // Get opcode and regclass of the output for the given store instruction.
231 switch (VT
.getSimpleVT().SimpleTy
) {
232 case MVT::f80
: // No f80 support yet.
233 default: return false;
235 // Mask out all but lowest bit.
236 unsigned AndResult
= createResultReg(X86::GR8RegisterClass
);
237 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
,
238 TII
.get(X86::AND8ri
), AndResult
).addReg(Val
).addImm(1);
241 // FALLTHROUGH, handling i1 as i8.
242 case MVT::i8
: Opc
= X86::MOV8mr
; break;
243 case MVT::i16
: Opc
= X86::MOV16mr
; break;
244 case MVT::i32
: Opc
= X86::MOV32mr
; break;
245 case MVT::i64
: Opc
= X86::MOV64mr
; break; // Must be in x86-64 mode.
247 Opc
= Subtarget
->hasSSE1() ? X86::MOVSSmr
: X86::ST_Fp32m
;
250 Opc
= Subtarget
->hasSSE2() ? X86::MOVSDmr
: X86::ST_Fp64m
;
254 addFullAddress(BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
,
255 DL
, TII
.get(Opc
)), AM
).addReg(Val
);
259 bool X86FastISel::X86FastEmitStore(EVT VT
, const Value
*Val
,
260 const X86AddressMode
&AM
) {
261 // Handle 'null' like i32/i64 0.
262 if (isa
<ConstantPointerNull
>(Val
))
263 Val
= Constant::getNullValue(TD
.getIntPtrType(Val
->getContext()));
265 // If this is a store of a simple constant, fold the constant into the store.
266 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(Val
)) {
269 switch (VT
.getSimpleVT().SimpleTy
) {
271 case MVT::i1
: Signed
= false; // FALLTHROUGH to handle as i8.
272 case MVT::i8
: Opc
= X86::MOV8mi
; break;
273 case MVT::i16
: Opc
= X86::MOV16mi
; break;
274 case MVT::i32
: Opc
= X86::MOV32mi
; break;
276 // Must be a 32-bit sign extended value.
277 if ((int)CI
->getSExtValue() == CI
->getSExtValue())
278 Opc
= X86::MOV64mi32
;
283 addFullAddress(BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
,
284 DL
, TII
.get(Opc
)), AM
)
285 .addImm(Signed
? (uint64_t) CI
->getSExtValue() :
291 unsigned ValReg
= getRegForValue(Val
);
295 return X86FastEmitStore(VT
, ValReg
, AM
);
298 /// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
299 /// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
300 /// ISD::SIGN_EXTEND).
301 bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc
, EVT DstVT
,
302 unsigned Src
, EVT SrcVT
,
303 unsigned &ResultReg
) {
304 unsigned RR
= FastEmit_r(SrcVT
.getSimpleVT(), DstVT
.getSimpleVT(), Opc
,
305 Src
, /*TODO: Kill=*/false);
314 /// X86SelectAddress - Attempt to fill in an address from the given value.
316 bool X86FastISel::X86SelectAddress(const Value
*V
, X86AddressMode
&AM
) {
317 const User
*U
= NULL
;
318 unsigned Opcode
= Instruction::UserOp1
;
319 if (const Instruction
*I
= dyn_cast
<Instruction
>(V
)) {
320 // Don't walk into other basic blocks; it's possible we haven't
321 // visited them yet, so the instructions may not yet be assigned
322 // virtual registers.
323 if (FuncInfo
.MBBMap
[I
->getParent()] != FuncInfo
.MBB
)
326 Opcode
= I
->getOpcode();
328 } else if (const ConstantExpr
*C
= dyn_cast
<ConstantExpr
>(V
)) {
329 Opcode
= C
->getOpcode();
333 if (const PointerType
*Ty
= dyn_cast
<PointerType
>(V
->getType()))
334 if (Ty
->getAddressSpace() > 255)
335 // Fast instruction selection doesn't support the special
341 case Instruction::BitCast
:
342 // Look past bitcasts.
343 return X86SelectAddress(U
->getOperand(0), AM
);
345 case Instruction::IntToPtr
:
346 // Look past no-op inttoptrs.
347 if (TLI
.getValueType(U
->getOperand(0)->getType()) == TLI
.getPointerTy())
348 return X86SelectAddress(U
->getOperand(0), AM
);
351 case Instruction::PtrToInt
:
352 // Look past no-op ptrtoints.
353 if (TLI
.getValueType(U
->getType()) == TLI
.getPointerTy())
354 return X86SelectAddress(U
->getOperand(0), AM
);
357 case Instruction::Alloca
: {
358 // Do static allocas.
359 const AllocaInst
*A
= cast
<AllocaInst
>(V
);
360 DenseMap
<const AllocaInst
*, int>::iterator SI
=
361 FuncInfo
.StaticAllocaMap
.find(A
);
362 if (SI
!= FuncInfo
.StaticAllocaMap
.end()) {
363 AM
.BaseType
= X86AddressMode::FrameIndexBase
;
364 AM
.Base
.FrameIndex
= SI
->second
;
370 case Instruction::Add
: {
371 // Adds of constants are common and easy enough.
372 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(U
->getOperand(1))) {
373 uint64_t Disp
= (int32_t)AM
.Disp
+ (uint64_t)CI
->getSExtValue();
374 // They have to fit in the 32-bit signed displacement field though.
375 if (isInt
<32>(Disp
)) {
376 AM
.Disp
= (uint32_t)Disp
;
377 return X86SelectAddress(U
->getOperand(0), AM
);
383 case Instruction::GetElementPtr
: {
384 X86AddressMode SavedAM
= AM
;
386 // Pattern-match simple GEPs.
387 uint64_t Disp
= (int32_t)AM
.Disp
;
388 unsigned IndexReg
= AM
.IndexReg
;
389 unsigned Scale
= AM
.Scale
;
390 gep_type_iterator GTI
= gep_type_begin(U
);
391 // Iterate through the indices, folding what we can. Constants can be
392 // folded, and one dynamic index can be handled, if the scale is supported.
393 for (User::const_op_iterator i
= U
->op_begin() + 1, e
= U
->op_end();
394 i
!= e
; ++i
, ++GTI
) {
395 const Value
*Op
= *i
;
396 if (const StructType
*STy
= dyn_cast
<StructType
>(*GTI
)) {
397 const StructLayout
*SL
= TD
.getStructLayout(STy
);
398 unsigned Idx
= cast
<ConstantInt
>(Op
)->getZExtValue();
399 Disp
+= SL
->getElementOffset(Idx
);
401 uint64_t S
= TD
.getTypeAllocSize(GTI
.getIndexedType());
402 SmallVector
<const Value
*, 4> Worklist
;
403 Worklist
.push_back(Op
);
405 Op
= Worklist
.pop_back_val();
406 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(Op
)) {
407 // Constant-offset addressing.
408 Disp
+= CI
->getSExtValue() * S
;
409 } else if (isa
<AddOperator
>(Op
) &&
410 isa
<ConstantInt
>(cast
<AddOperator
>(Op
)->getOperand(1))) {
411 // An add with a constant operand. Fold the constant.
413 cast
<ConstantInt
>(cast
<AddOperator
>(Op
)->getOperand(1));
414 Disp
+= CI
->getSExtValue() * S
;
415 // Add the other operand back to the work list.
416 Worklist
.push_back(cast
<AddOperator
>(Op
)->getOperand(0));
417 } else if (IndexReg
== 0 &&
418 (!AM
.GV
|| !Subtarget
->isPICStyleRIPRel()) &&
419 (S
== 1 || S
== 2 || S
== 4 || S
== 8)) {
420 // Scaled-index addressing.
422 IndexReg
= getRegForGEPIndex(Op
).first
;
427 goto unsupported_gep
;
428 } while (!Worklist
.empty());
431 // Check for displacement overflow.
432 if (!isInt
<32>(Disp
))
434 // Ok, the GEP indices were covered by constant-offset and scaled-index
435 // addressing. Update the address state and move on to examining the base.
436 AM
.IndexReg
= IndexReg
;
438 AM
.Disp
= (uint32_t)Disp
;
439 if (X86SelectAddress(U
->getOperand(0), AM
))
442 // If we couldn't merge the sub value into this addr mode, revert back to
443 // our address and just match the value instead of completely failing.
447 // Ok, the GEP indices weren't all covered.
452 // Handle constant address.
453 if (const GlobalValue
*GV
= dyn_cast
<GlobalValue
>(V
)) {
454 // Can't handle alternate code models yet.
455 if (TM
.getCodeModel() != CodeModel::Small
)
458 // RIP-relative addresses can't have additional register operands.
459 if (Subtarget
->isPICStyleRIPRel() &&
460 (AM
.Base
.Reg
!= 0 || AM
.IndexReg
!= 0))
463 // Can't handle TLS yet.
464 if (const GlobalVariable
*GVar
= dyn_cast
<GlobalVariable
>(GV
))
465 if (GVar
->isThreadLocal())
468 // Okay, we've committed to selecting this global. Set up the basic address.
471 // Allow the subtarget to classify the global.
472 unsigned char GVFlags
= Subtarget
->ClassifyGlobalReference(GV
, TM
);
474 // If this reference is relative to the pic base, set it now.
475 if (isGlobalRelativeToPICBase(GVFlags
)) {
476 // FIXME: How do we know Base.Reg is free??
477 AM
.Base
.Reg
= getInstrInfo()->getGlobalBaseReg(FuncInfo
.MF
);
480 // Unless the ABI requires an extra load, return a direct reference to
482 if (!isGlobalStubReference(GVFlags
)) {
483 if (Subtarget
->isPICStyleRIPRel()) {
484 // Use rip-relative addressing if we can. Above we verified that the
485 // base and index registers are unused.
486 assert(AM
.Base
.Reg
== 0 && AM
.IndexReg
== 0);
487 AM
.Base
.Reg
= X86::RIP
;
489 AM
.GVOpFlags
= GVFlags
;
493 // Ok, we need to do a load from a stub. If we've already loaded from this
494 // stub, reuse the loaded pointer, otherwise emit the load now.
495 DenseMap
<const Value
*, unsigned>::iterator I
= LocalValueMap
.find(V
);
497 if (I
!= LocalValueMap
.end() && I
->second
!= 0) {
500 // Issue load from stub.
502 const TargetRegisterClass
*RC
= NULL
;
503 X86AddressMode StubAM
;
504 StubAM
.Base
.Reg
= AM
.Base
.Reg
;
506 StubAM
.GVOpFlags
= GVFlags
;
508 // Prepare for inserting code in the local-value area.
509 SavePoint SaveInsertPt
= enterLocalValueArea();
511 if (TLI
.getPointerTy() == MVT::i64
) {
513 RC
= X86::GR64RegisterClass
;
515 if (Subtarget
->isPICStyleRIPRel())
516 StubAM
.Base
.Reg
= X86::RIP
;
519 RC
= X86::GR32RegisterClass
;
522 LoadReg
= createResultReg(RC
);
523 MachineInstrBuilder LoadMI
=
524 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(Opc
), LoadReg
);
525 addFullAddress(LoadMI
, StubAM
);
527 // Ok, back to normal mode.
528 leaveLocalValueArea(SaveInsertPt
);
530 // Prevent loading GV stub multiple times in same MBB.
531 LocalValueMap
[V
] = LoadReg
;
534 // Now construct the final address. Note that the Disp, Scale,
535 // and Index values may already be set here.
536 AM
.Base
.Reg
= LoadReg
;
541 // If all else fails, try to materialize the value in a register.
542 if (!AM
.GV
|| !Subtarget
->isPICStyleRIPRel()) {
543 if (AM
.Base
.Reg
== 0) {
544 AM
.Base
.Reg
= getRegForValue(V
);
545 return AM
.Base
.Reg
!= 0;
547 if (AM
.IndexReg
== 0) {
548 assert(AM
.Scale
== 1 && "Scale with no index!");
549 AM
.IndexReg
= getRegForValue(V
);
550 return AM
.IndexReg
!= 0;
557 /// X86SelectCallAddress - Attempt to fill in an address from the given value.
559 bool X86FastISel::X86SelectCallAddress(const Value
*V
, X86AddressMode
&AM
) {
560 const User
*U
= NULL
;
561 unsigned Opcode
= Instruction::UserOp1
;
562 if (const Instruction
*I
= dyn_cast
<Instruction
>(V
)) {
563 Opcode
= I
->getOpcode();
565 } else if (const ConstantExpr
*C
= dyn_cast
<ConstantExpr
>(V
)) {
566 Opcode
= C
->getOpcode();
572 case Instruction::BitCast
:
573 // Look past bitcasts.
574 return X86SelectCallAddress(U
->getOperand(0), AM
);
576 case Instruction::IntToPtr
:
577 // Look past no-op inttoptrs.
578 if (TLI
.getValueType(U
->getOperand(0)->getType()) == TLI
.getPointerTy())
579 return X86SelectCallAddress(U
->getOperand(0), AM
);
582 case Instruction::PtrToInt
:
583 // Look past no-op ptrtoints.
584 if (TLI
.getValueType(U
->getType()) == TLI
.getPointerTy())
585 return X86SelectCallAddress(U
->getOperand(0), AM
);
589 // Handle constant address.
590 if (const GlobalValue
*GV
= dyn_cast
<GlobalValue
>(V
)) {
591 // Can't handle alternate code models yet.
592 if (TM
.getCodeModel() != CodeModel::Small
)
595 // RIP-relative addresses can't have additional register operands.
596 if (Subtarget
->isPICStyleRIPRel() &&
597 (AM
.Base
.Reg
!= 0 || AM
.IndexReg
!= 0))
600 // Can't handle TLS or DLLImport.
601 if (const GlobalVariable
*GVar
= dyn_cast
<GlobalVariable
>(GV
))
602 if (GVar
->isThreadLocal() || GVar
->hasDLLImportLinkage())
605 // Okay, we've committed to selecting this global. Set up the basic address.
608 // No ABI requires an extra load for anything other than DLLImport, which
609 // we rejected above. Return a direct reference to the global.
610 if (Subtarget
->isPICStyleRIPRel()) {
611 // Use rip-relative addressing if we can. Above we verified that the
612 // base and index registers are unused.
613 assert(AM
.Base
.Reg
== 0 && AM
.IndexReg
== 0);
614 AM
.Base
.Reg
= X86::RIP
;
615 } else if (Subtarget
->isPICStyleStubPIC()) {
616 AM
.GVOpFlags
= X86II::MO_PIC_BASE_OFFSET
;
617 } else if (Subtarget
->isPICStyleGOT()) {
618 AM
.GVOpFlags
= X86II::MO_GOTOFF
;
624 // If all else fails, try to materialize the value in a register.
625 if (!AM
.GV
|| !Subtarget
->isPICStyleRIPRel()) {
626 if (AM
.Base
.Reg
== 0) {
627 AM
.Base
.Reg
= getRegForValue(V
);
628 return AM
.Base
.Reg
!= 0;
630 if (AM
.IndexReg
== 0) {
631 assert(AM
.Scale
== 1 && "Scale with no index!");
632 AM
.IndexReg
= getRegForValue(V
);
633 return AM
.IndexReg
!= 0;
641 /// X86SelectStore - Select and emit code to implement store instructions.
642 bool X86FastISel::X86SelectStore(const Instruction
*I
) {
644 if (!isTypeLegal(I
->getOperand(0)->getType(), VT
, /*AllowI1=*/true))
648 if (!X86SelectAddress(I
->getOperand(1), AM
))
651 return X86FastEmitStore(VT
, I
->getOperand(0), AM
);
654 /// X86SelectRet - Select and emit code to implement ret instructions.
655 bool X86FastISel::X86SelectRet(const Instruction
*I
) {
656 const ReturnInst
*Ret
= cast
<ReturnInst
>(I
);
657 const Function
&F
= *I
->getParent()->getParent();
659 if (!FuncInfo
.CanLowerReturn
)
662 CallingConv::ID CC
= F
.getCallingConv();
663 if (CC
!= CallingConv::C
&&
664 CC
!= CallingConv::Fast
&&
665 CC
!= CallingConv::X86_FastCall
)
668 if (Subtarget
->isTargetWin64())
671 // Don't handle popping bytes on return for now.
672 if (FuncInfo
.MF
->getInfo
<X86MachineFunctionInfo
>()
673 ->getBytesToPopOnReturn() != 0)
676 // fastcc with -tailcallopt is intended to provide a guaranteed
677 // tail call optimization. Fastisel doesn't know how to do that.
678 if (CC
== CallingConv::Fast
&& GuaranteedTailCallOpt
)
681 // Let SDISel handle vararg functions.
685 if (Ret
->getNumOperands() > 0) {
686 SmallVector
<ISD::OutputArg
, 4> Outs
;
687 GetReturnInfo(F
.getReturnType(), F
.getAttributes().getRetAttributes(),
690 // Analyze operands of the call, assigning locations to each operand.
691 SmallVector
<CCValAssign
, 16> ValLocs
;
692 CCState
CCInfo(CC
, F
.isVarArg(), TM
, ValLocs
, I
->getContext());
693 CCInfo
.AnalyzeReturn(Outs
, RetCC_X86
);
695 const Value
*RV
= Ret
->getOperand(0);
696 unsigned Reg
= getRegForValue(RV
);
700 // Only handle a single return value for now.
701 if (ValLocs
.size() != 1)
704 CCValAssign
&VA
= ValLocs
[0];
706 // Don't bother handling odd stuff for now.
707 if (VA
.getLocInfo() != CCValAssign::Full
)
709 // Only handle register returns for now.
712 // TODO: For now, don't try to handle cases where getLocInfo()
713 // says Full but the types don't match.
714 if (VA
.getValVT() != TLI
.getValueType(RV
->getType()))
717 // The calling-convention tables for x87 returns don't tell
719 if (VA
.getLocReg() == X86::ST0
|| VA
.getLocReg() == X86::ST1
)
723 unsigned SrcReg
= Reg
+ VA
.getValNo();
724 unsigned DstReg
= VA
.getLocReg();
725 const TargetRegisterClass
* SrcRC
= MRI
.getRegClass(SrcReg
);
726 // Avoid a cross-class copy. This is very unlikely.
727 if (!SrcRC
->contains(DstReg
))
729 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(TargetOpcode::COPY
),
730 DstReg
).addReg(SrcReg
);
732 // Mark the register as live out of the function.
733 MRI
.addLiveOut(VA
.getLocReg());
737 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(X86::RET
));
741 /// X86SelectLoad - Select and emit code to implement load instructions.
743 bool X86FastISel::X86SelectLoad(const Instruction
*I
) {
745 if (!isTypeLegal(I
->getType(), VT
, /*AllowI1=*/true))
749 if (!X86SelectAddress(I
->getOperand(0), AM
))
752 unsigned ResultReg
= 0;
753 if (X86FastEmitLoad(VT
, AM
, ResultReg
)) {
754 UpdateValueMap(I
, ResultReg
);
760 static unsigned X86ChooseCmpOpcode(EVT VT
, const X86Subtarget
*Subtarget
) {
761 switch (VT
.getSimpleVT().SimpleTy
) {
763 case MVT::i8
: return X86::CMP8rr
;
764 case MVT::i16
: return X86::CMP16rr
;
765 case MVT::i32
: return X86::CMP32rr
;
766 case MVT::i64
: return X86::CMP64rr
;
767 case MVT::f32
: return Subtarget
->hasSSE1() ? X86::UCOMISSrr
: 0;
768 case MVT::f64
: return Subtarget
->hasSSE2() ? X86::UCOMISDrr
: 0;
772 /// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
773 /// of the comparison, return an opcode that works for the compare (e.g.
774 /// CMP32ri) otherwise return 0.
775 static unsigned X86ChooseCmpImmediateOpcode(EVT VT
, const ConstantInt
*RHSC
) {
776 switch (VT
.getSimpleVT().SimpleTy
) {
777 // Otherwise, we can't fold the immediate into this comparison.
779 case MVT::i8
: return X86::CMP8ri
;
780 case MVT::i16
: return X86::CMP16ri
;
781 case MVT::i32
: return X86::CMP32ri
;
783 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
785 if ((int)RHSC
->getSExtValue() == RHSC
->getSExtValue())
786 return X86::CMP64ri32
;
791 bool X86FastISel::X86FastEmitCompare(const Value
*Op0
, const Value
*Op1
,
793 unsigned Op0Reg
= getRegForValue(Op0
);
794 if (Op0Reg
== 0) return false;
796 // Handle 'null' like i32/i64 0.
797 if (isa
<ConstantPointerNull
>(Op1
))
798 Op1
= Constant::getNullValue(TD
.getIntPtrType(Op0
->getContext()));
800 // We have two options: compare with register or immediate. If the RHS of
801 // the compare is an immediate that we can fold into this compare, use
802 // CMPri, otherwise use CMPrr.
803 if (const ConstantInt
*Op1C
= dyn_cast
<ConstantInt
>(Op1
)) {
804 if (unsigned CompareImmOpc
= X86ChooseCmpImmediateOpcode(VT
, Op1C
)) {
805 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(CompareImmOpc
))
807 .addImm(Op1C
->getSExtValue());
812 unsigned CompareOpc
= X86ChooseCmpOpcode(VT
, Subtarget
);
813 if (CompareOpc
== 0) return false;
815 unsigned Op1Reg
= getRegForValue(Op1
);
816 if (Op1Reg
== 0) return false;
817 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(CompareOpc
))
824 bool X86FastISel::X86SelectCmp(const Instruction
*I
) {
825 const CmpInst
*CI
= cast
<CmpInst
>(I
);
828 if (!isTypeLegal(I
->getOperand(0)->getType(), VT
))
831 unsigned ResultReg
= createResultReg(&X86::GR8RegClass
);
833 bool SwapArgs
; // false -> compare Op0, Op1. true -> compare Op1, Op0.
834 switch (CI
->getPredicate()) {
835 case CmpInst::FCMP_OEQ
: {
836 if (!X86FastEmitCompare(CI
->getOperand(0), CI
->getOperand(1), VT
))
839 unsigned EReg
= createResultReg(&X86::GR8RegClass
);
840 unsigned NPReg
= createResultReg(&X86::GR8RegClass
);
841 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(X86::SETEr
), EReg
);
842 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
,
843 TII
.get(X86::SETNPr
), NPReg
);
844 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
,
845 TII
.get(X86::AND8rr
), ResultReg
).addReg(NPReg
).addReg(EReg
);
846 UpdateValueMap(I
, ResultReg
);
849 case CmpInst::FCMP_UNE
: {
850 if (!X86FastEmitCompare(CI
->getOperand(0), CI
->getOperand(1), VT
))
853 unsigned NEReg
= createResultReg(&X86::GR8RegClass
);
854 unsigned PReg
= createResultReg(&X86::GR8RegClass
);
855 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
,
856 TII
.get(X86::SETNEr
), NEReg
);
857 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
,
858 TII
.get(X86::SETPr
), PReg
);
859 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
,
860 TII
.get(X86::OR8rr
), ResultReg
)
861 .addReg(PReg
).addReg(NEReg
);
862 UpdateValueMap(I
, ResultReg
);
865 case CmpInst::FCMP_OGT
: SwapArgs
= false; SetCCOpc
= X86::SETAr
; break;
866 case CmpInst::FCMP_OGE
: SwapArgs
= false; SetCCOpc
= X86::SETAEr
; break;
867 case CmpInst::FCMP_OLT
: SwapArgs
= true; SetCCOpc
= X86::SETAr
; break;
868 case CmpInst::FCMP_OLE
: SwapArgs
= true; SetCCOpc
= X86::SETAEr
; break;
869 case CmpInst::FCMP_ONE
: SwapArgs
= false; SetCCOpc
= X86::SETNEr
; break;
870 case CmpInst::FCMP_ORD
: SwapArgs
= false; SetCCOpc
= X86::SETNPr
; break;
871 case CmpInst::FCMP_UNO
: SwapArgs
= false; SetCCOpc
= X86::SETPr
; break;
872 case CmpInst::FCMP_UEQ
: SwapArgs
= false; SetCCOpc
= X86::SETEr
; break;
873 case CmpInst::FCMP_UGT
: SwapArgs
= true; SetCCOpc
= X86::SETBr
; break;
874 case CmpInst::FCMP_UGE
: SwapArgs
= true; SetCCOpc
= X86::SETBEr
; break;
875 case CmpInst::FCMP_ULT
: SwapArgs
= false; SetCCOpc
= X86::SETBr
; break;
876 case CmpInst::FCMP_ULE
: SwapArgs
= false; SetCCOpc
= X86::SETBEr
; break;
878 case CmpInst::ICMP_EQ
: SwapArgs
= false; SetCCOpc
= X86::SETEr
; break;
879 case CmpInst::ICMP_NE
: SwapArgs
= false; SetCCOpc
= X86::SETNEr
; break;
880 case CmpInst::ICMP_UGT
: SwapArgs
= false; SetCCOpc
= X86::SETAr
; break;
881 case CmpInst::ICMP_UGE
: SwapArgs
= false; SetCCOpc
= X86::SETAEr
; break;
882 case CmpInst::ICMP_ULT
: SwapArgs
= false; SetCCOpc
= X86::SETBr
; break;
883 case CmpInst::ICMP_ULE
: SwapArgs
= false; SetCCOpc
= X86::SETBEr
; break;
884 case CmpInst::ICMP_SGT
: SwapArgs
= false; SetCCOpc
= X86::SETGr
; break;
885 case CmpInst::ICMP_SGE
: SwapArgs
= false; SetCCOpc
= X86::SETGEr
; break;
886 case CmpInst::ICMP_SLT
: SwapArgs
= false; SetCCOpc
= X86::SETLr
; break;
887 case CmpInst::ICMP_SLE
: SwapArgs
= false; SetCCOpc
= X86::SETLEr
; break;
892 const Value
*Op0
= CI
->getOperand(0), *Op1
= CI
->getOperand(1);
896 // Emit a compare of Op0/Op1.
897 if (!X86FastEmitCompare(Op0
, Op1
, VT
))
900 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(SetCCOpc
), ResultReg
);
901 UpdateValueMap(I
, ResultReg
);
905 bool X86FastISel::X86SelectZExt(const Instruction
*I
) {
906 // Handle zero-extension from i1 to i8, which is common.
907 if (I
->getType()->isIntegerTy(8) &&
908 I
->getOperand(0)->getType()->isIntegerTy(1)) {
909 unsigned ResultReg
= getRegForValue(I
->getOperand(0));
910 if (ResultReg
== 0) return false;
911 // Set the high bits to zero.
912 ResultReg
= FastEmitZExtFromI1(MVT::i8
, ResultReg
, /*TODO: Kill=*/false);
913 if (ResultReg
== 0) return false;
914 UpdateValueMap(I
, ResultReg
);
922 bool X86FastISel::X86SelectBranch(const Instruction
*I
) {
923 // Unconditional branches are selected by tablegen-generated code.
924 // Handle a conditional branch.
925 const BranchInst
*BI
= cast
<BranchInst
>(I
);
926 MachineBasicBlock
*TrueMBB
= FuncInfo
.MBBMap
[BI
->getSuccessor(0)];
927 MachineBasicBlock
*FalseMBB
= FuncInfo
.MBBMap
[BI
->getSuccessor(1)];
929 // Fold the common case of a conditional branch with a comparison
930 // in the same block (values defined on other blocks may not have
931 // initialized registers).
932 if (const CmpInst
*CI
= dyn_cast
<CmpInst
>(BI
->getCondition())) {
933 if (CI
->hasOneUse() && CI
->getParent() == I
->getParent()) {
934 EVT VT
= TLI
.getValueType(CI
->getOperand(0)->getType());
936 // Try to take advantage of fallthrough opportunities.
937 CmpInst::Predicate Predicate
= CI
->getPredicate();
938 if (FuncInfo
.MBB
->isLayoutSuccessor(TrueMBB
)) {
939 std::swap(TrueMBB
, FalseMBB
);
940 Predicate
= CmpInst::getInversePredicate(Predicate
);
943 bool SwapArgs
; // false -> compare Op0, Op1. true -> compare Op1, Op0.
944 unsigned BranchOpc
; // Opcode to jump on, e.g. "X86::JA"
947 case CmpInst::FCMP_OEQ
:
948 std::swap(TrueMBB
, FalseMBB
);
949 Predicate
= CmpInst::FCMP_UNE
;
951 case CmpInst::FCMP_UNE
: SwapArgs
= false; BranchOpc
= X86::JNE_4
; break;
952 case CmpInst::FCMP_OGT
: SwapArgs
= false; BranchOpc
= X86::JA_4
; break;
953 case CmpInst::FCMP_OGE
: SwapArgs
= false; BranchOpc
= X86::JAE_4
; break;
954 case CmpInst::FCMP_OLT
: SwapArgs
= true; BranchOpc
= X86::JA_4
; break;
955 case CmpInst::FCMP_OLE
: SwapArgs
= true; BranchOpc
= X86::JAE_4
; break;
956 case CmpInst::FCMP_ONE
: SwapArgs
= false; BranchOpc
= X86::JNE_4
; break;
957 case CmpInst::FCMP_ORD
: SwapArgs
= false; BranchOpc
= X86::JNP_4
; break;
958 case CmpInst::FCMP_UNO
: SwapArgs
= false; BranchOpc
= X86::JP_4
; break;
959 case CmpInst::FCMP_UEQ
: SwapArgs
= false; BranchOpc
= X86::JE_4
; break;
960 case CmpInst::FCMP_UGT
: SwapArgs
= true; BranchOpc
= X86::JB_4
; break;
961 case CmpInst::FCMP_UGE
: SwapArgs
= true; BranchOpc
= X86::JBE_4
; break;
962 case CmpInst::FCMP_ULT
: SwapArgs
= false; BranchOpc
= X86::JB_4
; break;
963 case CmpInst::FCMP_ULE
: SwapArgs
= false; BranchOpc
= X86::JBE_4
; break;
965 case CmpInst::ICMP_EQ
: SwapArgs
= false; BranchOpc
= X86::JE_4
; break;
966 case CmpInst::ICMP_NE
: SwapArgs
= false; BranchOpc
= X86::JNE_4
; break;
967 case CmpInst::ICMP_UGT
: SwapArgs
= false; BranchOpc
= X86::JA_4
; break;
968 case CmpInst::ICMP_UGE
: SwapArgs
= false; BranchOpc
= X86::JAE_4
; break;
969 case CmpInst::ICMP_ULT
: SwapArgs
= false; BranchOpc
= X86::JB_4
; break;
970 case CmpInst::ICMP_ULE
: SwapArgs
= false; BranchOpc
= X86::JBE_4
; break;
971 case CmpInst::ICMP_SGT
: SwapArgs
= false; BranchOpc
= X86::JG_4
; break;
972 case CmpInst::ICMP_SGE
: SwapArgs
= false; BranchOpc
= X86::JGE_4
; break;
973 case CmpInst::ICMP_SLT
: SwapArgs
= false; BranchOpc
= X86::JL_4
; break;
974 case CmpInst::ICMP_SLE
: SwapArgs
= false; BranchOpc
= X86::JLE_4
; break;
979 const Value
*Op0
= CI
->getOperand(0), *Op1
= CI
->getOperand(1);
983 // Emit a compare of the LHS and RHS, setting the flags.
984 if (!X86FastEmitCompare(Op0
, Op1
, VT
))
987 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(BranchOpc
))
990 if (Predicate
== CmpInst::FCMP_UNE
) {
991 // X86 requires a second branch to handle UNE (and OEQ,
992 // which is mapped to UNE above).
993 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(X86::JP_4
))
997 FastEmitBranch(FalseMBB
, DL
);
998 FuncInfo
.MBB
->addSuccessor(TrueMBB
);
1001 } else if (ExtractValueInst
*EI
=
1002 dyn_cast
<ExtractValueInst
>(BI
->getCondition())) {
1003 // Check to see if the branch instruction is from an "arithmetic with
1004 // overflow" intrinsic. The main way these intrinsics are used is:
1006 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
1007 // %sum = extractvalue { i32, i1 } %t, 0
1008 // %obit = extractvalue { i32, i1 } %t, 1
1009 // br i1 %obit, label %overflow, label %normal
1011 // The %sum and %obit are converted in an ADD and a SETO/SETB before
1012 // reaching the branch. Therefore, we search backwards through the MBB
1013 // looking for the SETO/SETB instruction. If an instruction modifies the
1014 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
1015 // convert the branch into a JO/JB instruction.
1016 if (const IntrinsicInst
*CI
=
1017 dyn_cast
<IntrinsicInst
>(EI
->getAggregateOperand())){
1018 if (CI
->getIntrinsicID() == Intrinsic::sadd_with_overflow
||
1019 CI
->getIntrinsicID() == Intrinsic::uadd_with_overflow
) {
1020 const MachineInstr
*SetMI
= 0;
1021 unsigned Reg
= getRegForValue(EI
);
1023 for (MachineBasicBlock::const_reverse_iterator
1024 RI
= FuncInfo
.MBB
->rbegin(), RE
= FuncInfo
.MBB
->rend();
1026 const MachineInstr
&MI
= *RI
;
1028 if (MI
.definesRegister(Reg
)) {
1030 Reg
= MI
.getOperand(1).getReg();
1038 const TargetInstrDesc
&TID
= MI
.getDesc();
1039 if (TID
.hasUnmodeledSideEffects() ||
1040 TID
.hasImplicitDefOfPhysReg(X86::EFLAGS
))
1045 unsigned OpCode
= SetMI
->getOpcode();
1047 if (OpCode
== X86::SETOr
|| OpCode
== X86::SETBr
) {
1048 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
,
1049 TII
.get(OpCode
== X86::SETOr
? X86::JO_4
: X86::JB_4
))
1051 FastEmitBranch(FalseMBB
, DL
);
1052 FuncInfo
.MBB
->addSuccessor(TrueMBB
);
1060 // Otherwise do a clumsy setcc and re-test it.
1061 unsigned OpReg
= getRegForValue(BI
->getCondition());
1062 if (OpReg
== 0) return false;
1064 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(X86::TEST8rr
))
1065 .addReg(OpReg
).addReg(OpReg
);
1066 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(X86::JNE_4
))
1068 FastEmitBranch(FalseMBB
, DL
);
1069 FuncInfo
.MBB
->addSuccessor(TrueMBB
);
1073 bool X86FastISel::X86SelectShift(const Instruction
*I
) {
1074 unsigned CReg
= 0, OpReg
= 0, OpImm
= 0;
1075 const TargetRegisterClass
*RC
= NULL
;
1076 if (I
->getType()->isIntegerTy(8)) {
1078 RC
= &X86::GR8RegClass
;
1079 switch (I
->getOpcode()) {
1080 case Instruction::LShr
: OpReg
= X86::SHR8rCL
; OpImm
= X86::SHR8ri
; break;
1081 case Instruction::AShr
: OpReg
= X86::SAR8rCL
; OpImm
= X86::SAR8ri
; break;
1082 case Instruction::Shl
: OpReg
= X86::SHL8rCL
; OpImm
= X86::SHL8ri
; break;
1083 default: return false;
1085 } else if (I
->getType()->isIntegerTy(16)) {
1087 RC
= &X86::GR16RegClass
;
1088 switch (I
->getOpcode()) {
1089 case Instruction::LShr
: OpReg
= X86::SHR16rCL
; OpImm
= X86::SHR16ri
; break;
1090 case Instruction::AShr
: OpReg
= X86::SAR16rCL
; OpImm
= X86::SAR16ri
; break;
1091 case Instruction::Shl
: OpReg
= X86::SHL16rCL
; OpImm
= X86::SHL16ri
; break;
1092 default: return false;
1094 } else if (I
->getType()->isIntegerTy(32)) {
1096 RC
= &X86::GR32RegClass
;
1097 switch (I
->getOpcode()) {
1098 case Instruction::LShr
: OpReg
= X86::SHR32rCL
; OpImm
= X86::SHR32ri
; break;
1099 case Instruction::AShr
: OpReg
= X86::SAR32rCL
; OpImm
= X86::SAR32ri
; break;
1100 case Instruction::Shl
: OpReg
= X86::SHL32rCL
; OpImm
= X86::SHL32ri
; break;
1101 default: return false;
1103 } else if (I
->getType()->isIntegerTy(64)) {
1105 RC
= &X86::GR64RegClass
;
1106 switch (I
->getOpcode()) {
1107 case Instruction::LShr
: OpReg
= X86::SHR64rCL
; OpImm
= X86::SHR64ri
; break;
1108 case Instruction::AShr
: OpReg
= X86::SAR64rCL
; OpImm
= X86::SAR64ri
; break;
1109 case Instruction::Shl
: OpReg
= X86::SHL64rCL
; OpImm
= X86::SHL64ri
; break;
1110 default: return false;
1117 if (!isTypeLegal(I
->getType(), VT
))
1120 unsigned Op0Reg
= getRegForValue(I
->getOperand(0));
1121 if (Op0Reg
== 0) return false;
1123 // Fold immediate in shl(x,3).
1124 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(I
->getOperand(1))) {
1125 unsigned ResultReg
= createResultReg(RC
);
1126 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(OpImm
),
1127 ResultReg
).addReg(Op0Reg
).addImm(CI
->getZExtValue() & 0xff);
1128 UpdateValueMap(I
, ResultReg
);
1132 unsigned Op1Reg
= getRegForValue(I
->getOperand(1));
1133 if (Op1Reg
== 0) return false;
1134 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(TargetOpcode::COPY
),
1135 CReg
).addReg(Op1Reg
);
1137 // The shift instruction uses X86::CL. If we defined a super-register
1138 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
1139 if (CReg
!= X86::CL
)
1140 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
,
1141 TII
.get(TargetOpcode::KILL
), X86::CL
)
1142 .addReg(CReg
, RegState::Kill
);
1144 unsigned ResultReg
= createResultReg(RC
);
1145 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(OpReg
), ResultReg
)
1147 UpdateValueMap(I
, ResultReg
);
1151 bool X86FastISel::X86SelectSelect(const Instruction
*I
) {
1153 if (!isTypeLegal(I
->getType(), VT
))
1156 // We only use cmov here, if we don't have a cmov instruction bail.
1157 if (!Subtarget
->hasCMov()) return false;
1160 const TargetRegisterClass
*RC
= NULL
;
1161 if (VT
== MVT::i16
) {
1162 Opc
= X86::CMOVE16rr
;
1163 RC
= &X86::GR16RegClass
;
1164 } else if (VT
== MVT::i32
) {
1165 Opc
= X86::CMOVE32rr
;
1166 RC
= &X86::GR32RegClass
;
1167 } else if (VT
== MVT::i64
) {
1168 Opc
= X86::CMOVE64rr
;
1169 RC
= &X86::GR64RegClass
;
1174 unsigned Op0Reg
= getRegForValue(I
->getOperand(0));
1175 if (Op0Reg
== 0) return false;
1176 unsigned Op1Reg
= getRegForValue(I
->getOperand(1));
1177 if (Op1Reg
== 0) return false;
1178 unsigned Op2Reg
= getRegForValue(I
->getOperand(2));
1179 if (Op2Reg
== 0) return false;
1181 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(X86::TEST8rr
))
1182 .addReg(Op0Reg
).addReg(Op0Reg
);
1183 unsigned ResultReg
= createResultReg(RC
);
1184 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(Opc
), ResultReg
)
1185 .addReg(Op1Reg
).addReg(Op2Reg
);
1186 UpdateValueMap(I
, ResultReg
);
1190 bool X86FastISel::X86SelectFPExt(const Instruction
*I
) {
1191 // fpext from float to double.
1192 if (Subtarget
->hasSSE2() &&
1193 I
->getType()->isDoubleTy()) {
1194 const Value
*V
= I
->getOperand(0);
1195 if (V
->getType()->isFloatTy()) {
1196 unsigned OpReg
= getRegForValue(V
);
1197 if (OpReg
== 0) return false;
1198 unsigned ResultReg
= createResultReg(X86::FR64RegisterClass
);
1199 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
,
1200 TII
.get(X86::CVTSS2SDrr
), ResultReg
)
1202 UpdateValueMap(I
, ResultReg
);
1210 bool X86FastISel::X86SelectFPTrunc(const Instruction
*I
) {
1211 if (Subtarget
->hasSSE2()) {
1212 if (I
->getType()->isFloatTy()) {
1213 const Value
*V
= I
->getOperand(0);
1214 if (V
->getType()->isDoubleTy()) {
1215 unsigned OpReg
= getRegForValue(V
);
1216 if (OpReg
== 0) return false;
1217 unsigned ResultReg
= createResultReg(X86::FR32RegisterClass
);
1218 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
,
1219 TII
.get(X86::CVTSD2SSrr
), ResultReg
)
1221 UpdateValueMap(I
, ResultReg
);
1230 bool X86FastISel::X86SelectTrunc(const Instruction
*I
) {
1231 if (Subtarget
->is64Bit())
1232 // All other cases should be handled by the tblgen generated code.
1234 EVT SrcVT
= TLI
.getValueType(I
->getOperand(0)->getType());
1235 EVT DstVT
= TLI
.getValueType(I
->getType());
1237 // This code only handles truncation to byte right now.
1238 if (DstVT
!= MVT::i8
&& DstVT
!= MVT::i1
)
1239 // All other cases should be handled by the tblgen generated code.
1241 if (SrcVT
!= MVT::i16
&& SrcVT
!= MVT::i32
)
1242 // All other cases should be handled by the tblgen generated code.
1245 unsigned InputReg
= getRegForValue(I
->getOperand(0));
1247 // Unhandled operand. Halt "fast" selection and bail.
1250 // First issue a copy to GR16_ABCD or GR32_ABCD.
1251 const TargetRegisterClass
*CopyRC
= (SrcVT
== MVT::i16
)
1252 ? X86::GR16_ABCDRegisterClass
: X86::GR32_ABCDRegisterClass
;
1253 unsigned CopyReg
= createResultReg(CopyRC
);
1254 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(TargetOpcode::COPY
),
1255 CopyReg
).addReg(InputReg
);
1257 // Then issue an extract_subreg.
1258 unsigned ResultReg
= FastEmitInst_extractsubreg(MVT::i8
,
1259 CopyReg
, /*Kill=*/true,
1264 UpdateValueMap(I
, ResultReg
);
1268 bool X86FastISel::X86SelectExtractValue(const Instruction
*I
) {
1269 const ExtractValueInst
*EI
= cast
<ExtractValueInst
>(I
);
1270 const Value
*Agg
= EI
->getAggregateOperand();
1272 if (const IntrinsicInst
*CI
= dyn_cast
<IntrinsicInst
>(Agg
)) {
1273 switch (CI
->getIntrinsicID()) {
1275 case Intrinsic::sadd_with_overflow
:
1276 case Intrinsic::uadd_with_overflow
: {
1277 // Cheat a little. We know that the registers for "add" and "seto" are
1278 // allocated sequentially. However, we only keep track of the register
1279 // for "add" in the value map. Use extractvalue's index to get the
1280 // correct register for "seto".
1281 unsigned OpReg
= getRegForValue(Agg
);
1284 UpdateValueMap(I
, OpReg
+ *EI
->idx_begin());
1293 bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst
&I
) {
1294 // FIXME: Handle more intrinsics.
1295 switch (I
.getIntrinsicID()) {
1296 default: return false;
1297 case Intrinsic::stackprotector
: {
1298 // Emit code inline code to store the stack guard onto the stack.
1299 EVT PtrTy
= TLI
.getPointerTy();
1301 const Value
*Op1
= I
.getArgOperand(0); // The guard's value.
1302 const AllocaInst
*Slot
= cast
<AllocaInst
>(I
.getArgOperand(1));
1304 // Grab the frame index.
1306 if (!X86SelectAddress(Slot
, AM
)) return false;
1308 if (!X86FastEmitStore(PtrTy
, Op1
, AM
)) return false;
1312 case Intrinsic::objectsize
: {
1313 ConstantInt
*CI
= dyn_cast
<ConstantInt
>(I
.getArgOperand(1));
1314 const Type
*Ty
= I
.getCalledFunction()->getReturnType();
1316 assert(CI
&& "Non-constant type in Intrinsic::objectsize?");
1319 if (!isTypeLegal(Ty
, VT
))
1325 else if (VT
== MVT::i64
)
1330 unsigned ResultReg
= createResultReg(TLI
.getRegClassFor(VT
));
1331 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(OpC
), ResultReg
).
1332 addImm(CI
->isZero() ? -1ULL : 0);
1333 UpdateValueMap(&I
, ResultReg
);
1336 case Intrinsic::dbg_declare
: {
1337 const DbgDeclareInst
*DI
= cast
<DbgDeclareInst
>(&I
);
1339 assert(DI
->getAddress() && "Null address should be checked earlier!");
1340 if (!X86SelectAddress(DI
->getAddress(), AM
))
1342 const TargetInstrDesc
&II
= TII
.get(TargetOpcode::DBG_VALUE
);
1343 // FIXME may need to add RegState::Debug to any registers produced,
1344 // although ESP/EBP should be the only ones at the moment.
1345 addFullAddress(BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, II
), AM
).
1346 addImm(0).addMetadata(DI
->getVariable());
1349 case Intrinsic::trap
: {
1350 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(X86::TRAP
));
1353 case Intrinsic::sadd_with_overflow
:
1354 case Intrinsic::uadd_with_overflow
: {
1355 // Replace "add with overflow" intrinsics with an "add" instruction followed
1356 // by a seto/setc instruction. Later on, when the "extractvalue"
1357 // instructions are encountered, we use the fact that two registers were
1358 // created sequentially to get the correct registers for the "sum" and the
1360 const Function
*Callee
= I
.getCalledFunction();
1362 cast
<StructType
>(Callee
->getReturnType())->getTypeAtIndex(unsigned(0));
1365 if (!isTypeLegal(RetTy
, VT
))
1368 const Value
*Op1
= I
.getArgOperand(0);
1369 const Value
*Op2
= I
.getArgOperand(1);
1370 unsigned Reg1
= getRegForValue(Op1
);
1371 unsigned Reg2
= getRegForValue(Op2
);
1373 if (Reg1
== 0 || Reg2
== 0)
1374 // FIXME: Handle values *not* in registers.
1380 else if (VT
== MVT::i64
)
1385 unsigned ResultReg
= createResultReg(TLI
.getRegClassFor(VT
));
1386 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(OpC
), ResultReg
)
1387 .addReg(Reg1
).addReg(Reg2
);
1388 unsigned DestReg1
= UpdateValueMap(&I
, ResultReg
);
1390 // If the add with overflow is an intra-block value then we just want to
1391 // create temporaries for it like normal. If it is a cross-block value then
1392 // UpdateValueMap will return the cross-block register used. Since we
1393 // *really* want the value to be live in the register pair known by
1394 // UpdateValueMap, we have to use DestReg1+1 as the destination register in
1395 // the cross block case. In the non-cross-block case, we should just make
1396 // another register for the value.
1397 if (DestReg1
!= ResultReg
)
1398 ResultReg
= DestReg1
+1;
1400 ResultReg
= createResultReg(TLI
.getRegClassFor(MVT::i8
));
1402 unsigned Opc
= X86::SETBr
;
1403 if (I
.getIntrinsicID() == Intrinsic::sadd_with_overflow
)
1405 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(Opc
), ResultReg
);
1411 bool X86FastISel::X86SelectCall(const Instruction
*I
) {
1412 const CallInst
*CI
= cast
<CallInst
>(I
);
1413 const Value
*Callee
= CI
->getCalledValue();
1415 // Can't handle inline asm yet.
1416 if (isa
<InlineAsm
>(Callee
))
1419 // Handle intrinsic calls.
1420 if (const IntrinsicInst
*II
= dyn_cast
<IntrinsicInst
>(CI
))
1421 return X86VisitIntrinsicCall(*II
);
1423 // Handle only C and fastcc calling conventions for now.
1424 ImmutableCallSite
CS(CI
);
1425 CallingConv::ID CC
= CS
.getCallingConv();
1426 if (CC
!= CallingConv::C
&&
1427 CC
!= CallingConv::Fast
&&
1428 CC
!= CallingConv::X86_FastCall
)
1431 // fastcc with -tailcallopt is intended to provide a guaranteed
1432 // tail call optimization. Fastisel doesn't know how to do that.
1433 if (CC
== CallingConv::Fast
&& GuaranteedTailCallOpt
)
1436 // Let SDISel handle vararg functions.
1437 const PointerType
*PT
= cast
<PointerType
>(CS
.getCalledValue()->getType());
1438 const FunctionType
*FTy
= cast
<FunctionType
>(PT
->getElementType());
1439 if (FTy
->isVarArg())
1442 // Fast-isel doesn't know about callee-pop yet.
1443 if (Subtarget
->IsCalleePop(FTy
->isVarArg(), CC
))
1446 // Handle *simple* calls for now.
1447 const Type
*RetTy
= CS
.getType();
1449 if (RetTy
->isVoidTy())
1450 RetVT
= MVT::isVoid
;
1451 else if (!isTypeLegal(RetTy
, RetVT
, true))
1454 // Materialize callee address in a register. FIXME: GV address can be
1455 // handled with a CALLpcrel32 instead.
1456 X86AddressMode CalleeAM
;
1457 if (!X86SelectCallAddress(Callee
, CalleeAM
))
1459 unsigned CalleeOp
= 0;
1460 const GlobalValue
*GV
= 0;
1461 if (CalleeAM
.GV
!= 0) {
1463 } else if (CalleeAM
.Base
.Reg
!= 0) {
1464 CalleeOp
= CalleeAM
.Base
.Reg
;
1468 // Allow calls which produce i1 results.
1469 bool AndToI1
= false;
1470 if (RetVT
== MVT::i1
) {
1475 // Deal with call operands first.
1476 SmallVector
<const Value
*, 8> ArgVals
;
1477 SmallVector
<unsigned, 8> Args
;
1478 SmallVector
<MVT
, 8> ArgVTs
;
1479 SmallVector
<ISD::ArgFlagsTy
, 8> ArgFlags
;
1480 Args
.reserve(CS
.arg_size());
1481 ArgVals
.reserve(CS
.arg_size());
1482 ArgVTs
.reserve(CS
.arg_size());
1483 ArgFlags
.reserve(CS
.arg_size());
1484 for (ImmutableCallSite::arg_iterator i
= CS
.arg_begin(), e
= CS
.arg_end();
1486 unsigned Arg
= getRegForValue(*i
);
1489 ISD::ArgFlagsTy Flags
;
1490 unsigned AttrInd
= i
- CS
.arg_begin() + 1;
1491 if (CS
.paramHasAttr(AttrInd
, Attribute::SExt
))
1493 if (CS
.paramHasAttr(AttrInd
, Attribute::ZExt
))
1496 // FIXME: Only handle *easy* calls for now.
1497 if (CS
.paramHasAttr(AttrInd
, Attribute::InReg
) ||
1498 CS
.paramHasAttr(AttrInd
, Attribute::StructRet
) ||
1499 CS
.paramHasAttr(AttrInd
, Attribute::Nest
) ||
1500 CS
.paramHasAttr(AttrInd
, Attribute::ByVal
))
1503 const Type
*ArgTy
= (*i
)->getType();
1505 if (!isTypeLegal(ArgTy
, ArgVT
))
1507 unsigned OriginalAlignment
= TD
.getABITypeAlignment(ArgTy
);
1508 Flags
.setOrigAlign(OriginalAlignment
);
1510 Args
.push_back(Arg
);
1511 ArgVals
.push_back(*i
);
1512 ArgVTs
.push_back(ArgVT
);
1513 ArgFlags
.push_back(Flags
);
1516 // Analyze operands of the call, assigning locations to each operand.
1517 SmallVector
<CCValAssign
, 16> ArgLocs
;
1518 CCState
CCInfo(CC
, false, TM
, ArgLocs
, I
->getParent()->getContext());
1520 // Allocate shadow area for Win64
1521 if (Subtarget
->isTargetWin64()) {
1522 CCInfo
.AllocateStack(32, 8);
1525 CCInfo
.AnalyzeCallOperands(ArgVTs
, ArgFlags
, CC_X86
);
1527 // Get a count of how many bytes are to be pushed on the stack.
1528 unsigned NumBytes
= CCInfo
.getNextStackOffset();
1530 // Issue CALLSEQ_START
1531 unsigned AdjStackDown
= TM
.getRegisterInfo()->getCallFrameSetupOpcode();
1532 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(AdjStackDown
))
1535 // Process argument: walk the register/memloc assignments, inserting
1537 SmallVector
<unsigned, 4> RegArgs
;
1538 for (unsigned i
= 0, e
= ArgLocs
.size(); i
!= e
; ++i
) {
1539 CCValAssign
&VA
= ArgLocs
[i
];
1540 unsigned Arg
= Args
[VA
.getValNo()];
1541 EVT ArgVT
= ArgVTs
[VA
.getValNo()];
1543 // Promote the value if needed.
1544 switch (VA
.getLocInfo()) {
1545 default: llvm_unreachable("Unknown loc info!");
1546 case CCValAssign::Full
: break;
1547 case CCValAssign::SExt
: {
1548 bool Emitted
= X86FastEmitExtend(ISD::SIGN_EXTEND
, VA
.getLocVT(),
1550 assert(Emitted
&& "Failed to emit a sext!"); Emitted
=Emitted
;
1552 ArgVT
= VA
.getLocVT();
1555 case CCValAssign::ZExt
: {
1556 bool Emitted
= X86FastEmitExtend(ISD::ZERO_EXTEND
, VA
.getLocVT(),
1558 assert(Emitted
&& "Failed to emit a zext!"); Emitted
=Emitted
;
1560 ArgVT
= VA
.getLocVT();
1563 case CCValAssign::AExt
: {
1564 // We don't handle MMX parameters yet.
1565 if (VA
.getLocVT().isVector() && VA
.getLocVT().getSizeInBits() == 128)
1567 bool Emitted
= X86FastEmitExtend(ISD::ANY_EXTEND
, VA
.getLocVT(),
1570 Emitted
= X86FastEmitExtend(ISD::ZERO_EXTEND
, VA
.getLocVT(),
1573 Emitted
= X86FastEmitExtend(ISD::SIGN_EXTEND
, VA
.getLocVT(),
1576 assert(Emitted
&& "Failed to emit a aext!"); Emitted
=Emitted
;
1577 ArgVT
= VA
.getLocVT();
1580 case CCValAssign::BCvt
: {
1581 unsigned BC
= FastEmit_r(ArgVT
.getSimpleVT(), VA
.getLocVT(),
1582 ISD::BIT_CONVERT
, Arg
, /*TODO: Kill=*/false);
1583 assert(BC
!= 0 && "Failed to emit a bitcast!");
1585 ArgVT
= VA
.getLocVT();
1590 if (VA
.isRegLoc()) {
1591 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(TargetOpcode::COPY
),
1592 VA
.getLocReg()).addReg(Arg
);
1593 RegArgs
.push_back(VA
.getLocReg());
1595 unsigned LocMemOffset
= VA
.getLocMemOffset();
1597 AM
.Base
.Reg
= StackPtr
;
1598 AM
.Disp
= LocMemOffset
;
1599 const Value
*ArgVal
= ArgVals
[VA
.getValNo()];
1601 // If this is a really simple value, emit this with the Value* version of
1602 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1603 // can cause us to reevaluate the argument.
1604 if (isa
<ConstantInt
>(ArgVal
) || isa
<ConstantPointerNull
>(ArgVal
))
1605 X86FastEmitStore(ArgVT
, ArgVal
, AM
);
1607 X86FastEmitStore(ArgVT
, Arg
, AM
);
1611 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1613 if (Subtarget
->isPICStyleGOT()) {
1614 unsigned Base
= getInstrInfo()->getGlobalBaseReg(FuncInfo
.MF
);
1615 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(TargetOpcode::COPY
),
1616 X86::EBX
).addReg(Base
);
1620 MachineInstrBuilder MIB
;
1622 // Register-indirect call.
1624 if (Subtarget
->isTargetWin64())
1625 CallOpc
= X86::WINCALL64r
;
1626 else if (Subtarget
->is64Bit())
1627 CallOpc
= X86::CALL64r
;
1629 CallOpc
= X86::CALL32r
;
1630 MIB
= BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(CallOpc
))
1635 assert(GV
&& "Not a direct call");
1637 if (Subtarget
->isTargetWin64())
1638 CallOpc
= X86::WINCALL64pcrel32
;
1639 else if (Subtarget
->is64Bit())
1640 CallOpc
= X86::CALL64pcrel32
;
1642 CallOpc
= X86::CALLpcrel32
;
1644 // See if we need any target-specific flags on the GV operand.
1645 unsigned char OpFlags
= 0;
1647 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1648 // external symbols most go through the PLT in PIC mode. If the symbol
1649 // has hidden or protected visibility, or if it is static or local, then
1650 // we don't need to use the PLT - we can directly call it.
1651 if (Subtarget
->isTargetELF() &&
1652 TM
.getRelocationModel() == Reloc::PIC_
&&
1653 GV
->hasDefaultVisibility() && !GV
->hasLocalLinkage()) {
1654 OpFlags
= X86II::MO_PLT
;
1655 } else if (Subtarget
->isPICStyleStubAny() &&
1656 (GV
->isDeclaration() || GV
->isWeakForLinker()) &&
1657 Subtarget
->getDarwinVers() < 9) {
1658 // PC-relative references to external symbols should go through $stub,
1659 // unless we're building with the leopard linker or later, which
1660 // automatically synthesizes these stubs.
1661 OpFlags
= X86II::MO_DARWIN_STUB
;
1665 MIB
= BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(CallOpc
))
1666 .addGlobalAddress(GV
, 0, OpFlags
);
1669 // Add an implicit use GOT pointer in EBX.
1670 if (Subtarget
->isPICStyleGOT())
1671 MIB
.addReg(X86::EBX
);
1673 // Add implicit physical register uses to the call.
1674 for (unsigned i
= 0, e
= RegArgs
.size(); i
!= e
; ++i
)
1675 MIB
.addReg(RegArgs
[i
]);
1677 // Issue CALLSEQ_END
1678 unsigned AdjStackUp
= TM
.getRegisterInfo()->getCallFrameDestroyOpcode();
1679 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(AdjStackUp
))
1680 .addImm(NumBytes
).addImm(0);
1682 // Now handle call return value (if any).
1683 SmallVector
<unsigned, 4> UsedRegs
;
1684 if (RetVT
!= MVT::isVoid
) {
1685 SmallVector
<CCValAssign
, 16> RVLocs
;
1686 CCState
CCInfo(CC
, false, TM
, RVLocs
, I
->getParent()->getContext());
1687 CCInfo
.AnalyzeCallResult(RetVT
, RetCC_X86
);
1689 // Copy all of the result registers out of their specified physreg.
1690 assert(RVLocs
.size() == 1 && "Can't handle multi-value calls!");
1691 EVT CopyVT
= RVLocs
[0].getValVT();
1692 TargetRegisterClass
* DstRC
= TLI
.getRegClassFor(CopyVT
);
1694 // If this is a call to a function that returns an fp value on the x87 fp
1695 // stack, but where we prefer to use the value in xmm registers, copy it
1696 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1697 if ((RVLocs
[0].getLocReg() == X86::ST0
||
1698 RVLocs
[0].getLocReg() == X86::ST1
) &&
1699 isScalarFPTypeInSSEReg(RVLocs
[0].getValVT())) {
1701 DstRC
= X86::RFP80RegisterClass
;
1704 unsigned ResultReg
= createResultReg(DstRC
);
1705 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
, TII
.get(TargetOpcode::COPY
),
1706 ResultReg
).addReg(RVLocs
[0].getLocReg());
1707 UsedRegs
.push_back(RVLocs
[0].getLocReg());
1709 if (CopyVT
!= RVLocs
[0].getValVT()) {
1710 // Round the F80 the right size, which also moves to the appropriate xmm
1711 // register. This is accomplished by storing the F80 value in memory and
1712 // then loading it back. Ewww...
1713 EVT ResVT
= RVLocs
[0].getValVT();
1714 unsigned Opc
= ResVT
== MVT::f32
? X86::ST_Fp80m32
: X86::ST_Fp80m64
;
1715 unsigned MemSize
= ResVT
.getSizeInBits()/8;
1716 int FI
= MFI
.CreateStackObject(MemSize
, MemSize
, false);
1717 addFrameReference(BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
,
1720 DstRC
= ResVT
== MVT::f32
1721 ? X86::FR32RegisterClass
: X86::FR64RegisterClass
;
1722 Opc
= ResVT
== MVT::f32
? X86::MOVSSrm
: X86::MOVSDrm
;
1723 ResultReg
= createResultReg(DstRC
);
1724 addFrameReference(BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
,
1725 TII
.get(Opc
), ResultReg
), FI
);
1729 // Mask out all but lowest bit for some call which produces an i1.
1730 unsigned AndResult
= createResultReg(X86::GR8RegisterClass
);
1731 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
,
1732 TII
.get(X86::AND8ri
), AndResult
).addReg(ResultReg
).addImm(1);
1733 ResultReg
= AndResult
;
1736 UpdateValueMap(I
, ResultReg
);
1739 // Set all unused physreg defs as dead.
1740 static_cast<MachineInstr
*>(MIB
)->setPhysRegsDeadExcept(UsedRegs
, TRI
);
1747 X86FastISel::TargetSelectInstruction(const Instruction
*I
) {
1748 switch (I
->getOpcode()) {
1750 case Instruction::Load
:
1751 return X86SelectLoad(I
);
1752 case Instruction::Store
:
1753 return X86SelectStore(I
);
1754 case Instruction::Ret
:
1755 return X86SelectRet(I
);
1756 case Instruction::ICmp
:
1757 case Instruction::FCmp
:
1758 return X86SelectCmp(I
);
1759 case Instruction::ZExt
:
1760 return X86SelectZExt(I
);
1761 case Instruction::Br
:
1762 return X86SelectBranch(I
);
1763 case Instruction::Call
:
1764 return X86SelectCall(I
);
1765 case Instruction::LShr
:
1766 case Instruction::AShr
:
1767 case Instruction::Shl
:
1768 return X86SelectShift(I
);
1769 case Instruction::Select
:
1770 return X86SelectSelect(I
);
1771 case Instruction::Trunc
:
1772 return X86SelectTrunc(I
);
1773 case Instruction::FPExt
:
1774 return X86SelectFPExt(I
);
1775 case Instruction::FPTrunc
:
1776 return X86SelectFPTrunc(I
);
1777 case Instruction::ExtractValue
:
1778 return X86SelectExtractValue(I
);
1779 case Instruction::IntToPtr
: // Deliberate fall-through.
1780 case Instruction::PtrToInt
: {
1781 EVT SrcVT
= TLI
.getValueType(I
->getOperand(0)->getType());
1782 EVT DstVT
= TLI
.getValueType(I
->getType());
1783 if (DstVT
.bitsGT(SrcVT
))
1784 return X86SelectZExt(I
);
1785 if (DstVT
.bitsLT(SrcVT
))
1786 return X86SelectTrunc(I
);
1787 unsigned Reg
= getRegForValue(I
->getOperand(0));
1788 if (Reg
== 0) return false;
1789 UpdateValueMap(I
, Reg
);
1797 unsigned X86FastISel::TargetMaterializeConstant(const Constant
*C
) {
1799 if (!isTypeLegal(C
->getType(), VT
))
1802 // Get opcode and regclass of the output for the given load instruction.
1804 const TargetRegisterClass
*RC
= NULL
;
1805 switch (VT
.SimpleTy
) {
1806 default: return false;
1809 RC
= X86::GR8RegisterClass
;
1813 RC
= X86::GR16RegisterClass
;
1817 RC
= X86::GR32RegisterClass
;
1820 // Must be in x86-64 mode.
1822 RC
= X86::GR64RegisterClass
;
1825 if (Subtarget
->hasSSE1()) {
1827 RC
= X86::FR32RegisterClass
;
1829 Opc
= X86::LD_Fp32m
;
1830 RC
= X86::RFP32RegisterClass
;
1834 if (Subtarget
->hasSSE2()) {
1836 RC
= X86::FR64RegisterClass
;
1838 Opc
= X86::LD_Fp64m
;
1839 RC
= X86::RFP64RegisterClass
;
1843 // No f80 support yet.
1847 // Materialize addresses with LEA instructions.
1848 if (isa
<GlobalValue
>(C
)) {
1850 if (X86SelectAddress(C
, AM
)) {
1851 if (TLI
.getPointerTy() == MVT::i32
)
1855 unsigned ResultReg
= createResultReg(RC
);
1856 addFullAddress(BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
,
1857 TII
.get(Opc
), ResultReg
), AM
);
1863 // MachineConstantPool wants an explicit alignment.
1864 unsigned Align
= TD
.getPrefTypeAlignment(C
->getType());
1866 // Alignment of vector types. FIXME!
1867 Align
= TD
.getTypeAllocSize(C
->getType());
1870 // x86-32 PIC requires a PIC base register for constant pools.
1871 unsigned PICBase
= 0;
1872 unsigned char OpFlag
= 0;
1873 if (Subtarget
->isPICStyleStubPIC()) { // Not dynamic-no-pic
1874 OpFlag
= X86II::MO_PIC_BASE_OFFSET
;
1875 PICBase
= getInstrInfo()->getGlobalBaseReg(FuncInfo
.MF
);
1876 } else if (Subtarget
->isPICStyleGOT()) {
1877 OpFlag
= X86II::MO_GOTOFF
;
1878 PICBase
= getInstrInfo()->getGlobalBaseReg(FuncInfo
.MF
);
1879 } else if (Subtarget
->isPICStyleRIPRel() &&
1880 TM
.getCodeModel() == CodeModel::Small
) {
1884 // Create the load from the constant pool.
1885 unsigned MCPOffset
= MCP
.getConstantPoolIndex(C
, Align
);
1886 unsigned ResultReg
= createResultReg(RC
);
1887 addConstantPoolReference(BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
,
1888 TII
.get(Opc
), ResultReg
),
1889 MCPOffset
, PICBase
, OpFlag
);
1894 unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst
*C
) {
1895 // Fail on dynamic allocas. At this point, getRegForValue has already
1896 // checked its CSE maps, so if we're here trying to handle a dynamic
1897 // alloca, we're not going to succeed. X86SelectAddress has a
1898 // check for dynamic allocas, because it's called directly from
1899 // various places, but TargetMaterializeAlloca also needs a check
1900 // in order to avoid recursion between getRegForValue,
1901 // X86SelectAddrss, and TargetMaterializeAlloca.
1902 if (!FuncInfo
.StaticAllocaMap
.count(C
))
1906 if (!X86SelectAddress(C
, AM
))
1908 unsigned Opc
= Subtarget
->is64Bit() ? X86::LEA64r
: X86::LEA32r
;
1909 TargetRegisterClass
* RC
= TLI
.getRegClassFor(TLI
.getPointerTy());
1910 unsigned ResultReg
= createResultReg(RC
);
1911 addFullAddress(BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DL
,
1912 TII
.get(Opc
), ResultReg
), AM
);
1916 /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
1917 /// vreg is being provided by the specified load instruction. If possible,
1918 /// try to fold the load as an operand to the instruction, returning true if
1920 bool X86FastISel::TryToFoldLoad(MachineInstr
*MI
, unsigned OpNo
,
1921 const LoadInst
*LI
) {
1923 if (!X86SelectAddress(LI
->getOperand(0), AM
))
1926 X86InstrInfo
&XII
= (X86InstrInfo
&)TII
;
1928 unsigned Size
= TD
.getTypeAllocSize(LI
->getType());
1929 unsigned Alignment
= LI
->getAlignment();
1931 SmallVector
<MachineOperand
, 8> AddrOps
;
1932 AM
.getFullAddress(AddrOps
);
1934 MachineInstr
*Result
=
1935 XII
.foldMemoryOperandImpl(*FuncInfo
.MF
, MI
, OpNo
, AddrOps
, Size
, Alignment
);
1936 if (Result
== 0) return false;
1938 MI
->getParent()->insert(MI
, Result
);
1939 MI
->eraseFromParent();
1945 llvm::FastISel
*X86::createFastISel(FunctionLoweringInfo
&funcInfo
) {
1946 return new X86FastISel(funcInfo
);