blob: 253ceca15ebb4c5decba61f0cec37d172494043c [file] [log] [blame]
Dan Gohman3b172f12010-04-22 20:06:42 +00001//===-- FastISel.cpp - Implementation of the FastISel class ---------------===//
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the implementation of the FastISel class.
11//
Dan Gohman5ec9efd2008-09-30 20:48:29 +000012// "Fast" instruction selection is designed to emit very poor code quickly.
13// Also, it is not designed to be able to do much lowering, so most illegal
Chris Lattner44d2a982008-10-13 01:59:13 +000014// types (e.g. i64 on 32-bit targets) and operations are not supported. It is
15// also not intended to be able to do much optimization, except in a few cases
16// where doing optimizations reduces overall compile time. For example, folding
17// constants into immediate fields is often done, because it's cheap and it
18// reduces the number of instructions later phases have to examine.
Dan Gohman5ec9efd2008-09-30 20:48:29 +000019//
20// "Fast" instruction selection is able to fail gracefully and transfer
21// control to the SelectionDAG selector for operations that it doesn't
Chris Lattner44d2a982008-10-13 01:59:13 +000022// support. In many cases, this allows us to avoid duplicating a lot of
Dan Gohman5ec9efd2008-09-30 20:48:29 +000023// the complicated lowering logic that SelectionDAG currently has.
24//
25// The intended use for "fast" instruction selection is "-O0" mode
26// compilation, where the quality of the generated code is irrelevant when
Chris Lattner44d2a982008-10-13 01:59:13 +000027// weighed against the speed at which the code can be generated. Also,
Dan Gohman5ec9efd2008-09-30 20:48:29 +000028// at -O0, the LLVM optimizers are not running, and this makes the
29// compile time of codegen a much higher portion of the overall compile
Chris Lattner44d2a982008-10-13 01:59:13 +000030// time. Despite its limitations, "fast" instruction selection is able to
Dan Gohman5ec9efd2008-09-30 20:48:29 +000031// handle enough code on its own to provide noticeable overall speedups
32// in -O0 compiles.
33//
34// Basic operations are supported in a target-independent way, by reading
35// the same instruction descriptions that the SelectionDAG selector reads,
36// and identifying simple arithmetic operations that can be directly selected
Chris Lattner44d2a982008-10-13 01:59:13 +000037// from simple operators. More complicated operations currently require
Dan Gohman5ec9efd2008-09-30 20:48:29 +000038// target-specific code.
39//
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000040//===----------------------------------------------------------------------===//
41
Dan Gohman33134c42008-09-25 17:05:24 +000042#include "llvm/Function.h"
43#include "llvm/GlobalVariable.h"
Dan Gohman6f2766d2008-08-19 22:31:46 +000044#include "llvm/Instructions.h"
Dan Gohman33134c42008-09-25 17:05:24 +000045#include "llvm/IntrinsicInst.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000046#include "llvm/CodeGen/FastISel.h"
47#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman33134c42008-09-25 17:05:24 +000048#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000049#include "llvm/CodeGen/MachineRegisterInfo.h"
Devang Patel83489bb2009-01-13 00:35:13 +000050#include "llvm/Analysis/DebugInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000051#include "llvm/Target/TargetData.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000052#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000053#include "llvm/Target/TargetLowering.h"
Dan Gohmanbb466332008-08-20 21:05:57 +000054#include "llvm/Target/TargetMachine.h"
Dan Gohmanba5be5c2010-04-20 15:00:41 +000055#include "llvm/Support/ErrorHandling.h"
Dan Gohman66336ed2009-11-23 17:42:46 +000056#include "FunctionLoweringInfo.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000057using namespace llvm;
58
Dan Gohmana6cb6412010-05-11 23:54:07 +000059bool FastISel::hasTrivialKill(const Value *V) const {
Dan Gohmane1308d82010-05-13 19:19:32 +000060 // Don't consider constants or arguments to have trivial kills. Only
61 // instructions with a single use in the same basic block.
Dan Gohmana6cb6412010-05-11 23:54:07 +000062 const Instruction *I = dyn_cast<Instruction>(V);
Dan Gohmane1308d82010-05-13 19:19:32 +000063 return I &&
64 I->hasOneUse() &&
65 cast<Instruction>(I->use_begin())->getParent() == I->getParent();
Dan Gohmana6cb6412010-05-11 23:54:07 +000066}
67
Dan Gohman46510a72010-04-15 01:51:59 +000068unsigned FastISel::getRegForValue(const Value *V) {
Owen Andersone50ed302009-08-10 22:56:29 +000069 EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
Dan Gohman4fd55282009-04-07 20:40:11 +000070 // Don't handle non-simple values in FastISel.
71 if (!RealVT.isSimple())
72 return 0;
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +000073
74 // Ignore illegal types. We must do this before looking up the value
75 // in ValueMap because Arguments are given virtual registers regardless
76 // of whether FastISel can handle them.
Owen Anderson825b72b2009-08-11 20:47:22 +000077 MVT VT = RealVT.getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +000078 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +000079 // Promote MVT::i1 to a legal type though, because it's common and easy.
80 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +000081 VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +000082 else
83 return 0;
84 }
85
Dan Gohman104e4ce2008-09-03 23:32:19 +000086 // Look up the value to see if we already have a register for it. We
87 // cache values defined by Instructions across blocks, and other values
88 // only locally. This is because Instructions already have the SSA
Dan Gohman5c9cf192010-01-12 04:30:26 +000089 // def-dominates-use requirement enforced.
Owen Anderson99aaf102008-09-03 17:37:03 +000090 if (ValueMap.count(V))
91 return ValueMap[V];
Dan Gohman104e4ce2008-09-03 23:32:19 +000092 unsigned Reg = LocalValueMap[V];
93 if (Reg != 0)
94 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +000095
Dan Gohman97c94b82010-05-06 00:02:14 +000096 // In bottom-up mode, just create the virtual register which will be used
97 // to hold the value. It will be materialized later.
98 if (IsBottomUp) {
99 Reg = createResultReg(TLI.getRegClassFor(VT));
100 if (isa<Instruction>(V))
101 ValueMap[V] = Reg;
102 else
103 LocalValueMap[V] = Reg;
104 return Reg;
105 }
106
Dan Gohman1fdc6142010-05-03 23:36:34 +0000107 return materializeRegForValue(V, VT);
108}
109
110/// materializeRegForValue - Helper for getRegForVale. This function is
111/// called when the value isn't already available in a register and must
112/// be materialized with new instructions.
113unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
114 unsigned Reg = 0;
115
Dan Gohman46510a72010-04-15 01:51:59 +0000116 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000117 if (CI->getValue().getActiveBits() <= 64)
118 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +0000119 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000120 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +0000121 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman1e9e8c32008-10-07 22:03:27 +0000122 // Translate this as an integer zero so that it can be
123 // local-CSE'd with actual integer zeros.
Owen Anderson1d0be152009-08-13 21:58:54 +0000124 Reg =
125 getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
Dan Gohman46510a72010-04-15 01:51:59 +0000126 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman4183e312010-04-13 17:07:06 +0000127 // Try to emit the constant directly.
Dan Gohman104e4ce2008-09-03 23:32:19 +0000128 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000129
130 if (!Reg) {
Dan Gohman4183e312010-04-13 17:07:06 +0000131 // Try to emit the constant by using an integer constant with a cast.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000132 const APFloat &Flt = CF->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000133 EVT IntVT = TLI.getPointerTy();
Dan Gohmanad368ac2008-08-27 18:10:19 +0000134
135 uint64_t x[2];
136 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000137 bool isExact;
138 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
139 APFloat::rmTowardZero, &isExact);
140 if (isExact) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000141 APInt IntVal(IntBitWidth, 2, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000142
Owen Andersone922c022009-07-22 00:24:57 +0000143 unsigned IntegerReg =
Owen Andersoneed707b2009-07-24 23:12:02 +0000144 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000145 if (IntegerReg != 0)
Dan Gohmana6cb6412010-05-11 23:54:07 +0000146 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
147 IntegerReg, /*Kill=*/false);
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000148 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000149 }
Dan Gohman46510a72010-04-15 01:51:59 +0000150 } else if (const Operator *Op = dyn_cast<Operator>(V)) {
Dan Gohman32acbc12010-04-14 02:33:23 +0000151 if (!SelectOperator(Op, Op->getOpcode())) return 0;
152 Reg = LocalValueMap[Op];
Dan Gohman205d9252008-08-28 21:19:07 +0000153 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000154 Reg = createResultReg(TLI.getRegClassFor(VT));
Chris Lattner518bb532010-02-09 19:54:29 +0000155 BuildMI(MBB, DL, TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000156 }
Owen Andersond5d81a42008-09-03 17:51:57 +0000157
Dan Gohmandceffe62008-09-25 01:28:51 +0000158 // If target-independent code couldn't handle the value, give target-specific
159 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000160 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000161 Reg = TargetMaterializeConstant(cast<Constant>(V));
Owen Anderson6e607452008-09-05 23:36:01 +0000162
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000163 // Don't cache constant materializations in the general ValueMap.
164 // To do so would require tracking what uses they dominate.
Dan Gohmandceffe62008-09-25 01:28:51 +0000165 if (Reg != 0)
166 LocalValueMap[V] = Reg;
Dan Gohman104e4ce2008-09-03 23:32:19 +0000167 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000168}
169
Dan Gohman46510a72010-04-15 01:51:59 +0000170unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng59fbc802008-09-09 01:26:59 +0000171 // Look up the value to see if we already have a register for it. We
172 // cache values defined by Instructions across blocks, and other values
173 // only locally. This is because Instructions already have the SSA
Dan Gohman1fdc6142010-05-03 23:36:34 +0000174 // def-dominates-use requirement enforced.
Evan Cheng59fbc802008-09-09 01:26:59 +0000175 if (ValueMap.count(V))
176 return ValueMap[V];
177 return LocalValueMap[V];
178}
179
Owen Andersoncc54e762008-08-30 00:38:46 +0000180/// UpdateValueMap - Update the value map to include the new mapping for this
181/// instruction, or insert an extra copy to get the result in a previous
182/// determined register.
183/// NOTE: This is only necessary because we might select a block that uses
184/// a value before we select the block that defines the value. It might be
185/// possible to fix this by selecting blocks in reverse postorder.
Dan Gohman46510a72010-04-15 01:51:59 +0000186unsigned FastISel::UpdateValueMap(const Value *I, unsigned Reg) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000187 if (!isa<Instruction>(I)) {
188 LocalValueMap[I] = Reg;
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000189 return Reg;
Dan Gohman40b189e2008-09-05 18:18:20 +0000190 }
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000191
192 unsigned &AssignedReg = ValueMap[I];
193 if (AssignedReg == 0)
194 AssignedReg = Reg;
Chris Lattner36e39462009-04-12 07:46:30 +0000195 else if (Reg != AssignedReg) {
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000196 const TargetRegisterClass *RegClass = MRI.getRegClass(Reg);
197 TII.copyRegToReg(*MBB, MBB->end(), AssignedReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000198 Reg, RegClass, RegClass, DL);
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000199 }
200 return AssignedReg;
Owen Andersoncc54e762008-08-30 00:38:46 +0000201}
202
Dan Gohmana6cb6412010-05-11 23:54:07 +0000203std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000204 unsigned IdxN = getRegForValue(Idx);
205 if (IdxN == 0)
206 // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000207 return std::pair<unsigned, bool>(0, false);
208
209 bool IdxNIsKill = hasTrivialKill(Idx);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000210
211 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Anderson766b5ef2009-08-11 21:59:30 +0000212 MVT PtrVT = TLI.getPointerTy();
Owen Andersone50ed302009-08-10 22:56:29 +0000213 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000214 if (IdxVT.bitsLT(PtrVT)) {
215 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND,
216 IdxN, IdxNIsKill);
217 IdxNIsKill = true;
218 }
219 else if (IdxVT.bitsGT(PtrVT)) {
220 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE,
221 IdxN, IdxNIsKill);
222 IdxNIsKill = true;
223 }
224 return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000225}
226
Dan Gohmanbdedd442008-08-20 00:11:48 +0000227/// SelectBinaryOp - Select and emit code for a binary operator instruction,
228/// which has an opcode which directly corresponds to the given ISD opcode.
229///
Dan Gohman46510a72010-04-15 01:51:59 +0000230bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000231 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmanbdedd442008-08-20 00:11:48 +0000233 // Unhandled type. Halt "fast" selection and bail.
234 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000235
Dan Gohmanb71fea22008-08-26 20:52:40 +0000236 // We only handle legal types. For example, on x86-32 the instruction
237 // selector contains all of the 64-bit instructions from x86-64,
238 // under the assumption that i64 won't be used if the target doesn't
239 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000240 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000241 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000242 // don't require additional zeroing, which makes them easy.
Owen Anderson825b72b2009-08-11 20:47:22 +0000243 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000244 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
245 ISDOpcode == ISD::XOR))
Owen Anderson23b9b192009-08-12 00:36:31 +0000246 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohman638c6832008-09-05 18:44:22 +0000247 else
248 return false;
249 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000250
Dan Gohman3df24e62008-09-03 23:12:08 +0000251 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000252 if (Op0 == 0)
253 // Unhandled operand. Halt "fast" selection and bail.
254 return false;
255
Dan Gohmana6cb6412010-05-11 23:54:07 +0000256 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
257
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000258 // Check if the second operand is a constant and handle it appropriately.
259 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000260 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000261 ISDOpcode, Op0, Op0IsKill,
262 CI->getZExtValue());
Dan Gohmanad368ac2008-08-27 18:10:19 +0000263 if (ResultReg != 0) {
264 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000265 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000266 return true;
267 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000268 }
269
Dan Gohman10df0fa2008-08-27 01:09:54 +0000270 // Check if the second operand is a constant float.
271 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000272 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000273 ISDOpcode, Op0, Op0IsKill, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000274 if (ResultReg != 0) {
275 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000276 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000277 return true;
278 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000279 }
280
Dan Gohman3df24e62008-09-03 23:12:08 +0000281 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000282 if (Op1 == 0)
283 // Unhandled operand. Halt "fast" selection and bail.
284 return false;
285
Dan Gohmana6cb6412010-05-11 23:54:07 +0000286 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
287
Dan Gohmanad368ac2008-08-27 18:10:19 +0000288 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000289 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000290 ISDOpcode,
291 Op0, Op0IsKill,
292 Op1, Op1IsKill);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000293 if (ResultReg == 0)
294 // Target-specific code wasn't able to find a machine opcode for
295 // the given ISD opcode and type. Halt "fast" selection and bail.
296 return false;
297
Dan Gohman8014e862008-08-20 00:23:20 +0000298 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000299 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000300 return true;
301}
302
Dan Gohman46510a72010-04-15 01:51:59 +0000303bool FastISel::SelectGetElementPtr(const User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000304 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000305 if (N == 0)
306 // Unhandled operand. Halt "fast" selection and bail.
307 return false;
308
Dan Gohmana6cb6412010-05-11 23:54:07 +0000309 bool NIsKill = hasTrivialKill(I->getOperand(0));
310
Evan Cheng83785c82008-08-20 22:45:34 +0000311 const Type *Ty = I->getOperand(0)->getType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000312 MVT VT = TLI.getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +0000313 for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
314 E = I->op_end(); OI != E; ++OI) {
315 const Value *Idx = *OI;
Evan Cheng83785c82008-08-20 22:45:34 +0000316 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
317 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
318 if (Field) {
319 // N = N + Offset
320 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
321 // FIXME: This can be optimized by combining the add with a
322 // subsequent one.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000323 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000324 if (N == 0)
325 // Unhandled operand. Halt "fast" selection and bail.
326 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000327 NIsKill = true;
Evan Cheng83785c82008-08-20 22:45:34 +0000328 }
329 Ty = StTy->getElementType(Field);
330 } else {
331 Ty = cast<SequentialType>(Ty)->getElementType();
332
333 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +0000334 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Evan Cheng83785c82008-08-20 22:45:34 +0000335 if (CI->getZExtValue() == 0) continue;
336 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +0000337 TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohmana6cb6412010-05-11 23:54:07 +0000338 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000339 if (N == 0)
340 // Unhandled operand. Halt "fast" selection and bail.
341 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000342 NIsKill = true;
Evan Cheng83785c82008-08-20 22:45:34 +0000343 continue;
344 }
345
346 // N = N + Idx * ElementSize;
Duncan Sands777d2302009-05-09 07:06:46 +0000347 uint64_t ElementSize = TD.getTypeAllocSize(Ty);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000348 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
349 unsigned IdxN = Pair.first;
350 bool IdxNIsKill = Pair.second;
Evan Cheng83785c82008-08-20 22:45:34 +0000351 if (IdxN == 0)
352 // Unhandled operand. Halt "fast" selection and bail.
353 return false;
354
Dan Gohman80bc6e22008-08-26 20:57:08 +0000355 if (ElementSize != 1) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000356 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000357 if (IdxN == 0)
358 // Unhandled operand. Halt "fast" selection and bail.
359 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000360 IdxNIsKill = true;
Dan Gohman80bc6e22008-08-26 20:57:08 +0000361 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000362 N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
Evan Cheng83785c82008-08-20 22:45:34 +0000363 if (N == 0)
364 // Unhandled operand. Halt "fast" selection and bail.
365 return false;
366 }
367 }
368
369 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000370 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000371 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000372}
373
Dan Gohman46510a72010-04-15 01:51:59 +0000374bool FastISel::SelectCall(const User *I) {
375 const Function *F = cast<CallInst>(I)->getCalledFunction();
Dan Gohman33134c42008-09-25 17:05:24 +0000376 if (!F) return false;
377
Dan Gohman4183e312010-04-13 17:07:06 +0000378 // Handle selected intrinsic function calls.
Dan Gohman33134c42008-09-25 17:05:24 +0000379 unsigned IID = F->getIntrinsicID();
380 switch (IID) {
381 default: break;
Bill Wendling92c1e122009-02-13 02:16:35 +0000382 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +0000383 const DbgDeclareInst *DI = cast<DbgDeclareInst>(I);
Devang Patel02f0dbd2010-05-07 22:04:20 +0000384 if (!DIVariable(DI->getVariable()).Verify() ||
Chris Lattnered3a8062010-04-05 06:05:26 +0000385 !MF.getMMI().hasDebugInfo())
Devang Patel7e1e31f2009-07-02 22:43:26 +0000386 return true;
387
Dan Gohman46510a72010-04-15 01:51:59 +0000388 const Value *Address = DI->getAddress();
Dale Johannesendc918562010-02-06 02:26:02 +0000389 if (!Address)
390 return true;
Dale Johannesen343b42e2010-04-07 01:15:14 +0000391 if (isa<UndefValue>(Address))
392 return true;
Dan Gohman46510a72010-04-15 01:51:59 +0000393 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
Devang Patel7e1e31f2009-07-02 22:43:26 +0000394 // Don't handle byval struct arguments or VLAs, for example.
Dale Johannesen7dc78402010-04-25 21:03:54 +0000395 // Note that if we have a byval struct argument, fast ISel is turned off;
396 // those are handled in SelectionDAGBuilder.
Devang Patel54fc4d62010-04-28 19:27:33 +0000397 if (AI) {
398 DenseMap<const AllocaInst*, int>::iterator SI =
399 StaticAllocaMap.find(AI);
400 if (SI == StaticAllocaMap.end()) break; // VLAs.
401 int FI = SI->second;
402 if (!DI->getDebugLoc().isUnknown())
403 MF.getMMI().setVariableDbgInfo(DI->getVariable(), FI, DI->getDebugLoc());
404 } else
405 // Building the map above is target independent. Generating DBG_VALUE
406 // inline is target dependent; do this now.
407 (void)TargetSelectInstruction(cast<Instruction>(I));
Dan Gohman33134c42008-09-25 17:05:24 +0000408 return true;
Bill Wendling92c1e122009-02-13 02:16:35 +0000409 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000410 case Intrinsic::dbg_value: {
Dale Johannesen343b42e2010-04-07 01:15:14 +0000411 // This form of DBG_VALUE is target-independent.
Dan Gohman46510a72010-04-15 01:51:59 +0000412 const DbgValueInst *DI = cast<DbgValueInst>(I);
Dale Johannesen45df7612010-02-26 20:01:55 +0000413 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohman46510a72010-04-15 01:51:59 +0000414 const Value *V = DI->getValue();
Dale Johannesen45df7612010-02-26 20:01:55 +0000415 if (!V) {
416 // Currently the optimizer can produce this; insert an undef to
417 // help debugging. Probably the optimizer should not do this.
418 BuildMI(MBB, DL, II).addReg(0U).addImm(DI->getOffset()).
419 addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000420 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dale Johannesen45df7612010-02-26 20:01:55 +0000421 BuildMI(MBB, DL, II).addImm(CI->getZExtValue()).addImm(DI->getOffset()).
422 addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000423 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dale Johannesen45df7612010-02-26 20:01:55 +0000424 BuildMI(MBB, DL, II).addFPImm(CF).addImm(DI->getOffset()).
425 addMetadata(DI->getVariable());
426 } else if (unsigned Reg = lookUpRegForValue(V)) {
427 BuildMI(MBB, DL, II).addReg(Reg, RegState::Debug).addImm(DI->getOffset()).
428 addMetadata(DI->getVariable());
429 } else {
430 // We can't yet handle anything else here because it would require
431 // generating code, thus altering codegen because of debug info.
432 // Insert an undef so we can see what we dropped.
433 BuildMI(MBB, DL, II).addReg(0U).addImm(DI->getOffset()).
434 addMetadata(DI->getVariable());
435 }
436 return true;
437 }
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000438 case Intrinsic::eh_exception: {
Owen Andersone50ed302009-08-10 22:56:29 +0000439 EVT VT = TLI.getValueType(I->getType());
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000440 switch (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)) {
441 default: break;
442 case TargetLowering::Expand: {
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000443 assert(MBB->isLandingPad() && "Call to eh.exception not in landing pad!");
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000444 unsigned Reg = TLI.getExceptionAddressRegister();
445 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
446 unsigned ResultReg = createResultReg(RC);
447 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000448 Reg, RC, RC, DL);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000449 assert(InsertedCopy && "Can't copy address registers!");
Evan Cheng24ac4082008-11-24 07:09:49 +0000450 InsertedCopy = InsertedCopy;
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000451 UpdateValueMap(I, ResultReg);
452 return true;
453 }
454 }
455 break;
456 }
Duncan Sandsb01bbdc2009-10-14 16:11:37 +0000457 case Intrinsic::eh_selector: {
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::EHSELECTION, VT)) {
460 default: break;
461 case TargetLowering::Expand: {
Chris Lattnered3a8062010-04-05 06:05:26 +0000462 if (MBB->isLandingPad())
463 AddCatchInfo(*cast<CallInst>(I), &MF.getMMI(), MBB);
464 else {
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000465#ifndef NDEBUG
Chris Lattnered3a8062010-04-05 06:05:26 +0000466 CatchInfoLost.insert(cast<CallInst>(I));
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000467#endif
Chris Lattnered3a8062010-04-05 06:05:26 +0000468 // FIXME: Mark exception selector register as live in. Hack for PR1508.
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000469 unsigned Reg = TLI.getExceptionSelectorRegister();
Chris Lattnered3a8062010-04-05 06:05:26 +0000470 if (Reg) MBB->addLiveIn(Reg);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000471 }
Chris Lattnered3a8062010-04-05 06:05:26 +0000472
473 unsigned Reg = TLI.getExceptionSelectorRegister();
474 EVT SrcVT = TLI.getPointerTy();
475 const TargetRegisterClass *RC = TLI.getRegClassFor(SrcVT);
476 unsigned ResultReg = createResultReg(RC);
477 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, Reg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000478 RC, RC, DL);
Chris Lattnered3a8062010-04-05 06:05:26 +0000479 assert(InsertedCopy && "Can't copy address registers!");
480 InsertedCopy = InsertedCopy;
481
Dan Gohmana6cb6412010-05-11 23:54:07 +0000482 bool ResultRegIsKill = hasTrivialKill(I);
483
Chris Lattnered3a8062010-04-05 06:05:26 +0000484 // Cast the register to the type of the selector.
485 if (SrcVT.bitsGT(MVT::i32))
486 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, ISD::TRUNCATE,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000487 ResultReg, ResultRegIsKill);
Chris Lattnered3a8062010-04-05 06:05:26 +0000488 else if (SrcVT.bitsLT(MVT::i32))
489 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000490 ISD::SIGN_EXTEND, ResultReg, ResultRegIsKill);
Chris Lattnered3a8062010-04-05 06:05:26 +0000491 if (ResultReg == 0)
492 // Unhandled operand. Halt "fast" selection and bail.
493 return false;
494
495 UpdateValueMap(I, ResultReg);
496
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000497 return true;
498 }
499 }
500 break;
501 }
Dan Gohman33134c42008-09-25 17:05:24 +0000502 }
Dan Gohman4183e312010-04-13 17:07:06 +0000503
504 // An arbitrary call. Bail.
Dan Gohman33134c42008-09-25 17:05:24 +0000505 return false;
506}
507
Dan Gohman46510a72010-04-15 01:51:59 +0000508bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000509 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
510 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000511
Owen Anderson825b72b2009-08-11 20:47:22 +0000512 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
513 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersond0533c92008-08-26 23:46:32 +0000514 // Unhandled type. Halt "fast" selection and bail.
515 return false;
516
Dan Gohman474d3b32009-03-13 23:53:06 +0000517 // Check if the destination type is legal. Or as a special case,
518 // it may be i1 if we're doing a truncate because that's
519 // easy and somewhat common.
520 if (!TLI.isTypeLegal(DstVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000521 if (DstVT != MVT::i1 || Opcode != ISD::TRUNCATE)
Dan Gohman91b6f972008-10-03 01:28:47 +0000522 // Unhandled type. Halt "fast" selection and bail.
523 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000524
525 // Check if the source operand is legal. Or as a special case,
526 // it may be i1 if we're doing zero-extension because that's
527 // easy and somewhat common.
528 if (!TLI.isTypeLegal(SrcVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000529 if (SrcVT != MVT::i1 || Opcode != ISD::ZERO_EXTEND)
Dan Gohman474d3b32009-03-13 23:53:06 +0000530 // Unhandled type. Halt "fast" selection and bail.
531 return false;
532
Dan Gohman3df24e62008-09-03 23:12:08 +0000533 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000534 if (!InputReg)
535 // Unhandled operand. Halt "fast" selection and bail.
536 return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000537
Dan Gohmana6cb6412010-05-11 23:54:07 +0000538 bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
539
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000540 // If the operand is i1, arrange for the high bits in the register to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 if (SrcVT == MVT::i1) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000542 SrcVT = TLI.getTypeToTransformTo(I->getContext(), SrcVT);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000543 InputReg = FastEmitZExtFromI1(SrcVT.getSimpleVT(), InputReg, InputRegIsKill);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000544 if (!InputReg)
545 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000546 InputRegIsKill = true;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000547 }
Dan Gohman474d3b32009-03-13 23:53:06 +0000548 // If the result is i1, truncate to the target's type for i1 first.
Owen Anderson825b72b2009-08-11 20:47:22 +0000549 if (DstVT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +0000550 DstVT = TLI.getTypeToTransformTo(I->getContext(), DstVT);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000551
Owen Andersond0533c92008-08-26 23:46:32 +0000552 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
553 DstVT.getSimpleVT(),
554 Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000555 InputReg, InputRegIsKill);
Owen Andersond0533c92008-08-26 23:46:32 +0000556 if (!ResultReg)
557 return false;
558
Dan Gohman3df24e62008-09-03 23:12:08 +0000559 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000560 return true;
561}
562
Dan Gohman46510a72010-04-15 01:51:59 +0000563bool FastISel::SelectBitCast(const User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000564 // If the bitcast doesn't change the type, just use the operand value.
565 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000566 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000567 if (Reg == 0)
568 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000569 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000570 return true;
571 }
572
573 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Andersone50ed302009-08-10 22:56:29 +0000574 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
575 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000576
Owen Anderson825b72b2009-08-11 20:47:22 +0000577 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
578 DstVT == MVT::Other || !DstVT.isSimple() ||
Owen Andersond0533c92008-08-26 23:46:32 +0000579 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
580 // Unhandled type. Halt "fast" selection and bail.
581 return false;
582
Dan Gohman3df24e62008-09-03 23:12:08 +0000583 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000584 if (Op0 == 0)
585 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000586 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000587
588 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000589
Dan Gohmanad368ac2008-08-27 18:10:19 +0000590 // First, try to perform the bitcast by inserting a reg-reg copy.
591 unsigned ResultReg = 0;
592 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
593 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
594 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
595 ResultReg = createResultReg(DstClass);
596
597 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000598 Op0, DstClass, SrcClass, DL);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000599 if (!InsertedCopy)
600 ResultReg = 0;
601 }
602
603 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
604 if (!ResultReg)
605 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000606 ISD::BIT_CONVERT, Op0, Op0IsKill);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000607
608 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000609 return false;
610
Dan Gohman3df24e62008-09-03 23:12:08 +0000611 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000612 return true;
613}
614
Dan Gohman3df24e62008-09-03 23:12:08 +0000615bool
Dan Gohman46510a72010-04-15 01:51:59 +0000616FastISel::SelectInstruction(const Instruction *I) {
Dan Gohmane8c92dd2010-04-23 15:29:50 +0000617 // Just before the terminator instruction, insert instructions to
618 // feed PHI nodes in successor blocks.
619 if (isa<TerminatorInst>(I))
620 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
621 return false;
622
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000623 DL = I->getDebugLoc();
624
Dan Gohman6e3ff372009-12-05 01:27:58 +0000625 // First, try doing target-independent selection.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000626 if (SelectOperator(I, I->getOpcode())) {
627 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000628 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000629 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000630
631 // Next, try calling the target to attempt to handle the instruction.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000632 if (TargetSelectInstruction(I)) {
633 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000634 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000635 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000636
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000637 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000638 return false;
Dan Gohman40b189e2008-09-05 18:18:20 +0000639}
640
Dan Gohmand98d6202008-10-02 22:15:21 +0000641/// FastEmitBranch - Emit an unconditional branch to the given block,
642/// unless it is the immediate (fall-through) successor, and update
643/// the CFG.
644void
645FastISel::FastEmitBranch(MachineBasicBlock *MSucc) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000646 if (MBB->isLayoutSuccessor(MSucc)) {
647 // The unconditional fall-through case, which needs no instructions.
648 } else {
649 // The unconditional branch case.
650 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
651 }
652 MBB->addSuccessor(MSucc);
653}
654
Dan Gohman3d45a852009-09-03 22:53:57 +0000655/// SelectFNeg - Emit an FNeg operation.
656///
657bool
Dan Gohman46510a72010-04-15 01:51:59 +0000658FastISel::SelectFNeg(const User *I) {
Dan Gohman3d45a852009-09-03 22:53:57 +0000659 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
660 if (OpReg == 0) return false;
661
Dan Gohmana6cb6412010-05-11 23:54:07 +0000662 bool OpRegIsKill = hasTrivialKill(I);
663
Dan Gohman4a215a12009-09-11 00:36:43 +0000664 // If the target has ISD::FNEG, use it.
665 EVT VT = TLI.getValueType(I->getType());
666 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000667 ISD::FNEG, OpReg, OpRegIsKill);
Dan Gohman4a215a12009-09-11 00:36:43 +0000668 if (ResultReg != 0) {
669 UpdateValueMap(I, ResultReg);
670 return true;
671 }
672
Dan Gohman5e5abb72009-09-11 00:34:46 +0000673 // Bitcast the value to integer, twiddle the sign bit with xor,
674 // and then bitcast it back to floating-point.
Dan Gohman3d45a852009-09-03 22:53:57 +0000675 if (VT.getSizeInBits() > 64) return false;
Dan Gohman5e5abb72009-09-11 00:34:46 +0000676 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
677 if (!TLI.isTypeLegal(IntVT))
678 return false;
679
680 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000681 ISD::BIT_CONVERT, OpReg, OpRegIsKill);
Dan Gohman5e5abb72009-09-11 00:34:46 +0000682 if (IntReg == 0)
683 return false;
684
Dan Gohmana6cb6412010-05-11 23:54:07 +0000685 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
686 IntReg, /*Kill=*/true,
Dan Gohman5e5abb72009-09-11 00:34:46 +0000687 UINT64_C(1) << (VT.getSizeInBits()-1),
688 IntVT.getSimpleVT());
689 if (IntResultReg == 0)
690 return false;
691
692 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000693 ISD::BIT_CONVERT, IntResultReg, /*Kill=*/true);
Dan Gohman3d45a852009-09-03 22:53:57 +0000694 if (ResultReg == 0)
695 return false;
696
697 UpdateValueMap(I, ResultReg);
698 return true;
699}
700
Dan Gohman40b189e2008-09-05 18:18:20 +0000701bool
Dan Gohman46510a72010-04-15 01:51:59 +0000702FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000703 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000704 case Instruction::Add:
705 return SelectBinaryOp(I, ISD::ADD);
706 case Instruction::FAdd:
707 return SelectBinaryOp(I, ISD::FADD);
708 case Instruction::Sub:
709 return SelectBinaryOp(I, ISD::SUB);
710 case Instruction::FSub:
Dan Gohman3d45a852009-09-03 22:53:57 +0000711 // FNeg is currently represented in LLVM IR as a special case of FSub.
712 if (BinaryOperator::isFNeg(I))
713 return SelectFNeg(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000714 return SelectBinaryOp(I, ISD::FSUB);
715 case Instruction::Mul:
716 return SelectBinaryOp(I, ISD::MUL);
717 case Instruction::FMul:
718 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman3df24e62008-09-03 23:12:08 +0000719 case Instruction::SDiv:
720 return SelectBinaryOp(I, ISD::SDIV);
721 case Instruction::UDiv:
722 return SelectBinaryOp(I, ISD::UDIV);
723 case Instruction::FDiv:
724 return SelectBinaryOp(I, ISD::FDIV);
725 case Instruction::SRem:
726 return SelectBinaryOp(I, ISD::SREM);
727 case Instruction::URem:
728 return SelectBinaryOp(I, ISD::UREM);
729 case Instruction::FRem:
730 return SelectBinaryOp(I, ISD::FREM);
731 case Instruction::Shl:
732 return SelectBinaryOp(I, ISD::SHL);
733 case Instruction::LShr:
734 return SelectBinaryOp(I, ISD::SRL);
735 case Instruction::AShr:
736 return SelectBinaryOp(I, ISD::SRA);
737 case Instruction::And:
738 return SelectBinaryOp(I, ISD::AND);
739 case Instruction::Or:
740 return SelectBinaryOp(I, ISD::OR);
741 case Instruction::Xor:
742 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000743
Dan Gohman3df24e62008-09-03 23:12:08 +0000744 case Instruction::GetElementPtr:
745 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000746
Dan Gohman3df24e62008-09-03 23:12:08 +0000747 case Instruction::Br: {
Dan Gohman46510a72010-04-15 01:51:59 +0000748 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000749
Dan Gohman3df24e62008-09-03 23:12:08 +0000750 if (BI->isUnconditional()) {
Dan Gohman46510a72010-04-15 01:51:59 +0000751 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohman3df24e62008-09-03 23:12:08 +0000752 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
Dan Gohmand98d6202008-10-02 22:15:21 +0000753 FastEmitBranch(MSucc);
Dan Gohman3df24e62008-09-03 23:12:08 +0000754 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000755 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000756
757 // Conditional branches are not handed yet.
758 // Halt "fast" selection and bail.
759 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000760 }
761
Dan Gohman087c8502008-09-05 01:08:41 +0000762 case Instruction::Unreachable:
763 // Nothing to emit.
764 return true;
765
Dan Gohman0586d912008-09-10 20:11:02 +0000766 case Instruction::Alloca:
767 // FunctionLowering has the static-sized case covered.
768 if (StaticAllocaMap.count(cast<AllocaInst>(I)))
769 return true;
770
771 // Dynamic-sized alloca is not handled yet.
772 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000773
Dan Gohman33134c42008-09-25 17:05:24 +0000774 case Instruction::Call:
775 return SelectCall(I);
776
Dan Gohman3df24e62008-09-03 23:12:08 +0000777 case Instruction::BitCast:
778 return SelectBitCast(I);
779
780 case Instruction::FPToSI:
781 return SelectCast(I, ISD::FP_TO_SINT);
782 case Instruction::ZExt:
783 return SelectCast(I, ISD::ZERO_EXTEND);
784 case Instruction::SExt:
785 return SelectCast(I, ISD::SIGN_EXTEND);
786 case Instruction::Trunc:
787 return SelectCast(I, ISD::TRUNCATE);
788 case Instruction::SIToFP:
789 return SelectCast(I, ISD::SINT_TO_FP);
790
791 case Instruction::IntToPtr: // Deliberate fall-through.
792 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +0000793 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
794 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman3df24e62008-09-03 23:12:08 +0000795 if (DstVT.bitsGT(SrcVT))
796 return SelectCast(I, ISD::ZERO_EXTEND);
797 if (DstVT.bitsLT(SrcVT))
798 return SelectCast(I, ISD::TRUNCATE);
799 unsigned Reg = getRegForValue(I->getOperand(0));
800 if (Reg == 0) return false;
801 UpdateValueMap(I, Reg);
802 return true;
803 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000804
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000805 case Instruction::PHI:
806 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
807
Dan Gohman3df24e62008-09-03 23:12:08 +0000808 default:
809 // Unhandled instruction. Halt "fast" selection and bail.
810 return false;
811 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000812}
813
Dan Gohman3df24e62008-09-03 23:12:08 +0000814FastISel::FastISel(MachineFunction &mf,
815 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +0000816 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmanf81eca02010-04-22 20:46:50 +0000817 DenseMap<const AllocaInst *, int> &am,
818 std::vector<std::pair<MachineInstr*, unsigned> > &pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000819#ifndef NDEBUG
Dan Gohman25208642010-04-14 19:53:31 +0000820 , SmallSet<const Instruction *, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000821#endif
822 )
Dan Gohman3df24e62008-09-03 23:12:08 +0000823 : MBB(0),
824 ValueMap(vm),
825 MBBMap(bm),
Dan Gohman0586d912008-09-10 20:11:02 +0000826 StaticAllocaMap(am),
Dan Gohmanf81eca02010-04-22 20:46:50 +0000827 PHINodesToUpdate(pn),
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000828#ifndef NDEBUG
829 CatchInfoLost(cil),
830#endif
Dan Gohman3df24e62008-09-03 23:12:08 +0000831 MF(mf),
832 MRI(MF.getRegInfo()),
Dan Gohman0586d912008-09-10 20:11:02 +0000833 MFI(*MF.getFrameInfo()),
834 MCP(*MF.getConstantPool()),
Dan Gohman3df24e62008-09-03 23:12:08 +0000835 TM(MF.getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000836 TD(*TM.getTargetData()),
837 TII(*TM.getInstrInfo()),
Dan Gohmana7a0ed72010-05-05 23:58:35 +0000838 TLI(*TM.getTargetLowering()),
839 IsBottomUp(false) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000840}
841
Dan Gohmane285a742008-08-14 21:51:29 +0000842FastISel::~FastISel() {}
843
Owen Anderson825b72b2009-08-11 20:47:22 +0000844unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000845 unsigned) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000846 return 0;
847}
848
Owen Anderson825b72b2009-08-11 20:47:22 +0000849unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000850 unsigned,
851 unsigned /*Op0*/, bool /*Op0IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000852 return 0;
853}
854
Owen Anderson825b72b2009-08-11 20:47:22 +0000855unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000856 unsigned,
857 unsigned /*Op0*/, bool /*Op0IsKill*/,
858 unsigned /*Op1*/, bool /*Op1IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000859 return 0;
860}
861
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000862unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000863 return 0;
864}
865
Owen Anderson825b72b2009-08-11 20:47:22 +0000866unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohman46510a72010-04-15 01:51:59 +0000867 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000868 return 0;
869}
870
Owen Anderson825b72b2009-08-11 20:47:22 +0000871unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000872 unsigned,
873 unsigned /*Op0*/, bool /*Op0IsKill*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000874 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000875 return 0;
876}
877
Owen Anderson825b72b2009-08-11 20:47:22 +0000878unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000879 unsigned,
880 unsigned /*Op0*/, bool /*Op0IsKill*/,
Dan Gohman46510a72010-04-15 01:51:59 +0000881 const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000882 return 0;
883}
884
Owen Anderson825b72b2009-08-11 20:47:22 +0000885unsigned FastISel::FastEmit_rri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000886 unsigned,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000887 unsigned /*Op0*/, bool /*Op0IsKill*/,
888 unsigned /*Op1*/, bool /*Op1IsKill*/,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000889 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000890 return 0;
891}
892
893/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
894/// to emit an instruction with an immediate operand using FastEmit_ri.
895/// If that fails, it materializes the immediate into a register and try
896/// FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000897unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000898 unsigned Op0, bool Op0IsKill,
899 uint64_t Imm, MVT ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000900 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000901 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000902 if (ResultReg != 0)
903 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000904 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000905 if (MaterialReg == 0)
906 return 0;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000907 return FastEmit_rr(VT, VT, Opcode,
908 Op0, Op0IsKill,
909 MaterialReg, /*Kill=*/true);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000910}
911
Dan Gohman10df0fa2008-08-27 01:09:54 +0000912/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
913/// to emit an instruction with a floating-point immediate operand using
914/// FastEmit_rf. If that fails, it materializes the immediate into a register
915/// and try FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000916unsigned FastISel::FastEmit_rf_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000917 unsigned Op0, bool Op0IsKill,
918 const ConstantFP *FPImm, MVT ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000919 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000920 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, Op0IsKill, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000921 if (ResultReg != 0)
922 return ResultReg;
923
924 // Materialize the constant in a register.
925 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
926 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000927 // If the target doesn't have a way to directly enter a floating-point
928 // value into a register, use an alternate approach.
929 // TODO: The current approach only supports floating-point constants
930 // that can be constructed by conversion from integer values. This should
931 // be replaced by code that creates a load from a constant-pool entry,
932 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000933 const APFloat &Flt = FPImm->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000934 EVT IntVT = TLI.getPointerTy();
Dan Gohman10df0fa2008-08-27 01:09:54 +0000935
936 uint64_t x[2];
937 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000938 bool isExact;
939 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
940 APFloat::rmTowardZero, &isExact);
941 if (!isExact)
Dan Gohman10df0fa2008-08-27 01:09:54 +0000942 return 0;
943 APInt IntVal(IntBitWidth, 2, x);
944
945 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
946 ISD::Constant, IntVal.getZExtValue());
947 if (IntegerReg == 0)
948 return 0;
949 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000950 ISD::SINT_TO_FP, IntegerReg, /*Kill=*/true);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000951 if (MaterialReg == 0)
952 return 0;
953 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000954 return FastEmit_rr(VT, VT, Opcode,
955 Op0, Op0IsKill,
956 MaterialReg, /*Kill=*/true);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000957}
958
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000959unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
960 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000961}
962
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000963unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000964 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000965 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000966 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000967
Bill Wendling9bc96a52009-02-03 00:55:04 +0000968 BuildMI(MBB, DL, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000969 return ResultReg;
970}
971
972unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
973 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000974 unsigned Op0, bool Op0IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000975 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000976 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000977
Evan Cheng5960e4e2008-09-08 08:38:20 +0000978 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +0000979 BuildMI(MBB, DL, II, ResultReg).addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000980 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000981 BuildMI(MBB, DL, II).addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000982 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000983 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +0000984 if (!InsertedCopy)
985 ResultReg = 0;
986 }
987
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000988 return ResultReg;
989}
990
991unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
992 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000993 unsigned Op0, bool Op0IsKill,
994 unsigned Op1, bool Op1IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000995 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000996 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000997
Evan Cheng5960e4e2008-09-08 08:38:20 +0000998 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +0000999 BuildMI(MBB, DL, II, ResultReg)
1000 .addReg(Op0, Op0IsKill * RegState::Kill)
1001 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001002 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001003 BuildMI(MBB, DL, II)
1004 .addReg(Op0, Op0IsKill * RegState::Kill)
1005 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001006 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001007 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001008 if (!InsertedCopy)
1009 ResultReg = 0;
1010 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001011 return ResultReg;
1012}
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001013
1014unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1015 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001016 unsigned Op0, bool Op0IsKill,
1017 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001018 unsigned ResultReg = createResultReg(RC);
1019 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1020
Evan Cheng5960e4e2008-09-08 08:38:20 +00001021 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001022 BuildMI(MBB, DL, II, ResultReg)
1023 .addReg(Op0, Op0IsKill * RegState::Kill)
1024 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001025 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001026 BuildMI(MBB, DL, II)
1027 .addReg(Op0, Op0IsKill * RegState::Kill)
1028 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001029 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001030 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001031 if (!InsertedCopy)
1032 ResultReg = 0;
1033 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001034 return ResultReg;
1035}
1036
Dan Gohman10df0fa2008-08-27 01:09:54 +00001037unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
1038 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001039 unsigned Op0, bool Op0IsKill,
1040 const ConstantFP *FPImm) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001041 unsigned ResultReg = createResultReg(RC);
1042 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1043
Evan Cheng5960e4e2008-09-08 08:38:20 +00001044 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001045 BuildMI(MBB, DL, II, ResultReg)
1046 .addReg(Op0, Op0IsKill * RegState::Kill)
1047 .addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001048 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001049 BuildMI(MBB, DL, II)
1050 .addReg(Op0, Op0IsKill * RegState::Kill)
1051 .addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001052 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001053 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001054 if (!InsertedCopy)
1055 ResultReg = 0;
1056 }
Dan Gohman10df0fa2008-08-27 01:09:54 +00001057 return ResultReg;
1058}
1059
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001060unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
1061 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001062 unsigned Op0, bool Op0IsKill,
1063 unsigned Op1, bool Op1IsKill,
1064 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001065 unsigned ResultReg = createResultReg(RC);
1066 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1067
Evan Cheng5960e4e2008-09-08 08:38:20 +00001068 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001069 BuildMI(MBB, DL, II, ResultReg)
1070 .addReg(Op0, Op0IsKill * RegState::Kill)
1071 .addReg(Op1, Op1IsKill * RegState::Kill)
1072 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001073 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001074 BuildMI(MBB, DL, II)
1075 .addReg(Op0, Op0IsKill * RegState::Kill)
1076 .addReg(Op1, Op1IsKill * RegState::Kill)
1077 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001078 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001079 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001080 if (!InsertedCopy)
1081 ResultReg = 0;
1082 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001083 return ResultReg;
1084}
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001085
1086unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1087 const TargetRegisterClass *RC,
1088 uint64_t Imm) {
1089 unsigned ResultReg = createResultReg(RC);
1090 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1091
Evan Cheng5960e4e2008-09-08 08:38:20 +00001092 if (II.getNumDefs() >= 1)
Bill Wendling9bc96a52009-02-03 00:55:04 +00001093 BuildMI(MBB, DL, II, ResultReg).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001094 else {
Bill Wendling9bc96a52009-02-03 00:55:04 +00001095 BuildMI(MBB, DL, II).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001096 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001097 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001098 if (!InsertedCopy)
1099 ResultReg = 0;
1100 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001101 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +00001102}
Owen Anderson8970f002008-08-27 22:30:02 +00001103
Owen Anderson825b72b2009-08-11 20:47:22 +00001104unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001105 unsigned Op0, bool Op0IsKill,
1106 uint32_t Idx) {
Owen Anderson40a468f2008-08-28 17:47:37 +00001107 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +00001108
Evan Cheng536ab132009-01-22 09:10:11 +00001109 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Chris Lattner518bb532010-02-09 19:54:29 +00001110 const TargetInstrDesc &II = TII.get(TargetOpcode::EXTRACT_SUBREG);
Owen Anderson8970f002008-08-27 22:30:02 +00001111
Evan Cheng5960e4e2008-09-08 08:38:20 +00001112 if (II.getNumDefs() >= 1)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001113 BuildMI(MBB, DL, II, ResultReg)
1114 .addReg(Op0, Op0IsKill * RegState::Kill)
1115 .addImm(Idx);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001116 else {
Dan Gohmana6cb6412010-05-11 23:54:07 +00001117 BuildMI(MBB, DL, II)
1118 .addReg(Op0, Op0IsKill * RegState::Kill)
1119 .addImm(Idx);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001120 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman34dcc6f2010-05-06 20:33:48 +00001121 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001122 if (!InsertedCopy)
1123 ResultReg = 0;
1124 }
Owen Anderson8970f002008-08-27 22:30:02 +00001125 return ResultReg;
1126}
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001127
1128/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1129/// with all but the least significant bit set to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001130unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1131 return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001132}
Dan Gohmanf81eca02010-04-22 20:46:50 +00001133
1134/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1135/// Emit code to ensure constants are copied into registers when needed.
1136/// Remember the virtual registers that need to be added to the Machine PHI
1137/// nodes as input. We cannot just directly add them, because expansion
1138/// might result in multiple MBB's for one BB. As such, the start of the
1139/// BB might correspond to a different MBB than the end.
1140bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1141 const TerminatorInst *TI = LLVMBB->getTerminator();
1142
1143 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
1144 unsigned OrigNumPHINodesToUpdate = PHINodesToUpdate.size();
1145
1146 // Check successor nodes' PHI nodes that expect a constant to be available
1147 // from this block.
1148 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1149 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1150 if (!isa<PHINode>(SuccBB->begin())) continue;
1151 MachineBasicBlock *SuccMBB = MBBMap[SuccBB];
1152
1153 // If this terminator has multiple identical successors (common for
1154 // switches), only handle each succ once.
1155 if (!SuccsHandled.insert(SuccMBB)) continue;
1156
1157 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1158
1159 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1160 // nodes and Machine PHI nodes, but the incoming operands have not been
1161 // emitted yet.
1162 for (BasicBlock::const_iterator I = SuccBB->begin();
1163 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanfb95f892010-05-07 01:10:20 +00001164
Dan Gohmanf81eca02010-04-22 20:46:50 +00001165 // Ignore dead phi's.
1166 if (PN->use_empty()) continue;
1167
1168 // Only handle legal types. Two interesting things to note here. First,
1169 // by bailing out early, we may leave behind some dead instructions,
1170 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
1171 // own moves. Second, this check is necessary becuase FastISel doesn't
1172 // use CreateRegForValue to create registers, so it always creates
1173 // exactly one register for each non-void instruction.
1174 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1175 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
1176 // Promote MVT::i1.
1177 if (VT == MVT::i1)
1178 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1179 else {
1180 PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
1181 return false;
1182 }
1183 }
1184
1185 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1186
Dan Gohmanfb95f892010-05-07 01:10:20 +00001187 // Set the DebugLoc for the copy. Prefer the location of the operand
1188 // if there is one; use the location of the PHI otherwise.
1189 DL = PN->getDebugLoc();
1190 if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
1191 DL = Inst->getDebugLoc();
1192
Dan Gohmanf81eca02010-04-22 20:46:50 +00001193 unsigned Reg = getRegForValue(PHIOp);
1194 if (Reg == 0) {
1195 PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
1196 return false;
1197 }
1198 PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohmanfb95f892010-05-07 01:10:20 +00001199 DL = DebugLoc();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001200 }
1201 }
1202
1203 return true;
1204}