blob: 4952b5b575b7a5767e36d5482f21f6d1ce17ba1a [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"
Jay Foad562b84b2011-04-11 09:35:34 +000046#include "llvm/Operator.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000047#include "llvm/CodeGen/FastISel.h"
Dan Gohman4c3fd9f2010-07-07 16:01:37 +000048#include "llvm/CodeGen/FunctionLoweringInfo.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000049#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman33134c42008-09-25 17:05:24 +000050#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000051#include "llvm/CodeGen/MachineRegisterInfo.h"
Devang Patel83489bb2009-01-13 00:35:13 +000052#include "llvm/Analysis/DebugInfo.h"
Dan Gohman7fbcc982010-07-01 03:49:38 +000053#include "llvm/Analysis/Loads.h"
Evan Cheng83785c82008-08-20 22:45:34 +000054#include "llvm/Target/TargetData.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000055#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000056#include "llvm/Target/TargetLowering.h"
Dan Gohmanbb466332008-08-20 21:05:57 +000057#include "llvm/Target/TargetMachine.h"
Dan Gohmanba5be5c2010-04-20 15:00:41 +000058#include "llvm/Support/ErrorHandling.h"
Devang Patelafeaae72010-12-06 22:39:26 +000059#include "llvm/Support/Debug.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000060using namespace llvm;
61
Dan Gohman84023e02010-07-10 09:00:22 +000062/// startNewBlock - Set the current block to which generated machine
63/// instructions will be appended, and clear the local CSE map.
64///
65void FastISel::startNewBlock() {
66 LocalValueMap.clear();
67
68 // Start out as null, meaining no local-value instructions have
69 // been emitted.
70 LastLocalValue = 0;
71
72 // Advance the last local value past any EH_LABEL instructions.
73 MachineBasicBlock::iterator
74 I = FuncInfo.MBB->begin(), E = FuncInfo.MBB->end();
75 while (I != E && I->getOpcode() == TargetOpcode::EH_LABEL) {
76 LastLocalValue = I;
77 ++I;
78 }
79}
80
Dan Gohmana6cb6412010-05-11 23:54:07 +000081bool FastISel::hasTrivialKill(const Value *V) const {
Dan Gohman7f0d6952010-05-14 22:53:18 +000082 // Don't consider constants or arguments to have trivial kills.
Dan Gohmana6cb6412010-05-11 23:54:07 +000083 const Instruction *I = dyn_cast<Instruction>(V);
Dan Gohman7f0d6952010-05-14 22:53:18 +000084 if (!I)
85 return false;
86
87 // No-op casts are trivially coalesced by fast-isel.
88 if (const CastInst *Cast = dyn_cast<CastInst>(I))
89 if (Cast->isNoopCast(TD.getIntPtrType(Cast->getContext())) &&
90 !hasTrivialKill(Cast->getOperand(0)))
91 return false;
92
93 // Only instructions with a single use in the same basic block are considered
94 // to have trivial kills.
95 return I->hasOneUse() &&
96 !(I->getOpcode() == Instruction::BitCast ||
97 I->getOpcode() == Instruction::PtrToInt ||
98 I->getOpcode() == Instruction::IntToPtr) &&
Gabor Greif96f1d8e2010-07-22 13:36:47 +000099 cast<Instruction>(*I->use_begin())->getParent() == I->getParent();
Dan Gohmana6cb6412010-05-11 23:54:07 +0000100}
101
Dan Gohman46510a72010-04-15 01:51:59 +0000102unsigned FastISel::getRegForValue(const Value *V) {
Owen Andersone50ed302009-08-10 22:56:29 +0000103 EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
Dan Gohman4fd55282009-04-07 20:40:11 +0000104 // Don't handle non-simple values in FastISel.
105 if (!RealVT.isSimple())
106 return 0;
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000107
108 // Ignore illegal types. We must do this before looking up the value
109 // in ValueMap because Arguments are given virtual registers regardless
110 // of whether FastISel can handle them.
Owen Anderson825b72b2009-08-11 20:47:22 +0000111 MVT VT = RealVT.getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000112 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000113 // Promote MVT::i1 to a legal type though, because it's common and easy.
114 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +0000115 VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000116 else
117 return 0;
118 }
119
Dan Gohman104e4ce2008-09-03 23:32:19 +0000120 // Look up the value to see if we already have a register for it. We
121 // cache values defined by Instructions across blocks, and other values
122 // only locally. This is because Instructions already have the SSA
Dan Gohman5c9cf192010-01-12 04:30:26 +0000123 // def-dominates-use requirement enforced.
Dan Gohmana4160c32010-07-07 16:29:44 +0000124 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
Dan Gohman84023e02010-07-10 09:00:22 +0000125 if (I != FuncInfo.ValueMap.end()) {
126 unsigned Reg = I->second;
127 return Reg;
128 }
Dan Gohman104e4ce2008-09-03 23:32:19 +0000129 unsigned Reg = LocalValueMap[V];
130 if (Reg != 0)
131 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000132
Dan Gohman97c94b82010-05-06 00:02:14 +0000133 // In bottom-up mode, just create the virtual register which will be used
134 // to hold the value. It will be materialized later.
Dan Gohman84023e02010-07-10 09:00:22 +0000135 if (isa<Instruction>(V) &&
136 (!isa<AllocaInst>(V) ||
137 !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V))))
138 return FuncInfo.InitializeRegForValue(V);
Dan Gohman97c94b82010-05-06 00:02:14 +0000139
Dan Gohmana10b8492010-07-14 01:07:44 +0000140 SavePoint SaveInsertPt = enterLocalValueArea();
Dan Gohman84023e02010-07-10 09:00:22 +0000141
142 // Materialize the value in a register. Emit any instructions in the
143 // local value area.
144 Reg = materializeRegForValue(V, VT);
145
146 leaveLocalValueArea(SaveInsertPt);
147
148 return Reg;
Dan Gohman1fdc6142010-05-03 23:36:34 +0000149}
150
Eric Christopher44a2c342010-08-17 01:30:33 +0000151/// materializeRegForValue - Helper for getRegForValue. This function is
Dan Gohman1fdc6142010-05-03 23:36:34 +0000152/// called when the value isn't already available in a register and must
153/// be materialized with new instructions.
154unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
155 unsigned Reg = 0;
156
Dan Gohman46510a72010-04-15 01:51:59 +0000157 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000158 if (CI->getValue().getActiveBits() <= 64)
159 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +0000160 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000161 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +0000162 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman1e9e8c32008-10-07 22:03:27 +0000163 // Translate this as an integer zero so that it can be
164 // local-CSE'd with actual integer zeros.
Owen Anderson1d0be152009-08-13 21:58:54 +0000165 Reg =
166 getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
Dan Gohman46510a72010-04-15 01:51:59 +0000167 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman4183e312010-04-13 17:07:06 +0000168 // Try to emit the constant directly.
Dan Gohman104e4ce2008-09-03 23:32:19 +0000169 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000170
171 if (!Reg) {
Dan Gohman4183e312010-04-13 17:07:06 +0000172 // Try to emit the constant by using an integer constant with a cast.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000173 const APFloat &Flt = CF->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000174 EVT IntVT = TLI.getPointerTy();
Dan Gohmanad368ac2008-08-27 18:10:19 +0000175
176 uint64_t x[2];
177 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000178 bool isExact;
179 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
180 APFloat::rmTowardZero, &isExact);
181 if (isExact) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000182 APInt IntVal(IntBitWidth, 2, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000183
Owen Andersone922c022009-07-22 00:24:57 +0000184 unsigned IntegerReg =
Owen Andersoneed707b2009-07-24 23:12:02 +0000185 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000186 if (IntegerReg != 0)
Dan Gohmana6cb6412010-05-11 23:54:07 +0000187 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
188 IntegerReg, /*Kill=*/false);
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000189 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000190 }
Dan Gohman46510a72010-04-15 01:51:59 +0000191 } else if (const Operator *Op = dyn_cast<Operator>(V)) {
Dan Gohman20d4be12010-07-01 02:58:57 +0000192 if (!SelectOperator(Op, Op->getOpcode()))
193 if (!isa<Instruction>(Op) ||
194 !TargetSelectInstruction(cast<Instruction>(Op)))
195 return 0;
Dan Gohman37db6cd2010-06-21 14:17:46 +0000196 Reg = lookUpRegForValue(Op);
Dan Gohman205d9252008-08-28 21:19:07 +0000197 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000198 Reg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +0000199 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
200 TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000201 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000202
Dan Gohmandceffe62008-09-25 01:28:51 +0000203 // If target-independent code couldn't handle the value, give target-specific
204 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000205 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000206 Reg = TargetMaterializeConstant(cast<Constant>(V));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000207
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000208 // Don't cache constant materializations in the general ValueMap.
209 // To do so would require tracking what uses they dominate.
Dan Gohman84023e02010-07-10 09:00:22 +0000210 if (Reg != 0) {
Dan Gohmandceffe62008-09-25 01:28:51 +0000211 LocalValueMap[V] = Reg;
Dan Gohman84023e02010-07-10 09:00:22 +0000212 LastLocalValue = MRI.getVRegDef(Reg);
213 }
Dan Gohman104e4ce2008-09-03 23:32:19 +0000214 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000215}
216
Dan Gohman46510a72010-04-15 01:51:59 +0000217unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng59fbc802008-09-09 01:26:59 +0000218 // Look up the value to see if we already have a register for it. We
219 // cache values defined by Instructions across blocks, and other values
220 // only locally. This is because Instructions already have the SSA
Dan Gohman1fdc6142010-05-03 23:36:34 +0000221 // def-dominates-use requirement enforced.
Dan Gohmana4160c32010-07-07 16:29:44 +0000222 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
223 if (I != FuncInfo.ValueMap.end())
Dan Gohman3193a682010-06-21 14:21:47 +0000224 return I->second;
Evan Cheng59fbc802008-09-09 01:26:59 +0000225 return LocalValueMap[V];
226}
227
Owen Andersoncc54e762008-08-30 00:38:46 +0000228/// UpdateValueMap - Update the value map to include the new mapping for this
229/// instruction, or insert an extra copy to get the result in a previous
230/// determined register.
231/// NOTE: This is only necessary because we might select a block that uses
232/// a value before we select the block that defines the value. It might be
233/// possible to fix this by selecting blocks in reverse postorder.
Dan Gohman46510a72010-04-15 01:51:59 +0000234unsigned FastISel::UpdateValueMap(const Value *I, unsigned Reg) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000235 if (!isa<Instruction>(I)) {
236 LocalValueMap[I] = Reg;
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000237 return Reg;
Dan Gohman40b189e2008-09-05 18:18:20 +0000238 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000239
Dan Gohmana4160c32010-07-07 16:29:44 +0000240 unsigned &AssignedReg = FuncInfo.ValueMap[I];
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000241 if (AssignedReg == 0)
Dan Gohman84023e02010-07-10 09:00:22 +0000242 // Use the new register.
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000243 AssignedReg = Reg;
Chris Lattner36e39462009-04-12 07:46:30 +0000244 else if (Reg != AssignedReg) {
Dan Gohman84023e02010-07-10 09:00:22 +0000245 // Arrange for uses of AssignedReg to be replaced by uses of Reg.
246 FuncInfo.RegFixups[AssignedReg] = Reg;
247
248 AssignedReg = Reg;
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000249 }
Dan Gohman84023e02010-07-10 09:00:22 +0000250
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000251 return AssignedReg;
Owen Andersoncc54e762008-08-30 00:38:46 +0000252}
253
Dan Gohmana6cb6412010-05-11 23:54:07 +0000254std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000255 unsigned IdxN = getRegForValue(Idx);
256 if (IdxN == 0)
257 // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000258 return std::pair<unsigned, bool>(0, false);
259
260 bool IdxNIsKill = hasTrivialKill(Idx);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000261
262 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Anderson766b5ef2009-08-11 21:59:30 +0000263 MVT PtrVT = TLI.getPointerTy();
Owen Andersone50ed302009-08-10 22:56:29 +0000264 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000265 if (IdxVT.bitsLT(PtrVT)) {
266 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND,
267 IdxN, IdxNIsKill);
268 IdxNIsKill = true;
269 }
270 else if (IdxVT.bitsGT(PtrVT)) {
271 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE,
272 IdxN, IdxNIsKill);
273 IdxNIsKill = true;
274 }
275 return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000276}
277
Dan Gohman84023e02010-07-10 09:00:22 +0000278void FastISel::recomputeInsertPt() {
279 if (getLastLocalValue()) {
280 FuncInfo.InsertPt = getLastLocalValue();
Dan Gohmanc6e59b72010-07-19 22:48:56 +0000281 FuncInfo.MBB = FuncInfo.InsertPt->getParent();
Dan Gohman84023e02010-07-10 09:00:22 +0000282 ++FuncInfo.InsertPt;
283 } else
284 FuncInfo.InsertPt = FuncInfo.MBB->getFirstNonPHI();
285
286 // Now skip past any EH_LABELs, which must remain at the beginning.
287 while (FuncInfo.InsertPt != FuncInfo.MBB->end() &&
288 FuncInfo.InsertPt->getOpcode() == TargetOpcode::EH_LABEL)
289 ++FuncInfo.InsertPt;
290}
291
Dan Gohmana10b8492010-07-14 01:07:44 +0000292FastISel::SavePoint FastISel::enterLocalValueArea() {
Dan Gohman84023e02010-07-10 09:00:22 +0000293 MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt;
Dan Gohman163f78e2010-07-14 22:01:31 +0000294 DebugLoc OldDL = DL;
Dan Gohman84023e02010-07-10 09:00:22 +0000295 recomputeInsertPt();
Dan Gohmana10b8492010-07-14 01:07:44 +0000296 DL = DebugLoc();
Dan Gohman163f78e2010-07-14 22:01:31 +0000297 SavePoint SP = { OldInsertPt, OldDL };
Dan Gohmana10b8492010-07-14 01:07:44 +0000298 return SP;
Dan Gohman84023e02010-07-10 09:00:22 +0000299}
300
Dan Gohmana10b8492010-07-14 01:07:44 +0000301void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) {
Dan Gohman84023e02010-07-10 09:00:22 +0000302 if (FuncInfo.InsertPt != FuncInfo.MBB->begin())
303 LastLocalValue = llvm::prior(FuncInfo.InsertPt);
304
305 // Restore the previous insert position.
Dan Gohmana10b8492010-07-14 01:07:44 +0000306 FuncInfo.InsertPt = OldInsertPt.InsertPt;
307 DL = OldInsertPt.DL;
Dan Gohman84023e02010-07-10 09:00:22 +0000308}
309
Dan Gohmanbdedd442008-08-20 00:11:48 +0000310/// SelectBinaryOp - Select and emit code for a binary operator instruction,
311/// which has an opcode which directly corresponds to the given ISD opcode.
312///
Dan Gohman46510a72010-04-15 01:51:59 +0000313bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000314 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmanbdedd442008-08-20 00:11:48 +0000316 // Unhandled type. Halt "fast" selection and bail.
317 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000318
Dan Gohmanb71fea22008-08-26 20:52:40 +0000319 // We only handle legal types. For example, on x86-32 the instruction
320 // selector contains all of the 64-bit instructions from x86-64,
321 // under the assumption that i64 won't be used if the target doesn't
322 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000323 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000325 // don't require additional zeroing, which makes them easy.
Owen Anderson825b72b2009-08-11 20:47:22 +0000326 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000327 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
328 ISDOpcode == ISD::XOR))
Owen Anderson23b9b192009-08-12 00:36:31 +0000329 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohman638c6832008-09-05 18:44:22 +0000330 else
331 return false;
332 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000333
Dan Gohman3df24e62008-09-03 23:12:08 +0000334 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000335 if (Op0 == 0)
336 // Unhandled operand. Halt "fast" selection and bail.
337 return false;
338
Dan Gohmana6cb6412010-05-11 23:54:07 +0000339 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
340
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000341 // Check if the second operand is a constant and handle it appropriately.
342 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000343 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000344 ISDOpcode, Op0, Op0IsKill,
345 CI->getZExtValue());
Dan Gohmanad368ac2008-08-27 18:10:19 +0000346 if (ResultReg != 0) {
347 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000348 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000349 return true;
350 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000351 }
352
Dan Gohman10df0fa2008-08-27 01:09:54 +0000353 // Check if the second operand is a constant float.
354 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000355 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000356 ISDOpcode, Op0, Op0IsKill, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000357 if (ResultReg != 0) {
358 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000359 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000360 return true;
361 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000362 }
363
Dan Gohman3df24e62008-09-03 23:12:08 +0000364 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000365 if (Op1 == 0)
366 // Unhandled operand. Halt "fast" selection and bail.
367 return false;
368
Dan Gohmana6cb6412010-05-11 23:54:07 +0000369 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
370
Dan Gohmanad368ac2008-08-27 18:10:19 +0000371 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000372 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000373 ISDOpcode,
374 Op0, Op0IsKill,
375 Op1, Op1IsKill);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000376 if (ResultReg == 0)
377 // Target-specific code wasn't able to find a machine opcode for
378 // the given ISD opcode and type. Halt "fast" selection and bail.
379 return false;
380
Dan Gohman8014e862008-08-20 00:23:20 +0000381 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000382 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000383 return true;
384}
385
Dan Gohman46510a72010-04-15 01:51:59 +0000386bool FastISel::SelectGetElementPtr(const User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000387 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000388 if (N == 0)
389 // Unhandled operand. Halt "fast" selection and bail.
390 return false;
391
Dan Gohmana6cb6412010-05-11 23:54:07 +0000392 bool NIsKill = hasTrivialKill(I->getOperand(0));
393
Evan Cheng83785c82008-08-20 22:45:34 +0000394 const Type *Ty = I->getOperand(0)->getType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000395 MVT VT = TLI.getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +0000396 for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
397 E = I->op_end(); OI != E; ++OI) {
398 const Value *Idx = *OI;
Evan Cheng83785c82008-08-20 22:45:34 +0000399 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
400 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
401 if (Field) {
402 // N = N + Offset
403 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
404 // FIXME: This can be optimized by combining the add with a
405 // subsequent one.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000406 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000407 if (N == 0)
408 // Unhandled operand. Halt "fast" selection and bail.
409 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000410 NIsKill = true;
Evan Cheng83785c82008-08-20 22:45:34 +0000411 }
412 Ty = StTy->getElementType(Field);
413 } else {
414 Ty = cast<SequentialType>(Ty)->getElementType();
415
416 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +0000417 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +0000418 if (CI->isZero()) continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000419 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +0000420 TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohmana6cb6412010-05-11 23:54:07 +0000421 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000422 if (N == 0)
423 // Unhandled operand. Halt "fast" selection and bail.
424 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000425 NIsKill = true;
Evan Cheng83785c82008-08-20 22:45:34 +0000426 continue;
427 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000428
Evan Cheng83785c82008-08-20 22:45:34 +0000429 // N = N + Idx * ElementSize;
Duncan Sands777d2302009-05-09 07:06:46 +0000430 uint64_t ElementSize = TD.getTypeAllocSize(Ty);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000431 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
432 unsigned IdxN = Pair.first;
433 bool IdxNIsKill = Pair.second;
Evan Cheng83785c82008-08-20 22:45:34 +0000434 if (IdxN == 0)
435 // Unhandled operand. Halt "fast" selection and bail.
436 return false;
437
Dan Gohman80bc6e22008-08-26 20:57:08 +0000438 if (ElementSize != 1) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000439 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000440 if (IdxN == 0)
441 // Unhandled operand. Halt "fast" selection and bail.
442 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000443 IdxNIsKill = true;
Dan Gohman80bc6e22008-08-26 20:57:08 +0000444 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000445 N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
Evan Cheng83785c82008-08-20 22:45:34 +0000446 if (N == 0)
447 // Unhandled operand. Halt "fast" selection and bail.
448 return false;
449 }
450 }
451
452 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000453 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000454 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000455}
456
Dan Gohman46510a72010-04-15 01:51:59 +0000457bool FastISel::SelectCall(const User *I) {
458 const Function *F = cast<CallInst>(I)->getCalledFunction();
Dan Gohman33134c42008-09-25 17:05:24 +0000459 if (!F) return false;
460
Dan Gohman4183e312010-04-13 17:07:06 +0000461 // Handle selected intrinsic function calls.
Dan Gohman33134c42008-09-25 17:05:24 +0000462 unsigned IID = F->getIntrinsicID();
463 switch (IID) {
464 default: break;
Bill Wendling92c1e122009-02-13 02:16:35 +0000465 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +0000466 const DbgDeclareInst *DI = cast<DbgDeclareInst>(I);
Devang Patel02f0dbd2010-05-07 22:04:20 +0000467 if (!DIVariable(DI->getVariable()).Verify() ||
Dan Gohmana4160c32010-07-07 16:29:44 +0000468 !FuncInfo.MF->getMMI().hasDebugInfo())
Devang Patel7e1e31f2009-07-02 22:43:26 +0000469 return true;
470
Dan Gohman46510a72010-04-15 01:51:59 +0000471 const Value *Address = DI->getAddress();
Devang Patel6fe75aa2010-09-14 20:29:31 +0000472 if (!Address || isa<UndefValue>(Address) || isa<AllocaInst>(Address))
Dale Johannesendc918562010-02-06 02:26:02 +0000473 return true;
Devang Patel6fe75aa2010-09-14 20:29:31 +0000474
475 unsigned Reg = 0;
476 unsigned Offset = 0;
477 if (const Argument *Arg = dyn_cast<Argument>(Address)) {
478 if (Arg->hasByValAttr()) {
479 // Byval arguments' frame index is recorded during argument lowering.
480 // Use this info directly.
481 Offset = FuncInfo.getByValArgumentFrameIndex(Arg);
482 if (Offset)
483 Reg = TRI.getFrameRegister(*FuncInfo.MF);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000484 }
Devang Patel4bafda92010-09-10 20:32:09 +0000485 }
Devang Patel6fe75aa2010-09-14 20:29:31 +0000486 if (!Reg)
487 Reg = getRegForValue(Address);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000488
Devang Patel6fe75aa2010-09-14 20:29:31 +0000489 if (Reg)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000490 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Devang Patel6fe75aa2010-09-14 20:29:31 +0000491 TII.get(TargetOpcode::DBG_VALUE))
492 .addReg(Reg, RegState::Debug).addImm(Offset)
493 .addMetadata(DI->getVariable());
Dan Gohman33134c42008-09-25 17:05:24 +0000494 return true;
Bill Wendling92c1e122009-02-13 02:16:35 +0000495 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000496 case Intrinsic::dbg_value: {
Dale Johannesen343b42e2010-04-07 01:15:14 +0000497 // This form of DBG_VALUE is target-independent.
Dan Gohman46510a72010-04-15 01:51:59 +0000498 const DbgValueInst *DI = cast<DbgValueInst>(I);
Dale Johannesen45df7612010-02-26 20:01:55 +0000499 const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohman46510a72010-04-15 01:51:59 +0000500 const Value *V = DI->getValue();
Dale Johannesen45df7612010-02-26 20:01:55 +0000501 if (!V) {
502 // Currently the optimizer can produce this; insert an undef to
503 // help debugging. Probably the optimizer should not do this.
Dan Gohman84023e02010-07-10 09:00:22 +0000504 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
505 .addReg(0U).addImm(DI->getOffset())
506 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000507 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000508 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
509 .addImm(CI->getZExtValue()).addImm(DI->getOffset())
510 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000511 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000512 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
513 .addFPImm(CF).addImm(DI->getOffset())
514 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000515 } else if (unsigned Reg = lookUpRegForValue(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000516 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
517 .addReg(Reg, RegState::Debug).addImm(DI->getOffset())
518 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000519 } else {
520 // We can't yet handle anything else here because it would require
521 // generating code, thus altering codegen because of debug info.
Devang Patelafeaae72010-12-06 22:39:26 +0000522 DEBUG(dbgs() << "Dropping debug info for " << DI);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000523 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000524 return true;
525 }
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000526 case Intrinsic::eh_exception: {
Owen Andersone50ed302009-08-10 22:56:29 +0000527 EVT VT = TLI.getValueType(I->getType());
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000528 switch (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)) {
529 default: break;
530 case TargetLowering::Expand: {
Dan Gohman84023e02010-07-10 09:00:22 +0000531 assert(FuncInfo.MBB->isLandingPad() &&
532 "Call to eh.exception not in landing pad!");
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000533 unsigned Reg = TLI.getExceptionAddressRegister();
534 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
535 unsigned ResultReg = createResultReg(RC);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +0000536 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
537 ResultReg).addReg(Reg);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000538 UpdateValueMap(I, ResultReg);
539 return true;
540 }
541 }
542 break;
543 }
Duncan Sandsb01bbdc2009-10-14 16:11:37 +0000544 case Intrinsic::eh_selector: {
Owen Andersone50ed302009-08-10 22:56:29 +0000545 EVT VT = TLI.getValueType(I->getType());
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000546 switch (TLI.getOperationAction(ISD::EHSELECTION, VT)) {
547 default: break;
548 case TargetLowering::Expand: {
Dan Gohman84023e02010-07-10 09:00:22 +0000549 if (FuncInfo.MBB->isLandingPad())
550 AddCatchInfo(*cast<CallInst>(I), &FuncInfo.MF->getMMI(), FuncInfo.MBB);
Chris Lattnered3a8062010-04-05 06:05:26 +0000551 else {
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000552#ifndef NDEBUG
Dan Gohmana4160c32010-07-07 16:29:44 +0000553 FuncInfo.CatchInfoLost.insert(cast<CallInst>(I));
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000554#endif
Chris Lattnered3a8062010-04-05 06:05:26 +0000555 // FIXME: Mark exception selector register as live in. Hack for PR1508.
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000556 unsigned Reg = TLI.getExceptionSelectorRegister();
Dan Gohman84023e02010-07-10 09:00:22 +0000557 if (Reg) FuncInfo.MBB->addLiveIn(Reg);
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000558 }
Chris Lattnered3a8062010-04-05 06:05:26 +0000559
560 unsigned Reg = TLI.getExceptionSelectorRegister();
561 EVT SrcVT = TLI.getPointerTy();
562 const TargetRegisterClass *RC = TLI.getRegClassFor(SrcVT);
563 unsigned ResultReg = createResultReg(RC);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +0000564 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
565 ResultReg).addReg(Reg);
Chris Lattnered3a8062010-04-05 06:05:26 +0000566
Dan Gohmana6cb6412010-05-11 23:54:07 +0000567 bool ResultRegIsKill = hasTrivialKill(I);
568
Chris Lattnered3a8062010-04-05 06:05:26 +0000569 // Cast the register to the type of the selector.
570 if (SrcVT.bitsGT(MVT::i32))
571 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, ISD::TRUNCATE,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000572 ResultReg, ResultRegIsKill);
Chris Lattnered3a8062010-04-05 06:05:26 +0000573 else if (SrcVT.bitsLT(MVT::i32))
574 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000575 ISD::SIGN_EXTEND, ResultReg, ResultRegIsKill);
Chris Lattnered3a8062010-04-05 06:05:26 +0000576 if (ResultReg == 0)
577 // Unhandled operand. Halt "fast" selection and bail.
578 return false;
579
580 UpdateValueMap(I, ResultReg);
581
Dan Gohmandd5b58a2008-10-14 23:54:11 +0000582 return true;
583 }
584 }
585 break;
586 }
Dan Gohman33134c42008-09-25 17:05:24 +0000587 }
Dan Gohman4183e312010-04-13 17:07:06 +0000588
589 // An arbitrary call. Bail.
Dan Gohman33134c42008-09-25 17:05:24 +0000590 return false;
591}
592
Dan Gohman46510a72010-04-15 01:51:59 +0000593bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000594 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
595 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000596
Owen Anderson825b72b2009-08-11 20:47:22 +0000597 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
598 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersond0533c92008-08-26 23:46:32 +0000599 // Unhandled type. Halt "fast" selection and bail.
600 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000601
Dan Gohman474d3b32009-03-13 23:53:06 +0000602 // Check if the destination type is legal. Or as a special case,
603 // it may be i1 if we're doing a truncate because that's
604 // easy and somewhat common.
605 if (!TLI.isTypeLegal(DstVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000606 if (DstVT != MVT::i1 || Opcode != ISD::TRUNCATE)
Dan Gohman91b6f972008-10-03 01:28:47 +0000607 // Unhandled type. Halt "fast" selection and bail.
608 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000609
610 // Check if the source operand is legal. Or as a special case,
611 // it may be i1 if we're doing zero-extension because that's
612 // easy and somewhat common.
613 if (!TLI.isTypeLegal(SrcVT))
Owen Anderson825b72b2009-08-11 20:47:22 +0000614 if (SrcVT != MVT::i1 || Opcode != ISD::ZERO_EXTEND)
Dan Gohman474d3b32009-03-13 23:53:06 +0000615 // Unhandled type. Halt "fast" selection and bail.
616 return false;
617
Dan Gohman3df24e62008-09-03 23:12:08 +0000618 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000619 if (!InputReg)
620 // Unhandled operand. Halt "fast" selection and bail.
621 return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000622
Dan Gohmana6cb6412010-05-11 23:54:07 +0000623 bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
624
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000625 // If the operand is i1, arrange for the high bits in the register to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +0000626 if (SrcVT == MVT::i1) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000627 SrcVT = TLI.getTypeToTransformTo(I->getContext(), SrcVT);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000628 InputReg = FastEmitZExtFromI1(SrcVT.getSimpleVT(), InputReg, InputRegIsKill);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000629 if (!InputReg)
630 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000631 InputRegIsKill = true;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000632 }
Dan Gohman474d3b32009-03-13 23:53:06 +0000633 // If the result is i1, truncate to the target's type for i1 first.
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 if (DstVT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +0000635 DstVT = TLI.getTypeToTransformTo(I->getContext(), DstVT);
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000636
Owen Andersond0533c92008-08-26 23:46:32 +0000637 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
638 DstVT.getSimpleVT(),
639 Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000640 InputReg, InputRegIsKill);
Owen Andersond0533c92008-08-26 23:46:32 +0000641 if (!ResultReg)
642 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000643
Dan Gohman3df24e62008-09-03 23:12:08 +0000644 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000645 return true;
646}
647
Dan Gohman46510a72010-04-15 01:51:59 +0000648bool FastISel::SelectBitCast(const User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000649 // If the bitcast doesn't change the type, just use the operand value.
650 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000651 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000652 if (Reg == 0)
653 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000654 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000655 return true;
656 }
657
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000658 // Bitcasts of other values become reg-reg copies or BITCAST operators.
Owen Andersone50ed302009-08-10 22:56:29 +0000659 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
660 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000661
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
663 DstVT == MVT::Other || !DstVT.isSimple() ||
Owen Andersond0533c92008-08-26 23:46:32 +0000664 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
665 // Unhandled type. Halt "fast" selection and bail.
666 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000667
Dan Gohman3df24e62008-09-03 23:12:08 +0000668 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000669 if (Op0 == 0)
670 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000671 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000672
673 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000674
Dan Gohmanad368ac2008-08-27 18:10:19 +0000675 // First, try to perform the bitcast by inserting a reg-reg copy.
676 unsigned ResultReg = 0;
677 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
678 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
679 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
Jakob Stoklund Olesene7917bb2010-07-11 05:16:54 +0000680 // Don't attempt a cross-class copy. It will likely fail.
681 if (SrcClass == DstClass) {
682 ResultReg = createResultReg(DstClass);
683 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
684 ResultReg).addReg(Op0);
685 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000686 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000687
688 // If the reg-reg copy failed, select a BITCAST opcode.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000689 if (!ResultReg)
690 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000691 ISD::BITCAST, Op0, Op0IsKill);
692
Dan Gohmanad368ac2008-08-27 18:10:19 +0000693 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000694 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000695
Dan Gohman3df24e62008-09-03 23:12:08 +0000696 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000697 return true;
698}
699
Dan Gohman3df24e62008-09-03 23:12:08 +0000700bool
Dan Gohman46510a72010-04-15 01:51:59 +0000701FastISel::SelectInstruction(const Instruction *I) {
Dan Gohmane8c92dd2010-04-23 15:29:50 +0000702 // Just before the terminator instruction, insert instructions to
703 // feed PHI nodes in successor blocks.
704 if (isa<TerminatorInst>(I))
705 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
706 return false;
707
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000708 DL = I->getDebugLoc();
709
Dan Gohman6e3ff372009-12-05 01:27:58 +0000710 // First, try doing target-independent selection.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000711 if (SelectOperator(I, I->getOpcode())) {
712 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000713 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000714 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000715
716 // Next, try calling the target to attempt to handle the instruction.
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000717 if (TargetSelectInstruction(I)) {
718 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000719 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000720 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000721
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000722 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000723 return false;
Dan Gohman40b189e2008-09-05 18:18:20 +0000724}
725
Dan Gohmand98d6202008-10-02 22:15:21 +0000726/// FastEmitBranch - Emit an unconditional branch to the given block,
727/// unless it is the immediate (fall-through) successor, and update
728/// the CFG.
729void
Stuart Hastings3bf91252010-06-17 22:43:56 +0000730FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DL) {
Dan Gohman84023e02010-07-10 09:00:22 +0000731 if (FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
Dan Gohmand98d6202008-10-02 22:15:21 +0000732 // The unconditional fall-through case, which needs no instructions.
733 } else {
734 // The unconditional branch case.
Dan Gohman84023e02010-07-10 09:00:22 +0000735 TII.InsertBranch(*FuncInfo.MBB, MSucc, NULL,
736 SmallVector<MachineOperand, 0>(), DL);
Dan Gohmand98d6202008-10-02 22:15:21 +0000737 }
Dan Gohman84023e02010-07-10 09:00:22 +0000738 FuncInfo.MBB->addSuccessor(MSucc);
Dan Gohmand98d6202008-10-02 22:15:21 +0000739}
740
Dan Gohman3d45a852009-09-03 22:53:57 +0000741/// SelectFNeg - Emit an FNeg operation.
742///
743bool
Dan Gohman46510a72010-04-15 01:51:59 +0000744FastISel::SelectFNeg(const User *I) {
Dan Gohman3d45a852009-09-03 22:53:57 +0000745 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
746 if (OpReg == 0) return false;
747
Dan Gohmana6cb6412010-05-11 23:54:07 +0000748 bool OpRegIsKill = hasTrivialKill(I);
749
Dan Gohman4a215a12009-09-11 00:36:43 +0000750 // If the target has ISD::FNEG, use it.
751 EVT VT = TLI.getValueType(I->getType());
752 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000753 ISD::FNEG, OpReg, OpRegIsKill);
Dan Gohman4a215a12009-09-11 00:36:43 +0000754 if (ResultReg != 0) {
755 UpdateValueMap(I, ResultReg);
756 return true;
757 }
758
Dan Gohman5e5abb72009-09-11 00:34:46 +0000759 // Bitcast the value to integer, twiddle the sign bit with xor,
760 // and then bitcast it back to floating-point.
Dan Gohman3d45a852009-09-03 22:53:57 +0000761 if (VT.getSizeInBits() > 64) return false;
Dan Gohman5e5abb72009-09-11 00:34:46 +0000762 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
763 if (!TLI.isTypeLegal(IntVT))
764 return false;
765
766 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000767 ISD::BITCAST, OpReg, OpRegIsKill);
Dan Gohman5e5abb72009-09-11 00:34:46 +0000768 if (IntReg == 0)
769 return false;
770
Dan Gohmana6cb6412010-05-11 23:54:07 +0000771 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
772 IntReg, /*Kill=*/true,
Dan Gohman5e5abb72009-09-11 00:34:46 +0000773 UINT64_C(1) << (VT.getSizeInBits()-1),
774 IntVT.getSimpleVT());
775 if (IntResultReg == 0)
776 return false;
777
778 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000779 ISD::BITCAST, IntResultReg, /*Kill=*/true);
Dan Gohman3d45a852009-09-03 22:53:57 +0000780 if (ResultReg == 0)
781 return false;
782
783 UpdateValueMap(I, ResultReg);
784 return true;
785}
786
Dan Gohman40b189e2008-09-05 18:18:20 +0000787bool
Dan Gohman46510a72010-04-15 01:51:59 +0000788FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000789 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000790 case Instruction::Add:
791 return SelectBinaryOp(I, ISD::ADD);
792 case Instruction::FAdd:
793 return SelectBinaryOp(I, ISD::FADD);
794 case Instruction::Sub:
795 return SelectBinaryOp(I, ISD::SUB);
796 case Instruction::FSub:
Dan Gohman3d45a852009-09-03 22:53:57 +0000797 // FNeg is currently represented in LLVM IR as a special case of FSub.
798 if (BinaryOperator::isFNeg(I))
799 return SelectFNeg(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000800 return SelectBinaryOp(I, ISD::FSUB);
801 case Instruction::Mul:
802 return SelectBinaryOp(I, ISD::MUL);
803 case Instruction::FMul:
804 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman3df24e62008-09-03 23:12:08 +0000805 case Instruction::SDiv:
806 return SelectBinaryOp(I, ISD::SDIV);
807 case Instruction::UDiv:
808 return SelectBinaryOp(I, ISD::UDIV);
809 case Instruction::FDiv:
810 return SelectBinaryOp(I, ISD::FDIV);
811 case Instruction::SRem:
812 return SelectBinaryOp(I, ISD::SREM);
813 case Instruction::URem:
814 return SelectBinaryOp(I, ISD::UREM);
815 case Instruction::FRem:
816 return SelectBinaryOp(I, ISD::FREM);
817 case Instruction::Shl:
818 return SelectBinaryOp(I, ISD::SHL);
819 case Instruction::LShr:
820 return SelectBinaryOp(I, ISD::SRL);
821 case Instruction::AShr:
822 return SelectBinaryOp(I, ISD::SRA);
823 case Instruction::And:
824 return SelectBinaryOp(I, ISD::AND);
825 case Instruction::Or:
826 return SelectBinaryOp(I, ISD::OR);
827 case Instruction::Xor:
828 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000829
Dan Gohman3df24e62008-09-03 23:12:08 +0000830 case Instruction::GetElementPtr:
831 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000832
Dan Gohman3df24e62008-09-03 23:12:08 +0000833 case Instruction::Br: {
Dan Gohman46510a72010-04-15 01:51:59 +0000834 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000835
Dan Gohman3df24e62008-09-03 23:12:08 +0000836 if (BI->isUnconditional()) {
Dan Gohman46510a72010-04-15 01:51:59 +0000837 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohmana4160c32010-07-07 16:29:44 +0000838 MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
Stuart Hastings3bf91252010-06-17 22:43:56 +0000839 FastEmitBranch(MSucc, BI->getDebugLoc());
Dan Gohman3df24e62008-09-03 23:12:08 +0000840 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000841 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000842
843 // Conditional branches are not handed yet.
844 // Halt "fast" selection and bail.
845 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000846 }
847
Dan Gohman087c8502008-09-05 01:08:41 +0000848 case Instruction::Unreachable:
849 // Nothing to emit.
850 return true;
851
Dan Gohman0586d912008-09-10 20:11:02 +0000852 case Instruction::Alloca:
853 // FunctionLowering has the static-sized case covered.
Dan Gohmana4160c32010-07-07 16:29:44 +0000854 if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
Dan Gohman0586d912008-09-10 20:11:02 +0000855 return true;
856
857 // Dynamic-sized alloca is not handled yet.
858 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000859
Dan Gohman33134c42008-09-25 17:05:24 +0000860 case Instruction::Call:
861 return SelectCall(I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000862
Dan Gohman3df24e62008-09-03 23:12:08 +0000863 case Instruction::BitCast:
864 return SelectBitCast(I);
865
866 case Instruction::FPToSI:
867 return SelectCast(I, ISD::FP_TO_SINT);
868 case Instruction::ZExt:
869 return SelectCast(I, ISD::ZERO_EXTEND);
870 case Instruction::SExt:
871 return SelectCast(I, ISD::SIGN_EXTEND);
872 case Instruction::Trunc:
873 return SelectCast(I, ISD::TRUNCATE);
874 case Instruction::SIToFP:
875 return SelectCast(I, ISD::SINT_TO_FP);
876
877 case Instruction::IntToPtr: // Deliberate fall-through.
878 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +0000879 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
880 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman3df24e62008-09-03 23:12:08 +0000881 if (DstVT.bitsGT(SrcVT))
882 return SelectCast(I, ISD::ZERO_EXTEND);
883 if (DstVT.bitsLT(SrcVT))
884 return SelectCast(I, ISD::TRUNCATE);
885 unsigned Reg = getRegForValue(I->getOperand(0));
886 if (Reg == 0) return false;
887 UpdateValueMap(I, Reg);
888 return true;
889 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000890
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000891 case Instruction::PHI:
892 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
893
Dan Gohman3df24e62008-09-03 23:12:08 +0000894 default:
895 // Unhandled instruction. Halt "fast" selection and bail.
896 return false;
897 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000898}
899
Dan Gohmana4160c32010-07-07 16:29:44 +0000900FastISel::FastISel(FunctionLoweringInfo &funcInfo)
Dan Gohman84023e02010-07-10 09:00:22 +0000901 : FuncInfo(funcInfo),
Dan Gohmana4160c32010-07-07 16:29:44 +0000902 MRI(FuncInfo.MF->getRegInfo()),
903 MFI(*FuncInfo.MF->getFrameInfo()),
904 MCP(*FuncInfo.MF->getConstantPool()),
905 TM(FuncInfo.MF->getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000906 TD(*TM.getTargetData()),
907 TII(*TM.getInstrInfo()),
Dan Gohmana7a0ed72010-05-05 23:58:35 +0000908 TLI(*TM.getTargetLowering()),
Dan Gohman84023e02010-07-10 09:00:22 +0000909 TRI(*TM.getRegisterInfo()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000910}
911
Dan Gohmane285a742008-08-14 21:51:29 +0000912FastISel::~FastISel() {}
913
Owen Anderson825b72b2009-08-11 20:47:22 +0000914unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000915 unsigned) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000916 return 0;
917}
918
Owen Anderson825b72b2009-08-11 20:47:22 +0000919unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000920 unsigned,
921 unsigned /*Op0*/, bool /*Op0IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000922 return 0;
923}
924
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000925unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000926 unsigned,
927 unsigned /*Op0*/, bool /*Op0IsKill*/,
928 unsigned /*Op1*/, bool /*Op1IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000929 return 0;
930}
931
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000932unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000933 return 0;
934}
935
Owen Anderson825b72b2009-08-11 20:47:22 +0000936unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohman46510a72010-04-15 01:51:59 +0000937 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000938 return 0;
939}
940
Owen Anderson825b72b2009-08-11 20:47:22 +0000941unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000942 unsigned,
943 unsigned /*Op0*/, bool /*Op0IsKill*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000944 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000945 return 0;
946}
947
Owen Anderson825b72b2009-08-11 20:47:22 +0000948unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000949 unsigned,
950 unsigned /*Op0*/, bool /*Op0IsKill*/,
Dan Gohman46510a72010-04-15 01:51:59 +0000951 const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000952 return 0;
953}
954
Owen Anderson825b72b2009-08-11 20:47:22 +0000955unsigned FastISel::FastEmit_rri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000956 unsigned,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000957 unsigned /*Op0*/, bool /*Op0IsKill*/,
958 unsigned /*Op1*/, bool /*Op1IsKill*/,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000959 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000960 return 0;
961}
962
963/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
964/// to emit an instruction with an immediate operand using FastEmit_ri.
965/// If that fails, it materializes the immediate into a register and try
966/// FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000967unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000968 unsigned Op0, bool Op0IsKill,
969 uint64_t Imm, MVT ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000970 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000971 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000972 if (ResultReg != 0)
973 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000974 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000975 if (MaterialReg == 0)
976 return 0;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000977 return FastEmit_rr(VT, VT, Opcode,
978 Op0, Op0IsKill,
979 MaterialReg, /*Kill=*/true);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000980}
981
Dan Gohman10df0fa2008-08-27 01:09:54 +0000982/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
983/// to emit an instruction with a floating-point immediate operand using
984/// FastEmit_rf. If that fails, it materializes the immediate into a register
985/// and try FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +0000986unsigned FastISel::FastEmit_rf_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000987 unsigned Op0, bool Op0IsKill,
988 const ConstantFP *FPImm, MVT ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000989 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000990 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, Op0IsKill, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000991 if (ResultReg != 0)
992 return ResultReg;
993
994 // Materialize the constant in a register.
995 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
996 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000997 // If the target doesn't have a way to directly enter a floating-point
998 // value into a register, use an alternate approach.
999 // TODO: The current approach only supports floating-point constants
1000 // that can be constructed by conversion from integer values. This should
1001 // be replaced by code that creates a load from a constant-pool entry,
1002 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +00001003 const APFloat &Flt = FPImm->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +00001004 EVT IntVT = TLI.getPointerTy();
Dan Gohman10df0fa2008-08-27 01:09:54 +00001005
1006 uint64_t x[2];
1007 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +00001008 bool isExact;
1009 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
1010 APFloat::rmTowardZero, &isExact);
1011 if (!isExact)
Dan Gohman10df0fa2008-08-27 01:09:54 +00001012 return 0;
1013 APInt IntVal(IntBitWidth, 2, x);
1014
1015 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
1016 ISD::Constant, IntVal.getZExtValue());
1017 if (IntegerReg == 0)
1018 return 0;
1019 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001020 ISD::SINT_TO_FP, IntegerReg, /*Kill=*/true);
Dan Gohman10df0fa2008-08-27 01:09:54 +00001021 if (MaterialReg == 0)
1022 return 0;
1023 }
Dan Gohmana6cb6412010-05-11 23:54:07 +00001024 return FastEmit_rr(VT, VT, Opcode,
1025 Op0, Op0IsKill,
1026 MaterialReg, /*Kill=*/true);
Dan Gohman10df0fa2008-08-27 01:09:54 +00001027}
1028
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001029unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
1030 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +00001031}
1032
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001033unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +00001034 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001035 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +00001036 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001037
Dan Gohman84023e02010-07-10 09:00:22 +00001038 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001039 return ResultReg;
1040}
1041
1042unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
1043 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001044 unsigned Op0, bool Op0IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001045 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +00001046 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001047
Evan Cheng5960e4e2008-09-08 08:38:20 +00001048 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001049 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1050 .addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001051 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001052 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1053 .addReg(Op0, Op0IsKill * RegState::Kill);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001054 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1055 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001056 }
1057
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001058 return ResultReg;
1059}
1060
1061unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
1062 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001063 unsigned Op0, bool Op0IsKill,
1064 unsigned Op1, bool Op1IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001065 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +00001066 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001067
Evan Cheng5960e4e2008-09-08 08:38:20 +00001068 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001069 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001070 .addReg(Op0, Op0IsKill * RegState::Kill)
1071 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001072 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001073 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001074 .addReg(Op0, Op0IsKill * RegState::Kill)
1075 .addReg(Op1, Op1IsKill * RegState::Kill);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001076 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1077 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001078 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001079 return ResultReg;
1080}
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001081
1082unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1083 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001084 unsigned Op0, bool Op0IsKill,
1085 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001086 unsigned ResultReg = createResultReg(RC);
1087 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1088
Evan Cheng5960e4e2008-09-08 08:38:20 +00001089 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001090 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001091 .addReg(Op0, Op0IsKill * RegState::Kill)
1092 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001093 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001094 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001095 .addReg(Op0, Op0IsKill * RegState::Kill)
1096 .addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001097 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1098 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001099 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001100 return ResultReg;
1101}
1102
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001103unsigned FastISel::FastEmitInst_rii(unsigned MachineInstOpcode,
1104 const TargetRegisterClass *RC,
1105 unsigned Op0, bool Op0IsKill,
1106 uint64_t Imm1, uint64_t Imm2) {
1107 unsigned ResultReg = createResultReg(RC);
1108 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1109
1110 if (II.getNumDefs() >= 1)
1111 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1112 .addReg(Op0, Op0IsKill * RegState::Kill)
1113 .addImm(Imm1)
1114 .addImm(Imm2);
1115 else {
1116 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1117 .addReg(Op0, Op0IsKill * RegState::Kill)
1118 .addImm(Imm1)
1119 .addImm(Imm2);
1120 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1121 ResultReg).addReg(II.ImplicitDefs[0]);
1122 }
1123 return ResultReg;
1124}
1125
Dan Gohman10df0fa2008-08-27 01:09:54 +00001126unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
1127 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001128 unsigned Op0, bool Op0IsKill,
1129 const ConstantFP *FPImm) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001130 unsigned ResultReg = createResultReg(RC);
1131 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1132
Evan Cheng5960e4e2008-09-08 08:38:20 +00001133 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001134 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001135 .addReg(Op0, Op0IsKill * RegState::Kill)
1136 .addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001137 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001138 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001139 .addReg(Op0, Op0IsKill * RegState::Kill)
1140 .addFPImm(FPImm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001141 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1142 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001143 }
Dan Gohman10df0fa2008-08-27 01:09:54 +00001144 return ResultReg;
1145}
1146
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001147unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
1148 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001149 unsigned Op0, bool Op0IsKill,
1150 unsigned Op1, bool Op1IsKill,
1151 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001152 unsigned ResultReg = createResultReg(RC);
1153 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1154
Evan Cheng5960e4e2008-09-08 08:38:20 +00001155 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001156 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001157 .addReg(Op0, Op0IsKill * RegState::Kill)
1158 .addReg(Op1, Op1IsKill * RegState::Kill)
1159 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001160 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001161 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001162 .addReg(Op0, Op0IsKill * RegState::Kill)
1163 .addReg(Op1, Op1IsKill * RegState::Kill)
1164 .addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001165 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1166 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001167 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001168 return ResultReg;
1169}
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001170
1171unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1172 const TargetRegisterClass *RC,
1173 uint64_t Imm) {
1174 unsigned ResultReg = createResultReg(RC);
1175 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001176
Evan Cheng5960e4e2008-09-08 08:38:20 +00001177 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001178 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001179 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001180 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001181 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1182 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001183 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001184 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +00001185}
Owen Anderson8970f002008-08-27 22:30:02 +00001186
Owen Anderson825b72b2009-08-11 20:47:22 +00001187unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001188 unsigned Op0, bool Op0IsKill,
1189 uint32_t Idx) {
Evan Cheng536ab132009-01-22 09:10:11 +00001190 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001191 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
1192 "Cannot yet extract from physregs");
Dan Gohman84023e02010-07-10 09:00:22 +00001193 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
1194 DL, TII.get(TargetOpcode::COPY), ResultReg)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001195 .addReg(Op0, getKillRegState(Op0IsKill), Idx);
Owen Anderson8970f002008-08-27 22:30:02 +00001196 return ResultReg;
1197}
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001198
1199/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1200/// with all but the least significant bit set to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001201unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1202 return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001203}
Dan Gohmanf81eca02010-04-22 20:46:50 +00001204
1205/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1206/// Emit code to ensure constants are copied into registers when needed.
1207/// Remember the virtual registers that need to be added to the Machine PHI
1208/// nodes as input. We cannot just directly add them, because expansion
1209/// might result in multiple MBB's for one BB. As such, the start of the
1210/// BB might correspond to a different MBB than the end.
1211bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1212 const TerminatorInst *TI = LLVMBB->getTerminator();
1213
1214 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohmana4160c32010-07-07 16:29:44 +00001215 unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001216
1217 // Check successor nodes' PHI nodes that expect a constant to be available
1218 // from this block.
1219 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1220 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1221 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmana4160c32010-07-07 16:29:44 +00001222 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Dan Gohmanf81eca02010-04-22 20:46:50 +00001223
1224 // If this terminator has multiple identical successors (common for
1225 // switches), only handle each succ once.
1226 if (!SuccsHandled.insert(SuccMBB)) continue;
1227
1228 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1229
1230 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1231 // nodes and Machine PHI nodes, but the incoming operands have not been
1232 // emitted yet.
1233 for (BasicBlock::const_iterator I = SuccBB->begin();
1234 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanfb95f892010-05-07 01:10:20 +00001235
Dan Gohmanf81eca02010-04-22 20:46:50 +00001236 // Ignore dead phi's.
1237 if (PN->use_empty()) continue;
1238
1239 // Only handle legal types. Two interesting things to note here. First,
1240 // by bailing out early, we may leave behind some dead instructions,
1241 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
1242 // own moves. Second, this check is necessary becuase FastISel doesn't
Dan Gohman89496d02010-07-02 00:10:16 +00001243 // use CreateRegs to create registers, so it always creates
Dan Gohmanf81eca02010-04-22 20:46:50 +00001244 // exactly one register for each non-void instruction.
1245 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1246 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
1247 // Promote MVT::i1.
1248 if (VT == MVT::i1)
1249 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1250 else {
Dan Gohmana4160c32010-07-07 16:29:44 +00001251 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001252 return false;
1253 }
1254 }
1255
1256 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1257
Dan Gohmanfb95f892010-05-07 01:10:20 +00001258 // Set the DebugLoc for the copy. Prefer the location of the operand
1259 // if there is one; use the location of the PHI otherwise.
1260 DL = PN->getDebugLoc();
1261 if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
1262 DL = Inst->getDebugLoc();
1263
Dan Gohmanf81eca02010-04-22 20:46:50 +00001264 unsigned Reg = getRegForValue(PHIOp);
1265 if (Reg == 0) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001266 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001267 return false;
1268 }
Dan Gohmana4160c32010-07-07 16:29:44 +00001269 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohmanfb95f892010-05-07 01:10:20 +00001270 DL = DebugLoc();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001271 }
1272 }
1273
1274 return true;
1275}