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 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
44 /// floating point ops.
45 /// When SSE is available, use it for f32 operations.
46 /// When SSE2 is available, use it for f64 operations.
51 explicit X86FastISel(MachineFunction &mf,
52 MachineModuleInfo *mmi,
53 DenseMap<const Value *, unsigned> &vm,
54 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
55 DenseMap<const AllocaInst *, int> &am)
56 : FastISel(mf, mmi, vm, bm, am) {
57 Subtarget = &TM.getSubtarget<X86Subtarget>();
58 StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
59 X86ScalarSSEf64 = Subtarget->hasSSE2();
60 X86ScalarSSEf32 = Subtarget->hasSSE1();
63 virtual bool TargetSelectInstruction(Instruction *I);
65 #include "X86GenFastISel.inc"
68 bool X86FastEmitLoad(MVT VT, const X86AddressMode &AM, unsigned &RR);
70 bool X86FastEmitStore(MVT VT, unsigned Val,
71 const X86AddressMode &AM);
73 bool X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT, unsigned Src, MVT SrcVT,
76 bool X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall);
78 bool X86SelectLoad(Instruction *I);
80 bool X86SelectStore(Instruction *I);
82 bool X86SelectCmp(Instruction *I);
84 bool X86SelectZExt(Instruction *I);
86 bool X86SelectBranch(Instruction *I);
88 bool X86SelectShift(Instruction *I);
90 bool X86SelectSelect(Instruction *I);
92 bool X86SelectTrunc(Instruction *I);
94 unsigned X86ChooseCmpOpcode(MVT VT);
96 bool X86SelectFPExt(Instruction *I);
97 bool X86SelectFPTrunc(Instruction *I);
99 bool X86SelectCall(Instruction *I);
101 CCAssignFn *CCAssignFnForCall(unsigned CC, bool isTailCall = false);
103 const X86InstrInfo *getInstrInfo() const {
104 return getTargetMachine()->getInstrInfo();
106 const X86TargetMachine *getTargetMachine() const {
107 return static_cast<const X86TargetMachine *>(&TM);
110 unsigned TargetMaterializeConstant(Constant *C);
112 unsigned TargetMaterializeAlloca(AllocaInst *C);
114 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
115 /// computed in an SSE register, not on the X87 floating point stack.
116 bool isScalarFPTypeInSSEReg(MVT VT) const {
117 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
118 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
121 bool isTypeLegal(const Type *Ty, const TargetLowering &TLI, MVT &VT,
122 bool AllowI1 = false);
125 bool X86FastISel::isTypeLegal(const Type *Ty, const TargetLowering &TLI,
126 MVT &VT, bool AllowI1) {
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 // For now, require SSE/SSE2 for performing floating-point operations,
135 // since x87 requires additional work.
136 if (VT == MVT::f64 && !X86ScalarSSEf64)
138 if (VT == MVT::f32 && !X86ScalarSSEf32)
140 // Similarly, no f80 support yet.
143 // We only handle legal types. For example, on x86-32 the instruction
144 // selector contains all of the 64-bit instructions from x86-64,
145 // under the assumption that i64 won't be used if the target doesn't
147 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
150 #include "X86GenCallingConv.inc"
152 /// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
154 CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) {
155 if (Subtarget->is64Bit()) {
156 if (Subtarget->isTargetWin64())
157 return CC_X86_Win64_C;
158 else if (CC == CallingConv::Fast && isTaillCall)
159 return CC_X86_64_TailCall;
164 if (CC == CallingConv::X86_FastCall)
165 return CC_X86_32_FastCall;
166 else if (CC == CallingConv::Fast)
167 return CC_X86_32_FastCC;
172 /// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
173 /// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
174 /// Return true and the result register by reference if it is possible.
175 bool X86FastISel::X86FastEmitLoad(MVT VT, const X86AddressMode &AM,
176 unsigned &ResultReg) {
177 // Get opcode and regclass of the output for the given load instruction.
179 const TargetRegisterClass *RC = NULL;
180 switch (VT.getSimpleVT()) {
181 default: return false;
184 RC = X86::GR8RegisterClass;
188 RC = X86::GR16RegisterClass;
192 RC = X86::GR32RegisterClass;
195 // Must be in x86-64 mode.
197 RC = X86::GR64RegisterClass;
200 if (Subtarget->hasSSE1()) {
202 RC = X86::FR32RegisterClass;
205 RC = X86::RFP32RegisterClass;
209 if (Subtarget->hasSSE2()) {
211 RC = X86::FR64RegisterClass;
214 RC = X86::RFP64RegisterClass;
218 // No f80 support yet.
222 ResultReg = createResultReg(RC);
223 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
227 /// X86FastEmitStore - Emit a machine instruction to store a value Val of
228 /// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
229 /// and a displacement offset, or a GlobalAddress,
230 /// i.e. V. Return true if it is possible.
232 X86FastISel::X86FastEmitStore(MVT VT, unsigned Val,
233 const X86AddressMode &AM) {
234 // Get opcode and regclass of the output for the given store instruction.
236 const TargetRegisterClass *RC = NULL;
237 switch (VT.getSimpleVT()) {
238 default: return false;
241 RC = X86::GR8RegisterClass;
245 RC = X86::GR16RegisterClass;
249 RC = X86::GR32RegisterClass;
252 // Must be in x86-64 mode.
254 RC = X86::GR64RegisterClass;
257 if (Subtarget->hasSSE1()) {
259 RC = X86::FR32RegisterClass;
262 RC = X86::RFP32RegisterClass;
266 if (Subtarget->hasSSE2()) {
268 RC = X86::FR64RegisterClass;
271 RC = X86::RFP64RegisterClass;
275 // No f80 support yet.
279 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Val);
283 /// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
284 /// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
285 /// ISD::SIGN_EXTEND).
286 bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT,
287 unsigned Src, MVT SrcVT,
288 unsigned &ResultReg) {
289 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
298 /// X86SelectAddress - Attempt to fill in an address from the given value.
300 bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall) {
302 unsigned Opcode = Instruction::UserOp1;
303 if (Instruction *I = dyn_cast<Instruction>(V)) {
304 Opcode = I->getOpcode();
306 } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
307 Opcode = C->getOpcode();
313 case Instruction::BitCast:
314 // Look past bitcasts.
315 return X86SelectAddress(U->getOperand(0), AM, isCall);
317 case Instruction::IntToPtr:
318 // Look past no-op inttoptrs.
319 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
320 return X86SelectAddress(U->getOperand(0), AM, isCall);
322 case Instruction::PtrToInt:
323 // Look past no-op ptrtoints.
324 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
325 return X86SelectAddress(U->getOperand(0), AM, isCall);
327 case Instruction::Alloca: {
329 // Do static allocas.
330 const AllocaInst *A = cast<AllocaInst>(V);
331 DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
332 if (SI != StaticAllocaMap.end()) {
333 AM.BaseType = X86AddressMode::FrameIndexBase;
334 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 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
345 // They have to fit in the 32-bit signed displacement field though.
347 AM.Disp = (uint32_t)Disp;
348 return X86SelectAddress(U->getOperand(0), AM, isCall);
354 case Instruction::GetElementPtr: {
356 // Pattern-match simple GEPs.
357 uint64_t Disp = (int32_t)AM.Disp;
358 unsigned IndexReg = AM.IndexReg;
359 unsigned Scale = AM.Scale;
360 gep_type_iterator GTI = gep_type_begin(U);
361 // Look at all but the last index. Constants can be folded,
362 // and one dynamic index can be handled, if the scale is supported.
363 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
364 i != e; ++i, ++GTI) {
366 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
367 const StructLayout *SL = TD.getStructLayout(STy);
368 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
369 Disp += SL->getElementOffset(Idx);
371 uint64_t S = TD.getABITypeSize(GTI.getIndexedType());
372 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
373 // Constant-offset addressing.
374 Disp += CI->getSExtValue() * S;
375 } else if (IndexReg == 0 &&
377 !getTargetMachine()->symbolicAddressesAreRIPRel()) &&
378 (S == 1 || S == 2 || S == 4 || S == 8)) {
379 // Scaled-index addressing.
381 IndexReg = getRegForValue(Op);
386 goto unsupported_gep;
389 // Check for displacement overflow.
392 // Ok, the GEP indices were covered by constant-offset and scaled-index
393 // addressing. Update the address state and move on to examining the base.
394 AM.IndexReg = IndexReg;
396 AM.Disp = (uint32_t)Disp;
397 return X86SelectAddress(U->getOperand(0), AM, isCall);
399 // Ok, the GEP indices weren't all covered.
404 // Handle constant address.
405 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
406 // Can't handle alternate code models yet.
407 if (TM.getCodeModel() != CodeModel::Default &&
408 TM.getCodeModel() != CodeModel::Small)
411 // RIP-relative addresses can't have additional register operands.
412 if (getTargetMachine()->symbolicAddressesAreRIPRel() &&
413 (AM.Base.Reg != 0 || AM.IndexReg != 0))
416 // Set up the basic address.
419 TM.getRelocationModel() == Reloc::PIC_ &&
420 !Subtarget->is64Bit())
421 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(&MF);
423 // Emit an extra load if the ABI requires it.
424 if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) {
425 // Check to see if we've already materialized this
426 // value in a register in this block.
427 if (unsigned Reg = LocalValueMap[V]) {
432 // Issue load from stub if necessary.
434 const TargetRegisterClass *RC = NULL;
435 if (TLI.getPointerTy() == MVT::i32) {
437 RC = X86::GR32RegisterClass;
440 RC = X86::GR64RegisterClass;
443 X86AddressMode StubAM;
444 StubAM.Base.Reg = AM.Base.Reg;
446 unsigned ResultReg = createResultReg(RC);
447 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), StubAM);
449 // Now construct the final address. Note that the Disp, Scale,
450 // and Index values may already be set here.
451 AM.Base.Reg = ResultReg;
454 // Prevent loading GV stub multiple times in same MBB.
455 LocalValueMap[V] = AM.Base.Reg;
460 // If all else fails, try to materialize the value in a register.
461 if (!AM.GV || !getTargetMachine()->symbolicAddressesAreRIPRel()) {
462 if (AM.Base.Reg == 0) {
463 AM.Base.Reg = getRegForValue(V);
464 return AM.Base.Reg != 0;
466 if (AM.IndexReg == 0) {
467 assert(AM.Scale == 1 && "Scale with no index!");
468 AM.IndexReg = getRegForValue(V);
469 return AM.IndexReg != 0;
476 /// X86SelectStore - Select and emit code to implement store instructions.
477 bool X86FastISel::X86SelectStore(Instruction* I) {
479 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
481 unsigned Val = getRegForValue(I->getOperand(0));
483 // Unhandled operand. Halt "fast" selection and bail.
487 if (!X86SelectAddress(I->getOperand(1), AM, false))
490 return X86FastEmitStore(VT, Val, AM);
493 /// X86SelectLoad - Select and emit code to implement load instructions.
495 bool X86FastISel::X86SelectLoad(Instruction *I) {
497 if (!isTypeLegal(I->getType(), TLI, VT))
501 if (!X86SelectAddress(I->getOperand(0), AM, false))
504 unsigned ResultReg = 0;
505 if (X86FastEmitLoad(VT, AM, ResultReg)) {
506 UpdateValueMap(I, ResultReg);
512 unsigned X86FastISel::X86ChooseCmpOpcode(MVT VT) {
513 switch (VT.getSimpleVT()) {
514 case MVT::i8: return X86::CMP8rr;
515 case MVT::i16: return X86::CMP16rr;
516 case MVT::i32: return X86::CMP32rr;
517 case MVT::i64: return X86::CMP64rr;
518 case MVT::f32: return X86::UCOMISSrr;
519 case MVT::f64: return X86::UCOMISDrr;
525 bool X86FastISel::X86SelectCmp(Instruction *I) {
526 CmpInst *CI = cast<CmpInst>(I);
529 if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
532 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
533 if (Op0Reg == 0) return false;
534 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
535 if (Op1Reg == 0) return false;
537 unsigned Opc = X86ChooseCmpOpcode(VT);
539 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
540 switch (CI->getPredicate()) {
541 case CmpInst::FCMP_OEQ: {
542 unsigned EReg = createResultReg(&X86::GR8RegClass);
543 unsigned NPReg = createResultReg(&X86::GR8RegClass);
544 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
545 BuildMI(MBB, TII.get(X86::SETEr), EReg);
546 BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
547 BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
550 case CmpInst::FCMP_UNE: {
551 unsigned NEReg = createResultReg(&X86::GR8RegClass);
552 unsigned PReg = createResultReg(&X86::GR8RegClass);
553 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
554 BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
555 BuildMI(MBB, TII.get(X86::SETPr), PReg);
556 BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
559 case CmpInst::FCMP_OGT:
560 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
561 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
563 case CmpInst::FCMP_OGE:
564 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
565 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
567 case CmpInst::FCMP_OLT:
568 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
569 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
571 case CmpInst::FCMP_OLE:
572 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
573 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
575 case CmpInst::FCMP_ONE:
576 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
577 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
579 case CmpInst::FCMP_ORD:
580 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
581 BuildMI(MBB, TII.get(X86::SETNPr), ResultReg);
583 case CmpInst::FCMP_UNO:
584 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
585 BuildMI(MBB, TII.get(X86::SETPr), ResultReg);
587 case CmpInst::FCMP_UEQ:
588 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
589 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
591 case CmpInst::FCMP_UGT:
592 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
593 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
595 case CmpInst::FCMP_UGE:
596 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
597 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
599 case CmpInst::FCMP_ULT:
600 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
601 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
603 case CmpInst::FCMP_ULE:
604 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
605 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
607 case CmpInst::ICMP_EQ:
608 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
609 BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
611 case CmpInst::ICMP_NE:
612 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
613 BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
615 case CmpInst::ICMP_UGT:
616 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
617 BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
619 case CmpInst::ICMP_UGE:
620 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
621 BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
623 case CmpInst::ICMP_ULT:
624 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
625 BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
627 case CmpInst::ICMP_ULE:
628 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
629 BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
631 case CmpInst::ICMP_SGT:
632 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
633 BuildMI(MBB, TII.get(X86::SETGr), ResultReg);
635 case CmpInst::ICMP_SGE:
636 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
637 BuildMI(MBB, TII.get(X86::SETGEr), ResultReg);
639 case CmpInst::ICMP_SLT:
640 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
641 BuildMI(MBB, TII.get(X86::SETLr), ResultReg);
643 case CmpInst::ICMP_SLE:
644 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
645 BuildMI(MBB, TII.get(X86::SETLEr), ResultReg);
651 UpdateValueMap(I, ResultReg);
655 bool X86FastISel::X86SelectZExt(Instruction *I) {
656 // Special-case hack: The only i1 values we know how to produce currently
657 // set the upper bits of an i8 value to zero.
658 if (I->getType() == Type::Int8Ty &&
659 I->getOperand(0)->getType() == Type::Int1Ty) {
660 unsigned ResultReg = getRegForValue(I->getOperand(0));
661 if (ResultReg == 0) return false;
662 UpdateValueMap(I, ResultReg);
669 bool X86FastISel::X86SelectBranch(Instruction *I) {
670 // Unconditional branches are selected by tablegen-generated code.
671 // Handle a conditional branch.
672 BranchInst *BI = cast<BranchInst>(I);
673 MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
674 MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
676 // Fold the common case of a conditional branch with a comparison.
677 if (CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
678 if (CI->hasOneUse()) {
679 MVT VT = TLI.getValueType(CI->getOperand(0)->getType());
680 unsigned Opc = X86ChooseCmpOpcode(VT);
681 if (Opc == 0) return false;
683 // Try to take advantage of fallthrough opportunities.
684 CmpInst::Predicate Predicate = CI->getPredicate();
685 if (MBB->isLayoutSuccessor(TrueMBB)) {
686 std::swap(TrueMBB, FalseMBB);
687 Predicate = CmpInst::getInversePredicate(Predicate);
690 unsigned Op0Reg = getRegForValue(CI->getOperand(0));
691 if (Op0Reg == 0) return false;
692 unsigned Op1Reg = getRegForValue(CI->getOperand(1));
693 if (Op1Reg == 0) return false;
696 case CmpInst::FCMP_OGT:
697 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
698 BuildMI(MBB, TII.get(X86::JA)).addMBB(TrueMBB);
700 case CmpInst::FCMP_OGE:
701 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
702 BuildMI(MBB, TII.get(X86::JAE)).addMBB(TrueMBB);
704 case CmpInst::FCMP_OLT:
705 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
706 BuildMI(MBB, TII.get(X86::JA)).addMBB(TrueMBB);
708 case CmpInst::FCMP_OLE:
709 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
710 BuildMI(MBB, TII.get(X86::JAE)).addMBB(TrueMBB);
712 case CmpInst::FCMP_ONE:
713 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
714 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
716 case CmpInst::FCMP_ORD:
717 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
718 BuildMI(MBB, TII.get(X86::JNP)).addMBB(TrueMBB);
720 case CmpInst::FCMP_UNO:
721 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
722 BuildMI(MBB, TII.get(X86::JP)).addMBB(TrueMBB);
724 case CmpInst::FCMP_UEQ:
725 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
726 BuildMI(MBB, TII.get(X86::JE)).addMBB(TrueMBB);
728 case CmpInst::FCMP_UGT:
729 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
730 BuildMI(MBB, TII.get(X86::JB)).addMBB(TrueMBB);
732 case CmpInst::FCMP_UGE:
733 BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
734 BuildMI(MBB, TII.get(X86::JBE)).addMBB(TrueMBB);
736 case CmpInst::FCMP_ULT:
737 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
738 BuildMI(MBB, TII.get(X86::JB)).addMBB(TrueMBB);
740 case CmpInst::FCMP_ULE:
741 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
742 BuildMI(MBB, TII.get(X86::JBE)).addMBB(TrueMBB);
744 case CmpInst::ICMP_EQ:
745 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
746 BuildMI(MBB, TII.get(X86::JE)).addMBB(TrueMBB);
748 case CmpInst::ICMP_NE:
749 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
750 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
752 case CmpInst::ICMP_UGT:
753 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
754 BuildMI(MBB, TII.get(X86::JA)).addMBB(TrueMBB);
756 case CmpInst::ICMP_UGE:
757 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
758 BuildMI(MBB, TII.get(X86::JAE)).addMBB(TrueMBB);
760 case CmpInst::ICMP_ULT:
761 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
762 BuildMI(MBB, TII.get(X86::JB)).addMBB(TrueMBB);
764 case CmpInst::ICMP_ULE:
765 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
766 BuildMI(MBB, TII.get(X86::JBE)).addMBB(TrueMBB);
768 case CmpInst::ICMP_SGT:
769 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
770 BuildMI(MBB, TII.get(X86::JG)).addMBB(TrueMBB);
772 case CmpInst::ICMP_SGE:
773 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
774 BuildMI(MBB, TII.get(X86::JGE)).addMBB(TrueMBB);
776 case CmpInst::ICMP_SLT:
777 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
778 BuildMI(MBB, TII.get(X86::JL)).addMBB(TrueMBB);
780 case CmpInst::ICMP_SLE:
781 BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
782 BuildMI(MBB, TII.get(X86::JLE)).addMBB(TrueMBB);
787 MBB->addSuccessor(TrueMBB);
788 FastEmitBranch(FalseMBB);
793 // Otherwise do a clumsy setcc and re-test it.
794 unsigned OpReg = getRegForValue(BI->getCondition());
795 if (OpReg == 0) return false;
797 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
799 BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
800 MBB->addSuccessor(TrueMBB);
802 FastEmitBranch(FalseMBB);
807 bool X86FastISel::X86SelectShift(Instruction *I) {
808 unsigned CReg = 0, OpReg = 0, OpImm = 0;
809 const TargetRegisterClass *RC = NULL;
810 if (I->getType() == Type::Int8Ty) {
812 RC = &X86::GR8RegClass;
813 switch (I->getOpcode()) {
814 case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
815 case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
816 case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
817 default: return false;
819 } else if (I->getType() == Type::Int16Ty) {
821 RC = &X86::GR16RegClass;
822 switch (I->getOpcode()) {
823 case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
824 case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
825 case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
826 default: return false;
828 } else if (I->getType() == Type::Int32Ty) {
830 RC = &X86::GR32RegClass;
831 switch (I->getOpcode()) {
832 case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
833 case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
834 case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
835 default: return false;
837 } else if (I->getType() == Type::Int64Ty) {
839 RC = &X86::GR64RegClass;
840 switch (I->getOpcode()) {
841 case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
842 case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
843 case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
844 default: return false;
850 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
851 if (VT == MVT::Other || !isTypeLegal(I->getType(), TLI, VT))
854 unsigned Op0Reg = getRegForValue(I->getOperand(0));
855 if (Op0Reg == 0) return false;
857 // Fold immediate in shl(x,3).
858 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
859 unsigned ResultReg = createResultReg(RC);
860 BuildMI(MBB, TII.get(OpImm),
861 ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue());
862 UpdateValueMap(I, ResultReg);
866 unsigned Op1Reg = getRegForValue(I->getOperand(1));
867 if (Op1Reg == 0) return false;
868 TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
869 unsigned ResultReg = createResultReg(RC);
870 BuildMI(MBB, TII.get(OpReg), ResultReg).addReg(Op0Reg)
871 // FIXME: The "Local" register allocator's physreg liveness doesn't
872 // recognize subregs. Adding the superreg of CL that's actually defined
873 // prevents it from being re-allocated for this instruction.
874 .addReg(CReg, false, true);
875 UpdateValueMap(I, ResultReg);
879 bool X86FastISel::X86SelectSelect(Instruction *I) {
880 const Type *Ty = I->getType();
881 if (isa<PointerType>(Ty))
882 Ty = TD.getIntPtrType();
885 const TargetRegisterClass *RC = NULL;
886 if (Ty == Type::Int16Ty) {
887 Opc = X86::CMOVE16rr;
888 RC = &X86::GR16RegClass;
889 } else if (Ty == Type::Int32Ty) {
890 Opc = X86::CMOVE32rr;
891 RC = &X86::GR32RegClass;
892 } else if (Ty == Type::Int64Ty) {
893 Opc = X86::CMOVE64rr;
894 RC = &X86::GR64RegClass;
899 MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
900 if (VT == MVT::Other || !isTypeLegal(Ty, TLI, VT))
903 unsigned Op0Reg = getRegForValue(I->getOperand(0));
904 if (Op0Reg == 0) return false;
905 unsigned Op1Reg = getRegForValue(I->getOperand(1));
906 if (Op1Reg == 0) return false;
907 unsigned Op2Reg = getRegForValue(I->getOperand(2));
908 if (Op2Reg == 0) return false;
910 BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
911 unsigned ResultReg = createResultReg(RC);
912 BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
913 UpdateValueMap(I, ResultReg);
917 bool X86FastISel::X86SelectFPExt(Instruction *I) {
918 if (Subtarget->hasSSE2()) {
919 if (I->getType() == Type::DoubleTy) {
920 Value *V = I->getOperand(0);
921 if (V->getType() == Type::FloatTy) {
922 unsigned OpReg = getRegForValue(V);
923 if (OpReg == 0) return false;
924 unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
925 BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
926 UpdateValueMap(I, ResultReg);
935 bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
936 if (Subtarget->hasSSE2()) {
937 if (I->getType() == Type::FloatTy) {
938 Value *V = I->getOperand(0);
939 if (V->getType() == Type::DoubleTy) {
940 unsigned OpReg = getRegForValue(V);
941 if (OpReg == 0) return false;
942 unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
943 BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
944 UpdateValueMap(I, ResultReg);
953 bool X86FastISel::X86SelectTrunc(Instruction *I) {
954 if (Subtarget->is64Bit())
955 // All other cases should be handled by the tblgen generated code.
957 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
958 MVT DstVT = TLI.getValueType(I->getType());
959 if (DstVT != MVT::i8)
960 // All other cases should be handled by the tblgen generated code.
962 if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
963 // All other cases should be handled by the tblgen generated code.
966 unsigned InputReg = getRegForValue(I->getOperand(0));
968 // Unhandled operand. Halt "fast" selection and bail.
971 // First issue a copy to GR16_ or GR32_.
972 unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
973 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
974 ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
975 unsigned CopyReg = createResultReg(CopyRC);
976 BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
978 // Then issue an extract_subreg.
979 unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg,1); // x86_subreg_8bit
983 UpdateValueMap(I, ResultReg);
987 bool X86FastISel::X86SelectCall(Instruction *I) {
988 CallInst *CI = cast<CallInst>(I);
989 Value *Callee = I->getOperand(0);
991 // Can't handle inline asm yet.
992 if (isa<InlineAsm>(Callee))
995 // FIXME: Handle some intrinsics.
996 if (Function *F = CI->getCalledFunction()) {
997 if (F->isDeclaration() &&F->getIntrinsicID())
1001 // Handle only C and fastcc calling conventions for now.
1003 unsigned CC = CS.getCallingConv();
1004 if (CC != CallingConv::C &&
1005 CC != CallingConv::Fast &&
1006 CC != CallingConv::X86_FastCall)
1009 // Let SDISel handle vararg functions.
1010 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1011 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1012 if (FTy->isVarArg())
1015 // Handle *simple* calls for now.
1016 const Type *RetTy = CS.getType();
1018 if (RetTy == Type::VoidTy)
1019 RetVT = MVT::isVoid;
1020 else if (!isTypeLegal(RetTy, TLI, RetVT, true))
1023 // Materialize callee address in a register. FIXME: GV address can be
1024 // handled with a CALLpcrel32 instead.
1025 X86AddressMode CalleeAM;
1026 if (!X86SelectAddress(Callee, CalleeAM, true))
1028 unsigned CalleeOp = 0;
1029 GlobalValue *GV = 0;
1030 if (CalleeAM.Base.Reg != 0) {
1031 assert(CalleeAM.GV == 0);
1032 CalleeOp = CalleeAM.Base.Reg;
1033 } else if (CalleeAM.GV != 0) {
1034 assert(CalleeAM.GV != 0);
1039 // Allow calls which produce i1 results.
1040 bool AndToI1 = false;
1041 if (RetVT == MVT::i1) {
1046 // Deal with call operands first.
1047 SmallVector<unsigned, 4> Args;
1048 SmallVector<MVT, 4> ArgVTs;
1049 SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
1050 Args.reserve(CS.arg_size());
1051 ArgVTs.reserve(CS.arg_size());
1052 ArgFlags.reserve(CS.arg_size());
1053 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1055 unsigned Arg = getRegForValue(*i);
1058 ISD::ArgFlagsTy Flags;
1059 unsigned AttrInd = i - CS.arg_begin() + 1;
1060 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1062 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1065 // FIXME: Only handle *easy* calls for now.
1066 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1067 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1068 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1069 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1072 const Type *ArgTy = (*i)->getType();
1074 if (!isTypeLegal(ArgTy, TLI, ArgVT))
1076 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1077 Flags.setOrigAlign(OriginalAlignment);
1079 Args.push_back(Arg);
1080 ArgVTs.push_back(ArgVT);
1081 ArgFlags.push_back(Flags);
1084 // Analyze operands of the call, assigning locations to each operand.
1085 SmallVector<CCValAssign, 16> ArgLocs;
1086 CCState CCInfo(CC, false, TM, ArgLocs);
1087 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1089 // Get a count of how many bytes are to be pushed on the stack.
1090 unsigned NumBytes = CCInfo.getNextStackOffset();
1092 // Issue CALLSEQ_START
1093 unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
1094 BuildMI(MBB, TII.get(AdjStackDown)).addImm(NumBytes);
1096 // Process argumenet: walk the register/memloc assignments, inserting
1098 SmallVector<unsigned, 4> RegArgs;
1099 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1100 CCValAssign &VA = ArgLocs[i];
1101 unsigned Arg = Args[VA.getValNo()];
1102 MVT ArgVT = ArgVTs[VA.getValNo()];
1104 // Promote the value if needed.
1105 switch (VA.getLocInfo()) {
1106 default: assert(0 && "Unknown loc info!");
1107 case CCValAssign::Full: break;
1108 case CCValAssign::SExt: {
1109 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1111 assert(Emitted && "Failed to emit a sext!");
1112 ArgVT = VA.getLocVT();
1115 case CCValAssign::ZExt: {
1116 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1118 assert(Emitted && "Failed to emit a zext!");
1119 ArgVT = VA.getLocVT();
1122 case CCValAssign::AExt: {
1123 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1126 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1129 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1132 assert(Emitted && "Failed to emit a aext!");
1133 ArgVT = VA.getLocVT();
1138 if (VA.isRegLoc()) {
1139 TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
1140 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
1142 assert(Emitted && "Failed to emit a copy instruction!");
1143 RegArgs.push_back(VA.getLocReg());
1145 unsigned LocMemOffset = VA.getLocMemOffset();
1147 AM.Base.Reg = StackPtr;
1148 AM.Disp = LocMemOffset;
1149 X86FastEmitStore(ArgVT, Arg, AM);
1153 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1155 if (!Subtarget->is64Bit() &&
1156 TM.getRelocationModel() == Reloc::PIC_ &&
1157 Subtarget->isPICStyleGOT()) {
1158 TargetRegisterClass *RC = X86::GR32RegisterClass;
1159 unsigned Base = getInstrInfo()->getGlobalBaseReg(&MF);
1160 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
1161 assert(Emitted && "Failed to emit a copy instruction!");
1165 unsigned CallOpc = CalleeOp
1166 ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r)
1167 : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
1168 MachineInstrBuilder MIB = CalleeOp
1169 ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
1170 : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV);
1172 // Add an implicit use GOT pointer in EBX.
1173 if (!Subtarget->is64Bit() &&
1174 TM.getRelocationModel() == Reloc::PIC_ &&
1175 Subtarget->isPICStyleGOT())
1176 MIB.addReg(X86::EBX);
1178 // Add implicit physical register uses to the call.
1179 while (!RegArgs.empty()) {
1180 MIB.addReg(RegArgs.back());
1184 // Issue CALLSEQ_END
1185 unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
1186 BuildMI(MBB, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0);
1188 // Now handle call return value (if any).
1189 if (RetVT.getSimpleVT() != MVT::isVoid) {
1190 SmallVector<CCValAssign, 16> RVLocs;
1191 CCState CCInfo(CC, false, TM, RVLocs);
1192 CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1194 // Copy all of the result registers out of their specified physreg.
1195 assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
1196 MVT CopyVT = RVLocs[0].getValVT();
1197 TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1198 TargetRegisterClass *SrcRC = DstRC;
1200 // If this is a call to a function that returns an fp value on the x87 fp
1201 // stack, but where we prefer to use the value in xmm registers, copy it
1202 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1203 if ((RVLocs[0].getLocReg() == X86::ST0 ||
1204 RVLocs[0].getLocReg() == X86::ST1) &&
1205 isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
1207 SrcRC = X86::RSTRegisterClass;
1208 DstRC = X86::RFP80RegisterClass;
1211 unsigned ResultReg = createResultReg(DstRC);
1212 bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1213 RVLocs[0].getLocReg(), DstRC, SrcRC);
1214 assert(Emitted && "Failed to emit a copy instruction!");
1215 if (CopyVT != RVLocs[0].getValVT()) {
1216 // Round the F80 the right size, which also moves to the appropriate xmm
1217 // register. This is accomplished by storing the F80 value in memory and
1218 // then loading it back. Ewww...
1219 MVT ResVT = RVLocs[0].getValVT();
1220 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1221 unsigned MemSize = ResVT.getSizeInBits()/8;
1222 int FI = MFI.CreateStackObject(MemSize, MemSize);
1223 addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
1224 DstRC = ResVT == MVT::f32
1225 ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1226 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1227 ResultReg = createResultReg(DstRC);
1228 addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
1232 // Mask out all but lowest bit for some call which produces an i1.
1233 unsigned AndResult = createResultReg(X86::GR8RegisterClass);
1234 BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
1235 ResultReg = AndResult;
1238 UpdateValueMap(I, ResultReg);
1246 X86FastISel::TargetSelectInstruction(Instruction *I) {
1247 switch (I->getOpcode()) {
1249 case Instruction::Load:
1250 return X86SelectLoad(I);
1251 case Instruction::Store:
1252 return X86SelectStore(I);
1253 case Instruction::ICmp:
1254 case Instruction::FCmp:
1255 return X86SelectCmp(I);
1256 case Instruction::ZExt:
1257 return X86SelectZExt(I);
1258 case Instruction::Br:
1259 return X86SelectBranch(I);
1260 case Instruction::Call:
1261 return X86SelectCall(I);
1262 case Instruction::LShr:
1263 case Instruction::AShr:
1264 case Instruction::Shl:
1265 return X86SelectShift(I);
1266 case Instruction::Select:
1267 return X86SelectSelect(I);
1268 case Instruction::Trunc:
1269 return X86SelectTrunc(I);
1270 case Instruction::FPExt:
1271 return X86SelectFPExt(I);
1272 case Instruction::FPTrunc:
1273 return X86SelectFPTrunc(I);
1279 unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
1281 if (!isTypeLegal(C->getType(), TLI, VT))
1284 // Get opcode and regclass of the output for the given load instruction.
1286 const TargetRegisterClass *RC = NULL;
1287 switch (VT.getSimpleVT()) {
1288 default: return false;
1291 RC = X86::GR8RegisterClass;
1295 RC = X86::GR16RegisterClass;
1299 RC = X86::GR32RegisterClass;
1302 // Must be in x86-64 mode.
1304 RC = X86::GR64RegisterClass;
1307 if (Subtarget->hasSSE1()) {
1309 RC = X86::FR32RegisterClass;
1311 Opc = X86::LD_Fp32m;
1312 RC = X86::RFP32RegisterClass;
1316 if (Subtarget->hasSSE2()) {
1318 RC = X86::FR64RegisterClass;
1320 Opc = X86::LD_Fp64m;
1321 RC = X86::RFP64RegisterClass;
1325 // No f80 support yet.
1329 // Materialize addresses with LEA instructions.
1330 if (isa<GlobalValue>(C)) {
1332 if (X86SelectAddress(C, AM, false)) {
1333 if (TLI.getPointerTy() == MVT::i32)
1337 unsigned ResultReg = createResultReg(RC);
1338 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1344 // MachineConstantPool wants an explicit alignment.
1345 unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
1347 // Alignment of vector types. FIXME!
1348 Align = TD.getABITypeSize(C->getType());
1349 Align = Log2_64(Align);
1352 // x86-32 PIC requires a PIC base register for constant pools.
1353 unsigned PICBase = 0;
1354 if (TM.getRelocationModel() == Reloc::PIC_ &&
1355 !Subtarget->is64Bit())
1356 PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
1358 // Create the load from the constant pool.
1359 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
1360 unsigned ResultReg = createResultReg(RC);
1361 addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset,
1367 unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
1368 // Fail on dynamic allocas. At this point, getRegForValue has already
1369 // checked its CSE maps, so if we're here trying to handle a dynamic
1370 // alloca, we're not going to succeed. X86SelectAddress has a
1371 // check for dynamic allocas, because it's called directly from
1372 // various places, but TargetMaterializeAlloca also needs a check
1373 // in order to avoid recursion between getRegForValue,
1374 // X86SelectAddrss, and TargetMaterializeAlloca.
1375 if (!StaticAllocaMap.count(C))
1379 if (!X86SelectAddress(C, AM, false))
1381 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1382 TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1383 unsigned ResultReg = createResultReg(RC);
1384 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1389 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
1390 MachineModuleInfo *mmi,
1391 DenseMap<const Value *, unsigned> &vm,
1392 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
1393 DenseMap<const AllocaInst *, int> &am) {
1394 return new X86FastISel(mf, mmi, vm, bm, am);