blob: 2d6b78840f5f93543f0ccb84c5413c64a389c930 [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 Gohman20d4be12010-07-01 02:58:57 +0000164 if (!SelectOperator(Op, Op->getOpcode()))
165 if (!isa<Instruction>(Op) ||
166 !TargetSelectInstruction(cast<Instruction>(Op)))
167 return 0;
Dan Gohman37db6cd2010-06-21 14:17:46 +0000168 Reg = lookUpRegForValue(Op);
Dan Gohman205d9252008-08-28 21:19:07 +0000169 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000170 Reg = createResultReg(TLI.getRegClassFor(VT));
Chris Lattner518bb532010-02-09 19:54:29 +0000171 BuildMI(MBB, DL, TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000172 }
Owen Andersond5d81a42008-09-03 17:51:57 +0000173
Dan Gohmandceffe62008-09-25 01:28:51 +0000174 // If target-independent code couldn't handle the value, give target-specific
175 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000176 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000177 Reg = TargetMaterializeConstant(cast<Constant>(V));
Owen Anderson6e607452008-09-05 23:36:01 +0000178
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000179 // Don't cache constant materializations in the general ValueMap.
180 // To do so would require tracking what uses they dominate.
Dan Gohmandceffe62008-09-25 01:28:51 +0000181 if (Reg != 0)
182 LocalValueMap[V] = Reg;
Dan Gohman104e4ce2008-09-03 23:32:19 +0000183 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000184}
185
Dan Gohman46510a72010-04-15 01:51:59 +0000186unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng59fbc802008-09-09 01:26:59 +0000187 // Look up the value to see if we already have a register for it. We
188 // cache values defined by Instructions across blocks, and other values
189 // only locally. This is because Instructions already have the SSA
Dan Gohman1fdc6142010-05-03 23:36:34 +0000190 // def-dominates-use requirement enforced.
Dan Gohman3193a682010-06-21 14:21:47 +0000191 DenseMap<const Value *, unsigned>::iterator I = ValueMap.find(V);
192 if (I != ValueMap.end())
193 return I->second;
Evan Cheng59fbc802008-09-09 01:26:59 +0000194 return LocalValueMap[V];
195}
196
Owen Andersoncc54e762008-08-30 00:38:46 +0000197/// UpdateValueMap - Update the value map to include the new mapping for this
198/// instruction, or insert an extra copy to get the result in a previous
199/// determined register.
200/// NOTE: This is only necessary because we might select a block that uses
201/// a value before we select the block that defines the value. It might be
202/// possible to fix this by selecting blocks in reverse postorder.
Dan Gohman46510a72010-04-15 01:51:59 +0000203unsigned FastISel::UpdateValueMap(const Value *I, unsigned Reg) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000204 if (!isa<Instruction>(I)) {
205 LocalValueMap[I] = Reg;
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000206 return Reg;
Dan Gohman40b189e2008-09-05 18:18:20 +0000207 }
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000208
209 unsigned &AssignedReg = ValueMap[I];
210 if (AssignedReg == 0)
211 AssignedReg = Reg;
Chris Lattner36e39462009-04-12 07:46:30 +0000212 else if (Reg != AssignedReg) {
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000213 const TargetRegisterClass *RegClass = MRI.getRegClass(Reg);
214 TII.copyRegToReg(*MBB, MBB->end(), AssignedReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000215 Reg, RegClass, RegClass, DL);
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000216 }
217 return AssignedReg;
Owen Andersoncc54e762008-08-30 00:38:46 +0000218}
219
Dan Gohmana6cb6412010-05-11 23:54:07 +0000220std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000221 unsigned IdxN = getRegForValue(Idx);
222 if (IdxN == 0)
223 // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000224 return std::pair<unsigned, bool>(0, false);
225
226 bool IdxNIsKill = hasTrivialKill(Idx);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000227
228 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Anderson766b5ef2009-08-11 21:59:30 +0000229 MVT PtrVT = TLI.getPointerTy();
Owen Andersone50ed302009-08-10 22:56:29 +0000230 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000231 if (IdxVT.bitsLT(PtrVT)) {
232 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND,
233 IdxN, IdxNIsKill);
234 IdxNIsKill = true;
235 }
236 else if (IdxVT.bitsGT(PtrVT)) {
237 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE,
238 IdxN, IdxNIsKill);
239 IdxNIsKill = true;
240 }
241 return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000242}
243
Dan Gohmanbdedd442008-08-20 00:11:48 +0000244/// SelectBinaryOp - Select and emit code for a binary operator instruction,
245/// which has an opcode which directly corresponds to the given ISD opcode.
246///
Dan Gohman46510a72010-04-15 01:51:59 +0000247bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000248 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmanbdedd442008-08-20 00:11:48 +0000250 // Unhandled type. Halt "fast" selection and bail.
251 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000252
Dan Gohmanb71fea22008-08-26 20:52:40 +0000253 // We only handle legal types. For example, on x86-32 the instruction
254 // selector contains all of the 64-bit instructions from x86-64,
255 // under the assumption that i64 won't be used if the target doesn't
256 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000257 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000259 // don't require additional zeroing, which makes them easy.
Owen Anderson825b72b2009-08-11 20:47:22 +0000260 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000261 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
262 ISDOpcode == ISD::XOR))
Owen Anderson23b9b192009-08-12 00:36:31 +0000263 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohman638c6832008-09-05 18:44:22 +0000264 else
265 return false;
266 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000267
Dan Gohman3df24e62008-09-03 23:12:08 +0000268 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000269 if (Op0 == 0)
270 // Unhandled operand. Halt "fast" selection and bail.
271 return false;
272
Dan Gohmana6cb6412010-05-11 23:54:07 +0000273 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
274
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000275 // Check if the second operand is a constant and handle it appropriately.
276 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000277 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000278 ISDOpcode, Op0, Op0IsKill,
279 CI->getZExtValue());
Dan Gohmanad368ac2008-08-27 18:10:19 +0000280 if (ResultReg != 0) {
281 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000282 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000283 return true;
284 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000285 }
286
Dan Gohman10df0fa2008-08-27 01:09:54 +0000287 // Check if the second operand is a constant float.
288 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000289 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000290 ISDOpcode, Op0, Op0IsKill, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000291 if (ResultReg != 0) {
292 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000293 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000294 return true;
295 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000296 }
297
Dan Gohman3df24e62008-09-03 23:12:08 +0000298 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000299 if (Op1 == 0)
300 // Unhandled operand. Halt "fast" selection and bail.
301 return false;
302
Dan Gohmana6cb6412010-05-11 23:54:07 +0000303 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
304
Dan Gohmanad368ac2008-08-27 18:10:19 +0000305 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000306 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000307 ISDOpcode,
308 Op0, Op0IsKill,
309 Op1, Op1IsKill);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000310 if (ResultReg == 0)
311 // Target-specific code wasn't able to find a machine opcode for
312 // the given ISD opcode and type. Halt "fast" selection and bail.
313 return false;
314
Dan Gohman8014e862008-08-20 00:23:20 +0000315 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000316 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000317 return true;
318}
319
Dan Gohman46510a72010-04-15 01:51:59 +0000320bool FastISel::SelectGetElementPtr(const User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000321 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000322 if (N == 0)
323 // Unhandled operand. Halt "fast" selection and bail.
324 return false;
325
Dan Gohmana6cb6412010-05-11 23:54:07 +0000326 bool NIsKill = hasTrivialKill(I->getOperand(0));
327
Evan Cheng83785c82008-08-20 22:45:34 +0000328 const Type *Ty = I->getOperand(0)->getType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000329 MVT VT = TLI.getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +0000330 for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
331 E = I->op_end(); OI != E; ++OI) {
332 const Value *Idx = *OI;
Evan Cheng83785c82008-08-20 22:45:34 +0000333 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
334 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
335 if (Field) {
336 // N = N + Offset
337 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
338 // FIXME: This can be optimized by combining the add with a
339 // subsequent one.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000340 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000341 if (N == 0)
342 // Unhandled operand. Halt "fast" selection and bail.
343 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000344 NIsKill = true;
Evan Cheng83785c82008-08-20 22:45:34 +0000345 }
346 Ty = StTy->getElementType(Field);
347 } else {
348 Ty = cast<SequentialType>(Ty)->getElementType();
349
350 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +0000351 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +0000352 if (CI->isZero()) continue;
Evan Cheng83785c82008-08-20 22:45:34 +0000353 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +0000354 TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohmana6cb6412010-05-11 23:54:07 +0000355 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000356 if (N == 0)
357 // Unhandled operand. Halt "fast" selection and bail.
358 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000359 NIsKill = true;
Evan Cheng83785c82008-08-20 22:45:34 +0000360 continue;
361 }
362
363 // N = N + Idx * ElementSize;
Duncan Sands777d2302009-05-09 07:06:46 +0000364 uint64_t ElementSize = TD.getTypeAllocSize(Ty);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000365 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
366 unsigned IdxN = Pair.first;
367 bool IdxNIsKill = Pair.second;
Evan Cheng83785c82008-08-20 22:45:34 +0000368 if (IdxN == 0)
369 // Unhandled operand. Halt "fast" selection and bail.
370 return false;
371
Dan Gohman80bc6e22008-08-26 20:57:08 +0000372 if (ElementSize != 1) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000373 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000374 if (IdxN == 0)
375 // Unhandled operand. Halt "fast" selection and bail.
376 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000377 IdxNIsKill = true;
Dan Gohman80bc6e22008-08-26 20:57:08 +0000378 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000379 N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
Evan Cheng83785c82008-08-20 22:45:34 +0000380 if (N == 0)
381 // Unhandled operand. Halt "fast" selection and bail.
382 return false;
383 }
384 }
385
386 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000387 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000388 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000389}
390
Dan Gohman46510a72010-04-15 01:51:59 +0000391bool FastISel::SelectCall(const User *I) {
392 const Function *F = cast<CallInst>(I)->getCalledFunction();
Dan Gohman33134c42008-09-25 17:05:24 +0000393 if (!F) return false;
394
Dan Gohman4183e312010-04-13 17:07:06 +0000395 // Handle selected intrinsic function calls.
Dan Gohman33134c42008-09-25 17:05:24 +0000396 unsigned IID = F->getIntrinsicID();
397 switch (IID) {
398 default: break;
Bill Wendling92c1e122009-02-13 02:16:35 +0000399 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +0000400 const DbgDeclareInst *DI = cast<DbgDeclareInst>(I);
Devang Patel02f0dbd2010-05-07 22:04:20 +0000401 if (!DIVariable(DI->getVariable()).Verify() ||
Chris Lattnered3a8062010-04-05 06:05:26 +0000402 !MF.getMMI().hasDebugInfo())
Devang Patel7e1e31f2009-07-02 22:43:26 +0000403 return true;
404
Dan Gohman46510a72010-04-15 01:51:59 +0000405 const Value *Address = DI->getAddress();
Dale Johannesendc918562010-02-06 02:26:02 +0000406 if (!Address)
407 return true;
Dale Johannesen343b42e2010-04-07 01:15:14 +0000408 if (isa<UndefValue>(Address))
409 return true;
Dan Gohman46510a72010-04-15 01:51:59 +0000410 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
Devang Patel7e1e31f2009-07-02 22:43:26 +0000411 // Don't handle byval struct arguments or VLAs, for example.
Dale Johannesen7dc78402010-04-25 21:03:54 +0000412 // Note that if we have a byval struct argument, fast ISel is turned off;
413 // those are handled in SelectionDAGBuilder.
Devang Patel54fc4d62010-04-28 19:27:33 +0000414 if (AI) {
415 DenseMap<const AllocaInst*, int>::iterator SI =
416 StaticAllocaMap.find(AI);
417 if (SI == StaticAllocaMap.end()) break; // VLAs.
418 int FI = SI->second;
419 if (!DI->getDebugLoc().isUnknown())
420 MF.getMMI().setVariableDbgInfo(DI->getVariable(), FI, DI->getDebugLoc());
421 } else
422 // Building the map above is target independent. Generating DBG_VALUE
423 // inline is target dependent; do this now.
424 (void)TargetSelectInstruction(cast<Instruction>(I));
Dan Gohman33134c42008-09-25 17:05:24 +0000425 return true;
Bill Wendling92c1e122009-02-13 02:16:35 +0000426 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000427 case Intrinsic::dbg_value: {
Dale Johannesen343b42e2010-04-07 01:15:14 +0000428 // This form of DBG_VALUE is target-independent.
Dan Gohman46510a72010-04-15 01:51:59 +0000429 const DbgValueInst *DI = cast<DbgValueInst>(I);
Dale Johannesen45df7612010-02-26 20:01:55 +0000430 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohman46510a72010-04-15 01:51:59 +0000431 const Value *V = DI->getValue();
Dale Johannesen45df7612010-02-26 20:01:55 +0000432 if (!V) {
433 // Currently the optimizer can produce this; insert an undef to
434 // help debugging. Probably the optimizer should not do this.
435 BuildMI(MBB, DL, II).addReg(0U).addImm(DI->getOffset()).
436 addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000437 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dale Johannesen45df7612010-02-26 20:01:55 +0000438 BuildMI(MBB, DL, II).addImm(CI->getZExtValue()).addImm(DI->getOffset()).
439 addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000440 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dale Johannesen45df7612010-02-26 20:01:55 +0000441 BuildMI(MBB, DL, II).addFPImm(CF).addImm(DI->getOffset()).
442 addMetadata(DI->getVariable());
443 } else if (unsigned Reg = lookUpRegForValue(V)) {
444 BuildMI(MBB, DL, II).addReg(Reg, RegState::Debug).addImm(DI->getOffset()).
445 addMetadata(DI->getVariable());
446 } else {
447 // We can't yet handle anything else here because it would require
448 // generating code, thus altering codegen because of debug info.
449 // Insert an undef so we can see what we dropped.
450 BuildMI(MBB, DL, II).addReg(0U).addImm(DI->getOffset()).
451 addMetadata(DI->getVariable());
452 }
453 return true;
454 }
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000455 case Intrinsic::eh_exception: {
Owen Andersone50ed302009-08-10 22:56:29 +0000456 EVT VT = TLI.getValueType(I->getType());
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000457 switch (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)) {
458 default: break;
459 case TargetLowering::Expand: {
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000460 assert(MBB->isLandingPad() && "Call to eh.exception not in landing pad!");
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000461 unsigned Reg = TLI.getExceptionAddressRegister();
462 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
463 unsigned ResultReg = createResultReg(RC);
464 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000465 Reg, RC, RC, DL);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000466 assert(InsertedCopy && "Can't copy address registers!");
Evan Cheng24ac4082008-11-24 07:09:49 +0000467 InsertedCopy = InsertedCopy;
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000468 UpdateValueMap(I, ResultReg);
469 return true;
470 }
471 }
472 break;
473 }
Duncan Sandsb01bbdc2009-10-14 16:11:37 +0000474 case Intrinsic::eh_selector: {
Owen Andersone50ed302009-08-10 22:56:29 +0000475 EVT VT = TLI.getValueType(I->getType());
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000476 switch (TLI.getOperationAction(ISD::EHSELECTION, VT)) {
477 default: break;
478 case TargetLowering::Expand: {
Chris Lattnered3a8062010-04-05 06:05:26 +0000479 if (MBB->isLandingPad())
480 AddCatchInfo(*cast<CallInst>(I), &MF.getMMI(), MBB);
481 else {
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000482#ifndef NDEBUG
Chris Lattnered3a8062010-04-05 06:05:26 +0000483 CatchInfoLost.insert(cast<CallInst>(I));
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000484#endif
Chris Lattnered3a8062010-04-05 06:05:26 +0000485 // FIXME: Mark exception selector register as live in. Hack for PR1508.
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000486 unsigned Reg = TLI.getExceptionSelectorRegister();
Chris Lattnered3a8062010-04-05 06:05:26 +0000487 if (Reg) MBB->addLiveIn(Reg);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000488 }
Chris Lattnered3a8062010-04-05 06:05:26 +0000489
490 unsigned Reg = TLI.getExceptionSelectorRegister();
491 EVT SrcVT = TLI.getPointerTy();
492 const TargetRegisterClass *RC = TLI.getRegClassFor(SrcVT);
493 unsigned ResultReg = createResultReg(RC);
494 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, Reg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000495 RC, RC, DL);
Chris Lattnered3a8062010-04-05 06:05:26 +0000496 assert(InsertedCopy && "Can't copy address registers!");
497 InsertedCopy = InsertedCopy;
498
Dan Gohmana6cb6412010-05-11 23:54:07 +0000499 bool ResultRegIsKill = hasTrivialKill(I);
500
Chris Lattnered3a8062010-04-05 06:05:26 +0000501 // Cast the register to the type of the selector.
502 if (SrcVT.bitsGT(MVT::i32))
503 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, ISD::TRUNCATE,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000504 ResultReg, ResultRegIsKill);
Chris Lattnered3a8062010-04-05 06:05:26 +0000505 else if (SrcVT.bitsLT(MVT::i32))
506 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000507 ISD::SIGN_EXTEND, ResultReg, ResultRegIsKill);
Chris Lattnered3a8062010-04-05 06:05:26 +0000508 if (ResultReg == 0)
509 // Unhandled operand. Halt "fast" selection and bail.
510 return false;
511
512 UpdateValueMap(I, ResultReg);
513
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000514 return true;
515 }
516 }
517 break;
518 }
Dan Gohman33134c42008-09-25 17:05:24 +0000519 }
Dan Gohman4183e312010-04-13 17:07:06 +0000520
521 // An arbitrary call. Bail.
Dan Gohman33134c42008-09-25 17:05:24 +0000522 return false;
523}
524
Dan Gohman46510a72010-04-15 01:51:59 +0000525bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000526 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
527 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000528
Owen Anderson825b72b2009-08-11 20:47:22 +0000529 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
530 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersond0533c92008-08-26 23:46:32 +0000531 // Unhandled type. Halt "fast" selection and bail.
532 return false;
533
Dan Gohman474d3b32009-03-13 23:53:06 +0000534 // Check if the destination type is legal. Or as a special case,
535 // it may be i1 if we're doing a truncate because that's
536 // easy and somewhat common.
537 if (!TLI.isTypeLegal(DstVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 if (DstVT != MVT::i1 || Opcode != ISD::TRUNCATE)
Dan Gohman91b6f972008-10-03 01:28:47 +0000539 // Unhandled type. Halt "fast" selection and bail.
540 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000541
542 // Check if the source operand is legal. Or as a special case,
543 // it may be i1 if we're doing zero-extension because that's
544 // easy and somewhat common.
545 if (!TLI.isTypeLegal(SrcVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000546 if (SrcVT != MVT::i1 || Opcode != ISD::ZERO_EXTEND)
Dan Gohman474d3b32009-03-13 23:53:06 +0000547 // Unhandled type. Halt "fast" selection and bail.
548 return false;
549
Dan Gohman3df24e62008-09-03 23:12:08 +0000550 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000551 if (!InputReg)
552 // Unhandled operand. Halt "fast" selection and bail.
553 return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000554
Dan Gohmana6cb6412010-05-11 23:54:07 +0000555 bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
556
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000557 // If the operand is i1, arrange for the high bits in the register to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +0000558 if (SrcVT == MVT::i1) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000559 SrcVT = TLI.getTypeToTransformTo(I->getContext(), SrcVT);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000560 InputReg = FastEmitZExtFromI1(SrcVT.getSimpleVT(), InputReg, InputRegIsKill);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000561 if (!InputReg)
562 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000563 InputRegIsKill = true;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000564 }
Dan Gohman474d3b32009-03-13 23:53:06 +0000565 // If the result is i1, truncate to the target's type for i1 first.
Owen Anderson825b72b2009-08-11 20:47:22 +0000566 if (DstVT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +0000567 DstVT = TLI.getTypeToTransformTo(I->getContext(), DstVT);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000568
Owen Andersond0533c92008-08-26 23:46:32 +0000569 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
570 DstVT.getSimpleVT(),
571 Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000572 InputReg, InputRegIsKill);
Owen Andersond0533c92008-08-26 23:46:32 +0000573 if (!ResultReg)
574 return false;
575
Dan Gohman3df24e62008-09-03 23:12:08 +0000576 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000577 return true;
578}
579
Dan Gohman46510a72010-04-15 01:51:59 +0000580bool FastISel::SelectBitCast(const User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000581 // If the bitcast doesn't change the type, just use the operand value.
582 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000583 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000584 if (Reg == 0)
585 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000586 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000587 return true;
588 }
589
590 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Andersone50ed302009-08-10 22:56:29 +0000591 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
592 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000593
Owen Anderson825b72b2009-08-11 20:47:22 +0000594 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
595 DstVT == MVT::Other || !DstVT.isSimple() ||
Owen Andersond0533c92008-08-26 23:46:32 +0000596 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
597 // Unhandled type. Halt "fast" selection and bail.
598 return false;
599
Dan Gohman3df24e62008-09-03 23:12:08 +0000600 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000601 if (Op0 == 0)
602 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000603 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000604
605 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000606
Dan Gohmanad368ac2008-08-27 18:10:19 +0000607 // First, try to perform the bitcast by inserting a reg-reg copy.
608 unsigned ResultReg = 0;
609 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
610 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
611 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
612 ResultReg = createResultReg(DstClass);
613
614 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000615 Op0, DstClass, SrcClass, DL);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000616 if (!InsertedCopy)
617 ResultReg = 0;
618 }
619
620 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
621 if (!ResultReg)
622 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000623 ISD::BIT_CONVERT, Op0, Op0IsKill);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000624
625 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000626 return false;
627
Dan Gohman3df24e62008-09-03 23:12:08 +0000628 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000629 return true;
630}
631
Dan Gohman3df24e62008-09-03 23:12:08 +0000632bool
Dan Gohman46510a72010-04-15 01:51:59 +0000633FastISel::SelectInstruction(const Instruction *I) {
Dan Gohmane8c92dd2010-04-23 15:29:50 +0000634 // Just before the terminator instruction, insert instructions to
635 // feed PHI nodes in successor blocks.
636 if (isa<TerminatorInst>(I))
637 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
638 return false;
639
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000640 DL = I->getDebugLoc();
641
Dan Gohman6e3ff372009-12-05 01:27:58 +0000642 // First, try doing target-independent selection.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000643 if (SelectOperator(I, I->getOpcode())) {
644 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000645 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000646 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000647
648 // Next, try calling the target to attempt to handle the instruction.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000649 if (TargetSelectInstruction(I)) {
650 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000651 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000652 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000653
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000654 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000655 return false;
Dan Gohman40b189e2008-09-05 18:18:20 +0000656}
657
Dan Gohmand98d6202008-10-02 22:15:21 +0000658/// FastEmitBranch - Emit an unconditional branch to the given block,
659/// unless it is the immediate (fall-through) successor, and update
660/// the CFG.
661void
Stuart Hastings3bf91252010-06-17 22:43:56 +0000662FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DL) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000663 if (MBB->isLayoutSuccessor(MSucc)) {
664 // The unconditional fall-through case, which needs no instructions.
665 } else {
666 // The unconditional branch case.
Stuart Hastings3bf91252010-06-17 22:43:56 +0000667 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>(), DL);
Dan Gohmand98d6202008-10-02 22:15:21 +0000668 }
669 MBB->addSuccessor(MSucc);
670}
671
Dan Gohman3d45a852009-09-03 22:53:57 +0000672/// SelectFNeg - Emit an FNeg operation.
673///
674bool
Dan Gohman46510a72010-04-15 01:51:59 +0000675FastISel::SelectFNeg(const User *I) {
Dan Gohman3d45a852009-09-03 22:53:57 +0000676 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
677 if (OpReg == 0) return false;
678
Dan Gohmana6cb6412010-05-11 23:54:07 +0000679 bool OpRegIsKill = hasTrivialKill(I);
680
Dan Gohman4a215a12009-09-11 00:36:43 +0000681 // If the target has ISD::FNEG, use it.
682 EVT VT = TLI.getValueType(I->getType());
683 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000684 ISD::FNEG, OpReg, OpRegIsKill);
Dan Gohman4a215a12009-09-11 00:36:43 +0000685 if (ResultReg != 0) {
686 UpdateValueMap(I, ResultReg);
687 return true;
688 }
689
Dan Gohman5e5abb72009-09-11 00:34:46 +0000690 // Bitcast the value to integer, twiddle the sign bit with xor,
691 // and then bitcast it back to floating-point.
Dan Gohman3d45a852009-09-03 22:53:57 +0000692 if (VT.getSizeInBits() > 64) return false;
Dan Gohman5e5abb72009-09-11 00:34:46 +0000693 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
694 if (!TLI.isTypeLegal(IntVT))
695 return false;
696
697 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000698 ISD::BIT_CONVERT, OpReg, OpRegIsKill);
Dan Gohman5e5abb72009-09-11 00:34:46 +0000699 if (IntReg == 0)
700 return false;
701
Dan Gohmana6cb6412010-05-11 23:54:07 +0000702 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
703 IntReg, /*Kill=*/true,
Dan Gohman5e5abb72009-09-11 00:34:46 +0000704 UINT64_C(1) << (VT.getSizeInBits()-1),
705 IntVT.getSimpleVT());
706 if (IntResultReg == 0)
707 return false;
708
709 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000710 ISD::BIT_CONVERT, IntResultReg, /*Kill=*/true);
Dan Gohman3d45a852009-09-03 22:53:57 +0000711 if (ResultReg == 0)
712 return false;
713
714 UpdateValueMap(I, ResultReg);
715 return true;
716}
717
Dan Gohman40b189e2008-09-05 18:18:20 +0000718bool
Dan Gohman46510a72010-04-15 01:51:59 +0000719FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000720 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000721 case Instruction::Add:
722 return SelectBinaryOp(I, ISD::ADD);
723 case Instruction::FAdd:
724 return SelectBinaryOp(I, ISD::FADD);
725 case Instruction::Sub:
726 return SelectBinaryOp(I, ISD::SUB);
727 case Instruction::FSub:
Dan Gohman3d45a852009-09-03 22:53:57 +0000728 // FNeg is currently represented in LLVM IR as a special case of FSub.
729 if (BinaryOperator::isFNeg(I))
730 return SelectFNeg(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000731 return SelectBinaryOp(I, ISD::FSUB);
732 case Instruction::Mul:
733 return SelectBinaryOp(I, ISD::MUL);
734 case Instruction::FMul:
735 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman3df24e62008-09-03 23:12:08 +0000736 case Instruction::SDiv:
737 return SelectBinaryOp(I, ISD::SDIV);
738 case Instruction::UDiv:
739 return SelectBinaryOp(I, ISD::UDIV);
740 case Instruction::FDiv:
741 return SelectBinaryOp(I, ISD::FDIV);
742 case Instruction::SRem:
743 return SelectBinaryOp(I, ISD::SREM);
744 case Instruction::URem:
745 return SelectBinaryOp(I, ISD::UREM);
746 case Instruction::FRem:
747 return SelectBinaryOp(I, ISD::FREM);
748 case Instruction::Shl:
749 return SelectBinaryOp(I, ISD::SHL);
750 case Instruction::LShr:
751 return SelectBinaryOp(I, ISD::SRL);
752 case Instruction::AShr:
753 return SelectBinaryOp(I, ISD::SRA);
754 case Instruction::And:
755 return SelectBinaryOp(I, ISD::AND);
756 case Instruction::Or:
757 return SelectBinaryOp(I, ISD::OR);
758 case Instruction::Xor:
759 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000760
Dan Gohman3df24e62008-09-03 23:12:08 +0000761 case Instruction::GetElementPtr:
762 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000763
Dan Gohman3df24e62008-09-03 23:12:08 +0000764 case Instruction::Br: {
Dan Gohman46510a72010-04-15 01:51:59 +0000765 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000766
Dan Gohman3df24e62008-09-03 23:12:08 +0000767 if (BI->isUnconditional()) {
Dan Gohman46510a72010-04-15 01:51:59 +0000768 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohman3df24e62008-09-03 23:12:08 +0000769 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
Stuart Hastings3bf91252010-06-17 22:43:56 +0000770 FastEmitBranch(MSucc, BI->getDebugLoc());
Dan Gohman3df24e62008-09-03 23:12:08 +0000771 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000772 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000773
774 // Conditional branches are not handed yet.
775 // Halt "fast" selection and bail.
776 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000777 }
778
Dan Gohman087c8502008-09-05 01:08:41 +0000779 case Instruction::Unreachable:
780 // Nothing to emit.
781 return true;
782
Dan Gohman0586d912008-09-10 20:11:02 +0000783 case Instruction::Alloca:
784 // FunctionLowering has the static-sized case covered.
785 if (StaticAllocaMap.count(cast<AllocaInst>(I)))
786 return true;
787
788 // Dynamic-sized alloca is not handled yet.
789 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000790
Dan Gohman33134c42008-09-25 17:05:24 +0000791 case Instruction::Call:
792 return SelectCall(I);
793
Dan Gohman3df24e62008-09-03 23:12:08 +0000794 case Instruction::BitCast:
795 return SelectBitCast(I);
796
797 case Instruction::FPToSI:
798 return SelectCast(I, ISD::FP_TO_SINT);
799 case Instruction::ZExt:
800 return SelectCast(I, ISD::ZERO_EXTEND);
801 case Instruction::SExt:
802 return SelectCast(I, ISD::SIGN_EXTEND);
803 case Instruction::Trunc:
804 return SelectCast(I, ISD::TRUNCATE);
805 case Instruction::SIToFP:
806 return SelectCast(I, ISD::SINT_TO_FP);
807
808 case Instruction::IntToPtr: // Deliberate fall-through.
809 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +0000810 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
811 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman3df24e62008-09-03 23:12:08 +0000812 if (DstVT.bitsGT(SrcVT))
813 return SelectCast(I, ISD::ZERO_EXTEND);
814 if (DstVT.bitsLT(SrcVT))
815 return SelectCast(I, ISD::TRUNCATE);
816 unsigned Reg = getRegForValue(I->getOperand(0));
817 if (Reg == 0) return false;
818 UpdateValueMap(I, Reg);
819 return true;
820 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000821
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000822 case Instruction::PHI:
823 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
824
Dan Gohman3df24e62008-09-03 23:12:08 +0000825 default:
826 // Unhandled instruction. Halt "fast" selection and bail.
827 return false;
828 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000829}
830
Dan Gohman3df24e62008-09-03 23:12:08 +0000831FastISel::FastISel(MachineFunction &mf,
832 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +0000833 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmanf81eca02010-04-22 20:46:50 +0000834 DenseMap<const AllocaInst *, int> &am,
835 std::vector<std::pair<MachineInstr*, unsigned> > &pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000836#ifndef NDEBUG
Dan Gohman25208642010-04-14 19:53:31 +0000837 , SmallSet<const Instruction *, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000838#endif
839 )
Dan Gohman3df24e62008-09-03 23:12:08 +0000840 : MBB(0),
841 ValueMap(vm),
842 MBBMap(bm),
Dan Gohman0586d912008-09-10 20:11:02 +0000843 StaticAllocaMap(am),
Dan Gohmanf81eca02010-04-22 20:46:50 +0000844 PHINodesToUpdate(pn),
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000845#ifndef NDEBUG
846 CatchInfoLost(cil),
847#endif
Dan Gohman3df24e62008-09-03 23:12:08 +0000848 MF(mf),
849 MRI(MF.getRegInfo()),
Dan Gohman0586d912008-09-10 20:11:02 +0000850 MFI(*MF.getFrameInfo()),
851 MCP(*MF.getConstantPool()),
Dan Gohman3df24e62008-09-03 23:12:08 +0000852 TM(MF.getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000853 TD(*TM.getTargetData()),
854 TII(*TM.getInstrInfo()),
Dan Gohmana7a0ed72010-05-05 23:58:35 +0000855 TLI(*TM.getTargetLowering()),
Dan Gohmandb497122010-06-18 23:28:01 +0000856 TRI(*TM.getRegisterInfo()),
Dan Gohmana7a0ed72010-05-05 23:58:35 +0000857 IsBottomUp(false) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000858}
859
Dan Gohmane285a742008-08-14 21:51:29 +0000860FastISel::~FastISel() {}
861
Owen Anderson825b72b2009-08-11 20:47:22 +0000862unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000863 unsigned) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000864 return 0;
865}
866
Owen Anderson825b72b2009-08-11 20:47:22 +0000867unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000868 unsigned,
869 unsigned /*Op0*/, bool /*Op0IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000870 return 0;
871}
872
Owen Anderson825b72b2009-08-11 20:47:22 +0000873unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000874 unsigned,
875 unsigned /*Op0*/, bool /*Op0IsKill*/,
876 unsigned /*Op1*/, bool /*Op1IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000877 return 0;
878}
879
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000880unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000881 return 0;
882}
883
Owen Anderson825b72b2009-08-11 20:47:22 +0000884unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohman46510a72010-04-15 01:51:59 +0000885 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000886 return 0;
887}
888
Owen Anderson825b72b2009-08-11 20:47:22 +0000889unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000890 unsigned,
891 unsigned /*Op0*/, bool /*Op0IsKill*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000892 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000893 return 0;
894}
895
Owen Anderson825b72b2009-08-11 20:47:22 +0000896unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000897 unsigned,
898 unsigned /*Op0*/, bool /*Op0IsKill*/,
Dan Gohman46510a72010-04-15 01:51:59 +0000899 const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000900 return 0;
901}
902
Owen Anderson825b72b2009-08-11 20:47:22 +0000903unsigned FastISel::FastEmit_rri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000904 unsigned,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000905 unsigned /*Op0*/, bool /*Op0IsKill*/,
906 unsigned /*Op1*/, bool /*Op1IsKill*/,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000907 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000908 return 0;
909}
910
911/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
912/// to emit an instruction with an immediate operand using FastEmit_ri.
913/// If that fails, it materializes the immediate into a register and try
914/// FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000915unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000916 unsigned Op0, bool Op0IsKill,
917 uint64_t Imm, MVT ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000918 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000919 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000920 if (ResultReg != 0)
921 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000922 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000923 if (MaterialReg == 0)
924 return 0;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000925 return FastEmit_rr(VT, VT, Opcode,
926 Op0, Op0IsKill,
927 MaterialReg, /*Kill=*/true);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000928}
929
Dan Gohman10df0fa2008-08-27 01:09:54 +0000930/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
931/// to emit an instruction with a floating-point immediate operand using
932/// FastEmit_rf. If that fails, it materializes the immediate into a register
933/// and try FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000934unsigned FastISel::FastEmit_rf_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000935 unsigned Op0, bool Op0IsKill,
936 const ConstantFP *FPImm, MVT ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000937 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000938 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, Op0IsKill, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000939 if (ResultReg != 0)
940 return ResultReg;
941
942 // Materialize the constant in a register.
943 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
944 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000945 // If the target doesn't have a way to directly enter a floating-point
946 // value into a register, use an alternate approach.
947 // TODO: The current approach only supports floating-point constants
948 // that can be constructed by conversion from integer values. This should
949 // be replaced by code that creates a load from a constant-pool entry,
950 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000951 const APFloat &Flt = FPImm->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000952 EVT IntVT = TLI.getPointerTy();
Dan Gohman10df0fa2008-08-27 01:09:54 +0000953
954 uint64_t x[2];
955 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000956 bool isExact;
957 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
958 APFloat::rmTowardZero, &isExact);
959 if (!isExact)
Dan Gohman10df0fa2008-08-27 01:09:54 +0000960 return 0;
961 APInt IntVal(IntBitWidth, 2, x);
962
963 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
964 ISD::Constant, IntVal.getZExtValue());
965 if (IntegerReg == 0)
966 return 0;
967 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000968 ISD::SINT_TO_FP, IntegerReg, /*Kill=*/true);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000969 if (MaterialReg == 0)
970 return 0;
971 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000972 return FastEmit_rr(VT, VT, Opcode,
973 Op0, Op0IsKill,
974 MaterialReg, /*Kill=*/true);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000975}
976
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000977unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
978 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000979}
980
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000981unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000982 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000983 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000984 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000985
Bill Wendling9bc96a52009-02-03 00:55:04 +0000986 BuildMI(MBB, DL, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000987 return ResultReg;
988}
989
990unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
991 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000992 unsigned Op0, bool Op0IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000993 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000994 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000995
Evan Cheng5960e4e2008-09-08 08:38:20 +0000996 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +0000997 BuildMI(MBB, DL, II, ResultReg).addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000998 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000999 BuildMI(MBB, DL, II).addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001000 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001001 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001002 if (!InsertedCopy)
1003 ResultReg = 0;
1004 }
1005
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001006 return ResultReg;
1007}
1008
1009unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
1010 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001011 unsigned Op0, bool Op0IsKill,
1012 unsigned Op1, bool Op1IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001013 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +00001014 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001015
Evan Cheng5960e4e2008-09-08 08:38:20 +00001016 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001017 BuildMI(MBB, DL, II, ResultReg)
1018 .addReg(Op0, Op0IsKill * RegState::Kill)
1019 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001020 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001021 BuildMI(MBB, DL, II)
1022 .addReg(Op0, Op0IsKill * RegState::Kill)
1023 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001024 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001025 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001026 if (!InsertedCopy)
1027 ResultReg = 0;
1028 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001029 return ResultReg;
1030}
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001031
1032unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1033 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001034 unsigned Op0, bool Op0IsKill,
1035 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001036 unsigned ResultReg = createResultReg(RC);
1037 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1038
Evan Cheng5960e4e2008-09-08 08:38:20 +00001039 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001040 BuildMI(MBB, DL, II, ResultReg)
1041 .addReg(Op0, Op0IsKill * RegState::Kill)
1042 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001043 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001044 BuildMI(MBB, DL, II)
1045 .addReg(Op0, Op0IsKill * RegState::Kill)
1046 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001047 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001048 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001049 if (!InsertedCopy)
1050 ResultReg = 0;
1051 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001052 return ResultReg;
1053}
1054
Dan Gohman10df0fa2008-08-27 01:09:54 +00001055unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
1056 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001057 unsigned Op0, bool Op0IsKill,
1058 const ConstantFP *FPImm) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001059 unsigned ResultReg = createResultReg(RC);
1060 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1061
Evan Cheng5960e4e2008-09-08 08:38:20 +00001062 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001063 BuildMI(MBB, DL, II, ResultReg)
1064 .addReg(Op0, Op0IsKill * RegState::Kill)
1065 .addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001066 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001067 BuildMI(MBB, DL, II)
1068 .addReg(Op0, Op0IsKill * RegState::Kill)
1069 .addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001070 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001071 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001072 if (!InsertedCopy)
1073 ResultReg = 0;
1074 }
Dan Gohman10df0fa2008-08-27 01:09:54 +00001075 return ResultReg;
1076}
1077
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001078unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
1079 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001080 unsigned Op0, bool Op0IsKill,
1081 unsigned Op1, bool Op1IsKill,
1082 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001083 unsigned ResultReg = createResultReg(RC);
1084 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1085
Evan Cheng5960e4e2008-09-08 08:38:20 +00001086 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001087 BuildMI(MBB, DL, II, ResultReg)
1088 .addReg(Op0, Op0IsKill * RegState::Kill)
1089 .addReg(Op1, Op1IsKill * RegState::Kill)
1090 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001091 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001092 BuildMI(MBB, DL, II)
1093 .addReg(Op0, Op0IsKill * RegState::Kill)
1094 .addReg(Op1, Op1IsKill * RegState::Kill)
1095 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001096 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001097 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001098 if (!InsertedCopy)
1099 ResultReg = 0;
1100 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001101 return ResultReg;
1102}
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001103
1104unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1105 const TargetRegisterClass *RC,
1106 uint64_t Imm) {
1107 unsigned ResultReg = createResultReg(RC);
1108 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1109
Evan Cheng5960e4e2008-09-08 08:38:20 +00001110 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +00001111 BuildMI(MBB, DL, II, ResultReg).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001112 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +00001113 BuildMI(MBB, DL, II).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001114 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001115 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001116 if (!InsertedCopy)
1117 ResultReg = 0;
1118 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001119 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +00001120}
Owen Anderson8970f002008-08-27 22:30:02 +00001121
Owen Anderson825b72b2009-08-11 20:47:22 +00001122unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001123 unsigned Op0, bool Op0IsKill,
1124 uint32_t Idx) {
Owen Anderson40a468f2008-08-28 17:47:37 +00001125 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +00001126
Evan Cheng536ab132009-01-22 09:10:11 +00001127 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Chris Lattner518bb532010-02-09 19:54:29 +00001128 const TargetInstrDesc &II = TII.get(TargetOpcode::EXTRACT_SUBREG);
Owen Anderson8970f002008-08-27 22:30:02 +00001129
Evan Cheng5960e4e2008-09-08 08:38:20 +00001130 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001131 BuildMI(MBB, DL, II, ResultReg)
1132 .addReg(Op0, Op0IsKill * RegState::Kill)
1133 .addImm(Idx);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001134 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001135 BuildMI(MBB, DL, II)
1136 .addReg(Op0, Op0IsKill * RegState::Kill)
1137 .addImm(Idx);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001138 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001139 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001140 if (!InsertedCopy)
1141 ResultReg = 0;
1142 }
Owen Anderson8970f002008-08-27 22:30:02 +00001143 return ResultReg;
1144}
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001145
1146/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1147/// with all but the least significant bit set to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001148unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1149 return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001150}
Dan Gohmanf81eca02010-04-22 20:46:50 +00001151
1152/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1153/// Emit code to ensure constants are copied into registers when needed.
1154/// Remember the virtual registers that need to be added to the Machine PHI
1155/// nodes as input. We cannot just directly add them, because expansion
1156/// might result in multiple MBB's for one BB. As such, the start of the
1157/// BB might correspond to a different MBB than the end.
1158bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1159 const TerminatorInst *TI = LLVMBB->getTerminator();
1160
1161 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
1162 unsigned OrigNumPHINodesToUpdate = PHINodesToUpdate.size();
1163
1164 // Check successor nodes' PHI nodes that expect a constant to be available
1165 // from this block.
1166 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1167 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1168 if (!isa<PHINode>(SuccBB->begin())) continue;
1169 MachineBasicBlock *SuccMBB = MBBMap[SuccBB];
1170
1171 // If this terminator has multiple identical successors (common for
1172 // switches), only handle each succ once.
1173 if (!SuccsHandled.insert(SuccMBB)) continue;
1174
1175 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1176
1177 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1178 // nodes and Machine PHI nodes, but the incoming operands have not been
1179 // emitted yet.
1180 for (BasicBlock::const_iterator I = SuccBB->begin();
1181 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanfb95f892010-05-07 01:10:20 +00001182
Dan Gohmanf81eca02010-04-22 20:46:50 +00001183 // Ignore dead phi's.
1184 if (PN->use_empty()) continue;
1185
1186 // Only handle legal types. Two interesting things to note here. First,
1187 // by bailing out early, we may leave behind some dead instructions,
1188 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
1189 // own moves. Second, this check is necessary becuase FastISel doesn't
1190 // use CreateRegForValue to create registers, so it always creates
1191 // exactly one register for each non-void instruction.
1192 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1193 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
1194 // Promote MVT::i1.
1195 if (VT == MVT::i1)
1196 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1197 else {
1198 PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
1199 return false;
1200 }
1201 }
1202
1203 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1204
Dan Gohmanfb95f892010-05-07 01:10:20 +00001205 // Set the DebugLoc for the copy. Prefer the location of the operand
1206 // if there is one; use the location of the PHI otherwise.
1207 DL = PN->getDebugLoc();
1208 if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
1209 DL = Inst->getDebugLoc();
1210
Dan Gohmanf81eca02010-04-22 20:46:50 +00001211 unsigned Reg = getRegForValue(PHIOp);
1212 if (Reg == 0) {
1213 PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
1214 return false;
1215 }
1216 PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohmanfb95f892010-05-07 01:10:20 +00001217 DL = DebugLoc();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001218 }
1219 }
1220
1221 return true;
1222}