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 getTargetMachine()->getInstrInfo();
111 const X86TargetMachine *getTargetMachine() const {
112 return static_cast<const X86TargetMachine *>(&TM);
115 unsigned TargetMaterializeConstant(Constant *C);
117 unsigned TargetMaterializeAlloca(AllocaInst *C);
119 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
120 /// computed in an SSE register, not on the X87 floating point stack.
121 bool isScalarFPTypeInSSEReg(MVT VT) const {
122 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
123 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
126 bool isTypeLegal(const Type *Ty, const TargetLowering &TLI, MVT &VT,
127 bool AllowI1 = false);
130 bool X86FastISel::isTypeLegal(const Type *Ty, const TargetLowering &TLI,
131 MVT &VT, bool AllowI1) {
132 VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
133 if (VT == MVT::Other || !VT.isSimple())
134 // Unhandled type. Halt "fast" selection and bail.
138 VT = TLI.getPointerTy();
139 // For now, require SSE/SSE2 for performing floating-point operations,
140 // since x87 requires additional work.
141 if (VT == MVT::f64 && !X86ScalarSSEf64)
143 if (VT == MVT::f32 && !X86ScalarSSEf32)
145 // Similarly, no f80 support yet.
148 // We only handle legal types. For example, on x86-32 the instruction
149 // selector contains all of the 64-bit instructions from x86-64,
150 // under the assumption that i64 won't be used if the target doesn't
152 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
155 /// getGlobalBaseReg - Return the the global base register. Output
156 /// instructions required to initialize the global base register, if necessary.
158 unsigned X86FastISel::getGlobalBaseReg() {
159 assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
161 GlobalBaseReg = getInstrInfo()->initializeGlobalBaseReg(MBB->getParent());
162 return GlobalBaseReg;
165 #include "X86GenCallingConv.inc"
167 /// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
169 CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) {
170 if (Subtarget->is64Bit()) {
171 if (Subtarget->isTargetWin64())
172 return CC_X86_Win64_C;
173 else if (CC == CallingConv::Fast && isTaillCall)
174 return CC_X86_64_TailCall;
179 if (CC == CallingConv::X86_FastCall)
180 return CC_X86_32_FastCall;
181 else if (CC == CallingConv::Fast)
182 return CC_X86_32_FastCC;
187 /// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
188 /// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
189 /// Return true and the result register by reference if it is possible.
190 bool X86FastISel::X86FastEmitLoad(MVT VT, const X86AddressMode &AM,
191 unsigned &ResultReg) {
192 // Get opcode and regclass of the output for the given load instruction.
194 const TargetRegisterClass *RC = NULL;
195 switch (VT.getSimpleVT()) {
196 default: return false;
199 RC = X86::GR8RegisterClass;
203 RC = X86::GR16RegisterClass;
207 RC = X86::GR32RegisterClass;
210 // Must be in x86-64 mode.
212 RC = X86::GR64RegisterClass;
215 if (Subtarget->hasSSE1()) {
217 RC = X86::FR32RegisterClass;
220 RC = X86::RFP32RegisterClass;
224 if (Subtarget->hasSSE2()) {
226 RC = X86::FR64RegisterClass;
229 RC = X86::RFP64RegisterClass;
233 // No f80 support yet.
237 ResultReg = createResultReg(RC);
238 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
242 /// X86FastEmitStore - Emit a machine instruction to store a value Val of
243 /// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
244 /// and a displacement offset, or a GlobalAddress,
245 /// i.e. V. Return true if it is possible.
247 X86FastISel::X86FastEmitStore(MVT VT, unsigned Val,
248 const X86AddressMode &AM) {
249 // Get opcode and regclass of the output for the given store instruction.
251 const TargetRegisterClass *RC = NULL;
252 switch (VT.getSimpleVT()) {
253 default: return false;
256 RC = X86::GR8RegisterClass;
260 RC = X86::GR16RegisterClass;
264 RC = X86::GR32RegisterClass;
267 // Must be in x86-64 mode.
269 RC = X86::GR64RegisterClass;
272 if (Subtarget->hasSSE1()) {
274 RC = X86::FR32RegisterClass;
277 RC = X86::RFP32RegisterClass;
281 if (Subtarget->hasSSE2()) {
283 RC = X86::FR64RegisterClass;
286 RC = X86::RFP64RegisterClass;
290 // No f80 support yet.
294 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Val);
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, MVT DstVT,
302 unsigned Src, MVT SrcVT,
303 unsigned &ResultReg) {
304 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
313 /// X86SelectAddress - Attempt to fill in an address from the given value.
315 bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall) {
317 unsigned Opcode = Instruction::UserOp1;
318 if (Instruction *I = dyn_cast<Instruction>(V)) {
319 Opcode = I->getOpcode();
321 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
322 Opcode = C->getOpcode();
328 case Instruction::BitCast:
329 // Look past bitcasts.
330 return X86SelectAddress(U->getOperand(0), AM, isCall);
332 case Instruction::IntToPtr:
333 // Look past no-op inttoptrs.
334 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
335 return X86SelectAddress(U->getOperand(0), AM, isCall);
337 case Instruction::PtrToInt:
338 // Look past no-op ptrtoints.
339 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
340 return X86SelectAddress(U->getOperand(0), AM, isCall);
342 case Instruction::Alloca: {
344 // Do static allocas.
345 const AllocaInst *A = cast<AllocaInst>(V);
346 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
347 if (SI != StaticAllocaMap.end()) {
348 AM.BaseType = X86AddressMode::FrameIndexBase;
349 AM.Base.FrameIndex = SI->second;
355 case Instruction::Add: {
357 // Adds of constants are common and easy enough.
358 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
359 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
360 // They have to fit in the 32-bit signed displacement field though.
362 AM.Disp = (uint32_t)Disp;
363 return X86SelectAddress(U->getOperand(0), AM, isCall);
369 case Instruction::GetElementPtr: {
371 // Pattern-match simple GEPs.
372 uint64_t Disp = (int32_t)AM.Disp;
373 unsigned IndexReg = AM.IndexReg;
374 unsigned Scale = AM.Scale;
375 gep_type_iterator GTI = gep_type_begin(U);
376 // Look at all but the last index. Constants can be folded,
377 // and one dynamic index can be handled, if the scale is supported.
378 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
379 i != e; ++i, ++GTI) {
381 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
382 const StructLayout *SL = TD.getStructLayout(STy);
383 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
384 Disp += SL->getElementOffset(Idx);
386 uint64_t S = TD.getABITypeSize(GTI.getIndexedType());
387 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
388 // Constant-offset addressing.
389 Disp += CI->getSExtValue() * S;
390 } else if (IndexReg == 0 &&
392 !getTargetMachine()->symbolicAddressesAreRIPRel()) &&
393 (S == 1 || S == 2 || S == 4 || S == 8)) {
394 // Scaled-index addressing.
396 IndexReg = getRegForValue(Op);
401 goto unsupported_gep;
404 // Check for displacement overflow.
407 // Ok, the GEP indices were covered by constant-offset and scaled-index
408 // addressing. Update the address state and move on to examining the base.
409 AM.IndexReg = IndexReg;
411 AM.Disp = (uint32_t)Disp;
412 return X86SelectAddress(U->getOperand(0), AM, isCall);
414 // Ok, the GEP indices weren't all covered.
419 // Handle constant address.
420 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
421 // Can't handle alternate code models yet.
422 if (TM.getCodeModel() != CodeModel::Default &&
423 TM.getCodeModel() != CodeModel::Small)
426 // RIP-relative addresses can't have additional register operands.
427 if (getTargetMachine()->symbolicAddressesAreRIPRel() &&
428 (AM.Base.Reg != 0 || AM.IndexReg != 0))
431 // Set up the basic address.
434 TM.getRelocationModel() == Reloc::PIC_ &&
435 !Subtarget->is64Bit())
436 AM.Base.Reg = getGlobalBaseReg();
438 // Emit an extra load if the ABI requires it.
439 if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) {
440 // Check to see if we've already materialized this
441 // value in a register in this block.
442 if (unsigned Reg = LocalValueMap[V]) {
447 // Issue load from stub if necessary.
449 const TargetRegisterClass *RC = NULL;
450 if (TLI.getPointerTy() == MVT::i32) {
452 RC = X86::GR32RegisterClass;
455 RC = X86::GR64RegisterClass;
458 X86AddressMode StubAM;
459 StubAM.Base.Reg = AM.Base.Reg;
461 unsigned ResultReg = createResultReg(RC);
462 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), StubAM);
464 // Now construct the final address. Note that the Disp, Scale,
465 // and Index values may already be set here.
466 AM.Base.Reg = ResultReg;
469 // Prevent loading GV stub multiple times in same MBB.
470 LocalValueMap[V] = AM.Base.Reg;
475 // If all else fails, try to materialize the value in a register.
476 if (!AM.GV || !getTargetMachine()->symbolicAddressesAreRIPRel()) {
477 if (AM.Base.Reg == 0) {
478 AM.Base.Reg = getRegForValue(V);
479 return AM.Base.Reg != 0;
481 if (AM.IndexReg == 0) {
482 assert(AM.Scale == 1 && "Scale with no index!");
483 AM.IndexReg = getRegForValue(V);
484 return AM.IndexReg != 0;
491 /// X86SelectStore - Select and emit code to implement store instructions.
492 bool X86FastISel::X86SelectStore(Instruction* I) {
494 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
496 unsigned Val = getRegForValue(I->getOperand(0));
498 // Unhandled operand. Halt "fast" selection and bail.
502 if (!X86SelectAddress(I->getOperand(1), AM, false))
505 return X86FastEmitStore(VT, Val, AM);
508 /// X86SelectLoad - Select and emit code to implement load instructions.
510 bool X86FastISel::X86SelectLoad(Instruction *I) {
512 if (!isTypeLegal(I->getType(), TLI, VT))
516 if (!X86SelectAddress(I->getOperand(0), AM, false))
519 unsigned ResultReg = 0;
520 if (X86FastEmitLoad(VT, AM, ResultReg)) {
521 UpdateValueMap(I, ResultReg);
527 bool X86FastISel::X86SelectCmp(Instruction *I) {
528 CmpInst *CI = cast<CmpInst>(I);
531 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
534 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
535 if (Op0Reg == 0) return false;
536 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
537 if (Op1Reg == 0) return false;
540 switch (VT.getSimpleVT()) {
541 case MVT::i8: Opc = X86::CMP8rr; break;
542 case MVT::i16: Opc = X86::CMP16rr; break;
543 case MVT::i32: Opc = X86::CMP32rr; break;
544 case MVT::i64: Opc = X86::CMP64rr; break;
545 case MVT::f32: Opc = X86::UCOMISSrr; break;
546 case MVT::f64: Opc = X86::UCOMISDrr; break;
547 default: return false;
550 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
551 switch (CI->getPredicate()) {
552 case CmpInst::FCMP_OEQ: {
553 unsigned EReg = createResultReg(&X86::GR8RegClass);
554 unsigned NPReg = createResultReg(&X86::GR8RegClass);
555 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
556 BuildMI(MBB, TII.get(X86::SETEr), EReg);
557 BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
558 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
561 case CmpInst::FCMP_UNE: {
562 unsigned NEReg = createResultReg(&X86::GR8RegClass);
563 unsigned PReg = createResultReg(&X86::GR8RegClass);
564 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
565 BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
566 BuildMI(MBB, TII.get(X86::SETPr), PReg);
567 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
570 case CmpInst::FCMP_OGT:
571 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
572 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
574 case CmpInst::FCMP_OGE:
575 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
576 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
578 case CmpInst::FCMP_OLT:
579 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
580 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
582 case CmpInst::FCMP_OLE:
583 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
584 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
586 case CmpInst::FCMP_ONE:
587 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
588 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
590 case CmpInst::FCMP_ORD:
591 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
592 BuildMI(MBB, TII.get(X86::SETNPr), ResultReg);
594 case CmpInst::FCMP_UNO:
595 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
596 BuildMI(MBB, TII.get(X86::SETPr), ResultReg);
598 case CmpInst::FCMP_UEQ:
599 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
600 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
602 case CmpInst::FCMP_UGT:
603 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
604 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
606 case CmpInst::FCMP_UGE:
607 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
608 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
610 case CmpInst::FCMP_ULT:
611 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
612 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
614 case CmpInst::FCMP_ULE:
615 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
616 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
618 case CmpInst::ICMP_EQ:
619 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
620 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
622 case CmpInst::ICMP_NE:
623 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
624 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
626 case CmpInst::ICMP_UGT:
627 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
628 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
630 case CmpInst::ICMP_UGE:
631 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
632 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
634 case CmpInst::ICMP_ULT:
635 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
636 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
638 case CmpInst::ICMP_ULE:
639 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
640 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
642 case CmpInst::ICMP_SGT:
643 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
644 BuildMI(MBB, TII.get(X86::SETGr), ResultReg);
646 case CmpInst::ICMP_SGE:
647 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
648 BuildMI(MBB, TII.get(X86::SETGEr), ResultReg);
650 case CmpInst::ICMP_SLT:
651 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
652 BuildMI(MBB, TII.get(X86::SETLr), ResultReg);
654 case CmpInst::ICMP_SLE:
655 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
656 BuildMI(MBB, TII.get(X86::SETLEr), ResultReg);
662 UpdateValueMap(I, ResultReg);
666 bool X86FastISel::X86SelectZExt(Instruction *I) {
667 // Special-case hack: The only i1 values we know how to produce currently
668 // set the upper bits of an i8 value to zero.
669 if (I->getType() == Type::Int8Ty &&
670 I->getOperand(0)->getType() == Type::Int1Ty) {
671 unsigned ResultReg = getRegForValue(I->getOperand(0));
672 if (ResultReg == 0) return false;
673 UpdateValueMap(I, ResultReg);
680 bool X86FastISel::X86SelectBranch(Instruction *I) {
681 BranchInst *BI = cast<BranchInst>(I);
682 // Unconditional branches are selected by tablegen-generated code.
683 unsigned OpReg = getRegForValue(BI->getCondition());
684 if (OpReg == 0) return false;
685 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
686 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
688 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
689 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
690 BuildMI(MBB, TII.get(X86::JMP)).addMBB(FalseMBB);
692 MBB->addSuccessor(TrueMBB);
693 MBB->addSuccessor(FalseMBB);
698 bool X86FastISel::X86SelectShift(Instruction *I) {
699 unsigned CReg = 0, OpReg = 0, OpImm = 0;
700 const TargetRegisterClass *RC = NULL;
701 if (I->getType() == Type::Int8Ty) {
703 RC = &X86::GR8RegClass;
704 switch (I->getOpcode()) {
705 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
706 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
707 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
708 default: return false;
710 } else if (I->getType() == Type::Int16Ty) {
712 RC = &X86::GR16RegClass;
713 switch (I->getOpcode()) {
714 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
715 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
716 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
717 default: return false;
719 } else if (I->getType() == Type::Int32Ty) {
721 RC = &X86::GR32RegClass;
722 switch (I->getOpcode()) {
723 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
724 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
725 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
726 default: return false;
728 } else if (I->getType() == Type::Int64Ty) {
730 RC = &X86::GR64RegClass;
731 switch (I->getOpcode()) {
732 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
733 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
734 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
735 default: return false;
741 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
742 if (VT == MVT::Other || !isTypeLegal(I->getType(), TLI, VT))
745 unsigned Op0Reg = getRegForValue(I->getOperand(0));
746 if (Op0Reg == 0) return false;
748 // Fold immediate in shl(x,3).
749 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
750 unsigned ResultReg = createResultReg(RC);
751 BuildMI(MBB, TII.get(OpImm),
752 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue());
753 UpdateValueMap(I, ResultReg);
757 unsigned Op1Reg = getRegForValue(I->getOperand(1));
758 if (Op1Reg == 0) return false;
759 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
760 unsigned ResultReg = createResultReg(RC);
761 BuildMI(MBB, TII.get(OpReg), ResultReg).addReg(Op0Reg);
762 UpdateValueMap(I, ResultReg);
766 bool X86FastISel::X86SelectSelect(Instruction *I) {
767 const Type *Ty = I->getType();
768 if (isa<PointerType>(Ty))
769 Ty = TD.getIntPtrType();
772 const TargetRegisterClass *RC = NULL;
773 if (Ty == Type::Int16Ty) {
774 Opc = X86::CMOVE16rr;
775 RC = &X86::GR16RegClass;
776 } else if (Ty == Type::Int32Ty) {
777 Opc = X86::CMOVE32rr;
778 RC = &X86::GR32RegClass;
779 } else if (Ty == Type::Int64Ty) {
780 Opc = X86::CMOVE64rr;
781 RC = &X86::GR64RegClass;
786 MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
787 if (VT == MVT::Other || !isTypeLegal(Ty, TLI, VT))
790 unsigned Op0Reg = getRegForValue(I->getOperand(0));
791 if (Op0Reg == 0) return false;
792 unsigned Op1Reg = getRegForValue(I->getOperand(1));
793 if (Op1Reg == 0) return false;
794 unsigned Op2Reg = getRegForValue(I->getOperand(2));
795 if (Op2Reg == 0) return false;
797 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
798 unsigned ResultReg = createResultReg(RC);
799 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
800 UpdateValueMap(I, ResultReg);
804 bool X86FastISel::X86SelectFPExt(Instruction *I) {
805 if (Subtarget->hasSSE2()) {
806 if (I->getType() == Type::DoubleTy) {
807 Value *V = I->getOperand(0);
808 if (V->getType() == Type::FloatTy) {
809 unsigned OpReg = getRegForValue(V);
810 if (OpReg == 0) return false;
811 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
812 BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
813 UpdateValueMap(I, ResultReg);
822 bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
823 if (Subtarget->hasSSE2()) {
824 if (I->getType() == Type::FloatTy) {
825 Value *V = I->getOperand(0);
826 if (V->getType() == Type::DoubleTy) {
827 unsigned OpReg = getRegForValue(V);
828 if (OpReg == 0) return false;
829 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
830 BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
831 UpdateValueMap(I, ResultReg);
840 bool X86FastISel::X86SelectTrunc(Instruction *I) {
841 if (Subtarget->is64Bit())
842 // All other cases should be handled by the tblgen generated code.
844 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
845 MVT DstVT = TLI.getValueType(I->getType());
846 if (DstVT != MVT::i8)
847 // All other cases should be handled by the tblgen generated code.
849 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
850 // All other cases should be handled by the tblgen generated code.
853 unsigned InputReg = getRegForValue(I->getOperand(0));
855 // Unhandled operand. Halt "fast" selection and bail.
858 // First issue a copy to GR16_ or GR32_.
859 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
860 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
861 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
862 unsigned CopyReg = createResultReg(CopyRC);
863 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
865 // Then issue an extract_subreg.
866 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg,1); // x86_subreg_8bit
870 UpdateValueMap(I, ResultReg);
874 bool X86FastISel::X86SelectCall(Instruction *I) {
875 CallInst *CI = cast<CallInst>(I);
876 Value *Callee = I->getOperand(0);
878 // Can't handle inline asm yet.
879 if (isa<InlineAsm>(Callee))
882 // FIXME: Handle some intrinsics.
883 if (Function *F = CI->getCalledFunction()) {
884 if (F->isDeclaration() &&F->getIntrinsicID())
888 // Handle only C and fastcc calling conventions for now.
890 unsigned CC = CS.getCallingConv();
891 if (CC != CallingConv::C &&
892 CC != CallingConv::Fast &&
893 CC != CallingConv::X86_FastCall)
896 // Let SDISel handle vararg functions.
897 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
898 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
902 // Handle *simple* calls for now.
903 const Type *RetTy = CS.getType();
905 if (RetTy == Type::VoidTy)
907 else if (!isTypeLegal(RetTy, TLI, RetVT, true))
910 // Materialize callee address in a register. FIXME: GV address can be
911 // handled with a CALLpcrel32 instead.
912 X86AddressMode CalleeAM;
913 if (!X86SelectAddress(Callee, CalleeAM, true))
915 unsigned CalleeOp = 0;
917 if (CalleeAM.Base.Reg != 0) {
918 assert(CalleeAM.GV == 0);
919 CalleeOp = CalleeAM.Base.Reg;
920 } else if (CalleeAM.GV != 0) {
921 assert(CalleeAM.GV != 0);
926 // Allow calls which produce i1 results.
927 bool AndToI1 = false;
928 if (RetVT == MVT::i1) {
933 // Deal with call operands first.
934 SmallVector<unsigned, 4> Args;
935 SmallVector<MVT, 4> ArgVTs;
936 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
937 Args.reserve(CS.arg_size());
938 ArgVTs.reserve(CS.arg_size());
939 ArgFlags.reserve(CS.arg_size());
940 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
942 unsigned Arg = getRegForValue(*i);
945 ISD::ArgFlagsTy Flags;
946 unsigned AttrInd = i - CS.arg_begin() + 1;
947 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
949 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
952 // FIXME: Only handle *easy* calls for now.
953 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
954 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
955 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
956 CS.paramHasAttr(AttrInd, Attribute::ByVal))
959 const Type *ArgTy = (*i)->getType();
961 if (!isTypeLegal(ArgTy, TLI, ArgVT))
963 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
964 Flags.setOrigAlign(OriginalAlignment);
967 ArgVTs.push_back(ArgVT);
968 ArgFlags.push_back(Flags);
971 // Analyze operands of the call, assigning locations to each operand.
972 SmallVector<CCValAssign, 16> ArgLocs;
973 CCState CCInfo(CC, false, TM, ArgLocs);
974 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
976 // Get a count of how many bytes are to be pushed on the stack.
977 unsigned NumBytes = CCInfo.getNextStackOffset();
979 // Issue CALLSEQ_START
980 BuildMI(MBB, TII.get(X86::ADJCALLSTACKDOWN)).addImm(NumBytes);
982 // Process argumenet: walk the register/memloc assignments, inserting
984 SmallVector<unsigned, 4> RegArgs;
985 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
986 CCValAssign &VA = ArgLocs[i];
987 unsigned Arg = Args[VA.getValNo()];
988 MVT ArgVT = ArgVTs[VA.getValNo()];
990 // Promote the value if needed.
991 switch (VA.getLocInfo()) {
992 default: assert(0 && "Unknown loc info!");
993 case CCValAssign::Full: break;
994 case CCValAssign::SExt: {
995 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
997 assert(Emitted && "Failed to emit a sext!");
998 ArgVT = VA.getLocVT();
1001 case CCValAssign::ZExt: {
1002 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1004 assert(Emitted && "Failed to emit a zext!");
1005 ArgVT = VA.getLocVT();
1008 case CCValAssign::AExt: {
1009 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1012 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1015 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1018 assert(Emitted && "Failed to emit a aext!");
1019 ArgVT = VA.getLocVT();
1024 if (VA.isRegLoc()) {
1025 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
1026 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
1028 assert(Emitted && "Failed to emit a copy instruction!");
1029 RegArgs.push_back(VA.getLocReg());
1031 unsigned LocMemOffset = VA.getLocMemOffset();
1033 AM.Base.Reg = StackPtr;
1034 AM.Disp = LocMemOffset;
1035 X86FastEmitStore(ArgVT, Arg, AM);
1039 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1041 if (!Subtarget->is64Bit() &&
1042 TM.getRelocationModel() == Reloc::PIC_ &&
1043 Subtarget->isPICStyleGOT()) {
1044 TargetRegisterClass *RC = X86::GR32RegisterClass;
1045 unsigned Base = getGlobalBaseReg();
1046 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
1047 assert(Emitted && "Failed to emit a copy instruction!");
1051 unsigned CallOpc = CalleeOp
1052 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
1053 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
1054 MachineInstrBuilder MIB = CalleeOp
1055 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
1056 : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV);
1058 // Add an implicit use GOT pointer in EBX.
1059 if (!Subtarget->is64Bit() &&
1060 TM.getRelocationModel() == Reloc::PIC_ &&
1061 Subtarget->isPICStyleGOT())
1062 MIB.addReg(X86::EBX);
1064 // Add implicit physical register uses to the call.
1065 while (!RegArgs.empty()) {
1066 MIB.addReg(RegArgs.back());
1070 // Issue CALLSEQ_END
1071 BuildMI(MBB, TII.get(X86::ADJCALLSTACKUP)).addImm(NumBytes).addImm(0);
1073 // Now handle call return value (if any).
1074 if (RetVT.getSimpleVT() != MVT::isVoid) {
1075 SmallVector<CCValAssign, 16> RVLocs;
1076 CCState CCInfo(CC, false, TM, RVLocs);
1077 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1079 // Copy all of the result registers out of their specified physreg.
1080 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
1081 MVT CopyVT = RVLocs[0].getValVT();
1082 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1083 TargetRegisterClass *SrcRC = DstRC;
1085 // If this is a call to a function that returns an fp value on the x87 fp
1086 // stack, but where we prefer to use the value in xmm registers, copy it
1087 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1088 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1089 RVLocs[0].getLocReg() == X86::ST1) &&
1090 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
1092 SrcRC = X86::RSTRegisterClass;
1093 DstRC = X86::RFP80RegisterClass;
1096 unsigned ResultReg = createResultReg(DstRC);
1097 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1098 RVLocs[0].getLocReg(), DstRC, SrcRC);
1099 assert(Emitted && "Failed to emit a copy instruction!");
1100 if (CopyVT != RVLocs[0].getValVT()) {
1101 // Round the F80 the right size, which also moves to the appropriate xmm
1102 // register. This is accomplished by storing the F80 value in memory and
1103 // then loading it back. Ewww...
1104 MVT ResVT = RVLocs[0].getValVT();
1105 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1106 unsigned MemSize = ResVT.getSizeInBits()/8;
1107 int FI = MFI.CreateStackObject(MemSize, MemSize);
1108 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
1109 DstRC = ResVT == MVT::f32
1110 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1111 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1112 ResultReg = createResultReg(DstRC);
1113 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
1117 // Mask out all but lowest bit for some call which produces an i1.
1118 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
1119 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
1120 ResultReg = AndResult;
1123 UpdateValueMap(I, ResultReg);
1131 X86FastISel::TargetSelectInstruction(Instruction *I) {
1132 switch (I->getOpcode()) {
1134 case Instruction::Load:
1135 return X86SelectLoad(I);
1136 case Instruction::Store:
1137 return X86SelectStore(I);
1138 case Instruction::ICmp:
1139 case Instruction::FCmp:
1140 return X86SelectCmp(I);
1141 case Instruction::ZExt:
1142 return X86SelectZExt(I);
1143 case Instruction::Br:
1144 return X86SelectBranch(I);
1145 case Instruction::Call:
1146 return X86SelectCall(I);
1147 case Instruction::LShr:
1148 case Instruction::AShr:
1149 case Instruction::Shl:
1150 return X86SelectShift(I);
1151 case Instruction::Select:
1152 return X86SelectSelect(I);
1153 case Instruction::Trunc:
1154 return X86SelectTrunc(I);
1155 case Instruction::FPExt:
1156 return X86SelectFPExt(I);
1157 case Instruction::FPTrunc:
1158 return X86SelectFPTrunc(I);
1164 unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
1166 if (!isTypeLegal(C->getType(), TLI, VT))
1169 // Get opcode and regclass of the output for the given load instruction.
1171 const TargetRegisterClass *RC = NULL;
1172 switch (VT.getSimpleVT()) {
1173 default: return false;
1176 RC = X86::GR8RegisterClass;
1180 RC = X86::GR16RegisterClass;
1184 RC = X86::GR32RegisterClass;
1187 // Must be in x86-64 mode.
1189 RC = X86::GR64RegisterClass;
1192 if (Subtarget->hasSSE1()) {
1194 RC = X86::FR32RegisterClass;
1196 Opc = X86::LD_Fp32m;
1197 RC = X86::RFP32RegisterClass;
1201 if (Subtarget->hasSSE2()) {
1203 RC = X86::FR64RegisterClass;
1205 Opc = X86::LD_Fp64m;
1206 RC = X86::RFP64RegisterClass;
1210 // No f80 support yet.
1214 // Materialize addresses with LEA instructions.
1215 if (isa<GlobalValue>(C)) {
1217 if (X86SelectAddress(C, AM, false)) {
1218 if (TLI.getPointerTy() == MVT::i32)
1222 unsigned ResultReg = createResultReg(RC);
1223 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1229 // MachineConstantPool wants an explicit alignment.
1230 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
1232 // Alignment of vector types. FIXME!
1233 Align = TD.getABITypeSize(C->getType());
1234 Align = Log2_64(Align);
1237 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
1238 unsigned ResultReg = createResultReg(RC);
1239 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset);
1243 unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
1245 if (!X86SelectAddress(C, AM, false))
1247 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1248 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1249 unsigned ResultReg = createResultReg(RC);
1250 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1255 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
1256 MachineModuleInfo *mmi,
1257 DenseMap<const Value *, unsigned> &vm,
1258 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
1259 DenseMap<const AllocaInst *, int> &am) {
1260 return new X86FastISel(mf, mmi, vm, bm, am);