blob: f7da4d546d87860c09d3d3171101a3ac197f710b [file] [log] [blame]
Dan Gohmane149e982010-04-22 20:06:42 +00001//===-- FastISel.cpp - Implementation of the FastISel class ---------------===//
Dan Gohmanb2226e22008-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 Gohmanb4863502008-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 Lattnerc52af452008-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 Gohmanb4863502008-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 Lattnerc52af452008-10-13 01:59:13 +000022// support. In many cases, this allows us to avoid duplicating a lot of
Dan Gohmanb4863502008-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 Lattnerc52af452008-10-13 01:59:13 +000027// weighed against the speed at which the code can be generated. Also,
Dan Gohmanb4863502008-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 Lattnerc52af452008-10-13 01:59:13 +000030// time. Despite its limitations, "fast" instruction selection is able to
Dan Gohmanb4863502008-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 Lattnerc52af452008-10-13 01:59:13 +000037// from simple operators. More complicated operations currently require
Dan Gohmanb4863502008-09-30 20:48:29 +000038// target-specific code.
39//
Dan Gohmanb2226e22008-08-13 20:19:35 +000040//===----------------------------------------------------------------------===//
41
Chandler Carruthed0881b2012-12-03 16:50:05 +000042#include "llvm/CodeGen/FastISel.h"
David Blaikie0252265b2013-06-16 20:34:15 +000043#include "llvm/ADT/Optional.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000044#include "llvm/ADT/Statistic.h"
Juergen Ributzka454d3742014-06-13 00:45:11 +000045#include "llvm/Analysis/BranchProbabilityInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000046#include "llvm/Analysis/Loads.h"
47#include "llvm/CodeGen/Analysis.h"
48#include "llvm/CodeGen/FunctionLoweringInfo.h"
Juergen Ributzka04558dc2014-06-12 03:29:26 +000049#include "llvm/CodeGen/MachineFrameInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000050#include "llvm/CodeGen/MachineInstrBuilder.h"
51#include "llvm/CodeGen/MachineModuleInfo.h"
52#include "llvm/CodeGen/MachineRegisterInfo.h"
Juergen Ributzka04558dc2014-06-12 03:29:26 +000053#include "llvm/CodeGen/StackMaps.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000054#include "llvm/IR/DataLayout.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000055#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-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 Carruthed0881b2012-12-03 16:50:05 +000061#include "llvm/Support/Debug.h"
62#include "llvm/Support/ErrorHandling.h"
Dan Gohmanb2226e22008-08-13 20:19:35 +000063#include "llvm/Target/TargetInstrInfo.h"
Bob Wilson3e6fa462012-08-03 04:06:28 +000064#include "llvm/Target/TargetLibraryInfo.h"
Evan Cheng864fcc12008-08-20 22:45:34 +000065#include "llvm/Target/TargetLowering.h"
Dan Gohman02c84b82008-08-20 21:05:57 +000066#include "llvm/Target/TargetMachine.h"
Dan Gohmanb2226e22008-08-13 20:19:35 +000067using namespace llvm;
68
Chandler Carruth1b9dde02014-04-22 02:02:50 +000069#define DEBUG_TYPE "isel"
70
Chad Rosier61e8d102011-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 Rosier46addb92011-11-29 19:40:47 +000075STATISTIC(NumFastIselDead, "Number of dead insts removed on failure");
Chad Rosierff40b1e2011-11-16 21:05:28 +000076
Dan Gohmand7b5ce32010-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 Olesen6a7d6832013-07-04 04:53:49 +000083 // Instructions are appended to FuncInfo.MBB. If the basic block already
Jakob Stoklund Olesen3d8560c2013-07-04 04:32:39 +000084 // contains labels or copies, use the last instruction as the last local
85 // value.
Craig Topperc0196b12014-04-14 00:51:57 +000086 EmitStartPt = nullptr;
Jakob Stoklund Olesen3d8560c2013-07-04 04:32:39 +000087 if (!FuncInfo.MBB->empty())
88 EmitStartPt = &FuncInfo.MBB->back();
Ivan Krasind7cbd4c2011-08-18 22:06:10 +000089 LastLocalValue = EmitStartPt;
90}
91
Evan Cheng615620c2013-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 Lincfe7f352013-07-08 00:37:03 +000097
Evan Cheng615620c2013-02-11 01:27:15 +000098 if (!FastLowerArguments())
99 return false;
100
David Blaikie97c6c5b2013-06-21 22:56:30 +0000101 // Enter arguments into ValueMap for uses in non-entry BBs.
Evan Cheng615620c2013-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 Blaikie97c6c5b2013-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 Cheng615620c2013-02-11 01:27:15 +0000107 }
108 return true;
109}
110
Ivan Krasind7cbd4c2011-08-18 22:06:10 +0000111void FastISel::flushLocalValueMap() {
112 LocalValueMap.clear();
113 LastLocalValue = EmitStartPt;
114 recomputeInsertPt();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000115}
116
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000117bool FastISel::hasTrivialKill(const Value *V) const {
Dan Gohman88fb2532010-05-14 22:53:18 +0000118 // Don't consider constants or arguments to have trivial kills.
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000119 const Instruction *I = dyn_cast<Instruction>(V);
Dan Gohman88fb2532010-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))
Rafael Espindolaea09c592014-02-18 22:05:46 +0000125 if (Cast->isNoopCast(DL.getIntPtrType(Cast->getContext())) &&
Chandler Carruth7ec50852012-11-01 08:07:29 +0000126 !hasTrivialKill(Cast->getOperand(0)))
Dan Gohman88fb2532010-05-14 22:53:18 +0000127 return false;
128
Chad Rosier291ce472011-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 Gohman88fb2532010-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) &&
Chandler Carruthcdf47882014-03-09 03:16:01 +0000140 cast<Instruction>(*I->user_begin())->getParent() == I->getParent();
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000141}
142
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000143unsigned FastISel::getRegForValue(const Value *V) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000144 EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
Dan Gohmanca93aab2009-04-07 20:40:11 +0000145 // Don't handle non-simple values in FastISel.
146 if (!RealVT.isSimple())
147 return 0;
Dan Gohman4c315242008-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 Anderson9f944592009-08-11 20:47:22 +0000152 MVT VT = RealVT.getSimpleVT();
Dan Gohman4c315242008-12-08 07:57:47 +0000153 if (!TLI.isTypeLegal(VT)) {
Eli Friedmanc7035512011-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 Anderson117c9e82009-08-12 00:36:31 +0000156 VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
Dan Gohman4c315242008-12-08 07:57:47 +0000157 else
158 return 0;
159 }
160
Eric Christopher1a06cc92012-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 Gohmane039d552008-09-03 23:32:19 +0000163 if (Reg != 0)
164 return Reg;
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000165
Dan Gohmana7c717d82010-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 Gohmand7b5ce32010-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 Gohmana7c717d82010-05-06 00:02:14 +0000172
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000173 SavePoint SaveInsertPt = enterLocalValueArea();
Dan Gohmand7b5ce32010-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 Christopherf4fba5c2012-10-03 08:10:01 +0000179 leaveLocalValueArea(SaveInsertPt);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000180
181 return Reg;
Dan Gohman626b5d82010-05-03 23:36:34 +0000182}
183
Eric Christopher541f8012010-08-17 01:30:33 +0000184/// materializeRegForValue - Helper for getRegForValue. This function is
Dan Gohman626b5d82010-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 Gohmanbcaf6812010-04-15 01:51:59 +0000190 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman9801ba42008-09-19 22:16:54 +0000191 if (CI->getValue().getActiveBits() <= 64)
192 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman39d82f92008-09-10 20:11:02 +0000193 } else if (isa<AllocaInst>(V)) {
Dan Gohman9801ba42008-09-19 22:16:54 +0000194 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohmanc45733f2008-08-28 21:19:07 +0000195 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohmanc1d47c52008-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 Anderson55f1c092009-08-13 21:58:54 +0000198 Reg =
Rafael Espindolaea09c592014-02-18 22:05:46 +0000199 getRegForValue(Constant::getNullValue(DL.getIntPtrType(V->getContext())));
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000200 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Eli Friedman33c13392011-04-28 00:42:03 +0000201 if (CF->isNullValue()) {
Eli Friedman406c4712011-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 Gohmanb0b5a272008-08-27 18:10:19 +0000207
208 if (!Reg) {
Dan Gohman8a2dae52010-04-13 17:07:06 +0000209 // Try to emit the constant by using an integer constant with a cast.
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000210 const APFloat &Flt = CF->getValueAPF();
Owen Anderson53aa7a92009-08-10 22:56:29 +0000211 EVT IntVT = TLI.getPointerTy();
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000212
213 uint64_t x[2];
214 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000215 bool isExact;
216 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
Eric Christopher997aaa92012-03-20 01:07:56 +0000217 APFloat::rmTowardZero, &isExact);
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000218 if (isExact) {
Jeffrey Yasskin7a162882011-07-18 21:45:40 +0000219 APInt IntVal(IntBitWidth, x);
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000220
Owen Anderson47db9412009-07-22 00:24:57 +0000221 unsigned IntegerReg =
Owen Andersonedb4a702009-07-24 23:12:02 +0000222 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
Dan Gohman9801ba42008-09-19 22:16:54 +0000223 if (IntegerReg != 0)
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000224 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
225 IntegerReg, /*Kill=*/false);
Dan Gohman9801ba42008-09-19 22:16:54 +0000226 }
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000227 }
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000228 } else if (const Operator *Op = dyn_cast<Operator>(V)) {
Dan Gohman722f5fc2010-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 Gohman7c58cf72010-06-21 14:17:46 +0000233 Reg = lookUpRegForValue(Op);
Dan Gohmanc45733f2008-08-28 21:19:07 +0000234 } else if (isa<UndefValue>(V)) {
Dan Gohmane039d552008-09-03 23:32:19 +0000235 Reg = createResultReg(TLI.getRegClassFor(VT));
Rafael Espindolaea09c592014-02-18 22:05:46 +0000236 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000237 TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000238 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000239
Dan Gohman3663f152008-09-25 01:28:51 +0000240 // If target-independent code couldn't handle the value, give target-specific
241 // code a try.
Owen Anderson1dd2e402008-09-05 23:36:01 +0000242 if (!Reg && isa<Constant>(V))
Dan Gohman9801ba42008-09-19 22:16:54 +0000243 Reg = TargetMaterializeConstant(cast<Constant>(V));
Wesley Peck527da1b2010-11-23 03:31:01 +0000244
Dan Gohman9801ba42008-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 Gohmand7b5ce32010-07-10 09:00:22 +0000247 if (Reg != 0) {
Dan Gohman3663f152008-09-25 01:28:51 +0000248 LocalValueMap[V] = Reg;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000249 LastLocalValue = MRI.getVRegDef(Reg);
250 }
Dan Gohmane039d552008-09-03 23:32:19 +0000251 return Reg;
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000252}
253
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000254unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng1e979012008-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 Gohman626b5d82010-05-03 23:36:34 +0000258 // def-dominates-use requirement enforced.
Dan Gohman87fb4e82010-07-07 16:29:44 +0000259 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
260 if (I != FuncInfo.ValueMap.end())
Dan Gohmanf91aff52010-06-21 14:21:47 +0000261 return I->second;
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000262 return LocalValueMap[V];
Evan Cheng1e979012008-09-09 01:26:59 +0000263}
264
Owen Anderson6f0c51d2008-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 Friedmana4d4a012011-05-16 21:06:17 +0000271void FastISel::UpdateValueMap(const Value *I, unsigned Reg, unsigned NumRegs) {
Dan Gohmanfcf54562008-09-05 18:18:20 +0000272 if (!isa<Instruction>(I)) {
273 LocalValueMap[I] = Reg;
Eli Friedmana4d4a012011-05-16 21:06:17 +0000274 return;
Dan Gohmanfcf54562008-09-05 18:18:20 +0000275 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000276
Dan Gohman87fb4e82010-07-07 16:29:44 +0000277 unsigned &AssignedReg = FuncInfo.ValueMap[I];
Chris Lattnerada5d6c2009-04-12 07:45:01 +0000278 if (AssignedReg == 0)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000279 // Use the new register.
Chris Lattnerada5d6c2009-04-12 07:45:01 +0000280 AssignedReg = Reg;
Chris Lattnera101f6f2009-04-12 07:46:30 +0000281 else if (Reg != AssignedReg) {
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000282 // Arrange for uses of AssignedReg to be replaced by uses of Reg.
Eli Friedmana4d4a012011-05-16 21:06:17 +0000283 for (unsigned i = 0; i < NumRegs; i++)
284 FuncInfo.RegFixups[AssignedReg+i] = Reg+i;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000285
286 AssignedReg = Reg;
Chris Lattnerada5d6c2009-04-12 07:45:01 +0000287 }
Owen Anderson6f0c51d2008-08-30 00:38:46 +0000288}
289
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000290std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohman4c315242008-12-08 07:57:47 +0000291 unsigned IdxN = getRegForValue(Idx);
292 if (IdxN == 0)
293 // Unhandled operand. Halt "fast" selection and bail.
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000294 return std::pair<unsigned, bool>(0, false);
295
296 bool IdxNIsKill = hasTrivialKill(Idx);
Dan Gohman4c315242008-12-08 07:57:47 +0000297
298 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Andersonc6daf8f2009-08-11 21:59:30 +0000299 MVT PtrVT = TLI.getPointerTy();
Owen Anderson53aa7a92009-08-10 22:56:29 +0000300 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohman1a1b51f2010-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 Gohman4c315242008-12-08 07:57:47 +0000312}
313
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000314void FastISel::recomputeInsertPt() {
315 if (getLastLocalValue()) {
316 FuncInfo.InsertPt = getLastLocalValue();
Dan Gohmanb5e918d2010-07-19 22:48:56 +0000317 FuncInfo.MBB = FuncInfo.InsertPt->getParent();
Dan Gohmand7b5ce32010-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 Rosier46addb92011-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 Voung7857a642013-03-08 22:56:31 +0000335 ++NumFastIselDead;
Chad Rosier46addb92011-11-29 19:40:47 +0000336 }
337 recomputeInsertPt();
338}
339
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000340FastISel::SavePoint FastISel::enterLocalValueArea() {
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000341 MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000342 DebugLoc OldDL = DbgLoc;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000343 recomputeInsertPt();
Rafael Espindolaea09c592014-02-18 22:05:46 +0000344 DbgLoc = DebugLoc();
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000345 SavePoint SP = { OldInsertPt, OldDL };
346 return SP;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000347}
348
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000349void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) {
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000350 if (FuncInfo.InsertPt != FuncInfo.MBB->begin())
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000351 LastLocalValue = std::prev(FuncInfo.InsertPt);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000352
353 // Restore the previous insert position.
Eric Christopherf4fba5c2012-10-03 08:10:01 +0000354 FuncInfo.InsertPt = OldInsertPt.InsertPt;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000355 DbgLoc = OldInsertPt.DL;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000356}
357
Dan Gohmana3e4d5a2008-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 Gohmanbcaf6812010-04-15 01:51:59 +0000361bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000362 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson9f944592009-08-11 20:47:22 +0000363 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmana3e4d5a2008-08-20 00:11:48 +0000364 // Unhandled type. Halt "fast" selection and bail.
365 return false;
Dan Gohmanfd634592008-09-05 18:44:22 +0000366
Dan Gohman3bcbbec2008-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 Gohmanfd634592008-09-05 18:44:22 +0000371 if (!TLI.isTypeLegal(VT)) {
Owen Anderson9f944592009-08-11 20:47:22 +0000372 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohmanfd634592008-09-05 18:44:22 +0000373 // don't require additional zeroing, which makes them easy.
Owen Anderson9f944592009-08-11 20:47:22 +0000374 if (VT == MVT::i1 &&
Dan Gohman5e490a72008-09-25 17:22:52 +0000375 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
376 ISDOpcode == ISD::XOR))
Owen Anderson117c9e82009-08-12 00:36:31 +0000377 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohmanfd634592008-09-05 18:44:22 +0000378 else
379 return false;
380 }
Dan Gohmana3e4d5a2008-08-20 00:11:48 +0000381
Chris Lattnerfba7ca62011-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 Andersondd450b82011-04-22 23:38:06 +0000390
Chris Lattnerb53ccb82011-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 Andersondd450b82011-04-22 23:38:06 +0000395
Chris Lattnerb53ccb82011-04-17 20:23:29 +0000396 // We successfully emitted code for the given LLVM Instruction.
397 UpdateValueMap(I, ResultReg);
398 return true;
Chris Lattnerfba7ca62011-04-17 01:16:47 +0000399 }
Owen Andersondd450b82011-04-22 23:38:06 +0000400
401
Dan Gohman7bda51f2008-09-03 23:12:08 +0000402 unsigned Op0 = getRegForValue(I->getOperand(0));
Chris Lattnerb53ccb82011-04-17 20:23:29 +0000403 if (Op0 == 0) // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmanfe905652008-08-21 01:41:07 +0000404 return false;
405
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000406 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
407
Dan Gohmanfe905652008-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 Lattnerb53ccb82011-04-17 20:23:29 +0000410 uint64_t Imm = CI->getZExtValue();
Owen Andersondd450b82011-04-22 23:38:06 +0000411
Chris Lattner48f75ad2011-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 Andersondd450b82011-04-22 23:38:06 +0000419
Chad Rosier6a63a742012-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 Lattnerb53ccb82011-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 Andersondd450b82011-04-22 23:38:06 +0000430
Chris Lattnerb53ccb82011-04-17 20:23:29 +0000431 // We successfully emitted code for the given LLVM Instruction.
432 UpdateValueMap(I, ResultReg);
433 return true;
Dan Gohmanfe905652008-08-21 01:41:07 +0000434 }
435
Dan Gohman5ca269e2008-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 Gohmanb0b5a272008-08-27 18:10:19 +0000438 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000439 ISDOpcode, Op0, Op0IsKill, CF);
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000440 if (ResultReg != 0) {
441 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman7bda51f2008-09-03 23:12:08 +0000442 UpdateValueMap(I, ResultReg);
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000443 return true;
444 }
Dan Gohman5ca269e2008-08-27 01:09:54 +0000445 }
446
Dan Gohman7bda51f2008-09-03 23:12:08 +0000447 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmanfe905652008-08-21 01:41:07 +0000448 if (Op1 == 0)
449 // Unhandled operand. Halt "fast" selection and bail.
450 return false;
451
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000452 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
453
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000454 // Now we have both operands in registers. Emit the instruction.
Owen Anderson8dd01cc2008-08-25 23:58:18 +0000455 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000456 ISDOpcode,
457 Op0, Op0IsKill,
458 Op1, Op1IsKill);
Dan Gohmana3e4d5a2008-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 Gohmanb16a7782008-08-20 00:23:20 +0000464 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman7bda51f2008-09-03 23:12:08 +0000465 UpdateValueMap(I, ResultReg);
Dan Gohmana3e4d5a2008-08-20 00:11:48 +0000466 return true;
467}
468
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000469bool FastISel::SelectGetElementPtr(const User *I) {
Dan Gohman7bda51f2008-09-03 23:12:08 +0000470 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng864fcc12008-08-20 22:45:34 +0000471 if (N == 0)
472 // Unhandled operand. Halt "fast" selection and bail.
473 return false;
474
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000475 bool NIsKill = hasTrivialKill(I->getOperand(0));
476
Chad Rosierf83ab702011-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 Lattner229907c2011-07-18 04:54:35 +0000482 Type *Ty = I->getOperand(0)->getType();
Owen Anderson9f944592009-08-11 20:47:22 +0000483 MVT VT = TLI.getPointerTy();
Dan Gohmanbcaf6812010-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 Lattner229907c2011-07-18 04:54:35 +0000487 if (StructType *StTy = dyn_cast<StructType>(Ty)) {
Evan Cheng864fcc12008-08-20 22:45:34 +0000488 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
489 if (Field) {
490 // N = N + Offset
Rafael Espindolaea09c592014-02-18 22:05:46 +0000491 TotalOffs += DL.getStructLayout(StTy)->getElementOffset(Field);
Chad Rosierf83ab702011-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 Cheng864fcc12008-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 Gohmanbcaf6812010-04-15 01:51:59 +0000506 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmanf1d83042010-06-18 14:22:04 +0000507 if (CI->isZero()) continue;
Chad Rosierf83ab702011-11-17 07:15:58 +0000508 // N = N + Offset
Chad Rosier879c34f2012-07-06 17:44:22 +0000509 TotalOffs +=
Rafael Espindolaea09c592014-02-18 22:05:46 +0000510 DL.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Chad Rosierf83ab702011-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 Cheng864fcc12008-08-20 22:45:34 +0000523 if (N == 0)
524 // Unhandled operand. Halt "fast" selection and bail.
525 return false;
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000526 NIsKill = true;
Chad Rosierf83ab702011-11-17 07:15:58 +0000527 TotalOffs = 0;
Evan Cheng864fcc12008-08-20 22:45:34 +0000528 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000529
Evan Cheng864fcc12008-08-20 22:45:34 +0000530 // N = N + Idx * ElementSize;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000531 uint64_t ElementSize = DL.getTypeAllocSize(Ty);
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000532 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
533 unsigned IdxN = Pair.first;
534 bool IdxNIsKill = Pair.second;
Evan Cheng864fcc12008-08-20 22:45:34 +0000535 if (IdxN == 0)
536 // Unhandled operand. Halt "fast" selection and bail.
537 return false;
538
Dan Gohmanb5e04bf2008-08-26 20:57:08 +0000539 if (ElementSize != 1) {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000540 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
Dan Gohmanb5e04bf2008-08-26 20:57:08 +0000541 if (IdxN == 0)
542 // Unhandled operand. Halt "fast" selection and bail.
543 return false;
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000544 IdxNIsKill = true;
Dan Gohmanb5e04bf2008-08-26 20:57:08 +0000545 }
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000546 N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
Evan Cheng864fcc12008-08-20 22:45:34 +0000547 if (N == 0)
548 // Unhandled operand. Halt "fast" selection and bail.
549 return false;
550 }
551 }
Chad Rosierf83ab702011-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 Cheng864fcc12008-08-20 22:45:34 +0000558
559 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman7bda51f2008-09-03 23:12:08 +0000560 UpdateValueMap(I, N);
Evan Cheng864fcc12008-08-20 22:45:34 +0000561 return true;
Dan Gohmana3e4d5a2008-08-20 00:11:48 +0000562}
563
Juergen Ributzka04558dc2014-06-12 03:29:26 +0000564/// \brief Add a stack map intrinsic call's live variable operands to a stackmap
565/// or patchpoint machine instruction.
566///
567bool FastISel::addStackMapLiveVars(SmallVectorImpl<MachineOperand> &Ops,
568 const CallInst *CI, unsigned StartIdx) {
569 for (unsigned i = StartIdx, e = CI->getNumArgOperands(); i != e; ++i) {
570 Value *Val = CI->getArgOperand(i);
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 auto SI = FuncInfo.StaticAllocaMap.find(AI);
579 if (SI != FuncInfo.StaticAllocaMap.end())
580 Ops.push_back(MachineOperand::CreateFI(SI->second));
581 else
582 return false;
583 } else {
584 unsigned Reg = getRegForValue(Val);
585 if (Reg == 0)
586 return false;
587 Ops.push_back(MachineOperand::CreateReg(Reg, /*IsDef=*/false));
588 }
589 }
590
591 return true;
592}
593
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000594bool FastISel::SelectCall(const User *I) {
Dan Gohman7da91ae2011-04-26 17:18:34 +0000595 const CallInst *Call = cast<CallInst>(I);
596
597 // Handle simple inline asms.
Dan Gohmande239d22011-10-12 15:56:56 +0000598 if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) {
Dan Gohman7da91ae2011-04-26 17:18:34 +0000599 // Don't attempt to handle constraints.
600 if (!IA->getConstraintString().empty())
601 return false;
602
603 unsigned ExtraInfo = 0;
604 if (IA->hasSideEffects())
605 ExtraInfo |= InlineAsm::Extra_HasSideEffects;
606 if (IA->isAlignStack())
607 ExtraInfo |= InlineAsm::Extra_IsAlignStack;
608
Rafael Espindolaea09c592014-02-18 22:05:46 +0000609 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohman7da91ae2011-04-26 17:18:34 +0000610 TII.get(TargetOpcode::INLINEASM))
611 .addExternalSymbol(IA->getAsmString().c_str())
612 .addImm(ExtraInfo);
613 return true;
614 }
615
Michael J. Spencer8b98bf22012-02-22 19:06:13 +0000616 MachineModuleInfo &MMI = FuncInfo.MF->getMMI();
617 ComputeUsesVAFloatArgument(*Call, &MMI);
618
Dan Gohman7da91ae2011-04-26 17:18:34 +0000619 const Function *F = Call->getCalledFunction();
Dan Gohman32a733e2008-09-25 17:05:24 +0000620 if (!F) return false;
621
Dan Gohman8a2dae52010-04-13 17:07:06 +0000622 // Handle selected intrinsic function calls.
Chris Lattner91328b32011-04-19 05:52:03 +0000623 switch (F->getIntrinsicID()) {
Dan Gohman32a733e2008-09-25 17:05:24 +0000624 default: break;
Chad Rosiera33015d2012-05-11 23:21:01 +0000625 // At -O0 we don't care about the lifetime intrinsics.
Eric Christopher81e2bf22012-02-17 23:03:39 +0000626 case Intrinsic::lifetime_start:
627 case Intrinsic::lifetime_end:
Chad Rosier88d53ea2012-07-06 17:33:39 +0000628 // The donothing intrinsic does, well, nothing.
629 case Intrinsic::donothing:
Eric Christopher81e2bf22012-02-17 23:03:39 +0000630 return true;
Chad Rosier88d53ea2012-07-06 17:33:39 +0000631
Bill Wendling65c0fd42009-02-13 02:16:35 +0000632 case Intrinsic::dbg_declare: {
Dan Gohman7da91ae2011-04-26 17:18:34 +0000633 const DbgDeclareInst *DI = cast<DbgDeclareInst>(Call);
Manman Ren983a16c2013-06-28 05:43:10 +0000634 DIVariable DIVar(DI->getVariable());
Stephen Lincfe7f352013-07-08 00:37:03 +0000635 assert((!DIVar || DIVar.isVariable()) &&
Manman Ren983a16c2013-06-28 05:43:10 +0000636 "Variable in DbgDeclareInst should be either null or a DIVariable.");
637 if (!DIVar ||
Eric Christopher142820b2012-03-15 21:33:44 +0000638 !FuncInfo.MF->getMMI().hasDebugInfo()) {
639 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Devang Patel87127712009-07-02 22:43:26 +0000640 return true;
Eric Christopher142820b2012-03-15 21:33:44 +0000641 }
Devang Patel87127712009-07-02 22:43:26 +0000642
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000643 const Value *Address = DI->getAddress();
Eric Christopher3390a6e2012-03-15 21:33:47 +0000644 if (!Address || isa<UndefValue>(Address)) {
Eric Christopher142820b2012-03-15 21:33:44 +0000645 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Dale Johannesendb2eb472010-02-06 02:26:02 +0000646 return true;
Eric Christopher142820b2012-03-15 21:33:44 +0000647 }
Devang Patele4682fa2010-09-14 20:29:31 +0000648
Adrian Prantl418d1d12013-07-09 20:28:37 +0000649 unsigned Offset = 0;
David Blaikie0252265b2013-06-16 20:34:15 +0000650 Optional<MachineOperand> Op;
651 if (const Argument *Arg = dyn_cast<Argument>(Address))
Devang Patel9d904e12011-09-08 22:59:09 +0000652 // Some arguments' frame index is recorded during argument lowering.
Adrian Prantl418d1d12013-07-09 20:28:37 +0000653 Offset = FuncInfo.getArgumentFrameIndex(Arg);
654 if (Offset)
655 Op = MachineOperand::CreateFI(Offset);
David Blaikie0252265b2013-06-16 20:34:15 +0000656 if (!Op)
657 if (unsigned Reg = lookUpRegForValue(Address))
658 Op = MachineOperand::CreateReg(Reg, false);
Eric Christopher60e01c52012-03-20 01:07:58 +0000659
Bill Wendling9f829f12012-03-30 00:02:55 +0000660 // If we have a VLA that has a "use" in a metadata node that's then used
661 // here but it has no other uses, then we have a problem. E.g.,
662 //
663 // int foo (const int *x) {
664 // char a[*x];
665 // return 0;
666 // }
667 //
668 // If we assign 'a' a vreg and fast isel later on has to use the selection
669 // DAG isel, it will want to copy the value to the vreg. However, there are
670 // no uses, which goes counter to what selection DAG isel expects.
David Blaikie0252265b2013-06-16 20:34:15 +0000671 if (!Op && !Address->use_empty() && isa<Instruction>(Address) &&
Eric Christopher60e01c52012-03-20 01:07:58 +0000672 (!isa<AllocaInst>(Address) ||
673 !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(Address))))
David Blaikie0252265b2013-06-16 20:34:15 +0000674 Op = MachineOperand::CreateReg(FuncInfo.InitializeRegForValue(Address),
Adrian Prantl262bcf42013-09-18 22:08:59 +0000675 false);
Wesley Peck527da1b2010-11-23 03:31:01 +0000676
Adrian Prantl262bcf42013-09-18 22:08:59 +0000677 if (Op) {
Adrian Prantl418d1d12013-07-09 20:28:37 +0000678 if (Op->isReg()) {
679 Op->setIsDebug(true);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000680 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
David Blaikie6004dbc2013-10-14 20:15:04 +0000681 TII.get(TargetOpcode::DBG_VALUE), false, Op->getReg(), 0,
682 DI->getVariable());
683 } else
Rafael Espindolaea09c592014-02-18 22:05:46 +0000684 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
David Blaikie6004dbc2013-10-14 20:15:04 +0000685 TII.get(TargetOpcode::DBG_VALUE))
686 .addOperand(*Op)
687 .addImm(0)
688 .addMetadata(DI->getVariable());
Adrian Prantl262bcf42013-09-18 22:08:59 +0000689 } else {
Eric Christophere5e54c82012-03-20 01:07:53 +0000690 // We can't yet handle anything else here because it would require
691 // generating code, thus altering codegen because of debug info.
Adrian Prantl0d1e5592013-05-22 18:02:19 +0000692 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Adrian Prantl262bcf42013-09-18 22:08:59 +0000693 }
Dan Gohman32a733e2008-09-25 17:05:24 +0000694 return true;
Bill Wendling65c0fd42009-02-13 02:16:35 +0000695 }
Dale Johannesendd331042010-02-26 20:01:55 +0000696 case Intrinsic::dbg_value: {
Dale Johannesen5d7f0a02010-04-07 01:15:14 +0000697 // This form of DBG_VALUE is target-independent.
Dan Gohman7da91ae2011-04-26 17:18:34 +0000698 const DbgValueInst *DI = cast<DbgValueInst>(Call);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000699 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000700 const Value *V = DI->getValue();
Dale Johannesendd331042010-02-26 20:01:55 +0000701 if (!V) {
702 // Currently the optimizer can produce this; insert an undef to
703 // help debugging. Probably the optimizer should not do this.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000704 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000705 .addReg(0U).addImm(DI->getOffset())
706 .addMetadata(DI->getVariable());
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000707 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Devang Patelf071d722011-06-24 20:46:11 +0000708 if (CI->getBitWidth() > 64)
Rafael Espindolaea09c592014-02-18 22:05:46 +0000709 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Devang Patelf071d722011-06-24 20:46:11 +0000710 .addCImm(CI).addImm(DI->getOffset())
711 .addMetadata(DI->getVariable());
Chad Rosier879c34f2012-07-06 17:44:22 +0000712 else
Rafael Espindolaea09c592014-02-18 22:05:46 +0000713 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Devang Patelf071d722011-06-24 20:46:11 +0000714 .addImm(CI->getZExtValue()).addImm(DI->getOffset())
715 .addMetadata(DI->getVariable());
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000716 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000717 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000718 .addFPImm(CF).addImm(DI->getOffset())
719 .addMetadata(DI->getVariable());
Dale Johannesendd331042010-02-26 20:01:55 +0000720 } else if (unsigned Reg = lookUpRegForValue(V)) {
Adrian Prantldb3e26d2013-09-16 23:29:03 +0000721 // FIXME: This does not handle register-indirect values at offset 0.
Adrian Prantl418d1d12013-07-09 20:28:37 +0000722 bool IsIndirect = DI->getOffset() != 0;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000723 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, IsIndirect,
Adrian Prantl418d1d12013-07-09 20:28:37 +0000724 Reg, DI->getOffset(), DI->getVariable());
Dale Johannesendd331042010-02-26 20:01:55 +0000725 } else {
726 // We can't yet handle anything else here because it would require
727 // generating code, thus altering codegen because of debug info.
Adrian Prantl0d1e5592013-05-22 18:02:19 +0000728 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Wesley Peck527da1b2010-11-23 03:31:01 +0000729 }
Dale Johannesendd331042010-02-26 20:01:55 +0000730 return true;
731 }
Eli Friedman8f1e11c2011-05-14 00:47:51 +0000732 case Intrinsic::objectsize: {
733 ConstantInt *CI = cast<ConstantInt>(Call->getArgOperand(1));
734 unsigned long long Res = CI->isZero() ? -1ULL : 0;
735 Constant *ResCI = ConstantInt::get(Call->getType(), Res);
736 unsigned ResultReg = getRegForValue(ResCI);
737 if (ResultReg == 0)
738 return false;
739 UpdateValueMap(Call, ResultReg);
740 return true;
741 }
Chad Rosier9c1796f2013-03-07 20:42:17 +0000742 case Intrinsic::expect: {
Chad Rosier3a200e12013-03-07 21:38:33 +0000743 unsigned ResultReg = getRegForValue(Call->getArgOperand(0));
Nick Lewycky48beb212013-03-11 21:44:37 +0000744 if (ResultReg == 0)
745 return false;
Chad Rosier3a200e12013-03-07 21:38:33 +0000746 UpdateValueMap(Call, ResultReg);
747 return true;
Chad Rosier9c1796f2013-03-07 20:42:17 +0000748 }
Juergen Ributzka04558dc2014-06-12 03:29:26 +0000749 case Intrinsic::experimental_stackmap: {
750 // void @llvm.experimental.stackmap(i64 <id>, i32 <numShadowBytes>,
751 // [live variables...])
752
753 assert(Call->getCalledFunction()->getReturnType()->isVoidTy() &&
754 "Stackmap cannot return a value.");
755
756 // The stackmap intrinsic only records the live variables (the arguments
757 // passed to it) and emits NOPS (if requested). Unlike the patchpoint
758 // intrinsic, this won't be lowered to a function call. This means we don't
759 // have to worry about calling conventions and target-specific lowering
760 // code. Instead we perform the call lowering right here.
761 //
762 // CALLSEQ_START(0)
763 // STACKMAP(id, nbytes, ...)
764 // CALLSEQ_END(0, 0)
765 //
766
767 SmallVector<MachineOperand, 32> Ops;
768
769 // Add the <id> and <numBytes> constants.
770 assert(isa<ConstantInt>(Call->getOperand(PatchPointOpers::IDPos)) &&
771 "Expected a constant integer.");
772 auto IDVal = cast<ConstantInt>(Call->getOperand(PatchPointOpers::IDPos));
773 Ops.push_back(MachineOperand::CreateImm(IDVal->getZExtValue()));
774
775 assert(isa<ConstantInt>(Call->getOperand(PatchPointOpers::NBytesPos)) &&
776 "Expected a constant integer.");
777 auto NBytesVal =
778 cast<ConstantInt>(Call->getOperand(PatchPointOpers::NBytesPos));
779 Ops.push_back(MachineOperand::CreateImm(NBytesVal->getZExtValue()));
780
781 // Push live variables for the stack map.
782 if (!addStackMapLiveVars(Ops, Call, 2))
783 return false;
784
785 // We are not adding any register mask info here, because the stackmap
786 // doesn't clobber anything.
787
788 // Add scratch registers as implicit def and early clobber.
789 CallingConv::ID CC = Call->getCallingConv();
790 const MCPhysReg *ScratchRegs = TLI.getScratchRegisters(CC);
791 for (unsigned i = 0; ScratchRegs[i]; ++i)
792 Ops.push_back(MachineOperand::CreateReg(
793 ScratchRegs[i], /*IsDef=*/true, /*IsImp=*/true, /*IsKill=*/false,
794 /*IsDead=*/false, /*IsUndef=*/false, /*IsEarlyClobber=*/true));
795
796 // Issue CALLSEQ_START
797 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
798 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
799 .addImm(0);
800
801 // Issue STACKMAP.
802 MachineInstrBuilder MIB;
803 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
804 TII.get(TargetOpcode::STACKMAP));
805
806 for (auto const &MO : Ops)
807 MIB.addOperand(MO);
808
809 // Issue CALLSEQ_END
810 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
811 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
812 .addImm(0).addImm(0);
813
814 // Inform the Frame Information that we have a stackmap in this function.
815 FuncInfo.MF->getFrameInfo()->setHasStackMap();
816
817 return true;
818 }
Dan Gohman32a733e2008-09-25 17:05:24 +0000819 }
Dan Gohman8a2dae52010-04-13 17:07:06 +0000820
Ivan Krasind7cbd4c2011-08-18 22:06:10 +0000821 // Usually, it does not make sense to initialize a value,
822 // make an unrelated function call and use the value, because
823 // it tends to be spilled on the stack. So, we move the pointer
824 // to the last local value to the beginning of the block, so that
825 // all the values which have already been materialized,
826 // appear after the call. It also makes sense to skip intrinsics
827 // since they tend to be inlined.
Pete Cooper047f81a2013-02-22 01:50:38 +0000828 if (!isa<IntrinsicInst>(Call))
Ivan Krasind7cbd4c2011-08-18 22:06:10 +0000829 flushLocalValueMap();
830
Dan Gohman8a2dae52010-04-13 17:07:06 +0000831 // An arbitrary call. Bail.
Dan Gohman32a733e2008-09-25 17:05:24 +0000832 return false;
833}
834
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000835bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000836 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
837 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peck527da1b2010-11-23 03:31:01 +0000838
Owen Anderson9f944592009-08-11 20:47:22 +0000839 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
840 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersonca1711a2008-08-26 23:46:32 +0000841 // Unhandled type. Halt "fast" selection and bail.
842 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000843
Eli Friedmanc7035512011-05-25 23:49:02 +0000844 // Check if the destination type is legal.
Dan Gohmana62e4ab2009-03-13 23:53:06 +0000845 if (!TLI.isTypeLegal(DstVT))
Eli Friedmanc7035512011-05-25 23:49:02 +0000846 return false;
Dan Gohmana62e4ab2009-03-13 23:53:06 +0000847
Eli Friedmanc7035512011-05-25 23:49:02 +0000848 // Check if the source operand is legal.
Dan Gohmana62e4ab2009-03-13 23:53:06 +0000849 if (!TLI.isTypeLegal(SrcVT))
Eli Friedmanc7035512011-05-25 23:49:02 +0000850 return false;
Dan Gohmana62e4ab2009-03-13 23:53:06 +0000851
Dan Gohman7bda51f2008-09-03 23:12:08 +0000852 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersonca1711a2008-08-26 23:46:32 +0000853 if (!InputReg)
854 // Unhandled operand. Halt "fast" selection and bail.
855 return false;
Dan Gohmanc0bb9592009-03-13 20:42:20 +0000856
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000857 bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
858
Owen Andersonca1711a2008-08-26 23:46:32 +0000859 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
860 DstVT.getSimpleVT(),
861 Opcode,
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000862 InputReg, InputRegIsKill);
Owen Andersonca1711a2008-08-26 23:46:32 +0000863 if (!ResultReg)
864 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000865
Dan Gohman7bda51f2008-09-03 23:12:08 +0000866 UpdateValueMap(I, ResultReg);
Owen Andersonca1711a2008-08-26 23:46:32 +0000867 return true;
868}
869
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000870bool FastISel::SelectBitCast(const User *I) {
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000871 // If the bitcast doesn't change the type, just use the operand value.
872 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman7bda51f2008-09-03 23:12:08 +0000873 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohman61cfa302008-08-27 20:41:38 +0000874 if (Reg == 0)
875 return false;
Dan Gohman7bda51f2008-09-03 23:12:08 +0000876 UpdateValueMap(I, Reg);
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000877 return true;
878 }
879
Wesley Peck527da1b2010-11-23 03:31:01 +0000880 // Bitcasts of other values become reg-reg copies or BITCAST operators.
Patrik Hagglundc494d242012-12-17 14:30:06 +0000881 EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType());
882 EVT DstEVT = TLI.getValueType(I->getType());
883 if (SrcEVT == MVT::Other || DstEVT == MVT::Other ||
884 !TLI.isTypeLegal(SrcEVT) || !TLI.isTypeLegal(DstEVT))
Owen Andersonca1711a2008-08-26 23:46:32 +0000885 // Unhandled type. Halt "fast" selection and bail.
886 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000887
Patrik Hagglundc494d242012-12-17 14:30:06 +0000888 MVT SrcVT = SrcEVT.getSimpleVT();
889 MVT DstVT = DstEVT.getSimpleVT();
Dan Gohman7bda51f2008-09-03 23:12:08 +0000890 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000891 if (Op0 == 0)
892 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersonca1711a2008-08-26 23:46:32 +0000893 return false;
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000894
895 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
Wesley Peck527da1b2010-11-23 03:31:01 +0000896
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000897 // First, try to perform the bitcast by inserting a reg-reg copy.
898 unsigned ResultReg = 0;
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000899 if (SrcVT == DstVT) {
Craig Topper760b1342012-02-22 05:59:10 +0000900 const TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
901 const TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
Jakob Stoklund Olesen51642ae2010-07-11 05:16:54 +0000902 // Don't attempt a cross-class copy. It will likely fail.
903 if (SrcClass == DstClass) {
904 ResultReg = createResultReg(DstClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000905 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
906 TII.get(TargetOpcode::COPY), ResultReg).addReg(Op0);
Jakob Stoklund Olesen51642ae2010-07-11 05:16:54 +0000907 }
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000908 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000909
910 // If the reg-reg copy failed, select a BITCAST opcode.
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000911 if (!ResultReg)
Patrik Hagglund5e6c3612012-12-13 06:34:11 +0000912 ResultReg = FastEmit_r(SrcVT, DstVT, ISD::BITCAST, Op0, Op0IsKill);
Wesley Peck527da1b2010-11-23 03:31:01 +0000913
Dan Gohmanb0b5a272008-08-27 18:10:19 +0000914 if (!ResultReg)
Owen Andersonca1711a2008-08-26 23:46:32 +0000915 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000916
Dan Gohman7bda51f2008-09-03 23:12:08 +0000917 UpdateValueMap(I, ResultReg);
Owen Andersonca1711a2008-08-26 23:46:32 +0000918 return true;
919}
920
Dan Gohman7bda51f2008-09-03 23:12:08 +0000921bool
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000922FastISel::SelectInstruction(const Instruction *I) {
Dan Gohman6e9a8fc2010-04-23 15:29:50 +0000923 // Just before the terminator instruction, insert instructions to
924 // feed PHI nodes in successor blocks.
925 if (isa<TerminatorInst>(I))
926 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
927 return false;
928
Rafael Espindolaea09c592014-02-18 22:05:46 +0000929 DbgLoc = I->getDebugLoc();
Dan Gohmane450d742010-04-20 00:48:35 +0000930
Chad Rosier46addb92011-11-29 19:40:47 +0000931 MachineBasicBlock::iterator SavedInsertPt = FuncInfo.InsertPt;
932
Bob Wilson3e6fa462012-08-03 04:06:28 +0000933 if (const CallInst *Call = dyn_cast<CallInst>(I)) {
934 const Function *F = Call->getCalledFunction();
935 LibFunc::Func Func;
Akira Hatanaka3d90f992014-04-15 21:30:06 +0000936
937 // As a special case, don't handle calls to builtin library functions that
938 // may be translated directly to target instructions.
Bob Wilson3e6fa462012-08-03 04:06:28 +0000939 if (F && !F->hasLocalLinkage() && F->hasName() &&
940 LibInfo->getLibFunc(F->getName(), Func) &&
Bob Wilson871701c2012-08-03 21:26:24 +0000941 LibInfo->hasOptimizedCodeGen(Func))
Bob Wilson3e6fa462012-08-03 04:06:28 +0000942 return false;
Akira Hatanaka3d90f992014-04-15 21:30:06 +0000943
944 // Don't handle Intrinsic::trap if a trap funciton is specified.
945 if (F && F->getIntrinsicID() == Intrinsic::trap &&
946 !TM.Options.getTrapFunctionName().empty())
947 return false;
Bob Wilson3e6fa462012-08-03 04:06:28 +0000948 }
949
Dan Gohman18f94462009-12-05 01:27:58 +0000950 // First, try doing target-independent selection.
Michael Ilsemanba8446c2013-02-27 19:54:00 +0000951 if (SelectOperator(I, I->getOpcode())) {
Jan Wen Voung7857a642013-03-08 22:56:31 +0000952 ++NumFastIselSuccessIndependent;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000953 DbgLoc = DebugLoc();
Dan Gohman18f94462009-12-05 01:27:58 +0000954 return true;
Dan Gohmane450d742010-04-20 00:48:35 +0000955 }
Chad Rosier879c34f2012-07-06 17:44:22 +0000956 // Remove dead code. However, ignore call instructions since we've flushed
Chad Rosier46addb92011-11-29 19:40:47 +0000957 // the local value map and recomputed the insert point.
958 if (!isa<CallInst>(I)) {
959 recomputeInsertPt();
960 if (SavedInsertPt != FuncInfo.InsertPt)
961 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
962 }
Dan Gohman18f94462009-12-05 01:27:58 +0000963
964 // Next, try calling the target to attempt to handle the instruction.
Chad Rosier46addb92011-11-29 19:40:47 +0000965 SavedInsertPt = FuncInfo.InsertPt;
Dan Gohmane450d742010-04-20 00:48:35 +0000966 if (TargetSelectInstruction(I)) {
Jan Wen Voung7857a642013-03-08 22:56:31 +0000967 ++NumFastIselSuccessTarget;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000968 DbgLoc = DebugLoc();
Dan Gohman18f94462009-12-05 01:27:58 +0000969 return true;
Dan Gohmane450d742010-04-20 00:48:35 +0000970 }
Chad Rosier46addb92011-11-29 19:40:47 +0000971 // Check for dead code and remove as necessary.
972 recomputeInsertPt();
973 if (SavedInsertPt != FuncInfo.InsertPt)
974 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
Dan Gohman18f94462009-12-05 01:27:58 +0000975
Rafael Espindolaea09c592014-02-18 22:05:46 +0000976 DbgLoc = DebugLoc();
Dan Gohman18f94462009-12-05 01:27:58 +0000977 return false;
Dan Gohmanfcf54562008-09-05 18:18:20 +0000978}
979
Dan Gohman1ab1d312008-10-02 22:15:21 +0000980/// FastEmitBranch - Emit an unconditional branch to the given block,
981/// unless it is the immediate (fall-through) successor, and update
982/// the CFG.
983void
Rafael Espindolaea09c592014-02-18 22:05:46 +0000984FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DbgLoc) {
Evan Cheng615620c2013-02-11 01:27:15 +0000985 if (FuncInfo.MBB->getBasicBlock()->size() > 1 &&
986 FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
Eric Christophere9abba72012-04-10 18:18:10 +0000987 // For more accurate line information if this is the only instruction
988 // in the block then emit it, otherwise we have the unconditional
989 // fall-through case, which needs no instructions.
Dan Gohman1ab1d312008-10-02 22:15:21 +0000990 } else {
991 // The unconditional branch case.
Craig Topperc0196b12014-04-14 00:51:57 +0000992 TII.InsertBranch(*FuncInfo.MBB, MSucc, nullptr,
Rafael Espindolaea09c592014-02-18 22:05:46 +0000993 SmallVector<MachineOperand, 0>(), DbgLoc);
Dan Gohman1ab1d312008-10-02 22:15:21 +0000994 }
Juergen Ributzka454d3742014-06-13 00:45:11 +0000995 uint32_t BranchWeight = 0;
996 if (FuncInfo.BPI)
997 BranchWeight = FuncInfo.BPI->getEdgeWeight(FuncInfo.MBB->getBasicBlock(),
998 MSucc->getBasicBlock());
999 FuncInfo.MBB->addSuccessor(MSucc, BranchWeight);
Dan Gohman1ab1d312008-10-02 22:15:21 +00001000}
1001
Dan Gohmanaa92dc12009-09-03 22:53:57 +00001002/// SelectFNeg - Emit an FNeg operation.
1003///
1004bool
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001005FastISel::SelectFNeg(const User *I) {
Dan Gohmanaa92dc12009-09-03 22:53:57 +00001006 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
1007 if (OpReg == 0) return false;
1008
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001009 bool OpRegIsKill = hasTrivialKill(I);
1010
Dan Gohman9cbef322009-09-11 00:36:43 +00001011 // If the target has ISD::FNEG, use it.
1012 EVT VT = TLI.getValueType(I->getType());
1013 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001014 ISD::FNEG, OpReg, OpRegIsKill);
Dan Gohman9cbef322009-09-11 00:36:43 +00001015 if (ResultReg != 0) {
1016 UpdateValueMap(I, ResultReg);
1017 return true;
1018 }
1019
Dan Gohman89b090e2009-09-11 00:34:46 +00001020 // Bitcast the value to integer, twiddle the sign bit with xor,
1021 // and then bitcast it back to floating-point.
Dan Gohmanaa92dc12009-09-03 22:53:57 +00001022 if (VT.getSizeInBits() > 64) return false;
Dan Gohman89b090e2009-09-11 00:34:46 +00001023 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
1024 if (!TLI.isTypeLegal(IntVT))
1025 return false;
1026
1027 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
Wesley Peck527da1b2010-11-23 03:31:01 +00001028 ISD::BITCAST, OpReg, OpRegIsKill);
Dan Gohman89b090e2009-09-11 00:34:46 +00001029 if (IntReg == 0)
1030 return false;
1031
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001032 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
1033 IntReg, /*Kill=*/true,
Dan Gohman89b090e2009-09-11 00:34:46 +00001034 UINT64_C(1) << (VT.getSizeInBits()-1),
1035 IntVT.getSimpleVT());
1036 if (IntResultReg == 0)
1037 return false;
1038
1039 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
Wesley Peck527da1b2010-11-23 03:31:01 +00001040 ISD::BITCAST, IntResultReg, /*Kill=*/true);
Dan Gohmanaa92dc12009-09-03 22:53:57 +00001041 if (ResultReg == 0)
1042 return false;
1043
1044 UpdateValueMap(I, ResultReg);
1045 return true;
1046}
1047
Dan Gohmanfcf54562008-09-05 18:18:20 +00001048bool
Eli Friedman9ac94472011-05-16 20:27:46 +00001049FastISel::SelectExtractValue(const User *U) {
1050 const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U);
Eli Friedman4c08bb42011-05-16 20:34:53 +00001051 if (!EVI)
Eli Friedman9ac94472011-05-16 20:27:46 +00001052 return false;
1053
Eli Friedmana4d4a012011-05-16 21:06:17 +00001054 // Make sure we only try to handle extracts with a legal result. But also
1055 // allow i1 because it's easy.
Eli Friedman9ac94472011-05-16 20:27:46 +00001056 EVT RealVT = TLI.getValueType(EVI->getType(), /*AllowUnknown=*/true);
1057 if (!RealVT.isSimple())
1058 return false;
1059 MVT VT = RealVT.getSimpleVT();
Eli Friedmana4d4a012011-05-16 21:06:17 +00001060 if (!TLI.isTypeLegal(VT) && VT != MVT::i1)
Eli Friedman9ac94472011-05-16 20:27:46 +00001061 return false;
1062
1063 const Value *Op0 = EVI->getOperand(0);
Chris Lattner229907c2011-07-18 04:54:35 +00001064 Type *AggTy = Op0->getType();
Eli Friedman9ac94472011-05-16 20:27:46 +00001065
1066 // Get the base result register.
1067 unsigned ResultReg;
1068 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(Op0);
1069 if (I != FuncInfo.ValueMap.end())
1070 ResultReg = I->second;
Eli Friedmanbd375f12011-06-06 05:46:34 +00001071 else if (isa<Instruction>(Op0))
Eli Friedman9ac94472011-05-16 20:27:46 +00001072 ResultReg = FuncInfo.InitializeRegForValue(Op0);
Eli Friedmanbd375f12011-06-06 05:46:34 +00001073 else
1074 return false; // fast-isel can't handle aggregate constants at the moment
Eli Friedman9ac94472011-05-16 20:27:46 +00001075
1076 // Get the actual result register, which is an offset from the base register.
Jay Foad57aa6362011-07-13 10:26:04 +00001077 unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices());
Eli Friedman9ac94472011-05-16 20:27:46 +00001078
1079 SmallVector<EVT, 4> AggValueVTs;
1080 ComputeValueVTs(TLI, AggTy, AggValueVTs);
1081
1082 for (unsigned i = 0; i < VTIndex; i++)
1083 ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]);
1084
1085 UpdateValueMap(EVI, ResultReg);
1086 return true;
1087}
1088
1089bool
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001090FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohmanfcf54562008-09-05 18:18:20 +00001091 switch (Opcode) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001092 case Instruction::Add:
1093 return SelectBinaryOp(I, ISD::ADD);
1094 case Instruction::FAdd:
1095 return SelectBinaryOp(I, ISD::FADD);
1096 case Instruction::Sub:
1097 return SelectBinaryOp(I, ISD::SUB);
1098 case Instruction::FSub:
Dan Gohmanaa92dc12009-09-03 22:53:57 +00001099 // FNeg is currently represented in LLVM IR as a special case of FSub.
1100 if (BinaryOperator::isFNeg(I))
1101 return SelectFNeg(I);
Dan Gohmana5b96452009-06-04 22:49:04 +00001102 return SelectBinaryOp(I, ISD::FSUB);
1103 case Instruction::Mul:
1104 return SelectBinaryOp(I, ISD::MUL);
1105 case Instruction::FMul:
1106 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman7bda51f2008-09-03 23:12:08 +00001107 case Instruction::SDiv:
1108 return SelectBinaryOp(I, ISD::SDIV);
1109 case Instruction::UDiv:
1110 return SelectBinaryOp(I, ISD::UDIV);
1111 case Instruction::FDiv:
1112 return SelectBinaryOp(I, ISD::FDIV);
1113 case Instruction::SRem:
1114 return SelectBinaryOp(I, ISD::SREM);
1115 case Instruction::URem:
1116 return SelectBinaryOp(I, ISD::UREM);
1117 case Instruction::FRem:
1118 return SelectBinaryOp(I, ISD::FREM);
1119 case Instruction::Shl:
1120 return SelectBinaryOp(I, ISD::SHL);
1121 case Instruction::LShr:
1122 return SelectBinaryOp(I, ISD::SRL);
1123 case Instruction::AShr:
1124 return SelectBinaryOp(I, ISD::SRA);
1125 case Instruction::And:
1126 return SelectBinaryOp(I, ISD::AND);
1127 case Instruction::Or:
1128 return SelectBinaryOp(I, ISD::OR);
1129 case Instruction::Xor:
1130 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb2226e22008-08-13 20:19:35 +00001131
Dan Gohman7bda51f2008-09-03 23:12:08 +00001132 case Instruction::GetElementPtr:
1133 return SelectGetElementPtr(I);
Dan Gohmana3e4d5a2008-08-20 00:11:48 +00001134
Dan Gohman7bda51f2008-09-03 23:12:08 +00001135 case Instruction::Br: {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001136 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana3e4d5a2008-08-20 00:11:48 +00001137
Dan Gohman7bda51f2008-09-03 23:12:08 +00001138 if (BI->isUnconditional()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001139 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohman87fb4e82010-07-07 16:29:44 +00001140 MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
Stuart Hastings0125b642010-06-17 22:43:56 +00001141 FastEmitBranch(MSucc, BI->getDebugLoc());
Dan Gohman7bda51f2008-09-03 23:12:08 +00001142 return true;
Owen Anderson14054922008-08-27 00:31:01 +00001143 }
Dan Gohman7bda51f2008-09-03 23:12:08 +00001144
1145 // Conditional branches are not handed yet.
1146 // Halt "fast" selection and bail.
1147 return false;
Dan Gohmanb2226e22008-08-13 20:19:35 +00001148 }
1149
Dan Gohmanea56bdd2008-09-05 01:08:41 +00001150 case Instruction::Unreachable:
Yaron Kerend7ba46b2014-04-19 13:47:43 +00001151 if (TM.Options.TrapUnreachable)
1152 return FastEmit_(MVT::Other, MVT::Other, ISD::TRAP) != 0;
1153 else
1154 return true;
Dan Gohmanea56bdd2008-09-05 01:08:41 +00001155
Dan Gohman39d82f92008-09-10 20:11:02 +00001156 case Instruction::Alloca:
1157 // FunctionLowering has the static-sized case covered.
Dan Gohman87fb4e82010-07-07 16:29:44 +00001158 if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
Dan Gohman39d82f92008-09-10 20:11:02 +00001159 return true;
1160
1161 // Dynamic-sized alloca is not handled yet.
1162 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001163
Dan Gohman32a733e2008-09-25 17:05:24 +00001164 case Instruction::Call:
1165 return SelectCall(I);
Wesley Peck527da1b2010-11-23 03:31:01 +00001166
Dan Gohman7bda51f2008-09-03 23:12:08 +00001167 case Instruction::BitCast:
1168 return SelectBitCast(I);
1169
1170 case Instruction::FPToSI:
1171 return SelectCast(I, ISD::FP_TO_SINT);
1172 case Instruction::ZExt:
1173 return SelectCast(I, ISD::ZERO_EXTEND);
1174 case Instruction::SExt:
1175 return SelectCast(I, ISD::SIGN_EXTEND);
1176 case Instruction::Trunc:
1177 return SelectCast(I, ISD::TRUNCATE);
1178 case Instruction::SIToFP:
1179 return SelectCast(I, ISD::SINT_TO_FP);
1180
1181 case Instruction::IntToPtr: // Deliberate fall-through.
1182 case Instruction::PtrToInt: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001183 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1184 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman7bda51f2008-09-03 23:12:08 +00001185 if (DstVT.bitsGT(SrcVT))
1186 return SelectCast(I, ISD::ZERO_EXTEND);
1187 if (DstVT.bitsLT(SrcVT))
1188 return SelectCast(I, ISD::TRUNCATE);
1189 unsigned Reg = getRegForValue(I->getOperand(0));
1190 if (Reg == 0) return false;
1191 UpdateValueMap(I, Reg);
1192 return true;
1193 }
Dan Gohman918fe082008-09-23 21:53:34 +00001194
Eli Friedman9ac94472011-05-16 20:27:46 +00001195 case Instruction::ExtractValue:
1196 return SelectExtractValue(I);
1197
Dan Gohmanf41ad472010-04-20 15:00:41 +00001198 case Instruction::PHI:
1199 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
1200
Dan Gohman7bda51f2008-09-03 23:12:08 +00001201 default:
1202 // Unhandled instruction. Halt "fast" selection and bail.
1203 return false;
1204 }
Dan Gohmanb2226e22008-08-13 20:19:35 +00001205}
1206
Bob Wilson3e6fa462012-08-03 04:06:28 +00001207FastISel::FastISel(FunctionLoweringInfo &funcInfo,
1208 const TargetLibraryInfo *libInfo)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001209 : FuncInfo(funcInfo),
Dan Gohman87fb4e82010-07-07 16:29:44 +00001210 MRI(FuncInfo.MF->getRegInfo()),
1211 MFI(*FuncInfo.MF->getFrameInfo()),
1212 MCP(*FuncInfo.MF->getConstantPool()),
1213 TM(FuncInfo.MF->getTarget()),
Rafael Espindolaea09c592014-02-18 22:05:46 +00001214 DL(*TM.getDataLayout()),
Dan Gohman49e19e92008-08-22 00:20:26 +00001215 TII(*TM.getInstrInfo()),
Dan Gohmanffcb5902010-05-05 23:58:35 +00001216 TLI(*TM.getTargetLowering()),
Bob Wilson3e6fa462012-08-03 04:06:28 +00001217 TRI(*TM.getRegisterInfo()),
1218 LibInfo(libInfo) {
Dan Gohman02c84b82008-08-20 21:05:57 +00001219}
1220
Dan Gohmanc4442382008-08-14 21:51:29 +00001221FastISel::~FastISel() {}
1222
Evan Cheng615620c2013-02-11 01:27:15 +00001223bool FastISel::FastLowerArguments() {
1224 return false;
1225}
1226
Owen Anderson9f944592009-08-11 20:47:22 +00001227unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohman404a9842010-01-05 22:26:32 +00001228 unsigned) {
Dan Gohmanb2226e22008-08-13 20:19:35 +00001229 return 0;
1230}
1231
Owen Anderson9f944592009-08-11 20:47:22 +00001232unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001233 unsigned,
1234 unsigned /*Op0*/, bool /*Op0IsKill*/) {
Dan Gohmanb2226e22008-08-13 20:19:35 +00001235 return 0;
1236}
1237
Wesley Peck527da1b2010-11-23 03:31:01 +00001238unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001239 unsigned,
1240 unsigned /*Op0*/, bool /*Op0IsKill*/,
1241 unsigned /*Op1*/, bool /*Op1IsKill*/) {
Dan Gohmanb2226e22008-08-13 20:19:35 +00001242 return 0;
1243}
1244
Dan Gohman404a9842010-01-05 22:26:32 +00001245unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng864fcc12008-08-20 22:45:34 +00001246 return 0;
1247}
1248
Owen Anderson9f944592009-08-11 20:47:22 +00001249unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001250 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman5ca269e2008-08-27 01:09:54 +00001251 return 0;
1252}
1253
Owen Anderson9f944592009-08-11 20:47:22 +00001254unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001255 unsigned,
1256 unsigned /*Op0*/, bool /*Op0IsKill*/,
Owen Anderson8dd01cc2008-08-25 23:58:18 +00001257 uint64_t /*Imm*/) {
Dan Gohmanfe905652008-08-21 01:41:07 +00001258 return 0;
1259}
1260
Owen Anderson9f944592009-08-11 20:47:22 +00001261unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001262 unsigned,
1263 unsigned /*Op0*/, bool /*Op0IsKill*/,
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001264 const ConstantFP * /*FPImm*/) {
Dan Gohman5ca269e2008-08-27 01:09:54 +00001265 return 0;
1266}
1267
Owen Anderson9f944592009-08-11 20:47:22 +00001268unsigned FastISel::FastEmit_rri(MVT, MVT,
Dan Gohman404a9842010-01-05 22:26:32 +00001269 unsigned,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001270 unsigned /*Op0*/, bool /*Op0IsKill*/,
1271 unsigned /*Op1*/, bool /*Op1IsKill*/,
Dan Gohmanfe905652008-08-21 01:41:07 +00001272 uint64_t /*Imm*/) {
Evan Cheng864fcc12008-08-20 22:45:34 +00001273 return 0;
1274}
1275
1276/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
1277/// to emit an instruction with an immediate operand using FastEmit_ri.
1278/// If that fails, it materializes the immediate into a register and try
1279/// FastEmit_rr instead.
Dan Gohman404a9842010-01-05 22:26:32 +00001280unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001281 unsigned Op0, bool Op0IsKill,
1282 uint64_t Imm, MVT ImmType) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001283 // If this is a multiply by a power of two, emit this as a shift left.
1284 if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) {
1285 Opcode = ISD::SHL;
1286 Imm = Log2_64(Imm);
Chris Lattner562d6e82011-04-18 06:55:51 +00001287 } else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) {
1288 // div x, 8 -> srl x, 3
1289 Opcode = ISD::SRL;
1290 Imm = Log2_64(Imm);
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001291 }
Owen Andersondd450b82011-04-22 23:38:06 +00001292
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001293 // Horrible hack (to be removed), check to make sure shift amounts are
1294 // in-range.
1295 if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) &&
1296 Imm >= VT.getSizeInBits())
1297 return 0;
Owen Andersondd450b82011-04-22 23:38:06 +00001298
Evan Cheng864fcc12008-08-20 22:45:34 +00001299 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001300 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
Evan Cheng864fcc12008-08-20 22:45:34 +00001301 if (ResultReg != 0)
1302 return ResultReg;
Owen Anderson8dd01cc2008-08-25 23:58:18 +00001303 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Eli Friedman4105ed12011-04-29 23:34:52 +00001304 if (MaterialReg == 0) {
1305 // This is a bit ugly/slow, but failing here means falling out of
1306 // fast-isel, which would be very slow.
Chris Lattner229907c2011-07-18 04:54:35 +00001307 IntegerType *ITy = IntegerType::get(FuncInfo.Fn->getContext(),
Eli Friedman4105ed12011-04-29 23:34:52 +00001308 VT.getSizeInBits());
1309 MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm));
Chad Rosierdbac0252013-03-28 23:04:47 +00001310 assert (MaterialReg != 0 && "Unable to materialize imm.");
1311 if (MaterialReg == 0) return 0;
Eli Friedman4105ed12011-04-29 23:34:52 +00001312 }
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001313 return FastEmit_rr(VT, VT, Opcode,
1314 Op0, Op0IsKill,
1315 MaterialReg, /*Kill=*/true);
Dan Gohmanfe905652008-08-21 01:41:07 +00001316}
1317
1318unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
1319 return MRI.createVirtualRegister(RC);
Evan Cheng864fcc12008-08-20 22:45:34 +00001320}
1321
Tim Northover2f553f32014-04-15 13:59:49 +00001322unsigned FastISel::constrainOperandRegClass(const MCInstrDesc &II,
1323 unsigned Op, unsigned OpNum) {
1324 if (TargetRegisterInfo::isVirtualRegister(Op)) {
1325 const TargetRegisterClass *RegClass =
1326 TII.getRegClass(II, OpNum, &TRI, *FuncInfo.MF);
1327 if (!MRI.constrainRegClass(Op, RegClass)) {
1328 // If it's not legal to COPY between the register classes, something
1329 // has gone very wrong before we got here.
1330 unsigned NewOp = createResultReg(RegClass);
1331 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1332 TII.get(TargetOpcode::COPY), NewOp).addReg(Op);
1333 return NewOp;
1334 }
1335 }
1336 return Op;
1337}
1338
Dan Gohmanb2226e22008-08-13 20:19:35 +00001339unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman2471f6c2008-08-20 18:09:38 +00001340 const TargetRegisterClass* RC) {
Dan Gohmanfe905652008-08-21 01:41:07 +00001341 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +00001342 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb2226e22008-08-13 20:19:35 +00001343
Rafael Espindolaea09c592014-02-18 22:05:46 +00001344 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg);
Dan Gohmanb2226e22008-08-13 20:19:35 +00001345 return ResultReg;
1346}
1347
1348unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
1349 const TargetRegisterClass *RC,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001350 unsigned Op0, bool Op0IsKill) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001351 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb2226e22008-08-13 20:19:35 +00001352
Tim Northover2f553f32014-04-15 13:59:49 +00001353 unsigned ResultReg = createResultReg(RC);
1354 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1355
Evan Chenge775d352008-09-08 08:38:20 +00001356 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001357 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001358 .addReg(Op0, Op0IsKill * RegState::Kill);
Evan Chenge775d352008-09-08 08:38:20 +00001359 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001360 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001361 .addReg(Op0, Op0IsKill * RegState::Kill);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001362 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1363 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Chenge775d352008-09-08 08:38:20 +00001364 }
1365
Dan Gohmanb2226e22008-08-13 20:19:35 +00001366 return ResultReg;
1367}
1368
1369unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
1370 const TargetRegisterClass *RC,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001371 unsigned Op0, bool Op0IsKill,
1372 unsigned Op1, bool Op1IsKill) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001373 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb2226e22008-08-13 20:19:35 +00001374
Tim Northover2f553f32014-04-15 13:59:49 +00001375 unsigned ResultReg = createResultReg(RC);
1376 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1377 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1378
Evan Chenge775d352008-09-08 08:38:20 +00001379 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001380 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001381 .addReg(Op0, Op0IsKill * RegState::Kill)
1382 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Chenge775d352008-09-08 08:38:20 +00001383 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001384 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001385 .addReg(Op0, Op0IsKill * RegState::Kill)
1386 .addReg(Op1, Op1IsKill * RegState::Kill);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001387 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1388 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Chenge775d352008-09-08 08:38:20 +00001389 }
Dan Gohmanb2226e22008-08-13 20:19:35 +00001390 return ResultReg;
1391}
Dan Gohmanfe905652008-08-21 01:41:07 +00001392
Owen Anderson68b6b0e2011-05-05 17:59:04 +00001393unsigned FastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
1394 const TargetRegisterClass *RC,
1395 unsigned Op0, bool Op0IsKill,
1396 unsigned Op1, bool Op1IsKill,
1397 unsigned Op2, bool Op2IsKill) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001398 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Anderson68b6b0e2011-05-05 17:59:04 +00001399
Tim Northover2f553f32014-04-15 13:59:49 +00001400 unsigned ResultReg = createResultReg(RC);
1401 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1402 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1403 Op2 = constrainOperandRegClass(II, Op2, II.getNumDefs() + 2);
1404
Owen Anderson68b6b0e2011-05-05 17:59:04 +00001405 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001406 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Owen Anderson68b6b0e2011-05-05 17:59:04 +00001407 .addReg(Op0, Op0IsKill * RegState::Kill)
1408 .addReg(Op1, Op1IsKill * RegState::Kill)
1409 .addReg(Op2, Op2IsKill * RegState::Kill);
1410 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001411 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Owen Anderson68b6b0e2011-05-05 17:59:04 +00001412 .addReg(Op0, Op0IsKill * RegState::Kill)
1413 .addReg(Op1, Op1IsKill * RegState::Kill)
1414 .addReg(Op2, Op2IsKill * RegState::Kill);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001415 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1416 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Owen Anderson68b6b0e2011-05-05 17:59:04 +00001417 }
1418 return ResultReg;
1419}
1420
Dan Gohmanfe905652008-08-21 01:41:07 +00001421unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1422 const TargetRegisterClass *RC,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001423 unsigned Op0, bool Op0IsKill,
1424 uint64_t Imm) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001425 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanfe905652008-08-21 01:41:07 +00001426
Tim Northover2f553f32014-04-15 13:59:49 +00001427 unsigned ResultReg = createResultReg(RC);
1428 RC = TII.getRegClass(II, II.getNumDefs(), &TRI, *FuncInfo.MF);
1429 MRI.constrainRegClass(Op0, RC);
1430
Evan Chenge775d352008-09-08 08:38:20 +00001431 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001432 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001433 .addReg(Op0, Op0IsKill * RegState::Kill)
1434 .addImm(Imm);
Evan Chenge775d352008-09-08 08:38:20 +00001435 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001436 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001437 .addReg(Op0, Op0IsKill * RegState::Kill)
1438 .addImm(Imm);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001439 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1440 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Chenge775d352008-09-08 08:38:20 +00001441 }
Dan Gohmanfe905652008-08-21 01:41:07 +00001442 return ResultReg;
1443}
1444
Owen Anderson66443c02011-03-11 21:33:55 +00001445unsigned FastISel::FastEmitInst_rii(unsigned MachineInstOpcode,
1446 const TargetRegisterClass *RC,
1447 unsigned Op0, bool Op0IsKill,
1448 uint64_t Imm1, uint64_t Imm2) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001449 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Anderson66443c02011-03-11 21:33:55 +00001450
Tim Northover2f553f32014-04-15 13:59:49 +00001451 unsigned ResultReg = createResultReg(RC);
1452 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1453
Owen Anderson66443c02011-03-11 21:33:55 +00001454 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001455 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Owen Anderson66443c02011-03-11 21:33:55 +00001456 .addReg(Op0, Op0IsKill * RegState::Kill)
1457 .addImm(Imm1)
1458 .addImm(Imm2);
1459 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001460 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Owen Anderson66443c02011-03-11 21:33:55 +00001461 .addReg(Op0, Op0IsKill * RegState::Kill)
1462 .addImm(Imm1)
1463 .addImm(Imm2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001464 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1465 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Owen Anderson66443c02011-03-11 21:33:55 +00001466 }
1467 return ResultReg;
1468}
1469
Dan Gohman5ca269e2008-08-27 01:09:54 +00001470unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
1471 const TargetRegisterClass *RC,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001472 unsigned Op0, bool Op0IsKill,
1473 const ConstantFP *FPImm) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001474 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohman5ca269e2008-08-27 01:09:54 +00001475
Tim Northover2f553f32014-04-15 13:59:49 +00001476 unsigned ResultReg = createResultReg(RC);
1477 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1478
Evan Chenge775d352008-09-08 08:38:20 +00001479 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001480 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001481 .addReg(Op0, Op0IsKill * RegState::Kill)
1482 .addFPImm(FPImm);
Evan Chenge775d352008-09-08 08:38:20 +00001483 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001484 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001485 .addReg(Op0, Op0IsKill * RegState::Kill)
1486 .addFPImm(FPImm);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001487 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1488 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Chenge775d352008-09-08 08:38:20 +00001489 }
Dan Gohman5ca269e2008-08-27 01:09:54 +00001490 return ResultReg;
1491}
1492
Dan Gohmanfe905652008-08-21 01:41:07 +00001493unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
1494 const TargetRegisterClass *RC,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001495 unsigned Op0, bool Op0IsKill,
1496 unsigned Op1, bool Op1IsKill,
1497 uint64_t Imm) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001498 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanfe905652008-08-21 01:41:07 +00001499
Tim Northover2f553f32014-04-15 13:59:49 +00001500 unsigned ResultReg = createResultReg(RC);
1501 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1502 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1503
Evan Chenge775d352008-09-08 08:38:20 +00001504 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001505 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001506 .addReg(Op0, Op0IsKill * RegState::Kill)
1507 .addReg(Op1, Op1IsKill * RegState::Kill)
1508 .addImm(Imm);
Evan Chenge775d352008-09-08 08:38:20 +00001509 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001510 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001511 .addReg(Op0, Op0IsKill * RegState::Kill)
1512 .addReg(Op1, Op1IsKill * RegState::Kill)
1513 .addImm(Imm);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001514 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1515 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Chenge775d352008-09-08 08:38:20 +00001516 }
Dan Gohmanfe905652008-08-21 01:41:07 +00001517 return ResultReg;
1518}
Owen Anderson32635db2008-08-25 20:20:32 +00001519
Manman Rene8735522012-06-01 19:33:18 +00001520unsigned FastISel::FastEmitInst_rrii(unsigned MachineInstOpcode,
1521 const TargetRegisterClass *RC,
1522 unsigned Op0, bool Op0IsKill,
1523 unsigned Op1, bool Op1IsKill,
1524 uint64_t Imm1, uint64_t Imm2) {
Manman Rene8735522012-06-01 19:33:18 +00001525 const MCInstrDesc &II = TII.get(MachineInstOpcode);
1526
Tim Northover2f553f32014-04-15 13:59:49 +00001527 unsigned ResultReg = createResultReg(RC);
1528 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1529 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1530
Manman Rene8735522012-06-01 19:33:18 +00001531 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001532 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Manman Rene8735522012-06-01 19:33:18 +00001533 .addReg(Op0, Op0IsKill * RegState::Kill)
1534 .addReg(Op1, Op1IsKill * RegState::Kill)
1535 .addImm(Imm1).addImm(Imm2);
1536 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001537 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Manman Rene8735522012-06-01 19:33:18 +00001538 .addReg(Op0, Op0IsKill * RegState::Kill)
1539 .addReg(Op1, Op1IsKill * RegState::Kill)
1540 .addImm(Imm1).addImm(Imm2);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001541 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1542 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Manman Rene8735522012-06-01 19:33:18 +00001543 }
1544 return ResultReg;
1545}
1546
Owen Anderson32635db2008-08-25 20:20:32 +00001547unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1548 const TargetRegisterClass *RC,
1549 uint64_t Imm) {
1550 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +00001551 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Wesley Peck527da1b2010-11-23 03:31:01 +00001552
Evan Chenge775d352008-09-08 08:38:20 +00001553 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001554 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg).addImm(Imm);
Evan Chenge775d352008-09-08 08:38:20 +00001555 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001556 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addImm(Imm);
1557 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1558 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Chenge775d352008-09-08 08:38:20 +00001559 }
Owen Anderson32635db2008-08-25 20:20:32 +00001560 return ResultReg;
Evan Cheng2c067322008-08-25 22:20:39 +00001561}
Owen Anderson5f57bc22008-08-27 22:30:02 +00001562
Owen Andersondd450b82011-04-22 23:38:06 +00001563unsigned FastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
1564 const TargetRegisterClass *RC,
1565 uint64_t Imm1, uint64_t Imm2) {
1566 unsigned ResultReg = createResultReg(RC);
Evan Cheng6cc775f2011-06-28 19:10:37 +00001567 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Andersondd450b82011-04-22 23:38:06 +00001568
1569 if (II.getNumDefs() >= 1)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001570 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Owen Andersondd450b82011-04-22 23:38:06 +00001571 .addImm(Imm1).addImm(Imm2);
1572 else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001573 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addImm(Imm1).addImm(Imm2);
1574 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1575 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Owen Andersondd450b82011-04-22 23:38:06 +00001576 }
1577 return ResultReg;
1578}
1579
Owen Anderson9f944592009-08-11 20:47:22 +00001580unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001581 unsigned Op0, bool Op0IsKill,
1582 uint32_t Idx) {
Evan Cheng4a0bf662009-01-22 09:10:11 +00001583 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001584 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
1585 "Cannot yet extract from physregs");
Jakob Stoklund Olesen1f1c6ad2012-05-20 06:38:37 +00001586 const TargetRegisterClass *RC = MRI.getRegClass(Op0);
1587 MRI.constrainRegClass(Op0, TRI.getSubClassWithSubReg(RC, Idx));
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001588 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Rafael Espindolaea09c592014-02-18 22:05:46 +00001589 DbgLoc, TII.get(TargetOpcode::COPY), ResultReg)
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001590 .addReg(Op0, getKillRegState(Op0IsKill), Idx);
Owen Anderson5f57bc22008-08-27 22:30:02 +00001591 return ResultReg;
1592}
Dan Gohmanc0bb9592009-03-13 20:42:20 +00001593
1594/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1595/// with all but the least significant bit set to zero.
Dan Gohman1a1b51f2010-05-11 23:54:07 +00001596unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1597 return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
Dan Gohmanc0bb9592009-03-13 20:42:20 +00001598}
Dan Gohmanc594eab2010-04-22 20:46:50 +00001599
1600/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1601/// Emit code to ensure constants are copied into registers when needed.
1602/// Remember the virtual registers that need to be added to the Machine PHI
1603/// nodes as input. We cannot just directly add them, because expansion
1604/// might result in multiple MBB's for one BB. As such, the start of the
1605/// BB might correspond to a different MBB than the end.
1606bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1607 const TerminatorInst *TI = LLVMBB->getTerminator();
1608
1609 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohman87fb4e82010-07-07 16:29:44 +00001610 unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
Dan Gohmanc594eab2010-04-22 20:46:50 +00001611
1612 // Check successor nodes' PHI nodes that expect a constant to be available
1613 // from this block.
1614 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1615 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1616 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohman87fb4e82010-07-07 16:29:44 +00001617 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Dan Gohmanc594eab2010-04-22 20:46:50 +00001618
1619 // If this terminator has multiple identical successors (common for
1620 // switches), only handle each succ once.
1621 if (!SuccsHandled.insert(SuccMBB)) continue;
1622
1623 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1624
1625 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1626 // nodes and Machine PHI nodes, but the incoming operands have not been
1627 // emitted yet.
1628 for (BasicBlock::const_iterator I = SuccBB->begin();
1629 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmane6d40162010-05-07 01:10:20 +00001630
Dan Gohmanc594eab2010-04-22 20:46:50 +00001631 // Ignore dead phi's.
1632 if (PN->use_empty()) continue;
1633
1634 // Only handle legal types. Two interesting things to note here. First,
1635 // by bailing out early, we may leave behind some dead instructions,
1636 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001637 // own moves. Second, this check is necessary because FastISel doesn't
Dan Gohman93f59202010-07-02 00:10:16 +00001638 // use CreateRegs to create registers, so it always creates
Dan Gohmanc594eab2010-04-22 20:46:50 +00001639 // exactly one register for each non-void instruction.
1640 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1641 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
Chad Rosier6d68c7c2012-02-04 00:39:19 +00001642 // Handle integer promotions, though, because they're common and easy.
1643 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Dan Gohmanc594eab2010-04-22 20:46:50 +00001644 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1645 else {
Dan Gohman87fb4e82010-07-07 16:29:44 +00001646 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanc594eab2010-04-22 20:46:50 +00001647 return false;
1648 }
1649 }
1650
1651 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1652
Dan Gohmane6d40162010-05-07 01:10:20 +00001653 // Set the DebugLoc for the copy. Prefer the location of the operand
1654 // if there is one; use the location of the PHI otherwise.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001655 DbgLoc = PN->getDebugLoc();
Dan Gohmane6d40162010-05-07 01:10:20 +00001656 if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
Rafael Espindolaea09c592014-02-18 22:05:46 +00001657 DbgLoc = Inst->getDebugLoc();
Dan Gohmane6d40162010-05-07 01:10:20 +00001658
Dan Gohmanc594eab2010-04-22 20:46:50 +00001659 unsigned Reg = getRegForValue(PHIOp);
1660 if (Reg == 0) {
Dan Gohman87fb4e82010-07-07 16:29:44 +00001661 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanc594eab2010-04-22 20:46:50 +00001662 return false;
1663 }
Dan Gohman87fb4e82010-07-07 16:29:44 +00001664 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Rafael Espindolaea09c592014-02-18 22:05:46 +00001665 DbgLoc = DebugLoc();
Dan Gohmanc594eab2010-04-22 20:46:50 +00001666 }
1667 }
1668
1669 return true;
1670}
Eli Bendersky90dd3e72013-04-19 22:29:18 +00001671
1672bool FastISel::tryToFoldLoad(const LoadInst *LI, const Instruction *FoldInst) {
Eli Benderskye80691d2013-04-19 23:26:18 +00001673 assert(LI->hasOneUse() &&
1674 "tryToFoldLoad expected a LoadInst with a single use");
Eli Bendersky90dd3e72013-04-19 22:29:18 +00001675 // We know that the load has a single use, but don't know what it is. If it
1676 // isn't one of the folded instructions, then we can't succeed here. Handle
1677 // this by scanning the single-use users of the load until we get to FoldInst.
1678 unsigned MaxUsers = 6; // Don't scan down huge single-use chains of instrs.
1679
Chandler Carruthcdf47882014-03-09 03:16:01 +00001680 const Instruction *TheUser = LI->user_back();
Eli Bendersky90dd3e72013-04-19 22:29:18 +00001681 while (TheUser != FoldInst && // Scan up until we find FoldInst.
1682 // Stay in the right block.
1683 TheUser->getParent() == FoldInst->getParent() &&
1684 --MaxUsers) { // Don't scan too far.
1685 // If there are multiple or no uses of this instruction, then bail out.
1686 if (!TheUser->hasOneUse())
1687 return false;
1688
Chandler Carruthcdf47882014-03-09 03:16:01 +00001689 TheUser = TheUser->user_back();
Eli Bendersky90dd3e72013-04-19 22:29:18 +00001690 }
1691
1692 // If we didn't find the fold instruction, then we failed to collapse the
1693 // sequence.
1694 if (TheUser != FoldInst)
1695 return false;
1696
1697 // Don't try to fold volatile loads. Target has to deal with alignment
1698 // constraints.
Eli Benderskye80691d2013-04-19 23:26:18 +00001699 if (LI->isVolatile())
1700 return false;
Eli Bendersky90dd3e72013-04-19 22:29:18 +00001701
1702 // Figure out which vreg this is going into. If there is no assigned vreg yet
1703 // then there actually was no reference to it. Perhaps the load is referenced
1704 // by a dead instruction.
1705 unsigned LoadReg = getRegForValue(LI);
1706 if (LoadReg == 0)
1707 return false;
1708
Eli Benderskye80691d2013-04-19 23:26:18 +00001709 // We can't fold if this vreg has no uses or more than one use. Multiple uses
1710 // may mean that the instruction got lowered to multiple MIs, or the use of
1711 // the loaded value ended up being multiple operands of the result.
1712 if (!MRI.hasOneUse(LoadReg))
1713 return false;
1714
Eli Bendersky90dd3e72013-04-19 22:29:18 +00001715 MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LoadReg);
Owen Anderson16c6bf42014-03-13 23:12:04 +00001716 MachineInstr *User = RI->getParent();
Eli Bendersky90dd3e72013-04-19 22:29:18 +00001717
1718 // Set the insertion point properly. Folding the load can cause generation of
Eli Benderskye80691d2013-04-19 23:26:18 +00001719 // other random instructions (like sign extends) for addressing modes; make
Eli Bendersky90dd3e72013-04-19 22:29:18 +00001720 // sure they get inserted in a logical place before the new instruction.
1721 FuncInfo.InsertPt = User;
1722 FuncInfo.MBB = User->getParent();
1723
1724 // Ask the target to try folding the load.
1725 return tryToFoldLoadIntoMI(User, RI.getOperandNo(), LI);
1726}
1727
Bob Wilson9f3e6b22013-11-15 19:09:27 +00001728bool FastISel::canFoldAddIntoGEP(const User *GEP, const Value *Add) {
1729 // Must be an add.
1730 if (!isa<AddOperator>(Add))
1731 return false;
1732 // Type size needs to match.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001733 if (DL.getTypeSizeInBits(GEP->getType()) !=
1734 DL.getTypeSizeInBits(Add->getType()))
Bob Wilson9f3e6b22013-11-15 19:09:27 +00001735 return false;
1736 // Must be in the same basic block.
1737 if (isa<Instruction>(Add) &&
1738 FuncInfo.MBBMap[cast<Instruction>(Add)->getParent()] != FuncInfo.MBB)
1739 return false;
1740 // Must have a constant operand.
1741 return isa<ConstantInt>(cast<AddOperator>(Add)->getOperand(1));
1742}
Eli Bendersky90dd3e72013-04-19 22:29:18 +00001743
Juergen Ributzka349777d2014-06-12 23:27:57 +00001744MachineMemOperand *
1745FastISel::createMachineMemOperandFor(const Instruction *I) const {
1746 const Value *Ptr;
1747 Type *ValTy;
1748 unsigned Alignment;
1749 unsigned Flags;
1750 bool IsVolatile;
1751
1752 if (const auto *LI = dyn_cast<LoadInst>(I)) {
1753 Alignment = LI->getAlignment();
1754 IsVolatile = LI->isVolatile();
1755 Flags = MachineMemOperand::MOLoad;
1756 Ptr = LI->getPointerOperand();
1757 ValTy = LI->getType();
1758 } else if (const auto *SI = dyn_cast<StoreInst>(I)) {
1759 Alignment = SI->getAlignment();
1760 IsVolatile = SI->isVolatile();
1761 Flags = MachineMemOperand::MOStore;
1762 Ptr = SI->getPointerOperand();
1763 ValTy = SI->getValueOperand()->getType();
1764 } else {
1765 return nullptr;
1766 }
1767
1768 bool IsNonTemporal = I->getMetadata("nontemporal") != nullptr;
1769 bool IsInvariant = I->getMetadata("invariant.load") != nullptr;
1770 const MDNode *TBAAInfo = I->getMetadata(LLVMContext::MD_tbaa);
1771 const MDNode *Ranges = I->getMetadata(LLVMContext::MD_range);
1772
1773 if (Alignment == 0) // Ensure that codegen never sees alignment 0.
1774 Alignment = DL.getABITypeAlignment(ValTy);
1775
1776 unsigned Size = TM.getDataLayout()->getTypeStoreSize(ValTy);
1777
1778 if (IsVolatile)
1779 Flags |= MachineMemOperand::MOVolatile;
1780 if (IsNonTemporal)
1781 Flags |= MachineMemOperand::MONonTemporal;
1782 if (IsInvariant)
1783 Flags |= MachineMemOperand::MOInvariant;
1784
1785 return FuncInfo.MF->getMachineMemOperand(MachinePointerInfo(Ptr), Flags, Size,
1786 Alignment, TBAAInfo, Ranges);
1787}