blob: 5a245122775d9845f57716f5394c5811c9a110f1 [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"
Dan Gohman4c3fd9f2010-07-07 16:01:37 +000047#include "llvm/CodeGen/FunctionLoweringInfo.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000048#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman33134c42008-09-25 17:05:24 +000049#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000050#include "llvm/CodeGen/MachineRegisterInfo.h"
Devang Patel83489bb2009-01-13 00:35:13 +000051#include "llvm/Analysis/DebugInfo.h"
Dan Gohman7fbcc982010-07-01 03:49:38 +000052#include "llvm/Analysis/Loads.h"
Evan Cheng83785c82008-08-20 22:45:34 +000053#include "llvm/Target/TargetData.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000054#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000055#include "llvm/Target/TargetLowering.h"
Dan Gohmanbb466332008-08-20 21:05:57 +000056#include "llvm/Target/TargetMachine.h"
Dan Gohmanba5be5c2010-04-20 15:00:41 +000057#include "llvm/Support/ErrorHandling.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000058using namespace llvm;
59
Dan Gohman4df83ed2010-07-07 19:20:32 +000060/// startNewBlock - Set the current block to which generated machine
61/// instructions will be appended, and clear the local CSE map.
62///
63void FastISel::startNewBlock() {
64 LocalValueMap.clear();
65
66 // Start out as end(), meaining no local-value instructions have
67 // been emitted.
68 LastLocalValue = FuncInfo.MBB->end();
69}
70
Dan Gohmana6cb6412010-05-11 23:54:07 +000071bool FastISel::hasTrivialKill(const Value *V) const {
Dan Gohman7f0d6952010-05-14 22:53:18 +000072 // Don't consider constants or arguments to have trivial kills.
Dan Gohmana6cb6412010-05-11 23:54:07 +000073 const Instruction *I = dyn_cast<Instruction>(V);
Dan Gohman7f0d6952010-05-14 22:53:18 +000074 if (!I)
75 return false;
76
77 // No-op casts are trivially coalesced by fast-isel.
78 if (const CastInst *Cast = dyn_cast<CastInst>(I))
79 if (Cast->isNoopCast(TD.getIntPtrType(Cast->getContext())) &&
80 !hasTrivialKill(Cast->getOperand(0)))
81 return false;
82
83 // Only instructions with a single use in the same basic block are considered
84 // to have trivial kills.
85 return I->hasOneUse() &&
86 !(I->getOpcode() == Instruction::BitCast ||
87 I->getOpcode() == Instruction::PtrToInt ||
88 I->getOpcode() == Instruction::IntToPtr) &&
Dan Gohmane1308d82010-05-13 19:19:32 +000089 cast<Instruction>(I->use_begin())->getParent() == I->getParent();
Dan Gohmana6cb6412010-05-11 23:54:07 +000090}
91
Dan Gohman46510a72010-04-15 01:51:59 +000092unsigned FastISel::getRegForValue(const Value *V) {
Owen Andersone50ed302009-08-10 22:56:29 +000093 EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
Dan Gohman4fd55282009-04-07 20:40:11 +000094 // Don't handle non-simple values in FastISel.
95 if (!RealVT.isSimple())
96 return 0;
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +000097
98 // Ignore illegal types. We must do this before looking up the value
99 // in ValueMap because Arguments are given virtual registers regardless
100 // of whether FastISel can handle them.
Owen Anderson825b72b2009-08-11 20:47:22 +0000101 MVT VT = RealVT.getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000102 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000103 // Promote MVT::i1 to a legal type though, because it's common and easy.
104 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +0000105 VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000106 else
107 return 0;
108 }
109
Dan Gohman104e4ce2008-09-03 23:32:19 +0000110 // Look up the value to see if we already have a register for it. We
111 // cache values defined by Instructions across blocks, and other values
112 // only locally. This is because Instructions already have the SSA
Dan Gohman5c9cf192010-01-12 04:30:26 +0000113 // def-dominates-use requirement enforced.
Dan Gohmana4160c32010-07-07 16:29:44 +0000114 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
115 if (I != FuncInfo.ValueMap.end())
Dan Gohmaneddc1142010-05-25 21:59:42 +0000116 return I->second;
Dan Gohman104e4ce2008-09-03 23:32:19 +0000117 unsigned Reg = LocalValueMap[V];
118 if (Reg != 0)
119 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000120
Dan Gohman97c94b82010-05-06 00:02:14 +0000121 // In bottom-up mode, just create the virtual register which will be used
122 // to hold the value. It will be materialized later.
Dan Gohman4df83ed2010-07-07 19:20:32 +0000123 if (isa<Instruction>(V)) {
Dan Gohman97c94b82010-05-06 00:02:14 +0000124 Reg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman4df83ed2010-07-07 19:20:32 +0000125 FuncInfo.ValueMap[V] = Reg;
Dan Gohman97c94b82010-05-06 00:02:14 +0000126 return Reg;
127 }
128
Dan Gohman1fdc6142010-05-03 23:36:34 +0000129 return materializeRegForValue(V, VT);
130}
131
132/// materializeRegForValue - Helper for getRegForVale. This function is
133/// called when the value isn't already available in a register and must
134/// be materialized with new instructions.
135unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
136 unsigned Reg = 0;
137
Dan Gohman46510a72010-04-15 01:51:59 +0000138 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000139 if (CI->getValue().getActiveBits() <= 64)
140 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +0000141 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000142 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +0000143 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman1e9e8c32008-10-07 22:03:27 +0000144 // Translate this as an integer zero so that it can be
145 // local-CSE'd with actual integer zeros.
Owen Anderson1d0be152009-08-13 21:58:54 +0000146 Reg =
147 getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
Dan Gohman46510a72010-04-15 01:51:59 +0000148 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman4183e312010-04-13 17:07:06 +0000149 // Try to emit the constant directly.
Dan Gohman104e4ce2008-09-03 23:32:19 +0000150 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000151
152 if (!Reg) {
Dan Gohman4183e312010-04-13 17:07:06 +0000153 // Try to emit the constant by using an integer constant with a cast.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000154 const APFloat &Flt = CF->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000155 EVT IntVT = TLI.getPointerTy();
Dan Gohmanad368ac2008-08-27 18:10:19 +0000156
157 uint64_t x[2];
158 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000159 bool isExact;
160 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
161 APFloat::rmTowardZero, &isExact);
162 if (isExact) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000163 APInt IntVal(IntBitWidth, 2, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000164
Owen Andersone922c022009-07-22 00:24:57 +0000165 unsigned IntegerReg =
Owen Andersoneed707b2009-07-24 23:12:02 +0000166 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000167 if (IntegerReg != 0)
Dan Gohmana6cb6412010-05-11 23:54:07 +0000168 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
169 IntegerReg, /*Kill=*/false);
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000170 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000171 }
Dan Gohman46510a72010-04-15 01:51:59 +0000172 } else if (const Operator *Op = dyn_cast<Operator>(V)) {
Dan Gohman20d4be12010-07-01 02:58:57 +0000173 if (!SelectOperator(Op, Op->getOpcode()))
174 if (!isa<Instruction>(Op) ||
175 !TargetSelectInstruction(cast<Instruction>(Op)))
176 return 0;
Dan Gohman37db6cd2010-06-21 14:17:46 +0000177 Reg = lookUpRegForValue(Op);
Dan Gohman205d9252008-08-28 21:19:07 +0000178 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000179 Reg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohmaneabaed22010-07-07 16:47:08 +0000180 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
181 TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000182 }
Owen Andersond5d81a42008-09-03 17:51:57 +0000183
Dan Gohmandceffe62008-09-25 01:28:51 +0000184 // If target-independent code couldn't handle the value, give target-specific
185 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000186 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000187 Reg = TargetMaterializeConstant(cast<Constant>(V));
Owen Anderson6e607452008-09-05 23:36:01 +0000188
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000189 // Don't cache constant materializations in the general ValueMap.
190 // To do so would require tracking what uses they dominate.
Dan Gohman4df83ed2010-07-07 19:20:32 +0000191 if (Reg != 0) {
Dan Gohmandceffe62008-09-25 01:28:51 +0000192 LocalValueMap[V] = Reg;
Dan Gohman4df83ed2010-07-07 19:20:32 +0000193 LastLocalValue = MRI.getVRegDef(Reg);
194 }
Dan Gohman104e4ce2008-09-03 23:32:19 +0000195 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000196}
197
Dan Gohman46510a72010-04-15 01:51:59 +0000198unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng59fbc802008-09-09 01:26:59 +0000199 // Look up the value to see if we already have a register for it. We
200 // cache values defined by Instructions across blocks, and other values
201 // only locally. This is because Instructions already have the SSA
Dan Gohman1fdc6142010-05-03 23:36:34 +0000202 // def-dominates-use requirement enforced.
Dan Gohmana4160c32010-07-07 16:29:44 +0000203 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
204 if (I != FuncInfo.ValueMap.end())
Dan Gohman3193a682010-06-21 14:21:47 +0000205 return I->second;
Evan Cheng59fbc802008-09-09 01:26:59 +0000206 return LocalValueMap[V];
207}
208
Owen Andersoncc54e762008-08-30 00:38:46 +0000209/// UpdateValueMap - Update the value map to include the new mapping for this
210/// instruction, or insert an extra copy to get the result in a previous
211/// determined register.
212/// NOTE: This is only necessary because we might select a block that uses
213/// a value before we select the block that defines the value. It might be
214/// possible to fix this by selecting blocks in reverse postorder.
Dan Gohman46510a72010-04-15 01:51:59 +0000215unsigned FastISel::UpdateValueMap(const Value *I, unsigned Reg) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000216 if (!isa<Instruction>(I)) {
217 LocalValueMap[I] = Reg;
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000218 return Reg;
Dan Gohman40b189e2008-09-05 18:18:20 +0000219 }
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000220
Dan Gohmana4160c32010-07-07 16:29:44 +0000221 unsigned &AssignedReg = FuncInfo.ValueMap[I];
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000222 if (AssignedReg == 0)
Dan Gohman4df83ed2010-07-07 19:20:32 +0000223 // Use the new register.
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000224 AssignedReg = Reg;
Chris Lattner36e39462009-04-12 07:46:30 +0000225 else if (Reg != AssignedReg) {
Dan Gohman4df83ed2010-07-07 19:20:32 +0000226 // We already have a register for this value. Replace uses of
227 // the existing register with uses of the new one.
228 MRI.replaceRegWith(AssignedReg, Reg);
229 // Replace uses of the existing register in PHINodesToUpdate too.
230 for (unsigned i = 0, e = FuncInfo.PHINodesToUpdate.size(); i != e; ++i)
231 if (FuncInfo.PHINodesToUpdate[i].second == AssignedReg)
232 FuncInfo.PHINodesToUpdate[i].second = Reg;
233 // And update the ValueMap.
234 AssignedReg = Reg;
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000235 }
Dan Gohman4df83ed2010-07-07 19:20:32 +0000236
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000237 return AssignedReg;
Owen Andersoncc54e762008-08-30 00:38:46 +0000238}
239
Dan Gohmana6cb6412010-05-11 23:54:07 +0000240std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000241 unsigned IdxN = getRegForValue(Idx);
242 if (IdxN == 0)
243 // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000244 return std::pair<unsigned, bool>(0, false);
245
246 bool IdxNIsKill = hasTrivialKill(Idx);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000247
248 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Anderson766b5ef2009-08-11 21:59:30 +0000249 MVT PtrVT = TLI.getPointerTy();
Owen Andersone50ed302009-08-10 22:56:29 +0000250 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000251 if (IdxVT.bitsLT(PtrVT)) {
252 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND,
253 IdxN, IdxNIsKill);
254 IdxNIsKill = true;
255 }
256 else if (IdxVT.bitsGT(PtrVT)) {
257 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE,
258 IdxN, IdxNIsKill);
259 IdxNIsKill = true;
260 }
261 return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000262}
263
Dan Gohmanbdedd442008-08-20 00:11:48 +0000264/// SelectBinaryOp - Select and emit code for a binary operator instruction,
265/// which has an opcode which directly corresponds to the given ISD opcode.
266///
Dan Gohman46510a72010-04-15 01:51:59 +0000267bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000268 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000269 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmanbdedd442008-08-20 00:11:48 +0000270 // Unhandled type. Halt "fast" selection and bail.
271 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000272
Dan Gohmanb71fea22008-08-26 20:52:40 +0000273 // We only handle legal types. For example, on x86-32 the instruction
274 // selector contains all of the 64-bit instructions from x86-64,
275 // under the assumption that i64 won't be used if the target doesn't
276 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000277 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000279 // don't require additional zeroing, which makes them easy.
Owen Anderson825b72b2009-08-11 20:47:22 +0000280 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000281 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
282 ISDOpcode == ISD::XOR))
Owen Anderson23b9b192009-08-12 00:36:31 +0000283 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohman638c6832008-09-05 18:44:22 +0000284 else
285 return false;
286 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000287
Dan Gohman3df24e62008-09-03 23:12:08 +0000288 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000289 if (Op0 == 0)
290 // Unhandled operand. Halt "fast" selection and bail.
291 return false;
292
Dan Gohmana6cb6412010-05-11 23:54:07 +0000293 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
294
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000295 // Check if the second operand is a constant and handle it appropriately.
296 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000297 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000298 ISDOpcode, Op0, Op0IsKill,
299 CI->getZExtValue());
Dan Gohmanad368ac2008-08-27 18:10:19 +0000300 if (ResultReg != 0) {
301 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000302 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000303 return true;
304 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000305 }
306
Dan Gohman10df0fa2008-08-27 01:09:54 +0000307 // Check if the second operand is a constant float.
308 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000309 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000310 ISDOpcode, Op0, Op0IsKill, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000311 if (ResultReg != 0) {
312 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000313 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000314 return true;
315 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000316 }
317
Dan Gohman3df24e62008-09-03 23:12:08 +0000318 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000319 if (Op1 == 0)
320 // Unhandled operand. Halt "fast" selection and bail.
321 return false;
322
Dan Gohmana6cb6412010-05-11 23:54:07 +0000323 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
324
Dan Gohmanad368ac2008-08-27 18:10:19 +0000325 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000326 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000327 ISDOpcode,
328 Op0, Op0IsKill,
329 Op1, Op1IsKill);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000330 if (ResultReg == 0)
331 // Target-specific code wasn't able to find a machine opcode for
332 // the given ISD opcode and type. Halt "fast" selection and bail.
333 return false;
334
Dan Gohman8014e862008-08-20 00:23:20 +0000335 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000336 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000337 return true;
338}
339
Dan Gohman46510a72010-04-15 01:51:59 +0000340bool FastISel::SelectGetElementPtr(const User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000341 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000342 if (N == 0)
343 // Unhandled operand. Halt "fast" selection and bail.
344 return false;
345
Dan Gohmana6cb6412010-05-11 23:54:07 +0000346 bool NIsKill = hasTrivialKill(I->getOperand(0));
347
Evan Cheng83785c82008-08-20 22:45:34 +0000348 const Type *Ty = I->getOperand(0)->getType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 MVT VT = TLI.getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +0000350 for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
351 E = I->op_end(); OI != E; ++OI) {
352 const Value *Idx = *OI;
Evan Cheng83785c82008-08-20 22:45:34 +0000353 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
354 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
355 if (Field) {
356 // N = N + Offset
357 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
358 // FIXME: This can be optimized by combining the add with a
359 // subsequent one.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000360 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000361 if (N == 0)
362 // Unhandled operand. Halt "fast" selection and bail.
363 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000364 NIsKill = true;
Evan Cheng83785c82008-08-20 22:45:34 +0000365 }
366 Ty = StTy->getElementType(Field);
367 } else {
368 Ty = cast<SequentialType>(Ty)->getElementType();
369
370 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +0000371 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +0000372 if (CI->isZero()) continue;
Evan Cheng83785c82008-08-20 22:45:34 +0000373 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +0000374 TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohmana6cb6412010-05-11 23:54:07 +0000375 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000376 if (N == 0)
377 // Unhandled operand. Halt "fast" selection and bail.
378 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000379 NIsKill = true;
Evan Cheng83785c82008-08-20 22:45:34 +0000380 continue;
381 }
382
383 // N = N + Idx * ElementSize;
Duncan Sands777d2302009-05-09 07:06:46 +0000384 uint64_t ElementSize = TD.getTypeAllocSize(Ty);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000385 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
386 unsigned IdxN = Pair.first;
387 bool IdxNIsKill = Pair.second;
Evan Cheng83785c82008-08-20 22:45:34 +0000388 if (IdxN == 0)
389 // Unhandled operand. Halt "fast" selection and bail.
390 return false;
391
Dan Gohman80bc6e22008-08-26 20:57:08 +0000392 if (ElementSize != 1) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000393 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000394 if (IdxN == 0)
395 // Unhandled operand. Halt "fast" selection and bail.
396 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000397 IdxNIsKill = true;
Dan Gohman80bc6e22008-08-26 20:57:08 +0000398 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000399 N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
Evan Cheng83785c82008-08-20 22:45:34 +0000400 if (N == 0)
401 // Unhandled operand. Halt "fast" selection and bail.
402 return false;
403 }
404 }
405
406 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000407 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000408 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000409}
410
Dan Gohman46510a72010-04-15 01:51:59 +0000411bool FastISel::SelectCall(const User *I) {
412 const Function *F = cast<CallInst>(I)->getCalledFunction();
Dan Gohman33134c42008-09-25 17:05:24 +0000413 if (!F) return false;
414
Dan Gohman4183e312010-04-13 17:07:06 +0000415 // Handle selected intrinsic function calls.
Dan Gohman33134c42008-09-25 17:05:24 +0000416 unsigned IID = F->getIntrinsicID();
417 switch (IID) {
418 default: break;
Bill Wendling92c1e122009-02-13 02:16:35 +0000419 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +0000420 const DbgDeclareInst *DI = cast<DbgDeclareInst>(I);
Devang Patel02f0dbd2010-05-07 22:04:20 +0000421 if (!DIVariable(DI->getVariable()).Verify() ||
Dan Gohmana4160c32010-07-07 16:29:44 +0000422 !FuncInfo.MF->getMMI().hasDebugInfo())
Devang Patel7e1e31f2009-07-02 22:43:26 +0000423 return true;
424
Dan Gohman46510a72010-04-15 01:51:59 +0000425 const Value *Address = DI->getAddress();
Dale Johannesendc918562010-02-06 02:26:02 +0000426 if (!Address)
427 return true;
Dale Johannesen343b42e2010-04-07 01:15:14 +0000428 if (isa<UndefValue>(Address))
429 return true;
Dan Gohman46510a72010-04-15 01:51:59 +0000430 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
Devang Patel7e1e31f2009-07-02 22:43:26 +0000431 // Don't handle byval struct arguments or VLAs, for example.
Dale Johannesen7dc78402010-04-25 21:03:54 +0000432 // Note that if we have a byval struct argument, fast ISel is turned off;
433 // those are handled in SelectionDAGBuilder.
Devang Patel54fc4d62010-04-28 19:27:33 +0000434 if (AI) {
435 DenseMap<const AllocaInst*, int>::iterator SI =
Dan Gohmana4160c32010-07-07 16:29:44 +0000436 FuncInfo.StaticAllocaMap.find(AI);
437 if (SI == FuncInfo.StaticAllocaMap.end()) break; // VLAs.
Devang Patel54fc4d62010-04-28 19:27:33 +0000438 int FI = SI->second;
439 if (!DI->getDebugLoc().isUnknown())
Dan Gohmana4160c32010-07-07 16:29:44 +0000440 FuncInfo.MF->getMMI().setVariableDbgInfo(DI->getVariable(),
441 FI, DI->getDebugLoc());
Devang Patel54fc4d62010-04-28 19:27:33 +0000442 } else
443 // Building the map above is target independent. Generating DBG_VALUE
444 // inline is target dependent; do this now.
445 (void)TargetSelectInstruction(cast<Instruction>(I));
Dan Gohman33134c42008-09-25 17:05:24 +0000446 return true;
Bill Wendling92c1e122009-02-13 02:16:35 +0000447 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000448 case Intrinsic::dbg_value: {
Dale Johannesen343b42e2010-04-07 01:15:14 +0000449 // This form of DBG_VALUE is target-independent.
Dan Gohman46510a72010-04-15 01:51:59 +0000450 const DbgValueInst *DI = cast<DbgValueInst>(I);
Dale Johannesen45df7612010-02-26 20:01:55 +0000451 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohman46510a72010-04-15 01:51:59 +0000452 const Value *V = DI->getValue();
Dale Johannesen45df7612010-02-26 20:01:55 +0000453 if (!V) {
454 // Currently the optimizer can produce this; insert an undef to
455 // help debugging. Probably the optimizer should not do this.
Dan Gohmaneabaed22010-07-07 16:47:08 +0000456 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
457 .addReg(0U).addImm(DI->getOffset())
458 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000459 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohmaneabaed22010-07-07 16:47:08 +0000460 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
461 .addImm(CI->getZExtValue()).addImm(DI->getOffset())
462 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000463 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohmaneabaed22010-07-07 16:47:08 +0000464 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
465 .addFPImm(CF).addImm(DI->getOffset())
466 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000467 } else if (unsigned Reg = lookUpRegForValue(V)) {
Dan Gohmaneabaed22010-07-07 16:47:08 +0000468 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
469 .addReg(Reg, RegState::Debug).addImm(DI->getOffset())
470 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000471 } else {
472 // We can't yet handle anything else here because it would require
473 // generating code, thus altering codegen because of debug info.
474 // Insert an undef so we can see what we dropped.
Dan Gohmaneabaed22010-07-07 16:47:08 +0000475 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
476 .addReg(0U).addImm(DI->getOffset())
477 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000478 }
479 return true;
480 }
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000481 case Intrinsic::eh_exception: {
Owen Andersone50ed302009-08-10 22:56:29 +0000482 EVT VT = TLI.getValueType(I->getType());
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000483 switch (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)) {
484 default: break;
485 case TargetLowering::Expand: {
Dan Gohmaneabaed22010-07-07 16:47:08 +0000486 assert(FuncInfo.MBB->isLandingPad() &&
487 "Call to eh.exception not in landing pad!");
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000488 unsigned Reg = TLI.getExceptionAddressRegister();
489 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
490 unsigned ResultReg = createResultReg(RC);
Dan Gohmaneabaed22010-07-07 16:47:08 +0000491 bool InsertedCopy = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
492 ResultReg, Reg, RC, RC, DL);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000493 assert(InsertedCopy && "Can't copy address registers!");
Evan Cheng24ac4082008-11-24 07:09:49 +0000494 InsertedCopy = InsertedCopy;
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000495 UpdateValueMap(I, ResultReg);
496 return true;
497 }
498 }
499 break;
500 }
Duncan Sandsb01bbdc2009-10-14 16:11:37 +0000501 case Intrinsic::eh_selector: {
Owen Andersone50ed302009-08-10 22:56:29 +0000502 EVT VT = TLI.getValueType(I->getType());
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000503 switch (TLI.getOperationAction(ISD::EHSELECTION, VT)) {
504 default: break;
505 case TargetLowering::Expand: {
Dan Gohmaneabaed22010-07-07 16:47:08 +0000506 if (FuncInfo.MBB->isLandingPad())
507 AddCatchInfo(*cast<CallInst>(I), &FuncInfo.MF->getMMI(), FuncInfo.MBB);
Chris Lattnered3a8062010-04-05 06:05:26 +0000508 else {
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000509#ifndef NDEBUG
Dan Gohmana4160c32010-07-07 16:29:44 +0000510 FuncInfo.CatchInfoLost.insert(cast<CallInst>(I));
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000511#endif
Chris Lattnered3a8062010-04-05 06:05:26 +0000512 // FIXME: Mark exception selector register as live in. Hack for PR1508.
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000513 unsigned Reg = TLI.getExceptionSelectorRegister();
Dan Gohmaneabaed22010-07-07 16:47:08 +0000514 if (Reg) FuncInfo.MBB->addLiveIn(Reg);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000515 }
Chris Lattnered3a8062010-04-05 06:05:26 +0000516
517 unsigned Reg = TLI.getExceptionSelectorRegister();
518 EVT SrcVT = TLI.getPointerTy();
519 const TargetRegisterClass *RC = TLI.getRegClassFor(SrcVT);
520 unsigned ResultReg = createResultReg(RC);
Dan Gohmaneabaed22010-07-07 16:47:08 +0000521 bool InsertedCopy = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
522 ResultReg, Reg, RC, RC, DL);
Chris Lattnered3a8062010-04-05 06:05:26 +0000523 assert(InsertedCopy && "Can't copy address registers!");
524 InsertedCopy = InsertedCopy;
525
Dan Gohmana6cb6412010-05-11 23:54:07 +0000526 bool ResultRegIsKill = hasTrivialKill(I);
527
Chris Lattnered3a8062010-04-05 06:05:26 +0000528 // Cast the register to the type of the selector.
529 if (SrcVT.bitsGT(MVT::i32))
530 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, ISD::TRUNCATE,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000531 ResultReg, ResultRegIsKill);
Chris Lattnered3a8062010-04-05 06:05:26 +0000532 else if (SrcVT.bitsLT(MVT::i32))
533 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000534 ISD::SIGN_EXTEND, ResultReg, ResultRegIsKill);
Chris Lattnered3a8062010-04-05 06:05:26 +0000535 if (ResultReg == 0)
536 // Unhandled operand. Halt "fast" selection and bail.
537 return false;
538
539 UpdateValueMap(I, ResultReg);
540
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000541 return true;
542 }
543 }
544 break;
545 }
Dan Gohman33134c42008-09-25 17:05:24 +0000546 }
Dan Gohman4183e312010-04-13 17:07:06 +0000547
548 // An arbitrary call. Bail.
Dan Gohman33134c42008-09-25 17:05:24 +0000549 return false;
550}
551
Dan Gohman46510a72010-04-15 01:51:59 +0000552bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000553 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
554 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000555
Owen Anderson825b72b2009-08-11 20:47:22 +0000556 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
557 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersond0533c92008-08-26 23:46:32 +0000558 // Unhandled type. Halt "fast" selection and bail.
559 return false;
560
Dan Gohman474d3b32009-03-13 23:53:06 +0000561 // Check if the destination type is legal. Or as a special case,
562 // it may be i1 if we're doing a truncate because that's
563 // easy and somewhat common.
564 if (!TLI.isTypeLegal(DstVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000565 if (DstVT != MVT::i1 || Opcode != ISD::TRUNCATE)
Dan Gohman91b6f972008-10-03 01:28:47 +0000566 // Unhandled type. Halt "fast" selection and bail.
567 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000568
569 // Check if the source operand is legal. Or as a special case,
570 // it may be i1 if we're doing zero-extension because that's
571 // easy and somewhat common.
572 if (!TLI.isTypeLegal(SrcVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 if (SrcVT != MVT::i1 || Opcode != ISD::ZERO_EXTEND)
Dan Gohman474d3b32009-03-13 23:53:06 +0000574 // Unhandled type. Halt "fast" selection and bail.
575 return false;
576
Dan Gohman3df24e62008-09-03 23:12:08 +0000577 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000578 if (!InputReg)
579 // Unhandled operand. Halt "fast" selection and bail.
580 return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000581
Dan Gohmana6cb6412010-05-11 23:54:07 +0000582 bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
583
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000584 // If the operand is i1, arrange for the high bits in the register to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +0000585 if (SrcVT == MVT::i1) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000586 SrcVT = TLI.getTypeToTransformTo(I->getContext(), SrcVT);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000587 InputReg = FastEmitZExtFromI1(SrcVT.getSimpleVT(), InputReg, InputRegIsKill);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000588 if (!InputReg)
589 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000590 InputRegIsKill = true;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000591 }
Dan Gohman474d3b32009-03-13 23:53:06 +0000592 // If the result is i1, truncate to the target's type for i1 first.
Owen Anderson825b72b2009-08-11 20:47:22 +0000593 if (DstVT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +0000594 DstVT = TLI.getTypeToTransformTo(I->getContext(), DstVT);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000595
Owen Andersond0533c92008-08-26 23:46:32 +0000596 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
597 DstVT.getSimpleVT(),
598 Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000599 InputReg, InputRegIsKill);
Owen Andersond0533c92008-08-26 23:46:32 +0000600 if (!ResultReg)
601 return false;
602
Dan Gohman3df24e62008-09-03 23:12:08 +0000603 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000604 return true;
605}
606
Dan Gohman46510a72010-04-15 01:51:59 +0000607bool FastISel::SelectBitCast(const User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000608 // If the bitcast doesn't change the type, just use the operand value.
609 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000610 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000611 if (Reg == 0)
612 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000613 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000614 return true;
615 }
616
617 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Andersone50ed302009-08-10 22:56:29 +0000618 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
619 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000620
Owen Anderson825b72b2009-08-11 20:47:22 +0000621 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
622 DstVT == MVT::Other || !DstVT.isSimple() ||
Owen Andersond0533c92008-08-26 23:46:32 +0000623 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
624 // Unhandled type. Halt "fast" selection and bail.
625 return false;
626
Dan Gohman3df24e62008-09-03 23:12:08 +0000627 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000628 if (Op0 == 0)
629 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000630 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000631
632 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000633
Dan Gohmanad368ac2008-08-27 18:10:19 +0000634 // First, try to perform the bitcast by inserting a reg-reg copy.
635 unsigned ResultReg = 0;
636 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
637 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
638 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
639 ResultReg = createResultReg(DstClass);
640
Dan Gohmaneabaed22010-07-07 16:47:08 +0000641 bool InsertedCopy = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
642 ResultReg, Op0,
643 DstClass, SrcClass, DL);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000644 if (!InsertedCopy)
645 ResultReg = 0;
646 }
647
648 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
649 if (!ResultReg)
650 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000651 ISD::BIT_CONVERT, Op0, Op0IsKill);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000652
653 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000654 return false;
655
Dan Gohman3df24e62008-09-03 23:12:08 +0000656 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000657 return true;
658}
659
Dan Gohman3df24e62008-09-03 23:12:08 +0000660bool
Dan Gohman46510a72010-04-15 01:51:59 +0000661FastISel::SelectInstruction(const Instruction *I) {
Dan Gohmane8c92dd2010-04-23 15:29:50 +0000662 // Just before the terminator instruction, insert instructions to
663 // feed PHI nodes in successor blocks.
664 if (isa<TerminatorInst>(I))
665 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
666 return false;
667
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000668 DL = I->getDebugLoc();
669
Dan Gohman6e3ff372009-12-05 01:27:58 +0000670 // First, try doing target-independent selection.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000671 if (SelectOperator(I, I->getOpcode())) {
672 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000673 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000674 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000675
676 // Next, try calling the target to attempt to handle the instruction.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000677 if (TargetSelectInstruction(I)) {
678 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000679 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000680 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000681
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000682 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000683 return false;
Dan Gohman40b189e2008-09-05 18:18:20 +0000684}
685
Dan Gohmand98d6202008-10-02 22:15:21 +0000686/// FastEmitBranch - Emit an unconditional branch to the given block,
687/// unless it is the immediate (fall-through) successor, and update
688/// the CFG.
689void
Stuart Hastings3bf91252010-06-17 22:43:56 +0000690FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DL) {
Dan Gohmaneabaed22010-07-07 16:47:08 +0000691 if (FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000692 // The unconditional fall-through case, which needs no instructions.
693 } else {
694 // The unconditional branch case.
Dan Gohmaneabaed22010-07-07 16:47:08 +0000695 TII.InsertBranch(*FuncInfo.MBB, MSucc, NULL,
696 SmallVector<MachineOperand, 0>(), DL);
Dan Gohmand98d6202008-10-02 22:15:21 +0000697 }
Dan Gohmaneabaed22010-07-07 16:47:08 +0000698 FuncInfo.MBB->addSuccessor(MSucc);
Dan Gohmand98d6202008-10-02 22:15:21 +0000699}
700
Dan Gohman3d45a852009-09-03 22:53:57 +0000701/// SelectFNeg - Emit an FNeg operation.
702///
703bool
Dan Gohman46510a72010-04-15 01:51:59 +0000704FastISel::SelectFNeg(const User *I) {
Dan Gohman3d45a852009-09-03 22:53:57 +0000705 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
706 if (OpReg == 0) return false;
707
Dan Gohmana6cb6412010-05-11 23:54:07 +0000708 bool OpRegIsKill = hasTrivialKill(I);
709
Dan Gohman4a215a12009-09-11 00:36:43 +0000710 // If the target has ISD::FNEG, use it.
711 EVT VT = TLI.getValueType(I->getType());
712 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000713 ISD::FNEG, OpReg, OpRegIsKill);
Dan Gohman4a215a12009-09-11 00:36:43 +0000714 if (ResultReg != 0) {
715 UpdateValueMap(I, ResultReg);
716 return true;
717 }
718
Dan Gohman5e5abb72009-09-11 00:34:46 +0000719 // Bitcast the value to integer, twiddle the sign bit with xor,
720 // and then bitcast it back to floating-point.
Dan Gohman3d45a852009-09-03 22:53:57 +0000721 if (VT.getSizeInBits() > 64) return false;
Dan Gohman5e5abb72009-09-11 00:34:46 +0000722 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
723 if (!TLI.isTypeLegal(IntVT))
724 return false;
725
726 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000727 ISD::BIT_CONVERT, OpReg, OpRegIsKill);
Dan Gohman5e5abb72009-09-11 00:34:46 +0000728 if (IntReg == 0)
729 return false;
730
Dan Gohmana6cb6412010-05-11 23:54:07 +0000731 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
732 IntReg, /*Kill=*/true,
Dan Gohman5e5abb72009-09-11 00:34:46 +0000733 UINT64_C(1) << (VT.getSizeInBits()-1),
734 IntVT.getSimpleVT());
735 if (IntResultReg == 0)
736 return false;
737
738 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000739 ISD::BIT_CONVERT, IntResultReg, /*Kill=*/true);
Dan Gohman3d45a852009-09-03 22:53:57 +0000740 if (ResultReg == 0)
741 return false;
742
743 UpdateValueMap(I, ResultReg);
744 return true;
745}
746
Dan Gohman40b189e2008-09-05 18:18:20 +0000747bool
Dan Gohman7fbcc982010-07-01 03:49:38 +0000748FastISel::SelectLoad(const User *I) {
749 LoadInst *LI = const_cast<LoadInst *>(cast<LoadInst>(I));
750
751 // For a load from an alloca, make a limited effort to find the value
752 // already available in a register, avoiding redundant loads.
753 if (!LI->isVolatile() && isa<AllocaInst>(LI->getPointerOperand())) {
754 BasicBlock::iterator ScanFrom = LI;
755 if (const Value *V = FindAvailableLoadedValue(LI->getPointerOperand(),
756 LI->getParent(), ScanFrom)) {
Dan Gohman4df83ed2010-07-07 19:20:32 +0000757 if (!isa<Instruction>(V) ||
758 cast<Instruction>(V)->getParent() == LI->getParent() ||
759 (isa<AllocaInst>(V) && FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V)))) {
Dan Gohman7fbcc982010-07-01 03:49:38 +0000760 unsigned ResultReg = getRegForValue(V);
761 if (ResultReg != 0) {
762 UpdateValueMap(I, ResultReg);
763 return true;
764 }
Dan Gohman4df83ed2010-07-07 19:20:32 +0000765 }
Dan Gohman7fbcc982010-07-01 03:49:38 +0000766 }
767 }
768
769 return false;
770}
771
772bool
Dan Gohman46510a72010-04-15 01:51:59 +0000773FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000774 switch (Opcode) {
Dan Gohman7fbcc982010-07-01 03:49:38 +0000775 case Instruction::Load:
776 return SelectLoad(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000777 case Instruction::Add:
778 return SelectBinaryOp(I, ISD::ADD);
779 case Instruction::FAdd:
780 return SelectBinaryOp(I, ISD::FADD);
781 case Instruction::Sub:
782 return SelectBinaryOp(I, ISD::SUB);
783 case Instruction::FSub:
Dan Gohman3d45a852009-09-03 22:53:57 +0000784 // FNeg is currently represented in LLVM IR as a special case of FSub.
785 if (BinaryOperator::isFNeg(I))
786 return SelectFNeg(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000787 return SelectBinaryOp(I, ISD::FSUB);
788 case Instruction::Mul:
789 return SelectBinaryOp(I, ISD::MUL);
790 case Instruction::FMul:
791 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman3df24e62008-09-03 23:12:08 +0000792 case Instruction::SDiv:
793 return SelectBinaryOp(I, ISD::SDIV);
794 case Instruction::UDiv:
795 return SelectBinaryOp(I, ISD::UDIV);
796 case Instruction::FDiv:
797 return SelectBinaryOp(I, ISD::FDIV);
798 case Instruction::SRem:
799 return SelectBinaryOp(I, ISD::SREM);
800 case Instruction::URem:
801 return SelectBinaryOp(I, ISD::UREM);
802 case Instruction::FRem:
803 return SelectBinaryOp(I, ISD::FREM);
804 case Instruction::Shl:
805 return SelectBinaryOp(I, ISD::SHL);
806 case Instruction::LShr:
807 return SelectBinaryOp(I, ISD::SRL);
808 case Instruction::AShr:
809 return SelectBinaryOp(I, ISD::SRA);
810 case Instruction::And:
811 return SelectBinaryOp(I, ISD::AND);
812 case Instruction::Or:
813 return SelectBinaryOp(I, ISD::OR);
814 case Instruction::Xor:
815 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000816
Dan Gohman3df24e62008-09-03 23:12:08 +0000817 case Instruction::GetElementPtr:
818 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000819
Dan Gohman3df24e62008-09-03 23:12:08 +0000820 case Instruction::Br: {
Dan Gohman46510a72010-04-15 01:51:59 +0000821 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000822
Dan Gohman3df24e62008-09-03 23:12:08 +0000823 if (BI->isUnconditional()) {
Dan Gohman46510a72010-04-15 01:51:59 +0000824 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohmana4160c32010-07-07 16:29:44 +0000825 MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
Stuart Hastings3bf91252010-06-17 22:43:56 +0000826 FastEmitBranch(MSucc, BI->getDebugLoc());
Dan Gohman3df24e62008-09-03 23:12:08 +0000827 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000828 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000829
830 // Conditional branches are not handed yet.
831 // Halt "fast" selection and bail.
832 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000833 }
834
Dan Gohman087c8502008-09-05 01:08:41 +0000835 case Instruction::Unreachable:
836 // Nothing to emit.
837 return true;
838
Dan Gohman0586d912008-09-10 20:11:02 +0000839 case Instruction::Alloca:
840 // FunctionLowering has the static-sized case covered.
Dan Gohmana4160c32010-07-07 16:29:44 +0000841 if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
Dan Gohman0586d912008-09-10 20:11:02 +0000842 return true;
843
844 // Dynamic-sized alloca is not handled yet.
845 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000846
Dan Gohman33134c42008-09-25 17:05:24 +0000847 case Instruction::Call:
848 return SelectCall(I);
849
Dan Gohman3df24e62008-09-03 23:12:08 +0000850 case Instruction::BitCast:
851 return SelectBitCast(I);
852
853 case Instruction::FPToSI:
854 return SelectCast(I, ISD::FP_TO_SINT);
855 case Instruction::ZExt:
856 return SelectCast(I, ISD::ZERO_EXTEND);
857 case Instruction::SExt:
858 return SelectCast(I, ISD::SIGN_EXTEND);
859 case Instruction::Trunc:
860 return SelectCast(I, ISD::TRUNCATE);
861 case Instruction::SIToFP:
862 return SelectCast(I, ISD::SINT_TO_FP);
863
864 case Instruction::IntToPtr: // Deliberate fall-through.
865 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +0000866 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
867 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman3df24e62008-09-03 23:12:08 +0000868 if (DstVT.bitsGT(SrcVT))
869 return SelectCast(I, ISD::ZERO_EXTEND);
870 if (DstVT.bitsLT(SrcVT))
871 return SelectCast(I, ISD::TRUNCATE);
872 unsigned Reg = getRegForValue(I->getOperand(0));
873 if (Reg == 0) return false;
874 UpdateValueMap(I, Reg);
875 return true;
876 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000877
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000878 case Instruction::PHI:
879 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
880
Dan Gohman3df24e62008-09-03 23:12:08 +0000881 default:
882 // Unhandled instruction. Halt "fast" selection and bail.
883 return false;
884 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000885}
886
Dan Gohmana4160c32010-07-07 16:29:44 +0000887FastISel::FastISel(FunctionLoweringInfo &funcInfo)
Dan Gohmaneabaed22010-07-07 16:47:08 +0000888 : FuncInfo(funcInfo),
Dan Gohmana4160c32010-07-07 16:29:44 +0000889 MRI(FuncInfo.MF->getRegInfo()),
890 MFI(*FuncInfo.MF->getFrameInfo()),
891 MCP(*FuncInfo.MF->getConstantPool()),
892 TM(FuncInfo.MF->getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000893 TD(*TM.getTargetData()),
894 TII(*TM.getInstrInfo()),
Dan Gohmana7a0ed72010-05-05 23:58:35 +0000895 TLI(*TM.getTargetLowering()),
Dan Gohman4df83ed2010-07-07 19:20:32 +0000896 TRI(*TM.getRegisterInfo()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000897}
898
Dan Gohmane285a742008-08-14 21:51:29 +0000899FastISel::~FastISel() {}
900
Owen Anderson825b72b2009-08-11 20:47:22 +0000901unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000902 unsigned) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000903 return 0;
904}
905
Owen Anderson825b72b2009-08-11 20:47:22 +0000906unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000907 unsigned,
908 unsigned /*Op0*/, bool /*Op0IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000909 return 0;
910}
911
Owen Anderson825b72b2009-08-11 20:47:22 +0000912unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000913 unsigned,
914 unsigned /*Op0*/, bool /*Op0IsKill*/,
915 unsigned /*Op1*/, bool /*Op1IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000916 return 0;
917}
918
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000919unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000920 return 0;
921}
922
Owen Anderson825b72b2009-08-11 20:47:22 +0000923unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohman46510a72010-04-15 01:51:59 +0000924 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000925 return 0;
926}
927
Owen Anderson825b72b2009-08-11 20:47:22 +0000928unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000929 unsigned,
930 unsigned /*Op0*/, bool /*Op0IsKill*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000931 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000932 return 0;
933}
934
Owen Anderson825b72b2009-08-11 20:47:22 +0000935unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000936 unsigned,
937 unsigned /*Op0*/, bool /*Op0IsKill*/,
Dan Gohman46510a72010-04-15 01:51:59 +0000938 const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000939 return 0;
940}
941
Owen Anderson825b72b2009-08-11 20:47:22 +0000942unsigned FastISel::FastEmit_rri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000943 unsigned,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000944 unsigned /*Op0*/, bool /*Op0IsKill*/,
945 unsigned /*Op1*/, bool /*Op1IsKill*/,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000946 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000947 return 0;
948}
949
950/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
951/// to emit an instruction with an immediate operand using FastEmit_ri.
952/// If that fails, it materializes the immediate into a register and try
953/// FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000954unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000955 unsigned Op0, bool Op0IsKill,
956 uint64_t Imm, MVT ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000957 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000958 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000959 if (ResultReg != 0)
960 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000961 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000962 if (MaterialReg == 0)
963 return 0;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000964 return FastEmit_rr(VT, VT, Opcode,
965 Op0, Op0IsKill,
966 MaterialReg, /*Kill=*/true);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000967}
968
Dan Gohman10df0fa2008-08-27 01:09:54 +0000969/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
970/// to emit an instruction with a floating-point immediate operand using
971/// FastEmit_rf. If that fails, it materializes the immediate into a register
972/// and try FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000973unsigned FastISel::FastEmit_rf_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000974 unsigned Op0, bool Op0IsKill,
975 const ConstantFP *FPImm, MVT ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000976 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000977 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, Op0IsKill, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000978 if (ResultReg != 0)
979 return ResultReg;
980
981 // Materialize the constant in a register.
982 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
983 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000984 // If the target doesn't have a way to directly enter a floating-point
985 // value into a register, use an alternate approach.
986 // TODO: The current approach only supports floating-point constants
987 // that can be constructed by conversion from integer values. This should
988 // be replaced by code that creates a load from a constant-pool entry,
989 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000990 const APFloat &Flt = FPImm->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000991 EVT IntVT = TLI.getPointerTy();
Dan Gohman10df0fa2008-08-27 01:09:54 +0000992
993 uint64_t x[2];
994 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000995 bool isExact;
996 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
997 APFloat::rmTowardZero, &isExact);
998 if (!isExact)
Dan Gohman10df0fa2008-08-27 01:09:54 +0000999 return 0;
1000 APInt IntVal(IntBitWidth, 2, x);
1001
1002 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
1003 ISD::Constant, IntVal.getZExtValue());
1004 if (IntegerReg == 0)
1005 return 0;
1006 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001007 ISD::SINT_TO_FP, IntegerReg, /*Kill=*/true);
Dan Gohman10df0fa2008-08-27 01:09:54 +00001008 if (MaterialReg == 0)
1009 return 0;
1010 }
Dan Gohmana6cb6412010-05-11 23:54:07 +00001011 return FastEmit_rr(VT, VT, Opcode,
1012 Op0, Op0IsKill,
1013 MaterialReg, /*Kill=*/true);
Dan Gohman10df0fa2008-08-27 01:09:54 +00001014}
1015
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001016unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
1017 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +00001018}
1019
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001020unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +00001021 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001022 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +00001023 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001024
Dan Gohmaneabaed22010-07-07 16:47:08 +00001025 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001026 return ResultReg;
1027}
1028
1029unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
1030 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001031 unsigned Op0, bool Op0IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001032 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +00001033 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001034
Evan Cheng5960e4e2008-09-08 08:38:20 +00001035 if (II.getNumDefs() >= 1)
Dan Gohmaneabaed22010-07-07 16:47:08 +00001036 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1037 .addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001038 else {
Dan Gohmaneabaed22010-07-07 16:47:08 +00001039 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1040 .addReg(Op0, Op0IsKill * RegState::Kill);
1041 bool InsertedCopy = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
1042 ResultReg, II.ImplicitDefs[0],
1043 RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001044 if (!InsertedCopy)
1045 ResultReg = 0;
1046 }
1047
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001048 return ResultReg;
1049}
1050
1051unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
1052 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001053 unsigned Op0, bool Op0IsKill,
1054 unsigned Op1, bool Op1IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001055 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +00001056 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001057
Evan Cheng5960e4e2008-09-08 08:38:20 +00001058 if (II.getNumDefs() >= 1)
Dan Gohmaneabaed22010-07-07 16:47:08 +00001059 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001060 .addReg(Op0, Op0IsKill * RegState::Kill)
1061 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001062 else {
Dan Gohmaneabaed22010-07-07 16:47:08 +00001063 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001064 .addReg(Op0, Op0IsKill * RegState::Kill)
1065 .addReg(Op1, Op1IsKill * RegState::Kill);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001066 bool InsertedCopy = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
1067 ResultReg, II.ImplicitDefs[0],
1068 RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001069 if (!InsertedCopy)
1070 ResultReg = 0;
1071 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001072 return ResultReg;
1073}
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001074
1075unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1076 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001077 unsigned Op0, bool Op0IsKill,
1078 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001079 unsigned ResultReg = createResultReg(RC);
1080 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1081
Evan Cheng5960e4e2008-09-08 08:38:20 +00001082 if (II.getNumDefs() >= 1)
Dan Gohmaneabaed22010-07-07 16:47:08 +00001083 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001084 .addReg(Op0, Op0IsKill * RegState::Kill)
1085 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001086 else {
Dan Gohmaneabaed22010-07-07 16:47:08 +00001087 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001088 .addReg(Op0, Op0IsKill * RegState::Kill)
1089 .addImm(Imm);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001090 bool InsertedCopy = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
1091 ResultReg, II.ImplicitDefs[0],
1092 RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001093 if (!InsertedCopy)
1094 ResultReg = 0;
1095 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001096 return ResultReg;
1097}
1098
Dan Gohman10df0fa2008-08-27 01:09:54 +00001099unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
1100 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001101 unsigned Op0, bool Op0IsKill,
1102 const ConstantFP *FPImm) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001103 unsigned ResultReg = createResultReg(RC);
1104 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1105
Evan Cheng5960e4e2008-09-08 08:38:20 +00001106 if (II.getNumDefs() >= 1)
Dan Gohmaneabaed22010-07-07 16:47:08 +00001107 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001108 .addReg(Op0, Op0IsKill * RegState::Kill)
1109 .addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001110 else {
Dan Gohmaneabaed22010-07-07 16:47:08 +00001111 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001112 .addReg(Op0, Op0IsKill * RegState::Kill)
1113 .addFPImm(FPImm);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001114 bool InsertedCopy = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
1115 ResultReg, II.ImplicitDefs[0],
1116 RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001117 if (!InsertedCopy)
1118 ResultReg = 0;
1119 }
Dan Gohman10df0fa2008-08-27 01:09:54 +00001120 return ResultReg;
1121}
1122
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001123unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
1124 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001125 unsigned Op0, bool Op0IsKill,
1126 unsigned Op1, bool Op1IsKill,
1127 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001128 unsigned ResultReg = createResultReg(RC);
1129 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1130
Evan Cheng5960e4e2008-09-08 08:38:20 +00001131 if (II.getNumDefs() >= 1)
Dan Gohmaneabaed22010-07-07 16:47:08 +00001132 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001133 .addReg(Op0, Op0IsKill * RegState::Kill)
1134 .addReg(Op1, Op1IsKill * RegState::Kill)
1135 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001136 else {
Dan Gohmaneabaed22010-07-07 16:47:08 +00001137 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001138 .addReg(Op0, Op0IsKill * RegState::Kill)
1139 .addReg(Op1, Op1IsKill * RegState::Kill)
1140 .addImm(Imm);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001141 bool InsertedCopy = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
1142 ResultReg, II.ImplicitDefs[0],
1143 RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001144 if (!InsertedCopy)
1145 ResultReg = 0;
1146 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001147 return ResultReg;
1148}
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001149
1150unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1151 const TargetRegisterClass *RC,
1152 uint64_t Imm) {
1153 unsigned ResultReg = createResultReg(RC);
1154 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1155
Evan Cheng5960e4e2008-09-08 08:38:20 +00001156 if (II.getNumDefs() >= 1)
Dan Gohmaneabaed22010-07-07 16:47:08 +00001157 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001158 else {
Dan Gohmaneabaed22010-07-07 16:47:08 +00001159 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm);
1160 bool InsertedCopy = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
1161 ResultReg, II.ImplicitDefs[0],
1162 RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001163 if (!InsertedCopy)
1164 ResultReg = 0;
1165 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001166 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +00001167}
Owen Anderson8970f002008-08-27 22:30:02 +00001168
Owen Anderson825b72b2009-08-11 20:47:22 +00001169unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001170 unsigned Op0, bool Op0IsKill,
1171 uint32_t Idx) {
Owen Anderson40a468f2008-08-28 17:47:37 +00001172 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +00001173
Evan Cheng536ab132009-01-22 09:10:11 +00001174 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Chris Lattner518bb532010-02-09 19:54:29 +00001175 const TargetInstrDesc &II = TII.get(TargetOpcode::EXTRACT_SUBREG);
Owen Anderson8970f002008-08-27 22:30:02 +00001176
Evan Cheng5960e4e2008-09-08 08:38:20 +00001177 if (II.getNumDefs() >= 1)
Dan Gohmaneabaed22010-07-07 16:47:08 +00001178 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001179 .addReg(Op0, Op0IsKill * RegState::Kill)
1180 .addImm(Idx);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001181 else {
Dan Gohmaneabaed22010-07-07 16:47:08 +00001182 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001183 .addReg(Op0, Op0IsKill * RegState::Kill)
1184 .addImm(Idx);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001185 bool InsertedCopy = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
1186 ResultReg, II.ImplicitDefs[0],
1187 RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001188 if (!InsertedCopy)
1189 ResultReg = 0;
1190 }
Owen Anderson8970f002008-08-27 22:30:02 +00001191 return ResultReg;
1192}
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001193
1194/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1195/// with all but the least significant bit set to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001196unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1197 return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001198}
Dan Gohmanf81eca02010-04-22 20:46:50 +00001199
1200/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1201/// Emit code to ensure constants are copied into registers when needed.
1202/// Remember the virtual registers that need to be added to the Machine PHI
1203/// nodes as input. We cannot just directly add them, because expansion
1204/// might result in multiple MBB's for one BB. As such, the start of the
1205/// BB might correspond to a different MBB than the end.
1206bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1207 const TerminatorInst *TI = LLVMBB->getTerminator();
1208
1209 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohmana4160c32010-07-07 16:29:44 +00001210 unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001211
1212 // Check successor nodes' PHI nodes that expect a constant to be available
1213 // from this block.
1214 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1215 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1216 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmana4160c32010-07-07 16:29:44 +00001217 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Dan Gohmanf81eca02010-04-22 20:46:50 +00001218
1219 // If this terminator has multiple identical successors (common for
1220 // switches), only handle each succ once.
1221 if (!SuccsHandled.insert(SuccMBB)) continue;
1222
1223 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1224
1225 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1226 // nodes and Machine PHI nodes, but the incoming operands have not been
1227 // emitted yet.
1228 for (BasicBlock::const_iterator I = SuccBB->begin();
1229 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanfb95f892010-05-07 01:10:20 +00001230
Dan Gohmanf81eca02010-04-22 20:46:50 +00001231 // Ignore dead phi's.
1232 if (PN->use_empty()) continue;
1233
1234 // Only handle legal types. Two interesting things to note here. First,
1235 // by bailing out early, we may leave behind some dead instructions,
1236 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
1237 // own moves. Second, this check is necessary becuase FastISel doesn't
Dan Gohman89496d02010-07-02 00:10:16 +00001238 // use CreateRegs to create registers, so it always creates
Dan Gohmanf81eca02010-04-22 20:46:50 +00001239 // exactly one register for each non-void instruction.
1240 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1241 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
1242 // Promote MVT::i1.
1243 if (VT == MVT::i1)
1244 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1245 else {
Dan Gohmana4160c32010-07-07 16:29:44 +00001246 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001247 return false;
1248 }
1249 }
1250
1251 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1252
Dan Gohmanfb95f892010-05-07 01:10:20 +00001253 // Set the DebugLoc for the copy. Prefer the location of the operand
1254 // if there is one; use the location of the PHI otherwise.
1255 DL = PN->getDebugLoc();
1256 if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
1257 DL = Inst->getDebugLoc();
1258
Dan Gohmanf81eca02010-04-22 20:46:50 +00001259 unsigned Reg = getRegForValue(PHIOp);
1260 if (Reg == 0) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001261 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001262 return false;
1263 }
Dan Gohmana4160c32010-07-07 16:29:44 +00001264 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohmanfb95f892010-05-07 01:10:20 +00001265 DL = DebugLoc();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001266 }
1267 }
1268
1269 return true;
1270}