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/CodeGen/FastISel.h"
23 #include "llvm/CodeGen/MachineRegisterInfo.h"
27 class X86FastISel : public FastISel {
28 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
29 /// make the right decision when generating code for different targets.
30 const X86Subtarget *Subtarget;
33 explicit X86FastISel(MachineFunction &mf,
34 DenseMap<const Value *, unsigned> &vm,
35 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm)
36 : FastISel(mf, vm, bm) {
37 Subtarget = &TM.getSubtarget<X86Subtarget>();
40 virtual bool TargetSelectInstruction(Instruction *I);
42 #include "X86GenFastISel.inc"
45 bool X86SelectConstAddr(Value *V, unsigned &Op0);
47 bool X86SelectLoad(Instruction *I);
49 bool X86SelectStore(Instruction *I);
52 /// X86SelectConstAddr - Select and emit code to materialize constant address.
54 bool X86FastISel::X86SelectConstAddr(Value *V,
56 // FIXME: Only GlobalAddress for now.
57 GlobalValue *GV = dyn_cast<GlobalValue>(V);
61 if (Subtarget->GVRequiresExtraLoad(GV, TM, false)) {
62 // Issue load from stub if necessary.
64 const TargetRegisterClass *RC = NULL;
65 if (TLI.getPointerTy() == MVT::i32) {
67 RC = X86::GR32RegisterClass;
70 RC = X86::GR64RegisterClass;
72 Op0 = createResultReg(RC);
75 addFullAddress(BuildMI(MBB, TII.get(Opc), Op0), AM);
76 // Prevent loading GV stub multiple times in same MBB.
77 LocalValueMap[V] = Op0;
82 /// X86SelectStore - Select and emit code to implement store instructions.
83 bool X86FastISel::X86SelectStore(Instruction* I) {
84 MVT VT = MVT::getMVT(I->getOperand(0)->getType());
85 if (VT == MVT::Other || !VT.isSimple())
86 // Unhandled type. Halt "fast" selection and bail.
90 VT = TLI.getPointerTy();
91 // We only handle legal types. For example, on x86-32 the instruction
92 // selector contains all of the 64-bit instructions from x86-64,
93 // under the assumption that i64 won't be used if the target doesn't
95 if (!TLI.isTypeLegal(VT))
97 unsigned Op0 = getRegForValue(I->getOperand(0));
99 // Unhandled operand. Halt "fast" selection and bail.
102 Value *V = I->getOperand(1);
103 unsigned Op1 = getRegForValue(V);
105 // Handle constant load address.
106 if (!isa<Constant>(V) || !X86SelectConstAddr(V, Op1))
107 // Unhandled operand. Halt "fast" selection and bail.
111 // Get opcode and regclass of the output for the given load instruction.
113 const TargetRegisterClass *RC = NULL;
114 switch (VT.getSimpleVT()) {
115 default: return false;
118 RC = X86::GR8RegisterClass;
122 RC = X86::GR16RegisterClass;
126 RC = X86::GR32RegisterClass;
129 // Must be in x86-64 mode.
131 RC = X86::GR64RegisterClass;
134 if (Subtarget->hasSSE1()) {
136 RC = X86::FR32RegisterClass;
139 RC = X86::RFP32RegisterClass;
143 if (Subtarget->hasSSE2()) {
145 RC = X86::FR64RegisterClass;
148 RC = X86::RFP64RegisterClass;
153 RC = X86::RFP80RegisterClass;
159 // Address is in register.
162 AM.GV = cast<GlobalValue>(V);
163 addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Op0);
167 /// X86SelectLoad - Select and emit code to implement load instructions.
169 bool X86FastISel::X86SelectLoad(Instruction *I) {
170 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
171 if (VT == MVT::Other || !VT.isSimple())
172 // Unhandled type. Halt "fast" selection and bail.
176 VT = TLI.getPointerTy();
177 // We only handle legal types. For example, on x86-32 the instruction
178 // selector contains all of the 64-bit instructions from x86-64,
179 // under the assumption that i64 won't be used if the target doesn't
181 if (!TLI.isTypeLegal(VT))
184 Value *V = I->getOperand(0);
185 unsigned Op0 = getRegForValue(V);
187 // Handle constant load address.
188 if (!isa<Constant>(V) || !X86SelectConstAddr(V, Op0))
189 // Unhandled operand. Halt "fast" selection and bail.
193 // Get opcode and regclass of the output for the given load instruction.
195 const TargetRegisterClass *RC = NULL;
196 switch (VT.getSimpleVT()) {
197 default: return false;
200 RC = X86::GR8RegisterClass;
204 RC = X86::GR16RegisterClass;
208 RC = X86::GR32RegisterClass;
211 // Must be in x86-64 mode.
213 RC = X86::GR64RegisterClass;
216 if (Subtarget->hasSSE1()) {
218 RC = X86::FR32RegisterClass;
221 RC = X86::RFP32RegisterClass;
225 if (Subtarget->hasSSE2()) {
227 RC = X86::FR64RegisterClass;
230 RC = X86::RFP64RegisterClass;
235 RC = X86::RFP80RegisterClass;
239 unsigned ResultReg = createResultReg(RC);
242 // Address is in register.
245 AM.GV = cast<GlobalValue>(V);
246 addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
247 UpdateValueMap(I, ResultReg);
253 X86FastISel::TargetSelectInstruction(Instruction *I) {
254 switch (I->getOpcode()) {
256 case Instruction::Load:
257 return X86SelectLoad(I);
258 case Instruction::Store:
259 return X86SelectStore(I);
266 llvm::FastISel *X86::createFastISel(MachineFunction &mf,
267 DenseMap<const Value *, unsigned> &vm,
268 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm) {
269 return new X86FastISel(mf, vm, bm);