blob: b4c38332691c79c1d9ecda4d6261430e4794fe6a [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 Gohman1fdc6142010-05-03 23:36:34 +000087 return materializeRegForValue(V, VT);
88}
89
90/// materializeRegForValue - Helper for getRegForVale. This function is
91/// called when the value isn't already available in a register and must
92/// be materialized with new instructions.
93unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
94 unsigned Reg = 0;
95
Dan Gohman46510a72010-04-15 01:51:59 +000096 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +000097 if (CI->getValue().getActiveBits() <= 64)
98 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +000099 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000100 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +0000101 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman1e9e8c32008-10-07 22:03:27 +0000102 // Translate this as an integer zero so that it can be
103 // local-CSE'd with actual integer zeros.
Owen Anderson1d0be152009-08-13 21:58:54 +0000104 Reg =
105 getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
Dan Gohman46510a72010-04-15 01:51:59 +0000106 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman4183e312010-04-13 17:07:06 +0000107 // Try to emit the constant directly.
Dan Gohman104e4ce2008-09-03 23:32:19 +0000108 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000109
110 if (!Reg) {
Dan Gohman4183e312010-04-13 17:07:06 +0000111 // Try to emit the constant by using an integer constant with a cast.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000112 const APFloat &Flt = CF->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000113 EVT IntVT = TLI.getPointerTy();
Dan Gohmanad368ac2008-08-27 18:10:19 +0000114
115 uint64_t x[2];
116 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000117 bool isExact;
118 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
119 APFloat::rmTowardZero, &isExact);
120 if (isExact) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000121 APInt IntVal(IntBitWidth, 2, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000122
Owen Andersone922c022009-07-22 00:24:57 +0000123 unsigned IntegerReg =
Owen Andersoneed707b2009-07-24 23:12:02 +0000124 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000125 if (IntegerReg != 0)
126 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
127 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000128 }
Dan Gohman46510a72010-04-15 01:51:59 +0000129 } else if (const Operator *Op = dyn_cast<Operator>(V)) {
Dan Gohman32acbc12010-04-14 02:33:23 +0000130 if (!SelectOperator(Op, Op->getOpcode())) return 0;
131 Reg = LocalValueMap[Op];
Dan Gohman205d9252008-08-28 21:19:07 +0000132 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000133 Reg = createResultReg(TLI.getRegClassFor(VT));
Chris Lattner518bb532010-02-09 19:54:29 +0000134 BuildMI(MBB, DL, TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000135 }
Owen Andersond5d81a42008-09-03 17:51:57 +0000136
Dan Gohmandceffe62008-09-25 01:28:51 +0000137 // If target-independent code couldn't handle the value, give target-specific
138 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000139 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000140 Reg = TargetMaterializeConstant(cast<Constant>(V));
Owen Anderson6e607452008-09-05 23:36:01 +0000141
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000142 // Don't cache constant materializations in the general ValueMap.
143 // To do so would require tracking what uses they dominate.
Dan Gohmandceffe62008-09-25 01:28:51 +0000144 if (Reg != 0)
145 LocalValueMap[V] = Reg;
Dan Gohman104e4ce2008-09-03 23:32:19 +0000146 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000147}
148
Dan Gohman46510a72010-04-15 01:51:59 +0000149unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng59fbc802008-09-09 01:26:59 +0000150 // Look up the value to see if we already have a register for it. We
151 // cache values defined by Instructions across blocks, and other values
152 // only locally. This is because Instructions already have the SSA
Dan Gohman1fdc6142010-05-03 23:36:34 +0000153 // def-dominates-use requirement enforced.
Evan Cheng59fbc802008-09-09 01:26:59 +0000154 if (ValueMap.count(V))
155 return ValueMap[V];
156 return LocalValueMap[V];
157}
158
Owen Andersoncc54e762008-08-30 00:38:46 +0000159/// UpdateValueMap - Update the value map to include the new mapping for this
160/// instruction, or insert an extra copy to get the result in a previous
161/// determined register.
162/// NOTE: This is only necessary because we might select a block that uses
163/// a value before we select the block that defines the value. It might be
164/// possible to fix this by selecting blocks in reverse postorder.
Dan Gohman46510a72010-04-15 01:51:59 +0000165unsigned FastISel::UpdateValueMap(const Value *I, unsigned Reg) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000166 if (!isa<Instruction>(I)) {
167 LocalValueMap[I] = Reg;
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000168 return Reg;
Dan Gohman40b189e2008-09-05 18:18:20 +0000169 }
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000170
171 unsigned &AssignedReg = ValueMap[I];
172 if (AssignedReg == 0)
173 AssignedReg = Reg;
Chris Lattner36e39462009-04-12 07:46:30 +0000174 else if (Reg != AssignedReg) {
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000175 const TargetRegisterClass *RegClass = MRI.getRegClass(Reg);
176 TII.copyRegToReg(*MBB, MBB->end(), AssignedReg,
177 Reg, RegClass, RegClass);
178 }
179 return AssignedReg;
Owen Andersoncc54e762008-08-30 00:38:46 +0000180}
181
Dan Gohman46510a72010-04-15 01:51:59 +0000182unsigned FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000183 unsigned IdxN = getRegForValue(Idx);
184 if (IdxN == 0)
185 // Unhandled operand. Halt "fast" selection and bail.
186 return 0;
187
188 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Anderson766b5ef2009-08-11 21:59:30 +0000189 MVT PtrVT = TLI.getPointerTy();
Owen Andersone50ed302009-08-10 22:56:29 +0000190 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000191 if (IdxVT.bitsLT(PtrVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +0000192 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND, IdxN);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000193 else if (IdxVT.bitsGT(PtrVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +0000194 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE, IdxN);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000195 return IdxN;
196}
197
Dan Gohmanbdedd442008-08-20 00:11:48 +0000198/// SelectBinaryOp - Select and emit code for a binary operator instruction,
199/// which has an opcode which directly corresponds to the given ISD opcode.
200///
Dan Gohman46510a72010-04-15 01:51:59 +0000201bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000202 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000203 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmanbdedd442008-08-20 00:11:48 +0000204 // Unhandled type. Halt "fast" selection and bail.
205 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000206
Dan Gohmanb71fea22008-08-26 20:52:40 +0000207 // We only handle legal types. For example, on x86-32 the instruction
208 // selector contains all of the 64-bit instructions from x86-64,
209 // under the assumption that i64 won't be used if the target doesn't
210 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000211 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000212 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000213 // don't require additional zeroing, which makes them easy.
Owen Anderson825b72b2009-08-11 20:47:22 +0000214 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000215 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
216 ISDOpcode == ISD::XOR))
Owen Anderson23b9b192009-08-12 00:36:31 +0000217 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohman638c6832008-09-05 18:44:22 +0000218 else
219 return false;
220 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000221
Dan Gohman3df24e62008-09-03 23:12:08 +0000222 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000223 if (Op0 == 0)
224 // Unhandled operand. Halt "fast" selection and bail.
225 return false;
226
227 // Check if the second operand is a constant and handle it appropriately.
228 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000229 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
230 ISDOpcode, Op0, CI->getZExtValue());
231 if (ResultReg != 0) {
232 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000233 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000234 return true;
235 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000236 }
237
Dan Gohman10df0fa2008-08-27 01:09:54 +0000238 // Check if the second operand is a constant float.
239 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000240 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
241 ISDOpcode, Op0, CF);
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 Gohman10df0fa2008-08-27 01:09:54 +0000247 }
248
Dan Gohman3df24e62008-09-03 23:12:08 +0000249 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000250 if (Op1 == 0)
251 // Unhandled operand. Halt "fast" selection and bail.
252 return false;
253
Dan Gohmanad368ac2008-08-27 18:10:19 +0000254 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000255 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
256 ISDOpcode, Op0, Op1);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000257 if (ResultReg == 0)
258 // Target-specific code wasn't able to find a machine opcode for
259 // the given ISD opcode and type. Halt "fast" selection and bail.
260 return false;
261
Dan Gohman8014e862008-08-20 00:23:20 +0000262 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000263 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000264 return true;
265}
266
Dan Gohman46510a72010-04-15 01:51:59 +0000267bool FastISel::SelectGetElementPtr(const User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000268 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000269 if (N == 0)
270 // Unhandled operand. Halt "fast" selection and bail.
271 return false;
272
273 const Type *Ty = I->getOperand(0)->getType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 MVT VT = TLI.getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +0000275 for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
276 E = I->op_end(); OI != E; ++OI) {
277 const Value *Idx = *OI;
Evan Cheng83785c82008-08-20 22:45:34 +0000278 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
279 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
280 if (Field) {
281 // N = N + Offset
282 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
283 // FIXME: This can be optimized by combining the add with a
284 // subsequent one.
Dan Gohman7a0e6592008-08-21 17:25:26 +0000285 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000286 if (N == 0)
287 // Unhandled operand. Halt "fast" selection and bail.
288 return false;
289 }
290 Ty = StTy->getElementType(Field);
291 } else {
292 Ty = cast<SequentialType>(Ty)->getElementType();
293
294 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +0000295 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Evan Cheng83785c82008-08-20 22:45:34 +0000296 if (CI->getZExtValue() == 0) continue;
297 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +0000298 TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000299 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000300 if (N == 0)
301 // Unhandled operand. Halt "fast" selection and bail.
302 return false;
303 continue;
304 }
305
306 // N = N + Idx * ElementSize;
Duncan Sands777d2302009-05-09 07:06:46 +0000307 uint64_t ElementSize = TD.getTypeAllocSize(Ty);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000308 unsigned IdxN = getRegForGEPIndex(Idx);
Evan Cheng83785c82008-08-20 22:45:34 +0000309 if (IdxN == 0)
310 // Unhandled operand. Halt "fast" selection and bail.
311 return false;
312
Dan Gohman80bc6e22008-08-26 20:57:08 +0000313 if (ElementSize != 1) {
Dan Gohmanf93cf792008-08-21 17:37:05 +0000314 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000315 if (IdxN == 0)
316 // Unhandled operand. Halt "fast" selection and bail.
317 return false;
318 }
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000319 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000320 if (N == 0)
321 // Unhandled operand. Halt "fast" selection and bail.
322 return false;
323 }
324 }
325
326 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000327 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000328 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000329}
330
Dan Gohman46510a72010-04-15 01:51:59 +0000331bool FastISel::SelectCall(const User *I) {
332 const Function *F = cast<CallInst>(I)->getCalledFunction();
Dan Gohman33134c42008-09-25 17:05:24 +0000333 if (!F) return false;
334
Dan Gohman4183e312010-04-13 17:07:06 +0000335 // Handle selected intrinsic function calls.
Dan Gohman33134c42008-09-25 17:05:24 +0000336 unsigned IID = F->getIntrinsicID();
337 switch (IID) {
338 default: break;
Bill Wendling92c1e122009-02-13 02:16:35 +0000339 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +0000340 const DbgDeclareInst *DI = cast<DbgDeclareInst>(I);
Chris Lattnerd850ac72010-04-05 02:19:28 +0000341 if (!DIDescriptor::ValidDebugInfo(DI->getVariable(), CodeGenOpt::None) ||
Chris Lattnered3a8062010-04-05 06:05:26 +0000342 !MF.getMMI().hasDebugInfo())
Devang Patel7e1e31f2009-07-02 22:43:26 +0000343 return true;
344
Dan Gohman46510a72010-04-15 01:51:59 +0000345 const Value *Address = DI->getAddress();
Dale Johannesendc918562010-02-06 02:26:02 +0000346 if (!Address)
347 return true;
Dale Johannesen343b42e2010-04-07 01:15:14 +0000348 if (isa<UndefValue>(Address))
349 return true;
Dan Gohman46510a72010-04-15 01:51:59 +0000350 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
Devang Patel7e1e31f2009-07-02 22:43:26 +0000351 // Don't handle byval struct arguments or VLAs, for example.
Dale Johannesen7dc78402010-04-25 21:03:54 +0000352 // Note that if we have a byval struct argument, fast ISel is turned off;
353 // those are handled in SelectionDAGBuilder.
Devang Patel54fc4d62010-04-28 19:27:33 +0000354 if (AI) {
355 DenseMap<const AllocaInst*, int>::iterator SI =
356 StaticAllocaMap.find(AI);
357 if (SI == StaticAllocaMap.end()) break; // VLAs.
358 int FI = SI->second;
359 if (!DI->getDebugLoc().isUnknown())
360 MF.getMMI().setVariableDbgInfo(DI->getVariable(), FI, DI->getDebugLoc());
361 } else
362 // Building the map above is target independent. Generating DBG_VALUE
363 // inline is target dependent; do this now.
364 (void)TargetSelectInstruction(cast<Instruction>(I));
Dan Gohman33134c42008-09-25 17:05:24 +0000365 return true;
Bill Wendling92c1e122009-02-13 02:16:35 +0000366 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000367 case Intrinsic::dbg_value: {
Dale Johannesen343b42e2010-04-07 01:15:14 +0000368 // This form of DBG_VALUE is target-independent.
Dan Gohman46510a72010-04-15 01:51:59 +0000369 const DbgValueInst *DI = cast<DbgValueInst>(I);
Dale Johannesen45df7612010-02-26 20:01:55 +0000370 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohman46510a72010-04-15 01:51:59 +0000371 const Value *V = DI->getValue();
Dale Johannesen45df7612010-02-26 20:01:55 +0000372 if (!V) {
373 // Currently the optimizer can produce this; insert an undef to
374 // help debugging. Probably the optimizer should not do this.
375 BuildMI(MBB, DL, II).addReg(0U).addImm(DI->getOffset()).
376 addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000377 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dale Johannesen45df7612010-02-26 20:01:55 +0000378 BuildMI(MBB, DL, II).addImm(CI->getZExtValue()).addImm(DI->getOffset()).
379 addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000380 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dale Johannesen45df7612010-02-26 20:01:55 +0000381 BuildMI(MBB, DL, II).addFPImm(CF).addImm(DI->getOffset()).
382 addMetadata(DI->getVariable());
383 } else if (unsigned Reg = lookUpRegForValue(V)) {
384 BuildMI(MBB, DL, II).addReg(Reg, RegState::Debug).addImm(DI->getOffset()).
385 addMetadata(DI->getVariable());
386 } else {
387 // We can't yet handle anything else here because it would require
388 // generating code, thus altering codegen because of debug info.
389 // Insert an undef so we can see what we dropped.
390 BuildMI(MBB, DL, II).addReg(0U).addImm(DI->getOffset()).
391 addMetadata(DI->getVariable());
392 }
393 return true;
394 }
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000395 case Intrinsic::eh_exception: {
Owen Andersone50ed302009-08-10 22:56:29 +0000396 EVT VT = TLI.getValueType(I->getType());
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000397 switch (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)) {
398 default: break;
399 case TargetLowering::Expand: {
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000400 assert(MBB->isLandingPad() && "Call to eh.exception not in landing pad!");
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000401 unsigned Reg = TLI.getExceptionAddressRegister();
402 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
403 unsigned ResultReg = createResultReg(RC);
404 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
405 Reg, RC, RC);
406 assert(InsertedCopy && "Can't copy address registers!");
Evan Cheng24ac4082008-11-24 07:09:49 +0000407 InsertedCopy = InsertedCopy;
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000408 UpdateValueMap(I, ResultReg);
409 return true;
410 }
411 }
412 break;
413 }
Duncan Sandsb01bbdc2009-10-14 16:11:37 +0000414 case Intrinsic::eh_selector: {
Owen Andersone50ed302009-08-10 22:56:29 +0000415 EVT VT = TLI.getValueType(I->getType());
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000416 switch (TLI.getOperationAction(ISD::EHSELECTION, VT)) {
417 default: break;
418 case TargetLowering::Expand: {
Chris Lattnered3a8062010-04-05 06:05:26 +0000419 if (MBB->isLandingPad())
420 AddCatchInfo(*cast<CallInst>(I), &MF.getMMI(), MBB);
421 else {
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000422#ifndef NDEBUG
Chris Lattnered3a8062010-04-05 06:05:26 +0000423 CatchInfoLost.insert(cast<CallInst>(I));
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000424#endif
Chris Lattnered3a8062010-04-05 06:05:26 +0000425 // FIXME: Mark exception selector register as live in. Hack for PR1508.
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000426 unsigned Reg = TLI.getExceptionSelectorRegister();
Chris Lattnered3a8062010-04-05 06:05:26 +0000427 if (Reg) MBB->addLiveIn(Reg);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000428 }
Chris Lattnered3a8062010-04-05 06:05:26 +0000429
430 unsigned Reg = TLI.getExceptionSelectorRegister();
431 EVT SrcVT = TLI.getPointerTy();
432 const TargetRegisterClass *RC = TLI.getRegClassFor(SrcVT);
433 unsigned ResultReg = createResultReg(RC);
434 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, Reg,
435 RC, RC);
436 assert(InsertedCopy && "Can't copy address registers!");
437 InsertedCopy = InsertedCopy;
438
439 // Cast the register to the type of the selector.
440 if (SrcVT.bitsGT(MVT::i32))
441 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, ISD::TRUNCATE,
442 ResultReg);
443 else if (SrcVT.bitsLT(MVT::i32))
444 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32,
445 ISD::SIGN_EXTEND, ResultReg);
446 if (ResultReg == 0)
447 // Unhandled operand. Halt "fast" selection and bail.
448 return false;
449
450 UpdateValueMap(I, ResultReg);
451
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000452 return true;
453 }
454 }
455 break;
456 }
Dan Gohman33134c42008-09-25 17:05:24 +0000457 }
Dan Gohman4183e312010-04-13 17:07:06 +0000458
459 // An arbitrary call. Bail.
Dan Gohman33134c42008-09-25 17:05:24 +0000460 return false;
461}
462
Dan Gohman46510a72010-04-15 01:51:59 +0000463bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000464 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
465 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000466
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
468 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersond0533c92008-08-26 23:46:32 +0000469 // Unhandled type. Halt "fast" selection and bail.
470 return false;
471
Dan Gohman474d3b32009-03-13 23:53:06 +0000472 // Check if the destination type is legal. Or as a special case,
473 // it may be i1 if we're doing a truncate because that's
474 // easy and somewhat common.
475 if (!TLI.isTypeLegal(DstVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000476 if (DstVT != MVT::i1 || Opcode != ISD::TRUNCATE)
Dan Gohman91b6f972008-10-03 01:28:47 +0000477 // Unhandled type. Halt "fast" selection and bail.
478 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000479
480 // Check if the source operand is legal. Or as a special case,
481 // it may be i1 if we're doing zero-extension because that's
482 // easy and somewhat common.
483 if (!TLI.isTypeLegal(SrcVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000484 if (SrcVT != MVT::i1 || Opcode != ISD::ZERO_EXTEND)
Dan Gohman474d3b32009-03-13 23:53:06 +0000485 // Unhandled type. Halt "fast" selection and bail.
486 return false;
487
Dan Gohman3df24e62008-09-03 23:12:08 +0000488 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000489 if (!InputReg)
490 // Unhandled operand. Halt "fast" selection and bail.
491 return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000492
493 // If the operand is i1, arrange for the high bits in the register to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +0000494 if (SrcVT == MVT::i1) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000495 SrcVT = TLI.getTypeToTransformTo(I->getContext(), SrcVT);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000496 InputReg = FastEmitZExtFromI1(SrcVT.getSimpleVT(), InputReg);
497 if (!InputReg)
498 return false;
499 }
Dan Gohman474d3b32009-03-13 23:53:06 +0000500 // If the result is i1, truncate to the target's type for i1 first.
Owen Anderson825b72b2009-08-11 20:47:22 +0000501 if (DstVT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +0000502 DstVT = TLI.getTypeToTransformTo(I->getContext(), DstVT);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000503
Owen Andersond0533c92008-08-26 23:46:32 +0000504 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
505 DstVT.getSimpleVT(),
506 Opcode,
507 InputReg);
508 if (!ResultReg)
509 return false;
510
Dan Gohman3df24e62008-09-03 23:12:08 +0000511 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000512 return true;
513}
514
Dan Gohman46510a72010-04-15 01:51:59 +0000515bool FastISel::SelectBitCast(const User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000516 // If the bitcast doesn't change the type, just use the operand value.
517 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000518 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000519 if (Reg == 0)
520 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000521 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000522 return true;
523 }
524
525 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Andersone50ed302009-08-10 22:56:29 +0000526 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
527 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000528
Owen Anderson825b72b2009-08-11 20:47:22 +0000529 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
530 DstVT == MVT::Other || !DstVT.isSimple() ||
Owen Andersond0533c92008-08-26 23:46:32 +0000531 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
532 // Unhandled type. Halt "fast" selection and bail.
533 return false;
534
Dan Gohman3df24e62008-09-03 23:12:08 +0000535 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000536 if (Op0 == 0)
537 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000538 return false;
539
Dan Gohmanad368ac2008-08-27 18:10:19 +0000540 // First, try to perform the bitcast by inserting a reg-reg copy.
541 unsigned ResultReg = 0;
542 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
543 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
544 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
545 ResultReg = createResultReg(DstClass);
546
547 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
548 Op0, DstClass, SrcClass);
549 if (!InsertedCopy)
550 ResultReg = 0;
551 }
552
553 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
554 if (!ResultReg)
555 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
556 ISD::BIT_CONVERT, Op0);
557
558 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000559 return false;
560
Dan Gohman3df24e62008-09-03 23:12:08 +0000561 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000562 return true;
563}
564
Dan Gohman3df24e62008-09-03 23:12:08 +0000565bool
Dan Gohman46510a72010-04-15 01:51:59 +0000566FastISel::SelectInstruction(const Instruction *I) {
Dan Gohmane8c92dd2010-04-23 15:29:50 +0000567 // Just before the terminator instruction, insert instructions to
568 // feed PHI nodes in successor blocks.
569 if (isa<TerminatorInst>(I))
570 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
571 return false;
572
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000573 DL = I->getDebugLoc();
574
Dan Gohman6e3ff372009-12-05 01:27:58 +0000575 // First, try doing target-independent selection.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000576 if (SelectOperator(I, I->getOpcode())) {
577 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000578 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000579 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000580
581 // Next, try calling the target to attempt to handle the instruction.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000582 if (TargetSelectInstruction(I)) {
583 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000584 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000585 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000586
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000587 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000588 return false;
Dan Gohman40b189e2008-09-05 18:18:20 +0000589}
590
Dan Gohmand98d6202008-10-02 22:15:21 +0000591/// FastEmitBranch - Emit an unconditional branch to the given block,
592/// unless it is the immediate (fall-through) successor, and update
593/// the CFG.
594void
595FastISel::FastEmitBranch(MachineBasicBlock *MSucc) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000596 if (MBB->isLayoutSuccessor(MSucc)) {
597 // The unconditional fall-through case, which needs no instructions.
598 } else {
599 // The unconditional branch case.
600 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
601 }
602 MBB->addSuccessor(MSucc);
603}
604
Dan Gohman3d45a852009-09-03 22:53:57 +0000605/// SelectFNeg - Emit an FNeg operation.
606///
607bool
Dan Gohman46510a72010-04-15 01:51:59 +0000608FastISel::SelectFNeg(const User *I) {
Dan Gohman3d45a852009-09-03 22:53:57 +0000609 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
610 if (OpReg == 0) return false;
611
Dan Gohman4a215a12009-09-11 00:36:43 +0000612 // If the target has ISD::FNEG, use it.
613 EVT VT = TLI.getValueType(I->getType());
614 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
615 ISD::FNEG, OpReg);
616 if (ResultReg != 0) {
617 UpdateValueMap(I, ResultReg);
618 return true;
619 }
620
Dan Gohman5e5abb72009-09-11 00:34:46 +0000621 // Bitcast the value to integer, twiddle the sign bit with xor,
622 // and then bitcast it back to floating-point.
Dan Gohman3d45a852009-09-03 22:53:57 +0000623 if (VT.getSizeInBits() > 64) return false;
Dan Gohman5e5abb72009-09-11 00:34:46 +0000624 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
625 if (!TLI.isTypeLegal(IntVT))
626 return false;
627
628 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
629 ISD::BIT_CONVERT, OpReg);
630 if (IntReg == 0)
631 return false;
632
633 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR, IntReg,
634 UINT64_C(1) << (VT.getSizeInBits()-1),
635 IntVT.getSimpleVT());
636 if (IntResultReg == 0)
637 return false;
638
639 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
640 ISD::BIT_CONVERT, IntResultReg);
Dan Gohman3d45a852009-09-03 22:53:57 +0000641 if (ResultReg == 0)
642 return false;
643
644 UpdateValueMap(I, ResultReg);
645 return true;
646}
647
Dan Gohman40b189e2008-09-05 18:18:20 +0000648bool
Dan Gohman46510a72010-04-15 01:51:59 +0000649FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000650 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000651 case Instruction::Add:
652 return SelectBinaryOp(I, ISD::ADD);
653 case Instruction::FAdd:
654 return SelectBinaryOp(I, ISD::FADD);
655 case Instruction::Sub:
656 return SelectBinaryOp(I, ISD::SUB);
657 case Instruction::FSub:
Dan Gohman3d45a852009-09-03 22:53:57 +0000658 // FNeg is currently represented in LLVM IR as a special case of FSub.
659 if (BinaryOperator::isFNeg(I))
660 return SelectFNeg(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000661 return SelectBinaryOp(I, ISD::FSUB);
662 case Instruction::Mul:
663 return SelectBinaryOp(I, ISD::MUL);
664 case Instruction::FMul:
665 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman3df24e62008-09-03 23:12:08 +0000666 case Instruction::SDiv:
667 return SelectBinaryOp(I, ISD::SDIV);
668 case Instruction::UDiv:
669 return SelectBinaryOp(I, ISD::UDIV);
670 case Instruction::FDiv:
671 return SelectBinaryOp(I, ISD::FDIV);
672 case Instruction::SRem:
673 return SelectBinaryOp(I, ISD::SREM);
674 case Instruction::URem:
675 return SelectBinaryOp(I, ISD::UREM);
676 case Instruction::FRem:
677 return SelectBinaryOp(I, ISD::FREM);
678 case Instruction::Shl:
679 return SelectBinaryOp(I, ISD::SHL);
680 case Instruction::LShr:
681 return SelectBinaryOp(I, ISD::SRL);
682 case Instruction::AShr:
683 return SelectBinaryOp(I, ISD::SRA);
684 case Instruction::And:
685 return SelectBinaryOp(I, ISD::AND);
686 case Instruction::Or:
687 return SelectBinaryOp(I, ISD::OR);
688 case Instruction::Xor:
689 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000690
Dan Gohman3df24e62008-09-03 23:12:08 +0000691 case Instruction::GetElementPtr:
692 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000693
Dan Gohman3df24e62008-09-03 23:12:08 +0000694 case Instruction::Br: {
Dan Gohman46510a72010-04-15 01:51:59 +0000695 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000696
Dan Gohman3df24e62008-09-03 23:12:08 +0000697 if (BI->isUnconditional()) {
Dan Gohman46510a72010-04-15 01:51:59 +0000698 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohman3df24e62008-09-03 23:12:08 +0000699 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
Dan Gohmand98d6202008-10-02 22:15:21 +0000700 FastEmitBranch(MSucc);
Dan Gohman3df24e62008-09-03 23:12:08 +0000701 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000702 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000703
704 // Conditional branches are not handed yet.
705 // Halt "fast" selection and bail.
706 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000707 }
708
Dan Gohman087c8502008-09-05 01:08:41 +0000709 case Instruction::Unreachable:
710 // Nothing to emit.
711 return true;
712
Dan Gohman0586d912008-09-10 20:11:02 +0000713 case Instruction::Alloca:
714 // FunctionLowering has the static-sized case covered.
715 if (StaticAllocaMap.count(cast<AllocaInst>(I)))
716 return true;
717
718 // Dynamic-sized alloca is not handled yet.
719 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000720
Dan Gohman33134c42008-09-25 17:05:24 +0000721 case Instruction::Call:
722 return SelectCall(I);
723
Dan Gohman3df24e62008-09-03 23:12:08 +0000724 case Instruction::BitCast:
725 return SelectBitCast(I);
726
727 case Instruction::FPToSI:
728 return SelectCast(I, ISD::FP_TO_SINT);
729 case Instruction::ZExt:
730 return SelectCast(I, ISD::ZERO_EXTEND);
731 case Instruction::SExt:
732 return SelectCast(I, ISD::SIGN_EXTEND);
733 case Instruction::Trunc:
734 return SelectCast(I, ISD::TRUNCATE);
735 case Instruction::SIToFP:
736 return SelectCast(I, ISD::SINT_TO_FP);
737
738 case Instruction::IntToPtr: // Deliberate fall-through.
739 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +0000740 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
741 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman3df24e62008-09-03 23:12:08 +0000742 if (DstVT.bitsGT(SrcVT))
743 return SelectCast(I, ISD::ZERO_EXTEND);
744 if (DstVT.bitsLT(SrcVT))
745 return SelectCast(I, ISD::TRUNCATE);
746 unsigned Reg = getRegForValue(I->getOperand(0));
747 if (Reg == 0) return false;
748 UpdateValueMap(I, Reg);
749 return true;
750 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000751
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000752 case Instruction::PHI:
753 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
754
Dan Gohman3df24e62008-09-03 23:12:08 +0000755 default:
756 // Unhandled instruction. Halt "fast" selection and bail.
757 return false;
758 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000759}
760
Dan Gohman3df24e62008-09-03 23:12:08 +0000761FastISel::FastISel(MachineFunction &mf,
762 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +0000763 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmanf81eca02010-04-22 20:46:50 +0000764 DenseMap<const AllocaInst *, int> &am,
765 std::vector<std::pair<MachineInstr*, unsigned> > &pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000766#ifndef NDEBUG
Dan Gohman25208642010-04-14 19:53:31 +0000767 , SmallSet<const Instruction *, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000768#endif
769 )
Dan Gohman3df24e62008-09-03 23:12:08 +0000770 : MBB(0),
771 ValueMap(vm),
772 MBBMap(bm),
Dan Gohman0586d912008-09-10 20:11:02 +0000773 StaticAllocaMap(am),
Dan Gohmanf81eca02010-04-22 20:46:50 +0000774 PHINodesToUpdate(pn),
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000775#ifndef NDEBUG
776 CatchInfoLost(cil),
777#endif
Dan Gohman3df24e62008-09-03 23:12:08 +0000778 MF(mf),
779 MRI(MF.getRegInfo()),
Dan Gohman0586d912008-09-10 20:11:02 +0000780 MFI(*MF.getFrameInfo()),
781 MCP(*MF.getConstantPool()),
Dan Gohman3df24e62008-09-03 23:12:08 +0000782 TM(MF.getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000783 TD(*TM.getTargetData()),
784 TII(*TM.getInstrInfo()),
Owen Andersone922c022009-07-22 00:24:57 +0000785 TLI(*TM.getTargetLowering()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000786}
787
Dan Gohmane285a742008-08-14 21:51:29 +0000788FastISel::~FastISel() {}
789
Owen Anderson825b72b2009-08-11 20:47:22 +0000790unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000791 unsigned) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000792 return 0;
793}
794
Owen Anderson825b72b2009-08-11 20:47:22 +0000795unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000796 unsigned, unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000797 return 0;
798}
799
Owen Anderson825b72b2009-08-11 20:47:22 +0000800unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000801 unsigned, unsigned /*Op0*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000802 unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000803 return 0;
804}
805
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000806unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000807 return 0;
808}
809
Owen Anderson825b72b2009-08-11 20:47:22 +0000810unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohman46510a72010-04-15 01:51:59 +0000811 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000812 return 0;
813}
814
Owen Anderson825b72b2009-08-11 20:47:22 +0000815unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000816 unsigned, unsigned /*Op0*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000817 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000818 return 0;
819}
820
Owen Anderson825b72b2009-08-11 20:47:22 +0000821unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000822 unsigned, unsigned /*Op0*/,
Dan Gohman46510a72010-04-15 01:51:59 +0000823 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_rri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000828 unsigned,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000829 unsigned /*Op0*/, unsigned /*Op1*/,
830 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000831 return 0;
832}
833
834/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
835/// to emit an instruction with an immediate operand using FastEmit_ri.
836/// If that fails, it materializes the immediate into a register and try
837/// FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000838unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000839 unsigned Op0, uint64_t Imm,
Owen Anderson825b72b2009-08-11 20:47:22 +0000840 MVT ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000841 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman151ed612008-08-27 18:15:05 +0000842 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000843 if (ResultReg != 0)
844 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000845 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000846 if (MaterialReg == 0)
847 return 0;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000848 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000849}
850
Dan Gohman10df0fa2008-08-27 01:09:54 +0000851/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
852/// to emit an instruction with a floating-point immediate operand using
853/// FastEmit_rf. If that fails, it materializes the immediate into a register
854/// and try FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000855unsigned FastISel::FastEmit_rf_(MVT VT, unsigned Opcode,
Dan Gohman46510a72010-04-15 01:51:59 +0000856 unsigned Op0, const ConstantFP *FPImm,
Owen Anderson825b72b2009-08-11 20:47:22 +0000857 MVT ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000858 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohman151ed612008-08-27 18:15:05 +0000859 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000860 if (ResultReg != 0)
861 return ResultReg;
862
863 // Materialize the constant in a register.
864 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
865 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000866 // If the target doesn't have a way to directly enter a floating-point
867 // value into a register, use an alternate approach.
868 // TODO: The current approach only supports floating-point constants
869 // that can be constructed by conversion from integer values. This should
870 // be replaced by code that creates a load from a constant-pool entry,
871 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000872 const APFloat &Flt = FPImm->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000873 EVT IntVT = TLI.getPointerTy();
Dan Gohman10df0fa2008-08-27 01:09:54 +0000874
875 uint64_t x[2];
876 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000877 bool isExact;
878 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
879 APFloat::rmTowardZero, &isExact);
880 if (!isExact)
Dan Gohman10df0fa2008-08-27 01:09:54 +0000881 return 0;
882 APInt IntVal(IntBitWidth, 2, x);
883
884 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
885 ISD::Constant, IntVal.getZExtValue());
886 if (IntegerReg == 0)
887 return 0;
888 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
889 ISD::SINT_TO_FP, IntegerReg);
890 if (MaterialReg == 0)
891 return 0;
892 }
893 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
894}
895
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000896unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
897 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000898}
899
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000900unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000901 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000902 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000903 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000904
Bill Wendling9bc96a52009-02-03 00:55:04 +0000905 BuildMI(MBB, DL, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000906 return ResultReg;
907}
908
909unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
910 const TargetRegisterClass *RC,
911 unsigned Op0) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000912 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000913 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000914
Evan Cheng5960e4e2008-09-08 08:38:20 +0000915 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +0000916 BuildMI(MBB, DL, II, ResultReg).addReg(Op0);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000917 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +0000918 BuildMI(MBB, DL, II).addReg(Op0);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000919 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
920 II.ImplicitDefs[0], RC, RC);
921 if (!InsertedCopy)
922 ResultReg = 0;
923 }
924
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000925 return ResultReg;
926}
927
928unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
929 const TargetRegisterClass *RC,
930 unsigned Op0, unsigned Op1) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000931 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000932 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000933
Evan Cheng5960e4e2008-09-08 08:38:20 +0000934 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +0000935 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addReg(Op1);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000936 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +0000937 BuildMI(MBB, DL, II).addReg(Op0).addReg(Op1);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000938 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
939 II.ImplicitDefs[0], RC, RC);
940 if (!InsertedCopy)
941 ResultReg = 0;
942 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000943 return ResultReg;
944}
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000945
946unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
947 const TargetRegisterClass *RC,
948 unsigned Op0, uint64_t Imm) {
949 unsigned ResultReg = createResultReg(RC);
950 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
951
Evan Cheng5960e4e2008-09-08 08:38:20 +0000952 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +0000953 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000954 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +0000955 BuildMI(MBB, DL, II).addReg(Op0).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000956 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
957 II.ImplicitDefs[0], RC, RC);
958 if (!InsertedCopy)
959 ResultReg = 0;
960 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000961 return ResultReg;
962}
963
Dan Gohman10df0fa2008-08-27 01:09:54 +0000964unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
965 const TargetRegisterClass *RC,
Dan Gohman46510a72010-04-15 01:51:59 +0000966 unsigned Op0, const ConstantFP *FPImm) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000967 unsigned ResultReg = createResultReg(RC);
968 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
969
Evan Cheng5960e4e2008-09-08 08:38:20 +0000970 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +0000971 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000972 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +0000973 BuildMI(MBB, DL, II).addReg(Op0).addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000974 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
975 II.ImplicitDefs[0], RC, RC);
976 if (!InsertedCopy)
977 ResultReg = 0;
978 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000979 return ResultReg;
980}
981
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000982unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
983 const TargetRegisterClass *RC,
984 unsigned Op0, unsigned Op1, uint64_t Imm) {
985 unsigned ResultReg = createResultReg(RC);
986 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
987
Evan Cheng5960e4e2008-09-08 08:38:20 +0000988 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +0000989 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000990 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +0000991 BuildMI(MBB, DL, II).addReg(Op0).addReg(Op1).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000992 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
993 II.ImplicitDefs[0], RC, RC);
994 if (!InsertedCopy)
995 ResultReg = 0;
996 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000997 return ResultReg;
998}
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000999
1000unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1001 const TargetRegisterClass *RC,
1002 uint64_t Imm) {
1003 unsigned ResultReg = createResultReg(RC);
1004 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1005
Evan Cheng5960e4e2008-09-08 08:38:20 +00001006 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +00001007 BuildMI(MBB, DL, II, ResultReg).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001008 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +00001009 BuildMI(MBB, DL, II).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001010 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1011 II.ImplicitDefs[0], RC, RC);
1012 if (!InsertedCopy)
1013 ResultReg = 0;
1014 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001015 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +00001016}
Owen Anderson8970f002008-08-27 22:30:02 +00001017
Owen Anderson825b72b2009-08-11 20:47:22 +00001018unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Evan Cheng536ab132009-01-22 09:10:11 +00001019 unsigned Op0, uint32_t Idx) {
Owen Anderson40a468f2008-08-28 17:47:37 +00001020 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +00001021
Evan Cheng536ab132009-01-22 09:10:11 +00001022 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Chris Lattner518bb532010-02-09 19:54:29 +00001023 const TargetInstrDesc &II = TII.get(TargetOpcode::EXTRACT_SUBREG);
Owen Anderson8970f002008-08-27 22:30:02 +00001024
Evan Cheng5960e4e2008-09-08 08:38:20 +00001025 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +00001026 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addImm(Idx);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001027 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +00001028 BuildMI(MBB, DL, II).addReg(Op0).addImm(Idx);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001029 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1030 II.ImplicitDefs[0], RC, RC);
1031 if (!InsertedCopy)
1032 ResultReg = 0;
1033 }
Owen Anderson8970f002008-08-27 22:30:02 +00001034 return ResultReg;
1035}
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001036
1037/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1038/// with all but the least significant bit set to zero.
Owen Anderson825b72b2009-08-11 20:47:22 +00001039unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001040 return FastEmit_ri(VT, VT, ISD::AND, Op, 1);
1041}
Dan Gohmanf81eca02010-04-22 20:46:50 +00001042
1043/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1044/// Emit code to ensure constants are copied into registers when needed.
1045/// Remember the virtual registers that need to be added to the Machine PHI
1046/// nodes as input. We cannot just directly add them, because expansion
1047/// might result in multiple MBB's for one BB. As such, the start of the
1048/// BB might correspond to a different MBB than the end.
1049bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1050 const TerminatorInst *TI = LLVMBB->getTerminator();
1051
1052 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
1053 unsigned OrigNumPHINodesToUpdate = PHINodesToUpdate.size();
1054
1055 // Check successor nodes' PHI nodes that expect a constant to be available
1056 // from this block.
1057 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1058 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1059 if (!isa<PHINode>(SuccBB->begin())) continue;
1060 MachineBasicBlock *SuccMBB = MBBMap[SuccBB];
1061
1062 // If this terminator has multiple identical successors (common for
1063 // switches), only handle each succ once.
1064 if (!SuccsHandled.insert(SuccMBB)) continue;
1065
1066 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1067
1068 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1069 // nodes and Machine PHI nodes, but the incoming operands have not been
1070 // emitted yet.
1071 for (BasicBlock::const_iterator I = SuccBB->begin();
1072 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
1073 // Ignore dead phi's.
1074 if (PN->use_empty()) continue;
1075
1076 // Only handle legal types. Two interesting things to note here. First,
1077 // by bailing out early, we may leave behind some dead instructions,
1078 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
1079 // own moves. Second, this check is necessary becuase FastISel doesn't
1080 // use CreateRegForValue to create registers, so it always creates
1081 // exactly one register for each non-void instruction.
1082 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1083 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
1084 // Promote MVT::i1.
1085 if (VT == MVT::i1)
1086 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1087 else {
1088 PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
1089 return false;
1090 }
1091 }
1092
1093 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1094
1095 unsigned Reg = getRegForValue(PHIOp);
1096 if (Reg == 0) {
1097 PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
1098 return false;
1099 }
1100 PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
1101 }
1102 }
1103
1104 return true;
1105}