blob: 254b1fe42ccd5ec0e2bf1727ab6be9b6c637790d [file] [log] [blame]
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001///===-- FastISel.cpp - Implementation of the FastISel class --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the implementation of the FastISel class.
11//
12//===----------------------------------------------------------------------===//
13
Dan Gohman33134c42008-09-25 17:05:24 +000014#include "llvm/Function.h"
15#include "llvm/GlobalVariable.h"
Dan Gohman6f2766d2008-08-19 22:31:46 +000016#include "llvm/Instructions.h"
Dan Gohman33134c42008-09-25 17:05:24 +000017#include "llvm/IntrinsicInst.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000018#include "llvm/CodeGen/FastISel.h"
19#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman33134c42008-09-25 17:05:24 +000020#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000022#include "llvm/Target/TargetData.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000023#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000024#include "llvm/Target/TargetLowering.h"
Dan Gohmanbb466332008-08-20 21:05:57 +000025#include "llvm/Target/TargetMachine.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000026using namespace llvm;
27
Dan Gohman3df24e62008-09-03 23:12:08 +000028unsigned FastISel::getRegForValue(Value *V) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000029 // Look up the value to see if we already have a register for it. We
30 // cache values defined by Instructions across blocks, and other values
31 // only locally. This is because Instructions already have the SSA
32 // def-dominatess-use requirement enforced.
Owen Anderson99aaf102008-09-03 17:37:03 +000033 if (ValueMap.count(V))
34 return ValueMap[V];
Dan Gohman104e4ce2008-09-03 23:32:19 +000035 unsigned Reg = LocalValueMap[V];
36 if (Reg != 0)
37 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +000038
39 MVT::SimpleValueType VT = TLI.getValueType(V->getType()).getSimpleVT();
Dan Gohman82116482008-09-10 21:01:08 +000040
41 // Ignore illegal types.
42 if (!TLI.isTypeLegal(VT)) {
43 // Promote MVT::i1 to a legal type though, because it's common and easy.
44 if (VT == MVT::i1)
45 VT = TLI.getTypeToTransformTo(VT).getSimpleVT();
46 else
47 return 0;
48 }
49
Dan Gohmanad368ac2008-08-27 18:10:19 +000050 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +000051 if (CI->getValue().getActiveBits() <= 64)
52 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +000053 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +000054 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +000055 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000056 Reg = FastEmit_i(VT, VT, ISD::Constant, 0);
Dan Gohmanad368ac2008-08-27 18:10:19 +000057 } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000058 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +000059
60 if (!Reg) {
61 const APFloat &Flt = CF->getValueAPF();
62 MVT IntVT = TLI.getPointerTy();
63
64 uint64_t x[2];
65 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dan Gohman2ff7fd12008-09-19 22:16:54 +000066 if (!Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
67 APFloat::rmTowardZero) != APFloat::opOK) {
68 APInt IntVal(IntBitWidth, 2, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +000069
Dan Gohman2ff7fd12008-09-19 22:16:54 +000070 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
71 ISD::Constant, IntVal.getZExtValue());
72 if (IntegerReg != 0)
73 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
74 }
Dan Gohmanad368ac2008-08-27 18:10:19 +000075 }
Dan Gohman40b189e2008-09-05 18:18:20 +000076 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
77 if (!SelectOperator(CE, CE->getOpcode())) return 0;
78 Reg = LocalValueMap[CE];
Dan Gohman205d9252008-08-28 21:19:07 +000079 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000080 Reg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman205d9252008-08-28 21:19:07 +000081 BuildMI(MBB, TII.get(TargetInstrInfo::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +000082 }
Owen Andersond5d81a42008-09-03 17:51:57 +000083
Dan Gohmandceffe62008-09-25 01:28:51 +000084 // If target-independent code couldn't handle the value, give target-specific
85 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +000086 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +000087 Reg = TargetMaterializeConstant(cast<Constant>(V));
Owen Anderson6e607452008-09-05 23:36:01 +000088
Dan Gohman2ff7fd12008-09-19 22:16:54 +000089 // Don't cache constant materializations in the general ValueMap.
90 // To do so would require tracking what uses they dominate.
Dan Gohmandceffe62008-09-25 01:28:51 +000091 if (Reg != 0)
92 LocalValueMap[V] = Reg;
Dan Gohman104e4ce2008-09-03 23:32:19 +000093 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +000094}
95
Evan Cheng59fbc802008-09-09 01:26:59 +000096unsigned FastISel::lookUpRegForValue(Value *V) {
97 // Look up the value to see if we already have a register for it. We
98 // cache values defined by Instructions across blocks, and other values
99 // only locally. This is because Instructions already have the SSA
100 // def-dominatess-use requirement enforced.
101 if (ValueMap.count(V))
102 return ValueMap[V];
103 return LocalValueMap[V];
104}
105
Owen Andersoncc54e762008-08-30 00:38:46 +0000106/// UpdateValueMap - Update the value map to include the new mapping for this
107/// instruction, or insert an extra copy to get the result in a previous
108/// determined register.
109/// NOTE: This is only necessary because we might select a block that uses
110/// a value before we select the block that defines the value. It might be
111/// possible to fix this by selecting blocks in reverse postorder.
Owen Anderson95267a12008-09-05 00:06:23 +0000112void FastISel::UpdateValueMap(Value* I, unsigned Reg) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000113 if (!isa<Instruction>(I)) {
114 LocalValueMap[I] = Reg;
115 return;
116 }
Owen Andersoncc54e762008-08-30 00:38:46 +0000117 if (!ValueMap.count(I))
118 ValueMap[I] = Reg;
119 else
Evan Chengf0991782008-09-07 09:04:52 +0000120 TII.copyRegToReg(*MBB, MBB->end(), ValueMap[I],
121 Reg, MRI.getRegClass(Reg), MRI.getRegClass(Reg));
Owen Andersoncc54e762008-08-30 00:38:46 +0000122}
123
Dan Gohmanbdedd442008-08-20 00:11:48 +0000124/// SelectBinaryOp - Select and emit code for a binary operator instruction,
125/// which has an opcode which directly corresponds to the given ISD opcode.
126///
Dan Gohman40b189e2008-09-05 18:18:20 +0000127bool FastISel::SelectBinaryOp(User *I, ISD::NodeType ISDOpcode) {
Dan Gohmanbdedd442008-08-20 00:11:48 +0000128 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
129 if (VT == MVT::Other || !VT.isSimple())
130 // Unhandled type. Halt "fast" selection and bail.
131 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000132
Dan Gohmanb71fea22008-08-26 20:52:40 +0000133 // We only handle legal types. For example, on x86-32 the instruction
134 // selector contains all of the 64-bit instructions from x86-64,
135 // under the assumption that i64 won't be used if the target doesn't
136 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000137 if (!TLI.isTypeLegal(VT)) {
138 // MVT::i1 is special. Allow AND and OR (but not XOR) because they
139 // don't require additional zeroing, which makes them easy.
140 if (VT == MVT::i1 &&
141 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR))
142 VT = TLI.getTypeToTransformTo(VT);
143 else
144 return false;
145 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000146
Dan Gohman3df24e62008-09-03 23:12:08 +0000147 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000148 if (Op0 == 0)
149 // Unhandled operand. Halt "fast" selection and bail.
150 return false;
151
152 // Check if the second operand is a constant and handle it appropriately.
153 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000154 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
155 ISDOpcode, Op0, CI->getZExtValue());
156 if (ResultReg != 0) {
157 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000158 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000159 return true;
160 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000161 }
162
Dan Gohman10df0fa2008-08-27 01:09:54 +0000163 // Check if the second operand is a constant float.
164 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000165 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
166 ISDOpcode, Op0, CF);
167 if (ResultReg != 0) {
168 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000169 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000170 return true;
171 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000172 }
173
Dan Gohman3df24e62008-09-03 23:12:08 +0000174 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000175 if (Op1 == 0)
176 // Unhandled operand. Halt "fast" selection and bail.
177 return false;
178
Dan Gohmanad368ac2008-08-27 18:10:19 +0000179 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000180 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
181 ISDOpcode, Op0, Op1);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000182 if (ResultReg == 0)
183 // Target-specific code wasn't able to find a machine opcode for
184 // the given ISD opcode and type. Halt "fast" selection and bail.
185 return false;
186
Dan Gohman8014e862008-08-20 00:23:20 +0000187 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000188 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000189 return true;
190}
191
Dan Gohman40b189e2008-09-05 18:18:20 +0000192bool FastISel::SelectGetElementPtr(User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000193 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000194 if (N == 0)
195 // Unhandled operand. Halt "fast" selection and bail.
196 return false;
197
198 const Type *Ty = I->getOperand(0)->getType();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000199 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
Evan Cheng83785c82008-08-20 22:45:34 +0000200 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
201 OI != E; ++OI) {
202 Value *Idx = *OI;
203 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
204 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
205 if (Field) {
206 // N = N + Offset
207 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
208 // FIXME: This can be optimized by combining the add with a
209 // subsequent one.
Dan Gohman7a0e6592008-08-21 17:25:26 +0000210 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000211 if (N == 0)
212 // Unhandled operand. Halt "fast" selection and bail.
213 return false;
214 }
215 Ty = StTy->getElementType(Field);
216 } else {
217 Ty = cast<SequentialType>(Ty)->getElementType();
218
219 // If this is a constant subscript, handle it quickly.
220 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
221 if (CI->getZExtValue() == 0) continue;
222 uint64_t Offs =
223 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000224 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000225 if (N == 0)
226 // Unhandled operand. Halt "fast" selection and bail.
227 return false;
228 continue;
229 }
230
231 // N = N + Idx * ElementSize;
232 uint64_t ElementSize = TD.getABITypeSize(Ty);
Dan Gohman3df24e62008-09-03 23:12:08 +0000233 unsigned IdxN = getRegForValue(Idx);
Evan Cheng83785c82008-08-20 22:45:34 +0000234 if (IdxN == 0)
235 // Unhandled operand. Halt "fast" selection and bail.
236 return false;
237
238 // If the index is smaller or larger than intptr_t, truncate or extend
239 // it.
Evan Cheng2076aa82008-08-21 01:19:11 +0000240 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
Evan Cheng83785c82008-08-20 22:45:34 +0000241 if (IdxVT.bitsLT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000242 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000243 else if (IdxVT.bitsGT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000244 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000245 if (IdxN == 0)
246 // Unhandled operand. Halt "fast" selection and bail.
247 return false;
248
Dan Gohman80bc6e22008-08-26 20:57:08 +0000249 if (ElementSize != 1) {
Dan Gohmanf93cf792008-08-21 17:37:05 +0000250 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000251 if (IdxN == 0)
252 // Unhandled operand. Halt "fast" selection and bail.
253 return false;
254 }
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000255 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000256 if (N == 0)
257 // Unhandled operand. Halt "fast" selection and bail.
258 return false;
259 }
260 }
261
262 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000263 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000264 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000265}
266
Dan Gohman33134c42008-09-25 17:05:24 +0000267bool FastISel::SelectCall(User *I) {
268 Function *F = cast<CallInst>(I)->getCalledFunction();
269 if (!F) return false;
270
271 unsigned IID = F->getIntrinsicID();
272 switch (IID) {
273 default: break;
274 case Intrinsic::dbg_stoppoint: {
275 DbgStopPointInst *SPI = cast<DbgStopPointInst>(I);
276 if (MMI && SPI->getContext() && MMI->Verify(SPI->getContext())) {
277 DebugInfoDesc *DD = MMI->getDescFor(SPI->getContext());
278 assert(DD && "Not a debug information descriptor");
279 const CompileUnitDesc *CompileUnit = cast<CompileUnitDesc>(DD);
280 unsigned SrcFile = MMI->RecordSource(CompileUnit);
281 unsigned Line = SPI->getLine();
282 unsigned Col = SPI->getColumn();
283 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
284 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
285 BuildMI(MBB, II).addImm(ID);
286 }
287 return true;
288 }
289 case Intrinsic::dbg_region_start: {
290 DbgRegionStartInst *RSI = cast<DbgRegionStartInst>(I);
291 if (MMI && RSI->getContext() && MMI->Verify(RSI->getContext())) {
292 unsigned ID = MMI->RecordRegionStart(RSI->getContext());
293 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
294 BuildMI(MBB, II).addImm(ID);
295 }
296 return true;
297 }
298 case Intrinsic::dbg_region_end: {
299 DbgRegionEndInst *REI = cast<DbgRegionEndInst>(I);
300 if (MMI && REI->getContext() && MMI->Verify(REI->getContext())) {
301 unsigned ID = MMI->RecordRegionEnd(REI->getContext());
302 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
303 BuildMI(MBB, II).addImm(ID);
304 }
305 return true;
306 }
307 case Intrinsic::dbg_func_start: {
308 if (!MMI) return true;
309 DbgFuncStartInst *FSI = cast<DbgFuncStartInst>(I);
310 Value *SP = FSI->getSubprogram();
311 if (SP && MMI->Verify(SP)) {
312 // llvm.dbg.func.start implicitly defines a dbg_stoppoint which is
313 // what (most?) gdb expects.
314 DebugInfoDesc *DD = MMI->getDescFor(SP);
315 assert(DD && "Not a debug information descriptor");
316 SubprogramDesc *Subprogram = cast<SubprogramDesc>(DD);
317 const CompileUnitDesc *CompileUnit = Subprogram->getFile();
318 unsigned SrcFile = MMI->RecordSource(CompileUnit);
319 // Record the source line but does create a label. It will be emitted
320 // at asm emission time.
321 MMI->RecordSourceLine(Subprogram->getLine(), 0, SrcFile);
322 }
323 return true;
324 }
325 case Intrinsic::dbg_declare: {
326 DbgDeclareInst *DI = cast<DbgDeclareInst>(I);
327 Value *Variable = DI->getVariable();
328 if (MMI && Variable && MMI->Verify(Variable)) {
329 // Determine the address of the declared object.
330 Value *Address = DI->getAddress();
331 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
332 Address = BCI->getOperand(0);
333 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
334 // Don't handle byval struct arguments, for example.
335 if (!AI) break;
336 DenseMap<const AllocaInst*, int>::iterator SI =
337 StaticAllocaMap.find(AI);
338 assert(SI != StaticAllocaMap.end() && "Invalid dbg.declare!");
339 int FI = SI->second;
340
341 // Determine the debug globalvariable.
342 GlobalValue *GV = cast<GlobalVariable>(Variable);
343
344 // Build the DECLARE instruction.
345 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DECLARE);
346 BuildMI(MBB, II).addFrameIndex(FI).addGlobalAddress(GV);
347 }
348 return true;
349 }
350 }
351 return false;
352}
353
Dan Gohman40b189e2008-09-05 18:18:20 +0000354bool FastISel::SelectCast(User *I, ISD::NodeType Opcode) {
Owen Anderson6336b702008-08-27 18:58:30 +0000355 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
356 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000357
358 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
359 DstVT == MVT::Other || !DstVT.isSimple() ||
360 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
361 // Unhandled type. Halt "fast" selection and bail.
362 return false;
363
Dan Gohman3df24e62008-09-03 23:12:08 +0000364 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000365 if (!InputReg)
366 // Unhandled operand. Halt "fast" selection and bail.
367 return false;
368
369 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
370 DstVT.getSimpleVT(),
371 Opcode,
372 InputReg);
373 if (!ResultReg)
374 return false;
375
Dan Gohman3df24e62008-09-03 23:12:08 +0000376 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000377 return true;
378}
379
Dan Gohman40b189e2008-09-05 18:18:20 +0000380bool FastISel::SelectBitCast(User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000381 // If the bitcast doesn't change the type, just use the operand value.
382 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000383 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000384 if (Reg == 0)
385 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000386 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000387 return true;
388 }
389
390 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Anderson6336b702008-08-27 18:58:30 +0000391 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
392 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000393
394 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
395 DstVT == MVT::Other || !DstVT.isSimple() ||
396 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
397 // Unhandled type. Halt "fast" selection and bail.
398 return false;
399
Dan Gohman3df24e62008-09-03 23:12:08 +0000400 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000401 if (Op0 == 0)
402 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000403 return false;
404
Dan Gohmanad368ac2008-08-27 18:10:19 +0000405 // First, try to perform the bitcast by inserting a reg-reg copy.
406 unsigned ResultReg = 0;
407 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
408 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
409 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
410 ResultReg = createResultReg(DstClass);
411
412 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
413 Op0, DstClass, SrcClass);
414 if (!InsertedCopy)
415 ResultReg = 0;
416 }
417
418 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
419 if (!ResultReg)
420 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
421 ISD::BIT_CONVERT, Op0);
422
423 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000424 return false;
425
Dan Gohman3df24e62008-09-03 23:12:08 +0000426 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000427 return true;
428}
429
Dan Gohman3df24e62008-09-03 23:12:08 +0000430bool
431FastISel::SelectInstruction(Instruction *I) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000432 return SelectOperator(I, I->getOpcode());
433}
434
435bool
436FastISel::SelectOperator(User *I, unsigned Opcode) {
437 switch (Opcode) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000438 case Instruction::Add: {
439 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
440 return SelectBinaryOp(I, Opc);
441 }
442 case Instruction::Sub: {
443 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
444 return SelectBinaryOp(I, Opc);
445 }
446 case Instruction::Mul: {
447 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
448 return SelectBinaryOp(I, Opc);
449 }
450 case Instruction::SDiv:
451 return SelectBinaryOp(I, ISD::SDIV);
452 case Instruction::UDiv:
453 return SelectBinaryOp(I, ISD::UDIV);
454 case Instruction::FDiv:
455 return SelectBinaryOp(I, ISD::FDIV);
456 case Instruction::SRem:
457 return SelectBinaryOp(I, ISD::SREM);
458 case Instruction::URem:
459 return SelectBinaryOp(I, ISD::UREM);
460 case Instruction::FRem:
461 return SelectBinaryOp(I, ISD::FREM);
462 case Instruction::Shl:
463 return SelectBinaryOp(I, ISD::SHL);
464 case Instruction::LShr:
465 return SelectBinaryOp(I, ISD::SRL);
466 case Instruction::AShr:
467 return SelectBinaryOp(I, ISD::SRA);
468 case Instruction::And:
469 return SelectBinaryOp(I, ISD::AND);
470 case Instruction::Or:
471 return SelectBinaryOp(I, ISD::OR);
472 case Instruction::Xor:
473 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000474
Dan Gohman3df24e62008-09-03 23:12:08 +0000475 case Instruction::GetElementPtr:
476 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000477
Dan Gohman3df24e62008-09-03 23:12:08 +0000478 case Instruction::Br: {
479 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000480
Dan Gohman3df24e62008-09-03 23:12:08 +0000481 if (BI->isUnconditional()) {
482 MachineFunction::iterator NextMBB =
483 next(MachineFunction::iterator(MBB));
484 BasicBlock *LLVMSucc = BI->getSuccessor(0);
485 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
Dan Gohman6f2766d2008-08-19 22:31:46 +0000486
Dan Gohman3df24e62008-09-03 23:12:08 +0000487 if (NextMBB != MF.end() && MSucc == NextMBB) {
488 // The unconditional fall-through case, which needs no instructions.
Owen Anderson9d5b4162008-08-27 00:31:01 +0000489 } else {
Dan Gohman3df24e62008-09-03 23:12:08 +0000490 // The unconditional branch case.
491 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
Owen Anderson9d5b4162008-08-27 00:31:01 +0000492 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000493 MBB->addSuccessor(MSucc);
494 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000495 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000496
497 // Conditional branches are not handed yet.
498 // Halt "fast" selection and bail.
499 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000500 }
501
Dan Gohman087c8502008-09-05 01:08:41 +0000502 case Instruction::Unreachable:
503 // Nothing to emit.
504 return true;
505
Dan Gohman3df24e62008-09-03 23:12:08 +0000506 case Instruction::PHI:
507 // PHI nodes are already emitted.
508 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000509
510 case Instruction::Alloca:
511 // FunctionLowering has the static-sized case covered.
512 if (StaticAllocaMap.count(cast<AllocaInst>(I)))
513 return true;
514
515 // Dynamic-sized alloca is not handled yet.
516 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000517
Dan Gohman33134c42008-09-25 17:05:24 +0000518 case Instruction::Call:
519 return SelectCall(I);
520
Dan Gohman3df24e62008-09-03 23:12:08 +0000521 case Instruction::BitCast:
522 return SelectBitCast(I);
523
524 case Instruction::FPToSI:
525 return SelectCast(I, ISD::FP_TO_SINT);
526 case Instruction::ZExt:
527 return SelectCast(I, ISD::ZERO_EXTEND);
528 case Instruction::SExt:
529 return SelectCast(I, ISD::SIGN_EXTEND);
530 case Instruction::Trunc:
531 return SelectCast(I, ISD::TRUNCATE);
532 case Instruction::SIToFP:
533 return SelectCast(I, ISD::SINT_TO_FP);
534
535 case Instruction::IntToPtr: // Deliberate fall-through.
536 case Instruction::PtrToInt: {
537 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
538 MVT DstVT = TLI.getValueType(I->getType());
539 if (DstVT.bitsGT(SrcVT))
540 return SelectCast(I, ISD::ZERO_EXTEND);
541 if (DstVT.bitsLT(SrcVT))
542 return SelectCast(I, ISD::TRUNCATE);
543 unsigned Reg = getRegForValue(I->getOperand(0));
544 if (Reg == 0) return false;
545 UpdateValueMap(I, Reg);
546 return true;
547 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000548
Dan Gohman3df24e62008-09-03 23:12:08 +0000549 default:
550 // Unhandled instruction. Halt "fast" selection and bail.
551 return false;
552 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000553}
554
Dan Gohman3df24e62008-09-03 23:12:08 +0000555FastISel::FastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000556 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +0000557 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +0000558 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
559 DenseMap<const AllocaInst *, int> &am)
Dan Gohman3df24e62008-09-03 23:12:08 +0000560 : MBB(0),
561 ValueMap(vm),
562 MBBMap(bm),
Dan Gohman0586d912008-09-10 20:11:02 +0000563 StaticAllocaMap(am),
Dan Gohman3df24e62008-09-03 23:12:08 +0000564 MF(mf),
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000565 MMI(mmi),
Dan Gohman3df24e62008-09-03 23:12:08 +0000566 MRI(MF.getRegInfo()),
Dan Gohman0586d912008-09-10 20:11:02 +0000567 MFI(*MF.getFrameInfo()),
568 MCP(*MF.getConstantPool()),
Dan Gohman3df24e62008-09-03 23:12:08 +0000569 TM(MF.getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000570 TD(*TM.getTargetData()),
571 TII(*TM.getInstrInfo()),
572 TLI(*TM.getTargetLowering()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000573}
574
Dan Gohmane285a742008-08-14 21:51:29 +0000575FastISel::~FastISel() {}
576
Evan Cheng36fd9412008-09-02 21:59:13 +0000577unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType,
578 ISD::NodeType) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000579 return 0;
580}
581
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000582unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
583 ISD::NodeType, unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000584 return 0;
585}
586
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000587unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
588 ISD::NodeType, unsigned /*Op0*/,
589 unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000590 return 0;
591}
592
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000593unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
594 ISD::NodeType, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000595 return 0;
596}
597
Dan Gohman10df0fa2008-08-27 01:09:54 +0000598unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
599 ISD::NodeType, ConstantFP * /*FPImm*/) {
600 return 0;
601}
602
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000603unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
604 ISD::NodeType, unsigned /*Op0*/,
605 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000606 return 0;
607}
608
Dan Gohman10df0fa2008-08-27 01:09:54 +0000609unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
610 ISD::NodeType, unsigned /*Op0*/,
611 ConstantFP * /*FPImm*/) {
612 return 0;
613}
614
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000615unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
616 ISD::NodeType,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000617 unsigned /*Op0*/, unsigned /*Op1*/,
618 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000619 return 0;
620}
621
622/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
623/// to emit an instruction with an immediate operand using FastEmit_ri.
624/// If that fails, it materializes the immediate into a register and try
625/// FastEmit_rr instead.
626unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000627 unsigned Op0, uint64_t Imm,
628 MVT::SimpleValueType ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000629 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman151ed612008-08-27 18:15:05 +0000630 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000631 if (ResultReg != 0)
632 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000633 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000634 if (MaterialReg == 0)
635 return 0;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000636 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000637}
638
Dan Gohman10df0fa2008-08-27 01:09:54 +0000639/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
640/// to emit an instruction with a floating-point immediate operand using
641/// FastEmit_rf. If that fails, it materializes the immediate into a register
642/// and try FastEmit_rr instead.
643unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
644 unsigned Op0, ConstantFP *FPImm,
645 MVT::SimpleValueType ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000646 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohman151ed612008-08-27 18:15:05 +0000647 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000648 if (ResultReg != 0)
649 return ResultReg;
650
651 // Materialize the constant in a register.
652 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
653 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000654 // If the target doesn't have a way to directly enter a floating-point
655 // value into a register, use an alternate approach.
656 // TODO: The current approach only supports floating-point constants
657 // that can be constructed by conversion from integer values. This should
658 // be replaced by code that creates a load from a constant-pool entry,
659 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000660 const APFloat &Flt = FPImm->getValueAPF();
661 MVT IntVT = TLI.getPointerTy();
662
663 uint64_t x[2];
664 uint32_t IntBitWidth = IntVT.getSizeInBits();
665 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
666 APFloat::rmTowardZero) != APFloat::opOK)
667 return 0;
668 APInt IntVal(IntBitWidth, 2, x);
669
670 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
671 ISD::Constant, IntVal.getZExtValue());
672 if (IntegerReg == 0)
673 return 0;
674 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
675 ISD::SINT_TO_FP, IntegerReg);
676 if (MaterialReg == 0)
677 return 0;
678 }
679 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
680}
681
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000682unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
683 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000684}
685
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000686unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000687 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000688 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000689 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000690
Dan Gohmanfd903942008-08-20 23:53:10 +0000691 BuildMI(MBB, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000692 return ResultReg;
693}
694
695unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
696 const TargetRegisterClass *RC,
697 unsigned Op0) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000698 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000699 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000700
Evan Cheng5960e4e2008-09-08 08:38:20 +0000701 if (II.getNumDefs() >= 1)
702 BuildMI(MBB, II, ResultReg).addReg(Op0);
703 else {
704 BuildMI(MBB, II).addReg(Op0);
705 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
706 II.ImplicitDefs[0], RC, RC);
707 if (!InsertedCopy)
708 ResultReg = 0;
709 }
710
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000711 return ResultReg;
712}
713
714unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
715 const TargetRegisterClass *RC,
716 unsigned Op0, unsigned Op1) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000717 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000718 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000719
Evan Cheng5960e4e2008-09-08 08:38:20 +0000720 if (II.getNumDefs() >= 1)
721 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
722 else {
723 BuildMI(MBB, II).addReg(Op0).addReg(Op1);
724 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
725 II.ImplicitDefs[0], RC, RC);
726 if (!InsertedCopy)
727 ResultReg = 0;
728 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000729 return ResultReg;
730}
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000731
732unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
733 const TargetRegisterClass *RC,
734 unsigned Op0, uint64_t Imm) {
735 unsigned ResultReg = createResultReg(RC);
736 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
737
Evan Cheng5960e4e2008-09-08 08:38:20 +0000738 if (II.getNumDefs() >= 1)
739 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
740 else {
741 BuildMI(MBB, II).addReg(Op0).addImm(Imm);
742 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
743 II.ImplicitDefs[0], RC, RC);
744 if (!InsertedCopy)
745 ResultReg = 0;
746 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000747 return ResultReg;
748}
749
Dan Gohman10df0fa2008-08-27 01:09:54 +0000750unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
751 const TargetRegisterClass *RC,
752 unsigned Op0, ConstantFP *FPImm) {
753 unsigned ResultReg = createResultReg(RC);
754 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
755
Evan Cheng5960e4e2008-09-08 08:38:20 +0000756 if (II.getNumDefs() >= 1)
757 BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
758 else {
759 BuildMI(MBB, II).addReg(Op0).addFPImm(FPImm);
760 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
761 II.ImplicitDefs[0], RC, RC);
762 if (!InsertedCopy)
763 ResultReg = 0;
764 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000765 return ResultReg;
766}
767
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000768unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
769 const TargetRegisterClass *RC,
770 unsigned Op0, unsigned Op1, uint64_t Imm) {
771 unsigned ResultReg = createResultReg(RC);
772 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
773
Evan Cheng5960e4e2008-09-08 08:38:20 +0000774 if (II.getNumDefs() >= 1)
775 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
776 else {
777 BuildMI(MBB, II).addReg(Op0).addReg(Op1).addImm(Imm);
778 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
779 II.ImplicitDefs[0], RC, RC);
780 if (!InsertedCopy)
781 ResultReg = 0;
782 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000783 return ResultReg;
784}
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000785
786unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
787 const TargetRegisterClass *RC,
788 uint64_t Imm) {
789 unsigned ResultReg = createResultReg(RC);
790 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
791
Evan Cheng5960e4e2008-09-08 08:38:20 +0000792 if (II.getNumDefs() >= 1)
793 BuildMI(MBB, II, ResultReg).addImm(Imm);
794 else {
795 BuildMI(MBB, II).addImm(Imm);
796 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
797 II.ImplicitDefs[0], RC, RC);
798 if (!InsertedCopy)
799 ResultReg = 0;
800 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000801 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +0000802}
Owen Anderson8970f002008-08-27 22:30:02 +0000803
Owen Anderson40a468f2008-08-28 17:47:37 +0000804unsigned FastISel::FastEmitInst_extractsubreg(unsigned Op0, uint32_t Idx) {
805 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +0000806 const TargetRegisterClass* SRC = *(RC->subregclasses_begin()+Idx-1);
807
808 unsigned ResultReg = createResultReg(SRC);
809 const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
810
Evan Cheng5960e4e2008-09-08 08:38:20 +0000811 if (II.getNumDefs() >= 1)
812 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Idx);
813 else {
814 BuildMI(MBB, II).addReg(Op0).addImm(Idx);
815 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
816 II.ImplicitDefs[0], RC, RC);
817 if (!InsertedCopy)
818 ResultReg = 0;
819 }
Owen Anderson8970f002008-08-27 22:30:02 +0000820 return ResultReg;
821}