blob: 1996c192ec7120041e06c41f7c1988824dcfbbe8 [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 Gohmana6cb6412010-05-11 23:54:07 +000059bool FastISel::hasTrivialKill(const Value *V) const {
Dan Gohman7f0d6952010-05-14 22:53:18 +000060 // Don't consider constants or arguments to have trivial kills.
Dan Gohmana6cb6412010-05-11 23:54:07 +000061 const Instruction *I = dyn_cast<Instruction>(V);
Dan Gohman7f0d6952010-05-14 22:53:18 +000062 if (!I)
63 return false;
64
65 // No-op casts are trivially coalesced by fast-isel.
66 if (const CastInst *Cast = dyn_cast<CastInst>(I))
67 if (Cast->isNoopCast(TD.getIntPtrType(Cast->getContext())) &&
68 !hasTrivialKill(Cast->getOperand(0)))
69 return false;
70
71 // Only instructions with a single use in the same basic block are considered
72 // to have trivial kills.
73 return I->hasOneUse() &&
74 !(I->getOpcode() == Instruction::BitCast ||
75 I->getOpcode() == Instruction::PtrToInt ||
76 I->getOpcode() == Instruction::IntToPtr) &&
Dan Gohmane1308d82010-05-13 19:19:32 +000077 cast<Instruction>(I->use_begin())->getParent() == I->getParent();
Dan Gohmana6cb6412010-05-11 23:54:07 +000078}
79
Dan Gohman46510a72010-04-15 01:51:59 +000080unsigned FastISel::getRegForValue(const Value *V) {
Owen Andersone50ed302009-08-10 22:56:29 +000081 EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
Dan Gohman4fd55282009-04-07 20:40:11 +000082 // Don't handle non-simple values in FastISel.
83 if (!RealVT.isSimple())
84 return 0;
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +000085
86 // Ignore illegal types. We must do this before looking up the value
87 // in ValueMap because Arguments are given virtual registers regardless
88 // of whether FastISel can handle them.
Owen Anderson825b72b2009-08-11 20:47:22 +000089 MVT VT = RealVT.getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +000090 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +000091 // Promote MVT::i1 to a legal type though, because it's common and easy.
92 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +000093 VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +000094 else
95 return 0;
96 }
97
Dan Gohman104e4ce2008-09-03 23:32:19 +000098 // Look up the value to see if we already have a register for it. We
99 // cache values defined by Instructions across blocks, and other values
100 // only locally. This is because Instructions already have the SSA
Dan Gohman5c9cf192010-01-12 04:30:26 +0000101 // def-dominates-use requirement enforced.
Owen Anderson99aaf102008-09-03 17:37:03 +0000102 if (ValueMap.count(V))
103 return ValueMap[V];
Dan Gohman104e4ce2008-09-03 23:32:19 +0000104 unsigned Reg = LocalValueMap[V];
105 if (Reg != 0)
106 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000107
Dan Gohman97c94b82010-05-06 00:02:14 +0000108 // In bottom-up mode, just create the virtual register which will be used
109 // to hold the value. It will be materialized later.
110 if (IsBottomUp) {
111 Reg = createResultReg(TLI.getRegClassFor(VT));
112 if (isa<Instruction>(V))
113 ValueMap[V] = Reg;
114 else
115 LocalValueMap[V] = Reg;
116 return Reg;
117 }
118
Dan Gohman1fdc6142010-05-03 23:36:34 +0000119 return materializeRegForValue(V, VT);
120}
121
122/// materializeRegForValue - Helper for getRegForVale. This function is
123/// called when the value isn't already available in a register and must
124/// be materialized with new instructions.
125unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
126 unsigned Reg = 0;
127
Dan Gohman46510a72010-04-15 01:51:59 +0000128 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000129 if (CI->getValue().getActiveBits() <= 64)
130 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +0000131 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000132 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +0000133 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman1e9e8c32008-10-07 22:03:27 +0000134 // Translate this as an integer zero so that it can be
135 // local-CSE'd with actual integer zeros.
Owen Anderson1d0be152009-08-13 21:58:54 +0000136 Reg =
137 getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
Dan Gohman46510a72010-04-15 01:51:59 +0000138 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman4183e312010-04-13 17:07:06 +0000139 // Try to emit the constant directly.
Dan Gohman104e4ce2008-09-03 23:32:19 +0000140 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000141
142 if (!Reg) {
Dan Gohman4183e312010-04-13 17:07:06 +0000143 // Try to emit the constant by using an integer constant with a cast.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000144 const APFloat &Flt = CF->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000145 EVT IntVT = TLI.getPointerTy();
Dan Gohmanad368ac2008-08-27 18:10:19 +0000146
147 uint64_t x[2];
148 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000149 bool isExact;
150 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
151 APFloat::rmTowardZero, &isExact);
152 if (isExact) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000153 APInt IntVal(IntBitWidth, 2, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000154
Owen Andersone922c022009-07-22 00:24:57 +0000155 unsigned IntegerReg =
Owen Andersoneed707b2009-07-24 23:12:02 +0000156 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000157 if (IntegerReg != 0)
Dan Gohmana6cb6412010-05-11 23:54:07 +0000158 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
159 IntegerReg, /*Kill=*/false);
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000160 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000161 }
Dan Gohman46510a72010-04-15 01:51:59 +0000162 } else if (const Operator *Op = dyn_cast<Operator>(V)) {
Dan Gohman32acbc12010-04-14 02:33:23 +0000163 if (!SelectOperator(Op, Op->getOpcode())) return 0;
164 Reg = LocalValueMap[Op];
Dan Gohman205d9252008-08-28 21:19:07 +0000165 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000166 Reg = createResultReg(TLI.getRegClassFor(VT));
Chris Lattner518bb532010-02-09 19:54:29 +0000167 BuildMI(MBB, DL, TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000168 }
Owen Andersond5d81a42008-09-03 17:51:57 +0000169
Dan Gohmandceffe62008-09-25 01:28:51 +0000170 // If target-independent code couldn't handle the value, give target-specific
171 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000172 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000173 Reg = TargetMaterializeConstant(cast<Constant>(V));
Owen Anderson6e607452008-09-05 23:36:01 +0000174
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000175 // Don't cache constant materializations in the general ValueMap.
176 // To do so would require tracking what uses they dominate.
Dan Gohmandceffe62008-09-25 01:28:51 +0000177 if (Reg != 0)
178 LocalValueMap[V] = Reg;
Dan Gohman104e4ce2008-09-03 23:32:19 +0000179 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000180}
181
Dan Gohman46510a72010-04-15 01:51:59 +0000182unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng59fbc802008-09-09 01:26:59 +0000183 // Look up the value to see if we already have a register for it. We
184 // cache values defined by Instructions across blocks, and other values
185 // only locally. This is because Instructions already have the SSA
Dan Gohman1fdc6142010-05-03 23:36:34 +0000186 // def-dominates-use requirement enforced.
Evan Cheng59fbc802008-09-09 01:26:59 +0000187 if (ValueMap.count(V))
188 return ValueMap[V];
189 return LocalValueMap[V];
190}
191
Owen Andersoncc54e762008-08-30 00:38:46 +0000192/// UpdateValueMap - Update the value map to include the new mapping for this
193/// instruction, or insert an extra copy to get the result in a previous
194/// determined register.
195/// NOTE: This is only necessary because we might select a block that uses
196/// a value before we select the block that defines the value. It might be
197/// possible to fix this by selecting blocks in reverse postorder.
Dan Gohman46510a72010-04-15 01:51:59 +0000198unsigned FastISel::UpdateValueMap(const Value *I, unsigned Reg) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000199 if (!isa<Instruction>(I)) {
200 LocalValueMap[I] = Reg;
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000201 return Reg;
Dan Gohman40b189e2008-09-05 18:18:20 +0000202 }
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000203
204 unsigned &AssignedReg = ValueMap[I];
205 if (AssignedReg == 0)
206 AssignedReg = Reg;
Chris Lattner36e39462009-04-12 07:46:30 +0000207 else if (Reg != AssignedReg) {
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000208 const TargetRegisterClass *RegClass = MRI.getRegClass(Reg);
209 TII.copyRegToReg(*MBB, MBB->end(), AssignedReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000210 Reg, RegClass, RegClass, DL);
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000211 }
212 return AssignedReg;
Owen Andersoncc54e762008-08-30 00:38:46 +0000213}
214
Dan Gohmana6cb6412010-05-11 23:54:07 +0000215std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000216 unsigned IdxN = getRegForValue(Idx);
217 if (IdxN == 0)
218 // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000219 return std::pair<unsigned, bool>(0, false);
220
221 bool IdxNIsKill = hasTrivialKill(Idx);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000222
223 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Anderson766b5ef2009-08-11 21:59:30 +0000224 MVT PtrVT = TLI.getPointerTy();
Owen Andersone50ed302009-08-10 22:56:29 +0000225 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000226 if (IdxVT.bitsLT(PtrVT)) {
227 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND,
228 IdxN, IdxNIsKill);
229 IdxNIsKill = true;
230 }
231 else if (IdxVT.bitsGT(PtrVT)) {
232 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE,
233 IdxN, IdxNIsKill);
234 IdxNIsKill = true;
235 }
236 return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000237}
238
Dan Gohmanbdedd442008-08-20 00:11:48 +0000239/// SelectBinaryOp - Select and emit code for a binary operator instruction,
240/// which has an opcode which directly corresponds to the given ISD opcode.
241///
Dan Gohman46510a72010-04-15 01:51:59 +0000242bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000243 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmanbdedd442008-08-20 00:11:48 +0000245 // Unhandled type. Halt "fast" selection and bail.
246 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000247
Dan Gohmanb71fea22008-08-26 20:52:40 +0000248 // We only handle legal types. For example, on x86-32 the instruction
249 // selector contains all of the 64-bit instructions from x86-64,
250 // under the assumption that i64 won't be used if the target doesn't
251 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000252 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000253 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000254 // don't require additional zeroing, which makes them easy.
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000256 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
257 ISDOpcode == ISD::XOR))
Owen Anderson23b9b192009-08-12 00:36:31 +0000258 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohman638c6832008-09-05 18:44:22 +0000259 else
260 return false;
261 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000262
Dan Gohman3df24e62008-09-03 23:12:08 +0000263 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000264 if (Op0 == 0)
265 // Unhandled operand. Halt "fast" selection and bail.
266 return false;
267
Dan Gohmana6cb6412010-05-11 23:54:07 +0000268 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
269
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000270 // Check if the second operand is a constant and handle it appropriately.
271 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000272 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000273 ISDOpcode, Op0, Op0IsKill,
274 CI->getZExtValue());
Dan Gohmanad368ac2008-08-27 18:10:19 +0000275 if (ResultReg != 0) {
276 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000277 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000278 return true;
279 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000280 }
281
Dan Gohman10df0fa2008-08-27 01:09:54 +0000282 // Check if the second operand is a constant float.
283 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000284 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000285 ISDOpcode, Op0, Op0IsKill, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000286 if (ResultReg != 0) {
287 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000288 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000289 return true;
290 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000291 }
292
Dan Gohman3df24e62008-09-03 23:12:08 +0000293 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000294 if (Op1 == 0)
295 // Unhandled operand. Halt "fast" selection and bail.
296 return false;
297
Dan Gohmana6cb6412010-05-11 23:54:07 +0000298 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
299
Dan Gohmanad368ac2008-08-27 18:10:19 +0000300 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000301 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000302 ISDOpcode,
303 Op0, Op0IsKill,
304 Op1, Op1IsKill);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000305 if (ResultReg == 0)
306 // Target-specific code wasn't able to find a machine opcode for
307 // the given ISD opcode and type. Halt "fast" selection and bail.
308 return false;
309
Dan Gohman8014e862008-08-20 00:23:20 +0000310 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000311 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000312 return true;
313}
314
Dan Gohman46510a72010-04-15 01:51:59 +0000315bool FastISel::SelectGetElementPtr(const User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000316 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000317 if (N == 0)
318 // Unhandled operand. Halt "fast" selection and bail.
319 return false;
320
Dan Gohmana6cb6412010-05-11 23:54:07 +0000321 bool NIsKill = hasTrivialKill(I->getOperand(0));
322
Evan Cheng83785c82008-08-20 22:45:34 +0000323 const Type *Ty = I->getOperand(0)->getType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 MVT VT = TLI.getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +0000325 for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
326 E = I->op_end(); OI != E; ++OI) {
327 const Value *Idx = *OI;
Evan Cheng83785c82008-08-20 22:45:34 +0000328 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
329 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
330 if (Field) {
331 // N = N + Offset
332 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
333 // FIXME: This can be optimized by combining the add with a
334 // subsequent one.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000335 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000336 if (N == 0)
337 // Unhandled operand. Halt "fast" selection and bail.
338 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000339 NIsKill = true;
Evan Cheng83785c82008-08-20 22:45:34 +0000340 }
341 Ty = StTy->getElementType(Field);
342 } else {
343 Ty = cast<SequentialType>(Ty)->getElementType();
344
345 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +0000346 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Evan Cheng83785c82008-08-20 22:45:34 +0000347 if (CI->getZExtValue() == 0) continue;
348 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +0000349 TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohmana6cb6412010-05-11 23:54:07 +0000350 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000351 if (N == 0)
352 // Unhandled operand. Halt "fast" selection and bail.
353 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000354 NIsKill = true;
Evan Cheng83785c82008-08-20 22:45:34 +0000355 continue;
356 }
357
358 // N = N + Idx * ElementSize;
Duncan Sands777d2302009-05-09 07:06:46 +0000359 uint64_t ElementSize = TD.getTypeAllocSize(Ty);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000360 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
361 unsigned IdxN = Pair.first;
362 bool IdxNIsKill = Pair.second;
Evan Cheng83785c82008-08-20 22:45:34 +0000363 if (IdxN == 0)
364 // Unhandled operand. Halt "fast" selection and bail.
365 return false;
366
Dan Gohman80bc6e22008-08-26 20:57:08 +0000367 if (ElementSize != 1) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000368 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000369 if (IdxN == 0)
370 // Unhandled operand. Halt "fast" selection and bail.
371 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000372 IdxNIsKill = true;
Dan Gohman80bc6e22008-08-26 20:57:08 +0000373 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000374 N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
Evan Cheng83785c82008-08-20 22:45:34 +0000375 if (N == 0)
376 // Unhandled operand. Halt "fast" selection and bail.
377 return false;
378 }
379 }
380
381 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000382 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000383 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000384}
385
Dan Gohman46510a72010-04-15 01:51:59 +0000386bool FastISel::SelectCall(const User *I) {
387 const Function *F = cast<CallInst>(I)->getCalledFunction();
Dan Gohman33134c42008-09-25 17:05:24 +0000388 if (!F) return false;
389
Dan Gohman4183e312010-04-13 17:07:06 +0000390 // Handle selected intrinsic function calls.
Dan Gohman33134c42008-09-25 17:05:24 +0000391 unsigned IID = F->getIntrinsicID();
392 switch (IID) {
393 default: break;
Bill Wendling92c1e122009-02-13 02:16:35 +0000394 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +0000395 const DbgDeclareInst *DI = cast<DbgDeclareInst>(I);
Devang Patel02f0dbd2010-05-07 22:04:20 +0000396 if (!DIVariable(DI->getVariable()).Verify() ||
Chris Lattnered3a8062010-04-05 06:05:26 +0000397 !MF.getMMI().hasDebugInfo())
Devang Patel7e1e31f2009-07-02 22:43:26 +0000398 return true;
399
Dan Gohman46510a72010-04-15 01:51:59 +0000400 const Value *Address = DI->getAddress();
Dale Johannesendc918562010-02-06 02:26:02 +0000401 if (!Address)
402 return true;
Dale Johannesen343b42e2010-04-07 01:15:14 +0000403 if (isa<UndefValue>(Address))
404 return true;
Dan Gohman46510a72010-04-15 01:51:59 +0000405 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
Devang Patel7e1e31f2009-07-02 22:43:26 +0000406 // Don't handle byval struct arguments or VLAs, for example.
Dale Johannesen7dc78402010-04-25 21:03:54 +0000407 // Note that if we have a byval struct argument, fast ISel is turned off;
408 // those are handled in SelectionDAGBuilder.
Devang Patel54fc4d62010-04-28 19:27:33 +0000409 if (AI) {
410 DenseMap<const AllocaInst*, int>::iterator SI =
411 StaticAllocaMap.find(AI);
412 if (SI == StaticAllocaMap.end()) break; // VLAs.
413 int FI = SI->second;
414 if (!DI->getDebugLoc().isUnknown())
415 MF.getMMI().setVariableDbgInfo(DI->getVariable(), FI, DI->getDebugLoc());
416 } else
417 // Building the map above is target independent. Generating DBG_VALUE
418 // inline is target dependent; do this now.
419 (void)TargetSelectInstruction(cast<Instruction>(I));
Dan Gohman33134c42008-09-25 17:05:24 +0000420 return true;
Bill Wendling92c1e122009-02-13 02:16:35 +0000421 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000422 case Intrinsic::dbg_value: {
Dale Johannesen343b42e2010-04-07 01:15:14 +0000423 // This form of DBG_VALUE is target-independent.
Dan Gohman46510a72010-04-15 01:51:59 +0000424 const DbgValueInst *DI = cast<DbgValueInst>(I);
Dale Johannesen45df7612010-02-26 20:01:55 +0000425 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohman46510a72010-04-15 01:51:59 +0000426 const Value *V = DI->getValue();
Dale Johannesen45df7612010-02-26 20:01:55 +0000427 if (!V) {
428 // Currently the optimizer can produce this; insert an undef to
429 // help debugging. Probably the optimizer should not do this.
430 BuildMI(MBB, DL, II).addReg(0U).addImm(DI->getOffset()).
431 addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000432 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dale Johannesen45df7612010-02-26 20:01:55 +0000433 BuildMI(MBB, DL, II).addImm(CI->getZExtValue()).addImm(DI->getOffset()).
434 addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000435 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dale Johannesen45df7612010-02-26 20:01:55 +0000436 BuildMI(MBB, DL, II).addFPImm(CF).addImm(DI->getOffset()).
437 addMetadata(DI->getVariable());
438 } else if (unsigned Reg = lookUpRegForValue(V)) {
439 BuildMI(MBB, DL, II).addReg(Reg, RegState::Debug).addImm(DI->getOffset()).
440 addMetadata(DI->getVariable());
441 } else {
442 // We can't yet handle anything else here because it would require
443 // generating code, thus altering codegen because of debug info.
444 // Insert an undef so we can see what we dropped.
445 BuildMI(MBB, DL, II).addReg(0U).addImm(DI->getOffset()).
446 addMetadata(DI->getVariable());
447 }
448 return true;
449 }
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000450 case Intrinsic::eh_exception: {
Owen Andersone50ed302009-08-10 22:56:29 +0000451 EVT VT = TLI.getValueType(I->getType());
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000452 switch (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)) {
453 default: break;
454 case TargetLowering::Expand: {
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000455 assert(MBB->isLandingPad() && "Call to eh.exception not in landing pad!");
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000456 unsigned Reg = TLI.getExceptionAddressRegister();
457 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
458 unsigned ResultReg = createResultReg(RC);
459 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000460 Reg, RC, RC, DL);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000461 assert(InsertedCopy && "Can't copy address registers!");
Evan Cheng24ac4082008-11-24 07:09:49 +0000462 InsertedCopy = InsertedCopy;
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000463 UpdateValueMap(I, ResultReg);
464 return true;
465 }
466 }
467 break;
468 }
Duncan Sandsb01bbdc2009-10-14 16:11:37 +0000469 case Intrinsic::eh_selector: {
Owen Andersone50ed302009-08-10 22:56:29 +0000470 EVT VT = TLI.getValueType(I->getType());
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000471 switch (TLI.getOperationAction(ISD::EHSELECTION, VT)) {
472 default: break;
473 case TargetLowering::Expand: {
Chris Lattnered3a8062010-04-05 06:05:26 +0000474 if (MBB->isLandingPad())
475 AddCatchInfo(*cast<CallInst>(I), &MF.getMMI(), MBB);
476 else {
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000477#ifndef NDEBUG
Chris Lattnered3a8062010-04-05 06:05:26 +0000478 CatchInfoLost.insert(cast<CallInst>(I));
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000479#endif
Chris Lattnered3a8062010-04-05 06:05:26 +0000480 // FIXME: Mark exception selector register as live in. Hack for PR1508.
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000481 unsigned Reg = TLI.getExceptionSelectorRegister();
Chris Lattnered3a8062010-04-05 06:05:26 +0000482 if (Reg) MBB->addLiveIn(Reg);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000483 }
Chris Lattnered3a8062010-04-05 06:05:26 +0000484
485 unsigned Reg = TLI.getExceptionSelectorRegister();
486 EVT SrcVT = TLI.getPointerTy();
487 const TargetRegisterClass *RC = TLI.getRegClassFor(SrcVT);
488 unsigned ResultReg = createResultReg(RC);
489 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, Reg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000490 RC, RC, DL);
Chris Lattnered3a8062010-04-05 06:05:26 +0000491 assert(InsertedCopy && "Can't copy address registers!");
492 InsertedCopy = InsertedCopy;
493
Dan Gohmana6cb6412010-05-11 23:54:07 +0000494 bool ResultRegIsKill = hasTrivialKill(I);
495
Chris Lattnered3a8062010-04-05 06:05:26 +0000496 // Cast the register to the type of the selector.
497 if (SrcVT.bitsGT(MVT::i32))
498 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, ISD::TRUNCATE,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000499 ResultReg, ResultRegIsKill);
Chris Lattnered3a8062010-04-05 06:05:26 +0000500 else if (SrcVT.bitsLT(MVT::i32))
501 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000502 ISD::SIGN_EXTEND, ResultReg, ResultRegIsKill);
Chris Lattnered3a8062010-04-05 06:05:26 +0000503 if (ResultReg == 0)
504 // Unhandled operand. Halt "fast" selection and bail.
505 return false;
506
507 UpdateValueMap(I, ResultReg);
508
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000509 return true;
510 }
511 }
512 break;
513 }
Dan Gohman33134c42008-09-25 17:05:24 +0000514 }
Dan Gohman4183e312010-04-13 17:07:06 +0000515
516 // An arbitrary call. Bail.
Dan Gohman33134c42008-09-25 17:05:24 +0000517 return false;
518}
519
Dan Gohman46510a72010-04-15 01:51:59 +0000520bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000521 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
522 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000523
Owen Anderson825b72b2009-08-11 20:47:22 +0000524 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
525 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersond0533c92008-08-26 23:46:32 +0000526 // Unhandled type. Halt "fast" selection and bail.
527 return false;
528
Dan Gohman474d3b32009-03-13 23:53:06 +0000529 // Check if the destination type is legal. Or as a special case,
530 // it may be i1 if we're doing a truncate because that's
531 // easy and somewhat common.
532 if (!TLI.isTypeLegal(DstVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000533 if (DstVT != MVT::i1 || Opcode != ISD::TRUNCATE)
Dan Gohman91b6f972008-10-03 01:28:47 +0000534 // Unhandled type. Halt "fast" selection and bail.
535 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000536
537 // Check if the source operand is legal. Or as a special case,
538 // it may be i1 if we're doing zero-extension because that's
539 // easy and somewhat common.
540 if (!TLI.isTypeLegal(SrcVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 if (SrcVT != MVT::i1 || Opcode != ISD::ZERO_EXTEND)
Dan Gohman474d3b32009-03-13 23:53:06 +0000542 // Unhandled type. Halt "fast" selection and bail.
543 return false;
544
Dan Gohman3df24e62008-09-03 23:12:08 +0000545 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000546 if (!InputReg)
547 // Unhandled operand. Halt "fast" selection and bail.
548 return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000549
Dan Gohmana6cb6412010-05-11 23:54:07 +0000550 bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
551
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000552 // If the operand is i1, arrange for the high bits in the register to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 if (SrcVT == MVT::i1) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000554 SrcVT = TLI.getTypeToTransformTo(I->getContext(), SrcVT);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000555 InputReg = FastEmitZExtFromI1(SrcVT.getSimpleVT(), InputReg, InputRegIsKill);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000556 if (!InputReg)
557 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000558 InputRegIsKill = true;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000559 }
Dan Gohman474d3b32009-03-13 23:53:06 +0000560 // If the result is i1, truncate to the target's type for i1 first.
Owen Anderson825b72b2009-08-11 20:47:22 +0000561 if (DstVT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +0000562 DstVT = TLI.getTypeToTransformTo(I->getContext(), DstVT);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000563
Owen Andersond0533c92008-08-26 23:46:32 +0000564 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
565 DstVT.getSimpleVT(),
566 Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000567 InputReg, InputRegIsKill);
Owen Andersond0533c92008-08-26 23:46:32 +0000568 if (!ResultReg)
569 return false;
570
Dan Gohman3df24e62008-09-03 23:12:08 +0000571 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000572 return true;
573}
574
Dan Gohman46510a72010-04-15 01:51:59 +0000575bool FastISel::SelectBitCast(const User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000576 // If the bitcast doesn't change the type, just use the operand value.
577 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000578 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000579 if (Reg == 0)
580 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000581 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000582 return true;
583 }
584
585 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Andersone50ed302009-08-10 22:56:29 +0000586 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
587 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000588
Owen Anderson825b72b2009-08-11 20:47:22 +0000589 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
590 DstVT == MVT::Other || !DstVT.isSimple() ||
Owen Andersond0533c92008-08-26 23:46:32 +0000591 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
592 // Unhandled type. Halt "fast" selection and bail.
593 return false;
594
Dan Gohman3df24e62008-09-03 23:12:08 +0000595 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000596 if (Op0 == 0)
597 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000598 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000599
600 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000601
Dan Gohmanad368ac2008-08-27 18:10:19 +0000602 // First, try to perform the bitcast by inserting a reg-reg copy.
603 unsigned ResultReg = 0;
604 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
605 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
606 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
607 ResultReg = createResultReg(DstClass);
608
609 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000610 Op0, DstClass, SrcClass, DL);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000611 if (!InsertedCopy)
612 ResultReg = 0;
613 }
614
615 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
616 if (!ResultReg)
617 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000618 ISD::BIT_CONVERT, Op0, Op0IsKill);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000619
620 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000621 return false;
622
Dan Gohman3df24e62008-09-03 23:12:08 +0000623 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000624 return true;
625}
626
Dan Gohman3df24e62008-09-03 23:12:08 +0000627bool
Dan Gohman46510a72010-04-15 01:51:59 +0000628FastISel::SelectInstruction(const Instruction *I) {
Dan Gohmane8c92dd2010-04-23 15:29:50 +0000629 // Just before the terminator instruction, insert instructions to
630 // feed PHI nodes in successor blocks.
631 if (isa<TerminatorInst>(I))
632 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
633 return false;
634
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000635 DL = I->getDebugLoc();
636
Dan Gohman6e3ff372009-12-05 01:27:58 +0000637 // First, try doing target-independent selection.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000638 if (SelectOperator(I, I->getOpcode())) {
639 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000640 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000641 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000642
643 // Next, try calling the target to attempt to handle the instruction.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000644 if (TargetSelectInstruction(I)) {
645 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000646 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000647 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000648
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000649 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000650 return false;
Dan Gohman40b189e2008-09-05 18:18:20 +0000651}
652
Dan Gohmand98d6202008-10-02 22:15:21 +0000653/// FastEmitBranch - Emit an unconditional branch to the given block,
654/// unless it is the immediate (fall-through) successor, and update
655/// the CFG.
656void
657FastISel::FastEmitBranch(MachineBasicBlock *MSucc) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000658 if (MBB->isLayoutSuccessor(MSucc)) {
659 // The unconditional fall-through case, which needs no instructions.
660 } else {
661 // The unconditional branch case.
662 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
663 }
664 MBB->addSuccessor(MSucc);
665}
666
Dan Gohman3d45a852009-09-03 22:53:57 +0000667/// SelectFNeg - Emit an FNeg operation.
668///
669bool
Dan Gohman46510a72010-04-15 01:51:59 +0000670FastISel::SelectFNeg(const User *I) {
Dan Gohman3d45a852009-09-03 22:53:57 +0000671 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
672 if (OpReg == 0) return false;
673
Dan Gohmana6cb6412010-05-11 23:54:07 +0000674 bool OpRegIsKill = hasTrivialKill(I);
675
Dan Gohman4a215a12009-09-11 00:36:43 +0000676 // If the target has ISD::FNEG, use it.
677 EVT VT = TLI.getValueType(I->getType());
678 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000679 ISD::FNEG, OpReg, OpRegIsKill);
Dan Gohman4a215a12009-09-11 00:36:43 +0000680 if (ResultReg != 0) {
681 UpdateValueMap(I, ResultReg);
682 return true;
683 }
684
Dan Gohman5e5abb72009-09-11 00:34:46 +0000685 // Bitcast the value to integer, twiddle the sign bit with xor,
686 // and then bitcast it back to floating-point.
Dan Gohman3d45a852009-09-03 22:53:57 +0000687 if (VT.getSizeInBits() > 64) return false;
Dan Gohman5e5abb72009-09-11 00:34:46 +0000688 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
689 if (!TLI.isTypeLegal(IntVT))
690 return false;
691
692 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000693 ISD::BIT_CONVERT, OpReg, OpRegIsKill);
Dan Gohman5e5abb72009-09-11 00:34:46 +0000694 if (IntReg == 0)
695 return false;
696
Dan Gohmana6cb6412010-05-11 23:54:07 +0000697 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
698 IntReg, /*Kill=*/true,
Dan Gohman5e5abb72009-09-11 00:34:46 +0000699 UINT64_C(1) << (VT.getSizeInBits()-1),
700 IntVT.getSimpleVT());
701 if (IntResultReg == 0)
702 return false;
703
704 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000705 ISD::BIT_CONVERT, IntResultReg, /*Kill=*/true);
Dan Gohman3d45a852009-09-03 22:53:57 +0000706 if (ResultReg == 0)
707 return false;
708
709 UpdateValueMap(I, ResultReg);
710 return true;
711}
712
Dan Gohman40b189e2008-09-05 18:18:20 +0000713bool
Dan Gohman46510a72010-04-15 01:51:59 +0000714FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000715 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000716 case Instruction::Add:
717 return SelectBinaryOp(I, ISD::ADD);
718 case Instruction::FAdd:
719 return SelectBinaryOp(I, ISD::FADD);
720 case Instruction::Sub:
721 return SelectBinaryOp(I, ISD::SUB);
722 case Instruction::FSub:
Dan Gohman3d45a852009-09-03 22:53:57 +0000723 // FNeg is currently represented in LLVM IR as a special case of FSub.
724 if (BinaryOperator::isFNeg(I))
725 return SelectFNeg(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000726 return SelectBinaryOp(I, ISD::FSUB);
727 case Instruction::Mul:
728 return SelectBinaryOp(I, ISD::MUL);
729 case Instruction::FMul:
730 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman3df24e62008-09-03 23:12:08 +0000731 case Instruction::SDiv:
732 return SelectBinaryOp(I, ISD::SDIV);
733 case Instruction::UDiv:
734 return SelectBinaryOp(I, ISD::UDIV);
735 case Instruction::FDiv:
736 return SelectBinaryOp(I, ISD::FDIV);
737 case Instruction::SRem:
738 return SelectBinaryOp(I, ISD::SREM);
739 case Instruction::URem:
740 return SelectBinaryOp(I, ISD::UREM);
741 case Instruction::FRem:
742 return SelectBinaryOp(I, ISD::FREM);
743 case Instruction::Shl:
744 return SelectBinaryOp(I, ISD::SHL);
745 case Instruction::LShr:
746 return SelectBinaryOp(I, ISD::SRL);
747 case Instruction::AShr:
748 return SelectBinaryOp(I, ISD::SRA);
749 case Instruction::And:
750 return SelectBinaryOp(I, ISD::AND);
751 case Instruction::Or:
752 return SelectBinaryOp(I, ISD::OR);
753 case Instruction::Xor:
754 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000755
Dan Gohman3df24e62008-09-03 23:12:08 +0000756 case Instruction::GetElementPtr:
757 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000758
Dan Gohman3df24e62008-09-03 23:12:08 +0000759 case Instruction::Br: {
Dan Gohman46510a72010-04-15 01:51:59 +0000760 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000761
Dan Gohman3df24e62008-09-03 23:12:08 +0000762 if (BI->isUnconditional()) {
Dan Gohman46510a72010-04-15 01:51:59 +0000763 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohman3df24e62008-09-03 23:12:08 +0000764 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
Dan Gohmand98d6202008-10-02 22:15:21 +0000765 FastEmitBranch(MSucc);
Dan Gohman3df24e62008-09-03 23:12:08 +0000766 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000767 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000768
769 // Conditional branches are not handed yet.
770 // Halt "fast" selection and bail.
771 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000772 }
773
Dan Gohman087c8502008-09-05 01:08:41 +0000774 case Instruction::Unreachable:
775 // Nothing to emit.
776 return true;
777
Dan Gohman0586d912008-09-10 20:11:02 +0000778 case Instruction::Alloca:
779 // FunctionLowering has the static-sized case covered.
780 if (StaticAllocaMap.count(cast<AllocaInst>(I)))
781 return true;
782
783 // Dynamic-sized alloca is not handled yet.
784 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000785
Dan Gohman33134c42008-09-25 17:05:24 +0000786 case Instruction::Call:
787 return SelectCall(I);
788
Dan Gohman3df24e62008-09-03 23:12:08 +0000789 case Instruction::BitCast:
790 return SelectBitCast(I);
791
792 case Instruction::FPToSI:
793 return SelectCast(I, ISD::FP_TO_SINT);
794 case Instruction::ZExt:
795 return SelectCast(I, ISD::ZERO_EXTEND);
796 case Instruction::SExt:
797 return SelectCast(I, ISD::SIGN_EXTEND);
798 case Instruction::Trunc:
799 return SelectCast(I, ISD::TRUNCATE);
800 case Instruction::SIToFP:
801 return SelectCast(I, ISD::SINT_TO_FP);
802
803 case Instruction::IntToPtr: // Deliberate fall-through.
804 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +0000805 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
806 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman3df24e62008-09-03 23:12:08 +0000807 if (DstVT.bitsGT(SrcVT))
808 return SelectCast(I, ISD::ZERO_EXTEND);
809 if (DstVT.bitsLT(SrcVT))
810 return SelectCast(I, ISD::TRUNCATE);
811 unsigned Reg = getRegForValue(I->getOperand(0));
812 if (Reg == 0) return false;
813 UpdateValueMap(I, Reg);
814 return true;
815 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000816
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000817 case Instruction::PHI:
818 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
819
Dan Gohman3df24e62008-09-03 23:12:08 +0000820 default:
821 // Unhandled instruction. Halt "fast" selection and bail.
822 return false;
823 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000824}
825
Dan Gohman3df24e62008-09-03 23:12:08 +0000826FastISel::FastISel(MachineFunction &mf,
827 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +0000828 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmanf81eca02010-04-22 20:46:50 +0000829 DenseMap<const AllocaInst *, int> &am,
830 std::vector<std::pair<MachineInstr*, unsigned> > &pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000831#ifndef NDEBUG
Dan Gohman25208642010-04-14 19:53:31 +0000832 , SmallSet<const Instruction *, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000833#endif
834 )
Dan Gohman3df24e62008-09-03 23:12:08 +0000835 : MBB(0),
836 ValueMap(vm),
837 MBBMap(bm),
Dan Gohman0586d912008-09-10 20:11:02 +0000838 StaticAllocaMap(am),
Dan Gohmanf81eca02010-04-22 20:46:50 +0000839 PHINodesToUpdate(pn),
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000840#ifndef NDEBUG
841 CatchInfoLost(cil),
842#endif
Dan Gohman3df24e62008-09-03 23:12:08 +0000843 MF(mf),
844 MRI(MF.getRegInfo()),
Dan Gohman0586d912008-09-10 20:11:02 +0000845 MFI(*MF.getFrameInfo()),
846 MCP(*MF.getConstantPool()),
Dan Gohman3df24e62008-09-03 23:12:08 +0000847 TM(MF.getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000848 TD(*TM.getTargetData()),
849 TII(*TM.getInstrInfo()),
Dan Gohmana7a0ed72010-05-05 23:58:35 +0000850 TLI(*TM.getTargetLowering()),
851 IsBottomUp(false) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000852}
853
Dan Gohmane285a742008-08-14 21:51:29 +0000854FastISel::~FastISel() {}
855
Owen Anderson825b72b2009-08-11 20:47:22 +0000856unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000857 unsigned) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000858 return 0;
859}
860
Owen Anderson825b72b2009-08-11 20:47:22 +0000861unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000862 unsigned,
863 unsigned /*Op0*/, bool /*Op0IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000864 return 0;
865}
866
Owen Anderson825b72b2009-08-11 20:47:22 +0000867unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000868 unsigned,
869 unsigned /*Op0*/, bool /*Op0IsKill*/,
870 unsigned /*Op1*/, bool /*Op1IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000871 return 0;
872}
873
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000874unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000875 return 0;
876}
877
Owen Anderson825b72b2009-08-11 20:47:22 +0000878unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohman46510a72010-04-15 01:51:59 +0000879 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000880 return 0;
881}
882
Owen Anderson825b72b2009-08-11 20:47:22 +0000883unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000884 unsigned,
885 unsigned /*Op0*/, bool /*Op0IsKill*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000886 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000887 return 0;
888}
889
Owen Anderson825b72b2009-08-11 20:47:22 +0000890unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000891 unsigned,
892 unsigned /*Op0*/, bool /*Op0IsKill*/,
Dan Gohman46510a72010-04-15 01:51:59 +0000893 const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000894 return 0;
895}
896
Owen Anderson825b72b2009-08-11 20:47:22 +0000897unsigned FastISel::FastEmit_rri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000898 unsigned,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000899 unsigned /*Op0*/, bool /*Op0IsKill*/,
900 unsigned /*Op1*/, bool /*Op1IsKill*/,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000901 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000902 return 0;
903}
904
905/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
906/// to emit an instruction with an immediate operand using FastEmit_ri.
907/// If that fails, it materializes the immediate into a register and try
908/// FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000909unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000910 unsigned Op0, bool Op0IsKill,
911 uint64_t Imm, MVT ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000912 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000913 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000914 if (ResultReg != 0)
915 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000916 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000917 if (MaterialReg == 0)
918 return 0;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000919 return FastEmit_rr(VT, VT, Opcode,
920 Op0, Op0IsKill,
921 MaterialReg, /*Kill=*/true);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000922}
923
Dan Gohman10df0fa2008-08-27 01:09:54 +0000924/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
925/// to emit an instruction with a floating-point immediate operand using
926/// FastEmit_rf. If that fails, it materializes the immediate into a register
927/// and try FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000928unsigned FastISel::FastEmit_rf_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000929 unsigned Op0, bool Op0IsKill,
930 const ConstantFP *FPImm, MVT ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000931 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000932 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, Op0IsKill, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000933 if (ResultReg != 0)
934 return ResultReg;
935
936 // Materialize the constant in a register.
937 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
938 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000939 // If the target doesn't have a way to directly enter a floating-point
940 // value into a register, use an alternate approach.
941 // TODO: The current approach only supports floating-point constants
942 // that can be constructed by conversion from integer values. This should
943 // be replaced by code that creates a load from a constant-pool entry,
944 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000945 const APFloat &Flt = FPImm->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000946 EVT IntVT = TLI.getPointerTy();
Dan Gohman10df0fa2008-08-27 01:09:54 +0000947
948 uint64_t x[2];
949 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000950 bool isExact;
951 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
952 APFloat::rmTowardZero, &isExact);
953 if (!isExact)
Dan Gohman10df0fa2008-08-27 01:09:54 +0000954 return 0;
955 APInt IntVal(IntBitWidth, 2, x);
956
957 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
958 ISD::Constant, IntVal.getZExtValue());
959 if (IntegerReg == 0)
960 return 0;
961 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000962 ISD::SINT_TO_FP, IntegerReg, /*Kill=*/true);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000963 if (MaterialReg == 0)
964 return 0;
965 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000966 return FastEmit_rr(VT, VT, Opcode,
967 Op0, Op0IsKill,
968 MaterialReg, /*Kill=*/true);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000969}
970
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000971unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
972 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000973}
974
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000975unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000976 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000977 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000978 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000979
Bill Wendling9bc96a52009-02-03 00:55:04 +0000980 BuildMI(MBB, DL, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000981 return ResultReg;
982}
983
984unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
985 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000986 unsigned Op0, bool Op0IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000987 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000988 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000989
Evan Cheng5960e4e2008-09-08 08:38:20 +0000990 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +0000991 BuildMI(MBB, DL, II, ResultReg).addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000992 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000993 BuildMI(MBB, DL, II).addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000994 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000995 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000996 if (!InsertedCopy)
997 ResultReg = 0;
998 }
999
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001000 return ResultReg;
1001}
1002
1003unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
1004 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001005 unsigned Op0, bool Op0IsKill,
1006 unsigned Op1, bool Op1IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001007 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +00001008 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001009
Evan Cheng5960e4e2008-09-08 08:38:20 +00001010 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001011 BuildMI(MBB, DL, II, ResultReg)
1012 .addReg(Op0, Op0IsKill * RegState::Kill)
1013 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001014 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001015 BuildMI(MBB, DL, II)
1016 .addReg(Op0, Op0IsKill * RegState::Kill)
1017 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001018 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001019 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001020 if (!InsertedCopy)
1021 ResultReg = 0;
1022 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001023 return ResultReg;
1024}
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001025
1026unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1027 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001028 unsigned Op0, bool Op0IsKill,
1029 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001030 unsigned ResultReg = createResultReg(RC);
1031 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1032
Evan Cheng5960e4e2008-09-08 08:38:20 +00001033 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001034 BuildMI(MBB, DL, II, ResultReg)
1035 .addReg(Op0, Op0IsKill * RegState::Kill)
1036 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001037 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001038 BuildMI(MBB, DL, II)
1039 .addReg(Op0, Op0IsKill * RegState::Kill)
1040 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001041 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001042 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001043 if (!InsertedCopy)
1044 ResultReg = 0;
1045 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001046 return ResultReg;
1047}
1048
Dan Gohman10df0fa2008-08-27 01:09:54 +00001049unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
1050 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001051 unsigned Op0, bool Op0IsKill,
1052 const ConstantFP *FPImm) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001053 unsigned ResultReg = createResultReg(RC);
1054 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1055
Evan Cheng5960e4e2008-09-08 08:38:20 +00001056 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001057 BuildMI(MBB, DL, II, ResultReg)
1058 .addReg(Op0, Op0IsKill * RegState::Kill)
1059 .addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001060 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001061 BuildMI(MBB, DL, II)
1062 .addReg(Op0, Op0IsKill * RegState::Kill)
1063 .addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001064 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001065 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001066 if (!InsertedCopy)
1067 ResultReg = 0;
1068 }
Dan Gohman10df0fa2008-08-27 01:09:54 +00001069 return ResultReg;
1070}
1071
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001072unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
1073 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001074 unsigned Op0, bool Op0IsKill,
1075 unsigned Op1, bool Op1IsKill,
1076 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001077 unsigned ResultReg = createResultReg(RC);
1078 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1079
Evan Cheng5960e4e2008-09-08 08:38:20 +00001080 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001081 BuildMI(MBB, DL, II, ResultReg)
1082 .addReg(Op0, Op0IsKill * RegState::Kill)
1083 .addReg(Op1, Op1IsKill * RegState::Kill)
1084 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001085 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001086 BuildMI(MBB, DL, II)
1087 .addReg(Op0, Op0IsKill * RegState::Kill)
1088 .addReg(Op1, Op1IsKill * RegState::Kill)
1089 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001090 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001091 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001092 if (!InsertedCopy)
1093 ResultReg = 0;
1094 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001095 return ResultReg;
1096}
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001097
1098unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1099 const TargetRegisterClass *RC,
1100 uint64_t Imm) {
1101 unsigned ResultReg = createResultReg(RC);
1102 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1103
Evan Cheng5960e4e2008-09-08 08:38:20 +00001104 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +00001105 BuildMI(MBB, DL, II, ResultReg).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001106 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +00001107 BuildMI(MBB, DL, II).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001108 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001109 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001110 if (!InsertedCopy)
1111 ResultReg = 0;
1112 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001113 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +00001114}
Owen Anderson8970f002008-08-27 22:30:02 +00001115
Owen Anderson825b72b2009-08-11 20:47:22 +00001116unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001117 unsigned Op0, bool Op0IsKill,
1118 uint32_t Idx) {
Owen Anderson40a468f2008-08-28 17:47:37 +00001119 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +00001120
Evan Cheng536ab132009-01-22 09:10:11 +00001121 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Chris Lattner518bb532010-02-09 19:54:29 +00001122 const TargetInstrDesc &II = TII.get(TargetOpcode::EXTRACT_SUBREG);
Owen Anderson8970f002008-08-27 22:30:02 +00001123
Evan Cheng5960e4e2008-09-08 08:38:20 +00001124 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001125 BuildMI(MBB, DL, II, ResultReg)
1126 .addReg(Op0, Op0IsKill * RegState::Kill)
1127 .addImm(Idx);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001128 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001129 BuildMI(MBB, DL, II)
1130 .addReg(Op0, Op0IsKill * RegState::Kill)
1131 .addImm(Idx);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001132 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001133 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001134 if (!InsertedCopy)
1135 ResultReg = 0;
1136 }
Owen Anderson8970f002008-08-27 22:30:02 +00001137 return ResultReg;
1138}
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001139
1140/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1141/// with all but the least significant bit set to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001142unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1143 return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001144}
Dan Gohmanf81eca02010-04-22 20:46:50 +00001145
1146/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1147/// Emit code to ensure constants are copied into registers when needed.
1148/// Remember the virtual registers that need to be added to the Machine PHI
1149/// nodes as input. We cannot just directly add them, because expansion
1150/// might result in multiple MBB's for one BB. As such, the start of the
1151/// BB might correspond to a different MBB than the end.
1152bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1153 const TerminatorInst *TI = LLVMBB->getTerminator();
1154
1155 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
1156 unsigned OrigNumPHINodesToUpdate = PHINodesToUpdate.size();
1157
1158 // Check successor nodes' PHI nodes that expect a constant to be available
1159 // from this block.
1160 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1161 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1162 if (!isa<PHINode>(SuccBB->begin())) continue;
1163 MachineBasicBlock *SuccMBB = MBBMap[SuccBB];
1164
1165 // If this terminator has multiple identical successors (common for
1166 // switches), only handle each succ once.
1167 if (!SuccsHandled.insert(SuccMBB)) continue;
1168
1169 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1170
1171 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1172 // nodes and Machine PHI nodes, but the incoming operands have not been
1173 // emitted yet.
1174 for (BasicBlock::const_iterator I = SuccBB->begin();
1175 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanfb95f892010-05-07 01:10:20 +00001176
Dan Gohmanf81eca02010-04-22 20:46:50 +00001177 // Ignore dead phi's.
1178 if (PN->use_empty()) continue;
1179
1180 // Only handle legal types. Two interesting things to note here. First,
1181 // by bailing out early, we may leave behind some dead instructions,
1182 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
1183 // own moves. Second, this check is necessary becuase FastISel doesn't
1184 // use CreateRegForValue to create registers, so it always creates
1185 // exactly one register for each non-void instruction.
1186 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1187 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
1188 // Promote MVT::i1.
1189 if (VT == MVT::i1)
1190 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1191 else {
1192 PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
1193 return false;
1194 }
1195 }
1196
1197 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1198
Dan Gohmanfb95f892010-05-07 01:10:20 +00001199 // Set the DebugLoc for the copy. Prefer the location of the operand
1200 // if there is one; use the location of the PHI otherwise.
1201 DL = PN->getDebugLoc();
1202 if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
1203 DL = Inst->getDebugLoc();
1204
Dan Gohmanf81eca02010-04-22 20:46:50 +00001205 unsigned Reg = getRegForValue(PHIOp);
1206 if (Reg == 0) {
1207 PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
1208 return false;
1209 }
1210 PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohmanfb95f892010-05-07 01:10:20 +00001211 DL = DebugLoc();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001212 }
1213 }
1214
1215 return true;
1216}