blob: c0e8418c4c5853900436bc1a5b8b8a59c5a85de7 [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//
Dan Gohman5ec9efd2008-09-30 20:48:29 +000012// "Fast" instruction selection is designed to emit very poor code quickly.
13// Also, it is not designed to be able to do much lowering, so most illegal
14// types (e.g. i64 on 32-bit targets) and operations (e.g. calls) are not
15// supported. It is also not intended to be able to do much optimization,
16// except in a few cases where doing optimizations reduces overall compile
17// time (e.g. folding constants into immediate fields, because it's cheap
18// and it reduces the number of instructions later phases have to examine).
19//
20// "Fast" instruction selection is able to fail gracefully and transfer
21// control to the SelectionDAG selector for operations that it doesn't
22// support. In many cases, this allows us to avoid duplicating a lot of
23// the complicated lowering logic that SelectionDAG currently has.
24//
25// The intended use for "fast" instruction selection is "-O0" mode
26// compilation, where the quality of the generated code is irrelevant when
27// weighed against the speed at which the code can be generated. Also,
28// at -O0, the LLVM optimizers are not running, and this makes the
29// compile time of codegen a much higher portion of the overall compile
30// time. Despite its limitations, "fast" instruction selection is able to
31// handle enough code on its own to provide noticeable overall speedups
32// in -O0 compiles.
33//
34// Basic operations are supported in a target-independent way, by reading
35// the same instruction descriptions that the SelectionDAG selector reads,
36// and identifying simple arithmetic operations that can be directly selected
37// from simple operators. More complicated operations currently require
38// target-specific code.
39//
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000040//===----------------------------------------------------------------------===//
41
Dan Gohman33134c42008-09-25 17:05:24 +000042#include "llvm/Function.h"
43#include "llvm/GlobalVariable.h"
Dan Gohman6f2766d2008-08-19 22:31:46 +000044#include "llvm/Instructions.h"
Dan Gohman33134c42008-09-25 17:05:24 +000045#include "llvm/IntrinsicInst.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000046#include "llvm/CodeGen/FastISel.h"
47#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman33134c42008-09-25 17:05:24 +000048#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000049#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000050#include "llvm/Target/TargetData.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000051#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000052#include "llvm/Target/TargetLowering.h"
Dan Gohmanbb466332008-08-20 21:05:57 +000053#include "llvm/Target/TargetMachine.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000054using namespace llvm;
55
Dan Gohman3df24e62008-09-03 23:12:08 +000056unsigned FastISel::getRegForValue(Value *V) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000057 // Look up the value to see if we already have a register for it. We
58 // cache values defined by Instructions across blocks, and other values
59 // only locally. This is because Instructions already have the SSA
60 // def-dominatess-use requirement enforced.
Owen Anderson99aaf102008-09-03 17:37:03 +000061 if (ValueMap.count(V))
62 return ValueMap[V];
Dan Gohman104e4ce2008-09-03 23:32:19 +000063 unsigned Reg = LocalValueMap[V];
64 if (Reg != 0)
65 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +000066
67 MVT::SimpleValueType VT = TLI.getValueType(V->getType()).getSimpleVT();
Dan Gohman82116482008-09-10 21:01:08 +000068
69 // Ignore illegal types.
70 if (!TLI.isTypeLegal(VT)) {
71 // Promote MVT::i1 to a legal type though, because it's common and easy.
72 if (VT == MVT::i1)
73 VT = TLI.getTypeToTransformTo(VT).getSimpleVT();
74 else
75 return 0;
76 }
77
Dan Gohmanad368ac2008-08-27 18:10:19 +000078 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +000079 if (CI->getValue().getActiveBits() <= 64)
80 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +000081 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +000082 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +000083 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000084 Reg = FastEmit_i(VT, VT, ISD::Constant, 0);
Dan Gohmanad368ac2008-08-27 18:10:19 +000085 } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000086 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +000087
88 if (!Reg) {
89 const APFloat &Flt = CF->getValueAPF();
90 MVT IntVT = TLI.getPointerTy();
91
92 uint64_t x[2];
93 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dan Gohman2ff7fd12008-09-19 22:16:54 +000094 if (!Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
95 APFloat::rmTowardZero) != APFloat::opOK) {
96 APInt IntVal(IntBitWidth, 2, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +000097
Dan Gohman2ff7fd12008-09-19 22:16:54 +000098 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
99 ISD::Constant, IntVal.getZExtValue());
100 if (IntegerReg != 0)
101 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
102 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000103 }
Dan Gohman40b189e2008-09-05 18:18:20 +0000104 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
105 if (!SelectOperator(CE, CE->getOpcode())) return 0;
106 Reg = LocalValueMap[CE];
Dan Gohman205d9252008-08-28 21:19:07 +0000107 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000108 Reg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman205d9252008-08-28 21:19:07 +0000109 BuildMI(MBB, TII.get(TargetInstrInfo::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000110 }
Owen Andersond5d81a42008-09-03 17:51:57 +0000111
Dan Gohmandceffe62008-09-25 01:28:51 +0000112 // If target-independent code couldn't handle the value, give target-specific
113 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000114 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000115 Reg = TargetMaterializeConstant(cast<Constant>(V));
Owen Anderson6e607452008-09-05 23:36:01 +0000116
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000117 // Don't cache constant materializations in the general ValueMap.
118 // To do so would require tracking what uses they dominate.
Dan Gohmandceffe62008-09-25 01:28:51 +0000119 if (Reg != 0)
120 LocalValueMap[V] = Reg;
Dan Gohman104e4ce2008-09-03 23:32:19 +0000121 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000122}
123
Evan Cheng59fbc802008-09-09 01:26:59 +0000124unsigned FastISel::lookUpRegForValue(Value *V) {
125 // Look up the value to see if we already have a register for it. We
126 // cache values defined by Instructions across blocks, and other values
127 // only locally. This is because Instructions already have the SSA
128 // def-dominatess-use requirement enforced.
129 if (ValueMap.count(V))
130 return ValueMap[V];
131 return LocalValueMap[V];
132}
133
Owen Andersoncc54e762008-08-30 00:38:46 +0000134/// UpdateValueMap - Update the value map to include the new mapping for this
135/// instruction, or insert an extra copy to get the result in a previous
136/// determined register.
137/// NOTE: This is only necessary because we might select a block that uses
138/// a value before we select the block that defines the value. It might be
139/// possible to fix this by selecting blocks in reverse postorder.
Owen Anderson95267a12008-09-05 00:06:23 +0000140void FastISel::UpdateValueMap(Value* I, unsigned Reg) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000141 if (!isa<Instruction>(I)) {
142 LocalValueMap[I] = Reg;
143 return;
144 }
Owen Andersoncc54e762008-08-30 00:38:46 +0000145 if (!ValueMap.count(I))
146 ValueMap[I] = Reg;
147 else
Evan Chengf0991782008-09-07 09:04:52 +0000148 TII.copyRegToReg(*MBB, MBB->end(), ValueMap[I],
149 Reg, MRI.getRegClass(Reg), MRI.getRegClass(Reg));
Owen Andersoncc54e762008-08-30 00:38:46 +0000150}
151
Dan Gohmanbdedd442008-08-20 00:11:48 +0000152/// SelectBinaryOp - Select and emit code for a binary operator instruction,
153/// which has an opcode which directly corresponds to the given ISD opcode.
154///
Dan Gohman40b189e2008-09-05 18:18:20 +0000155bool FastISel::SelectBinaryOp(User *I, ISD::NodeType ISDOpcode) {
Dan Gohmanbdedd442008-08-20 00:11:48 +0000156 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
157 if (VT == MVT::Other || !VT.isSimple())
158 // Unhandled type. Halt "fast" selection and bail.
159 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000160
Dan Gohmanb71fea22008-08-26 20:52:40 +0000161 // We only handle legal types. For example, on x86-32 the instruction
162 // selector contains all of the 64-bit instructions from x86-64,
163 // under the assumption that i64 won't be used if the target doesn't
164 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000165 if (!TLI.isTypeLegal(VT)) {
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000166 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000167 // don't require additional zeroing, which makes them easy.
168 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000169 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
170 ISDOpcode == ISD::XOR))
Dan Gohman638c6832008-09-05 18:44:22 +0000171 VT = TLI.getTypeToTransformTo(VT);
172 else
173 return false;
174 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000175
Dan Gohman3df24e62008-09-03 23:12:08 +0000176 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000177 if (Op0 == 0)
178 // Unhandled operand. Halt "fast" selection and bail.
179 return false;
180
181 // Check if the second operand is a constant and handle it appropriately.
182 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000183 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
184 ISDOpcode, Op0, CI->getZExtValue());
185 if (ResultReg != 0) {
186 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000187 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000188 return true;
189 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000190 }
191
Dan Gohman10df0fa2008-08-27 01:09:54 +0000192 // Check if the second operand is a constant float.
193 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000194 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
195 ISDOpcode, Op0, CF);
196 if (ResultReg != 0) {
197 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000198 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000199 return true;
200 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000201 }
202
Dan Gohman3df24e62008-09-03 23:12:08 +0000203 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000204 if (Op1 == 0)
205 // Unhandled operand. Halt "fast" selection and bail.
206 return false;
207
Dan Gohmanad368ac2008-08-27 18:10:19 +0000208 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000209 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
210 ISDOpcode, Op0, Op1);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000211 if (ResultReg == 0)
212 // Target-specific code wasn't able to find a machine opcode for
213 // the given ISD opcode and type. Halt "fast" selection and bail.
214 return false;
215
Dan Gohman8014e862008-08-20 00:23:20 +0000216 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000217 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000218 return true;
219}
220
Dan Gohman40b189e2008-09-05 18:18:20 +0000221bool FastISel::SelectGetElementPtr(User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000222 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000223 if (N == 0)
224 // Unhandled operand. Halt "fast" selection and bail.
225 return false;
226
227 const Type *Ty = I->getOperand(0)->getType();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000228 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
Evan Cheng83785c82008-08-20 22:45:34 +0000229 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
230 OI != E; ++OI) {
231 Value *Idx = *OI;
232 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
233 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
234 if (Field) {
235 // N = N + Offset
236 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
237 // FIXME: This can be optimized by combining the add with a
238 // subsequent one.
Dan Gohman7a0e6592008-08-21 17:25:26 +0000239 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000240 if (N == 0)
241 // Unhandled operand. Halt "fast" selection and bail.
242 return false;
243 }
244 Ty = StTy->getElementType(Field);
245 } else {
246 Ty = cast<SequentialType>(Ty)->getElementType();
247
248 // If this is a constant subscript, handle it quickly.
249 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
250 if (CI->getZExtValue() == 0) continue;
251 uint64_t Offs =
252 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000253 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000254 if (N == 0)
255 // Unhandled operand. Halt "fast" selection and bail.
256 return false;
257 continue;
258 }
259
260 // N = N + Idx * ElementSize;
261 uint64_t ElementSize = TD.getABITypeSize(Ty);
Dan Gohman3df24e62008-09-03 23:12:08 +0000262 unsigned IdxN = getRegForValue(Idx);
Evan Cheng83785c82008-08-20 22:45:34 +0000263 if (IdxN == 0)
264 // Unhandled operand. Halt "fast" selection and bail.
265 return false;
266
267 // If the index is smaller or larger than intptr_t, truncate or extend
268 // it.
Evan Cheng2076aa82008-08-21 01:19:11 +0000269 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
Evan Cheng83785c82008-08-20 22:45:34 +0000270 if (IdxVT.bitsLT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000271 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000272 else if (IdxVT.bitsGT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000273 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000274 if (IdxN == 0)
275 // Unhandled operand. Halt "fast" selection and bail.
276 return false;
277
Dan Gohman80bc6e22008-08-26 20:57:08 +0000278 if (ElementSize != 1) {
Dan Gohmanf93cf792008-08-21 17:37:05 +0000279 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000280 if (IdxN == 0)
281 // Unhandled operand. Halt "fast" selection and bail.
282 return false;
283 }
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000284 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000285 if (N == 0)
286 // Unhandled operand. Halt "fast" selection and bail.
287 return false;
288 }
289 }
290
291 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000292 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000293 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000294}
295
Dan Gohman33134c42008-09-25 17:05:24 +0000296bool FastISel::SelectCall(User *I) {
297 Function *F = cast<CallInst>(I)->getCalledFunction();
298 if (!F) return false;
299
300 unsigned IID = F->getIntrinsicID();
301 switch (IID) {
302 default: break;
303 case Intrinsic::dbg_stoppoint: {
304 DbgStopPointInst *SPI = cast<DbgStopPointInst>(I);
305 if (MMI && SPI->getContext() && MMI->Verify(SPI->getContext())) {
306 DebugInfoDesc *DD = MMI->getDescFor(SPI->getContext());
307 assert(DD && "Not a debug information descriptor");
308 const CompileUnitDesc *CompileUnit = cast<CompileUnitDesc>(DD);
309 unsigned SrcFile = MMI->RecordSource(CompileUnit);
310 unsigned Line = SPI->getLine();
311 unsigned Col = SPI->getColumn();
312 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
313 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
314 BuildMI(MBB, II).addImm(ID);
315 }
316 return true;
317 }
318 case Intrinsic::dbg_region_start: {
319 DbgRegionStartInst *RSI = cast<DbgRegionStartInst>(I);
320 if (MMI && RSI->getContext() && MMI->Verify(RSI->getContext())) {
321 unsigned ID = MMI->RecordRegionStart(RSI->getContext());
322 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
323 BuildMI(MBB, II).addImm(ID);
324 }
325 return true;
326 }
327 case Intrinsic::dbg_region_end: {
328 DbgRegionEndInst *REI = cast<DbgRegionEndInst>(I);
329 if (MMI && REI->getContext() && MMI->Verify(REI->getContext())) {
330 unsigned ID = MMI->RecordRegionEnd(REI->getContext());
331 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
332 BuildMI(MBB, II).addImm(ID);
333 }
334 return true;
335 }
336 case Intrinsic::dbg_func_start: {
337 if (!MMI) return true;
338 DbgFuncStartInst *FSI = cast<DbgFuncStartInst>(I);
339 Value *SP = FSI->getSubprogram();
340 if (SP && MMI->Verify(SP)) {
341 // llvm.dbg.func.start implicitly defines a dbg_stoppoint which is
342 // what (most?) gdb expects.
343 DebugInfoDesc *DD = MMI->getDescFor(SP);
344 assert(DD && "Not a debug information descriptor");
345 SubprogramDesc *Subprogram = cast<SubprogramDesc>(DD);
346 const CompileUnitDesc *CompileUnit = Subprogram->getFile();
347 unsigned SrcFile = MMI->RecordSource(CompileUnit);
348 // Record the source line but does create a label. It will be emitted
349 // at asm emission time.
350 MMI->RecordSourceLine(Subprogram->getLine(), 0, SrcFile);
351 }
352 return true;
353 }
354 case Intrinsic::dbg_declare: {
355 DbgDeclareInst *DI = cast<DbgDeclareInst>(I);
356 Value *Variable = DI->getVariable();
357 if (MMI && Variable && MMI->Verify(Variable)) {
358 // Determine the address of the declared object.
359 Value *Address = DI->getAddress();
360 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
361 Address = BCI->getOperand(0);
362 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
363 // Don't handle byval struct arguments, for example.
364 if (!AI) break;
365 DenseMap<const AllocaInst*, int>::iterator SI =
366 StaticAllocaMap.find(AI);
367 assert(SI != StaticAllocaMap.end() && "Invalid dbg.declare!");
368 int FI = SI->second;
369
370 // Determine the debug globalvariable.
371 GlobalValue *GV = cast<GlobalVariable>(Variable);
372
373 // Build the DECLARE instruction.
374 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DECLARE);
375 BuildMI(MBB, II).addFrameIndex(FI).addGlobalAddress(GV);
376 }
377 return true;
378 }
379 }
380 return false;
381}
382
Dan Gohman40b189e2008-09-05 18:18:20 +0000383bool FastISel::SelectCast(User *I, ISD::NodeType Opcode) {
Owen Anderson6336b702008-08-27 18:58:30 +0000384 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
385 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000386
387 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
388 DstVT == MVT::Other || !DstVT.isSimple() ||
389 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
390 // Unhandled type. Halt "fast" selection and bail.
391 return false;
392
Dan Gohman3df24e62008-09-03 23:12:08 +0000393 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000394 if (!InputReg)
395 // Unhandled operand. Halt "fast" selection and bail.
396 return false;
397
398 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
399 DstVT.getSimpleVT(),
400 Opcode,
401 InputReg);
402 if (!ResultReg)
403 return false;
404
Dan Gohman3df24e62008-09-03 23:12:08 +0000405 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000406 return true;
407}
408
Dan Gohman40b189e2008-09-05 18:18:20 +0000409bool FastISel::SelectBitCast(User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000410 // If the bitcast doesn't change the type, just use the operand value.
411 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000412 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000413 if (Reg == 0)
414 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000415 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000416 return true;
417 }
418
419 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Anderson6336b702008-08-27 18:58:30 +0000420 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
421 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000422
423 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
424 DstVT == MVT::Other || !DstVT.isSimple() ||
425 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
426 // Unhandled type. Halt "fast" selection and bail.
427 return false;
428
Dan Gohman3df24e62008-09-03 23:12:08 +0000429 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000430 if (Op0 == 0)
431 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000432 return false;
433
Dan Gohmanad368ac2008-08-27 18:10:19 +0000434 // First, try to perform the bitcast by inserting a reg-reg copy.
435 unsigned ResultReg = 0;
436 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
437 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
438 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
439 ResultReg = createResultReg(DstClass);
440
441 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
442 Op0, DstClass, SrcClass);
443 if (!InsertedCopy)
444 ResultReg = 0;
445 }
446
447 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
448 if (!ResultReg)
449 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
450 ISD::BIT_CONVERT, Op0);
451
452 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000453 return false;
454
Dan Gohman3df24e62008-09-03 23:12:08 +0000455 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000456 return true;
457}
458
Dan Gohman3df24e62008-09-03 23:12:08 +0000459bool
460FastISel::SelectInstruction(Instruction *I) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000461 return SelectOperator(I, I->getOpcode());
462}
463
Dan Gohmand98d6202008-10-02 22:15:21 +0000464/// FastEmitBranch - Emit an unconditional branch to the given block,
465/// unless it is the immediate (fall-through) successor, and update
466/// the CFG.
467void
468FastISel::FastEmitBranch(MachineBasicBlock *MSucc) {
469 MachineFunction::iterator NextMBB =
470 next(MachineFunction::iterator(MBB));
471
472 if (MBB->isLayoutSuccessor(MSucc)) {
473 // The unconditional fall-through case, which needs no instructions.
474 } else {
475 // The unconditional branch case.
476 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
477 }
478 MBB->addSuccessor(MSucc);
479}
480
Dan Gohman40b189e2008-09-05 18:18:20 +0000481bool
482FastISel::SelectOperator(User *I, unsigned Opcode) {
483 switch (Opcode) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000484 case Instruction::Add: {
485 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
486 return SelectBinaryOp(I, Opc);
487 }
488 case Instruction::Sub: {
489 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
490 return SelectBinaryOp(I, Opc);
491 }
492 case Instruction::Mul: {
493 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
494 return SelectBinaryOp(I, Opc);
495 }
496 case Instruction::SDiv:
497 return SelectBinaryOp(I, ISD::SDIV);
498 case Instruction::UDiv:
499 return SelectBinaryOp(I, ISD::UDIV);
500 case Instruction::FDiv:
501 return SelectBinaryOp(I, ISD::FDIV);
502 case Instruction::SRem:
503 return SelectBinaryOp(I, ISD::SREM);
504 case Instruction::URem:
505 return SelectBinaryOp(I, ISD::UREM);
506 case Instruction::FRem:
507 return SelectBinaryOp(I, ISD::FREM);
508 case Instruction::Shl:
509 return SelectBinaryOp(I, ISD::SHL);
510 case Instruction::LShr:
511 return SelectBinaryOp(I, ISD::SRL);
512 case Instruction::AShr:
513 return SelectBinaryOp(I, ISD::SRA);
514 case Instruction::And:
515 return SelectBinaryOp(I, ISD::AND);
516 case Instruction::Or:
517 return SelectBinaryOp(I, ISD::OR);
518 case Instruction::Xor:
519 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000520
Dan Gohman3df24e62008-09-03 23:12:08 +0000521 case Instruction::GetElementPtr:
522 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000523
Dan Gohman3df24e62008-09-03 23:12:08 +0000524 case Instruction::Br: {
525 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000526
Dan Gohman3df24e62008-09-03 23:12:08 +0000527 if (BI->isUnconditional()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000528 BasicBlock *LLVMSucc = BI->getSuccessor(0);
529 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
Dan Gohmand98d6202008-10-02 22:15:21 +0000530 FastEmitBranch(MSucc);
Dan Gohman3df24e62008-09-03 23:12:08 +0000531 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000532 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000533
534 // Conditional branches are not handed yet.
535 // Halt "fast" selection and bail.
536 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000537 }
538
Dan Gohman087c8502008-09-05 01:08:41 +0000539 case Instruction::Unreachable:
540 // Nothing to emit.
541 return true;
542
Dan Gohman3df24e62008-09-03 23:12:08 +0000543 case Instruction::PHI:
544 // PHI nodes are already emitted.
545 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000546
547 case Instruction::Alloca:
548 // FunctionLowering has the static-sized case covered.
549 if (StaticAllocaMap.count(cast<AllocaInst>(I)))
550 return true;
551
552 // Dynamic-sized alloca is not handled yet.
553 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000554
Dan Gohman33134c42008-09-25 17:05:24 +0000555 case Instruction::Call:
556 return SelectCall(I);
557
Dan Gohman3df24e62008-09-03 23:12:08 +0000558 case Instruction::BitCast:
559 return SelectBitCast(I);
560
561 case Instruction::FPToSI:
562 return SelectCast(I, ISD::FP_TO_SINT);
563 case Instruction::ZExt:
564 return SelectCast(I, ISD::ZERO_EXTEND);
565 case Instruction::SExt:
566 return SelectCast(I, ISD::SIGN_EXTEND);
567 case Instruction::Trunc:
568 return SelectCast(I, ISD::TRUNCATE);
569 case Instruction::SIToFP:
570 return SelectCast(I, ISD::SINT_TO_FP);
571
572 case Instruction::IntToPtr: // Deliberate fall-through.
573 case Instruction::PtrToInt: {
574 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
575 MVT DstVT = TLI.getValueType(I->getType());
576 if (DstVT.bitsGT(SrcVT))
577 return SelectCast(I, ISD::ZERO_EXTEND);
578 if (DstVT.bitsLT(SrcVT))
579 return SelectCast(I, ISD::TRUNCATE);
580 unsigned Reg = getRegForValue(I->getOperand(0));
581 if (Reg == 0) return false;
582 UpdateValueMap(I, Reg);
583 return true;
584 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000585
Dan Gohman3df24e62008-09-03 23:12:08 +0000586 default:
587 // Unhandled instruction. Halt "fast" selection and bail.
588 return false;
589 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000590}
591
Dan Gohman3df24e62008-09-03 23:12:08 +0000592FastISel::FastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000593 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +0000594 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +0000595 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
596 DenseMap<const AllocaInst *, int> &am)
Dan Gohman3df24e62008-09-03 23:12:08 +0000597 : MBB(0),
598 ValueMap(vm),
599 MBBMap(bm),
Dan Gohman0586d912008-09-10 20:11:02 +0000600 StaticAllocaMap(am),
Dan Gohman3df24e62008-09-03 23:12:08 +0000601 MF(mf),
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000602 MMI(mmi),
Dan Gohman3df24e62008-09-03 23:12:08 +0000603 MRI(MF.getRegInfo()),
Dan Gohman0586d912008-09-10 20:11:02 +0000604 MFI(*MF.getFrameInfo()),
605 MCP(*MF.getConstantPool()),
Dan Gohman3df24e62008-09-03 23:12:08 +0000606 TM(MF.getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000607 TD(*TM.getTargetData()),
608 TII(*TM.getInstrInfo()),
609 TLI(*TM.getTargetLowering()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000610}
611
Dan Gohmane285a742008-08-14 21:51:29 +0000612FastISel::~FastISel() {}
613
Evan Cheng36fd9412008-09-02 21:59:13 +0000614unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType,
615 ISD::NodeType) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000616 return 0;
617}
618
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000619unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
620 ISD::NodeType, unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000621 return 0;
622}
623
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000624unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
625 ISD::NodeType, unsigned /*Op0*/,
626 unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000627 return 0;
628}
629
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000630unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
631 ISD::NodeType, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000632 return 0;
633}
634
Dan Gohman10df0fa2008-08-27 01:09:54 +0000635unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
636 ISD::NodeType, ConstantFP * /*FPImm*/) {
637 return 0;
638}
639
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000640unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
641 ISD::NodeType, unsigned /*Op0*/,
642 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000643 return 0;
644}
645
Dan Gohman10df0fa2008-08-27 01:09:54 +0000646unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
647 ISD::NodeType, unsigned /*Op0*/,
648 ConstantFP * /*FPImm*/) {
649 return 0;
650}
651
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000652unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
653 ISD::NodeType,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000654 unsigned /*Op0*/, unsigned /*Op1*/,
655 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000656 return 0;
657}
658
659/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
660/// to emit an instruction with an immediate operand using FastEmit_ri.
661/// If that fails, it materializes the immediate into a register and try
662/// FastEmit_rr instead.
663unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000664 unsigned Op0, uint64_t Imm,
665 MVT::SimpleValueType ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000666 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman151ed612008-08-27 18:15:05 +0000667 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000668 if (ResultReg != 0)
669 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000670 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000671 if (MaterialReg == 0)
672 return 0;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000673 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000674}
675
Dan Gohman10df0fa2008-08-27 01:09:54 +0000676/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
677/// to emit an instruction with a floating-point immediate operand using
678/// FastEmit_rf. If that fails, it materializes the immediate into a register
679/// and try FastEmit_rr instead.
680unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
681 unsigned Op0, ConstantFP *FPImm,
682 MVT::SimpleValueType ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000683 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohman151ed612008-08-27 18:15:05 +0000684 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000685 if (ResultReg != 0)
686 return ResultReg;
687
688 // Materialize the constant in a register.
689 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
690 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000691 // If the target doesn't have a way to directly enter a floating-point
692 // value into a register, use an alternate approach.
693 // TODO: The current approach only supports floating-point constants
694 // that can be constructed by conversion from integer values. This should
695 // be replaced by code that creates a load from a constant-pool entry,
696 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000697 const APFloat &Flt = FPImm->getValueAPF();
698 MVT IntVT = TLI.getPointerTy();
699
700 uint64_t x[2];
701 uint32_t IntBitWidth = IntVT.getSizeInBits();
702 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
703 APFloat::rmTowardZero) != APFloat::opOK)
704 return 0;
705 APInt IntVal(IntBitWidth, 2, x);
706
707 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
708 ISD::Constant, IntVal.getZExtValue());
709 if (IntegerReg == 0)
710 return 0;
711 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
712 ISD::SINT_TO_FP, IntegerReg);
713 if (MaterialReg == 0)
714 return 0;
715 }
716 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
717}
718
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000719unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
720 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000721}
722
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000723unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000724 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000725 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000726 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000727
Dan Gohmanfd903942008-08-20 23:53:10 +0000728 BuildMI(MBB, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000729 return ResultReg;
730}
731
732unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
733 const TargetRegisterClass *RC,
734 unsigned Op0) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000735 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000736 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000737
Evan Cheng5960e4e2008-09-08 08:38:20 +0000738 if (II.getNumDefs() >= 1)
739 BuildMI(MBB, II, ResultReg).addReg(Op0);
740 else {
741 BuildMI(MBB, II).addReg(Op0);
742 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
743 II.ImplicitDefs[0], RC, RC);
744 if (!InsertedCopy)
745 ResultReg = 0;
746 }
747
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000748 return ResultReg;
749}
750
751unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
752 const TargetRegisterClass *RC,
753 unsigned Op0, unsigned Op1) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000754 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000755 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000756
Evan Cheng5960e4e2008-09-08 08:38:20 +0000757 if (II.getNumDefs() >= 1)
758 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
759 else {
760 BuildMI(MBB, II).addReg(Op0).addReg(Op1);
761 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
762 II.ImplicitDefs[0], RC, RC);
763 if (!InsertedCopy)
764 ResultReg = 0;
765 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000766 return ResultReg;
767}
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000768
769unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
770 const TargetRegisterClass *RC,
771 unsigned Op0, uint64_t Imm) {
772 unsigned ResultReg = createResultReg(RC);
773 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
774
Evan Cheng5960e4e2008-09-08 08:38:20 +0000775 if (II.getNumDefs() >= 1)
776 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
777 else {
778 BuildMI(MBB, II).addReg(Op0).addImm(Imm);
779 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
780 II.ImplicitDefs[0], RC, RC);
781 if (!InsertedCopy)
782 ResultReg = 0;
783 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000784 return ResultReg;
785}
786
Dan Gohman10df0fa2008-08-27 01:09:54 +0000787unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
788 const TargetRegisterClass *RC,
789 unsigned Op0, ConstantFP *FPImm) {
790 unsigned ResultReg = createResultReg(RC);
791 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
792
Evan Cheng5960e4e2008-09-08 08:38:20 +0000793 if (II.getNumDefs() >= 1)
794 BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
795 else {
796 BuildMI(MBB, II).addReg(Op0).addFPImm(FPImm);
797 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
798 II.ImplicitDefs[0], RC, RC);
799 if (!InsertedCopy)
800 ResultReg = 0;
801 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000802 return ResultReg;
803}
804
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000805unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
806 const TargetRegisterClass *RC,
807 unsigned Op0, unsigned Op1, uint64_t Imm) {
808 unsigned ResultReg = createResultReg(RC);
809 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
810
Evan Cheng5960e4e2008-09-08 08:38:20 +0000811 if (II.getNumDefs() >= 1)
812 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
813 else {
814 BuildMI(MBB, II).addReg(Op0).addReg(Op1).addImm(Imm);
815 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
816 II.ImplicitDefs[0], RC, RC);
817 if (!InsertedCopy)
818 ResultReg = 0;
819 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000820 return ResultReg;
821}
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000822
823unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
824 const TargetRegisterClass *RC,
825 uint64_t Imm) {
826 unsigned ResultReg = createResultReg(RC);
827 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
828
Evan Cheng5960e4e2008-09-08 08:38:20 +0000829 if (II.getNumDefs() >= 1)
830 BuildMI(MBB, II, ResultReg).addImm(Imm);
831 else {
832 BuildMI(MBB, II).addImm(Imm);
833 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
834 II.ImplicitDefs[0], RC, RC);
835 if (!InsertedCopy)
836 ResultReg = 0;
837 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000838 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +0000839}
Owen Anderson8970f002008-08-27 22:30:02 +0000840
Owen Anderson40a468f2008-08-28 17:47:37 +0000841unsigned FastISel::FastEmitInst_extractsubreg(unsigned Op0, uint32_t Idx) {
842 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +0000843 const TargetRegisterClass* SRC = *(RC->subregclasses_begin()+Idx-1);
844
845 unsigned ResultReg = createResultReg(SRC);
846 const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
847
Evan Cheng5960e4e2008-09-08 08:38:20 +0000848 if (II.getNumDefs() >= 1)
849 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Idx);
850 else {
851 BuildMI(MBB, II).addReg(Op0).addImm(Idx);
852 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
853 II.ImplicitDefs[0], RC, RC);
854 if (!InsertedCopy)
855 ResultReg = 0;
856 }
Owen Anderson8970f002008-08-27 22:30:02 +0000857 return ResultReg;
858}