blob: 6107c8cb7a689576c90f9524f8be30af0fd5161e [file] [log] [blame]
Dan Gohman3b172f12010-04-22 20:06:42 +00001//===-- FastISel.cpp - Implementation of the FastISel class ---------------===//
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00002//
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"
Devang Patel83489bb2009-01-13 00:35:13 +000050#include "llvm/Analysis/DebugInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000051#include "llvm/Target/TargetData.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000052#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000053#include "llvm/Target/TargetLowering.h"
Dan Gohmanbb466332008-08-20 21:05:57 +000054#include "llvm/Target/TargetMachine.h"
Dan Gohmanba5be5c2010-04-20 15:00:41 +000055#include "llvm/Support/ErrorHandling.h"
Dan Gohman66336ed2009-11-23 17:42:46 +000056#include "FunctionLoweringInfo.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000057using namespace llvm;
58
Dan Gohman46510a72010-04-15 01:51:59 +000059unsigned FastISel::getRegForValue(const Value *V) {
Owen Andersone50ed302009-08-10 22:56:29 +000060 EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
Dan Gohman4fd55282009-04-07 20:40:11 +000061 // Don't handle non-simple values in FastISel.
62 if (!RealVT.isSimple())
63 return 0;
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +000064
65 // Ignore illegal types. We must do this before looking up the value
66 // in ValueMap because Arguments are given virtual registers regardless
67 // of whether FastISel can handle them.
Owen Anderson825b72b2009-08-11 20:47:22 +000068 MVT VT = RealVT.getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +000069 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +000070 // Promote MVT::i1 to a legal type though, because it's common and easy.
71 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +000072 VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +000073 else
74 return 0;
75 }
76
Dan Gohman104e4ce2008-09-03 23:32:19 +000077 // Look up the value to see if we already have a register for it. We
78 // cache values defined by Instructions across blocks, and other values
79 // only locally. This is because Instructions already have the SSA
Dan Gohman5c9cf192010-01-12 04:30:26 +000080 // def-dominates-use requirement enforced.
Owen Anderson99aaf102008-09-03 17:37:03 +000081 if (ValueMap.count(V))
82 return ValueMap[V];
Dan Gohman104e4ce2008-09-03 23:32:19 +000083 unsigned Reg = LocalValueMap[V];
84 if (Reg != 0)
85 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +000086
Dan Gohman97c94b82010-05-06 00:02:14 +000087 // In bottom-up mode, just create the virtual register which will be used
88 // to hold the value. It will be materialized later.
89 if (IsBottomUp) {
90 Reg = createResultReg(TLI.getRegClassFor(VT));
91 if (isa<Instruction>(V))
92 ValueMap[V] = Reg;
93 else
94 LocalValueMap[V] = Reg;
95 return Reg;
96 }
97
Dan Gohman1fdc6142010-05-03 23:36:34 +000098 return materializeRegForValue(V, VT);
99}
100
101/// materializeRegForValue - Helper for getRegForVale. This function is
102/// called when the value isn't already available in a register and must
103/// be materialized with new instructions.
104unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
105 unsigned Reg = 0;
106
Dan Gohman46510a72010-04-15 01:51:59 +0000107 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000108 if (CI->getValue().getActiveBits() <= 64)
109 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +0000110 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000111 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +0000112 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman1e9e8c32008-10-07 22:03:27 +0000113 // Translate this as an integer zero so that it can be
114 // local-CSE'd with actual integer zeros.
Owen Anderson1d0be152009-08-13 21:58:54 +0000115 Reg =
116 getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
Dan Gohman46510a72010-04-15 01:51:59 +0000117 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman4183e312010-04-13 17:07:06 +0000118 // Try to emit the constant directly.
Dan Gohman104e4ce2008-09-03 23:32:19 +0000119 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000120
121 if (!Reg) {
Dan Gohman4183e312010-04-13 17:07:06 +0000122 // Try to emit the constant by using an integer constant with a cast.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000123 const APFloat &Flt = CF->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000124 EVT IntVT = TLI.getPointerTy();
Dan Gohmanad368ac2008-08-27 18:10:19 +0000125
126 uint64_t x[2];
127 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000128 bool isExact;
129 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
130 APFloat::rmTowardZero, &isExact);
131 if (isExact) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000132 APInt IntVal(IntBitWidth, 2, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000133
Owen Andersone922c022009-07-22 00:24:57 +0000134 unsigned IntegerReg =
Owen Andersoneed707b2009-07-24 23:12:02 +0000135 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000136 if (IntegerReg != 0)
137 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
138 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000139 }
Dan Gohman46510a72010-04-15 01:51:59 +0000140 } else if (const Operator *Op = dyn_cast<Operator>(V)) {
Dan Gohman32acbc12010-04-14 02:33:23 +0000141 if (!SelectOperator(Op, Op->getOpcode())) return 0;
142 Reg = LocalValueMap[Op];
Dan Gohman205d9252008-08-28 21:19:07 +0000143 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000144 Reg = createResultReg(TLI.getRegClassFor(VT));
Chris Lattner518bb532010-02-09 19:54:29 +0000145 BuildMI(MBB, DL, TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000146 }
Owen Andersond5d81a42008-09-03 17:51:57 +0000147
Dan Gohmandceffe62008-09-25 01:28:51 +0000148 // If target-independent code couldn't handle the value, give target-specific
149 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000150 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000151 Reg = TargetMaterializeConstant(cast<Constant>(V));
Owen Anderson6e607452008-09-05 23:36:01 +0000152
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000153 // Don't cache constant materializations in the general ValueMap.
154 // To do so would require tracking what uses they dominate.
Dan Gohmandceffe62008-09-25 01:28:51 +0000155 if (Reg != 0)
156 LocalValueMap[V] = Reg;
Dan Gohman104e4ce2008-09-03 23:32:19 +0000157 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000158}
159
Dan Gohman46510a72010-04-15 01:51:59 +0000160unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng59fbc802008-09-09 01:26:59 +0000161 // Look up the value to see if we already have a register for it. We
162 // cache values defined by Instructions across blocks, and other values
163 // only locally. This is because Instructions already have the SSA
Dan Gohman1fdc6142010-05-03 23:36:34 +0000164 // def-dominates-use requirement enforced.
Evan Cheng59fbc802008-09-09 01:26:59 +0000165 if (ValueMap.count(V))
166 return ValueMap[V];
167 return LocalValueMap[V];
168}
169
Owen Andersoncc54e762008-08-30 00:38:46 +0000170/// UpdateValueMap - Update the value map to include the new mapping for this
171/// instruction, or insert an extra copy to get the result in a previous
172/// determined register.
173/// NOTE: This is only necessary because we might select a block that uses
174/// a value before we select the block that defines the value. It might be
175/// possible to fix this by selecting blocks in reverse postorder.
Dan Gohman46510a72010-04-15 01:51:59 +0000176unsigned FastISel::UpdateValueMap(const Value *I, unsigned Reg) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000177 if (!isa<Instruction>(I)) {
178 LocalValueMap[I] = Reg;
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000179 return Reg;
Dan Gohman40b189e2008-09-05 18:18:20 +0000180 }
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000181
182 unsigned &AssignedReg = ValueMap[I];
183 if (AssignedReg == 0)
184 AssignedReg = Reg;
Chris Lattner36e39462009-04-12 07:46:30 +0000185 else if (Reg != AssignedReg) {
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000186 const TargetRegisterClass *RegClass = MRI.getRegClass(Reg);
187 TII.copyRegToReg(*MBB, MBB->end(), AssignedReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000188 Reg, RegClass, RegClass, DL);
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000189 }
190 return AssignedReg;
Owen Andersoncc54e762008-08-30 00:38:46 +0000191}
192
Dan Gohman46510a72010-04-15 01:51:59 +0000193unsigned FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000194 unsigned IdxN = getRegForValue(Idx);
195 if (IdxN == 0)
196 // Unhandled operand. Halt "fast" selection and bail.
197 return 0;
198
199 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Anderson766b5ef2009-08-11 21:59:30 +0000200 MVT PtrVT = TLI.getPointerTy();
Owen Andersone50ed302009-08-10 22:56:29 +0000201 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000202 if (IdxVT.bitsLT(PtrVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +0000203 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND, IdxN);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000204 else if (IdxVT.bitsGT(PtrVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +0000205 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE, IdxN);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000206 return IdxN;
207}
208
Dan Gohmanbdedd442008-08-20 00:11:48 +0000209/// SelectBinaryOp - Select and emit code for a binary operator instruction,
210/// which has an opcode which directly corresponds to the given ISD opcode.
211///
Dan Gohman46510a72010-04-15 01:51:59 +0000212bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000213 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000214 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmanbdedd442008-08-20 00:11:48 +0000215 // Unhandled type. Halt "fast" selection and bail.
216 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000217
Dan Gohmanb71fea22008-08-26 20:52:40 +0000218 // We only handle legal types. For example, on x86-32 the instruction
219 // selector contains all of the 64-bit instructions from x86-64,
220 // under the assumption that i64 won't be used if the target doesn't
221 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000222 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000223 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000224 // don't require additional zeroing, which makes them easy.
Owen Anderson825b72b2009-08-11 20:47:22 +0000225 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000226 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
227 ISDOpcode == ISD::XOR))
Owen Anderson23b9b192009-08-12 00:36:31 +0000228 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohman638c6832008-09-05 18:44:22 +0000229 else
230 return false;
231 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000232
Dan Gohman3df24e62008-09-03 23:12:08 +0000233 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000234 if (Op0 == 0)
235 // Unhandled operand. Halt "fast" selection and bail.
236 return false;
237
238 // Check if the second operand is a constant and handle it appropriately.
239 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000240 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
241 ISDOpcode, Op0, CI->getZExtValue());
242 if (ResultReg != 0) {
243 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000244 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000245 return true;
246 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000247 }
248
Dan Gohman10df0fa2008-08-27 01:09:54 +0000249 // Check if the second operand is a constant float.
250 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000251 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
252 ISDOpcode, Op0, CF);
253 if (ResultReg != 0) {
254 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000255 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000256 return true;
257 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000258 }
259
Dan Gohman3df24e62008-09-03 23:12:08 +0000260 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000261 if (Op1 == 0)
262 // Unhandled operand. Halt "fast" selection and bail.
263 return false;
264
Dan Gohmanad368ac2008-08-27 18:10:19 +0000265 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000266 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
267 ISDOpcode, Op0, Op1);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000268 if (ResultReg == 0)
269 // Target-specific code wasn't able to find a machine opcode for
270 // the given ISD opcode and type. Halt "fast" selection and bail.
271 return false;
272
Dan Gohman8014e862008-08-20 00:23:20 +0000273 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000274 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000275 return true;
276}
277
Dan Gohman46510a72010-04-15 01:51:59 +0000278bool FastISel::SelectGetElementPtr(const User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000279 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000280 if (N == 0)
281 // Unhandled operand. Halt "fast" selection and bail.
282 return false;
283
284 const Type *Ty = I->getOperand(0)->getType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 MVT VT = TLI.getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +0000286 for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
287 E = I->op_end(); OI != E; ++OI) {
288 const Value *Idx = *OI;
Evan Cheng83785c82008-08-20 22:45:34 +0000289 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
290 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
291 if (Field) {
292 // N = N + Offset
293 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
294 // FIXME: This can be optimized by combining the add with a
295 // subsequent one.
Dan Gohman7a0e6592008-08-21 17:25:26 +0000296 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000297 if (N == 0)
298 // Unhandled operand. Halt "fast" selection and bail.
299 return false;
300 }
301 Ty = StTy->getElementType(Field);
302 } else {
303 Ty = cast<SequentialType>(Ty)->getElementType();
304
305 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +0000306 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Evan Cheng83785c82008-08-20 22:45:34 +0000307 if (CI->getZExtValue() == 0) continue;
308 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +0000309 TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000310 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000311 if (N == 0)
312 // Unhandled operand. Halt "fast" selection and bail.
313 return false;
314 continue;
315 }
316
317 // N = N + Idx * ElementSize;
Duncan Sands777d2302009-05-09 07:06:46 +0000318 uint64_t ElementSize = TD.getTypeAllocSize(Ty);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000319 unsigned IdxN = getRegForGEPIndex(Idx);
Evan Cheng83785c82008-08-20 22:45:34 +0000320 if (IdxN == 0)
321 // Unhandled operand. Halt "fast" selection and bail.
322 return false;
323
Dan Gohman80bc6e22008-08-26 20:57:08 +0000324 if (ElementSize != 1) {
Dan Gohmanf93cf792008-08-21 17:37:05 +0000325 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000326 if (IdxN == 0)
327 // Unhandled operand. Halt "fast" selection and bail.
328 return false;
329 }
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000330 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000331 if (N == 0)
332 // Unhandled operand. Halt "fast" selection and bail.
333 return false;
334 }
335 }
336
337 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000338 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000339 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000340}
341
Dan Gohman46510a72010-04-15 01:51:59 +0000342bool FastISel::SelectCall(const User *I) {
343 const Function *F = cast<CallInst>(I)->getCalledFunction();
Dan Gohman33134c42008-09-25 17:05:24 +0000344 if (!F) return false;
345
Dan Gohman4183e312010-04-13 17:07:06 +0000346 // Handle selected intrinsic function calls.
Dan Gohman33134c42008-09-25 17:05:24 +0000347 unsigned IID = F->getIntrinsicID();
348 switch (IID) {
349 default: break;
Bill Wendling92c1e122009-02-13 02:16:35 +0000350 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +0000351 const DbgDeclareInst *DI = cast<DbgDeclareInst>(I);
Chris Lattnerd850ac72010-04-05 02:19:28 +0000352 if (!DIDescriptor::ValidDebugInfo(DI->getVariable(), CodeGenOpt::None) ||
Chris Lattnered3a8062010-04-05 06:05:26 +0000353 !MF.getMMI().hasDebugInfo())
Devang Patel7e1e31f2009-07-02 22:43:26 +0000354 return true;
355
Dan Gohman46510a72010-04-15 01:51:59 +0000356 const Value *Address = DI->getAddress();
Dale Johannesendc918562010-02-06 02:26:02 +0000357 if (!Address)
358 return true;
Dale Johannesen343b42e2010-04-07 01:15:14 +0000359 if (isa<UndefValue>(Address))
360 return true;
Dan Gohman46510a72010-04-15 01:51:59 +0000361 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
Devang Patel7e1e31f2009-07-02 22:43:26 +0000362 // Don't handle byval struct arguments or VLAs, for example.
Dale Johannesen7dc78402010-04-25 21:03:54 +0000363 // Note that if we have a byval struct argument, fast ISel is turned off;
364 // those are handled in SelectionDAGBuilder.
Devang Patel54fc4d62010-04-28 19:27:33 +0000365 if (AI) {
366 DenseMap<const AllocaInst*, int>::iterator SI =
367 StaticAllocaMap.find(AI);
368 if (SI == StaticAllocaMap.end()) break; // VLAs.
369 int FI = SI->second;
370 if (!DI->getDebugLoc().isUnknown())
371 MF.getMMI().setVariableDbgInfo(DI->getVariable(), FI, DI->getDebugLoc());
372 } else
373 // Building the map above is target independent. Generating DBG_VALUE
374 // inline is target dependent; do this now.
375 (void)TargetSelectInstruction(cast<Instruction>(I));
Dan Gohman33134c42008-09-25 17:05:24 +0000376 return true;
Bill Wendling92c1e122009-02-13 02:16:35 +0000377 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000378 case Intrinsic::dbg_value: {
Dale Johannesen343b42e2010-04-07 01:15:14 +0000379 // This form of DBG_VALUE is target-independent.
Dan Gohman46510a72010-04-15 01:51:59 +0000380 const DbgValueInst *DI = cast<DbgValueInst>(I);
Dale Johannesen45df7612010-02-26 20:01:55 +0000381 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohman46510a72010-04-15 01:51:59 +0000382 const Value *V = DI->getValue();
Dale Johannesen45df7612010-02-26 20:01:55 +0000383 if (!V) {
384 // Currently the optimizer can produce this; insert an undef to
385 // help debugging. Probably the optimizer should not do this.
386 BuildMI(MBB, DL, II).addReg(0U).addImm(DI->getOffset()).
387 addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000388 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dale Johannesen45df7612010-02-26 20:01:55 +0000389 BuildMI(MBB, DL, II).addImm(CI->getZExtValue()).addImm(DI->getOffset()).
390 addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000391 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dale Johannesen45df7612010-02-26 20:01:55 +0000392 BuildMI(MBB, DL, II).addFPImm(CF).addImm(DI->getOffset()).
393 addMetadata(DI->getVariable());
394 } else if (unsigned Reg = lookUpRegForValue(V)) {
395 BuildMI(MBB, DL, II).addReg(Reg, RegState::Debug).addImm(DI->getOffset()).
396 addMetadata(DI->getVariable());
397 } else {
398 // We can't yet handle anything else here because it would require
399 // generating code, thus altering codegen because of debug info.
400 // Insert an undef so we can see what we dropped.
401 BuildMI(MBB, DL, II).addReg(0U).addImm(DI->getOffset()).
402 addMetadata(DI->getVariable());
403 }
404 return true;
405 }
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000406 case Intrinsic::eh_exception: {
Owen Andersone50ed302009-08-10 22:56:29 +0000407 EVT VT = TLI.getValueType(I->getType());
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000408 switch (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)) {
409 default: break;
410 case TargetLowering::Expand: {
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000411 assert(MBB->isLandingPad() && "Call to eh.exception not in landing pad!");
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000412 unsigned Reg = TLI.getExceptionAddressRegister();
413 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
414 unsigned ResultReg = createResultReg(RC);
415 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000416 Reg, RC, RC, DL);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000417 assert(InsertedCopy && "Can't copy address registers!");
Evan Cheng24ac4082008-11-24 07:09:49 +0000418 InsertedCopy = InsertedCopy;
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000419 UpdateValueMap(I, ResultReg);
420 return true;
421 }
422 }
423 break;
424 }
Duncan Sandsb01bbdc2009-10-14 16:11:37 +0000425 case Intrinsic::eh_selector: {
Owen Andersone50ed302009-08-10 22:56:29 +0000426 EVT VT = TLI.getValueType(I->getType());
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000427 switch (TLI.getOperationAction(ISD::EHSELECTION, VT)) {
428 default: break;
429 case TargetLowering::Expand: {
Chris Lattnered3a8062010-04-05 06:05:26 +0000430 if (MBB->isLandingPad())
431 AddCatchInfo(*cast<CallInst>(I), &MF.getMMI(), MBB);
432 else {
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000433#ifndef NDEBUG
Chris Lattnered3a8062010-04-05 06:05:26 +0000434 CatchInfoLost.insert(cast<CallInst>(I));
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000435#endif
Chris Lattnered3a8062010-04-05 06:05:26 +0000436 // FIXME: Mark exception selector register as live in. Hack for PR1508.
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000437 unsigned Reg = TLI.getExceptionSelectorRegister();
Chris Lattnered3a8062010-04-05 06:05:26 +0000438 if (Reg) MBB->addLiveIn(Reg);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000439 }
Chris Lattnered3a8062010-04-05 06:05:26 +0000440
441 unsigned Reg = TLI.getExceptionSelectorRegister();
442 EVT SrcVT = TLI.getPointerTy();
443 const TargetRegisterClass *RC = TLI.getRegClassFor(SrcVT);
444 unsigned ResultReg = createResultReg(RC);
445 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, Reg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000446 RC, RC, DL);
Chris Lattnered3a8062010-04-05 06:05:26 +0000447 assert(InsertedCopy && "Can't copy address registers!");
448 InsertedCopy = InsertedCopy;
449
450 // Cast the register to the type of the selector.
451 if (SrcVT.bitsGT(MVT::i32))
452 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, ISD::TRUNCATE,
453 ResultReg);
454 else if (SrcVT.bitsLT(MVT::i32))
455 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32,
456 ISD::SIGN_EXTEND, ResultReg);
457 if (ResultReg == 0)
458 // Unhandled operand. Halt "fast" selection and bail.
459 return false;
460
461 UpdateValueMap(I, ResultReg);
462
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000463 return true;
464 }
465 }
466 break;
467 }
Dan Gohman33134c42008-09-25 17:05:24 +0000468 }
Dan Gohman4183e312010-04-13 17:07:06 +0000469
470 // An arbitrary call. Bail.
Dan Gohman33134c42008-09-25 17:05:24 +0000471 return false;
472}
473
Dan Gohman46510a72010-04-15 01:51:59 +0000474bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000475 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
476 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000477
Owen Anderson825b72b2009-08-11 20:47:22 +0000478 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
479 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersond0533c92008-08-26 23:46:32 +0000480 // Unhandled type. Halt "fast" selection and bail.
481 return false;
482
Dan Gohman474d3b32009-03-13 23:53:06 +0000483 // Check if the destination type is legal. Or as a special case,
484 // it may be i1 if we're doing a truncate because that's
485 // easy and somewhat common.
486 if (!TLI.isTypeLegal(DstVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000487 if (DstVT != MVT::i1 || Opcode != ISD::TRUNCATE)
Dan Gohman91b6f972008-10-03 01:28:47 +0000488 // Unhandled type. Halt "fast" selection and bail.
489 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000490
491 // Check if the source operand is legal. Or as a special case,
492 // it may be i1 if we're doing zero-extension because that's
493 // easy and somewhat common.
494 if (!TLI.isTypeLegal(SrcVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000495 if (SrcVT != MVT::i1 || Opcode != ISD::ZERO_EXTEND)
Dan Gohman474d3b32009-03-13 23:53:06 +0000496 // Unhandled type. Halt "fast" selection and bail.
497 return false;
498
Dan Gohman3df24e62008-09-03 23:12:08 +0000499 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000500 if (!InputReg)
501 // Unhandled operand. Halt "fast" selection and bail.
502 return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000503
504 // If the operand is i1, arrange for the high bits in the register to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +0000505 if (SrcVT == MVT::i1) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000506 SrcVT = TLI.getTypeToTransformTo(I->getContext(), SrcVT);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000507 InputReg = FastEmitZExtFromI1(SrcVT.getSimpleVT(), InputReg);
508 if (!InputReg)
509 return false;
510 }
Dan Gohman474d3b32009-03-13 23:53:06 +0000511 // If the result is i1, truncate to the target's type for i1 first.
Owen Anderson825b72b2009-08-11 20:47:22 +0000512 if (DstVT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +0000513 DstVT = TLI.getTypeToTransformTo(I->getContext(), DstVT);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000514
Owen Andersond0533c92008-08-26 23:46:32 +0000515 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
516 DstVT.getSimpleVT(),
517 Opcode,
518 InputReg);
519 if (!ResultReg)
520 return false;
521
Dan Gohman3df24e62008-09-03 23:12:08 +0000522 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000523 return true;
524}
525
Dan Gohman46510a72010-04-15 01:51:59 +0000526bool FastISel::SelectBitCast(const User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000527 // If the bitcast doesn't change the type, just use the operand value.
528 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000529 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000530 if (Reg == 0)
531 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000532 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000533 return true;
534 }
535
536 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Andersone50ed302009-08-10 22:56:29 +0000537 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
538 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000539
Owen Anderson825b72b2009-08-11 20:47:22 +0000540 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
541 DstVT == MVT::Other || !DstVT.isSimple() ||
Owen Andersond0533c92008-08-26 23:46:32 +0000542 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
543 // Unhandled type. Halt "fast" selection and bail.
544 return false;
545
Dan Gohman3df24e62008-09-03 23:12:08 +0000546 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000547 if (Op0 == 0)
548 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000549 return false;
550
Dan Gohmanad368ac2008-08-27 18:10:19 +0000551 // First, try to perform the bitcast by inserting a reg-reg copy.
552 unsigned ResultReg = 0;
553 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
554 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
555 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
556 ResultReg = createResultReg(DstClass);
557
558 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000559 Op0, DstClass, SrcClass, DL);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000560 if (!InsertedCopy)
561 ResultReg = 0;
562 }
563
564 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
565 if (!ResultReg)
566 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
567 ISD::BIT_CONVERT, Op0);
568
569 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000570 return false;
571
Dan Gohman3df24e62008-09-03 23:12:08 +0000572 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000573 return true;
574}
575
Dan Gohman3df24e62008-09-03 23:12:08 +0000576bool
Dan Gohman46510a72010-04-15 01:51:59 +0000577FastISel::SelectInstruction(const Instruction *I) {
Dan Gohmane8c92dd2010-04-23 15:29:50 +0000578 // Just before the terminator instruction, insert instructions to
579 // feed PHI nodes in successor blocks.
580 if (isa<TerminatorInst>(I))
581 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
582 return false;
583
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000584 DL = I->getDebugLoc();
585
Dan Gohman6e3ff372009-12-05 01:27:58 +0000586 // First, try doing target-independent selection.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000587 if (SelectOperator(I, I->getOpcode())) {
588 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000589 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000590 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000591
592 // Next, try calling the target to attempt to handle the instruction.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000593 if (TargetSelectInstruction(I)) {
594 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000595 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000596 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000597
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000598 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000599 return false;
Dan Gohman40b189e2008-09-05 18:18:20 +0000600}
601
Dan Gohmand98d6202008-10-02 22:15:21 +0000602/// FastEmitBranch - Emit an unconditional branch to the given block,
603/// unless it is the immediate (fall-through) successor, and update
604/// the CFG.
605void
606FastISel::FastEmitBranch(MachineBasicBlock *MSucc) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000607 if (MBB->isLayoutSuccessor(MSucc)) {
608 // The unconditional fall-through case, which needs no instructions.
609 } else {
610 // The unconditional branch case.
611 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
612 }
613 MBB->addSuccessor(MSucc);
614}
615
Dan Gohman3d45a852009-09-03 22:53:57 +0000616/// SelectFNeg - Emit an FNeg operation.
617///
618bool
Dan Gohman46510a72010-04-15 01:51:59 +0000619FastISel::SelectFNeg(const User *I) {
Dan Gohman3d45a852009-09-03 22:53:57 +0000620 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
621 if (OpReg == 0) return false;
622
Dan Gohman4a215a12009-09-11 00:36:43 +0000623 // If the target has ISD::FNEG, use it.
624 EVT VT = TLI.getValueType(I->getType());
625 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
626 ISD::FNEG, OpReg);
627 if (ResultReg != 0) {
628 UpdateValueMap(I, ResultReg);
629 return true;
630 }
631
Dan Gohman5e5abb72009-09-11 00:34:46 +0000632 // Bitcast the value to integer, twiddle the sign bit with xor,
633 // and then bitcast it back to floating-point.
Dan Gohman3d45a852009-09-03 22:53:57 +0000634 if (VT.getSizeInBits() > 64) return false;
Dan Gohman5e5abb72009-09-11 00:34:46 +0000635 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
636 if (!TLI.isTypeLegal(IntVT))
637 return false;
638
639 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
640 ISD::BIT_CONVERT, OpReg);
641 if (IntReg == 0)
642 return false;
643
644 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR, IntReg,
645 UINT64_C(1) << (VT.getSizeInBits()-1),
646 IntVT.getSimpleVT());
647 if (IntResultReg == 0)
648 return false;
649
650 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
651 ISD::BIT_CONVERT, IntResultReg);
Dan Gohman3d45a852009-09-03 22:53:57 +0000652 if (ResultReg == 0)
653 return false;
654
655 UpdateValueMap(I, ResultReg);
656 return true;
657}
658
Dan Gohman40b189e2008-09-05 18:18:20 +0000659bool
Dan Gohman46510a72010-04-15 01:51:59 +0000660FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000661 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000662 case Instruction::Add:
663 return SelectBinaryOp(I, ISD::ADD);
664 case Instruction::FAdd:
665 return SelectBinaryOp(I, ISD::FADD);
666 case Instruction::Sub:
667 return SelectBinaryOp(I, ISD::SUB);
668 case Instruction::FSub:
Dan Gohman3d45a852009-09-03 22:53:57 +0000669 // FNeg is currently represented in LLVM IR as a special case of FSub.
670 if (BinaryOperator::isFNeg(I))
671 return SelectFNeg(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000672 return SelectBinaryOp(I, ISD::FSUB);
673 case Instruction::Mul:
674 return SelectBinaryOp(I, ISD::MUL);
675 case Instruction::FMul:
676 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman3df24e62008-09-03 23:12:08 +0000677 case Instruction::SDiv:
678 return SelectBinaryOp(I, ISD::SDIV);
679 case Instruction::UDiv:
680 return SelectBinaryOp(I, ISD::UDIV);
681 case Instruction::FDiv:
682 return SelectBinaryOp(I, ISD::FDIV);
683 case Instruction::SRem:
684 return SelectBinaryOp(I, ISD::SREM);
685 case Instruction::URem:
686 return SelectBinaryOp(I, ISD::UREM);
687 case Instruction::FRem:
688 return SelectBinaryOp(I, ISD::FREM);
689 case Instruction::Shl:
690 return SelectBinaryOp(I, ISD::SHL);
691 case Instruction::LShr:
692 return SelectBinaryOp(I, ISD::SRL);
693 case Instruction::AShr:
694 return SelectBinaryOp(I, ISD::SRA);
695 case Instruction::And:
696 return SelectBinaryOp(I, ISD::AND);
697 case Instruction::Or:
698 return SelectBinaryOp(I, ISD::OR);
699 case Instruction::Xor:
700 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000701
Dan Gohman3df24e62008-09-03 23:12:08 +0000702 case Instruction::GetElementPtr:
703 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000704
Dan Gohman3df24e62008-09-03 23:12:08 +0000705 case Instruction::Br: {
Dan Gohman46510a72010-04-15 01:51:59 +0000706 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000707
Dan Gohman3df24e62008-09-03 23:12:08 +0000708 if (BI->isUnconditional()) {
Dan Gohman46510a72010-04-15 01:51:59 +0000709 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohman3df24e62008-09-03 23:12:08 +0000710 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
Dan Gohmand98d6202008-10-02 22:15:21 +0000711 FastEmitBranch(MSucc);
Dan Gohman3df24e62008-09-03 23:12:08 +0000712 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000713 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000714
715 // Conditional branches are not handed yet.
716 // Halt "fast" selection and bail.
717 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000718 }
719
Dan Gohman087c8502008-09-05 01:08:41 +0000720 case Instruction::Unreachable:
721 // Nothing to emit.
722 return true;
723
Dan Gohman0586d912008-09-10 20:11:02 +0000724 case Instruction::Alloca:
725 // FunctionLowering has the static-sized case covered.
726 if (StaticAllocaMap.count(cast<AllocaInst>(I)))
727 return true;
728
729 // Dynamic-sized alloca is not handled yet.
730 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000731
Dan Gohman33134c42008-09-25 17:05:24 +0000732 case Instruction::Call:
733 return SelectCall(I);
734
Dan Gohman3df24e62008-09-03 23:12:08 +0000735 case Instruction::BitCast:
736 return SelectBitCast(I);
737
738 case Instruction::FPToSI:
739 return SelectCast(I, ISD::FP_TO_SINT);
740 case Instruction::ZExt:
741 return SelectCast(I, ISD::ZERO_EXTEND);
742 case Instruction::SExt:
743 return SelectCast(I, ISD::SIGN_EXTEND);
744 case Instruction::Trunc:
745 return SelectCast(I, ISD::TRUNCATE);
746 case Instruction::SIToFP:
747 return SelectCast(I, ISD::SINT_TO_FP);
748
749 case Instruction::IntToPtr: // Deliberate fall-through.
750 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +0000751 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
752 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman3df24e62008-09-03 23:12:08 +0000753 if (DstVT.bitsGT(SrcVT))
754 return SelectCast(I, ISD::ZERO_EXTEND);
755 if (DstVT.bitsLT(SrcVT))
756 return SelectCast(I, ISD::TRUNCATE);
757 unsigned Reg = getRegForValue(I->getOperand(0));
758 if (Reg == 0) return false;
759 UpdateValueMap(I, Reg);
760 return true;
761 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000762
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000763 case Instruction::PHI:
764 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
765
Dan Gohman3df24e62008-09-03 23:12:08 +0000766 default:
767 // Unhandled instruction. Halt "fast" selection and bail.
768 return false;
769 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000770}
771
Dan Gohman3df24e62008-09-03 23:12:08 +0000772FastISel::FastISel(MachineFunction &mf,
773 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +0000774 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmanf81eca02010-04-22 20:46:50 +0000775 DenseMap<const AllocaInst *, int> &am,
776 std::vector<std::pair<MachineInstr*, unsigned> > &pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000777#ifndef NDEBUG
Dan Gohman25208642010-04-14 19:53:31 +0000778 , SmallSet<const Instruction *, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000779#endif
780 )
Dan Gohman3df24e62008-09-03 23:12:08 +0000781 : MBB(0),
782 ValueMap(vm),
783 MBBMap(bm),
Dan Gohman0586d912008-09-10 20:11:02 +0000784 StaticAllocaMap(am),
Dan Gohmanf81eca02010-04-22 20:46:50 +0000785 PHINodesToUpdate(pn),
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000786#ifndef NDEBUG
787 CatchInfoLost(cil),
788#endif
Dan Gohman3df24e62008-09-03 23:12:08 +0000789 MF(mf),
790 MRI(MF.getRegInfo()),
Dan Gohman0586d912008-09-10 20:11:02 +0000791 MFI(*MF.getFrameInfo()),
792 MCP(*MF.getConstantPool()),
Dan Gohman3df24e62008-09-03 23:12:08 +0000793 TM(MF.getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000794 TD(*TM.getTargetData()),
795 TII(*TM.getInstrInfo()),
Dan Gohmana7a0ed72010-05-05 23:58:35 +0000796 TLI(*TM.getTargetLowering()),
797 IsBottomUp(false) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000798}
799
Dan Gohmane285a742008-08-14 21:51:29 +0000800FastISel::~FastISel() {}
801
Owen Anderson825b72b2009-08-11 20:47:22 +0000802unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000803 unsigned) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000804 return 0;
805}
806
Owen Anderson825b72b2009-08-11 20:47:22 +0000807unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000808 unsigned, unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000809 return 0;
810}
811
Owen Anderson825b72b2009-08-11 20:47:22 +0000812unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000813 unsigned, unsigned /*Op0*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000814 unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000815 return 0;
816}
817
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000818unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000819 return 0;
820}
821
Owen Anderson825b72b2009-08-11 20:47:22 +0000822unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohman46510a72010-04-15 01:51:59 +0000823 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000824 return 0;
825}
826
Owen Anderson825b72b2009-08-11 20:47:22 +0000827unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000828 unsigned, unsigned /*Op0*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000829 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000830 return 0;
831}
832
Owen Anderson825b72b2009-08-11 20:47:22 +0000833unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000834 unsigned, unsigned /*Op0*/,
Dan Gohman46510a72010-04-15 01:51:59 +0000835 const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000836 return 0;
837}
838
Owen Anderson825b72b2009-08-11 20:47:22 +0000839unsigned FastISel::FastEmit_rri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000840 unsigned,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000841 unsigned /*Op0*/, unsigned /*Op1*/,
842 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000843 return 0;
844}
845
846/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
847/// to emit an instruction with an immediate operand using FastEmit_ri.
848/// If that fails, it materializes the immediate into a register and try
849/// FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000850unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000851 unsigned Op0, uint64_t Imm,
Owen Anderson825b72b2009-08-11 20:47:22 +0000852 MVT ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000853 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman151ed612008-08-27 18:15:05 +0000854 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000855 if (ResultReg != 0)
856 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000857 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000858 if (MaterialReg == 0)
859 return 0;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000860 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000861}
862
Dan Gohman10df0fa2008-08-27 01:09:54 +0000863/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
864/// to emit an instruction with a floating-point immediate operand using
865/// FastEmit_rf. If that fails, it materializes the immediate into a register
866/// and try FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000867unsigned FastISel::FastEmit_rf_(MVT VT, unsigned Opcode,
Dan Gohman46510a72010-04-15 01:51:59 +0000868 unsigned Op0, const ConstantFP *FPImm,
Owen Anderson825b72b2009-08-11 20:47:22 +0000869 MVT ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000870 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohman151ed612008-08-27 18:15:05 +0000871 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000872 if (ResultReg != 0)
873 return ResultReg;
874
875 // Materialize the constant in a register.
876 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
877 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000878 // If the target doesn't have a way to directly enter a floating-point
879 // value into a register, use an alternate approach.
880 // TODO: The current approach only supports floating-point constants
881 // that can be constructed by conversion from integer values. This should
882 // be replaced by code that creates a load from a constant-pool entry,
883 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000884 const APFloat &Flt = FPImm->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000885 EVT IntVT = TLI.getPointerTy();
Dan Gohman10df0fa2008-08-27 01:09:54 +0000886
887 uint64_t x[2];
888 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000889 bool isExact;
890 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
891 APFloat::rmTowardZero, &isExact);
892 if (!isExact)
Dan Gohman10df0fa2008-08-27 01:09:54 +0000893 return 0;
894 APInt IntVal(IntBitWidth, 2, x);
895
896 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
897 ISD::Constant, IntVal.getZExtValue());
898 if (IntegerReg == 0)
899 return 0;
900 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
901 ISD::SINT_TO_FP, IntegerReg);
902 if (MaterialReg == 0)
903 return 0;
904 }
905 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
906}
907
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000908unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
909 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000910}
911
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000912unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000913 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000914 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000915 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000916
Bill Wendling9bc96a52009-02-03 00:55:04 +0000917 BuildMI(MBB, DL, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000918 return ResultReg;
919}
920
921unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
922 const TargetRegisterClass *RC,
923 unsigned Op0) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000924 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000925 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000926
Evan Cheng5960e4e2008-09-08 08:38:20 +0000927 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +0000928 BuildMI(MBB, DL, II, ResultReg).addReg(Op0);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000929 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +0000930 BuildMI(MBB, DL, II).addReg(Op0);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000931 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000932 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000933 if (!InsertedCopy)
934 ResultReg = 0;
935 }
936
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000937 return ResultReg;
938}
939
940unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
941 const TargetRegisterClass *RC,
942 unsigned Op0, unsigned Op1) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000943 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000944 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000945
Evan Cheng5960e4e2008-09-08 08:38:20 +0000946 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +0000947 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addReg(Op1);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000948 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +0000949 BuildMI(MBB, DL, II).addReg(Op0).addReg(Op1);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000950 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000951 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000952 if (!InsertedCopy)
953 ResultReg = 0;
954 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000955 return ResultReg;
956}
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000957
958unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
959 const TargetRegisterClass *RC,
960 unsigned Op0, uint64_t Imm) {
961 unsigned ResultReg = createResultReg(RC);
962 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
963
Evan Cheng5960e4e2008-09-08 08:38:20 +0000964 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +0000965 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000966 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +0000967 BuildMI(MBB, DL, II).addReg(Op0).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000968 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000969 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000970 if (!InsertedCopy)
971 ResultReg = 0;
972 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000973 return ResultReg;
974}
975
Dan Gohman10df0fa2008-08-27 01:09:54 +0000976unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
977 const TargetRegisterClass *RC,
Dan Gohman46510a72010-04-15 01:51:59 +0000978 unsigned Op0, const ConstantFP *FPImm) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000979 unsigned ResultReg = createResultReg(RC);
980 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
981
Evan Cheng5960e4e2008-09-08 08:38:20 +0000982 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +0000983 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000984 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +0000985 BuildMI(MBB, DL, II).addReg(Op0).addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000986 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000987 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000988 if (!InsertedCopy)
989 ResultReg = 0;
990 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000991 return ResultReg;
992}
993
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000994unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
995 const TargetRegisterClass *RC,
996 unsigned Op0, unsigned Op1, uint64_t Imm) {
997 unsigned ResultReg = createResultReg(RC);
998 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
999
Evan Cheng5960e4e2008-09-08 08:38:20 +00001000 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +00001001 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001002 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +00001003 BuildMI(MBB, DL, II).addReg(Op0).addReg(Op1).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001004 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001005 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001006 if (!InsertedCopy)
1007 ResultReg = 0;
1008 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001009 return ResultReg;
1010}
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001011
1012unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1013 const TargetRegisterClass *RC,
1014 uint64_t Imm) {
1015 unsigned ResultReg = createResultReg(RC);
1016 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1017
Evan Cheng5960e4e2008-09-08 08:38:20 +00001018 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +00001019 BuildMI(MBB, DL, II, ResultReg).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001020 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +00001021 BuildMI(MBB, DL, II).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001022 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001023 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001024 if (!InsertedCopy)
1025 ResultReg = 0;
1026 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001027 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +00001028}
Owen Anderson8970f002008-08-27 22:30:02 +00001029
Owen Anderson825b72b2009-08-11 20:47:22 +00001030unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Evan Cheng536ab132009-01-22 09:10:11 +00001031 unsigned Op0, uint32_t Idx) {
Owen Anderson40a468f2008-08-28 17:47:37 +00001032 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +00001033
Evan Cheng536ab132009-01-22 09:10:11 +00001034 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Chris Lattner518bb532010-02-09 19:54:29 +00001035 const TargetInstrDesc &II = TII.get(TargetOpcode::EXTRACT_SUBREG);
Owen Anderson8970f002008-08-27 22:30:02 +00001036
Evan Cheng5960e4e2008-09-08 08:38:20 +00001037 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +00001038 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addImm(Idx);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001039 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +00001040 BuildMI(MBB, DL, II).addReg(Op0).addImm(Idx);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001041 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001042 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001043 if (!InsertedCopy)
1044 ResultReg = 0;
1045 }
Owen Anderson8970f002008-08-27 22:30:02 +00001046 return ResultReg;
1047}
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001048
1049/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1050/// with all but the least significant bit set to zero.
Owen Anderson825b72b2009-08-11 20:47:22 +00001051unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001052 return FastEmit_ri(VT, VT, ISD::AND, Op, 1);
1053}
Dan Gohmanf81eca02010-04-22 20:46:50 +00001054
1055/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1056/// Emit code to ensure constants are copied into registers when needed.
1057/// Remember the virtual registers that need to be added to the Machine PHI
1058/// nodes as input. We cannot just directly add them, because expansion
1059/// might result in multiple MBB's for one BB. As such, the start of the
1060/// BB might correspond to a different MBB than the end.
1061bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1062 const TerminatorInst *TI = LLVMBB->getTerminator();
1063
1064 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
1065 unsigned OrigNumPHINodesToUpdate = PHINodesToUpdate.size();
1066
1067 // Check successor nodes' PHI nodes that expect a constant to be available
1068 // from this block.
1069 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1070 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1071 if (!isa<PHINode>(SuccBB->begin())) continue;
1072 MachineBasicBlock *SuccMBB = MBBMap[SuccBB];
1073
1074 // If this terminator has multiple identical successors (common for
1075 // switches), only handle each succ once.
1076 if (!SuccsHandled.insert(SuccMBB)) continue;
1077
1078 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1079
1080 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1081 // nodes and Machine PHI nodes, but the incoming operands have not been
1082 // emitted yet.
1083 for (BasicBlock::const_iterator I = SuccBB->begin();
1084 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanfb95f892010-05-07 01:10:20 +00001085
Dan Gohmanf81eca02010-04-22 20:46:50 +00001086 // Ignore dead phi's.
1087 if (PN->use_empty()) continue;
1088
1089 // Only handle legal types. Two interesting things to note here. First,
1090 // by bailing out early, we may leave behind some dead instructions,
1091 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
1092 // own moves. Second, this check is necessary becuase FastISel doesn't
1093 // use CreateRegForValue to create registers, so it always creates
1094 // exactly one register for each non-void instruction.
1095 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1096 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
1097 // Promote MVT::i1.
1098 if (VT == MVT::i1)
1099 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1100 else {
1101 PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
1102 return false;
1103 }
1104 }
1105
1106 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1107
Dan Gohmanfb95f892010-05-07 01:10:20 +00001108 // Set the DebugLoc for the copy. Prefer the location of the operand
1109 // if there is one; use the location of the PHI otherwise.
1110 DL = PN->getDebugLoc();
1111 if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
1112 DL = Inst->getDebugLoc();
1113
Dan Gohmanf81eca02010-04-22 20:46:50 +00001114 unsigned Reg = getRegForValue(PHIOp);
1115 if (Reg == 0) {
1116 PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
1117 return false;
1118 }
1119 PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohmanfb95f892010-05-07 01:10:20 +00001120 DL = DebugLoc();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001121 }
1122 }
1123
1124 return true;
1125}