blob: b45a4fd53696305380b3d0adc4d929824d40716d [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 Gohman104e4ce2008-09-03 23:32:19 +000058 // Look up the value to see if we already have a register for it. We
59 // cache values defined by Instructions across blocks, and other values
60 // only locally. This is because Instructions already have the SSA
61 // def-dominatess-use requirement enforced.
Owen Anderson99aaf102008-09-03 17:37:03 +000062 if (ValueMap.count(V))
63 return ValueMap[V];
Dan Gohman104e4ce2008-09-03 23:32:19 +000064 unsigned Reg = LocalValueMap[V];
65 if (Reg != 0)
66 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +000067
68 MVT::SimpleValueType VT = TLI.getValueType(V->getType()).getSimpleVT();
Dan Gohman82116482008-09-10 21:01:08 +000069
70 // Ignore illegal types.
71 if (!TLI.isTypeLegal(VT)) {
72 // Promote MVT::i1 to a legal type though, because it's common and easy.
73 if (VT == MVT::i1)
74 VT = TLI.getTypeToTransformTo(VT).getSimpleVT();
75 else
76 return 0;
77 }
78
Dan Gohmanad368ac2008-08-27 18:10:19 +000079 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +000080 if (CI->getValue().getActiveBits() <= 64)
81 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +000082 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +000083 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +000084 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman1e9e8c32008-10-07 22:03:27 +000085 // Translate this as an integer zero so that it can be
86 // local-CSE'd with actual integer zeros.
87 Reg = getRegForValue(Constant::getNullValue(TD.getIntPtrType()));
Dan Gohmanad368ac2008-08-27 18:10:19 +000088 } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000089 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +000090
91 if (!Reg) {
92 const APFloat &Flt = CF->getValueAPF();
93 MVT IntVT = TLI.getPointerTy();
94
95 uint64_t x[2];
96 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +000097 bool isExact;
98 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
99 APFloat::rmTowardZero, &isExact);
100 if (isExact) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000101 APInt IntVal(IntBitWidth, 2, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000102
Dan Gohman1e9e8c32008-10-07 22:03:27 +0000103 unsigned IntegerReg = getRegForValue(ConstantInt::get(IntVal));
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000104 if (IntegerReg != 0)
105 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
106 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000107 }
Dan Gohman40b189e2008-09-05 18:18:20 +0000108 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
109 if (!SelectOperator(CE, CE->getOpcode())) return 0;
110 Reg = LocalValueMap[CE];
Dan Gohman205d9252008-08-28 21:19:07 +0000111 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000112 Reg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman205d9252008-08-28 21:19:07 +0000113 BuildMI(MBB, TII.get(TargetInstrInfo::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000114 }
Owen Andersond5d81a42008-09-03 17:51:57 +0000115
Dan Gohmandceffe62008-09-25 01:28:51 +0000116 // If target-independent code couldn't handle the value, give target-specific
117 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000118 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000119 Reg = TargetMaterializeConstant(cast<Constant>(V));
Owen Anderson6e607452008-09-05 23:36:01 +0000120
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000121 // Don't cache constant materializations in the general ValueMap.
122 // To do so would require tracking what uses they dominate.
Dan Gohmandceffe62008-09-25 01:28:51 +0000123 if (Reg != 0)
124 LocalValueMap[V] = Reg;
Dan Gohman104e4ce2008-09-03 23:32:19 +0000125 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000126}
127
Evan Cheng59fbc802008-09-09 01:26:59 +0000128unsigned FastISel::lookUpRegForValue(Value *V) {
129 // Look up the value to see if we already have a register for it. We
130 // cache values defined by Instructions across blocks, and other values
131 // only locally. This is because Instructions already have the SSA
132 // def-dominatess-use requirement enforced.
133 if (ValueMap.count(V))
134 return ValueMap[V];
135 return LocalValueMap[V];
136}
137
Owen Andersoncc54e762008-08-30 00:38:46 +0000138/// UpdateValueMap - Update the value map to include the new mapping for this
139/// instruction, or insert an extra copy to get the result in a previous
140/// determined register.
141/// NOTE: This is only necessary because we might select a block that uses
142/// a value before we select the block that defines the value. It might be
143/// possible to fix this by selecting blocks in reverse postorder.
Owen Anderson95267a12008-09-05 00:06:23 +0000144void FastISel::UpdateValueMap(Value* I, unsigned Reg) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000145 if (!isa<Instruction>(I)) {
146 LocalValueMap[I] = Reg;
147 return;
148 }
Owen Andersoncc54e762008-08-30 00:38:46 +0000149 if (!ValueMap.count(I))
150 ValueMap[I] = Reg;
151 else
Evan Chengf0991782008-09-07 09:04:52 +0000152 TII.copyRegToReg(*MBB, MBB->end(), ValueMap[I],
153 Reg, MRI.getRegClass(Reg), MRI.getRegClass(Reg));
Owen Andersoncc54e762008-08-30 00:38:46 +0000154}
155
Dan Gohmanbdedd442008-08-20 00:11:48 +0000156/// SelectBinaryOp - Select and emit code for a binary operator instruction,
157/// which has an opcode which directly corresponds to the given ISD opcode.
158///
Dan Gohman40b189e2008-09-05 18:18:20 +0000159bool FastISel::SelectBinaryOp(User *I, ISD::NodeType ISDOpcode) {
Dan Gohmanbdedd442008-08-20 00:11:48 +0000160 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
161 if (VT == MVT::Other || !VT.isSimple())
162 // Unhandled type. Halt "fast" selection and bail.
163 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000164
Dan Gohmanb71fea22008-08-26 20:52:40 +0000165 // We only handle legal types. For example, on x86-32 the instruction
166 // selector contains all of the 64-bit instructions from x86-64,
167 // under the assumption that i64 won't be used if the target doesn't
168 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000169 if (!TLI.isTypeLegal(VT)) {
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000170 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000171 // don't require additional zeroing, which makes them easy.
172 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000173 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
174 ISDOpcode == ISD::XOR))
Dan Gohman638c6832008-09-05 18:44:22 +0000175 VT = TLI.getTypeToTransformTo(VT);
176 else
177 return false;
178 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000179
Dan Gohman3df24e62008-09-03 23:12:08 +0000180 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000181 if (Op0 == 0)
182 // Unhandled operand. Halt "fast" selection and bail.
183 return false;
184
185 // Check if the second operand is a constant and handle it appropriately.
186 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000187 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
188 ISDOpcode, Op0, CI->getZExtValue());
189 if (ResultReg != 0) {
190 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000191 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000192 return true;
193 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000194 }
195
Dan Gohman10df0fa2008-08-27 01:09:54 +0000196 // Check if the second operand is a constant float.
197 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000198 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
199 ISDOpcode, Op0, CF);
200 if (ResultReg != 0) {
201 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000202 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000203 return true;
204 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000205 }
206
Dan Gohman3df24e62008-09-03 23:12:08 +0000207 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000208 if (Op1 == 0)
209 // Unhandled operand. Halt "fast" selection and bail.
210 return false;
211
Dan Gohmanad368ac2008-08-27 18:10:19 +0000212 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000213 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
214 ISDOpcode, Op0, Op1);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000215 if (ResultReg == 0)
216 // Target-specific code wasn't able to find a machine opcode for
217 // the given ISD opcode and type. Halt "fast" selection and bail.
218 return false;
219
Dan Gohman8014e862008-08-20 00:23:20 +0000220 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000221 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000222 return true;
223}
224
Dan Gohman40b189e2008-09-05 18:18:20 +0000225bool FastISel::SelectGetElementPtr(User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000226 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000227 if (N == 0)
228 // Unhandled operand. Halt "fast" selection and bail.
229 return false;
230
231 const Type *Ty = I->getOperand(0)->getType();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000232 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
Evan Cheng83785c82008-08-20 22:45:34 +0000233 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
234 OI != E; ++OI) {
235 Value *Idx = *OI;
236 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
237 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
238 if (Field) {
239 // N = N + Offset
240 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
241 // FIXME: This can be optimized by combining the add with a
242 // subsequent one.
Dan Gohman7a0e6592008-08-21 17:25:26 +0000243 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000244 if (N == 0)
245 // Unhandled operand. Halt "fast" selection and bail.
246 return false;
247 }
248 Ty = StTy->getElementType(Field);
249 } else {
250 Ty = cast<SequentialType>(Ty)->getElementType();
251
252 // If this is a constant subscript, handle it quickly.
253 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
254 if (CI->getZExtValue() == 0) continue;
255 uint64_t Offs =
256 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000257 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000258 if (N == 0)
259 // Unhandled operand. Halt "fast" selection and bail.
260 return false;
261 continue;
262 }
263
264 // N = N + Idx * ElementSize;
265 uint64_t ElementSize = TD.getABITypeSize(Ty);
Dan Gohman3df24e62008-09-03 23:12:08 +0000266 unsigned IdxN = getRegForValue(Idx);
Evan Cheng83785c82008-08-20 22:45:34 +0000267 if (IdxN == 0)
268 // Unhandled operand. Halt "fast" selection and bail.
269 return false;
270
271 // If the index is smaller or larger than intptr_t, truncate or extend
272 // it.
Evan Cheng2076aa82008-08-21 01:19:11 +0000273 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
Evan Cheng83785c82008-08-20 22:45:34 +0000274 if (IdxVT.bitsLT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000275 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000276 else if (IdxVT.bitsGT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000277 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000278 if (IdxN == 0)
279 // Unhandled operand. Halt "fast" selection and bail.
280 return false;
281
Dan Gohman80bc6e22008-08-26 20:57:08 +0000282 if (ElementSize != 1) {
Dan Gohmanf93cf792008-08-21 17:37:05 +0000283 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000284 if (IdxN == 0)
285 // Unhandled operand. Halt "fast" selection and bail.
286 return false;
287 }
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000288 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000289 if (N == 0)
290 // Unhandled operand. Halt "fast" selection and bail.
291 return false;
292 }
293 }
294
295 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000296 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000297 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000298}
299
Dan Gohman33134c42008-09-25 17:05:24 +0000300bool FastISel::SelectCall(User *I) {
301 Function *F = cast<CallInst>(I)->getCalledFunction();
302 if (!F) return false;
303
304 unsigned IID = F->getIntrinsicID();
305 switch (IID) {
306 default: break;
307 case Intrinsic::dbg_stoppoint: {
308 DbgStopPointInst *SPI = cast<DbgStopPointInst>(I);
309 if (MMI && SPI->getContext() && MMI->Verify(SPI->getContext())) {
310 DebugInfoDesc *DD = MMI->getDescFor(SPI->getContext());
311 assert(DD && "Not a debug information descriptor");
312 const CompileUnitDesc *CompileUnit = cast<CompileUnitDesc>(DD);
313 unsigned SrcFile = MMI->RecordSource(CompileUnit);
314 unsigned Line = SPI->getLine();
315 unsigned Col = SPI->getColumn();
316 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
317 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
318 BuildMI(MBB, II).addImm(ID);
319 }
320 return true;
321 }
322 case Intrinsic::dbg_region_start: {
323 DbgRegionStartInst *RSI = cast<DbgRegionStartInst>(I);
324 if (MMI && RSI->getContext() && MMI->Verify(RSI->getContext())) {
325 unsigned ID = MMI->RecordRegionStart(RSI->getContext());
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_end: {
332 DbgRegionEndInst *REI = cast<DbgRegionEndInst>(I);
333 if (MMI && REI->getContext() && MMI->Verify(REI->getContext())) {
334 unsigned ID = MMI->RecordRegionEnd(REI->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_func_start: {
341 if (!MMI) return true;
342 DbgFuncStartInst *FSI = cast<DbgFuncStartInst>(I);
343 Value *SP = FSI->getSubprogram();
344 if (SP && MMI->Verify(SP)) {
345 // llvm.dbg.func.start implicitly defines a dbg_stoppoint which is
346 // what (most?) gdb expects.
347 DebugInfoDesc *DD = MMI->getDescFor(SP);
348 assert(DD && "Not a debug information descriptor");
349 SubprogramDesc *Subprogram = cast<SubprogramDesc>(DD);
350 const CompileUnitDesc *CompileUnit = Subprogram->getFile();
351 unsigned SrcFile = MMI->RecordSource(CompileUnit);
Devang Patele75808c2008-11-06 21:28:20 +0000352 // Record the source line but does not create a label for the normal
353 // function start. It will be emitted at asm emission time. However,
354 // create a label if this is a beginning of inlined function.
355 unsigned LabelID = MMI->RecordSourceLine(Subprogram->getLine(), 0, SrcFile);
356 if (MMI->getSourceLines().size() != 1) {
357 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
358 BuildMI(MBB, II).addImm(LabelID);
359 }
Dan Gohman33134c42008-09-25 17:05:24 +0000360 }
361 return true;
362 }
363 case Intrinsic::dbg_declare: {
364 DbgDeclareInst *DI = cast<DbgDeclareInst>(I);
365 Value *Variable = DI->getVariable();
366 if (MMI && Variable && MMI->Verify(Variable)) {
367 // Determine the address of the declared object.
368 Value *Address = DI->getAddress();
369 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
370 Address = BCI->getOperand(0);
371 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
372 // Don't handle byval struct arguments, for example.
373 if (!AI) break;
374 DenseMap<const AllocaInst*, int>::iterator SI =
375 StaticAllocaMap.find(AI);
376 assert(SI != StaticAllocaMap.end() && "Invalid dbg.declare!");
377 int FI = SI->second;
378
379 // Determine the debug globalvariable.
380 GlobalValue *GV = cast<GlobalVariable>(Variable);
381
382 // Build the DECLARE instruction.
383 const TargetInstrDesc &II = TII.get(TargetInstrInfo::DECLARE);
384 BuildMI(MBB, II).addFrameIndex(FI).addGlobalAddress(GV);
385 }
386 return true;
387 }
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000388 case Intrinsic::eh_exception: {
389 MVT VT = TLI.getValueType(I->getType());
390 switch (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)) {
391 default: break;
392 case TargetLowering::Expand: {
393 if (!MBB->isLandingPad()) {
394 // FIXME: Mark exception register as live in. Hack for PR1508.
395 unsigned Reg = TLI.getExceptionAddressRegister();
396 if (Reg) MBB->addLiveIn(Reg);
397 }
398 unsigned Reg = TLI.getExceptionAddressRegister();
399 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
400 unsigned ResultReg = createResultReg(RC);
401 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
402 Reg, RC, RC);
403 assert(InsertedCopy && "Can't copy address registers!");
404 UpdateValueMap(I, ResultReg);
405 return true;
406 }
407 }
408 break;
409 }
410 case Intrinsic::eh_selector_i32:
411 case Intrinsic::eh_selector_i64: {
412 MVT VT = TLI.getValueType(I->getType());
413 switch (TLI.getOperationAction(ISD::EHSELECTION, VT)) {
414 default: break;
415 case TargetLowering::Expand: {
416 MVT VT = (IID == Intrinsic::eh_selector_i32 ?
417 MVT::i32 : MVT::i64);
418
419 if (MMI) {
420 if (MBB->isLandingPad())
421 AddCatchInfo(*cast<CallInst>(I), MMI, MBB);
422 else {
423#ifndef NDEBUG
424 CatchInfoLost.insert(cast<CallInst>(I));
425#endif
426 // FIXME: Mark exception selector register as live in. Hack for PR1508.
427 unsigned Reg = TLI.getExceptionSelectorRegister();
428 if (Reg) MBB->addLiveIn(Reg);
429 }
430
431 unsigned Reg = TLI.getExceptionSelectorRegister();
432 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
433 unsigned ResultReg = createResultReg(RC);
434 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
435 Reg, RC, RC);
436 assert(InsertedCopy && "Can't copy address registers!");
437 UpdateValueMap(I, ResultReg);
438 } else {
439 unsigned ResultReg =
440 getRegForValue(Constant::getNullValue(I->getType()));
441 UpdateValueMap(I, ResultReg);
442 }
443 return true;
444 }
445 }
446 break;
447 }
Dan Gohman33134c42008-09-25 17:05:24 +0000448 }
449 return false;
450}
451
Dan Gohman40b189e2008-09-05 18:18:20 +0000452bool FastISel::SelectCast(User *I, ISD::NodeType Opcode) {
Owen Anderson6336b702008-08-27 18:58:30 +0000453 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
454 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000455
456 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
457 DstVT == MVT::Other || !DstVT.isSimple() ||
Dan Gohman91b6f972008-10-03 01:28:47 +0000458 !TLI.isTypeLegal(DstVT))
Owen Andersond0533c92008-08-26 23:46:32 +0000459 // Unhandled type. Halt "fast" selection and bail.
460 return false;
461
Dan Gohman91b6f972008-10-03 01:28:47 +0000462 // Check if the source operand is legal. Or as a special case,
463 // it may be i1 if we're doing zero-extension because that's
464 // trivially easy and somewhat common.
465 if (!TLI.isTypeLegal(SrcVT)) {
466 if (SrcVT == MVT::i1 && Opcode == ISD::ZERO_EXTEND)
467 SrcVT = TLI.getTypeToTransformTo(SrcVT);
468 else
469 // Unhandled type. Halt "fast" selection and bail.
470 return false;
471 }
472
Dan Gohman3df24e62008-09-03 23:12:08 +0000473 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000474 if (!InputReg)
475 // Unhandled operand. Halt "fast" selection and bail.
476 return false;
477
478 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
479 DstVT.getSimpleVT(),
480 Opcode,
481 InputReg);
482 if (!ResultReg)
483 return false;
484
Dan Gohman3df24e62008-09-03 23:12:08 +0000485 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000486 return true;
487}
488
Dan Gohman40b189e2008-09-05 18:18:20 +0000489bool FastISel::SelectBitCast(User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000490 // If the bitcast doesn't change the type, just use the operand value.
491 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000492 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000493 if (Reg == 0)
494 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000495 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000496 return true;
497 }
498
499 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Anderson6336b702008-08-27 18:58:30 +0000500 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
501 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000502
503 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
504 DstVT == MVT::Other || !DstVT.isSimple() ||
505 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
506 // Unhandled type. Halt "fast" selection and bail.
507 return false;
508
Dan Gohman3df24e62008-09-03 23:12:08 +0000509 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000510 if (Op0 == 0)
511 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000512 return false;
513
Dan Gohmanad368ac2008-08-27 18:10:19 +0000514 // First, try to perform the bitcast by inserting a reg-reg copy.
515 unsigned ResultReg = 0;
516 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
517 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
518 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
519 ResultReg = createResultReg(DstClass);
520
521 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
522 Op0, DstClass, SrcClass);
523 if (!InsertedCopy)
524 ResultReg = 0;
525 }
526
527 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
528 if (!ResultReg)
529 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
530 ISD::BIT_CONVERT, Op0);
531
532 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000533 return false;
534
Dan Gohman3df24e62008-09-03 23:12:08 +0000535 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000536 return true;
537}
538
Dan Gohman3df24e62008-09-03 23:12:08 +0000539bool
540FastISel::SelectInstruction(Instruction *I) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000541 return SelectOperator(I, I->getOpcode());
542}
543
Dan Gohmand98d6202008-10-02 22:15:21 +0000544/// FastEmitBranch - Emit an unconditional branch to the given block,
545/// unless it is the immediate (fall-through) successor, and update
546/// the CFG.
547void
548FastISel::FastEmitBranch(MachineBasicBlock *MSucc) {
549 MachineFunction::iterator NextMBB =
550 next(MachineFunction::iterator(MBB));
551
552 if (MBB->isLayoutSuccessor(MSucc)) {
553 // The unconditional fall-through case, which needs no instructions.
554 } else {
555 // The unconditional branch case.
556 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
557 }
558 MBB->addSuccessor(MSucc);
559}
560
Dan Gohman40b189e2008-09-05 18:18:20 +0000561bool
562FastISel::SelectOperator(User *I, unsigned Opcode) {
563 switch (Opcode) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000564 case Instruction::Add: {
565 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
566 return SelectBinaryOp(I, Opc);
567 }
568 case Instruction::Sub: {
569 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
570 return SelectBinaryOp(I, Opc);
571 }
572 case Instruction::Mul: {
573 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
574 return SelectBinaryOp(I, Opc);
575 }
576 case Instruction::SDiv:
577 return SelectBinaryOp(I, ISD::SDIV);
578 case Instruction::UDiv:
579 return SelectBinaryOp(I, ISD::UDIV);
580 case Instruction::FDiv:
581 return SelectBinaryOp(I, ISD::FDIV);
582 case Instruction::SRem:
583 return SelectBinaryOp(I, ISD::SREM);
584 case Instruction::URem:
585 return SelectBinaryOp(I, ISD::UREM);
586 case Instruction::FRem:
587 return SelectBinaryOp(I, ISD::FREM);
588 case Instruction::Shl:
589 return SelectBinaryOp(I, ISD::SHL);
590 case Instruction::LShr:
591 return SelectBinaryOp(I, ISD::SRL);
592 case Instruction::AShr:
593 return SelectBinaryOp(I, ISD::SRA);
594 case Instruction::And:
595 return SelectBinaryOp(I, ISD::AND);
596 case Instruction::Or:
597 return SelectBinaryOp(I, ISD::OR);
598 case Instruction::Xor:
599 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000600
Dan Gohman3df24e62008-09-03 23:12:08 +0000601 case Instruction::GetElementPtr:
602 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000603
Dan Gohman3df24e62008-09-03 23:12:08 +0000604 case Instruction::Br: {
605 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000606
Dan Gohman3df24e62008-09-03 23:12:08 +0000607 if (BI->isUnconditional()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000608 BasicBlock *LLVMSucc = BI->getSuccessor(0);
609 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
Dan Gohmand98d6202008-10-02 22:15:21 +0000610 FastEmitBranch(MSucc);
Dan Gohman3df24e62008-09-03 23:12:08 +0000611 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000612 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000613
614 // Conditional branches are not handed yet.
615 // Halt "fast" selection and bail.
616 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000617 }
618
Dan Gohman087c8502008-09-05 01:08:41 +0000619 case Instruction::Unreachable:
620 // Nothing to emit.
621 return true;
622
Dan Gohman3df24e62008-09-03 23:12:08 +0000623 case Instruction::PHI:
624 // PHI nodes are already emitted.
625 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000626
627 case Instruction::Alloca:
628 // FunctionLowering has the static-sized case covered.
629 if (StaticAllocaMap.count(cast<AllocaInst>(I)))
630 return true;
631
632 // Dynamic-sized alloca is not handled yet.
633 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000634
Dan Gohman33134c42008-09-25 17:05:24 +0000635 case Instruction::Call:
636 return SelectCall(I);
637
Dan Gohman3df24e62008-09-03 23:12:08 +0000638 case Instruction::BitCast:
639 return SelectBitCast(I);
640
641 case Instruction::FPToSI:
642 return SelectCast(I, ISD::FP_TO_SINT);
643 case Instruction::ZExt:
644 return SelectCast(I, ISD::ZERO_EXTEND);
645 case Instruction::SExt:
646 return SelectCast(I, ISD::SIGN_EXTEND);
647 case Instruction::Trunc:
648 return SelectCast(I, ISD::TRUNCATE);
649 case Instruction::SIToFP:
650 return SelectCast(I, ISD::SINT_TO_FP);
651
652 case Instruction::IntToPtr: // Deliberate fall-through.
653 case Instruction::PtrToInt: {
654 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
655 MVT DstVT = TLI.getValueType(I->getType());
656 if (DstVT.bitsGT(SrcVT))
657 return SelectCast(I, ISD::ZERO_EXTEND);
658 if (DstVT.bitsLT(SrcVT))
659 return SelectCast(I, ISD::TRUNCATE);
660 unsigned Reg = getRegForValue(I->getOperand(0));
661 if (Reg == 0) return false;
662 UpdateValueMap(I, Reg);
663 return true;
664 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000665
Dan Gohman3df24e62008-09-03 23:12:08 +0000666 default:
667 // Unhandled instruction. Halt "fast" selection and bail.
668 return false;
669 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000670}
671
Dan Gohman3df24e62008-09-03 23:12:08 +0000672FastISel::FastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000673 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +0000674 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +0000675 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000676 DenseMap<const AllocaInst *, int> &am
677#ifndef NDEBUG
678 , SmallSet<Instruction*, 8> &cil
679#endif
680 )
Dan Gohman3df24e62008-09-03 23:12:08 +0000681 : MBB(0),
682 ValueMap(vm),
683 MBBMap(bm),
Dan Gohman0586d912008-09-10 20:11:02 +0000684 StaticAllocaMap(am),
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000685#ifndef NDEBUG
686 CatchInfoLost(cil),
687#endif
Dan Gohman3df24e62008-09-03 23:12:08 +0000688 MF(mf),
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000689 MMI(mmi),
Dan Gohman3df24e62008-09-03 23:12:08 +0000690 MRI(MF.getRegInfo()),
Dan Gohman0586d912008-09-10 20:11:02 +0000691 MFI(*MF.getFrameInfo()),
692 MCP(*MF.getConstantPool()),
Dan Gohman3df24e62008-09-03 23:12:08 +0000693 TM(MF.getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000694 TD(*TM.getTargetData()),
695 TII(*TM.getInstrInfo()),
696 TLI(*TM.getTargetLowering()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000697}
698
Dan Gohmane285a742008-08-14 21:51:29 +0000699FastISel::~FastISel() {}
700
Evan Cheng36fd9412008-09-02 21:59:13 +0000701unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType,
702 ISD::NodeType) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000703 return 0;
704}
705
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000706unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
707 ISD::NodeType, unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000708 return 0;
709}
710
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000711unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
712 ISD::NodeType, unsigned /*Op0*/,
713 unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000714 return 0;
715}
716
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000717unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
718 ISD::NodeType, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000719 return 0;
720}
721
Dan Gohman10df0fa2008-08-27 01:09:54 +0000722unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
723 ISD::NodeType, ConstantFP * /*FPImm*/) {
724 return 0;
725}
726
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000727unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
728 ISD::NodeType, unsigned /*Op0*/,
729 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000730 return 0;
731}
732
Dan Gohman10df0fa2008-08-27 01:09:54 +0000733unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
734 ISD::NodeType, unsigned /*Op0*/,
735 ConstantFP * /*FPImm*/) {
736 return 0;
737}
738
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000739unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
740 ISD::NodeType,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000741 unsigned /*Op0*/, unsigned /*Op1*/,
742 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000743 return 0;
744}
745
746/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
747/// to emit an instruction with an immediate operand using FastEmit_ri.
748/// If that fails, it materializes the immediate into a register and try
749/// FastEmit_rr instead.
750unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000751 unsigned Op0, uint64_t Imm,
752 MVT::SimpleValueType ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000753 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman151ed612008-08-27 18:15:05 +0000754 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000755 if (ResultReg != 0)
756 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000757 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000758 if (MaterialReg == 0)
759 return 0;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000760 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000761}
762
Dan Gohman10df0fa2008-08-27 01:09:54 +0000763/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
764/// to emit an instruction with a floating-point immediate operand using
765/// FastEmit_rf. If that fails, it materializes the immediate into a register
766/// and try FastEmit_rr instead.
767unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
768 unsigned Op0, ConstantFP *FPImm,
769 MVT::SimpleValueType ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000770 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohman151ed612008-08-27 18:15:05 +0000771 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000772 if (ResultReg != 0)
773 return ResultReg;
774
775 // Materialize the constant in a register.
776 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
777 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000778 // If the target doesn't have a way to directly enter a floating-point
779 // value into a register, use an alternate approach.
780 // TODO: The current approach only supports floating-point constants
781 // that can be constructed by conversion from integer values. This should
782 // be replaced by code that creates a load from a constant-pool entry,
783 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000784 const APFloat &Flt = FPImm->getValueAPF();
785 MVT IntVT = TLI.getPointerTy();
786
787 uint64_t x[2];
788 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000789 bool isExact;
790 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
791 APFloat::rmTowardZero, &isExact);
792 if (!isExact)
Dan Gohman10df0fa2008-08-27 01:09:54 +0000793 return 0;
794 APInt IntVal(IntBitWidth, 2, x);
795
796 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
797 ISD::Constant, IntVal.getZExtValue());
798 if (IntegerReg == 0)
799 return 0;
800 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
801 ISD::SINT_TO_FP, IntegerReg);
802 if (MaterialReg == 0)
803 return 0;
804 }
805 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
806}
807
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000808unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
809 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000810}
811
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000812unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000813 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000814 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000815 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000816
Dan Gohmanfd903942008-08-20 23:53:10 +0000817 BuildMI(MBB, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000818 return ResultReg;
819}
820
821unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
822 const TargetRegisterClass *RC,
823 unsigned Op0) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000824 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000825 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000826
Evan Cheng5960e4e2008-09-08 08:38:20 +0000827 if (II.getNumDefs() >= 1)
828 BuildMI(MBB, II, ResultReg).addReg(Op0);
829 else {
830 BuildMI(MBB, II).addReg(Op0);
831 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
832 II.ImplicitDefs[0], RC, RC);
833 if (!InsertedCopy)
834 ResultReg = 0;
835 }
836
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000837 return ResultReg;
838}
839
840unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
841 const TargetRegisterClass *RC,
842 unsigned Op0, unsigned Op1) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000843 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000844 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000845
Evan Cheng5960e4e2008-09-08 08:38:20 +0000846 if (II.getNumDefs() >= 1)
847 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
848 else {
849 BuildMI(MBB, II).addReg(Op0).addReg(Op1);
850 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
851 II.ImplicitDefs[0], RC, RC);
852 if (!InsertedCopy)
853 ResultReg = 0;
854 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000855 return ResultReg;
856}
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000857
858unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
859 const TargetRegisterClass *RC,
860 unsigned Op0, uint64_t Imm) {
861 unsigned ResultReg = createResultReg(RC);
862 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
863
Evan Cheng5960e4e2008-09-08 08:38:20 +0000864 if (II.getNumDefs() >= 1)
865 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
866 else {
867 BuildMI(MBB, II).addReg(Op0).addImm(Imm);
868 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
869 II.ImplicitDefs[0], RC, RC);
870 if (!InsertedCopy)
871 ResultReg = 0;
872 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000873 return ResultReg;
874}
875
Dan Gohman10df0fa2008-08-27 01:09:54 +0000876unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
877 const TargetRegisterClass *RC,
878 unsigned Op0, ConstantFP *FPImm) {
879 unsigned ResultReg = createResultReg(RC);
880 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
881
Evan Cheng5960e4e2008-09-08 08:38:20 +0000882 if (II.getNumDefs() >= 1)
883 BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
884 else {
885 BuildMI(MBB, II).addReg(Op0).addFPImm(FPImm);
886 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
887 II.ImplicitDefs[0], RC, RC);
888 if (!InsertedCopy)
889 ResultReg = 0;
890 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000891 return ResultReg;
892}
893
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000894unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
895 const TargetRegisterClass *RC,
896 unsigned Op0, unsigned Op1, uint64_t Imm) {
897 unsigned ResultReg = createResultReg(RC);
898 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
899
Evan Cheng5960e4e2008-09-08 08:38:20 +0000900 if (II.getNumDefs() >= 1)
901 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
902 else {
903 BuildMI(MBB, II).addReg(Op0).addReg(Op1).addImm(Imm);
904 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
905 II.ImplicitDefs[0], RC, RC);
906 if (!InsertedCopy)
907 ResultReg = 0;
908 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000909 return ResultReg;
910}
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000911
912unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
913 const TargetRegisterClass *RC,
914 uint64_t Imm) {
915 unsigned ResultReg = createResultReg(RC);
916 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
917
Evan Cheng5960e4e2008-09-08 08:38:20 +0000918 if (II.getNumDefs() >= 1)
919 BuildMI(MBB, II, ResultReg).addImm(Imm);
920 else {
921 BuildMI(MBB, II).addImm(Imm);
922 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
923 II.ImplicitDefs[0], RC, RC);
924 if (!InsertedCopy)
925 ResultReg = 0;
926 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000927 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +0000928}
Owen Anderson8970f002008-08-27 22:30:02 +0000929
Owen Anderson40a468f2008-08-28 17:47:37 +0000930unsigned FastISel::FastEmitInst_extractsubreg(unsigned Op0, uint32_t Idx) {
931 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +0000932 const TargetRegisterClass* SRC = *(RC->subregclasses_begin()+Idx-1);
933
934 unsigned ResultReg = createResultReg(SRC);
935 const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
936
Evan Cheng5960e4e2008-09-08 08:38:20 +0000937 if (II.getNumDefs() >= 1)
938 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Idx);
939 else {
940 BuildMI(MBB, II).addReg(Op0).addImm(Idx);
941 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
942 II.ImplicitDefs[0], RC, RC);
943 if (!InsertedCopy)
944 ResultReg = 0;
945 }
Owen Anderson8970f002008-08-27 22:30:02 +0000946 return ResultReg;
947}