blob: 8aa37a06dc2f3e403d0d418fff1e60ecd0e7da08 [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 Gohman46510a72010-04-15 01:51:59 +000087 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +000088 if (CI->getValue().getActiveBits() <= 64)
89 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +000090 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +000091 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +000092 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman1e9e8c32008-10-07 22:03:27 +000093 // Translate this as an integer zero so that it can be
94 // local-CSE'd with actual integer zeros.
Owen Anderson1d0be152009-08-13 21:58:54 +000095 Reg =
96 getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
Dan Gohman46510a72010-04-15 01:51:59 +000097 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman4183e312010-04-13 17:07:06 +000098 // Try to emit the constant directly.
Dan Gohman104e4ce2008-09-03 23:32:19 +000099 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000100
101 if (!Reg) {
Dan Gohman4183e312010-04-13 17:07:06 +0000102 // Try to emit the constant by using an integer constant with a cast.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000103 const APFloat &Flt = CF->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000104 EVT IntVT = TLI.getPointerTy();
Dan Gohmanad368ac2008-08-27 18:10:19 +0000105
106 uint64_t x[2];
107 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000108 bool isExact;
109 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
110 APFloat::rmTowardZero, &isExact);
111 if (isExact) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000112 APInt IntVal(IntBitWidth, 2, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000113
Owen Andersone922c022009-07-22 00:24:57 +0000114 unsigned IntegerReg =
Owen Andersoneed707b2009-07-24 23:12:02 +0000115 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000116 if (IntegerReg != 0)
117 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
118 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000119 }
Dan Gohman46510a72010-04-15 01:51:59 +0000120 } else if (const Operator *Op = dyn_cast<Operator>(V)) {
Dan Gohman32acbc12010-04-14 02:33:23 +0000121 if (!SelectOperator(Op, Op->getOpcode())) return 0;
122 Reg = LocalValueMap[Op];
Dan Gohman205d9252008-08-28 21:19:07 +0000123 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000124 Reg = createResultReg(TLI.getRegClassFor(VT));
Chris Lattner518bb532010-02-09 19:54:29 +0000125 BuildMI(MBB, DL, TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000126 }
Owen Andersond5d81a42008-09-03 17:51:57 +0000127
Dan Gohmandceffe62008-09-25 01:28:51 +0000128 // If target-independent code couldn't handle the value, give target-specific
129 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000130 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000131 Reg = TargetMaterializeConstant(cast<Constant>(V));
Owen Anderson6e607452008-09-05 23:36:01 +0000132
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000133 // Don't cache constant materializations in the general ValueMap.
134 // To do so would require tracking what uses they dominate.
Dan Gohmandceffe62008-09-25 01:28:51 +0000135 if (Reg != 0)
136 LocalValueMap[V] = Reg;
Dan Gohman104e4ce2008-09-03 23:32:19 +0000137 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000138}
139
Dan Gohman46510a72010-04-15 01:51:59 +0000140unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng59fbc802008-09-09 01:26:59 +0000141 // Look up the value to see if we already have a register for it. We
142 // cache values defined by Instructions across blocks, and other values
143 // only locally. This is because Instructions already have the SSA
144 // def-dominatess-use requirement enforced.
145 if (ValueMap.count(V))
146 return ValueMap[V];
147 return LocalValueMap[V];
148}
149
Owen Andersoncc54e762008-08-30 00:38:46 +0000150/// UpdateValueMap - Update the value map to include the new mapping for this
151/// instruction, or insert an extra copy to get the result in a previous
152/// determined register.
153/// NOTE: This is only necessary because we might select a block that uses
154/// a value before we select the block that defines the value. It might be
155/// possible to fix this by selecting blocks in reverse postorder.
Dan Gohman46510a72010-04-15 01:51:59 +0000156unsigned FastISel::UpdateValueMap(const Value *I, unsigned Reg) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000157 if (!isa<Instruction>(I)) {
158 LocalValueMap[I] = Reg;
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000159 return Reg;
Dan Gohman40b189e2008-09-05 18:18:20 +0000160 }
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000161
162 unsigned &AssignedReg = ValueMap[I];
163 if (AssignedReg == 0)
164 AssignedReg = Reg;
Chris Lattner36e39462009-04-12 07:46:30 +0000165 else if (Reg != AssignedReg) {
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000166 const TargetRegisterClass *RegClass = MRI.getRegClass(Reg);
167 TII.copyRegToReg(*MBB, MBB->end(), AssignedReg,
168 Reg, RegClass, RegClass);
169 }
170 return AssignedReg;
Owen Andersoncc54e762008-08-30 00:38:46 +0000171}
172
Dan Gohman46510a72010-04-15 01:51:59 +0000173unsigned FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000174 unsigned IdxN = getRegForValue(Idx);
175 if (IdxN == 0)
176 // Unhandled operand. Halt "fast" selection and bail.
177 return 0;
178
179 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Anderson766b5ef2009-08-11 21:59:30 +0000180 MVT PtrVT = TLI.getPointerTy();
Owen Andersone50ed302009-08-10 22:56:29 +0000181 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000182 if (IdxVT.bitsLT(PtrVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +0000183 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND, IdxN);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000184 else if (IdxVT.bitsGT(PtrVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +0000185 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE, IdxN);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000186 return IdxN;
187}
188
Dan Gohmanbdedd442008-08-20 00:11:48 +0000189/// SelectBinaryOp - Select and emit code for a binary operator instruction,
190/// which has an opcode which directly corresponds to the given ISD opcode.
191///
Dan Gohman46510a72010-04-15 01:51:59 +0000192bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000193 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000194 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmanbdedd442008-08-20 00:11:48 +0000195 // Unhandled type. Halt "fast" selection and bail.
196 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000197
Dan Gohmanb71fea22008-08-26 20:52:40 +0000198 // We only handle legal types. For example, on x86-32 the instruction
199 // selector contains all of the 64-bit instructions from x86-64,
200 // under the assumption that i64 won't be used if the target doesn't
201 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000202 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000203 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000204 // don't require additional zeroing, which makes them easy.
Owen Anderson825b72b2009-08-11 20:47:22 +0000205 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000206 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
207 ISDOpcode == ISD::XOR))
Owen Anderson23b9b192009-08-12 00:36:31 +0000208 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohman638c6832008-09-05 18:44:22 +0000209 else
210 return false;
211 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000212
Dan Gohman3df24e62008-09-03 23:12:08 +0000213 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000214 if (Op0 == 0)
215 // Unhandled operand. Halt "fast" selection and bail.
216 return false;
217
218 // Check if the second operand is a constant and handle it appropriately.
219 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000220 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
221 ISDOpcode, Op0, CI->getZExtValue());
222 if (ResultReg != 0) {
223 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000224 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000225 return true;
226 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000227 }
228
Dan Gohman10df0fa2008-08-27 01:09:54 +0000229 // Check if the second operand is a constant float.
230 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000231 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
232 ISDOpcode, Op0, CF);
233 if (ResultReg != 0) {
234 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000235 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000236 return true;
237 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000238 }
239
Dan Gohman3df24e62008-09-03 23:12:08 +0000240 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000241 if (Op1 == 0)
242 // Unhandled operand. Halt "fast" selection and bail.
243 return false;
244
Dan Gohmanad368ac2008-08-27 18:10:19 +0000245 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000246 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
247 ISDOpcode, Op0, Op1);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000248 if (ResultReg == 0)
249 // Target-specific code wasn't able to find a machine opcode for
250 // the given ISD opcode and type. Halt "fast" selection and bail.
251 return false;
252
Dan Gohman8014e862008-08-20 00:23:20 +0000253 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000254 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000255 return true;
256}
257
Dan Gohman46510a72010-04-15 01:51:59 +0000258bool FastISel::SelectGetElementPtr(const User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000259 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000260 if (N == 0)
261 // Unhandled operand. Halt "fast" selection and bail.
262 return false;
263
264 const Type *Ty = I->getOperand(0)->getType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000265 MVT VT = TLI.getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +0000266 for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
267 E = I->op_end(); OI != E; ++OI) {
268 const Value *Idx = *OI;
Evan Cheng83785c82008-08-20 22:45:34 +0000269 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
270 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
271 if (Field) {
272 // N = N + Offset
273 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
274 // FIXME: This can be optimized by combining the add with a
275 // subsequent one.
Dan Gohman7a0e6592008-08-21 17:25:26 +0000276 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000277 if (N == 0)
278 // Unhandled operand. Halt "fast" selection and bail.
279 return false;
280 }
281 Ty = StTy->getElementType(Field);
282 } else {
283 Ty = cast<SequentialType>(Ty)->getElementType();
284
285 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +0000286 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Evan Cheng83785c82008-08-20 22:45:34 +0000287 if (CI->getZExtValue() == 0) continue;
288 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +0000289 TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000290 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000291 if (N == 0)
292 // Unhandled operand. Halt "fast" selection and bail.
293 return false;
294 continue;
295 }
296
297 // N = N + Idx * ElementSize;
Duncan Sands777d2302009-05-09 07:06:46 +0000298 uint64_t ElementSize = TD.getTypeAllocSize(Ty);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000299 unsigned IdxN = getRegForGEPIndex(Idx);
Evan Cheng83785c82008-08-20 22:45:34 +0000300 if (IdxN == 0)
301 // Unhandled operand. Halt "fast" selection and bail.
302 return false;
303
Dan Gohman80bc6e22008-08-26 20:57:08 +0000304 if (ElementSize != 1) {
Dan Gohmanf93cf792008-08-21 17:37:05 +0000305 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000306 if (IdxN == 0)
307 // Unhandled operand. Halt "fast" selection and bail.
308 return false;
309 }
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000310 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000311 if (N == 0)
312 // Unhandled operand. Halt "fast" selection and bail.
313 return false;
314 }
315 }
316
317 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000318 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000319 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000320}
321
Dan Gohman46510a72010-04-15 01:51:59 +0000322bool FastISel::SelectCall(const User *I) {
323 const Function *F = cast<CallInst>(I)->getCalledFunction();
Dan Gohman33134c42008-09-25 17:05:24 +0000324 if (!F) return false;
325
Dan Gohman4183e312010-04-13 17:07:06 +0000326 // Handle selected intrinsic function calls.
Dan Gohman33134c42008-09-25 17:05:24 +0000327 unsigned IID = F->getIntrinsicID();
328 switch (IID) {
329 default: break;
Bill Wendling92c1e122009-02-13 02:16:35 +0000330 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +0000331 const DbgDeclareInst *DI = cast<DbgDeclareInst>(I);
Chris Lattnerd850ac72010-04-05 02:19:28 +0000332 if (!DIDescriptor::ValidDebugInfo(DI->getVariable(), CodeGenOpt::None) ||
Chris Lattnered3a8062010-04-05 06:05:26 +0000333 !MF.getMMI().hasDebugInfo())
Devang Patel7e1e31f2009-07-02 22:43:26 +0000334 return true;
335
Dan Gohman46510a72010-04-15 01:51:59 +0000336 const Value *Address = DI->getAddress();
Dale Johannesendc918562010-02-06 02:26:02 +0000337 if (!Address)
338 return true;
Dale Johannesen343b42e2010-04-07 01:15:14 +0000339 if (isa<UndefValue>(Address))
340 return true;
Dan Gohman46510a72010-04-15 01:51:59 +0000341 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
Devang Patel7e1e31f2009-07-02 22:43:26 +0000342 // Don't handle byval struct arguments or VLAs, for example.
Dale Johannesen7dc78402010-04-25 21:03:54 +0000343 // Note that if we have a byval struct argument, fast ISel is turned off;
344 // those are handled in SelectionDAGBuilder.
Devang Patel7e1e31f2009-07-02 22:43:26 +0000345 if (!AI) break;
346 DenseMap<const AllocaInst*, int>::iterator SI =
347 StaticAllocaMap.find(AI);
348 if (SI == StaticAllocaMap.end()) break; // VLAs.
349 int FI = SI->second;
Chris Lattnerde4845c2010-04-02 19:42:39 +0000350 if (!DI->getDebugLoc().isUnknown())
Chris Lattnered3a8062010-04-05 06:05:26 +0000351 MF.getMMI().setVariableDbgInfo(DI->getVariable(), FI, DI->getDebugLoc());
Dale Johannesen7dc78402010-04-25 21:03:54 +0000352
Dale Johannesen10fedd22010-02-10 00:11:11 +0000353 // Building the map above is target independent. Generating DBG_VALUE
Dale Johannesen5ed17ae2010-01-26 00:09:58 +0000354 // inline is target dependent; do this now.
355 (void)TargetSelectInstruction(cast<Instruction>(I));
Dan Gohman33134c42008-09-25 17:05:24 +0000356 return true;
Bill Wendling92c1e122009-02-13 02:16:35 +0000357 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000358 case Intrinsic::dbg_value: {
Dale Johannesen343b42e2010-04-07 01:15:14 +0000359 // This form of DBG_VALUE is target-independent.
Dan Gohman46510a72010-04-15 01:51:59 +0000360 const DbgValueInst *DI = cast<DbgValueInst>(I);
Dale Johannesen45df7612010-02-26 20:01:55 +0000361 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohman46510a72010-04-15 01:51:59 +0000362 const Value *V = DI->getValue();
Dale Johannesen45df7612010-02-26 20:01:55 +0000363 if (!V) {
364 // Currently the optimizer can produce this; insert an undef to
365 // help debugging. Probably the optimizer should not do this.
366 BuildMI(MBB, DL, II).addReg(0U).addImm(DI->getOffset()).
367 addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000368 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dale Johannesen45df7612010-02-26 20:01:55 +0000369 BuildMI(MBB, DL, II).addImm(CI->getZExtValue()).addImm(DI->getOffset()).
370 addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000371 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dale Johannesen45df7612010-02-26 20:01:55 +0000372 BuildMI(MBB, DL, II).addFPImm(CF).addImm(DI->getOffset()).
373 addMetadata(DI->getVariable());
374 } else if (unsigned Reg = lookUpRegForValue(V)) {
375 BuildMI(MBB, DL, II).addReg(Reg, RegState::Debug).addImm(DI->getOffset()).
376 addMetadata(DI->getVariable());
377 } else {
378 // We can't yet handle anything else here because it would require
379 // generating code, thus altering codegen because of debug info.
380 // Insert an undef so we can see what we dropped.
381 BuildMI(MBB, DL, II).addReg(0U).addImm(DI->getOffset()).
382 addMetadata(DI->getVariable());
383 }
384 return true;
385 }
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000386 case Intrinsic::eh_exception: {
Owen Andersone50ed302009-08-10 22:56:29 +0000387 EVT VT = TLI.getValueType(I->getType());
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000388 switch (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)) {
389 default: break;
390 case TargetLowering::Expand: {
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000391 assert(MBB->isLandingPad() && "Call to eh.exception not in landing pad!");
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000392 unsigned Reg = TLI.getExceptionAddressRegister();
393 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
394 unsigned ResultReg = createResultReg(RC);
395 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
396 Reg, RC, RC);
397 assert(InsertedCopy && "Can't copy address registers!");
Evan Cheng24ac4082008-11-24 07:09:49 +0000398 InsertedCopy = InsertedCopy;
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000399 UpdateValueMap(I, ResultReg);
400 return true;
401 }
402 }
403 break;
404 }
Duncan Sandsb01bbdc2009-10-14 16:11:37 +0000405 case Intrinsic::eh_selector: {
Owen Andersone50ed302009-08-10 22:56:29 +0000406 EVT VT = TLI.getValueType(I->getType());
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000407 switch (TLI.getOperationAction(ISD::EHSELECTION, VT)) {
408 default: break;
409 case TargetLowering::Expand: {
Chris Lattnered3a8062010-04-05 06:05:26 +0000410 if (MBB->isLandingPad())
411 AddCatchInfo(*cast<CallInst>(I), &MF.getMMI(), MBB);
412 else {
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000413#ifndef NDEBUG
Chris Lattnered3a8062010-04-05 06:05:26 +0000414 CatchInfoLost.insert(cast<CallInst>(I));
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000415#endif
Chris Lattnered3a8062010-04-05 06:05:26 +0000416 // FIXME: Mark exception selector register as live in. Hack for PR1508.
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000417 unsigned Reg = TLI.getExceptionSelectorRegister();
Chris Lattnered3a8062010-04-05 06:05:26 +0000418 if (Reg) MBB->addLiveIn(Reg);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000419 }
Chris Lattnered3a8062010-04-05 06:05:26 +0000420
421 unsigned Reg = TLI.getExceptionSelectorRegister();
422 EVT SrcVT = TLI.getPointerTy();
423 const TargetRegisterClass *RC = TLI.getRegClassFor(SrcVT);
424 unsigned ResultReg = createResultReg(RC);
425 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, Reg,
426 RC, RC);
427 assert(InsertedCopy && "Can't copy address registers!");
428 InsertedCopy = InsertedCopy;
429
430 // Cast the register to the type of the selector.
431 if (SrcVT.bitsGT(MVT::i32))
432 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, ISD::TRUNCATE,
433 ResultReg);
434 else if (SrcVT.bitsLT(MVT::i32))
435 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32,
436 ISD::SIGN_EXTEND, ResultReg);
437 if (ResultReg == 0)
438 // Unhandled operand. Halt "fast" selection and bail.
439 return false;
440
441 UpdateValueMap(I, ResultReg);
442
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000443 return true;
444 }
445 }
446 break;
447 }
Dan Gohman33134c42008-09-25 17:05:24 +0000448 }
Dan Gohman4183e312010-04-13 17:07:06 +0000449
450 // An arbitrary call. Bail.
Dan Gohman33134c42008-09-25 17:05:24 +0000451 return false;
452}
453
Dan Gohman46510a72010-04-15 01:51:59 +0000454bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000455 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
456 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000457
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
459 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersond0533c92008-08-26 23:46:32 +0000460 // Unhandled type. Halt "fast" selection and bail.
461 return false;
462
Dan Gohman474d3b32009-03-13 23:53:06 +0000463 // Check if the destination type is legal. Or as a special case,
464 // it may be i1 if we're doing a truncate because that's
465 // easy and somewhat common.
466 if (!TLI.isTypeLegal(DstVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 if (DstVT != MVT::i1 || Opcode != ISD::TRUNCATE)
Dan Gohman91b6f972008-10-03 01:28:47 +0000468 // Unhandled type. Halt "fast" selection and bail.
469 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000470
471 // Check if the source operand is legal. Or as a special case,
472 // it may be i1 if we're doing zero-extension because that's
473 // easy and somewhat common.
474 if (!TLI.isTypeLegal(SrcVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 if (SrcVT != MVT::i1 || Opcode != ISD::ZERO_EXTEND)
Dan Gohman474d3b32009-03-13 23:53:06 +0000476 // Unhandled type. Halt "fast" selection and bail.
477 return false;
478
Dan Gohman3df24e62008-09-03 23:12:08 +0000479 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000480 if (!InputReg)
481 // Unhandled operand. Halt "fast" selection and bail.
482 return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000483
484 // If the operand is i1, arrange for the high bits in the register to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 if (SrcVT == MVT::i1) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000486 SrcVT = TLI.getTypeToTransformTo(I->getContext(), SrcVT);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000487 InputReg = FastEmitZExtFromI1(SrcVT.getSimpleVT(), InputReg);
488 if (!InputReg)
489 return false;
490 }
Dan Gohman474d3b32009-03-13 23:53:06 +0000491 // If the result is i1, truncate to the target's type for i1 first.
Owen Anderson825b72b2009-08-11 20:47:22 +0000492 if (DstVT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +0000493 DstVT = TLI.getTypeToTransformTo(I->getContext(), DstVT);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000494
Owen Andersond0533c92008-08-26 23:46:32 +0000495 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
496 DstVT.getSimpleVT(),
497 Opcode,
498 InputReg);
499 if (!ResultReg)
500 return false;
501
Dan Gohman3df24e62008-09-03 23:12:08 +0000502 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000503 return true;
504}
505
Dan Gohman46510a72010-04-15 01:51:59 +0000506bool FastISel::SelectBitCast(const User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000507 // If the bitcast doesn't change the type, just use the operand value.
508 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000509 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000510 if (Reg == 0)
511 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000512 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000513 return true;
514 }
515
516 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Andersone50ed302009-08-10 22:56:29 +0000517 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
518 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000519
Owen Anderson825b72b2009-08-11 20:47:22 +0000520 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
521 DstVT == MVT::Other || !DstVT.isSimple() ||
Owen Andersond0533c92008-08-26 23:46:32 +0000522 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
523 // Unhandled type. Halt "fast" selection and bail.
524 return false;
525
Dan Gohman3df24e62008-09-03 23:12:08 +0000526 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000527 if (Op0 == 0)
528 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000529 return false;
530
Dan Gohmanad368ac2008-08-27 18:10:19 +0000531 // First, try to perform the bitcast by inserting a reg-reg copy.
532 unsigned ResultReg = 0;
533 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
534 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
535 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
536 ResultReg = createResultReg(DstClass);
537
538 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
539 Op0, DstClass, SrcClass);
540 if (!InsertedCopy)
541 ResultReg = 0;
542 }
543
544 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
545 if (!ResultReg)
546 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
547 ISD::BIT_CONVERT, Op0);
548
549 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000550 return false;
551
Dan Gohman3df24e62008-09-03 23:12:08 +0000552 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000553 return true;
554}
555
Dan Gohman3df24e62008-09-03 23:12:08 +0000556bool
Dan Gohman46510a72010-04-15 01:51:59 +0000557FastISel::SelectInstruction(const Instruction *I) {
Dan Gohmane8c92dd2010-04-23 15:29:50 +0000558 // Just before the terminator instruction, insert instructions to
559 // feed PHI nodes in successor blocks.
560 if (isa<TerminatorInst>(I))
561 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
562 return false;
563
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000564 DL = I->getDebugLoc();
565
Dan Gohman6e3ff372009-12-05 01:27:58 +0000566 // First, try doing target-independent selection.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000567 if (SelectOperator(I, I->getOpcode())) {
568 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000569 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000570 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000571
572 // Next, try calling the target to attempt to handle the instruction.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000573 if (TargetSelectInstruction(I)) {
574 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000575 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000576 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000577
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000578 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000579 return false;
Dan Gohman40b189e2008-09-05 18:18:20 +0000580}
581
Dan Gohmand98d6202008-10-02 22:15:21 +0000582/// FastEmitBranch - Emit an unconditional branch to the given block,
583/// unless it is the immediate (fall-through) successor, and update
584/// the CFG.
585void
586FastISel::FastEmitBranch(MachineBasicBlock *MSucc) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000587 if (MBB->isLayoutSuccessor(MSucc)) {
588 // The unconditional fall-through case, which needs no instructions.
589 } else {
590 // The unconditional branch case.
591 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
592 }
593 MBB->addSuccessor(MSucc);
594}
595
Dan Gohman3d45a852009-09-03 22:53:57 +0000596/// SelectFNeg - Emit an FNeg operation.
597///
598bool
Dan Gohman46510a72010-04-15 01:51:59 +0000599FastISel::SelectFNeg(const User *I) {
Dan Gohman3d45a852009-09-03 22:53:57 +0000600 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
601 if (OpReg == 0) return false;
602
Dan Gohman4a215a12009-09-11 00:36:43 +0000603 // If the target has ISD::FNEG, use it.
604 EVT VT = TLI.getValueType(I->getType());
605 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
606 ISD::FNEG, OpReg);
607 if (ResultReg != 0) {
608 UpdateValueMap(I, ResultReg);
609 return true;
610 }
611
Dan Gohman5e5abb72009-09-11 00:34:46 +0000612 // Bitcast the value to integer, twiddle the sign bit with xor,
613 // and then bitcast it back to floating-point.
Dan Gohman3d45a852009-09-03 22:53:57 +0000614 if (VT.getSizeInBits() > 64) return false;
Dan Gohman5e5abb72009-09-11 00:34:46 +0000615 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
616 if (!TLI.isTypeLegal(IntVT))
617 return false;
618
619 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
620 ISD::BIT_CONVERT, OpReg);
621 if (IntReg == 0)
622 return false;
623
624 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR, IntReg,
625 UINT64_C(1) << (VT.getSizeInBits()-1),
626 IntVT.getSimpleVT());
627 if (IntResultReg == 0)
628 return false;
629
630 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
631 ISD::BIT_CONVERT, IntResultReg);
Dan Gohman3d45a852009-09-03 22:53:57 +0000632 if (ResultReg == 0)
633 return false;
634
635 UpdateValueMap(I, ResultReg);
636 return true;
637}
638
Dan Gohman40b189e2008-09-05 18:18:20 +0000639bool
Dan Gohman46510a72010-04-15 01:51:59 +0000640FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000641 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000642 case Instruction::Add:
643 return SelectBinaryOp(I, ISD::ADD);
644 case Instruction::FAdd:
645 return SelectBinaryOp(I, ISD::FADD);
646 case Instruction::Sub:
647 return SelectBinaryOp(I, ISD::SUB);
648 case Instruction::FSub:
Dan Gohman3d45a852009-09-03 22:53:57 +0000649 // FNeg is currently represented in LLVM IR as a special case of FSub.
650 if (BinaryOperator::isFNeg(I))
651 return SelectFNeg(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000652 return SelectBinaryOp(I, ISD::FSUB);
653 case Instruction::Mul:
654 return SelectBinaryOp(I, ISD::MUL);
655 case Instruction::FMul:
656 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman3df24e62008-09-03 23:12:08 +0000657 case Instruction::SDiv:
658 return SelectBinaryOp(I, ISD::SDIV);
659 case Instruction::UDiv:
660 return SelectBinaryOp(I, ISD::UDIV);
661 case Instruction::FDiv:
662 return SelectBinaryOp(I, ISD::FDIV);
663 case Instruction::SRem:
664 return SelectBinaryOp(I, ISD::SREM);
665 case Instruction::URem:
666 return SelectBinaryOp(I, ISD::UREM);
667 case Instruction::FRem:
668 return SelectBinaryOp(I, ISD::FREM);
669 case Instruction::Shl:
670 return SelectBinaryOp(I, ISD::SHL);
671 case Instruction::LShr:
672 return SelectBinaryOp(I, ISD::SRL);
673 case Instruction::AShr:
674 return SelectBinaryOp(I, ISD::SRA);
675 case Instruction::And:
676 return SelectBinaryOp(I, ISD::AND);
677 case Instruction::Or:
678 return SelectBinaryOp(I, ISD::OR);
679 case Instruction::Xor:
680 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000681
Dan Gohman3df24e62008-09-03 23:12:08 +0000682 case Instruction::GetElementPtr:
683 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000684
Dan Gohman3df24e62008-09-03 23:12:08 +0000685 case Instruction::Br: {
Dan Gohman46510a72010-04-15 01:51:59 +0000686 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000687
Dan Gohman3df24e62008-09-03 23:12:08 +0000688 if (BI->isUnconditional()) {
Dan Gohman46510a72010-04-15 01:51:59 +0000689 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohman3df24e62008-09-03 23:12:08 +0000690 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
Dan Gohmand98d6202008-10-02 22:15:21 +0000691 FastEmitBranch(MSucc);
Dan Gohman3df24e62008-09-03 23:12:08 +0000692 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000693 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000694
695 // Conditional branches are not handed yet.
696 // Halt "fast" selection and bail.
697 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000698 }
699
Dan Gohman087c8502008-09-05 01:08:41 +0000700 case Instruction::Unreachable:
701 // Nothing to emit.
702 return true;
703
Dan Gohman0586d912008-09-10 20:11:02 +0000704 case Instruction::Alloca:
705 // FunctionLowering has the static-sized case covered.
706 if (StaticAllocaMap.count(cast<AllocaInst>(I)))
707 return true;
708
709 // Dynamic-sized alloca is not handled yet.
710 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000711
Dan Gohman33134c42008-09-25 17:05:24 +0000712 case Instruction::Call:
713 return SelectCall(I);
714
Dan Gohman3df24e62008-09-03 23:12:08 +0000715 case Instruction::BitCast:
716 return SelectBitCast(I);
717
718 case Instruction::FPToSI:
719 return SelectCast(I, ISD::FP_TO_SINT);
720 case Instruction::ZExt:
721 return SelectCast(I, ISD::ZERO_EXTEND);
722 case Instruction::SExt:
723 return SelectCast(I, ISD::SIGN_EXTEND);
724 case Instruction::Trunc:
725 return SelectCast(I, ISD::TRUNCATE);
726 case Instruction::SIToFP:
727 return SelectCast(I, ISD::SINT_TO_FP);
728
729 case Instruction::IntToPtr: // Deliberate fall-through.
730 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +0000731 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
732 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman3df24e62008-09-03 23:12:08 +0000733 if (DstVT.bitsGT(SrcVT))
734 return SelectCast(I, ISD::ZERO_EXTEND);
735 if (DstVT.bitsLT(SrcVT))
736 return SelectCast(I, ISD::TRUNCATE);
737 unsigned Reg = getRegForValue(I->getOperand(0));
738 if (Reg == 0) return false;
739 UpdateValueMap(I, Reg);
740 return true;
741 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000742
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000743 case Instruction::PHI:
744 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
745
Dan Gohman3df24e62008-09-03 23:12:08 +0000746 default:
747 // Unhandled instruction. Halt "fast" selection and bail.
748 return false;
749 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000750}
751
Dan Gohman3df24e62008-09-03 23:12:08 +0000752FastISel::FastISel(MachineFunction &mf,
753 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +0000754 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmanf81eca02010-04-22 20:46:50 +0000755 DenseMap<const AllocaInst *, int> &am,
756 std::vector<std::pair<MachineInstr*, unsigned> > &pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000757#ifndef NDEBUG
Dan Gohman25208642010-04-14 19:53:31 +0000758 , SmallSet<const Instruction *, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000759#endif
760 )
Dan Gohman3df24e62008-09-03 23:12:08 +0000761 : MBB(0),
762 ValueMap(vm),
763 MBBMap(bm),
Dan Gohman0586d912008-09-10 20:11:02 +0000764 StaticAllocaMap(am),
Dan Gohmanf81eca02010-04-22 20:46:50 +0000765 PHINodesToUpdate(pn),
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000766#ifndef NDEBUG
767 CatchInfoLost(cil),
768#endif
Dan Gohman3df24e62008-09-03 23:12:08 +0000769 MF(mf),
770 MRI(MF.getRegInfo()),
Dan Gohman0586d912008-09-10 20:11:02 +0000771 MFI(*MF.getFrameInfo()),
772 MCP(*MF.getConstantPool()),
Dan Gohman3df24e62008-09-03 23:12:08 +0000773 TM(MF.getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000774 TD(*TM.getTargetData()),
775 TII(*TM.getInstrInfo()),
Owen Andersone922c022009-07-22 00:24:57 +0000776 TLI(*TM.getTargetLowering()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000777}
778
Dan Gohmane285a742008-08-14 21:51:29 +0000779FastISel::~FastISel() {}
780
Owen Anderson825b72b2009-08-11 20:47:22 +0000781unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000782 unsigned) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000783 return 0;
784}
785
Owen Anderson825b72b2009-08-11 20:47:22 +0000786unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000787 unsigned, unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000788 return 0;
789}
790
Owen Anderson825b72b2009-08-11 20:47:22 +0000791unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000792 unsigned, unsigned /*Op0*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000793 unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000794 return 0;
795}
796
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000797unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000798 return 0;
799}
800
Owen Anderson825b72b2009-08-11 20:47:22 +0000801unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohman46510a72010-04-15 01:51:59 +0000802 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000803 return 0;
804}
805
Owen Anderson825b72b2009-08-11 20:47:22 +0000806unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000807 unsigned, unsigned /*Op0*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000808 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000809 return 0;
810}
811
Owen Anderson825b72b2009-08-11 20:47:22 +0000812unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000813 unsigned, unsigned /*Op0*/,
Dan Gohman46510a72010-04-15 01:51:59 +0000814 const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000815 return 0;
816}
817
Owen Anderson825b72b2009-08-11 20:47:22 +0000818unsigned FastISel::FastEmit_rri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000819 unsigned,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000820 unsigned /*Op0*/, unsigned /*Op1*/,
821 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000822 return 0;
823}
824
825/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
826/// to emit an instruction with an immediate operand using FastEmit_ri.
827/// If that fails, it materializes the immediate into a register and try
828/// FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000829unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000830 unsigned Op0, uint64_t Imm,
Owen Anderson825b72b2009-08-11 20:47:22 +0000831 MVT ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000832 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman151ed612008-08-27 18:15:05 +0000833 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000834 if (ResultReg != 0)
835 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000836 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000837 if (MaterialReg == 0)
838 return 0;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000839 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000840}
841
Dan Gohman10df0fa2008-08-27 01:09:54 +0000842/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
843/// to emit an instruction with a floating-point immediate operand using
844/// FastEmit_rf. If that fails, it materializes the immediate into a register
845/// and try FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000846unsigned FastISel::FastEmit_rf_(MVT VT, unsigned Opcode,
Dan Gohman46510a72010-04-15 01:51:59 +0000847 unsigned Op0, const ConstantFP *FPImm,
Owen Anderson825b72b2009-08-11 20:47:22 +0000848 MVT ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000849 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohman151ed612008-08-27 18:15:05 +0000850 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000851 if (ResultReg != 0)
852 return ResultReg;
853
854 // Materialize the constant in a register.
855 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
856 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000857 // If the target doesn't have a way to directly enter a floating-point
858 // value into a register, use an alternate approach.
859 // TODO: The current approach only supports floating-point constants
860 // that can be constructed by conversion from integer values. This should
861 // be replaced by code that creates a load from a constant-pool entry,
862 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000863 const APFloat &Flt = FPImm->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000864 EVT IntVT = TLI.getPointerTy();
Dan Gohman10df0fa2008-08-27 01:09:54 +0000865
866 uint64_t x[2];
867 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000868 bool isExact;
869 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
870 APFloat::rmTowardZero, &isExact);
871 if (!isExact)
Dan Gohman10df0fa2008-08-27 01:09:54 +0000872 return 0;
873 APInt IntVal(IntBitWidth, 2, x);
874
875 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
876 ISD::Constant, IntVal.getZExtValue());
877 if (IntegerReg == 0)
878 return 0;
879 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
880 ISD::SINT_TO_FP, IntegerReg);
881 if (MaterialReg == 0)
882 return 0;
883 }
884 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
885}
886
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000887unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
888 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000889}
890
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000891unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000892 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000893 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000894 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000895
Bill Wendling9bc96a52009-02-03 00:55:04 +0000896 BuildMI(MBB, DL, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000897 return ResultReg;
898}
899
900unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
901 const TargetRegisterClass *RC,
902 unsigned Op0) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000903 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000904 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000905
Evan Cheng5960e4e2008-09-08 08:38:20 +0000906 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +0000907 BuildMI(MBB, DL, II, ResultReg).addReg(Op0);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000908 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +0000909 BuildMI(MBB, DL, II).addReg(Op0);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000910 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
911 II.ImplicitDefs[0], RC, RC);
912 if (!InsertedCopy)
913 ResultReg = 0;
914 }
915
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000916 return ResultReg;
917}
918
919unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
920 const TargetRegisterClass *RC,
921 unsigned Op0, unsigned Op1) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000922 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000923 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000924
Evan Cheng5960e4e2008-09-08 08:38:20 +0000925 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +0000926 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addReg(Op1);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000927 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +0000928 BuildMI(MBB, DL, II).addReg(Op0).addReg(Op1);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000929 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
930 II.ImplicitDefs[0], RC, RC);
931 if (!InsertedCopy)
932 ResultReg = 0;
933 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000934 return ResultReg;
935}
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000936
937unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
938 const TargetRegisterClass *RC,
939 unsigned Op0, uint64_t Imm) {
940 unsigned ResultReg = createResultReg(RC);
941 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
942
Evan Cheng5960e4e2008-09-08 08:38:20 +0000943 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +0000944 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000945 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +0000946 BuildMI(MBB, DL, II).addReg(Op0).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000947 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
948 II.ImplicitDefs[0], RC, RC);
949 if (!InsertedCopy)
950 ResultReg = 0;
951 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000952 return ResultReg;
953}
954
Dan Gohman10df0fa2008-08-27 01:09:54 +0000955unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
956 const TargetRegisterClass *RC,
Dan Gohman46510a72010-04-15 01:51:59 +0000957 unsigned Op0, const ConstantFP *FPImm) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000958 unsigned ResultReg = createResultReg(RC);
959 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
960
Evan Cheng5960e4e2008-09-08 08:38:20 +0000961 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +0000962 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000963 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +0000964 BuildMI(MBB, DL, II).addReg(Op0).addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000965 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
966 II.ImplicitDefs[0], RC, RC);
967 if (!InsertedCopy)
968 ResultReg = 0;
969 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000970 return ResultReg;
971}
972
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000973unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
974 const TargetRegisterClass *RC,
975 unsigned Op0, unsigned Op1, uint64_t Imm) {
976 unsigned ResultReg = createResultReg(RC);
977 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
978
Evan Cheng5960e4e2008-09-08 08:38:20 +0000979 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +0000980 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000981 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +0000982 BuildMI(MBB, DL, II).addReg(Op0).addReg(Op1).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000983 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
984 II.ImplicitDefs[0], RC, RC);
985 if (!InsertedCopy)
986 ResultReg = 0;
987 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000988 return ResultReg;
989}
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000990
991unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
992 const TargetRegisterClass *RC,
993 uint64_t Imm) {
994 unsigned ResultReg = createResultReg(RC);
995 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
996
Evan Cheng5960e4e2008-09-08 08:38:20 +0000997 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +0000998 BuildMI(MBB, DL, II, ResultReg).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000999 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +00001000 BuildMI(MBB, DL, II).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001001 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1002 II.ImplicitDefs[0], RC, RC);
1003 if (!InsertedCopy)
1004 ResultReg = 0;
1005 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001006 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +00001007}
Owen Anderson8970f002008-08-27 22:30:02 +00001008
Owen Anderson825b72b2009-08-11 20:47:22 +00001009unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Evan Cheng536ab132009-01-22 09:10:11 +00001010 unsigned Op0, uint32_t Idx) {
Owen Anderson40a468f2008-08-28 17:47:37 +00001011 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +00001012
Evan Cheng536ab132009-01-22 09:10:11 +00001013 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Chris Lattner518bb532010-02-09 19:54:29 +00001014 const TargetInstrDesc &II = TII.get(TargetOpcode::EXTRACT_SUBREG);
Owen Anderson8970f002008-08-27 22:30:02 +00001015
Evan Cheng5960e4e2008-09-08 08:38:20 +00001016 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +00001017 BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addImm(Idx);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001018 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +00001019 BuildMI(MBB, DL, II).addReg(Op0).addImm(Idx);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001020 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1021 II.ImplicitDefs[0], RC, RC);
1022 if (!InsertedCopy)
1023 ResultReg = 0;
1024 }
Owen Anderson8970f002008-08-27 22:30:02 +00001025 return ResultReg;
1026}
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001027
1028/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1029/// with all but the least significant bit set to zero.
Owen Anderson825b72b2009-08-11 20:47:22 +00001030unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op) {
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001031 return FastEmit_ri(VT, VT, ISD::AND, Op, 1);
1032}
Dan Gohmanf81eca02010-04-22 20:46:50 +00001033
1034/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1035/// Emit code to ensure constants are copied into registers when needed.
1036/// Remember the virtual registers that need to be added to the Machine PHI
1037/// nodes as input. We cannot just directly add them, because expansion
1038/// might result in multiple MBB's for one BB. As such, the start of the
1039/// BB might correspond to a different MBB than the end.
1040bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1041 const TerminatorInst *TI = LLVMBB->getTerminator();
1042
1043 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
1044 unsigned OrigNumPHINodesToUpdate = PHINodesToUpdate.size();
1045
1046 // Check successor nodes' PHI nodes that expect a constant to be available
1047 // from this block.
1048 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1049 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1050 if (!isa<PHINode>(SuccBB->begin())) continue;
1051 MachineBasicBlock *SuccMBB = MBBMap[SuccBB];
1052
1053 // If this terminator has multiple identical successors (common for
1054 // switches), only handle each succ once.
1055 if (!SuccsHandled.insert(SuccMBB)) continue;
1056
1057 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1058
1059 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1060 // nodes and Machine PHI nodes, but the incoming operands have not been
1061 // emitted yet.
1062 for (BasicBlock::const_iterator I = SuccBB->begin();
1063 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
1064 // Ignore dead phi's.
1065 if (PN->use_empty()) continue;
1066
1067 // Only handle legal types. Two interesting things to note here. First,
1068 // by bailing out early, we may leave behind some dead instructions,
1069 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
1070 // own moves. Second, this check is necessary becuase FastISel doesn't
1071 // use CreateRegForValue to create registers, so it always creates
1072 // exactly one register for each non-void instruction.
1073 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1074 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
1075 // Promote MVT::i1.
1076 if (VT == MVT::i1)
1077 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1078 else {
1079 PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
1080 return false;
1081 }
1082 }
1083
1084 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1085
1086 unsigned Reg = getRegForValue(PHIOp);
1087 if (Reg == 0) {
1088 PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
1089 return false;
1090 }
1091 PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
1092 }
1093 }
1094
1095 return true;
1096}