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/Instructions.h"
25 #include "llvm/CodeGen/FastISel.h"
26 #include "llvm/CodeGen/MachineConstantPool.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineRegisterInfo.h"
29 #include "llvm/Support/CallSite.h"
30 #include "llvm/Support/GetElementPtrTypeIterator.h"
34 class X86FastISel : public FastISel {
35 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
36 /// make the right decision when generating code for different targets.
37 const X86Subtarget *Subtarget;
39 /// StackPtr - Register used as the stack pointer.
43 /// GlobalBaseReg - keeps track of the virtual register mapped onto global
45 unsigned GlobalBaseReg;
47 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
48 /// floating point ops.
49 /// When SSE is available, use it for f32 operations.
50 /// When SSE2 is available, use it for f64 operations.
55 explicit X86FastISel(MachineFunction &mf,
56 MachineModuleInfo *mmi,
57 DenseMap<const Value *, unsigned> &vm,
58 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
59 DenseMap<const AllocaInst *, int> &am)
60 : FastISel(mf, mmi, vm, bm, am) {
61 Subtarget = &TM.getSubtarget<X86Subtarget>();
62 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
64 X86ScalarSSEf64 = Subtarget->hasSSE2();
65 X86ScalarSSEf32 = Subtarget->hasSSE1();
68 virtual bool TargetSelectInstruction(Instruction *I);
70 #include "X86GenFastISel.inc"
73 bool X86FastEmitLoad(MVT VT, const X86AddressMode &AM, unsigned &RR);
75 bool X86FastEmitStore(MVT VT, unsigned Val,
76 const X86AddressMode &AM);
78 bool X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT, unsigned Src, MVT SrcVT,
81 bool X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall);
83 bool X86SelectLoad(Instruction *I);
85 bool X86SelectStore(Instruction *I);
87 bool X86SelectCmp(Instruction *I);
89 bool X86SelectZExt(Instruction *I);
91 bool X86SelectBranch(Instruction *I);
93 bool X86SelectShift(Instruction *I);
95 bool X86SelectSelect(Instruction *I);
97 bool X86SelectTrunc(Instruction *I);
99 bool X86SelectFPExt(Instruction *I);
100 bool X86SelectFPTrunc(Instruction *I);
102 bool X86SelectCall(Instruction *I);
104 CCAssignFn *CCAssignFnForCall(unsigned CC, bool isTailCall = false);
106 unsigned getGlobalBaseReg();
108 const X86InstrInfo *getInstrInfo() const {
109 return static_cast<const X86InstrInfo *>(TM.getInstrInfo());
112 unsigned TargetMaterializeConstant(Constant *C);
114 unsigned TargetMaterializeAlloca(AllocaInst *C);
116 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
117 /// computed in an SSE register, not on the X87 floating point stack.
118 bool isScalarFPTypeInSSEReg(MVT VT) const {
119 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
120 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
125 static bool isTypeLegal(const Type *Ty, const TargetLowering &TLI, MVT &VT,
126 bool AllowI1 = false) {
127 VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
128 if (VT == MVT::Other || !VT.isSimple())
129 // Unhandled type. Halt "fast" selection and bail.
133 VT = TLI.getPointerTy();
134 // We only handle legal types. For example, on x86-32 the instruction
135 // selector contains all of the 64-bit instructions from x86-64,
136 // under the assumption that i64 won't be used if the target doesn't
138 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
141 /// getGlobalBaseReg - Return the the global base register. Output
142 /// instructions required to initialize the global base register, if necessary.
144 unsigned X86FastISel::getGlobalBaseReg() {
145 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
147 GlobalBaseReg = getInstrInfo()->initializeGlobalBaseReg(MBB->getParent());
148 return GlobalBaseReg;
151 #include "X86GenCallingConv.inc"
153 /// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
155 CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) {
156 if (Subtarget->is64Bit()) {
157 if (Subtarget->isTargetWin64())
158 return CC_X86_Win64_C;
159 else if (CC == CallingConv::Fast && isTaillCall)
160 return CC_X86_64_TailCall;
165 if (CC == CallingConv::X86_FastCall)
166 return CC_X86_32_FastCall;
167 else if (CC == CallingConv::Fast)
168 return CC_X86_32_FastCC;
173 /// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
174 /// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
175 /// Return true and the result register by reference if it is possible.
176 bool X86FastISel::X86FastEmitLoad(MVT VT, const X86AddressMode &AM,
177 unsigned &ResultReg) {
178 // Get opcode and regclass of the output for the given load instruction.
180 const TargetRegisterClass *RC = NULL;
181 switch (VT.getSimpleVT()) {
182 default: return false;
185 RC = X86::GR8RegisterClass;
189 RC = X86::GR16RegisterClass;
193 RC = X86::GR32RegisterClass;
196 // Must be in x86-64 mode.
198 RC = X86::GR64RegisterClass;
201 if (Subtarget->hasSSE1()) {
203 RC = X86::FR32RegisterClass;
206 RC = X86::RFP32RegisterClass;
210 if (Subtarget->hasSSE2()) {
212 RC = X86::FR64RegisterClass;
215 RC = X86::RFP64RegisterClass;
219 // No f80 support yet.
223 ResultReg = createResultReg(RC);
224 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
228 /// X86FastEmitStore - Emit a machine instruction to store a value Val of
229 /// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
230 /// and a displacement offset, or a GlobalAddress,
231 /// i.e. V. Return true if it is possible.
233 X86FastISel::X86FastEmitStore(MVT VT, unsigned Val,
234 const X86AddressMode &AM) {
235 // Get opcode and regclass of the output for the given store instruction.
237 const TargetRegisterClass *RC = NULL;
238 switch (VT.getSimpleVT()) {
239 default: return false;
242 RC = X86::GR8RegisterClass;
246 RC = X86::GR16RegisterClass;
250 RC = X86::GR32RegisterClass;
253 // Must be in x86-64 mode.
255 RC = X86::GR64RegisterClass;
258 if (Subtarget->hasSSE1()) {
260 RC = X86::FR32RegisterClass;
263 RC = X86::RFP32RegisterClass;
267 if (Subtarget->hasSSE2()) {
269 RC = X86::FR64RegisterClass;
272 RC = X86::RFP64RegisterClass;
276 // No f80 support yet.
280 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Val);
284 /// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
285 /// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
286 /// ISD::SIGN_EXTEND).
287 bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT,
288 unsigned Src, MVT SrcVT,
289 unsigned &ResultReg) {
290 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
299 /// X86SelectAddress - Attempt to fill in an address from the given value.
301 bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall) {
303 unsigned Opcode = Instruction::UserOp1;
304 if (Instruction *I = dyn_cast<Instruction>(V)) {
305 Opcode = I->getOpcode();
307 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
308 Opcode = C->getOpcode();
314 case Instruction::BitCast:
315 // Look past bitcasts.
316 return X86SelectAddress(U->getOperand(0), AM, isCall);
318 case Instruction::IntToPtr:
319 // Look past no-op inttoptrs.
320 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
321 return X86SelectAddress(U->getOperand(0), AM, isCall);
323 case Instruction::PtrToInt:
324 // Look past no-op ptrtoints.
325 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
326 return X86SelectAddress(U->getOperand(0), AM, isCall);
328 case Instruction::Alloca: {
330 // Do static allocas.
331 const AllocaInst *A = cast<AllocaInst>(V);
332 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
333 if (SI == StaticAllocaMap.end())
335 AM.BaseType = X86AddressMode::FrameIndexBase;
336 AM.Base.FrameIndex = SI->second;
340 case Instruction::Add: {
342 // Adds of constants are common and easy enough.
343 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
344 AM.Disp += CI->getZExtValue();
345 return X86SelectAddress(U->getOperand(0), AM, isCall);
350 case Instruction::GetElementPtr: {
352 // Pattern-match simple GEPs.
353 uint64_t Disp = AM.Disp;
354 unsigned IndexReg = AM.IndexReg;
355 unsigned Scale = AM.Scale;
356 gep_type_iterator GTI = gep_type_begin(U);
357 // Look at all but the last index. Constants can be folded,
358 // and one dynamic index can be handled, if the scale is supported.
359 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
360 i != e; ++i, ++GTI) {
362 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
363 const StructLayout *SL = TD.getStructLayout(STy);
364 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
365 Disp += SL->getElementOffset(Idx);
367 uint64_t S = TD.getABITypeSize(GTI.getIndexedType());
368 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
369 // Constant-offset addressing.
370 Disp += CI->getZExtValue() * S;
371 } else if (IndexReg == 0 &&
372 (S == 1 || S == 2 || S == 4 || S == 8)) {
373 // Scaled-index addressing.
375 IndexReg = getRegForValue(Op);
380 goto unsupported_gep;
383 // Ok, the GEP indices were covered by constant-offset and scaled-index
384 // addressing. Update the address state and move on to examining the base.
385 AM.IndexReg = IndexReg;
388 return X86SelectAddress(U->getOperand(0), AM, isCall);
390 // Ok, the GEP indices weren't all covered.
395 // Handle constant address.
396 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
397 // Can't handle alternate code models yet.
398 if (TM.getCodeModel() != CodeModel::Default &&
399 TM.getCodeModel() != CodeModel::Small)
402 // Set up the basic address.
405 TM.getRelocationModel() == Reloc::PIC_ &&
406 !Subtarget->is64Bit())
407 AM.Base.Reg = getGlobalBaseReg();
409 // Emit an extra load if the ABI requires it.
410 if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) {
411 // Check to see if we've already materialized this
412 // value in a register in this block.
413 if (unsigned Reg = LocalValueMap[V]) {
418 // Issue load from stub if necessary.
420 const TargetRegisterClass *RC = NULL;
421 if (TLI.getPointerTy() == MVT::i32) {
423 RC = X86::GR32RegisterClass;
426 RC = X86::GR64RegisterClass;
429 X86AddressMode StubAM;
430 StubAM.Base.Reg = AM.Base.Reg;
432 unsigned ResultReg = createResultReg(RC);
433 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), StubAM);
435 // Now construct the final address. Note that the Disp, Scale,
436 // and Index values may already be set here.
437 AM.Base.Reg = ResultReg;
440 // Prevent loading GV stub multiple times in same MBB.
441 LocalValueMap[V] = AM.Base.Reg;
446 // If all else fails, just materialize the value in a register.
447 AM.Base.Reg = getRegForValue(V);
448 return AM.Base.Reg != 0;
451 /// X86SelectStore - Select and emit code to implement store instructions.
452 bool X86FastISel::X86SelectStore(Instruction* I) {
454 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
456 unsigned Val = getRegForValue(I->getOperand(0));
458 // Unhandled operand. Halt "fast" selection and bail.
462 if (!X86SelectAddress(I->getOperand(1), AM, false))
465 return X86FastEmitStore(VT, Val, AM);
468 /// X86SelectLoad - Select and emit code to implement load instructions.
470 bool X86FastISel::X86SelectLoad(Instruction *I) {
472 if (!isTypeLegal(I->getType(), TLI, VT))
476 if (!X86SelectAddress(I->getOperand(0), AM, false))
479 unsigned ResultReg = 0;
480 if (X86FastEmitLoad(VT, AM, ResultReg)) {
481 UpdateValueMap(I, ResultReg);
487 bool X86FastISel::X86SelectCmp(Instruction *I) {
488 CmpInst *CI = cast<CmpInst>(I);
490 MVT VT = TLI.getValueType(I->getOperand(0)->getType());
491 if (!TLI.isTypeLegal(VT))
494 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
495 if (Op0Reg == 0) return false;
496 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
497 if (Op1Reg == 0) return false;
500 switch (VT.getSimpleVT()) {
501 case MVT::i8: Opc = X86::CMP8rr; break;
502 case MVT::i16: Opc = X86::CMP16rr; break;
503 case MVT::i32: Opc = X86::CMP32rr; break;
504 case MVT::i64: Opc = X86::CMP64rr; break;
505 case MVT::f32: Opc = X86::UCOMISSrr; break;
506 case MVT::f64: Opc = X86::UCOMISDrr; break;
507 default: return false;
510 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
511 switch (CI->getPredicate()) {
512 case CmpInst::FCMP_OEQ: {
513 unsigned EReg = createResultReg(&X86::GR8RegClass);
514 unsigned NPReg = createResultReg(&X86::GR8RegClass);
515 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
516 BuildMI(MBB, TII.get(X86::SETEr), EReg);
517 BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
518 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
521 case CmpInst::FCMP_UNE: {
522 unsigned NEReg = createResultReg(&X86::GR8RegClass);
523 unsigned PReg = createResultReg(&X86::GR8RegClass);
524 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
525 BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
526 BuildMI(MBB, TII.get(X86::SETPr), PReg);
527 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
530 case CmpInst::FCMP_OGT:
531 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
532 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
534 case CmpInst::FCMP_OGE:
535 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
536 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
538 case CmpInst::FCMP_OLT:
539 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
540 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
542 case CmpInst::FCMP_OLE:
543 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
544 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
546 case CmpInst::FCMP_ONE:
547 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
548 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
550 case CmpInst::FCMP_ORD:
551 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
552 BuildMI(MBB, TII.get(X86::SETNPr), ResultReg);
554 case CmpInst::FCMP_UNO:
555 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
556 BuildMI(MBB, TII.get(X86::SETPr), ResultReg);
558 case CmpInst::FCMP_UEQ:
559 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
560 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
562 case CmpInst::FCMP_UGT:
563 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
564 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
566 case CmpInst::FCMP_UGE:
567 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
568 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
570 case CmpInst::FCMP_ULT:
571 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
572 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
574 case CmpInst::FCMP_ULE:
575 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
576 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
578 case CmpInst::ICMP_EQ:
579 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
580 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
582 case CmpInst::ICMP_NE:
583 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
584 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
586 case CmpInst::ICMP_UGT:
587 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
588 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
590 case CmpInst::ICMP_UGE:
591 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
592 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
594 case CmpInst::ICMP_ULT:
595 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
596 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
598 case CmpInst::ICMP_ULE:
599 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
600 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
602 case CmpInst::ICMP_SGT:
603 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
604 BuildMI(MBB, TII.get(X86::SETGr), ResultReg);
606 case CmpInst::ICMP_SGE:
607 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
608 BuildMI(MBB, TII.get(X86::SETGEr), ResultReg);
610 case CmpInst::ICMP_SLT:
611 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
612 BuildMI(MBB, TII.get(X86::SETLr), ResultReg);
614 case CmpInst::ICMP_SLE:
615 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
616 BuildMI(MBB, TII.get(X86::SETLEr), ResultReg);
622 UpdateValueMap(I, ResultReg);
626 bool X86FastISel::X86SelectZExt(Instruction *I) {
627 // Special-case hack: The only i1 values we know how to produce currently
628 // set the upper bits of an i8 value to zero.
629 if (I->getType() == Type::Int8Ty &&
630 I->getOperand(0)->getType() == Type::Int1Ty) {
631 unsigned ResultReg = getRegForValue(I->getOperand(0));
632 if (ResultReg == 0) return false;
633 UpdateValueMap(I, ResultReg);
640 bool X86FastISel::X86SelectBranch(Instruction *I) {
641 BranchInst *BI = cast<BranchInst>(I);
642 // Unconditional branches are selected by tablegen-generated code.
643 unsigned OpReg = getRegForValue(BI->getCondition());
644 if (OpReg == 0) return false;
645 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
646 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
648 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
649 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
650 BuildMI(MBB, TII.get(X86::JMP)).addMBB(FalseMBB);
652 MBB->addSuccessor(TrueMBB);
653 MBB->addSuccessor(FalseMBB);
658 bool X86FastISel::X86SelectShift(Instruction *I) {
659 unsigned CReg = 0, OpReg = 0, OpImm = 0;
660 const TargetRegisterClass *RC = NULL;
661 if (I->getType() == Type::Int8Ty) {
663 RC = &X86::GR8RegClass;
664 switch (I->getOpcode()) {
665 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
666 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
667 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
668 default: return false;
670 } else if (I->getType() == Type::Int16Ty) {
672 RC = &X86::GR16RegClass;
673 switch (I->getOpcode()) {
674 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
675 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
676 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
677 default: return false;
679 } else if (I->getType() == Type::Int32Ty) {
681 RC = &X86::GR32RegClass;
682 switch (I->getOpcode()) {
683 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
684 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
685 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
686 default: return false;
688 } else if (I->getType() == Type::Int64Ty) {
690 RC = &X86::GR64RegClass;
691 switch (I->getOpcode()) {
692 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
693 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
694 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
695 default: return false;
701 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
702 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
705 unsigned Op0Reg = getRegForValue(I->getOperand(0));
706 if (Op0Reg == 0) return false;
708 // Fold immediate in shl(x,3).
709 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
710 unsigned ResultReg = createResultReg(RC);
711 BuildMI(MBB, TII.get(OpImm),
712 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue());
713 UpdateValueMap(I, ResultReg);
717 unsigned Op1Reg = getRegForValue(I->getOperand(1));
718 if (Op1Reg == 0) return false;
719 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
720 unsigned ResultReg = createResultReg(RC);
721 BuildMI(MBB, TII.get(OpReg), ResultReg).addReg(Op0Reg);
722 UpdateValueMap(I, ResultReg);
726 bool X86FastISel::X86SelectSelect(Instruction *I) {
727 const Type *Ty = I->getType();
728 if (isa<PointerType>(Ty))
729 Ty = TD.getIntPtrType();
732 const TargetRegisterClass *RC = NULL;
733 if (Ty == Type::Int16Ty) {
734 Opc = X86::CMOVE16rr;
735 RC = &X86::GR16RegClass;
736 } else if (Ty == Type::Int32Ty) {
737 Opc = X86::CMOVE32rr;
738 RC = &X86::GR32RegClass;
739 } else if (Ty == Type::Int64Ty) {
740 Opc = X86::CMOVE64rr;
741 RC = &X86::GR64RegClass;
746 MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
747 if (VT == MVT::Other || !TLI.isTypeLegal(VT))
750 unsigned Op0Reg = getRegForValue(I->getOperand(0));
751 if (Op0Reg == 0) return false;
752 unsigned Op1Reg = getRegForValue(I->getOperand(1));
753 if (Op1Reg == 0) return false;
754 unsigned Op2Reg = getRegForValue(I->getOperand(2));
755 if (Op2Reg == 0) return false;
757 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
758 unsigned ResultReg = createResultReg(RC);
759 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
760 UpdateValueMap(I, ResultReg);
764 bool X86FastISel::X86SelectFPExt(Instruction *I) {
765 if (Subtarget->hasSSE2()) {
766 if (I->getType() == Type::DoubleTy) {
767 Value *V = I->getOperand(0);
768 if (V->getType() == Type::FloatTy) {
769 unsigned OpReg = getRegForValue(V);
770 if (OpReg == 0) return false;
771 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
772 BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
773 UpdateValueMap(I, ResultReg);
782 bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
783 if (Subtarget->hasSSE2()) {
784 if (I->getType() == Type::FloatTy) {
785 Value *V = I->getOperand(0);
786 if (V->getType() == Type::DoubleTy) {
787 unsigned OpReg = getRegForValue(V);
788 if (OpReg == 0) return false;
789 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
790 BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
791 UpdateValueMap(I, ResultReg);
800 bool X86FastISel::X86SelectTrunc(Instruction *I) {
801 if (Subtarget->is64Bit())
802 // All other cases should be handled by the tblgen generated code.
804 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
805 MVT DstVT = TLI.getValueType(I->getType());
806 if (DstVT != MVT::i8)
807 // All other cases should be handled by the tblgen generated code.
809 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
810 // All other cases should be handled by the tblgen generated code.
813 unsigned InputReg = getRegForValue(I->getOperand(0));
815 // Unhandled operand. Halt "fast" selection and bail.
818 // First issue a copy to GR16_ or GR32_.
819 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
820 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
821 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
822 unsigned CopyReg = createResultReg(CopyRC);
823 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
825 // Then issue an extract_subreg.
826 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg,1); // x86_subreg_8bit
830 UpdateValueMap(I, ResultReg);
834 bool X86FastISel::X86SelectCall(Instruction *I) {
835 CallInst *CI = cast<CallInst>(I);
836 Value *Callee = I->getOperand(0);
838 // Can't handle inline asm yet.
839 if (isa<InlineAsm>(Callee))
842 // FIXME: Handle some intrinsics.
843 if (Function *F = CI->getCalledFunction()) {
844 if (F->isDeclaration() &&F->getIntrinsicID())
848 // Handle only C and fastcc calling conventions for now.
850 unsigned CC = CS.getCallingConv();
851 if (CC != CallingConv::C &&
852 CC != CallingConv::Fast &&
853 CC != CallingConv::X86_FastCall)
856 // Let SDISel handle vararg functions.
857 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
858 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
862 // Handle *simple* calls for now.
863 const Type *RetTy = CS.getType();
865 if (RetTy == Type::VoidTy)
867 else if (!isTypeLegal(RetTy, TLI, RetVT, true))
870 // Materialize callee address in a register. FIXME: GV address can be
871 // handled with a CALLpcrel32 instead.
872 X86AddressMode CalleeAM;
873 if (!X86SelectAddress(Callee, CalleeAM, true))
875 unsigned CalleeOp = 0;
877 if (CalleeAM.Base.Reg != 0) {
878 assert(CalleeAM.GV == 0);
879 CalleeOp = CalleeAM.Base.Reg;
880 } else if (CalleeAM.GV != 0) {
881 assert(CalleeAM.GV != 0);
886 // Allow calls which produce i1 results.
887 bool AndToI1 = false;
888 if (RetVT == MVT::i1) {
893 // Deal with call operands first.
894 SmallVector<unsigned, 4> Args;
895 SmallVector<MVT, 4> ArgVTs;
896 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
897 Args.reserve(CS.arg_size());
898 ArgVTs.reserve(CS.arg_size());
899 ArgFlags.reserve(CS.arg_size());
900 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
902 unsigned Arg = getRegForValue(*i);
905 ISD::ArgFlagsTy Flags;
906 unsigned AttrInd = i - CS.arg_begin() + 1;
907 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
909 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
912 // FIXME: Only handle *easy* calls for now.
913 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
914 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
915 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
916 CS.paramHasAttr(AttrInd, Attribute::ByVal))
919 const Type *ArgTy = (*i)->getType();
921 if (!isTypeLegal(ArgTy, TLI, ArgVT))
923 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
924 Flags.setOrigAlign(OriginalAlignment);
927 ArgVTs.push_back(ArgVT);
928 ArgFlags.push_back(Flags);
931 // Analyze operands of the call, assigning locations to each operand.
932 SmallVector<CCValAssign, 16> ArgLocs;
933 CCState CCInfo(CC, false, TM, ArgLocs);
934 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
936 // Get a count of how many bytes are to be pushed on the stack.
937 unsigned NumBytes = CCInfo.getNextStackOffset();
939 // Issue CALLSEQ_START
940 BuildMI(MBB, TII.get(X86::ADJCALLSTACKDOWN)).addImm(NumBytes);
942 // Process argumenet: walk the register/memloc assignments, inserting
944 SmallVector<unsigned, 4> RegArgs;
945 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
946 CCValAssign &VA = ArgLocs[i];
947 unsigned Arg = Args[VA.getValNo()];
948 MVT ArgVT = ArgVTs[VA.getValNo()];
950 // Promote the value if needed.
951 switch (VA.getLocInfo()) {
952 default: assert(0 && "Unknown loc info!");
953 case CCValAssign::Full: break;
954 case CCValAssign::SExt: {
955 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
957 assert(Emitted && "Failed to emit a sext!");
958 ArgVT = VA.getLocVT();
961 case CCValAssign::ZExt: {
962 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
964 assert(Emitted && "Failed to emit a zext!");
965 ArgVT = VA.getLocVT();
968 case CCValAssign::AExt: {
969 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
972 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
975 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
978 assert(Emitted && "Failed to emit a aext!");
979 ArgVT = VA.getLocVT();
985 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
986 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
988 assert(Emitted && "Failed to emit a copy instruction!");
989 RegArgs.push_back(VA.getLocReg());
991 unsigned LocMemOffset = VA.getLocMemOffset();
993 AM.Base.Reg = StackPtr;
994 AM.Disp = LocMemOffset;
995 X86FastEmitStore(ArgVT, Arg, AM);
999 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1001 if (!Subtarget->is64Bit() &&
1002 TM.getRelocationModel() == Reloc::PIC_ &&
1003 Subtarget->isPICStyleGOT()) {
1004 TargetRegisterClass *RC = X86::GR32RegisterClass;
1005 unsigned Base = getGlobalBaseReg();
1006 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
1007 assert(Emitted && "Failed to emit a copy instruction!");
1011 unsigned CallOpc = CalleeOp
1012 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
1013 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
1014 MachineInstrBuilder MIB = CalleeOp
1015 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
1016 : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV);
1018 // Add an implicit use GOT pointer in EBX.
1019 if (!Subtarget->is64Bit() &&
1020 TM.getRelocationModel() == Reloc::PIC_ &&
1021 Subtarget->isPICStyleGOT())
1022 MIB.addReg(X86::EBX);
1024 // Add implicit physical register uses to the call.
1025 while (!RegArgs.empty()) {
1026 MIB.addReg(RegArgs.back());
1030 // Issue CALLSEQ_END
1031 BuildMI(MBB, TII.get(X86::ADJCALLSTACKUP)).addImm(NumBytes).addImm(0);
1033 // Now handle call return value (if any).
1034 if (RetVT.getSimpleVT() != MVT::isVoid) {
1035 SmallVector<CCValAssign, 16> RVLocs;
1036 CCState CCInfo(CC, false, TM, RVLocs);
1037 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1039 // Copy all of the result registers out of their specified physreg.
1040 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
1041 MVT CopyVT = RVLocs[0].getValVT();
1042 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1043 TargetRegisterClass *SrcRC = DstRC;
1045 // If this is a call to a function that returns an fp value on the x87 fp
1046 // stack, but where we prefer to use the value in xmm registers, copy it
1047 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1048 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1049 RVLocs[0].getLocReg() == X86::ST1) &&
1050 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
1052 SrcRC = X86::RSTRegisterClass;
1053 DstRC = X86::RFP80RegisterClass;
1056 unsigned ResultReg = createResultReg(DstRC);
1057 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1058 RVLocs[0].getLocReg(), DstRC, SrcRC);
1059 assert(Emitted && "Failed to emit a copy instruction!");
1060 if (CopyVT != RVLocs[0].getValVT()) {
1061 // Round the F80 the right size, which also moves to the appropriate xmm
1062 // register. This is accomplished by storing the F80 value in memory and
1063 // then loading it back. Ewww...
1064 MVT ResVT = RVLocs[0].getValVT();
1065 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1066 unsigned MemSize = ResVT.getSizeInBits()/8;
1067 int FI = MFI.CreateStackObject(MemSize, MemSize);
1068 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
1069 DstRC = ResVT == MVT::f32
1070 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1071 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1072 ResultReg = createResultReg(DstRC);
1073 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
1077 // Mask out all but lowest bit for some call which produces an i1.
1078 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
1079 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
1080 ResultReg = AndResult;
1083 UpdateValueMap(I, ResultReg);
1091 X86FastISel::TargetSelectInstruction(Instruction *I) {
1092 switch (I->getOpcode()) {
1094 case Instruction::Load:
1095 return X86SelectLoad(I);
1096 case Instruction::Store:
1097 return X86SelectStore(I);
1098 case Instruction::ICmp:
1099 case Instruction::FCmp:
1100 return X86SelectCmp(I);
1101 case Instruction::ZExt:
1102 return X86SelectZExt(I);
1103 case Instruction::Br:
1104 return X86SelectBranch(I);
1105 case Instruction::Call:
1106 return X86SelectCall(I);
1107 case Instruction::LShr:
1108 case Instruction::AShr:
1109 case Instruction::Shl:
1110 return X86SelectShift(I);
1111 case Instruction::Select:
1112 return X86SelectSelect(I);
1113 case Instruction::Trunc:
1114 return X86SelectTrunc(I);
1115 case Instruction::FPExt:
1116 return X86SelectFPExt(I);
1117 case Instruction::FPTrunc:
1118 return X86SelectFPTrunc(I);
1124 unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
1126 if (!isTypeLegal(C->getType(), TLI, VT))
1129 // Get opcode and regclass of the output for the given load instruction.
1131 const TargetRegisterClass *RC = NULL;
1132 switch (VT.getSimpleVT()) {
1133 default: return false;
1136 RC = X86::GR8RegisterClass;
1140 RC = X86::GR16RegisterClass;
1144 RC = X86::GR32RegisterClass;
1147 // Must be in x86-64 mode.
1149 RC = X86::GR64RegisterClass;
1152 if (Subtarget->hasSSE1()) {
1154 RC = X86::FR32RegisterClass;
1156 Opc = X86::LD_Fp32m;
1157 RC = X86::RFP32RegisterClass;
1161 if (Subtarget->hasSSE2()) {
1163 RC = X86::FR64RegisterClass;
1165 Opc = X86::LD_Fp64m;
1166 RC = X86::RFP64RegisterClass;
1170 // No f80 support yet.
1174 // Materialize addresses with LEA instructions.
1175 if (isa<GlobalValue>(C)) {
1177 if (X86SelectAddress(C, AM, false)) {
1178 if (TLI.getPointerTy() == MVT::i32)
1182 unsigned ResultReg = createResultReg(RC);
1183 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1189 // MachineConstantPool wants an explicit alignment.
1190 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
1192 // Alignment of vector types. FIXME!
1193 Align = TD.getABITypeSize(C->getType());
1194 Align = Log2_64(Align);
1197 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
1198 unsigned ResultReg = createResultReg(RC);
1199 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset);
1203 unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
1205 if (!X86SelectAddress(C, AM, false))
1207 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1208 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1209 unsigned ResultReg = createResultReg(RC);
1210 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1215 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
1216 MachineModuleInfo *mmi,
1217 DenseMap<const Value *, unsigned> &vm,
1218 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
1219 DenseMap<const AllocaInst *, int> &am) {
1220 return new X86FastISel(mf, mmi, vm, bm, am);