blob: aa1686fb7d553302aaa6eca757374d287b659479 [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
Chad Rosier053e69a2011-11-16 21:05:28 +000042#define DEBUG_TYPE "isel"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000043#include "llvm/CodeGen/FastISel.h"
David Blaikie6d9dbd52013-06-16 20:34:15 +000044#include "llvm/ADT/Optional.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000045#include "llvm/ADT/Statistic.h"
46#include "llvm/Analysis/Loads.h"
47#include "llvm/CodeGen/Analysis.h"
48#include "llvm/CodeGen/FunctionLoweringInfo.h"
49#include "llvm/CodeGen/MachineInstrBuilder.h"
50#include "llvm/CodeGen/MachineModuleInfo.h"
51#include "llvm/CodeGen/MachineRegisterInfo.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000052#include "llvm/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000053#include "llvm/IR/DataLayout.h"
54#include "llvm/IR/Function.h"
55#include "llvm/IR/GlobalVariable.h"
56#include "llvm/IR/Instructions.h"
57#include "llvm/IR/IntrinsicInst.h"
58#include "llvm/IR/Operator.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000059#include "llvm/Support/Debug.h"
60#include "llvm/Support/ErrorHandling.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000061#include "llvm/Target/TargetInstrInfo.h"
Bob Wilsond49edb72012-08-03 04:06:28 +000062#include "llvm/Target/TargetLibraryInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000063#include "llvm/Target/TargetLowering.h"
Dan Gohmanbb466332008-08-20 21:05:57 +000064#include "llvm/Target/TargetMachine.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000065using namespace llvm;
66
Chad Rosieraa5656c2011-11-28 19:59:09 +000067STATISTIC(NumFastIselSuccessIndependent, "Number of insts selected by "
68 "target-independent selector");
69STATISTIC(NumFastIselSuccessTarget, "Number of insts selected by "
70 "target-specific selector");
Chad Rosierae6f2cb2011-11-29 19:40:47 +000071STATISTIC(NumFastIselDead, "Number of dead insts removed on failure");
Chad Rosier053e69a2011-11-16 21:05:28 +000072
Dan Gohman84023e02010-07-10 09:00:22 +000073/// startNewBlock - Set the current block to which generated machine
74/// instructions will be appended, and clear the local CSE map.
75///
76void FastISel::startNewBlock() {
77 LocalValueMap.clear();
78
Ivan Krasin74af88a2011-08-18 22:06:10 +000079 EmitStartPt = 0;
Dan Gohman84023e02010-07-10 09:00:22 +000080
Ivan Krasin74af88a2011-08-18 22:06:10 +000081 // Advance the emit start point past any EH_LABEL instructions.
Dan Gohman84023e02010-07-10 09:00:22 +000082 MachineBasicBlock::iterator
83 I = FuncInfo.MBB->begin(), E = FuncInfo.MBB->end();
84 while (I != E && I->getOpcode() == TargetOpcode::EH_LABEL) {
Ivan Krasin74af88a2011-08-18 22:06:10 +000085 EmitStartPt = I;
Dan Gohman84023e02010-07-10 09:00:22 +000086 ++I;
87 }
Ivan Krasin74af88a2011-08-18 22:06:10 +000088 LastLocalValue = EmitStartPt;
89}
90
Evan Cheng092e5e72013-02-11 01:27:15 +000091bool FastISel::LowerArguments() {
92 if (!FuncInfo.CanLowerReturn)
93 // Fallback to SDISel argument lowering code to deal with sret pointer
94 // parameter.
95 return false;
96
97 if (!FastLowerArguments())
98 return false;
99
David Blaikie19489102013-06-21 22:56:30 +0000100 // Enter arguments into ValueMap for uses in non-entry BBs.
Evan Cheng092e5e72013-02-11 01:27:15 +0000101 for (Function::const_arg_iterator I = FuncInfo.Fn->arg_begin(),
102 E = FuncInfo.Fn->arg_end(); I != E; ++I) {
David Blaikie19489102013-06-21 22:56:30 +0000103 DenseMap<const Value *, unsigned>::iterator VI = LocalValueMap.find(I);
104 assert(VI != LocalValueMap.end() && "Missed an argument?");
105 FuncInfo.ValueMap[I] = VI->second;
Evan Cheng092e5e72013-02-11 01:27:15 +0000106 }
107 return true;
108}
109
Ivan Krasin74af88a2011-08-18 22:06:10 +0000110void FastISel::flushLocalValueMap() {
111 LocalValueMap.clear();
112 LastLocalValue = EmitStartPt;
113 recomputeInsertPt();
Dan Gohman84023e02010-07-10 09:00:22 +0000114}
115
Dan Gohmana6cb6412010-05-11 23:54:07 +0000116bool FastISel::hasTrivialKill(const Value *V) const {
Dan Gohman7f0d6952010-05-14 22:53:18 +0000117 // Don't consider constants or arguments to have trivial kills.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000118 const Instruction *I = dyn_cast<Instruction>(V);
Dan Gohman7f0d6952010-05-14 22:53:18 +0000119 if (!I)
120 return false;
121
122 // No-op casts are trivially coalesced by fast-isel.
123 if (const CastInst *Cast = dyn_cast<CastInst>(I))
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000124 if (Cast->isNoopCast(TD.getIntPtrType(Cast->getContext())) &&
125 !hasTrivialKill(Cast->getOperand(0)))
Dan Gohman7f0d6952010-05-14 22:53:18 +0000126 return false;
127
Chad Rosier22b34cc2011-11-15 23:34:05 +0000128 // GEPs with all zero indices are trivially coalesced by fast-isel.
129 if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I))
130 if (GEP->hasAllZeroIndices() && !hasTrivialKill(GEP->getOperand(0)))
131 return false;
132
Dan Gohman7f0d6952010-05-14 22:53:18 +0000133 // Only instructions with a single use in the same basic block are considered
134 // to have trivial kills.
135 return I->hasOneUse() &&
136 !(I->getOpcode() == Instruction::BitCast ||
137 I->getOpcode() == Instruction::PtrToInt ||
138 I->getOpcode() == Instruction::IntToPtr) &&
Gabor Greif96f1d8e2010-07-22 13:36:47 +0000139 cast<Instruction>(*I->use_begin())->getParent() == I->getParent();
Dan Gohmana6cb6412010-05-11 23:54:07 +0000140}
141
Dan Gohman46510a72010-04-15 01:51:59 +0000142unsigned FastISel::getRegForValue(const Value *V) {
Owen Andersone50ed302009-08-10 22:56:29 +0000143 EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
Dan Gohman4fd55282009-04-07 20:40:11 +0000144 // Don't handle non-simple values in FastISel.
145 if (!RealVT.isSimple())
146 return 0;
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000147
148 // Ignore illegal types. We must do this before looking up the value
149 // in ValueMap because Arguments are given virtual registers regardless
150 // of whether FastISel can handle them.
Owen Anderson825b72b2009-08-11 20:47:22 +0000151 MVT VT = RealVT.getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000152 if (!TLI.isTypeLegal(VT)) {
Eli Friedman76927d732011-05-25 23:49:02 +0000153 // Handle integer promotions, though, because they're common and easy.
154 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Owen Anderson23b9b192009-08-12 00:36:31 +0000155 VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000156 else
157 return 0;
158 }
159
Eric Christopher4e270272012-03-20 01:07:47 +0000160 // Look up the value to see if we already have a register for it.
161 unsigned Reg = lookUpRegForValue(V);
Dan Gohman104e4ce2008-09-03 23:32:19 +0000162 if (Reg != 0)
163 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000164
Dan Gohman97c94b82010-05-06 00:02:14 +0000165 // In bottom-up mode, just create the virtual register which will be used
166 // to hold the value. It will be materialized later.
Dan Gohman84023e02010-07-10 09:00:22 +0000167 if (isa<Instruction>(V) &&
168 (!isa<AllocaInst>(V) ||
169 !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V))))
170 return FuncInfo.InitializeRegForValue(V);
Dan Gohman97c94b82010-05-06 00:02:14 +0000171
Eric Christopher76ad43c2012-10-03 08:10:01 +0000172 SavePoint SaveInsertPt = enterLocalValueArea();
Dan Gohman84023e02010-07-10 09:00:22 +0000173
174 // Materialize the value in a register. Emit any instructions in the
175 // local value area.
176 Reg = materializeRegForValue(V, VT);
177
Eric Christopher76ad43c2012-10-03 08:10:01 +0000178 leaveLocalValueArea(SaveInsertPt);
Dan Gohman84023e02010-07-10 09:00:22 +0000179
180 return Reg;
Dan Gohman1fdc6142010-05-03 23:36:34 +0000181}
182
Eric Christopher44a2c342010-08-17 01:30:33 +0000183/// materializeRegForValue - Helper for getRegForValue. This function is
Dan Gohman1fdc6142010-05-03 23:36:34 +0000184/// called when the value isn't already available in a register and must
185/// be materialized with new instructions.
186unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
187 unsigned Reg = 0;
188
Dan Gohman46510a72010-04-15 01:51:59 +0000189 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000190 if (CI->getValue().getActiveBits() <= 64)
191 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +0000192 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000193 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +0000194 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman1e9e8c32008-10-07 22:03:27 +0000195 // Translate this as an integer zero so that it can be
196 // local-CSE'd with actual integer zeros.
Owen Anderson1d0be152009-08-13 21:58:54 +0000197 Reg =
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000198 getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
Dan Gohman46510a72010-04-15 01:51:59 +0000199 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Eli Friedmanbd125382011-04-28 00:42:03 +0000200 if (CF->isNullValue()) {
Eli Friedman2790ba82011-04-27 22:41:55 +0000201 Reg = TargetMaterializeFloatZero(CF);
202 } else {
203 // Try to emit the constant directly.
204 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
205 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000206
207 if (!Reg) {
Dan Gohman4183e312010-04-13 17:07:06 +0000208 // Try to emit the constant by using an integer constant with a cast.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000209 const APFloat &Flt = CF->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000210 EVT IntVT = TLI.getPointerTy();
Dan Gohmanad368ac2008-08-27 18:10:19 +0000211
212 uint64_t x[2];
213 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000214 bool isExact;
215 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
Eric Christopherc415af22012-03-20 01:07:56 +0000216 APFloat::rmTowardZero, &isExact);
Dale Johannesen23a98552008-10-09 23:00:39 +0000217 if (isExact) {
Jeffrey Yasskin3ba292d2011-07-18 21:45:40 +0000218 APInt IntVal(IntBitWidth, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000219
Owen Andersone922c022009-07-22 00:24:57 +0000220 unsigned IntegerReg =
Owen Andersoneed707b2009-07-24 23:12:02 +0000221 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000222 if (IntegerReg != 0)
Dan Gohmana6cb6412010-05-11 23:54:07 +0000223 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
224 IntegerReg, /*Kill=*/false);
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000225 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000226 }
Dan Gohman46510a72010-04-15 01:51:59 +0000227 } else if (const Operator *Op = dyn_cast<Operator>(V)) {
Dan Gohman20d4be12010-07-01 02:58:57 +0000228 if (!SelectOperator(Op, Op->getOpcode()))
229 if (!isa<Instruction>(Op) ||
230 !TargetSelectInstruction(cast<Instruction>(Op)))
231 return 0;
Dan Gohman37db6cd2010-06-21 14:17:46 +0000232 Reg = lookUpRegForValue(Op);
Dan Gohman205d9252008-08-28 21:19:07 +0000233 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000234 Reg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman84023e02010-07-10 09:00:22 +0000235 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
236 TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000237 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000238
Dan Gohmandceffe62008-09-25 01:28:51 +0000239 // If target-independent code couldn't handle the value, give target-specific
240 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000241 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000242 Reg = TargetMaterializeConstant(cast<Constant>(V));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000243
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000244 // Don't cache constant materializations in the general ValueMap.
245 // To do so would require tracking what uses they dominate.
Dan Gohman84023e02010-07-10 09:00:22 +0000246 if (Reg != 0) {
Dan Gohmandceffe62008-09-25 01:28:51 +0000247 LocalValueMap[V] = Reg;
Dan Gohman84023e02010-07-10 09:00:22 +0000248 LastLocalValue = MRI.getVRegDef(Reg);
249 }
Dan Gohman104e4ce2008-09-03 23:32:19 +0000250 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000251}
252
Dan Gohman46510a72010-04-15 01:51:59 +0000253unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng59fbc802008-09-09 01:26:59 +0000254 // Look up the value to see if we already have a register for it. We
255 // cache values defined by Instructions across blocks, and other values
256 // only locally. This is because Instructions already have the SSA
Dan Gohman1fdc6142010-05-03 23:36:34 +0000257 // def-dominates-use requirement enforced.
Dan Gohmana4160c32010-07-07 16:29:44 +0000258 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
259 if (I != FuncInfo.ValueMap.end())
Dan Gohman3193a682010-06-21 14:21:47 +0000260 return I->second;
Eric Christopher76ad43c2012-10-03 08:10:01 +0000261 return LocalValueMap[V];
Evan Cheng59fbc802008-09-09 01:26:59 +0000262}
263
Owen Andersoncc54e762008-08-30 00:38:46 +0000264/// UpdateValueMap - Update the value map to include the new mapping for this
265/// instruction, or insert an extra copy to get the result in a previous
266/// determined register.
267/// NOTE: This is only necessary because we might select a block that uses
268/// a value before we select the block that defines the value. It might be
269/// possible to fix this by selecting blocks in reverse postorder.
Eli Friedman482feb32011-05-16 21:06:17 +0000270void FastISel::UpdateValueMap(const Value *I, unsigned Reg, unsigned NumRegs) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000271 if (!isa<Instruction>(I)) {
272 LocalValueMap[I] = Reg;
Eli Friedman482feb32011-05-16 21:06:17 +0000273 return;
Dan Gohman40b189e2008-09-05 18:18:20 +0000274 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000275
Dan Gohmana4160c32010-07-07 16:29:44 +0000276 unsigned &AssignedReg = FuncInfo.ValueMap[I];
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000277 if (AssignedReg == 0)
Dan Gohman84023e02010-07-10 09:00:22 +0000278 // Use the new register.
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000279 AssignedReg = Reg;
Chris Lattner36e39462009-04-12 07:46:30 +0000280 else if (Reg != AssignedReg) {
Dan Gohman84023e02010-07-10 09:00:22 +0000281 // Arrange for uses of AssignedReg to be replaced by uses of Reg.
Eli Friedman482feb32011-05-16 21:06:17 +0000282 for (unsigned i = 0; i < NumRegs; i++)
283 FuncInfo.RegFixups[AssignedReg+i] = Reg+i;
Dan Gohman84023e02010-07-10 09:00:22 +0000284
285 AssignedReg = Reg;
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000286 }
Owen Andersoncc54e762008-08-30 00:38:46 +0000287}
288
Dan Gohmana6cb6412010-05-11 23:54:07 +0000289std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000290 unsigned IdxN = getRegForValue(Idx);
291 if (IdxN == 0)
292 // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000293 return std::pair<unsigned, bool>(0, false);
294
295 bool IdxNIsKill = hasTrivialKill(Idx);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000296
297 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Anderson766b5ef2009-08-11 21:59:30 +0000298 MVT PtrVT = TLI.getPointerTy();
Owen Andersone50ed302009-08-10 22:56:29 +0000299 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000300 if (IdxVT.bitsLT(PtrVT)) {
301 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND,
302 IdxN, IdxNIsKill);
303 IdxNIsKill = true;
304 }
305 else if (IdxVT.bitsGT(PtrVT)) {
306 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE,
307 IdxN, IdxNIsKill);
308 IdxNIsKill = true;
309 }
310 return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000311}
312
Dan Gohman84023e02010-07-10 09:00:22 +0000313void FastISel::recomputeInsertPt() {
314 if (getLastLocalValue()) {
315 FuncInfo.InsertPt = getLastLocalValue();
Dan Gohmanc6e59b72010-07-19 22:48:56 +0000316 FuncInfo.MBB = FuncInfo.InsertPt->getParent();
Dan Gohman84023e02010-07-10 09:00:22 +0000317 ++FuncInfo.InsertPt;
318 } else
319 FuncInfo.InsertPt = FuncInfo.MBB->getFirstNonPHI();
320
321 // Now skip past any EH_LABELs, which must remain at the beginning.
322 while (FuncInfo.InsertPt != FuncInfo.MBB->end() &&
323 FuncInfo.InsertPt->getOpcode() == TargetOpcode::EH_LABEL)
324 ++FuncInfo.InsertPt;
325}
326
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000327void FastISel::removeDeadCode(MachineBasicBlock::iterator I,
328 MachineBasicBlock::iterator E) {
329 assert (I && E && std::distance(I, E) > 0 && "Invalid iterator!");
330 while (I != E) {
331 MachineInstr *Dead = &*I;
332 ++I;
333 Dead->eraseFromParent();
Jan Wen Voungfa785cb2013-03-08 22:56:31 +0000334 ++NumFastIselDead;
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000335 }
336 recomputeInsertPt();
337}
338
Eric Christopher76ad43c2012-10-03 08:10:01 +0000339FastISel::SavePoint FastISel::enterLocalValueArea() {
Dan Gohman84023e02010-07-10 09:00:22 +0000340 MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt;
Eric Christopher76ad43c2012-10-03 08:10:01 +0000341 DebugLoc OldDL = DL;
Dan Gohman84023e02010-07-10 09:00:22 +0000342 recomputeInsertPt();
Eric Christopher76ad43c2012-10-03 08:10:01 +0000343 DL = DebugLoc();
344 SavePoint SP = { OldInsertPt, OldDL };
345 return SP;
Dan Gohman84023e02010-07-10 09:00:22 +0000346}
347
Eric Christopher76ad43c2012-10-03 08:10:01 +0000348void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) {
Dan Gohman84023e02010-07-10 09:00:22 +0000349 if (FuncInfo.InsertPt != FuncInfo.MBB->begin())
350 LastLocalValue = llvm::prior(FuncInfo.InsertPt);
351
352 // Restore the previous insert position.
Eric Christopher76ad43c2012-10-03 08:10:01 +0000353 FuncInfo.InsertPt = OldInsertPt.InsertPt;
354 DL = OldInsertPt.DL;
Dan Gohman84023e02010-07-10 09:00:22 +0000355}
356
Dan Gohmanbdedd442008-08-20 00:11:48 +0000357/// SelectBinaryOp - Select and emit code for a binary operator instruction,
358/// which has an opcode which directly corresponds to the given ISD opcode.
359///
Dan Gohman46510a72010-04-15 01:51:59 +0000360bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000361 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000362 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmanbdedd442008-08-20 00:11:48 +0000363 // Unhandled type. Halt "fast" selection and bail.
364 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000365
Dan Gohmanb71fea22008-08-26 20:52:40 +0000366 // We only handle legal types. For example, on x86-32 the instruction
367 // selector contains all of the 64-bit instructions from x86-64,
368 // under the assumption that i64 won't be used if the target doesn't
369 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000370 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000372 // don't require additional zeroing, which makes them easy.
Owen Anderson825b72b2009-08-11 20:47:22 +0000373 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000374 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
375 ISDOpcode == ISD::XOR))
Owen Anderson23b9b192009-08-12 00:36:31 +0000376 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohman638c6832008-09-05 18:44:22 +0000377 else
378 return false;
379 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000380
Chris Lattnerfff65b32011-04-17 01:16:47 +0000381 // Check if the first operand is a constant, and handle it as "ri". At -O0,
382 // we don't have anything that canonicalizes operand order.
383 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(0)))
384 if (isa<Instruction>(I) && cast<Instruction>(I)->isCommutative()) {
385 unsigned Op1 = getRegForValue(I->getOperand(1));
386 if (Op1 == 0) return false;
387
388 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
Owen Andersond74ea772011-04-22 23:38:06 +0000389
Chris Lattner602fc062011-04-17 20:23:29 +0000390 unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op1,
391 Op1IsKill, CI->getZExtValue(),
392 VT.getSimpleVT());
393 if (ResultReg == 0) return false;
Owen Andersond74ea772011-04-22 23:38:06 +0000394
Chris Lattner602fc062011-04-17 20:23:29 +0000395 // We successfully emitted code for the given LLVM Instruction.
396 UpdateValueMap(I, ResultReg);
397 return true;
Chris Lattnerfff65b32011-04-17 01:16:47 +0000398 }
Owen Andersond74ea772011-04-22 23:38:06 +0000399
400
Dan Gohman3df24e62008-09-03 23:12:08 +0000401 unsigned Op0 = getRegForValue(I->getOperand(0));
Chris Lattner602fc062011-04-17 20:23:29 +0000402 if (Op0 == 0) // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000403 return false;
404
Dan Gohmana6cb6412010-05-11 23:54:07 +0000405 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
406
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000407 // Check if the second operand is a constant and handle it appropriately.
408 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner602fc062011-04-17 20:23:29 +0000409 uint64_t Imm = CI->getZExtValue();
Owen Andersond74ea772011-04-22 23:38:06 +0000410
Chris Lattnerf051c1a2011-04-18 07:00:40 +0000411 // Transform "sdiv exact X, 8" -> "sra X, 3".
412 if (ISDOpcode == ISD::SDIV && isa<BinaryOperator>(I) &&
413 cast<BinaryOperator>(I)->isExact() &&
414 isPowerOf2_64(Imm)) {
415 Imm = Log2_64(Imm);
416 ISDOpcode = ISD::SRA;
417 }
Owen Andersond74ea772011-04-22 23:38:06 +0000418
Chad Rosier544b9b42012-03-22 00:21:17 +0000419 // Transform "urem x, pow2" -> "and x, pow2-1".
420 if (ISDOpcode == ISD::UREM && isa<BinaryOperator>(I) &&
421 isPowerOf2_64(Imm)) {
422 --Imm;
423 ISDOpcode = ISD::AND;
424 }
425
Chris Lattner602fc062011-04-17 20:23:29 +0000426 unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0,
427 Op0IsKill, Imm, VT.getSimpleVT());
428 if (ResultReg == 0) return false;
Owen Andersond74ea772011-04-22 23:38:06 +0000429
Chris Lattner602fc062011-04-17 20:23:29 +0000430 // We successfully emitted code for the given LLVM Instruction.
431 UpdateValueMap(I, ResultReg);
432 return true;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000433 }
434
Dan Gohman10df0fa2008-08-27 01:09:54 +0000435 // Check if the second operand is a constant float.
436 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000437 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000438 ISDOpcode, Op0, Op0IsKill, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000439 if (ResultReg != 0) {
440 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000441 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000442 return true;
443 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000444 }
445
Dan Gohman3df24e62008-09-03 23:12:08 +0000446 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000447 if (Op1 == 0)
448 // Unhandled operand. Halt "fast" selection and bail.
449 return false;
450
Dan Gohmana6cb6412010-05-11 23:54:07 +0000451 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
452
Dan Gohmanad368ac2008-08-27 18:10:19 +0000453 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000454 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000455 ISDOpcode,
456 Op0, Op0IsKill,
457 Op1, Op1IsKill);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000458 if (ResultReg == 0)
459 // Target-specific code wasn't able to find a machine opcode for
460 // the given ISD opcode and type. Halt "fast" selection and bail.
461 return false;
462
Dan Gohman8014e862008-08-20 00:23:20 +0000463 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000464 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000465 return true;
466}
467
Dan Gohman46510a72010-04-15 01:51:59 +0000468bool FastISel::SelectGetElementPtr(const User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000469 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000470 if (N == 0)
471 // Unhandled operand. Halt "fast" selection and bail.
472 return false;
473
Dan Gohmana6cb6412010-05-11 23:54:07 +0000474 bool NIsKill = hasTrivialKill(I->getOperand(0));
475
Chad Rosier478b06c2011-11-17 07:15:58 +0000476 // Keep a running tab of the total offset to coalesce multiple N = N + Offset
477 // into a single N = N + TotalOffset.
478 uint64_t TotalOffs = 0;
479 // FIXME: What's a good SWAG number for MaxOffs?
480 uint64_t MaxOffs = 2048;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000481 Type *Ty = I->getOperand(0)->getType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000482 MVT VT = TLI.getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +0000483 for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
484 E = I->op_end(); OI != E; ++OI) {
485 const Value *Idx = *OI;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000486 if (StructType *StTy = dyn_cast<StructType>(Ty)) {
Evan Cheng83785c82008-08-20 22:45:34 +0000487 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
488 if (Field) {
489 // N = N + Offset
Chad Rosier478b06c2011-11-17 07:15:58 +0000490 TotalOffs += TD.getStructLayout(StTy)->getElementOffset(Field);
491 if (TotalOffs >= MaxOffs) {
492 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
493 if (N == 0)
494 // Unhandled operand. Halt "fast" selection and bail.
495 return false;
496 NIsKill = true;
497 TotalOffs = 0;
498 }
Evan Cheng83785c82008-08-20 22:45:34 +0000499 }
500 Ty = StTy->getElementType(Field);
501 } else {
502 Ty = cast<SequentialType>(Ty)->getElementType();
503
504 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +0000505 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +0000506 if (CI->isZero()) continue;
Chad Rosier478b06c2011-11-17 07:15:58 +0000507 // N = N + Offset
Chad Rosier6016a4a2012-07-06 17:44:22 +0000508 TotalOffs +=
Duncan Sands777d2302009-05-09 07:06:46 +0000509 TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Chad Rosier478b06c2011-11-17 07:15:58 +0000510 if (TotalOffs >= MaxOffs) {
511 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
512 if (N == 0)
513 // Unhandled operand. Halt "fast" selection and bail.
514 return false;
515 NIsKill = true;
516 TotalOffs = 0;
517 }
518 continue;
519 }
520 if (TotalOffs) {
521 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000522 if (N == 0)
523 // Unhandled operand. Halt "fast" selection and bail.
524 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000525 NIsKill = true;
Chad Rosier478b06c2011-11-17 07:15:58 +0000526 TotalOffs = 0;
Evan Cheng83785c82008-08-20 22:45:34 +0000527 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000528
Evan Cheng83785c82008-08-20 22:45:34 +0000529 // N = N + Idx * ElementSize;
Duncan Sands777d2302009-05-09 07:06:46 +0000530 uint64_t ElementSize = TD.getTypeAllocSize(Ty);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000531 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
532 unsigned IdxN = Pair.first;
533 bool IdxNIsKill = Pair.second;
Evan Cheng83785c82008-08-20 22:45:34 +0000534 if (IdxN == 0)
535 // Unhandled operand. Halt "fast" selection and bail.
536 return false;
537
Dan Gohman80bc6e22008-08-26 20:57:08 +0000538 if (ElementSize != 1) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000539 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000540 if (IdxN == 0)
541 // Unhandled operand. Halt "fast" selection and bail.
542 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000543 IdxNIsKill = true;
Dan Gohman80bc6e22008-08-26 20:57:08 +0000544 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000545 N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
Evan Cheng83785c82008-08-20 22:45:34 +0000546 if (N == 0)
547 // Unhandled operand. Halt "fast" selection and bail.
548 return false;
549 }
550 }
Chad Rosier478b06c2011-11-17 07:15:58 +0000551 if (TotalOffs) {
552 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
553 if (N == 0)
554 // Unhandled operand. Halt "fast" selection and bail.
555 return false;
556 }
Evan Cheng83785c82008-08-20 22:45:34 +0000557
558 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000559 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000560 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000561}
562
Dan Gohman46510a72010-04-15 01:51:59 +0000563bool FastISel::SelectCall(const User *I) {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000564 const CallInst *Call = cast<CallInst>(I);
565
566 // Handle simple inline asms.
Dan Gohman9e15d652011-10-12 15:56:56 +0000567 if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000568 // Don't attempt to handle constraints.
569 if (!IA->getConstraintString().empty())
570 return false;
571
572 unsigned ExtraInfo = 0;
573 if (IA->hasSideEffects())
574 ExtraInfo |= InlineAsm::Extra_HasSideEffects;
575 if (IA->isAlignStack())
576 ExtraInfo |= InlineAsm::Extra_IsAlignStack;
577
578 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
579 TII.get(TargetOpcode::INLINEASM))
580 .addExternalSymbol(IA->getAsmString().c_str())
581 .addImm(ExtraInfo);
582 return true;
583 }
584
Michael J. Spencerc9c137b2012-02-22 19:06:13 +0000585 MachineModuleInfo &MMI = FuncInfo.MF->getMMI();
586 ComputeUsesVAFloatArgument(*Call, &MMI);
587
Dan Gohmana61e73b2011-04-26 17:18:34 +0000588 const Function *F = Call->getCalledFunction();
Dan Gohman33134c42008-09-25 17:05:24 +0000589 if (!F) return false;
590
Dan Gohman4183e312010-04-13 17:07:06 +0000591 // Handle selected intrinsic function calls.
Chris Lattner832e4942011-04-19 05:52:03 +0000592 switch (F->getIntrinsicID()) {
Dan Gohman33134c42008-09-25 17:05:24 +0000593 default: break;
Chad Rosieraefd36b2012-05-11 23:21:01 +0000594 // At -O0 we don't care about the lifetime intrinsics.
Eric Christopher9b5d6b82012-02-17 23:03:39 +0000595 case Intrinsic::lifetime_start:
596 case Intrinsic::lifetime_end:
Chad Rosierfd065bb2012-07-06 17:33:39 +0000597 // The donothing intrinsic does, well, nothing.
598 case Intrinsic::donothing:
Eric Christopher9b5d6b82012-02-17 23:03:39 +0000599 return true;
Chad Rosierfd065bb2012-07-06 17:33:39 +0000600
Bill Wendling92c1e122009-02-13 02:16:35 +0000601 case Intrinsic::dbg_declare: {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000602 const DbgDeclareInst *DI = cast<DbgDeclareInst>(Call);
Manman Rencbafae62013-06-28 05:43:10 +0000603 DIVariable DIVar(DI->getVariable());
604 assert((!DIVar || DIVar.isVariable()) &&
605 "Variable in DbgDeclareInst should be either null or a DIVariable.");
606 if (!DIVar ||
Eric Christopherbb54d212012-03-15 21:33:44 +0000607 !FuncInfo.MF->getMMI().hasDebugInfo()) {
608 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Devang Patel7e1e31f2009-07-02 22:43:26 +0000609 return true;
Eric Christopherbb54d212012-03-15 21:33:44 +0000610 }
Devang Patel7e1e31f2009-07-02 22:43:26 +0000611
Dan Gohman46510a72010-04-15 01:51:59 +0000612 const Value *Address = DI->getAddress();
Eric Christopherccaea7d2012-03-15 21:33:47 +0000613 if (!Address || isa<UndefValue>(Address)) {
Eric Christopherbb54d212012-03-15 21:33:44 +0000614 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Dale Johannesendc918562010-02-06 02:26:02 +0000615 return true;
Eric Christopherbb54d212012-03-15 21:33:44 +0000616 }
Devang Patel6fe75aa2010-09-14 20:29:31 +0000617
David Blaikie6d9dbd52013-06-16 20:34:15 +0000618 Optional<MachineOperand> Op;
619 if (const Argument *Arg = dyn_cast<Argument>(Address))
Devang Patel9aee3352011-09-08 22:59:09 +0000620 // Some arguments' frame index is recorded during argument lowering.
David Blaikie6d9dbd52013-06-16 20:34:15 +0000621 if (int FI = FuncInfo.getArgumentFrameIndex(Arg))
622 Op = MachineOperand::CreateFI(FI);
623 if (!Op)
624 if (unsigned Reg = lookUpRegForValue(Address))
625 Op = MachineOperand::CreateReg(Reg, false);
Eric Christopher8c5293c2012-03-20 01:07:58 +0000626
Bill Wendling84364a42012-03-30 00:02:55 +0000627 // If we have a VLA that has a "use" in a metadata node that's then used
628 // here but it has no other uses, then we have a problem. E.g.,
629 //
630 // int foo (const int *x) {
631 // char a[*x];
632 // return 0;
633 // }
634 //
635 // If we assign 'a' a vreg and fast isel later on has to use the selection
636 // DAG isel, it will want to copy the value to the vreg. However, there are
637 // no uses, which goes counter to what selection DAG isel expects.
David Blaikie6d9dbd52013-06-16 20:34:15 +0000638 if (!Op && !Address->use_empty() && isa<Instruction>(Address) &&
Eric Christopher8c5293c2012-03-20 01:07:58 +0000639 (!isa<AllocaInst>(Address) ||
640 !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(Address))))
David Blaikie6d9dbd52013-06-16 20:34:15 +0000641 Op = MachineOperand::CreateReg(FuncInfo.InitializeRegForValue(Address),
642 false);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000643
David Blaikie6d9dbd52013-06-16 20:34:15 +0000644 if (Op && Op->isReg())
645 Op->setIsDebug(true);
646
647 if (Op)
Adrian Prantl86a87d92013-04-30 22:35:14 +0000648 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
David Blaikie6d9dbd52013-06-16 20:34:15 +0000649 TII.get(TargetOpcode::DBG_VALUE)).addOperand(*Op).addImm(0)
650 .addMetadata(DI->getVariable());
Adrian Prantl86a87d92013-04-30 22:35:14 +0000651 else
Eric Christopher4476bae2012-03-20 01:07:53 +0000652 // We can't yet handle anything else here because it would require
653 // generating code, thus altering codegen because of debug info.
Adrian Prantl5da4e4f2013-05-22 18:02:19 +0000654 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Dan Gohman33134c42008-09-25 17:05:24 +0000655 return true;
Bill Wendling92c1e122009-02-13 02:16:35 +0000656 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000657 case Intrinsic::dbg_value: {
Dale Johannesen343b42e2010-04-07 01:15:14 +0000658 // This form of DBG_VALUE is target-independent.
Dan Gohmana61e73b2011-04-26 17:18:34 +0000659 const DbgValueInst *DI = cast<DbgValueInst>(Call);
Evan Chenge837dea2011-06-28 19:10:37 +0000660 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohman46510a72010-04-15 01:51:59 +0000661 const Value *V = DI->getValue();
Dale Johannesen45df7612010-02-26 20:01:55 +0000662 if (!V) {
663 // Currently the optimizer can produce this; insert an undef to
664 // help debugging. Probably the optimizer should not do this.
Dan Gohman84023e02010-07-10 09:00:22 +0000665 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
666 .addReg(0U).addImm(DI->getOffset())
667 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000668 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Devang Patel8594d422011-06-24 20:46:11 +0000669 if (CI->getBitWidth() > 64)
670 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
671 .addCImm(CI).addImm(DI->getOffset())
672 .addMetadata(DI->getVariable());
Chad Rosier6016a4a2012-07-06 17:44:22 +0000673 else
Devang Patel8594d422011-06-24 20:46:11 +0000674 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
675 .addImm(CI->getZExtValue()).addImm(DI->getOffset())
676 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000677 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000678 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
679 .addFPImm(CF).addImm(DI->getOffset())
680 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000681 } else if (unsigned Reg = lookUpRegForValue(V)) {
Adrian Prantl86a87d92013-04-30 22:35:14 +0000682 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
683 .addReg(Reg, RegState::Debug).addImm(DI->getOffset())
684 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000685 } else {
686 // We can't yet handle anything else here because it would require
687 // generating code, thus altering codegen because of debug info.
Adrian Prantl5da4e4f2013-05-22 18:02:19 +0000688 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000689 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000690 return true;
691 }
Eli Friedmand0118a22011-05-14 00:47:51 +0000692 case Intrinsic::objectsize: {
693 ConstantInt *CI = cast<ConstantInt>(Call->getArgOperand(1));
694 unsigned long long Res = CI->isZero() ? -1ULL : 0;
695 Constant *ResCI = ConstantInt::get(Call->getType(), Res);
696 unsigned ResultReg = getRegForValue(ResCI);
697 if (ResultReg == 0)
698 return false;
699 UpdateValueMap(Call, ResultReg);
700 return true;
701 }
Chad Rosier33947b42013-03-07 20:42:17 +0000702 case Intrinsic::expect: {
Chad Rosier4fde76d2013-03-07 21:38:33 +0000703 unsigned ResultReg = getRegForValue(Call->getArgOperand(0));
Nick Lewycky33cdfe92013-03-11 21:44:37 +0000704 if (ResultReg == 0)
705 return false;
Chad Rosier4fde76d2013-03-07 21:38:33 +0000706 UpdateValueMap(Call, ResultReg);
707 return true;
Chad Rosier33947b42013-03-07 20:42:17 +0000708 }
Dan Gohman33134c42008-09-25 17:05:24 +0000709 }
Dan Gohman4183e312010-04-13 17:07:06 +0000710
Ivan Krasin74af88a2011-08-18 22:06:10 +0000711 // Usually, it does not make sense to initialize a value,
712 // make an unrelated function call and use the value, because
713 // it tends to be spilled on the stack. So, we move the pointer
714 // to the last local value to the beginning of the block, so that
715 // all the values which have already been materialized,
716 // appear after the call. It also makes sense to skip intrinsics
717 // since they tend to be inlined.
Pete Cooperb704ffb2013-02-22 01:50:38 +0000718 if (!isa<IntrinsicInst>(Call))
Ivan Krasin74af88a2011-08-18 22:06:10 +0000719 flushLocalValueMap();
720
Dan Gohman4183e312010-04-13 17:07:06 +0000721 // An arbitrary call. Bail.
Dan Gohman33134c42008-09-25 17:05:24 +0000722 return false;
723}
724
Dan Gohman46510a72010-04-15 01:51:59 +0000725bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000726 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
727 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000728
Owen Anderson825b72b2009-08-11 20:47:22 +0000729 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
730 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersond0533c92008-08-26 23:46:32 +0000731 // Unhandled type. Halt "fast" selection and bail.
732 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000733
Eli Friedman76927d732011-05-25 23:49:02 +0000734 // Check if the destination type is legal.
Dan Gohman474d3b32009-03-13 23:53:06 +0000735 if (!TLI.isTypeLegal(DstVT))
Eli Friedman76927d732011-05-25 23:49:02 +0000736 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000737
Eli Friedman76927d732011-05-25 23:49:02 +0000738 // Check if the source operand is legal.
Dan Gohman474d3b32009-03-13 23:53:06 +0000739 if (!TLI.isTypeLegal(SrcVT))
Eli Friedman76927d732011-05-25 23:49:02 +0000740 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000741
Dan Gohman3df24e62008-09-03 23:12:08 +0000742 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000743 if (!InputReg)
744 // Unhandled operand. Halt "fast" selection and bail.
745 return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000746
Dan Gohmana6cb6412010-05-11 23:54:07 +0000747 bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
748
Owen Andersond0533c92008-08-26 23:46:32 +0000749 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
750 DstVT.getSimpleVT(),
751 Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000752 InputReg, InputRegIsKill);
Owen Andersond0533c92008-08-26 23:46:32 +0000753 if (!ResultReg)
754 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000755
Dan Gohman3df24e62008-09-03 23:12:08 +0000756 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000757 return true;
758}
759
Dan Gohman46510a72010-04-15 01:51:59 +0000760bool FastISel::SelectBitCast(const User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000761 // If the bitcast doesn't change the type, just use the operand value.
762 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000763 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000764 if (Reg == 0)
765 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000766 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000767 return true;
768 }
769
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000770 // Bitcasts of other values become reg-reg copies or BITCAST operators.
Patrik Hagglund3d170e62012-12-17 14:30:06 +0000771 EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType());
772 EVT DstEVT = TLI.getValueType(I->getType());
773 if (SrcEVT == MVT::Other || DstEVT == MVT::Other ||
774 !TLI.isTypeLegal(SrcEVT) || !TLI.isTypeLegal(DstEVT))
Owen Andersond0533c92008-08-26 23:46:32 +0000775 // Unhandled type. Halt "fast" selection and bail.
776 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000777
Patrik Hagglund3d170e62012-12-17 14:30:06 +0000778 MVT SrcVT = SrcEVT.getSimpleVT();
779 MVT DstVT = DstEVT.getSimpleVT();
Dan Gohman3df24e62008-09-03 23:12:08 +0000780 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000781 if (Op0 == 0)
782 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000783 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000784
785 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000786
Dan Gohmanad368ac2008-08-27 18:10:19 +0000787 // First, try to perform the bitcast by inserting a reg-reg copy.
788 unsigned ResultReg = 0;
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000789 if (SrcVT == DstVT) {
Craig Topper44d23822012-02-22 05:59:10 +0000790 const TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
791 const TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
Jakob Stoklund Olesene7917bb2010-07-11 05:16:54 +0000792 // Don't attempt a cross-class copy. It will likely fail.
793 if (SrcClass == DstClass) {
794 ResultReg = createResultReg(DstClass);
795 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
796 ResultReg).addReg(Op0);
797 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000798 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000799
800 // If the reg-reg copy failed, select a BITCAST opcode.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000801 if (!ResultReg)
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000802 ResultReg = FastEmit_r(SrcVT, DstVT, ISD::BITCAST, Op0, Op0IsKill);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000803
Dan Gohmanad368ac2008-08-27 18:10:19 +0000804 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000805 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000806
Dan Gohman3df24e62008-09-03 23:12:08 +0000807 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000808 return true;
809}
810
Dan Gohman3df24e62008-09-03 23:12:08 +0000811bool
Dan Gohman46510a72010-04-15 01:51:59 +0000812FastISel::SelectInstruction(const Instruction *I) {
Dan Gohmane8c92dd2010-04-23 15:29:50 +0000813 // Just before the terminator instruction, insert instructions to
814 // feed PHI nodes in successor blocks.
815 if (isa<TerminatorInst>(I))
816 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
817 return false;
818
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000819 DL = I->getDebugLoc();
820
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000821 MachineBasicBlock::iterator SavedInsertPt = FuncInfo.InsertPt;
822
Bob Wilson982dc842012-08-03 21:26:24 +0000823 // As a special case, don't handle calls to builtin library functions that
824 // may be translated directly to target instructions.
Bob Wilsond49edb72012-08-03 04:06:28 +0000825 if (const CallInst *Call = dyn_cast<CallInst>(I)) {
826 const Function *F = Call->getCalledFunction();
827 LibFunc::Func Func;
828 if (F && !F->hasLocalLinkage() && F->hasName() &&
829 LibInfo->getLibFunc(F->getName(), Func) &&
Bob Wilson982dc842012-08-03 21:26:24 +0000830 LibInfo->hasOptimizedCodeGen(Func))
Bob Wilsond49edb72012-08-03 04:06:28 +0000831 return false;
832 }
833
Dan Gohman6e3ff372009-12-05 01:27:58 +0000834 // First, try doing target-independent selection.
Michael Ilseman7dbd34b2013-02-27 19:54:00 +0000835 if (SelectOperator(I, I->getOpcode())) {
Jan Wen Voungfa785cb2013-03-08 22:56:31 +0000836 ++NumFastIselSuccessIndependent;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000837 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000838 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000839 }
Chad Rosier6016a4a2012-07-06 17:44:22 +0000840 // Remove dead code. However, ignore call instructions since we've flushed
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000841 // the local value map and recomputed the insert point.
842 if (!isa<CallInst>(I)) {
843 recomputeInsertPt();
844 if (SavedInsertPt != FuncInfo.InsertPt)
845 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
846 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000847
848 // Next, try calling the target to attempt to handle the instruction.
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000849 SavedInsertPt = FuncInfo.InsertPt;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000850 if (TargetSelectInstruction(I)) {
Jan Wen Voungfa785cb2013-03-08 22:56:31 +0000851 ++NumFastIselSuccessTarget;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000852 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000853 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000854 }
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000855 // Check for dead code and remove as necessary.
856 recomputeInsertPt();
857 if (SavedInsertPt != FuncInfo.InsertPt)
858 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
Dan Gohman6e3ff372009-12-05 01:27:58 +0000859
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000860 DL = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000861 return false;
Dan Gohman40b189e2008-09-05 18:18:20 +0000862}
863
Dan Gohmand98d6202008-10-02 22:15:21 +0000864/// FastEmitBranch - Emit an unconditional branch to the given block,
865/// unless it is the immediate (fall-through) successor, and update
866/// the CFG.
867void
Stuart Hastings3bf91252010-06-17 22:43:56 +0000868FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DL) {
Eric Christopher18112d82012-04-10 18:18:10 +0000869
Evan Cheng092e5e72013-02-11 01:27:15 +0000870 if (FuncInfo.MBB->getBasicBlock()->size() > 1 &&
871 FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
Eric Christopher18112d82012-04-10 18:18:10 +0000872 // For more accurate line information if this is the only instruction
873 // in the block then emit it, otherwise we have the unconditional
874 // fall-through case, which needs no instructions.
Dan Gohmand98d6202008-10-02 22:15:21 +0000875 } else {
876 // The unconditional branch case.
Dan Gohman84023e02010-07-10 09:00:22 +0000877 TII.InsertBranch(*FuncInfo.MBB, MSucc, NULL,
878 SmallVector<MachineOperand, 0>(), DL);
Dan Gohmand98d6202008-10-02 22:15:21 +0000879 }
Dan Gohman84023e02010-07-10 09:00:22 +0000880 FuncInfo.MBB->addSuccessor(MSucc);
Dan Gohmand98d6202008-10-02 22:15:21 +0000881}
882
Dan Gohman3d45a852009-09-03 22:53:57 +0000883/// SelectFNeg - Emit an FNeg operation.
884///
885bool
Dan Gohman46510a72010-04-15 01:51:59 +0000886FastISel::SelectFNeg(const User *I) {
Dan Gohman3d45a852009-09-03 22:53:57 +0000887 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
888 if (OpReg == 0) return false;
889
Dan Gohmana6cb6412010-05-11 23:54:07 +0000890 bool OpRegIsKill = hasTrivialKill(I);
891
Dan Gohman4a215a12009-09-11 00:36:43 +0000892 // If the target has ISD::FNEG, use it.
893 EVT VT = TLI.getValueType(I->getType());
894 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000895 ISD::FNEG, OpReg, OpRegIsKill);
Dan Gohman4a215a12009-09-11 00:36:43 +0000896 if (ResultReg != 0) {
897 UpdateValueMap(I, ResultReg);
898 return true;
899 }
900
Dan Gohman5e5abb72009-09-11 00:34:46 +0000901 // Bitcast the value to integer, twiddle the sign bit with xor,
902 // and then bitcast it back to floating-point.
Dan Gohman3d45a852009-09-03 22:53:57 +0000903 if (VT.getSizeInBits() > 64) return false;
Dan Gohman5e5abb72009-09-11 00:34:46 +0000904 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
905 if (!TLI.isTypeLegal(IntVT))
906 return false;
907
908 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000909 ISD::BITCAST, OpReg, OpRegIsKill);
Dan Gohman5e5abb72009-09-11 00:34:46 +0000910 if (IntReg == 0)
911 return false;
912
Dan Gohmana6cb6412010-05-11 23:54:07 +0000913 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
914 IntReg, /*Kill=*/true,
Dan Gohman5e5abb72009-09-11 00:34:46 +0000915 UINT64_C(1) << (VT.getSizeInBits()-1),
916 IntVT.getSimpleVT());
917 if (IntResultReg == 0)
918 return false;
919
920 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000921 ISD::BITCAST, IntResultReg, /*Kill=*/true);
Dan Gohman3d45a852009-09-03 22:53:57 +0000922 if (ResultReg == 0)
923 return false;
924
925 UpdateValueMap(I, ResultReg);
926 return true;
927}
928
Dan Gohman40b189e2008-09-05 18:18:20 +0000929bool
Eli Friedman2586b8f2011-05-16 20:27:46 +0000930FastISel::SelectExtractValue(const User *U) {
931 const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U);
Eli Friedmana4c920d2011-05-16 20:34:53 +0000932 if (!EVI)
Eli Friedman2586b8f2011-05-16 20:27:46 +0000933 return false;
934
Eli Friedman482feb32011-05-16 21:06:17 +0000935 // Make sure we only try to handle extracts with a legal result. But also
936 // allow i1 because it's easy.
Eli Friedman2586b8f2011-05-16 20:27:46 +0000937 EVT RealVT = TLI.getValueType(EVI->getType(), /*AllowUnknown=*/true);
938 if (!RealVT.isSimple())
939 return false;
940 MVT VT = RealVT.getSimpleVT();
Eli Friedman482feb32011-05-16 21:06:17 +0000941 if (!TLI.isTypeLegal(VT) && VT != MVT::i1)
Eli Friedman2586b8f2011-05-16 20:27:46 +0000942 return false;
943
944 const Value *Op0 = EVI->getOperand(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000945 Type *AggTy = Op0->getType();
Eli Friedman2586b8f2011-05-16 20:27:46 +0000946
947 // Get the base result register.
948 unsigned ResultReg;
949 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(Op0);
950 if (I != FuncInfo.ValueMap.end())
951 ResultReg = I->second;
Eli Friedman0b4d96b2011-06-06 05:46:34 +0000952 else if (isa<Instruction>(Op0))
Eli Friedman2586b8f2011-05-16 20:27:46 +0000953 ResultReg = FuncInfo.InitializeRegForValue(Op0);
Eli Friedman0b4d96b2011-06-06 05:46:34 +0000954 else
955 return false; // fast-isel can't handle aggregate constants at the moment
Eli Friedman2586b8f2011-05-16 20:27:46 +0000956
957 // Get the actual result register, which is an offset from the base register.
Jay Foadfc6d3a42011-07-13 10:26:04 +0000958 unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices());
Eli Friedman2586b8f2011-05-16 20:27:46 +0000959
960 SmallVector<EVT, 4> AggValueVTs;
961 ComputeValueVTs(TLI, AggTy, AggValueVTs);
962
963 for (unsigned i = 0; i < VTIndex; i++)
964 ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]);
965
966 UpdateValueMap(EVI, ResultReg);
967 return true;
968}
969
970bool
Dan Gohman46510a72010-04-15 01:51:59 +0000971FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000972 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000973 case Instruction::Add:
974 return SelectBinaryOp(I, ISD::ADD);
975 case Instruction::FAdd:
976 return SelectBinaryOp(I, ISD::FADD);
977 case Instruction::Sub:
978 return SelectBinaryOp(I, ISD::SUB);
979 case Instruction::FSub:
Dan Gohman3d45a852009-09-03 22:53:57 +0000980 // FNeg is currently represented in LLVM IR as a special case of FSub.
981 if (BinaryOperator::isFNeg(I))
982 return SelectFNeg(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000983 return SelectBinaryOp(I, ISD::FSUB);
984 case Instruction::Mul:
985 return SelectBinaryOp(I, ISD::MUL);
986 case Instruction::FMul:
987 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman3df24e62008-09-03 23:12:08 +0000988 case Instruction::SDiv:
989 return SelectBinaryOp(I, ISD::SDIV);
990 case Instruction::UDiv:
991 return SelectBinaryOp(I, ISD::UDIV);
992 case Instruction::FDiv:
993 return SelectBinaryOp(I, ISD::FDIV);
994 case Instruction::SRem:
995 return SelectBinaryOp(I, ISD::SREM);
996 case Instruction::URem:
997 return SelectBinaryOp(I, ISD::UREM);
998 case Instruction::FRem:
999 return SelectBinaryOp(I, ISD::FREM);
1000 case Instruction::Shl:
1001 return SelectBinaryOp(I, ISD::SHL);
1002 case Instruction::LShr:
1003 return SelectBinaryOp(I, ISD::SRL);
1004 case Instruction::AShr:
1005 return SelectBinaryOp(I, ISD::SRA);
1006 case Instruction::And:
1007 return SelectBinaryOp(I, ISD::AND);
1008 case Instruction::Or:
1009 return SelectBinaryOp(I, ISD::OR);
1010 case Instruction::Xor:
1011 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001012
Dan Gohman3df24e62008-09-03 23:12:08 +00001013 case Instruction::GetElementPtr:
1014 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +00001015
Dan Gohman3df24e62008-09-03 23:12:08 +00001016 case Instruction::Br: {
Dan Gohman46510a72010-04-15 01:51:59 +00001017 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +00001018
Dan Gohman3df24e62008-09-03 23:12:08 +00001019 if (BI->isUnconditional()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001020 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohmana4160c32010-07-07 16:29:44 +00001021 MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
Stuart Hastings3bf91252010-06-17 22:43:56 +00001022 FastEmitBranch(MSucc, BI->getDebugLoc());
Dan Gohman3df24e62008-09-03 23:12:08 +00001023 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +00001024 }
Dan Gohman3df24e62008-09-03 23:12:08 +00001025
1026 // Conditional branches are not handed yet.
1027 // Halt "fast" selection and bail.
1028 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001029 }
1030
Dan Gohman087c8502008-09-05 01:08:41 +00001031 case Instruction::Unreachable:
1032 // Nothing to emit.
1033 return true;
1034
Dan Gohman0586d912008-09-10 20:11:02 +00001035 case Instruction::Alloca:
1036 // FunctionLowering has the static-sized case covered.
Dan Gohmana4160c32010-07-07 16:29:44 +00001037 if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
Dan Gohman0586d912008-09-10 20:11:02 +00001038 return true;
1039
1040 // Dynamic-sized alloca is not handled yet.
1041 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001042
Dan Gohman33134c42008-09-25 17:05:24 +00001043 case Instruction::Call:
1044 return SelectCall(I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001045
Dan Gohman3df24e62008-09-03 23:12:08 +00001046 case Instruction::BitCast:
1047 return SelectBitCast(I);
1048
1049 case Instruction::FPToSI:
1050 return SelectCast(I, ISD::FP_TO_SINT);
1051 case Instruction::ZExt:
1052 return SelectCast(I, ISD::ZERO_EXTEND);
1053 case Instruction::SExt:
1054 return SelectCast(I, ISD::SIGN_EXTEND);
1055 case Instruction::Trunc:
1056 return SelectCast(I, ISD::TRUNCATE);
1057 case Instruction::SIToFP:
1058 return SelectCast(I, ISD::SINT_TO_FP);
1059
1060 case Instruction::IntToPtr: // Deliberate fall-through.
1061 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001062 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1063 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman3df24e62008-09-03 23:12:08 +00001064 if (DstVT.bitsGT(SrcVT))
1065 return SelectCast(I, ISD::ZERO_EXTEND);
1066 if (DstVT.bitsLT(SrcVT))
1067 return SelectCast(I, ISD::TRUNCATE);
1068 unsigned Reg = getRegForValue(I->getOperand(0));
1069 if (Reg == 0) return false;
1070 UpdateValueMap(I, Reg);
1071 return true;
1072 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001073
Eli Friedman2586b8f2011-05-16 20:27:46 +00001074 case Instruction::ExtractValue:
1075 return SelectExtractValue(I);
1076
Dan Gohmanba5be5c2010-04-20 15:00:41 +00001077 case Instruction::PHI:
1078 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
1079
Dan Gohman3df24e62008-09-03 23:12:08 +00001080 default:
1081 // Unhandled instruction. Halt "fast" selection and bail.
1082 return false;
1083 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001084}
1085
Bob Wilsond49edb72012-08-03 04:06:28 +00001086FastISel::FastISel(FunctionLoweringInfo &funcInfo,
1087 const TargetLibraryInfo *libInfo)
Dan Gohman84023e02010-07-10 09:00:22 +00001088 : FuncInfo(funcInfo),
Dan Gohmana4160c32010-07-07 16:29:44 +00001089 MRI(FuncInfo.MF->getRegInfo()),
1090 MFI(*FuncInfo.MF->getFrameInfo()),
1091 MCP(*FuncInfo.MF->getConstantPool()),
1092 TM(FuncInfo.MF->getTarget()),
Micah Villmow3574eca2012-10-08 16:38:25 +00001093 TD(*TM.getDataLayout()),
Dan Gohman22bb3112008-08-22 00:20:26 +00001094 TII(*TM.getInstrInfo()),
Dan Gohmana7a0ed72010-05-05 23:58:35 +00001095 TLI(*TM.getTargetLowering()),
Bob Wilsond49edb72012-08-03 04:06:28 +00001096 TRI(*TM.getRegisterInfo()),
1097 LibInfo(libInfo) {
Dan Gohmanbb466332008-08-20 21:05:57 +00001098}
1099
Dan Gohmane285a742008-08-14 21:51:29 +00001100FastISel::~FastISel() {}
1101
Evan Cheng092e5e72013-02-11 01:27:15 +00001102bool FastISel::FastLowerArguments() {
1103 return false;
1104}
1105
Owen Anderson825b72b2009-08-11 20:47:22 +00001106unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001107 unsigned) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001108 return 0;
1109}
1110
Owen Anderson825b72b2009-08-11 20:47:22 +00001111unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001112 unsigned,
1113 unsigned /*Op0*/, bool /*Op0IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001114 return 0;
1115}
1116
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001117unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001118 unsigned,
1119 unsigned /*Op0*/, bool /*Op0IsKill*/,
1120 unsigned /*Op1*/, bool /*Op1IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001121 return 0;
1122}
1123
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001124unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +00001125 return 0;
1126}
1127
Owen Anderson825b72b2009-08-11 20:47:22 +00001128unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohman46510a72010-04-15 01:51:59 +00001129 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001130 return 0;
1131}
1132
Owen Anderson825b72b2009-08-11 20:47:22 +00001133unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001134 unsigned,
1135 unsigned /*Op0*/, bool /*Op0IsKill*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +00001136 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001137 return 0;
1138}
1139
Owen Anderson825b72b2009-08-11 20:47:22 +00001140unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001141 unsigned,
1142 unsigned /*Op0*/, bool /*Op0IsKill*/,
Dan Gohman46510a72010-04-15 01:51:59 +00001143 const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001144 return 0;
1145}
1146
Owen Anderson825b72b2009-08-11 20:47:22 +00001147unsigned FastISel::FastEmit_rri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001148 unsigned,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001149 unsigned /*Op0*/, bool /*Op0IsKill*/,
1150 unsigned /*Op1*/, bool /*Op1IsKill*/,
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001151 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +00001152 return 0;
1153}
1154
1155/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
1156/// to emit an instruction with an immediate operand using FastEmit_ri.
1157/// If that fails, it materializes the immediate into a register and try
1158/// FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001159unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001160 unsigned Op0, bool Op0IsKill,
1161 uint64_t Imm, MVT ImmType) {
Chris Lattner602fc062011-04-17 20:23:29 +00001162 // If this is a multiply by a power of two, emit this as a shift left.
1163 if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) {
1164 Opcode = ISD::SHL;
1165 Imm = Log2_64(Imm);
Chris Lattner090ca912011-04-18 06:55:51 +00001166 } else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) {
1167 // div x, 8 -> srl x, 3
1168 Opcode = ISD::SRL;
1169 Imm = Log2_64(Imm);
Chris Lattner602fc062011-04-17 20:23:29 +00001170 }
Owen Andersond74ea772011-04-22 23:38:06 +00001171
Chris Lattner602fc062011-04-17 20:23:29 +00001172 // Horrible hack (to be removed), check to make sure shift amounts are
1173 // in-range.
1174 if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) &&
1175 Imm >= VT.getSizeInBits())
1176 return 0;
Owen Andersond74ea772011-04-22 23:38:06 +00001177
Evan Cheng83785c82008-08-20 22:45:34 +00001178 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001179 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +00001180 if (ResultReg != 0)
1181 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +00001182 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001183 if (MaterialReg == 0) {
1184 // This is a bit ugly/slow, but failing here means falling out of
1185 // fast-isel, which would be very slow.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001186 IntegerType *ITy = IntegerType::get(FuncInfo.Fn->getContext(),
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001187 VT.getSizeInBits());
1188 MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm));
Chad Rosier7ae3bb82013-03-28 23:04:47 +00001189 assert (MaterialReg != 0 && "Unable to materialize imm.");
1190 if (MaterialReg == 0) return 0;
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001191 }
Dan Gohmana6cb6412010-05-11 23:54:07 +00001192 return FastEmit_rr(VT, VT, Opcode,
1193 Op0, Op0IsKill,
1194 MaterialReg, /*Kill=*/true);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001195}
1196
1197unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
1198 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +00001199}
1200
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001201unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +00001202 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001203 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001204 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001205
Dan Gohman84023e02010-07-10 09:00:22 +00001206 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001207 return ResultReg;
1208}
1209
1210unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
1211 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001212 unsigned Op0, bool Op0IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001213 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001214 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001215
Evan Cheng5960e4e2008-09-08 08:38:20 +00001216 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001217 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1218 .addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001219 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001220 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1221 .addReg(Op0, Op0IsKill * RegState::Kill);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001222 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1223 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001224 }
1225
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001226 return ResultReg;
1227}
1228
1229unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
1230 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001231 unsigned Op0, bool Op0IsKill,
1232 unsigned Op1, bool Op1IsKill) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001233 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001234 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001235
Evan Cheng5960e4e2008-09-08 08:38:20 +00001236 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001237 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001238 .addReg(Op0, Op0IsKill * RegState::Kill)
1239 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001240 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001241 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001242 .addReg(Op0, Op0IsKill * RegState::Kill)
1243 .addReg(Op1, Op1IsKill * RegState::Kill);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001244 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1245 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001246 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001247 return ResultReg;
1248}
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001249
Owen Andersond71867a2011-05-05 17:59:04 +00001250unsigned FastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
1251 const TargetRegisterClass *RC,
1252 unsigned Op0, bool Op0IsKill,
1253 unsigned Op1, bool Op1IsKill,
1254 unsigned Op2, bool Op2IsKill) {
1255 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001256 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Andersond71867a2011-05-05 17:59:04 +00001257
1258 if (II.getNumDefs() >= 1)
1259 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1260 .addReg(Op0, Op0IsKill * RegState::Kill)
1261 .addReg(Op1, Op1IsKill * RegState::Kill)
1262 .addReg(Op2, Op2IsKill * RegState::Kill);
1263 else {
1264 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1265 .addReg(Op0, Op0IsKill * RegState::Kill)
1266 .addReg(Op1, Op1IsKill * RegState::Kill)
1267 .addReg(Op2, Op2IsKill * RegState::Kill);
1268 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1269 ResultReg).addReg(II.ImplicitDefs[0]);
1270 }
1271 return ResultReg;
1272}
1273
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001274unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1275 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001276 unsigned Op0, bool Op0IsKill,
1277 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001278 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001279 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001280
Evan Cheng5960e4e2008-09-08 08:38:20 +00001281 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001282 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001283 .addReg(Op0, Op0IsKill * RegState::Kill)
1284 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001285 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001286 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001287 .addReg(Op0, Op0IsKill * RegState::Kill)
1288 .addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001289 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1290 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001291 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001292 return ResultReg;
1293}
1294
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001295unsigned FastISel::FastEmitInst_rii(unsigned MachineInstOpcode,
1296 const TargetRegisterClass *RC,
1297 unsigned Op0, bool Op0IsKill,
1298 uint64_t Imm1, uint64_t Imm2) {
1299 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001300 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001301
1302 if (II.getNumDefs() >= 1)
1303 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1304 .addReg(Op0, Op0IsKill * RegState::Kill)
1305 .addImm(Imm1)
1306 .addImm(Imm2);
1307 else {
1308 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1309 .addReg(Op0, Op0IsKill * RegState::Kill)
1310 .addImm(Imm1)
1311 .addImm(Imm2);
1312 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1313 ResultReg).addReg(II.ImplicitDefs[0]);
1314 }
1315 return ResultReg;
1316}
1317
Dan Gohman10df0fa2008-08-27 01:09:54 +00001318unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
1319 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001320 unsigned Op0, bool Op0IsKill,
1321 const ConstantFP *FPImm) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001322 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001323 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohman10df0fa2008-08-27 01:09:54 +00001324
Evan Cheng5960e4e2008-09-08 08:38:20 +00001325 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001326 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001327 .addReg(Op0, Op0IsKill * RegState::Kill)
1328 .addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001329 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001330 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001331 .addReg(Op0, Op0IsKill * RegState::Kill)
1332 .addFPImm(FPImm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001333 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1334 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001335 }
Dan Gohman10df0fa2008-08-27 01:09:54 +00001336 return ResultReg;
1337}
1338
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001339unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
1340 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001341 unsigned Op0, bool Op0IsKill,
1342 unsigned Op1, bool Op1IsKill,
1343 uint64_t Imm) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001344 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001345 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001346
Evan Cheng5960e4e2008-09-08 08:38:20 +00001347 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001348 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001349 .addReg(Op0, Op0IsKill * RegState::Kill)
1350 .addReg(Op1, Op1IsKill * RegState::Kill)
1351 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001352 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001353 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001354 .addReg(Op0, Op0IsKill * RegState::Kill)
1355 .addReg(Op1, Op1IsKill * RegState::Kill)
1356 .addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001357 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1358 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001359 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001360 return ResultReg;
1361}
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001362
Manman Ren68f25572012-06-01 19:33:18 +00001363unsigned FastISel::FastEmitInst_rrii(unsigned MachineInstOpcode,
1364 const TargetRegisterClass *RC,
1365 unsigned Op0, bool Op0IsKill,
1366 unsigned Op1, bool Op1IsKill,
1367 uint64_t Imm1, uint64_t Imm2) {
1368 unsigned ResultReg = createResultReg(RC);
1369 const MCInstrDesc &II = TII.get(MachineInstOpcode);
1370
1371 if (II.getNumDefs() >= 1)
1372 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1373 .addReg(Op0, Op0IsKill * RegState::Kill)
1374 .addReg(Op1, Op1IsKill * RegState::Kill)
1375 .addImm(Imm1).addImm(Imm2);
1376 else {
1377 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1378 .addReg(Op0, Op0IsKill * RegState::Kill)
1379 .addReg(Op1, Op1IsKill * RegState::Kill)
1380 .addImm(Imm1).addImm(Imm2);
1381 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1382 ResultReg).addReg(II.ImplicitDefs[0]);
1383 }
1384 return ResultReg;
1385}
1386
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001387unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1388 const TargetRegisterClass *RC,
1389 uint64_t Imm) {
1390 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001391 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001392
Evan Cheng5960e4e2008-09-08 08:38:20 +00001393 if (II.getNumDefs() >= 1)
Dan Gohman84023e02010-07-10 09:00:22 +00001394 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001395 else {
Dan Gohman84023e02010-07-10 09:00:22 +00001396 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm);
Jakob Stoklund Olesene797e0c2010-07-11 03:31:05 +00001397 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1398 ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001399 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001400 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +00001401}
Owen Anderson8970f002008-08-27 22:30:02 +00001402
Owen Andersond74ea772011-04-22 23:38:06 +00001403unsigned FastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
1404 const TargetRegisterClass *RC,
1405 uint64_t Imm1, uint64_t Imm2) {
1406 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001407 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Andersond74ea772011-04-22 23:38:06 +00001408
1409 if (II.getNumDefs() >= 1)
1410 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1411 .addImm(Imm1).addImm(Imm2);
1412 else {
1413 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm1).addImm(Imm2);
1414 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1415 ResultReg).addReg(II.ImplicitDefs[0]);
1416 }
1417 return ResultReg;
1418}
1419
Owen Anderson825b72b2009-08-11 20:47:22 +00001420unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001421 unsigned Op0, bool Op0IsKill,
1422 uint32_t Idx) {
Evan Cheng536ab132009-01-22 09:10:11 +00001423 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001424 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
1425 "Cannot yet extract from physregs");
Jakob Stoklund Olesenee0d5d42012-05-20 06:38:37 +00001426 const TargetRegisterClass *RC = MRI.getRegClass(Op0);
1427 MRI.constrainRegClass(Op0, TRI.getSubClassWithSubReg(RC, Idx));
Dan Gohman84023e02010-07-10 09:00:22 +00001428 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
1429 DL, TII.get(TargetOpcode::COPY), ResultReg)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001430 .addReg(Op0, getKillRegState(Op0IsKill), Idx);
Owen Anderson8970f002008-08-27 22:30:02 +00001431 return ResultReg;
1432}
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001433
1434/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1435/// with all but the least significant bit set to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001436unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1437 return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001438}
Dan Gohmanf81eca02010-04-22 20:46:50 +00001439
1440/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1441/// Emit code to ensure constants are copied into registers when needed.
1442/// Remember the virtual registers that need to be added to the Machine PHI
1443/// nodes as input. We cannot just directly add them, because expansion
1444/// might result in multiple MBB's for one BB. As such, the start of the
1445/// BB might correspond to a different MBB than the end.
1446bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1447 const TerminatorInst *TI = LLVMBB->getTerminator();
1448
1449 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohmana4160c32010-07-07 16:29:44 +00001450 unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001451
1452 // Check successor nodes' PHI nodes that expect a constant to be available
1453 // from this block.
1454 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1455 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1456 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmana4160c32010-07-07 16:29:44 +00001457 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Dan Gohmanf81eca02010-04-22 20:46:50 +00001458
1459 // If this terminator has multiple identical successors (common for
1460 // switches), only handle each succ once.
1461 if (!SuccsHandled.insert(SuccMBB)) continue;
1462
1463 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1464
1465 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1466 // nodes and Machine PHI nodes, but the incoming operands have not been
1467 // emitted yet.
1468 for (BasicBlock::const_iterator I = SuccBB->begin();
1469 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanfb95f892010-05-07 01:10:20 +00001470
Dan Gohmanf81eca02010-04-22 20:46:50 +00001471 // Ignore dead phi's.
1472 if (PN->use_empty()) continue;
1473
1474 // Only handle legal types. Two interesting things to note here. First,
1475 // by bailing out early, we may leave behind some dead instructions,
1476 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001477 // own moves. Second, this check is necessary because FastISel doesn't
Dan Gohman89496d02010-07-02 00:10:16 +00001478 // use CreateRegs to create registers, so it always creates
Dan Gohmanf81eca02010-04-22 20:46:50 +00001479 // exactly one register for each non-void instruction.
1480 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1481 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
Chad Rosier2f2d1d72012-02-04 00:39:19 +00001482 // Handle integer promotions, though, because they're common and easy.
1483 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Dan Gohmanf81eca02010-04-22 20:46:50 +00001484 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1485 else {
Dan Gohmana4160c32010-07-07 16:29:44 +00001486 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001487 return false;
1488 }
1489 }
1490
1491 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1492
Dan Gohmanfb95f892010-05-07 01:10:20 +00001493 // Set the DebugLoc for the copy. Prefer the location of the operand
1494 // if there is one; use the location of the PHI otherwise.
1495 DL = PN->getDebugLoc();
1496 if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
1497 DL = Inst->getDebugLoc();
1498
Dan Gohmanf81eca02010-04-22 20:46:50 +00001499 unsigned Reg = getRegForValue(PHIOp);
1500 if (Reg == 0) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001501 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001502 return false;
1503 }
Dan Gohmana4160c32010-07-07 16:29:44 +00001504 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohmanfb95f892010-05-07 01:10:20 +00001505 DL = DebugLoc();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001506 }
1507 }
1508
1509 return true;
1510}
Eli Bendersky75299e32013-04-19 22:29:18 +00001511
1512bool FastISel::tryToFoldLoad(const LoadInst *LI, const Instruction *FoldInst) {
Eli Bendersky462123f2013-04-19 23:26:18 +00001513 assert(LI->hasOneUse() &&
1514 "tryToFoldLoad expected a LoadInst with a single use");
Eli Bendersky75299e32013-04-19 22:29:18 +00001515 // We know that the load has a single use, but don't know what it is. If it
1516 // isn't one of the folded instructions, then we can't succeed here. Handle
1517 // this by scanning the single-use users of the load until we get to FoldInst.
1518 unsigned MaxUsers = 6; // Don't scan down huge single-use chains of instrs.
1519
1520 const Instruction *TheUser = LI->use_back();
1521 while (TheUser != FoldInst && // Scan up until we find FoldInst.
1522 // Stay in the right block.
1523 TheUser->getParent() == FoldInst->getParent() &&
1524 --MaxUsers) { // Don't scan too far.
1525 // If there are multiple or no uses of this instruction, then bail out.
1526 if (!TheUser->hasOneUse())
1527 return false;
1528
1529 TheUser = TheUser->use_back();
1530 }
1531
1532 // If we didn't find the fold instruction, then we failed to collapse the
1533 // sequence.
1534 if (TheUser != FoldInst)
1535 return false;
1536
1537 // Don't try to fold volatile loads. Target has to deal with alignment
1538 // constraints.
Eli Bendersky462123f2013-04-19 23:26:18 +00001539 if (LI->isVolatile())
1540 return false;
Eli Bendersky75299e32013-04-19 22:29:18 +00001541
1542 // Figure out which vreg this is going into. If there is no assigned vreg yet
1543 // then there actually was no reference to it. Perhaps the load is referenced
1544 // by a dead instruction.
1545 unsigned LoadReg = getRegForValue(LI);
1546 if (LoadReg == 0)
1547 return false;
1548
Eli Bendersky462123f2013-04-19 23:26:18 +00001549 // We can't fold if this vreg has no uses or more than one use. Multiple uses
1550 // may mean that the instruction got lowered to multiple MIs, or the use of
1551 // the loaded value ended up being multiple operands of the result.
1552 if (!MRI.hasOneUse(LoadReg))
1553 return false;
1554
Eli Bendersky75299e32013-04-19 22:29:18 +00001555 MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LoadReg);
Eli Bendersky75299e32013-04-19 22:29:18 +00001556 MachineInstr *User = &*RI;
1557
1558 // Set the insertion point properly. Folding the load can cause generation of
Eli Bendersky462123f2013-04-19 23:26:18 +00001559 // other random instructions (like sign extends) for addressing modes; make
Eli Bendersky75299e32013-04-19 22:29:18 +00001560 // sure they get inserted in a logical place before the new instruction.
1561 FuncInfo.InsertPt = User;
1562 FuncInfo.MBB = User->getParent();
1563
1564 // Ask the target to try folding the load.
1565 return tryToFoldLoadIntoMI(User, RI.getOperandNo(), LI);
1566}
1567
1568