blob: 2ba8315a7e66287dc1165e1155858e51669d211c [file] [log] [blame]
Dan Gohman3b172f12010-04-22 20:06:42 +00001//===-- FastISel.cpp - Implementation of the FastISel class ---------------===//
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the implementation of the FastISel class.
11//
Dan Gohman5ec9efd2008-09-30 20:48:29 +000012// "Fast" instruction selection is designed to emit very poor code quickly.
13// Also, it is not designed to be able to do much lowering, so most illegal
Chris Lattner44d2a982008-10-13 01:59:13 +000014// types (e.g. i64 on 32-bit targets) and operations are not supported. It is
15// also not intended to be able to do much optimization, except in a few cases
16// where doing optimizations reduces overall compile time. For example, folding
17// constants into immediate fields is often done, because it's cheap and it
18// reduces the number of instructions later phases have to examine.
Dan Gohman5ec9efd2008-09-30 20:48:29 +000019//
20// "Fast" instruction selection is able to fail gracefully and transfer
21// control to the SelectionDAG selector for operations that it doesn't
Chris Lattner44d2a982008-10-13 01:59:13 +000022// support. In many cases, this allows us to avoid duplicating a lot of
Dan Gohman5ec9efd2008-09-30 20:48:29 +000023// the complicated lowering logic that SelectionDAG currently has.
24//
25// The intended use for "fast" instruction selection is "-O0" mode
26// compilation, where the quality of the generated code is irrelevant when
Chris Lattner44d2a982008-10-13 01:59:13 +000027// weighed against the speed at which the code can be generated. Also,
Dan Gohman5ec9efd2008-09-30 20:48:29 +000028// at -O0, the LLVM optimizers are not running, and this makes the
29// compile time of codegen a much higher portion of the overall compile
Chris Lattner44d2a982008-10-13 01:59:13 +000030// time. Despite its limitations, "fast" instruction selection is able to
Dan Gohman5ec9efd2008-09-30 20:48:29 +000031// handle enough code on its own to provide noticeable overall speedups
32// in -O0 compiles.
33//
34// Basic operations are supported in a target-independent way, by reading
35// the same instruction descriptions that the SelectionDAG selector reads,
36// and identifying simple arithmetic operations that can be directly selected
Chris Lattner44d2a982008-10-13 01:59:13 +000037// from simple operators. More complicated operations currently require
Dan Gohman5ec9efd2008-09-30 20:48:29 +000038// target-specific code.
39//
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000040//===----------------------------------------------------------------------===//
41
Dan Gohman33134c42008-09-25 17:05:24 +000042#include "llvm/Function.h"
43#include "llvm/GlobalVariable.h"
Dan Gohman6f2766d2008-08-19 22:31:46 +000044#include "llvm/Instructions.h"
Dan Gohman33134c42008-09-25 17:05:24 +000045#include "llvm/IntrinsicInst.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000046#include "llvm/CodeGen/FastISel.h"
Dan Gohman4c3fd9f2010-07-07 16:01:37 +000047#include "llvm/CodeGen/FunctionLoweringInfo.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000048#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman33134c42008-09-25 17:05:24 +000049#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000050#include "llvm/CodeGen/MachineRegisterInfo.h"
Devang Patel83489bb2009-01-13 00:35:13 +000051#include "llvm/Analysis/DebugInfo.h"
Dan Gohman7fbcc982010-07-01 03:49:38 +000052#include "llvm/Analysis/Loads.h"
Evan Cheng83785c82008-08-20 22:45:34 +000053#include "llvm/Target/TargetData.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000054#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000055#include "llvm/Target/TargetLowering.h"
Dan Gohmanbb466332008-08-20 21:05:57 +000056#include "llvm/Target/TargetMachine.h"
Dan Gohmanba5be5c2010-04-20 15:00:41 +000057#include "llvm/Support/ErrorHandling.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000058using namespace llvm;
59
Dan Gohmana6cb6412010-05-11 23:54:07 +000060bool FastISel::hasTrivialKill(const Value *V) const {
Dan Gohman7f0d6952010-05-14 22:53:18 +000061 // Don't consider constants or arguments to have trivial kills.
Dan Gohmana6cb6412010-05-11 23:54:07 +000062 const Instruction *I = dyn_cast<Instruction>(V);
Dan Gohman7f0d6952010-05-14 22:53:18 +000063 if (!I)
64 return false;
65
66 // No-op casts are trivially coalesced by fast-isel.
67 if (const CastInst *Cast = dyn_cast<CastInst>(I))
68 if (Cast->isNoopCast(TD.getIntPtrType(Cast->getContext())) &&
69 !hasTrivialKill(Cast->getOperand(0)))
70 return false;
71
72 // Only instructions with a single use in the same basic block are considered
73 // to have trivial kills.
74 return I->hasOneUse() &&
75 !(I->getOpcode() == Instruction::BitCast ||
76 I->getOpcode() == Instruction::PtrToInt ||
77 I->getOpcode() == Instruction::IntToPtr) &&
Dan Gohmane1308d82010-05-13 19:19:32 +000078 cast<Instruction>(I->use_begin())->getParent() == I->getParent();
Dan Gohmana6cb6412010-05-11 23:54:07 +000079}
80
Dan Gohman46510a72010-04-15 01:51:59 +000081unsigned FastISel::getRegForValue(const Value *V) {
Owen Andersone50ed302009-08-10 22:56:29 +000082 EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
Dan Gohman4fd55282009-04-07 20:40:11 +000083 // Don't handle non-simple values in FastISel.
84 if (!RealVT.isSimple())
85 return 0;
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +000086
87 // Ignore illegal types. We must do this before looking up the value
88 // in ValueMap because Arguments are given virtual registers regardless
89 // of whether FastISel can handle them.
Owen Anderson825b72b2009-08-11 20:47:22 +000090 MVT VT = RealVT.getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +000091 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +000092 // Promote MVT::i1 to a legal type though, because it's common and easy.
93 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +000094 VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +000095 else
96 return 0;
97 }
98
Dan Gohman104e4ce2008-09-03 23:32:19 +000099 // Look up the value to see if we already have a register for it. We
100 // cache values defined by Instructions across blocks, and other values
101 // only locally. This is because Instructions already have the SSA
Dan Gohman5c9cf192010-01-12 04:30:26 +0000102 // def-dominates-use requirement enforced.
Dan Gohmana4160c32010-07-07 16:29:44 +0000103 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
104 if (I != FuncInfo.ValueMap.end())
Dan Gohmaneddc1142010-05-25 21:59:42 +0000105 return I->second;
Dan Gohman104e4ce2008-09-03 23:32:19 +0000106 unsigned Reg = LocalValueMap[V];
107 if (Reg != 0)
108 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000109
Dan Gohman97c94b82010-05-06 00:02:14 +0000110 // In bottom-up mode, just create the virtual register which will be used
111 // to hold the value. It will be materialized later.
112 if (IsBottomUp) {
113 Reg = createResultReg(TLI.getRegClassFor(VT));
114 if (isa<Instruction>(V))
Dan Gohmana4160c32010-07-07 16:29:44 +0000115 FuncInfo.ValueMap[V] = Reg;
Dan Gohman97c94b82010-05-06 00:02:14 +0000116 else
117 LocalValueMap[V] = Reg;
118 return Reg;
119 }
120
Dan Gohman1fdc6142010-05-03 23:36:34 +0000121 return materializeRegForValue(V, VT);
122}
123
124/// materializeRegForValue - Helper for getRegForVale. This function is
125/// called when the value isn't already available in a register and must
126/// be materialized with new instructions.
127unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
128 unsigned Reg = 0;
129
Dan Gohman46510a72010-04-15 01:51:59 +0000130 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000131 if (CI->getValue().getActiveBits() <= 64)
132 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +0000133 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000134 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +0000135 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman1e9e8c32008-10-07 22:03:27 +0000136 // Translate this as an integer zero so that it can be
137 // local-CSE'd with actual integer zeros.
Owen Anderson1d0be152009-08-13 21:58:54 +0000138 Reg =
139 getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
Dan Gohman46510a72010-04-15 01:51:59 +0000140 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman4183e312010-04-13 17:07:06 +0000141 // Try to emit the constant directly.
Dan Gohman104e4ce2008-09-03 23:32:19 +0000142 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000143
144 if (!Reg) {
Dan Gohman4183e312010-04-13 17:07:06 +0000145 // Try to emit the constant by using an integer constant with a cast.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000146 const APFloat &Flt = CF->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000147 EVT IntVT = TLI.getPointerTy();
Dan Gohmanad368ac2008-08-27 18:10:19 +0000148
149 uint64_t x[2];
150 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000151 bool isExact;
152 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
153 APFloat::rmTowardZero, &isExact);
154 if (isExact) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000155 APInt IntVal(IntBitWidth, 2, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000156
Owen Andersone922c022009-07-22 00:24:57 +0000157 unsigned IntegerReg =
Owen Andersoneed707b2009-07-24 23:12:02 +0000158 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000159 if (IntegerReg != 0)
Dan Gohmana6cb6412010-05-11 23:54:07 +0000160 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
161 IntegerReg, /*Kill=*/false);
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000162 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000163 }
Dan Gohman46510a72010-04-15 01:51:59 +0000164 } else if (const Operator *Op = dyn_cast<Operator>(V)) {
Dan Gohman20d4be12010-07-01 02:58:57 +0000165 if (!SelectOperator(Op, Op->getOpcode()))
166 if (!isa<Instruction>(Op) ||
167 !TargetSelectInstruction(cast<Instruction>(Op)))
168 return 0;
Dan Gohman37db6cd2010-06-21 14:17:46 +0000169 Reg = lookUpRegForValue(Op);
Dan Gohman205d9252008-08-28 21:19:07 +0000170 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000171 Reg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohmaneabaed22010-07-07 16:47:08 +0000172 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
173 TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000174 }
Owen Andersond5d81a42008-09-03 17:51:57 +0000175
Dan Gohmandceffe62008-09-25 01:28:51 +0000176 // If target-independent code couldn't handle the value, give target-specific
177 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000178 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000179 Reg = TargetMaterializeConstant(cast<Constant>(V));
Owen Anderson6e607452008-09-05 23:36:01 +0000180
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000181 // Don't cache constant materializations in the general ValueMap.
182 // To do so would require tracking what uses they dominate.
Dan Gohmandceffe62008-09-25 01:28:51 +0000183 if (Reg != 0)
184 LocalValueMap[V] = Reg;
Dan Gohman104e4ce2008-09-03 23:32:19 +0000185 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000186}
187
Dan Gohman46510a72010-04-15 01:51:59 +0000188unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng59fbc802008-09-09 01:26:59 +0000189 // Look up the value to see if we already have a register for it. We
190 // cache values defined by Instructions across blocks, and other values
191 // only locally. This is because Instructions already have the SSA
Dan Gohman1fdc6142010-05-03 23:36:34 +0000192 // def-dominates-use requirement enforced.
Dan Gohmana4160c32010-07-07 16:29:44 +0000193 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
194 if (I != FuncInfo.ValueMap.end())
Dan Gohman3193a682010-06-21 14:21:47 +0000195 return I->second;
Evan Cheng59fbc802008-09-09 01:26:59 +0000196 return LocalValueMap[V];
197}
198
Owen Andersoncc54e762008-08-30 00:38:46 +0000199/// UpdateValueMap - Update the value map to include the new mapping for this
200/// instruction, or insert an extra copy to get the result in a previous
201/// determined register.
202/// NOTE: This is only necessary because we might select a block that uses
203/// a value before we select the block that defines the value. It might be
204/// possible to fix this by selecting blocks in reverse postorder.
Dan Gohman46510a72010-04-15 01:51:59 +0000205unsigned FastISel::UpdateValueMap(const Value *I, unsigned Reg) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000206 if (!isa<Instruction>(I)) {
207 LocalValueMap[I] = Reg;
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000208 return Reg;
Dan Gohman40b189e2008-09-05 18:18:20 +0000209 }
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000210
Dan Gohmana4160c32010-07-07 16:29:44 +0000211 unsigned &AssignedReg = FuncInfo.ValueMap[I];
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000212 if (AssignedReg == 0)
213 AssignedReg = Reg;
Chris Lattner36e39462009-04-12 07:46:30 +0000214 else if (Reg != AssignedReg) {
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000215 const TargetRegisterClass *RegClass = MRI.getRegClass(Reg);
Dan Gohmaneabaed22010-07-07 16:47:08 +0000216 TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt, AssignedReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000217 Reg, RegClass, RegClass, DL);
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000218 }
219 return AssignedReg;
Owen Andersoncc54e762008-08-30 00:38:46 +0000220}
221
Dan Gohmana6cb6412010-05-11 23:54:07 +0000222std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000223 unsigned IdxN = getRegForValue(Idx);
224 if (IdxN == 0)
225 // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000226 return std::pair<unsigned, bool>(0, false);
227
228 bool IdxNIsKill = hasTrivialKill(Idx);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000229
230 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Anderson766b5ef2009-08-11 21:59:30 +0000231 MVT PtrVT = TLI.getPointerTy();
Owen Andersone50ed302009-08-10 22:56:29 +0000232 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000233 if (IdxVT.bitsLT(PtrVT)) {
234 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND,
235 IdxN, IdxNIsKill);
236 IdxNIsKill = true;
237 }
238 else if (IdxVT.bitsGT(PtrVT)) {
239 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE,
240 IdxN, IdxNIsKill);
241 IdxNIsKill = true;
242 }
243 return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000244}
245
Dan Gohmanbdedd442008-08-20 00:11:48 +0000246/// SelectBinaryOp - Select and emit code for a binary operator instruction,
247/// which has an opcode which directly corresponds to the given ISD opcode.
248///
Dan Gohman46510a72010-04-15 01:51:59 +0000249bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000250 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000251 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmanbdedd442008-08-20 00:11:48 +0000252 // Unhandled type. Halt "fast" selection and bail.
253 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000254
Dan Gohmanb71fea22008-08-26 20:52:40 +0000255 // We only handle legal types. For example, on x86-32 the instruction
256 // selector contains all of the 64-bit instructions from x86-64,
257 // under the assumption that i64 won't be used if the target doesn't
258 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000259 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000260 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000261 // don't require additional zeroing, which makes them easy.
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000263 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
264 ISDOpcode == ISD::XOR))
Owen Anderson23b9b192009-08-12 00:36:31 +0000265 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohman638c6832008-09-05 18:44:22 +0000266 else
267 return false;
268 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000269
Dan Gohman3df24e62008-09-03 23:12:08 +0000270 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000271 if (Op0 == 0)
272 // Unhandled operand. Halt "fast" selection and bail.
273 return false;
274
Dan Gohmana6cb6412010-05-11 23:54:07 +0000275 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
276
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000277 // Check if the second operand is a constant and handle it appropriately.
278 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000279 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000280 ISDOpcode, Op0, Op0IsKill,
281 CI->getZExtValue());
Dan Gohmanad368ac2008-08-27 18:10:19 +0000282 if (ResultReg != 0) {
283 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000284 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000285 return true;
286 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000287 }
288
Dan Gohman10df0fa2008-08-27 01:09:54 +0000289 // Check if the second operand is a constant float.
290 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000291 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000292 ISDOpcode, Op0, Op0IsKill, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000293 if (ResultReg != 0) {
294 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000295 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000296 return true;
297 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000298 }
299
Dan Gohman3df24e62008-09-03 23:12:08 +0000300 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000301 if (Op1 == 0)
302 // Unhandled operand. Halt "fast" selection and bail.
303 return false;
304
Dan Gohmana6cb6412010-05-11 23:54:07 +0000305 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
306
Dan Gohmanad368ac2008-08-27 18:10:19 +0000307 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000308 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000309 ISDOpcode,
310 Op0, Op0IsKill,
311 Op1, Op1IsKill);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000312 if (ResultReg == 0)
313 // Target-specific code wasn't able to find a machine opcode for
314 // the given ISD opcode and type. Halt "fast" selection and bail.
315 return false;
316
Dan Gohman8014e862008-08-20 00:23:20 +0000317 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000318 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000319 return true;
320}
321
Dan Gohman46510a72010-04-15 01:51:59 +0000322bool FastISel::SelectGetElementPtr(const User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000323 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000324 if (N == 0)
325 // Unhandled operand. Halt "fast" selection and bail.
326 return false;
327
Dan Gohmana6cb6412010-05-11 23:54:07 +0000328 bool NIsKill = hasTrivialKill(I->getOperand(0));
329
Evan Cheng83785c82008-08-20 22:45:34 +0000330 const Type *Ty = I->getOperand(0)->getType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000331 MVT VT = TLI.getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +0000332 for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
333 E = I->op_end(); OI != E; ++OI) {
334 const Value *Idx = *OI;
Evan Cheng83785c82008-08-20 22:45:34 +0000335 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
336 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
337 if (Field) {
338 // N = N + Offset
339 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
340 // FIXME: This can be optimized by combining the add with a
341 // subsequent one.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000342 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000343 if (N == 0)
344 // Unhandled operand. Halt "fast" selection and bail.
345 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000346 NIsKill = true;
Evan Cheng83785c82008-08-20 22:45:34 +0000347 }
348 Ty = StTy->getElementType(Field);
349 } else {
350 Ty = cast<SequentialType>(Ty)->getElementType();
351
352 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +0000353 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +0000354 if (CI->isZero()) continue;
Evan Cheng83785c82008-08-20 22:45:34 +0000355 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +0000356 TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohmana6cb6412010-05-11 23:54:07 +0000357 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000358 if (N == 0)
359 // Unhandled operand. Halt "fast" selection and bail.
360 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000361 NIsKill = true;
Evan Cheng83785c82008-08-20 22:45:34 +0000362 continue;
363 }
364
365 // N = N + Idx * ElementSize;
Duncan Sands777d2302009-05-09 07:06:46 +0000366 uint64_t ElementSize = TD.getTypeAllocSize(Ty);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000367 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
368 unsigned IdxN = Pair.first;
369 bool IdxNIsKill = Pair.second;
Evan Cheng83785c82008-08-20 22:45:34 +0000370 if (IdxN == 0)
371 // Unhandled operand. Halt "fast" selection and bail.
372 return false;
373
Dan Gohman80bc6e22008-08-26 20:57:08 +0000374 if (ElementSize != 1) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000375 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000376 if (IdxN == 0)
377 // Unhandled operand. Halt "fast" selection and bail.
378 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000379 IdxNIsKill = true;
Dan Gohman80bc6e22008-08-26 20:57:08 +0000380 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000381 N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
Evan Cheng83785c82008-08-20 22:45:34 +0000382 if (N == 0)
383 // Unhandled operand. Halt "fast" selection and bail.
384 return false;
385 }
386 }
387
388 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000389 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000390 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000391}
392
Dan Gohman46510a72010-04-15 01:51:59 +0000393bool FastISel::SelectCall(const User *I) {
394 const Function *F = cast<CallInst>(I)->getCalledFunction();
Dan Gohman33134c42008-09-25 17:05:24 +0000395 if (!F) return false;
396
Dan Gohman4183e312010-04-13 17:07:06 +0000397 // Handle selected intrinsic function calls.
Dan Gohman33134c42008-09-25 17:05:24 +0000398 unsigned IID = F->getIntrinsicID();
399 switch (IID) {
400 default: break;
Bill Wendling92c1e122009-02-13 02:16:35 +0000401 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +0000402 const DbgDeclareInst *DI = cast<DbgDeclareInst>(I);
Devang Patel02f0dbd2010-05-07 22:04:20 +0000403 if (!DIVariable(DI->getVariable()).Verify() ||
Dan Gohmana4160c32010-07-07 16:29:44 +0000404 !FuncInfo.MF->getMMI().hasDebugInfo())
Devang Patel7e1e31f2009-07-02 22:43:26 +0000405 return true;
406
Dan Gohman46510a72010-04-15 01:51:59 +0000407 const Value *Address = DI->getAddress();
Dale Johannesendc918562010-02-06 02:26:02 +0000408 if (!Address)
409 return true;
Dale Johannesen343b42e2010-04-07 01:15:14 +0000410 if (isa<UndefValue>(Address))
411 return true;
Dan Gohman46510a72010-04-15 01:51:59 +0000412 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
Devang Patel7e1e31f2009-07-02 22:43:26 +0000413 // Don't handle byval struct arguments or VLAs, for example.
Dale Johannesen7dc78402010-04-25 21:03:54 +0000414 // Note that if we have a byval struct argument, fast ISel is turned off;
415 // those are handled in SelectionDAGBuilder.
Devang Patel54fc4d62010-04-28 19:27:33 +0000416 if (AI) {
417 DenseMap<const AllocaInst*, int>::iterator SI =
Dan Gohmana4160c32010-07-07 16:29:44 +0000418 FuncInfo.StaticAllocaMap.find(AI);
419 if (SI == FuncInfo.StaticAllocaMap.end()) break; // VLAs.
Devang Patel54fc4d62010-04-28 19:27:33 +0000420 int FI = SI->second;
421 if (!DI->getDebugLoc().isUnknown())
Dan Gohmana4160c32010-07-07 16:29:44 +0000422 FuncInfo.MF->getMMI().setVariableDbgInfo(DI->getVariable(),
423 FI, DI->getDebugLoc());
Devang Patel54fc4d62010-04-28 19:27:33 +0000424 } else
425 // Building the map above is target independent. Generating DBG_VALUE
426 // inline is target dependent; do this now.
427 (void)TargetSelectInstruction(cast<Instruction>(I));
Dan Gohman33134c42008-09-25 17:05:24 +0000428 return true;
Bill Wendling92c1e122009-02-13 02:16:35 +0000429 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000430 case Intrinsic::dbg_value: {
Dale Johannesen343b42e2010-04-07 01:15:14 +0000431 // This form of DBG_VALUE is target-independent.
Dan Gohman46510a72010-04-15 01:51:59 +0000432 const DbgValueInst *DI = cast<DbgValueInst>(I);
Dale Johannesen45df7612010-02-26 20:01:55 +0000433 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohman46510a72010-04-15 01:51:59 +0000434 const Value *V = DI->getValue();
Dale Johannesen45df7612010-02-26 20:01:55 +0000435 if (!V) {
436 // Currently the optimizer can produce this; insert an undef to
437 // help debugging. Probably the optimizer should not do this.
Dan Gohmaneabaed22010-07-07 16:47:08 +0000438 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
439 .addReg(0U).addImm(DI->getOffset())
440 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000441 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohmaneabaed22010-07-07 16:47:08 +0000442 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
443 .addImm(CI->getZExtValue()).addImm(DI->getOffset())
444 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000445 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohmaneabaed22010-07-07 16:47:08 +0000446 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
447 .addFPImm(CF).addImm(DI->getOffset())
448 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000449 } else if (unsigned Reg = lookUpRegForValue(V)) {
Dan Gohmaneabaed22010-07-07 16:47:08 +0000450 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
451 .addReg(Reg, RegState::Debug).addImm(DI->getOffset())
452 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000453 } else {
454 // We can't yet handle anything else here because it would require
455 // generating code, thus altering codegen because of debug info.
456 // Insert an undef so we can see what we dropped.
Dan Gohmaneabaed22010-07-07 16:47:08 +0000457 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
458 .addReg(0U).addImm(DI->getOffset())
459 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000460 }
461 return true;
462 }
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000463 case Intrinsic::eh_exception: {
Owen Andersone50ed302009-08-10 22:56:29 +0000464 EVT VT = TLI.getValueType(I->getType());
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000465 switch (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)) {
466 default: break;
467 case TargetLowering::Expand: {
Dan Gohmaneabaed22010-07-07 16:47:08 +0000468 assert(FuncInfo.MBB->isLandingPad() &&
469 "Call to eh.exception not in landing pad!");
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000470 unsigned Reg = TLI.getExceptionAddressRegister();
471 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
472 unsigned ResultReg = createResultReg(RC);
Dan Gohmaneabaed22010-07-07 16:47:08 +0000473 bool InsertedCopy = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
474 ResultReg, Reg, RC, RC, DL);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000475 assert(InsertedCopy && "Can't copy address registers!");
Evan Cheng24ac4082008-11-24 07:09:49 +0000476 InsertedCopy = InsertedCopy;
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000477 UpdateValueMap(I, ResultReg);
478 return true;
479 }
480 }
481 break;
482 }
Duncan Sandsb01bbdc2009-10-14 16:11:37 +0000483 case Intrinsic::eh_selector: {
Owen Andersone50ed302009-08-10 22:56:29 +0000484 EVT VT = TLI.getValueType(I->getType());
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000485 switch (TLI.getOperationAction(ISD::EHSELECTION, VT)) {
486 default: break;
487 case TargetLowering::Expand: {
Dan Gohmaneabaed22010-07-07 16:47:08 +0000488 if (FuncInfo.MBB->isLandingPad())
489 AddCatchInfo(*cast<CallInst>(I), &FuncInfo.MF->getMMI(), FuncInfo.MBB);
Chris Lattnered3a8062010-04-05 06:05:26 +0000490 else {
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000491#ifndef NDEBUG
Dan Gohmana4160c32010-07-07 16:29:44 +0000492 FuncInfo.CatchInfoLost.insert(cast<CallInst>(I));
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000493#endif
Chris Lattnered3a8062010-04-05 06:05:26 +0000494 // FIXME: Mark exception selector register as live in. Hack for PR1508.
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000495 unsigned Reg = TLI.getExceptionSelectorRegister();
Dan Gohmaneabaed22010-07-07 16:47:08 +0000496 if (Reg) FuncInfo.MBB->addLiveIn(Reg);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000497 }
Chris Lattnered3a8062010-04-05 06:05:26 +0000498
499 unsigned Reg = TLI.getExceptionSelectorRegister();
500 EVT SrcVT = TLI.getPointerTy();
501 const TargetRegisterClass *RC = TLI.getRegClassFor(SrcVT);
502 unsigned ResultReg = createResultReg(RC);
Dan Gohmaneabaed22010-07-07 16:47:08 +0000503 bool InsertedCopy = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
504 ResultReg, Reg, RC, RC, DL);
Chris Lattnered3a8062010-04-05 06:05:26 +0000505 assert(InsertedCopy && "Can't copy address registers!");
506 InsertedCopy = InsertedCopy;
507
Dan Gohmana6cb6412010-05-11 23:54:07 +0000508 bool ResultRegIsKill = hasTrivialKill(I);
509
Chris Lattnered3a8062010-04-05 06:05:26 +0000510 // Cast the register to the type of the selector.
511 if (SrcVT.bitsGT(MVT::i32))
512 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, ISD::TRUNCATE,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000513 ResultReg, ResultRegIsKill);
Chris Lattnered3a8062010-04-05 06:05:26 +0000514 else if (SrcVT.bitsLT(MVT::i32))
515 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000516 ISD::SIGN_EXTEND, ResultReg, ResultRegIsKill);
Chris Lattnered3a8062010-04-05 06:05:26 +0000517 if (ResultReg == 0)
518 // Unhandled operand. Halt "fast" selection and bail.
519 return false;
520
521 UpdateValueMap(I, ResultReg);
522
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000523 return true;
524 }
525 }
526 break;
527 }
Dan Gohman33134c42008-09-25 17:05:24 +0000528 }
Dan Gohman4183e312010-04-13 17:07:06 +0000529
530 // An arbitrary call. Bail.
Dan Gohman33134c42008-09-25 17:05:24 +0000531 return false;
532}
533
Dan Gohman46510a72010-04-15 01:51:59 +0000534bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000535 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
536 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000537
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
539 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersond0533c92008-08-26 23:46:32 +0000540 // Unhandled type. Halt "fast" selection and bail.
541 return false;
542
Dan Gohman474d3b32009-03-13 23:53:06 +0000543 // Check if the destination type is legal. Or as a special case,
544 // it may be i1 if we're doing a truncate because that's
545 // easy and somewhat common.
546 if (!TLI.isTypeLegal(DstVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000547 if (DstVT != MVT::i1 || Opcode != ISD::TRUNCATE)
Dan Gohman91b6f972008-10-03 01:28:47 +0000548 // Unhandled type. Halt "fast" selection and bail.
549 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000550
551 // Check if the source operand is legal. Or as a special case,
552 // it may be i1 if we're doing zero-extension because that's
553 // easy and somewhat common.
554 if (!TLI.isTypeLegal(SrcVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000555 if (SrcVT != MVT::i1 || Opcode != ISD::ZERO_EXTEND)
Dan Gohman474d3b32009-03-13 23:53:06 +0000556 // Unhandled type. Halt "fast" selection and bail.
557 return false;
558
Dan Gohman3df24e62008-09-03 23:12:08 +0000559 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000560 if (!InputReg)
561 // Unhandled operand. Halt "fast" selection and bail.
562 return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000563
Dan Gohmana6cb6412010-05-11 23:54:07 +0000564 bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
565
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000566 // If the operand is i1, arrange for the high bits in the register to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +0000567 if (SrcVT == MVT::i1) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000568 SrcVT = TLI.getTypeToTransformTo(I->getContext(), SrcVT);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000569 InputReg = FastEmitZExtFromI1(SrcVT.getSimpleVT(), InputReg, InputRegIsKill);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000570 if (!InputReg)
571 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000572 InputRegIsKill = true;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000573 }
Dan Gohman474d3b32009-03-13 23:53:06 +0000574 // If the result is i1, truncate to the target's type for i1 first.
Owen Anderson825b72b2009-08-11 20:47:22 +0000575 if (DstVT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +0000576 DstVT = TLI.getTypeToTransformTo(I->getContext(), DstVT);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000577
Owen Andersond0533c92008-08-26 23:46:32 +0000578 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
579 DstVT.getSimpleVT(),
580 Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000581 InputReg, InputRegIsKill);
Owen Andersond0533c92008-08-26 23:46:32 +0000582 if (!ResultReg)
583 return false;
584
Dan Gohman3df24e62008-09-03 23:12:08 +0000585 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000586 return true;
587}
588
Dan Gohman46510a72010-04-15 01:51:59 +0000589bool FastISel::SelectBitCast(const User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000590 // If the bitcast doesn't change the type, just use the operand value.
591 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000592 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000593 if (Reg == 0)
594 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000595 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000596 return true;
597 }
598
599 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Andersone50ed302009-08-10 22:56:29 +0000600 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
601 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000602
Owen Anderson825b72b2009-08-11 20:47:22 +0000603 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
604 DstVT == MVT::Other || !DstVT.isSimple() ||
Owen Andersond0533c92008-08-26 23:46:32 +0000605 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
606 // Unhandled type. Halt "fast" selection and bail.
607 return false;
608
Dan Gohman3df24e62008-09-03 23:12:08 +0000609 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000610 if (Op0 == 0)
611 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000612 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000613
614 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000615
Dan Gohmanad368ac2008-08-27 18:10:19 +0000616 // First, try to perform the bitcast by inserting a reg-reg copy.
617 unsigned ResultReg = 0;
618 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
619 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
620 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
621 ResultReg = createResultReg(DstClass);
622
Dan Gohmaneabaed22010-07-07 16:47:08 +0000623 bool InsertedCopy = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
624 ResultReg, Op0,
625 DstClass, SrcClass, DL);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000626 if (!InsertedCopy)
627 ResultReg = 0;
628 }
629
630 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
631 if (!ResultReg)
632 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000633 ISD::BIT_CONVERT, Op0, Op0IsKill);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000634
635 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000636 return false;
637
Dan Gohman3df24e62008-09-03 23:12:08 +0000638 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000639 return true;
640}
641
Dan Gohman3df24e62008-09-03 23:12:08 +0000642bool
Dan Gohman46510a72010-04-15 01:51:59 +0000643FastISel::SelectInstruction(const Instruction *I) {
Dan Gohmane8c92dd2010-04-23 15:29:50 +0000644 // Just before the terminator instruction, insert instructions to
645 // feed PHI nodes in successor blocks.
646 if (isa<TerminatorInst>(I))
647 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
648 return false;
649
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000650 DL = I->getDebugLoc();
651
Dan Gohman6e3ff372009-12-05 01:27:58 +0000652 // First, try doing target-independent selection.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000653 if (SelectOperator(I, I->getOpcode())) {
654 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000655 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000656 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000657
658 // Next, try calling the target to attempt to handle the instruction.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000659 if (TargetSelectInstruction(I)) {
660 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000661 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000662 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000663
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000664 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000665 return false;
Dan Gohman40b189e2008-09-05 18:18:20 +0000666}
667
Dan Gohmand98d6202008-10-02 22:15:21 +0000668/// FastEmitBranch - Emit an unconditional branch to the given block,
669/// unless it is the immediate (fall-through) successor, and update
670/// the CFG.
671void
Stuart Hastings3bf91252010-06-17 22:43:56 +0000672FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DL) {
Dan Gohmaneabaed22010-07-07 16:47:08 +0000673 if (FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000674 // The unconditional fall-through case, which needs no instructions.
675 } else {
676 // The unconditional branch case.
Dan Gohmaneabaed22010-07-07 16:47:08 +0000677 TII.InsertBranch(*FuncInfo.MBB, MSucc, NULL,
678 SmallVector<MachineOperand, 0>(), DL);
Dan Gohmand98d6202008-10-02 22:15:21 +0000679 }
Dan Gohmaneabaed22010-07-07 16:47:08 +0000680 FuncInfo.MBB->addSuccessor(MSucc);
Dan Gohmand98d6202008-10-02 22:15:21 +0000681}
682
Dan Gohman3d45a852009-09-03 22:53:57 +0000683/// SelectFNeg - Emit an FNeg operation.
684///
685bool
Dan Gohman46510a72010-04-15 01:51:59 +0000686FastISel::SelectFNeg(const User *I) {
Dan Gohman3d45a852009-09-03 22:53:57 +0000687 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
688 if (OpReg == 0) return false;
689
Dan Gohmana6cb6412010-05-11 23:54:07 +0000690 bool OpRegIsKill = hasTrivialKill(I);
691
Dan Gohman4a215a12009-09-11 00:36:43 +0000692 // If the target has ISD::FNEG, use it.
693 EVT VT = TLI.getValueType(I->getType());
694 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000695 ISD::FNEG, OpReg, OpRegIsKill);
Dan Gohman4a215a12009-09-11 00:36:43 +0000696 if (ResultReg != 0) {
697 UpdateValueMap(I, ResultReg);
698 return true;
699 }
700
Dan Gohman5e5abb72009-09-11 00:34:46 +0000701 // Bitcast the value to integer, twiddle the sign bit with xor,
702 // and then bitcast it back to floating-point.
Dan Gohman3d45a852009-09-03 22:53:57 +0000703 if (VT.getSizeInBits() > 64) return false;
Dan Gohman5e5abb72009-09-11 00:34:46 +0000704 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
705 if (!TLI.isTypeLegal(IntVT))
706 return false;
707
708 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000709 ISD::BIT_CONVERT, OpReg, OpRegIsKill);
Dan Gohman5e5abb72009-09-11 00:34:46 +0000710 if (IntReg == 0)
711 return false;
712
Dan Gohmana6cb6412010-05-11 23:54:07 +0000713 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
714 IntReg, /*Kill=*/true,
Dan Gohman5e5abb72009-09-11 00:34:46 +0000715 UINT64_C(1) << (VT.getSizeInBits()-1),
716 IntVT.getSimpleVT());
717 if (IntResultReg == 0)
718 return false;
719
720 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000721 ISD::BIT_CONVERT, IntResultReg, /*Kill=*/true);
Dan Gohman3d45a852009-09-03 22:53:57 +0000722 if (ResultReg == 0)
723 return false;
724
725 UpdateValueMap(I, ResultReg);
726 return true;
727}
728
Dan Gohman40b189e2008-09-05 18:18:20 +0000729bool
Dan Gohman7fbcc982010-07-01 03:49:38 +0000730FastISel::SelectLoad(const User *I) {
731 LoadInst *LI = const_cast<LoadInst *>(cast<LoadInst>(I));
732
733 // For a load from an alloca, make a limited effort to find the value
734 // already available in a register, avoiding redundant loads.
735 if (!LI->isVolatile() && isa<AllocaInst>(LI->getPointerOperand())) {
736 BasicBlock::iterator ScanFrom = LI;
737 if (const Value *V = FindAvailableLoadedValue(LI->getPointerOperand(),
738 LI->getParent(), ScanFrom)) {
739 unsigned ResultReg = getRegForValue(V);
740 if (ResultReg != 0) {
741 UpdateValueMap(I, ResultReg);
742 return true;
743 }
744 }
745 }
746
747 return false;
748}
749
750bool
Dan Gohman46510a72010-04-15 01:51:59 +0000751FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000752 switch (Opcode) {
Dan Gohman7fbcc982010-07-01 03:49:38 +0000753 case Instruction::Load:
754 return SelectLoad(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000755 case Instruction::Add:
756 return SelectBinaryOp(I, ISD::ADD);
757 case Instruction::FAdd:
758 return SelectBinaryOp(I, ISD::FADD);
759 case Instruction::Sub:
760 return SelectBinaryOp(I, ISD::SUB);
761 case Instruction::FSub:
Dan Gohman3d45a852009-09-03 22:53:57 +0000762 // FNeg is currently represented in LLVM IR as a special case of FSub.
763 if (BinaryOperator::isFNeg(I))
764 return SelectFNeg(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000765 return SelectBinaryOp(I, ISD::FSUB);
766 case Instruction::Mul:
767 return SelectBinaryOp(I, ISD::MUL);
768 case Instruction::FMul:
769 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman3df24e62008-09-03 23:12:08 +0000770 case Instruction::SDiv:
771 return SelectBinaryOp(I, ISD::SDIV);
772 case Instruction::UDiv:
773 return SelectBinaryOp(I, ISD::UDIV);
774 case Instruction::FDiv:
775 return SelectBinaryOp(I, ISD::FDIV);
776 case Instruction::SRem:
777 return SelectBinaryOp(I, ISD::SREM);
778 case Instruction::URem:
779 return SelectBinaryOp(I, ISD::UREM);
780 case Instruction::FRem:
781 return SelectBinaryOp(I, ISD::FREM);
782 case Instruction::Shl:
783 return SelectBinaryOp(I, ISD::SHL);
784 case Instruction::LShr:
785 return SelectBinaryOp(I, ISD::SRL);
786 case Instruction::AShr:
787 return SelectBinaryOp(I, ISD::SRA);
788 case Instruction::And:
789 return SelectBinaryOp(I, ISD::AND);
790 case Instruction::Or:
791 return SelectBinaryOp(I, ISD::OR);
792 case Instruction::Xor:
793 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000794
Dan Gohman3df24e62008-09-03 23:12:08 +0000795 case Instruction::GetElementPtr:
796 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000797
Dan Gohman3df24e62008-09-03 23:12:08 +0000798 case Instruction::Br: {
Dan Gohman46510a72010-04-15 01:51:59 +0000799 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000800
Dan Gohman3df24e62008-09-03 23:12:08 +0000801 if (BI->isUnconditional()) {
Dan Gohman46510a72010-04-15 01:51:59 +0000802 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohmana4160c32010-07-07 16:29:44 +0000803 MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
Stuart Hastings3bf91252010-06-17 22:43:56 +0000804 FastEmitBranch(MSucc, BI->getDebugLoc());
Dan Gohman3df24e62008-09-03 23:12:08 +0000805 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000806 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000807
808 // Conditional branches are not handed yet.
809 // Halt "fast" selection and bail.
810 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000811 }
812
Dan Gohman087c8502008-09-05 01:08:41 +0000813 case Instruction::Unreachable:
814 // Nothing to emit.
815 return true;
816
Dan Gohman0586d912008-09-10 20:11:02 +0000817 case Instruction::Alloca:
818 // FunctionLowering has the static-sized case covered.
Dan Gohmana4160c32010-07-07 16:29:44 +0000819 if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
Dan Gohman0586d912008-09-10 20:11:02 +0000820 return true;
821
822 // Dynamic-sized alloca is not handled yet.
823 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000824
Dan Gohman33134c42008-09-25 17:05:24 +0000825 case Instruction::Call:
826 return SelectCall(I);
827
Dan Gohman3df24e62008-09-03 23:12:08 +0000828 case Instruction::BitCast:
829 return SelectBitCast(I);
830
831 case Instruction::FPToSI:
832 return SelectCast(I, ISD::FP_TO_SINT);
833 case Instruction::ZExt:
834 return SelectCast(I, ISD::ZERO_EXTEND);
835 case Instruction::SExt:
836 return SelectCast(I, ISD::SIGN_EXTEND);
837 case Instruction::Trunc:
838 return SelectCast(I, ISD::TRUNCATE);
839 case Instruction::SIToFP:
840 return SelectCast(I, ISD::SINT_TO_FP);
841
842 case Instruction::IntToPtr: // Deliberate fall-through.
843 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +0000844 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
845 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman3df24e62008-09-03 23:12:08 +0000846 if (DstVT.bitsGT(SrcVT))
847 return SelectCast(I, ISD::ZERO_EXTEND);
848 if (DstVT.bitsLT(SrcVT))
849 return SelectCast(I, ISD::TRUNCATE);
850 unsigned Reg = getRegForValue(I->getOperand(0));
851 if (Reg == 0) return false;
852 UpdateValueMap(I, Reg);
853 return true;
854 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000855
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000856 case Instruction::PHI:
857 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
858
Dan Gohman3df24e62008-09-03 23:12:08 +0000859 default:
860 // Unhandled instruction. Halt "fast" selection and bail.
861 return false;
862 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000863}
864
Dan Gohmana4160c32010-07-07 16:29:44 +0000865FastISel::FastISel(FunctionLoweringInfo &funcInfo)
Dan Gohmaneabaed22010-07-07 16:47:08 +0000866 : FuncInfo(funcInfo),
Dan Gohmana4160c32010-07-07 16:29:44 +0000867 MRI(FuncInfo.MF->getRegInfo()),
868 MFI(*FuncInfo.MF->getFrameInfo()),
869 MCP(*FuncInfo.MF->getConstantPool()),
870 TM(FuncInfo.MF->getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000871 TD(*TM.getTargetData()),
872 TII(*TM.getInstrInfo()),
Dan Gohmana7a0ed72010-05-05 23:58:35 +0000873 TLI(*TM.getTargetLowering()),
Dan Gohmandb497122010-06-18 23:28:01 +0000874 TRI(*TM.getRegisterInfo()),
Dan Gohmana7a0ed72010-05-05 23:58:35 +0000875 IsBottomUp(false) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000876}
877
Dan Gohmane285a742008-08-14 21:51:29 +0000878FastISel::~FastISel() {}
879
Owen Anderson825b72b2009-08-11 20:47:22 +0000880unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000881 unsigned) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000882 return 0;
883}
884
Owen Anderson825b72b2009-08-11 20:47:22 +0000885unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000886 unsigned,
887 unsigned /*Op0*/, bool /*Op0IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000888 return 0;
889}
890
Owen Anderson825b72b2009-08-11 20:47:22 +0000891unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000892 unsigned,
893 unsigned /*Op0*/, bool /*Op0IsKill*/,
894 unsigned /*Op1*/, bool /*Op1IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000895 return 0;
896}
897
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000898unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000899 return 0;
900}
901
Owen Anderson825b72b2009-08-11 20:47:22 +0000902unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohman46510a72010-04-15 01:51:59 +0000903 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000904 return 0;
905}
906
Owen Anderson825b72b2009-08-11 20:47:22 +0000907unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000908 unsigned,
909 unsigned /*Op0*/, bool /*Op0IsKill*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000910 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000911 return 0;
912}
913
Owen Anderson825b72b2009-08-11 20:47:22 +0000914unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000915 unsigned,
916 unsigned /*Op0*/, bool /*Op0IsKill*/,
Dan Gohman46510a72010-04-15 01:51:59 +0000917 const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000918 return 0;
919}
920
Owen Anderson825b72b2009-08-11 20:47:22 +0000921unsigned FastISel::FastEmit_rri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000922 unsigned,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000923 unsigned /*Op0*/, bool /*Op0IsKill*/,
924 unsigned /*Op1*/, bool /*Op1IsKill*/,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000925 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000926 return 0;
927}
928
929/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
930/// to emit an instruction with an immediate operand using FastEmit_ri.
931/// If that fails, it materializes the immediate into a register and try
932/// FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000933unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000934 unsigned Op0, bool Op0IsKill,
935 uint64_t Imm, MVT ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000936 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000937 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000938 if (ResultReg != 0)
939 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000940 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000941 if (MaterialReg == 0)
942 return 0;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000943 return FastEmit_rr(VT, VT, Opcode,
944 Op0, Op0IsKill,
945 MaterialReg, /*Kill=*/true);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000946}
947
Dan Gohman10df0fa2008-08-27 01:09:54 +0000948/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
949/// to emit an instruction with a floating-point immediate operand using
950/// FastEmit_rf. If that fails, it materializes the immediate into a register
951/// and try FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000952unsigned FastISel::FastEmit_rf_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000953 unsigned Op0, bool Op0IsKill,
954 const ConstantFP *FPImm, MVT ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000955 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000956 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, Op0IsKill, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000957 if (ResultReg != 0)
958 return ResultReg;
959
960 // Materialize the constant in a register.
961 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
962 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000963 // If the target doesn't have a way to directly enter a floating-point
964 // value into a register, use an alternate approach.
965 // TODO: The current approach only supports floating-point constants
966 // that can be constructed by conversion from integer values. This should
967 // be replaced by code that creates a load from a constant-pool entry,
968 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000969 const APFloat &Flt = FPImm->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000970 EVT IntVT = TLI.getPointerTy();
Dan Gohman10df0fa2008-08-27 01:09:54 +0000971
972 uint64_t x[2];
973 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000974 bool isExact;
975 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
976 APFloat::rmTowardZero, &isExact);
977 if (!isExact)
Dan Gohman10df0fa2008-08-27 01:09:54 +0000978 return 0;
979 APInt IntVal(IntBitWidth, 2, x);
980
981 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
982 ISD::Constant, IntVal.getZExtValue());
983 if (IntegerReg == 0)
984 return 0;
985 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000986 ISD::SINT_TO_FP, IntegerReg, /*Kill=*/true);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000987 if (MaterialReg == 0)
988 return 0;
989 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000990 return FastEmit_rr(VT, VT, Opcode,
991 Op0, Op0IsKill,
992 MaterialReg, /*Kill=*/true);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000993}
994
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000995unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
996 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000997}
998
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000999unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +00001000 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001001 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +00001002 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001003
Dan Gohmaneabaed22010-07-07 16:47:08 +00001004 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001005 return ResultReg;
1006}
1007
1008unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
1009 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001010 unsigned Op0, bool Op0IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001011 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +00001012 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001013
Evan Cheng5960e4e2008-09-08 08:38:20 +00001014 if (II.getNumDefs() >= 1)
Dan Gohmaneabaed22010-07-07 16:47:08 +00001015 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1016 .addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001017 else {
Dan Gohmaneabaed22010-07-07 16:47:08 +00001018 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1019 .addReg(Op0, Op0IsKill * RegState::Kill);
1020 bool InsertedCopy = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
1021 ResultReg, II.ImplicitDefs[0],
1022 RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001023 if (!InsertedCopy)
1024 ResultReg = 0;
1025 }
1026
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001027 return ResultReg;
1028}
1029
1030unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
1031 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001032 unsigned Op0, bool Op0IsKill,
1033 unsigned Op1, bool Op1IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001034 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +00001035 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001036
Evan Cheng5960e4e2008-09-08 08:38:20 +00001037 if (II.getNumDefs() >= 1)
Dan Gohmaneabaed22010-07-07 16:47:08 +00001038 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001039 .addReg(Op0, Op0IsKill * RegState::Kill)
1040 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001041 else {
Dan Gohmaneabaed22010-07-07 16:47:08 +00001042 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001043 .addReg(Op0, Op0IsKill * RegState::Kill)
1044 .addReg(Op1, Op1IsKill * RegState::Kill);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001045 bool InsertedCopy = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
1046 ResultReg, II.ImplicitDefs[0],
1047 RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001048 if (!InsertedCopy)
1049 ResultReg = 0;
1050 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001051 return ResultReg;
1052}
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001053
1054unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1055 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001056 unsigned Op0, bool Op0IsKill,
1057 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001058 unsigned ResultReg = createResultReg(RC);
1059 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1060
Evan Cheng5960e4e2008-09-08 08:38:20 +00001061 if (II.getNumDefs() >= 1)
Dan Gohmaneabaed22010-07-07 16:47:08 +00001062 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001063 .addReg(Op0, Op0IsKill * RegState::Kill)
1064 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001065 else {
Dan Gohmaneabaed22010-07-07 16:47:08 +00001066 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001067 .addReg(Op0, Op0IsKill * RegState::Kill)
1068 .addImm(Imm);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001069 bool InsertedCopy = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
1070 ResultReg, II.ImplicitDefs[0],
1071 RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001072 if (!InsertedCopy)
1073 ResultReg = 0;
1074 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001075 return ResultReg;
1076}
1077
Dan Gohman10df0fa2008-08-27 01:09:54 +00001078unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
1079 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001080 unsigned Op0, bool Op0IsKill,
1081 const ConstantFP *FPImm) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001082 unsigned ResultReg = createResultReg(RC);
1083 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1084
Evan Cheng5960e4e2008-09-08 08:38:20 +00001085 if (II.getNumDefs() >= 1)
Dan Gohmaneabaed22010-07-07 16:47:08 +00001086 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001087 .addReg(Op0, Op0IsKill * RegState::Kill)
1088 .addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001089 else {
Dan Gohmaneabaed22010-07-07 16:47:08 +00001090 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001091 .addReg(Op0, Op0IsKill * RegState::Kill)
1092 .addFPImm(FPImm);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001093 bool InsertedCopy = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
1094 ResultReg, II.ImplicitDefs[0],
1095 RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001096 if (!InsertedCopy)
1097 ResultReg = 0;
1098 }
Dan Gohman10df0fa2008-08-27 01:09:54 +00001099 return ResultReg;
1100}
1101
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001102unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
1103 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001104 unsigned Op0, bool Op0IsKill,
1105 unsigned Op1, bool Op1IsKill,
1106 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001107 unsigned ResultReg = createResultReg(RC);
1108 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1109
Evan Cheng5960e4e2008-09-08 08:38:20 +00001110 if (II.getNumDefs() >= 1)
Dan Gohmaneabaed22010-07-07 16:47:08 +00001111 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001112 .addReg(Op0, Op0IsKill * RegState::Kill)
1113 .addReg(Op1, Op1IsKill * RegState::Kill)
1114 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001115 else {
Dan Gohmaneabaed22010-07-07 16:47:08 +00001116 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001117 .addReg(Op0, Op0IsKill * RegState::Kill)
1118 .addReg(Op1, Op1IsKill * RegState::Kill)
1119 .addImm(Imm);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001120 bool InsertedCopy = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
1121 ResultReg, II.ImplicitDefs[0],
1122 RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001123 if (!InsertedCopy)
1124 ResultReg = 0;
1125 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001126 return ResultReg;
1127}
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001128
1129unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1130 const TargetRegisterClass *RC,
1131 uint64_t Imm) {
1132 unsigned ResultReg = createResultReg(RC);
1133 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1134
Evan Cheng5960e4e2008-09-08 08:38:20 +00001135 if (II.getNumDefs() >= 1)
Dan Gohmaneabaed22010-07-07 16:47:08 +00001136 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001137 else {
Dan Gohmaneabaed22010-07-07 16:47:08 +00001138 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm);
1139 bool InsertedCopy = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
1140 ResultReg, II.ImplicitDefs[0],
1141 RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001142 if (!InsertedCopy)
1143 ResultReg = 0;
1144 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001145 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +00001146}
Owen Anderson8970f002008-08-27 22:30:02 +00001147
Owen Anderson825b72b2009-08-11 20:47:22 +00001148unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001149 unsigned Op0, bool Op0IsKill,
1150 uint32_t Idx) {
Owen Anderson40a468f2008-08-28 17:47:37 +00001151 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +00001152
Evan Cheng536ab132009-01-22 09:10:11 +00001153 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Chris Lattner518bb532010-02-09 19:54:29 +00001154 const TargetInstrDesc &II = TII.get(TargetOpcode::EXTRACT_SUBREG);
Owen Anderson8970f002008-08-27 22:30:02 +00001155
Evan Cheng5960e4e2008-09-08 08:38:20 +00001156 if (II.getNumDefs() >= 1)
Dan Gohmaneabaed22010-07-07 16:47:08 +00001157 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001158 .addReg(Op0, Op0IsKill * RegState::Kill)
1159 .addImm(Idx);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001160 else {
Dan Gohmaneabaed22010-07-07 16:47:08 +00001161 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001162 .addReg(Op0, Op0IsKill * RegState::Kill)
1163 .addImm(Idx);
Dan Gohmaneabaed22010-07-07 16:47:08 +00001164 bool InsertedCopy = TII.copyRegToReg(*FuncInfo.MBB, FuncInfo.InsertPt,
1165 ResultReg, II.ImplicitDefs[0],
1166 RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001167 if (!InsertedCopy)
1168 ResultReg = 0;
1169 }
Owen Anderson8970f002008-08-27 22:30:02 +00001170 return ResultReg;
1171}
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001172
1173/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1174/// with all but the least significant bit set to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001175unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1176 return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001177}
Dan Gohmanf81eca02010-04-22 20:46:50 +00001178
1179/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1180/// Emit code to ensure constants are copied into registers when needed.
1181/// Remember the virtual registers that need to be added to the Machine PHI
1182/// nodes as input. We cannot just directly add them, because expansion
1183/// might result in multiple MBB's for one BB. As such, the start of the
1184/// BB might correspond to a different MBB than the end.
1185bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1186 const TerminatorInst *TI = LLVMBB->getTerminator();
1187
1188 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohmana4160c32010-07-07 16:29:44 +00001189 unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001190
1191 // Check successor nodes' PHI nodes that expect a constant to be available
1192 // from this block.
1193 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1194 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1195 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmana4160c32010-07-07 16:29:44 +00001196 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Dan Gohmanf81eca02010-04-22 20:46:50 +00001197
1198 // If this terminator has multiple identical successors (common for
1199 // switches), only handle each succ once.
1200 if (!SuccsHandled.insert(SuccMBB)) continue;
1201
1202 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1203
1204 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1205 // nodes and Machine PHI nodes, but the incoming operands have not been
1206 // emitted yet.
1207 for (BasicBlock::const_iterator I = SuccBB->begin();
1208 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanfb95f892010-05-07 01:10:20 +00001209
Dan Gohmanf81eca02010-04-22 20:46:50 +00001210 // Ignore dead phi's.
1211 if (PN->use_empty()) continue;
1212
1213 // Only handle legal types. Two interesting things to note here. First,
1214 // by bailing out early, we may leave behind some dead instructions,
1215 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
1216 // own moves. Second, this check is necessary becuase FastISel doesn't
Dan Gohman89496d02010-07-02 00:10:16 +00001217 // use CreateRegs to create registers, so it always creates
Dan Gohmanf81eca02010-04-22 20:46:50 +00001218 // exactly one register for each non-void instruction.
1219 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1220 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
1221 // Promote MVT::i1.
1222 if (VT == MVT::i1)
1223 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1224 else {
Dan Gohmana4160c32010-07-07 16:29:44 +00001225 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001226 return false;
1227 }
1228 }
1229
1230 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1231
Dan Gohmanfb95f892010-05-07 01:10:20 +00001232 // Set the DebugLoc for the copy. Prefer the location of the operand
1233 // if there is one; use the location of the PHI otherwise.
1234 DL = PN->getDebugLoc();
1235 if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
1236 DL = Inst->getDebugLoc();
1237
Dan Gohmanf81eca02010-04-22 20:46:50 +00001238 unsigned Reg = getRegForValue(PHIOp);
1239 if (Reg == 0) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001240 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001241 return false;
1242 }
Dan Gohmana4160c32010-07-07 16:29:44 +00001243 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohmanfb95f892010-05-07 01:10:20 +00001244 DL = DebugLoc();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001245 }
1246 }
1247
1248 return true;
1249}