blob: 99931c14ebcba7c02dec08c366fc331980975a01 [file] [log] [blame]
Dan Gohman3b172f12010-04-22 20:06:42 +00001//===-- FastISel.cpp - Implementation of the FastISel class ---------------===//
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the implementation of the FastISel class.
11//
Dan Gohman5ec9efd2008-09-30 20:48:29 +000012// "Fast" instruction selection is designed to emit very poor code quickly.
13// Also, it is not designed to be able to do much lowering, so most illegal
Chris Lattner44d2a982008-10-13 01:59:13 +000014// types (e.g. i64 on 32-bit targets) and operations are not supported. It is
15// also not intended to be able to do much optimization, except in a few cases
16// where doing optimizations reduces overall compile time. For example, folding
17// constants into immediate fields is often done, because it's cheap and it
18// reduces the number of instructions later phases have to examine.
Dan Gohman5ec9efd2008-09-30 20:48:29 +000019//
20// "Fast" instruction selection is able to fail gracefully and transfer
21// control to the SelectionDAG selector for operations that it doesn't
Chris Lattner44d2a982008-10-13 01:59:13 +000022// support. In many cases, this allows us to avoid duplicating a lot of
Dan Gohman5ec9efd2008-09-30 20:48:29 +000023// the complicated lowering logic that SelectionDAG currently has.
24//
25// The intended use for "fast" instruction selection is "-O0" mode
26// compilation, where the quality of the generated code is irrelevant when
Chris Lattner44d2a982008-10-13 01:59:13 +000027// weighed against the speed at which the code can be generated. Also,
Dan Gohman5ec9efd2008-09-30 20:48:29 +000028// at -O0, the LLVM optimizers are not running, and this makes the
29// compile time of codegen a much higher portion of the overall compile
Chris Lattner44d2a982008-10-13 01:59:13 +000030// time. Despite its limitations, "fast" instruction selection is able to
Dan Gohman5ec9efd2008-09-30 20:48:29 +000031// handle enough code on its own to provide noticeable overall speedups
32// in -O0 compiles.
33//
34// Basic operations are supported in a target-independent way, by reading
35// the same instruction descriptions that the SelectionDAG selector reads,
36// and identifying simple arithmetic operations that can be directly selected
Chris Lattner44d2a982008-10-13 01:59:13 +000037// from simple operators. More complicated operations currently require
Dan Gohman5ec9efd2008-09-30 20:48:29 +000038// target-specific code.
39//
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000040//===----------------------------------------------------------------------===//
41
Chandler Carruthd04a8d42012-12-03 16:50:05 +000042#include "llvm/CodeGen/FastISel.h"
David Blaikie6d9dbd52013-06-16 20:34:15 +000043#include "llvm/ADT/Optional.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000044#include "llvm/ADT/Statistic.h"
45#include "llvm/Analysis/Loads.h"
46#include "llvm/CodeGen/Analysis.h"
47#include "llvm/CodeGen/FunctionLoweringInfo.h"
48#include "llvm/CodeGen/MachineInstrBuilder.h"
49#include "llvm/CodeGen/MachineModuleInfo.h"
50#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000051#include "llvm/IR/DataLayout.h"
Stephen Hines36b56882014-04-23 16:57:46 -070052#include "llvm/IR/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000053#include "llvm/IR/Function.h"
54#include "llvm/IR/GlobalVariable.h"
55#include "llvm/IR/Instructions.h"
56#include "llvm/IR/IntrinsicInst.h"
57#include "llvm/IR/Operator.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000058#include "llvm/Support/Debug.h"
59#include "llvm/Support/ErrorHandling.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000060#include "llvm/Target/TargetInstrInfo.h"
Bob Wilsond49edb72012-08-03 04:06:28 +000061#include "llvm/Target/TargetLibraryInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000062#include "llvm/Target/TargetLowering.h"
Dan Gohmanbb466332008-08-20 21:05:57 +000063#include "llvm/Target/TargetMachine.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000064using namespace llvm;
65
Stephen Hinesdce4a402014-05-29 02:49:00 -070066#define DEBUG_TYPE "isel"
67
Chad Rosieraa5656c2011-11-28 19:59:09 +000068STATISTIC(NumFastIselSuccessIndependent, "Number of insts selected by "
69 "target-independent selector");
70STATISTIC(NumFastIselSuccessTarget, "Number of insts selected by "
71 "target-specific selector");
Chad Rosierae6f2cb2011-11-29 19:40:47 +000072STATISTIC(NumFastIselDead, "Number of dead insts removed on failure");
Chad Rosier053e69a2011-11-16 21:05:28 +000073
Dan Gohman84023e02010-07-10 09:00:22 +000074/// startNewBlock - Set the current block to which generated machine
75/// instructions will be appended, and clear the local CSE map.
76///
77void FastISel::startNewBlock() {
78 LocalValueMap.clear();
79
Jakob Stoklund Olesen1ab111e2013-07-04 04:53:49 +000080 // Instructions are appended to FuncInfo.MBB. If the basic block already
Jakob Stoklund Olesenef22e0e2013-07-04 04:32:39 +000081 // contains labels or copies, use the last instruction as the last local
82 // value.
Stephen Hinesdce4a402014-05-29 02:49:00 -070083 EmitStartPt = nullptr;
Jakob Stoklund Olesenef22e0e2013-07-04 04:32:39 +000084 if (!FuncInfo.MBB->empty())
85 EmitStartPt = &FuncInfo.MBB->back();
Ivan Krasin74af88a2011-08-18 22:06:10 +000086 LastLocalValue = EmitStartPt;
87}
88
Evan Cheng092e5e72013-02-11 01:27:15 +000089bool FastISel::LowerArguments() {
90 if (!FuncInfo.CanLowerReturn)
91 // Fallback to SDISel argument lowering code to deal with sret pointer
92 // parameter.
93 return false;
Stephen Lin155615d2013-07-08 00:37:03 +000094
Evan Cheng092e5e72013-02-11 01:27:15 +000095 if (!FastLowerArguments())
96 return false;
97
David Blaikie19489102013-06-21 22:56:30 +000098 // Enter arguments into ValueMap for uses in non-entry BBs.
Evan Cheng092e5e72013-02-11 01:27:15 +000099 for (Function::const_arg_iterator I = FuncInfo.Fn->arg_begin(),
100 E = FuncInfo.Fn->arg_end(); I != E; ++I) {
David Blaikie19489102013-06-21 22:56:30 +0000101 DenseMap<const Value *, unsigned>::iterator VI = LocalValueMap.find(I);
102 assert(VI != LocalValueMap.end() && "Missed an argument?");
103 FuncInfo.ValueMap[I] = VI->second;
Evan Cheng092e5e72013-02-11 01:27:15 +0000104 }
105 return true;
106}
107
Ivan Krasin74af88a2011-08-18 22:06:10 +0000108void FastISel::flushLocalValueMap() {
109 LocalValueMap.clear();
110 LastLocalValue = EmitStartPt;
111 recomputeInsertPt();
Dan Gohman84023e02010-07-10 09:00:22 +0000112}
113
Dan Gohmana6cb6412010-05-11 23:54:07 +0000114bool FastISel::hasTrivialKill(const Value *V) const {
Dan Gohman7f0d6952010-05-14 22:53:18 +0000115 // Don't consider constants or arguments to have trivial kills.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000116 const Instruction *I = dyn_cast<Instruction>(V);
Dan Gohman7f0d6952010-05-14 22:53:18 +0000117 if (!I)
118 return false;
119
120 // No-op casts are trivially coalesced by fast-isel.
121 if (const CastInst *Cast = dyn_cast<CastInst>(I))
Stephen Hines36b56882014-04-23 16:57:46 -0700122 if (Cast->isNoopCast(DL.getIntPtrType(Cast->getContext())) &&
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000123 !hasTrivialKill(Cast->getOperand(0)))
Dan Gohman7f0d6952010-05-14 22:53:18 +0000124 return false;
125
Chad Rosier22b34cc2011-11-15 23:34:05 +0000126 // GEPs with all zero indices are trivially coalesced by fast-isel.
127 if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I))
128 if (GEP->hasAllZeroIndices() && !hasTrivialKill(GEP->getOperand(0)))
129 return false;
130
Dan Gohman7f0d6952010-05-14 22:53:18 +0000131 // Only instructions with a single use in the same basic block are considered
132 // to have trivial kills.
133 return I->hasOneUse() &&
134 !(I->getOpcode() == Instruction::BitCast ||
135 I->getOpcode() == Instruction::PtrToInt ||
136 I->getOpcode() == Instruction::IntToPtr) &&
Stephen Hines36b56882014-04-23 16:57:46 -0700137 cast<Instruction>(*I->user_begin())->getParent() == I->getParent();
Dan Gohmana6cb6412010-05-11 23:54:07 +0000138}
139
Dan Gohman46510a72010-04-15 01:51:59 +0000140unsigned FastISel::getRegForValue(const Value *V) {
Owen Andersone50ed302009-08-10 22:56:29 +0000141 EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
Dan Gohman4fd55282009-04-07 20:40:11 +0000142 // Don't handle non-simple values in FastISel.
143 if (!RealVT.isSimple())
144 return 0;
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000145
146 // Ignore illegal types. We must do this before looking up the value
147 // in ValueMap because Arguments are given virtual registers regardless
148 // of whether FastISel can handle them.
Owen Anderson825b72b2009-08-11 20:47:22 +0000149 MVT VT = RealVT.getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000150 if (!TLI.isTypeLegal(VT)) {
Eli Friedman76927d732011-05-25 23:49:02 +0000151 // Handle integer promotions, though, because they're common and easy.
152 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Owen Anderson23b9b192009-08-12 00:36:31 +0000153 VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000154 else
155 return 0;
156 }
157
Eric Christopher4e270272012-03-20 01:07:47 +0000158 // Look up the value to see if we already have a register for it.
159 unsigned Reg = lookUpRegForValue(V);
Dan Gohman104e4ce2008-09-03 23:32:19 +0000160 if (Reg != 0)
161 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000162
Dan Gohman97c94b82010-05-06 00:02:14 +0000163 // In bottom-up mode, just create the virtual register which will be used
164 // to hold the value. It will be materialized later.
Dan Gohman84023e02010-07-10 09:00:22 +0000165 if (isa<Instruction>(V) &&
166 (!isa<AllocaInst>(V) ||
167 !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V))))
168 return FuncInfo.InitializeRegForValue(V);
Dan Gohman97c94b82010-05-06 00:02:14 +0000169
Eric Christopher76ad43c2012-10-03 08:10:01 +0000170 SavePoint SaveInsertPt = enterLocalValueArea();
Dan Gohman84023e02010-07-10 09:00:22 +0000171
172 // Materialize the value in a register. Emit any instructions in the
173 // local value area.
174 Reg = materializeRegForValue(V, VT);
175
Eric Christopher76ad43c2012-10-03 08:10:01 +0000176 leaveLocalValueArea(SaveInsertPt);
Dan Gohman84023e02010-07-10 09:00:22 +0000177
178 return Reg;
Dan Gohman1fdc6142010-05-03 23:36:34 +0000179}
180
Eric Christopher44a2c342010-08-17 01:30:33 +0000181/// materializeRegForValue - Helper for getRegForValue. This function is
Dan Gohman1fdc6142010-05-03 23:36:34 +0000182/// called when the value isn't already available in a register and must
183/// be materialized with new instructions.
184unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
185 unsigned Reg = 0;
186
Dan Gohman46510a72010-04-15 01:51:59 +0000187 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000188 if (CI->getValue().getActiveBits() <= 64)
189 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +0000190 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000191 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +0000192 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman1e9e8c32008-10-07 22:03:27 +0000193 // Translate this as an integer zero so that it can be
194 // local-CSE'd with actual integer zeros.
Owen Anderson1d0be152009-08-13 21:58:54 +0000195 Reg =
Stephen Hines36b56882014-04-23 16:57:46 -0700196 getRegForValue(Constant::getNullValue(DL.getIntPtrType(V->getContext())));
Dan Gohman46510a72010-04-15 01:51:59 +0000197 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Eli Friedmanbd125382011-04-28 00:42:03 +0000198 if (CF->isNullValue()) {
Eli Friedman2790ba82011-04-27 22:41:55 +0000199 Reg = TargetMaterializeFloatZero(CF);
200 } else {
201 // Try to emit the constant directly.
202 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
203 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000204
205 if (!Reg) {
Dan Gohman4183e312010-04-13 17:07:06 +0000206 // Try to emit the constant by using an integer constant with a cast.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000207 const APFloat &Flt = CF->getValueAPF();
Owen Andersone50ed302009-08-10 22:56:29 +0000208 EVT IntVT = TLI.getPointerTy();
Dan Gohmanad368ac2008-08-27 18:10:19 +0000209
210 uint64_t x[2];
211 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dale Johannesen23a98552008-10-09 23:00:39 +0000212 bool isExact;
213 (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
Eric Christopherc415af22012-03-20 01:07:56 +0000214 APFloat::rmTowardZero, &isExact);
Dale Johannesen23a98552008-10-09 23:00:39 +0000215 if (isExact) {
Jeffrey Yasskin3ba292d2011-07-18 21:45:40 +0000216 APInt IntVal(IntBitWidth, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000217
Owen Andersone922c022009-07-22 00:24:57 +0000218 unsigned IntegerReg =
Owen Andersoneed707b2009-07-24 23:12:02 +0000219 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000220 if (IntegerReg != 0)
Dan Gohmana6cb6412010-05-11 23:54:07 +0000221 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
222 IntegerReg, /*Kill=*/false);
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000223 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000224 }
Dan Gohman46510a72010-04-15 01:51:59 +0000225 } else if (const Operator *Op = dyn_cast<Operator>(V)) {
Dan Gohman20d4be12010-07-01 02:58:57 +0000226 if (!SelectOperator(Op, Op->getOpcode()))
227 if (!isa<Instruction>(Op) ||
228 !TargetSelectInstruction(cast<Instruction>(Op)))
229 return 0;
Dan Gohman37db6cd2010-06-21 14:17:46 +0000230 Reg = lookUpRegForValue(Op);
Dan Gohman205d9252008-08-28 21:19:07 +0000231 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +0000232 Reg = createResultReg(TLI.getRegClassFor(VT));
Stephen Hines36b56882014-04-23 16:57:46 -0700233 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohman84023e02010-07-10 09:00:22 +0000234 TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000235 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000236
Dan Gohmandceffe62008-09-25 01:28:51 +0000237 // If target-independent code couldn't handle the value, give target-specific
238 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +0000239 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000240 Reg = TargetMaterializeConstant(cast<Constant>(V));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000241
Dan Gohman2ff7fd12008-09-19 22:16:54 +0000242 // Don't cache constant materializations in the general ValueMap.
243 // To do so would require tracking what uses they dominate.
Dan Gohman84023e02010-07-10 09:00:22 +0000244 if (Reg != 0) {
Dan Gohmandceffe62008-09-25 01:28:51 +0000245 LocalValueMap[V] = Reg;
Dan Gohman84023e02010-07-10 09:00:22 +0000246 LastLocalValue = MRI.getVRegDef(Reg);
247 }
Dan Gohman104e4ce2008-09-03 23:32:19 +0000248 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +0000249}
250
Dan Gohman46510a72010-04-15 01:51:59 +0000251unsigned FastISel::lookUpRegForValue(const Value *V) {
Evan Cheng59fbc802008-09-09 01:26:59 +0000252 // Look up the value to see if we already have a register for it. We
253 // cache values defined by Instructions across blocks, and other values
254 // only locally. This is because Instructions already have the SSA
Dan Gohman1fdc6142010-05-03 23:36:34 +0000255 // def-dominates-use requirement enforced.
Dan Gohmana4160c32010-07-07 16:29:44 +0000256 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
257 if (I != FuncInfo.ValueMap.end())
Dan Gohman3193a682010-06-21 14:21:47 +0000258 return I->second;
Eric Christopher76ad43c2012-10-03 08:10:01 +0000259 return LocalValueMap[V];
Evan Cheng59fbc802008-09-09 01:26:59 +0000260}
261
Owen Andersoncc54e762008-08-30 00:38:46 +0000262/// UpdateValueMap - Update the value map to include the new mapping for this
263/// instruction, or insert an extra copy to get the result in a previous
264/// determined register.
265/// NOTE: This is only necessary because we might select a block that uses
266/// a value before we select the block that defines the value. It might be
267/// possible to fix this by selecting blocks in reverse postorder.
Eli Friedman482feb32011-05-16 21:06:17 +0000268void FastISel::UpdateValueMap(const Value *I, unsigned Reg, unsigned NumRegs) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000269 if (!isa<Instruction>(I)) {
270 LocalValueMap[I] = Reg;
Eli Friedman482feb32011-05-16 21:06:17 +0000271 return;
Dan Gohman40b189e2008-09-05 18:18:20 +0000272 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000273
Dan Gohmana4160c32010-07-07 16:29:44 +0000274 unsigned &AssignedReg = FuncInfo.ValueMap[I];
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000275 if (AssignedReg == 0)
Dan Gohman84023e02010-07-10 09:00:22 +0000276 // Use the new register.
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000277 AssignedReg = Reg;
Chris Lattner36e39462009-04-12 07:46:30 +0000278 else if (Reg != AssignedReg) {
Dan Gohman84023e02010-07-10 09:00:22 +0000279 // Arrange for uses of AssignedReg to be replaced by uses of Reg.
Eli Friedman482feb32011-05-16 21:06:17 +0000280 for (unsigned i = 0; i < NumRegs; i++)
281 FuncInfo.RegFixups[AssignedReg+i] = Reg+i;
Dan Gohman84023e02010-07-10 09:00:22 +0000282
283 AssignedReg = Reg;
Chris Lattnerc5040ab2009-04-12 07:45:01 +0000284 }
Owen Andersoncc54e762008-08-30 00:38:46 +0000285}
286
Dan Gohmana6cb6412010-05-11 23:54:07 +0000287std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000288 unsigned IdxN = getRegForValue(Idx);
289 if (IdxN == 0)
290 // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmana6cb6412010-05-11 23:54:07 +0000291 return std::pair<unsigned, bool>(0, false);
292
293 bool IdxNIsKill = hasTrivialKill(Idx);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000294
295 // If the index is smaller or larger than intptr_t, truncate or extend it.
Owen Anderson766b5ef2009-08-11 21:59:30 +0000296 MVT PtrVT = TLI.getPointerTy();
Owen Andersone50ed302009-08-10 22:56:29 +0000297 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000298 if (IdxVT.bitsLT(PtrVT)) {
299 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND,
300 IdxN, IdxNIsKill);
301 IdxNIsKill = true;
302 }
303 else if (IdxVT.bitsGT(PtrVT)) {
304 IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE,
305 IdxN, IdxNIsKill);
306 IdxNIsKill = true;
307 }
308 return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000309}
310
Dan Gohman84023e02010-07-10 09:00:22 +0000311void FastISel::recomputeInsertPt() {
312 if (getLastLocalValue()) {
313 FuncInfo.InsertPt = getLastLocalValue();
Dan Gohmanc6e59b72010-07-19 22:48:56 +0000314 FuncInfo.MBB = FuncInfo.InsertPt->getParent();
Dan Gohman84023e02010-07-10 09:00:22 +0000315 ++FuncInfo.InsertPt;
316 } else
317 FuncInfo.InsertPt = FuncInfo.MBB->getFirstNonPHI();
318
319 // Now skip past any EH_LABELs, which must remain at the beginning.
320 while (FuncInfo.InsertPt != FuncInfo.MBB->end() &&
321 FuncInfo.InsertPt->getOpcode() == TargetOpcode::EH_LABEL)
322 ++FuncInfo.InsertPt;
323}
324
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000325void FastISel::removeDeadCode(MachineBasicBlock::iterator I,
326 MachineBasicBlock::iterator E) {
327 assert (I && E && std::distance(I, E) > 0 && "Invalid iterator!");
328 while (I != E) {
329 MachineInstr *Dead = &*I;
330 ++I;
331 Dead->eraseFromParent();
Jan Wen Voungfa785cb2013-03-08 22:56:31 +0000332 ++NumFastIselDead;
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000333 }
334 recomputeInsertPt();
335}
336
Eric Christopher76ad43c2012-10-03 08:10:01 +0000337FastISel::SavePoint FastISel::enterLocalValueArea() {
Dan Gohman84023e02010-07-10 09:00:22 +0000338 MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt;
Stephen Hines36b56882014-04-23 16:57:46 -0700339 DebugLoc OldDL = DbgLoc;
Dan Gohman84023e02010-07-10 09:00:22 +0000340 recomputeInsertPt();
Stephen Hines36b56882014-04-23 16:57:46 -0700341 DbgLoc = DebugLoc();
Eric Christopher76ad43c2012-10-03 08:10:01 +0000342 SavePoint SP = { OldInsertPt, OldDL };
343 return SP;
Dan Gohman84023e02010-07-10 09:00:22 +0000344}
345
Eric Christopher76ad43c2012-10-03 08:10:01 +0000346void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) {
Dan Gohman84023e02010-07-10 09:00:22 +0000347 if (FuncInfo.InsertPt != FuncInfo.MBB->begin())
Stephen Hines36b56882014-04-23 16:57:46 -0700348 LastLocalValue = std::prev(FuncInfo.InsertPt);
Dan Gohman84023e02010-07-10 09:00:22 +0000349
350 // Restore the previous insert position.
Eric Christopher76ad43c2012-10-03 08:10:01 +0000351 FuncInfo.InsertPt = OldInsertPt.InsertPt;
Stephen Hines36b56882014-04-23 16:57:46 -0700352 DbgLoc = OldInsertPt.DL;
Dan Gohman84023e02010-07-10 09:00:22 +0000353}
354
Dan Gohmanbdedd442008-08-20 00:11:48 +0000355/// SelectBinaryOp - Select and emit code for a binary operator instruction,
356/// which has an opcode which directly corresponds to the given ISD opcode.
357///
Dan Gohman46510a72010-04-15 01:51:59 +0000358bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000359 EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000360 if (VT == MVT::Other || !VT.isSimple())
Dan Gohmanbdedd442008-08-20 00:11:48 +0000361 // Unhandled type. Halt "fast" selection and bail.
362 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000363
Dan Gohmanb71fea22008-08-26 20:52:40 +0000364 // We only handle legal types. For example, on x86-32 the instruction
365 // selector contains all of the 64-bit instructions from x86-64,
366 // under the assumption that i64 won't be used if the target doesn't
367 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000368 if (!TLI.isTypeLegal(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000369 // MVT::i1 is special. Allow AND, OR, or XOR because they
Dan Gohman638c6832008-09-05 18:44:22 +0000370 // don't require additional zeroing, which makes them easy.
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 if (VT == MVT::i1 &&
Dan Gohman5dd9c2e2008-09-25 17:22:52 +0000372 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
373 ISDOpcode == ISD::XOR))
Owen Anderson23b9b192009-08-12 00:36:31 +0000374 VT = TLI.getTypeToTransformTo(I->getContext(), VT);
Dan Gohman638c6832008-09-05 18:44:22 +0000375 else
376 return false;
377 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000378
Chris Lattnerfff65b32011-04-17 01:16:47 +0000379 // Check if the first operand is a constant, and handle it as "ri". At -O0,
380 // we don't have anything that canonicalizes operand order.
381 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(0)))
382 if (isa<Instruction>(I) && cast<Instruction>(I)->isCommutative()) {
383 unsigned Op1 = getRegForValue(I->getOperand(1));
384 if (Op1 == 0) return false;
385
386 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
Owen Andersond74ea772011-04-22 23:38:06 +0000387
Chris Lattner602fc062011-04-17 20:23:29 +0000388 unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op1,
389 Op1IsKill, CI->getZExtValue(),
390 VT.getSimpleVT());
391 if (ResultReg == 0) return false;
Owen Andersond74ea772011-04-22 23:38:06 +0000392
Chris Lattner602fc062011-04-17 20:23:29 +0000393 // We successfully emitted code for the given LLVM Instruction.
394 UpdateValueMap(I, ResultReg);
395 return true;
Chris Lattnerfff65b32011-04-17 01:16:47 +0000396 }
Owen Andersond74ea772011-04-22 23:38:06 +0000397
398
Dan Gohman3df24e62008-09-03 23:12:08 +0000399 unsigned Op0 = getRegForValue(I->getOperand(0));
Chris Lattner602fc062011-04-17 20:23:29 +0000400 if (Op0 == 0) // Unhandled operand. Halt "fast" selection and bail.
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000401 return false;
402
Dan Gohmana6cb6412010-05-11 23:54:07 +0000403 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
404
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000405 // Check if the second operand is a constant and handle it appropriately.
406 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Chris Lattner602fc062011-04-17 20:23:29 +0000407 uint64_t Imm = CI->getZExtValue();
Owen Andersond74ea772011-04-22 23:38:06 +0000408
Chris Lattnerf051c1a2011-04-18 07:00:40 +0000409 // Transform "sdiv exact X, 8" -> "sra X, 3".
410 if (ISDOpcode == ISD::SDIV && isa<BinaryOperator>(I) &&
411 cast<BinaryOperator>(I)->isExact() &&
412 isPowerOf2_64(Imm)) {
413 Imm = Log2_64(Imm);
414 ISDOpcode = ISD::SRA;
415 }
Owen Andersond74ea772011-04-22 23:38:06 +0000416
Chad Rosier544b9b42012-03-22 00:21:17 +0000417 // Transform "urem x, pow2" -> "and x, pow2-1".
418 if (ISDOpcode == ISD::UREM && isa<BinaryOperator>(I) &&
419 isPowerOf2_64(Imm)) {
420 --Imm;
421 ISDOpcode = ISD::AND;
422 }
423
Chris Lattner602fc062011-04-17 20:23:29 +0000424 unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0,
425 Op0IsKill, Imm, VT.getSimpleVT());
426 if (ResultReg == 0) return false;
Owen Andersond74ea772011-04-22 23:38:06 +0000427
Chris Lattner602fc062011-04-17 20:23:29 +0000428 // We successfully emitted code for the given LLVM Instruction.
429 UpdateValueMap(I, ResultReg);
430 return true;
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000431 }
432
Dan Gohman10df0fa2008-08-27 01:09:54 +0000433 // Check if the second operand is a constant float.
434 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000435 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000436 ISDOpcode, Op0, Op0IsKill, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000437 if (ResultReg != 0) {
438 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000439 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000440 return true;
441 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000442 }
443
Dan Gohman3df24e62008-09-03 23:12:08 +0000444 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000445 if (Op1 == 0)
446 // Unhandled operand. Halt "fast" selection and bail.
447 return false;
448
Dan Gohmana6cb6412010-05-11 23:54:07 +0000449 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
450
Dan Gohmanad368ac2008-08-27 18:10:19 +0000451 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000452 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000453 ISDOpcode,
454 Op0, Op0IsKill,
455 Op1, Op1IsKill);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000456 if (ResultReg == 0)
457 // Target-specific code wasn't able to find a machine opcode for
458 // the given ISD opcode and type. Halt "fast" selection and bail.
459 return false;
460
Dan Gohman8014e862008-08-20 00:23:20 +0000461 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000462 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000463 return true;
464}
465
Dan Gohman46510a72010-04-15 01:51:59 +0000466bool FastISel::SelectGetElementPtr(const User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000467 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000468 if (N == 0)
469 // Unhandled operand. Halt "fast" selection and bail.
470 return false;
471
Dan Gohmana6cb6412010-05-11 23:54:07 +0000472 bool NIsKill = hasTrivialKill(I->getOperand(0));
473
Chad Rosier478b06c2011-11-17 07:15:58 +0000474 // Keep a running tab of the total offset to coalesce multiple N = N + Offset
475 // into a single N = N + TotalOffset.
476 uint64_t TotalOffs = 0;
477 // FIXME: What's a good SWAG number for MaxOffs?
478 uint64_t MaxOffs = 2048;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000479 Type *Ty = I->getOperand(0)->getType();
Owen Anderson825b72b2009-08-11 20:47:22 +0000480 MVT VT = TLI.getPointerTy();
Dan Gohman46510a72010-04-15 01:51:59 +0000481 for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
482 E = I->op_end(); OI != E; ++OI) {
483 const Value *Idx = *OI;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000484 if (StructType *StTy = dyn_cast<StructType>(Ty)) {
Evan Cheng83785c82008-08-20 22:45:34 +0000485 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
486 if (Field) {
487 // N = N + Offset
Stephen Hines36b56882014-04-23 16:57:46 -0700488 TotalOffs += DL.getStructLayout(StTy)->getElementOffset(Field);
Chad Rosier478b06c2011-11-17 07:15:58 +0000489 if (TotalOffs >= MaxOffs) {
490 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
491 if (N == 0)
492 // Unhandled operand. Halt "fast" selection and bail.
493 return false;
494 NIsKill = true;
495 TotalOffs = 0;
496 }
Evan Cheng83785c82008-08-20 22:45:34 +0000497 }
498 Ty = StTy->getElementType(Field);
499 } else {
500 Ty = cast<SequentialType>(Ty)->getElementType();
501
502 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +0000503 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +0000504 if (CI->isZero()) continue;
Chad Rosier478b06c2011-11-17 07:15:58 +0000505 // N = N + Offset
Chad Rosier6016a4a2012-07-06 17:44:22 +0000506 TotalOffs +=
Stephen Hines36b56882014-04-23 16:57:46 -0700507 DL.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Chad Rosier478b06c2011-11-17 07:15:58 +0000508 if (TotalOffs >= MaxOffs) {
509 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
510 if (N == 0)
511 // Unhandled operand. Halt "fast" selection and bail.
512 return false;
513 NIsKill = true;
514 TotalOffs = 0;
515 }
516 continue;
517 }
518 if (TotalOffs) {
519 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000520 if (N == 0)
521 // Unhandled operand. Halt "fast" selection and bail.
522 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000523 NIsKill = true;
Chad Rosier478b06c2011-11-17 07:15:58 +0000524 TotalOffs = 0;
Evan Cheng83785c82008-08-20 22:45:34 +0000525 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000526
Evan Cheng83785c82008-08-20 22:45:34 +0000527 // N = N + Idx * ElementSize;
Stephen Hines36b56882014-04-23 16:57:46 -0700528 uint64_t ElementSize = DL.getTypeAllocSize(Ty);
Dan Gohmana6cb6412010-05-11 23:54:07 +0000529 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
530 unsigned IdxN = Pair.first;
531 bool IdxNIsKill = Pair.second;
Evan Cheng83785c82008-08-20 22:45:34 +0000532 if (IdxN == 0)
533 // Unhandled operand. Halt "fast" selection and bail.
534 return false;
535
Dan Gohman80bc6e22008-08-26 20:57:08 +0000536 if (ElementSize != 1) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000537 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000538 if (IdxN == 0)
539 // Unhandled operand. Halt "fast" selection and bail.
540 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000541 IdxNIsKill = true;
Dan Gohman80bc6e22008-08-26 20:57:08 +0000542 }
Dan Gohmana6cb6412010-05-11 23:54:07 +0000543 N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
Evan Cheng83785c82008-08-20 22:45:34 +0000544 if (N == 0)
545 // Unhandled operand. Halt "fast" selection and bail.
546 return false;
547 }
548 }
Chad Rosier478b06c2011-11-17 07:15:58 +0000549 if (TotalOffs) {
550 N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
551 if (N == 0)
552 // Unhandled operand. Halt "fast" selection and bail.
553 return false;
554 }
Evan Cheng83785c82008-08-20 22:45:34 +0000555
556 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000557 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000558 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000559}
560
Dan Gohman46510a72010-04-15 01:51:59 +0000561bool FastISel::SelectCall(const User *I) {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000562 const CallInst *Call = cast<CallInst>(I);
563
564 // Handle simple inline asms.
Dan Gohman9e15d652011-10-12 15:56:56 +0000565 if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000566 // Don't attempt to handle constraints.
567 if (!IA->getConstraintString().empty())
568 return false;
569
570 unsigned ExtraInfo = 0;
571 if (IA->hasSideEffects())
572 ExtraInfo |= InlineAsm::Extra_HasSideEffects;
573 if (IA->isAlignStack())
574 ExtraInfo |= InlineAsm::Extra_IsAlignStack;
575
Stephen Hines36b56882014-04-23 16:57:46 -0700576 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmana61e73b2011-04-26 17:18:34 +0000577 TII.get(TargetOpcode::INLINEASM))
578 .addExternalSymbol(IA->getAsmString().c_str())
579 .addImm(ExtraInfo);
580 return true;
581 }
582
Michael J. Spencerc9c137b2012-02-22 19:06:13 +0000583 MachineModuleInfo &MMI = FuncInfo.MF->getMMI();
584 ComputeUsesVAFloatArgument(*Call, &MMI);
585
Dan Gohmana61e73b2011-04-26 17:18:34 +0000586 const Function *F = Call->getCalledFunction();
Dan Gohman33134c42008-09-25 17:05:24 +0000587 if (!F) return false;
588
Dan Gohman4183e312010-04-13 17:07:06 +0000589 // Handle selected intrinsic function calls.
Chris Lattner832e4942011-04-19 05:52:03 +0000590 switch (F->getIntrinsicID()) {
Dan Gohman33134c42008-09-25 17:05:24 +0000591 default: break;
Chad Rosieraefd36b2012-05-11 23:21:01 +0000592 // At -O0 we don't care about the lifetime intrinsics.
Eric Christopher9b5d6b82012-02-17 23:03:39 +0000593 case Intrinsic::lifetime_start:
594 case Intrinsic::lifetime_end:
Chad Rosierfd065bb2012-07-06 17:33:39 +0000595 // The donothing intrinsic does, well, nothing.
596 case Intrinsic::donothing:
Eric Christopher9b5d6b82012-02-17 23:03:39 +0000597 return true;
Chad Rosierfd065bb2012-07-06 17:33:39 +0000598
Bill Wendling92c1e122009-02-13 02:16:35 +0000599 case Intrinsic::dbg_declare: {
Dan Gohmana61e73b2011-04-26 17:18:34 +0000600 const DbgDeclareInst *DI = cast<DbgDeclareInst>(Call);
Manman Rencbafae62013-06-28 05:43:10 +0000601 DIVariable DIVar(DI->getVariable());
Stephen Lin155615d2013-07-08 00:37:03 +0000602 assert((!DIVar || DIVar.isVariable()) &&
Manman Rencbafae62013-06-28 05:43:10 +0000603 "Variable in DbgDeclareInst should be either null or a DIVariable.");
604 if (!DIVar ||
Eric Christopherbb54d212012-03-15 21:33:44 +0000605 !FuncInfo.MF->getMMI().hasDebugInfo()) {
606 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Devang Patel7e1e31f2009-07-02 22:43:26 +0000607 return true;
Eric Christopherbb54d212012-03-15 21:33:44 +0000608 }
Devang Patel7e1e31f2009-07-02 22:43:26 +0000609
Dan Gohman46510a72010-04-15 01:51:59 +0000610 const Value *Address = DI->getAddress();
Eric Christopherccaea7d2012-03-15 21:33:47 +0000611 if (!Address || isa<UndefValue>(Address)) {
Eric Christopherbb54d212012-03-15 21:33:44 +0000612 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Dale Johannesendc918562010-02-06 02:26:02 +0000613 return true;
Eric Christopherbb54d212012-03-15 21:33:44 +0000614 }
Devang Patel6fe75aa2010-09-14 20:29:31 +0000615
Adrian Prantl35176402013-07-09 20:28:37 +0000616 unsigned Offset = 0;
David Blaikie6d9dbd52013-06-16 20:34:15 +0000617 Optional<MachineOperand> Op;
618 if (const Argument *Arg = dyn_cast<Argument>(Address))
Devang Patel9aee3352011-09-08 22:59:09 +0000619 // Some arguments' frame index is recorded during argument lowering.
Adrian Prantl35176402013-07-09 20:28:37 +0000620 Offset = FuncInfo.getArgumentFrameIndex(Arg);
621 if (Offset)
622 Op = MachineOperand::CreateFI(Offset);
David Blaikie6d9dbd52013-06-16 20:34:15 +0000623 if (!Op)
624 if (unsigned Reg = lookUpRegForValue(Address))
625 Op = MachineOperand::CreateReg(Reg, false);
Eric Christopher8c5293c2012-03-20 01:07:58 +0000626
Bill Wendling84364a42012-03-30 00:02:55 +0000627 // If we have a VLA that has a "use" in a metadata node that's then used
628 // here but it has no other uses, then we have a problem. E.g.,
629 //
630 // int foo (const int *x) {
631 // char a[*x];
632 // return 0;
633 // }
634 //
635 // If we assign 'a' a vreg and fast isel later on has to use the selection
636 // DAG isel, it will want to copy the value to the vreg. However, there are
637 // no uses, which goes counter to what selection DAG isel expects.
David Blaikie6d9dbd52013-06-16 20:34:15 +0000638 if (!Op && !Address->use_empty() && isa<Instruction>(Address) &&
Eric Christopher8c5293c2012-03-20 01:07:58 +0000639 (!isa<AllocaInst>(Address) ||
640 !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(Address))))
David Blaikie6d9dbd52013-06-16 20:34:15 +0000641 Op = MachineOperand::CreateReg(FuncInfo.InitializeRegForValue(Address),
Adrian Prantl0a4371a2013-09-18 22:08:59 +0000642 false);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000643
Adrian Prantl0a4371a2013-09-18 22:08:59 +0000644 if (Op) {
Adrian Prantl35176402013-07-09 20:28:37 +0000645 if (Op->isReg()) {
646 Op->setIsDebug(true);
Stephen Hines36b56882014-04-23 16:57:46 -0700647 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
David Blaikie54de36b2013-10-14 20:15:04 +0000648 TII.get(TargetOpcode::DBG_VALUE), false, Op->getReg(), 0,
649 DI->getVariable());
650 } else
Stephen Hines36b56882014-04-23 16:57:46 -0700651 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
David Blaikie54de36b2013-10-14 20:15:04 +0000652 TII.get(TargetOpcode::DBG_VALUE))
653 .addOperand(*Op)
654 .addImm(0)
655 .addMetadata(DI->getVariable());
Adrian Prantl0a4371a2013-09-18 22:08:59 +0000656 } else {
Eric Christopher4476bae2012-03-20 01:07:53 +0000657 // We can't yet handle anything else here because it would require
658 // generating code, thus altering codegen because of debug info.
Adrian Prantl5da4e4f2013-05-22 18:02:19 +0000659 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Adrian Prantl0a4371a2013-09-18 22:08:59 +0000660 }
Dan Gohman33134c42008-09-25 17:05:24 +0000661 return true;
Bill Wendling92c1e122009-02-13 02:16:35 +0000662 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000663 case Intrinsic::dbg_value: {
Dale Johannesen343b42e2010-04-07 01:15:14 +0000664 // This form of DBG_VALUE is target-independent.
Dan Gohmana61e73b2011-04-26 17:18:34 +0000665 const DbgValueInst *DI = cast<DbgValueInst>(Call);
Evan Chenge837dea2011-06-28 19:10:37 +0000666 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dan Gohman46510a72010-04-15 01:51:59 +0000667 const Value *V = DI->getValue();
Dale Johannesen45df7612010-02-26 20:01:55 +0000668 if (!V) {
669 // Currently the optimizer can produce this; insert an undef to
670 // help debugging. Probably the optimizer should not do this.
Stephen Hines36b56882014-04-23 16:57:46 -0700671 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohman84023e02010-07-10 09:00:22 +0000672 .addReg(0U).addImm(DI->getOffset())
673 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000674 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Devang Patel8594d422011-06-24 20:46:11 +0000675 if (CI->getBitWidth() > 64)
Stephen Hines36b56882014-04-23 16:57:46 -0700676 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Devang Patel8594d422011-06-24 20:46:11 +0000677 .addCImm(CI).addImm(DI->getOffset())
678 .addMetadata(DI->getVariable());
Chad Rosier6016a4a2012-07-06 17:44:22 +0000679 else
Stephen Hines36b56882014-04-23 16:57:46 -0700680 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Devang Patel8594d422011-06-24 20:46:11 +0000681 .addImm(CI->getZExtValue()).addImm(DI->getOffset())
682 .addMetadata(DI->getVariable());
Dan Gohman46510a72010-04-15 01:51:59 +0000683 } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Stephen Hines36b56882014-04-23 16:57:46 -0700684 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohman84023e02010-07-10 09:00:22 +0000685 .addFPImm(CF).addImm(DI->getOffset())
686 .addMetadata(DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000687 } else if (unsigned Reg = lookUpRegForValue(V)) {
Adrian Prantl818833f2013-09-16 23:29:03 +0000688 // FIXME: This does not handle register-indirect values at offset 0.
Adrian Prantl35176402013-07-09 20:28:37 +0000689 bool IsIndirect = DI->getOffset() != 0;
Stephen Hines36b56882014-04-23 16:57:46 -0700690 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, IsIndirect,
Adrian Prantl35176402013-07-09 20:28:37 +0000691 Reg, DI->getOffset(), DI->getVariable());
Dale Johannesen45df7612010-02-26 20:01:55 +0000692 } else {
693 // We can't yet handle anything else here because it would require
694 // generating code, thus altering codegen because of debug info.
Adrian Prantl5da4e4f2013-05-22 18:02:19 +0000695 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000696 }
Dale Johannesen45df7612010-02-26 20:01:55 +0000697 return true;
698 }
Eli Friedmand0118a22011-05-14 00:47:51 +0000699 case Intrinsic::objectsize: {
700 ConstantInt *CI = cast<ConstantInt>(Call->getArgOperand(1));
701 unsigned long long Res = CI->isZero() ? -1ULL : 0;
702 Constant *ResCI = ConstantInt::get(Call->getType(), Res);
703 unsigned ResultReg = getRegForValue(ResCI);
704 if (ResultReg == 0)
705 return false;
706 UpdateValueMap(Call, ResultReg);
707 return true;
708 }
Chad Rosier33947b42013-03-07 20:42:17 +0000709 case Intrinsic::expect: {
Chad Rosier4fde76d2013-03-07 21:38:33 +0000710 unsigned ResultReg = getRegForValue(Call->getArgOperand(0));
Nick Lewycky33cdfe92013-03-11 21:44:37 +0000711 if (ResultReg == 0)
712 return false;
Chad Rosier4fde76d2013-03-07 21:38:33 +0000713 UpdateValueMap(Call, ResultReg);
714 return true;
Chad Rosier33947b42013-03-07 20:42:17 +0000715 }
Dan Gohman33134c42008-09-25 17:05:24 +0000716 }
Dan Gohman4183e312010-04-13 17:07:06 +0000717
Ivan Krasin74af88a2011-08-18 22:06:10 +0000718 // Usually, it does not make sense to initialize a value,
719 // make an unrelated function call and use the value, because
720 // it tends to be spilled on the stack. So, we move the pointer
721 // to the last local value to the beginning of the block, so that
722 // all the values which have already been materialized,
723 // appear after the call. It also makes sense to skip intrinsics
724 // since they tend to be inlined.
Pete Cooperb704ffb2013-02-22 01:50:38 +0000725 if (!isa<IntrinsicInst>(Call))
Ivan Krasin74af88a2011-08-18 22:06:10 +0000726 flushLocalValueMap();
727
Dan Gohman4183e312010-04-13 17:07:06 +0000728 // An arbitrary call. Bail.
Dan Gohman33134c42008-09-25 17:05:24 +0000729 return false;
730}
731
Dan Gohman46510a72010-04-15 01:51:59 +0000732bool FastISel::SelectCast(const User *I, unsigned Opcode) {
Owen Andersone50ed302009-08-10 22:56:29 +0000733 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
734 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000735
Owen Anderson825b72b2009-08-11 20:47:22 +0000736 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
737 DstVT == MVT::Other || !DstVT.isSimple())
Owen Andersond0533c92008-08-26 23:46:32 +0000738 // Unhandled type. Halt "fast" selection and bail.
739 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000740
Eli Friedman76927d732011-05-25 23:49:02 +0000741 // Check if the destination type is legal.
Dan Gohman474d3b32009-03-13 23:53:06 +0000742 if (!TLI.isTypeLegal(DstVT))
Eli Friedman76927d732011-05-25 23:49:02 +0000743 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000744
Eli Friedman76927d732011-05-25 23:49:02 +0000745 // Check if the source operand is legal.
Dan Gohman474d3b32009-03-13 23:53:06 +0000746 if (!TLI.isTypeLegal(SrcVT))
Eli Friedman76927d732011-05-25 23:49:02 +0000747 return false;
Dan Gohman474d3b32009-03-13 23:53:06 +0000748
Dan Gohman3df24e62008-09-03 23:12:08 +0000749 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000750 if (!InputReg)
751 // Unhandled operand. Halt "fast" selection and bail.
752 return false;
Dan Gohman14ea1ec2009-03-13 20:42:20 +0000753
Dan Gohmana6cb6412010-05-11 23:54:07 +0000754 bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
755
Owen Andersond0533c92008-08-26 23:46:32 +0000756 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
757 DstVT.getSimpleVT(),
758 Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +0000759 InputReg, InputRegIsKill);
Owen Andersond0533c92008-08-26 23:46:32 +0000760 if (!ResultReg)
761 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000762
Dan Gohman3df24e62008-09-03 23:12:08 +0000763 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000764 return true;
765}
766
Dan Gohman46510a72010-04-15 01:51:59 +0000767bool FastISel::SelectBitCast(const User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000768 // If the bitcast doesn't change the type, just use the operand value.
769 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000770 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000771 if (Reg == 0)
772 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000773 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000774 return true;
775 }
776
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000777 // Bitcasts of other values become reg-reg copies or BITCAST operators.
Patrik Hagglund3d170e62012-12-17 14:30:06 +0000778 EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType());
779 EVT DstEVT = TLI.getValueType(I->getType());
780 if (SrcEVT == MVT::Other || DstEVT == MVT::Other ||
781 !TLI.isTypeLegal(SrcEVT) || !TLI.isTypeLegal(DstEVT))
Owen Andersond0533c92008-08-26 23:46:32 +0000782 // Unhandled type. Halt "fast" selection and bail.
783 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000784
Patrik Hagglund3d170e62012-12-17 14:30:06 +0000785 MVT SrcVT = SrcEVT.getSimpleVT();
786 MVT DstVT = DstEVT.getSimpleVT();
Dan Gohman3df24e62008-09-03 23:12:08 +0000787 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000788 if (Op0 == 0)
789 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000790 return false;
Dan Gohmana6cb6412010-05-11 23:54:07 +0000791
792 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000793
Dan Gohmanad368ac2008-08-27 18:10:19 +0000794 // First, try to perform the bitcast by inserting a reg-reg copy.
795 unsigned ResultReg = 0;
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000796 if (SrcVT == DstVT) {
Craig Topper44d23822012-02-22 05:59:10 +0000797 const TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
798 const TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
Jakob Stoklund Olesene7917bb2010-07-11 05:16:54 +0000799 // Don't attempt a cross-class copy. It will likely fail.
800 if (SrcClass == DstClass) {
801 ResultReg = createResultReg(DstClass);
Stephen Hines36b56882014-04-23 16:57:46 -0700802 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
803 TII.get(TargetOpcode::COPY), ResultReg).addReg(Op0);
Jakob Stoklund Olesene7917bb2010-07-11 05:16:54 +0000804 }
Dan Gohmanad368ac2008-08-27 18:10:19 +0000805 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000806
807 // If the reg-reg copy failed, select a BITCAST opcode.
Dan Gohmanad368ac2008-08-27 18:10:19 +0000808 if (!ResultReg)
Patrik Hagglunda61b17c2012-12-13 06:34:11 +0000809 ResultReg = FastEmit_r(SrcVT, DstVT, ISD::BITCAST, Op0, Op0IsKill);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000810
Dan Gohmanad368ac2008-08-27 18:10:19 +0000811 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000812 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000813
Dan Gohman3df24e62008-09-03 23:12:08 +0000814 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000815 return true;
816}
817
Dan Gohman3df24e62008-09-03 23:12:08 +0000818bool
Dan Gohman46510a72010-04-15 01:51:59 +0000819FastISel::SelectInstruction(const Instruction *I) {
Dan Gohmane8c92dd2010-04-23 15:29:50 +0000820 // Just before the terminator instruction, insert instructions to
821 // feed PHI nodes in successor blocks.
822 if (isa<TerminatorInst>(I))
823 if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
824 return false;
825
Stephen Hines36b56882014-04-23 16:57:46 -0700826 DbgLoc = I->getDebugLoc();
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000827
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000828 MachineBasicBlock::iterator SavedInsertPt = FuncInfo.InsertPt;
829
Bob Wilsond49edb72012-08-03 04:06:28 +0000830 if (const CallInst *Call = dyn_cast<CallInst>(I)) {
831 const Function *F = Call->getCalledFunction();
832 LibFunc::Func Func;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700833
834 // As a special case, don't handle calls to builtin library functions that
835 // may be translated directly to target instructions.
Bob Wilsond49edb72012-08-03 04:06:28 +0000836 if (F && !F->hasLocalLinkage() && F->hasName() &&
837 LibInfo->getLibFunc(F->getName(), Func) &&
Bob Wilson982dc842012-08-03 21:26:24 +0000838 LibInfo->hasOptimizedCodeGen(Func))
Bob Wilsond49edb72012-08-03 04:06:28 +0000839 return false;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700840
841 // Don't handle Intrinsic::trap if a trap funciton is specified.
842 if (F && F->getIntrinsicID() == Intrinsic::trap &&
843 !TM.Options.getTrapFunctionName().empty())
844 return false;
Bob Wilsond49edb72012-08-03 04:06:28 +0000845 }
846
Dan Gohman6e3ff372009-12-05 01:27:58 +0000847 // First, try doing target-independent selection.
Michael Ilseman7dbd34b2013-02-27 19:54:00 +0000848 if (SelectOperator(I, I->getOpcode())) {
Jan Wen Voungfa785cb2013-03-08 22:56:31 +0000849 ++NumFastIselSuccessIndependent;
Stephen Hines36b56882014-04-23 16:57:46 -0700850 DbgLoc = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000851 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000852 }
Chad Rosier6016a4a2012-07-06 17:44:22 +0000853 // Remove dead code. However, ignore call instructions since we've flushed
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000854 // the local value map and recomputed the insert point.
855 if (!isa<CallInst>(I)) {
856 recomputeInsertPt();
857 if (SavedInsertPt != FuncInfo.InsertPt)
858 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
859 }
Dan Gohman6e3ff372009-12-05 01:27:58 +0000860
861 // Next, try calling the target to attempt to handle the instruction.
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000862 SavedInsertPt = FuncInfo.InsertPt;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000863 if (TargetSelectInstruction(I)) {
Jan Wen Voungfa785cb2013-03-08 22:56:31 +0000864 ++NumFastIselSuccessTarget;
Stephen Hines36b56882014-04-23 16:57:46 -0700865 DbgLoc = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000866 return true;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000867 }
Chad Rosierae6f2cb2011-11-29 19:40:47 +0000868 // Check for dead code and remove as necessary.
869 recomputeInsertPt();
870 if (SavedInsertPt != FuncInfo.InsertPt)
871 removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
Dan Gohman6e3ff372009-12-05 01:27:58 +0000872
Stephen Hines36b56882014-04-23 16:57:46 -0700873 DbgLoc = DebugLoc();
Dan Gohman6e3ff372009-12-05 01:27:58 +0000874 return false;
Dan Gohman40b189e2008-09-05 18:18:20 +0000875}
876
Dan Gohmand98d6202008-10-02 22:15:21 +0000877/// FastEmitBranch - Emit an unconditional branch to the given block,
878/// unless it is the immediate (fall-through) successor, and update
879/// the CFG.
880void
Stephen Hines36b56882014-04-23 16:57:46 -0700881FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DbgLoc) {
Eric Christopher18112d82012-04-10 18:18:10 +0000882
Evan Cheng092e5e72013-02-11 01:27:15 +0000883 if (FuncInfo.MBB->getBasicBlock()->size() > 1 &&
884 FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
Eric Christopher18112d82012-04-10 18:18:10 +0000885 // For more accurate line information if this is the only instruction
886 // in the block then emit it, otherwise we have the unconditional
887 // fall-through case, which needs no instructions.
Dan Gohmand98d6202008-10-02 22:15:21 +0000888 } else {
889 // The unconditional branch case.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700890 TII.InsertBranch(*FuncInfo.MBB, MSucc, nullptr,
Stephen Hines36b56882014-04-23 16:57:46 -0700891 SmallVector<MachineOperand, 0>(), DbgLoc);
Dan Gohmand98d6202008-10-02 22:15:21 +0000892 }
Dan Gohman84023e02010-07-10 09:00:22 +0000893 FuncInfo.MBB->addSuccessor(MSucc);
Dan Gohmand98d6202008-10-02 22:15:21 +0000894}
895
Dan Gohman3d45a852009-09-03 22:53:57 +0000896/// SelectFNeg - Emit an FNeg operation.
897///
898bool
Dan Gohman46510a72010-04-15 01:51:59 +0000899FastISel::SelectFNeg(const User *I) {
Dan Gohman3d45a852009-09-03 22:53:57 +0000900 unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
901 if (OpReg == 0) return false;
902
Dan Gohmana6cb6412010-05-11 23:54:07 +0000903 bool OpRegIsKill = hasTrivialKill(I);
904
Dan Gohman4a215a12009-09-11 00:36:43 +0000905 // If the target has ISD::FNEG, use it.
906 EVT VT = TLI.getValueType(I->getType());
907 unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
Dan Gohmana6cb6412010-05-11 23:54:07 +0000908 ISD::FNEG, OpReg, OpRegIsKill);
Dan Gohman4a215a12009-09-11 00:36:43 +0000909 if (ResultReg != 0) {
910 UpdateValueMap(I, ResultReg);
911 return true;
912 }
913
Dan Gohman5e5abb72009-09-11 00:34:46 +0000914 // Bitcast the value to integer, twiddle the sign bit with xor,
915 // and then bitcast it back to floating-point.
Dan Gohman3d45a852009-09-03 22:53:57 +0000916 if (VT.getSizeInBits() > 64) return false;
Dan Gohman5e5abb72009-09-11 00:34:46 +0000917 EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
918 if (!TLI.isTypeLegal(IntVT))
919 return false;
920
921 unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000922 ISD::BITCAST, OpReg, OpRegIsKill);
Dan Gohman5e5abb72009-09-11 00:34:46 +0000923 if (IntReg == 0)
924 return false;
925
Dan Gohmana6cb6412010-05-11 23:54:07 +0000926 unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
927 IntReg, /*Kill=*/true,
Dan Gohman5e5abb72009-09-11 00:34:46 +0000928 UINT64_C(1) << (VT.getSizeInBits()-1),
929 IntVT.getSimpleVT());
930 if (IntResultReg == 0)
931 return false;
932
933 ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000934 ISD::BITCAST, IntResultReg, /*Kill=*/true);
Dan Gohman3d45a852009-09-03 22:53:57 +0000935 if (ResultReg == 0)
936 return false;
937
938 UpdateValueMap(I, ResultReg);
939 return true;
940}
941
Dan Gohman40b189e2008-09-05 18:18:20 +0000942bool
Eli Friedman2586b8f2011-05-16 20:27:46 +0000943FastISel::SelectExtractValue(const User *U) {
944 const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U);
Eli Friedmana4c920d2011-05-16 20:34:53 +0000945 if (!EVI)
Eli Friedman2586b8f2011-05-16 20:27:46 +0000946 return false;
947
Eli Friedman482feb32011-05-16 21:06:17 +0000948 // Make sure we only try to handle extracts with a legal result. But also
949 // allow i1 because it's easy.
Eli Friedman2586b8f2011-05-16 20:27:46 +0000950 EVT RealVT = TLI.getValueType(EVI->getType(), /*AllowUnknown=*/true);
951 if (!RealVT.isSimple())
952 return false;
953 MVT VT = RealVT.getSimpleVT();
Eli Friedman482feb32011-05-16 21:06:17 +0000954 if (!TLI.isTypeLegal(VT) && VT != MVT::i1)
Eli Friedman2586b8f2011-05-16 20:27:46 +0000955 return false;
956
957 const Value *Op0 = EVI->getOperand(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000958 Type *AggTy = Op0->getType();
Eli Friedman2586b8f2011-05-16 20:27:46 +0000959
960 // Get the base result register.
961 unsigned ResultReg;
962 DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(Op0);
963 if (I != FuncInfo.ValueMap.end())
964 ResultReg = I->second;
Eli Friedman0b4d96b2011-06-06 05:46:34 +0000965 else if (isa<Instruction>(Op0))
Eli Friedman2586b8f2011-05-16 20:27:46 +0000966 ResultReg = FuncInfo.InitializeRegForValue(Op0);
Eli Friedman0b4d96b2011-06-06 05:46:34 +0000967 else
968 return false; // fast-isel can't handle aggregate constants at the moment
Eli Friedman2586b8f2011-05-16 20:27:46 +0000969
970 // Get the actual result register, which is an offset from the base register.
Jay Foadfc6d3a42011-07-13 10:26:04 +0000971 unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices());
Eli Friedman2586b8f2011-05-16 20:27:46 +0000972
973 SmallVector<EVT, 4> AggValueVTs;
974 ComputeValueVTs(TLI, AggTy, AggValueVTs);
975
976 for (unsigned i = 0; i < VTIndex; i++)
977 ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]);
978
979 UpdateValueMap(EVI, ResultReg);
980 return true;
981}
982
983bool
Dan Gohman46510a72010-04-15 01:51:59 +0000984FastISel::SelectOperator(const User *I, unsigned Opcode) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000985 switch (Opcode) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000986 case Instruction::Add:
987 return SelectBinaryOp(I, ISD::ADD);
988 case Instruction::FAdd:
989 return SelectBinaryOp(I, ISD::FADD);
990 case Instruction::Sub:
991 return SelectBinaryOp(I, ISD::SUB);
992 case Instruction::FSub:
Dan Gohman3d45a852009-09-03 22:53:57 +0000993 // FNeg is currently represented in LLVM IR as a special case of FSub.
994 if (BinaryOperator::isFNeg(I))
995 return SelectFNeg(I);
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000996 return SelectBinaryOp(I, ISD::FSUB);
997 case Instruction::Mul:
998 return SelectBinaryOp(I, ISD::MUL);
999 case Instruction::FMul:
1000 return SelectBinaryOp(I, ISD::FMUL);
Dan Gohman3df24e62008-09-03 23:12:08 +00001001 case Instruction::SDiv:
1002 return SelectBinaryOp(I, ISD::SDIV);
1003 case Instruction::UDiv:
1004 return SelectBinaryOp(I, ISD::UDIV);
1005 case Instruction::FDiv:
1006 return SelectBinaryOp(I, ISD::FDIV);
1007 case Instruction::SRem:
1008 return SelectBinaryOp(I, ISD::SREM);
1009 case Instruction::URem:
1010 return SelectBinaryOp(I, ISD::UREM);
1011 case Instruction::FRem:
1012 return SelectBinaryOp(I, ISD::FREM);
1013 case Instruction::Shl:
1014 return SelectBinaryOp(I, ISD::SHL);
1015 case Instruction::LShr:
1016 return SelectBinaryOp(I, ISD::SRL);
1017 case Instruction::AShr:
1018 return SelectBinaryOp(I, ISD::SRA);
1019 case Instruction::And:
1020 return SelectBinaryOp(I, ISD::AND);
1021 case Instruction::Or:
1022 return SelectBinaryOp(I, ISD::OR);
1023 case Instruction::Xor:
1024 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001025
Dan Gohman3df24e62008-09-03 23:12:08 +00001026 case Instruction::GetElementPtr:
1027 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +00001028
Dan Gohman3df24e62008-09-03 23:12:08 +00001029 case Instruction::Br: {
Dan Gohman46510a72010-04-15 01:51:59 +00001030 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +00001031
Dan Gohman3df24e62008-09-03 23:12:08 +00001032 if (BI->isUnconditional()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001033 const BasicBlock *LLVMSucc = BI->getSuccessor(0);
Dan Gohmana4160c32010-07-07 16:29:44 +00001034 MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
Stuart Hastings3bf91252010-06-17 22:43:56 +00001035 FastEmitBranch(MSucc, BI->getDebugLoc());
Dan Gohman3df24e62008-09-03 23:12:08 +00001036 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +00001037 }
Dan Gohman3df24e62008-09-03 23:12:08 +00001038
1039 // Conditional branches are not handed yet.
1040 // Halt "fast" selection and bail.
1041 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001042 }
1043
Dan Gohman087c8502008-09-05 01:08:41 +00001044 case Instruction::Unreachable:
Stephen Hinesdce4a402014-05-29 02:49:00 -07001045 if (TM.Options.TrapUnreachable)
1046 return FastEmit_(MVT::Other, MVT::Other, ISD::TRAP) != 0;
1047 else
1048 return true;
Dan Gohman087c8502008-09-05 01:08:41 +00001049
Dan Gohman0586d912008-09-10 20:11:02 +00001050 case Instruction::Alloca:
1051 // FunctionLowering has the static-sized case covered.
Dan Gohmana4160c32010-07-07 16:29:44 +00001052 if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
Dan Gohman0586d912008-09-10 20:11:02 +00001053 return true;
1054
1055 // Dynamic-sized alloca is not handled yet.
1056 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001057
Dan Gohman33134c42008-09-25 17:05:24 +00001058 case Instruction::Call:
1059 return SelectCall(I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001060
Dan Gohman3df24e62008-09-03 23:12:08 +00001061 case Instruction::BitCast:
1062 return SelectBitCast(I);
1063
1064 case Instruction::FPToSI:
1065 return SelectCast(I, ISD::FP_TO_SINT);
1066 case Instruction::ZExt:
1067 return SelectCast(I, ISD::ZERO_EXTEND);
1068 case Instruction::SExt:
1069 return SelectCast(I, ISD::SIGN_EXTEND);
1070 case Instruction::Trunc:
1071 return SelectCast(I, ISD::TRUNCATE);
1072 case Instruction::SIToFP:
1073 return SelectCast(I, ISD::SINT_TO_FP);
1074
1075 case Instruction::IntToPtr: // Deliberate fall-through.
1076 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00001077 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1078 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman3df24e62008-09-03 23:12:08 +00001079 if (DstVT.bitsGT(SrcVT))
1080 return SelectCast(I, ISD::ZERO_EXTEND);
1081 if (DstVT.bitsLT(SrcVT))
1082 return SelectCast(I, ISD::TRUNCATE);
1083 unsigned Reg = getRegForValue(I->getOperand(0));
1084 if (Reg == 0) return false;
1085 UpdateValueMap(I, Reg);
1086 return true;
1087 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001088
Eli Friedman2586b8f2011-05-16 20:27:46 +00001089 case Instruction::ExtractValue:
1090 return SelectExtractValue(I);
1091
Dan Gohmanba5be5c2010-04-20 15:00:41 +00001092 case Instruction::PHI:
1093 llvm_unreachable("FastISel shouldn't visit PHI nodes!");
1094
Dan Gohman3df24e62008-09-03 23:12:08 +00001095 default:
1096 // Unhandled instruction. Halt "fast" selection and bail.
1097 return false;
1098 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001099}
1100
Bob Wilsond49edb72012-08-03 04:06:28 +00001101FastISel::FastISel(FunctionLoweringInfo &funcInfo,
1102 const TargetLibraryInfo *libInfo)
Dan Gohman84023e02010-07-10 09:00:22 +00001103 : FuncInfo(funcInfo),
Dan Gohmana4160c32010-07-07 16:29:44 +00001104 MRI(FuncInfo.MF->getRegInfo()),
1105 MFI(*FuncInfo.MF->getFrameInfo()),
1106 MCP(*FuncInfo.MF->getConstantPool()),
1107 TM(FuncInfo.MF->getTarget()),
Stephen Hines36b56882014-04-23 16:57:46 -07001108 DL(*TM.getDataLayout()),
Dan Gohman22bb3112008-08-22 00:20:26 +00001109 TII(*TM.getInstrInfo()),
Dan Gohmana7a0ed72010-05-05 23:58:35 +00001110 TLI(*TM.getTargetLowering()),
Bob Wilsond49edb72012-08-03 04:06:28 +00001111 TRI(*TM.getRegisterInfo()),
1112 LibInfo(libInfo) {
Dan Gohmanbb466332008-08-20 21:05:57 +00001113}
1114
Dan Gohmane285a742008-08-14 21:51:29 +00001115FastISel::~FastISel() {}
1116
Evan Cheng092e5e72013-02-11 01:27:15 +00001117bool FastISel::FastLowerArguments() {
1118 return false;
1119}
1120
Owen Anderson825b72b2009-08-11 20:47:22 +00001121unsigned FastISel::FastEmit_(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001122 unsigned) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001123 return 0;
1124}
1125
Owen Anderson825b72b2009-08-11 20:47:22 +00001126unsigned FastISel::FastEmit_r(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001127 unsigned,
1128 unsigned /*Op0*/, bool /*Op0IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001129 return 0;
1130}
1131
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001132unsigned FastISel::FastEmit_rr(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001133 unsigned,
1134 unsigned /*Op0*/, bool /*Op0IsKill*/,
1135 unsigned /*Op1*/, bool /*Op1IsKill*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001136 return 0;
1137}
1138
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001139unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +00001140 return 0;
1141}
1142
Owen Anderson825b72b2009-08-11 20:47:22 +00001143unsigned FastISel::FastEmit_f(MVT, MVT,
Dan Gohman46510a72010-04-15 01:51:59 +00001144 unsigned, const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001145 return 0;
1146}
1147
Owen Anderson825b72b2009-08-11 20:47:22 +00001148unsigned FastISel::FastEmit_ri(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001149 unsigned,
1150 unsigned /*Op0*/, bool /*Op0IsKill*/,
Owen Anderson0f84e4e2008-08-25 23:58:18 +00001151 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001152 return 0;
1153}
1154
Owen Anderson825b72b2009-08-11 20:47:22 +00001155unsigned FastISel::FastEmit_rf(MVT, MVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001156 unsigned,
1157 unsigned /*Op0*/, bool /*Op0IsKill*/,
Dan Gohman46510a72010-04-15 01:51:59 +00001158 const ConstantFP * /*FPImm*/) {
Dan Gohman10df0fa2008-08-27 01:09:54 +00001159 return 0;
1160}
1161
Owen Anderson825b72b2009-08-11 20:47:22 +00001162unsigned FastISel::FastEmit_rri(MVT, MVT,
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001163 unsigned,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001164 unsigned /*Op0*/, bool /*Op0IsKill*/,
1165 unsigned /*Op1*/, bool /*Op1IsKill*/,
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001166 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +00001167 return 0;
1168}
1169
1170/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
1171/// to emit an instruction with an immediate operand using FastEmit_ri.
1172/// If that fails, it materializes the immediate into a register and try
1173/// FastEmit_rr instead.
Dan Gohman7c3ecb62010-01-05 22:26:32 +00001174unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001175 unsigned Op0, bool Op0IsKill,
1176 uint64_t Imm, MVT ImmType) {
Chris Lattner602fc062011-04-17 20:23:29 +00001177 // If this is a multiply by a power of two, emit this as a shift left.
1178 if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) {
1179 Opcode = ISD::SHL;
1180 Imm = Log2_64(Imm);
Chris Lattner090ca912011-04-18 06:55:51 +00001181 } else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) {
1182 // div x, 8 -> srl x, 3
1183 Opcode = ISD::SRL;
1184 Imm = Log2_64(Imm);
Chris Lattner602fc062011-04-17 20:23:29 +00001185 }
Owen Andersond74ea772011-04-22 23:38:06 +00001186
Chris Lattner602fc062011-04-17 20:23:29 +00001187 // Horrible hack (to be removed), check to make sure shift amounts are
1188 // in-range.
1189 if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) &&
1190 Imm >= VT.getSizeInBits())
1191 return 0;
Owen Andersond74ea772011-04-22 23:38:06 +00001192
Evan Cheng83785c82008-08-20 22:45:34 +00001193 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001194 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +00001195 if (ResultReg != 0)
1196 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +00001197 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001198 if (MaterialReg == 0) {
1199 // This is a bit ugly/slow, but failing here means falling out of
1200 // fast-isel, which would be very slow.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001201 IntegerType *ITy = IntegerType::get(FuncInfo.Fn->getContext(),
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001202 VT.getSizeInBits());
1203 MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm));
Chad Rosier7ae3bb82013-03-28 23:04:47 +00001204 assert (MaterialReg != 0 && "Unable to materialize imm.");
1205 if (MaterialReg == 0) return 0;
Eli Friedmanb2b03fc2011-04-29 23:34:52 +00001206 }
Dan Gohmana6cb6412010-05-11 23:54:07 +00001207 return FastEmit_rr(VT, VT, Opcode,
1208 Op0, Op0IsKill,
1209 MaterialReg, /*Kill=*/true);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001210}
1211
1212unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
1213 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +00001214}
1215
Stephen Hinesdce4a402014-05-29 02:49:00 -07001216unsigned FastISel::constrainOperandRegClass(const MCInstrDesc &II,
1217 unsigned Op, unsigned OpNum) {
1218 if (TargetRegisterInfo::isVirtualRegister(Op)) {
1219 const TargetRegisterClass *RegClass =
1220 TII.getRegClass(II, OpNum, &TRI, *FuncInfo.MF);
1221 if (!MRI.constrainRegClass(Op, RegClass)) {
1222 // If it's not legal to COPY between the register classes, something
1223 // has gone very wrong before we got here.
1224 unsigned NewOp = createResultReg(RegClass);
1225 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1226 TII.get(TargetOpcode::COPY), NewOp).addReg(Op);
1227 return NewOp;
1228 }
1229 }
1230 return Op;
1231}
1232
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001233unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +00001234 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001235 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001236 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001237
Stephen Hines36b56882014-04-23 16:57:46 -07001238 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001239 return ResultReg;
1240}
1241
1242unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
1243 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001244 unsigned Op0, bool Op0IsKill) {
Evan Chenge837dea2011-06-28 19:10:37 +00001245 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001246
Stephen Hinesdce4a402014-05-29 02:49:00 -07001247 unsigned ResultReg = createResultReg(RC);
1248 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1249
Evan Cheng5960e4e2008-09-08 08:38:20 +00001250 if (II.getNumDefs() >= 1)
Stephen Hines36b56882014-04-23 16:57:46 -07001251 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Dan Gohman84023e02010-07-10 09:00:22 +00001252 .addReg(Op0, Op0IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001253 else {
Stephen Hines36b56882014-04-23 16:57:46 -07001254 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohman84023e02010-07-10 09:00:22 +00001255 .addReg(Op0, Op0IsKill * RegState::Kill);
Stephen Hines36b56882014-04-23 16:57:46 -07001256 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1257 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001258 }
1259
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001260 return ResultReg;
1261}
1262
1263unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
1264 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001265 unsigned Op0, bool Op0IsKill,
1266 unsigned Op1, bool Op1IsKill) {
Evan Chenge837dea2011-06-28 19:10:37 +00001267 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001268
Stephen Hinesdce4a402014-05-29 02:49:00 -07001269 unsigned ResultReg = createResultReg(RC);
1270 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1271 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1272
Evan Cheng5960e4e2008-09-08 08:38:20 +00001273 if (II.getNumDefs() >= 1)
Stephen Hines36b56882014-04-23 16:57:46 -07001274 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001275 .addReg(Op0, Op0IsKill * RegState::Kill)
1276 .addReg(Op1, Op1IsKill * RegState::Kill);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001277 else {
Stephen Hines36b56882014-04-23 16:57:46 -07001278 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001279 .addReg(Op0, Op0IsKill * RegState::Kill)
1280 .addReg(Op1, Op1IsKill * RegState::Kill);
Stephen Hines36b56882014-04-23 16:57:46 -07001281 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1282 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001283 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001284 return ResultReg;
1285}
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001286
Owen Andersond71867a2011-05-05 17:59:04 +00001287unsigned FastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
1288 const TargetRegisterClass *RC,
1289 unsigned Op0, bool Op0IsKill,
1290 unsigned Op1, bool Op1IsKill,
1291 unsigned Op2, bool Op2IsKill) {
Evan Chenge837dea2011-06-28 19:10:37 +00001292 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Andersond71867a2011-05-05 17:59:04 +00001293
Stephen Hinesdce4a402014-05-29 02:49:00 -07001294 unsigned ResultReg = createResultReg(RC);
1295 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1296 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1297 Op2 = constrainOperandRegClass(II, Op2, II.getNumDefs() + 2);
1298
Owen Andersond71867a2011-05-05 17:59:04 +00001299 if (II.getNumDefs() >= 1)
Stephen Hines36b56882014-04-23 16:57:46 -07001300 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Owen Andersond71867a2011-05-05 17:59:04 +00001301 .addReg(Op0, Op0IsKill * RegState::Kill)
1302 .addReg(Op1, Op1IsKill * RegState::Kill)
1303 .addReg(Op2, Op2IsKill * RegState::Kill);
1304 else {
Stephen Hines36b56882014-04-23 16:57:46 -07001305 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Owen Andersond71867a2011-05-05 17:59:04 +00001306 .addReg(Op0, Op0IsKill * RegState::Kill)
1307 .addReg(Op1, Op1IsKill * RegState::Kill)
1308 .addReg(Op2, Op2IsKill * RegState::Kill);
Stephen Hines36b56882014-04-23 16:57:46 -07001309 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1310 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Owen Andersond71867a2011-05-05 17:59:04 +00001311 }
1312 return ResultReg;
1313}
1314
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001315unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1316 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001317 unsigned Op0, bool Op0IsKill,
1318 uint64_t Imm) {
Evan Chenge837dea2011-06-28 19:10:37 +00001319 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001320
Stephen Hinesdce4a402014-05-29 02:49:00 -07001321 unsigned ResultReg = createResultReg(RC);
1322 RC = TII.getRegClass(II, II.getNumDefs(), &TRI, *FuncInfo.MF);
1323 MRI.constrainRegClass(Op0, RC);
1324
Evan Cheng5960e4e2008-09-08 08:38:20 +00001325 if (II.getNumDefs() >= 1)
Stephen Hines36b56882014-04-23 16:57:46 -07001326 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001327 .addReg(Op0, Op0IsKill * RegState::Kill)
1328 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001329 else {
Stephen Hines36b56882014-04-23 16:57:46 -07001330 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001331 .addReg(Op0, Op0IsKill * RegState::Kill)
1332 .addImm(Imm);
Stephen Hines36b56882014-04-23 16:57:46 -07001333 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1334 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001335 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001336 return ResultReg;
1337}
1338
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001339unsigned FastISel::FastEmitInst_rii(unsigned MachineInstOpcode,
1340 const TargetRegisterClass *RC,
1341 unsigned Op0, bool Op0IsKill,
1342 uint64_t Imm1, uint64_t Imm2) {
Evan Chenge837dea2011-06-28 19:10:37 +00001343 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001344
Stephen Hinesdce4a402014-05-29 02:49:00 -07001345 unsigned ResultReg = createResultReg(RC);
1346 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1347
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001348 if (II.getNumDefs() >= 1)
Stephen Hines36b56882014-04-23 16:57:46 -07001349 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001350 .addReg(Op0, Op0IsKill * RegState::Kill)
1351 .addImm(Imm1)
1352 .addImm(Imm2);
1353 else {
Stephen Hines36b56882014-04-23 16:57:46 -07001354 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001355 .addReg(Op0, Op0IsKill * RegState::Kill)
1356 .addImm(Imm1)
1357 .addImm(Imm2);
Stephen Hines36b56882014-04-23 16:57:46 -07001358 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1359 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Owen Anderson2ce5bf12011-03-11 21:33:55 +00001360 }
1361 return ResultReg;
1362}
1363
Dan Gohman10df0fa2008-08-27 01:09:54 +00001364unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
1365 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001366 unsigned Op0, bool Op0IsKill,
1367 const ConstantFP *FPImm) {
Evan Chenge837dea2011-06-28 19:10:37 +00001368 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohman10df0fa2008-08-27 01:09:54 +00001369
Stephen Hinesdce4a402014-05-29 02:49:00 -07001370 unsigned ResultReg = createResultReg(RC);
1371 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1372
Evan Cheng5960e4e2008-09-08 08:38:20 +00001373 if (II.getNumDefs() >= 1)
Stephen Hines36b56882014-04-23 16:57:46 -07001374 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001375 .addReg(Op0, Op0IsKill * RegState::Kill)
1376 .addFPImm(FPImm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001377 else {
Stephen Hines36b56882014-04-23 16:57:46 -07001378 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001379 .addReg(Op0, Op0IsKill * RegState::Kill)
1380 .addFPImm(FPImm);
Stephen Hines36b56882014-04-23 16:57:46 -07001381 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1382 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001383 }
Dan Gohman10df0fa2008-08-27 01:09:54 +00001384 return ResultReg;
1385}
1386
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001387unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
1388 const TargetRegisterClass *RC,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001389 unsigned Op0, bool Op0IsKill,
1390 unsigned Op1, bool Op1IsKill,
1391 uint64_t Imm) {
Evan Chenge837dea2011-06-28 19:10:37 +00001392 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001393
Stephen Hinesdce4a402014-05-29 02:49:00 -07001394 unsigned ResultReg = createResultReg(RC);
1395 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1396 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1397
Evan Cheng5960e4e2008-09-08 08:38:20 +00001398 if (II.getNumDefs() >= 1)
Stephen Hines36b56882014-04-23 16:57:46 -07001399 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001400 .addReg(Op0, Op0IsKill * RegState::Kill)
1401 .addReg(Op1, Op1IsKill * RegState::Kill)
1402 .addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001403 else {
Stephen Hines36b56882014-04-23 16:57:46 -07001404 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Dan Gohmana6cb6412010-05-11 23:54:07 +00001405 .addReg(Op0, Op0IsKill * RegState::Kill)
1406 .addReg(Op1, Op1IsKill * RegState::Kill)
1407 .addImm(Imm);
Stephen Hines36b56882014-04-23 16:57:46 -07001408 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1409 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001410 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +00001411 return ResultReg;
1412}
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001413
Manman Ren68f25572012-06-01 19:33:18 +00001414unsigned FastISel::FastEmitInst_rrii(unsigned MachineInstOpcode,
1415 const TargetRegisterClass *RC,
1416 unsigned Op0, bool Op0IsKill,
1417 unsigned Op1, bool Op1IsKill,
1418 uint64_t Imm1, uint64_t Imm2) {
Manman Ren68f25572012-06-01 19:33:18 +00001419 const MCInstrDesc &II = TII.get(MachineInstOpcode);
1420
Stephen Hinesdce4a402014-05-29 02:49:00 -07001421 unsigned ResultReg = createResultReg(RC);
1422 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1423 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1424
Manman Ren68f25572012-06-01 19:33:18 +00001425 if (II.getNumDefs() >= 1)
Stephen Hines36b56882014-04-23 16:57:46 -07001426 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Manman Ren68f25572012-06-01 19:33:18 +00001427 .addReg(Op0, Op0IsKill * RegState::Kill)
1428 .addReg(Op1, Op1IsKill * RegState::Kill)
1429 .addImm(Imm1).addImm(Imm2);
1430 else {
Stephen Hines36b56882014-04-23 16:57:46 -07001431 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
Manman Ren68f25572012-06-01 19:33:18 +00001432 .addReg(Op0, Op0IsKill * RegState::Kill)
1433 .addReg(Op1, Op1IsKill * RegState::Kill)
1434 .addImm(Imm1).addImm(Imm2);
Stephen Hines36b56882014-04-23 16:57:46 -07001435 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1436 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Manman Ren68f25572012-06-01 19:33:18 +00001437 }
1438 return ResultReg;
1439}
1440
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001441unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1442 const TargetRegisterClass *RC,
1443 uint64_t Imm) {
1444 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001445 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001446
Evan Cheng5960e4e2008-09-08 08:38:20 +00001447 if (II.getNumDefs() >= 1)
Stephen Hines36b56882014-04-23 16:57:46 -07001448 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg).addImm(Imm);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001449 else {
Stephen Hines36b56882014-04-23 16:57:46 -07001450 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addImm(Imm);
1451 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1452 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Evan Cheng5960e4e2008-09-08 08:38:20 +00001453 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +00001454 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +00001455}
Owen Anderson8970f002008-08-27 22:30:02 +00001456
Owen Andersond74ea772011-04-22 23:38:06 +00001457unsigned FastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
1458 const TargetRegisterClass *RC,
1459 uint64_t Imm1, uint64_t Imm2) {
1460 unsigned ResultReg = createResultReg(RC);
Evan Chenge837dea2011-06-28 19:10:37 +00001461 const MCInstrDesc &II = TII.get(MachineInstOpcode);
Owen Andersond74ea772011-04-22 23:38:06 +00001462
1463 if (II.getNumDefs() >= 1)
Stephen Hines36b56882014-04-23 16:57:46 -07001464 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
Owen Andersond74ea772011-04-22 23:38:06 +00001465 .addImm(Imm1).addImm(Imm2);
1466 else {
Stephen Hines36b56882014-04-23 16:57:46 -07001467 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addImm(Imm1).addImm(Imm2);
1468 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1469 TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
Owen Andersond74ea772011-04-22 23:38:06 +00001470 }
1471 return ResultReg;
1472}
1473
Owen Anderson825b72b2009-08-11 20:47:22 +00001474unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
Dan Gohmana6cb6412010-05-11 23:54:07 +00001475 unsigned Op0, bool Op0IsKill,
1476 uint32_t Idx) {
Evan Cheng536ab132009-01-22 09:10:11 +00001477 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001478 assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
1479 "Cannot yet extract from physregs");
Jakob Stoklund Olesenee0d5d42012-05-20 06:38:37 +00001480 const TargetRegisterClass *RC = MRI.getRegClass(Op0);
1481 MRI.constrainRegClass(Op0, TRI.getSubClassWithSubReg(RC, Idx));
Dan Gohman84023e02010-07-10 09:00:22 +00001482 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Stephen Hines36b56882014-04-23 16:57:46 -07001483 DbgLoc, TII.get(TargetOpcode::COPY), ResultReg)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001484 .addReg(Op0, getKillRegState(Op0IsKill), Idx);
Owen Anderson8970f002008-08-27 22:30:02 +00001485 return ResultReg;
1486}
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001487
1488/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1489/// with all but the least significant bit set to zero.
Dan Gohmana6cb6412010-05-11 23:54:07 +00001490unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1491 return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
Dan Gohman14ea1ec2009-03-13 20:42:20 +00001492}
Dan Gohmanf81eca02010-04-22 20:46:50 +00001493
1494/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1495/// Emit code to ensure constants are copied into registers when needed.
1496/// Remember the virtual registers that need to be added to the Machine PHI
1497/// nodes as input. We cannot just directly add them, because expansion
1498/// might result in multiple MBB's for one BB. As such, the start of the
1499/// BB might correspond to a different MBB than the end.
1500bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1501 const TerminatorInst *TI = LLVMBB->getTerminator();
1502
1503 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohmana4160c32010-07-07 16:29:44 +00001504 unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001505
1506 // Check successor nodes' PHI nodes that expect a constant to be available
1507 // from this block.
1508 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1509 const BasicBlock *SuccBB = TI->getSuccessor(succ);
1510 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmana4160c32010-07-07 16:29:44 +00001511 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Dan Gohmanf81eca02010-04-22 20:46:50 +00001512
1513 // If this terminator has multiple identical successors (common for
1514 // switches), only handle each succ once.
1515 if (!SuccsHandled.insert(SuccMBB)) continue;
1516
1517 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1518
1519 // At this point we know that there is a 1-1 correspondence between LLVM PHI
1520 // nodes and Machine PHI nodes, but the incoming operands have not been
1521 // emitted yet.
1522 for (BasicBlock::const_iterator I = SuccBB->begin();
1523 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanfb95f892010-05-07 01:10:20 +00001524
Dan Gohmanf81eca02010-04-22 20:46:50 +00001525 // Ignore dead phi's.
1526 if (PN->use_empty()) continue;
1527
1528 // Only handle legal types. Two interesting things to note here. First,
1529 // by bailing out early, we may leave behind some dead instructions,
1530 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001531 // own moves. Second, this check is necessary because FastISel doesn't
Dan Gohman89496d02010-07-02 00:10:16 +00001532 // use CreateRegs to create registers, so it always creates
Dan Gohmanf81eca02010-04-22 20:46:50 +00001533 // exactly one register for each non-void instruction.
1534 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1535 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
Chad Rosier2f2d1d72012-02-04 00:39:19 +00001536 // Handle integer promotions, though, because they're common and easy.
1537 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
Dan Gohmanf81eca02010-04-22 20:46:50 +00001538 VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1539 else {
Dan Gohmana4160c32010-07-07 16:29:44 +00001540 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001541 return false;
1542 }
1543 }
1544
1545 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1546
Dan Gohmanfb95f892010-05-07 01:10:20 +00001547 // Set the DebugLoc for the copy. Prefer the location of the operand
1548 // if there is one; use the location of the PHI otherwise.
Stephen Hines36b56882014-04-23 16:57:46 -07001549 DbgLoc = PN->getDebugLoc();
Dan Gohmanfb95f892010-05-07 01:10:20 +00001550 if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
Stephen Hines36b56882014-04-23 16:57:46 -07001551 DbgLoc = Inst->getDebugLoc();
Dan Gohmanfb95f892010-05-07 01:10:20 +00001552
Dan Gohmanf81eca02010-04-22 20:46:50 +00001553 unsigned Reg = getRegForValue(PHIOp);
1554 if (Reg == 0) {
Dan Gohmana4160c32010-07-07 16:29:44 +00001555 FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohmanf81eca02010-04-22 20:46:50 +00001556 return false;
1557 }
Dan Gohmana4160c32010-07-07 16:29:44 +00001558 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Stephen Hines36b56882014-04-23 16:57:46 -07001559 DbgLoc = DebugLoc();
Dan Gohmanf81eca02010-04-22 20:46:50 +00001560 }
1561 }
1562
1563 return true;
1564}
Eli Bendersky75299e32013-04-19 22:29:18 +00001565
1566bool FastISel::tryToFoldLoad(const LoadInst *LI, const Instruction *FoldInst) {
Eli Bendersky462123f2013-04-19 23:26:18 +00001567 assert(LI->hasOneUse() &&
1568 "tryToFoldLoad expected a LoadInst with a single use");
Eli Bendersky75299e32013-04-19 22:29:18 +00001569 // We know that the load has a single use, but don't know what it is. If it
1570 // isn't one of the folded instructions, then we can't succeed here. Handle
1571 // this by scanning the single-use users of the load until we get to FoldInst.
1572 unsigned MaxUsers = 6; // Don't scan down huge single-use chains of instrs.
1573
Stephen Hines36b56882014-04-23 16:57:46 -07001574 const Instruction *TheUser = LI->user_back();
Eli Bendersky75299e32013-04-19 22:29:18 +00001575 while (TheUser != FoldInst && // Scan up until we find FoldInst.
1576 // Stay in the right block.
1577 TheUser->getParent() == FoldInst->getParent() &&
1578 --MaxUsers) { // Don't scan too far.
1579 // If there are multiple or no uses of this instruction, then bail out.
1580 if (!TheUser->hasOneUse())
1581 return false;
1582
Stephen Hines36b56882014-04-23 16:57:46 -07001583 TheUser = TheUser->user_back();
Eli Bendersky75299e32013-04-19 22:29:18 +00001584 }
1585
1586 // If we didn't find the fold instruction, then we failed to collapse the
1587 // sequence.
1588 if (TheUser != FoldInst)
1589 return false;
1590
1591 // Don't try to fold volatile loads. Target has to deal with alignment
1592 // constraints.
Eli Bendersky462123f2013-04-19 23:26:18 +00001593 if (LI->isVolatile())
1594 return false;
Eli Bendersky75299e32013-04-19 22:29:18 +00001595
1596 // Figure out which vreg this is going into. If there is no assigned vreg yet
1597 // then there actually was no reference to it. Perhaps the load is referenced
1598 // by a dead instruction.
1599 unsigned LoadReg = getRegForValue(LI);
1600 if (LoadReg == 0)
1601 return false;
1602
Eli Bendersky462123f2013-04-19 23:26:18 +00001603 // We can't fold if this vreg has no uses or more than one use. Multiple uses
1604 // may mean that the instruction got lowered to multiple MIs, or the use of
1605 // the loaded value ended up being multiple operands of the result.
1606 if (!MRI.hasOneUse(LoadReg))
1607 return false;
1608
Eli Bendersky75299e32013-04-19 22:29:18 +00001609 MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LoadReg);
Stephen Hines36b56882014-04-23 16:57:46 -07001610 MachineInstr *User = RI->getParent();
Eli Bendersky75299e32013-04-19 22:29:18 +00001611
1612 // Set the insertion point properly. Folding the load can cause generation of
Eli Bendersky462123f2013-04-19 23:26:18 +00001613 // other random instructions (like sign extends) for addressing modes; make
Eli Bendersky75299e32013-04-19 22:29:18 +00001614 // sure they get inserted in a logical place before the new instruction.
1615 FuncInfo.InsertPt = User;
1616 FuncInfo.MBB = User->getParent();
1617
1618 // Ask the target to try folding the load.
1619 return tryToFoldLoadIntoMI(User, RI.getOperandNo(), LI);
1620}
1621
Bob Wilsoncc705232013-11-15 19:09:27 +00001622bool FastISel::canFoldAddIntoGEP(const User *GEP, const Value *Add) {
1623 // Must be an add.
1624 if (!isa<AddOperator>(Add))
1625 return false;
1626 // Type size needs to match.
Stephen Hines36b56882014-04-23 16:57:46 -07001627 if (DL.getTypeSizeInBits(GEP->getType()) !=
1628 DL.getTypeSizeInBits(Add->getType()))
Bob Wilsoncc705232013-11-15 19:09:27 +00001629 return false;
1630 // Must be in the same basic block.
1631 if (isa<Instruction>(Add) &&
1632 FuncInfo.MBBMap[cast<Instruction>(Add)->getParent()] != FuncInfo.MBB)
1633 return false;
1634 // Must have a constant operand.
1635 return isa<ConstantInt>(cast<AddOperator>(Add)->getOperand(1));
1636}
Eli Bendersky75299e32013-04-19 22:29:18 +00001637