blob: 3d0348d5d118e118472c186a3cb0e8be2f7dc7b4 [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 Gohmandd5b58a2008-10-14 23:54:11 +000054#include "SelectionDAGBuild.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000055using namespace llvm;
56
Dan Gohman3df24e62008-09-03 23:12:08 +000057unsigned FastISel::getRegForValue(Value *V) {
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +000058 MVT::SimpleValueType VT = TLI.getValueType(V->getType()).getSimpleVT();
59
60 // Ignore illegal types. We must do this before looking up the value
61 // in ValueMap because Arguments are given virtual registers regardless
62 // of whether FastISel can handle them.
63 if (!TLI.isTypeLegal(VT)) {
64 // Promote MVT::i1 to a legal type though, because it's common and easy.
65 if (VT == MVT::i1)
66 VT = TLI.getTypeToTransformTo(VT).getSimpleVT();
67 else
68 return 0;
69 }
70
Dan Gohman104e4ce2008-09-03 23:32:19 +000071 // Look up the value to see if we already have a register for it. We
72 // cache values defined by Instructions across blocks, and other values
73 // only locally. This is because Instructions already have the SSA
74 // def-dominatess-use requirement enforced.
Owen Anderson99aaf102008-09-03 17:37:03 +000075 if (ValueMap.count(V))
76 return ValueMap[V];
Dan Gohman104e4ce2008-09-03 23:32:19 +000077 unsigned Reg = LocalValueMap[V];
78 if (Reg != 0)
79 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +000080
Dan Gohmanad368ac2008-08-27 18:10:19 +000081 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +000082 if (CI->getValue().getActiveBits() <= 64)
83 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +000084 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +000085 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +000086 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman1e9e8c32008-10-07 22:03:27 +000087 // Translate this as an integer zero so that it can be
88 // local-CSE'd with actual integer zeros.
89 Reg = getRegForValue(Constant::getNullValue(TD.getIntPtrType()));
Dan Gohmanad368ac2008-08-27 18:10:19 +000090 } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000091 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +000092
93 if (!Reg) {
94 const APFloat &Flt = CF->getValueAPF();
95 MVT IntVT = TLI.getPointerTy();
96
97 uint64_t x[2];
98 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +000099 bool isExact;
100 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
101 APFloat::rmTowardZero, &isExact);
102 if (isExact) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000103 APInt IntVal(IntBitWidth, 2, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000104
Dan Gohman1e9e8c32008-10-07 22:03:27 +0000105 unsigned IntegerReg = getRegForValue(ConstantInt::get(IntVal));
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000106 if (IntegerReg != 0)
107 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
108 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000109 }
Dan Gohman40b189e2008-09-05 18:18:20 +0000110 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
111 if (!SelectOperator(CE, CE->getOpcode())) return 0;
112 Reg = LocalValueMap[CE];
Dan Gohman205d9252008-08-28 21:19:07 +0000113 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000114 Reg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman205d9252008-08-28 21:19:07 +0000115 BuildMI(MBB, TII.get(TargetInstrInfo::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000116 }
Owen Andersond5d81a42008-09-03 17:51:57 +0000117
Dan Gohmandceffe62008-09-25 01:28:51 +0000118 // If target-independent code couldn't handle the value, give target-specific
119 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000120 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000121 Reg = TargetMaterializeConstant(cast<Constant>(V));
Owen Anderson6e607452008-09-05 23:36:01 +0000122
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000123 // Don't cache constant materializations in the general ValueMap.
124 // To do so would require tracking what uses they dominate.
Dan Gohmandceffe62008-09-25 01:28:51 +0000125 if (Reg != 0)
126 LocalValueMap[V] = Reg;
Dan Gohman104e4ce2008-09-03 23:32:19 +0000127 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000128}
129
Evan Cheng59fbc802008-09-09 01:26:59 +0000130unsigned FastISel::lookUpRegForValue(Value *V) {
131 // Look up the value to see if we already have a register for it. We
132 // cache values defined by Instructions across blocks, and other values
133 // only locally. This is because Instructions already have the SSA
134 // def-dominatess-use requirement enforced.
135 if (ValueMap.count(V))
136 return ValueMap[V];
137 return LocalValueMap[V];
138}
139
Owen Andersoncc54e762008-08-30 00:38:46 +0000140/// UpdateValueMap - Update the value map to include the new mapping for this
141/// instruction, or insert an extra copy to get the result in a previous
142/// determined register.
143/// NOTE: This is only necessary because we might select a block that uses
144/// a value before we select the block that defines the value. It might be
145/// possible to fix this by selecting blocks in reverse postorder.
Owen Anderson95267a12008-09-05 00:06:23 +0000146void FastISel::UpdateValueMap(Value* I, unsigned Reg) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000147 if (!isa<Instruction>(I)) {
148 LocalValueMap[I] = Reg;
149 return;
150 }
Owen Andersoncc54e762008-08-30 00:38:46 +0000151 if (!ValueMap.count(I))
152 ValueMap[I] = Reg;
153 else
Evan Chengf0991782008-09-07 09:04:52 +0000154 TII.copyRegToReg(*MBB, MBB->end(), ValueMap[I],
155 Reg, MRI.getRegClass(Reg), MRI.getRegClass(Reg));
Owen Andersoncc54e762008-08-30 00:38:46 +0000156}
157
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000158unsigned FastISel::getRegForGEPIndex(Value *Idx) {
159 unsigned IdxN = getRegForValue(Idx);
160 if (IdxN == 0)
161 // Unhandled operand. Halt "fast" selection and bail.
162 return 0;
163
164 // If the index is smaller or larger than intptr_t, truncate or extend it.
165 MVT PtrVT = TLI.getPointerTy();
166 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
167 if (IdxVT.bitsLT(PtrVT))
168 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT.getSimpleVT(),
169 ISD::SIGN_EXTEND, IdxN);
170 else if (IdxVT.bitsGT(PtrVT))
171 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT.getSimpleVT(),
172 ISD::TRUNCATE, IdxN);
173 return IdxN;
174}
175
Dan Gohmanbdedd442008-08-20 00:11:48 +0000176/// SelectBinaryOp - Select and emit code for a binary operator instruction,
177/// which has an opcode which directly corresponds to the given ISD opcode.
178///
Dan Gohman40b189e2008-09-05 18:18:20 +0000179bool FastISel::SelectBinaryOp(User *I, ISD::NodeType ISDOpcode) {
Dan Gohmanbdedd442008-08-20 00:11:48 +0000180 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
181 if (VT == MVT::Other || !VT.isSimple())
182 // Unhandled type. Halt "fast" selection and bail.
183 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000184
Dan Gohmanb71fea22008-08-26 20:52:40 +0000185 // We only handle legal types. For example, on x86-32 the instruction
186 // selector contains all of the 64-bit instructions from x86-64,
187 // under the assumption that i64 won't be used if the target doesn't
188 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000189 if (!TLI.isTypeLegal(VT)) {
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000190 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000191 // don't require additional zeroing, which makes them easy.
192 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000193 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
194 ISDOpcode == ISD::XOR))
Dan Gohman638c6832008-09-05 18:44:22 +0000195 VT = TLI.getTypeToTransformTo(VT);
196 else
197 return false;
198 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000199
Dan Gohman3df24e62008-09-03 23:12:08 +0000200 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000201 if (Op0 == 0)
202 // Unhandled operand. Halt "fast" selection and bail.
203 return false;
204
205 // Check if the second operand is a constant and handle it appropriately.
206 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000207 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
208 ISDOpcode, Op0, CI->getZExtValue());
209 if (ResultReg != 0) {
210 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000211 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000212 return true;
213 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000214 }
215
Dan Gohman10df0fa2008-08-27 01:09:54 +0000216 // Check if the second operand is a constant float.
217 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000218 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
219 ISDOpcode, Op0, CF);
220 if (ResultReg != 0) {
221 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000222 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000223 return true;
224 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000225 }
226
Dan Gohman3df24e62008-09-03 23:12:08 +0000227 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000228 if (Op1 == 0)
229 // Unhandled operand. Halt "fast" selection and bail.
230 return false;
231
Dan Gohmanad368ac2008-08-27 18:10:19 +0000232 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000233 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
234 ISDOpcode, Op0, Op1);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000235 if (ResultReg == 0)
236 // Target-specific code wasn't able to find a machine opcode for
237 // the given ISD opcode and type. Halt "fast" selection and bail.
238 return false;
239
Dan Gohman8014e862008-08-20 00:23:20 +0000240 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000241 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000242 return true;
243}
244
Dan Gohman40b189e2008-09-05 18:18:20 +0000245bool FastISel::SelectGetElementPtr(User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000246 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000247 if (N == 0)
248 // Unhandled operand. Halt "fast" selection and bail.
249 return false;
250
251 const Type *Ty = I->getOperand(0)->getType();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000252 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
Evan Cheng83785c82008-08-20 22:45:34 +0000253 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
254 OI != E; ++OI) {
255 Value *Idx = *OI;
256 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
257 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
258 if (Field) {
259 // N = N + Offset
260 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
261 // FIXME: This can be optimized by combining the add with a
262 // subsequent one.
Dan Gohman7a0e6592008-08-21 17:25:26 +0000263 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000264 if (N == 0)
265 // Unhandled operand. Halt "fast" selection and bail.
266 return false;
267 }
268 Ty = StTy->getElementType(Field);
269 } else {
270 Ty = cast<SequentialType>(Ty)->getElementType();
271
272 // If this is a constant subscript, handle it quickly.
273 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
274 if (CI->getZExtValue() == 0) continue;
275 uint64_t Offs =
276 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000277 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000278 if (N == 0)
279 // Unhandled operand. Halt "fast" selection and bail.
280 return false;
281 continue;
282 }
283
284 // N = N + Idx * ElementSize;
285 uint64_t ElementSize = TD.getABITypeSize(Ty);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000286 unsigned IdxN = getRegForGEPIndex(Idx);
Evan Cheng83785c82008-08-20 22:45:34 +0000287 if (IdxN == 0)
288 // Unhandled operand. Halt "fast" selection and bail.
289 return false;
290
Dan Gohman80bc6e22008-08-26 20:57:08 +0000291 if (ElementSize != 1) {
Dan Gohmanf93cf792008-08-21 17:37:05 +0000292 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000293 if (IdxN == 0)
294 // Unhandled operand. Halt "fast" selection and bail.
295 return false;
296 }
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000297 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000298 if (N == 0)
299 // Unhandled operand. Halt "fast" selection and bail.
300 return false;
301 }
302 }
303
304 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000305 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000306 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000307}
308
Dan Gohman33134c42008-09-25 17:05:24 +0000309bool FastISel::SelectCall(User *I) {
310 Function *F = cast<CallInst>(I)->getCalledFunction();
311 if (!F) return false;
312
313 unsigned IID = F->getIntrinsicID();
314 switch (IID) {
315 default: break;
316 case Intrinsic::dbg_stoppoint: {
317 DbgStopPointInst *SPI = cast<DbgStopPointInst>(I);
318 if (MMI && SPI->getContext() && MMI->Verify(SPI->getContext())) {
319 DebugInfoDesc *DD = MMI->getDescFor(SPI->getContext());
320 assert(DD && "Not a debug information descriptor");
321 const CompileUnitDesc *CompileUnit = cast<CompileUnitDesc>(DD);
322 unsigned SrcFile = MMI->RecordSource(CompileUnit);
323 unsigned Line = SPI->getLine();
324 unsigned Col = SPI->getColumn();
325 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
326 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
327 BuildMI(MBB, II).addImm(ID);
328 }
329 return true;
330 }
331 case Intrinsic::dbg_region_start: {
332 DbgRegionStartInst *RSI = cast<DbgRegionStartInst>(I);
333 if (MMI && RSI->getContext() && MMI->Verify(RSI->getContext())) {
334 unsigned ID = MMI->RecordRegionStart(RSI->getContext());
335 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
336 BuildMI(MBB, II).addImm(ID);
337 }
338 return true;
339 }
340 case Intrinsic::dbg_region_end: {
341 DbgRegionEndInst *REI = cast<DbgRegionEndInst>(I);
342 if (MMI && REI->getContext() && MMI->Verify(REI->getContext())) {
343 unsigned ID = MMI->RecordRegionEnd(REI->getContext());
344 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
345 BuildMI(MBB, II).addImm(ID);
346 }
347 return true;
348 }
349 case Intrinsic::dbg_func_start: {
350 if (!MMI) return true;
351 DbgFuncStartInst *FSI = cast<DbgFuncStartInst>(I);
352 Value *SP = FSI->getSubprogram();
353 if (SP && MMI->Verify(SP)) {
354 // llvm.dbg.func.start implicitly defines a dbg_stoppoint which is
355 // what (most?) gdb expects.
356 DebugInfoDesc *DD = MMI->getDescFor(SP);
357 assert(DD && "Not a debug information descriptor");
358 SubprogramDesc *Subprogram = cast<SubprogramDesc>(DD);
359 const CompileUnitDesc *CompileUnit = Subprogram->getFile();
360 unsigned SrcFile = MMI->RecordSource(CompileUnit);
Devang Patele75808c2008-11-06 21:28:20 +0000361 // Record the source line but does not create a label for the normal
362 // function start. It will be emitted at asm emission time. However,
363 // create a label if this is a beginning of inlined function.
364 unsigned LabelID = MMI->RecordSourceLine(Subprogram->getLine(), 0, SrcFile);
365 if (MMI->getSourceLines().size() != 1) {
366 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
367 BuildMI(MBB, II).addImm(LabelID);
368 }
Dan Gohman33134c42008-09-25 17:05:24 +0000369 }
370 return true;
371 }
372 case Intrinsic::dbg_declare: {
373 DbgDeclareInst *DI = cast<DbgDeclareInst>(I);
374 Value *Variable = DI->getVariable();
375 if (MMI && Variable && MMI->Verify(Variable)) {
376 // Determine the address of the declared object.
377 Value *Address = DI->getAddress();
378 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
379 Address = BCI->getOperand(0);
380 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
381 // Don't handle byval struct arguments, for example.
382 if (!AI) break;
383 DenseMap<const AllocaInst*, int>::iterator SI =
384 StaticAllocaMap.find(AI);
385 assert(SI != StaticAllocaMap.end() && "Invalid dbg.declare!");
386 int FI = SI->second;
387
388 // Determine the debug globalvariable.
389 GlobalValue *GV = cast<GlobalVariable>(Variable);
390
391 // Build the DECLARE instruction.
392 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DECLARE);
393 BuildMI(MBB, II).addFrameIndex(FI).addGlobalAddress(GV);
394 }
395 return true;
396 }
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000397 case Intrinsic::eh_exception: {
398 MVT VT = TLI.getValueType(I->getType());
399 switch (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)) {
400 default: break;
401 case TargetLowering::Expand: {
402 if (!MBB->isLandingPad()) {
403 // FIXME: Mark exception register as live in. Hack for PR1508.
404 unsigned Reg = TLI.getExceptionAddressRegister();
405 if (Reg) MBB->addLiveIn(Reg);
406 }
407 unsigned Reg = TLI.getExceptionAddressRegister();
408 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
409 unsigned ResultReg = createResultReg(RC);
410 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
411 Reg, RC, RC);
412 assert(InsertedCopy && "Can't copy address registers!");
Evan Cheng24ac4082008-11-24 07:09:49 +0000413 InsertedCopy = InsertedCopy;
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000414 UpdateValueMap(I, ResultReg);
415 return true;
416 }
417 }
418 break;
419 }
420 case Intrinsic::eh_selector_i32:
421 case Intrinsic::eh_selector_i64: {
422 MVT VT = TLI.getValueType(I->getType());
423 switch (TLI.getOperationAction(ISD::EHSELECTION, VT)) {
424 default: break;
425 case TargetLowering::Expand: {
426 MVT VT = (IID == Intrinsic::eh_selector_i32 ?
427 MVT::i32 : MVT::i64);
428
429 if (MMI) {
430 if (MBB->isLandingPad())
431 AddCatchInfo(*cast<CallInst>(I), MMI, MBB);
432 else {
433#ifndef NDEBUG
434 CatchInfoLost.insert(cast<CallInst>(I));
435#endif
436 // FIXME: Mark exception selector register as live in. Hack for PR1508.
437 unsigned Reg = TLI.getExceptionSelectorRegister();
438 if (Reg) MBB->addLiveIn(Reg);
439 }
440
441 unsigned Reg = TLI.getExceptionSelectorRegister();
442 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
443 unsigned ResultReg = createResultReg(RC);
444 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
445 Reg, RC, RC);
446 assert(InsertedCopy && "Can't copy address registers!");
Evan Cheng24ac4082008-11-24 07:09:49 +0000447 InsertedCopy = InsertedCopy;
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000448 UpdateValueMap(I, ResultReg);
449 } else {
450 unsigned ResultReg =
451 getRegForValue(Constant::getNullValue(I->getType()));
452 UpdateValueMap(I, ResultReg);
453 }
454 return true;
455 }
456 }
457 break;
458 }
Dan Gohman33134c42008-09-25 17:05:24 +0000459 }
460 return false;
461}
462
Dan Gohman40b189e2008-09-05 18:18:20 +0000463bool FastISel::SelectCast(User *I, ISD::NodeType Opcode) {
Owen Anderson6336b702008-08-27 18:58:30 +0000464 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
465 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000466
467 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
468 DstVT == MVT::Other || !DstVT.isSimple() ||
Dan Gohman91b6f972008-10-03 01:28:47 +0000469 !TLI.isTypeLegal(DstVT))
Owen Andersond0533c92008-08-26 23:46:32 +0000470 // Unhandled type. Halt "fast" selection and bail.
471 return false;
472
Dan Gohman91b6f972008-10-03 01:28:47 +0000473 // Check if the source operand is legal. Or as a special case,
474 // it may be i1 if we're doing zero-extension because that's
475 // trivially easy and somewhat common.
476 if (!TLI.isTypeLegal(SrcVT)) {
477 if (SrcVT == MVT::i1 && Opcode == ISD::ZERO_EXTEND)
478 SrcVT = TLI.getTypeToTransformTo(SrcVT);
479 else
480 // Unhandled type. Halt "fast" selection and bail.
481 return false;
482 }
483
Dan Gohman3df24e62008-09-03 23:12:08 +0000484 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000485 if (!InputReg)
486 // Unhandled operand. Halt "fast" selection and bail.
487 return false;
488
489 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
490 DstVT.getSimpleVT(),
491 Opcode,
492 InputReg);
493 if (!ResultReg)
494 return false;
495
Dan Gohman3df24e62008-09-03 23:12:08 +0000496 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000497 return true;
498}
499
Dan Gohman40b189e2008-09-05 18:18:20 +0000500bool FastISel::SelectBitCast(User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000501 // If the bitcast doesn't change the type, just use the operand value.
502 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000503 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000504 if (Reg == 0)
505 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000506 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000507 return true;
508 }
509
510 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Anderson6336b702008-08-27 18:58:30 +0000511 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
512 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000513
514 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
515 DstVT == MVT::Other || !DstVT.isSimple() ||
516 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
517 // Unhandled type. Halt "fast" selection and bail.
518 return false;
519
Dan Gohman3df24e62008-09-03 23:12:08 +0000520 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000521 if (Op0 == 0)
522 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000523 return false;
524
Dan Gohmanad368ac2008-08-27 18:10:19 +0000525 // First, try to perform the bitcast by inserting a reg-reg copy.
526 unsigned ResultReg = 0;
527 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
528 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
529 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
530 ResultReg = createResultReg(DstClass);
531
532 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
533 Op0, DstClass, SrcClass);
534 if (!InsertedCopy)
535 ResultReg = 0;
536 }
537
538 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
539 if (!ResultReg)
540 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
541 ISD::BIT_CONVERT, Op0);
542
543 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000544 return false;
545
Dan Gohman3df24e62008-09-03 23:12:08 +0000546 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000547 return true;
548}
549
Dan Gohman3df24e62008-09-03 23:12:08 +0000550bool
551FastISel::SelectInstruction(Instruction *I) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000552 return SelectOperator(I, I->getOpcode());
553}
554
Dan Gohmand98d6202008-10-02 22:15:21 +0000555/// FastEmitBranch - Emit an unconditional branch to the given block,
556/// unless it is the immediate (fall-through) successor, and update
557/// the CFG.
558void
559FastISel::FastEmitBranch(MachineBasicBlock *MSucc) {
560 MachineFunction::iterator NextMBB =
561 next(MachineFunction::iterator(MBB));
562
563 if (MBB->isLayoutSuccessor(MSucc)) {
564 // The unconditional fall-through case, which needs no instructions.
565 } else {
566 // The unconditional branch case.
567 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
568 }
569 MBB->addSuccessor(MSucc);
570}
571
Dan Gohman40b189e2008-09-05 18:18:20 +0000572bool
573FastISel::SelectOperator(User *I, unsigned Opcode) {
574 switch (Opcode) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000575 case Instruction::Add: {
576 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
577 return SelectBinaryOp(I, Opc);
578 }
579 case Instruction::Sub: {
580 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
581 return SelectBinaryOp(I, Opc);
582 }
583 case Instruction::Mul: {
584 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
585 return SelectBinaryOp(I, Opc);
586 }
587 case Instruction::SDiv:
588 return SelectBinaryOp(I, ISD::SDIV);
589 case Instruction::UDiv:
590 return SelectBinaryOp(I, ISD::UDIV);
591 case Instruction::FDiv:
592 return SelectBinaryOp(I, ISD::FDIV);
593 case Instruction::SRem:
594 return SelectBinaryOp(I, ISD::SREM);
595 case Instruction::URem:
596 return SelectBinaryOp(I, ISD::UREM);
597 case Instruction::FRem:
598 return SelectBinaryOp(I, ISD::FREM);
599 case Instruction::Shl:
600 return SelectBinaryOp(I, ISD::SHL);
601 case Instruction::LShr:
602 return SelectBinaryOp(I, ISD::SRL);
603 case Instruction::AShr:
604 return SelectBinaryOp(I, ISD::SRA);
605 case Instruction::And:
606 return SelectBinaryOp(I, ISD::AND);
607 case Instruction::Or:
608 return SelectBinaryOp(I, ISD::OR);
609 case Instruction::Xor:
610 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000611
Dan Gohman3df24e62008-09-03 23:12:08 +0000612 case Instruction::GetElementPtr:
613 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000614
Dan Gohman3df24e62008-09-03 23:12:08 +0000615 case Instruction::Br: {
616 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000617
Dan Gohman3df24e62008-09-03 23:12:08 +0000618 if (BI->isUnconditional()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000619 BasicBlock *LLVMSucc = BI->getSuccessor(0);
620 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
Dan Gohmand98d6202008-10-02 22:15:21 +0000621 FastEmitBranch(MSucc);
Dan Gohman3df24e62008-09-03 23:12:08 +0000622 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000623 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000624
625 // Conditional branches are not handed yet.
626 // Halt "fast" selection and bail.
627 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000628 }
629
Dan Gohman087c8502008-09-05 01:08:41 +0000630 case Instruction::Unreachable:
631 // Nothing to emit.
632 return true;
633
Dan Gohman3df24e62008-09-03 23:12:08 +0000634 case Instruction::PHI:
635 // PHI nodes are already emitted.
636 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000637
638 case Instruction::Alloca:
639 // FunctionLowering has the static-sized case covered.
640 if (StaticAllocaMap.count(cast<AllocaInst>(I)))
641 return true;
642
643 // Dynamic-sized alloca is not handled yet.
644 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000645
Dan Gohman33134c42008-09-25 17:05:24 +0000646 case Instruction::Call:
647 return SelectCall(I);
648
Dan Gohman3df24e62008-09-03 23:12:08 +0000649 case Instruction::BitCast:
650 return SelectBitCast(I);
651
652 case Instruction::FPToSI:
653 return SelectCast(I, ISD::FP_TO_SINT);
654 case Instruction::ZExt:
655 return SelectCast(I, ISD::ZERO_EXTEND);
656 case Instruction::SExt:
657 return SelectCast(I, ISD::SIGN_EXTEND);
658 case Instruction::Trunc:
659 return SelectCast(I, ISD::TRUNCATE);
660 case Instruction::SIToFP:
661 return SelectCast(I, ISD::SINT_TO_FP);
662
663 case Instruction::IntToPtr: // Deliberate fall-through.
664 case Instruction::PtrToInt: {
665 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
666 MVT DstVT = TLI.getValueType(I->getType());
667 if (DstVT.bitsGT(SrcVT))
668 return SelectCast(I, ISD::ZERO_EXTEND);
669 if (DstVT.bitsLT(SrcVT))
670 return SelectCast(I, ISD::TRUNCATE);
671 unsigned Reg = getRegForValue(I->getOperand(0));
672 if (Reg == 0) return false;
673 UpdateValueMap(I, Reg);
674 return true;
675 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000676
Dan Gohman3df24e62008-09-03 23:12:08 +0000677 default:
678 // Unhandled instruction. Halt "fast" selection and bail.
679 return false;
680 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000681}
682
Dan Gohman3df24e62008-09-03 23:12:08 +0000683FastISel::FastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000684 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +0000685 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +0000686 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000687 DenseMap<const AllocaInst *, int> &am
688#ifndef NDEBUG
689 , SmallSet<Instruction*, 8> &cil
690#endif
691 )
Dan Gohman3df24e62008-09-03 23:12:08 +0000692 : MBB(0),
693 ValueMap(vm),
694 MBBMap(bm),
Dan Gohman0586d912008-09-10 20:11:02 +0000695 StaticAllocaMap(am),
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000696#ifndef NDEBUG
697 CatchInfoLost(cil),
698#endif
Dan Gohman3df24e62008-09-03 23:12:08 +0000699 MF(mf),
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000700 MMI(mmi),
Dan Gohman3df24e62008-09-03 23:12:08 +0000701 MRI(MF.getRegInfo()),
Dan Gohman0586d912008-09-10 20:11:02 +0000702 MFI(*MF.getFrameInfo()),
703 MCP(*MF.getConstantPool()),
Dan Gohman3df24e62008-09-03 23:12:08 +0000704 TM(MF.getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000705 TD(*TM.getTargetData()),
706 TII(*TM.getInstrInfo()),
707 TLI(*TM.getTargetLowering()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000708}
709
Dan Gohmane285a742008-08-14 21:51:29 +0000710FastISel::~FastISel() {}
711
Evan Cheng36fd9412008-09-02 21:59:13 +0000712unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType,
713 ISD::NodeType) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000714 return 0;
715}
716
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000717unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
718 ISD::NodeType, unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000719 return 0;
720}
721
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000722unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
723 ISD::NodeType, unsigned /*Op0*/,
724 unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000725 return 0;
726}
727
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000728unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
729 ISD::NodeType, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000730 return 0;
731}
732
Dan Gohman10df0fa2008-08-27 01:09:54 +0000733unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
734 ISD::NodeType, ConstantFP * /*FPImm*/) {
735 return 0;
736}
737
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000738unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
739 ISD::NodeType, unsigned /*Op0*/,
740 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000741 return 0;
742}
743
Dan Gohman10df0fa2008-08-27 01:09:54 +0000744unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
745 ISD::NodeType, unsigned /*Op0*/,
746 ConstantFP * /*FPImm*/) {
747 return 0;
748}
749
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000750unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
751 ISD::NodeType,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000752 unsigned /*Op0*/, unsigned /*Op1*/,
753 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000754 return 0;
755}
756
757/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
758/// to emit an instruction with an immediate operand using FastEmit_ri.
759/// If that fails, it materializes the immediate into a register and try
760/// FastEmit_rr instead.
761unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000762 unsigned Op0, uint64_t Imm,
763 MVT::SimpleValueType ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000764 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman151ed612008-08-27 18:15:05 +0000765 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000766 if (ResultReg != 0)
767 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000768 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000769 if (MaterialReg == 0)
770 return 0;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000771 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000772}
773
Dan Gohman10df0fa2008-08-27 01:09:54 +0000774/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
775/// to emit an instruction with a floating-point immediate operand using
776/// FastEmit_rf. If that fails, it materializes the immediate into a register
777/// and try FastEmit_rr instead.
778unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
779 unsigned Op0, ConstantFP *FPImm,
780 MVT::SimpleValueType ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000781 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohman151ed612008-08-27 18:15:05 +0000782 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000783 if (ResultReg != 0)
784 return ResultReg;
785
786 // Materialize the constant in a register.
787 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
788 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000789 // If the target doesn't have a way to directly enter a floating-point
790 // value into a register, use an alternate approach.
791 // TODO: The current approach only supports floating-point constants
792 // that can be constructed by conversion from integer values. This should
793 // be replaced by code that creates a load from a constant-pool entry,
794 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000795 const APFloat &Flt = FPImm->getValueAPF();
796 MVT IntVT = TLI.getPointerTy();
797
798 uint64_t x[2];
799 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000800 bool isExact;
801 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
802 APFloat::rmTowardZero, &isExact);
803 if (!isExact)
Dan Gohman10df0fa2008-08-27 01:09:54 +0000804 return 0;
805 APInt IntVal(IntBitWidth, 2, x);
806
807 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
808 ISD::Constant, IntVal.getZExtValue());
809 if (IntegerReg == 0)
810 return 0;
811 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
812 ISD::SINT_TO_FP, IntegerReg);
813 if (MaterialReg == 0)
814 return 0;
815 }
816 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
817}
818
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000819unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
820 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000821}
822
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000823unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000824 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000825 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000826 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000827
Dan Gohmanfd903942008-08-20 23:53:10 +0000828 BuildMI(MBB, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000829 return ResultReg;
830}
831
832unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
833 const TargetRegisterClass *RC,
834 unsigned Op0) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000835 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000836 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000837
Evan Cheng5960e4e2008-09-08 08:38:20 +0000838 if (II.getNumDefs() >= 1)
839 BuildMI(MBB, II, ResultReg).addReg(Op0);
840 else {
841 BuildMI(MBB, II).addReg(Op0);
842 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
843 II.ImplicitDefs[0], RC, RC);
844 if (!InsertedCopy)
845 ResultReg = 0;
846 }
847
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000848 return ResultReg;
849}
850
851unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
852 const TargetRegisterClass *RC,
853 unsigned Op0, unsigned Op1) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000854 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000855 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000856
Evan Cheng5960e4e2008-09-08 08:38:20 +0000857 if (II.getNumDefs() >= 1)
858 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
859 else {
860 BuildMI(MBB, II).addReg(Op0).addReg(Op1);
861 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
862 II.ImplicitDefs[0], RC, RC);
863 if (!InsertedCopy)
864 ResultReg = 0;
865 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000866 return ResultReg;
867}
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000868
869unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
870 const TargetRegisterClass *RC,
871 unsigned Op0, uint64_t Imm) {
872 unsigned ResultReg = createResultReg(RC);
873 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
874
Evan Cheng5960e4e2008-09-08 08:38:20 +0000875 if (II.getNumDefs() >= 1)
876 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
877 else {
878 BuildMI(MBB, II).addReg(Op0).addImm(Imm);
879 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
880 II.ImplicitDefs[0], RC, RC);
881 if (!InsertedCopy)
882 ResultReg = 0;
883 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000884 return ResultReg;
885}
886
Dan Gohman10df0fa2008-08-27 01:09:54 +0000887unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
888 const TargetRegisterClass *RC,
889 unsigned Op0, ConstantFP *FPImm) {
890 unsigned ResultReg = createResultReg(RC);
891 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
892
Evan Cheng5960e4e2008-09-08 08:38:20 +0000893 if (II.getNumDefs() >= 1)
894 BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
895 else {
896 BuildMI(MBB, II).addReg(Op0).addFPImm(FPImm);
897 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
898 II.ImplicitDefs[0], RC, RC);
899 if (!InsertedCopy)
900 ResultReg = 0;
901 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000902 return ResultReg;
903}
904
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000905unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
906 const TargetRegisterClass *RC,
907 unsigned Op0, unsigned Op1, uint64_t Imm) {
908 unsigned ResultReg = createResultReg(RC);
909 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
910
Evan Cheng5960e4e2008-09-08 08:38:20 +0000911 if (II.getNumDefs() >= 1)
912 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
913 else {
914 BuildMI(MBB, II).addReg(Op0).addReg(Op1).addImm(Imm);
915 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
916 II.ImplicitDefs[0], RC, RC);
917 if (!InsertedCopy)
918 ResultReg = 0;
919 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000920 return ResultReg;
921}
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000922
923unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
924 const TargetRegisterClass *RC,
925 uint64_t Imm) {
926 unsigned ResultReg = createResultReg(RC);
927 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
928
Evan Cheng5960e4e2008-09-08 08:38:20 +0000929 if (II.getNumDefs() >= 1)
930 BuildMI(MBB, II, ResultReg).addImm(Imm);
931 else {
932 BuildMI(MBB, II).addImm(Imm);
933 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
934 II.ImplicitDefs[0], RC, RC);
935 if (!InsertedCopy)
936 ResultReg = 0;
937 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000938 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +0000939}
Owen Anderson8970f002008-08-27 22:30:02 +0000940
Owen Anderson40a468f2008-08-28 17:47:37 +0000941unsigned FastISel::FastEmitInst_extractsubreg(unsigned Op0, uint32_t Idx) {
942 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +0000943 const TargetRegisterClass* SRC = *(RC->subregclasses_begin()+Idx-1);
944
945 unsigned ResultReg = createResultReg(SRC);
946 const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
947
Evan Cheng5960e4e2008-09-08 08:38:20 +0000948 if (II.getNumDefs() >= 1)
949 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Idx);
950 else {
951 BuildMI(MBB, II).addReg(Op0).addImm(Idx);
952 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
953 II.ImplicitDefs[0], RC, RC);
954 if (!InsertedCopy)
955 ResultReg = 0;
956 }
Owen Anderson8970f002008-08-27 22:30:02 +0000957 return ResultReg;
958}