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 "X86ISelLowering.h"
19 #include "X86RegisterInfo.h"
20 #include "X86Subtarget.h"
21 #include "X86TargetMachine.h"
22 #include "llvm/CallingConv.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/GlobalVariable.h"
25 #include "llvm/Instructions.h"
26 #include "llvm/IntrinsicInst.h"
27 #include "llvm/CodeGen/FastISel.h"
28 #include "llvm/CodeGen/MachineConstantPool.h"
29 #include "llvm/CodeGen/MachineFrameInfo.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/Support/CallSite.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/GetElementPtrTypeIterator.h"
34 #include "llvm/Target/TargetOptions.h"
39 class X86FastISel
: public FastISel
{
40 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
41 /// make the right decision when generating code for different targets.
42 const X86Subtarget
*Subtarget
;
44 /// StackPtr - Register used as the stack pointer.
48 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
49 /// floating point ops.
50 /// When SSE is available, use it for f32 operations.
51 /// When SSE2 is available, use it for f64 operations.
56 explicit X86FastISel(MachineFunction
&mf
,
57 MachineModuleInfo
*mmi
,
59 DenseMap
<const Value
*, unsigned> &vm
,
60 DenseMap
<const BasicBlock
*, MachineBasicBlock
*> &bm
,
61 DenseMap
<const AllocaInst
*, int> &am
63 , SmallSet
<Instruction
*, 8> &cil
66 : FastISel(mf
, mmi
, dw
, vm
, bm
, am
71 Subtarget
= &TM
.getSubtarget
<X86Subtarget
>();
72 StackPtr
= Subtarget
->is64Bit() ? X86::RSP
: X86::ESP
;
73 X86ScalarSSEf64
= Subtarget
->hasSSE2();
74 X86ScalarSSEf32
= Subtarget
->hasSSE1();
77 virtual bool TargetSelectInstruction(Instruction
*I
);
79 #include "X86GenFastISel.inc"
82 bool X86FastEmitCompare(Value
*LHS
, Value
*RHS
, MVT VT
);
84 bool X86FastEmitLoad(MVT VT
, const X86AddressMode
&AM
, unsigned &RR
);
86 bool X86FastEmitStore(MVT VT
, Value
*Val
,
87 const X86AddressMode
&AM
);
88 bool X86FastEmitStore(MVT VT
, unsigned Val
,
89 const X86AddressMode
&AM
);
91 bool X86FastEmitExtend(ISD::NodeType Opc
, MVT DstVT
, unsigned Src
, MVT SrcVT
,
94 bool X86SelectAddress(Value
*V
, X86AddressMode
&AM
);
95 bool X86SelectCallAddress(Value
*V
, X86AddressMode
&AM
);
97 bool X86SelectLoad(Instruction
*I
);
99 bool X86SelectStore(Instruction
*I
);
101 bool X86SelectCmp(Instruction
*I
);
103 bool X86SelectZExt(Instruction
*I
);
105 bool X86SelectBranch(Instruction
*I
);
107 bool X86SelectShift(Instruction
*I
);
109 bool X86SelectSelect(Instruction
*I
);
111 bool X86SelectTrunc(Instruction
*I
);
113 bool X86SelectFPExt(Instruction
*I
);
114 bool X86SelectFPTrunc(Instruction
*I
);
116 bool X86SelectExtractValue(Instruction
*I
);
118 bool X86VisitIntrinsicCall(IntrinsicInst
&I
);
119 bool X86SelectCall(Instruction
*I
);
121 CCAssignFn
*CCAssignFnForCall(unsigned CC
, bool isTailCall
= false);
123 const X86InstrInfo
*getInstrInfo() const {
124 return getTargetMachine()->getInstrInfo();
126 const X86TargetMachine
*getTargetMachine() const {
127 return static_cast<const X86TargetMachine
*>(&TM
);
130 unsigned TargetMaterializeConstant(Constant
*C
);
132 unsigned TargetMaterializeAlloca(AllocaInst
*C
);
134 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
135 /// computed in an SSE register, not on the X87 floating point stack.
136 bool isScalarFPTypeInSSEReg(MVT VT
) const {
137 return (VT
== MVT::f64
&& X86ScalarSSEf64
) || // f64 is when SSE2
138 (VT
== MVT::f32
&& X86ScalarSSEf32
); // f32 is when SSE1
141 bool isTypeLegal(const Type
*Ty
, MVT
&VT
, bool AllowI1
= false);
144 } // end anonymous namespace.
146 bool X86FastISel::isTypeLegal(const Type
*Ty
, MVT
&VT
, bool AllowI1
) {
147 VT
= TLI
.getValueType(Ty
, /*HandleUnknown=*/true);
148 if (VT
== MVT::Other
|| !VT
.isSimple())
149 // Unhandled type. Halt "fast" selection and bail.
152 // For now, require SSE/SSE2 for performing floating-point operations,
153 // since x87 requires additional work.
154 if (VT
== MVT::f64
&& !X86ScalarSSEf64
)
156 if (VT
== MVT::f32
&& !X86ScalarSSEf32
)
158 // Similarly, no f80 support yet.
161 // We only handle legal types. For example, on x86-32 the instruction
162 // selector contains all of the 64-bit instructions from x86-64,
163 // under the assumption that i64 won't be used if the target doesn't
165 return (AllowI1
&& VT
== MVT::i1
) || TLI
.isTypeLegal(VT
);
168 #include "X86GenCallingConv.inc"
170 /// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
172 CCAssignFn
*X86FastISel::CCAssignFnForCall(unsigned CC
, bool isTaillCall
) {
173 if (Subtarget
->is64Bit()) {
174 if (Subtarget
->isTargetWin64())
175 return CC_X86_Win64_C
;
180 if (CC
== CallingConv::X86_FastCall
)
181 return CC_X86_32_FastCall
;
182 else if (CC
== CallingConv::Fast
)
183 return CC_X86_32_FastCC
;
188 /// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
189 /// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
190 /// Return true and the result register by reference if it is possible.
191 bool X86FastISel::X86FastEmitLoad(MVT VT
, const X86AddressMode
&AM
,
192 unsigned &ResultReg
) {
193 // Get opcode and regclass of the output for the given load instruction.
195 const TargetRegisterClass
*RC
= NULL
;
196 switch (VT
.getSimpleVT()) {
197 default: return false;
200 RC
= X86::GR8RegisterClass
;
204 RC
= X86::GR16RegisterClass
;
208 RC
= X86::GR32RegisterClass
;
211 // Must be in x86-64 mode.
213 RC
= X86::GR64RegisterClass
;
216 if (Subtarget
->hasSSE1()) {
218 RC
= X86::FR32RegisterClass
;
221 RC
= X86::RFP32RegisterClass
;
225 if (Subtarget
->hasSSE2()) {
227 RC
= X86::FR64RegisterClass
;
230 RC
= X86::RFP64RegisterClass
;
234 // No f80 support yet.
238 ResultReg
= createResultReg(RC
);
239 addFullAddress(BuildMI(MBB
, DL
, TII
.get(Opc
), ResultReg
), AM
);
243 /// X86FastEmitStore - Emit a machine instruction to store a value Val of
244 /// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
245 /// and a displacement offset, or a GlobalAddress,
246 /// i.e. V. Return true if it is possible.
248 X86FastISel::X86FastEmitStore(MVT VT
, unsigned Val
,
249 const X86AddressMode
&AM
) {
250 // Get opcode and regclass of the output for the given store instruction.
252 switch (VT
.getSimpleVT()) {
253 case MVT::f80
: // No f80 support yet.
254 default: return false;
255 case MVT::i8
: Opc
= X86::MOV8mr
; break;
256 case MVT::i16
: Opc
= X86::MOV16mr
; break;
257 case MVT::i32
: Opc
= X86::MOV32mr
; break;
258 case MVT::i64
: Opc
= X86::MOV64mr
; break; // Must be in x86-64 mode.
260 Opc
= Subtarget
->hasSSE1() ? X86::MOVSSmr
: X86::ST_Fp32m
;
263 Opc
= Subtarget
->hasSSE2() ? X86::MOVSDmr
: X86::ST_Fp64m
;
267 addFullAddress(BuildMI(MBB
, DL
, TII
.get(Opc
)), AM
).addReg(Val
);
271 bool X86FastISel::X86FastEmitStore(MVT VT
, Value
*Val
,
272 const X86AddressMode
&AM
) {
273 // Handle 'null' like i32/i64 0.
274 if (isa
<ConstantPointerNull
>(Val
))
275 Val
= Constant::getNullValue(TD
.getIntPtrType());
277 // If this is a store of a simple constant, fold the constant into the store.
278 if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(Val
)) {
280 switch (VT
.getSimpleVT()) {
282 case MVT::i8
: Opc
= X86::MOV8mi
; break;
283 case MVT::i16
: Opc
= X86::MOV16mi
; break;
284 case MVT::i32
: Opc
= X86::MOV32mi
; break;
286 // Must be a 32-bit sign extended value.
287 if ((int)CI
->getSExtValue() == CI
->getSExtValue())
288 Opc
= X86::MOV64mi32
;
293 addFullAddress(BuildMI(MBB
, DL
, TII
.get(Opc
)), AM
)
294 .addImm(CI
->getSExtValue());
299 unsigned ValReg
= getRegForValue(Val
);
303 return X86FastEmitStore(VT
, ValReg
, AM
);
306 /// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
307 /// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
308 /// ISD::SIGN_EXTEND).
309 bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc
, MVT DstVT
,
310 unsigned Src
, MVT SrcVT
,
311 unsigned &ResultReg
) {
312 unsigned RR
= FastEmit_r(SrcVT
.getSimpleVT(), DstVT
.getSimpleVT(), Opc
, Src
);
321 /// X86SelectAddress - Attempt to fill in an address from the given value.
323 bool X86FastISel::X86SelectAddress(Value
*V
, X86AddressMode
&AM
) {
325 unsigned Opcode
= Instruction::UserOp1
;
326 if (Instruction
*I
= dyn_cast
<Instruction
>(V
)) {
327 Opcode
= I
->getOpcode();
329 } else if (ConstantExpr
*C
= dyn_cast
<ConstantExpr
>(V
)) {
330 Opcode
= C
->getOpcode();
336 case Instruction::BitCast
:
337 // Look past bitcasts.
338 return X86SelectAddress(U
->getOperand(0), AM
);
340 case Instruction::IntToPtr
:
341 // Look past no-op inttoptrs.
342 if (TLI
.getValueType(U
->getOperand(0)->getType()) == TLI
.getPointerTy())
343 return X86SelectAddress(U
->getOperand(0), AM
);
346 case Instruction::PtrToInt
:
347 // Look past no-op ptrtoints.
348 if (TLI
.getValueType(U
->getType()) == TLI
.getPointerTy())
349 return X86SelectAddress(U
->getOperand(0), AM
);
352 case Instruction::Alloca
: {
353 // Do static allocas.
354 const AllocaInst
*A
= cast
<AllocaInst
>(V
);
355 DenseMap
<const AllocaInst
*, int>::iterator SI
= StaticAllocaMap
.find(A
);
356 if (SI
!= StaticAllocaMap
.end()) {
357 AM
.BaseType
= X86AddressMode::FrameIndexBase
;
358 AM
.Base
.FrameIndex
= SI
->second
;
364 case Instruction::Add
: {
365 // Adds of constants are common and easy enough.
366 if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(U
->getOperand(1))) {
367 uint64_t Disp
= (int32_t)AM
.Disp
+ (uint64_t)CI
->getSExtValue();
368 // They have to fit in the 32-bit signed displacement field though.
370 AM
.Disp
= (uint32_t)Disp
;
371 return X86SelectAddress(U
->getOperand(0), AM
);
377 case Instruction::GetElementPtr
: {
378 // Pattern-match simple GEPs.
379 uint64_t Disp
= (int32_t)AM
.Disp
;
380 unsigned IndexReg
= AM
.IndexReg
;
381 unsigned Scale
= AM
.Scale
;
382 gep_type_iterator GTI
= gep_type_begin(U
);
383 // Iterate through the indices, folding what we can. Constants can be
384 // folded, and one dynamic index can be handled, if the scale is supported.
385 for (User::op_iterator i
= U
->op_begin() + 1, e
= U
->op_end();
386 i
!= e
; ++i
, ++GTI
) {
388 if (const StructType
*STy
= dyn_cast
<StructType
>(*GTI
)) {
389 const StructLayout
*SL
= TD
.getStructLayout(STy
);
390 unsigned Idx
= cast
<ConstantInt
>(Op
)->getZExtValue();
391 Disp
+= SL
->getElementOffset(Idx
);
393 uint64_t S
= TD
.getTypeAllocSize(GTI
.getIndexedType());
394 if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(Op
)) {
395 // Constant-offset addressing.
396 Disp
+= CI
->getSExtValue() * S
;
397 } else if (IndexReg
== 0 &&
398 (!AM
.GV
|| !Subtarget
->isPICStyleRIPRel()) &&
399 (S
== 1 || S
== 2 || S
== 4 || S
== 8)) {
400 // Scaled-index addressing.
402 IndexReg
= getRegForGEPIndex(Op
);
407 goto unsupported_gep
;
410 // Check for displacement overflow.
413 // Ok, the GEP indices were covered by constant-offset and scaled-index
414 // addressing. Update the address state and move on to examining the base.
415 AM
.IndexReg
= IndexReg
;
417 AM
.Disp
= (uint32_t)Disp
;
418 return X86SelectAddress(U
->getOperand(0), AM
);
420 // Ok, the GEP indices weren't all covered.
425 // Handle constant address.
426 if (GlobalValue
*GV
= dyn_cast
<GlobalValue
>(V
)) {
427 // Can't handle alternate code models yet.
428 if (TM
.getCodeModel() != CodeModel::Small
)
431 // RIP-relative addresses can't have additional register operands.
432 if (Subtarget
->isPICStyleRIPRel() &&
433 (AM
.Base
.Reg
!= 0 || AM
.IndexReg
!= 0))
436 // Can't handle TLS yet.
437 if (GlobalVariable
*GVar
= dyn_cast
<GlobalVariable
>(GV
))
438 if (GVar
->isThreadLocal())
441 // Okay, we've committed to selecting this global. Set up the basic address.
444 // Allow the subtarget to classify the global.
445 unsigned char GVFlags
= Subtarget
->ClassifyGlobalReference(GV
, TM
);
447 // If this reference is relative to the pic base, set it now.
448 if (isGlobalRelativeToPICBase(GVFlags
)) {
449 // FIXME: How do we know Base.Reg is free??
450 AM
.Base
.Reg
= getInstrInfo()->getGlobalBaseReg(&MF
);
453 // Unless the ABI requires an extra load, return a direct reference to
455 if (!isGlobalStubReference(GVFlags
)) {
456 if (Subtarget
->isPICStyleRIPRel()) {
457 // Use rip-relative addressing if we can. Above we verified that the
458 // base and index registers are unused.
459 assert(AM
.Base
.Reg
== 0 && AM
.IndexReg
== 0);
460 AM
.Base
.Reg
= X86::RIP
;
462 AM
.GVOpFlags
= GVFlags
;
466 // Ok, we need to do a load from a stub. If we've already loaded from this
467 // stub, reuse the loaded pointer, otherwise emit the load now.
468 DenseMap
<const Value
*, unsigned>::iterator I
= LocalValueMap
.find(V
);
470 if (I
!= LocalValueMap
.end() && I
->second
!= 0) {
473 // Issue load from stub.
475 const TargetRegisterClass
*RC
= NULL
;
476 X86AddressMode StubAM
;
477 StubAM
.Base
.Reg
= AM
.Base
.Reg
;
479 StubAM
.GVOpFlags
= GVFlags
;
481 if (TLI
.getPointerTy() == MVT::i64
) {
483 RC
= X86::GR64RegisterClass
;
485 if (Subtarget
->isPICStyleRIPRel())
486 StubAM
.Base
.Reg
= X86::RIP
;
489 RC
= X86::GR32RegisterClass
;
492 LoadReg
= createResultReg(RC
);
493 addFullAddress(BuildMI(MBB
, DL
, TII
.get(Opc
), LoadReg
), StubAM
);
495 // Prevent loading GV stub multiple times in same MBB.
496 LocalValueMap
[V
] = LoadReg
;
499 // Now construct the final address. Note that the Disp, Scale,
500 // and Index values may already be set here.
501 AM
.Base
.Reg
= LoadReg
;
506 // If all else fails, try to materialize the value in a register.
507 if (!AM
.GV
|| !Subtarget
->isPICStyleRIPRel()) {
508 if (AM
.Base
.Reg
== 0) {
509 AM
.Base
.Reg
= getRegForValue(V
);
510 return AM
.Base
.Reg
!= 0;
512 if (AM
.IndexReg
== 0) {
513 assert(AM
.Scale
== 1 && "Scale with no index!");
514 AM
.IndexReg
= getRegForValue(V
);
515 return AM
.IndexReg
!= 0;
522 /// X86SelectCallAddress - Attempt to fill in an address from the given value.
524 bool X86FastISel::X86SelectCallAddress(Value
*V
, X86AddressMode
&AM
) {
526 unsigned Opcode
= Instruction::UserOp1
;
527 if (Instruction
*I
= dyn_cast
<Instruction
>(V
)) {
528 Opcode
= I
->getOpcode();
530 } else if (ConstantExpr
*C
= dyn_cast
<ConstantExpr
>(V
)) {
531 Opcode
= C
->getOpcode();
537 case Instruction::BitCast
:
538 // Look past bitcasts.
539 return X86SelectCallAddress(U
->getOperand(0), AM
);
541 case Instruction::IntToPtr
:
542 // Look past no-op inttoptrs.
543 if (TLI
.getValueType(U
->getOperand(0)->getType()) == TLI
.getPointerTy())
544 return X86SelectCallAddress(U
->getOperand(0), AM
);
547 case Instruction::PtrToInt
:
548 // Look past no-op ptrtoints.
549 if (TLI
.getValueType(U
->getType()) == TLI
.getPointerTy())
550 return X86SelectCallAddress(U
->getOperand(0), AM
);
554 // Handle constant address.
555 if (GlobalValue
*GV
= dyn_cast
<GlobalValue
>(V
)) {
556 // Can't handle alternate code models yet.
557 if (TM
.getCodeModel() != CodeModel::Small
)
560 // RIP-relative addresses can't have additional register operands.
561 if (Subtarget
->isPICStyleRIPRel() &&
562 (AM
.Base
.Reg
!= 0 || AM
.IndexReg
!= 0))
565 // Can't handle TLS or DLLImport.
566 if (GlobalVariable
*GVar
= dyn_cast
<GlobalVariable
>(GV
))
567 if (GVar
->isThreadLocal() || GVar
->hasDLLImportLinkage())
570 // Okay, we've committed to selecting this global. Set up the basic address.
573 // No ABI requires an extra load for anything other than DLLImport, which
574 // we rejected above. Return a direct reference to the global.
575 if (Subtarget
->isPICStyleRIPRel()) {
576 // Use rip-relative addressing if we can. Above we verified that the
577 // base and index registers are unused.
578 assert(AM
.Base
.Reg
== 0 && AM
.IndexReg
== 0);
579 AM
.Base
.Reg
= X86::RIP
;
580 } else if (Subtarget
->isPICStyleStubPIC()) {
581 AM
.GVOpFlags
= X86II::MO_PIC_BASE_OFFSET
;
582 } else if (Subtarget
->isPICStyleGOT()) {
583 AM
.GVOpFlags
= X86II::MO_GOTOFF
;
589 // If all else fails, try to materialize the value in a register.
590 if (!AM
.GV
|| !Subtarget
->isPICStyleRIPRel()) {
591 if (AM
.Base
.Reg
== 0) {
592 AM
.Base
.Reg
= getRegForValue(V
);
593 return AM
.Base
.Reg
!= 0;
595 if (AM
.IndexReg
== 0) {
596 assert(AM
.Scale
== 1 && "Scale with no index!");
597 AM
.IndexReg
= getRegForValue(V
);
598 return AM
.IndexReg
!= 0;
606 /// X86SelectStore - Select and emit code to implement store instructions.
607 bool X86FastISel::X86SelectStore(Instruction
* I
) {
609 if (!isTypeLegal(I
->getOperand(0)->getType(), VT
))
613 if (!X86SelectAddress(I
->getOperand(1), AM
))
616 return X86FastEmitStore(VT
, I
->getOperand(0), AM
);
619 /// X86SelectLoad - Select and emit code to implement load instructions.
621 bool X86FastISel::X86SelectLoad(Instruction
*I
) {
623 if (!isTypeLegal(I
->getType(), VT
))
627 if (!X86SelectAddress(I
->getOperand(0), AM
))
630 unsigned ResultReg
= 0;
631 if (X86FastEmitLoad(VT
, AM
, ResultReg
)) {
632 UpdateValueMap(I
, ResultReg
);
638 static unsigned X86ChooseCmpOpcode(MVT VT
) {
639 switch (VT
.getSimpleVT()) {
641 case MVT::i8
: return X86::CMP8rr
;
642 case MVT::i16
: return X86::CMP16rr
;
643 case MVT::i32
: return X86::CMP32rr
;
644 case MVT::i64
: return X86::CMP64rr
;
645 case MVT::f32
: return X86::UCOMISSrr
;
646 case MVT::f64
: return X86::UCOMISDrr
;
650 /// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
651 /// of the comparison, return an opcode that works for the compare (e.g.
652 /// CMP32ri) otherwise return 0.
653 static unsigned X86ChooseCmpImmediateOpcode(MVT VT
, ConstantInt
*RHSC
) {
654 switch (VT
.getSimpleVT()) {
655 // Otherwise, we can't fold the immediate into this comparison.
657 case MVT::i8
: return X86::CMP8ri
;
658 case MVT::i16
: return X86::CMP16ri
;
659 case MVT::i32
: return X86::CMP32ri
;
661 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
663 if ((int)RHSC
->getSExtValue() == RHSC
->getSExtValue())
664 return X86::CMP64ri32
;
669 bool X86FastISel::X86FastEmitCompare(Value
*Op0
, Value
*Op1
, MVT VT
) {
670 unsigned Op0Reg
= getRegForValue(Op0
);
671 if (Op0Reg
== 0) return false;
673 // Handle 'null' like i32/i64 0.
674 if (isa
<ConstantPointerNull
>(Op1
))
675 Op1
= Constant::getNullValue(TD
.getIntPtrType());
677 // We have two options: compare with register or immediate. If the RHS of
678 // the compare is an immediate that we can fold into this compare, use
679 // CMPri, otherwise use CMPrr.
680 if (ConstantInt
*Op1C
= dyn_cast
<ConstantInt
>(Op1
)) {
681 if (unsigned CompareImmOpc
= X86ChooseCmpImmediateOpcode(VT
, Op1C
)) {
682 BuildMI(MBB
, DL
, TII
.get(CompareImmOpc
)).addReg(Op0Reg
)
683 .addImm(Op1C
->getSExtValue());
688 unsigned CompareOpc
= X86ChooseCmpOpcode(VT
);
689 if (CompareOpc
== 0) return false;
691 unsigned Op1Reg
= getRegForValue(Op1
);
692 if (Op1Reg
== 0) return false;
693 BuildMI(MBB
, DL
, TII
.get(CompareOpc
)).addReg(Op0Reg
).addReg(Op1Reg
);
698 bool X86FastISel::X86SelectCmp(Instruction
*I
) {
699 CmpInst
*CI
= cast
<CmpInst
>(I
);
702 if (!isTypeLegal(I
->getOperand(0)->getType(), VT
))
705 unsigned ResultReg
= createResultReg(&X86::GR8RegClass
);
707 bool SwapArgs
; // false -> compare Op0, Op1. true -> compare Op1, Op0.
708 switch (CI
->getPredicate()) {
709 case CmpInst::FCMP_OEQ
: {
710 if (!X86FastEmitCompare(CI
->getOperand(0), CI
->getOperand(1), VT
))
713 unsigned EReg
= createResultReg(&X86::GR8RegClass
);
714 unsigned NPReg
= createResultReg(&X86::GR8RegClass
);
715 BuildMI(MBB
, DL
, TII
.get(X86::SETEr
), EReg
);
716 BuildMI(MBB
, DL
, TII
.get(X86::SETNPr
), NPReg
);
718 TII
.get(X86::AND8rr
), ResultReg
).addReg(NPReg
).addReg(EReg
);
719 UpdateValueMap(I
, ResultReg
);
722 case CmpInst::FCMP_UNE
: {
723 if (!X86FastEmitCompare(CI
->getOperand(0), CI
->getOperand(1), VT
))
726 unsigned NEReg
= createResultReg(&X86::GR8RegClass
);
727 unsigned PReg
= createResultReg(&X86::GR8RegClass
);
728 BuildMI(MBB
, DL
, TII
.get(X86::SETNEr
), NEReg
);
729 BuildMI(MBB
, DL
, TII
.get(X86::SETPr
), PReg
);
730 BuildMI(MBB
, DL
, TII
.get(X86::OR8rr
), ResultReg
).addReg(PReg
).addReg(NEReg
);
731 UpdateValueMap(I
, ResultReg
);
734 case CmpInst::FCMP_OGT
: SwapArgs
= false; SetCCOpc
= X86::SETAr
; break;
735 case CmpInst::FCMP_OGE
: SwapArgs
= false; SetCCOpc
= X86::SETAEr
; break;
736 case CmpInst::FCMP_OLT
: SwapArgs
= true; SetCCOpc
= X86::SETAr
; break;
737 case CmpInst::FCMP_OLE
: SwapArgs
= true; SetCCOpc
= X86::SETAEr
; break;
738 case CmpInst::FCMP_ONE
: SwapArgs
= false; SetCCOpc
= X86::SETNEr
; break;
739 case CmpInst::FCMP_ORD
: SwapArgs
= false; SetCCOpc
= X86::SETNPr
; break;
740 case CmpInst::FCMP_UNO
: SwapArgs
= false; SetCCOpc
= X86::SETPr
; break;
741 case CmpInst::FCMP_UEQ
: SwapArgs
= false; SetCCOpc
= X86::SETEr
; break;
742 case CmpInst::FCMP_UGT
: SwapArgs
= true; SetCCOpc
= X86::SETBr
; break;
743 case CmpInst::FCMP_UGE
: SwapArgs
= true; SetCCOpc
= X86::SETBEr
; break;
744 case CmpInst::FCMP_ULT
: SwapArgs
= false; SetCCOpc
= X86::SETBr
; break;
745 case CmpInst::FCMP_ULE
: SwapArgs
= false; SetCCOpc
= X86::SETBEr
; break;
747 case CmpInst::ICMP_EQ
: SwapArgs
= false; SetCCOpc
= X86::SETEr
; break;
748 case CmpInst::ICMP_NE
: SwapArgs
= false; SetCCOpc
= X86::SETNEr
; break;
749 case CmpInst::ICMP_UGT
: SwapArgs
= false; SetCCOpc
= X86::SETAr
; break;
750 case CmpInst::ICMP_UGE
: SwapArgs
= false; SetCCOpc
= X86::SETAEr
; break;
751 case CmpInst::ICMP_ULT
: SwapArgs
= false; SetCCOpc
= X86::SETBr
; break;
752 case CmpInst::ICMP_ULE
: SwapArgs
= false; SetCCOpc
= X86::SETBEr
; break;
753 case CmpInst::ICMP_SGT
: SwapArgs
= false; SetCCOpc
= X86::SETGr
; break;
754 case CmpInst::ICMP_SGE
: SwapArgs
= false; SetCCOpc
= X86::SETGEr
; break;
755 case CmpInst::ICMP_SLT
: SwapArgs
= false; SetCCOpc
= X86::SETLr
; break;
756 case CmpInst::ICMP_SLE
: SwapArgs
= false; SetCCOpc
= X86::SETLEr
; break;
761 Value
*Op0
= CI
->getOperand(0), *Op1
= CI
->getOperand(1);
765 // Emit a compare of Op0/Op1.
766 if (!X86FastEmitCompare(Op0
, Op1
, VT
))
769 BuildMI(MBB
, DL
, TII
.get(SetCCOpc
), ResultReg
);
770 UpdateValueMap(I
, ResultReg
);
774 bool X86FastISel::X86SelectZExt(Instruction
*I
) {
775 // Handle zero-extension from i1 to i8, which is common.
776 if (I
->getType() == Type::Int8Ty
&&
777 I
->getOperand(0)->getType() == Type::Int1Ty
) {
778 unsigned ResultReg
= getRegForValue(I
->getOperand(0));
779 if (ResultReg
== 0) return false;
780 // Set the high bits to zero.
781 ResultReg
= FastEmitZExtFromI1(MVT::i8
, ResultReg
);
782 if (ResultReg
== 0) return false;
783 UpdateValueMap(I
, ResultReg
);
791 bool X86FastISel::X86SelectBranch(Instruction
*I
) {
792 // Unconditional branches are selected by tablegen-generated code.
793 // Handle a conditional branch.
794 BranchInst
*BI
= cast
<BranchInst
>(I
);
795 MachineBasicBlock
*TrueMBB
= MBBMap
[BI
->getSuccessor(0)];
796 MachineBasicBlock
*FalseMBB
= MBBMap
[BI
->getSuccessor(1)];
798 // Fold the common case of a conditional branch with a comparison.
799 if (CmpInst
*CI
= dyn_cast
<CmpInst
>(BI
->getCondition())) {
800 if (CI
->hasOneUse()) {
801 MVT VT
= TLI
.getValueType(CI
->getOperand(0)->getType());
803 // Try to take advantage of fallthrough opportunities.
804 CmpInst::Predicate Predicate
= CI
->getPredicate();
805 if (MBB
->isLayoutSuccessor(TrueMBB
)) {
806 std::swap(TrueMBB
, FalseMBB
);
807 Predicate
= CmpInst::getInversePredicate(Predicate
);
810 bool SwapArgs
; // false -> compare Op0, Op1. true -> compare Op1, Op0.
811 unsigned BranchOpc
; // Opcode to jump on, e.g. "X86::JA"
814 case CmpInst::FCMP_OEQ
:
815 std::swap(TrueMBB
, FalseMBB
);
816 Predicate
= CmpInst::FCMP_UNE
;
818 case CmpInst::FCMP_UNE
: SwapArgs
= false; BranchOpc
= X86::JNE
; break;
819 case CmpInst::FCMP_OGT
: SwapArgs
= false; BranchOpc
= X86::JA
; break;
820 case CmpInst::FCMP_OGE
: SwapArgs
= false; BranchOpc
= X86::JAE
; break;
821 case CmpInst::FCMP_OLT
: SwapArgs
= true; BranchOpc
= X86::JA
; break;
822 case CmpInst::FCMP_OLE
: SwapArgs
= true; BranchOpc
= X86::JAE
; break;
823 case CmpInst::FCMP_ONE
: SwapArgs
= false; BranchOpc
= X86::JNE
; break;
824 case CmpInst::FCMP_ORD
: SwapArgs
= false; BranchOpc
= X86::JNP
; break;
825 case CmpInst::FCMP_UNO
: SwapArgs
= false; BranchOpc
= X86::JP
; break;
826 case CmpInst::FCMP_UEQ
: SwapArgs
= false; BranchOpc
= X86::JE
; break;
827 case CmpInst::FCMP_UGT
: SwapArgs
= true; BranchOpc
= X86::JB
; break;
828 case CmpInst::FCMP_UGE
: SwapArgs
= true; BranchOpc
= X86::JBE
; break;
829 case CmpInst::FCMP_ULT
: SwapArgs
= false; BranchOpc
= X86::JB
; break;
830 case CmpInst::FCMP_ULE
: SwapArgs
= false; BranchOpc
= X86::JBE
; break;
832 case CmpInst::ICMP_EQ
: SwapArgs
= false; BranchOpc
= X86::JE
; break;
833 case CmpInst::ICMP_NE
: SwapArgs
= false; BranchOpc
= X86::JNE
; break;
834 case CmpInst::ICMP_UGT
: SwapArgs
= false; BranchOpc
= X86::JA
; break;
835 case CmpInst::ICMP_UGE
: SwapArgs
= false; BranchOpc
= X86::JAE
; break;
836 case CmpInst::ICMP_ULT
: SwapArgs
= false; BranchOpc
= X86::JB
; break;
837 case CmpInst::ICMP_ULE
: SwapArgs
= false; BranchOpc
= X86::JBE
; break;
838 case CmpInst::ICMP_SGT
: SwapArgs
= false; BranchOpc
= X86::JG
; break;
839 case CmpInst::ICMP_SGE
: SwapArgs
= false; BranchOpc
= X86::JGE
; break;
840 case CmpInst::ICMP_SLT
: SwapArgs
= false; BranchOpc
= X86::JL
; break;
841 case CmpInst::ICMP_SLE
: SwapArgs
= false; BranchOpc
= X86::JLE
; break;
846 Value
*Op0
= CI
->getOperand(0), *Op1
= CI
->getOperand(1);
850 // Emit a compare of the LHS and RHS, setting the flags.
851 if (!X86FastEmitCompare(Op0
, Op1
, VT
))
854 BuildMI(MBB
, DL
, TII
.get(BranchOpc
)).addMBB(TrueMBB
);
856 if (Predicate
== CmpInst::FCMP_UNE
) {
857 // X86 requires a second branch to handle UNE (and OEQ,
858 // which is mapped to UNE above).
859 BuildMI(MBB
, DL
, TII
.get(X86::JP
)).addMBB(TrueMBB
);
862 FastEmitBranch(FalseMBB
);
863 MBB
->addSuccessor(TrueMBB
);
866 } else if (ExtractValueInst
*EI
=
867 dyn_cast
<ExtractValueInst
>(BI
->getCondition())) {
868 // Check to see if the branch instruction is from an "arithmetic with
869 // overflow" intrinsic. The main way these intrinsics are used is:
871 // %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
872 // %sum = extractvalue { i32, i1 } %t, 0
873 // %obit = extractvalue { i32, i1 } %t, 1
874 // br i1 %obit, label %overflow, label %normal
876 // The %sum and %obit are converted in an ADD and a SETO/SETB before
877 // reaching the branch. Therefore, we search backwards through the MBB
878 // looking for the SETO/SETB instruction. If an instruction modifies the
879 // EFLAGS register before we reach the SETO/SETB instruction, then we can't
880 // convert the branch into a JO/JB instruction.
881 if (IntrinsicInst
*CI
= dyn_cast
<IntrinsicInst
>(EI
->getAggregateOperand())){
882 if (CI
->getIntrinsicID() == Intrinsic::sadd_with_overflow
||
883 CI
->getIntrinsicID() == Intrinsic::uadd_with_overflow
) {
884 const MachineInstr
*SetMI
= 0;
885 unsigned Reg
= lookUpRegForValue(EI
);
887 for (MachineBasicBlock::const_reverse_iterator
888 RI
= MBB
->rbegin(), RE
= MBB
->rend(); RI
!= RE
; ++RI
) {
889 const MachineInstr
&MI
= *RI
;
891 if (MI
.modifiesRegister(Reg
)) {
892 unsigned Src
, Dst
, SrcSR
, DstSR
;
894 if (getInstrInfo()->isMoveInstr(MI
, Src
, Dst
, SrcSR
, DstSR
)) {
903 const TargetInstrDesc
&TID
= MI
.getDesc();
904 if (TID
.hasUnmodeledSideEffects() ||
905 TID
.hasImplicitDefOfPhysReg(X86::EFLAGS
))
910 unsigned OpCode
= SetMI
->getOpcode();
912 if (OpCode
== X86::SETOr
|| OpCode
== X86::SETBr
) {
913 BuildMI(MBB
, DL
, TII
.get(OpCode
== X86::SETOr
? X86::JO
: X86::JB
))
915 FastEmitBranch(FalseMBB
);
916 MBB
->addSuccessor(TrueMBB
);
924 // Otherwise do a clumsy setcc and re-test it.
925 unsigned OpReg
= getRegForValue(BI
->getCondition());
926 if (OpReg
== 0) return false;
928 BuildMI(MBB
, DL
, TII
.get(X86::TEST8rr
)).addReg(OpReg
).addReg(OpReg
);
929 BuildMI(MBB
, DL
, TII
.get(X86::JNE
)).addMBB(TrueMBB
);
930 FastEmitBranch(FalseMBB
);
931 MBB
->addSuccessor(TrueMBB
);
935 bool X86FastISel::X86SelectShift(Instruction
*I
) {
936 unsigned CReg
= 0, OpReg
= 0, OpImm
= 0;
937 const TargetRegisterClass
*RC
= NULL
;
938 if (I
->getType() == Type::Int8Ty
) {
940 RC
= &X86::GR8RegClass
;
941 switch (I
->getOpcode()) {
942 case Instruction::LShr
: OpReg
= X86::SHR8rCL
; OpImm
= X86::SHR8ri
; break;
943 case Instruction::AShr
: OpReg
= X86::SAR8rCL
; OpImm
= X86::SAR8ri
; break;
944 case Instruction::Shl
: OpReg
= X86::SHL8rCL
; OpImm
= X86::SHL8ri
; break;
945 default: return false;
947 } else if (I
->getType() == Type::Int16Ty
) {
949 RC
= &X86::GR16RegClass
;
950 switch (I
->getOpcode()) {
951 case Instruction::LShr
: OpReg
= X86::SHR16rCL
; OpImm
= X86::SHR16ri
; break;
952 case Instruction::AShr
: OpReg
= X86::SAR16rCL
; OpImm
= X86::SAR16ri
; break;
953 case Instruction::Shl
: OpReg
= X86::SHL16rCL
; OpImm
= X86::SHL16ri
; break;
954 default: return false;
956 } else if (I
->getType() == Type::Int32Ty
) {
958 RC
= &X86::GR32RegClass
;
959 switch (I
->getOpcode()) {
960 case Instruction::LShr
: OpReg
= X86::SHR32rCL
; OpImm
= X86::SHR32ri
; break;
961 case Instruction::AShr
: OpReg
= X86::SAR32rCL
; OpImm
= X86::SAR32ri
; break;
962 case Instruction::Shl
: OpReg
= X86::SHL32rCL
; OpImm
= X86::SHL32ri
; break;
963 default: return false;
965 } else if (I
->getType() == Type::Int64Ty
) {
967 RC
= &X86::GR64RegClass
;
968 switch (I
->getOpcode()) {
969 case Instruction::LShr
: OpReg
= X86::SHR64rCL
; OpImm
= X86::SHR64ri
; break;
970 case Instruction::AShr
: OpReg
= X86::SAR64rCL
; OpImm
= X86::SAR64ri
; break;
971 case Instruction::Shl
: OpReg
= X86::SHL64rCL
; OpImm
= X86::SHL64ri
; break;
972 default: return false;
978 MVT VT
= TLI
.getValueType(I
->getType(), /*HandleUnknown=*/true);
979 if (VT
== MVT::Other
|| !isTypeLegal(I
->getType(), VT
))
982 unsigned Op0Reg
= getRegForValue(I
->getOperand(0));
983 if (Op0Reg
== 0) return false;
985 // Fold immediate in shl(x,3).
986 if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(I
->getOperand(1))) {
987 unsigned ResultReg
= createResultReg(RC
);
988 BuildMI(MBB
, DL
, TII
.get(OpImm
),
989 ResultReg
).addReg(Op0Reg
).addImm(CI
->getZExtValue() & 0xff);
990 UpdateValueMap(I
, ResultReg
);
994 unsigned Op1Reg
= getRegForValue(I
->getOperand(1));
995 if (Op1Reg
== 0) return false;
996 TII
.copyRegToReg(*MBB
, MBB
->end(), CReg
, Op1Reg
, RC
, RC
);
998 // The shift instruction uses X86::CL. If we defined a super-register
999 // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what
1000 // we're doing here.
1001 if (CReg
!= X86::CL
)
1002 BuildMI(MBB
, DL
, TII
.get(TargetInstrInfo::EXTRACT_SUBREG
), X86::CL
)
1003 .addReg(CReg
).addImm(X86::SUBREG_8BIT
);
1005 unsigned ResultReg
= createResultReg(RC
);
1006 BuildMI(MBB
, DL
, TII
.get(OpReg
), ResultReg
).addReg(Op0Reg
);
1007 UpdateValueMap(I
, ResultReg
);
1011 bool X86FastISel::X86SelectSelect(Instruction
*I
) {
1012 MVT VT
= TLI
.getValueType(I
->getType(), /*HandleUnknown=*/true);
1013 if (VT
== MVT::Other
|| !isTypeLegal(I
->getType(), VT
))
1017 const TargetRegisterClass
*RC
= NULL
;
1018 if (VT
.getSimpleVT() == MVT::i16
) {
1019 Opc
= X86::CMOVE16rr
;
1020 RC
= &X86::GR16RegClass
;
1021 } else if (VT
.getSimpleVT() == MVT::i32
) {
1022 Opc
= X86::CMOVE32rr
;
1023 RC
= &X86::GR32RegClass
;
1024 } else if (VT
.getSimpleVT() == MVT::i64
) {
1025 Opc
= X86::CMOVE64rr
;
1026 RC
= &X86::GR64RegClass
;
1031 unsigned Op0Reg
= getRegForValue(I
->getOperand(0));
1032 if (Op0Reg
== 0) return false;
1033 unsigned Op1Reg
= getRegForValue(I
->getOperand(1));
1034 if (Op1Reg
== 0) return false;
1035 unsigned Op2Reg
= getRegForValue(I
->getOperand(2));
1036 if (Op2Reg
== 0) return false;
1038 BuildMI(MBB
, DL
, TII
.get(X86::TEST8rr
)).addReg(Op0Reg
).addReg(Op0Reg
);
1039 unsigned ResultReg
= createResultReg(RC
);
1040 BuildMI(MBB
, DL
, TII
.get(Opc
), ResultReg
).addReg(Op1Reg
).addReg(Op2Reg
);
1041 UpdateValueMap(I
, ResultReg
);
1045 bool X86FastISel::X86SelectFPExt(Instruction
*I
) {
1046 // fpext from float to double.
1047 if (Subtarget
->hasSSE2() && I
->getType() == Type::DoubleTy
) {
1048 Value
*V
= I
->getOperand(0);
1049 if (V
->getType() == Type::FloatTy
) {
1050 unsigned OpReg
= getRegForValue(V
);
1051 if (OpReg
== 0) return false;
1052 unsigned ResultReg
= createResultReg(X86::FR64RegisterClass
);
1053 BuildMI(MBB
, DL
, TII
.get(X86::CVTSS2SDrr
), ResultReg
).addReg(OpReg
);
1054 UpdateValueMap(I
, ResultReg
);
1062 bool X86FastISel::X86SelectFPTrunc(Instruction
*I
) {
1063 if (Subtarget
->hasSSE2()) {
1064 if (I
->getType() == Type::FloatTy
) {
1065 Value
*V
= I
->getOperand(0);
1066 if (V
->getType() == Type::DoubleTy
) {
1067 unsigned OpReg
= getRegForValue(V
);
1068 if (OpReg
== 0) return false;
1069 unsigned ResultReg
= createResultReg(X86::FR32RegisterClass
);
1070 BuildMI(MBB
, DL
, TII
.get(X86::CVTSD2SSrr
), ResultReg
).addReg(OpReg
);
1071 UpdateValueMap(I
, ResultReg
);
1080 bool X86FastISel::X86SelectTrunc(Instruction
*I
) {
1081 if (Subtarget
->is64Bit())
1082 // All other cases should be handled by the tblgen generated code.
1084 MVT SrcVT
= TLI
.getValueType(I
->getOperand(0)->getType());
1085 MVT DstVT
= TLI
.getValueType(I
->getType());
1087 // This code only handles truncation to byte right now.
1088 if (DstVT
!= MVT::i8
&& DstVT
!= MVT::i1
)
1089 // All other cases should be handled by the tblgen generated code.
1091 if (SrcVT
!= MVT::i16
&& SrcVT
!= MVT::i32
)
1092 // All other cases should be handled by the tblgen generated code.
1095 unsigned InputReg
= getRegForValue(I
->getOperand(0));
1097 // Unhandled operand. Halt "fast" selection and bail.
1100 // First issue a copy to GR16_ABCD or GR32_ABCD.
1101 unsigned CopyOpc
= (SrcVT
== MVT::i16
) ? X86::MOV16rr
: X86::MOV32rr
;
1102 const TargetRegisterClass
*CopyRC
= (SrcVT
== MVT::i16
)
1103 ? X86::GR16_ABCDRegisterClass
: X86::GR32_ABCDRegisterClass
;
1104 unsigned CopyReg
= createResultReg(CopyRC
);
1105 BuildMI(MBB
, DL
, TII
.get(CopyOpc
), CopyReg
).addReg(InputReg
);
1107 // Then issue an extract_subreg.
1108 unsigned ResultReg
= FastEmitInst_extractsubreg(MVT::i8
,
1109 CopyReg
, X86::SUBREG_8BIT
);
1113 UpdateValueMap(I
, ResultReg
);
1117 bool X86FastISel::X86SelectExtractValue(Instruction
*I
) {
1118 ExtractValueInst
*EI
= cast
<ExtractValueInst
>(I
);
1119 Value
*Agg
= EI
->getAggregateOperand();
1121 if (IntrinsicInst
*CI
= dyn_cast
<IntrinsicInst
>(Agg
)) {
1122 switch (CI
->getIntrinsicID()) {
1124 case Intrinsic::sadd_with_overflow
:
1125 case Intrinsic::uadd_with_overflow
:
1126 // Cheat a little. We know that the registers for "add" and "seto" are
1127 // allocated sequentially. However, we only keep track of the register
1128 // for "add" in the value map. Use extractvalue's index to get the
1129 // correct register for "seto".
1130 UpdateValueMap(I
, lookUpRegForValue(Agg
) + *EI
->idx_begin());
1138 bool X86FastISel::X86VisitIntrinsicCall(IntrinsicInst
&I
) {
1139 // FIXME: Handle more intrinsics.
1140 switch (I
.getIntrinsicID()) {
1141 default: return false;
1142 case Intrinsic::sadd_with_overflow
:
1143 case Intrinsic::uadd_with_overflow
: {
1144 // Replace "add with overflow" intrinsics with an "add" instruction followed
1145 // by a seto/setc instruction. Later on, when the "extractvalue"
1146 // instructions are encountered, we use the fact that two registers were
1147 // created sequentially to get the correct registers for the "sum" and the
1149 const Function
*Callee
= I
.getCalledFunction();
1151 cast
<StructType
>(Callee
->getReturnType())->getTypeAtIndex(unsigned(0));
1154 if (!isTypeLegal(RetTy
, VT
))
1157 Value
*Op1
= I
.getOperand(1);
1158 Value
*Op2
= I
.getOperand(2);
1159 unsigned Reg1
= getRegForValue(Op1
);
1160 unsigned Reg2
= getRegForValue(Op2
);
1162 if (Reg1
== 0 || Reg2
== 0)
1163 // FIXME: Handle values *not* in registers.
1169 else if (VT
== MVT::i64
)
1174 unsigned ResultReg
= createResultReg(TLI
.getRegClassFor(VT
));
1175 BuildMI(MBB
, DL
, TII
.get(OpC
), ResultReg
).addReg(Reg1
).addReg(Reg2
);
1176 unsigned DestReg1
= UpdateValueMap(&I
, ResultReg
);
1178 // If the add with overflow is an intra-block value then we just want to
1179 // create temporaries for it like normal. If it is a cross-block value then
1180 // UpdateValueMap will return the cross-block register used. Since we
1181 // *really* want the value to be live in the register pair known by
1182 // UpdateValueMap, we have to use DestReg1+1 as the destination register in
1183 // the cross block case. In the non-cross-block case, we should just make
1184 // another register for the value.
1185 if (DestReg1
!= ResultReg
)
1186 ResultReg
= DestReg1
+1;
1188 ResultReg
= createResultReg(TLI
.getRegClassFor(MVT::i8
));
1190 unsigned Opc
= X86::SETBr
;
1191 if (I
.getIntrinsicID() == Intrinsic::sadd_with_overflow
)
1193 BuildMI(MBB
, DL
, TII
.get(Opc
), ResultReg
);
1199 bool X86FastISel::X86SelectCall(Instruction
*I
) {
1200 CallInst
*CI
= cast
<CallInst
>(I
);
1201 Value
*Callee
= I
->getOperand(0);
1203 // Can't handle inline asm yet.
1204 if (isa
<InlineAsm
>(Callee
))
1207 // Handle intrinsic calls.
1208 if (IntrinsicInst
*II
= dyn_cast
<IntrinsicInst
>(CI
))
1209 return X86VisitIntrinsicCall(*II
);
1211 // Handle only C and fastcc calling conventions for now.
1213 unsigned CC
= CS
.getCallingConv();
1214 if (CC
!= CallingConv::C
&&
1215 CC
!= CallingConv::Fast
&&
1216 CC
!= CallingConv::X86_FastCall
)
1219 // On X86, -tailcallopt changes the fastcc ABI. FastISel doesn't
1220 // handle this for now.
1221 if (CC
== CallingConv::Fast
&& PerformTailCallOpt
)
1224 // Let SDISel handle vararg functions.
1225 const PointerType
*PT
= cast
<PointerType
>(CS
.getCalledValue()->getType());
1226 const FunctionType
*FTy
= cast
<FunctionType
>(PT
->getElementType());
1227 if (FTy
->isVarArg())
1230 // Handle *simple* calls for now.
1231 const Type
*RetTy
= CS
.getType();
1233 if (RetTy
== Type::VoidTy
)
1234 RetVT
= MVT::isVoid
;
1235 else if (!isTypeLegal(RetTy
, RetVT
, true))
1238 // Materialize callee address in a register. FIXME: GV address can be
1239 // handled with a CALLpcrel32 instead.
1240 X86AddressMode CalleeAM
;
1241 if (!X86SelectCallAddress(Callee
, CalleeAM
))
1243 unsigned CalleeOp
= 0;
1244 GlobalValue
*GV
= 0;
1245 if (CalleeAM
.GV
!= 0) {
1247 } else if (CalleeAM
.Base
.Reg
!= 0) {
1248 CalleeOp
= CalleeAM
.Base
.Reg
;
1252 // Allow calls which produce i1 results.
1253 bool AndToI1
= false;
1254 if (RetVT
== MVT::i1
) {
1259 // Deal with call operands first.
1260 SmallVector
<Value
*, 8> ArgVals
;
1261 SmallVector
<unsigned, 8> Args
;
1262 SmallVector
<MVT
, 8> ArgVTs
;
1263 SmallVector
<ISD::ArgFlagsTy
, 8> ArgFlags
;
1264 Args
.reserve(CS
.arg_size());
1265 ArgVals
.reserve(CS
.arg_size());
1266 ArgVTs
.reserve(CS
.arg_size());
1267 ArgFlags
.reserve(CS
.arg_size());
1268 for (CallSite::arg_iterator i
= CS
.arg_begin(), e
= CS
.arg_end();
1270 unsigned Arg
= getRegForValue(*i
);
1273 ISD::ArgFlagsTy Flags
;
1274 unsigned AttrInd
= i
- CS
.arg_begin() + 1;
1275 if (CS
.paramHasAttr(AttrInd
, Attribute::SExt
))
1277 if (CS
.paramHasAttr(AttrInd
, Attribute::ZExt
))
1280 // FIXME: Only handle *easy* calls for now.
1281 if (CS
.paramHasAttr(AttrInd
, Attribute::InReg
) ||
1282 CS
.paramHasAttr(AttrInd
, Attribute::StructRet
) ||
1283 CS
.paramHasAttr(AttrInd
, Attribute::Nest
) ||
1284 CS
.paramHasAttr(AttrInd
, Attribute::ByVal
))
1287 const Type
*ArgTy
= (*i
)->getType();
1289 if (!isTypeLegal(ArgTy
, ArgVT
))
1291 unsigned OriginalAlignment
= TD
.getABITypeAlignment(ArgTy
);
1292 Flags
.setOrigAlign(OriginalAlignment
);
1294 Args
.push_back(Arg
);
1295 ArgVals
.push_back(*i
);
1296 ArgVTs
.push_back(ArgVT
);
1297 ArgFlags
.push_back(Flags
);
1300 // Analyze operands of the call, assigning locations to each operand.
1301 SmallVector
<CCValAssign
, 16> ArgLocs
;
1302 CCState
CCInfo(CC
, false, TM
, ArgLocs
, I
->getParent()->getContext());
1303 CCInfo
.AnalyzeCallOperands(ArgVTs
, ArgFlags
, CCAssignFnForCall(CC
));
1305 // Get a count of how many bytes are to be pushed on the stack.
1306 unsigned NumBytes
= CCInfo
.getNextStackOffset();
1308 // Issue CALLSEQ_START
1309 unsigned AdjStackDown
= TM
.getRegisterInfo()->getCallFrameSetupOpcode();
1310 BuildMI(MBB
, DL
, TII
.get(AdjStackDown
)).addImm(NumBytes
);
1312 // Process argument: walk the register/memloc assignments, inserting
1314 SmallVector
<unsigned, 4> RegArgs
;
1315 for (unsigned i
= 0, e
= ArgLocs
.size(); i
!= e
; ++i
) {
1316 CCValAssign
&VA
= ArgLocs
[i
];
1317 unsigned Arg
= Args
[VA
.getValNo()];
1318 MVT ArgVT
= ArgVTs
[VA
.getValNo()];
1320 // Promote the value if needed.
1321 switch (VA
.getLocInfo()) {
1322 default: llvm_unreachable("Unknown loc info!");
1323 case CCValAssign::Full
: break;
1324 case CCValAssign::SExt
: {
1325 bool Emitted
= X86FastEmitExtend(ISD::SIGN_EXTEND
, VA
.getLocVT(),
1327 assert(Emitted
&& "Failed to emit a sext!"); Emitted
=Emitted
;
1329 ArgVT
= VA
.getLocVT();
1332 case CCValAssign::ZExt
: {
1333 bool Emitted
= X86FastEmitExtend(ISD::ZERO_EXTEND
, VA
.getLocVT(),
1335 assert(Emitted
&& "Failed to emit a zext!"); Emitted
=Emitted
;
1337 ArgVT
= VA
.getLocVT();
1340 case CCValAssign::AExt
: {
1341 bool Emitted
= X86FastEmitExtend(ISD::ANY_EXTEND
, VA
.getLocVT(),
1344 Emitted
= X86FastEmitExtend(ISD::ZERO_EXTEND
, VA
.getLocVT(),
1347 Emitted
= X86FastEmitExtend(ISD::SIGN_EXTEND
, VA
.getLocVT(),
1350 assert(Emitted
&& "Failed to emit a aext!"); Emitted
=Emitted
;
1351 ArgVT
= VA
.getLocVT();
1356 if (VA
.isRegLoc()) {
1357 TargetRegisterClass
* RC
= TLI
.getRegClassFor(ArgVT
);
1358 bool Emitted
= TII
.copyRegToReg(*MBB
, MBB
->end(), VA
.getLocReg(),
1360 assert(Emitted
&& "Failed to emit a copy instruction!"); Emitted
=Emitted
;
1362 RegArgs
.push_back(VA
.getLocReg());
1364 unsigned LocMemOffset
= VA
.getLocMemOffset();
1366 AM
.Base
.Reg
= StackPtr
;
1367 AM
.Disp
= LocMemOffset
;
1368 Value
*ArgVal
= ArgVals
[VA
.getValNo()];
1370 // If this is a really simple value, emit this with the Value* version of
1371 // X86FastEmitStore. If it isn't simple, we don't want to do this, as it
1372 // can cause us to reevaluate the argument.
1373 if (isa
<ConstantInt
>(ArgVal
) || isa
<ConstantPointerNull
>(ArgVal
))
1374 X86FastEmitStore(ArgVT
, ArgVal
, AM
);
1376 X86FastEmitStore(ArgVT
, Arg
, AM
);
1380 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1382 if (Subtarget
->isPICStyleGOT()) {
1383 TargetRegisterClass
*RC
= X86::GR32RegisterClass
;
1384 unsigned Base
= getInstrInfo()->getGlobalBaseReg(&MF
);
1385 bool Emitted
= TII
.copyRegToReg(*MBB
, MBB
->end(), X86::EBX
, Base
, RC
, RC
);
1386 assert(Emitted
&& "Failed to emit a copy instruction!"); Emitted
=Emitted
;
1391 MachineInstrBuilder MIB
;
1393 // Register-indirect call.
1394 unsigned CallOpc
= Subtarget
->is64Bit() ? X86::CALL64r
: X86::CALL32r
;
1395 MIB
= BuildMI(MBB
, DL
, TII
.get(CallOpc
)).addReg(CalleeOp
);
1399 assert(GV
&& "Not a direct call");
1401 Subtarget
->is64Bit() ? X86::CALL64pcrel32
: X86::CALLpcrel32
;
1403 // See if we need any target-specific flags on the GV operand.
1404 unsigned char OpFlags
= 0;
1406 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1407 // external symbols most go through the PLT in PIC mode. If the symbol
1408 // has hidden or protected visibility, or if it is static or local, then
1409 // we don't need to use the PLT - we can directly call it.
1410 if (Subtarget
->isTargetELF() &&
1411 TM
.getRelocationModel() == Reloc::PIC_
&&
1412 GV
->hasDefaultVisibility() && !GV
->hasLocalLinkage()) {
1413 OpFlags
= X86II::MO_PLT
;
1414 } else if (Subtarget
->isPICStyleStubAny() &&
1415 (GV
->isDeclaration() || GV
->isWeakForLinker()) &&
1416 Subtarget
->getDarwinVers() < 9) {
1417 // PC-relative references to external symbols should go through $stub,
1418 // unless we're building with the leopard linker or later, which
1419 // automatically synthesizes these stubs.
1420 OpFlags
= X86II::MO_DARWIN_STUB
;
1424 MIB
= BuildMI(MBB
, DL
, TII
.get(CallOpc
)).addGlobalAddress(GV
, 0, OpFlags
);
1427 // Add an implicit use GOT pointer in EBX.
1428 if (Subtarget
->isPICStyleGOT())
1429 MIB
.addReg(X86::EBX
);
1431 // Add implicit physical register uses to the call.
1432 for (unsigned i
= 0, e
= RegArgs
.size(); i
!= e
; ++i
)
1433 MIB
.addReg(RegArgs
[i
]);
1435 // Issue CALLSEQ_END
1436 unsigned AdjStackUp
= TM
.getRegisterInfo()->getCallFrameDestroyOpcode();
1437 BuildMI(MBB
, DL
, TII
.get(AdjStackUp
)).addImm(NumBytes
).addImm(0);
1439 // Now handle call return value (if any).
1440 if (RetVT
.getSimpleVT() != MVT::isVoid
) {
1441 SmallVector
<CCValAssign
, 16> RVLocs
;
1442 CCState
CCInfo(CC
, false, TM
, RVLocs
, I
->getParent()->getContext());
1443 CCInfo
.AnalyzeCallResult(RetVT
, RetCC_X86
);
1445 // Copy all of the result registers out of their specified physreg.
1446 assert(RVLocs
.size() == 1 && "Can't handle multi-value calls!");
1447 MVT CopyVT
= RVLocs
[0].getValVT();
1448 TargetRegisterClass
* DstRC
= TLI
.getRegClassFor(CopyVT
);
1449 TargetRegisterClass
*SrcRC
= DstRC
;
1451 // If this is a call to a function that returns an fp value on the x87 fp
1452 // stack, but where we prefer to use the value in xmm registers, copy it
1453 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1454 if ((RVLocs
[0].getLocReg() == X86::ST0
||
1455 RVLocs
[0].getLocReg() == X86::ST1
) &&
1456 isScalarFPTypeInSSEReg(RVLocs
[0].getValVT())) {
1458 SrcRC
= X86::RSTRegisterClass
;
1459 DstRC
= X86::RFP80RegisterClass
;
1462 unsigned ResultReg
= createResultReg(DstRC
);
1463 bool Emitted
= TII
.copyRegToReg(*MBB
, MBB
->end(), ResultReg
,
1464 RVLocs
[0].getLocReg(), DstRC
, SrcRC
);
1465 assert(Emitted
&& "Failed to emit a copy instruction!"); Emitted
=Emitted
;
1467 if (CopyVT
!= RVLocs
[0].getValVT()) {
1468 // Round the F80 the right size, which also moves to the appropriate xmm
1469 // register. This is accomplished by storing the F80 value in memory and
1470 // then loading it back. Ewww...
1471 MVT ResVT
= RVLocs
[0].getValVT();
1472 unsigned Opc
= ResVT
== MVT::f32
? X86::ST_Fp80m32
: X86::ST_Fp80m64
;
1473 unsigned MemSize
= ResVT
.getSizeInBits()/8;
1474 int FI
= MFI
.CreateStackObject(MemSize
, MemSize
);
1475 addFrameReference(BuildMI(MBB
, DL
, TII
.get(Opc
)), FI
).addReg(ResultReg
);
1476 DstRC
= ResVT
== MVT::f32
1477 ? X86::FR32RegisterClass
: X86::FR64RegisterClass
;
1478 Opc
= ResVT
== MVT::f32
? X86::MOVSSrm
: X86::MOVSDrm
;
1479 ResultReg
= createResultReg(DstRC
);
1480 addFrameReference(BuildMI(MBB
, DL
, TII
.get(Opc
), ResultReg
), FI
);
1484 // Mask out all but lowest bit for some call which produces an i1.
1485 unsigned AndResult
= createResultReg(X86::GR8RegisterClass
);
1487 TII
.get(X86::AND8ri
), AndResult
).addReg(ResultReg
).addImm(1);
1488 ResultReg
= AndResult
;
1491 UpdateValueMap(I
, ResultReg
);
1499 X86FastISel::TargetSelectInstruction(Instruction
*I
) {
1500 switch (I
->getOpcode()) {
1502 case Instruction::Load
:
1503 return X86SelectLoad(I
);
1504 case Instruction::Store
:
1505 return X86SelectStore(I
);
1506 case Instruction::ICmp
:
1507 case Instruction::FCmp
:
1508 return X86SelectCmp(I
);
1509 case Instruction::ZExt
:
1510 return X86SelectZExt(I
);
1511 case Instruction::Br
:
1512 return X86SelectBranch(I
);
1513 case Instruction::Call
:
1514 return X86SelectCall(I
);
1515 case Instruction::LShr
:
1516 case Instruction::AShr
:
1517 case Instruction::Shl
:
1518 return X86SelectShift(I
);
1519 case Instruction::Select
:
1520 return X86SelectSelect(I
);
1521 case Instruction::Trunc
:
1522 return X86SelectTrunc(I
);
1523 case Instruction::FPExt
:
1524 return X86SelectFPExt(I
);
1525 case Instruction::FPTrunc
:
1526 return X86SelectFPTrunc(I
);
1527 case Instruction::ExtractValue
:
1528 return X86SelectExtractValue(I
);
1529 case Instruction::IntToPtr
: // Deliberate fall-through.
1530 case Instruction::PtrToInt
: {
1531 MVT SrcVT
= TLI
.getValueType(I
->getOperand(0)->getType());
1532 MVT DstVT
= TLI
.getValueType(I
->getType());
1533 if (DstVT
.bitsGT(SrcVT
))
1534 return X86SelectZExt(I
);
1535 if (DstVT
.bitsLT(SrcVT
))
1536 return X86SelectTrunc(I
);
1537 unsigned Reg
= getRegForValue(I
->getOperand(0));
1538 if (Reg
== 0) return false;
1539 UpdateValueMap(I
, Reg
);
1547 unsigned X86FastISel::TargetMaterializeConstant(Constant
*C
) {
1549 if (!isTypeLegal(C
->getType(), VT
))
1552 // Get opcode and regclass of the output for the given load instruction.
1554 const TargetRegisterClass
*RC
= NULL
;
1555 switch (VT
.getSimpleVT()) {
1556 default: return false;
1559 RC
= X86::GR8RegisterClass
;
1563 RC
= X86::GR16RegisterClass
;
1567 RC
= X86::GR32RegisterClass
;
1570 // Must be in x86-64 mode.
1572 RC
= X86::GR64RegisterClass
;
1575 if (Subtarget
->hasSSE1()) {
1577 RC
= X86::FR32RegisterClass
;
1579 Opc
= X86::LD_Fp32m
;
1580 RC
= X86::RFP32RegisterClass
;
1584 if (Subtarget
->hasSSE2()) {
1586 RC
= X86::FR64RegisterClass
;
1588 Opc
= X86::LD_Fp64m
;
1589 RC
= X86::RFP64RegisterClass
;
1593 // No f80 support yet.
1597 // Materialize addresses with LEA instructions.
1598 if (isa
<GlobalValue
>(C
)) {
1600 if (X86SelectAddress(C
, AM
)) {
1601 if (TLI
.getPointerTy() == MVT::i32
)
1605 unsigned ResultReg
= createResultReg(RC
);
1606 addLeaAddress(BuildMI(MBB
, DL
, TII
.get(Opc
), ResultReg
), AM
);
1612 // MachineConstantPool wants an explicit alignment.
1613 unsigned Align
= TD
.getPrefTypeAlignment(C
->getType());
1615 // Alignment of vector types. FIXME!
1616 Align
= TD
.getTypeAllocSize(C
->getType());
1619 // x86-32 PIC requires a PIC base register for constant pools.
1620 unsigned PICBase
= 0;
1621 unsigned char OpFlag
= 0;
1622 if (Subtarget
->isPICStyleStubPIC()) { // Not dynamic-no-pic
1623 OpFlag
= X86II::MO_PIC_BASE_OFFSET
;
1624 PICBase
= getInstrInfo()->getGlobalBaseReg(&MF
);
1625 } else if (Subtarget
->isPICStyleGOT()) {
1626 OpFlag
= X86II::MO_GOTOFF
;
1627 PICBase
= getInstrInfo()->getGlobalBaseReg(&MF
);
1628 } else if (Subtarget
->isPICStyleRIPRel() &&
1629 TM
.getCodeModel() == CodeModel::Small
) {
1633 // Create the load from the constant pool.
1634 unsigned MCPOffset
= MCP
.getConstantPoolIndex(C
, Align
);
1635 unsigned ResultReg
= createResultReg(RC
);
1636 addConstantPoolReference(BuildMI(MBB
, DL
, TII
.get(Opc
), ResultReg
),
1637 MCPOffset
, PICBase
, OpFlag
);
1642 unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst
*C
) {
1643 // Fail on dynamic allocas. At this point, getRegForValue has already
1644 // checked its CSE maps, so if we're here trying to handle a dynamic
1645 // alloca, we're not going to succeed. X86SelectAddress has a
1646 // check for dynamic allocas, because it's called directly from
1647 // various places, but TargetMaterializeAlloca also needs a check
1648 // in order to avoid recursion between getRegForValue,
1649 // X86SelectAddrss, and TargetMaterializeAlloca.
1650 if (!StaticAllocaMap
.count(C
))
1654 if (!X86SelectAddress(C
, AM
))
1656 unsigned Opc
= Subtarget
->is64Bit() ? X86::LEA64r
: X86::LEA32r
;
1657 TargetRegisterClass
* RC
= TLI
.getRegClassFor(TLI
.getPointerTy());
1658 unsigned ResultReg
= createResultReg(RC
);
1659 addLeaAddress(BuildMI(MBB
, DL
, TII
.get(Opc
), ResultReg
), AM
);
1664 llvm::FastISel
*X86::createFastISel(MachineFunction
&mf
,
1665 MachineModuleInfo
*mmi
,
1667 DenseMap
<const Value
*, unsigned> &vm
,
1668 DenseMap
<const BasicBlock
*, MachineBasicBlock
*> &bm
,
1669 DenseMap
<const AllocaInst
*, int> &am
1671 , SmallSet
<Instruction
*, 8> &cil
1674 return new X86FastISel(mf
, mmi
, dw
, vm
, bm
, am