blob: a917cdddeea16ed2e2c3cfd82c502404610945eb [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));
Chris Lattner518bb532010-02-09 19:54:29 +0000172 BuildMI(MBB, DL, TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000173 }
Owen Andersond5d81a42008-09-03 17:51:57 +0000174
Dan Gohmandceffe62008-09-25 01:28:51 +0000175 // If target-independent code couldn't handle the value, give target-specific
176 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000177 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000178 Reg = TargetMaterializeConstant(cast<Constant>(V));
Owen Anderson6e607452008-09-05 23:36:01 +0000179
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000180 // Don't cache constant materializations in the general ValueMap.
181 // To do so would require tracking what uses they dominate.
Dan Gohmandceffe62008-09-25 01:28:51 +0000182 if (Reg != 0)
183 LocalValueMap[V] = Reg;
Dan Gohman104e4ce2008-09-03 23:32:19 +0000184 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000185}
186
Dan Gohman46510a72010-04-15 01:51:59 +0000187unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng59fbc802008-09-09 01:26:59 +0000188 // Look up the value to see if we already have a register for it. We
189 // cache values defined by Instructions across blocks, and other values
190 // only locally. This is because Instructions already have the SSA
Dan Gohman1fdc6142010-05-03 23:36:34 +0000191 // def-dominates-use requirement enforced.
Dan Gohmana4160c32010-07-07 16:29:44 +0000192 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
193 if (I != FuncInfo.ValueMap.end())
Dan Gohman3193a682010-06-21 14:21:47 +0000194 return I->second;
Evan Cheng59fbc802008-09-09 01:26:59 +0000195 return LocalValueMap[V];
196}
197
Owen Andersoncc54e762008-08-30 00:38:46 +0000198/// UpdateValueMap - Update the value map to include the new mapping for this
199/// instruction, or insert an extra copy to get the result in a previous
200/// determined register.
201/// NOTE: This is only necessary because we might select a block that uses
202/// a value before we select the block that defines the value. It might be
203/// possible to fix this by selecting blocks in reverse postorder.
Dan Gohman46510a72010-04-15 01:51:59 +0000204unsigned FastISel::UpdateValueMap(const Value *I, unsigned Reg) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000205 if (!isa<Instruction>(I)) {
206 LocalValueMap[I] = Reg;
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000207 return Reg;
Dan Gohman40b189e2008-09-05 18:18:20 +0000208 }
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000209
Dan Gohmana4160c32010-07-07 16:29:44 +0000210 unsigned &AssignedReg = FuncInfo.ValueMap[I];
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000211 if (AssignedReg == 0)
212 AssignedReg = Reg;
Chris Lattner36e39462009-04-12 07:46:30 +0000213 else if (Reg != AssignedReg) {
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000214 const TargetRegisterClass *RegClass = MRI.getRegClass(Reg);
215 TII.copyRegToReg(*MBB, MBB->end(), AssignedReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000216 Reg, RegClass, RegClass, DL);
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000217 }
218 return AssignedReg;
Owen Andersoncc54e762008-08-30 00:38:46 +0000219}
220
Dan Gohmana6cb6412010-05-11 23:54:07 +0000221std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000222 unsigned IdxN = getRegForValue(Idx);
223 if (IdxN == 0)
224 // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000225 return std::pair<unsigned, bool>(0, false);
226
227 bool IdxNIsKill = hasTrivialKill(Idx);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000228
229 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Anderson766b5ef2009-08-11 21:59:30 +0000230 MVT PtrVT = TLI.getPointerTy();
Owen Andersone50ed302009-08-10 22:56:29 +0000231 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000232 if (IdxVT.bitsLT(PtrVT)) {
233 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND,
234 IdxN, IdxNIsKill);
235 IdxNIsKill = true;
236 }
237 else if (IdxVT.bitsGT(PtrVT)) {
238 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE,
239 IdxN, IdxNIsKill);
240 IdxNIsKill = true;
241 }
242 return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000243}
244
Dan Gohmanbdedd442008-08-20 00:11:48 +0000245/// SelectBinaryOp - Select and emit code for a binary operator instruction,
246/// which has an opcode which directly corresponds to the given ISD opcode.
247///
Dan Gohman46510a72010-04-15 01:51:59 +0000248bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000249 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmanbdedd442008-08-20 00:11:48 +0000251 // Unhandled type. Halt "fast" selection and bail.
252 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000253
Dan Gohmanb71fea22008-08-26 20:52:40 +0000254 // We only handle legal types. For example, on x86-32 the instruction
255 // selector contains all of the 64-bit instructions from x86-64,
256 // under the assumption that i64 won't be used if the target doesn't
257 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000258 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000259 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000260 // don't require additional zeroing, which makes them easy.
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000262 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
263 ISDOpcode == ISD::XOR))
Owen Anderson23b9b192009-08-12 00:36:31 +0000264 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohman638c6832008-09-05 18:44:22 +0000265 else
266 return false;
267 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000268
Dan Gohman3df24e62008-09-03 23:12:08 +0000269 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000270 if (Op0 == 0)
271 // Unhandled operand. Halt "fast" selection and bail.
272 return false;
273
Dan Gohmana6cb6412010-05-11 23:54:07 +0000274 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
275
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000276 // Check if the second operand is a constant and handle it appropriately.
277 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000278 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000279 ISDOpcode, Op0, Op0IsKill,
280 CI->getZExtValue());
Dan Gohmanad368ac2008-08-27 18:10:19 +0000281 if (ResultReg != 0) {
282 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000283 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000284 return true;
285 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000286 }
287
Dan Gohman10df0fa2008-08-27 01:09:54 +0000288 // Check if the second operand is a constant float.
289 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000290 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000291 ISDOpcode, Op0, Op0IsKill, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000292 if (ResultReg != 0) {
293 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000294 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000295 return true;
296 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000297 }
298
Dan Gohman3df24e62008-09-03 23:12:08 +0000299 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000300 if (Op1 == 0)
301 // Unhandled operand. Halt "fast" selection and bail.
302 return false;
303
Dan Gohmana6cb6412010-05-11 23:54:07 +0000304 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
305
Dan Gohmanad368ac2008-08-27 18:10:19 +0000306 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000307 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000308 ISDOpcode,
309 Op0, Op0IsKill,
310 Op1, Op1IsKill);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000311 if (ResultReg == 0)
312 // Target-specific code wasn't able to find a machine opcode for
313 // the given ISD opcode and type. Halt "fast" selection and bail.
314 return false;
315
Dan Gohman8014e862008-08-20 00:23:20 +0000316 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000317 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000318 return true;
319}
320
Dan Gohman46510a72010-04-15 01:51:59 +0000321bool FastISel::SelectGetElementPtr(const User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000322 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000323 if (N == 0)
324 // Unhandled operand. Halt "fast" selection and bail.
325 return false;
326
Dan Gohmana6cb6412010-05-11 23:54:07 +0000327 bool NIsKill = hasTrivialKill(I->getOperand(0));
328
Evan Cheng83785c82008-08-20 22:45:34 +0000329 const Type *Ty = I->getOperand(0)->getType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000330 MVT VT = TLI.getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +0000331 for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
332 E = I->op_end(); OI != E; ++OI) {
333 const Value *Idx = *OI;
Evan Cheng83785c82008-08-20 22:45:34 +0000334 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
335 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
336 if (Field) {
337 // N = N + Offset
338 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
339 // FIXME: This can be optimized by combining the add with a
340 // subsequent one.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000341 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000342 if (N == 0)
343 // Unhandled operand. Halt "fast" selection and bail.
344 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000345 NIsKill = true;
Evan Cheng83785c82008-08-20 22:45:34 +0000346 }
347 Ty = StTy->getElementType(Field);
348 } else {
349 Ty = cast<SequentialType>(Ty)->getElementType();
350
351 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +0000352 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +0000353 if (CI->isZero()) continue;
Evan Cheng83785c82008-08-20 22:45:34 +0000354 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +0000355 TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohmana6cb6412010-05-11 23:54:07 +0000356 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000357 if (N == 0)
358 // Unhandled operand. Halt "fast" selection and bail.
359 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000360 NIsKill = true;
Evan Cheng83785c82008-08-20 22:45:34 +0000361 continue;
362 }
363
364 // N = N + Idx * ElementSize;
Duncan Sands777d2302009-05-09 07:06:46 +0000365 uint64_t ElementSize = TD.getTypeAllocSize(Ty);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000366 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
367 unsigned IdxN = Pair.first;
368 bool IdxNIsKill = Pair.second;
Evan Cheng83785c82008-08-20 22:45:34 +0000369 if (IdxN == 0)
370 // Unhandled operand. Halt "fast" selection and bail.
371 return false;
372
Dan Gohman80bc6e22008-08-26 20:57:08 +0000373 if (ElementSize != 1) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000374 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000375 if (IdxN == 0)
376 // Unhandled operand. Halt "fast" selection and bail.
377 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000378 IdxNIsKill = true;
Dan Gohman80bc6e22008-08-26 20:57:08 +0000379 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000380 N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
Evan Cheng83785c82008-08-20 22:45:34 +0000381 if (N == 0)
382 // Unhandled operand. Halt "fast" selection and bail.
383 return false;
384 }
385 }
386
387 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000388 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000389 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000390}
391
Dan Gohman46510a72010-04-15 01:51:59 +0000392bool FastISel::SelectCall(const User *I) {
393 const Function *F = cast<CallInst>(I)->getCalledFunction();
Dan Gohman33134c42008-09-25 17:05:24 +0000394 if (!F) return false;
395
Dan Gohman4183e312010-04-13 17:07:06 +0000396 // Handle selected intrinsic function calls.
Dan Gohman33134c42008-09-25 17:05:24 +0000397 unsigned IID = F->getIntrinsicID();
398 switch (IID) {
399 default: break;
Bill Wendling92c1e122009-02-13 02:16:35 +0000400 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +0000401 const DbgDeclareInst *DI = cast<DbgDeclareInst>(I);
Devang Patel02f0dbd2010-05-07 22:04:20 +0000402 if (!DIVariable(DI->getVariable()).Verify() ||
Dan Gohmana4160c32010-07-07 16:29:44 +0000403 !FuncInfo.MF->getMMI().hasDebugInfo())
Devang Patel7e1e31f2009-07-02 22:43:26 +0000404 return true;
405
Dan Gohman46510a72010-04-15 01:51:59 +0000406 const Value *Address = DI->getAddress();
Dale Johannesendc918562010-02-06 02:26:02 +0000407 if (!Address)
408 return true;
Dale Johannesen343b42e2010-04-07 01:15:14 +0000409 if (isa<UndefValue>(Address))
410 return true;
Dan Gohman46510a72010-04-15 01:51:59 +0000411 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
Devang Patel7e1e31f2009-07-02 22:43:26 +0000412 // Don't handle byval struct arguments or VLAs, for example.
Dale Johannesen7dc78402010-04-25 21:03:54 +0000413 // Note that if we have a byval struct argument, fast ISel is turned off;
414 // those are handled in SelectionDAGBuilder.
Devang Patel54fc4d62010-04-28 19:27:33 +0000415 if (AI) {
416 DenseMap<const AllocaInst*, int>::iterator SI =
Dan Gohmana4160c32010-07-07 16:29:44 +0000417 FuncInfo.StaticAllocaMap.find(AI);
418 if (SI == FuncInfo.StaticAllocaMap.end()) break; // VLAs.
Devang Patel54fc4d62010-04-28 19:27:33 +0000419 int FI = SI->second;
420 if (!DI->getDebugLoc().isUnknown())
Dan Gohmana4160c32010-07-07 16:29:44 +0000421 FuncInfo.MF->getMMI().setVariableDbgInfo(DI->getVariable(),
422 FI, DI->getDebugLoc());
Devang Patel54fc4d62010-04-28 19:27:33 +0000423 } else
424 // Building the map above is target independent. Generating DBG_VALUE
425 // inline is target dependent; do this now.
426 (void)TargetSelectInstruction(cast<Instruction>(I));
Dan Gohman33134c42008-09-25 17:05:24 +0000427 return true;
Bill Wendling92c1e122009-02-13 02:16:35 +0000428 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000429 case Intrinsic::dbg_value: {
Dale Johannesen343b42e2010-04-07 01:15:14 +0000430 // This form of DBG_VALUE is target-independent.
Dan Gohman46510a72010-04-15 01:51:59 +0000431 const DbgValueInst *DI = cast<DbgValueInst>(I);
Dale Johannesen45df7612010-02-26 20:01:55 +0000432 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohman46510a72010-04-15 01:51:59 +0000433 const Value *V = DI->getValue();
Dale Johannesen45df7612010-02-26 20:01:55 +0000434 if (!V) {
435 // Currently the optimizer can produce this; insert an undef to
436 // help debugging. Probably the optimizer should not do this.
437 BuildMI(MBB, DL, II).addReg(0U).addImm(DI->getOffset()).
438 addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000439 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dale Johannesen45df7612010-02-26 20:01:55 +0000440 BuildMI(MBB, DL, II).addImm(CI->getZExtValue()).addImm(DI->getOffset()).
441 addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000442 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dale Johannesen45df7612010-02-26 20:01:55 +0000443 BuildMI(MBB, DL, II).addFPImm(CF).addImm(DI->getOffset()).
444 addMetadata(DI->getVariable());
445 } else if (unsigned Reg = lookUpRegForValue(V)) {
446 BuildMI(MBB, DL, II).addReg(Reg, RegState::Debug).addImm(DI->getOffset()).
447 addMetadata(DI->getVariable());
448 } else {
449 // We can't yet handle anything else here because it would require
450 // generating code, thus altering codegen because of debug info.
451 // Insert an undef so we can see what we dropped.
452 BuildMI(MBB, DL, II).addReg(0U).addImm(DI->getOffset()).
453 addMetadata(DI->getVariable());
454 }
455 return true;
456 }
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000457 case Intrinsic::eh_exception: {
Owen Andersone50ed302009-08-10 22:56:29 +0000458 EVT VT = TLI.getValueType(I->getType());
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000459 switch (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)) {
460 default: break;
461 case TargetLowering::Expand: {
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000462 assert(MBB->isLandingPad() && "Call to eh.exception not in landing pad!");
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000463 unsigned Reg = TLI.getExceptionAddressRegister();
464 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
465 unsigned ResultReg = createResultReg(RC);
466 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000467 Reg, RC, RC, DL);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000468 assert(InsertedCopy && "Can't copy address registers!");
Evan Cheng24ac4082008-11-24 07:09:49 +0000469 InsertedCopy = InsertedCopy;
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000470 UpdateValueMap(I, ResultReg);
471 return true;
472 }
473 }
474 break;
475 }
Duncan Sandsb01bbdc2009-10-14 16:11:37 +0000476 case Intrinsic::eh_selector: {
Owen Andersone50ed302009-08-10 22:56:29 +0000477 EVT VT = TLI.getValueType(I->getType());
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000478 switch (TLI.getOperationAction(ISD::EHSELECTION, VT)) {
479 default: break;
480 case TargetLowering::Expand: {
Chris Lattnered3a8062010-04-05 06:05:26 +0000481 if (MBB->isLandingPad())
Dan Gohmana4160c32010-07-07 16:29:44 +0000482 AddCatchInfo(*cast<CallInst>(I), &FuncInfo.MF->getMMI(), MBB);
Chris Lattnered3a8062010-04-05 06:05:26 +0000483 else {
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000484#ifndef NDEBUG
Dan Gohmana4160c32010-07-07 16:29:44 +0000485 FuncInfo.CatchInfoLost.insert(cast<CallInst>(I));
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000486#endif
Chris Lattnered3a8062010-04-05 06:05:26 +0000487 // FIXME: Mark exception selector register as live in. Hack for PR1508.
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000488 unsigned Reg = TLI.getExceptionSelectorRegister();
Chris Lattnered3a8062010-04-05 06:05:26 +0000489 if (Reg) MBB->addLiveIn(Reg);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000490 }
Chris Lattnered3a8062010-04-05 06:05:26 +0000491
492 unsigned Reg = TLI.getExceptionSelectorRegister();
493 EVT SrcVT = TLI.getPointerTy();
494 const TargetRegisterClass *RC = TLI.getRegClassFor(SrcVT);
495 unsigned ResultReg = createResultReg(RC);
496 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, Reg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000497 RC, RC, DL);
Chris Lattnered3a8062010-04-05 06:05:26 +0000498 assert(InsertedCopy && "Can't copy address registers!");
499 InsertedCopy = InsertedCopy;
500
Dan Gohmana6cb6412010-05-11 23:54:07 +0000501 bool ResultRegIsKill = hasTrivialKill(I);
502
Chris Lattnered3a8062010-04-05 06:05:26 +0000503 // Cast the register to the type of the selector.
504 if (SrcVT.bitsGT(MVT::i32))
505 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, ISD::TRUNCATE,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000506 ResultReg, ResultRegIsKill);
Chris Lattnered3a8062010-04-05 06:05:26 +0000507 else if (SrcVT.bitsLT(MVT::i32))
508 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000509 ISD::SIGN_EXTEND, ResultReg, ResultRegIsKill);
Chris Lattnered3a8062010-04-05 06:05:26 +0000510 if (ResultReg == 0)
511 // Unhandled operand. Halt "fast" selection and bail.
512 return false;
513
514 UpdateValueMap(I, ResultReg);
515
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000516 return true;
517 }
518 }
519 break;
520 }
Dan Gohman33134c42008-09-25 17:05:24 +0000521 }
Dan Gohman4183e312010-04-13 17:07:06 +0000522
523 // An arbitrary call. Bail.
Dan Gohman33134c42008-09-25 17:05:24 +0000524 return false;
525}
526
Dan Gohman46510a72010-04-15 01:51:59 +0000527bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000528 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
529 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000530
Owen Anderson825b72b2009-08-11 20:47:22 +0000531 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
532 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersond0533c92008-08-26 23:46:32 +0000533 // Unhandled type. Halt "fast" selection and bail.
534 return false;
535
Dan Gohman474d3b32009-03-13 23:53:06 +0000536 // Check if the destination type is legal. Or as a special case,
537 // it may be i1 if we're doing a truncate because that's
538 // easy and somewhat common.
539 if (!TLI.isTypeLegal(DstVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000540 if (DstVT != MVT::i1 || Opcode != ISD::TRUNCATE)
Dan Gohman91b6f972008-10-03 01:28:47 +0000541 // Unhandled type. Halt "fast" selection and bail.
542 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000543
544 // Check if the source operand is legal. Or as a special case,
545 // it may be i1 if we're doing zero-extension because that's
546 // easy and somewhat common.
547 if (!TLI.isTypeLegal(SrcVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000548 if (SrcVT != MVT::i1 || Opcode != ISD::ZERO_EXTEND)
Dan Gohman474d3b32009-03-13 23:53:06 +0000549 // Unhandled type. Halt "fast" selection and bail.
550 return false;
551
Dan Gohman3df24e62008-09-03 23:12:08 +0000552 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000553 if (!InputReg)
554 // Unhandled operand. Halt "fast" selection and bail.
555 return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000556
Dan Gohmana6cb6412010-05-11 23:54:07 +0000557 bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
558
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000559 // If the operand is i1, arrange for the high bits in the register to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +0000560 if (SrcVT == MVT::i1) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000561 SrcVT = TLI.getTypeToTransformTo(I->getContext(), SrcVT);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000562 InputReg = FastEmitZExtFromI1(SrcVT.getSimpleVT(), InputReg, InputRegIsKill);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000563 if (!InputReg)
564 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000565 InputRegIsKill = true;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000566 }
Dan Gohman474d3b32009-03-13 23:53:06 +0000567 // If the result is i1, truncate to the target's type for i1 first.
Owen Anderson825b72b2009-08-11 20:47:22 +0000568 if (DstVT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +0000569 DstVT = TLI.getTypeToTransformTo(I->getContext(), DstVT);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000570
Owen Andersond0533c92008-08-26 23:46:32 +0000571 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
572 DstVT.getSimpleVT(),
573 Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000574 InputReg, InputRegIsKill);
Owen Andersond0533c92008-08-26 23:46:32 +0000575 if (!ResultReg)
576 return false;
577
Dan Gohman3df24e62008-09-03 23:12:08 +0000578 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000579 return true;
580}
581
Dan Gohman46510a72010-04-15 01:51:59 +0000582bool FastISel::SelectBitCast(const User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000583 // If the bitcast doesn't change the type, just use the operand value.
584 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000585 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000586 if (Reg == 0)
587 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000588 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000589 return true;
590 }
591
592 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Andersone50ed302009-08-10 22:56:29 +0000593 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
594 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000595
Owen Anderson825b72b2009-08-11 20:47:22 +0000596 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
597 DstVT == MVT::Other || !DstVT.isSimple() ||
Owen Andersond0533c92008-08-26 23:46:32 +0000598 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
599 // Unhandled type. Halt "fast" selection and bail.
600 return false;
601
Dan Gohman3df24e62008-09-03 23:12:08 +0000602 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000603 if (Op0 == 0)
604 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000605 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000606
607 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000608
Dan Gohmanad368ac2008-08-27 18:10:19 +0000609 // First, try to perform the bitcast by inserting a reg-reg copy.
610 unsigned ResultReg = 0;
611 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
612 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
613 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
614 ResultReg = createResultReg(DstClass);
615
616 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000617 Op0, DstClass, SrcClass, DL);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000618 if (!InsertedCopy)
619 ResultReg = 0;
620 }
621
622 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
623 if (!ResultReg)
624 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000625 ISD::BIT_CONVERT, Op0, Op0IsKill);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000626
627 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000628 return false;
629
Dan Gohman3df24e62008-09-03 23:12:08 +0000630 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000631 return true;
632}
633
Dan Gohman3df24e62008-09-03 23:12:08 +0000634bool
Dan Gohman46510a72010-04-15 01:51:59 +0000635FastISel::SelectInstruction(const Instruction *I) {
Dan Gohmane8c92dd2010-04-23 15:29:50 +0000636 // Just before the terminator instruction, insert instructions to
637 // feed PHI nodes in successor blocks.
638 if (isa<TerminatorInst>(I))
639 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
640 return false;
641
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000642 DL = I->getDebugLoc();
643
Dan Gohman6e3ff372009-12-05 01:27:58 +0000644 // First, try doing target-independent selection.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000645 if (SelectOperator(I, I->getOpcode())) {
646 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000647 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000648 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000649
650 // Next, try calling the target to attempt to handle the instruction.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000651 if (TargetSelectInstruction(I)) {
652 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000653 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000654 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000655
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000656 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000657 return false;
Dan Gohman40b189e2008-09-05 18:18:20 +0000658}
659
Dan Gohmand98d6202008-10-02 22:15:21 +0000660/// FastEmitBranch - Emit an unconditional branch to the given block,
661/// unless it is the immediate (fall-through) successor, and update
662/// the CFG.
663void
Stuart Hastings3bf91252010-06-17 22:43:56 +0000664FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DL) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000665 if (MBB->isLayoutSuccessor(MSucc)) {
666 // The unconditional fall-through case, which needs no instructions.
667 } else {
668 // The unconditional branch case.
Stuart Hastings3bf91252010-06-17 22:43:56 +0000669 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>(), DL);
Dan Gohmand98d6202008-10-02 22:15:21 +0000670 }
671 MBB->addSuccessor(MSucc);
672}
673
Dan Gohman3d45a852009-09-03 22:53:57 +0000674/// SelectFNeg - Emit an FNeg operation.
675///
676bool
Dan Gohman46510a72010-04-15 01:51:59 +0000677FastISel::SelectFNeg(const User *I) {
Dan Gohman3d45a852009-09-03 22:53:57 +0000678 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
679 if (OpReg == 0) return false;
680
Dan Gohmana6cb6412010-05-11 23:54:07 +0000681 bool OpRegIsKill = hasTrivialKill(I);
682
Dan Gohman4a215a12009-09-11 00:36:43 +0000683 // If the target has ISD::FNEG, use it.
684 EVT VT = TLI.getValueType(I->getType());
685 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000686 ISD::FNEG, OpReg, OpRegIsKill);
Dan Gohman4a215a12009-09-11 00:36:43 +0000687 if (ResultReg != 0) {
688 UpdateValueMap(I, ResultReg);
689 return true;
690 }
691
Dan Gohman5e5abb72009-09-11 00:34:46 +0000692 // Bitcast the value to integer, twiddle the sign bit with xor,
693 // and then bitcast it back to floating-point.
Dan Gohman3d45a852009-09-03 22:53:57 +0000694 if (VT.getSizeInBits() > 64) return false;
Dan Gohman5e5abb72009-09-11 00:34:46 +0000695 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
696 if (!TLI.isTypeLegal(IntVT))
697 return false;
698
699 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000700 ISD::BIT_CONVERT, OpReg, OpRegIsKill);
Dan Gohman5e5abb72009-09-11 00:34:46 +0000701 if (IntReg == 0)
702 return false;
703
Dan Gohmana6cb6412010-05-11 23:54:07 +0000704 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
705 IntReg, /*Kill=*/true,
Dan Gohman5e5abb72009-09-11 00:34:46 +0000706 UINT64_C(1) << (VT.getSizeInBits()-1),
707 IntVT.getSimpleVT());
708 if (IntResultReg == 0)
709 return false;
710
711 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000712 ISD::BIT_CONVERT, IntResultReg, /*Kill=*/true);
Dan Gohman3d45a852009-09-03 22:53:57 +0000713 if (ResultReg == 0)
714 return false;
715
716 UpdateValueMap(I, ResultReg);
717 return true;
718}
719
Dan Gohman40b189e2008-09-05 18:18:20 +0000720bool
Dan Gohman7fbcc982010-07-01 03:49:38 +0000721FastISel::SelectLoad(const User *I) {
722 LoadInst *LI = const_cast<LoadInst *>(cast<LoadInst>(I));
723
724 // For a load from an alloca, make a limited effort to find the value
725 // already available in a register, avoiding redundant loads.
726 if (!LI->isVolatile() && isa<AllocaInst>(LI->getPointerOperand())) {
727 BasicBlock::iterator ScanFrom = LI;
728 if (const Value *V = FindAvailableLoadedValue(LI->getPointerOperand(),
729 LI->getParent(), ScanFrom)) {
730 unsigned ResultReg = getRegForValue(V);
731 if (ResultReg != 0) {
732 UpdateValueMap(I, ResultReg);
733 return true;
734 }
735 }
736 }
737
738 return false;
739}
740
741bool
Dan Gohman46510a72010-04-15 01:51:59 +0000742FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000743 switch (Opcode) {
Dan Gohman7fbcc982010-07-01 03:49:38 +0000744 case Instruction::Load:
745 return SelectLoad(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000746 case Instruction::Add:
747 return SelectBinaryOp(I, ISD::ADD);
748 case Instruction::FAdd:
749 return SelectBinaryOp(I, ISD::FADD);
750 case Instruction::Sub:
751 return SelectBinaryOp(I, ISD::SUB);
752 case Instruction::FSub:
Dan Gohman3d45a852009-09-03 22:53:57 +0000753 // FNeg is currently represented in LLVM IR as a special case of FSub.
754 if (BinaryOperator::isFNeg(I))
755 return SelectFNeg(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000756 return SelectBinaryOp(I, ISD::FSUB);
757 case Instruction::Mul:
758 return SelectBinaryOp(I, ISD::MUL);
759 case Instruction::FMul:
760 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman3df24e62008-09-03 23:12:08 +0000761 case Instruction::SDiv:
762 return SelectBinaryOp(I, ISD::SDIV);
763 case Instruction::UDiv:
764 return SelectBinaryOp(I, ISD::UDIV);
765 case Instruction::FDiv:
766 return SelectBinaryOp(I, ISD::FDIV);
767 case Instruction::SRem:
768 return SelectBinaryOp(I, ISD::SREM);
769 case Instruction::URem:
770 return SelectBinaryOp(I, ISD::UREM);
771 case Instruction::FRem:
772 return SelectBinaryOp(I, ISD::FREM);
773 case Instruction::Shl:
774 return SelectBinaryOp(I, ISD::SHL);
775 case Instruction::LShr:
776 return SelectBinaryOp(I, ISD::SRL);
777 case Instruction::AShr:
778 return SelectBinaryOp(I, ISD::SRA);
779 case Instruction::And:
780 return SelectBinaryOp(I, ISD::AND);
781 case Instruction::Or:
782 return SelectBinaryOp(I, ISD::OR);
783 case Instruction::Xor:
784 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000785
Dan Gohman3df24e62008-09-03 23:12:08 +0000786 case Instruction::GetElementPtr:
787 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000788
Dan Gohman3df24e62008-09-03 23:12:08 +0000789 case Instruction::Br: {
Dan Gohman46510a72010-04-15 01:51:59 +0000790 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000791
Dan Gohman3df24e62008-09-03 23:12:08 +0000792 if (BI->isUnconditional()) {
Dan Gohman46510a72010-04-15 01:51:59 +0000793 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohmana4160c32010-07-07 16:29:44 +0000794 MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
Stuart Hastings3bf91252010-06-17 22:43:56 +0000795 FastEmitBranch(MSucc, BI->getDebugLoc());
Dan Gohman3df24e62008-09-03 23:12:08 +0000796 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000797 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000798
799 // Conditional branches are not handed yet.
800 // Halt "fast" selection and bail.
801 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000802 }
803
Dan Gohman087c8502008-09-05 01:08:41 +0000804 case Instruction::Unreachable:
805 // Nothing to emit.
806 return true;
807
Dan Gohman0586d912008-09-10 20:11:02 +0000808 case Instruction::Alloca:
809 // FunctionLowering has the static-sized case covered.
Dan Gohmana4160c32010-07-07 16:29:44 +0000810 if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
Dan Gohman0586d912008-09-10 20:11:02 +0000811 return true;
812
813 // Dynamic-sized alloca is not handled yet.
814 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000815
Dan Gohman33134c42008-09-25 17:05:24 +0000816 case Instruction::Call:
817 return SelectCall(I);
818
Dan Gohman3df24e62008-09-03 23:12:08 +0000819 case Instruction::BitCast:
820 return SelectBitCast(I);
821
822 case Instruction::FPToSI:
823 return SelectCast(I, ISD::FP_TO_SINT);
824 case Instruction::ZExt:
825 return SelectCast(I, ISD::ZERO_EXTEND);
826 case Instruction::SExt:
827 return SelectCast(I, ISD::SIGN_EXTEND);
828 case Instruction::Trunc:
829 return SelectCast(I, ISD::TRUNCATE);
830 case Instruction::SIToFP:
831 return SelectCast(I, ISD::SINT_TO_FP);
832
833 case Instruction::IntToPtr: // Deliberate fall-through.
834 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +0000835 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
836 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman3df24e62008-09-03 23:12:08 +0000837 if (DstVT.bitsGT(SrcVT))
838 return SelectCast(I, ISD::ZERO_EXTEND);
839 if (DstVT.bitsLT(SrcVT))
840 return SelectCast(I, ISD::TRUNCATE);
841 unsigned Reg = getRegForValue(I->getOperand(0));
842 if (Reg == 0) return false;
843 UpdateValueMap(I, Reg);
844 return true;
845 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000846
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000847 case Instruction::PHI:
848 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
849
Dan Gohman3df24e62008-09-03 23:12:08 +0000850 default:
851 // Unhandled instruction. Halt "fast" selection and bail.
852 return false;
853 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000854}
855
Dan Gohmana4160c32010-07-07 16:29:44 +0000856FastISel::FastISel(FunctionLoweringInfo &funcInfo)
Dan Gohman3df24e62008-09-03 23:12:08 +0000857 : MBB(0),
Dan Gohmana4160c32010-07-07 16:29:44 +0000858 FuncInfo(funcInfo),
859 MRI(FuncInfo.MF->getRegInfo()),
860 MFI(*FuncInfo.MF->getFrameInfo()),
861 MCP(*FuncInfo.MF->getConstantPool()),
862 TM(FuncInfo.MF->getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000863 TD(*TM.getTargetData()),
864 TII(*TM.getInstrInfo()),
Dan Gohmana7a0ed72010-05-05 23:58:35 +0000865 TLI(*TM.getTargetLowering()),
Dan Gohmandb497122010-06-18 23:28:01 +0000866 TRI(*TM.getRegisterInfo()),
Dan Gohmana7a0ed72010-05-05 23:58:35 +0000867 IsBottomUp(false) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000868}
869
Dan Gohmane285a742008-08-14 21:51:29 +0000870FastISel::~FastISel() {}
871
Owen Anderson825b72b2009-08-11 20:47:22 +0000872unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000873 unsigned) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000874 return 0;
875}
876
Owen Anderson825b72b2009-08-11 20:47:22 +0000877unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000878 unsigned,
879 unsigned /*Op0*/, bool /*Op0IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000880 return 0;
881}
882
Owen Anderson825b72b2009-08-11 20:47:22 +0000883unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000884 unsigned,
885 unsigned /*Op0*/, bool /*Op0IsKill*/,
886 unsigned /*Op1*/, bool /*Op1IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000887 return 0;
888}
889
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000890unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000891 return 0;
892}
893
Owen Anderson825b72b2009-08-11 20:47:22 +0000894unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohman46510a72010-04-15 01:51:59 +0000895 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000896 return 0;
897}
898
Owen Anderson825b72b2009-08-11 20:47:22 +0000899unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000900 unsigned,
901 unsigned /*Op0*/, bool /*Op0IsKill*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000902 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000903 return 0;
904}
905
Owen Anderson825b72b2009-08-11 20:47:22 +0000906unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000907 unsigned,
908 unsigned /*Op0*/, bool /*Op0IsKill*/,
Dan Gohman46510a72010-04-15 01:51:59 +0000909 const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000910 return 0;
911}
912
Owen Anderson825b72b2009-08-11 20:47:22 +0000913unsigned FastISel::FastEmit_rri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000914 unsigned,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000915 unsigned /*Op0*/, bool /*Op0IsKill*/,
916 unsigned /*Op1*/, bool /*Op1IsKill*/,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000917 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000918 return 0;
919}
920
921/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
922/// to emit an instruction with an immediate operand using FastEmit_ri.
923/// If that fails, it materializes the immediate into a register and try
924/// FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000925unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000926 unsigned Op0, bool Op0IsKill,
927 uint64_t Imm, MVT ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000928 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000929 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000930 if (ResultReg != 0)
931 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000932 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000933 if (MaterialReg == 0)
934 return 0;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000935 return FastEmit_rr(VT, VT, Opcode,
936 Op0, Op0IsKill,
937 MaterialReg, /*Kill=*/true);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000938}
939
Dan Gohman10df0fa2008-08-27 01:09:54 +0000940/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
941/// to emit an instruction with a floating-point immediate operand using
942/// FastEmit_rf. If that fails, it materializes the immediate into a register
943/// and try FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000944unsigned FastISel::FastEmit_rf_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000945 unsigned Op0, bool Op0IsKill,
946 const ConstantFP *FPImm, MVT ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000947 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000948 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, Op0IsKill, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000949 if (ResultReg != 0)
950 return ResultReg;
951
952 // Materialize the constant in a register.
953 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
954 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000955 // If the target doesn't have a way to directly enter a floating-point
956 // value into a register, use an alternate approach.
957 // TODO: The current approach only supports floating-point constants
958 // that can be constructed by conversion from integer values. This should
959 // be replaced by code that creates a load from a constant-pool entry,
960 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000961 const APFloat &Flt = FPImm->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000962 EVT IntVT = TLI.getPointerTy();
Dan Gohman10df0fa2008-08-27 01:09:54 +0000963
964 uint64_t x[2];
965 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000966 bool isExact;
967 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
968 APFloat::rmTowardZero, &isExact);
969 if (!isExact)
Dan Gohman10df0fa2008-08-27 01:09:54 +0000970 return 0;
971 APInt IntVal(IntBitWidth, 2, x);
972
973 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
974 ISD::Constant, IntVal.getZExtValue());
975 if (IntegerReg == 0)
976 return 0;
977 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000978 ISD::SINT_TO_FP, IntegerReg, /*Kill=*/true);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000979 if (MaterialReg == 0)
980 return 0;
981 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000982 return FastEmit_rr(VT, VT, Opcode,
983 Op0, Op0IsKill,
984 MaterialReg, /*Kill=*/true);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000985}
986
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000987unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
988 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000989}
990
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000991unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000992 const TargetRegisterClass* RC) {
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
Bill Wendling9bc96a52009-02-03 00:55:04 +0000996 BuildMI(MBB, DL, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000997 return ResultReg;
998}
999
1000unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
1001 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001002 unsigned Op0, bool Op0IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001003 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +00001004 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001005
Evan Cheng5960e4e2008-09-08 08:38:20 +00001006 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001007 BuildMI(MBB, DL, II, ResultReg).addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001008 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001009 BuildMI(MBB, DL, II).addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001010 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001011 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001012 if (!InsertedCopy)
1013 ResultReg = 0;
1014 }
1015
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001016 return ResultReg;
1017}
1018
1019unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
1020 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001021 unsigned Op0, bool Op0IsKill,
1022 unsigned Op1, bool Op1IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001023 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +00001024 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001025
Evan Cheng5960e4e2008-09-08 08:38:20 +00001026 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001027 BuildMI(MBB, DL, II, ResultReg)
1028 .addReg(Op0, Op0IsKill * RegState::Kill)
1029 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001030 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001031 BuildMI(MBB, DL, II)
1032 .addReg(Op0, Op0IsKill * RegState::Kill)
1033 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001034 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001035 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001036 if (!InsertedCopy)
1037 ResultReg = 0;
1038 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001039 return ResultReg;
1040}
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001041
1042unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1043 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001044 unsigned Op0, bool Op0IsKill,
1045 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001046 unsigned ResultReg = createResultReg(RC);
1047 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1048
Evan Cheng5960e4e2008-09-08 08:38:20 +00001049 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001050 BuildMI(MBB, DL, II, ResultReg)
1051 .addReg(Op0, Op0IsKill * RegState::Kill)
1052 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001053 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001054 BuildMI(MBB, DL, II)
1055 .addReg(Op0, Op0IsKill * RegState::Kill)
1056 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001057 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001058 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001059 if (!InsertedCopy)
1060 ResultReg = 0;
1061 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001062 return ResultReg;
1063}
1064
Dan Gohman10df0fa2008-08-27 01:09:54 +00001065unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
1066 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001067 unsigned Op0, bool Op0IsKill,
1068 const ConstantFP *FPImm) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001069 unsigned ResultReg = createResultReg(RC);
1070 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1071
Evan Cheng5960e4e2008-09-08 08:38:20 +00001072 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001073 BuildMI(MBB, DL, II, ResultReg)
1074 .addReg(Op0, Op0IsKill * RegState::Kill)
1075 .addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001076 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001077 BuildMI(MBB, DL, II)
1078 .addReg(Op0, Op0IsKill * RegState::Kill)
1079 .addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001080 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001081 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001082 if (!InsertedCopy)
1083 ResultReg = 0;
1084 }
Dan Gohman10df0fa2008-08-27 01:09:54 +00001085 return ResultReg;
1086}
1087
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001088unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
1089 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001090 unsigned Op0, bool Op0IsKill,
1091 unsigned Op1, bool Op1IsKill,
1092 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001093 unsigned ResultReg = createResultReg(RC);
1094 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1095
Evan Cheng5960e4e2008-09-08 08:38:20 +00001096 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001097 BuildMI(MBB, DL, II, ResultReg)
1098 .addReg(Op0, Op0IsKill * RegState::Kill)
1099 .addReg(Op1, Op1IsKill * RegState::Kill)
1100 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001101 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001102 BuildMI(MBB, DL, II)
1103 .addReg(Op0, Op0IsKill * RegState::Kill)
1104 .addReg(Op1, Op1IsKill * RegState::Kill)
1105 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001106 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001107 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001108 if (!InsertedCopy)
1109 ResultReg = 0;
1110 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001111 return ResultReg;
1112}
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001113
1114unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1115 const TargetRegisterClass *RC,
1116 uint64_t Imm) {
1117 unsigned ResultReg = createResultReg(RC);
1118 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1119
Evan Cheng5960e4e2008-09-08 08:38:20 +00001120 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +00001121 BuildMI(MBB, DL, II, ResultReg).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001122 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +00001123 BuildMI(MBB, DL, II).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001124 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001125 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001126 if (!InsertedCopy)
1127 ResultReg = 0;
1128 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001129 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +00001130}
Owen Anderson8970f002008-08-27 22:30:02 +00001131
Owen Anderson825b72b2009-08-11 20:47:22 +00001132unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001133 unsigned Op0, bool Op0IsKill,
1134 uint32_t Idx) {
Owen Anderson40a468f2008-08-28 17:47:37 +00001135 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +00001136
Evan Cheng536ab132009-01-22 09:10:11 +00001137 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Chris Lattner518bb532010-02-09 19:54:29 +00001138 const TargetInstrDesc &II = TII.get(TargetOpcode::EXTRACT_SUBREG);
Owen Anderson8970f002008-08-27 22:30:02 +00001139
Evan Cheng5960e4e2008-09-08 08:38:20 +00001140 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001141 BuildMI(MBB, DL, II, ResultReg)
1142 .addReg(Op0, Op0IsKill * RegState::Kill)
1143 .addImm(Idx);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001144 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001145 BuildMI(MBB, DL, II)
1146 .addReg(Op0, Op0IsKill * RegState::Kill)
1147 .addImm(Idx);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001148 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001149 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001150 if (!InsertedCopy)
1151 ResultReg = 0;
1152 }
Owen Anderson8970f002008-08-27 22:30:02 +00001153 return ResultReg;
1154}
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001155
1156/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1157/// with all but the least significant bit set to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001158unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1159 return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001160}
Dan Gohmanf81eca02010-04-22 20:46:50 +00001161
1162/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1163/// Emit code to ensure constants are copied into registers when needed.
1164/// Remember the virtual registers that need to be added to the Machine PHI
1165/// nodes as input. We cannot just directly add them, because expansion
1166/// might result in multiple MBB's for one BB. As such, the start of the
1167/// BB might correspond to a different MBB than the end.
1168bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1169 const TerminatorInst *TI = LLVMBB->getTerminator();
1170
1171 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohmana4160c32010-07-07 16:29:44 +00001172 unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001173
1174 // Check successor nodes' PHI nodes that expect a constant to be available
1175 // from this block.
1176 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1177 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1178 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmana4160c32010-07-07 16:29:44 +00001179 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Dan Gohmanf81eca02010-04-22 20:46:50 +00001180
1181 // If this terminator has multiple identical successors (common for
1182 // switches), only handle each succ once.
1183 if (!SuccsHandled.insert(SuccMBB)) continue;
1184
1185 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1186
1187 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1188 // nodes and Machine PHI nodes, but the incoming operands have not been
1189 // emitted yet.
1190 for (BasicBlock::const_iterator I = SuccBB->begin();
1191 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanfb95f892010-05-07 01:10:20 +00001192
Dan Gohmanf81eca02010-04-22 20:46:50 +00001193 // Ignore dead phi's.
1194 if (PN->use_empty()) continue;
1195
1196 // Only handle legal types. Two interesting things to note here. First,
1197 // by bailing out early, we may leave behind some dead instructions,
1198 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
1199 // own moves. Second, this check is necessary becuase FastISel doesn't
Dan Gohman89496d02010-07-02 00:10:16 +00001200 // use CreateRegs to create registers, so it always creates
Dan Gohmanf81eca02010-04-22 20:46:50 +00001201 // exactly one register for each non-void instruction.
1202 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1203 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
1204 // Promote MVT::i1.
1205 if (VT == MVT::i1)
1206 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1207 else {
Dan Gohmana4160c32010-07-07 16:29:44 +00001208 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001209 return false;
1210 }
1211 }
1212
1213 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1214
Dan Gohmanfb95f892010-05-07 01:10:20 +00001215 // Set the DebugLoc for the copy. Prefer the location of the operand
1216 // if there is one; use the location of the PHI otherwise.
1217 DL = PN->getDebugLoc();
1218 if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
1219 DL = Inst->getDebugLoc();
1220
Dan Gohmanf81eca02010-04-22 20:46:50 +00001221 unsigned Reg = getRegForValue(PHIOp);
1222 if (Reg == 0) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001223 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001224 return false;
1225 }
Dan Gohmana4160c32010-07-07 16:29:44 +00001226 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohmanfb95f892010-05-07 01:10:20 +00001227 DL = DebugLoc();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001228 }
1229 }
1230
1231 return true;
1232}