blob: b12665b237aa67e088b5d2de54ed8bf26643b90a [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
Chris Lattner44d2a982008-10-13 01:59:13 +000014// types (e.g. i64 on 32-bit targets) and operations are not supported. It is
15// also not intended to be able to do much optimization, except in a few cases
16// where doing optimizations reduces overall compile time. For example, folding
17// constants into immediate fields is often done, because it's cheap and it
18// reduces the number of instructions later phases have to examine.
Dan Gohman5ec9efd2008-09-30 20:48:29 +000019//
20// "Fast" instruction selection is able to fail gracefully and transfer
21// control to the SelectionDAG selector for operations that it doesn't
Chris Lattner44d2a982008-10-13 01:59:13 +000022// support. In many cases, this allows us to avoid duplicating a lot of
Dan Gohman5ec9efd2008-09-30 20:48:29 +000023// 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
Chris Lattner44d2a982008-10-13 01:59:13 +000027// weighed against the speed at which the code can be generated. Also,
Dan Gohman5ec9efd2008-09-30 20:48:29 +000028// 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
Chris Lattner44d2a982008-10-13 01:59:13 +000030// time. Despite its limitations, "fast" instruction selection is able to
Dan Gohman5ec9efd2008-09-30 20:48:29 +000031// 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
Chris Lattner44d2a982008-10-13 01:59:13 +000037// from simple operators. More complicated operations currently require
Dan Gohman5ec9efd2008-09-30 20:48:29 +000038// 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 Gohman1e9e8c32008-10-07 22:03:27 +000084 // Translate this as an integer zero so that it can be
85 // local-CSE'd with actual integer zeros.
86 Reg = getRegForValue(Constant::getNullValue(TD.getIntPtrType()));
Dan Gohmanad368ac2008-08-27 18:10:19 +000087 } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000088 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +000089
90 if (!Reg) {
91 const APFloat &Flt = CF->getValueAPF();
92 MVT IntVT = TLI.getPointerTy();
93
94 uint64_t x[2];
95 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +000096 bool isExact;
97 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
98 APFloat::rmTowardZero, &isExact);
99 if (isExact) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000100 APInt IntVal(IntBitWidth, 2, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000101
Dan Gohman1e9e8c32008-10-07 22:03:27 +0000102 unsigned IntegerReg = getRegForValue(ConstantInt::get(IntVal));
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000103 if (IntegerReg != 0)
104 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
105 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000106 }
Dan Gohman40b189e2008-09-05 18:18:20 +0000107 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
108 if (!SelectOperator(CE, CE->getOpcode())) return 0;
109 Reg = LocalValueMap[CE];
Dan Gohman205d9252008-08-28 21:19:07 +0000110 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000111 Reg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman205d9252008-08-28 21:19:07 +0000112 BuildMI(MBB, TII.get(TargetInstrInfo::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000113 }
Owen Andersond5d81a42008-09-03 17:51:57 +0000114
Dan Gohmandceffe62008-09-25 01:28:51 +0000115 // If target-independent code couldn't handle the value, give target-specific
116 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000117 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000118 Reg = TargetMaterializeConstant(cast<Constant>(V));
Owen Anderson6e607452008-09-05 23:36:01 +0000119
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000120 // Don't cache constant materializations in the general ValueMap.
121 // To do so would require tracking what uses they dominate.
Dan Gohmandceffe62008-09-25 01:28:51 +0000122 if (Reg != 0)
123 LocalValueMap[V] = Reg;
Dan Gohman104e4ce2008-09-03 23:32:19 +0000124 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000125}
126
Evan Cheng59fbc802008-09-09 01:26:59 +0000127unsigned FastISel::lookUpRegForValue(Value *V) {
128 // Look up the value to see if we already have a register for it. We
129 // cache values defined by Instructions across blocks, and other values
130 // only locally. This is because Instructions already have the SSA
131 // def-dominatess-use requirement enforced.
132 if (ValueMap.count(V))
133 return ValueMap[V];
134 return LocalValueMap[V];
135}
136
Owen Andersoncc54e762008-08-30 00:38:46 +0000137/// UpdateValueMap - Update the value map to include the new mapping for this
138/// instruction, or insert an extra copy to get the result in a previous
139/// determined register.
140/// NOTE: This is only necessary because we might select a block that uses
141/// a value before we select the block that defines the value. It might be
142/// possible to fix this by selecting blocks in reverse postorder.
Owen Anderson95267a12008-09-05 00:06:23 +0000143void FastISel::UpdateValueMap(Value* I, unsigned Reg) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000144 if (!isa<Instruction>(I)) {
145 LocalValueMap[I] = Reg;
146 return;
147 }
Owen Andersoncc54e762008-08-30 00:38:46 +0000148 if (!ValueMap.count(I))
149 ValueMap[I] = Reg;
150 else
Evan Chengf0991782008-09-07 09:04:52 +0000151 TII.copyRegToReg(*MBB, MBB->end(), ValueMap[I],
152 Reg, MRI.getRegClass(Reg), MRI.getRegClass(Reg));
Owen Andersoncc54e762008-08-30 00:38:46 +0000153}
154
Dan Gohmanbdedd442008-08-20 00:11:48 +0000155/// SelectBinaryOp - Select and emit code for a binary operator instruction,
156/// which has an opcode which directly corresponds to the given ISD opcode.
157///
Dan Gohman40b189e2008-09-05 18:18:20 +0000158bool FastISel::SelectBinaryOp(User *I, ISD::NodeType ISDOpcode) {
Dan Gohmanbdedd442008-08-20 00:11:48 +0000159 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
160 if (VT == MVT::Other || !VT.isSimple())
161 // Unhandled type. Halt "fast" selection and bail.
162 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000163
Dan Gohmanb71fea22008-08-26 20:52:40 +0000164 // We only handle legal types. For example, on x86-32 the instruction
165 // selector contains all of the 64-bit instructions from x86-64,
166 // under the assumption that i64 won't be used if the target doesn't
167 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000168 if (!TLI.isTypeLegal(VT)) {
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000169 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000170 // don't require additional zeroing, which makes them easy.
171 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000172 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
173 ISDOpcode == ISD::XOR))
Dan Gohman638c6832008-09-05 18:44:22 +0000174 VT = TLI.getTypeToTransformTo(VT);
175 else
176 return false;
177 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000178
Dan Gohman3df24e62008-09-03 23:12:08 +0000179 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000180 if (Op0 == 0)
181 // Unhandled operand. Halt "fast" selection and bail.
182 return false;
183
184 // Check if the second operand is a constant and handle it appropriately.
185 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000186 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
187 ISDOpcode, Op0, CI->getZExtValue());
188 if (ResultReg != 0) {
189 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000190 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000191 return true;
192 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000193 }
194
Dan Gohman10df0fa2008-08-27 01:09:54 +0000195 // Check if the second operand is a constant float.
196 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000197 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
198 ISDOpcode, Op0, CF);
199 if (ResultReg != 0) {
200 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000201 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000202 return true;
203 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000204 }
205
Dan Gohman3df24e62008-09-03 23:12:08 +0000206 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000207 if (Op1 == 0)
208 // Unhandled operand. Halt "fast" selection and bail.
209 return false;
210
Dan Gohmanad368ac2008-08-27 18:10:19 +0000211 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000212 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
213 ISDOpcode, Op0, Op1);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000214 if (ResultReg == 0)
215 // Target-specific code wasn't able to find a machine opcode for
216 // the given ISD opcode and type. Halt "fast" selection and bail.
217 return false;
218
Dan Gohman8014e862008-08-20 00:23:20 +0000219 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000220 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000221 return true;
222}
223
Dan Gohman40b189e2008-09-05 18:18:20 +0000224bool FastISel::SelectGetElementPtr(User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000225 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000226 if (N == 0)
227 // Unhandled operand. Halt "fast" selection and bail.
228 return false;
229
230 const Type *Ty = I->getOperand(0)->getType();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000231 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
Evan Cheng83785c82008-08-20 22:45:34 +0000232 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
233 OI != E; ++OI) {
234 Value *Idx = *OI;
235 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
236 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
237 if (Field) {
238 // N = N + Offset
239 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
240 // FIXME: This can be optimized by combining the add with a
241 // subsequent one.
Dan Gohman7a0e6592008-08-21 17:25:26 +0000242 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000243 if (N == 0)
244 // Unhandled operand. Halt "fast" selection and bail.
245 return false;
246 }
247 Ty = StTy->getElementType(Field);
248 } else {
249 Ty = cast<SequentialType>(Ty)->getElementType();
250
251 // If this is a constant subscript, handle it quickly.
252 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
253 if (CI->getZExtValue() == 0) continue;
254 uint64_t Offs =
255 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000256 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000257 if (N == 0)
258 // Unhandled operand. Halt "fast" selection and bail.
259 return false;
260 continue;
261 }
262
263 // N = N + Idx * ElementSize;
264 uint64_t ElementSize = TD.getABITypeSize(Ty);
Dan Gohman3df24e62008-09-03 23:12:08 +0000265 unsigned IdxN = getRegForValue(Idx);
Evan Cheng83785c82008-08-20 22:45:34 +0000266 if (IdxN == 0)
267 // Unhandled operand. Halt "fast" selection and bail.
268 return false;
269
270 // If the index is smaller or larger than intptr_t, truncate or extend
271 // it.
Evan Cheng2076aa82008-08-21 01:19:11 +0000272 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
Evan Cheng83785c82008-08-20 22:45:34 +0000273 if (IdxVT.bitsLT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000274 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000275 else if (IdxVT.bitsGT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000276 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000277 if (IdxN == 0)
278 // Unhandled operand. Halt "fast" selection and bail.
279 return false;
280
Dan Gohman80bc6e22008-08-26 20:57:08 +0000281 if (ElementSize != 1) {
Dan Gohmanf93cf792008-08-21 17:37:05 +0000282 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000283 if (IdxN == 0)
284 // Unhandled operand. Halt "fast" selection and bail.
285 return false;
286 }
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000287 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000288 if (N == 0)
289 // Unhandled operand. Halt "fast" selection and bail.
290 return false;
291 }
292 }
293
294 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000295 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000296 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000297}
298
Dan Gohman33134c42008-09-25 17:05:24 +0000299bool FastISel::SelectCall(User *I) {
300 Function *F = cast<CallInst>(I)->getCalledFunction();
301 if (!F) return false;
302
303 unsigned IID = F->getIntrinsicID();
304 switch (IID) {
305 default: break;
306 case Intrinsic::dbg_stoppoint: {
307 DbgStopPointInst *SPI = cast<DbgStopPointInst>(I);
308 if (MMI && SPI->getContext() && MMI->Verify(SPI->getContext())) {
309 DebugInfoDesc *DD = MMI->getDescFor(SPI->getContext());
310 assert(DD && "Not a debug information descriptor");
311 const CompileUnitDesc *CompileUnit = cast<CompileUnitDesc>(DD);
312 unsigned SrcFile = MMI->RecordSource(CompileUnit);
313 unsigned Line = SPI->getLine();
314 unsigned Col = SPI->getColumn();
315 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
316 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
317 BuildMI(MBB, II).addImm(ID);
318 }
319 return true;
320 }
321 case Intrinsic::dbg_region_start: {
322 DbgRegionStartInst *RSI = cast<DbgRegionStartInst>(I);
323 if (MMI && RSI->getContext() && MMI->Verify(RSI->getContext())) {
324 unsigned ID = MMI->RecordRegionStart(RSI->getContext());
325 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
326 BuildMI(MBB, II).addImm(ID);
327 }
328 return true;
329 }
330 case Intrinsic::dbg_region_end: {
331 DbgRegionEndInst *REI = cast<DbgRegionEndInst>(I);
332 if (MMI && REI->getContext() && MMI->Verify(REI->getContext())) {
333 unsigned ID = MMI->RecordRegionEnd(REI->getContext());
334 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
335 BuildMI(MBB, II).addImm(ID);
336 }
337 return true;
338 }
339 case Intrinsic::dbg_func_start: {
340 if (!MMI) return true;
341 DbgFuncStartInst *FSI = cast<DbgFuncStartInst>(I);
342 Value *SP = FSI->getSubprogram();
343 if (SP && MMI->Verify(SP)) {
344 // llvm.dbg.func.start implicitly defines a dbg_stoppoint which is
345 // what (most?) gdb expects.
346 DebugInfoDesc *DD = MMI->getDescFor(SP);
347 assert(DD && "Not a debug information descriptor");
348 SubprogramDesc *Subprogram = cast<SubprogramDesc>(DD);
349 const CompileUnitDesc *CompileUnit = Subprogram->getFile();
350 unsigned SrcFile = MMI->RecordSource(CompileUnit);
351 // Record the source line but does create a label. It will be emitted
352 // at asm emission time.
353 MMI->RecordSourceLine(Subprogram->getLine(), 0, SrcFile);
354 }
355 return true;
356 }
357 case Intrinsic::dbg_declare: {
358 DbgDeclareInst *DI = cast<DbgDeclareInst>(I);
359 Value *Variable = DI->getVariable();
360 if (MMI && Variable && MMI->Verify(Variable)) {
361 // Determine the address of the declared object.
362 Value *Address = DI->getAddress();
363 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
364 Address = BCI->getOperand(0);
365 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
366 // Don't handle byval struct arguments, for example.
367 if (!AI) break;
368 DenseMap<const AllocaInst*, int>::iterator SI =
369 StaticAllocaMap.find(AI);
370 assert(SI != StaticAllocaMap.end() && "Invalid dbg.declare!");
371 int FI = SI->second;
372
373 // Determine the debug globalvariable.
374 GlobalValue *GV = cast<GlobalVariable>(Variable);
375
376 // Build the DECLARE instruction.
377 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DECLARE);
378 BuildMI(MBB, II).addFrameIndex(FI).addGlobalAddress(GV);
379 }
380 return true;
381 }
382 }
383 return false;
384}
385
Dan Gohman40b189e2008-09-05 18:18:20 +0000386bool FastISel::SelectCast(User *I, ISD::NodeType Opcode) {
Owen Anderson6336b702008-08-27 18:58:30 +0000387 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
388 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000389
390 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
391 DstVT == MVT::Other || !DstVT.isSimple() ||
Dan Gohman91b6f972008-10-03 01:28:47 +0000392 !TLI.isTypeLegal(DstVT))
Owen Andersond0533c92008-08-26 23:46:32 +0000393 // Unhandled type. Halt "fast" selection and bail.
394 return false;
395
Dan Gohman91b6f972008-10-03 01:28:47 +0000396 // Check if the source operand is legal. Or as a special case,
397 // it may be i1 if we're doing zero-extension because that's
398 // trivially easy and somewhat common.
399 if (!TLI.isTypeLegal(SrcVT)) {
400 if (SrcVT == MVT::i1 && Opcode == ISD::ZERO_EXTEND)
401 SrcVT = TLI.getTypeToTransformTo(SrcVT);
402 else
403 // Unhandled type. Halt "fast" selection and bail.
404 return false;
405 }
406
Dan Gohman3df24e62008-09-03 23:12:08 +0000407 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000408 if (!InputReg)
409 // Unhandled operand. Halt "fast" selection and bail.
410 return false;
411
412 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
413 DstVT.getSimpleVT(),
414 Opcode,
415 InputReg);
416 if (!ResultReg)
417 return false;
418
Dan Gohman3df24e62008-09-03 23:12:08 +0000419 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000420 return true;
421}
422
Dan Gohman40b189e2008-09-05 18:18:20 +0000423bool FastISel::SelectBitCast(User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000424 // If the bitcast doesn't change the type, just use the operand value.
425 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000426 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000427 if (Reg == 0)
428 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000429 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000430 return true;
431 }
432
433 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Anderson6336b702008-08-27 18:58:30 +0000434 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
435 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000436
437 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
438 DstVT == MVT::Other || !DstVT.isSimple() ||
439 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
440 // Unhandled type. Halt "fast" selection and bail.
441 return false;
442
Dan Gohman3df24e62008-09-03 23:12:08 +0000443 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000444 if (Op0 == 0)
445 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000446 return false;
447
Dan Gohmanad368ac2008-08-27 18:10:19 +0000448 // First, try to perform the bitcast by inserting a reg-reg copy.
449 unsigned ResultReg = 0;
450 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
451 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
452 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
453 ResultReg = createResultReg(DstClass);
454
455 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
456 Op0, DstClass, SrcClass);
457 if (!InsertedCopy)
458 ResultReg = 0;
459 }
460
461 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
462 if (!ResultReg)
463 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
464 ISD::BIT_CONVERT, Op0);
465
466 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000467 return false;
468
Dan Gohman3df24e62008-09-03 23:12:08 +0000469 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000470 return true;
471}
472
Dan Gohman3df24e62008-09-03 23:12:08 +0000473bool
474FastISel::SelectInstruction(Instruction *I) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000475 return SelectOperator(I, I->getOpcode());
476}
477
Dan Gohmand98d6202008-10-02 22:15:21 +0000478/// FastEmitBranch - Emit an unconditional branch to the given block,
479/// unless it is the immediate (fall-through) successor, and update
480/// the CFG.
481void
482FastISel::FastEmitBranch(MachineBasicBlock *MSucc) {
483 MachineFunction::iterator NextMBB =
484 next(MachineFunction::iterator(MBB));
485
486 if (MBB->isLayoutSuccessor(MSucc)) {
487 // The unconditional fall-through case, which needs no instructions.
488 } else {
489 // The unconditional branch case.
490 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
491 }
492 MBB->addSuccessor(MSucc);
493}
494
Dan Gohman40b189e2008-09-05 18:18:20 +0000495bool
496FastISel::SelectOperator(User *I, unsigned Opcode) {
497 switch (Opcode) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000498 case Instruction::Add: {
499 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
500 return SelectBinaryOp(I, Opc);
501 }
502 case Instruction::Sub: {
503 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
504 return SelectBinaryOp(I, Opc);
505 }
506 case Instruction::Mul: {
507 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
508 return SelectBinaryOp(I, Opc);
509 }
510 case Instruction::SDiv:
511 return SelectBinaryOp(I, ISD::SDIV);
512 case Instruction::UDiv:
513 return SelectBinaryOp(I, ISD::UDIV);
514 case Instruction::FDiv:
515 return SelectBinaryOp(I, ISD::FDIV);
516 case Instruction::SRem:
517 return SelectBinaryOp(I, ISD::SREM);
518 case Instruction::URem:
519 return SelectBinaryOp(I, ISD::UREM);
520 case Instruction::FRem:
521 return SelectBinaryOp(I, ISD::FREM);
522 case Instruction::Shl:
523 return SelectBinaryOp(I, ISD::SHL);
524 case Instruction::LShr:
525 return SelectBinaryOp(I, ISD::SRL);
526 case Instruction::AShr:
527 return SelectBinaryOp(I, ISD::SRA);
528 case Instruction::And:
529 return SelectBinaryOp(I, ISD::AND);
530 case Instruction::Or:
531 return SelectBinaryOp(I, ISD::OR);
532 case Instruction::Xor:
533 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000534
Dan Gohman3df24e62008-09-03 23:12:08 +0000535 case Instruction::GetElementPtr:
536 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000537
Dan Gohman3df24e62008-09-03 23:12:08 +0000538 case Instruction::Br: {
539 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000540
Dan Gohman3df24e62008-09-03 23:12:08 +0000541 if (BI->isUnconditional()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000542 BasicBlock *LLVMSucc = BI->getSuccessor(0);
543 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
Dan Gohmand98d6202008-10-02 22:15:21 +0000544 FastEmitBranch(MSucc);
Dan Gohman3df24e62008-09-03 23:12:08 +0000545 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000546 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000547
548 // Conditional branches are not handed yet.
549 // Halt "fast" selection and bail.
550 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000551 }
552
Dan Gohman087c8502008-09-05 01:08:41 +0000553 case Instruction::Unreachable:
554 // Nothing to emit.
555 return true;
556
Dan Gohman3df24e62008-09-03 23:12:08 +0000557 case Instruction::PHI:
558 // PHI nodes are already emitted.
559 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000560
561 case Instruction::Alloca:
562 // FunctionLowering has the static-sized case covered.
563 if (StaticAllocaMap.count(cast<AllocaInst>(I)))
564 return true;
565
566 // Dynamic-sized alloca is not handled yet.
567 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000568
Dan Gohman33134c42008-09-25 17:05:24 +0000569 case Instruction::Call:
570 return SelectCall(I);
571
Dan Gohman3df24e62008-09-03 23:12:08 +0000572 case Instruction::BitCast:
573 return SelectBitCast(I);
574
575 case Instruction::FPToSI:
576 return SelectCast(I, ISD::FP_TO_SINT);
577 case Instruction::ZExt:
578 return SelectCast(I, ISD::ZERO_EXTEND);
579 case Instruction::SExt:
580 return SelectCast(I, ISD::SIGN_EXTEND);
581 case Instruction::Trunc:
582 return SelectCast(I, ISD::TRUNCATE);
583 case Instruction::SIToFP:
584 return SelectCast(I, ISD::SINT_TO_FP);
585
586 case Instruction::IntToPtr: // Deliberate fall-through.
587 case Instruction::PtrToInt: {
588 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
589 MVT DstVT = TLI.getValueType(I->getType());
590 if (DstVT.bitsGT(SrcVT))
591 return SelectCast(I, ISD::ZERO_EXTEND);
592 if (DstVT.bitsLT(SrcVT))
593 return SelectCast(I, ISD::TRUNCATE);
594 unsigned Reg = getRegForValue(I->getOperand(0));
595 if (Reg == 0) return false;
596 UpdateValueMap(I, Reg);
597 return true;
598 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000599
Dan Gohman3df24e62008-09-03 23:12:08 +0000600 default:
601 // Unhandled instruction. Halt "fast" selection and bail.
602 return false;
603 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000604}
605
Dan Gohman3df24e62008-09-03 23:12:08 +0000606FastISel::FastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000607 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +0000608 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +0000609 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
610 DenseMap<const AllocaInst *, int> &am)
Dan Gohman3df24e62008-09-03 23:12:08 +0000611 : MBB(0),
612 ValueMap(vm),
613 MBBMap(bm),
Dan Gohman0586d912008-09-10 20:11:02 +0000614 StaticAllocaMap(am),
Dan Gohman3df24e62008-09-03 23:12:08 +0000615 MF(mf),
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000616 MMI(mmi),
Dan Gohman3df24e62008-09-03 23:12:08 +0000617 MRI(MF.getRegInfo()),
Dan Gohman0586d912008-09-10 20:11:02 +0000618 MFI(*MF.getFrameInfo()),
619 MCP(*MF.getConstantPool()),
Dan Gohman3df24e62008-09-03 23:12:08 +0000620 TM(MF.getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000621 TD(*TM.getTargetData()),
622 TII(*TM.getInstrInfo()),
623 TLI(*TM.getTargetLowering()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000624}
625
Dan Gohmane285a742008-08-14 21:51:29 +0000626FastISel::~FastISel() {}
627
Evan Cheng36fd9412008-09-02 21:59:13 +0000628unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType,
629 ISD::NodeType) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000630 return 0;
631}
632
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000633unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
634 ISD::NodeType, unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000635 return 0;
636}
637
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000638unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
639 ISD::NodeType, unsigned /*Op0*/,
640 unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000641 return 0;
642}
643
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000644unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
645 ISD::NodeType, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000646 return 0;
647}
648
Dan Gohman10df0fa2008-08-27 01:09:54 +0000649unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
650 ISD::NodeType, ConstantFP * /*FPImm*/) {
651 return 0;
652}
653
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000654unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
655 ISD::NodeType, unsigned /*Op0*/,
656 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000657 return 0;
658}
659
Dan Gohman10df0fa2008-08-27 01:09:54 +0000660unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
661 ISD::NodeType, unsigned /*Op0*/,
662 ConstantFP * /*FPImm*/) {
663 return 0;
664}
665
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000666unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
667 ISD::NodeType,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000668 unsigned /*Op0*/, unsigned /*Op1*/,
669 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000670 return 0;
671}
672
673/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
674/// to emit an instruction with an immediate operand using FastEmit_ri.
675/// If that fails, it materializes the immediate into a register and try
676/// FastEmit_rr instead.
677unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000678 unsigned Op0, uint64_t Imm,
679 MVT::SimpleValueType ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000680 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman151ed612008-08-27 18:15:05 +0000681 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000682 if (ResultReg != 0)
683 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000684 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000685 if (MaterialReg == 0)
686 return 0;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000687 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000688}
689
Dan Gohman10df0fa2008-08-27 01:09:54 +0000690/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
691/// to emit an instruction with a floating-point immediate operand using
692/// FastEmit_rf. If that fails, it materializes the immediate into a register
693/// and try FastEmit_rr instead.
694unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
695 unsigned Op0, ConstantFP *FPImm,
696 MVT::SimpleValueType ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000697 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohman151ed612008-08-27 18:15:05 +0000698 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000699 if (ResultReg != 0)
700 return ResultReg;
701
702 // Materialize the constant in a register.
703 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
704 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000705 // If the target doesn't have a way to directly enter a floating-point
706 // value into a register, use an alternate approach.
707 // TODO: The current approach only supports floating-point constants
708 // that can be constructed by conversion from integer values. This should
709 // be replaced by code that creates a load from a constant-pool entry,
710 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000711 const APFloat &Flt = FPImm->getValueAPF();
712 MVT IntVT = TLI.getPointerTy();
713
714 uint64_t x[2];
715 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000716 bool isExact;
717 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
718 APFloat::rmTowardZero, &isExact);
719 if (!isExact)
Dan Gohman10df0fa2008-08-27 01:09:54 +0000720 return 0;
721 APInt IntVal(IntBitWidth, 2, x);
722
723 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
724 ISD::Constant, IntVal.getZExtValue());
725 if (IntegerReg == 0)
726 return 0;
727 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
728 ISD::SINT_TO_FP, IntegerReg);
729 if (MaterialReg == 0)
730 return 0;
731 }
732 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
733}
734
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000735unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
736 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000737}
738
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000739unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000740 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000741 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000742 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000743
Dan Gohmanfd903942008-08-20 23:53:10 +0000744 BuildMI(MBB, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000745 return ResultReg;
746}
747
748unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
749 const TargetRegisterClass *RC,
750 unsigned Op0) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000751 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000752 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000753
Evan Cheng5960e4e2008-09-08 08:38:20 +0000754 if (II.getNumDefs() >= 1)
755 BuildMI(MBB, II, ResultReg).addReg(Op0);
756 else {
757 BuildMI(MBB, II).addReg(Op0);
758 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
759 II.ImplicitDefs[0], RC, RC);
760 if (!InsertedCopy)
761 ResultReg = 0;
762 }
763
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000764 return ResultReg;
765}
766
767unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
768 const TargetRegisterClass *RC,
769 unsigned Op0, unsigned Op1) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000770 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000771 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000772
Evan Cheng5960e4e2008-09-08 08:38:20 +0000773 if (II.getNumDefs() >= 1)
774 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
775 else {
776 BuildMI(MBB, II).addReg(Op0).addReg(Op1);
777 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
778 II.ImplicitDefs[0], RC, RC);
779 if (!InsertedCopy)
780 ResultReg = 0;
781 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000782 return ResultReg;
783}
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000784
785unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
786 const TargetRegisterClass *RC,
787 unsigned Op0, uint64_t Imm) {
788 unsigned ResultReg = createResultReg(RC);
789 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
790
Evan Cheng5960e4e2008-09-08 08:38:20 +0000791 if (II.getNumDefs() >= 1)
792 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
793 else {
794 BuildMI(MBB, II).addReg(Op0).addImm(Imm);
795 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
796 II.ImplicitDefs[0], RC, RC);
797 if (!InsertedCopy)
798 ResultReg = 0;
799 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000800 return ResultReg;
801}
802
Dan Gohman10df0fa2008-08-27 01:09:54 +0000803unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
804 const TargetRegisterClass *RC,
805 unsigned Op0, ConstantFP *FPImm) {
806 unsigned ResultReg = createResultReg(RC);
807 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
808
Evan Cheng5960e4e2008-09-08 08:38:20 +0000809 if (II.getNumDefs() >= 1)
810 BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
811 else {
812 BuildMI(MBB, II).addReg(Op0).addFPImm(FPImm);
813 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
814 II.ImplicitDefs[0], RC, RC);
815 if (!InsertedCopy)
816 ResultReg = 0;
817 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000818 return ResultReg;
819}
820
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000821unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
822 const TargetRegisterClass *RC,
823 unsigned Op0, unsigned Op1, uint64_t Imm) {
824 unsigned ResultReg = createResultReg(RC);
825 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
826
Evan Cheng5960e4e2008-09-08 08:38:20 +0000827 if (II.getNumDefs() >= 1)
828 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
829 else {
830 BuildMI(MBB, II).addReg(Op0).addReg(Op1).addImm(Imm);
831 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
832 II.ImplicitDefs[0], RC, RC);
833 if (!InsertedCopy)
834 ResultReg = 0;
835 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000836 return ResultReg;
837}
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000838
839unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
840 const TargetRegisterClass *RC,
841 uint64_t Imm) {
842 unsigned ResultReg = createResultReg(RC);
843 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
844
Evan Cheng5960e4e2008-09-08 08:38:20 +0000845 if (II.getNumDefs() >= 1)
846 BuildMI(MBB, II, ResultReg).addImm(Imm);
847 else {
848 BuildMI(MBB, II).addImm(Imm);
849 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
850 II.ImplicitDefs[0], RC, RC);
851 if (!InsertedCopy)
852 ResultReg = 0;
853 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000854 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +0000855}
Owen Anderson8970f002008-08-27 22:30:02 +0000856
Owen Anderson40a468f2008-08-28 17:47:37 +0000857unsigned FastISel::FastEmitInst_extractsubreg(unsigned Op0, uint32_t Idx) {
858 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +0000859 const TargetRegisterClass* SRC = *(RC->subregclasses_begin()+Idx-1);
860
861 unsigned ResultReg = createResultReg(SRC);
862 const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
863
Evan Cheng5960e4e2008-09-08 08:38:20 +0000864 if (II.getNumDefs() >= 1)
865 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Idx);
866 else {
867 BuildMI(MBB, II).addReg(Op0).addImm(Idx);
868 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
869 II.ImplicitDefs[0], RC, RC);
870 if (!InsertedCopy)
871 ResultReg = 0;
872 }
Owen Anderson8970f002008-08-27 22:30:02 +0000873 return ResultReg;
874}