blob: 58d8344479ca2d6ef20aac2984d143096f41e6df [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.
Dan Gohmaneddc1142010-05-25 21:59:42 +0000102 DenseMap<const Value *, unsigned>::iterator I = ValueMap.find(V);
103 if (I != ValueMap.end())
104 return I->second;
Dan Gohman104e4ce2008-09-03 23:32:19 +0000105 unsigned Reg = LocalValueMap[V];
106 if (Reg != 0)
107 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000108
Dan Gohman97c94b82010-05-06 00:02:14 +0000109 // In bottom-up mode, just create the virtual register which will be used
110 // to hold the value. It will be materialized later.
111 if (IsBottomUp) {
112 Reg = createResultReg(TLI.getRegClassFor(VT));
113 if (isa<Instruction>(V))
114 ValueMap[V] = Reg;
115 else
116 LocalValueMap[V] = Reg;
117 return Reg;
118 }
119
Dan Gohman1fdc6142010-05-03 23:36:34 +0000120 return materializeRegForValue(V, VT);
121}
122
123/// materializeRegForValue - Helper for getRegForVale. This function is
124/// called when the value isn't already available in a register and must
125/// be materialized with new instructions.
126unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
127 unsigned Reg = 0;
128
Dan Gohman46510a72010-04-15 01:51:59 +0000129 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000130 if (CI->getValue().getActiveBits() <= 64)
131 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +0000132 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000133 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +0000134 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman1e9e8c32008-10-07 22:03:27 +0000135 // Translate this as an integer zero so that it can be
136 // local-CSE'd with actual integer zeros.
Owen Anderson1d0be152009-08-13 21:58:54 +0000137 Reg =
138 getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
Dan Gohman46510a72010-04-15 01:51:59 +0000139 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman4183e312010-04-13 17:07:06 +0000140 // Try to emit the constant directly.
Dan Gohman104e4ce2008-09-03 23:32:19 +0000141 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000142
143 if (!Reg) {
Dan Gohman4183e312010-04-13 17:07:06 +0000144 // Try to emit the constant by using an integer constant with a cast.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000145 const APFloat &Flt = CF->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000146 EVT IntVT = TLI.getPointerTy();
Dan Gohmanad368ac2008-08-27 18:10:19 +0000147
148 uint64_t x[2];
149 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000150 bool isExact;
151 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
152 APFloat::rmTowardZero, &isExact);
153 if (isExact) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000154 APInt IntVal(IntBitWidth, 2, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000155
Owen Andersone922c022009-07-22 00:24:57 +0000156 unsigned IntegerReg =
Owen Andersoneed707b2009-07-24 23:12:02 +0000157 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000158 if (IntegerReg != 0)
Dan Gohmana6cb6412010-05-11 23:54:07 +0000159 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
160 IntegerReg, /*Kill=*/false);
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000161 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000162 }
Dan Gohman46510a72010-04-15 01:51:59 +0000163 } else if (const Operator *Op = dyn_cast<Operator>(V)) {
Dan Gohman32acbc12010-04-14 02:33:23 +0000164 if (!SelectOperator(Op, Op->getOpcode())) return 0;
Dan Gohman37db6cd2010-06-21 14:17:46 +0000165 Reg = lookUpRegForValue(Op);
Dan Gohman205d9252008-08-28 21:19:07 +0000166 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000167 Reg = createResultReg(TLI.getRegClassFor(VT));
Chris Lattner518bb532010-02-09 19:54:29 +0000168 BuildMI(MBB, DL, TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000169 }
Owen Andersond5d81a42008-09-03 17:51:57 +0000170
Dan Gohmandceffe62008-09-25 01:28:51 +0000171 // If target-independent code couldn't handle the value, give target-specific
172 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000173 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000174 Reg = TargetMaterializeConstant(cast<Constant>(V));
Owen Anderson6e607452008-09-05 23:36:01 +0000175
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000176 // Don't cache constant materializations in the general ValueMap.
177 // To do so would require tracking what uses they dominate.
Dan Gohmandceffe62008-09-25 01:28:51 +0000178 if (Reg != 0)
179 LocalValueMap[V] = Reg;
Dan Gohman104e4ce2008-09-03 23:32:19 +0000180 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000181}
182
Dan Gohman46510a72010-04-15 01:51:59 +0000183unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng59fbc802008-09-09 01:26:59 +0000184 // Look up the value to see if we already have a register for it. We
185 // cache values defined by Instructions across blocks, and other values
186 // only locally. This is because Instructions already have the SSA
Dan Gohman1fdc6142010-05-03 23:36:34 +0000187 // def-dominates-use requirement enforced.
Evan Cheng59fbc802008-09-09 01:26:59 +0000188 if (ValueMap.count(V))
189 return ValueMap[V];
190 return LocalValueMap[V];
191}
192
Owen Andersoncc54e762008-08-30 00:38:46 +0000193/// UpdateValueMap - Update the value map to include the new mapping for this
194/// instruction, or insert an extra copy to get the result in a previous
195/// determined register.
196/// NOTE: This is only necessary because we might select a block that uses
197/// a value before we select the block that defines the value. It might be
198/// possible to fix this by selecting blocks in reverse postorder.
Dan Gohman46510a72010-04-15 01:51:59 +0000199unsigned FastISel::UpdateValueMap(const Value *I, unsigned Reg) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000200 if (!isa<Instruction>(I)) {
201 LocalValueMap[I] = Reg;
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000202 return Reg;
Dan Gohman40b189e2008-09-05 18:18:20 +0000203 }
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000204
205 unsigned &AssignedReg = ValueMap[I];
206 if (AssignedReg == 0)
207 AssignedReg = Reg;
Chris Lattner36e39462009-04-12 07:46:30 +0000208 else if (Reg != AssignedReg) {
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000209 const TargetRegisterClass *RegClass = MRI.getRegClass(Reg);
210 TII.copyRegToReg(*MBB, MBB->end(), AssignedReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000211 Reg, RegClass, RegClass, DL);
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000212 }
213 return AssignedReg;
Owen Andersoncc54e762008-08-30 00:38:46 +0000214}
215
Dan Gohmana6cb6412010-05-11 23:54:07 +0000216std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000217 unsigned IdxN = getRegForValue(Idx);
218 if (IdxN == 0)
219 // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000220 return std::pair<unsigned, bool>(0, false);
221
222 bool IdxNIsKill = hasTrivialKill(Idx);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000223
224 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Anderson766b5ef2009-08-11 21:59:30 +0000225 MVT PtrVT = TLI.getPointerTy();
Owen Andersone50ed302009-08-10 22:56:29 +0000226 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000227 if (IdxVT.bitsLT(PtrVT)) {
228 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND,
229 IdxN, IdxNIsKill);
230 IdxNIsKill = true;
231 }
232 else if (IdxVT.bitsGT(PtrVT)) {
233 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE,
234 IdxN, IdxNIsKill);
235 IdxNIsKill = true;
236 }
237 return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000238}
239
Dan Gohmanbdedd442008-08-20 00:11:48 +0000240/// SelectBinaryOp - Select and emit code for a binary operator instruction,
241/// which has an opcode which directly corresponds to the given ISD opcode.
242///
Dan Gohman46510a72010-04-15 01:51:59 +0000243bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000244 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmanbdedd442008-08-20 00:11:48 +0000246 // Unhandled type. Halt "fast" selection and bail.
247 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000248
Dan Gohmanb71fea22008-08-26 20:52:40 +0000249 // We only handle legal types. For example, on x86-32 the instruction
250 // selector contains all of the 64-bit instructions from x86-64,
251 // under the assumption that i64 won't be used if the target doesn't
252 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000253 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000254 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000255 // don't require additional zeroing, which makes them easy.
Owen Anderson825b72b2009-08-11 20:47:22 +0000256 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000257 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
258 ISDOpcode == ISD::XOR))
Owen Anderson23b9b192009-08-12 00:36:31 +0000259 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohman638c6832008-09-05 18:44:22 +0000260 else
261 return false;
262 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000263
Dan Gohman3df24e62008-09-03 23:12:08 +0000264 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000265 if (Op0 == 0)
266 // Unhandled operand. Halt "fast" selection and bail.
267 return false;
268
Dan Gohmana6cb6412010-05-11 23:54:07 +0000269 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
270
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000271 // Check if the second operand is a constant and handle it appropriately.
272 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000273 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000274 ISDOpcode, Op0, Op0IsKill,
275 CI->getZExtValue());
Dan Gohmanad368ac2008-08-27 18:10:19 +0000276 if (ResultReg != 0) {
277 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000278 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000279 return true;
280 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000281 }
282
Dan Gohman10df0fa2008-08-27 01:09:54 +0000283 // Check if the second operand is a constant float.
284 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000285 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000286 ISDOpcode, Op0, Op0IsKill, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000287 if (ResultReg != 0) {
288 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000289 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000290 return true;
291 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000292 }
293
Dan Gohman3df24e62008-09-03 23:12:08 +0000294 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000295 if (Op1 == 0)
296 // Unhandled operand. Halt "fast" selection and bail.
297 return false;
298
Dan Gohmana6cb6412010-05-11 23:54:07 +0000299 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
300
Dan Gohmanad368ac2008-08-27 18:10:19 +0000301 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000302 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000303 ISDOpcode,
304 Op0, Op0IsKill,
305 Op1, Op1IsKill);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000306 if (ResultReg == 0)
307 // Target-specific code wasn't able to find a machine opcode for
308 // the given ISD opcode and type. Halt "fast" selection and bail.
309 return false;
310
Dan Gohman8014e862008-08-20 00:23:20 +0000311 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000312 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000313 return true;
314}
315
Dan Gohman46510a72010-04-15 01:51:59 +0000316bool FastISel::SelectGetElementPtr(const User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000317 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000318 if (N == 0)
319 // Unhandled operand. Halt "fast" selection and bail.
320 return false;
321
Dan Gohmana6cb6412010-05-11 23:54:07 +0000322 bool NIsKill = hasTrivialKill(I->getOperand(0));
323
Evan Cheng83785c82008-08-20 22:45:34 +0000324 const Type *Ty = I->getOperand(0)->getType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 MVT VT = TLI.getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +0000326 for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
327 E = I->op_end(); OI != E; ++OI) {
328 const Value *Idx = *OI;
Evan Cheng83785c82008-08-20 22:45:34 +0000329 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
330 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
331 if (Field) {
332 // N = N + Offset
333 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
334 // FIXME: This can be optimized by combining the add with a
335 // subsequent one.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000336 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000337 if (N == 0)
338 // Unhandled operand. Halt "fast" selection and bail.
339 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000340 NIsKill = true;
Evan Cheng83785c82008-08-20 22:45:34 +0000341 }
342 Ty = StTy->getElementType(Field);
343 } else {
344 Ty = cast<SequentialType>(Ty)->getElementType();
345
346 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +0000347 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +0000348 if (CI->isZero()) continue;
Evan Cheng83785c82008-08-20 22:45:34 +0000349 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +0000350 TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohmana6cb6412010-05-11 23:54:07 +0000351 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000352 if (N == 0)
353 // Unhandled operand. Halt "fast" selection and bail.
354 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000355 NIsKill = true;
Evan Cheng83785c82008-08-20 22:45:34 +0000356 continue;
357 }
358
359 // N = N + Idx * ElementSize;
Duncan Sands777d2302009-05-09 07:06:46 +0000360 uint64_t ElementSize = TD.getTypeAllocSize(Ty);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000361 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
362 unsigned IdxN = Pair.first;
363 bool IdxNIsKill = Pair.second;
Evan Cheng83785c82008-08-20 22:45:34 +0000364 if (IdxN == 0)
365 // Unhandled operand. Halt "fast" selection and bail.
366 return false;
367
Dan Gohman80bc6e22008-08-26 20:57:08 +0000368 if (ElementSize != 1) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000369 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000370 if (IdxN == 0)
371 // Unhandled operand. Halt "fast" selection and bail.
372 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000373 IdxNIsKill = true;
Dan Gohman80bc6e22008-08-26 20:57:08 +0000374 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000375 N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
Evan Cheng83785c82008-08-20 22:45:34 +0000376 if (N == 0)
377 // Unhandled operand. Halt "fast" selection and bail.
378 return false;
379 }
380 }
381
382 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000383 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000384 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000385}
386
Dan Gohman46510a72010-04-15 01:51:59 +0000387bool FastISel::SelectCall(const User *I) {
388 const Function *F = cast<CallInst>(I)->getCalledFunction();
Dan Gohman33134c42008-09-25 17:05:24 +0000389 if (!F) return false;
390
Dan Gohman4183e312010-04-13 17:07:06 +0000391 // Handle selected intrinsic function calls.
Dan Gohman33134c42008-09-25 17:05:24 +0000392 unsigned IID = F->getIntrinsicID();
393 switch (IID) {
394 default: break;
Bill Wendling92c1e122009-02-13 02:16:35 +0000395 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +0000396 const DbgDeclareInst *DI = cast<DbgDeclareInst>(I);
Devang Patel02f0dbd2010-05-07 22:04:20 +0000397 if (!DIVariable(DI->getVariable()).Verify() ||
Chris Lattnered3a8062010-04-05 06:05:26 +0000398 !MF.getMMI().hasDebugInfo())
Devang Patel7e1e31f2009-07-02 22:43:26 +0000399 return true;
400
Dan Gohman46510a72010-04-15 01:51:59 +0000401 const Value *Address = DI->getAddress();
Dale Johannesendc918562010-02-06 02:26:02 +0000402 if (!Address)
403 return true;
Dale Johannesen343b42e2010-04-07 01:15:14 +0000404 if (isa<UndefValue>(Address))
405 return true;
Dan Gohman46510a72010-04-15 01:51:59 +0000406 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
Devang Patel7e1e31f2009-07-02 22:43:26 +0000407 // Don't handle byval struct arguments or VLAs, for example.
Dale Johannesen7dc78402010-04-25 21:03:54 +0000408 // Note that if we have a byval struct argument, fast ISel is turned off;
409 // those are handled in SelectionDAGBuilder.
Devang Patel54fc4d62010-04-28 19:27:33 +0000410 if (AI) {
411 DenseMap<const AllocaInst*, int>::iterator SI =
412 StaticAllocaMap.find(AI);
413 if (SI == StaticAllocaMap.end()) break; // VLAs.
414 int FI = SI->second;
415 if (!DI->getDebugLoc().isUnknown())
416 MF.getMMI().setVariableDbgInfo(DI->getVariable(), FI, DI->getDebugLoc());
417 } else
418 // Building the map above is target independent. Generating DBG_VALUE
419 // inline is target dependent; do this now.
420 (void)TargetSelectInstruction(cast<Instruction>(I));
Dan Gohman33134c42008-09-25 17:05:24 +0000421 return true;
Bill Wendling92c1e122009-02-13 02:16:35 +0000422 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000423 case Intrinsic::dbg_value: {
Dale Johannesen343b42e2010-04-07 01:15:14 +0000424 // This form of DBG_VALUE is target-independent.
Dan Gohman46510a72010-04-15 01:51:59 +0000425 const DbgValueInst *DI = cast<DbgValueInst>(I);
Dale Johannesen45df7612010-02-26 20:01:55 +0000426 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohman46510a72010-04-15 01:51:59 +0000427 const Value *V = DI->getValue();
Dale Johannesen45df7612010-02-26 20:01:55 +0000428 if (!V) {
429 // Currently the optimizer can produce this; insert an undef to
430 // help debugging. Probably the optimizer should not do this.
431 BuildMI(MBB, DL, II).addReg(0U).addImm(DI->getOffset()).
432 addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000433 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dale Johannesen45df7612010-02-26 20:01:55 +0000434 BuildMI(MBB, DL, II).addImm(CI->getZExtValue()).addImm(DI->getOffset()).
435 addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000436 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dale Johannesen45df7612010-02-26 20:01:55 +0000437 BuildMI(MBB, DL, II).addFPImm(CF).addImm(DI->getOffset()).
438 addMetadata(DI->getVariable());
439 } else if (unsigned Reg = lookUpRegForValue(V)) {
440 BuildMI(MBB, DL, II).addReg(Reg, RegState::Debug).addImm(DI->getOffset()).
441 addMetadata(DI->getVariable());
442 } else {
443 // We can't yet handle anything else here because it would require
444 // generating code, thus altering codegen because of debug info.
445 // Insert an undef so we can see what we dropped.
446 BuildMI(MBB, DL, II).addReg(0U).addImm(DI->getOffset()).
447 addMetadata(DI->getVariable());
448 }
449 return true;
450 }
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000451 case Intrinsic::eh_exception: {
Owen Andersone50ed302009-08-10 22:56:29 +0000452 EVT VT = TLI.getValueType(I->getType());
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000453 switch (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)) {
454 default: break;
455 case TargetLowering::Expand: {
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000456 assert(MBB->isLandingPad() && "Call to eh.exception not in landing pad!");
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000457 unsigned Reg = TLI.getExceptionAddressRegister();
458 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
459 unsigned ResultReg = createResultReg(RC);
460 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000461 Reg, RC, RC, DL);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000462 assert(InsertedCopy && "Can't copy address registers!");
Evan Cheng24ac4082008-11-24 07:09:49 +0000463 InsertedCopy = InsertedCopy;
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000464 UpdateValueMap(I, ResultReg);
465 return true;
466 }
467 }
468 break;
469 }
Duncan Sandsb01bbdc2009-10-14 16:11:37 +0000470 case Intrinsic::eh_selector: {
Owen Andersone50ed302009-08-10 22:56:29 +0000471 EVT VT = TLI.getValueType(I->getType());
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000472 switch (TLI.getOperationAction(ISD::EHSELECTION, VT)) {
473 default: break;
474 case TargetLowering::Expand: {
Chris Lattnered3a8062010-04-05 06:05:26 +0000475 if (MBB->isLandingPad())
476 AddCatchInfo(*cast<CallInst>(I), &MF.getMMI(), MBB);
477 else {
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000478#ifndef NDEBUG
Chris Lattnered3a8062010-04-05 06:05:26 +0000479 CatchInfoLost.insert(cast<CallInst>(I));
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000480#endif
Chris Lattnered3a8062010-04-05 06:05:26 +0000481 // FIXME: Mark exception selector register as live in. Hack for PR1508.
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000482 unsigned Reg = TLI.getExceptionSelectorRegister();
Chris Lattnered3a8062010-04-05 06:05:26 +0000483 if (Reg) MBB->addLiveIn(Reg);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000484 }
Chris Lattnered3a8062010-04-05 06:05:26 +0000485
486 unsigned Reg = TLI.getExceptionSelectorRegister();
487 EVT SrcVT = TLI.getPointerTy();
488 const TargetRegisterClass *RC = TLI.getRegClassFor(SrcVT);
489 unsigned ResultReg = createResultReg(RC);
490 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, Reg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000491 RC, RC, DL);
Chris Lattnered3a8062010-04-05 06:05:26 +0000492 assert(InsertedCopy && "Can't copy address registers!");
493 InsertedCopy = InsertedCopy;
494
Dan Gohmana6cb6412010-05-11 23:54:07 +0000495 bool ResultRegIsKill = hasTrivialKill(I);
496
Chris Lattnered3a8062010-04-05 06:05:26 +0000497 // Cast the register to the type of the selector.
498 if (SrcVT.bitsGT(MVT::i32))
499 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, ISD::TRUNCATE,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000500 ResultReg, ResultRegIsKill);
Chris Lattnered3a8062010-04-05 06:05:26 +0000501 else if (SrcVT.bitsLT(MVT::i32))
502 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000503 ISD::SIGN_EXTEND, ResultReg, ResultRegIsKill);
Chris Lattnered3a8062010-04-05 06:05:26 +0000504 if (ResultReg == 0)
505 // Unhandled operand. Halt "fast" selection and bail.
506 return false;
507
508 UpdateValueMap(I, ResultReg);
509
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000510 return true;
511 }
512 }
513 break;
514 }
Dan Gohman33134c42008-09-25 17:05:24 +0000515 }
Dan Gohman4183e312010-04-13 17:07:06 +0000516
517 // An arbitrary call. Bail.
Dan Gohman33134c42008-09-25 17:05:24 +0000518 return false;
519}
520
Dan Gohman46510a72010-04-15 01:51:59 +0000521bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000522 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
523 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000524
Owen Anderson825b72b2009-08-11 20:47:22 +0000525 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
526 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersond0533c92008-08-26 23:46:32 +0000527 // Unhandled type. Halt "fast" selection and bail.
528 return false;
529
Dan Gohman474d3b32009-03-13 23:53:06 +0000530 // Check if the destination type is legal. Or as a special case,
531 // it may be i1 if we're doing a truncate because that's
532 // easy and somewhat common.
533 if (!TLI.isTypeLegal(DstVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000534 if (DstVT != MVT::i1 || Opcode != ISD::TRUNCATE)
Dan Gohman91b6f972008-10-03 01:28:47 +0000535 // Unhandled type. Halt "fast" selection and bail.
536 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000537
538 // Check if the source operand is legal. Or as a special case,
539 // it may be i1 if we're doing zero-extension because that's
540 // easy and somewhat common.
541 if (!TLI.isTypeLegal(SrcVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000542 if (SrcVT != MVT::i1 || Opcode != ISD::ZERO_EXTEND)
Dan Gohman474d3b32009-03-13 23:53:06 +0000543 // Unhandled type. Halt "fast" selection and bail.
544 return false;
545
Dan Gohman3df24e62008-09-03 23:12:08 +0000546 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000547 if (!InputReg)
548 // Unhandled operand. Halt "fast" selection and bail.
549 return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000550
Dan Gohmana6cb6412010-05-11 23:54:07 +0000551 bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
552
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000553 // If the operand is i1, arrange for the high bits in the register to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +0000554 if (SrcVT == MVT::i1) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000555 SrcVT = TLI.getTypeToTransformTo(I->getContext(), SrcVT);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000556 InputReg = FastEmitZExtFromI1(SrcVT.getSimpleVT(), InputReg, InputRegIsKill);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000557 if (!InputReg)
558 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000559 InputRegIsKill = true;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000560 }
Dan Gohman474d3b32009-03-13 23:53:06 +0000561 // If the result is i1, truncate to the target's type for i1 first.
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 if (DstVT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +0000563 DstVT = TLI.getTypeToTransformTo(I->getContext(), DstVT);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000564
Owen Andersond0533c92008-08-26 23:46:32 +0000565 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
566 DstVT.getSimpleVT(),
567 Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000568 InputReg, InputRegIsKill);
Owen Andersond0533c92008-08-26 23:46:32 +0000569 if (!ResultReg)
570 return false;
571
Dan Gohman3df24e62008-09-03 23:12:08 +0000572 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000573 return true;
574}
575
Dan Gohman46510a72010-04-15 01:51:59 +0000576bool FastISel::SelectBitCast(const User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000577 // If the bitcast doesn't change the type, just use the operand value.
578 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000579 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000580 if (Reg == 0)
581 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000582 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000583 return true;
584 }
585
586 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Andersone50ed302009-08-10 22:56:29 +0000587 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
588 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000589
Owen Anderson825b72b2009-08-11 20:47:22 +0000590 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
591 DstVT == MVT::Other || !DstVT.isSimple() ||
Owen Andersond0533c92008-08-26 23:46:32 +0000592 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
593 // Unhandled type. Halt "fast" selection and bail.
594 return false;
595
Dan Gohman3df24e62008-09-03 23:12:08 +0000596 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000597 if (Op0 == 0)
598 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000599 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000600
601 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000602
Dan Gohmanad368ac2008-08-27 18:10:19 +0000603 // First, try to perform the bitcast by inserting a reg-reg copy.
604 unsigned ResultReg = 0;
605 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
606 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
607 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
608 ResultReg = createResultReg(DstClass);
609
610 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000611 Op0, DstClass, SrcClass, DL);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000612 if (!InsertedCopy)
613 ResultReg = 0;
614 }
615
616 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
617 if (!ResultReg)
618 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000619 ISD::BIT_CONVERT, Op0, Op0IsKill);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000620
621 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000622 return false;
623
Dan Gohman3df24e62008-09-03 23:12:08 +0000624 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000625 return true;
626}
627
Dan Gohman3df24e62008-09-03 23:12:08 +0000628bool
Dan Gohman46510a72010-04-15 01:51:59 +0000629FastISel::SelectInstruction(const Instruction *I) {
Dan Gohmane8c92dd2010-04-23 15:29:50 +0000630 // Just before the terminator instruction, insert instructions to
631 // feed PHI nodes in successor blocks.
632 if (isa<TerminatorInst>(I))
633 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
634 return false;
635
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000636 DL = I->getDebugLoc();
637
Dan Gohman6e3ff372009-12-05 01:27:58 +0000638 // First, try doing target-independent selection.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000639 if (SelectOperator(I, I->getOpcode())) {
640 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000641 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000642 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000643
644 // Next, try calling the target to attempt to handle the instruction.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000645 if (TargetSelectInstruction(I)) {
646 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000647 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000648 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000649
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000650 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000651 return false;
Dan Gohman40b189e2008-09-05 18:18:20 +0000652}
653
Dan Gohmand98d6202008-10-02 22:15:21 +0000654/// FastEmitBranch - Emit an unconditional branch to the given block,
655/// unless it is the immediate (fall-through) successor, and update
656/// the CFG.
657void
Stuart Hastings3bf91252010-06-17 22:43:56 +0000658FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DL) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000659 if (MBB->isLayoutSuccessor(MSucc)) {
660 // The unconditional fall-through case, which needs no instructions.
661 } else {
662 // The unconditional branch case.
Stuart Hastings3bf91252010-06-17 22:43:56 +0000663 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>(), DL);
Dan Gohmand98d6202008-10-02 22:15:21 +0000664 }
665 MBB->addSuccessor(MSucc);
666}
667
Dan Gohman3d45a852009-09-03 22:53:57 +0000668/// SelectFNeg - Emit an FNeg operation.
669///
670bool
Dan Gohman46510a72010-04-15 01:51:59 +0000671FastISel::SelectFNeg(const User *I) {
Dan Gohman3d45a852009-09-03 22:53:57 +0000672 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
673 if (OpReg == 0) return false;
674
Dan Gohmana6cb6412010-05-11 23:54:07 +0000675 bool OpRegIsKill = hasTrivialKill(I);
676
Dan Gohman4a215a12009-09-11 00:36:43 +0000677 // If the target has ISD::FNEG, use it.
678 EVT VT = TLI.getValueType(I->getType());
679 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000680 ISD::FNEG, OpReg, OpRegIsKill);
Dan Gohman4a215a12009-09-11 00:36:43 +0000681 if (ResultReg != 0) {
682 UpdateValueMap(I, ResultReg);
683 return true;
684 }
685
Dan Gohman5e5abb72009-09-11 00:34:46 +0000686 // Bitcast the value to integer, twiddle the sign bit with xor,
687 // and then bitcast it back to floating-point.
Dan Gohman3d45a852009-09-03 22:53:57 +0000688 if (VT.getSizeInBits() > 64) return false;
Dan Gohman5e5abb72009-09-11 00:34:46 +0000689 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
690 if (!TLI.isTypeLegal(IntVT))
691 return false;
692
693 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000694 ISD::BIT_CONVERT, OpReg, OpRegIsKill);
Dan Gohman5e5abb72009-09-11 00:34:46 +0000695 if (IntReg == 0)
696 return false;
697
Dan Gohmana6cb6412010-05-11 23:54:07 +0000698 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
699 IntReg, /*Kill=*/true,
Dan Gohman5e5abb72009-09-11 00:34:46 +0000700 UINT64_C(1) << (VT.getSizeInBits()-1),
701 IntVT.getSimpleVT());
702 if (IntResultReg == 0)
703 return false;
704
705 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000706 ISD::BIT_CONVERT, IntResultReg, /*Kill=*/true);
Dan Gohman3d45a852009-09-03 22:53:57 +0000707 if (ResultReg == 0)
708 return false;
709
710 UpdateValueMap(I, ResultReg);
711 return true;
712}
713
Dan Gohman40b189e2008-09-05 18:18:20 +0000714bool
Dan Gohman46510a72010-04-15 01:51:59 +0000715FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000716 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000717 case Instruction::Add:
718 return SelectBinaryOp(I, ISD::ADD);
719 case Instruction::FAdd:
720 return SelectBinaryOp(I, ISD::FADD);
721 case Instruction::Sub:
722 return SelectBinaryOp(I, ISD::SUB);
723 case Instruction::FSub:
Dan Gohman3d45a852009-09-03 22:53:57 +0000724 // FNeg is currently represented in LLVM IR as a special case of FSub.
725 if (BinaryOperator::isFNeg(I))
726 return SelectFNeg(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000727 return SelectBinaryOp(I, ISD::FSUB);
728 case Instruction::Mul:
729 return SelectBinaryOp(I, ISD::MUL);
730 case Instruction::FMul:
731 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman3df24e62008-09-03 23:12:08 +0000732 case Instruction::SDiv:
733 return SelectBinaryOp(I, ISD::SDIV);
734 case Instruction::UDiv:
735 return SelectBinaryOp(I, ISD::UDIV);
736 case Instruction::FDiv:
737 return SelectBinaryOp(I, ISD::FDIV);
738 case Instruction::SRem:
739 return SelectBinaryOp(I, ISD::SREM);
740 case Instruction::URem:
741 return SelectBinaryOp(I, ISD::UREM);
742 case Instruction::FRem:
743 return SelectBinaryOp(I, ISD::FREM);
744 case Instruction::Shl:
745 return SelectBinaryOp(I, ISD::SHL);
746 case Instruction::LShr:
747 return SelectBinaryOp(I, ISD::SRL);
748 case Instruction::AShr:
749 return SelectBinaryOp(I, ISD::SRA);
750 case Instruction::And:
751 return SelectBinaryOp(I, ISD::AND);
752 case Instruction::Or:
753 return SelectBinaryOp(I, ISD::OR);
754 case Instruction::Xor:
755 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000756
Dan Gohman3df24e62008-09-03 23:12:08 +0000757 case Instruction::GetElementPtr:
758 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000759
Dan Gohman3df24e62008-09-03 23:12:08 +0000760 case Instruction::Br: {
Dan Gohman46510a72010-04-15 01:51:59 +0000761 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000762
Dan Gohman3df24e62008-09-03 23:12:08 +0000763 if (BI->isUnconditional()) {
Dan Gohman46510a72010-04-15 01:51:59 +0000764 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohman3df24e62008-09-03 23:12:08 +0000765 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
Stuart Hastings3bf91252010-06-17 22:43:56 +0000766 FastEmitBranch(MSucc, BI->getDebugLoc());
Dan Gohman3df24e62008-09-03 23:12:08 +0000767 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000768 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000769
770 // Conditional branches are not handed yet.
771 // Halt "fast" selection and bail.
772 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000773 }
774
Dan Gohman087c8502008-09-05 01:08:41 +0000775 case Instruction::Unreachable:
776 // Nothing to emit.
777 return true;
778
Dan Gohman0586d912008-09-10 20:11:02 +0000779 case Instruction::Alloca:
780 // FunctionLowering has the static-sized case covered.
781 if (StaticAllocaMap.count(cast<AllocaInst>(I)))
782 return true;
783
784 // Dynamic-sized alloca is not handled yet.
785 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000786
Dan Gohman33134c42008-09-25 17:05:24 +0000787 case Instruction::Call:
788 return SelectCall(I);
789
Dan Gohman3df24e62008-09-03 23:12:08 +0000790 case Instruction::BitCast:
791 return SelectBitCast(I);
792
793 case Instruction::FPToSI:
794 return SelectCast(I, ISD::FP_TO_SINT);
795 case Instruction::ZExt:
796 return SelectCast(I, ISD::ZERO_EXTEND);
797 case Instruction::SExt:
798 return SelectCast(I, ISD::SIGN_EXTEND);
799 case Instruction::Trunc:
800 return SelectCast(I, ISD::TRUNCATE);
801 case Instruction::SIToFP:
802 return SelectCast(I, ISD::SINT_TO_FP);
803
804 case Instruction::IntToPtr: // Deliberate fall-through.
805 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +0000806 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
807 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman3df24e62008-09-03 23:12:08 +0000808 if (DstVT.bitsGT(SrcVT))
809 return SelectCast(I, ISD::ZERO_EXTEND);
810 if (DstVT.bitsLT(SrcVT))
811 return SelectCast(I, ISD::TRUNCATE);
812 unsigned Reg = getRegForValue(I->getOperand(0));
813 if (Reg == 0) return false;
814 UpdateValueMap(I, Reg);
815 return true;
816 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000817
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000818 case Instruction::PHI:
819 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
820
Dan Gohman3df24e62008-09-03 23:12:08 +0000821 default:
822 // Unhandled instruction. Halt "fast" selection and bail.
823 return false;
824 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000825}
826
Dan Gohman3df24e62008-09-03 23:12:08 +0000827FastISel::FastISel(MachineFunction &mf,
828 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +0000829 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmanf81eca02010-04-22 20:46:50 +0000830 DenseMap<const AllocaInst *, int> &am,
831 std::vector<std::pair<MachineInstr*, unsigned> > &pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000832#ifndef NDEBUG
Dan Gohman25208642010-04-14 19:53:31 +0000833 , SmallSet<const Instruction *, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000834#endif
835 )
Dan Gohman3df24e62008-09-03 23:12:08 +0000836 : MBB(0),
837 ValueMap(vm),
838 MBBMap(bm),
Dan Gohman0586d912008-09-10 20:11:02 +0000839 StaticAllocaMap(am),
Dan Gohmanf81eca02010-04-22 20:46:50 +0000840 PHINodesToUpdate(pn),
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000841#ifndef NDEBUG
842 CatchInfoLost(cil),
843#endif
Dan Gohman3df24e62008-09-03 23:12:08 +0000844 MF(mf),
845 MRI(MF.getRegInfo()),
Dan Gohman0586d912008-09-10 20:11:02 +0000846 MFI(*MF.getFrameInfo()),
847 MCP(*MF.getConstantPool()),
Dan Gohman3df24e62008-09-03 23:12:08 +0000848 TM(MF.getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000849 TD(*TM.getTargetData()),
850 TII(*TM.getInstrInfo()),
Dan Gohmana7a0ed72010-05-05 23:58:35 +0000851 TLI(*TM.getTargetLowering()),
Dan Gohmandb497122010-06-18 23:28:01 +0000852 TRI(*TM.getRegisterInfo()),
Dan Gohmana7a0ed72010-05-05 23:58:35 +0000853 IsBottomUp(false) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000854}
855
Dan Gohmane285a742008-08-14 21:51:29 +0000856FastISel::~FastISel() {}
857
Owen Anderson825b72b2009-08-11 20:47:22 +0000858unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000859 unsigned) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000860 return 0;
861}
862
Owen Anderson825b72b2009-08-11 20:47:22 +0000863unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000864 unsigned,
865 unsigned /*Op0*/, bool /*Op0IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000866 return 0;
867}
868
Owen Anderson825b72b2009-08-11 20:47:22 +0000869unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000870 unsigned,
871 unsigned /*Op0*/, bool /*Op0IsKill*/,
872 unsigned /*Op1*/, bool /*Op1IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000873 return 0;
874}
875
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000876unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000877 return 0;
878}
879
Owen Anderson825b72b2009-08-11 20:47:22 +0000880unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohman46510a72010-04-15 01:51:59 +0000881 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000882 return 0;
883}
884
Owen Anderson825b72b2009-08-11 20:47:22 +0000885unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000886 unsigned,
887 unsigned /*Op0*/, bool /*Op0IsKill*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000888 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000889 return 0;
890}
891
Owen Anderson825b72b2009-08-11 20:47:22 +0000892unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000893 unsigned,
894 unsigned /*Op0*/, bool /*Op0IsKill*/,
Dan Gohman46510a72010-04-15 01:51:59 +0000895 const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000896 return 0;
897}
898
Owen Anderson825b72b2009-08-11 20:47:22 +0000899unsigned FastISel::FastEmit_rri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000900 unsigned,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000901 unsigned /*Op0*/, bool /*Op0IsKill*/,
902 unsigned /*Op1*/, bool /*Op1IsKill*/,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000903 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000904 return 0;
905}
906
907/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
908/// to emit an instruction with an immediate operand using FastEmit_ri.
909/// If that fails, it materializes the immediate into a register and try
910/// FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000911unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000912 unsigned Op0, bool Op0IsKill,
913 uint64_t Imm, MVT ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000914 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000915 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000916 if (ResultReg != 0)
917 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000918 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000919 if (MaterialReg == 0)
920 return 0;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000921 return FastEmit_rr(VT, VT, Opcode,
922 Op0, Op0IsKill,
923 MaterialReg, /*Kill=*/true);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000924}
925
Dan Gohman10df0fa2008-08-27 01:09:54 +0000926/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
927/// to emit an instruction with a floating-point immediate operand using
928/// FastEmit_rf. If that fails, it materializes the immediate into a register
929/// and try FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000930unsigned FastISel::FastEmit_rf_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000931 unsigned Op0, bool Op0IsKill,
932 const ConstantFP *FPImm, MVT ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000933 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000934 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, Op0IsKill, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000935 if (ResultReg != 0)
936 return ResultReg;
937
938 // Materialize the constant in a register.
939 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
940 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000941 // If the target doesn't have a way to directly enter a floating-point
942 // value into a register, use an alternate approach.
943 // TODO: The current approach only supports floating-point constants
944 // that can be constructed by conversion from integer values. This should
945 // be replaced by code that creates a load from a constant-pool entry,
946 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000947 const APFloat &Flt = FPImm->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000948 EVT IntVT = TLI.getPointerTy();
Dan Gohman10df0fa2008-08-27 01:09:54 +0000949
950 uint64_t x[2];
951 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000952 bool isExact;
953 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
954 APFloat::rmTowardZero, &isExact);
955 if (!isExact)
Dan Gohman10df0fa2008-08-27 01:09:54 +0000956 return 0;
957 APInt IntVal(IntBitWidth, 2, x);
958
959 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
960 ISD::Constant, IntVal.getZExtValue());
961 if (IntegerReg == 0)
962 return 0;
963 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000964 ISD::SINT_TO_FP, IntegerReg, /*Kill=*/true);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000965 if (MaterialReg == 0)
966 return 0;
967 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000968 return FastEmit_rr(VT, VT, Opcode,
969 Op0, Op0IsKill,
970 MaterialReg, /*Kill=*/true);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000971}
972
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000973unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
974 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000975}
976
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000977unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000978 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000979 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000980 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000981
Bill Wendling9bc96a52009-02-03 00:55:04 +0000982 BuildMI(MBB, DL, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000983 return ResultReg;
984}
985
986unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
987 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000988 unsigned Op0, bool Op0IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000989 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000990 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000991
Evan Cheng5960e4e2008-09-08 08:38:20 +0000992 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +0000993 BuildMI(MBB, DL, II, ResultReg).addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000994 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000995 BuildMI(MBB, DL, II).addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000996 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000997 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000998 if (!InsertedCopy)
999 ResultReg = 0;
1000 }
1001
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001002 return ResultReg;
1003}
1004
1005unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
1006 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001007 unsigned Op0, bool Op0IsKill,
1008 unsigned Op1, bool Op1IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001009 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +00001010 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001011
Evan Cheng5960e4e2008-09-08 08:38:20 +00001012 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001013 BuildMI(MBB, DL, II, ResultReg)
1014 .addReg(Op0, Op0IsKill * RegState::Kill)
1015 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001016 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001017 BuildMI(MBB, DL, II)
1018 .addReg(Op0, Op0IsKill * RegState::Kill)
1019 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001020 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001021 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001022 if (!InsertedCopy)
1023 ResultReg = 0;
1024 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001025 return ResultReg;
1026}
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001027
1028unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1029 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001030 unsigned Op0, bool Op0IsKill,
1031 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001032 unsigned ResultReg = createResultReg(RC);
1033 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1034
Evan Cheng5960e4e2008-09-08 08:38:20 +00001035 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001036 BuildMI(MBB, DL, II, ResultReg)
1037 .addReg(Op0, Op0IsKill * RegState::Kill)
1038 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001039 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001040 BuildMI(MBB, DL, II)
1041 .addReg(Op0, Op0IsKill * RegState::Kill)
1042 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001043 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001044 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001045 if (!InsertedCopy)
1046 ResultReg = 0;
1047 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001048 return ResultReg;
1049}
1050
Dan Gohman10df0fa2008-08-27 01:09:54 +00001051unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
1052 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001053 unsigned Op0, bool Op0IsKill,
1054 const ConstantFP *FPImm) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001055 unsigned ResultReg = createResultReg(RC);
1056 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1057
Evan Cheng5960e4e2008-09-08 08:38:20 +00001058 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001059 BuildMI(MBB, DL, II, ResultReg)
1060 .addReg(Op0, Op0IsKill * RegState::Kill)
1061 .addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001062 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001063 BuildMI(MBB, DL, II)
1064 .addReg(Op0, Op0IsKill * RegState::Kill)
1065 .addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001066 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001067 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001068 if (!InsertedCopy)
1069 ResultReg = 0;
1070 }
Dan Gohman10df0fa2008-08-27 01:09:54 +00001071 return ResultReg;
1072}
1073
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001074unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
1075 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001076 unsigned Op0, bool Op0IsKill,
1077 unsigned Op1, bool Op1IsKill,
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 Gohmana6cb6412010-05-11 23:54:07 +00001083 BuildMI(MBB, DL, II, ResultReg)
1084 .addReg(Op0, Op0IsKill * RegState::Kill)
1085 .addReg(Op1, Op1IsKill * RegState::Kill)
1086 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001087 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001088 BuildMI(MBB, DL, II)
1089 .addReg(Op0, Op0IsKill * RegState::Kill)
1090 .addReg(Op1, Op1IsKill * RegState::Kill)
1091 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001092 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001093 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001094 if (!InsertedCopy)
1095 ResultReg = 0;
1096 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001097 return ResultReg;
1098}
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001099
1100unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1101 const TargetRegisterClass *RC,
1102 uint64_t Imm) {
1103 unsigned ResultReg = createResultReg(RC);
1104 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1105
Evan Cheng5960e4e2008-09-08 08:38:20 +00001106 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +00001107 BuildMI(MBB, DL, II, ResultReg).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001108 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +00001109 BuildMI(MBB, DL, II).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001110 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001111 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001112 if (!InsertedCopy)
1113 ResultReg = 0;
1114 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001115 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +00001116}
Owen Anderson8970f002008-08-27 22:30:02 +00001117
Owen Anderson825b72b2009-08-11 20:47:22 +00001118unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001119 unsigned Op0, bool Op0IsKill,
1120 uint32_t Idx) {
Owen Anderson40a468f2008-08-28 17:47:37 +00001121 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +00001122
Evan Cheng536ab132009-01-22 09:10:11 +00001123 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Chris Lattner518bb532010-02-09 19:54:29 +00001124 const TargetInstrDesc &II = TII.get(TargetOpcode::EXTRACT_SUBREG);
Owen Anderson8970f002008-08-27 22:30:02 +00001125
Evan Cheng5960e4e2008-09-08 08:38:20 +00001126 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001127 BuildMI(MBB, DL, II, ResultReg)
1128 .addReg(Op0, Op0IsKill * RegState::Kill)
1129 .addImm(Idx);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001130 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001131 BuildMI(MBB, DL, II)
1132 .addReg(Op0, Op0IsKill * RegState::Kill)
1133 .addImm(Idx);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001134 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001135 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001136 if (!InsertedCopy)
1137 ResultReg = 0;
1138 }
Owen Anderson8970f002008-08-27 22:30:02 +00001139 return ResultReg;
1140}
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001141
1142/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1143/// with all but the least significant bit set to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001144unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1145 return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001146}
Dan Gohmanf81eca02010-04-22 20:46:50 +00001147
1148/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1149/// Emit code to ensure constants are copied into registers when needed.
1150/// Remember the virtual registers that need to be added to the Machine PHI
1151/// nodes as input. We cannot just directly add them, because expansion
1152/// might result in multiple MBB's for one BB. As such, the start of the
1153/// BB might correspond to a different MBB than the end.
1154bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1155 const TerminatorInst *TI = LLVMBB->getTerminator();
1156
1157 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
1158 unsigned OrigNumPHINodesToUpdate = PHINodesToUpdate.size();
1159
1160 // Check successor nodes' PHI nodes that expect a constant to be available
1161 // from this block.
1162 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1163 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1164 if (!isa<PHINode>(SuccBB->begin())) continue;
1165 MachineBasicBlock *SuccMBB = MBBMap[SuccBB];
1166
1167 // If this terminator has multiple identical successors (common for
1168 // switches), only handle each succ once.
1169 if (!SuccsHandled.insert(SuccMBB)) continue;
1170
1171 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1172
1173 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1174 // nodes and Machine PHI nodes, but the incoming operands have not been
1175 // emitted yet.
1176 for (BasicBlock::const_iterator I = SuccBB->begin();
1177 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanfb95f892010-05-07 01:10:20 +00001178
Dan Gohmanf81eca02010-04-22 20:46:50 +00001179 // Ignore dead phi's.
1180 if (PN->use_empty()) continue;
1181
1182 // Only handle legal types. Two interesting things to note here. First,
1183 // by bailing out early, we may leave behind some dead instructions,
1184 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
1185 // own moves. Second, this check is necessary becuase FastISel doesn't
1186 // use CreateRegForValue to create registers, so it always creates
1187 // exactly one register for each non-void instruction.
1188 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1189 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
1190 // Promote MVT::i1.
1191 if (VT == MVT::i1)
1192 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1193 else {
1194 PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
1195 return false;
1196 }
1197 }
1198
1199 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1200
Dan Gohmanfb95f892010-05-07 01:10:20 +00001201 // Set the DebugLoc for the copy. Prefer the location of the operand
1202 // if there is one; use the location of the PHI otherwise.
1203 DL = PN->getDebugLoc();
1204 if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
1205 DL = Inst->getDebugLoc();
1206
Dan Gohmanf81eca02010-04-22 20:46:50 +00001207 unsigned Reg = getRegForValue(PHIOp);
1208 if (Reg == 0) {
1209 PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
1210 return false;
1211 }
1212 PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohmanfb95f892010-05-07 01:10:20 +00001213 DL = DebugLoc();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001214 }
1215 }
1216
1217 return true;
1218}