blob: 24456504ccf13185356153c0f6eac15c5121a118 [file] [log] [blame]
Dan Gohmanb9f92952010-04-22 20:06:42 +00001//===-- FastISel.cpp - Implementation of the FastISel class ---------------===//
Dan Gohmanb75dead2008-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 Gohmanff1ab062008-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 Lattner61d84a02008-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 Gohmanff1ab062008-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 Lattner61d84a02008-10-13 01:59:13 +000022// support. In many cases, this allows us to avoid duplicating a lot of
Dan Gohmanff1ab062008-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 Lattner61d84a02008-10-13 01:59:13 +000027// weighed against the speed at which the code can be generated. Also,
Dan Gohmanff1ab062008-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 Lattner61d84a02008-10-13 01:59:13 +000030// time. Despite its limitations, "fast" instruction selection is able to
Dan Gohmanff1ab062008-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 Lattner61d84a02008-10-13 01:59:13 +000037// from simple operators. More complicated operations currently require
Dan Gohmanff1ab062008-09-30 20:48:29 +000038// target-specific code.
39//
Dan Gohmanb75dead2008-08-13 20:19:35 +000040//===----------------------------------------------------------------------===//
41
Dan Gohman78ae76d2008-09-25 17:05:24 +000042#include "llvm/Function.h"
43#include "llvm/GlobalVariable.h"
Dan Gohman0193cd42008-08-19 22:31:46 +000044#include "llvm/Instructions.h"
Dan Gohman78ae76d2008-09-25 17:05:24 +000045#include "llvm/IntrinsicInst.h"
Dan Gohmanb75dead2008-08-13 20:19:35 +000046#include "llvm/CodeGen/FastISel.h"
47#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman78ae76d2008-09-25 17:05:24 +000048#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohmanb75dead2008-08-13 20:19:35 +000049#include "llvm/CodeGen/MachineRegisterInfo.h"
Devang Patelfcf1c752009-01-13 00:35:13 +000050#include "llvm/Analysis/DebugInfo.h"
Evan Chengd486a9d2008-08-20 22:45:34 +000051#include "llvm/Target/TargetData.h"
Dan Gohmanb75dead2008-08-13 20:19:35 +000052#include "llvm/Target/TargetInstrInfo.h"
Evan Chengd486a9d2008-08-20 22:45:34 +000053#include "llvm/Target/TargetLowering.h"
Dan Gohman7bc5a3d2008-08-20 21:05:57 +000054#include "llvm/Target/TargetMachine.h"
Dan Gohmana3344122010-04-20 15:00:41 +000055#include "llvm/Support/ErrorHandling.h"
Dan Gohmanc073abf2009-11-23 17:42:46 +000056#include "FunctionLoweringInfo.h"
Dan Gohmanb75dead2008-08-13 20:19:35 +000057using namespace llvm;
58
Dan Gohman5d4cee02010-05-11 23:54:07 +000059bool FastISel::hasTrivialKill(const Value *V) const {
60 // Don't consider constants or arguments to have trivial kills.
61 const Instruction *I = dyn_cast<Instruction>(V);
62 return I && I->hasOneUse();
63}
64
Dan Gohman36c56d02010-04-15 01:51:59 +000065unsigned FastISel::getRegForValue(const Value *V) {
Owen Andersonac9de032009-08-10 22:56:29 +000066 EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
Dan Gohman09532df2009-04-07 20:40:11 +000067 // Don't handle non-simple values in FastISel.
68 if (!RealVT.isSimple())
69 return 0;
Dan Gohman009a81f2008-12-08 07:57:47 +000070
71 // Ignore illegal types. We must do this before looking up the value
72 // in ValueMap because Arguments are given virtual registers regardless
73 // of whether FastISel can handle them.
Owen Anderson36e3a6e2009-08-11 20:47:22 +000074 MVT VT = RealVT.getSimpleVT();
Dan Gohman009a81f2008-12-08 07:57:47 +000075 if (!TLI.isTypeLegal(VT)) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +000076 // Promote MVT::i1 to a legal type though, because it's common and easy.
77 if (VT == MVT::i1)
Owen Anderson77f4eb52009-08-12 00:36:31 +000078 VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
Dan Gohman009a81f2008-12-08 07:57:47 +000079 else
80 return 0;
81 }
82
Dan Gohman5ac35422008-09-03 23:32:19 +000083 // Look up the value to see if we already have a register for it. We
84 // cache values defined by Instructions across blocks, and other values
85 // only locally. This is because Instructions already have the SSA
Dan Gohmanc9058802010-01-12 04:30:26 +000086 // def-dominates-use requirement enforced.
Owen Anderson95a87fd2008-09-03 17:37:03 +000087 if (ValueMap.count(V))
88 return ValueMap[V];
Dan Gohman5ac35422008-09-03 23:32:19 +000089 unsigned Reg = LocalValueMap[V];
90 if (Reg != 0)
91 return Reg;
Dan Gohman1e0ff772008-08-27 18:10:19 +000092
Dan Gohman42b46122010-05-06 00:02:14 +000093 // In bottom-up mode, just create the virtual register which will be used
94 // to hold the value. It will be materialized later.
95 if (IsBottomUp) {
96 Reg = createResultReg(TLI.getRegClassFor(VT));
97 if (isa<Instruction>(V))
98 ValueMap[V] = Reg;
99 else
100 LocalValueMap[V] = Reg;
101 return Reg;
102 }
103
Dan Gohman080b7d42010-05-03 23:36:34 +0000104 return materializeRegForValue(V, VT);
105}
106
107/// materializeRegForValue - Helper for getRegForVale. This function is
108/// called when the value isn't already available in a register and must
109/// be materialized with new instructions.
110unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
111 unsigned Reg = 0;
112
Dan Gohman36c56d02010-04-15 01:51:59 +0000113 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman0dd5fd92008-09-19 22:16:54 +0000114 if (CI->getValue().getActiveBits() <= 64)
115 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohmand6211a72008-09-10 20:11:02 +0000116 } else if (isa<AllocaInst>(V)) {
Dan Gohman0dd5fd92008-09-19 22:16:54 +0000117 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman41e4a502008-08-28 21:19:07 +0000118 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman3a49d0e2008-10-07 22:03:27 +0000119 // Translate this as an integer zero so that it can be
120 // local-CSE'd with actual integer zeros.
Owen Anderson35b47072009-08-13 21:58:54 +0000121 Reg =
122 getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
Dan Gohman36c56d02010-04-15 01:51:59 +0000123 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman37391a52010-04-13 17:07:06 +0000124 // Try to emit the constant directly.
Dan Gohman5ac35422008-09-03 23:32:19 +0000125 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
Dan Gohman1e0ff772008-08-27 18:10:19 +0000126
127 if (!Reg) {
Dan Gohman37391a52010-04-13 17:07:06 +0000128 // Try to emit the constant by using an integer constant with a cast.
Dan Gohman1e0ff772008-08-27 18:10:19 +0000129 const APFloat &Flt = CF->getValueAPF();
Owen Andersonac9de032009-08-10 22:56:29 +0000130 EVT IntVT = TLI.getPointerTy();
Dan Gohman1e0ff772008-08-27 18:10:19 +0000131
132 uint64_t x[2];
133 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen6e547b42008-10-09 23:00:39 +0000134 bool isExact;
135 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
136 APFloat::rmTowardZero, &isExact);
137 if (isExact) {
Dan Gohman0dd5fd92008-09-19 22:16:54 +0000138 APInt IntVal(IntBitWidth, 2, x);
Dan Gohman1e0ff772008-08-27 18:10:19 +0000139
Owen Anderson175b6542009-07-22 00:24:57 +0000140 unsigned IntegerReg =
Owen Andersoneacb44d2009-07-24 23:12:02 +0000141 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
Dan Gohman0dd5fd92008-09-19 22:16:54 +0000142 if (IntegerReg != 0)
Dan Gohman5d4cee02010-05-11 23:54:07 +0000143 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
144 IntegerReg, /*Kill=*/false);
Dan Gohman0dd5fd92008-09-19 22:16:54 +0000145 }
Dan Gohman1e0ff772008-08-27 18:10:19 +0000146 }
Dan Gohman36c56d02010-04-15 01:51:59 +0000147 } else if (const Operator *Op = dyn_cast<Operator>(V)) {
Dan Gohman91bdbdd2010-04-14 02:33:23 +0000148 if (!SelectOperator(Op, Op->getOpcode())) return 0;
149 Reg = LocalValueMap[Op];
Dan Gohman41e4a502008-08-28 21:19:07 +0000150 } else if (isa<UndefValue>(V)) {
Dan Gohman5ac35422008-09-03 23:32:19 +0000151 Reg = createResultReg(TLI.getRegClassFor(VT));
Chris Lattner4052b292010-02-09 19:54:29 +0000152 BuildMI(MBB, DL, TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohman1e0ff772008-08-27 18:10:19 +0000153 }
Owen Anderson9ecc0c72008-09-03 17:51:57 +0000154
Dan Gohman1c395702008-09-25 01:28:51 +0000155 // If target-independent code couldn't handle the value, give target-specific
156 // code a try.
Owen Anderson134f6e52008-09-05 23:36:01 +0000157 if (!Reg && isa<Constant>(V))
Dan Gohman0dd5fd92008-09-19 22:16:54 +0000158 Reg = TargetMaterializeConstant(cast<Constant>(V));
Owen Anderson134f6e52008-09-05 23:36:01 +0000159
Dan Gohman0dd5fd92008-09-19 22:16:54 +0000160 // Don't cache constant materializations in the general ValueMap.
161 // To do so would require tracking what uses they dominate.
Dan Gohman1c395702008-09-25 01:28:51 +0000162 if (Reg != 0)
163 LocalValueMap[V] = Reg;
Dan Gohman5ac35422008-09-03 23:32:19 +0000164 return Reg;
Dan Gohman1e0ff772008-08-27 18:10:19 +0000165}
166
Dan Gohman36c56d02010-04-15 01:51:59 +0000167unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng483f0db2008-09-09 01:26:59 +0000168 // Look up the value to see if we already have a register for it. We
169 // cache values defined by Instructions across blocks, and other values
170 // only locally. This is because Instructions already have the SSA
Dan Gohman080b7d42010-05-03 23:36:34 +0000171 // def-dominates-use requirement enforced.
Evan Cheng483f0db2008-09-09 01:26:59 +0000172 if (ValueMap.count(V))
173 return ValueMap[V];
174 return LocalValueMap[V];
175}
176
Owen Anderson64205032008-08-30 00:38:46 +0000177/// UpdateValueMap - Update the value map to include the new mapping for this
178/// instruction, or insert an extra copy to get the result in a previous
179/// determined register.
180/// NOTE: This is only necessary because we might select a block that uses
181/// a value before we select the block that defines the value. It might be
182/// possible to fix this by selecting blocks in reverse postorder.
Dan Gohman36c56d02010-04-15 01:51:59 +0000183unsigned FastISel::UpdateValueMap(const Value *I, unsigned Reg) {
Dan Gohman336a1932008-09-05 18:18:20 +0000184 if (!isa<Instruction>(I)) {
185 LocalValueMap[I] = Reg;
Chris Lattnerbc846232009-04-12 07:45:01 +0000186 return Reg;
Dan Gohman336a1932008-09-05 18:18:20 +0000187 }
Chris Lattnerbc846232009-04-12 07:45:01 +0000188
189 unsigned &AssignedReg = ValueMap[I];
190 if (AssignedReg == 0)
191 AssignedReg = Reg;
Chris Lattnerf0e7da22009-04-12 07:46:30 +0000192 else if (Reg != AssignedReg) {
Chris Lattnerbc846232009-04-12 07:45:01 +0000193 const TargetRegisterClass *RegClass = MRI.getRegClass(Reg);
194 TII.copyRegToReg(*MBB, MBB->end(), AssignedReg,
Dan Gohman75a44ec2010-05-06 20:33:48 +0000195 Reg, RegClass, RegClass, DL);
Chris Lattnerbc846232009-04-12 07:45:01 +0000196 }
197 return AssignedReg;
Owen Anderson64205032008-08-30 00:38:46 +0000198}
199
Dan Gohman5d4cee02010-05-11 23:54:07 +0000200std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohman009a81f2008-12-08 07:57:47 +0000201 unsigned IdxN = getRegForValue(Idx);
202 if (IdxN == 0)
203 // Unhandled operand. Halt "fast" selection and bail.
Dan Gohman5d4cee02010-05-11 23:54:07 +0000204 return std::pair<unsigned, bool>(0, false);
205
206 bool IdxNIsKill = hasTrivialKill(Idx);
Dan Gohman009a81f2008-12-08 07:57:47 +0000207
208 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Anderson2dd68a22009-08-11 21:59:30 +0000209 MVT PtrVT = TLI.getPointerTy();
Owen Andersonac9de032009-08-10 22:56:29 +0000210 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohman5d4cee02010-05-11 23:54:07 +0000211 if (IdxVT.bitsLT(PtrVT)) {
212 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND,
213 IdxN, IdxNIsKill);
214 IdxNIsKill = true;
215 }
216 else if (IdxVT.bitsGT(PtrVT)) {
217 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE,
218 IdxN, IdxNIsKill);
219 IdxNIsKill = true;
220 }
221 return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
Dan Gohman009a81f2008-12-08 07:57:47 +0000222}
223
Dan Gohmane9ab71e2008-08-20 00:11:48 +0000224/// SelectBinaryOp - Select and emit code for a binary operator instruction,
225/// which has an opcode which directly corresponds to the given ISD opcode.
226///
Dan Gohman36c56d02010-04-15 01:51:59 +0000227bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Andersonac9de032009-08-10 22:56:29 +0000228 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000229 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmane9ab71e2008-08-20 00:11:48 +0000230 // Unhandled type. Halt "fast" selection and bail.
231 return false;
Dan Gohman8a8980e2008-09-05 18:44:22 +0000232
Dan Gohman0254ba12008-08-26 20:52:40 +0000233 // We only handle legal types. For example, on x86-32 the instruction
234 // selector contains all of the 64-bit instructions from x86-64,
235 // under the assumption that i64 won't be used if the target doesn't
236 // support it.
Dan Gohman8a8980e2008-09-05 18:44:22 +0000237 if (!TLI.isTypeLegal(VT)) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000238 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman8a8980e2008-09-05 18:44:22 +0000239 // don't require additional zeroing, which makes them easy.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000240 if (VT == MVT::i1 &&
Dan Gohman55c60ad2008-09-25 17:22:52 +0000241 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
242 ISDOpcode == ISD::XOR))
Owen Anderson77f4eb52009-08-12 00:36:31 +0000243 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohman8a8980e2008-09-05 18:44:22 +0000244 else
245 return false;
246 }
Dan Gohmane9ab71e2008-08-20 00:11:48 +0000247
Dan Gohmanca4857a2008-09-03 23:12:08 +0000248 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohman895213e2008-08-21 01:41:07 +0000249 if (Op0 == 0)
250 // Unhandled operand. Halt "fast" selection and bail.
251 return false;
252
Dan Gohman5d4cee02010-05-11 23:54:07 +0000253 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
254
Dan Gohman895213e2008-08-21 01:41:07 +0000255 // Check if the second operand is a constant and handle it appropriately.
256 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohman1e0ff772008-08-27 18:10:19 +0000257 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohman5d4cee02010-05-11 23:54:07 +0000258 ISDOpcode, Op0, Op0IsKill,
259 CI->getZExtValue());
Dan Gohman1e0ff772008-08-27 18:10:19 +0000260 if (ResultReg != 0) {
261 // We successfully emitted code for the given LLVM Instruction.
Dan Gohmanca4857a2008-09-03 23:12:08 +0000262 UpdateValueMap(I, ResultReg);
Dan Gohman1e0ff772008-08-27 18:10:19 +0000263 return true;
264 }
Dan Gohman895213e2008-08-21 01:41:07 +0000265 }
266
Dan Gohman9f28bc52008-08-27 01:09:54 +0000267 // Check if the second operand is a constant float.
268 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohman1e0ff772008-08-27 18:10:19 +0000269 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohman5d4cee02010-05-11 23:54:07 +0000270 ISDOpcode, Op0, Op0IsKill, CF);
Dan Gohman1e0ff772008-08-27 18:10:19 +0000271 if (ResultReg != 0) {
272 // We successfully emitted code for the given LLVM Instruction.
Dan Gohmanca4857a2008-09-03 23:12:08 +0000273 UpdateValueMap(I, ResultReg);
Dan Gohman1e0ff772008-08-27 18:10:19 +0000274 return true;
275 }
Dan Gohman9f28bc52008-08-27 01:09:54 +0000276 }
277
Dan Gohmanca4857a2008-09-03 23:12:08 +0000278 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohman895213e2008-08-21 01:41:07 +0000279 if (Op1 == 0)
280 // Unhandled operand. Halt "fast" selection and bail.
281 return false;
282
Dan Gohman5d4cee02010-05-11 23:54:07 +0000283 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
284
Dan Gohman1e0ff772008-08-27 18:10:19 +0000285 // Now we have both operands in registers. Emit the instruction.
Owen Anderson3ac15da2008-08-25 23:58:18 +0000286 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohman5d4cee02010-05-11 23:54:07 +0000287 ISDOpcode,
288 Op0, Op0IsKill,
289 Op1, Op1IsKill);
Dan Gohmane9ab71e2008-08-20 00:11:48 +0000290 if (ResultReg == 0)
291 // Target-specific code wasn't able to find a machine opcode for
292 // the given ISD opcode and type. Halt "fast" selection and bail.
293 return false;
294
Dan Gohmanc712e8f2008-08-20 00:23:20 +0000295 // We successfully emitted code for the given LLVM Instruction.
Dan Gohmanca4857a2008-09-03 23:12:08 +0000296 UpdateValueMap(I, ResultReg);
Dan Gohmane9ab71e2008-08-20 00:11:48 +0000297 return true;
298}
299
Dan Gohman36c56d02010-04-15 01:51:59 +0000300bool FastISel::SelectGetElementPtr(const User *I) {
Dan Gohmanca4857a2008-09-03 23:12:08 +0000301 unsigned N = getRegForValue(I->getOperand(0));
Evan Chengd486a9d2008-08-20 22:45:34 +0000302 if (N == 0)
303 // Unhandled operand. Halt "fast" selection and bail.
304 return false;
305
Dan Gohman5d4cee02010-05-11 23:54:07 +0000306 bool NIsKill = hasTrivialKill(I->getOperand(0));
307
Evan Chengd486a9d2008-08-20 22:45:34 +0000308 const Type *Ty = I->getOperand(0)->getType();
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000309 MVT VT = TLI.getPointerTy();
Dan Gohman36c56d02010-04-15 01:51:59 +0000310 for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
311 E = I->op_end(); OI != E; ++OI) {
312 const Value *Idx = *OI;
Evan Chengd486a9d2008-08-20 22:45:34 +0000313 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
314 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
315 if (Field) {
316 // N = N + Offset
317 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
318 // FIXME: This can be optimized by combining the add with a
319 // subsequent one.
Dan Gohman5d4cee02010-05-11 23:54:07 +0000320 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
Evan Chengd486a9d2008-08-20 22:45:34 +0000321 if (N == 0)
322 // Unhandled operand. Halt "fast" selection and bail.
323 return false;
Dan Gohman5d4cee02010-05-11 23:54:07 +0000324 NIsKill = true;
Evan Chengd486a9d2008-08-20 22:45:34 +0000325 }
326 Ty = StTy->getElementType(Field);
327 } else {
328 Ty = cast<SequentialType>(Ty)->getElementType();
329
330 // If this is a constant subscript, handle it quickly.
Dan Gohman36c56d02010-04-15 01:51:59 +0000331 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Evan Chengd486a9d2008-08-20 22:45:34 +0000332 if (CI->getZExtValue() == 0) continue;
333 uint64_t Offs =
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000334 TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman5d4cee02010-05-11 23:54:07 +0000335 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
Evan Chengd486a9d2008-08-20 22:45:34 +0000336 if (N == 0)
337 // Unhandled operand. Halt "fast" selection and bail.
338 return false;
Dan Gohman5d4cee02010-05-11 23:54:07 +0000339 NIsKill = true;
Evan Chengd486a9d2008-08-20 22:45:34 +0000340 continue;
341 }
342
343 // N = N + Idx * ElementSize;
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000344 uint64_t ElementSize = TD.getTypeAllocSize(Ty);
Dan Gohman5d4cee02010-05-11 23:54:07 +0000345 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
346 unsigned IdxN = Pair.first;
347 bool IdxNIsKill = Pair.second;
Evan Chengd486a9d2008-08-20 22:45:34 +0000348 if (IdxN == 0)
349 // Unhandled operand. Halt "fast" selection and bail.
350 return false;
351
Dan Gohman8d6f1b22008-08-26 20:57:08 +0000352 if (ElementSize != 1) {
Dan Gohman5d4cee02010-05-11 23:54:07 +0000353 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
Dan Gohman8d6f1b22008-08-26 20:57:08 +0000354 if (IdxN == 0)
355 // Unhandled operand. Halt "fast" selection and bail.
356 return false;
Dan Gohman5d4cee02010-05-11 23:54:07 +0000357 IdxNIsKill = true;
Dan Gohman8d6f1b22008-08-26 20:57:08 +0000358 }
Dan Gohman5d4cee02010-05-11 23:54:07 +0000359 N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
Evan Chengd486a9d2008-08-20 22:45:34 +0000360 if (N == 0)
361 // Unhandled operand. Halt "fast" selection and bail.
362 return false;
363 }
364 }
365
366 // We successfully emitted code for the given LLVM Instruction.
Dan Gohmanca4857a2008-09-03 23:12:08 +0000367 UpdateValueMap(I, N);
Evan Chengd486a9d2008-08-20 22:45:34 +0000368 return true;
Dan Gohmane9ab71e2008-08-20 00:11:48 +0000369}
370
Dan Gohman36c56d02010-04-15 01:51:59 +0000371bool FastISel::SelectCall(const User *I) {
372 const Function *F = cast<CallInst>(I)->getCalledFunction();
Dan Gohman78ae76d2008-09-25 17:05:24 +0000373 if (!F) return false;
374
Dan Gohman37391a52010-04-13 17:07:06 +0000375 // Handle selected intrinsic function calls.
Dan Gohman78ae76d2008-09-25 17:05:24 +0000376 unsigned IID = F->getIntrinsicID();
377 switch (IID) {
378 default: break;
Bill Wendlingb0940162009-02-13 02:16:35 +0000379 case Intrinsic::dbg_declare: {
Dan Gohman36c56d02010-04-15 01:51:59 +0000380 const DbgDeclareInst *DI = cast<DbgDeclareInst>(I);
Devang Patel5117aa62010-05-07 22:04:20 +0000381 if (!DIVariable(DI->getVariable()).Verify() ||
Chris Lattnerbc491002010-04-05 06:05:26 +0000382 !MF.getMMI().hasDebugInfo())
Devang Patel1540fd62009-07-02 22:43:26 +0000383 return true;
384
Dan Gohman36c56d02010-04-15 01:51:59 +0000385 const Value *Address = DI->getAddress();
Dale Johannesen1a68ec12010-02-06 02:26:02 +0000386 if (!Address)
387 return true;
Dale Johannesenfbd947e2010-04-07 01:15:14 +0000388 if (isa<UndefValue>(Address))
389 return true;
Dan Gohman36c56d02010-04-15 01:51:59 +0000390 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
Devang Patel1540fd62009-07-02 22:43:26 +0000391 // Don't handle byval struct arguments or VLAs, for example.
Dale Johannesenf9265f22010-04-25 21:03:54 +0000392 // Note that if we have a byval struct argument, fast ISel is turned off;
393 // those are handled in SelectionDAGBuilder.
Devang Patel01f2b922010-04-28 19:27:33 +0000394 if (AI) {
395 DenseMap<const AllocaInst*, int>::iterator SI =
396 StaticAllocaMap.find(AI);
397 if (SI == StaticAllocaMap.end()) break; // VLAs.
398 int FI = SI->second;
399 if (!DI->getDebugLoc().isUnknown())
400 MF.getMMI().setVariableDbgInfo(DI->getVariable(), FI, DI->getDebugLoc());
401 } else
402 // Building the map above is target independent. Generating DBG_VALUE
403 // inline is target dependent; do this now.
404 (void)TargetSelectInstruction(cast<Instruction>(I));
Dan Gohman78ae76d2008-09-25 17:05:24 +0000405 return true;
Bill Wendlingb0940162009-02-13 02:16:35 +0000406 }
Dale Johannesen10712662010-02-26 20:01:55 +0000407 case Intrinsic::dbg_value: {
Dale Johannesenfbd947e2010-04-07 01:15:14 +0000408 // This form of DBG_VALUE is target-independent.
Dan Gohman36c56d02010-04-15 01:51:59 +0000409 const DbgValueInst *DI = cast<DbgValueInst>(I);
Dale Johannesen10712662010-02-26 20:01:55 +0000410 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohman36c56d02010-04-15 01:51:59 +0000411 const Value *V = DI->getValue();
Dale Johannesen10712662010-02-26 20:01:55 +0000412 if (!V) {
413 // Currently the optimizer can produce this; insert an undef to
414 // help debugging. Probably the optimizer should not do this.
415 BuildMI(MBB, DL, II).addReg(0U).addImm(DI->getOffset()).
416 addMetadata(DI->getVariable());
Dan Gohman36c56d02010-04-15 01:51:59 +0000417 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dale Johannesen10712662010-02-26 20:01:55 +0000418 BuildMI(MBB, DL, II).addImm(CI->getZExtValue()).addImm(DI->getOffset()).
419 addMetadata(DI->getVariable());
Dan Gohman36c56d02010-04-15 01:51:59 +0000420 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dale Johannesen10712662010-02-26 20:01:55 +0000421 BuildMI(MBB, DL, II).addFPImm(CF).addImm(DI->getOffset()).
422 addMetadata(DI->getVariable());
423 } else if (unsigned Reg = lookUpRegForValue(V)) {
424 BuildMI(MBB, DL, II).addReg(Reg, RegState::Debug).addImm(DI->getOffset()).
425 addMetadata(DI->getVariable());
426 } else {
427 // We can't yet handle anything else here because it would require
428 // generating code, thus altering codegen because of debug info.
429 // Insert an undef so we can see what we dropped.
430 BuildMI(MBB, DL, II).addReg(0U).addImm(DI->getOffset()).
431 addMetadata(DI->getVariable());
432 }
433 return true;
434 }
Dan Gohman9dd43582008-10-14 23:54:11 +0000435 case Intrinsic::eh_exception: {
Owen Andersonac9de032009-08-10 22:56:29 +0000436 EVT VT = TLI.getValueType(I->getType());
Dan Gohman9dd43582008-10-14 23:54:11 +0000437 switch (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)) {
438 default: break;
439 case TargetLowering::Expand: {
Duncan Sandsf325c482009-05-22 20:36:31 +0000440 assert(MBB->isLandingPad() && "Call to eh.exception not in landing pad!");
Dan Gohman9dd43582008-10-14 23:54:11 +0000441 unsigned Reg = TLI.getExceptionAddressRegister();
442 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
443 unsigned ResultReg = createResultReg(RC);
444 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman75a44ec2010-05-06 20:33:48 +0000445 Reg, RC, RC, DL);
Dan Gohman9dd43582008-10-14 23:54:11 +0000446 assert(InsertedCopy && "Can't copy address registers!");
Evan Chengcf576fd2008-11-24 07:09:49 +0000447 InsertedCopy = InsertedCopy;
Dan Gohman9dd43582008-10-14 23:54:11 +0000448 UpdateValueMap(I, ResultReg);
449 return true;
450 }
451 }
452 break;
453 }
Duncan Sands4205cfe2009-10-14 16:11:37 +0000454 case Intrinsic::eh_selector: {
Owen Andersonac9de032009-08-10 22:56:29 +0000455 EVT VT = TLI.getValueType(I->getType());
Dan Gohman9dd43582008-10-14 23:54:11 +0000456 switch (TLI.getOperationAction(ISD::EHSELECTION, VT)) {
457 default: break;
458 case TargetLowering::Expand: {
Chris Lattnerbc491002010-04-05 06:05:26 +0000459 if (MBB->isLandingPad())
460 AddCatchInfo(*cast<CallInst>(I), &MF.getMMI(), MBB);
461 else {
Dan Gohman9dd43582008-10-14 23:54:11 +0000462#ifndef NDEBUG
Chris Lattnerbc491002010-04-05 06:05:26 +0000463 CatchInfoLost.insert(cast<CallInst>(I));
Dan Gohman9dd43582008-10-14 23:54:11 +0000464#endif
Chris Lattnerbc491002010-04-05 06:05:26 +0000465 // FIXME: Mark exception selector register as live in. Hack for PR1508.
Dan Gohman9dd43582008-10-14 23:54:11 +0000466 unsigned Reg = TLI.getExceptionSelectorRegister();
Chris Lattnerbc491002010-04-05 06:05:26 +0000467 if (Reg) MBB->addLiveIn(Reg);
Dan Gohman9dd43582008-10-14 23:54:11 +0000468 }
Chris Lattnerbc491002010-04-05 06:05:26 +0000469
470 unsigned Reg = TLI.getExceptionSelectorRegister();
471 EVT SrcVT = TLI.getPointerTy();
472 const TargetRegisterClass *RC = TLI.getRegClassFor(SrcVT);
473 unsigned ResultReg = createResultReg(RC);
474 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, Reg,
Dan Gohman75a44ec2010-05-06 20:33:48 +0000475 RC, RC, DL);
Chris Lattnerbc491002010-04-05 06:05:26 +0000476 assert(InsertedCopy && "Can't copy address registers!");
477 InsertedCopy = InsertedCopy;
478
Dan Gohman5d4cee02010-05-11 23:54:07 +0000479 bool ResultRegIsKill = hasTrivialKill(I);
480
Chris Lattnerbc491002010-04-05 06:05:26 +0000481 // Cast the register to the type of the selector.
482 if (SrcVT.bitsGT(MVT::i32))
483 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, ISD::TRUNCATE,
Dan Gohman5d4cee02010-05-11 23:54:07 +0000484 ResultReg, ResultRegIsKill);
Chris Lattnerbc491002010-04-05 06:05:26 +0000485 else if (SrcVT.bitsLT(MVT::i32))
486 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32,
Dan Gohman5d4cee02010-05-11 23:54:07 +0000487 ISD::SIGN_EXTEND, ResultReg, ResultRegIsKill);
Chris Lattnerbc491002010-04-05 06:05:26 +0000488 if (ResultReg == 0)
489 // Unhandled operand. Halt "fast" selection and bail.
490 return false;
491
492 UpdateValueMap(I, ResultReg);
493
Dan Gohman9dd43582008-10-14 23:54:11 +0000494 return true;
495 }
496 }
497 break;
498 }
Dan Gohman78ae76d2008-09-25 17:05:24 +0000499 }
Dan Gohman37391a52010-04-13 17:07:06 +0000500
501 // An arbitrary call. Bail.
Dan Gohman78ae76d2008-09-25 17:05:24 +0000502 return false;
503}
504
Dan Gohman36c56d02010-04-15 01:51:59 +0000505bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Andersonac9de032009-08-10 22:56:29 +0000506 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
507 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersonc619ed22008-08-26 23:46:32 +0000508
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000509 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
510 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersonc619ed22008-08-26 23:46:32 +0000511 // Unhandled type. Halt "fast" selection and bail.
512 return false;
513
Dan Gohman94fc47a2009-03-13 23:53:06 +0000514 // Check if the destination type is legal. Or as a special case,
515 // it may be i1 if we're doing a truncate because that's
516 // easy and somewhat common.
517 if (!TLI.isTypeLegal(DstVT))
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000518 if (DstVT != MVT::i1 || Opcode != ISD::TRUNCATE)
Dan Gohmand7629082008-10-03 01:28:47 +0000519 // Unhandled type. Halt "fast" selection and bail.
520 return false;
Dan Gohman94fc47a2009-03-13 23:53:06 +0000521
522 // Check if the source operand is legal. Or as a special case,
523 // it may be i1 if we're doing zero-extension because that's
524 // easy and somewhat common.
525 if (!TLI.isTypeLegal(SrcVT))
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000526 if (SrcVT != MVT::i1 || Opcode != ISD::ZERO_EXTEND)
Dan Gohman94fc47a2009-03-13 23:53:06 +0000527 // Unhandled type. Halt "fast" selection and bail.
528 return false;
529
Dan Gohmanca4857a2008-09-03 23:12:08 +0000530 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersonc619ed22008-08-26 23:46:32 +0000531 if (!InputReg)
532 // Unhandled operand. Halt "fast" selection and bail.
533 return false;
Dan Gohman01648d92009-03-13 20:42:20 +0000534
Dan Gohman5d4cee02010-05-11 23:54:07 +0000535 bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
536
Dan Gohman01648d92009-03-13 20:42:20 +0000537 // If the operand is i1, arrange for the high bits in the register to be zero.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000538 if (SrcVT == MVT::i1) {
Owen Anderson77f4eb52009-08-12 00:36:31 +0000539 SrcVT = TLI.getTypeToTransformTo(I->getContext(), SrcVT);
Dan Gohman5d4cee02010-05-11 23:54:07 +0000540 InputReg = FastEmitZExtFromI1(SrcVT.getSimpleVT(), InputReg, InputRegIsKill);
Dan Gohman01648d92009-03-13 20:42:20 +0000541 if (!InputReg)
542 return false;
Dan Gohman5d4cee02010-05-11 23:54:07 +0000543 InputRegIsKill = true;
Dan Gohman01648d92009-03-13 20:42:20 +0000544 }
Dan Gohman94fc47a2009-03-13 23:53:06 +0000545 // If the result is i1, truncate to the target's type for i1 first.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000546 if (DstVT == MVT::i1)
Owen Anderson77f4eb52009-08-12 00:36:31 +0000547 DstVT = TLI.getTypeToTransformTo(I->getContext(), DstVT);
Dan Gohman01648d92009-03-13 20:42:20 +0000548
Owen Andersonc619ed22008-08-26 23:46:32 +0000549 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
550 DstVT.getSimpleVT(),
551 Opcode,
Dan Gohman5d4cee02010-05-11 23:54:07 +0000552 InputReg, InputRegIsKill);
Owen Andersonc619ed22008-08-26 23:46:32 +0000553 if (!ResultReg)
554 return false;
555
Dan Gohmanca4857a2008-09-03 23:12:08 +0000556 UpdateValueMap(I, ResultReg);
Owen Andersonc619ed22008-08-26 23:46:32 +0000557 return true;
558}
559
Dan Gohman36c56d02010-04-15 01:51:59 +0000560bool FastISel::SelectBitCast(const User *I) {
Dan Gohman1e0ff772008-08-27 18:10:19 +0000561 // If the bitcast doesn't change the type, just use the operand value.
562 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohmanca4857a2008-09-03 23:12:08 +0000563 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmanf2075e02008-08-27 20:41:38 +0000564 if (Reg == 0)
565 return false;
Dan Gohmanca4857a2008-09-03 23:12:08 +0000566 UpdateValueMap(I, Reg);
Dan Gohman1e0ff772008-08-27 18:10:19 +0000567 return true;
568 }
569
570 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Andersonac9de032009-08-10 22:56:29 +0000571 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
572 EVT DstVT = TLI.getValueType(I->getType());
Owen Andersonc619ed22008-08-26 23:46:32 +0000573
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000574 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
575 DstVT == MVT::Other || !DstVT.isSimple() ||
Owen Andersonc619ed22008-08-26 23:46:32 +0000576 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
577 // Unhandled type. Halt "fast" selection and bail.
578 return false;
579
Dan Gohmanca4857a2008-09-03 23:12:08 +0000580 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohman1e0ff772008-08-27 18:10:19 +0000581 if (Op0 == 0)
582 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersonc619ed22008-08-26 23:46:32 +0000583 return false;
Dan Gohman5d4cee02010-05-11 23:54:07 +0000584
585 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
Owen Andersonc619ed22008-08-26 23:46:32 +0000586
Dan Gohman1e0ff772008-08-27 18:10:19 +0000587 // First, try to perform the bitcast by inserting a reg-reg copy.
588 unsigned ResultReg = 0;
589 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
590 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
591 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
592 ResultReg = createResultReg(DstClass);
593
594 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman75a44ec2010-05-06 20:33:48 +0000595 Op0, DstClass, SrcClass, DL);
Dan Gohman1e0ff772008-08-27 18:10:19 +0000596 if (!InsertedCopy)
597 ResultReg = 0;
598 }
599
600 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
601 if (!ResultReg)
602 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
Dan Gohman5d4cee02010-05-11 23:54:07 +0000603 ISD::BIT_CONVERT, Op0, Op0IsKill);
Dan Gohman1e0ff772008-08-27 18:10:19 +0000604
605 if (!ResultReg)
Owen Andersonc619ed22008-08-26 23:46:32 +0000606 return false;
607
Dan Gohmanca4857a2008-09-03 23:12:08 +0000608 UpdateValueMap(I, ResultReg);
Owen Andersonc619ed22008-08-26 23:46:32 +0000609 return true;
610}
611
Dan Gohmanca4857a2008-09-03 23:12:08 +0000612bool
Dan Gohman36c56d02010-04-15 01:51:59 +0000613FastISel::SelectInstruction(const Instruction *I) {
Dan Gohman26c78402010-04-23 15:29:50 +0000614 // Just before the terminator instruction, insert instructions to
615 // feed PHI nodes in successor blocks.
616 if (isa<TerminatorInst>(I))
617 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
618 return false;
619
Dan Gohman550184a2010-04-20 00:48:35 +0000620 DL = I->getDebugLoc();
621
Dan Gohman581cdf92009-12-05 01:27:58 +0000622 // First, try doing target-independent selection.
Dan Gohman550184a2010-04-20 00:48:35 +0000623 if (SelectOperator(I, I->getOpcode())) {
624 DL = DebugLoc();
Dan Gohman581cdf92009-12-05 01:27:58 +0000625 return true;
Dan Gohman550184a2010-04-20 00:48:35 +0000626 }
Dan Gohman581cdf92009-12-05 01:27:58 +0000627
628 // Next, try calling the target to attempt to handle the instruction.
Dan Gohman550184a2010-04-20 00:48:35 +0000629 if (TargetSelectInstruction(I)) {
630 DL = DebugLoc();
Dan Gohman581cdf92009-12-05 01:27:58 +0000631 return true;
Dan Gohman550184a2010-04-20 00:48:35 +0000632 }
Dan Gohman581cdf92009-12-05 01:27:58 +0000633
Dan Gohman550184a2010-04-20 00:48:35 +0000634 DL = DebugLoc();
Dan Gohman581cdf92009-12-05 01:27:58 +0000635 return false;
Dan Gohman336a1932008-09-05 18:18:20 +0000636}
637
Dan Gohman8766d8e2008-10-02 22:15:21 +0000638/// FastEmitBranch - Emit an unconditional branch to the given block,
639/// unless it is the immediate (fall-through) successor, and update
640/// the CFG.
641void
642FastISel::FastEmitBranch(MachineBasicBlock *MSucc) {
Dan Gohman8766d8e2008-10-02 22:15:21 +0000643 if (MBB->isLayoutSuccessor(MSucc)) {
644 // The unconditional fall-through case, which needs no instructions.
645 } else {
646 // The unconditional branch case.
647 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
648 }
649 MBB->addSuccessor(MSucc);
650}
651
Dan Gohmanf154271b2009-09-03 22:53:57 +0000652/// SelectFNeg - Emit an FNeg operation.
653///
654bool
Dan Gohman36c56d02010-04-15 01:51:59 +0000655FastISel::SelectFNeg(const User *I) {
Dan Gohmanf154271b2009-09-03 22:53:57 +0000656 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
657 if (OpReg == 0) return false;
658
Dan Gohman5d4cee02010-05-11 23:54:07 +0000659 bool OpRegIsKill = hasTrivialKill(I);
660
Dan Gohman8c5f55f2009-09-11 00:36:43 +0000661 // If the target has ISD::FNEG, use it.
662 EVT VT = TLI.getValueType(I->getType());
663 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohman5d4cee02010-05-11 23:54:07 +0000664 ISD::FNEG, OpReg, OpRegIsKill);
Dan Gohman8c5f55f2009-09-11 00:36:43 +0000665 if (ResultReg != 0) {
666 UpdateValueMap(I, ResultReg);
667 return true;
668 }
669
Dan Gohmanb144a522009-09-11 00:34:46 +0000670 // Bitcast the value to integer, twiddle the sign bit with xor,
671 // and then bitcast it back to floating-point.
Dan Gohmanf154271b2009-09-03 22:53:57 +0000672 if (VT.getSizeInBits() > 64) return false;
Dan Gohmanb144a522009-09-11 00:34:46 +0000673 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
674 if (!TLI.isTypeLegal(IntVT))
675 return false;
676
677 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
Dan Gohman5d4cee02010-05-11 23:54:07 +0000678 ISD::BIT_CONVERT, OpReg, OpRegIsKill);
Dan Gohmanb144a522009-09-11 00:34:46 +0000679 if (IntReg == 0)
680 return false;
681
Dan Gohman5d4cee02010-05-11 23:54:07 +0000682 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
683 IntReg, /*Kill=*/true,
Dan Gohmanb144a522009-09-11 00:34:46 +0000684 UINT64_C(1) << (VT.getSizeInBits()-1),
685 IntVT.getSimpleVT());
686 if (IntResultReg == 0)
687 return false;
688
689 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohman5d4cee02010-05-11 23:54:07 +0000690 ISD::BIT_CONVERT, IntResultReg, /*Kill=*/true);
Dan Gohmanf154271b2009-09-03 22:53:57 +0000691 if (ResultReg == 0)
692 return false;
693
694 UpdateValueMap(I, ResultReg);
695 return true;
696}
697
Dan Gohman336a1932008-09-05 18:18:20 +0000698bool
Dan Gohman36c56d02010-04-15 01:51:59 +0000699FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohman336a1932008-09-05 18:18:20 +0000700 switch (Opcode) {
Dan Gohman7ce405e2009-06-04 22:49:04 +0000701 case Instruction::Add:
702 return SelectBinaryOp(I, ISD::ADD);
703 case Instruction::FAdd:
704 return SelectBinaryOp(I, ISD::FADD);
705 case Instruction::Sub:
706 return SelectBinaryOp(I, ISD::SUB);
707 case Instruction::FSub:
Dan Gohmanf154271b2009-09-03 22:53:57 +0000708 // FNeg is currently represented in LLVM IR as a special case of FSub.
709 if (BinaryOperator::isFNeg(I))
710 return SelectFNeg(I);
Dan Gohman7ce405e2009-06-04 22:49:04 +0000711 return SelectBinaryOp(I, ISD::FSUB);
712 case Instruction::Mul:
713 return SelectBinaryOp(I, ISD::MUL);
714 case Instruction::FMul:
715 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohmanca4857a2008-09-03 23:12:08 +0000716 case Instruction::SDiv:
717 return SelectBinaryOp(I, ISD::SDIV);
718 case Instruction::UDiv:
719 return SelectBinaryOp(I, ISD::UDIV);
720 case Instruction::FDiv:
721 return SelectBinaryOp(I, ISD::FDIV);
722 case Instruction::SRem:
723 return SelectBinaryOp(I, ISD::SREM);
724 case Instruction::URem:
725 return SelectBinaryOp(I, ISD::UREM);
726 case Instruction::FRem:
727 return SelectBinaryOp(I, ISD::FREM);
728 case Instruction::Shl:
729 return SelectBinaryOp(I, ISD::SHL);
730 case Instruction::LShr:
731 return SelectBinaryOp(I, ISD::SRL);
732 case Instruction::AShr:
733 return SelectBinaryOp(I, ISD::SRA);
734 case Instruction::And:
735 return SelectBinaryOp(I, ISD::AND);
736 case Instruction::Or:
737 return SelectBinaryOp(I, ISD::OR);
738 case Instruction::Xor:
739 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb75dead2008-08-13 20:19:35 +0000740
Dan Gohmanca4857a2008-09-03 23:12:08 +0000741 case Instruction::GetElementPtr:
742 return SelectGetElementPtr(I);
Dan Gohmane9ab71e2008-08-20 00:11:48 +0000743
Dan Gohmanca4857a2008-09-03 23:12:08 +0000744 case Instruction::Br: {
Dan Gohman36c56d02010-04-15 01:51:59 +0000745 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmane9ab71e2008-08-20 00:11:48 +0000746
Dan Gohmanca4857a2008-09-03 23:12:08 +0000747 if (BI->isUnconditional()) {
Dan Gohman36c56d02010-04-15 01:51:59 +0000748 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohmanca4857a2008-09-03 23:12:08 +0000749 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
Dan Gohman8766d8e2008-10-02 22:15:21 +0000750 FastEmitBranch(MSucc);
Dan Gohmanca4857a2008-09-03 23:12:08 +0000751 return true;
Owen Andersond5d9a902008-08-27 00:31:01 +0000752 }
Dan Gohmanca4857a2008-09-03 23:12:08 +0000753
754 // Conditional branches are not handed yet.
755 // Halt "fast" selection and bail.
756 return false;
Dan Gohmanb75dead2008-08-13 20:19:35 +0000757 }
758
Dan Gohman36c296e2008-09-05 01:08:41 +0000759 case Instruction::Unreachable:
760 // Nothing to emit.
761 return true;
762
Dan Gohmand6211a72008-09-10 20:11:02 +0000763 case Instruction::Alloca:
764 // FunctionLowering has the static-sized case covered.
765 if (StaticAllocaMap.count(cast<AllocaInst>(I)))
766 return true;
767
768 // Dynamic-sized alloca is not handled yet.
769 return false;
Dan Gohmanca4857a2008-09-03 23:12:08 +0000770
Dan Gohman78ae76d2008-09-25 17:05:24 +0000771 case Instruction::Call:
772 return SelectCall(I);
773
Dan Gohmanca4857a2008-09-03 23:12:08 +0000774 case Instruction::BitCast:
775 return SelectBitCast(I);
776
777 case Instruction::FPToSI:
778 return SelectCast(I, ISD::FP_TO_SINT);
779 case Instruction::ZExt:
780 return SelectCast(I, ISD::ZERO_EXTEND);
781 case Instruction::SExt:
782 return SelectCast(I, ISD::SIGN_EXTEND);
783 case Instruction::Trunc:
784 return SelectCast(I, ISD::TRUNCATE);
785 case Instruction::SIToFP:
786 return SelectCast(I, ISD::SINT_TO_FP);
787
788 case Instruction::IntToPtr: // Deliberate fall-through.
789 case Instruction::PtrToInt: {
Owen Andersonac9de032009-08-10 22:56:29 +0000790 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
791 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohmanca4857a2008-09-03 23:12:08 +0000792 if (DstVT.bitsGT(SrcVT))
793 return SelectCast(I, ISD::ZERO_EXTEND);
794 if (DstVT.bitsLT(SrcVT))
795 return SelectCast(I, ISD::TRUNCATE);
796 unsigned Reg = getRegForValue(I->getOperand(0));
797 if (Reg == 0) return false;
798 UpdateValueMap(I, Reg);
799 return true;
800 }
Dan Gohman76dd96e2008-09-23 21:53:34 +0000801
Dan Gohmana3344122010-04-20 15:00:41 +0000802 case Instruction::PHI:
803 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
804
Dan Gohmanca4857a2008-09-03 23:12:08 +0000805 default:
806 // Unhandled instruction. Halt "fast" selection and bail.
807 return false;
808 }
Dan Gohmanb75dead2008-08-13 20:19:35 +0000809}
810
Dan Gohmanca4857a2008-09-03 23:12:08 +0000811FastISel::FastISel(MachineFunction &mf,
812 DenseMap<const Value *, unsigned> &vm,
Dan Gohmand6211a72008-09-10 20:11:02 +0000813 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
Dan Gohmanc603a5e2010-04-22 20:46:50 +0000814 DenseMap<const AllocaInst *, int> &am,
815 std::vector<std::pair<MachineInstr*, unsigned> > &pn
Dan Gohman9dd43582008-10-14 23:54:11 +0000816#ifndef NDEBUG
Dan Gohman68cd2d92010-04-14 19:53:31 +0000817 , SmallSet<const Instruction *, 8> &cil
Dan Gohman9dd43582008-10-14 23:54:11 +0000818#endif
819 )
Dan Gohmanca4857a2008-09-03 23:12:08 +0000820 : MBB(0),
821 ValueMap(vm),
822 MBBMap(bm),
Dan Gohmand6211a72008-09-10 20:11:02 +0000823 StaticAllocaMap(am),
Dan Gohmanc603a5e2010-04-22 20:46:50 +0000824 PHINodesToUpdate(pn),
Dan Gohman9dd43582008-10-14 23:54:11 +0000825#ifndef NDEBUG
826 CatchInfoLost(cil),
827#endif
Dan Gohmanca4857a2008-09-03 23:12:08 +0000828 MF(mf),
829 MRI(MF.getRegInfo()),
Dan Gohmand6211a72008-09-10 20:11:02 +0000830 MFI(*MF.getFrameInfo()),
831 MCP(*MF.getConstantPool()),
Dan Gohmanca4857a2008-09-03 23:12:08 +0000832 TM(MF.getTarget()),
Dan Gohmane97f1a32008-08-22 00:20:26 +0000833 TD(*TM.getTargetData()),
834 TII(*TM.getInstrInfo()),
Dan Gohman33dcdb72010-05-05 23:58:35 +0000835 TLI(*TM.getTargetLowering()),
836 IsBottomUp(false) {
Dan Gohman7bc5a3d2008-08-20 21:05:57 +0000837}
838
Dan Gohmaneeb6e302008-08-14 21:51:29 +0000839FastISel::~FastISel() {}
840
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000841unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohmanf45e5d12010-01-05 22:26:32 +0000842 unsigned) {
Dan Gohmanb75dead2008-08-13 20:19:35 +0000843 return 0;
844}
845
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000846unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohman5d4cee02010-05-11 23:54:07 +0000847 unsigned,
848 unsigned /*Op0*/, bool /*Op0IsKill*/) {
Dan Gohmanb75dead2008-08-13 20:19:35 +0000849 return 0;
850}
851
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000852unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohman5d4cee02010-05-11 23:54:07 +0000853 unsigned,
854 unsigned /*Op0*/, bool /*Op0IsKill*/,
855 unsigned /*Op1*/, bool /*Op1IsKill*/) {
Dan Gohmanb75dead2008-08-13 20:19:35 +0000856 return 0;
857}
858
Dan Gohmanf45e5d12010-01-05 22:26:32 +0000859unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Chengd486a9d2008-08-20 22:45:34 +0000860 return 0;
861}
862
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000863unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohman36c56d02010-04-15 01:51:59 +0000864 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman9f28bc52008-08-27 01:09:54 +0000865 return 0;
866}
867
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000868unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohman5d4cee02010-05-11 23:54:07 +0000869 unsigned,
870 unsigned /*Op0*/, bool /*Op0IsKill*/,
Owen Anderson3ac15da2008-08-25 23:58:18 +0000871 uint64_t /*Imm*/) {
Dan Gohman895213e2008-08-21 01:41:07 +0000872 return 0;
873}
874
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000875unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohman5d4cee02010-05-11 23:54:07 +0000876 unsigned,
877 unsigned /*Op0*/, bool /*Op0IsKill*/,
Dan Gohman36c56d02010-04-15 01:51:59 +0000878 const ConstantFP * /*FPImm*/) {
Dan Gohman9f28bc52008-08-27 01:09:54 +0000879 return 0;
880}
881
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000882unsigned FastISel::FastEmit_rri(MVT, MVT,
Dan Gohmanf45e5d12010-01-05 22:26:32 +0000883 unsigned,
Dan Gohman5d4cee02010-05-11 23:54:07 +0000884 unsigned /*Op0*/, bool /*Op0IsKill*/,
885 unsigned /*Op1*/, bool /*Op1IsKill*/,
Dan Gohman895213e2008-08-21 01:41:07 +0000886 uint64_t /*Imm*/) {
Evan Chengd486a9d2008-08-20 22:45:34 +0000887 return 0;
888}
889
890/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
891/// to emit an instruction with an immediate operand using FastEmit_ri.
892/// If that fails, it materializes the immediate into a register and try
893/// FastEmit_rr instead.
Dan Gohmanf45e5d12010-01-05 22:26:32 +0000894unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohman5d4cee02010-05-11 23:54:07 +0000895 unsigned Op0, bool Op0IsKill,
896 uint64_t Imm, MVT ImmType) {
Evan Chengd486a9d2008-08-20 22:45:34 +0000897 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman5d4cee02010-05-11 23:54:07 +0000898 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
Evan Chengd486a9d2008-08-20 22:45:34 +0000899 if (ResultReg != 0)
900 return ResultReg;
Owen Anderson3ac15da2008-08-25 23:58:18 +0000901 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohman895213e2008-08-21 01:41:07 +0000902 if (MaterialReg == 0)
903 return 0;
Dan Gohman5d4cee02010-05-11 23:54:07 +0000904 return FastEmit_rr(VT, VT, Opcode,
905 Op0, Op0IsKill,
906 MaterialReg, /*Kill=*/true);
Dan Gohman895213e2008-08-21 01:41:07 +0000907}
908
Dan Gohman9f28bc52008-08-27 01:09:54 +0000909/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
910/// to emit an instruction with a floating-point immediate operand using
911/// FastEmit_rf. If that fails, it materializes the immediate into a register
912/// and try FastEmit_rr instead.
Dan Gohmanf45e5d12010-01-05 22:26:32 +0000913unsigned FastISel::FastEmit_rf_(MVT VT, unsigned Opcode,
Dan Gohman5d4cee02010-05-11 23:54:07 +0000914 unsigned Op0, bool Op0IsKill,
915 const ConstantFP *FPImm, MVT ImmType) {
Dan Gohman9f28bc52008-08-27 01:09:54 +0000916 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohman5d4cee02010-05-11 23:54:07 +0000917 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, Op0IsKill, FPImm);
Dan Gohman9f28bc52008-08-27 01:09:54 +0000918 if (ResultReg != 0)
919 return ResultReg;
920
921 // Materialize the constant in a register.
922 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
923 if (MaterialReg == 0) {
Dan Gohman30872402008-08-27 18:01:42 +0000924 // If the target doesn't have a way to directly enter a floating-point
925 // value into a register, use an alternate approach.
926 // TODO: The current approach only supports floating-point constants
927 // that can be constructed by conversion from integer values. This should
928 // be replaced by code that creates a load from a constant-pool entry,
929 // which will require some target-specific work.
Dan Gohman9f28bc52008-08-27 01:09:54 +0000930 const APFloat &Flt = FPImm->getValueAPF();
Owen Andersonac9de032009-08-10 22:56:29 +0000931 EVT IntVT = TLI.getPointerTy();
Dan Gohman9f28bc52008-08-27 01:09:54 +0000932
933 uint64_t x[2];
934 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen6e547b42008-10-09 23:00:39 +0000935 bool isExact;
936 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
937 APFloat::rmTowardZero, &isExact);
938 if (!isExact)
Dan Gohman9f28bc52008-08-27 01:09:54 +0000939 return 0;
940 APInt IntVal(IntBitWidth, 2, x);
941
942 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
943 ISD::Constant, IntVal.getZExtValue());
944 if (IntegerReg == 0)
945 return 0;
946 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
Dan Gohman5d4cee02010-05-11 23:54:07 +0000947 ISD::SINT_TO_FP, IntegerReg, /*Kill=*/true);
Dan Gohman9f28bc52008-08-27 01:09:54 +0000948 if (MaterialReg == 0)
949 return 0;
950 }
Dan Gohman5d4cee02010-05-11 23:54:07 +0000951 return FastEmit_rr(VT, VT, Opcode,
952 Op0, Op0IsKill,
953 MaterialReg, /*Kill=*/true);
Dan Gohman9f28bc52008-08-27 01:09:54 +0000954}
955
Dan Gohman895213e2008-08-21 01:41:07 +0000956unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
957 return MRI.createVirtualRegister(RC);
Evan Chengd486a9d2008-08-20 22:45:34 +0000958}
959
Dan Gohmanb75dead2008-08-13 20:19:35 +0000960unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohmana4c482f2008-08-20 18:09:38 +0000961 const TargetRegisterClass* RC) {
Dan Gohman895213e2008-08-21 01:41:07 +0000962 unsigned ResultReg = createResultReg(RC);
Dan Gohman7bc5a3d2008-08-20 21:05:57 +0000963 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb75dead2008-08-13 20:19:35 +0000964
Bill Wendling5aa0ddb2009-02-03 00:55:04 +0000965 BuildMI(MBB, DL, II, ResultReg);
Dan Gohmanb75dead2008-08-13 20:19:35 +0000966 return ResultReg;
967}
968
969unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
970 const TargetRegisterClass *RC,
Dan Gohman5d4cee02010-05-11 23:54:07 +0000971 unsigned Op0, bool Op0IsKill) {
Dan Gohman895213e2008-08-21 01:41:07 +0000972 unsigned ResultReg = createResultReg(RC);
Dan Gohman7bc5a3d2008-08-20 21:05:57 +0000973 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb75dead2008-08-13 20:19:35 +0000974
Evan Cheng82bfc842008-09-08 08:38:20 +0000975 if (II.getNumDefs() >= 1)
Dan Gohman5d4cee02010-05-11 23:54:07 +0000976 BuildMI(MBB, DL, II, ResultReg).addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng82bfc842008-09-08 08:38:20 +0000977 else {
Dan Gohman5d4cee02010-05-11 23:54:07 +0000978 BuildMI(MBB, DL, II).addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng82bfc842008-09-08 08:38:20 +0000979 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman75a44ec2010-05-06 20:33:48 +0000980 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng82bfc842008-09-08 08:38:20 +0000981 if (!InsertedCopy)
982 ResultReg = 0;
983 }
984
Dan Gohmanb75dead2008-08-13 20:19:35 +0000985 return ResultReg;
986}
987
988unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
989 const TargetRegisterClass *RC,
Dan Gohman5d4cee02010-05-11 23:54:07 +0000990 unsigned Op0, bool Op0IsKill,
991 unsigned Op1, bool Op1IsKill) {
Dan Gohman895213e2008-08-21 01:41:07 +0000992 unsigned ResultReg = createResultReg(RC);
Dan Gohman7bc5a3d2008-08-20 21:05:57 +0000993 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb75dead2008-08-13 20:19:35 +0000994
Evan Cheng82bfc842008-09-08 08:38:20 +0000995 if (II.getNumDefs() >= 1)
Dan Gohman5d4cee02010-05-11 23:54:07 +0000996 BuildMI(MBB, DL, II, ResultReg)
997 .addReg(Op0, Op0IsKill * RegState::Kill)
998 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng82bfc842008-09-08 08:38:20 +0000999 else {
Dan Gohman5d4cee02010-05-11 23:54:07 +00001000 BuildMI(MBB, DL, II)
1001 .addReg(Op0, Op0IsKill * RegState::Kill)
1002 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng82bfc842008-09-08 08:38:20 +00001003 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman75a44ec2010-05-06 20:33:48 +00001004 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng82bfc842008-09-08 08:38:20 +00001005 if (!InsertedCopy)
1006 ResultReg = 0;
1007 }
Dan Gohmanb75dead2008-08-13 20:19:35 +00001008 return ResultReg;
1009}
Dan Gohman895213e2008-08-21 01:41:07 +00001010
1011unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1012 const TargetRegisterClass *RC,
Dan Gohman5d4cee02010-05-11 23:54:07 +00001013 unsigned Op0, bool Op0IsKill,
1014 uint64_t Imm) {
Dan Gohman895213e2008-08-21 01:41:07 +00001015 unsigned ResultReg = createResultReg(RC);
1016 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1017
Evan Cheng82bfc842008-09-08 08:38:20 +00001018 if (II.getNumDefs() >= 1)
Dan Gohman5d4cee02010-05-11 23:54:07 +00001019 BuildMI(MBB, DL, II, ResultReg)
1020 .addReg(Op0, Op0IsKill * RegState::Kill)
1021 .addImm(Imm);
Evan Cheng82bfc842008-09-08 08:38:20 +00001022 else {
Dan Gohman5d4cee02010-05-11 23:54:07 +00001023 BuildMI(MBB, DL, II)
1024 .addReg(Op0, Op0IsKill * RegState::Kill)
1025 .addImm(Imm);
Evan Cheng82bfc842008-09-08 08:38:20 +00001026 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman75a44ec2010-05-06 20:33:48 +00001027 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng82bfc842008-09-08 08:38:20 +00001028 if (!InsertedCopy)
1029 ResultReg = 0;
1030 }
Dan Gohman895213e2008-08-21 01:41:07 +00001031 return ResultReg;
1032}
1033
Dan Gohman9f28bc52008-08-27 01:09:54 +00001034unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
1035 const TargetRegisterClass *RC,
Dan Gohman5d4cee02010-05-11 23:54:07 +00001036 unsigned Op0, bool Op0IsKill,
1037 const ConstantFP *FPImm) {
Dan Gohman9f28bc52008-08-27 01:09:54 +00001038 unsigned ResultReg = createResultReg(RC);
1039 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1040
Evan Cheng82bfc842008-09-08 08:38:20 +00001041 if (II.getNumDefs() >= 1)
Dan Gohman5d4cee02010-05-11 23:54:07 +00001042 BuildMI(MBB, DL, II, ResultReg)
1043 .addReg(Op0, Op0IsKill * RegState::Kill)
1044 .addFPImm(FPImm);
Evan Cheng82bfc842008-09-08 08:38:20 +00001045 else {
Dan Gohman5d4cee02010-05-11 23:54:07 +00001046 BuildMI(MBB, DL, II)
1047 .addReg(Op0, Op0IsKill * RegState::Kill)
1048 .addFPImm(FPImm);
Evan Cheng82bfc842008-09-08 08:38:20 +00001049 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman75a44ec2010-05-06 20:33:48 +00001050 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng82bfc842008-09-08 08:38:20 +00001051 if (!InsertedCopy)
1052 ResultReg = 0;
1053 }
Dan Gohman9f28bc52008-08-27 01:09:54 +00001054 return ResultReg;
1055}
1056
Dan Gohman895213e2008-08-21 01:41:07 +00001057unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
1058 const TargetRegisterClass *RC,
Dan Gohman5d4cee02010-05-11 23:54:07 +00001059 unsigned Op0, bool Op0IsKill,
1060 unsigned Op1, bool Op1IsKill,
1061 uint64_t Imm) {
Dan Gohman895213e2008-08-21 01:41:07 +00001062 unsigned ResultReg = createResultReg(RC);
1063 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1064
Evan Cheng82bfc842008-09-08 08:38:20 +00001065 if (II.getNumDefs() >= 1)
Dan Gohman5d4cee02010-05-11 23:54:07 +00001066 BuildMI(MBB, DL, II, ResultReg)
1067 .addReg(Op0, Op0IsKill * RegState::Kill)
1068 .addReg(Op1, Op1IsKill * RegState::Kill)
1069 .addImm(Imm);
Evan Cheng82bfc842008-09-08 08:38:20 +00001070 else {
Dan Gohman5d4cee02010-05-11 23:54:07 +00001071 BuildMI(MBB, DL, II)
1072 .addReg(Op0, Op0IsKill * RegState::Kill)
1073 .addReg(Op1, Op1IsKill * RegState::Kill)
1074 .addImm(Imm);
Evan Cheng82bfc842008-09-08 08:38:20 +00001075 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman75a44ec2010-05-06 20:33:48 +00001076 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng82bfc842008-09-08 08:38:20 +00001077 if (!InsertedCopy)
1078 ResultReg = 0;
1079 }
Dan Gohman895213e2008-08-21 01:41:07 +00001080 return ResultReg;
1081}
Owen Anderson43337272008-08-25 20:20:32 +00001082
1083unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1084 const TargetRegisterClass *RC,
1085 uint64_t Imm) {
1086 unsigned ResultReg = createResultReg(RC);
1087 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1088
Evan Cheng82bfc842008-09-08 08:38:20 +00001089 if (II.getNumDefs() >= 1)
Bill Wendling5aa0ddb2009-02-03 00:55:04 +00001090 BuildMI(MBB, DL, II, ResultReg).addImm(Imm);
Evan Cheng82bfc842008-09-08 08:38:20 +00001091 else {
Bill Wendling5aa0ddb2009-02-03 00:55:04 +00001092 BuildMI(MBB, DL, II).addImm(Imm);
Evan Cheng82bfc842008-09-08 08:38:20 +00001093 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman75a44ec2010-05-06 20:33:48 +00001094 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng82bfc842008-09-08 08:38:20 +00001095 if (!InsertedCopy)
1096 ResultReg = 0;
1097 }
Owen Anderson43337272008-08-25 20:20:32 +00001098 return ResultReg;
Evan Cheng993d0062008-08-25 22:20:39 +00001099}
Owen Anderson44e9a2f2008-08-27 22:30:02 +00001100
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001101unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Dan Gohman5d4cee02010-05-11 23:54:07 +00001102 unsigned Op0, bool Op0IsKill,
1103 uint32_t Idx) {
Owen Anderson8b6bf042008-08-28 17:47:37 +00001104 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson44e9a2f2008-08-27 22:30:02 +00001105
Evan Chengbfda7272009-01-22 09:10:11 +00001106 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Chris Lattner4052b292010-02-09 19:54:29 +00001107 const TargetInstrDesc &II = TII.get(TargetOpcode::EXTRACT_SUBREG);
Owen Anderson44e9a2f2008-08-27 22:30:02 +00001108
Evan Cheng82bfc842008-09-08 08:38:20 +00001109 if (II.getNumDefs() >= 1)
Dan Gohman5d4cee02010-05-11 23:54:07 +00001110 BuildMI(MBB, DL, II, ResultReg)
1111 .addReg(Op0, Op0IsKill * RegState::Kill)
1112 .addImm(Idx);
Evan Cheng82bfc842008-09-08 08:38:20 +00001113 else {
Dan Gohman5d4cee02010-05-11 23:54:07 +00001114 BuildMI(MBB, DL, II)
1115 .addReg(Op0, Op0IsKill * RegState::Kill)
1116 .addImm(Idx);
Evan Cheng82bfc842008-09-08 08:38:20 +00001117 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
Dan Gohman75a44ec2010-05-06 20:33:48 +00001118 II.ImplicitDefs[0], RC, RC, DL);
Evan Cheng82bfc842008-09-08 08:38:20 +00001119 if (!InsertedCopy)
1120 ResultReg = 0;
1121 }
Owen Anderson44e9a2f2008-08-27 22:30:02 +00001122 return ResultReg;
1123}
Dan Gohman01648d92009-03-13 20:42:20 +00001124
1125/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1126/// with all but the least significant bit set to zero.
Dan Gohman5d4cee02010-05-11 23:54:07 +00001127unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1128 return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
Dan Gohman01648d92009-03-13 20:42:20 +00001129}
Dan Gohmanc603a5e2010-04-22 20:46:50 +00001130
1131/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1132/// Emit code to ensure constants are copied into registers when needed.
1133/// Remember the virtual registers that need to be added to the Machine PHI
1134/// nodes as input. We cannot just directly add them, because expansion
1135/// might result in multiple MBB's for one BB. As such, the start of the
1136/// BB might correspond to a different MBB than the end.
1137bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1138 const TerminatorInst *TI = LLVMBB->getTerminator();
1139
1140 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
1141 unsigned OrigNumPHINodesToUpdate = PHINodesToUpdate.size();
1142
1143 // Check successor nodes' PHI nodes that expect a constant to be available
1144 // from this block.
1145 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1146 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1147 if (!isa<PHINode>(SuccBB->begin())) continue;
1148 MachineBasicBlock *SuccMBB = MBBMap[SuccBB];
1149
1150 // If this terminator has multiple identical successors (common for
1151 // switches), only handle each succ once.
1152 if (!SuccsHandled.insert(SuccMBB)) continue;
1153
1154 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1155
1156 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1157 // nodes and Machine PHI nodes, but the incoming operands have not been
1158 // emitted yet.
1159 for (BasicBlock::const_iterator I = SuccBB->begin();
1160 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanebdca482010-05-07 01:10:20 +00001161
Dan Gohmanc603a5e2010-04-22 20:46:50 +00001162 // Ignore dead phi's.
1163 if (PN->use_empty()) continue;
1164
1165 // Only handle legal types. Two interesting things to note here. First,
1166 // by bailing out early, we may leave behind some dead instructions,
1167 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
1168 // own moves. Second, this check is necessary becuase FastISel doesn't
1169 // use CreateRegForValue to create registers, so it always creates
1170 // exactly one register for each non-void instruction.
1171 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1172 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
1173 // Promote MVT::i1.
1174 if (VT == MVT::i1)
1175 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1176 else {
1177 PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
1178 return false;
1179 }
1180 }
1181
1182 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1183
Dan Gohmanebdca482010-05-07 01:10:20 +00001184 // Set the DebugLoc for the copy. Prefer the location of the operand
1185 // if there is one; use the location of the PHI otherwise.
1186 DL = PN->getDebugLoc();
1187 if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
1188 DL = Inst->getDebugLoc();
1189
Dan Gohmanc603a5e2010-04-22 20:46:50 +00001190 unsigned Reg = getRegForValue(PHIOp);
1191 if (Reg == 0) {
1192 PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
1193 return false;
1194 }
1195 PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohmanebdca482010-05-07 01:10:20 +00001196 DL = DebugLoc();
Dan Gohmanc603a5e2010-04-22 20:46:50 +00001197 }
1198 }
1199
1200 return true;
1201}