blob: 445572a55297333d6c3fd0ab41c81e63ea8dcda1 [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
Chandler Carruthd04a8d42012-12-03 16:50:05 +000042#include "llvm/CodeGen/FastISel.h"
David Blaikie6d9dbd52013-06-16 20:34:15 +000043#include "llvm/ADT/Optional.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000044#include "llvm/ADT/Statistic.h"
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070045#include "llvm/Analysis/BranchProbabilityInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000046#include "llvm/Analysis/Loads.h"
47#include "llvm/CodeGen/Analysis.h"
48#include "llvm/CodeGen/FunctionLoweringInfo.h"
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070049#include "llvm/CodeGen/MachineFrameInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000050#include "llvm/CodeGen/MachineInstrBuilder.h"
51#include "llvm/CodeGen/MachineModuleInfo.h"
52#include "llvm/CodeGen/MachineRegisterInfo.h"
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070053#include "llvm/CodeGen/StackMaps.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000054#include "llvm/IR/DataLayout.h"
Stephen Hines36b56882014-04-23 16:57:46 -070055#include "llvm/IR/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000056#include "llvm/IR/Function.h"
57#include "llvm/IR/GlobalVariable.h"
58#include "llvm/IR/Instructions.h"
59#include "llvm/IR/IntrinsicInst.h"
60#include "llvm/IR/Operator.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000061#include "llvm/Support/Debug.h"
62#include "llvm/Support/ErrorHandling.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000063#include "llvm/Target/TargetInstrInfo.h"
Bob Wilsond49edb72012-08-03 04:06:28 +000064#include "llvm/Target/TargetLibraryInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000065#include "llvm/Target/TargetLowering.h"
Dan Gohmanbb466332008-08-20 21:05:57 +000066#include "llvm/Target/TargetMachine.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000067using namespace llvm;
68
Stephen Hinesdce4a402014-05-29 02:49:00 -070069#define DEBUG_TYPE "isel"
70
Chad Rosieraa5656c2011-11-28 19:59:09 +000071STATISTIC(NumFastIselSuccessIndependent, "Number of insts selected by "
72 "target-independent selector");
73STATISTIC(NumFastIselSuccessTarget, "Number of insts selected by "
74 "target-specific selector");
Chad Rosierae6f2cb2011-11-29 19:40:47 +000075STATISTIC(NumFastIselDead, "Number of dead insts removed on failure");
Chad Rosier053e69a2011-11-16 21:05:28 +000076
Dan Gohman84023e02010-07-10 09:00:22 +000077/// startNewBlock - Set the current block to which generated machine
78/// instructions will be appended, and clear the local CSE map.
79///
80void FastISel::startNewBlock() {
81 LocalValueMap.clear();
82
Jakob Stoklund Olesen1ab111e2013-07-04 04:53:49 +000083 // Instructions are appended to FuncInfo.MBB. If the basic block already
Jakob Stoklund Olesenef22e0e2013-07-04 04:32:39 +000084 // contains labels or copies, use the last instruction as the last local
85 // value.
Stephen Hinesdce4a402014-05-29 02:49:00 -070086 EmitStartPt = nullptr;
Jakob Stoklund Olesenef22e0e2013-07-04 04:32:39 +000087 if (!FuncInfo.MBB->empty())
88 EmitStartPt = &FuncInfo.MBB->back();
Ivan Krasin74af88a2011-08-18 22:06:10 +000089 LastLocalValue = EmitStartPt;
90}
91
Evan Cheng092e5e72013-02-11 01:27:15 +000092bool FastISel::LowerArguments() {
93 if (!FuncInfo.CanLowerReturn)
94 // Fallback to SDISel argument lowering code to deal with sret pointer
95 // parameter.
96 return false;
Stephen Lin155615d2013-07-08 00:37:03 +000097
Evan Cheng092e5e72013-02-11 01:27:15 +000098 if (!FastLowerArguments())
99 return false;
100
David Blaikie19489102013-06-21 22:56:30 +0000101 // Enter arguments into ValueMap for uses in non-entry BBs.
Evan Cheng092e5e72013-02-11 01:27:15 +0000102 for (Function::const_arg_iterator I = FuncInfo.Fn->arg_begin(),
103 E = FuncInfo.Fn->arg_end(); I != E; ++I) {
David Blaikie19489102013-06-21 22:56:30 +0000104 DenseMap<const Value *, unsigned>::iterator VI = LocalValueMap.find(I);
105 assert(VI != LocalValueMap.end() && "Missed an argument?");
106 FuncInfo.ValueMap[I] = VI->second;
Evan Cheng092e5e72013-02-11 01:27:15 +0000107 }
108 return true;
109}
110
Ivan Krasin74af88a2011-08-18 22:06:10 +0000111void FastISel::flushLocalValueMap() {
112 LocalValueMap.clear();
113 LastLocalValue = EmitStartPt;
114 recomputeInsertPt();
Dan Gohman84023e02010-07-10 09:00:22 +0000115}
116
Dan Gohmana6cb6412010-05-11 23:54:07 +0000117bool FastISel::hasTrivialKill(const Value *V) const {
Dan Gohman7f0d6952010-05-14 22:53:18 +0000118 // Don't consider constants or arguments to have trivial kills.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000119 const Instruction *I = dyn_cast<Instruction>(V);
Dan Gohman7f0d6952010-05-14 22:53:18 +0000120 if (!I)
121 return false;
122
123 // No-op casts are trivially coalesced by fast-isel.
124 if (const CastInst *Cast = dyn_cast<CastInst>(I))
Stephen Hines36b56882014-04-23 16:57:46 -0700125 if (Cast->isNoopCast(DL.getIntPtrType(Cast->getContext())) &&
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000126 !hasTrivialKill(Cast->getOperand(0)))
Dan Gohman7f0d6952010-05-14 22:53:18 +0000127 return false;
128
Chad Rosier22b34cc2011-11-15 23:34:05 +0000129 // GEPs with all zero indices are trivially coalesced by fast-isel.
130 if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I))
131 if (GEP->hasAllZeroIndices() && !hasTrivialKill(GEP->getOperand(0)))
132 return false;
133
Dan Gohman7f0d6952010-05-14 22:53:18 +0000134 // Only instructions with a single use in the same basic block are considered
135 // to have trivial kills.
136 return I->hasOneUse() &&
137 !(I->getOpcode() == Instruction::BitCast ||
138 I->getOpcode() == Instruction::PtrToInt ||
139 I->getOpcode() == Instruction::IntToPtr) &&
Stephen Hines36b56882014-04-23 16:57:46 -0700140 cast<Instruction>(*I->user_begin())->getParent() == I->getParent();
Dan Gohmana6cb6412010-05-11 23:54:07 +0000141}
142
Dan Gohman46510a72010-04-15 01:51:59 +0000143unsigned FastISel::getRegForValue(const Value *V) {
Owen Andersone50ed302009-08-10 22:56:29 +0000144 EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
Dan Gohman4fd55282009-04-07 20:40:11 +0000145 // Don't handle non-simple values in FastISel.
146 if (!RealVT.isSimple())
147 return 0;
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000148
149 // Ignore illegal types. We must do this before looking up the value
150 // in ValueMap because Arguments are given virtual registers regardless
151 // of whether FastISel can handle them.
Owen Anderson825b72b2009-08-11 20:47:22 +0000152 MVT VT = RealVT.getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000153 if (!TLI.isTypeLegal(VT)) {
Eli Friedman76927d732011-05-25 23:49:02 +0000154 // Handle integer promotions, though, because they're common and easy.
155 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Owen Anderson23b9b192009-08-12 00:36:31 +0000156 VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000157 else
158 return 0;
159 }
160
Eric Christopher4e270272012-03-20 01:07:47 +0000161 // Look up the value to see if we already have a register for it.
162 unsigned Reg = lookUpRegForValue(V);
Dan Gohman104e4ce2008-09-03 23:32:19 +0000163 if (Reg != 0)
164 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000165
Dan Gohman97c94b82010-05-06 00:02:14 +0000166 // In bottom-up mode, just create the virtual register which will be used
167 // to hold the value. It will be materialized later.
Dan Gohman84023e02010-07-10 09:00:22 +0000168 if (isa<Instruction>(V) &&
169 (!isa<AllocaInst>(V) ||
170 !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V))))
171 return FuncInfo.InitializeRegForValue(V);
Dan Gohman97c94b82010-05-06 00:02:14 +0000172
Eric Christopher76ad43c2012-10-03 08:10:01 +0000173 SavePoint SaveInsertPt = enterLocalValueArea();
Dan Gohman84023e02010-07-10 09:00:22 +0000174
175 // Materialize the value in a register. Emit any instructions in the
176 // local value area.
177 Reg = materializeRegForValue(V, VT);
178
Eric Christopher76ad43c2012-10-03 08:10:01 +0000179 leaveLocalValueArea(SaveInsertPt);
Dan Gohman84023e02010-07-10 09:00:22 +0000180
181 return Reg;
Dan Gohman1fdc6142010-05-03 23:36:34 +0000182}
183
Eric Christopher44a2c342010-08-17 01:30:33 +0000184/// materializeRegForValue - Helper for getRegForValue. This function is
Dan Gohman1fdc6142010-05-03 23:36:34 +0000185/// called when the value isn't already available in a register and must
186/// be materialized with new instructions.
187unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
188 unsigned Reg = 0;
189
Dan Gohman46510a72010-04-15 01:51:59 +0000190 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000191 if (CI->getValue().getActiveBits() <= 64)
192 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +0000193 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000194 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +0000195 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman1e9e8c32008-10-07 22:03:27 +0000196 // Translate this as an integer zero so that it can be
197 // local-CSE'd with actual integer zeros.
Owen Anderson1d0be152009-08-13 21:58:54 +0000198 Reg =
Stephen Hines36b56882014-04-23 16:57:46 -0700199 getRegForValue(Constant::getNullValue(DL.getIntPtrType(V->getContext())));
Dan Gohman46510a72010-04-15 01:51:59 +0000200 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Eli Friedmanbd125382011-04-28 00:42:03 +0000201 if (CF->isNullValue()) {
Eli Friedman2790ba82011-04-27 22:41:55 +0000202 Reg = TargetMaterializeFloatZero(CF);
203 } else {
204 // Try to emit the constant directly.
205 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
206 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000207
208 if (!Reg) {
Dan Gohman4183e312010-04-13 17:07:06 +0000209 // Try to emit the constant by using an integer constant with a cast.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000210 const APFloat &Flt = CF->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000211 EVT IntVT = TLI.getPointerTy();
Dan Gohmanad368ac2008-08-27 18:10:19 +0000212
213 uint64_t x[2];
214 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000215 bool isExact;
216 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
Eric Christopherc415af22012-03-20 01:07:56 +0000217 APFloat::rmTowardZero, &isExact);
Dale Johannesen23a98552008-10-09 23:00:39 +0000218 if (isExact) {
Jeffrey Yasskin3ba292d2011-07-18 21:45:40 +0000219 APInt IntVal(IntBitWidth, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000220
Owen Andersone922c022009-07-22 00:24:57 +0000221 unsigned IntegerReg =
Owen Andersoneed707b2009-07-24 23:12:02 +0000222 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000223 if (IntegerReg != 0)
Dan Gohmana6cb6412010-05-11 23:54:07 +0000224 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
225 IntegerReg, /*Kill=*/false);
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000226 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000227 }
Dan Gohman46510a72010-04-15 01:51:59 +0000228 } else if (const Operator *Op = dyn_cast<Operator>(V)) {
Dan Gohman20d4be12010-07-01 02:58:57 +0000229 if (!SelectOperator(Op, Op->getOpcode()))
230 if (!isa<Instruction>(Op) ||
231 !TargetSelectInstruction(cast<Instruction>(Op)))
232 return 0;
Dan Gohman37db6cd2010-06-21 14:17:46 +0000233 Reg = lookUpRegForValue(Op);
Dan Gohman205d9252008-08-28 21:19:07 +0000234 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000235 Reg = createResultReg(TLI.getRegClassFor(VT));
Stephen Hines36b56882014-04-23 16:57:46 -0700236 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohman84023e02010-07-10 09:00:22 +0000237 TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000238 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000239
Dan Gohmandceffe62008-09-25 01:28:51 +0000240 // If target-independent code couldn't handle the value, give target-specific
241 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000242 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000243 Reg = TargetMaterializeConstant(cast<Constant>(V));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000244
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000245 // Don't cache constant materializations in the general ValueMap.
246 // To do so would require tracking what uses they dominate.
Dan Gohman84023e02010-07-10 09:00:22 +0000247 if (Reg != 0) {
Dan Gohmandceffe62008-09-25 01:28:51 +0000248 LocalValueMap[V] = Reg;
Dan Gohman84023e02010-07-10 09:00:22 +0000249 LastLocalValue = MRI.getVRegDef(Reg);
250 }
Dan Gohman104e4ce2008-09-03 23:32:19 +0000251 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000252}
253
Dan Gohman46510a72010-04-15 01:51:59 +0000254unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng59fbc802008-09-09 01:26:59 +0000255 // Look up the value to see if we already have a register for it. We
256 // cache values defined by Instructions across blocks, and other values
257 // only locally. This is because Instructions already have the SSA
Dan Gohman1fdc6142010-05-03 23:36:34 +0000258 // def-dominates-use requirement enforced.
Dan Gohmana4160c32010-07-07 16:29:44 +0000259 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
260 if (I != FuncInfo.ValueMap.end())
Dan Gohman3193a682010-06-21 14:21:47 +0000261 return I->second;
Eric Christopher76ad43c2012-10-03 08:10:01 +0000262 return LocalValueMap[V];
Evan Cheng59fbc802008-09-09 01:26:59 +0000263}
264
Owen Andersoncc54e762008-08-30 00:38:46 +0000265/// UpdateValueMap - Update the value map to include the new mapping for this
266/// instruction, or insert an extra copy to get the result in a previous
267/// determined register.
268/// NOTE: This is only necessary because we might select a block that uses
269/// a value before we select the block that defines the value. It might be
270/// possible to fix this by selecting blocks in reverse postorder.
Eli Friedman482feb32011-05-16 21:06:17 +0000271void FastISel::UpdateValueMap(const Value *I, unsigned Reg, unsigned NumRegs) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000272 if (!isa<Instruction>(I)) {
273 LocalValueMap[I] = Reg;
Eli Friedman482feb32011-05-16 21:06:17 +0000274 return;
Dan Gohman40b189e2008-09-05 18:18:20 +0000275 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000276
Dan Gohmana4160c32010-07-07 16:29:44 +0000277 unsigned &AssignedReg = FuncInfo.ValueMap[I];
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000278 if (AssignedReg == 0)
Dan Gohman84023e02010-07-10 09:00:22 +0000279 // Use the new register.
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000280 AssignedReg = Reg;
Chris Lattner36e39462009-04-12 07:46:30 +0000281 else if (Reg != AssignedReg) {
Dan Gohman84023e02010-07-10 09:00:22 +0000282 // Arrange for uses of AssignedReg to be replaced by uses of Reg.
Eli Friedman482feb32011-05-16 21:06:17 +0000283 for (unsigned i = 0; i < NumRegs; i++)
284 FuncInfo.RegFixups[AssignedReg+i] = Reg+i;
Dan Gohman84023e02010-07-10 09:00:22 +0000285
286 AssignedReg = Reg;
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000287 }
Owen Andersoncc54e762008-08-30 00:38:46 +0000288}
289
Dan Gohmana6cb6412010-05-11 23:54:07 +0000290std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000291 unsigned IdxN = getRegForValue(Idx);
292 if (IdxN == 0)
293 // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000294 return std::pair<unsigned, bool>(0, false);
295
296 bool IdxNIsKill = hasTrivialKill(Idx);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000297
298 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Anderson766b5ef2009-08-11 21:59:30 +0000299 MVT PtrVT = TLI.getPointerTy();
Owen Andersone50ed302009-08-10 22:56:29 +0000300 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000301 if (IdxVT.bitsLT(PtrVT)) {
302 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND,
303 IdxN, IdxNIsKill);
304 IdxNIsKill = true;
305 }
306 else if (IdxVT.bitsGT(PtrVT)) {
307 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE,
308 IdxN, IdxNIsKill);
309 IdxNIsKill = true;
310 }
311 return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000312}
313
Dan Gohman84023e02010-07-10 09:00:22 +0000314void FastISel::recomputeInsertPt() {
315 if (getLastLocalValue()) {
316 FuncInfo.InsertPt = getLastLocalValue();
Dan Gohmanc6e59b72010-07-19 22:48:56 +0000317 FuncInfo.MBB = FuncInfo.InsertPt->getParent();
Dan Gohman84023e02010-07-10 09:00:22 +0000318 ++FuncInfo.InsertPt;
319 } else
320 FuncInfo.InsertPt = FuncInfo.MBB->getFirstNonPHI();
321
322 // Now skip past any EH_LABELs, which must remain at the beginning.
323 while (FuncInfo.InsertPt != FuncInfo.MBB->end() &&
324 FuncInfo.InsertPt->getOpcode() == TargetOpcode::EH_LABEL)
325 ++FuncInfo.InsertPt;
326}
327
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000328void FastISel::removeDeadCode(MachineBasicBlock::iterator I,
329 MachineBasicBlock::iterator E) {
330 assert (I && E && std::distance(I, E) > 0 && "Invalid iterator!");
331 while (I != E) {
332 MachineInstr *Dead = &*I;
333 ++I;
334 Dead->eraseFromParent();
Jan Wen Voungfa785cb2013-03-08 22:56:31 +0000335 ++NumFastIselDead;
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000336 }
337 recomputeInsertPt();
338}
339
Eric Christopher76ad43c2012-10-03 08:10:01 +0000340FastISel::SavePoint FastISel::enterLocalValueArea() {
Dan Gohman84023e02010-07-10 09:00:22 +0000341 MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt;
Stephen Hines36b56882014-04-23 16:57:46 -0700342 DebugLoc OldDL = DbgLoc;
Dan Gohman84023e02010-07-10 09:00:22 +0000343 recomputeInsertPt();
Stephen Hines36b56882014-04-23 16:57:46 -0700344 DbgLoc = DebugLoc();
Eric Christopher76ad43c2012-10-03 08:10:01 +0000345 SavePoint SP = { OldInsertPt, OldDL };
346 return SP;
Dan Gohman84023e02010-07-10 09:00:22 +0000347}
348
Eric Christopher76ad43c2012-10-03 08:10:01 +0000349void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) {
Dan Gohman84023e02010-07-10 09:00:22 +0000350 if (FuncInfo.InsertPt != FuncInfo.MBB->begin())
Stephen Hines36b56882014-04-23 16:57:46 -0700351 LastLocalValue = std::prev(FuncInfo.InsertPt);
Dan Gohman84023e02010-07-10 09:00:22 +0000352
353 // Restore the previous insert position.
Eric Christopher76ad43c2012-10-03 08:10:01 +0000354 FuncInfo.InsertPt = OldInsertPt.InsertPt;
Stephen Hines36b56882014-04-23 16:57:46 -0700355 DbgLoc = OldInsertPt.DL;
Dan Gohman84023e02010-07-10 09:00:22 +0000356}
357
Dan Gohmanbdedd442008-08-20 00:11:48 +0000358/// SelectBinaryOp - Select and emit code for a binary operator instruction,
359/// which has an opcode which directly corresponds to the given ISD opcode.
360///
Dan Gohman46510a72010-04-15 01:51:59 +0000361bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000362 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000363 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmanbdedd442008-08-20 00:11:48 +0000364 // Unhandled type. Halt "fast" selection and bail.
365 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000366
Dan Gohmanb71fea22008-08-26 20:52:40 +0000367 // We only handle legal types. For example, on x86-32 the instruction
368 // selector contains all of the 64-bit instructions from x86-64,
369 // under the assumption that i64 won't be used if the target doesn't
370 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000371 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000373 // don't require additional zeroing, which makes them easy.
Owen Anderson825b72b2009-08-11 20:47:22 +0000374 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000375 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
376 ISDOpcode == ISD::XOR))
Owen Anderson23b9b192009-08-12 00:36:31 +0000377 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohman638c6832008-09-05 18:44:22 +0000378 else
379 return false;
380 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000381
Chris Lattnerfff65b32011-04-17 01:16:47 +0000382 // Check if the first operand is a constant, and handle it as "ri". At -O0,
383 // we don't have anything that canonicalizes operand order.
384 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(0)))
385 if (isa<Instruction>(I) && cast<Instruction>(I)->isCommutative()) {
386 unsigned Op1 = getRegForValue(I->getOperand(1));
387 if (Op1 == 0) return false;
388
389 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
Owen Andersond74ea772011-04-22 23:38:06 +0000390
Chris Lattner602fc062011-04-17 20:23:29 +0000391 unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op1,
392 Op1IsKill, CI->getZExtValue(),
393 VT.getSimpleVT());
394 if (ResultReg == 0) return false;
Owen Andersond74ea772011-04-22 23:38:06 +0000395
Chris Lattner602fc062011-04-17 20:23:29 +0000396 // We successfully emitted code for the given LLVM Instruction.
397 UpdateValueMap(I, ResultReg);
398 return true;
Chris Lattnerfff65b32011-04-17 01:16:47 +0000399 }
Owen Andersond74ea772011-04-22 23:38:06 +0000400
401
Dan Gohman3df24e62008-09-03 23:12:08 +0000402 unsigned Op0 = getRegForValue(I->getOperand(0));
Chris Lattner602fc062011-04-17 20:23:29 +0000403 if (Op0 == 0) // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000404 return false;
405
Dan Gohmana6cb6412010-05-11 23:54:07 +0000406 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
407
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000408 // Check if the second operand is a constant and handle it appropriately.
409 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner602fc062011-04-17 20:23:29 +0000410 uint64_t Imm = CI->getZExtValue();
Owen Andersond74ea772011-04-22 23:38:06 +0000411
Chris Lattnerf051c1a2011-04-18 07:00:40 +0000412 // Transform "sdiv exact X, 8" -> "sra X, 3".
413 if (ISDOpcode == ISD::SDIV && isa<BinaryOperator>(I) &&
414 cast<BinaryOperator>(I)->isExact() &&
415 isPowerOf2_64(Imm)) {
416 Imm = Log2_64(Imm);
417 ISDOpcode = ISD::SRA;
418 }
Owen Andersond74ea772011-04-22 23:38:06 +0000419
Chad Rosier544b9b42012-03-22 00:21:17 +0000420 // Transform "urem x, pow2" -> "and x, pow2-1".
421 if (ISDOpcode == ISD::UREM && isa<BinaryOperator>(I) &&
422 isPowerOf2_64(Imm)) {
423 --Imm;
424 ISDOpcode = ISD::AND;
425 }
426
Chris Lattner602fc062011-04-17 20:23:29 +0000427 unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0,
428 Op0IsKill, Imm, VT.getSimpleVT());
429 if (ResultReg == 0) return false;
Owen Andersond74ea772011-04-22 23:38:06 +0000430
Chris Lattner602fc062011-04-17 20:23:29 +0000431 // We successfully emitted code for the given LLVM Instruction.
432 UpdateValueMap(I, ResultReg);
433 return true;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000434 }
435
Dan Gohman10df0fa2008-08-27 01:09:54 +0000436 // Check if the second operand is a constant float.
437 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000438 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000439 ISDOpcode, Op0, Op0IsKill, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000440 if (ResultReg != 0) {
441 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000442 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000443 return true;
444 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000445 }
446
Dan Gohman3df24e62008-09-03 23:12:08 +0000447 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000448 if (Op1 == 0)
449 // Unhandled operand. Halt "fast" selection and bail.
450 return false;
451
Dan Gohmana6cb6412010-05-11 23:54:07 +0000452 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
453
Dan Gohmanad368ac2008-08-27 18:10:19 +0000454 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000455 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000456 ISDOpcode,
457 Op0, Op0IsKill,
458 Op1, Op1IsKill);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000459 if (ResultReg == 0)
460 // Target-specific code wasn't able to find a machine opcode for
461 // the given ISD opcode and type. Halt "fast" selection and bail.
462 return false;
463
Dan Gohman8014e862008-08-20 00:23:20 +0000464 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000465 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000466 return true;
467}
468
Dan Gohman46510a72010-04-15 01:51:59 +0000469bool FastISel::SelectGetElementPtr(const User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000470 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000471 if (N == 0)
472 // Unhandled operand. Halt "fast" selection and bail.
473 return false;
474
Dan Gohmana6cb6412010-05-11 23:54:07 +0000475 bool NIsKill = hasTrivialKill(I->getOperand(0));
476
Chad Rosier478b06c2011-11-17 07:15:58 +0000477 // Keep a running tab of the total offset to coalesce multiple N = N + Offset
478 // into a single N = N + TotalOffset.
479 uint64_t TotalOffs = 0;
480 // FIXME: What's a good SWAG number for MaxOffs?
481 uint64_t MaxOffs = 2048;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000482 Type *Ty = I->getOperand(0)->getType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000483 MVT VT = TLI.getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +0000484 for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
485 E = I->op_end(); OI != E; ++OI) {
486 const Value *Idx = *OI;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000487 if (StructType *StTy = dyn_cast<StructType>(Ty)) {
Evan Cheng83785c82008-08-20 22:45:34 +0000488 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
489 if (Field) {
490 // N = N + Offset
Stephen Hines36b56882014-04-23 16:57:46 -0700491 TotalOffs += DL.getStructLayout(StTy)->getElementOffset(Field);
Chad Rosier478b06c2011-11-17 07:15:58 +0000492 if (TotalOffs >= MaxOffs) {
493 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
494 if (N == 0)
495 // Unhandled operand. Halt "fast" selection and bail.
496 return false;
497 NIsKill = true;
498 TotalOffs = 0;
499 }
Evan Cheng83785c82008-08-20 22:45:34 +0000500 }
501 Ty = StTy->getElementType(Field);
502 } else {
503 Ty = cast<SequentialType>(Ty)->getElementType();
504
505 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +0000506 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +0000507 if (CI->isZero()) continue;
Chad Rosier478b06c2011-11-17 07:15:58 +0000508 // N = N + Offset
Chad Rosier6016a4a2012-07-06 17:44:22 +0000509 TotalOffs +=
Stephen Hines36b56882014-04-23 16:57:46 -0700510 DL.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Chad Rosier478b06c2011-11-17 07:15:58 +0000511 if (TotalOffs >= MaxOffs) {
512 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
513 if (N == 0)
514 // Unhandled operand. Halt "fast" selection and bail.
515 return false;
516 NIsKill = true;
517 TotalOffs = 0;
518 }
519 continue;
520 }
521 if (TotalOffs) {
522 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000523 if (N == 0)
524 // Unhandled operand. Halt "fast" selection and bail.
525 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000526 NIsKill = true;
Chad Rosier478b06c2011-11-17 07:15:58 +0000527 TotalOffs = 0;
Evan Cheng83785c82008-08-20 22:45:34 +0000528 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000529
Evan Cheng83785c82008-08-20 22:45:34 +0000530 // N = N + Idx * ElementSize;
Stephen Hines36b56882014-04-23 16:57:46 -0700531 uint64_t ElementSize = DL.getTypeAllocSize(Ty);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000532 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
533 unsigned IdxN = Pair.first;
534 bool IdxNIsKill = Pair.second;
Evan Cheng83785c82008-08-20 22:45:34 +0000535 if (IdxN == 0)
536 // Unhandled operand. Halt "fast" selection and bail.
537 return false;
538
Dan Gohman80bc6e22008-08-26 20:57:08 +0000539 if (ElementSize != 1) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000540 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000541 if (IdxN == 0)
542 // Unhandled operand. Halt "fast" selection and bail.
543 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000544 IdxNIsKill = true;
Dan Gohman80bc6e22008-08-26 20:57:08 +0000545 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000546 N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
Evan Cheng83785c82008-08-20 22:45:34 +0000547 if (N == 0)
548 // Unhandled operand. Halt "fast" selection and bail.
549 return false;
550 }
551 }
Chad Rosier478b06c2011-11-17 07:15:58 +0000552 if (TotalOffs) {
553 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
554 if (N == 0)
555 // Unhandled operand. Halt "fast" selection and bail.
556 return false;
557 }
Evan Cheng83785c82008-08-20 22:45:34 +0000558
559 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000560 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000561 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000562}
563
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700564/// \brief Add a stackmap or patchpoint intrinsic call's live variable operands
565/// to a stackmap or patchpoint machine instruction.
566bool FastISel::addStackMapLiveVars(SmallVectorImpl<MachineOperand> &Ops,
567 const CallInst *CI, unsigned StartIdx) {
568 for (unsigned i = StartIdx, e = CI->getNumArgOperands(); i != e; ++i) {
569 Value *Val = CI->getArgOperand(i);
570 // Check for constants and encode them with a StackMaps::ConstantOp prefix.
571 if (auto *C = dyn_cast<ConstantInt>(Val)) {
572 Ops.push_back(MachineOperand::CreateImm(StackMaps::ConstantOp));
573 Ops.push_back(MachineOperand::CreateImm(C->getSExtValue()));
574 } else if (isa<ConstantPointerNull>(Val)) {
575 Ops.push_back(MachineOperand::CreateImm(StackMaps::ConstantOp));
576 Ops.push_back(MachineOperand::CreateImm(0));
577 } else if (auto *AI = dyn_cast<AllocaInst>(Val)) {
578 // Values coming from a stack location also require a sepcial encoding,
579 // but that is added later on by the target specific frame index
580 // elimination implementation.
581 auto SI = FuncInfo.StaticAllocaMap.find(AI);
582 if (SI != FuncInfo.StaticAllocaMap.end())
583 Ops.push_back(MachineOperand::CreateFI(SI->second));
584 else
585 return false;
586 } else {
587 unsigned Reg = getRegForValue(Val);
588 if (Reg == 0)
589 return false;
590 Ops.push_back(MachineOperand::CreateReg(Reg, /*IsDef=*/false));
591 }
592 }
593
594 return true;
595}
596
597bool FastISel::SelectStackmap(const CallInst *I) {
598 // void @llvm.experimental.stackmap(i64 <id>, i32 <numShadowBytes>,
599 // [live variables...])
600 assert(I->getCalledFunction()->getReturnType()->isVoidTy() &&
601 "Stackmap cannot return a value.");
602
603 // The stackmap intrinsic only records the live variables (the arguments
604 // passed to it) and emits NOPS (if requested). Unlike the patchpoint
605 // intrinsic, this won't be lowered to a function call. This means we don't
606 // have to worry about calling conventions and target-specific lowering code.
607 // Instead we perform the call lowering right here.
608 //
609 // CALLSEQ_START(0)
610 // STACKMAP(id, nbytes, ...)
611 // CALLSEQ_END(0, 0)
612 //
613 SmallVector<MachineOperand, 32> Ops;
614
615 // Add the <id> and <numBytes> constants.
616 assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::IDPos)) &&
617 "Expected a constant integer.");
618 const auto *ID = cast<ConstantInt>(I->getOperand(PatchPointOpers::IDPos));
619 Ops.push_back(MachineOperand::CreateImm(ID->getZExtValue()));
620
621 assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::NBytesPos)) &&
622 "Expected a constant integer.");
623 const auto *NumBytes =
624 cast<ConstantInt>(I->getOperand(PatchPointOpers::NBytesPos));
625 Ops.push_back(MachineOperand::CreateImm(NumBytes->getZExtValue()));
626
627 // Push live variables for the stack map (skipping the first two arguments
628 // <id> and <numBytes>).
629 if (!addStackMapLiveVars(Ops, I, 2))
630 return false;
631
632 // We are not adding any register mask info here, because the stackmap doesn't
633 // clobber anything.
634
635 // Add scratch registers as implicit def and early clobber.
636 CallingConv::ID CC = I->getCallingConv();
637 const MCPhysReg *ScratchRegs = TLI.getScratchRegisters(CC);
638 for (unsigned i = 0; ScratchRegs[i]; ++i)
639 Ops.push_back(MachineOperand::CreateReg(
640 ScratchRegs[i], /*IsDef=*/true, /*IsImp=*/true, /*IsKill=*/false,
641 /*IsDead=*/false, /*IsUndef=*/false, /*IsEarlyClobber=*/true));
642
643 // Issue CALLSEQ_START
644 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
645 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
646 .addImm(0);
647
648 // Issue STACKMAP.
649 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
650 TII.get(TargetOpcode::STACKMAP));
651 for (auto const &MO : Ops)
652 MIB.addOperand(MO);
653
654 // Issue CALLSEQ_END
655 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
656 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
657 .addImm(0).addImm(0);
658
659 // Inform the Frame Information that we have a stackmap in this function.
660 FuncInfo.MF->getFrameInfo()->setHasStackMap();
661
662 return true;
663}
664
Dan Gohman46510a72010-04-15 01:51:59 +0000665bool FastISel::SelectCall(const User *I) {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000666 const CallInst *Call = cast<CallInst>(I);
667
668 // Handle simple inline asms.
Dan Gohman9e15d652011-10-12 15:56:56 +0000669 if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000670 // Don't attempt to handle constraints.
671 if (!IA->getConstraintString().empty())
672 return false;
673
674 unsigned ExtraInfo = 0;
675 if (IA->hasSideEffects())
676 ExtraInfo |= InlineAsm::Extra_HasSideEffects;
677 if (IA->isAlignStack())
678 ExtraInfo |= InlineAsm::Extra_IsAlignStack;
679
Stephen Hines36b56882014-04-23 16:57:46 -0700680 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmana61e73b2011-04-26 17:18:34 +0000681 TII.get(TargetOpcode::INLINEASM))
682 .addExternalSymbol(IA->getAsmString().c_str())
683 .addImm(ExtraInfo);
684 return true;
685 }
686
Michael J. Spencerc9c137b2012-02-22 19:06:13 +0000687 MachineModuleInfo &MMI = FuncInfo.MF->getMMI();
688 ComputeUsesVAFloatArgument(*Call, &MMI);
689
Dan Gohmana61e73b2011-04-26 17:18:34 +0000690 const Function *F = Call->getCalledFunction();
Dan Gohman33134c42008-09-25 17:05:24 +0000691 if (!F) return false;
692
Dan Gohman4183e312010-04-13 17:07:06 +0000693 // Handle selected intrinsic function calls.
Chris Lattner832e4942011-04-19 05:52:03 +0000694 switch (F->getIntrinsicID()) {
Dan Gohman33134c42008-09-25 17:05:24 +0000695 default: break;
Chad Rosieraefd36b2012-05-11 23:21:01 +0000696 // At -O0 we don't care about the lifetime intrinsics.
Eric Christopher9b5d6b82012-02-17 23:03:39 +0000697 case Intrinsic::lifetime_start:
698 case Intrinsic::lifetime_end:
Chad Rosierfd065bb2012-07-06 17:33:39 +0000699 // The donothing intrinsic does, well, nothing.
700 case Intrinsic::donothing:
Eric Christopher9b5d6b82012-02-17 23:03:39 +0000701 return true;
Chad Rosierfd065bb2012-07-06 17:33:39 +0000702
Bill Wendling92c1e122009-02-13 02:16:35 +0000703 case Intrinsic::dbg_declare: {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000704 const DbgDeclareInst *DI = cast<DbgDeclareInst>(Call);
Manman Rencbafae62013-06-28 05:43:10 +0000705 DIVariable DIVar(DI->getVariable());
Stephen Lin155615d2013-07-08 00:37:03 +0000706 assert((!DIVar || DIVar.isVariable()) &&
Manman Rencbafae62013-06-28 05:43:10 +0000707 "Variable in DbgDeclareInst should be either null or a DIVariable.");
708 if (!DIVar ||
Eric Christopherbb54d212012-03-15 21:33:44 +0000709 !FuncInfo.MF->getMMI().hasDebugInfo()) {
710 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Devang Patel7e1e31f2009-07-02 22:43:26 +0000711 return true;
Eric Christopherbb54d212012-03-15 21:33:44 +0000712 }
Devang Patel7e1e31f2009-07-02 22:43:26 +0000713
Dan Gohman46510a72010-04-15 01:51:59 +0000714 const Value *Address = DI->getAddress();
Eric Christopherccaea7d2012-03-15 21:33:47 +0000715 if (!Address || isa<UndefValue>(Address)) {
Eric Christopherbb54d212012-03-15 21:33:44 +0000716 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Dale Johannesendc918562010-02-06 02:26:02 +0000717 return true;
Eric Christopherbb54d212012-03-15 21:33:44 +0000718 }
Devang Patel6fe75aa2010-09-14 20:29:31 +0000719
Adrian Prantl35176402013-07-09 20:28:37 +0000720 unsigned Offset = 0;
David Blaikie6d9dbd52013-06-16 20:34:15 +0000721 Optional<MachineOperand> Op;
722 if (const Argument *Arg = dyn_cast<Argument>(Address))
Devang Patel9aee3352011-09-08 22:59:09 +0000723 // Some arguments' frame index is recorded during argument lowering.
Adrian Prantl35176402013-07-09 20:28:37 +0000724 Offset = FuncInfo.getArgumentFrameIndex(Arg);
725 if (Offset)
726 Op = MachineOperand::CreateFI(Offset);
David Blaikie6d9dbd52013-06-16 20:34:15 +0000727 if (!Op)
728 if (unsigned Reg = lookUpRegForValue(Address))
729 Op = MachineOperand::CreateReg(Reg, false);
Eric Christopher8c5293c2012-03-20 01:07:58 +0000730
Bill Wendling84364a42012-03-30 00:02:55 +0000731 // If we have a VLA that has a "use" in a metadata node that's then used
732 // here but it has no other uses, then we have a problem. E.g.,
733 //
734 // int foo (const int *x) {
735 // char a[*x];
736 // return 0;
737 // }
738 //
739 // If we assign 'a' a vreg and fast isel later on has to use the selection
740 // DAG isel, it will want to copy the value to the vreg. However, there are
741 // no uses, which goes counter to what selection DAG isel expects.
David Blaikie6d9dbd52013-06-16 20:34:15 +0000742 if (!Op && !Address->use_empty() && isa<Instruction>(Address) &&
Eric Christopher8c5293c2012-03-20 01:07:58 +0000743 (!isa<AllocaInst>(Address) ||
744 !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(Address))))
David Blaikie6d9dbd52013-06-16 20:34:15 +0000745 Op = MachineOperand::CreateReg(FuncInfo.InitializeRegForValue(Address),
Adrian Prantl0a4371a2013-09-18 22:08:59 +0000746 false);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000747
Adrian Prantl0a4371a2013-09-18 22:08:59 +0000748 if (Op) {
Adrian Prantl35176402013-07-09 20:28:37 +0000749 if (Op->isReg()) {
750 Op->setIsDebug(true);
Stephen Hines36b56882014-04-23 16:57:46 -0700751 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
David Blaikie54de36b2013-10-14 20:15:04 +0000752 TII.get(TargetOpcode::DBG_VALUE), false, Op->getReg(), 0,
753 DI->getVariable());
754 } else
Stephen Hines36b56882014-04-23 16:57:46 -0700755 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
David Blaikie54de36b2013-10-14 20:15:04 +0000756 TII.get(TargetOpcode::DBG_VALUE))
757 .addOperand(*Op)
758 .addImm(0)
759 .addMetadata(DI->getVariable());
Adrian Prantl0a4371a2013-09-18 22:08:59 +0000760 } else {
Eric Christopher4476bae2012-03-20 01:07:53 +0000761 // We can't yet handle anything else here because it would require
762 // generating code, thus altering codegen because of debug info.
Adrian Prantl5da4e4f2013-05-22 18:02:19 +0000763 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Adrian Prantl0a4371a2013-09-18 22:08:59 +0000764 }
Dan Gohman33134c42008-09-25 17:05:24 +0000765 return true;
Bill Wendling92c1e122009-02-13 02:16:35 +0000766 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000767 case Intrinsic::dbg_value: {
Dale Johannesen343b42e2010-04-07 01:15:14 +0000768 // This form of DBG_VALUE is target-independent.
Dan Gohmana61e73b2011-04-26 17:18:34 +0000769 const DbgValueInst *DI = cast<DbgValueInst>(Call);
Evan Chenge837dea2011-06-28 19:10:37 +0000770 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohman46510a72010-04-15 01:51:59 +0000771 const Value *V = DI->getValue();
Dale Johannesen45df7612010-02-26 20:01:55 +0000772 if (!V) {
773 // Currently the optimizer can produce this; insert an undef to
774 // help debugging. Probably the optimizer should not do this.
Stephen Hines36b56882014-04-23 16:57:46 -0700775 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohman84023e02010-07-10 09:00:22 +0000776 .addReg(0U).addImm(DI->getOffset())
777 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000778 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Devang Patel8594d422011-06-24 20:46:11 +0000779 if (CI->getBitWidth() > 64)
Stephen Hines36b56882014-04-23 16:57:46 -0700780 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Devang Patel8594d422011-06-24 20:46:11 +0000781 .addCImm(CI).addImm(DI->getOffset())
782 .addMetadata(DI->getVariable());
Chad Rosier6016a4a2012-07-06 17:44:22 +0000783 else
Stephen Hines36b56882014-04-23 16:57:46 -0700784 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Devang Patel8594d422011-06-24 20:46:11 +0000785 .addImm(CI->getZExtValue()).addImm(DI->getOffset())
786 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000787 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Stephen Hines36b56882014-04-23 16:57:46 -0700788 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohman84023e02010-07-10 09:00:22 +0000789 .addFPImm(CF).addImm(DI->getOffset())
790 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000791 } else if (unsigned Reg = lookUpRegForValue(V)) {
Adrian Prantl818833f2013-09-16 23:29:03 +0000792 // FIXME: This does not handle register-indirect values at offset 0.
Adrian Prantl35176402013-07-09 20:28:37 +0000793 bool IsIndirect = DI->getOffset() != 0;
Stephen Hines36b56882014-04-23 16:57:46 -0700794 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, IsIndirect,
Adrian Prantl35176402013-07-09 20:28:37 +0000795 Reg, DI->getOffset(), DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000796 } else {
797 // We can't yet handle anything else here because it would require
798 // generating code, thus altering codegen because of debug info.
Adrian Prantl5da4e4f2013-05-22 18:02:19 +0000799 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000800 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000801 return true;
802 }
Eli Friedmand0118a22011-05-14 00:47:51 +0000803 case Intrinsic::objectsize: {
804 ConstantInt *CI = cast<ConstantInt>(Call->getArgOperand(1));
805 unsigned long long Res = CI->isZero() ? -1ULL : 0;
806 Constant *ResCI = ConstantInt::get(Call->getType(), Res);
807 unsigned ResultReg = getRegForValue(ResCI);
808 if (ResultReg == 0)
809 return false;
810 UpdateValueMap(Call, ResultReg);
811 return true;
812 }
Chad Rosier33947b42013-03-07 20:42:17 +0000813 case Intrinsic::expect: {
Chad Rosier4fde76d2013-03-07 21:38:33 +0000814 unsigned ResultReg = getRegForValue(Call->getArgOperand(0));
Nick Lewycky33cdfe92013-03-11 21:44:37 +0000815 if (ResultReg == 0)
816 return false;
Chad Rosier4fde76d2013-03-07 21:38:33 +0000817 UpdateValueMap(Call, ResultReg);
818 return true;
Chad Rosier33947b42013-03-07 20:42:17 +0000819 }
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700820 case Intrinsic::experimental_stackmap:
821 return SelectStackmap(Call);
Dan Gohman33134c42008-09-25 17:05:24 +0000822 }
Dan Gohman4183e312010-04-13 17:07:06 +0000823
Ivan Krasin74af88a2011-08-18 22:06:10 +0000824 // Usually, it does not make sense to initialize a value,
825 // make an unrelated function call and use the value, because
826 // it tends to be spilled on the stack. So, we move the pointer
827 // to the last local value to the beginning of the block, so that
828 // all the values which have already been materialized,
829 // appear after the call. It also makes sense to skip intrinsics
830 // since they tend to be inlined.
Pete Cooperb704ffb2013-02-22 01:50:38 +0000831 if (!isa<IntrinsicInst>(Call))
Ivan Krasin74af88a2011-08-18 22:06:10 +0000832 flushLocalValueMap();
833
Dan Gohman4183e312010-04-13 17:07:06 +0000834 // An arbitrary call. Bail.
Dan Gohman33134c42008-09-25 17:05:24 +0000835 return false;
836}
837
Dan Gohman46510a72010-04-15 01:51:59 +0000838bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000839 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
840 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000841
Owen Anderson825b72b2009-08-11 20:47:22 +0000842 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
843 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersond0533c92008-08-26 23:46:32 +0000844 // Unhandled type. Halt "fast" selection and bail.
845 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000846
Eli Friedman76927d732011-05-25 23:49:02 +0000847 // Check if the destination type is legal.
Dan Gohman474d3b32009-03-13 23:53:06 +0000848 if (!TLI.isTypeLegal(DstVT))
Eli Friedman76927d732011-05-25 23:49:02 +0000849 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000850
Eli Friedman76927d732011-05-25 23:49:02 +0000851 // Check if the source operand is legal.
Dan Gohman474d3b32009-03-13 23:53:06 +0000852 if (!TLI.isTypeLegal(SrcVT))
Eli Friedman76927d732011-05-25 23:49:02 +0000853 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000854
Dan Gohman3df24e62008-09-03 23:12:08 +0000855 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000856 if (!InputReg)
857 // Unhandled operand. Halt "fast" selection and bail.
858 return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000859
Dan Gohmana6cb6412010-05-11 23:54:07 +0000860 bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
861
Owen Andersond0533c92008-08-26 23:46:32 +0000862 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
863 DstVT.getSimpleVT(),
864 Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000865 InputReg, InputRegIsKill);
Owen Andersond0533c92008-08-26 23:46:32 +0000866 if (!ResultReg)
867 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000868
Dan Gohman3df24e62008-09-03 23:12:08 +0000869 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000870 return true;
871}
872
Dan Gohman46510a72010-04-15 01:51:59 +0000873bool FastISel::SelectBitCast(const User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000874 // If the bitcast doesn't change the type, just use the operand value.
875 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000876 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000877 if (Reg == 0)
878 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000879 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000880 return true;
881 }
882
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000883 // Bitcasts of other values become reg-reg copies or BITCAST operators.
Patrik Hagglund3d170e62012-12-17 14:30:06 +0000884 EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType());
885 EVT DstEVT = TLI.getValueType(I->getType());
886 if (SrcEVT == MVT::Other || DstEVT == MVT::Other ||
887 !TLI.isTypeLegal(SrcEVT) || !TLI.isTypeLegal(DstEVT))
Owen Andersond0533c92008-08-26 23:46:32 +0000888 // Unhandled type. Halt "fast" selection and bail.
889 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000890
Patrik Hagglund3d170e62012-12-17 14:30:06 +0000891 MVT SrcVT = SrcEVT.getSimpleVT();
892 MVT DstVT = DstEVT.getSimpleVT();
Dan Gohman3df24e62008-09-03 23:12:08 +0000893 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000894 if (Op0 == 0)
895 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000896 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000897
898 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000899
Dan Gohmanad368ac2008-08-27 18:10:19 +0000900 // First, try to perform the bitcast by inserting a reg-reg copy.
901 unsigned ResultReg = 0;
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000902 if (SrcVT == DstVT) {
Craig Topper44d23822012-02-22 05:59:10 +0000903 const TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
904 const TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
Jakob Stoklund Olesene7917bb2010-07-11 05:16:54 +0000905 // Don't attempt a cross-class copy. It will likely fail.
906 if (SrcClass == DstClass) {
907 ResultReg = createResultReg(DstClass);
Stephen Hines36b56882014-04-23 16:57:46 -0700908 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
909 TII.get(TargetOpcode::COPY), ResultReg).addReg(Op0);
Jakob Stoklund Olesene7917bb2010-07-11 05:16:54 +0000910 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000911 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000912
913 // If the reg-reg copy failed, select a BITCAST opcode.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000914 if (!ResultReg)
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000915 ResultReg = FastEmit_r(SrcVT, DstVT, ISD::BITCAST, Op0, Op0IsKill);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000916
Dan Gohmanad368ac2008-08-27 18:10:19 +0000917 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000918 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000919
Dan Gohman3df24e62008-09-03 23:12:08 +0000920 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000921 return true;
922}
923
Dan Gohman3df24e62008-09-03 23:12:08 +0000924bool
Dan Gohman46510a72010-04-15 01:51:59 +0000925FastISel::SelectInstruction(const Instruction *I) {
Dan Gohmane8c92dd2010-04-23 15:29:50 +0000926 // Just before the terminator instruction, insert instructions to
927 // feed PHI nodes in successor blocks.
928 if (isa<TerminatorInst>(I))
929 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
930 return false;
931
Stephen Hines36b56882014-04-23 16:57:46 -0700932 DbgLoc = I->getDebugLoc();
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000933
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000934 MachineBasicBlock::iterator SavedInsertPt = FuncInfo.InsertPt;
935
Bob Wilsond49edb72012-08-03 04:06:28 +0000936 if (const CallInst *Call = dyn_cast<CallInst>(I)) {
937 const Function *F = Call->getCalledFunction();
938 LibFunc::Func Func;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700939
940 // As a special case, don't handle calls to builtin library functions that
941 // may be translated directly to target instructions.
Bob Wilsond49edb72012-08-03 04:06:28 +0000942 if (F && !F->hasLocalLinkage() && F->hasName() &&
943 LibInfo->getLibFunc(F->getName(), Func) &&
Bob Wilson982dc842012-08-03 21:26:24 +0000944 LibInfo->hasOptimizedCodeGen(Func))
Bob Wilsond49edb72012-08-03 04:06:28 +0000945 return false;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700946
947 // Don't handle Intrinsic::trap if a trap funciton is specified.
948 if (F && F->getIntrinsicID() == Intrinsic::trap &&
949 !TM.Options.getTrapFunctionName().empty())
950 return false;
Bob Wilsond49edb72012-08-03 04:06:28 +0000951 }
952
Dan Gohman6e3ff372009-12-05 01:27:58 +0000953 // First, try doing target-independent selection.
Michael Ilseman7dbd34b2013-02-27 19:54:00 +0000954 if (SelectOperator(I, I->getOpcode())) {
Jan Wen Voungfa785cb2013-03-08 22:56:31 +0000955 ++NumFastIselSuccessIndependent;
Stephen Hines36b56882014-04-23 16:57:46 -0700956 DbgLoc = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000957 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000958 }
Chad Rosier6016a4a2012-07-06 17:44:22 +0000959 // Remove dead code. However, ignore call instructions since we've flushed
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000960 // the local value map and recomputed the insert point.
961 if (!isa<CallInst>(I)) {
962 recomputeInsertPt();
963 if (SavedInsertPt != FuncInfo.InsertPt)
964 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
965 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000966
967 // Next, try calling the target to attempt to handle the instruction.
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000968 SavedInsertPt = FuncInfo.InsertPt;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000969 if (TargetSelectInstruction(I)) {
Jan Wen Voungfa785cb2013-03-08 22:56:31 +0000970 ++NumFastIselSuccessTarget;
Stephen Hines36b56882014-04-23 16:57:46 -0700971 DbgLoc = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000972 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000973 }
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000974 // Check for dead code and remove as necessary.
975 recomputeInsertPt();
976 if (SavedInsertPt != FuncInfo.InsertPt)
977 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
Dan Gohman6e3ff372009-12-05 01:27:58 +0000978
Stephen Hines36b56882014-04-23 16:57:46 -0700979 DbgLoc = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000980 return false;
Dan Gohman40b189e2008-09-05 18:18:20 +0000981}
982
Dan Gohmand98d6202008-10-02 22:15:21 +0000983/// FastEmitBranch - Emit an unconditional branch to the given block,
984/// unless it is the immediate (fall-through) successor, and update
985/// the CFG.
986void
Stephen Hines36b56882014-04-23 16:57:46 -0700987FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DbgLoc) {
Evan Cheng092e5e72013-02-11 01:27:15 +0000988 if (FuncInfo.MBB->getBasicBlock()->size() > 1 &&
989 FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
Eric Christopher18112d82012-04-10 18:18:10 +0000990 // For more accurate line information if this is the only instruction
991 // in the block then emit it, otherwise we have the unconditional
992 // fall-through case, which needs no instructions.
Dan Gohmand98d6202008-10-02 22:15:21 +0000993 } else {
994 // The unconditional branch case.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700995 TII.InsertBranch(*FuncInfo.MBB, MSucc, nullptr,
Stephen Hines36b56882014-04-23 16:57:46 -0700996 SmallVector<MachineOperand, 0>(), DbgLoc);
Dan Gohmand98d6202008-10-02 22:15:21 +0000997 }
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700998 uint32_t BranchWeight = 0;
999 if (FuncInfo.BPI)
1000 BranchWeight = FuncInfo.BPI->getEdgeWeight(FuncInfo.MBB->getBasicBlock(),
1001 MSucc->getBasicBlock());
1002 FuncInfo.MBB->addSuccessor(MSucc, BranchWeight);
Dan Gohmand98d6202008-10-02 22:15:21 +00001003}
1004
Dan Gohman3d45a852009-09-03 22:53:57 +00001005/// SelectFNeg - Emit an FNeg operation.
1006///
1007bool
Dan Gohman46510a72010-04-15 01:51:59 +00001008FastISel::SelectFNeg(const User *I) {
Dan Gohman3d45a852009-09-03 22:53:57 +00001009 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
1010 if (OpReg == 0) return false;
1011
Dan Gohmana6cb6412010-05-11 23:54:07 +00001012 bool OpRegIsKill = hasTrivialKill(I);
1013
Dan Gohman4a215a12009-09-11 00:36:43 +00001014 // If the target has ISD::FNEG, use it.
1015 EVT VT = TLI.getValueType(I->getType());
1016 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +00001017 ISD::FNEG, OpReg, OpRegIsKill);
Dan Gohman4a215a12009-09-11 00:36:43 +00001018 if (ResultReg != 0) {
1019 UpdateValueMap(I, ResultReg);
1020 return true;
1021 }
1022
Dan Gohman5e5abb72009-09-11 00:34:46 +00001023 // Bitcast the value to integer, twiddle the sign bit with xor,
1024 // and then bitcast it back to floating-point.
Dan Gohman3d45a852009-09-03 22:53:57 +00001025 if (VT.getSizeInBits() > 64) return false;
Dan Gohman5e5abb72009-09-11 00:34:46 +00001026 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
1027 if (!TLI.isTypeLegal(IntVT))
1028 return false;
1029
1030 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001031 ISD::BITCAST, OpReg, OpRegIsKill);
Dan Gohman5e5abb72009-09-11 00:34:46 +00001032 if (IntReg == 0)
1033 return false;
1034
Dan Gohmana6cb6412010-05-11 23:54:07 +00001035 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
1036 IntReg, /*Kill=*/true,
Dan Gohman5e5abb72009-09-11 00:34:46 +00001037 UINT64_C(1) << (VT.getSizeInBits()-1),
1038 IntVT.getSimpleVT());
1039 if (IntResultReg == 0)
1040 return false;
1041
1042 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001043 ISD::BITCAST, IntResultReg, /*Kill=*/true);
Dan Gohman3d45a852009-09-03 22:53:57 +00001044 if (ResultReg == 0)
1045 return false;
1046
1047 UpdateValueMap(I, ResultReg);
1048 return true;
1049}
1050
Dan Gohman40b189e2008-09-05 18:18:20 +00001051bool
Eli Friedman2586b8f2011-05-16 20:27:46 +00001052FastISel::SelectExtractValue(const User *U) {
1053 const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U);
Eli Friedmana4c920d2011-05-16 20:34:53 +00001054 if (!EVI)
Eli Friedman2586b8f2011-05-16 20:27:46 +00001055 return false;
1056
Eli Friedman482feb32011-05-16 21:06:17 +00001057 // Make sure we only try to handle extracts with a legal result. But also
1058 // allow i1 because it's easy.
Eli Friedman2586b8f2011-05-16 20:27:46 +00001059 EVT RealVT = TLI.getValueType(EVI->getType(), /*AllowUnknown=*/true);
1060 if (!RealVT.isSimple())
1061 return false;
1062 MVT VT = RealVT.getSimpleVT();
Eli Friedman482feb32011-05-16 21:06:17 +00001063 if (!TLI.isTypeLegal(VT) && VT != MVT::i1)
Eli Friedman2586b8f2011-05-16 20:27:46 +00001064 return false;
1065
1066 const Value *Op0 = EVI->getOperand(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001067 Type *AggTy = Op0->getType();
Eli Friedman2586b8f2011-05-16 20:27:46 +00001068
1069 // Get the base result register.
1070 unsigned ResultReg;
1071 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(Op0);
1072 if (I != FuncInfo.ValueMap.end())
1073 ResultReg = I->second;
Eli Friedman0b4d96b2011-06-06 05:46:34 +00001074 else if (isa<Instruction>(Op0))
Eli Friedman2586b8f2011-05-16 20:27:46 +00001075 ResultReg = FuncInfo.InitializeRegForValue(Op0);
Eli Friedman0b4d96b2011-06-06 05:46:34 +00001076 else
1077 return false; // fast-isel can't handle aggregate constants at the moment
Eli Friedman2586b8f2011-05-16 20:27:46 +00001078
1079 // Get the actual result register, which is an offset from the base register.
Jay Foadfc6d3a42011-07-13 10:26:04 +00001080 unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices());
Eli Friedman2586b8f2011-05-16 20:27:46 +00001081
1082 SmallVector<EVT, 4> AggValueVTs;
1083 ComputeValueVTs(TLI, AggTy, AggValueVTs);
1084
1085 for (unsigned i = 0; i < VTIndex; i++)
1086 ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]);
1087
1088 UpdateValueMap(EVI, ResultReg);
1089 return true;
1090}
1091
1092bool
Dan Gohman46510a72010-04-15 01:51:59 +00001093FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohman40b189e2008-09-05 18:18:20 +00001094 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001095 case Instruction::Add:
1096 return SelectBinaryOp(I, ISD::ADD);
1097 case Instruction::FAdd:
1098 return SelectBinaryOp(I, ISD::FADD);
1099 case Instruction::Sub:
1100 return SelectBinaryOp(I, ISD::SUB);
1101 case Instruction::FSub:
Dan Gohman3d45a852009-09-03 22:53:57 +00001102 // FNeg is currently represented in LLVM IR as a special case of FSub.
1103 if (BinaryOperator::isFNeg(I))
1104 return SelectFNeg(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001105 return SelectBinaryOp(I, ISD::FSUB);
1106 case Instruction::Mul:
1107 return SelectBinaryOp(I, ISD::MUL);
1108 case Instruction::FMul:
1109 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman3df24e62008-09-03 23:12:08 +00001110 case Instruction::SDiv:
1111 return SelectBinaryOp(I, ISD::SDIV);
1112 case Instruction::UDiv:
1113 return SelectBinaryOp(I, ISD::UDIV);
1114 case Instruction::FDiv:
1115 return SelectBinaryOp(I, ISD::FDIV);
1116 case Instruction::SRem:
1117 return SelectBinaryOp(I, ISD::SREM);
1118 case Instruction::URem:
1119 return SelectBinaryOp(I, ISD::UREM);
1120 case Instruction::FRem:
1121 return SelectBinaryOp(I, ISD::FREM);
1122 case Instruction::Shl:
1123 return SelectBinaryOp(I, ISD::SHL);
1124 case Instruction::LShr:
1125 return SelectBinaryOp(I, ISD::SRL);
1126 case Instruction::AShr:
1127 return SelectBinaryOp(I, ISD::SRA);
1128 case Instruction::And:
1129 return SelectBinaryOp(I, ISD::AND);
1130 case Instruction::Or:
1131 return SelectBinaryOp(I, ISD::OR);
1132 case Instruction::Xor:
1133 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001134
Dan Gohman3df24e62008-09-03 23:12:08 +00001135 case Instruction::GetElementPtr:
1136 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +00001137
Dan Gohman3df24e62008-09-03 23:12:08 +00001138 case Instruction::Br: {
Dan Gohman46510a72010-04-15 01:51:59 +00001139 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +00001140
Dan Gohman3df24e62008-09-03 23:12:08 +00001141 if (BI->isUnconditional()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001142 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohmana4160c32010-07-07 16:29:44 +00001143 MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
Stuart Hastings3bf91252010-06-17 22:43:56 +00001144 FastEmitBranch(MSucc, BI->getDebugLoc());
Dan Gohman3df24e62008-09-03 23:12:08 +00001145 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +00001146 }
Dan Gohman3df24e62008-09-03 23:12:08 +00001147
1148 // Conditional branches are not handed yet.
1149 // Halt "fast" selection and bail.
1150 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001151 }
1152
Dan Gohman087c8502008-09-05 01:08:41 +00001153 case Instruction::Unreachable:
Stephen Hinesdce4a402014-05-29 02:49:00 -07001154 if (TM.Options.TrapUnreachable)
1155 return FastEmit_(MVT::Other, MVT::Other, ISD::TRAP) != 0;
1156 else
1157 return true;
Dan Gohman087c8502008-09-05 01:08:41 +00001158
Dan Gohman0586d912008-09-10 20:11:02 +00001159 case Instruction::Alloca:
1160 // FunctionLowering has the static-sized case covered.
Dan Gohmana4160c32010-07-07 16:29:44 +00001161 if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
Dan Gohman0586d912008-09-10 20:11:02 +00001162 return true;
1163
1164 // Dynamic-sized alloca is not handled yet.
1165 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001166
Dan Gohman33134c42008-09-25 17:05:24 +00001167 case Instruction::Call:
1168 return SelectCall(I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001169
Dan Gohman3df24e62008-09-03 23:12:08 +00001170 case Instruction::BitCast:
1171 return SelectBitCast(I);
1172
1173 case Instruction::FPToSI:
1174 return SelectCast(I, ISD::FP_TO_SINT);
1175 case Instruction::ZExt:
1176 return SelectCast(I, ISD::ZERO_EXTEND);
1177 case Instruction::SExt:
1178 return SelectCast(I, ISD::SIGN_EXTEND);
1179 case Instruction::Trunc:
1180 return SelectCast(I, ISD::TRUNCATE);
1181 case Instruction::SIToFP:
1182 return SelectCast(I, ISD::SINT_TO_FP);
1183
1184 case Instruction::IntToPtr: // Deliberate fall-through.
1185 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001186 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1187 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman3df24e62008-09-03 23:12:08 +00001188 if (DstVT.bitsGT(SrcVT))
1189 return SelectCast(I, ISD::ZERO_EXTEND);
1190 if (DstVT.bitsLT(SrcVT))
1191 return SelectCast(I, ISD::TRUNCATE);
1192 unsigned Reg = getRegForValue(I->getOperand(0));
1193 if (Reg == 0) return false;
1194 UpdateValueMap(I, Reg);
1195 return true;
1196 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001197
Eli Friedman2586b8f2011-05-16 20:27:46 +00001198 case Instruction::ExtractValue:
1199 return SelectExtractValue(I);
1200
Dan Gohmanba5be5c2010-04-20 15:00:41 +00001201 case Instruction::PHI:
1202 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
1203
Dan Gohman3df24e62008-09-03 23:12:08 +00001204 default:
1205 // Unhandled instruction. Halt "fast" selection and bail.
1206 return false;
1207 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001208}
1209
Bob Wilsond49edb72012-08-03 04:06:28 +00001210FastISel::FastISel(FunctionLoweringInfo &funcInfo,
1211 const TargetLibraryInfo *libInfo)
Dan Gohman84023e02010-07-10 09:00:22 +00001212 : FuncInfo(funcInfo),
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001213 MF(funcInfo.MF),
Dan Gohmana4160c32010-07-07 16:29:44 +00001214 MRI(FuncInfo.MF->getRegInfo()),
1215 MFI(*FuncInfo.MF->getFrameInfo()),
1216 MCP(*FuncInfo.MF->getConstantPool()),
1217 TM(FuncInfo.MF->getTarget()),
Stephen Hines36b56882014-04-23 16:57:46 -07001218 DL(*TM.getDataLayout()),
Dan Gohman22bb3112008-08-22 00:20:26 +00001219 TII(*TM.getInstrInfo()),
Dan Gohmana7a0ed72010-05-05 23:58:35 +00001220 TLI(*TM.getTargetLowering()),
Bob Wilsond49edb72012-08-03 04:06:28 +00001221 TRI(*TM.getRegisterInfo()),
1222 LibInfo(libInfo) {
Dan Gohmanbb466332008-08-20 21:05:57 +00001223}
1224
Dan Gohmane285a742008-08-14 21:51:29 +00001225FastISel::~FastISel() {}
1226
Evan Cheng092e5e72013-02-11 01:27:15 +00001227bool FastISel::FastLowerArguments() {
1228 return false;
1229}
1230
Owen Anderson825b72b2009-08-11 20:47:22 +00001231unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001232 unsigned) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001233 return 0;
1234}
1235
Owen Anderson825b72b2009-08-11 20:47:22 +00001236unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001237 unsigned,
1238 unsigned /*Op0*/, bool /*Op0IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001239 return 0;
1240}
1241
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001242unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001243 unsigned,
1244 unsigned /*Op0*/, bool /*Op0IsKill*/,
1245 unsigned /*Op1*/, bool /*Op1IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001246 return 0;
1247}
1248
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001249unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +00001250 return 0;
1251}
1252
Owen Anderson825b72b2009-08-11 20:47:22 +00001253unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohman46510a72010-04-15 01:51:59 +00001254 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001255 return 0;
1256}
1257
Owen Anderson825b72b2009-08-11 20:47:22 +00001258unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001259 unsigned,
1260 unsigned /*Op0*/, bool /*Op0IsKill*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +00001261 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001262 return 0;
1263}
1264
Owen Anderson825b72b2009-08-11 20:47:22 +00001265unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001266 unsigned,
1267 unsigned /*Op0*/, bool /*Op0IsKill*/,
Dan Gohman46510a72010-04-15 01:51:59 +00001268 const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001269 return 0;
1270}
1271
Owen Anderson825b72b2009-08-11 20:47:22 +00001272unsigned FastISel::FastEmit_rri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001273 unsigned,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001274 unsigned /*Op0*/, bool /*Op0IsKill*/,
1275 unsigned /*Op1*/, bool /*Op1IsKill*/,
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001276 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +00001277 return 0;
1278}
1279
1280/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
1281/// to emit an instruction with an immediate operand using FastEmit_ri.
1282/// If that fails, it materializes the immediate into a register and try
1283/// FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001284unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001285 unsigned Op0, bool Op0IsKill,
1286 uint64_t Imm, MVT ImmType) {
Chris Lattner602fc062011-04-17 20:23:29 +00001287 // If this is a multiply by a power of two, emit this as a shift left.
1288 if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) {
1289 Opcode = ISD::SHL;
1290 Imm = Log2_64(Imm);
Chris Lattner090ca912011-04-18 06:55:51 +00001291 } else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) {
1292 // div x, 8 -> srl x, 3
1293 Opcode = ISD::SRL;
1294 Imm = Log2_64(Imm);
Chris Lattner602fc062011-04-17 20:23:29 +00001295 }
Owen Andersond74ea772011-04-22 23:38:06 +00001296
Chris Lattner602fc062011-04-17 20:23:29 +00001297 // Horrible hack (to be removed), check to make sure shift amounts are
1298 // in-range.
1299 if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) &&
1300 Imm >= VT.getSizeInBits())
1301 return 0;
Owen Andersond74ea772011-04-22 23:38:06 +00001302
Evan Cheng83785c82008-08-20 22:45:34 +00001303 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001304 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +00001305 if (ResultReg != 0)
1306 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +00001307 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001308 if (MaterialReg == 0) {
1309 // This is a bit ugly/slow, but failing here means falling out of
1310 // fast-isel, which would be very slow.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001311 IntegerType *ITy = IntegerType::get(FuncInfo.Fn->getContext(),
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001312 VT.getSizeInBits());
1313 MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm));
Chad Rosier7ae3bb82013-03-28 23:04:47 +00001314 assert (MaterialReg != 0 && "Unable to materialize imm.");
1315 if (MaterialReg == 0) return 0;
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001316 }
Dan Gohmana6cb6412010-05-11 23:54:07 +00001317 return FastEmit_rr(VT, VT, Opcode,
1318 Op0, Op0IsKill,
1319 MaterialReg, /*Kill=*/true);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001320}
1321
1322unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
1323 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +00001324}
1325
Stephen Hinesdce4a402014-05-29 02:49:00 -07001326unsigned FastISel::constrainOperandRegClass(const MCInstrDesc &II,
1327 unsigned Op, unsigned OpNum) {
1328 if (TargetRegisterInfo::isVirtualRegister(Op)) {
1329 const TargetRegisterClass *RegClass =
1330 TII.getRegClass(II, OpNum, &TRI, *FuncInfo.MF);
1331 if (!MRI.constrainRegClass(Op, RegClass)) {
1332 // If it's not legal to COPY between the register classes, something
1333 // has gone very wrong before we got here.
1334 unsigned NewOp = createResultReg(RegClass);
1335 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1336 TII.get(TargetOpcode::COPY), NewOp).addReg(Op);
1337 return NewOp;
1338 }
1339 }
1340 return Op;
1341}
1342
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001343unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +00001344 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001345 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001346 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001347
Stephen Hines36b56882014-04-23 16:57:46 -07001348 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001349 return ResultReg;
1350}
1351
1352unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
1353 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001354 unsigned Op0, bool Op0IsKill) {
Evan Chenge837dea2011-06-28 19:10:37 +00001355 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001356
Stephen Hinesdce4a402014-05-29 02:49:00 -07001357 unsigned ResultReg = createResultReg(RC);
1358 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1359
Evan Cheng5960e4e2008-09-08 08:38:20 +00001360 if (II.getNumDefs() >= 1)
Stephen Hines36b56882014-04-23 16:57:46 -07001361 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Dan Gohman84023e02010-07-10 09:00:22 +00001362 .addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001363 else {
Stephen Hines36b56882014-04-23 16:57:46 -07001364 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohman84023e02010-07-10 09:00:22 +00001365 .addReg(Op0, Op0IsKill * RegState::Kill);
Stephen Hines36b56882014-04-23 16:57:46 -07001366 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1367 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001368 }
1369
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001370 return ResultReg;
1371}
1372
1373unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
1374 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001375 unsigned Op0, bool Op0IsKill,
1376 unsigned Op1, bool Op1IsKill) {
Evan Chenge837dea2011-06-28 19:10:37 +00001377 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001378
Stephen Hinesdce4a402014-05-29 02:49:00 -07001379 unsigned ResultReg = createResultReg(RC);
1380 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1381 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1382
Evan Cheng5960e4e2008-09-08 08:38:20 +00001383 if (II.getNumDefs() >= 1)
Stephen Hines36b56882014-04-23 16:57:46 -07001384 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001385 .addReg(Op0, Op0IsKill * RegState::Kill)
1386 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001387 else {
Stephen Hines36b56882014-04-23 16:57:46 -07001388 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001389 .addReg(Op0, Op0IsKill * RegState::Kill)
1390 .addReg(Op1, Op1IsKill * RegState::Kill);
Stephen Hines36b56882014-04-23 16:57:46 -07001391 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1392 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001393 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001394 return ResultReg;
1395}
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001396
Owen Andersond71867a2011-05-05 17:59:04 +00001397unsigned FastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
1398 const TargetRegisterClass *RC,
1399 unsigned Op0, bool Op0IsKill,
1400 unsigned Op1, bool Op1IsKill,
1401 unsigned Op2, bool Op2IsKill) {
Evan Chenge837dea2011-06-28 19:10:37 +00001402 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Andersond71867a2011-05-05 17:59:04 +00001403
Stephen Hinesdce4a402014-05-29 02:49:00 -07001404 unsigned ResultReg = createResultReg(RC);
1405 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1406 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1407 Op2 = constrainOperandRegClass(II, Op2, II.getNumDefs() + 2);
1408
Owen Andersond71867a2011-05-05 17:59:04 +00001409 if (II.getNumDefs() >= 1)
Stephen Hines36b56882014-04-23 16:57:46 -07001410 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Owen Andersond71867a2011-05-05 17:59:04 +00001411 .addReg(Op0, Op0IsKill * RegState::Kill)
1412 .addReg(Op1, Op1IsKill * RegState::Kill)
1413 .addReg(Op2, Op2IsKill * RegState::Kill);
1414 else {
Stephen Hines36b56882014-04-23 16:57:46 -07001415 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Owen Andersond71867a2011-05-05 17:59:04 +00001416 .addReg(Op0, Op0IsKill * RegState::Kill)
1417 .addReg(Op1, Op1IsKill * RegState::Kill)
1418 .addReg(Op2, Op2IsKill * RegState::Kill);
Stephen Hines36b56882014-04-23 16:57:46 -07001419 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1420 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Owen Andersond71867a2011-05-05 17:59:04 +00001421 }
1422 return ResultReg;
1423}
1424
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001425unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1426 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001427 unsigned Op0, bool Op0IsKill,
1428 uint64_t Imm) {
Evan Chenge837dea2011-06-28 19:10:37 +00001429 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001430
Stephen Hinesdce4a402014-05-29 02:49:00 -07001431 unsigned ResultReg = createResultReg(RC);
1432 RC = TII.getRegClass(II, II.getNumDefs(), &TRI, *FuncInfo.MF);
1433 MRI.constrainRegClass(Op0, RC);
1434
Evan Cheng5960e4e2008-09-08 08:38:20 +00001435 if (II.getNumDefs() >= 1)
Stephen Hines36b56882014-04-23 16:57:46 -07001436 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001437 .addReg(Op0, Op0IsKill * RegState::Kill)
1438 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001439 else {
Stephen Hines36b56882014-04-23 16:57:46 -07001440 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001441 .addReg(Op0, Op0IsKill * RegState::Kill)
1442 .addImm(Imm);
Stephen Hines36b56882014-04-23 16:57:46 -07001443 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1444 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001445 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001446 return ResultReg;
1447}
1448
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001449unsigned FastISel::FastEmitInst_rii(unsigned MachineInstOpcode,
1450 const TargetRegisterClass *RC,
1451 unsigned Op0, bool Op0IsKill,
1452 uint64_t Imm1, uint64_t Imm2) {
Evan Chenge837dea2011-06-28 19:10:37 +00001453 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001454
Stephen Hinesdce4a402014-05-29 02:49:00 -07001455 unsigned ResultReg = createResultReg(RC);
1456 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1457
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001458 if (II.getNumDefs() >= 1)
Stephen Hines36b56882014-04-23 16:57:46 -07001459 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001460 .addReg(Op0, Op0IsKill * RegState::Kill)
1461 .addImm(Imm1)
1462 .addImm(Imm2);
1463 else {
Stephen Hines36b56882014-04-23 16:57:46 -07001464 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001465 .addReg(Op0, Op0IsKill * RegState::Kill)
1466 .addImm(Imm1)
1467 .addImm(Imm2);
Stephen Hines36b56882014-04-23 16:57:46 -07001468 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1469 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001470 }
1471 return ResultReg;
1472}
1473
Dan Gohman10df0fa2008-08-27 01:09:54 +00001474unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
1475 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001476 unsigned Op0, bool Op0IsKill,
1477 const ConstantFP *FPImm) {
Evan Chenge837dea2011-06-28 19:10:37 +00001478 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohman10df0fa2008-08-27 01:09:54 +00001479
Stephen Hinesdce4a402014-05-29 02:49:00 -07001480 unsigned ResultReg = createResultReg(RC);
1481 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1482
Evan Cheng5960e4e2008-09-08 08:38:20 +00001483 if (II.getNumDefs() >= 1)
Stephen Hines36b56882014-04-23 16:57:46 -07001484 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001485 .addReg(Op0, Op0IsKill * RegState::Kill)
1486 .addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001487 else {
Stephen Hines36b56882014-04-23 16:57:46 -07001488 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001489 .addReg(Op0, Op0IsKill * RegState::Kill)
1490 .addFPImm(FPImm);
Stephen Hines36b56882014-04-23 16:57:46 -07001491 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1492 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001493 }
Dan Gohman10df0fa2008-08-27 01:09:54 +00001494 return ResultReg;
1495}
1496
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001497unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
1498 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001499 unsigned Op0, bool Op0IsKill,
1500 unsigned Op1, bool Op1IsKill,
1501 uint64_t Imm) {
Evan Chenge837dea2011-06-28 19:10:37 +00001502 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001503
Stephen Hinesdce4a402014-05-29 02:49:00 -07001504 unsigned ResultReg = createResultReg(RC);
1505 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1506 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1507
Evan Cheng5960e4e2008-09-08 08:38:20 +00001508 if (II.getNumDefs() >= 1)
Stephen Hines36b56882014-04-23 16:57:46 -07001509 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001510 .addReg(Op0, Op0IsKill * RegState::Kill)
1511 .addReg(Op1, Op1IsKill * RegState::Kill)
1512 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001513 else {
Stephen Hines36b56882014-04-23 16:57:46 -07001514 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001515 .addReg(Op0, Op0IsKill * RegState::Kill)
1516 .addReg(Op1, Op1IsKill * RegState::Kill)
1517 .addImm(Imm);
Stephen Hines36b56882014-04-23 16:57:46 -07001518 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1519 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001520 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001521 return ResultReg;
1522}
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001523
Manman Ren68f25572012-06-01 19:33:18 +00001524unsigned FastISel::FastEmitInst_rrii(unsigned MachineInstOpcode,
1525 const TargetRegisterClass *RC,
1526 unsigned Op0, bool Op0IsKill,
1527 unsigned Op1, bool Op1IsKill,
1528 uint64_t Imm1, uint64_t Imm2) {
Manman Ren68f25572012-06-01 19:33:18 +00001529 const MCInstrDesc &II = TII.get(MachineInstOpcode);
1530
Stephen Hinesdce4a402014-05-29 02:49:00 -07001531 unsigned ResultReg = createResultReg(RC);
1532 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1533 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1534
Manman Ren68f25572012-06-01 19:33:18 +00001535 if (II.getNumDefs() >= 1)
Stephen Hines36b56882014-04-23 16:57:46 -07001536 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Manman Ren68f25572012-06-01 19:33:18 +00001537 .addReg(Op0, Op0IsKill * RegState::Kill)
1538 .addReg(Op1, Op1IsKill * RegState::Kill)
1539 .addImm(Imm1).addImm(Imm2);
1540 else {
Stephen Hines36b56882014-04-23 16:57:46 -07001541 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Manman Ren68f25572012-06-01 19:33:18 +00001542 .addReg(Op0, Op0IsKill * RegState::Kill)
1543 .addReg(Op1, Op1IsKill * RegState::Kill)
1544 .addImm(Imm1).addImm(Imm2);
Stephen Hines36b56882014-04-23 16:57:46 -07001545 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1546 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Manman Ren68f25572012-06-01 19:33:18 +00001547 }
1548 return ResultReg;
1549}
1550
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001551unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1552 const TargetRegisterClass *RC,
1553 uint64_t Imm) {
1554 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001555 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001556
Evan Cheng5960e4e2008-09-08 08:38:20 +00001557 if (II.getNumDefs() >= 1)
Stephen Hines36b56882014-04-23 16:57:46 -07001558 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001559 else {
Stephen Hines36b56882014-04-23 16:57:46 -07001560 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addImm(Imm);
1561 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1562 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001563 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001564 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +00001565}
Owen Anderson8970f002008-08-27 22:30:02 +00001566
Owen Andersond74ea772011-04-22 23:38:06 +00001567unsigned FastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
1568 const TargetRegisterClass *RC,
1569 uint64_t Imm1, uint64_t Imm2) {
1570 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001571 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Andersond74ea772011-04-22 23:38:06 +00001572
1573 if (II.getNumDefs() >= 1)
Stephen Hines36b56882014-04-23 16:57:46 -07001574 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Owen Andersond74ea772011-04-22 23:38:06 +00001575 .addImm(Imm1).addImm(Imm2);
1576 else {
Stephen Hines36b56882014-04-23 16:57:46 -07001577 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addImm(Imm1).addImm(Imm2);
1578 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1579 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Owen Andersond74ea772011-04-22 23:38:06 +00001580 }
1581 return ResultReg;
1582}
1583
Owen Anderson825b72b2009-08-11 20:47:22 +00001584unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001585 unsigned Op0, bool Op0IsKill,
1586 uint32_t Idx) {
Evan Cheng536ab132009-01-22 09:10:11 +00001587 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001588 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
1589 "Cannot yet extract from physregs");
Jakob Stoklund Olesenee0d5d42012-05-20 06:38:37 +00001590 const TargetRegisterClass *RC = MRI.getRegClass(Op0);
1591 MRI.constrainRegClass(Op0, TRI.getSubClassWithSubReg(RC, Idx));
Dan Gohman84023e02010-07-10 09:00:22 +00001592 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Stephen Hines36b56882014-04-23 16:57:46 -07001593 DbgLoc, TII.get(TargetOpcode::COPY), ResultReg)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001594 .addReg(Op0, getKillRegState(Op0IsKill), Idx);
Owen Anderson8970f002008-08-27 22:30:02 +00001595 return ResultReg;
1596}
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001597
1598/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1599/// with all but the least significant bit set to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001600unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1601 return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001602}
Dan Gohmanf81eca02010-04-22 20:46:50 +00001603
1604/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1605/// Emit code to ensure constants are copied into registers when needed.
1606/// Remember the virtual registers that need to be added to the Machine PHI
1607/// nodes as input. We cannot just directly add them, because expansion
1608/// might result in multiple MBB's for one BB. As such, the start of the
1609/// BB might correspond to a different MBB than the end.
1610bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1611 const TerminatorInst *TI = LLVMBB->getTerminator();
1612
1613 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohmana4160c32010-07-07 16:29:44 +00001614 unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001615
1616 // Check successor nodes' PHI nodes that expect a constant to be available
1617 // from this block.
1618 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1619 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1620 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmana4160c32010-07-07 16:29:44 +00001621 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Dan Gohmanf81eca02010-04-22 20:46:50 +00001622
1623 // If this terminator has multiple identical successors (common for
1624 // switches), only handle each succ once.
1625 if (!SuccsHandled.insert(SuccMBB)) continue;
1626
1627 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1628
1629 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1630 // nodes and Machine PHI nodes, but the incoming operands have not been
1631 // emitted yet.
1632 for (BasicBlock::const_iterator I = SuccBB->begin();
1633 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanfb95f892010-05-07 01:10:20 +00001634
Dan Gohmanf81eca02010-04-22 20:46:50 +00001635 // Ignore dead phi's.
1636 if (PN->use_empty()) continue;
1637
1638 // Only handle legal types. Two interesting things to note here. First,
1639 // by bailing out early, we may leave behind some dead instructions,
1640 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001641 // own moves. Second, this check is necessary because FastISel doesn't
Dan Gohman89496d02010-07-02 00:10:16 +00001642 // use CreateRegs to create registers, so it always creates
Dan Gohmanf81eca02010-04-22 20:46:50 +00001643 // exactly one register for each non-void instruction.
1644 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1645 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
Chad Rosier2f2d1d72012-02-04 00:39:19 +00001646 // Handle integer promotions, though, because they're common and easy.
1647 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Dan Gohmanf81eca02010-04-22 20:46:50 +00001648 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1649 else {
Dan Gohmana4160c32010-07-07 16:29:44 +00001650 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001651 return false;
1652 }
1653 }
1654
1655 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1656
Dan Gohmanfb95f892010-05-07 01:10:20 +00001657 // Set the DebugLoc for the copy. Prefer the location of the operand
1658 // if there is one; use the location of the PHI otherwise.
Stephen Hines36b56882014-04-23 16:57:46 -07001659 DbgLoc = PN->getDebugLoc();
Dan Gohmanfb95f892010-05-07 01:10:20 +00001660 if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
Stephen Hines36b56882014-04-23 16:57:46 -07001661 DbgLoc = Inst->getDebugLoc();
Dan Gohmanfb95f892010-05-07 01:10:20 +00001662
Dan Gohmanf81eca02010-04-22 20:46:50 +00001663 unsigned Reg = getRegForValue(PHIOp);
1664 if (Reg == 0) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001665 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001666 return false;
1667 }
Dan Gohmana4160c32010-07-07 16:29:44 +00001668 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Stephen Hines36b56882014-04-23 16:57:46 -07001669 DbgLoc = DebugLoc();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001670 }
1671 }
1672
1673 return true;
1674}
Eli Bendersky75299e32013-04-19 22:29:18 +00001675
1676bool FastISel::tryToFoldLoad(const LoadInst *LI, const Instruction *FoldInst) {
Eli Bendersky462123f2013-04-19 23:26:18 +00001677 assert(LI->hasOneUse() &&
1678 "tryToFoldLoad expected a LoadInst with a single use");
Eli Bendersky75299e32013-04-19 22:29:18 +00001679 // We know that the load has a single use, but don't know what it is. If it
1680 // isn't one of the folded instructions, then we can't succeed here. Handle
1681 // this by scanning the single-use users of the load until we get to FoldInst.
1682 unsigned MaxUsers = 6; // Don't scan down huge single-use chains of instrs.
1683
Stephen Hines36b56882014-04-23 16:57:46 -07001684 const Instruction *TheUser = LI->user_back();
Eli Bendersky75299e32013-04-19 22:29:18 +00001685 while (TheUser != FoldInst && // Scan up until we find FoldInst.
1686 // Stay in the right block.
1687 TheUser->getParent() == FoldInst->getParent() &&
1688 --MaxUsers) { // Don't scan too far.
1689 // If there are multiple or no uses of this instruction, then bail out.
1690 if (!TheUser->hasOneUse())
1691 return false;
1692
Stephen Hines36b56882014-04-23 16:57:46 -07001693 TheUser = TheUser->user_back();
Eli Bendersky75299e32013-04-19 22:29:18 +00001694 }
1695
1696 // If we didn't find the fold instruction, then we failed to collapse the
1697 // sequence.
1698 if (TheUser != FoldInst)
1699 return false;
1700
1701 // Don't try to fold volatile loads. Target has to deal with alignment
1702 // constraints.
Eli Bendersky462123f2013-04-19 23:26:18 +00001703 if (LI->isVolatile())
1704 return false;
Eli Bendersky75299e32013-04-19 22:29:18 +00001705
1706 // Figure out which vreg this is going into. If there is no assigned vreg yet
1707 // then there actually was no reference to it. Perhaps the load is referenced
1708 // by a dead instruction.
1709 unsigned LoadReg = getRegForValue(LI);
1710 if (LoadReg == 0)
1711 return false;
1712
Eli Bendersky462123f2013-04-19 23:26:18 +00001713 // We can't fold if this vreg has no uses or more than one use. Multiple uses
1714 // may mean that the instruction got lowered to multiple MIs, or the use of
1715 // the loaded value ended up being multiple operands of the result.
1716 if (!MRI.hasOneUse(LoadReg))
1717 return false;
1718
Eli Bendersky75299e32013-04-19 22:29:18 +00001719 MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LoadReg);
Stephen Hines36b56882014-04-23 16:57:46 -07001720 MachineInstr *User = RI->getParent();
Eli Bendersky75299e32013-04-19 22:29:18 +00001721
1722 // Set the insertion point properly. Folding the load can cause generation of
Eli Bendersky462123f2013-04-19 23:26:18 +00001723 // other random instructions (like sign extends) for addressing modes; make
Eli Bendersky75299e32013-04-19 22:29:18 +00001724 // sure they get inserted in a logical place before the new instruction.
1725 FuncInfo.InsertPt = User;
1726 FuncInfo.MBB = User->getParent();
1727
1728 // Ask the target to try folding the load.
1729 return tryToFoldLoadIntoMI(User, RI.getOperandNo(), LI);
1730}
1731
Bob Wilsoncc705232013-11-15 19:09:27 +00001732bool FastISel::canFoldAddIntoGEP(const User *GEP, const Value *Add) {
1733 // Must be an add.
1734 if (!isa<AddOperator>(Add))
1735 return false;
1736 // Type size needs to match.
Stephen Hines36b56882014-04-23 16:57:46 -07001737 if (DL.getTypeSizeInBits(GEP->getType()) !=
1738 DL.getTypeSizeInBits(Add->getType()))
Bob Wilsoncc705232013-11-15 19:09:27 +00001739 return false;
1740 // Must be in the same basic block.
1741 if (isa<Instruction>(Add) &&
1742 FuncInfo.MBBMap[cast<Instruction>(Add)->getParent()] != FuncInfo.MBB)
1743 return false;
1744 // Must have a constant operand.
1745 return isa<ConstantInt>(cast<AddOperator>(Add)->getOperand(1));
1746}
Eli Bendersky75299e32013-04-19 22:29:18 +00001747
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001748MachineMemOperand *
1749FastISel::createMachineMemOperandFor(const Instruction *I) const {
1750 const Value *Ptr;
1751 Type *ValTy;
1752 unsigned Alignment;
1753 unsigned Flags;
1754 bool IsVolatile;
1755
1756 if (const auto *LI = dyn_cast<LoadInst>(I)) {
1757 Alignment = LI->getAlignment();
1758 IsVolatile = LI->isVolatile();
1759 Flags = MachineMemOperand::MOLoad;
1760 Ptr = LI->getPointerOperand();
1761 ValTy = LI->getType();
1762 } else if (const auto *SI = dyn_cast<StoreInst>(I)) {
1763 Alignment = SI->getAlignment();
1764 IsVolatile = SI->isVolatile();
1765 Flags = MachineMemOperand::MOStore;
1766 Ptr = SI->getPointerOperand();
1767 ValTy = SI->getValueOperand()->getType();
1768 } else {
1769 return nullptr;
1770 }
1771
1772 bool IsNonTemporal = I->getMetadata("nontemporal") != nullptr;
1773 bool IsInvariant = I->getMetadata("invariant.load") != nullptr;
1774 const MDNode *TBAAInfo = I->getMetadata(LLVMContext::MD_tbaa);
1775 const MDNode *Ranges = I->getMetadata(LLVMContext::MD_range);
1776
1777 if (Alignment == 0) // Ensure that codegen never sees alignment 0.
1778 Alignment = DL.getABITypeAlignment(ValTy);
1779
1780 unsigned Size = TM.getDataLayout()->getTypeStoreSize(ValTy);
1781
1782 if (IsVolatile)
1783 Flags |= MachineMemOperand::MOVolatile;
1784 if (IsNonTemporal)
1785 Flags |= MachineMemOperand::MONonTemporal;
1786 if (IsInvariant)
1787 Flags |= MachineMemOperand::MOInvariant;
1788
1789 return FuncInfo.MF->getMachineMemOperand(MachinePointerInfo(Ptr), Flags, Size,
1790 Alignment, TBAAInfo, Ranges);
1791}